Debian Codegraph @

pod/pTk/CrtItemType.pod

raw
1 # Copyright (c) 1994-1995 Sun Microsystems, Inc.
2 # See the file "license.terms" for information on usage and redistribution
3 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
4 #
5 #
6
7 =head1 NAME
8
9 Tk_CreateItemType, Tk_GetItemTypes - define new kind of canvas item
10
11 =for category C Programming
12
13 =head1 SYNOPSIS
14
15 B<#include E<lt>tk.hE<gt>>
16
17 B<Tk_CreateItemType>(I<typePtr>)
18
19 Tk_ItemType *
20 B<Tk_GetItemTypes>()
21
22 =head1 ARGUMENTS
23
24 =over 4
25
26 =item Tk_ItemType *typePtr (in)
27
28 Structure that defines the new type of canvas item.
29
30 =back
31
32 =head1 INTRODUCTION
33
34 B<Tk_CreateItemType> is invoked to define a new kind of canvas item
35 described by the I<typePtr> argument.
36 An item type corresponds to a particular value of the I<type>
37 argument to the B<create> method for canvases, and
38 the code that implements a canvas item type is called a I<type manager>.
39 Tk defines several built-in item types, such as B<rectangle>
40 and B<text> and B<image>, but B<Tk_CreateItemType>
41 allows additional item types to be defined.
42 Once B<Tk_CreateItemType> returns, the new item type may be used
43 in new or existing canvas widgets just like the built-in item
44 types.
45
46 B<Tk_GetItemTypes> returns a pointer to the first in the list
47 of all item types currently defined for canvases.
48 The entries in the list are linked together through their
49 I<nextPtr> fields, with the end of the list marked by a
50 NULL I<nextPtr>.
51
52 You may find it easier to understand the rest of this manual entry
53 by looking at the code for an existing canvas item type such as
54 bitmap (file tkCanvBmap.c) or text (tkCanvText.c).
55 The easiest way to create a new type manager is to copy the code
56 for an existing type and modify it for the new type.
57
58 Tk provides a number of utility procedures for the use of canvas
59 type managers, such as B<Tk_CanvasCoords> and B<Tk_CanvasPsColor>;
60 these are described in separate manual entries.
61
62 =head1 DATA STRUCTURES
63
64 A type manager consists of a collection of procedures that provide a
65 standard set of operations on items of that type.
66 The type manager deals with three kinds of data
67 structures.
68 The first data structure is a Tk_ItemType; it contains
69 information such as the name of the type and pointers to
70 the standard procedures implemented by the type manager:
71
72 typedef struct Tk_ItemType {
73 char *name;
74 int itemSize;
75 Tk_ItemCreateProc *createProc;
76 Tk_ConfigSpec *configSpecs;
77 Tk_ItemConfigureProc *configProc;
78 Tk_ItemCoordProc *coordProc;
79 Tk_ItemDeleteProc *deleteProc;
80 Tk_ItemDisplayProc *displayProc;
81 int alwaysRedraw;
82 Tk_ItemPointProc *pointProc;
83 Tk_ItemAreaProc *areaProc;
84 Tk_ItemPostscriptProc *postscriptProc;
85 Tk_ItemScaleProc *scaleProc;
86 Tk_ItemTranslateProc *translateProc;
87 Tk_ItemIndexProc *indexProc;
88 Tk_ItemCursorProc *icursorProc;
89 Tk_ItemSelectionProc *selectionProc;
90 Tk_ItemInsertProc *insertProc;
91 Tk_ItemDCharsProc *dCharsProc;
92 Tk_ItemType *nextPtr;
93 } Tk_ItemType;
94
95 The fields of a Tk_ItemType structure are described in more detail
96 later in this manual entry.
97 When B<Tk_CreateItemType> is called, its I<typePtr>
98 argument must point to a structure with all of the fields initialized
99 except I<nextPtr>, which Tk sets to link all the types together
100 into a list.
101 The structure must be in permanent memory (either statically
102 allocated or dynamically allocated but never freed); Tk retains
103 a pointer to this structure.
104
105 The second data structure manipulated by a type manager is an
106 I<item record>.
107 For each item in a canvas there exists one item record.
108 All of the items of a given type generally have item records with
109 the same structure, but different types usually have different
110 formats for their item records.
111 The first part of each item record is a header with a standard structure
112 defined by Tk via the type Tk_Item; the rest of the item
113 record is defined by the type manager.
114 A type manager must define its item records with a Tk_Item as
115 the first field.
116 For example, the item record for bitmap items is defined as follows:
117
118 typedef struct BitmapItem {
119 Tk_Item header;
120 double x, y;
121 Tk_Anchor anchor;
122 Pixmap bitmap;
123 XColor *fgColor;
124 XColor *bgColor;
125 GC gc;
126 } BitmapItem;
127
128 The I<header> substructure contains information used by Tk
129 to manage the item, such as its identifier, its tags, its type,
130 and its bounding box.
131 The fields starting with I<x> belong to the type manager:
132 Tk will never read or write them.
133 The type manager should not need to read or write any of the
134 fields in the header except for four fields
135 whose names are I<x1>, I<y1>, I<x2>, and I<y2>.
136 These fields give a bounding box for the items using integer
137 canvas coordinates: the item should not cover any pixels
138 with x-coordinate lower than I<x1> or y-coordinate
139 lower than I<y1>, nor should it cover any pixels with
140 x-coordinate greater than or equal to I<x2> or y-coordinate
141 greater than or equal to I<y2>.
142 It is up to the type manager to keep the bounding box up to
143 date as the item is moved and reconfigured.
144
145 Whenever Tk calls a procedure in a type manager it passes in a pointer
146 to an item record.
147 The argument is always passed as a pointer to a Tk_Item; the type
148 manager will typically cast this into a pointer to its own specific
149 type, such as BitmapItem.
150
151 The third data structure used by type managers has type
152 Tk_Canvas; it serves as an opaque handle for the canvas widget
153 as a whole.
154 Type managers need not know anything about the contents of this
155 structure.
156 A Tk_Canvas handle is typically passed in to the
157 procedures of a type manager, and the type manager can pass the
158 handle back to library procedures such as Tk_CanvasTkwin
159 to fetch information about the canvas.
160
161 =head1 B<name>
162
163 This section and the ones that follow describe each of the fields
164 in a Tk_ItemType structure in detail.
165 The I<name> field provides a string name for the item type.
166 Once B<Tk_CreateImageType> returns, this name may be used
167 in B<create> methods to create items of the new
168 type.
169 If there already existed an item type by this name then
170 the new item type replaces the old one.
171
172 =head1 B<itemSize>
173
174 I<typePtr-E<gt>itemSize> gives the size in bytes of item records
175 of this type, including the Tk_Item header.
176 Tk uses this size to allocate memory space for items of the type.
177 All of the item records for a given type must have the same size.
178 If variable length fields are needed for an item (such as a list
179 of points for a polygon), the type manager can allocate a separate
180 object of variable length and keep a pointer to it in the item record.
181
182 =head1 CREATEPROC
183
184 I<typePtr-E<gt>createProc> points to a procedure for
185 Tk to call whenever a new item of this type is created.
186 I<typePtr-E<gt>createProc> must match the following prototype:
187
188 typedef int Tk_ItemCreateProc(
189 Tcl_Interp *interp,
190 Tk_Canvas canvas,
191 Tk_Item *itemPtr,
192 int argc,
193 char **argv);
194
195 The I<interp> argument is the interpreter in which the canvas's
196 B<create> method was invoked, and I<canvas> is a
197 handle for the canvas widget.
198 I<itemPtr> is a pointer to a newly-allocated item of
199 size I<typePtr-E<gt>itemSize>.
200 Tk has already initialized the item's header (the first
201 B<sizeof(Tk_ItemType)> bytes).
202 The I<argc> and I<argv> arguments describe all of the
203 arguments to the B<create> command after the I<type>
204 argument.
205 For example, in the method
206
207 .c create rectangle 10 20 50 50 -fill black
208
209 I<argc> will be B<6> and I<argv>[0] will contain the
210 string B<10>.
211
212 I<createProc> should use I<argc> and I<argv> to initialize
213 the type-specific parts of the item record and set an initial value
214 for the bounding box in the item's header.
215 It should return a standard Tcl completion code and leave an
216 error message in I<interp-E<gt>result> if an error occurs.
217 If an error occurs Tk will free the item record, so I<createProc>
218 must be sure to leave the item record in a clean state if it returns an error
219 (e.g., it must free any additional memory that it allocated for
220 the item).
221
222 =head1 CONFIGSPECS
223
224 Each type manager must provide a standard table describing its
225 configuration options, in a form suitable for use with
226 B<Tk_ConfigureWidget>.
227 This table will normally be used by I<typePtr-E<gt>createProc>
228 and I<typePtr-E<gt>configProc>, but Tk also uses it directly
229 to retrieve option information in the B<itemcget> and
230 B<itemconfigure> methods.
231 I<typePtr-E<gt>configSpecs> must point to the configuration table
232 for this type.
233 Note: Tk provides a custom option type B<tk_CanvasTagsOption>
234 for implementing the B<-tags> option; see an existing type
235 manager for an example of how to use it in I<configSpecs>.
236
237 =head1 CONFIGPROC
238
239 I<typePtr-E<gt>configProc> is called by Tk whenever the
240 B<itemconfigure> method is invoked to change the
241 configuration options for a canvas item.
242 This procedure must match the following prototype:
243
244 typedef int Tk_ItemConfigureProc(
245 Tcl_Interp *interp,
246 Tk_Canvas canvas,
247 Tk_Item *itemPtr,
248 int argc,
249 char **argv,
250 int flags);
251
252 The I<interp> argument identifies the interpreter in which the
253 method was invoked, I<canvas> is a handle for the canvas
254 widget, and I<itemPtr> is a pointer to the item being configured.
255 I<argc> and I<argv> contain the configuration options. For
256 example, if the following command is invoked:
257
258 .c itemconfigure 2 -fill red -outline black
259
260 I<argc> is B<4> and I<argv> contains the strings B<-fill>
261 through B<black>.
262 I<argc> will always be an even value.
263 The I<flags> argument contains flags to pass to B<Tk_ConfigureWidget>;
264 currently this value is always TK_CONFIG_ARGV_ONLY when Tk
265 invokes I<typePtr-E<gt>configProc>, but the type manager's I<createProc>
266 procedure will usually invoke I<configProc> with different flag values.
267
268 I<typePtr-E<gt>configProc> returns a standard Tcl completion code and
269 leaves an error message in I<interp-E<gt>result> if an error occurs.
270 It must update the item's bounding box to reflect the new configuration
271 options.
272
273 =head1 COORDPROC
274
275 I<typePtr-E<gt>coordProc> is invoked by Tk to implement the B<coords>
276 method for an item.
277 It must match the following prototype:
278
279 typedef int Tk_ItemCoordProc(
280 Tcl_Interp *interp,
281 Tk_Canvas canvas,
282 Tk_Item *itemPtr,
283 int argc,
284 char **argv);
285
286 The arguments I<interp>, I<canvas>, and I<itemPtr>
287 all have the standard meanings, and I<argc> and I<argv>
288 describe the coordinate arguments.
289 For example, if the following method is invoked:
290
291 .c coords 2 30 90
292
293 I<argc> will be B<2> and B<argv> will contain the string values
294 B<30> and B<90>.
295
296 The I<coordProc> procedure should process the new coordinates,
297 update the item appropriately (e.g., it must reset the bounding
298 box in the item's header), and return a standard Tcl completion
299 code.
300 If an error occurs, I<coordProc> must leave an error message in
301 I<interp-E<gt>result>.
302
303 =head1 DELETEPROC
304
305 I<typePtr-E<gt>deleteProc> is invoked by Tk to delete an item
306 and free any resources allocated to it.
307 It must match the following prototype:
308
309 typedef void Tk_ItemDeleteProc(
310 Tk_Canvas canvas,
311 Tk_Item *itemPtr,
312 Display *display);
313
314 The I<canvas> and I<itemPtr> arguments have the usual
315 interpretations, and I<display> identifies the X display containing
316 the canvas.
317 I<deleteProc> must free up any resources allocated for the item,
318 so that Tk can free the item record.
319 I<deleteProc> should not actually free the item record; this will
320 be done by Tk when I<deleteProc> returns.
321
322 =head1 DISPLAYPROC AND ALWAYSREDRAW
323
324 I<typePtr-E<gt>displayProc> is invoked by Tk to redraw an item
325 on the screen.
326 It must match the following prototype:
327
328 typedef void Tk_ItemDisplayProc(
329 Tk_Canvas canvas,
330 Tk_Item *itemPtr,
331 Display *display,
332 Drawable dst,
333 int x,
334 int y,
335 int width,
336 int height);
337
338 The I<canvas> and I<itemPtr> arguments have the usual meaning.
339 I<display> identifies the display containing the canvas, and
340 I<dst> specifies a drawable in which the item should be rendered;
341 typically this is an off-screen pixmap, which Tk will copy into
342 the canvas's window once all relevant items have been drawn.
343 I<x>, I<y>, I<width>, and I<height> specify a rectangular
344 region in canvas coordinates, which is the area to be redrawn;
345 only information that overlaps this area needs to be redrawn.
346 Tk will not call I<displayProc> unless the item's bounding box
347 overlaps the redraw area, but the type manager may wish to use
348 the redraw area to optimize the redisplay of the item.
349
350 Because of scrolling and the use of off-screen pixmaps for
351 double-buffered redisplay, the item's coordinates in I<dst>
352 will not necessarily be the same as those in the canvas.
353 I<displayProc> should call B<Tk_CanvasDrawableCoords>
354 to transform coordinates from those of the canvas to those
355 of I<dst>.
356
357 Normally an item's I<displayProc> is only invoked if the item
358 overlaps the area being displayed.
359 However, if I<typePtr-E<gt>alwaysRedraw> has a non-zero value, then
360 I<displayProc> is invoked during every redisplay operation,
361 even if the item doesn't overlap the area of redisplay.
362 I<alwaysRedraw> should normally be set to 0; it is only
363 set to 1 in special cases such as window items that need to be
364 unmapped when they are off-screen.
365
366 =head1 POINTPROC
367
368 I<typePtr-E<gt>pointProc> is invoked by Tk to find out how close
369 a given point is to a canvas item.
370 Tk uses this procedure for purposes such as locating the item
371 under the mouse or finding the closest item to a given point.
372 The procedure must match the following prototype:
373
374 typedef double Tk_ItemPointProc(
375 Tk_Canvas canvas,
376 Tk_Item *itemPtr,
377 double *pointPtr);
378
379 I<canvas> and I<itemPtr> have the usual meaning.
380 I<pointPtr> points to an array of two numbers giving
381 the x and y coordinates of a point.
382 I<pointProc> must return a real value giving the distance
383 from the point to the item, or 0 if the point lies inside
384 the item.
385
386 =head1 AREAPROC
387
388 I<typePtr-E<gt>areaProc> is invoked by Tk to find out the relationship
389 between an item and a rectangular area.
390 It must match the following prototype:
391
392 typedef int Tk_ItemAreaProc(
393 Tk_Canvas canvas,
394 Tk_Item *itemPtr,
395 double *rectPtr);
396
397 I<canvas> and I<itemPtr> have the usual meaning.
398 I<rectPtr> points to an array of four real numbers;
399 the first two give the x and y coordinates of the upper left
400 corner of a rectangle, and the second two give the x and y
401 coordinates of the lower right corner.
402 I<areaProc> must return -1 if the item lies entirely outside
403 the given area, 0 if it lies partially inside and partially
404 outside the area, and 1 if it lies entirely inside the area.
405
406 =head1 POSTSCRIPTPROC
407
408 I<typePtr-E<gt>postscriptProc> is invoked by Tk to generate
409 Postcript for an item during the B<postscript> method.
410 If the type manager is not capable of generating Postscript then
411 I<typePtr-E<gt>postscriptProc> should be NULL.
412 The procedure must match the following prototype:
413
414 typedef int Tk_ItemPostscriptProc(
415 Tcl_Interp *interp,
416 Tk_Canvas canvas,
417 Tk_Item *itemPtr,
418 int prepass);
419
420 The I<interp>, I<canvas>, and I<itemPtr> arguments all have
421 standard meanings; I<prepass> will be described below.
422 If I<postscriptProc> completes successfully, it should append
423 Postscript for the item to the information in I<interp-E<gt>result>
424 (e.g. by calling B<Tcl_AppendResult>, not B<Tcl_SetResult>)
425 and return TCL_OK.
426 If an error occurs, I<postscriptProc> should clear the result
427 and replace its contents with an error message; then it should
428 return TCL_ERROR.
429
430 Tk provides a collection of utility procedures to simplify
431 I<postscriptProc>.
432 For example, B<Tk_CanvasPsColor> will generate Postscript to set
433 the current color to a given Tk color and B<Tk_CanvasPsFont> will
434 set up font information.
435 When generating Postscript, the type manager is free to change the
436 graphics state of the Postscript interpreter, since Tk places
437 B<gsave> and B<grestore> commands around the Postscript for
438 the item.
439 The type manager can use canvas x coordinates directly in its Postscript,
440 but it must call B<Tk_CanvasPsY> to convert y coordinates from
441 the space of the canvas (where the origin is at the
442 upper left) to the space of Postscript (where the origin is at the
443 lower left).
444
445 In order to generate Postscript that complies with the Adobe Document
446 Structuring Conventions, Tk actually generates Postscript in two passes.
447 It calls each item's I<postscriptProc> in each pass.
448 The only purpose of the first pass is to collect font information
449 (which is done by B<Tk_CanvPsFont>); the actual Postscript is
450 discarded.
451 Tk sets the I<prepass> argument to I<postscriptProc> to 1
452 during the first pass; the type manager can use I<prepass> to skip
453 all Postscript generation except for calls to B<Tk_CanvasPsFont>.
454 During the second pass I<prepass> will be 0, so the type manager
455 must generate complete Postscript.
456
457 =head1 SCALEPROC
458
459 I<typePtr-E<gt>scaleProc> is invoked by Tk to rescale a canvas item
460 during the B<scale> method.
461 The procedure must match the following prototype:
462
463 typedef void Tk_ItemScaleProc(
464 Tk_Canvas canvas,
465 Tk_Item *itemPtr,
466 double originX,
467 double originY,
468 double scaleX,
469 double scaleY);
470
471 The I<canvas> and I<itemPtr> arguments have the usual meaning.
472 I<originX> and I<originY> specify an origin relative to which
473 the item is to be scaled, and I<scaleX> and I<scaleY> give the
474 x and y scale factors.
475 The item should adjust its coordinates so that a point in the item
476 that used to have coordinates I<x> and I<y> will have new
477 coordinates I<x'> and I<y'>, where
478
479 x' = originX + scaleX*(x-originX)
480 y' = originY + scaleY*(y-originY)
481
482 I<scaleProc> must also update the bounding box in the item's
483 header.
484
485 =head1 TRANSLATEPROC
486
487 I<typePtr-E<gt>translateProc> is invoked by Tk to translate a canvas item
488 during the B<move> method.
489 The procedure must match the following prototype:
490
491 typedef void Tk_ItemTranslateProc(
492 Tk_Canvas canvas,
493 Tk_Item *itemPtr,
494 double deltaX,
495 double deltaY);
496
497 The I<canvas> and I<itemPtr> arguments have the usual meaning,
498 and I<deltaX> and I<deltaY> give the amounts that should be
499 added to each x and y coordinate within the item.
500 The type manager should adjust the item's coordinates and
501 update the bounding box in the item's header.
502
503 =head1 INDEXPROC
504
505 I<typePtr-E<gt>indexProc> is invoked by Tk to translate a string
506 index specification into a numerical index, for example during the
507 B<index> method.
508 It is only relevant for item types that support indexable text;
509 I<typePtr-E<gt>indexProc> may be specified as NULL for non-textual
510 item types.
511 The procedure must match the following prototype:
512
513 typedef int Tk_ItemIndexProc(
514 Tcl_Interp *interp,
515 Tk_Canvas canvas,
516 Tk_Item *itemPtr,
517 char indexString,
518 int *indexPtr);
519
520 The I<interp>, I<canvas>, and I<itemPtr> arguments all
521 have the usual meaning.
522 I<indexString> contains a textual description of an index,
523 and I<indexPtr> points to an integer value that should be
524 filled in with a numerical index.
525 It is up to the type manager to decide what forms of index
526 are supported (e.g., numbers, B<insert>, B<sel.first>,
527 B<end>, etc.).
528 I<indexProc> should return a Tcl completion code and set
529 I<interp-E<gt>result> in the event of an error.
530
531 =head1 ICURSORPROC
532
533 I<typePtr-E<gt>icursorProc> is invoked by Tk during
534 the B<icursor> method to set the position of the
535 insertion cursor in a textual item.
536 It is only relevant for item types that support an insertion cursor;
537 I<typePtr-E<gt>icursorProc> may be specified as NULL for item types
538 that don't support an insertion cursor.
539 The procedure must match the following prototype:
540
541 typedef void Tk_ItemIndexProc(
542 Tk_Canvas canvas,
543 Tk_Item *itemPtr,
544 int index);
545
546 I<canvas> and I<itemPtr> have the usual meanings, and
547 I<index> is an index into the item's text, as returned by a
548 previous call to I<typePtr-E<gt>insertProc>.
549 The type manager should position the insertion cursor in the
550 item just before the character given by I<index>.
551 Whether or not to actually display the insertion cursor is
552 determined by other information provided by B<Tk_CanvasGetTextInfo>.
553
554 =head1 SELECTIONPROC
555
556 I<typePtr-E<gt>selectionProc> is invoked by Tk during selection
557 retrievals; it must return part or all of the selected text in
558 the item (if any).
559 It is only relevant for item types that support text;
560 I<typePtr-E<gt>selectionProc> may be specified as NULL for non-textual
561 item types.
562 The procedure must match the following prototype:
563
564 typedef int Tk_ItemSelectionProc(
565 Tk_Canvas canvas,
566 Tk_Item *itemPtr,
567 int offset,
568 char *buffer,
569 int maxBytes);
570
571 I<canvas> and I<itemPtr> have the usual meanings.
572 I<offset> is an offset in bytes into the selection where 0 refers
573 to the first byte of the selection; it identifies
574 the first character that is to be returned in this call.
575 I<buffer> points to an area of memory in which to store the
576 requested bytes, and I<maxBytes> specifies the maximum number
577 of bytes to return.
578 I<selectionProc> should extract up to I<maxBytes> characters
579 from the selection and copy them to I<maxBytes>; it should
580 return a count of the number of bytes actually copied, which may
581 be less than I<maxBytes> if there aren't I<offset+maxBytes> bytes
582 in the selection.
583
584 =head1 INSERTPROC
585
586 I<typePtr-E<gt>insertProc> is invoked by Tk during
587 the B<insert> method to insert new text into a
588 canvas item.
589 It is only relevant for item types that support text;
590 I<typePtr-E<gt>insertProc> may be specified as NULL for non-textual
591 item types.
592 The procedure must match the following prototype:
593
594 typedef void Tk_ItemInsertProc(
595 Tk_Canvas canvas,
596 Tk_Item *itemPtr,
597 int index,
598 char *string);
599
600 I<canvas> and I<itemPtr> have the usual meanings.
601 I<index> is an index into the item's text, as returned by a
602 previous call to I<typePtr-E<gt>insertProc>, and I<string>
603 contains new text to insert just before the character given
604 by I<index>.
605 The type manager should insert the text and recompute the bounding
606 box in the item's header.
607
608 =head1 DCHARSPROC
609
610 I<typePtr-E<gt>dCharsProc> is invoked by Tk during the B<dchars>
611 method to delete a range of text from a canvas item.
612 It is only relevant for item types that support text;
613 I<typePtr-E<gt>dCharsProc> may be specified as NULL for non-textual
614 item types.
615 The procedure must match the following prototype:
616
617 typedef void Tk_ItemDCharsProc(
618 Tk_Canvas canvas,
619 Tk_Item *itemPtr,
620 int first,
621 int last);
622
623 I<canvas> and I<itemPtr> have the usual meanings.
624 I<first> and I<last> give the indices of the first and last bytes
625 to be deleted, as returned by previous calls to I<typePtr-E<gt>indexProc>.
626 The type manager should delete the specified characters and update
627 the bounding box in the item's header.
628
629 =head1 SEE ALSO
630
631 L<Tk::CanvPsY>, L<Tk::CanvTxtInfo>, L<Tk::CanvTkwin>
632
633 =head1 KEYWORDS
634
635 canvas, focus, item type, selection, type manager