control 42 KB

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