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.

566 lines
18KB

  1. /**
  2. \page subclassing Adding and Extending Widgets
  3. This chapter describes how to add your own widgets or extend existing
  4. widgets in FLTK.
  5. \section subclassing_subclassing Subclassing
  6. New widgets are created by \e subclassing an existing FLTK widget,
  7. typically Fl_Widget for controls and Fl_Group for composite widgets.
  8. A control widget typically interacts with the user to receive and/or
  9. display a value of some sort.
  10. A composite widget holds a list of child widgets and handles moving,
  11. sizing, showing, or hiding them as needed. Fl_Group is the main
  12. composite widget class in FLTK, and all of the other composite widgets
  13. (Fl_Pack, Fl_Scroll, Fl_Tabs, Fl_Tile, and Fl_Window) are subclasses of it.
  14. You can also subclass other existing widgets to provide a different
  15. look or user-interface. For example, the button widgets are all
  16. subclasses of Fl_Button since they all interact with the user
  17. via a mouse button click. The only difference is the code that draws
  18. the face of the button.
  19. \section subclassing_fl_widget Making a Subclass of Fl_Widget
  20. Your subclasses can directly descend from Fl_Widget or any
  21. subclass of Fl_Widget. Fl_Widget has only four
  22. virtual methods, and overriding some or all of these may be necessary.
  23. \section subclassing_constructor The Constructor
  24. The constructor should have the following arguments:
  25. \code
  26. MyClass(int x, int y, int w, int h, const char *label = 0);
  27. \endcode
  28. This will allow the class to be used in
  29. \ref fluid "FLUID"
  30. without problems.
  31. The constructor must call the constructor for the base class and
  32. pass the same arguments:
  33. \code
  34. MyClass::MyClass(int x, int y, int w, int h, const char *label)
  35. : Fl_Widget(x, y, w, h, label) {
  36. // do initialization stuff...
  37. }
  38. \endcode
  39. Fl_Widget's protected constructor sets \p x(), \p y(),
  40. \p w(), \p h(), and \p label() to the passed values
  41. and initializes the other instance variables to:
  42. \code
  43. type(0);
  44. box(FL_NO_BOX);
  45. color(FL_BACKGROUND_COLOR);
  46. selection_color(FL_BACKGROUND_COLOR);
  47. labeltype(FL_NORMAL_LABEL);
  48. labelstyle(FL_NORMAL_STYLE);
  49. labelsize(FL_NORMAL_SIZE);
  50. labelcolor(FL_FOREGROUND_COLOR);
  51. align(FL_ALIGN_CENTER);
  52. callback(default_callback,0);
  53. flags(ACTIVE|VISIBLE);
  54. image(0);
  55. deimage(0);
  56. \endcode
  57. \section subclassing_protected Protected Methods of Fl_Widget
  58. The following methods are provided for subclasses to use:
  59. \li \ref subclassing_clear_visible "clear_visible()"
  60. \li \ref subclassing_damage "damage()"
  61. \li \ref subclassing_draw_box "draw_box()"
  62. \li \ref subclassing_draw_focus "draw_focus()"
  63. \li \ref subclassing_draw_label "draw_label()"
  64. \li \ref subclassing_set_flag "set_flag()"
  65. \li \ref subclassing_set_visible "set_visible()"
  66. \li \ref subclassing_test_shortcut "test_shortcut()"
  67. \li \ref subclassing_type "type()"
  68. \anchor subclassing_damage
  69. void Fl_Widget::damage(uchar mask) <br>
  70. void Fl_Widget::damage(uchar mask, int x, int y, int w, int h) <br>
  71. uchar Fl_Widget::damage()
  72. \par
  73. The first form indicates that a partial update of the object is
  74. needed. The bits in mask are OR'd into
  75. \ref subclassing_damage "damage()".
  76. Your \p draw() routine can examine these bits to limit what it is
  77. drawing. The public method Fl_Widget::redraw() simply does
  78. \p Fl_Widget::damage(FL_DAMAGE_ALL),
  79. but the implementation of your widget can call the public
  80. \p damage(n).
  81. \par
  82. The second form indicates that a region is damaged. If only these
  83. calls are done in a window (no calls to \p damage(n)) then FLTK
  84. will clip to the union of all these calls before drawing anything.
  85. This can greatly speed up incremental displays. The mask bits are
  86. OR'd into \p damage() unless this is a Fl_Window widget.
  87. \par
  88. The third form returns the bitwise-OR of all \p damage(n)
  89. calls done since the last \p draw().
  90. \par
  91. <I>When redrawing your widgets you should look at the damage bits to
  92. see what parts of your widget need redrawing.</I> The \p handle()
  93. method can then set individual damage bits to limit the amount of drawing
  94. that needs to be done:
  95. \code
  96. MyClass::handle(int event) {
  97. ...
  98. if (change_to_part1) damage(1);
  99. if (change_to_part2) damage(2);
  100. if (change_to_part3) damage(4);
  101. }
  102. MyClass::draw() {
  103. if (damage() & FL_DAMAGE_ALL) {
  104. ... draw frame/box and other static stuff ...
  105. }
  106. if (damage() & (FL_DAMAGE_ALL | 1)) draw_part1();
  107. if (damage() & (FL_DAMAGE_ALL | 2)) draw_part2();
  108. if (damage() & (FL_DAMAGE_ALL | 4)) draw_part3();
  109. }
  110. \endcode
  111. \todo Clarify Fl_Window::damage(uchar) handling - seems confused/wrong?
  112. ORing value doesn't match setting behaviour in FL_Widget.H!
  113. \anchor subclassing_draw_box
  114. void Fl_Widget::draw_box() const <br>
  115. void Fl_Widget::draw_box(Fl_Boxtype t, Fl_Color c) const
  116. \par
  117. The first form draws this widget's \p box(), using the
  118. dimensions of the widget. The second form uses \p t as the box
  119. type and \p c as the color for the box.
  120. \anchor subclassing_draw_focus
  121. void Fl_Widget::draw_focus() <br>
  122. void Fl_Widget::draw_focus(Fl_Boxtype t, int x, int y, int w, int h) const
  123. \par
  124. Draws a focus box inside the widget's bounding box. The second
  125. form allows you to specify a different bounding box.
  126. \anchor subclassing_draw_label
  127. void Fl_Widget::draw_label() const <br>
  128. void Fl_Widget::draw_label(int x, int y, int w, int h) const <br>
  129. void Fl_Widget::draw_label(int x, int y, int w, int h, Fl_Align align) const
  130. \par
  131. The first form is the usual function for a \p draw() method to call to
  132. draw the widget's label. It does not draw the label if it is supposed
  133. to be outside the box (on the assumption that the enclosing group will
  134. draw those labels).
  135. \par
  136. The second form uses the passed bounding box instead of the widget's
  137. bounding box. This is useful so "centered" labels are aligned with some
  138. feature, like a moving slider.
  139. \par
  140. The third form draws the label anywhere. It acts as though
  141. \p FL_ALIGN_INSIDE has been forced on so the label will appear inside
  142. the passed bounding box. This is designed for parent groups to draw
  143. labels with.
  144. \anchor subclassing_set_flag
  145. void Fl_Widget::set_flag(int c) <br>
  146. \par
  147. Calling \p set_flag(SHORTCUT_LABEL) modifies the behavior of
  148. \ref subclassing_draw_label "draw_label()" so that '\&' characters
  149. cause an underscore to be printed under the next letter.
  150. \anchor subclassing_clear_visible
  151. \anchor subclassing_set_visible
  152. void Fl_Widget::set_visible() <br>
  153. void Fl_Widget::clear_visible()
  154. \par
  155. Fast inline versions of Fl_Widget::hide() and Fl_Widget::show().
  156. These do not send the \p FL_HIDE and \p FL_SHOW events to the widget.
  157. \anchor subclassing_test_shortcut
  158. int Fl_Widget::test_shortcut() <br>
  159. static int Fl_Widget::test_shortcut(const char *s)
  160. \par
  161. The first version tests Fl_Widget::label() against the current event
  162. (which should be a \p FL_SHORTCUT event). If the label contains a '&'
  163. character and the character after it matches the keypress, this returns
  164. true. This returns false if the \p SHORTCUT_LABEL flag is off, if the
  165. label is \p NULL, or does not have a '&' character in it, or if the
  166. keypress does not match the character.
  167. \par
  168. The second version lets you do this test against an arbitrary string.
  169. \todo Clarify Fl_Widget::test_shortcut() explanations. Fl_Widget.h
  170. says Internal Use only, but subclassing chapter gives details!
  171. \anchor subclassing_type
  172. uchar Fl_Widget::type() const <br>
  173. void Fl_Widget::type(uchar t)
  174. \par
  175. The property Fl_Widget::type() can return an arbitrary 8-bit
  176. identifier, and can be set with the protected method <tt>type(uchar t)</tt>.
  177. This value had to be provided for Forms compatibility, but you can
  178. use it for any purpose you want. Try to keep the value less than 100
  179. to not interfere with reserved values.
  180. \par
  181. FLTK does not use RTTI (Run Time Typing Information), to enhance
  182. portability. But this may change in the near future if RTTI becomes
  183. standard everywhere.
  184. \par
  185. If you don't have RTTI you can use the clumsy FLTK mechanism, by
  186. having \p type() use a unique value. These unique values must
  187. be greater than the symbol \p FL_RESERVED_TYPE (which is 100) and
  188. less than \p FL_WINDOW (unless you make a subclass of Fl_Window).
  189. Look through the header files for \p FL_RESERVED_TYPE to find an
  190. unused number. If you make a subclass of Fl_Window you must use
  191. <tt>FL_WINDOW + n</tt> (where \p n must be in the range 1 to 7).
  192. \section subclassing_events Handling Events
  193. The virtual method Fl_Widget::handle(int event) is called
  194. to handle each event passed to the widget. It can:
  195. \li Change the state of the widget.
  196. \li Call Fl_Widget::redraw() if the widget needs to be redisplayed.
  197. \li Call Fl_Widget::damage(uchar c) if the widget needs a partial-update
  198. (assuming you provide support for this in your
  199. \ref subclassing_drawing "draw()"
  200. method).
  201. \li Call Fl_Widget::do_callback() if a callback should be generated.
  202. \li Call Fl_Widget::handle() on child widgets.
  203. Events are identified by the integer argument. Other information
  204. about the most recent event is stored in static locations and acquired
  205. by calling the
  206. \ref events_event_xxx.
  207. This information remains valid until another event is handled.
  208. Here is a sample \p handle() method for a widget that acts as
  209. a pushbutton and also accepts the keystroke \p 'x' to cause the callback:
  210. \code
  211. int MyClass::handle(int event) {
  212. switch(event) {
  213. case FL_PUSH:
  214. highlight = 1;
  215. redraw();
  216. return 1;
  217. case FL_DRAG: {
  218. int t = Fl::event_inside(this);
  219. if (t != highlight) {
  220. highlight = t;
  221. redraw();
  222. }
  223. }
  224. return 1;
  225. case FL_RELEASE:
  226. if (highlight) {
  227. highlight = 0;
  228. redraw();
  229. do_callback();
  230. // never do anything after a callback, as the callback
  231. // may delete the widget!
  232. }
  233. return 1;
  234. case FL_SHORTCUT:
  235. if (Fl::event_key() == 'x') {
  236. do_callback();
  237. return 1;
  238. }
  239. return 0;
  240. default:
  241. return Fl_Widget::handle(event);
  242. }
  243. }
  244. \endcode
  245. You must return non-zero if your \p handle() method
  246. uses the event. If you return zero, the parent widget will try
  247. sending the event to another widget.
  248. For debugging purposes, event numbers can be printed as their actual event names
  249. using the \ref fl_eventnames[] array, e.g.:
  250. \code
  251. #include <FL/names.h> // defines fl_eventnames[]
  252. [..]
  253. int MyClass::handle(int e) {
  254. printf("Event was %s (%d)\n", fl_eventnames[e], e); // e.g. "Event was FL_PUSH (1)"
  255. [..]
  256. \endcode
  257. \section subclassing_drawing Drawing the Widget
  258. The \p draw() virtual method is called when FLTK wants
  259. you to redraw your widget. It will be called if and only if
  260. \p damage() is non-zero, and \p damage() will be
  261. cleared to zero after it returns. The \p draw() method
  262. should be declared protected so that it can't be called from
  263. non-drawing code.
  264. The \p damage() value contains the bitwise-OR of all
  265. the \p damage(n) calls to this widget since it was last
  266. drawn. This can be used for minimal update, by only redrawing
  267. the parts whose bits are set. FLTK will turn on the
  268. \p FL_DAMAGE_ALL bit if it thinks the entire widget must
  269. be redrawn, e.g. for an expose event.
  270. Expose events (and the
  271. \ref subclassing_damage "damage(mask,x,y,w,h)" function described
  272. above) will cause \p draw() to be called with FLTK's
  273. \ref ssect_Clipping "clipping"
  274. turned on. You can greatly speed up redrawing in some
  275. cases by testing \p fl_not_clipped(x,y,w,h) or \p %fl_clip_box()
  276. and skipping invisible parts.
  277. Besides the protected methods described above, FLTK provides a large
  278. number of basic drawing functions, which are described in the chapter
  279. \ref drawing.
  280. \section subclassing_resizing Resizing the Widget
  281. The \p resize(x,y,w,h) method is called when
  282. the widget is being resized or moved. The arguments are the new
  283. position, width, and height. \p x(), \p y(), \p w(),
  284. and \p h() still remain the old size. You must call \p resize()
  285. on your base class with the same arguments to get the widget size to
  286. actually change.
  287. This should \e not call \p redraw(), at least if only the
  288. \p x() and \p y() change. This is because composite widgets like
  289. Fl_Scroll may have a more efficient way of drawing the new position.
  290. \section subclassing_composite Making a Composite Widget
  291. A "composite" widget contains one or more "child" widgets.
  292. To make a composite widget you should subclass Fl_Group.
  293. It is possible to make a composite object that is not a subclass of
  294. Fl_Group, but you'll have to duplicate the code in Fl_Group
  295. anyways.
  296. Instances of the child widgets may be included in the parent:
  297. \code
  298. class MyClass : public Fl_Group {
  299. Fl_Button the_button;
  300. Fl_Slider the_slider;
  301. ...
  302. };
  303. \endcode
  304. The constructor has to initialize these instances. They are automatically
  305. added to the group, since the Fl_Group constructor does
  306. Fl_Group::begin().
  307. <I>Don't forget to call Fl_Group::end() or use the Fl_End pseudo-class:</I>
  308. \code
  309. MyClass::MyClass(int x, int y, int w, int h) :
  310. Fl_Group(x, y, w, h),
  311. the_button(x + 5, y + 5, 100, 20),
  312. the_slider(x, y + 50, w, 20)
  313. {
  314. ...(you could add dynamically created child widgets here)...
  315. end(); // don't forget to do this!
  316. }
  317. \endcode
  318. The child widgets need callbacks. These will be called with a pointer
  319. to the children, but the widget itself may be found in the \p parent()
  320. pointer of the child. Usually these callbacks can be static private
  321. methods, with a matching private method:
  322. \code
  323. void MyClass::static_slider_cb(Fl_Widget* v, void *) { // static method
  324. ((MyClass*)(v->parent())->slider_cb();
  325. }
  326. void MyClass::slider_cb() { // normal method
  327. use(the_slider->value());
  328. }
  329. \endcode
  330. If you make the \p handle() method, you can quickly pass all the
  331. events to the children using the Fl_Group::handle() method.
  332. You don't need to override \p handle() if your composite widget
  333. does nothing other than pass events to the children:
  334. \code
  335. int MyClass::handle(int event) {
  336. if (Fl_Group::handle(event)) return 1;
  337. ... handle events that children don't want ...
  338. }
  339. \endcode
  340. If you override \p draw() you need to draw all the children.
  341. If \p redraw() or \p damage() is called on a child,
  342. \p damage(FL_DAMAGE_CHILD) is done to the group,
  343. so this bit of \p damage() can be used to indicate
  344. that a child needs to be drawn. It is fastest if you avoid
  345. drawing anything else in this case:
  346. \code
  347. int MyClass::draw() {
  348. Fl_Widget *const*a = array();
  349. if (damage() == FL_DAMAGE_CHILD) { // only redraw some children
  350. for (int i = children(); i --; a ++) update_child(**a);
  351. } else { // total redraw
  352. ... draw background graphics ...
  353. // now draw all the children atop the background:
  354. for (int i = children_; i --; a ++) {
  355. draw_child(**a);
  356. draw_outside_label(**a); // you may not need to do this
  357. }
  358. }
  359. }
  360. \endcode
  361. Fl_Group provides some protected methods to make drawing easier:
  362. \li \ref subclassing_draw_child "draw_child()"
  363. \li \ref subclassing_draw_children "draw_children()"
  364. \li \ref subclassing_draw_outside_label "draw_outside_label()"
  365. \li \ref subclassing_update_child "update_child()"
  366. \anchor subclassing_draw_child
  367. void Fl_Group::draw_child(Fl_Widget &widget) const
  368. \par
  369. This will force the child's \p damage() bits all to one and call
  370. \p draw() on it, then clear the \p damage(). You should call
  371. this on all children if a total redraw of your widget is requested, or
  372. if you draw something (like a background box) that damages the child.
  373. Nothing is done if the child is not \p visible() or if it is
  374. clipped.
  375. \anchor subclassing_draw_children
  376. void Fl_Group::draw_children()
  377. \par
  378. A convenience function that draws all children of the group.
  379. This is useful if you derived a widget from Fl_Group and want to draw
  380. a special border or background. You can call \p draw_children() from the
  381. derived \p draw() method after drawing the box, border, or background.
  382. \anchor subclassing_draw_outside_label
  383. void Fl_Group::draw_outside_label(const Fl_Widget &widget) const
  384. \par
  385. Draw the labels that are \e not drawn by
  386. \ref subclassing_draw_label "draw_label()".
  387. If you want more control over the label positions you might want to call
  388. <tt>child->draw_label(x,y,w,h,a)</tt>.
  389. \anchor subclassing_update_child
  390. void Fl_Group::update_child(Fl_Widget& widget) const
  391. \par
  392. Draws the child only if its \p damage() is non-zero. You
  393. should call this on all the children if your own damage is equal to
  394. \p FL_DAMAGE_CHILD. Nothing is done if the child is not \p visible()
  395. or if it is clipped.
  396. \section subclassing_cutnpaste Cut and Paste Support
  397. FLTK provides routines to cut and paste 8-bit text (in the future this
  398. may be UTF-8) between applications:
  399. \li Fl::paste()
  400. \li Fl::selection()
  401. \li Fl::selection_owner()
  402. It may be possible to cut/paste non-text data by using Fl::add_handler().
  403. Note that handling events beyond those provided by FLTK may be operating
  404. system specific. See \ref osissues for more details.
  405. \section subclassing_dragndrop Drag And Drop Support
  406. FLTK provides routines to drag and drop 8-bit text between applications:
  407. Drag'n'drop operations are initiated by copying data to the
  408. clipboard and calling the function Fl::dnd().
  409. Drop attempts are handled via the following events,
  410. already described under \ref events_dnd in a previous chapter:
  411. \li \p FL_DND_ENTER
  412. \li \p FL_DND_DRAG
  413. \li \p FL_DND_LEAVE
  414. \li \p FL_DND_RELEASE
  415. \li \p FL_PASTE
  416. \section subclassing_fl_window Making a subclass of Fl_Window
  417. You may want your widget to be a subclass of
  418. Fl_Window, Fl_Double_Window, or
  419. Fl_Gl_Window. This can be useful if your widget wants
  420. to occupy an entire window, and can also be used to take
  421. advantage of system-provided clipping, or to work with a library
  422. that expects a system window ID to indicate where to draw.
  423. Subclassing Fl_Window is almost exactly like
  424. subclassing Fl_Group, and in fact you can easily
  425. switch a subclass back and forth. Watch out for the following
  426. differences:
  427. -# Fl_Window is a subclass of Fl_Group so
  428. <I>make sure your constructor calls</I> \p end()
  429. unless you actually want children added to your window.
  430. -# When handling events and drawing, the upper-left corner is at
  431. 0,0, not <tt>x(),y()</tt> as in other Fl_Widget's.
  432. For instance, to draw a box around the widget, call
  433. <tt>draw_box(0,0,w(),h())</tt>, rather than
  434. <tt>draw_box(x(),y(),w(),h())</tt>.
  435. You may also want to subclass Fl_Window in order to
  436. get access to different visuals or to change other attributes of
  437. the windows. See the
  438. \ref osissues chapter for more information.
  439. \htmlonly
  440. <hr>
  441. <table summary="navigation bar" width="100%" border="0">
  442. <tr>
  443. <td width="45%" align="LEFT">
  444. <a class="el" href="events.html">
  445. [Prev]
  446. Handling Events
  447. </a>
  448. </td>
  449. <td width="10%" align="CENTER">
  450. <a class="el" href="index.html">[Index]</a>
  451. </td>
  452. <td width="45%" align="RIGHT">
  453. <a class="el" href="opengl.html">
  454. Using OpenGL
  455. [Next]
  456. </a>
  457. </td>
  458. </tr>
  459. </table>
  460. \endhtmlonly
  461. */