home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / doc / Xaw / CH2 < prev    next >
Encoding:
Text File  |  1991-07-30  |  34.5 KB  |  1,078 lines

  1. .bp
  2. \&
  3. .sp 1
  4. .ce 3
  5. \s+1\fBChapter 2\fP\s-1
  6.  
  7. \s+1\fBUsing Widgets\fP\s-1
  8. .sp 2
  9. .nr H1 2
  10. .nr H2 0
  11. .nr H3 0
  12. .nr H4 0
  13. .nr H5 0
  14. .LP
  15. .XS
  16. Chapter 2 \- Using Widgets
  17. .XE
  18. .IN "using widgets" "" "@DEF@"
  19. Widgets serve as the primary tools for building a user interface or
  20. application environment.  The Athena widget set consists of primitive
  21. widgets that contain no children (for example, a command button) and
  22. composite widgets which may contain one or more widget children (for
  23. example, a Box widget). 
  24. .LP
  25. The remaining chapters explain the widgets that are provided
  26. by the Athena widget set.
  27. These user-interface components serve as an interface for 
  28. application programmers who do not want to implement their own widgets.
  29. In addition, they serve as a starting point
  30. for those widget programmers who, using the \*(xI mechanisms,
  31. want to implement alternative application programming interfaces.
  32. .LP
  33. This chapter is a brief introduction to widget programming.  The
  34. examples provided use the Athena widgets, though most of the concepts
  35. will apply to all widget sets.  Although there are several programming
  36. interfaces to the \*(tk, but only one is described here.  A full
  37. description of the programming interface is provided in the document
  38. \fI\*(xT\fP.
  39. .NH 2
  40. Initializing the Toolkit
  41. .LP
  42. .XS
  43.     Initializing the Toolkit
  44. .XE
  45. You must call a toolkit initialization function before invoking any
  46. other toolkit routines.  
  47. .PN XtAppInitialize
  48. opens the X server connection, parses the command line,
  49. and creates an initial widget that will serve as the root of
  50. a tree of widgets created by this application.
  51. .IN "initialization" "" "@DEF@"
  52. .IN "toolkit initialization" "" "@DEF@"
  53. .IN "XtAppInitialize" "" "@DEF@"
  54. .IN "fallback resources" "" "@DEF@"
  55. .FD 0
  56. Widget XtAppInitialize(\fIapp_context_return\fP, \fIapplication_class\fP, \
  57. \fIoptions\fP, \fInum_options\fP, \fIargc_in_out\fP, \fIargv_in_out\fP, \fIfallback_resources\fP, \fIargs\fP, \fInum_args\fP)
  58. .br
  59.       XtAppContext *\fIapp_context_return\fP;    
  60. .br
  61.       String \fIapplication_class\fP;
  62. .br
  63.       XrmOptionDescRec \fIoptions\fP[];
  64. .br
  65.       Cardinal \fInum_options\fP;
  66. .br
  67.       int *\fIargc_in_out\fP;
  68. .br
  69.       String *\fIargv_in_out\fP[];
  70. .br
  71.       String *\fIfallback_resources\fP;
  72. .br
  73.       ArgList \fIargs\fP;
  74. .br
  75.       Cardinal \fInum_args\fP;
  76. .FN
  77. .IP \fIapp_con_return\fP 1.5i
  78. Returns the application context of this application, if non-NULL.
  79. .IP \fIapplication_class\fP 1.5i
  80. Specifies the class name of this application,
  81. which is usually the generic name for all instances of this application.
  82. A useful convention is to form the class name by capitalizing the
  83. first letter of the application name. For example, the application named
  84. ``xman'' has a class name of ``Xman''. 
  85. .IP \fIoptions\fP 1.5i
  86. Specifies how to parse the command line for any application-specific
  87. resources.
  88. The options argument is passed as a parameter to
  89. .PN XrmParseCommand .
  90. For further information,
  91. see \fI\*(xL\fP.
  92. .IP \fInum_options\fP 1.5i
  93. Specifies the number of entries in the options list.
  94. .IP \fIargc_in_out\fP 1.5i
  95. Specifies a pointer to the number of command line parameters.
  96. .IP \fIargv_in_out\fP 1.5i
  97. Specifies the command line parameters.
  98. .IP \fIfallback_resources\fP 1.5i
  99. Specifies resource values to be used if the site-wide application class
  100. defaults file cannot be opened, or NULL.
  101. .IP \fIargs\fP 1.5i
  102. Specifies the argument list to use when creating the Application shell.
  103. .IP \fInum_args\fP 1.5i
  104. Specifies the number of arguments in \fIargs\fP.
  105. .LP
  106. This function will remove the command line arguments that the toolkit
  107. reads from \fIargc_in_out\fP, and \fIargv_in_out\fP.  It will then
  108. attempt to open the display.  If the display cannot be opened, an error
  109. message is issued and XtAppInitialize terminates the application.  Once
  110. the display is opened, all resources are read from the locations
  111. specified by the \*(xI.  This function returns an ApplicationShell
  112. widget to be used as the root of the application's widget tree.
  113. .NH 2
  114. Creating a Widget 
  115. .LP
  116. .XS
  117.     Creating a Widget
  118. .XE
  119. .IN "widget creation" "" "@DEF@"
  120. .IN "creating widgets" "" "@DEF@"
  121. .IN "XtRealizeWidget" "" ""
  122. Creating a widget is a three-step process.  First, the widget instance
  123. is allocated, and various instance-specific attributes are set by
  124. using \fBXtCreateWidget\fP.  Second, the widget's parent is informed
  125. of the new child by using \fBXtManageChild\fP.  Finally, X windows are
  126. created for the parent and all its children by using \fBXtRealizeWidget\fP
  127. and specifying the top-most widget. The first two steps can be
  128. combined by using \fBXtCreateManagedWidget\fP.  In addition, 
  129. \fBXtRealizeWidget\fP is automatically called when the child becomes
  130. managed if the parent is already realized.
  131. .LP
  132. To allocate, initialize, and manage a widget, use
  133. .PN XtCreateManagedWidget .
  134. .IN "XtCreateManagedWidget" "" "@DEF@"
  135. .FD 0
  136. Widget XtCreateManagedWidget(\fIname\fP, \fIwidget_class\fP, \fIparent\fP, \
  137. \fIargs\fP, \fInum_args\fP)
  138. .br
  139.       String \fIname\fP;
  140. .br
  141.       WidgetClass \fIwidget_class\fP;
  142. .br
  143.       Widget \fIparent\fP;
  144. .br
  145.       ArgList \fIargs\fP;
  146. .br
  147.       Cardinal \fInum_args\fP;
  148. .FN
  149. .IP \fIname\fP 1i
  150. Specifies the instance name for the created widget that is used for retrieving
  151. widget resources.
  152. .IP \fIwidget_class\fP 1i
  153. Specifies the widget class pointer for the created widget.
  154. .IP \fIparent\fP 1i
  155. Specifies the parent widget ID.
  156. .IP \fIargs\fP 1i
  157. Specifies the argument list.  The argument list is a variable-length
  158. list composed of name and value pairs that contain information
  159. pertaining to the specific widget instance being created.  For further
  160. information, see Section 2.7.2.
  161. .IP \fInum_args\fP 1i
  162. Specifies the number of arguments in the argument list.
  163. If the num_args is zero, the argument list is never referenced.
  164. .LP
  165. When a widget instance is successfully created, the widget identifier
  166. is returned to the application.  If an error is encountered, the
  167. .PN XtError
  168. routine is invoked to inform the user of the error.
  169. .IN "XtError" "" ""
  170. .LP
  171. For further information, see \fI\*(xT\fP.
  172. .NH 2
  173. Common Resources
  174. .XS
  175.     Common Resources
  176. .XE
  177. .IN "resource" ""
  178. .LP
  179. Although a widget can have unique arguments that it understands, all
  180. widgets have common arguments that provide some regularity of operation.
  181. The common arguments allow arbitrary widgets to be managed by
  182. higher-level components without regard for the individual widget type.
  183. Widgets will ignore any argument that they do not understand.
  184. .LP
  185. The following resources are retrieved from the argument list
  186. or from the resource database by all of the Athena widgets:
  187. .ps 9
  188. .nr PS 9
  189. .vs 11
  190. .nr VS 11
  191. .TS H
  192. lw(1.5i) lw(1i) lw(1i) lw(2i).
  193. _
  194. .sp 3p
  195. .TB
  196. Name    Class    Type    Default Value
  197. .sp 3p
  198. _
  199. .TH
  200. .R
  201. .sp 3p
  202. accelerators    Accelerators    AcceleratorTable    NULL
  203. ancestorSensitive    AncestorSensitive    Boolean    True
  204. background    Background    Pixel    XtDefaultBackground
  205. backgroundPixmap    Pixmap    Pixmap    XtUnspecifiedPixmap
  206. borderColor    BorderColor    Pixel    XtDefaultForeground
  207. borderPixmap    Pixmap    Pixmap    XtUnspecifiedPixmap
  208. borderWidth    BorderWidth    Dimension    1
  209. colormap    Colormap    Colormap    Parent's Colormap
  210. depth    Depth    int    Parent's Depth
  211. destroyCallback    Callback    XtCallbackList    NULL
  212. height    Height    Dimension    \fIwidget dependent\fP
  213. mappedWhenManaged    MappedWhenManaged    Boolean    True
  214. screen    Screen    Screen    Parent's Screen
  215. sensitive    Sensitive    Boolean    True
  216. translations    Translations    TranslationTable    \fIwidget dependent\fP
  217. width    Width    Dimension    \fIwidget dependent\fP
  218. x    Position    Position    0
  219. y    Position    Position    0
  220. .sp 3p
  221. _
  222. .TE
  223. .ps 11
  224. .nr PS 11
  225. .vs 13
  226. .nr VS 13
  227. .IN "XtDefaultForeground" "" ""
  228. .IN "XtDefaultBackground" "" ""
  229. .LP
  230. The following additional resources are retrieved from the argument list
  231. or from the resource database by many of the Athena widgets:
  232. .ps 9
  233. .nr PS 9
  234. .vs 11
  235. .nr VS 11
  236. .TS H
  237. lw(1.5i) lw(1i) lw(1i) lw(2i).
  238. _
  239. .sp 3p
  240. .TB
  241. Name    Class    Type    Default Value
  242. .sp 3p
  243. _
  244. .TH
  245. .R
  246. .sp 3p
  247. callback    Callback    XtCallbackList    NULL
  248. cursor    Cursor    Cursor    \fIwidget dependent\fP
  249. foreground    Foreground    Pixel    XtDefaultForeground
  250. insensitiveBorder    Insensitive    Pixmap    GreyPixmap
  251. .sp 3p
  252. _
  253. .TE
  254. .ps 11
  255. .nr PS 11
  256. .vs 13
  257. .nr VS 13
  258. .IN "XtDefaultForeground" "" ""
  259. .NH 2
  260. Resource Conversions
  261. .XS
  262.     Resource Conversions
  263. .XE
  264. .IN "conversions" "" "@DEF@"
  265. .IN "string conversions" "" "@DEF@"
  266. .IN "type conversions" "" "@DEF@"
  267. .LP
  268. Most resources in the Athena widget set have a converter registered that
  269. will translate the string in a resource file to the correct internal
  270. representation.  While some are obvious (string to integer, for example),
  271. others need specific mention of the allowable values.  Three general
  272. converters are described here:
  273. .IP \(bu 5
  274. Cursor
  275. .IP \(bu 5
  276. Pixel
  277. .IP \(bu 5
  278. Bitmap
  279. .LP
  280. Many widgets have defined special converters that apply only to that
  281. widget.  When these occur, the documentation section for that widget
  282. will describe the converter.
  283. .NH 3
  284. Cursor Conversion
  285. .IN "conversions" "ColorCursor" "@DEF@"
  286. .IN "conversions" "Cursor" "@DEF@"
  287. .IN "cursor" "" ""
  288. .LP
  289. The value for the \fBcursorName\fP resource is specified in the resource
  290. database as a string, and is of the following forms:
  291. .IP \(bu 5
  292. A standard X cursor name from \fB< X11/cursorfont.h >\fP.
  293. The names in \fBcursorfont.h\fP each describe a specific cursor.  The
  294. resource names for these cursors are exactly like the names in this file
  295. except the \fBXC_\fP is not used.  The cursor definition \fBXC_gumby\fP
  296. has a resource name of \fBgumby\fP.
  297. .IP \(bu 5
  298. Glyphs, as in \fIFONT font-name glyph-index [[ font-name ] glyph-index ]\fP.
  299. The first font and glyph specify the cursor source pixmap.
  300. The second font and glyph specify the cursor mask pixmap.
  301. The mask font defaults to the source font,
  302. and the mask glyph index defaults to the source glyph index.
  303. .IP \(bu 5
  304. A relative or absolute file name.
  305. If a relative or absolute file name is specified, that file is used to
  306. create the source pixmap.  Then the string "Mask" is appended to
  307. locate the cursor mask pixmap.  If the "Mask" file does not exist, the
  308. suffix "msk" is tried.  If "msk" fails, no cursor mask will be used.
  309. If the filename does not start with '/' or './' the the bitmap
  310. file path is used (see section 2.4.3).
  311. .NH 3
  312. Pixel Conversion
  313. .LP
  314. .IN "conversions" "Pixel" "@DEF@"
  315. .IN "pixel" "" ""
  316. .IN "rgb.txt" "" ""
  317. .IN "XtDefaultForeground" "" ""
  318. .IN "XtDefaultBackground" "" ""
  319. The string-to-pixel converter takes any name that is acceptable to
  320. XParseColor (see \fI\*(xL\fP).  In addition this routine understands
  321. the special toolkit symbols `XtDefaultForeground' and
  322. `XtDefaultBackground', described in \fI\*(xT\fP.  In short the acceptable
  323. pixel names are:
  324. .IP \(bu 5
  325. Any color name for the rgb.txt file (typically in the directory
  326. /usr/lib/X11 on POSIX systems).
  327. .IP \(bu 5
  328. A numeric specification of the form #<red><green><blue> where these
  329. numeric values are hexadecimal digits (both upper and lower case).
  330. .IP \(bu 5
  331. The special strings `XtDefaultForeground' and `XtDefaultBackground'
  332. .NH 3
  333. Bitmap Conversion
  334. .IN "bitmap conversions" "" "@DEF@"
  335. .IN "conversions" "Bitmap" "@DEF@"
  336. .IN "bitmapFilePath" "" "@DEF@"
  337. .IN "BitmapFilePath" "" "@DEF@"
  338. .IN "/usr/include/X11/bitmaps" "" ""
  339. .LP
  340. The string-to-bitmap converter attempts to locate a file containing
  341. bitmap data whose name is specified by the input string.  If the file
  342. name is relative (i.e. does not begin with / or ./), the directories to
  343. be searched are specified in the \fBbitmapFilePath\fP resource--class
  344. \fBBitmapFilePath\fP.  This resource specifies a colon (:) separated
  345. list of directories that will be searched for the named bitmap or
  346. cursor glyph (see section 2.4.1).  The \fBbitmapFilePath\fP resource is
  347. global to the application, and may \fBnot\fP be specified differently
  348. for each widget that wishes to convert a cursor to bitmap.  In addition
  349. to the directories specified in the \fBbitmapFilePath\fP resource a
  350. default directory is searched.  When using POSIX the default
  351. directory is
  352. .PN /usr/include/X11/bitmaps .
  353. .NH 2
  354. Realizing a Widget
  355. .LP
  356. .XS
  357.     Realizing a Widget
  358. .XE
  359. .IN "realizing widgets" "" "@DEF@"
  360. The
  361. .PN XtRealizeWidget
  362. function performs two tasks:
  363. .IP \(bu 5
  364. Calculates the geometry constraints of all managed descendants
  365. of this widget.  The actual calculation is put off until realize time
  366. for performance reasons.
  367. .IP \(bu 5
  368. Creates an X window for the widget and, if it is a composite widget,
  369. realizes each of its managed children.
  370. .IN "XtRealizeWidget" "" "@DEF@"
  371. .FD 0
  372. void XtRealizeWidget(\fIw\fP)
  373. .br
  374.       Widget \fIw\fP;
  375. .FN
  376. .IP \fIw\fP 1i
  377. Specifies the widget.
  378. .LP
  379. For further information about this function,
  380. see the \fI\*(xT\fP.
  381. .NH 2
  382. Processing Events
  383. .LP
  384. .XS
  385.     Processing Events
  386. .XE
  387. .IN "events" "" ""
  388. .IN "XtAppInitialize" "" ""
  389. Now that the application has created, managed and realized its
  390. widgets, it is ready to process the events that will be delivered by the
  391. X Server to this client.  A function call that will process the
  392. events is \fBXtAppMainLoop\fP.
  393. .IN "XtAppMainLoop" "" "@DEF@"
  394. .FD 0
  395. void XtAppMainLoop(\fIapp_context\fP)
  396. .br
  397.       XtAppContext \fIapp_context\fP;
  398. .FN
  399. .IP \fIapp_context\fP 1i
  400. Specifies the application context of this application.  The value is
  401. normally returned by \fBXtAppInitialize\fP.
  402. .LP
  403. This function never returns: it is an infinite loop that processes the
  404. X events.  User input can be handled through callback procedures and
  405. application defined action routines.  More details are provided in
  406. \fI\*(xT\fP.
  407. .NH 2
  408. Standard Widget Manipulation Functions
  409. .XS
  410.     Standard Widget Manipulation Functions
  411. .XE
  412. .LP
  413. After a widget has been created, a client can interact with that
  414. widget by calling one of the standard widget manipulation routines
  415. provide by the \*(xI, or a widget class-specific manipulation routine.
  416. .LP
  417. The \*(xI provide generic routines to give the application programmer
  418. access to a set of standard widget functions.  The common widget
  419. routines let an application or composite widget perform the following
  420. operations on widgets without requiring explicit knowledge of the widget
  421. type.
  422. .IP \(bu 5
  423. Control the mapping of widget windows
  424. .IP \(bu 5
  425. Destroy a widget instance
  426. .IP \(bu 5
  427. Obtain an argument value
  428. .IP \(bu 5
  429. Set an argument value
  430. .NH 3
  431. Mapping Widgets
  432. .LP
  433. By default,
  434. widget windows are mapped (made viewable) automatically by
  435. \fBXtRealizeWidget\fP.  This behavior can be disabled by using 
  436. \fBXtSetMappedWhenManaged\fP, making the client responsible for calling
  437. \fBXtMapWidget\fP to make the widget viewable.
  438. .IN "XtSetMappedWhenManaged" "" "@DEF@"
  439. .IN "XtMapWidget" "" ""
  440. .IN "XtRealizeWidget" "" ""
  441. .FD 0
  442. void XtSetMappedWhenManaged(\fIw\fP, \fImap_when_managed\fP)
  443. .br
  444.       Widget \fIw\fP;
  445. .br
  446.       Boolean \fImap_when_managed\fP;
  447. .FN
  448. .IP \fIw\fP 1i
  449. Specifies the widget.
  450. .IP \fImap_when_managed\fP 1i
  451. Specifies the new value.
  452. If map_when_managed is \fBTrue\fP, the widget is mapped automatically
  453. when it is realized.  If map_when_managed is \fBFalse\fP, the client
  454. must call
  455. .PN XtMapWidget
  456. or make a second call to
  457. .PN XtSetMappedWhenManaged
  458. to cause the child window to be mapped.
  459. .LP
  460. .sp
  461. The definition for
  462. .PN XtMapWidget
  463. is:
  464. .IN "XtMapWidget" "" "@DEF@"
  465. .FD 0
  466. void XtMapWidget(\fIw\fP)
  467. .br
  468.      Widget \fIw\fP;
  469. .FN
  470. .IP \fIw\fP 1i
  471. Specifies the widget.
  472. .LP
  473. When you are creating several children in sequence for a previously
  474. realized common parent it is generally more efficient to construct a
  475. list of children as they are created (using \fBXtCreateWidget\fP) and
  476. then use \fBXtManageChildren\fP to request that their parent managed
  477. them all at once.  By managing a list of children at one time, the
  478. parent can avoid wasteful duplication of geometry processing and the
  479. associated ``screen flash''.
  480. .IN "XtManageChildren" "" "@DEF@"
  481. .IN "XtCreateWidget" "" ""
  482. .FD 0
  483. void XtManageChildren(\fIchildren\fP, \fInum_children\fP)
  484. .br
  485.       WidgetList \fIchildren\fP;
  486. .br
  487.       Cardinal \fInum_children\fP;
  488. .FN
  489. .IP \fIchildren\fP 1i
  490. Specifies a list of children to add.
  491. .IP \fInum_children\fP 1i
  492. Specifies the number of children to add.
  493. .LP
  494. If the parent is already visible on the screen, it is especially
  495. important to batch updates so that the minimum amount of visible window
  496. reconfiguration is performed.
  497. .LP
  498. For further information about these functions,
  499. see the \fI\*(xI\fP.
  500. .NH 3
  501. Destroying Widgets
  502. .LP
  503. To destroy a widget instance of any type, use
  504. .PN XtDestroyWidget .
  505. .IN "XtDestroyWidget" "" "@DEF@"
  506. .FD 0
  507. void XtDestroyWidget(\fIw\fP)
  508. .br
  509.       Widget \fIw\fP;
  510. .FN
  511. .IP \fIw\fP 1i
  512. Specifies the widget.
  513. .LP
  514. .PN XtDestroyWidget
  515. destroys the widget and recursively destroys any children that it may have,
  516. including the windows created by its children.
  517. After calling
  518. .PN XtDestroyWidget ,
  519. no further references should be made to the widget or any children
  520. that the destroyed widget may have had. 
  521. .NH 3
  522. Retrieving Widget Resource Values
  523. .LP
  524. To retrieve the current value of a resource attribute associated 
  525. with a widget instance, use
  526. .PN XtGetValues .
  527. .IN "XtGetValues" "" "@DEF@"
  528. .FD 0
  529. void XtGetValues(\fIw\fP, \fIargs\fP, \fInum_args\fP)
  530. .br
  531.       Widget \fIw\fP;
  532. .br
  533.       ArgList \fIargs\fP;
  534. .br
  535.       Cardinal \fInum_args\fP;
  536. .FN
  537. .IP \fIw\fP 1i
  538. Specifies the widget.
  539. .IP \fIargs\fP 1i
  540. Specifies a variable-length argument list of name and \fBaddress\fP
  541. pairs that contain the resource name and the address into which the
  542. resource value is stored.
  543. .IP \fInum_args\fP 1i
  544. Specifies the number of arguments in the argument list.
  545. .LP
  546. The arguments and values passed in the argument list are dependent on
  547. the widget. Note that the caller is responsible for providing space
  548. into which the returned resource value is copied; the \fBArgList\fP
  549. contains a pointer to this storage (e.g. x and y must be
  550. allocated as Position).  For further information, see the \fI\*(xT\fP.
  551. .NH 3
  552. Modifying Widget Resource Values
  553. .LP
  554. To modify the current value of a resource attribute associated with a
  555. widget instance, use
  556. .PN XtSetValues .
  557. .IN "XtSetValues" "" "@DEF@"
  558. .FD 0
  559. void XtSetValues(\fIw\fP, \fIargs\fP, \fInum_args\fP)
  560. .br
  561.       Widget \fIw\fP;
  562. .br
  563.       ArgList \fIargs\fP;
  564. .br
  565.       Cardinal \fInum_args\fP;
  566. .FN
  567. .IP \fIw\fP 1i
  568. Specifies the widget.
  569. .IP \fIargs\fP 1i
  570. Specifies an array of name and \fBvalue\fP pairs that contain the
  571. arguments to be modified and their new values.
  572. .IP \fInum_args\fP 1i
  573. Specifies the number of arguments in the argument list.
  574. .LP
  575. The arguments and values that are passed will depend on the widget
  576. being modified.  Some widgets may not allow certain resources to be
  577. modified after the widget instance has been created or realized.
  578. No notification is given if any part of a \fBXtSetValues\fP request is
  579. ignored. 
  580. .LP
  581. For further information about these functions, see the \fI\*(xI\fP.
  582. .IN "XtGetValues" "" ""
  583. .IN "XtSetValues" "" ""
  584. .NT
  585. The argument list entry for
  586. .PN XtGetValues
  587. specifies the address to which the caller wants the value copied.  The
  588. argument list entry for
  589. .PN XtSetValues ,
  590. however, contains the new value itself, if the size of value is less than
  591. sizeof(XtArgVal) (architecture dependent, but at least sizeof(long));
  592. otherwise, it is a pointer to the value.  String resources are always
  593. passed as pointers, regardless of the length of the string.
  594. .NE
  595. .NH 2
  596. Using the Client Callback Interface
  597. .LP
  598. .XS
  599.     Using the Client Callback Interface
  600. .XE
  601. .IN "callbacks" "" "" 
  602. Widgets can communicate changes in their state to their clients
  603. by means of a callback facility.
  604. The format for a client's callback handler is:
  605. .IN "CallbackProc" "" "@DEF@"
  606. .FD 0
  607. void \fICallbackProc\fP(\fIw\fP, \fIclient_data\fP, \fIcall_data\fP)
  608. .br
  609.       Widget \fIw\fP;
  610. .br
  611.       XtPointer \fIclient_data\fP;
  612. .br
  613.       XtPointer \fIcall_data\fP;
  614. .FN
  615. .IP \fIw\fP 1i
  616. Specifies widget for which the callback is registered.
  617. .IP \fIclient_data\fP 1i
  618. Specifies arbitrary client-supplied data that the widget should pass
  619. back to the client when the widget executes the client's callback
  620. procedure.  This is a way for the client registering the callback to
  621. also register client-specific data: a pointer to additional information
  622. about the widget, a reason for invoking the callback, and so on. If no
  623. additional information is necessary, NULL may be passed as this argument.
  624. This field is also frequently known as the \fIclosure\fP.
  625. .IP \fIcall_data\fP 1i
  626. Specifies any callback-specific data the widget wants to pass to the client.
  627. For example, when Scrollbar executes its \fBjumpProc\fP callback list, 
  628. it passes the current position of the thumb in \fIcall_data\fP.
  629. .LP
  630. Callbacks can be registered either by creating an argument containing
  631. the callback list described below or by using the special convenience
  632. routines \fBXtAddCallback\fP and \fBXtAddCallbacks\fP.  When the widget
  633. is created, a pointer to a list of callback procedure and data pairs can
  634. be passed in the argument list to
  635. .PN XtCreateWidget .
  636. The list is of type
  637. .PN XtCallbackList :
  638. .IN "XtCallbackProc" 
  639. .IN "XtAddCallbacks" 
  640. .IN "XtAddCallback" 
  641. .IN "XtCallbackList" "" "@DEF@"
  642. .IN "XtCallbackRec" "" "@DEF@"
  643. .LP
  644. .Ds 0
  645. .TA .5i 3i
  646. .ta .5i 3i
  647. typedef struct {
  648.     XtCallbackProc callback;
  649.     XtPointer closure;
  650. } XtCallbackRec, *XtCallbackList;
  651. .De
  652. .LP
  653. The callback list must be allocated and initialized before calling
  654. .PN XtCreateWidget .
  655. .IN "XtCreateWidget" 
  656. The end of the list is identified by an entry containing NULL in
  657. callback and closure.  Once the widget is created, the client can change
  658. or de-allocate this list; the widget itself makes no further reference
  659. to it.  The closure field contains the client_data passed to the
  660. callback when the callback list is executed.
  661. .LP
  662. The second method for registering callbacks is to use
  663. .PN XtAddCallback 
  664. after the widget has been created.
  665. .IN "XtAddCallback" "" "@DEF@"
  666. .FD 0
  667. void XtAddCallback(\fIw\fP, \fIcallback_name, \fP\fIcallback\fP, \
  668. \fIclient_data\fP)
  669. .br
  670.       Widget \fIw\fP;
  671. .br
  672.       String \fIcallback_name\fP;
  673. .br
  674.       XtCallbackProc \fIcallback\fP;
  675. .br
  676.       XtPointer \fIclient_data\fP;
  677. .FN
  678. .IP \fIw\fP 1i
  679. Specifies the widget to add the callback to.
  680. .IP \fIcallback_name\fP 1i
  681. Specifies the callback list within the widget to append to.
  682. .IP \fIcallback\fP 1i
  683. Specifies the callback procedure to add.
  684. .IP \fIclient_data\fP 1i
  685. Specifies the data to be passed to the callback when it is invoked.
  686. .LP
  687. .PN XtAddCallback
  688. adds the specified callback to the list for the named widget.
  689. .LP
  690. All widgets provide a callback list named
  691. .PN destroyCallback
  692. .IN "destroyCallback" "" "@DEF@"
  693. where clients can register procedures that are to be executed when the
  694. widget is destroyed.  The destroy callbacks are executed when the widget
  695. or an ancestor is destroyed.  The \fIcall_data\fP argument is unused for
  696. destroy callbacks.
  697. .NH 2
  698. Programming Considerations
  699. .LP
  700. .XS
  701.     Programming Considerations
  702. .XE
  703. This section provides some guidelines on how to set up an application
  704. program that uses the \*(tk.
  705. .NH 3
  706. Writing Applications
  707. .LP
  708. .IN "writing applications"
  709. .IN "StringDefs.h"
  710. .IN "Intrinsic.h"
  711. When writing an application that uses the X Toolkit, 
  712. you should make sure that your application performs the following:
  713. .IP 1. 5
  714. Include
  715. .Pn < X11/Intrinsic.h >
  716. in your application programs.
  717. This header file automatically includes
  718. .Pn < X11/Xlib.h >,
  719. so all Xlib functions also are defined.
  720. I may also be necessary to include \fB< X11/StringDefs.h >\fP when setting
  721. up argument lists, as many of the XtN\fIsomething\fP definitions are
  722. only defined in this file.
  723. .IP 2. 5
  724. Include the widget-specific header files for each widget type 
  725. that you need to use.
  726. For example, 
  727. .Pn < X11/Xaw/Label.h >
  728. and
  729. .Pn < X11/Xaw/Command.h >.
  730. .IP 3. 5
  731. Call the
  732. .PN XtAppInitialize
  733. .IN "XtAppInitialize"
  734. function before invoking any other toolkit or Xlib functions.
  735. For further information,
  736. see Section 2.1 and the \fI\*(xT\fP.
  737. .IP 4. 5
  738. To pass attributes to the widget creation routines that will override
  739. any site or user customizations, set up argument lists.  In this
  740. document, a list of valid argument names is provided in the discussion
  741. of each widget.  The names each have a global symbol defined that begins
  742. with \fBXtN\fP to help catch spelling errors.  For example,
  743. \fBXtNlabel\fP is defined for the \fBlabel\fP resource of many widgets.
  744. .IN "XtN" "" "@DEF@"
  745. .IP
  746. For further information, see Section 2.9.2.2.
  747. .IP 5. 5
  748. When the argument list is set up, create the widget with the
  749. \fBXtCreateManagedWidget\fP function.  For further information, see
  750. Section 2.2 and the \fI\*(xT\fP.
  751. .IN "XtCreateManagedWidget"
  752. .IP 6. 5
  753. If the widget has any callback routines, set by the 
  754. .PN XtNcallback
  755. argument or the
  756. .PN XtAddCallback 
  757. function, declare these routines within the application.
  758. .IN "XtAddCallback"
  759. .IP 7. 5
  760. After creating the initial widget hierarchy, windows must be created
  761. for each widget by calling
  762. .PN XtRealizeWidget
  763. on the top level widget.
  764. .IN "XtRealizeWidget"
  765. .IP 8. 5
  766. Most applications now sit in a loop processing events using
  767. .PN XtAppMainLoop ,
  768. for example:
  769. .IN "XtAppMainLoop"
  770. .IP
  771. .Ds 0
  772. XtCreateManagedWidget(\fIname\fP, \fIclass\fP, \fIparent\fP, \fIargs\fP, \fInum_args\fP);
  773. XtRealizeWidget(\fIshell\fP);
  774. XtAppMainLoop(\fIapp_context\fP);
  775. .De
  776. .IP
  777. For information about this function, see the \fI\*(xT\fP.
  778. .IP 9. 5
  779. Link your application with 
  780. .PN libXaw.a
  781. (the Athena widgets),
  782. .PN libXmu.a
  783. (miscellaneous utilities),
  784. .PN libXt.a 
  785. (the \*(tk \*(xI), 
  786. .PN libXext.a
  787. (the extension library needed for the shape extension code which allows
  788. rounded Command buttons), and
  789. .PN libX11.a 
  790. (the core X library).
  791. The following provides a sample command line:
  792. .IN "libXaw.a"
  793. .IN "libXmu.a"
  794. .IN "libXt.a"
  795. .IN "libXext.a"
  796. .IN "libX11.a"
  797. .IN "linking applications"
  798. .IN "compiling applications"
  799. .IP
  800. .Ds 0
  801. cc -o \fIapplication\fP \fIapplication\fP.c \-lXaw \-lXmu \-lXt \
  802. \-lXext \-lX11
  803. .De
  804. .NH 3
  805. Changing Resource Values
  806. .IN "resource" ""
  807. .LP
  808. The \*(xI support two methods of changing the default resource
  809. values; the resource manager, and an argument list passed into
  810. XtCreateWidget.  While resources values will get updated no matter
  811. which method you use, the two methods provide slightly different
  812. functionality. 
  813. .IP Resource Manager 1.5i
  814. This method picks up resource definitions described in \fI\*(xL\fP from
  815. many different locations at run time.  The locations most important to
  816. the application programmer are the \fIfallback resources\fP and the
  817. \fIapp-defaults\fP file, (see \fI\*(xT\fP for the complete list).
  818. Since these resource are loaded at run time, they can be overridden by
  819. the user, allowing an application to be customized to fit the
  820. particular needs of each individual user.  These values can also be
  821. modified without the need to rebuild the application, allowing rapid
  822. prototyping of user interfaces.  Application programmers should use
  823. resources in preference to hard-coded values whenever possible.
  824. .IP Argument Lists 1.5i
  825. The values passed into the widget at creation time via an argument list
  826. cannot be modified by the user, and allow no opportunity for
  827. customization.  It is used to set resources that cannot be specified as
  828. strings (e.g. callback lists) or resources that should not be
  829. overridden (e.g. window depth) by the user.
  830. .NH 4
  831. Specifying Resources
  832. .LP
  833. It is important for all X Toolkit application programmers to
  834. understand how to use the X Resource Manager to specify resources for
  835. widgets in an X application.  This section will describe the most common
  836. methods used to specify these resources, and how to use the X Resource
  837. manager.
  838. .IN "xrdb"
  839. .IP \fBXrdb\fP 1.5i
  840. The \fBxrdb\fP utility may be used to load a file containing
  841. resources into the X server.   Once the resources are loaded, the
  842. resources will affect any new applications started on the display that
  843. they were loaded onto.  
  844. .IN "application defaults"
  845. .IN "app-defaults"
  846. .IN "/usr/lib/X11/app-defaults"
  847. .IP "\fBApplication Defaults\fP" 1.5i
  848. The application defaults (app-defaults) file (normally in
  849. /usr/lib/X11/app-defaults/\fIclassname\fP) for an application is loaded
  850. whenever the application is started.
  851. .LP
  852. The resource specification has two colon-separated parts, a name, and
  853. a value.  The \fIvalue\fP is a string whose format is dependent on the
  854. resource specified by \fIname\fP.  \fIName\fP is constructed by
  855. appending a resource name to a full widget name.
  856. .LP
  857. The full widget name is a list of the name of every ancestor of the
  858. desired widget separated by periods (.).  Each widget also has a class
  859. associated with it.  A class is a type of widget (e.g. Label or
  860. Scrollbar or Box).  Notice that class names, by convention, begin with
  861. capital letters and instance names begin with lower case letters.  The
  862. class of any widget may be used in place of its name in a resource
  863. specification.  Here are a few examples:
  864. .IP xman.form.button1 1.5i
  865. This is a fully specified resource name that matches exactly one
  866. widget instance.
  867. .IP Xman.Form.Command 1.5i
  868. This will match any widget in applications of class \fIXman\fP that is
  869. a Command widget and a child of a Form widget.
  870. .IP Xman.Form.button1 1.5i
  871. This is a mixed resource name with both widget names and classes specified.
  872. .LP
  873. This syntax allows an application programmer to specify any widget
  874. in the widget tree.  To match more than one widget (for example a user
  875. may want to make all Command buttons blue), use an asterisk (*)
  876. instead of a period.  When an asterisk is used, any number of widgets
  877. (including zero) may exist between the two widget names. For example:
  878. .IP Xman*Command 1.5i
  879. This matches all Command widgets in the Xman application.
  880. .IP Foo*button1 1.5i
  881. This matches any widget in the Foo application that is named \fIbutton1\fP.
  882. .LP
  883. The root of all application widget trees is the widget returned by
  884. \fBXtAppInitialize\fP.  Even though this is actually an
  885. ApplicationShell widget, the toolkit replaces its widget class with the
  886. class name of the application.  The name of this widget is either
  887. the name used to invoke the application (\fBargv[0]\fP) or the name of
  888. the application specified using the standard \fI-name\fP command line
  889. option supported by the \*(xI.
  890. .LP
  891. The last step in constructing the resource name is to append the name of
  892. the resource with either a period or asterisk to the full or partial
  893. widget name already constructed. 
  894. .IP *foreground:Blue 2.25i
  895. Specifies that all widgets in all applications will have a foreground
  896. color of blue.
  897. .IP Xman*borderWidth:10 2.25i
  898. Specifies that all widgets in an application whose class is Xman will
  899. have a border width of 10 (pixels).
  900. .IP xman.form.button1.label:Testing 2.25i
  901. Specifies that a particular widget in the xman application will have a
  902. label named \fITesting\fP.
  903. .LP
  904. An exclamation point (!) in the first column of a line indicates
  905. that the rest of the line should be treated as a comment.
  906. .LP
  907. \fBFinal Words\fP
  908. .LP
  909. The Resource manager is a powerful tool that can be used very
  910. effectively to customize \*(tk applications at run time by either the
  911. application programmer or the user.  Some final points to note:
  912. .IP \(bu 5
  913. An application programmer may add new resources to their
  914. application.  These resources are associated with the global
  915. application, and not any particular widget.  The \*(tk function used for
  916. adding the application resources is \fBXtGetApplicationResources\fP.
  917. .IN "XtGetApplicationResources"
  918. .IP \(bu 5
  919. Be careful when creating resource files.  Since widgets will
  920. ignore resources that they do not understand, any spelling
  921. errors will cause a resource to have no effect.
  922. .IP \(bu 5
  923. Only one resource line will match any given resource.  There is a set
  924. of precedence rules, which take the following general stance.
  925. .ta 10n
  926. .IP "" 5
  927. \(bu    More specific overrides less specific, thus period always overrides asterisk.
  928. .IP "" 5
  929. \(bu    Names on the left are more specific and override names on the right.
  930. .IP "" 5
  931. \(bu    When resource specifications are exactly the same, user defaults
  932. .br
  933.     will override program defaults.
  934. .LP
  935. For a complete explanation of the rules of precedence, and
  936. other specific topics see \fI\*(xT\fP and \fI\*(xL\fP.
  937. .NH 4
  938. Creating Argument Lists
  939. .IN "argument lists" "" "@DEF@"
  940. .LP
  941. To set up an argument list for the inline specification of widget attributes,
  942. you may use any of the four approaches discussed in this section.
  943. Each resource name has a global symbol associated with it.  This
  944. global symbol has the form XtN\fIresource name\fP.  For example, the
  945. symbol for ``foreground'' is \fBXtNforeground\fP. For further information,
  946. see the \fI\*(xI\fP.
  947. .LP
  948. Argument are specified by using the following structure:
  949. .IN "ArgList" "" "@DEF@"
  950. .IN "Arg" "" "@DEF@"
  951. .LP
  952. .Ds 0
  953. .TA .5i 1.5i
  954. .ta .5i 1.5i
  955. typedef struct {
  956.     String name;
  957.     XtArgVal value;
  958. } Arg, *ArgList;
  959. .De
  960. .LP
  961. The first approach is to statically initialize the argument list.
  962. For example:
  963. .LP
  964. .Ds 0
  965. .TA .5i
  966. .ta .5i
  967. static Arg arglist[] = {
  968.     {XtNwidth, (XtArgVal) 400},
  969.     {XtNheight, (XtArgVal) 300},
  970. };
  971. .De
  972. .LP
  973. This approach is convenient for lists that do not need to be computed
  974. at runtime and makes adding or deleting new elements easy.
  975. The 
  976. .IN "XtNumber"
  977. .PN XtNumber 
  978. macro is used to compute the number of elements in the argument list,
  979. preventing simple programming errors:
  980. .LP
  981. .Ds 
  982. XtCreateWidget(\fIname\fP, \fIclass\fP, \fIparent\fP, \fIarglist\fP, XtNumber(\fIarglist\fP));
  983. .De
  984. .IN "XtSetArg" "" "@DEF@"
  985. .LP
  986. The second approach is to use the 
  987. .PN XtSetArg
  988. macro.
  989. For example:
  990. .LP
  991. .Ds 0
  992. .TA .5i
  993. .ta .5i
  994. Arg arglist[10];
  995. XtSetArg(arglist[1], XtNwidth, 400);
  996. XtSetArg(arglist[2], XtNheight, 300);
  997. .De
  998. .LP
  999. To make it easier to insert and delete entries, 
  1000. you also can use a variable index:
  1001. .LP
  1002. .Ds 0
  1003. .TA .5i
  1004. .ta .5i
  1005. Arg arglist[10];
  1006. Cardinal i=0;
  1007. XtSetArg(arglist[i], XtNwidth,  400);       i++;
  1008. XtSetArg(arglist[i], XtNheight, 300);       i++;
  1009. .De
  1010. .LP
  1011. The i variable can then be used as the argument list count in the widget
  1012. create function.
  1013. In this example,
  1014. .IN "XtNumber"
  1015. .PN XtNumber 
  1016. would return 10, not 2, and therefore is not useful.
  1017. .NT
  1018. You should not use auto-increment or auto-decrement 
  1019. within the first argument to
  1020. .PN XtSetArg .
  1021. As it is currently implemented,
  1022. .PN XtSetArg
  1023. is a macro that dereferences the first argument twice.
  1024. .NE
  1025. .LP
  1026. The third approach is to individually set the elements of the
  1027. argument list array:
  1028. .LP
  1029. .Ds 0
  1030. .TA .5i
  1031. .ta .5i
  1032. Arg arglist[10];
  1033. arglist[0].name  = XtNwidth;
  1034. arglist[0].value = (XtArgVal) 400;
  1035. arglist[1].name  = XtNheight;
  1036. arglist[1].value = (XtArgVal) 300;
  1037. .De
  1038. .LP
  1039. Note that in this example, as in the previous example,
  1040. .IN "XtNumber"
  1041. .PN XtNumber 
  1042. would return 10, not 2, and therefore would not be useful.
  1043. .LP
  1044. The fourth approach is to use a mixture of the first and third approaches:
  1045. you can statically define the argument list but modify some entries at runtime.
  1046. For example:
  1047. .LP
  1048. .Ds 0
  1049. .TA .5i
  1050. .ta .5i
  1051. static Arg arglist[] = {
  1052.     {XtNwidth, (XtArgVal) 400},
  1053.     {XtNheight, (XtArgVal) NULL},
  1054. };
  1055. arglist[1].value = (XtArgVal) 300;
  1056. .De
  1057. .LP
  1058. In this example,
  1059. .IN "XtNumber"
  1060. .PN XtNumber 
  1061. can be used, as in the first approach, for easier code maintenance.
  1062. .NH 2
  1063. Example Programs
  1064. .XS
  1065.     Example Programs
  1066. .XE
  1067. .IN "examples"
  1068. .LP
  1069. The best way to understand how to use any programming library is by
  1070. trying some simple examples.  A collection of example programs that
  1071. introduces each of the widgets in that Athena widget set, as well as many
  1072. important toolkit programming concepts, is available in the X11R5
  1073. release as distributed by MIT.  It can be found in the distribution directory
  1074. \fBcontrib/examples/mit/Xaw\fP, but see your site administrator for the exact
  1075. location of these files on your system.
  1076. See the README file from that directory for a guide to the examples.
  1077.  
  1078.