# Copyright (c) 1992-1994 The Regents of the University of California. # Copyright (c) 1994-1996 Sun Microsystems, Inc. # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # =head1 NAME Tk::Canvas - Create and manipulate Canvas widgets =for category Tk Widget Classes =head1 SYNOPSIS I<$canvas> = I<$parent>-EB(?I?); =head1 STANDARD OPTIONS B<-background> B<-highlightthickness> B<-insertwidth> B<-state> B<-borderwidth> B<-insertbackground> B<-relief> B<-tile> B<-cursor> B<-insertborderwidth> B<-selectbackground> B<-takefocus> B<-highlightbackground> B<-insertofftime> B<-selectborderwidth> B<-xscrollcommand> B<-highlightcolor> B<-insertontime> B<-selectforeground> B<-yscrollcommand> =head1 WIDGET-SPECIFIC OPTIONS =over 4 =item Name: B =item Class: B =item Switch: B<-closeenough> Specifies a floating-point value indicating how close the mouse cursor must be to an item before it is considered to be ``inside'' the item. Defaults to 1.0. =item Name: B =item Class: B =item Switch: B<-confine> Specifies a boolean value that indicates whether or not it should be allowable to set the canvas's view outside the region defined by the B argument. Defaults to true, which means that the view will be constrained within the scroll region. =item Name: B =item Class: B =item Switch: B<-height> Specifies a desired window height that the canvas widget should request from its geometry manager. The value may be specified in any of the forms described in the L<"COORDINATES"> section below. =item Name: B =item Class: B =item Switch: B<-scrollregion> Specifies a list with four coordinates describing the left, top, right, and bottom coordinates of a rectangular region. This region is used for scrolling purposes and is considered to be the boundary of the information in the canvas. Each of the coordinates may be specified in any of the forms given in the L<"COORDINATES"> section below. =item Name: B =item Class: B =item Switch: B<-state> Modifies the default state of the canvas where I may be set to one of: normal, disabled, or hidden. Individual canvas objects all have their own state option, which overrides the default state. Many options can take separate specifications such that the appearance of the item can be different in different situations. The options that start with "active" control the appearence when the mouse pointer is over it, while the option starting with "disabled" controls the appearence when the state is disabled. =item Name: B =item Class: B =item Switch: B<-width> Specifies a desired window width that the canvas widget should request from its geometry manager. The value may be specified in any of the forms described in the L<"COORDINATES"> section below. =item Name: B =item Class: B =item Switch: B<-xscrollincrement> Specifies an increment for horizontal scrolling, in any of the usual forms permitted for screen distances. If the value of this option is greater than zero, the horizontal view in the window will be constrained so that the canvas x coordinate at the left edge of the window is always an even multiple of B; furthermore, the units for scrolling (e.g., the change in view when the left and right arrows of a scrollbar are selected) will also be B. If the value of this option is less than or equal to zero, then horizontal scrolling is unconstrained. =item Name: B =item Class: B =item Switch: B<-yscrollincrement> Specifies an increment for vertical scrolling, in any of the usual forms permitted for screen distances. If the value of this option is greater than zero, the vertical view in the window will be constrained so that the canvas y coordinate at the top edge of the window is always an even multiple of B; furthermore, the units for scrolling (e.g., the change in view when the top and bottom arrows of a scrollbar are selected) will also be B. If the value of this option is less than or equal to zero, then vertical scrolling is unconstrained. =back =head1 DESCRIPTION The B method creates a new window (given by the $canvas argument) and makes it into a canvas widget. Additional options, described above, may be specified on the command line or in the option database to configure aspects of the canvas such as its colors and 3-D relief. The B command returns its $canvas argument. At the time this command is invoked, there must not exist a window named $canvas, but $canvas's parent must exist. Canvas widgets implement structured graphics. A canvas displays any number of I, which may be things like rectangles, circles, lines, and text. Items may be manipulated (e.g. moved or re-colored) and L may be associated with items in much the same way that the L method allows callbacks to be bound to widgets. For example, a particular callback may be associated with the BButton-1E> event so that the callback is invoked whenever button 1 is pressed with the mouse cursor over an item. This means that items in a canvas can have behaviors defined by the Callbacks bound to them. =head1 DISPLAY LIST The items in a canvas are ordered for purposes of display, with the first item in the display list being displayed first, followed by the next item in the list, and so on. Items later in the display list obscure those that are earlier in the display list and are sometimes referred to as being I<``on top''> of earlier items. When a new item is created it is placed at the end of the display list, on top of everything else. Widget methods may be used to re-arrange the order of the display list. Window items are an exception to the above rules. The underlying window systems require them always to be drawn on top of other items. In addition, the stacking order of window items is not affected by any of the canvas methods; you must use the L and L Tk widget methods instead. =head1 ITEM IDS AND TAGS Items in a canvas widget may be named in either of two ways: by id or by tag. Each item has a unique identifying number which is assigned to that item when it is created. The id of an item never changes and id numbers are never re-used within the lifetime of a canvas widget. Each item may also have any number of I associated with it. A tag is just a string of characters, and it may take any form except that of an integer. For example, ``x123'' is OK but ``123'' isn't. The same tag may be associated with many different items. This is commonly done to group items in various interesting ways; for example, all selected items might be given the tag ``selected''. The tag B is implicitly associated with every item in the canvas; it may be used to invoke operations on all the items in the canvas. The tag B is managed automatically by Tk; it applies to the I, which is the topmost item whose drawn area covers the position of the mouse cursor. If the mouse is not in the canvas widget or is not over an item, then no item has the B tag. When specifying items in canvas methods, if the specifier is an integer then it is assumed to refer to the single item with that id. If the specifier is not an integer, then it is assumed to refer to all of the items in the canvas that have a tag matching the specifier. The symbol I is used below to indicate that an argument specifies either an id that selects a single item or a tag that selects zero or more items. I may contain a logical expressions of tags by using operators: 'EE', '||', '^' '!', and parenthezised subexpressions. For example: $c-Efind('withtag', '(aEE!b)|(!aEEb)'); or equivalently: $c-Efind('withtag', 'a^b'); will find only those items with either "a" or "b" tags, but not both. Some methods only operate on a single item at a time; if I is specified in a way that names multiple items, then the normal behavior is for the methods is to use the first (lowest) of these items in the display list that is suitable for the method. Exceptions are noted in the method descriptions below. =head1 COORDINATES All coordinates related to canvases are stored as floating-point numbers. Coordinates and distances are specified in screen units, which are floating-point numbers optionally followed by one of several letters. If no letter is supplied then the distance is in pixels. If the letter is B then the distance is in millimeters on the screen; if it is B then the distance is in centimeters; B means inches, and B

means printers points (1/72 inch). Larger y-coordinates refer to points lower on the screen; larger x-coordinates refer to points farther to the right. =head1 TRANSFORMATIONS Normally the origin of the canvas coordinate system is at the upper-left corner of the window containing the canvas. It is possible to adjust the origin of the canvas coordinate system relative to the origin of the window using the B and B methods; this is typically used for scrolling. Canvases do not support scaling or rotation of the canvas coordinate system relative to the window coordinate system. Individual items may be moved or scaled using methods described below, but they may not be rotated. =head1 INDICES Text items support the notion of an I for identifying particular positions within the item. Indices are used for methods such as inserting text, deleting a range of characters, and setting the insertion cursor position. An index may be specified in any of a number of ways, and different types of items may support different forms for specifying indices. In a similar fashion, line and polygon items support I for identifying, inserting and deleting subsets of their coordinates. Indices are used for commands such as inserting or deleting a range of characters or coordinates, and setting the insertion cursor position. An index may be specified in any of a number of ways, and different types of items may support different forms for specifying indices. Text items support the following forms for an index; if you define new types of text-like items, it would be advisable to support as many of these forms as practical. Note that it is possible to refer to the character just after the last one in the text item; this is necessary for such tasks as inserting new text at the end of the item. Lines and Polygons don't support the insertion cursor and the selection. Their indices are supposed to be even always, because coordinates always appear in pairs. =over 4 =item I A decimal number giving the position of the desired character within the text item. 0 refers to the first character, 1 to the next character, and so on. If indexes are odd for lines and polygons, they will be automatically decremented by one. A number less than 0 is treated as if it were zero, and a number greater than the length of the text item is treated as if it were equal to the length of the text item. For polygons, numbers less than 0 or greater then the length of the coordinate list will be adjusted by adding or substracting the length until the result is between zero and the length, inclusive. =item B Refers to the character or coordinate just after the last one in the item (same as the number of characters or coordinates in the item). =item B Refers to the character just before which the insertion cursor is drawn in this item. Not valid for lines and polygons. =item B Refers to the first selected character in the item. If the selection isn't in this item then this form is illegal. =item B Refers to the last selected character in the item. If the selection isn't in this item then this form is illegal. =item B<[>IB<]> Refers to the character or coordinate at the point given by I and I, where I and I are specified in the coordinate system of the canvas. If I and I lie outside the coordinates covered by the text item, then they refer to the first or last character in the line that is closest to the given point. The Tcl string form "@x,y" is also allowed. =back =head1 DASH PATTERNS Many items support the notion of an dash pattern for outlines. The first possible syntax is a list of integers. Each element represents the number of pixels of a line segment. Only the odd segments are drawn using the "outline" color. The other segments are drawn transparant. The second possible syntax is a character list containing only 5 possible characters B<[.,-_ ]>. The space can be used to enlarge the space between other line elements, and can not occur as the first position in the string. Some examples: -dash . = -dash [2,4] -dash - = -dash [6,4] -dash -. = -dash [6,4,2,4] -dash -.. = -dash [6,4,2,4,2,4] -dash '. ' = -dash [2,8] -dash ',' = -dash [4,4] The main difference of this syntax with the previous is that it it shape-conserving. This means that all values in the dash list will be multiplied by the line width before display. This assures that "." will always be displayed as a dot and "-" always as a dash regardless of the line width. On systems where only a limited set of dash patterns, the dash pattern will be displayed as the most close dash pattern that is available. For example, on Windows only the first 4 of the above examples are available. The last 2 examples will be displayed identically as the first one. =head1 WIDGET METHODS The B method creates a widget object. This object supports the B and B methods described in L which can be used to enquire and modify the options described above. The widget also inherits all the methods provided by the generic L class. The following additional methods are available for canvas widgets: =over 4 =item I<$canvas>-EB(I?I?) For each item that meets the constraints specified by I and the Is, add I to the list of tags associated with the item if it isn't already present on that list. It is possible that no items will satisfy the constraints given by I and Is, in which case the method has no effect. This command returns an empty string as result. I and I's may take any of the following forms: =over 8 =item BI Selects the item just after (above) the one given by I in the display list. If I denotes more than one item, then the last (topmost) of these items in the display list is used. =item B Selects all the items in the canvas. =item BI Selects the item just before (below) the one given by I in the display list. If I denotes more than one item, then the first (lowest) of these items in the display list is used. =item BI?I? ?I? Selects the item closest to the point given by I and I. If more than one item is at the same closest distance (e.g. two items overlap the point), then the top-most of these items (the last one in the display list) is used. If I is specified, then it must be a non-negative value. Any item closer than I to the point is considered to overlap it. The I argument may be used to step circularly through all the closest items. If I is specified, it names an item using a tag or id (if by tag, it selects the first item in the display list with the given tag). Instead of selecting the topmost closest item, this form will select the topmost closest item that is below I in the display list; if no such item exists, then the selection behaves as if the I argument had not been specified. =item B I I I I Selects all the items completely enclosed within the rectangular region given by I, I, I, and I. I must be no greater then I and I must be no greater than I. =item B I I I I Selects all the items that overlap or are enclosed within the rectangular region given by I, I, I, and I. I must be no greater then I and I must be no greater than I. =item BI Selects all the items given by I. =back =item I<$canvas>-EB(I?I?) Returns a list with four elements giving an approximate bounding box for all the items named by the I arguments. The list has the form ``I'' such that the drawn areas of all the named elements are within the region bounded by I on the left, I on the right, I on the top, and I on the bottom. The return value may overestimate the actual bounding box by a few pixels. If no items match any of the I arguments or if the matching items have empty bounding boxes (i.e. they have nothing to display) then an empty string is returned. =item I<$canvas>-EB(I?, I? ?,I?) This method associates I with all the items given by I such that whenever the event sequence given by I occurs for one of the items the callback will be invoked. This method is similar to the B method except that it operates on items in a canvas rather than entire widgets. See L for complete details on the syntax of I and the substitutions performed on I before invoking it. If all arguments are specified then a new binding is created, replacing any existing binding for the same I and I (if the first character of I is ``+'' then I augments an existing binding rather than replacing it). In this case the return value is an empty string. If I is omitted then the method returns the I associated with I and I (an error occurs if there is no such binding). If both I and I are omitted then the method returns a list of all the sequences for which bindings have been defined for I. =over 8 The only events for which bindings may be specified are those related to the mouse and keyboard (such as B, B, B, B, and B) or virtual events. The handling of events in canvases uses the current item defined in L<"ITEM IDS AND TAGS"> above. B and B events trigger for an item when it becomes the current item or ceases to be the current item; note that these events are different than B and B events for windows. Mouse-related events are directed to the current item, if any. Keyboard-related events are directed to the focus item, if any (see the L method below for more on this). If a virtual event is used in a binding, that binding can trigger only if the virtual event is defined by an underlying mouse-related or keyboard-related event. It is possible for multiple bindings to match a particular event. This could occur, for example, if one binding is associated with the item's id and another is associated with one of the item's tags. When this occurs, all of the matching bindings are invoked. A binding associated with the B tag is invoked first, followed by one binding for each of the item's tags (in order), followed by a binding associated with the item's id. If there are multiple matching bindings for a single tag, then only the most specific binding is invoked. A B in a callback terminates that subroutine, and a B method terminates that subroutine and skips any remaining callbacks for the event, just as for the B method. If bindings have been created for a canvas window using the B method, then they are invoked in addition to bindings created for the canvas's items using the B method. The bindings for items will be invoked before any of the bindings for the window as a whole. =back =item I<$canvas>-EB(I?, I?) Given a window x-coordinate in the canvas I, this method returns the canvas x-coordinate that is displayed at that location. If I is specified, then the canvas coordinate is rounded to the nearest multiple of I units. =item I<$canvas>-EB(I?I?) Given a window y-coordinate in the canvas I this method returns the canvas y-coordinate that is displayed at that location. If I is specified, then the canvas coordinate is rounded to the nearest multiple of I units. =item I<$canvas>-EB(I?I?) Query or modify the coordinates that define an item. If no coordinates are specified, this method returns a list whose elements are the coordinates of the item named by I. If coordinates are specified, then they replace the current coordinates for the named item. If I refers to multiple items, then the first one in the display list is used. =item I<$canvas>-EB(I?I?, ?I?) Create a new item in I<$canvas> of type I. The exact format of the arguments after B depends on B, but usually they consist of the coordinates for one or more points, followed by specifications for zero or more item options. See the subsections on individual item types below for more on the syntax of this method. This method returns the id for the new item. =item I<$canvas>-EB(I?I?) For each item given by I, delete the characters, or coordinates, in the range given by I and I, inclusive. If some of the items given by I don't support Text items interpret I and I as indices to a character, line and polygon items interpret them indices to a coordinate (an x,y pair). within the item(s) as described in L<"INDICES"> above. If I is omitted, it defaults to I. This method returns an empty string. =item I<$canvas>-EB(?I?) Delete each of the items given by each I, and return an empty string. =item I<$canvas>-EB(I?I?) For each of the items given by I, delete the tag given by I from the list of those associated with the item. If an item doesn't have the tag I then the item is unaffected by the method. If I is omitted then it defaults to I. This method returns an empty string. =item I<$canvas>-EB(I?I?) This method returns a list consisting of all the items that meet the constraints specified by I and I's. I and I have any of the forms accepted by the B method. The items are returned in stacking order, with the lowest item first. =item focus =item I<$canvas>-EB(?I?) Set the keyboard focus for the canvas widget to the item given by I. If I refers to several items, then the focus is set to the first such item in the display list that supports the insertion cursor. If I doesn't refer to any items, or if none of them support the insertion cursor, then the focus isn't changed. If I is an empty string, then the focus item is reset so that no item has the focus. If I is not specified then the method returns the id for the item that currently has the focus, or an empty string if no item has the focus. =over 8 Once the focus has been set to an item, the item will display the insertion cursor and all keyboard events will be directed to that item. The focus item within a canvas and the focus window on the screen (set with the B method) are totally independent: a given item doesn't actually have the input focus unless (a) its canvas is the focus window and (b) the item is the focus item within the canvas. In most cases it is advisable to follow the B widget method with the B method to set the focus window to the canvas (if it wasn't there already). =back =item I<$canvas>-EB(I) Return a list whose elements are the tags associated with the item given by I. If I refers to more than one item, then the tags are returned from the first such item in the display list. If I doesn't refer to any items, or if the item contains no tags, then an empty string is returned. =item I<$canvas>-EB(I) Set the position of the insertion cursor for the item(s) given by I to just before the character whose position is given by I. If some or all of the items given by I don't support an insertion cursor then this method has no effect on them. See L<"INDICES"> above for a description of the legal forms for I. Note: the insertion cursor is only displayed in an item if that item currently has the keyboard focus (see the widget method B, below), but the cursor position may be set even when the item doesn't have the focus. This method returns an empty string. =item I<$canvas>-EB(I) This method returns a decimal string giving the numerical index within I corresponding to I. I gives a textual description of the desired position as described in L<"INDICES"> above. Text items interpret I as an index to a character, line and polygon items interpret it as an index to a coordinate (an x,y pair). The return value is guaranteed to lie between 0 and the number of characters, or coordinates, within the item, inclusive. If I refers to multiple items, then the index is processed in the first of these items that supports indexing operations (in display list order). =item I<$canvas>-EB(I) For each of the items given by I, if the item supports text or coordinate, insertion then I is inserted into the item's text just before the character, or coordinate, whose index is I. Text items interpret I as an index to a character, line and polygon items interpret it as an index to a coordinate (an x,y pair). For lines and polygons the I must be a valid coordinate sequence. See L<"INDICES"> above for information about the forms allowed for I. This method returns an empty string. =item I<$canvas>-EB(II

. =item B<-pageheight> =E I Specifies that the Postscript should be scaled in both x and y so that the printed area is I high on the Postscript page. I consists of a floating-point number followed by B for centimeters, B for inches, B for millimeters, or B

or nothing for printer's points (1/72 inch). Defaults to the height of the printed area on the screen. If both B<-pageheight> and B<-pagewidth> are specified then the scale factor from B<-pagewidth> is used (non-uniform scaling is not implemented). =item B<-pagewidth> =E I Specifies that the Postscript should be scaled in both x and y so that the printed area is I wide on the Postscript page. I has the same form as for B<-pageheight>. Defaults to the width of the printed area on the screen. If both B<-pageheight> and B<-pagewidth> are specified then the scale factor from B<-pagewidth> is used (non-uniform scaling is not implemented). =item B<-pagex> =E I I gives the x-coordinate of the positioning point on the Postscript page, using any of the forms allowed for B<-pageheight>. Used in conjunction with the B<-pagey> and B<-pageanchor> options to determine where the printed area appears on the Postscript page. Defaults to the center of the page. =item B<-pagey> =E I I gives the y-coordinate of the positioning point on the Postscript page, using any of the forms allowed for B<-pageheight>. Used in conjunction with the B<-pagex> and B<-pageanchor> options to determine where the printed area appears on the Postscript page. Defaults to the center of the page. =item B<-rotate> =E I I specifies whether the printed area is to be rotated 90 degrees. In non-rotated output the x-axis of the printed area runs along the short dimension of the page (``portrait'' orientation); in rotated output the x-axis runs along the long dimension of the page (``landscape'' orientation). Defaults to non-rotated. =item B<-width> =E I Specifies the width of the area of the canvas to print. Defaults to the width of the canvas window. =item B<-x> => I Specifies the x-coordinate of the left edge of the area of the canvas that is to be printed, in canvas coordinates, not window coordinates. Defaults to the coordinate of the left edge of the window. =item B<-y> => I Specifies the y-coordinate of the top edge of the area of the canvas that is to be printed, in canvas coordinates, not window coordinates. Defaults to the coordinate of the top edge of the window. =back =item I<$canvas>-EB(I?I?) Move all of the items given by I to a new position in the display list just after the item given by I. If I refers to more than one item then all are moved but the relative order of the moved items will not be changed. I is a tag or id; if it refers to more than one item then the last (topmost) of these items in the display list is used as the destination location for the moved items. Note: this method has no effect on window items. Window items always obscure other item types, and the stacking order of window items is determined by the B and B widget commands, not the B and B methods for canvases. This method returns an empty string. =item I<$canvas>-EB(I) Rescale all of the items given by I in canvas coordinate space. I and I identify the origin for the scaling operation and I and I identify the scale factors for x- and y-coordinates, respectively (a scale factor of 1.0 implies no change to that coordinate). For each of the points defining each item, the x-coordinate is adjusted to change the distance from I by a factor of I. Similarly, each y-coordinate is adjusted to change the distance from I by a factor of I. This method returns an empty string. =item I<$canvas>-EB(I) This method is used to implement scanning on canvases. It has two forms, depending on I

then the text is centered on the point; if I is B then the text will be drawn such that the top center point of the rectangular region occupied by the text will be at the positioning point. This option defaults to B
. =item B<-fill> =E I =item B<-activefill> =E I =item B<-disabledfill> =E I Specifies the color to be used to fill the text in its normal, active, and disabled states. I may have any of the forms accepted by B. If I is undef then the text will be transparent. If this option isn't specified then it defaults to B. =item B<-font> =E I Specifies the font to use for the text item. I may be any string acceptable to B. If this option isn't specified, it defaults to a system-dependent font. =item B<-justify> =E I Specifies how to justify the text within its bounding region. I must be one of the values B, B, or B
. This option will only matter if the text is displayed as multiple lines. If the option is omitted, it defaults to B. =item B<-state> =E I Modifies the state of the text item where I may be set to one of: normal, disabled, or hidden. A text item may also be in the "active" state if the mouse is currently over it. Many options can take separate specifications in normal, active and disabled states such that the appearance of the item can be different in each state. =item B<-stipple> =E I =item B<-activestipple> =E I =item B<-disabledstipple> =E I This option specifies stipple patterns that should be used to fill the the text in its normal, active and disabled states. I specifies the stipple pattern to use, in any of the forms accepted by B. If I is an empty string (the default) then the text is drawn in a solid fashion. =item B<-tags> =E I Specifies a set of tags to apply to the item. I consists of a list of tag names, which replace any existing tags for the item. I may be an empty list. =item B<-text> =E I I specifies the characters to be displayed in the text item. Newline characters cause line breaks. The characters in the item may also be changed with the B and B methods. This option defaults to an empty string. =item B<-updatecommand> =E I Specifies a callback that is to be executed every time the text item is updated on the screen. =item B<-width> =E I Specifies a maximum line length for the text, in any of the forms described in the L<"COORDINATES"> section above. If this option is zero (the default) the text is broken into lines only at newline characters. However, if this option is non-zero then any line that would be longer than I is broken just before a space character to make the line shorter than I; the space character is treated as if it were a newline character. =back =head1 WINDOW ITEMS Items of type B cause a particular window to be displayed at a given position on the canvas. Window items are created with methods of the following form: S< >I<$canvas>->B(I?, I<-option>=>I, I<-option>=>I, ...?) The arguments I and I specify the coordinates of a point used to position the window on the display (see the B<-anchor> option below for more information on how bitmaps are displayed). After the coordinates there may be any number of I pairs, each of which sets one of the configuration options for the item. These same I