You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

646 lines
19KB

  1. /**
  2. \page common Common Widgets and Attributes
  3. This chapter describes many of the widgets that are provided
  4. with FLTK and covers how to query and set the standard
  5. attributes.
  6. \section common_buttons Buttons
  7. FLTK provides many types of buttons:
  8. \li Fl_Button - A standard push button.
  9. \li Fl_Check_Button - A button with a check box.
  10. \li Fl_Light_Button - A push button with a light.
  11. \li Fl_Repeat_Button - A push button that repeats when held.
  12. \li Fl_Return_Button - A push button that is activated by the
  13. \p Enter key.
  14. \li Fl_Round_Button - A button with a radio circle.
  15. \image html buttons.png "Figure 3-1: FLTK Button Widgets"
  16. \image latex buttons.png "FLTK Button Widgets" width=10cm
  17. All of these buttons just need the corresponding
  18. <tt><FL/Fl_xyz_Button.H></tt> header file. The constructor
  19. takes the bounding box of the button and optionally a label
  20. string:
  21. \code
  22. Fl_Button *button = new Fl_Button(x, y, width, height, "label");
  23. Fl_Light_Button *lbutton = new Fl_Light_Button(x, y, width, height);
  24. Fl_Round_Button *rbutton = new Fl_Round_Button(x, y, width, height, "label");
  25. \endcode
  26. Each button has an associated \p type() which allows
  27. it to behave as a push button, toggle button, or radio button:
  28. \code
  29. button->type(FL_NORMAL_BUTTON);
  30. lbutton->type(FL_TOGGLE_BUTTON);
  31. rbutton->type(FL_RADIO_BUTTON);
  32. \endcode
  33. For toggle and radio buttons, the \p value() method returns
  34. the current button state (0 = off, 1 = on). The \p set() and
  35. \p clear() methods can be used on toggle buttons to turn a
  36. toggle button on or off, respectively.
  37. Radio buttons can be turned on with the \p setonly()
  38. method; this will also turn off other radio buttons in the same
  39. group.
  40. \section common_text Text
  41. FLTK provides several text widgets for displaying and receiving text:
  42. \li Fl_Input - A one-line text input field.
  43. \li Fl_Output - A one-line text output field.
  44. \li Fl_Multiline_Input - A multi-line text input field.
  45. \li Fl_Multiline_Output - A multi-line text output field.
  46. \li Fl_Text_Display - A multi-line text display widget.
  47. \li Fl_Text_Editor - A multi-line text editing widget.
  48. \li Fl_Help_View - A HTML text display widget.
  49. The Fl_Output and Fl_Multiline_Output
  50. widgets allow the user to copy text from the output field but
  51. not change it.
  52. The \p value() method is used to get or set the
  53. string that is displayed:
  54. \code
  55. Fl_Input *input = new Fl_Input(x, y, width, height, "label");
  56. input->value("Now is the time for all good men...");
  57. \endcode
  58. The string is copied to the widget's own storage when you set
  59. the \p value() of the widget.
  60. The Fl_Text_Display and Fl_Text_Editor
  61. widgets use an associated Fl_Text_Buffer class for the
  62. value, instead of a simple string.
  63. <!-- NEED 4in -->
  64. \section common_valuators Valuators
  65. Unlike text widgets, valuators keep track of numbers instead of
  66. strings. FLTK provides the following valuators:
  67. \li Fl_Counter - A widget with arrow buttons that shows the current value.
  68. \li Fl_Dial - A round knob.
  69. \li Fl_Roller - An SGI-like dolly widget.
  70. \li Fl_Scrollbar - A standard scrollbar widget.
  71. \li Fl_Slider - A scrollbar with a knob.
  72. \li Fl_Value_Slider - A slider that shows the current value.
  73. \image html valuators.png "Figure 3-2: FLTK valuator widgets"
  74. \image latex valuators.png "FLTK valuator widgets" width=10cm
  75. The \p value() method gets and sets the current value
  76. of the widget. The \p minimum() and \p maximum()
  77. methods set the range of values that are reported by the
  78. widget.
  79. <!-- NEED 5in -->
  80. \section common_groups Groups
  81. The Fl_Group widget class is used as a general
  82. purpose "container" widget. Besides grouping radio
  83. buttons, the groups are used to encapsulate windows, tabs, and
  84. scrolled windows. The following group classes are available
  85. with FLTK:
  86. \li Fl_Double_Window - A double-buffered window on the screen.
  87. \li Fl_Gl_Window - An OpenGL window on the screen.
  88. \li Fl_Group - The base container class; can be used to group
  89. any widgets together.
  90. \li Fl_Pack - A collection of widgets that are packed into the group area.
  91. \li Fl_Scroll - A scrolled window area.
  92. \li Fl_Tabs - Displays child widgets as tabs.
  93. \li Fl_Tile - A tiled window area.
  94. \li Fl_Window - A window on the screen.
  95. \li Fl_Wizard - Displays one group of widgets at a time.
  96. \section common_sizeposition Setting the Size and Position of Widgets
  97. The size and position of widgets is usually set when you create them.
  98. You can access them with the \p x(), \p y(), \p w(), and \p h()
  99. methods.
  100. You can change the size and position by using the \p position(),
  101. \p resize(), and \p size() methods:
  102. \code
  103. button->position(x, y);
  104. group->resize(x, y, width, height);
  105. window->size(width, height);
  106. \endcode
  107. If you change a widget's size or position after it is
  108. displayed you will have to call \p redraw() on the
  109. widget's parent.
  110. \section common_colors Colors
  111. FLTK stores the colors of widgets as an 32-bit unsigned
  112. number that is either an index into a color palette of 256
  113. colors or a 24-bit RGB color. The color palette is \e not
  114. the X or MS Windows colormap, but instead is an internal table with
  115. fixed contents.
  116. See the
  117. \ref drawing_colors
  118. section of
  119. \ref drawing
  120. for implementation details.
  121. There are symbols for naming some of the more common colors:
  122. \li \p FL_BLACK
  123. \li \p FL_RED
  124. \li \p FL_GREEN
  125. \li \p FL_YELLOW
  126. \li \p FL_BLUE
  127. \li \p FL_MAGENTA
  128. \li \p FL_CYAN
  129. \li \p FL_WHITE
  130. \li \p FL_WHITE
  131. Other symbols are used as the default colors for all FLTK widgets.
  132. \li \p FL_FOREGROUND_COLOR
  133. \li \p FL_BACKGROUND_COLOR
  134. \li \p FL_INACTIVE_COLOR
  135. \li \p FL_SELECTION_COLOR
  136. The full list of named color values can be found in
  137. \ref enumerations_colors "FLTK Enumerations".
  138. A color value can be created from its RGB components by using the
  139. \p %fl_rgb_color() function, and decomposed again with
  140. \p Fl::get_color():
  141. \code
  142. Fl_Color c = fl_rgb_color(85, 170, 255); // RGB to Fl_Color
  143. Fl::get_color(c, r, g, b); // Fl_Color to RGB
  144. \endcode
  145. The widget color is set using the \p color() method:
  146. \code
  147. button->color(FL_RED); // set color using named value
  148. \endcode
  149. Similarly, the label color is set using the \p labelcolor() method:
  150. \code
  151. button->labelcolor(FL_WHITE);
  152. \endcode
  153. The Fl_Color encoding maps to a 32-bit unsigned integer representing
  154. RGBI, so it is also possible to specify a color using a hex constant
  155. as a color map index:
  156. \code
  157. button->color(0x000000ff); // colormap index #255 (FL_WHITE)
  158. \endcode
  159. or specify a color using a hex constant for the RGB components:
  160. \code
  161. button->color(0xff000000); // RGB: red
  162. button->color(0x00ff0000); // RGB: green
  163. button->color(0x0000ff00); // RGB: blue
  164. button->color(0xffffff00); // RGB: white
  165. \endcode
  166. \note
  167. If TrueColor is not available, any RGB colors will be set to
  168. the nearest entry in the colormap.
  169. \section common_boxtypes Box Types
  170. The type Fl_Boxtype stored and returned in Fl_Widget::box()
  171. is an enumeration defined in Enumerations.H.
  172. Figure 3-3 shows the standard box types included with FLTK.
  173. \image html boxtypes.png "Figure 3-3: FLTK box types"
  174. \image latex boxtypes.png "FLTK box types" width=12cm
  175. \p FL_NO_BOX means nothing is drawn at all, so whatever is
  176. already on the screen remains. The <tt>FL_..._FRAME</tt> types only
  177. draw their edges, leaving the interior unchanged. The blue color in
  178. Figure 3-3 is the area that is not drawn by the frame types.
  179. \subsection common_custom_boxtypes Making Your Own Boxtypes
  180. You can define your own boxtypes by making a small function that draws
  181. the box and adding it to the table of boxtypes.
  182. <CENTER><TABLE WIDTH="80%" BORDER="1" CELLPADDING="5" CELLSPACING="0" BGCOLOR="#cccccc">
  183. <TR>
  184. <TD><B>Note:</B>
  185. <P>This interface has changed in FLTK 2.0!
  186. </TD>
  187. </TR>
  188. </TABLE></CENTER>
  189. \par The Drawing Function
  190. The drawing function is passed the bounding box and background color
  191. for the widget:
  192. \code
  193. void xyz_draw(int x, int y, int w, int h, Fl_Color c) {
  194. ...
  195. }
  196. \endcode
  197. <!-- NEED 3in -->
  198. A simple drawing function might fill a rectangle with the
  199. given color and then draw a black outline:
  200. \code
  201. void xyz_draw(int x, int y, int w, int h, Fl_Color c) {
  202. fl_color(c);
  203. fl_rectf(x, y, w, h);
  204. fl_color(FL_BLACK);
  205. fl_rect(x, y, w, h);
  206. }
  207. \endcode
  208. \anchor common_fl_down
  209. Fl_Boxtype fl_down(Fl_Boxtype b)
  210. \par
  211. fl_down() returns the "pressed" or "down" version of a box.
  212. If no "down" version of a given box exists, the behavior of this function
  213. is undefined and some random box or frame is returned.
  214. See \ref drawing_fl_frame "Drawing Functions" for more details.
  215. \anchor common_fl_frame
  216. Fl_Boxtype fl_frame(Fl_Boxtype b)
  217. \par
  218. fl_frame() returns the unfilled, frame-only version of a box.
  219. If no frame version of a given box exists, the behavior of this function
  220. is undefined and some random box or frame is returned.
  221. See \ref drawing_fl_frame "Drawing Functions" for more details.
  222. Fl_Boxtype fl_box(Fl_Boxtype b)
  223. \par
  224. fl_box() returns the filled version of a frame.
  225. If no filled version of a given frame exists, the behavior of this function
  226. is undefined and some random box or frame is returned.
  227. See \ref drawing_fl_frame "Drawing Functions" for more details.
  228. \par Adding Your Box Type
  229. The Fl::set_boxtype() method adds or replaces the specified box type:
  230. \code
  231. #define XYZ_BOX FL_FREE_BOXTYPE
  232. Fl::set_boxtype(XYZ_BOX, xyz_draw, 1, 1, 2, 2);
  233. \endcode
  234. The last 4 arguments to Fl::set_boxtype() are the
  235. offsets for the \p x, \p y, \p width, and \p height values that should be
  236. subtracted when drawing the label inside the box.
  237. A complete box design contains four box types in this order:
  238. a filled, neutral box (<tt>UP_BOX</tt>),
  239. a filled, depressed box (<tt>DOWN_BOX</tt>),
  240. and the same as outlines only (<tt>UP_FRAME</tt> and <tt>DOWN_FRAME</tt>).
  241. The function
  242. \ref common_fl_down "fl_down(Fl_Boxtype)"
  243. expects the neutral design on a boxtype with a numerical
  244. value evenly dividable by two.
  245. \ref common_fl_frame "fl_frame(Fl_Boxtype)"
  246. expects the \p UP_BOX design at a value dividable by four.
  247. \section common_labels Labels and Label Types
  248. The \p label(), \p align(), \p labelfont(), \p labelsize(),
  249. \p labeltype(), \p image(), and \p deimage() methods control the
  250. labeling of widgets.
  251. \par label()
  252. The \p label() method sets the string that is displayed
  253. for the label. Symbols can be included with the label string by
  254. escaping them using the "@" symbol - "@@" displays a single at
  255. sign. Figure 3-4 shows the available symbols.
  256. \image html symbols.png "Figure 3-4: FLTK label symbols"
  257. \image latex symbols.png "FLTK label symbols" width=10cm
  258. <!-- NEED 2in -->
  259. The @ sign may also be followed by the following optional
  260. "formatting" characters, in this order:
  261. \li '#' forces square scaling, rather than distortion to the widget's shape.
  262. \li +[1-9] or -[1-9] tweaks the scaling a little bigger or smaller.
  263. \li '$' flips the symbol horizontally, '%' flips it vertically.
  264. \li [0-9] - rotates by a multiple of 45 degrees. '5' and '6' do no rotation
  265. while the others point in the direction of that key on a numeric keypad.
  266. '0', followed by four more digits rotates the symbol by that amount in
  267. degrees.
  268. Thus, to show a very large arrow pointing downward you would use the
  269. label string "@+92->".
  270. \par align()
  271. The \p align() method positions the label. The following
  272. constants are defined and may be OR'd together as needed:
  273. \li \p FL_ALIGN_CENTER - center the label in the widget.
  274. \li \p FL_ALIGN_TOP - align the label at the top of the widget.
  275. \li \p FL_ALIGN_BOTTOM - align the label at the bottom of the widget.
  276. \li \p FL_ALIGN_LEFT - align the label to the left of the widget.
  277. \li \p FL_ALIGN_RIGHT - align the label to the right of the widget.
  278. \li \p FL_ALIGN_LEFT_TOP - The label appears to the left of the widget, aligned at the top. Outside labels only.
  279. \li \p FL_ALIGN_RIGHT_TOP - The label appears to the right of the widget, aligned at the top. Outside labels only.
  280. \li \p FL_ALIGN_LEFT_BOTTOM - The label appears to the left of the widget, aligned at the bottom. Outside labels only.
  281. \li \p FL_ALIGN_RIGHT_BOTTOM - The label appears to the right of the widget, aligned at the bottom. Outside labels only.
  282. \li \p FL_ALIGN_INSIDE - align the label inside the widget.
  283. \li \p FL_ALIGN_CLIP - clip the label to the widget's bounding box.
  284. \li \p FL_ALIGN_WRAP - wrap the label text as needed.
  285. \li \p FL_ALIGN_TEXT_OVER_IMAGE - show the label text over the image.
  286. \li \p FL_ALIGN_IMAGE_OVER_TEXT - show the label image over the text (default).
  287. \li \p FL_ALIGN_IMAGE_NEXT_TO_TEXT - The image will appear to the left of the text.
  288. \li \p FL_ALIGN_TEXT_NEXT_TO_IMAGE - The image will appear to the right of the text.
  289. \li \p FL_ALIGN_IMAGE_BACKDROP - The image will be used as a background for the widget.
  290. \anchor common_labeltype
  291. \par labeltype()
  292. The \p labeltype() method sets the type of the label. The
  293. following standard label types are included:
  294. \li \p FL_NORMAL_LABEL - draws the text.
  295. \li \p FL_NO_LABEL - does nothing.
  296. \li \p FL_SHADOW_LABEL - draws a drop shadow under the text.
  297. \li \p FL_ENGRAVED_LABEL - draws edges as though the text is engraved.
  298. \li \p FL_EMBOSSED_LABEL - draws edges as thought the text is raised.
  299. \li \p FL_ICON_LABEL - draws the icon associated with the text.
  300. \par image() and deimage()
  301. The \p image() and \p deimage() methods set an image that
  302. will be displayed with the widget. The \p deimage() method sets the
  303. image that is shown when the widget is inactive, while the \p image()
  304. method sets the image that is shown when the widget is active.
  305. To make an image you use a subclass of
  306. \ref ssect_Fl_Image "Fl_Image".
  307. \par Making Your Own Label Types
  308. Label types are actually indexes into a table of functions
  309. that draw them. The primary purpose of this is to use this to
  310. draw the labels in ways inaccessible through the
  311. fl_font() mechanism (e.g. <tt>FL_ENGRAVED_LABEL</tt>) or
  312. with program-generated letters or symbology.
  313. <CENTER><TABLE WIDTH="80%" BORDER="1" CELLPADDING="5" CELLSPACING="0" BGCOLOR="#cccccc">
  314. <TR>
  315. <TD><B>Note:</B>
  316. <P>This interface has changed in FLTK 2.0!
  317. </TD>
  318. </TR>
  319. </TABLE></CENTER>
  320. \par Label Type Functions
  321. To setup your own label type you will need to write two
  322. functions: one to draw and one to measure the label. The draw
  323. function is called with a pointer to a Fl_Label
  324. structure containing the label information, the bounding box for
  325. the label, and the label alignment:
  326. \code
  327. void xyz_draw(const Fl_Label *label, int x, int y, int w, int h, Fl_Align align) {
  328. ...
  329. }
  330. \endcode
  331. The label should be drawn \e inside this bounding box,
  332. even if \p FL_ALIGN_INSIDE is not enabled. The function
  333. is not called if the label value is \p NULL.
  334. The measure function is called with a pointer to a
  335. Fl_Label structure and references to the width and
  336. height:
  337. \code
  338. void xyz_measure(const Fl_Label *label, int &w, int &h) {
  339. ...
  340. }
  341. \endcode
  342. The function should measure the size of the label and set
  343. \p w and \p h to the size it will occupy.
  344. \par Adding Your Label Type
  345. The Fl::set_labeltype() method creates a label type
  346. using your draw and measure functions:
  347. \code
  348. #define XYZ_LABEL FL_FREE_LABELTYPE
  349. Fl::set_labeltype(XYZ_LABEL, xyz_draw, xyz_measure);
  350. \endcode
  351. The label type number \p n can be any integer value
  352. starting at the constant \p FL_FREE_LABELTYPE. Once you
  353. have added the label type you can use the \p labeltype()
  354. method to select your label type.
  355. The Fl::set_labeltype() method can also be used to overload
  356. an existing label type such as \p FL_NORMAL_LABEL.
  357. \par Making your own symbols
  358. It is also possible to define your own drawings and add
  359. them to the symbol list, so they can be rendered as part of
  360. any label.
  361. To create a new symbol, you implement a drawing function
  362. <tt>void drawit(Fl_Color c)</tt> which typically uses the
  363. functions described in \ref ssect_Complex
  364. to generate a vector shape inside a two-by-two units sized box
  365. around the origin. This function is then linked into the symbols
  366. table using fl_add_symbol():
  367. \code
  368. int fl_add_symbol(const char *name, void (*drawit)(Fl_Color), int scalable)
  369. \endcode
  370. \p name is the name of the symbol without the "@"; \p scalable
  371. must be set to 1 if the symbol is generated using scalable vector drawing
  372. functions.
  373. \code
  374. int fl_draw_symbol(const char *name,int x,int y,int w,int h,Fl_Color col)
  375. \endcode
  376. This function draws a named symbol fitting the given rectangle.
  377. \section common_callbacks Callbacks
  378. Callbacks are functions that are called when the value of a
  379. widget changes. A callback function is sent a Fl_Widget
  380. pointer of the widget that changed and a pointer to data that
  381. you provide:
  382. \code
  383. void xyz_callback(Fl_Widget *w, void *data) {
  384. ...
  385. }
  386. \endcode
  387. The \p callback() method sets the callback function for a
  388. widget. You can optionally pass a pointer to some data needed for the
  389. callback:
  390. \code
  391. int xyz_data;
  392. button->callback(xyz_callback, &xyz_data);
  393. \endcode
  394. Normally callbacks are performed only when the value of the
  395. widget changes. You can change this using the Fl_Widget::when()
  396. method:
  397. \code
  398. button->when(FL_WHEN_NEVER);
  399. button->when(FL_WHEN_CHANGED);
  400. button->when(FL_WHEN_RELEASE);
  401. button->when(FL_WHEN_RELEASE_ALWAYS);
  402. button->when(FL_WHEN_ENTER_KEY);
  403. button->when(FL_WHEN_ENTER_KEY_ALWAYS);
  404. button->when(FL_WHEN_CHANGED | FL_WHEN_NOT_CHANGED);
  405. \endcode
  406. <CENTER><TABLE WIDTH="80%" BORDER="1" CELLPADDING="5" CELLSPACING="0" BGCOLOR="#cccccc">
  407. <TR>
  408. <TD><B>Note:</B>
  409. You cannot delete a widget inside a callback, as the
  410. widget may still be accessed by FLTK after your callback
  411. is completed. Instead, use the Fl::delete_widget()
  412. method to mark your widget for deletion when it is safe
  413. to do so.
  414. <B>Hint:</B>
  415. Many programmers new to FLTK or C++ try to use a
  416. non-static class method instead of a static class method
  417. or function for their callback. Since callbacks are done
  418. outside a C++ class, the <tt>this</tt> pointer is not
  419. initialized for class methods.
  420. To work around this problem, define a static method
  421. in your class that accepts a pointer to the class, and
  422. then have the static method call the class method(s) as
  423. needed. The data pointer you provide to the
  424. \p callback() method of the widget can be a
  425. pointer to the instance of your class.
  426. \code
  427. class Foo {
  428. void my_callback(Fl_Widget *w);
  429. static void my_static_callback(Fl_Widget *w, void *f) { ((Foo *)f)->my_callback(w); }
  430. ...
  431. }
  432. ...
  433. w->callback(my_static_callback, (void *)this);
  434. \endcode
  435. </TD>
  436. </TR>
  437. </TABLE></CENTER>
  438. \section common_shortcuts Shortcuts
  439. Shortcuts are key sequences that activate widgets such as
  440. buttons or menu items. The \p shortcut() method sets the
  441. shortcut for a widget:
  442. \code
  443. button->shortcut(FL_Enter);
  444. button->shortcut(FL_SHIFT + 'b');
  445. button->shortcut(FL_CTRL + 'b');
  446. button->shortcut(FL_ALT + 'b');
  447. button->shortcut(FL_CTRL + FL_ALT + 'b');
  448. button->shortcut(0); // no shortcut
  449. \endcode
  450. The shortcut value is the key event value - the ASCII value
  451. or one of the special keys described in
  452. \ref enumerations_event_key
  453. combined with any modifiers like \p Shift , \p Alt , and \p Control.
  454. \htmlonly
  455. <hr>
  456. <table summary="navigation bar" width="100%" border="0">
  457. <tr>
  458. <td width="45%" align="LEFT">
  459. <a class="el" href="basics.html">
  460. [Prev]
  461. FLTK Basics
  462. </a>
  463. </td>
  464. <td width="10%" align="CENTER">
  465. <a class="el" href="index.html">[Index]</a>
  466. </td>
  467. <td width="45%" align="RIGHT">
  468. <a class="el" href="editor.html">
  469. Designing a Simple Text Editor
  470. [Next]
  471. </a>
  472. </td>
  473. </tr>
  474. </table>
  475. \endhtmlonly
  476. */