control 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  1. .TH CONTROL 2
  2. .SH NAME
  3. Control,
  4. Controlset,
  5. activate,
  6. closecontrol,
  7. closecontrolset,
  8. controlcalled,
  9. controlwire,
  10. createbox,
  11. createboxbox,
  12. createbutton,
  13. createcolumn,
  14. createentry,
  15. createkeyboard,
  16. createlabel,
  17. createmenu,
  18. createradiobutton,
  19. createrow,
  20. createscribble,
  21. createslider,
  22. createstack,
  23. createtab,
  24. createtext,
  25. createtextbutton,
  26. ctlerror,
  27. ctlmalloc,
  28. ctlrealloc,
  29. ctlstrdup,
  30. ctlprint,
  31. deactivate,
  32. freectlfont,
  33. freectlimage,
  34. initcontrols,
  35. namectlfont,
  36. namectlimage,
  37. newcontrolset,
  38. resizecontrolset
  39. \- interactive graphical controls
  40. .SH SYNOPSIS
  41. .EX
  42. .ta 4n +4n +4n +4n +4n +4n +4n
  43. #include <u.h>
  44. #include <libc.h>
  45. #include <draw.h>
  46. #include <thread.h>
  47. #include <keyboard.h>
  48. #include <mouse.h>
  49. #include <control.h>
  50. .sp 0.3
  51. typedef struct Control Control;
  52. typedef struct Controlset Controlset;
  53. .sp 0.3
  54. struct Control
  55. {
  56. char *name;
  57. Rectangle rect; /* area on screen */
  58. Rectangle size; /* min/max Dx, Dy (not a rect) */
  59. Channel *event; /* chan(char*) to client */
  60. Channel *data; /* chan(char*) to client */
  61. \&...
  62. };
  63. .sp 0.3
  64. struct Controlset
  65. {
  66. \&...
  67. Channel *ctl;
  68. Channel *data;
  69. \&...
  70. int clicktotype;
  71. \&...
  72. };
  73. .sp 0.3
  74. void initcontrols(void)
  75. .sp 0.3
  76. Controlset* newcontrolset(Image *i, Channel *kc, Channel *mc, Channel *rc)
  77. .sp 0.3
  78. void closecontrolset(Controlset *cs)
  79. .sp 0.3
  80. int namectlfont(Font *font, char *name)
  81. .sp 0.3
  82. int freectlfont(char *name)
  83. .sp 0.3
  84. int namectlimage(Image *image, char *name)
  85. .sp 0.3
  86. int freectlimage(char *name)
  87. .sp 0.3
  88. Control* createbox(Controlset *cs, char *name)
  89. .sp 0.3
  90. Control* createboxbox(Controlset *cs, char *name)
  91. .sp 0.3
  92. Control* createbutton(Controlset *cs, char *name)
  93. .sp 0.3
  94. Control* createcolumn(Controlset*, char*)
  95. .sp 0.3
  96. Control* createentry(Controlset *cs, char *name)
  97. .sp 0.3
  98. Control* createkeyboard(Controlset *cs, char *name)
  99. .sp 0.3
  100. Control* createlabel(Controlset *cs, char *name)
  101. .sp 0.3
  102. Control* createmenu(Controlset *cs, char *name)
  103. .sp 0.3
  104. Control* createradiobutton(Controlset *cs, char *name)
  105. .sp 0.3
  106. Control* createrow(Controlset*, char*)
  107. .sp 0.3
  108. Control* createscribble(Controlset *cs, char *name)
  109. .sp 0.3
  110. Control* createslider(Controlset *cs, char *name)
  111. .sp 0.3
  112. Control* createstack(Controlset*, char*)
  113. .sp 0.3
  114. Control* createtab(Controlset*, char *)
  115. .sp 0.3
  116. Control* createtext(Controlset *cs, char *name)
  117. .sp 0.3
  118. Control* createtextbutton(Controlset *cs, char *name)
  119. .sp 0.3
  120. void closecontrol(Control *c)
  121. .sp 0.3
  122. int ctlprint(Control*, char*, ...);
  123. .sp 0.3
  124. void ctlerror(char *fmt, ...)
  125. .sp 0.3
  126. Control* controlcalled(char *name)
  127. .sp 0.3
  128. void controlwire(Control *c, char *cname, Channel *ch)
  129. .sp 0.3
  130. void activate(Control *c)
  131. .sp 0.3
  132. void deactivate(Control *c)
  133. .sp 0.3
  134. void resizecontrolset(Controlset *cs)
  135. .sp 0.3
  136. void* ctlmalloc(uint n)
  137. .sp 0.3
  138. void* ctlrealloc(void *p, uint n)
  139. .sp 0.3
  140. char* ctlstrdup(char *s)
  141. .sp 0.3
  142. int ctldeletequits;
  143. .EE
  144. .SH DESCRIPTION
  145. This library provides a set of interactive
  146. controls for graphical displays: buttons, sliders, text entry boxes, and so on.
  147. It also provides aggregator
  148. .BR Control s:
  149. boxes, columns, rows and stacks of
  150. .BR Control s.
  151. A stack is a collection of co-located
  152. .BR Control s,
  153. of which one is normally visible.
  154. A
  155. .B Controlset
  156. collects a group of
  157. .BR Control s
  158. that share mouse and keyboard. Each
  159. .B Controlset
  160. has a separate thread of control that processes keyboard and mouse events as
  161. well as commands to be passed on to the
  162. .BR Control s.
  163. Since each
  164. .B Controlset
  165. uses a thread, programs using the control library must
  166. be linked with the thread library,
  167. .IR thread (2).
  168. .PP
  169. .BR Control s
  170. are manipulated by reading and writing to the control channel,
  171. .BR ctl ,
  172. of their
  173. .BR Controlset .
  174. .BR Channel s
  175. are defined in
  176. .IR thread (2).
  177. Each
  178. .B Control
  179. has two output channels:
  180. .B Event
  181. delivers messages about actions within the control (such as a button press) and
  182. .B data
  183. delivers (if requested by an appropriate write to
  184. .BR ctl )
  185. control-specific data such as the contents of a field.
  186. .PP
  187. The library provides a simple mechanism for automatic layout:
  188. the minimum and maximum sizes of each simple control can be specified.
  189. .BR Boxbox ,
  190. .BR row ,
  191. .B column
  192. and
  193. .B stack
  194. .BR Control s
  195. then use these sizes to lay out their constituent
  196. .BR Control s
  197. when called upon
  198. to do so. See the description of these grouping
  199. .BR Control s
  200. for further details.
  201. .SS "Message format
  202. All messages are represented as
  203. .SM UTF\c
  204. -8
  205. text.
  206. Numbers are formatted in decimal, and strings are transmitted in the
  207. quoted form of
  208. .IR quote (2).
  209. .PP
  210. Messages sent to a
  211. .B Controlset
  212. are of the form,
  213. .IP
  214. .IR sender :
  215. .I destination
  216. .I verb
  217. .RI [ argument
  218. \&... ]
  219. .LP
  220. The sender (and the colon following it) may be ommitted.
  221. For example, the initial field of a text entry control called
  222. .I entry
  223. could be set by sending the message,
  224. .IP
  225. .B "entry value 'Hello, world!'
  226. .PP
  227. to its
  228. .BR Controlset 's
  229. .B ctl
  230. file.
  231. This message contains the verb
  232. .B value
  233. and the single argument
  234. .B "Hello, world!"
  235. .PP
  236. To make it easy to write messages, the function
  237. .IR chanprint
  238. (see
  239. .IR thread (2))
  240. can be used to print formatted text to a
  241. .BR Controlset 's
  242. channel.
  243. .PP
  244. The
  245. .B %q
  246. and
  247. .B %Q
  248. formats are convenient for properly quoting string arguments,
  249. as in
  250. .IP
  251. .EX
  252. chanprint(e->event, "value %q", "Don't touch!");
  253. .EE
  254. .PP
  255. It is wise to use
  256. .B %q
  257. always instead of
  258. .BR %s
  259. when sending messages, and avoid dealing with the quoting explicitly.
  260. In the other direction,
  261. .B tokenize
  262. (see
  263. .IR getfields (2))
  264. parses these messages and interprets the quotes correctly.
  265. .PP
  266. The destination of a message can be a named control, or a set of controls identified
  267. by name or type. The command
  268. .IP
  269. .B "'entry slider' show
  270. .PP
  271. (note the quotation) sends the `show' command to the entry named
  272. .I entry
  273. and all controls of type
  274. .IR slider .
  275. If there were a control whose name was
  276. .I slider
  277. that control would also be shown.
  278. .LP
  279. \f2
  280. Note that we are still experimenting with destination names.
  281. One proposal is that
  282. a destination of the form
  283. \fR"`name1 name2 ⋯ type1 type2 ⋯'\fP
  284. selects all controls of the named types in the control hierarchies (of columns, rows and
  285. stacks) whose names precede the types.
  286. .LP
  287. Messages sent by a control on its
  288. .B event
  289. channel are of the form
  290. .IP
  291. .IB sender :
  292. .IB event
  293. .PP
  294. The
  295. .I sender
  296. is the name of the control sending the message;
  297. the
  298. .I event
  299. describes the event. Its format can often be controlled by setting the
  300. .BR Control 's
  301. .IR "format string" .
  302. For example, when the user types a newline at a text entry
  303. .B Control
  304. named
  305. .BR entry,
  306. the control sends the message
  307. .IP
  308. .B entry:\ value\ 'Hello\ again!'
  309. on its
  310. .B event
  311. channel.
  312. .SS "Initialization and Control sets
  313. After
  314. .B initdraw
  315. (see
  316. .IR graphics (2))
  317. is called,
  318. the function
  319. .I initcontrols
  320. should be called to initialize the library.
  321. It calls
  322. .I quotefmtinstall
  323. to install the
  324. .B %q
  325. and
  326. .B %Q
  327. formats; see
  328. .IR quote (2).
  329. .PP
  330. Each control is represented by a
  331. .B Control
  332. data structure and is associated with a
  333. .B Controlset
  334. that groups a set of controls sharing mouse, keyboard, and display.
  335. Most applications will need only one
  336. .BR Controlset ;
  337. only those with multiple windows or unusual configurations will need
  338. more than one.
  339. The function
  340. .I newcontrolset
  341. creates a
  342. .BR Controlset .
  343. Its arguments are the image (usually a window)
  344. on which its controls will appear, typically the
  345. .B screen
  346. variable in the draw library, and three channels:
  347. .BR kc ,
  348. a channel of
  349. .B Runes
  350. from the keyboard;
  351. .BR mc ,
  352. a channel of
  353. .B Mouse
  354. structures from the mouse;
  355. and
  356. .BR rc ,
  357. a channel of
  358. .B int
  359. that indicates when the window has been resized.
  360. Any of the channels may be nil,
  361. in which case
  362. .I newcontrolset
  363. will call
  364. .B initkeyboard
  365. and/or
  366. .B initmouse
  367. (see
  368. .IR keyboard (2)
  369. and
  370. .IR mouse (2))
  371. to initialize the keyboard and mouse
  372. and connect them to the control set.
  373. The mouse and resize channels must both be nil or both be non-nil.
  374. .PP
  375. The function
  376. .I closecontrolset
  377. frees all the controls in the control set and tears down all the associated threads.
  378. It does not close the mouse and keyboard.
  379. .PP
  380. The public elements of a
  381. .B Controlset
  382. are the flag
  383. .BR clicktotype ,
  384. and the
  385. .I ctl
  386. and
  387. .I data
  388. channels.
  389. .PP
  390. .I Clicktotype
  391. is zero by default.
  392. If it is set to non-zero, the controls
  393. in the set will acquire `focus' by the click-to-type paradigm.
  394. Otherwise, focus is always given to the control under the mouse.
  395. .PP
  396. Commands for controls are sent through the
  397. .BR Controlset 's
  398. .I ctl
  399. channel.
  400. One special command is recognized by the
  401. .B Controlset
  402. itself: Sending
  403. the string
  404. .B sync
  405. to the
  406. .I ctl
  407. channel causes tha string to be echoed to the
  408. .BR Controlset 's
  409. .I data
  410. channel when all commands up to the
  411. .I sync
  412. command have been processed. The string is
  413. allocated and must be freed (see
  414. .IR malloc (2)).
  415. Synchronization is necessary between sending a command, for example, to resize
  416. all controls, and using their
  417. .I rect
  418. fields.
  419. .PP
  420. The function
  421. .I resizecontrolset
  422. must be provided by the user.
  423. When the associated window is resized, the library will call
  424. .I resizecontrolset
  425. with the affected
  426. .BR Controlset ;
  427. the function should reconnect to and redraw the window.
  428. .PP
  429. If all windows are organized in a hierachy of
  430. .IR boxboxes ,
  431. .IR columns ,
  432. .I rows
  433. and
  434. .IR stacks ,
  435. and minimum and maximum sizes have already been supplied, only
  436. the top control needs to be resized (see the
  437. .I rect
  438. command below).
  439. .SS "Fonts and images
  440. Fonts and images must be given names so they may be referenced
  441. in messages.
  442. The functions
  443. .I namectlfont
  444. and
  445. .I namectlimage
  446. associate a (unique) name with the specified font or image.
  447. The association is removed by
  448. .I freectlfont
  449. and
  450. .IR freectlimage .
  451. The font or image is not freed by these functions, however.
  452. .PP
  453. The function
  454. .I initcontrols
  455. establishes name bindings for all the colors mentioned in
  456. .BR <draw.h> ,
  457. such as
  458. .BR black ,
  459. .BR white ,
  460. .BR red ,
  461. .BR yellow ,
  462. etc., as well as masks
  463. .B transparent
  464. and
  465. .BR opaque .
  466. It also sets the name
  467. .B font
  468. to refer to the default
  469. .B font
  470. variable set up by
  471. .BR initdraw .
  472. .SS Creation
  473. Each type of control has an associated creation function:
  474. .IR createbutton ,
  475. .IR createentry ,
  476. etc.,
  477. whose arguments are the
  478. .B Controlset
  479. to attach it to and a globally unique name for it.
  480. A control may be destroyed by calling
  481. .IR closecontrol .
  482. .PP
  483. The function
  484. .I controlcalled
  485. returns a pointer to the
  486. .B Control
  487. with the given
  488. .IR name ,
  489. or nil if no such control exists.
  490. .SS Configuration
  491. After a control is created, it must be configured using the control-specific
  492. commands documented below.
  493. Commands are sent to the
  494. .B ctl
  495. channel of the
  496. .BR Controlset .
  497. Multiple commands may be sent in a single message; newline characters
  498. separate commands.
  499. For an example, see the implementation of
  500. .I resizecontrolset
  501. in the
  502. .B EXAMPLES
  503. section.
  504. Note that newline is a separator, not a terminator; the final command
  505. does not need a newline.
  506. .PP
  507. Messages sent to the
  508. .I ctl
  509. channel are delivered to all controls that match the
  510. .I destination
  511. field. This field is a set of names separated by spaces, tabs or newlines.
  512. A control matches the destination if its name or its type is among the set.
  513. .PP
  514. The recipient of a message ignores the initial
  515. .IB sender :
  516. field of the message, if present,
  517. making it possible to send messages generated on an
  518. .B event
  519. channel directly to another control's
  520. .B ctl
  521. channel.
  522. .SS Activation
  523. When they are created, controls are disabled: they do not respond to
  524. user input.
  525. Not all controls need to be responsive;
  526. for example, labels are static and a text display
  527. might show a log of messages but not be useful to edit.
  528. But buttons, entry boxes, and other text displays should be active.
  529. .PP
  530. To enable a control, call the
  531. .I activate
  532. function, which
  533. specifies that the
  534. .B Control
  535. .I c
  536. should respond to mouse and keyboard events;
  537. .I deactivate
  538. turns it off again.
  539. .PP
  540. Controls can be either
  541. .I revealed
  542. (default) or
  543. .IR hidden .
  544. When a control is hidden, it will not receive mouse or keyboard events
  545. and state changes or
  546. .I show
  547. commands will be ignored until the control is once again
  548. .IR revealed .
  549. Control hiding is particularly useful when different controls are overlayed,
  550. revealing only the `top' one.
  551. .PP
  552. The function
  553. .I controlwire
  554. permits rearrangement of the channels associated with a
  555. .BR Control .
  556. The channel
  557. .I cname
  558. (one of
  559. \f5"data"\fP
  560. or
  561. \f5"event"\fP)
  562. of
  563. .B Control
  564. .I c
  565. is reassigned to the channel
  566. .IR ch .
  567. There are several uses for this operation:
  568. one may reassign all the
  569. .B event
  570. channels to a single channel, in effect multiplexing all the events onto
  571. a single channel;
  572. or connect the
  573. .B event
  574. channel of a slider to the
  575. .B ctl
  576. channel for delivery to a text display (after setting the format for the slider's messages
  577. to name the destination control and the appropriate syntax for the rest of the command)
  578. to let the slider act as a scroll bar for the text without rerouting the messages explicitly.
  579. .SS Controls
  580. The following sections document the individual controls in alphabetical order.
  581. The layout of each section is a brief description of the control's behavior,
  582. followed by the messages it sends on
  583. .BR event ,
  584. followed by the messages it accepts via the
  585. .B ctl
  586. channel.
  587. The
  588. .B event
  589. messages are triggered
  590. .I only
  591. by mouse or keyboard action; messages to the
  592. .B ctl
  593. file do not cause events to be generated.
  594. .PP
  595. All controls accept the following messages:
  596. .TF \fLreveal
  597. .TP
  598. .BI rect " minx miny maxx maxy
  599. Set the bounding rectangle for the control on the display.
  600. The syntax generated by the
  601. .B %R
  602. print format of the draw library is also acceptable for the coordinates.
  603. .TP
  604. .BR size " [ \f2minΔx minΔy maxΔx maxΔy\fP ]
  605. Set the minimum and maximum size for automatic layout in
  606. .IR columns ,
  607. .I rows
  608. and
  609. .IR stacks .
  610. Without its four arguments, this command is ignored by primitive controls
  611. and used by grouping controls to calculate their minimum and maximum sizes
  612. by examining those of their constituent members.
  613. If all primitive controls have been assigned a size, a single size request addressed
  614. to the top of a layout hierarchy will assign sizes to all grouping
  615. .BR Control s.
  616. .TP
  617. .B hide
  618. Disable drawing of the control and ignore mouse and keyboard events
  619. until the control is once again revealed.
  620. Grouping
  621. .BR Control s
  622. (\f2column\fP, \f2row\fP, and \f2stack\fP) pass the
  623. request down to their constituent
  624. .BR Control s.
  625. .TP
  626. .B reveal
  627. This is the opposite of
  628. .BR hide :
  629. the
  630. .B Control
  631. is displayed and mouse and keyboard operations resume.
  632. Grouping
  633. .BR Control s
  634. (\f2column\fP, \f2row\fP, and \f2stack\fP) pass the
  635. request down to their constituent
  636. .BR Control s.
  637. The
  638. .B reveal
  639. command for
  640. .I stacks
  641. takes an optional argument naming the
  642. .B Control
  643. to be revealed; all
  644. other
  645. .BR Control s
  646. will be hidden.
  647. .TP
  648. .B show
  649. Display the
  650. .B Control
  651. on its screen if not hidden.
  652. Some actions will also cause the
  653. .BR Control s
  654. to show themselves automatically
  655. (but never when the
  656. .B control
  657. is hidden).
  658. Grouping
  659. .BR Control s
  660. (\f2column\fP, \f2row\fP, and \f2stack\fP) pass the
  661. request down to their constituent
  662. .BR Control s.
  663. .PD
  664. .PP
  665. Many messages are common between multiple
  666. .BR Control s.
  667. Such messages are described in detail here to avoid repetition.
  668. In the individual descriptions, only the syntax is presented.
  669. .TF "\fLformat fmt"
  670. .TP
  671. .BI align " n
  672. Specify the alignment of (some part of) the
  673. .BR Control 's
  674. display within its rectangle.
  675. For textual
  676. .BR control s,
  677. the alignment specifies where the text should appear.
  678. For multiline text, the alignment refers to each line within its box,
  679. and only the
  680. horizontal part is honored.
  681. For other
  682. .BR Control s,
  683. the alignment affects the appearance of the display in
  684. a reasonable way.
  685. The valid alignments are words with obvious interpretations:
  686. .BR upperleft ,
  687. .BR uppercenter ,
  688. .BR upperright ,
  689. .BR centerleft ,
  690. .BR center ,
  691. .BR centerright ,
  692. .BR lowerleft,
  693. .BR lowercenter ,
  694. and
  695. .BR lowerright .
  696. .TP
  697. .BI border " n
  698. Inset the
  699. .B Control
  700. (or separate constituent
  701. .BR Control s
  702. in
  703. .IR boxbox ,
  704. .I column
  705. and
  706. .I row
  707. .BR Control s
  708. after the next
  709. .I rect
  710. command) within its rectangle by
  711. .I n
  712. pixels, default zero.
  713. .TP
  714. .BI bordercolor " name
  715. Paint the border of the control with the named color, default black.
  716. .TP
  717. .BI focus " n
  718. The
  719. .B Control
  720. now has (if
  721. .I n
  722. is non-zero) or does not have ( if
  723. .I n
  724. is zero) focus.
  725. Most
  726. .BR Control s
  727. ignore the message; there are plans to make them react.
  728. .TP
  729. .BI format " fmt
  730. Set the format of `value' messages sent on the
  731. .B event
  732. channel.
  733. By default, the format is
  734. .B \&"%q: value %q"
  735. for string-valued
  736. .BR Control s,
  737. .B \&"%q: value %d"
  738. for integer-valued
  739. .B Control s
  740. such as buttons,
  741. and
  742. .B \&"%q: value 0x%x"
  743. for the keyboard and scribble
  744. .BR Control s.
  745. The
  746. .B %q
  747. prints the name of the
  748. .BR Control ;
  749. the rest the value.
  750. Any supplied format string must be type-equivalent to the default for that
  751. .BR Control .
  752. .TP
  753. .BI image " name
  754. .TP
  755. .BI light " name
  756. .TP
  757. .BI mask " name
  758. Many controls set a background image or color for display.
  759. The
  760. .B image
  761. message sets the image.
  762. The
  763. .B mask
  764. and
  765. .B light
  766. images together specify how the
  767. .B Control
  768. shows it is enabled:
  769. the
  770. .B light
  771. is printed through the
  772. .B mask
  773. when the state is `on' or `pressed'.
  774. Otherwise, the image appears unmodified.
  775. The default image is white; mask opaque; light yellow.
  776. .TP
  777. .BI font " name
  778. .TP
  779. .BI textcolor " name
  780. These commands set the font and color for displaying text.
  781. The defaults are the default
  782. .B font
  783. set up by the draw library, and black.
  784. .TP
  785. .BI value " v
  786. Set the value of the
  787. .BR Control .
  788. Textual images accept an arbitrary string;
  789. others an integral value.
  790. .SS Box
  791. A box is a trivial control that does nothing more than pass keyboard, mouse,
  792. and focus messages back on its
  793. .B event
  794. channel.
  795. Keyboard characters are sent in the format
  796. .IP
  797. .EX
  798. boxname: key 0x\f2nn
  799. .EE
  800. .PP
  801. where
  802. .I nn
  803. is the hexadecimal value of the character.
  804. Mouse messages are sent in the format
  805. .IP
  806. .EX
  807. boxname: mouse [\f2x\fP \f2y\fP] \f2but\fP \f2msec\fP
  808. .EE
  809. .PP
  810. where
  811. .IR x ,
  812. .IR y ,
  813. .IR but ,
  814. and
  815. .I msec
  816. are the various fields of the
  817. .B Mouse
  818. structure.
  819. The focus message is just
  820. .IP
  821. .EX
  822. boxname: focus \f2n\f1
  823. .EE
  824. .PP
  825. where
  826. .I n
  827. is 0 if the box has lost focus, 1 if it has acquired it.
  828. .PP
  829. The box displays within its rectangle
  830. an image, under mask, with specified alignment.
  831. The control messages it accepts are:
  832. .TF "\fLalign a"
  833. .TP
  834. .BI align " a
  835. Controls the placement of the image in the rectangle (unimplemented).
  836. .TP
  837. .BI border " b
  838. .TP
  839. .BI bordercolor " name
  840. .TP
  841. .BI focus " n
  842. .TP
  843. .BI hide
  844. .TP
  845. .BI image " name
  846. .TP
  847. .BI rect " minx miny maxx maxy
  848. .TP
  849. .BI reveal
  850. .TP
  851. .BI show
  852. .TP
  853. .BI size " minΔx minΔy maxΔx maxΔy
  854. .PD
  855. .SS Boxbox
  856. A
  857. .B boxbox
  858. allows a set of controls (``boxes'')
  859. to be displayed in rows and columns within the
  860. rectangle of the
  861. .IR boxbox .
  862. The maximum of the minimum heights of the constituent controls determines the
  863. number of rows to be displayed. The number of columns is the minimum that
  864. allows all
  865. .BR Control s
  866. to be displayed. This aggregator works well for collections
  867. of buttons, labels, or textbuttons that all have a fixed height.
  868. .TF "\fLadd name ..."
  869. .TP
  870. .BI add " name ...
  871. adds the named control to the box of controls. The display order
  872. is determined by the order of adding. The first named control is top left,
  873. the second goes below it, etc.
  874. It is possible to add one control to multiple grouping controls but
  875. the layout of the result will be quite unpredictable.
  876. .TP
  877. .BI border " width
  878. .TP
  879. .BI bordercolor " color
  880. .TP
  881. .B hide
  882. This command is passed on to the member controls.
  883. .TP
  884. .BR image " color
  885. Background color displayed between member controls.
  886. .TP
  887. .B reveal
  888. This command is passed on to the member controls.
  889. .TP
  890. .BI separation " width
  891. Set the separation between member controls to
  892. .I n
  893. pixels.
  894. .TP
  895. .BI rect " minx miny maxx maxy
  896. The member controls are layed out within the given rectangle according to
  897. the minimum and maximum sizes given. If the rectangle is not large enough
  898. for the minimum a fatal error is currently generated.
  899. If the controls at their maximum size are not big enough to fit, they are top-left justified
  900. at their maximum size in the space given.
  901. Otherwise, controls will get their minimum size and be enlarged proportional
  902. to the extra size given by the maximum until they fit given rectangle.
  903. The members are separated by borders of the width established by
  904. .IR borderwidth .
  905. .TP
  906. .BI remove " name
  907. Remove the named
  908. control from the box.
  909. .TP
  910. .B show
  911. This command is passed on to the member controls.
  912. Show also (re)displays background and borders.
  913. .TP
  914. .BR size " \f2minΔx minΔy maxΔx maxΔy\fP
  915. .PD
  916. .SS Button
  917. A button is a simple control that toggles its state when mouse button 1 is pressed on its rectangle.
  918. Each state change triggers an event message:
  919. .IP
  920. .EX
  921. buttonname: value \f2n\fP
  922. .EE
  923. where
  924. .I n
  925. encodes the mouse buttons used to make the selection.
  926. .PP
  927. The button displays an image (which may of course be a simple color)
  928. and illuminates in the standard way when it is `on'.
  929. The control messages it accepts are:
  930. .TF "\fLborder b"
  931. .TP
  932. .BI align " a
  933. Controls the placement of the image in the rectangle (unimplemented).
  934. .TP
  935. .BI border " b
  936. .TP
  937. .BI bordercolor " name
  938. .TP
  939. .BI focus " n
  940. .TP
  941. .BI format " fmt
  942. .TP
  943. .BI hide
  944. .TP
  945. .BI image " name
  946. .TP
  947. .BI light " name
  948. .TP
  949. .BI mask " name
  950. .TP
  951. .BI rect " minx miny maxx maxy
  952. .TP
  953. .BI reveal
  954. .TP
  955. .B show
  956. .TP
  957. .BI size " minΔx minΔy maxΔx maxΔy
  958. .TP
  959. .BI value " n
  960. Set the button to `on' (if
  961. .I n
  962. is non-zero) or `off' (if
  963. .I n
  964. is zero).
  965. .PD
  966. .SS Column
  967. A column is a grouping control which lays out its members vertically,
  968. from top to bottom. Currently, columns ignore mouse and keyboard
  969. events, but there are plans to allow dragging the borders (when they
  970. have non-zero width) between constituent members.
  971. .TF "\fLadd name .."
  972. .TP
  973. .BI add " name ...
  974. adds the named control to the column of controls. The vertical order
  975. is determined by the order of adding. The first named control goes at
  976. the top. It is possible to add one control to multiple grouping controls but
  977. the layout of the result will be quite unpredictable.
  978. .TP
  979. .BI border " width
  980. Set the border between members to the width given.
  981. .TP
  982. .BI bordercolor " color
  983. .TP
  984. .B hide
  985. .TP
  986. .BR image " color
  987. Background color displayed between member controls.
  988. .TP
  989. .B reveal
  990. .TP
  991. .BI separation " width
  992. Set the separation between member controls to
  993. .I n
  994. pixels.
  995. .TP
  996. .B show
  997. These three commands are passed on to the member controls.
  998. Show also (re)displays the borders between members.
  999. .TP
  1000. .BI rect " minx miny maxx maxy
  1001. The member controls are layed out within the given rectangle according to
  1002. the minimum and maximum sizes given. If the rectangle is not large enough
  1003. for the minimum a fatal error is currently generated. However, see the example
  1004. at the end of this man page.
  1005. If the controls at their maximum size are not big enough to fit, they are centered
  1006. at their maximum size in the space given.
  1007. Otherwise, controls will get their minimum size and be enlarged proportional
  1008. to the extra size given by the maximum until they fit given rectangle.
  1009. The members are separated by borders of the width established by
  1010. .IR borderwidth .
  1011. .TP
  1012. .BI remove " name
  1013. Remove the named control from the column.
  1014. .TP
  1015. .BR size " [ \f2minΔx minΔy maxΔx maxΔy\fP ]
  1016. Without arguments, this command computes the minimum and
  1017. maximum size of a column by adding the minimum and maximum
  1018. heights to set
  1019. .I minΔy
  1020. and
  1021. .IR maxΔy ,
  1022. and it finds the largest minimum and maximum widths to set
  1023. .I minΔy
  1024. and
  1025. .IR maxΔy .
  1026. When called with arguments, it simply sets the minimum and maximum
  1027. sizes to those given.
  1028. .PD
  1029. .SS Entry
  1030. The entry control manages a single line of editable text.
  1031. When the user hits a carriage return anywhere
  1032. in the text, the control generates the event message,
  1033. .IP
  1034. .EX
  1035. entryname: value \f2s\fP
  1036. .EE
  1037. .PP
  1038. with
  1039. .I s
  1040. the complete text of the entry box.
  1041. .PP
  1042. The cursor can be moved by clicking button 1; at the moment,
  1043. there is no way to select characters, only a typing position.
  1044. Some control characters have special actions:
  1045. control-H (backspace) deletes the character before the cursor;
  1046. control-U clears the line; and
  1047. control-V pastes the snarf buffer at the typing position.
  1048. Most important, carriage return sends the text to the event channel.
  1049. .PP
  1050. To enter passwords and other secret text without displaying the
  1051. contents, set the font to one in which all characters are the same.
  1052. The easiest way to do this is to make a font containing only one character,
  1053. at position 0 (NUL), since that position is used to render all
  1054. characters not otherwise defined in the font (see
  1055. .IR draw (2)).
  1056. The file
  1057. .B /lib/font/bit/lucm/passwd.9.font
  1058. defines such a font.
  1059. .PP
  1060. The control messages the entry control accepts are:
  1061. .TF "\fLborder b"
  1062. .TP
  1063. .BI align " a
  1064. Controls the placement of the text in the rectangle.
  1065. .TP
  1066. .BI border " b
  1067. .TP
  1068. .BI bordercolor " name
  1069. .TP
  1070. .BI data
  1071. After receiving this message, the entry will send its value to its
  1072. .B data
  1073. channel as an unadorned, unquoted string.
  1074. .TP
  1075. .BI focus " n
  1076. When it receives focus, the entry box displays a typing cursor.
  1077. When it does not have focus, the cursor is not displayed.
  1078. .TP
  1079. .BI font " name
  1080. .TP
  1081. .BI format " fmt
  1082. .TP
  1083. .BI hide
  1084. .TP
  1085. .BI image " name
  1086. .TP
  1087. .BI rect " minx miny maxx maxy
  1088. .TP
  1089. .B reveal
  1090. .TP
  1091. .B show
  1092. .TP
  1093. .BI size " minΔx minΔy maxΔx maxΔy
  1094. .TP
  1095. .BI textcolor " name
  1096. .TP
  1097. .BI value " s
  1098. Set the string displayed in the entry box.
  1099. .PD
  1100. .SS Keyboard
  1101. The keyboard control implements a simulated keyboard useful on palmtop devices.
  1102. Keystrokes, generated by mouse button 1 on the simulated keys,
  1103. are sent as event messages:
  1104. .IP
  1105. .EX
  1106. keyboardname: value 0x\f2nn\f1
  1107. .EE
  1108. .PP
  1109. where
  1110. .I nn
  1111. is the hexadecimal Unicode value of the character.
  1112. Shift, control, and caps lock are handled by the keyboard control itself;
  1113. shift and control affect only the next regular keystroke.
  1114. The Alt key is unimplemented; it will become equivalent to the standard Plan 9
  1115. key for synthesizing non-ASCII characters.
  1116. .PP
  1117. There are two special keys,
  1118. .B Scrib
  1119. and
  1120. .BR Menu ,
  1121. which return values
  1122. .B 0x10000
  1123. and
  1124. .BR 0x10001 .
  1125. .PP
  1126. The image, mask, light rules are used to indicate that a key is pressed,
  1127. but to aid clumsy fingers the keystroke is not generated until the key is released,
  1128. so it is possible to slide the pointer to a different key to correct for bad aim.
  1129. .PP
  1130. The control messages the keyboard accepts are:
  1131. .TF "\fLfont"
  1132. .TP
  1133. .BI border " b
  1134. .TP
  1135. .BI bordercolor " name
  1136. .TP
  1137. .BI focus " n
  1138. .TP
  1139. .BI font " name1 name2
  1140. Sets the font for the keys.
  1141. If only one font is named, it is used for all keys.
  1142. If two are named, the second is used for key caps with special names such
  1143. as Shift and Enter.
  1144. (Good choices on the Bitsy are
  1145. .B /lib/font/bit/lucidasans/boldlatin1.6.font
  1146. for the first and
  1147. .B /lib/font/bit/lucidasans/unicode.6.font
  1148. for the second argument.)
  1149. If neither is specified, both will be set to the default global font.
  1150. .TP
  1151. .BI format " fmt
  1152. .TP
  1153. .BI hide
  1154. .TP
  1155. .BI image " name
  1156. .TP
  1157. .BI light " name
  1158. .TP
  1159. .BI mask " name
  1160. .TP
  1161. .BI rect " minx miny maxx maxy
  1162. .TP
  1163. .BI reveal
  1164. .TP
  1165. .B show
  1166. .TP
  1167. .BI size " minx miny maxx maxy
  1168. .PD
  1169. .SS Label
  1170. A label is like a textbutton
  1171. .RI ( q.v. )
  1172. that does not react, but whose value is the text it displays.
  1173. The control messages it accepts are:
  1174. .TF "\fLvalue s"
  1175. .TP
  1176. .BI align " a
  1177. Controls the placement of the image in the rectangle.
  1178. .TP
  1179. .BI border " b
  1180. .TP
  1181. .BI bordercolor " name
  1182. .TP
  1183. .BI focus " n
  1184. .TP
  1185. .BI font " name
  1186. .TP
  1187. .BI hide
  1188. .TP
  1189. .BI image " name
  1190. .TP
  1191. .BI rect " minx miny maxx maxy
  1192. .TP
  1193. .BI reveal
  1194. .TP
  1195. .B show
  1196. .TP
  1197. .BI size " minx miny maxx maxy
  1198. .TP
  1199. .BI textcolor " name
  1200. .TP
  1201. .BI value " s
  1202. The value is a string that can be modified only by sending this message to the
  1203. .B ctl
  1204. file.
  1205. .PD
  1206. .SS Menu
  1207. A menu is a pop-up window containing a set of textual selections.
  1208. When a selection is made, it removes itself from the screen and reports the selection
  1209. by value:
  1210. .IP
  1211. .EX
  1212. menuname: value \f2n\fP
  1213. .EE
  1214. .PP
  1215. If no selection is made, no message is reported.
  1216. Because it creates a window, programs using a menu must have their
  1217. .B screen
  1218. variable (see
  1219. .IR graphics (2)
  1220. and
  1221. .IR window (2))
  1222. set up to be refreshed properly.
  1223. The easiest way to do this is to call
  1224. .B getwindow
  1225. with refresh argument
  1226. .B Refbackup
  1227. (see
  1228. .IR graphics (2));
  1229. most programs use
  1230. .BR Refnone .
  1231. .PP
  1232. The control messages accepted by a menu are:
  1233. .TF "\fLwindow n"
  1234. .TP
  1235. .BI add " text
  1236. Add a line of
  1237. .I text
  1238. to the end of the menu.
  1239. .TP
  1240. .BI align " a
  1241. Controls the left-right placement of the text in its rectangle.
  1242. .TP
  1243. .BI border " b
  1244. .TP
  1245. .BI bordercolor " name
  1246. .TP
  1247. .BI focus " n
  1248. .TP
  1249. .BI font " name
  1250. .TP
  1251. .BI format " fmt
  1252. .TP
  1253. .BI hide
  1254. .TP
  1255. .BI image " name
  1256. .TP
  1257. .BI rect " minx miny maxx maxy
  1258. .TP
  1259. .BI reveal
  1260. .TP
  1261. .BI size " minx miny maxx maxy
  1262. Only the origin of the rectangle is significant; menus calculate the appropriate size.
  1263. .TP
  1264. .BI selectcolor " name
  1265. Set the color in which to highlight selected lines; default yellow.
  1266. .TP
  1267. .BI selecttextcolor " name
  1268. Set the color in which to draw the text in selected lines; default black.
  1269. .TP
  1270. .B show
  1271. Display the menu. Not usually needed unless the menu is changed while visible; use
  1272. .I window
  1273. instead.
  1274. .TP
  1275. .B window
  1276. .TP
  1277. .BI window " n
  1278. With no arguments, toggle the menu's visibility; otherwise make it visible (1) or invisible (0).
  1279. When the selection is made, the menu will remove its window automatically.
  1280. .PD
  1281. .SS Radiobutton
  1282. The radiobutton assembles a group of buttons or textbuttons into a
  1283. single control with a numeric value.
  1284. Its value is \-1 if none of the constituent buttons is pressed; otherwise
  1285. it is the index, starting at zero, of the button that is pressed.
  1286. Only one button may be pressed; the radiobutton manipulates its
  1287. buttons to guarantee this.
  1288. State changes trigger an event message:
  1289. .IP
  1290. .EX
  1291. radiobuttonname: value \f2n\fP
  1292. .EE
  1293. .PP
  1294. Buttons are added to the radio button using the
  1295. .B add
  1296. message; there is no way to remove them, although they may be turned off
  1297. independently using
  1298. .IR deactivate .
  1299. The index reported in the value is defined by the order
  1300. in which the buttons are added.
  1301. The constituent buttons should be configured and layed out in the usual way;
  1302. the rectangle of the radiobutton is used only to `catch' mouse events and
  1303. should almost always correspond to the bounding box of the constituent
  1304. buttons.
  1305. In other words, the geometry is not maintained automatically.
  1306. .PP
  1307. The control messages the radiobutton accepts are:
  1308. .TF "\fLadd name"
  1309. .TP
  1310. .BI add " name
  1311. Add the control with the specified
  1312. .I name
  1313. to the radiobutton.
  1314. .TP
  1315. .BI focus " n
  1316. .TP
  1317. .BI format " fmt
  1318. .TP
  1319. .BI hide
  1320. .TP
  1321. .BI rect " minx miny maxx maxy
  1322. .TP
  1323. .BI reveal
  1324. .TP
  1325. .BI size " minx miny maxx maxy
  1326. .TP
  1327. .B show
  1328. .TP
  1329. .BI value " n
  1330. .PD
  1331. .SS Row
  1332. A row groups a number of member controls left to right in a rectangle.
  1333. Rows behave exactly like columns with the roles of
  1334. .I x
  1335. and
  1336. .I y
  1337. interchanged.
  1338. .PP
  1339. The control messages it accepts are:
  1340. .TF "\fLfont"
  1341. .TP
  1342. .BI add " name ...
  1343. .TP
  1344. .BI border " width
  1345. .TP
  1346. .BI bordercolor " color
  1347. .TP
  1348. .BI hide
  1349. .TP
  1350. .BR image " color
  1351. .TP
  1352. .BI rect " minx miny maxx maxy
  1353. .TP
  1354. .BI remove " name
  1355. .TP
  1356. .BI reveal
  1357. .TP
  1358. .BI separation " width
  1359. .TP
  1360. .BI show
  1361. .TP
  1362. .BR size " [ \f2minΔx minΔy maxΔx maxΔy\fP ]
  1363. .PD
  1364. .SS Scribble
  1365. The scribble control provides a region in which strokes drawn with mouse button
  1366. 1 are interpreted as characters in the manner of
  1367. .IR scribble (2).
  1368. In most respects, including the format of its event messages, it is equivalent
  1369. to a keyboard control.
  1370. .PP
  1371. The control messages it accepts are:
  1372. .TF "\fLlinecolor \fIname\f(CW "
  1373. .TP
  1374. .BI align " a
  1375. Controls the placement of the image in the rectangle (unimplemented).
  1376. .TP
  1377. .BI border " b
  1378. .TP
  1379. .BI bordercolor " name
  1380. .TP
  1381. .BI focus " n
  1382. .TP
  1383. .BI font " name
  1384. Used to display the indicia.
  1385. .TP
  1386. .BI hide
  1387. .TP
  1388. .BI image " name
  1389. .TP
  1390. .BI linecolor " name
  1391. The color in which to draw the strokes; default black.
  1392. .TP
  1393. .BI rect " minx miny maxx maxy
  1394. .TP
  1395. .BI reveal
  1396. .TP
  1397. .BI size " minx miny maxx maxy
  1398. .TP
  1399. .B show
  1400. .PD
  1401. .SS Stack
  1402. A stack groups a number of member controls in the same shared rectangle.
  1403. Only one of these controls will be visible (revealed), the others are hidden.
  1404. .PP
  1405. The control messages it accepts are:
  1406. .TF "\fLreveal [\f2n\fP]"
  1407. .TP
  1408. .BI hide
  1409. .TP
  1410. .BI rect " minx miny maxx maxy
  1411. .TP
  1412. .BI remove " name
  1413. .TP
  1414. .BR reveal " [ \f2n\fP ]
  1415. Without argument,
  1416. .B reveal
  1417. is the opposite of
  1418. .BR hide :
  1419. it makes its selected control visible after it was hidden.
  1420. With an argument, it makes the
  1421. .IR n 'th
  1422. added control visible, hiding all others.
  1423. .TP
  1424. .BI show
  1425. .TP
  1426. .BR size " [ \f2minΔx minΔy maxΔx maxΔy\fP ]
  1427. Without argument,
  1428. .I size
  1429. computes the maximum of the minimum and maximum sizes of
  1430. its constituent controls. With arguments, it sets the size to the
  1431. given values.
  1432. .PD
  1433. .SS Slider
  1434. A slider controls an integer value by dragging the mouse with a button.
  1435. Configured appropriately, it can serve as a scroll bar with the standard
  1436. Plan 9 behavior.
  1437. When the value changes, an event message is sent:
  1438. .IP
  1439. .EX
  1440. slidername: value \f2n\f1
  1441. .EE
  1442. .PP
  1443. The slider is a good candidate for connecting to another control
  1444. by setting its format and rewiring its
  1445. .B event
  1446. channel to the other's
  1447. .B ctl
  1448. channel.
  1449. .PP
  1450. The geometry of the slider is defined by three numbers:
  1451. .B max
  1452. is a number representing the range of the slider;
  1453. .B vis
  1454. is a number representing how much of what is being controlled is visible;
  1455. and
  1456. .B value
  1457. is a number representing the value of the slider within its range.
  1458. For example, if the slider is managing a textual display of 1000 lines, with
  1459. 18 visible, and the first visible line (numbered starting form 0) is 304,
  1460. .B max
  1461. will be 1000,
  1462. .B vis
  1463. will be 18, and
  1464. .B value
  1465. will be 304.
  1466. The
  1467. .I indicator
  1468. is the visual representation of the
  1469. .I vis
  1470. portion of the controlled object.
  1471. .PP
  1472. The control messages the slider accepts are:
  1473. .TF "\fLabsolute n"
  1474. .TP
  1475. .BI absolute " n
  1476. If
  1477. .I n
  1478. is zero,
  1479. the slider behaves like a Plan 9 scroll bar:
  1480. button 2 sets absolute position, button 1 decreases the value,
  1481. and button 3 increases it.
  1482. If
  1483. .I n
  1484. is non-zero, all buttons behave like button 2, setting the absolute value.
  1485. .TP
  1486. .BI border " b
  1487. .TP
  1488. .BI bordercolor " name
  1489. .TP
  1490. .BI clamp " end n
  1491. The
  1492. .I end
  1493. is either the word
  1494. .B high
  1495. or
  1496. .BR low ;
  1497. .I n
  1498. sets whether that end is clamped or not.
  1499. If it is clamped, that end of the indicator is always at its supremum.
  1500. A standard scroll bar has neither end clamped; a volume slider would
  1501. have its low end clamped.
  1502. If the low end is clamped, the value of the slider is represented by the
  1503. high end of the indicator; otherwise it is represented by the low end.
  1504. .TP
  1505. .BI focus " n
  1506. .TP
  1507. .BI format " fmt
  1508. .TP
  1509. .BI hide
  1510. .TP
  1511. .BI image " name
  1512. .TP
  1513. .BI indicatorcolor " name
  1514. Set the color in which to draw the indicator; default black.
  1515. .TP
  1516. .BI max " n
  1517. Set the maximum value of the range covered by the slider.
  1518. .TP
  1519. .BI orient " dir
  1520. The string
  1521. .I dir
  1522. begins either
  1523. .B hor
  1524. or
  1525. .B ver
  1526. to specify the orientation of the slider.
  1527. The default is vertical.
  1528. The value always increases to the right for horizontal sliders and
  1529. downwards for vertical sliders.
  1530. .TP
  1531. .BI rect " minx miny maxx maxy
  1532. .TP
  1533. .BI reveal
  1534. .TP
  1535. .BI size " minx miny maxx maxy
  1536. .TP
  1537. .B show
  1538. .TP
  1539. .BI value " n
  1540. .TP
  1541. .BI vis " n
  1542. Set the visible area shown by the indicator.
  1543. .PD
  1544. .SS Tab
  1545. A tab control combines radiobottuns with a stack of windows giving the
  1546. appearance of tabbed controls. Currently, the tabs are positioned at the
  1547. top of the stack. The radiobutton consists of textbuttons, the stack
  1548. can be composed of any type of control.
  1549. .PP
  1550. Control messages are
  1551. .TF "\fLformat fmt"
  1552. .TP
  1553. .BI add " button control button control ...
  1554. Adds a button to the radiobutton, and an associated control to the stack.
  1555. Buttons and controls are numbered in the order of addition. There is
  1556. no remove operation.
  1557. .TP
  1558. .BI border " b
  1559. .TP
  1560. .BI bordercolor " color
  1561. .TP
  1562. .BI focus " n
  1563. .TP
  1564. .BI format " fmt
  1565. When a format string is defined, the tab control reports which tab
  1566. is selected using the format string (which must print a
  1567. .B char*
  1568. and an
  1569. .BR int ).
  1570. .TP
  1571. .BI image " color
  1572. Color between member controls.
  1573. .TP
  1574. .BI separation " n
  1575. Spacing between buttons in the radiobutton and between the row of
  1576. buttons and the stack below it.
  1577. .TP
  1578. .BI rect " n n n n
  1579. .TP
  1580. .B hide
  1581. .TP
  1582. .B reveal
  1583. .TP
  1584. .BI size " n n n n
  1585. .TP
  1586. .B show
  1587. .TP
  1588. .BI value " n
  1589. Value must be an integer indicating which tab to bring to the top.
  1590. .PD
  1591. .SS Text
  1592. A text control presents a set of lines of text.
  1593. The text cannot be edited with the keyboard, but can be
  1594. changed by control messages.
  1595. (A more interactive text control will be created eventually.)
  1596. The mouse can be used to select lines of text.
  1597. The only event message reports a state change in the selection of a line:
  1598. .IP
  1599. .EX
  1600. textname: select \f2n\f1 \f2s\f1
  1601. .EE
  1602. .PP
  1603. states that line
  1604. .I n
  1605. has changed its selection state to
  1606. .IR s ,
  1607. either zero (unselected) or non-zero (selected).
  1608. The non-zero value encodes the mouse buttons that were down
  1609. when the selection occurred.
  1610. .PP
  1611. The control messages the text control accepts are:
  1612. .TF "\fLselectmode \fIs\fP "
  1613. .TP
  1614. .BI accumulate " s
  1615. .TP
  1616. .BI accumulate " n s
  1617. .TP
  1618. .BI add " s
  1619. .TP
  1620. .BI add " n s
  1621. With one argument, append the string
  1622. .I s
  1623. as a new last line of the control; if
  1624. .I n
  1625. is specified, add the line
  1626. .I before
  1627. the current line
  1628. .IR n ,
  1629. making the new line number
  1630. .IR n.
  1631. The lines are zero indexed and
  1632. .I n
  1633. can be no greater than the current number of lines.
  1634. .I Add
  1635. refreshes the display, but
  1636. .I accumulate
  1637. does not, to avoid n-squared behavior when assembling a piece of text.
  1638. .TP
  1639. .BI align " a
  1640. Controls the placement of each line of text left-to-right in its rectangle.
  1641. Vertically, lines are tightly packed with separation set by the font's interline
  1642. spacing.
  1643. .TP
  1644. .BI border " b
  1645. .TP
  1646. .BI bordercolor " name
  1647. .TP
  1648. .BI clear
  1649. Delete all text.
  1650. .TP
  1651. .BI delete " n
  1652. Delete line
  1653. .IR n .
  1654. .TP
  1655. .BI focus " n
  1656. .TP
  1657. .BI font " name
  1658. .TP
  1659. .BI image " name
  1660. .TP
  1661. .BI rect " minx miny maxx maxy
  1662. .TP
  1663. .BI replace " n s
  1664. Replace line
  1665. .I n
  1666. by the string
  1667. .IR s .
  1668. .TP
  1669. .BI reveal
  1670. .TP
  1671. .BI scroll " n
  1672. If
  1673. .I n
  1674. is non-zero, the text will automatically scroll so the last line is always visible
  1675. when new text is added.
  1676. .TP
  1677. .BI select " n m
  1678. Set the selection state of line
  1679. .I n
  1680. to
  1681. .IR m .
  1682. .TP
  1683. .BI selectcolor " name
  1684. Set the color in which to highlight selected lines; default yellow.
  1685. .TP
  1686. .BI selectmode " s
  1687. The string
  1688. .I s
  1689. is either
  1690. .B single
  1691. or
  1692. .BR multi .
  1693. If
  1694. .BR single ,
  1695. the default,
  1696. only one line may be selected at a time; when a line is selected,
  1697. other lines are unselected.
  1698. If
  1699. .BR multi ,
  1700. the selection state of individual lines can be toggled independently.
  1701. .TP
  1702. .BI size " minx miny maxx maxy
  1703. .TP
  1704. .B show
  1705. .TP
  1706. .BI textcolor " name
  1707. .TP
  1708. .BI topline " n
  1709. Scroll the text so the top visible line is number
  1710. .IR n .
  1711. .TP
  1712. .BI value " s
  1713. Delete all the text in the control and then add the single line
  1714. .IR s .
  1715. .PD
  1716. .SS Textbutton
  1717. A textbutton is a textual variant of a plain button.
  1718. Each state change triggers an event message:
  1719. .IP
  1720. .EX
  1721. textbuttonname: value \f2n\fP
  1722. .EE
  1723. .LP
  1724. where
  1725. .I n
  1726. encodes the mouse buttons used to make the selection.
  1727. .PP
  1728. Like a regular button, the value of a textbutton is an integer; the
  1729. .I text
  1730. is the string that appears in the button.
  1731. It uses the image, light, mask method of indicating its state;
  1732. moreover, the color of the text can be set to change when the button is pressed.
  1733. The control messages it accepts are:
  1734. .TF "\fLalign a"
  1735. .TP
  1736. .BI align " a
  1737. Controls the placement of the text in the rectangle.
  1738. .TP
  1739. .BI border " b
  1740. .TP
  1741. .BI bordercolor " name
  1742. .TP
  1743. .BI focus " n
  1744. .TP
  1745. .BI font " name
  1746. .TP
  1747. .BI format " fmt
  1748. .TP
  1749. .B hide
  1750. .TP
  1751. .BI image " name
  1752. .TP
  1753. .BI light " name
  1754. .TP
  1755. .BI mask " name
  1756. .TP
  1757. .BI pressedtextcolor " name
  1758. Set the color in which to display text when the textbutton is pressed.
  1759. .TP
  1760. .BI rect " minx miny maxx maxy
  1761. .TP
  1762. .B reveal
  1763. .TP
  1764. .BI size " minx miny maxx maxy
  1765. .TP
  1766. .B show
  1767. .TP
  1768. .BI text " s
  1769. Set the text displayed in the button.
  1770. .TP
  1771. .BI textcolor " name
  1772. .TP
  1773. .BI value " n
  1774. Set the button to `on' (if
  1775. .I n
  1776. is non-zero) or `off' (if
  1777. .I n
  1778. is zero).
  1779. .SS "Helper functions
  1780. The function
  1781. .I ctlerror
  1782. is called when the library encounters an error.
  1783. It prints the formatted message and exits the program.
  1784. .PP
  1785. The functions
  1786. .IR ctlmalloc ,
  1787. .IR ctlrealloc ,
  1788. .IR ctlstrdup ,
  1789. and
  1790. .I ctlrunestrdup
  1791. are packagings of the corresponding C library functions.
  1792. They call
  1793. .I ctlerror
  1794. if they fail to allocate memory, and
  1795. .I ctlmalloc
  1796. zeros the memory it returns.
  1797. .PP
  1798. Finally, for debugging, if the global variable
  1799. .I ctldeletequits
  1800. is set to a non-zero value, typing a
  1801. .SM DEL
  1802. will cause the program to call
  1803. .IP
  1804. .EX
  1805. ctlerror("delete");
  1806. .EE
  1807. .SS Caveat
  1808. This library is very new and is still missing a number of important features.
  1809. The details are all subject to change.
  1810. Another level of library that handles geometry and has sensible default
  1811. appearances for the controls would be useful.
  1812. .PP
  1813. One unusual design goal of this library was to make the controls themselves
  1814. easy to implement.
  1815. The reader is encouraged
  1816. to create new controls by adapting the source to existing ones.
  1817. .SH EXAMPLES
  1818. This example creates two entry boxes,
  1819. .BR top
  1820. and
  1821. .BR bot ,
  1822. and copies the contents of one to the other whenever a newline is typed.
  1823. .PP
  1824. .EX
  1825. #include <u.h>
  1826. #include <libc.h>
  1827. #include <thread.h>
  1828. #include <draw.h>
  1829. #include <mouse.h>
  1830. #include <keyboard.h>
  1831. #include <control.h>
  1832. .sp 0.4v
  1833. Controlset *cs;
  1834. .sp 0.4v
  1835. int ctldeletequits = 1;
  1836. .sp 0.4v
  1837. void
  1838. resizecontrolset(Controlset*)
  1839. {
  1840. int i;
  1841. Rectangle r, r1, r2;
  1842. .sp 0.4v
  1843. if(getwindow(display, Refnone) < 0)
  1844. sysfatal("resize failed: %r");
  1845. r = insetrect(screen->r, 10);
  1846. r1 = r;
  1847. r2 = r;
  1848. r1.max.y = r1.min.y+1+font->height+1;
  1849. r2.min.y = r1.max.y+10;
  1850. r2.max.y = r2.min.y+1+font->height+1;
  1851. chanprint(cs->ctl, "top rect %R\entop show", r1);
  1852. chanprint(cs->ctl, "bot rect %R\enbot show", r2);
  1853. }
  1854. .sp 0.4v
  1855. void
  1856. threadmain(int argc, char *argv[])
  1857. {
  1858. char *s, *args[3];
  1859. Channel *c;
  1860. Control *top, *bot;
  1861. int n;
  1862. .sp 0.4v
  1863. initdraw(0, 0, "example");
  1864. initcontrols();
  1865. cs = newcontrolset(screen, nil, nil, nil);
  1866. cs->clicktotype = 1;
  1867. .sp 0.4v
  1868. top = createentry(cs, "top");
  1869. chanprint(cs->ctl, "top image paleyellow");
  1870. chanprint(cs->ctl, "top border 1");
  1871. bot = createentry(cs, "bot");
  1872. chanprint(cs->ctl, "bot image paleyellow");
  1873. chanprint(cs->ctl, "bot border 1");
  1874. .sp 0.4v
  1875. c = chancreate(sizeof(char*), 0);
  1876. controlwire(top, "event", c);
  1877. controlwire(bot, "event", c);
  1878. .sp 0.4v
  1879. activate(top);
  1880. activate(bot);
  1881. resizecontrolset(cs);
  1882. .sp 0.4v
  1883. for(;;){
  1884. s = recvp(c);
  1885. n = tokenize(s, args, nelem(args));
  1886. if(n==3 && strcmp(args[1], "value")==0){
  1887. if(strcmp(args[0], "top:") == 0)
  1888. chanprint(cs->ctl, "bot value %q", args[2]);
  1889. else
  1890. chanprint(cs->ctl, "top value %q", args[2]);
  1891. }
  1892. }
  1893. threadexitsall(nil);
  1894. }
  1895. .EE
  1896. .PP
  1897. A richer variant couples a text entry box to a slider.
  1898. Since the value of a slider is its numerical setting, as a decimal number,
  1899. all that needs changing is the setup of
  1900. .BR bot :
  1901. .PP
  1902. .EX
  1903. bot = createslider(cs, "bot");
  1904. chanprint(cs->ctl, "bot border 1");
  1905. chanprint(cs->ctl, "bot image paleyellow");
  1906. chanprint(cs->ctl, "bot indicatorcolor red");
  1907. chanprint(cs->ctl, "bot max 100");
  1908. chanprint(cs->ctl, "bot clamp low 1");
  1909. chanprint(cs->ctl, "bot orient horizontal");
  1910. .EE
  1911. .PP
  1912. The rest is the same.
  1913. Of course, the value of the entry box is only meaningful to the slider
  1914. if it is also a decimal number.
  1915. .PP
  1916. Finally, we can avoid processing events altogether by cross-coupling
  1917. the controls. Replace the rest of
  1918. .B threadmain
  1919. with this:
  1920. .PP
  1921. .EX
  1922. chanprint(cs->ctl, "bot format %q", "%q: top value %q");
  1923. chanprint(cs->ctl, "top format %q", "%q: bot value %q");
  1924. .sp 0.4v
  1925. controlwire(top, "event", cs->ctl);
  1926. controlwire(bot, "event", cs->ctl);
  1927. .sp 0.4v
  1928. activate(top);
  1929. activate(bot);
  1930. resizecontrolset(cs);
  1931. .sp 0.4v
  1932. for(;;)
  1933. yield();
  1934. threadexitsall(nil);
  1935. .EE
  1936. .SH SOURCE
  1937. .B /sys/src/libcontrol
  1938. .SH SEE ALSO
  1939. .IR draw (2),
  1940. .IR frame (2),
  1941. .IR graphics (2),
  1942. .IR quote (2),
  1943. .IR thread (2)
  1944. .SH BUGS
  1945. The library is strict about matters of formatting, argument count in messages,
  1946. etc., and calls
  1947. .I ctlerror
  1948. in situations where it may be fine to ignore the error and continue.