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.

1007 lines
37KB

  1. //
  2. // "$Id: Fl_Widget.H 8623 2011-04-24 17:09:41Z AlbrechtS $"
  3. //
  4. // Widget header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. /** \file
  28. Fl_Widget, Fl_Label classes . */
  29. #ifndef Fl_Widget_H
  30. #define Fl_Widget_H
  31. #include "Enumerations.H"
  32. /**
  33. \todo typedef's fl_intptr_t and fl_uintptr_t should be documented.
  34. */
  35. #ifdef _WIN64
  36. #ifdef __GNUC__
  37. #include <stdint.h>
  38. #else
  39. #include <stddef.h> // M$VC
  40. #endif
  41. typedef intptr_t fl_intptr_t;
  42. typedef uintptr_t fl_uintptr_t;
  43. #else
  44. typedef long fl_intptr_t;
  45. typedef unsigned long fl_uintptr_t;
  46. #endif
  47. class Fl_Widget;
  48. class Fl_Window;
  49. class Fl_Group;
  50. class Fl_Image;
  51. /** Default callback type definition for all fltk widgets (by far the most used) */
  52. typedef void (Fl_Callback )(Fl_Widget*, void*);
  53. /** Default callback type pointer definition for all fltk widgets */
  54. typedef Fl_Callback* Fl_Callback_p; // needed for BORLAND
  55. /** One parameter callback type definition passing only the widget */
  56. typedef void (Fl_Callback0)(Fl_Widget*);
  57. /** Callback type definition passing the widget and a long data value */
  58. typedef void (Fl_Callback1)(Fl_Widget*, long);
  59. /** This struct stores all information for a text or mixed graphics label.
  60. \todo For FLTK 1.3, the Fl_Label type will become a widget by itself. That way
  61. we will be avoiding a lot of code duplication by handling labels in
  62. a similar fashion to widgets containing text. We also provide an easy
  63. interface for very complex labels, containing html or vector graphics.
  64. */
  65. struct FL_EXPORT Fl_Label {
  66. /** label text */
  67. const char* value;
  68. /** optional image for an active label */
  69. Fl_Image* image;
  70. /** optional image for a deactivated label */
  71. Fl_Image* deimage;
  72. /** label font used in text */
  73. Fl_Font font;
  74. /** size of label font */
  75. Fl_Fontsize size;
  76. /** text color */
  77. Fl_Color color;
  78. /** alignment of label */
  79. Fl_Align align_;
  80. /** type of label. \see Fl_Labeltype */
  81. uchar type;
  82. /** Draws the label aligned to the given box */
  83. void draw(int,int,int,int, Fl_Align) const ;
  84. void measure(int &w, int &h) const ;
  85. };
  86. /** Fl_Widget is the base class for all widgets in FLTK.
  87. You can't create one of these because the constructor is not public.
  88. However you can subclass it.
  89. All "property" accessing methods, such as color(), parent(), or argument()
  90. are implemented as trivial inline functions and thus are as fast and small
  91. as accessing fields in a structure. Unless otherwise noted, the property
  92. setting methods such as color(n) or label(s) are also trivial inline
  93. functions, even if they change the widget's appearance. It is up to the
  94. user code to call redraw() after these.
  95. */
  96. class FL_EXPORT Fl_Widget {
  97. friend class Fl_Group;
  98. Fl_Group* parent_;
  99. Fl_Callback* callback_;
  100. void* user_data_;
  101. int x_,y_,w_,h_;
  102. Fl_Label label_;
  103. unsigned int flags_;
  104. Fl_Color color_;
  105. Fl_Color color2_;
  106. uchar type_;
  107. uchar damage_;
  108. uchar box_;
  109. uchar when_;
  110. const char *tooltip_;
  111. /** unimplemented copy ctor */
  112. Fl_Widget(const Fl_Widget &);
  113. /** unimplemented assignment operator */
  114. Fl_Widget& operator=(const Fl_Widget &);
  115. protected:
  116. /** Creates a widget at the given position and size.
  117. The Fl_Widget is a protected constructor, but all derived widgets have a
  118. matching public constructor. It takes a value for x(), y(), w(), h(), and
  119. an optional value for label().
  120. \param[in] x, y the position of the widget relative to the enclosing window
  121. \param[in] w, h size of the widget in pixels
  122. \param[in] label optional text for the widget label
  123. */
  124. Fl_Widget(int x, int y, int w, int h, const char *label=0L);
  125. /** Internal use only. Use position(int,int), size(int,int) or resize(int,int,int,int) instead. */
  126. void x(int v) {x_ = v;}
  127. /** Internal use only. Use position(int,int), size(int,int) or resize(int,int,int,int) instead. */
  128. void y(int v) {y_ = v;}
  129. /** Internal use only. Use position(int,int), size(int,int) or resize(int,int,int,int) instead. */
  130. void w(int v) {w_ = v;}
  131. /** Internal use only. Use position(int,int), size(int,int) or resize(int,int,int,int) instead. */
  132. void h(int v) {h_ = v;}
  133. /** Gets the widget flags mask */
  134. unsigned int flags() const {return flags_;}
  135. /** Sets a flag in the flags mask */
  136. void set_flag(unsigned int c) {flags_ |= c;}
  137. /** Clears a flag in the flags mask */
  138. void clear_flag(unsigned int c) {flags_ &= ~c;}
  139. /** flags possible values enumeration.
  140. See activate(), output(), visible(), changed(), set_visible_focus()
  141. */
  142. enum {
  143. INACTIVE = 1<<0, ///< the widget can't receive focus, and is disabled but potentially visible
  144. INVISIBLE = 1<<1, ///< the widget is not drawn, but can receive a few special events
  145. OUTPUT = 1<<2, ///< for output only
  146. NOBORDER = 1<<3, ///< don't draw a decoration (Fl_Window)
  147. FORCE_POSITION = 1<<4, ///< don't let the window manager position the window (Fl_Window)
  148. NON_MODAL = 1<<5, ///< this is a hovering toolbar window (Fl_Window)
  149. SHORTCUT_LABEL = 1<<6, ///< the label contains a shortcut we need to draw
  150. CHANGED = 1<<7, ///< the widget value changed
  151. OVERRIDE = 1<<8, ///< position window on top (Fl_Window)
  152. VISIBLE_FOCUS = 1<<9, ///< accepts keyboard focus navigation if the widget can have the focus
  153. COPIED_LABEL = 1<<10, ///< the widget label is internally copied, its destruction is handled by the widget
  154. CLIP_CHILDREN = 1<<11, ///< all drawing within this widget will be clipped (Fl_Group)
  155. MENU_WINDOW = 1<<12, ///< a temporary popup window, dismissed by clicking outside (Fl_Window)
  156. TOOLTIP_WINDOW = 1<<13, ///< a temporary popup, transparent to events, and dismissed easily (Fl_Window)
  157. MODAL = 1<<14, ///< a window blocking input to all other winows (Fl_Window)
  158. NO_OVERLAY = 1<<15, ///< window not using a hardware overlay plane (Fl_Menu_Window)
  159. GROUP_RELATIVE = 1<<16, ///< position this widget relative to the parent group, not to the window
  160. COPIED_TOOLTIP = 1<<17, ///< the widget tooltip is internally copied, its destruction is handled by the widget
  161. // (space for more flags)
  162. USERFLAG3 = 1<<29, ///< reserved for 3rd party extensions
  163. USERFLAG2 = 1<<30, ///< reserved for 3rd party extensions
  164. USERFLAG1 = 1<<31 ///< reserved for 3rd party extensions
  165. };
  166. void draw_box() const;
  167. void draw_box(Fl_Boxtype t, Fl_Color c) const;
  168. void draw_box(Fl_Boxtype t, int x,int y,int w,int h, Fl_Color c) const;
  169. void draw_backdrop() const;
  170. /** draws a focus rectangle around the widget */
  171. void draw_focus() {draw_focus(box(),x(),y(),w(),h());}
  172. void draw_focus(Fl_Boxtype t, int x,int y,int w,int h) const;
  173. void draw_label() const;
  174. void draw_label(int, int, int, int) const;
  175. public:
  176. /** Destroys the widget.
  177. Destroying single widgets is not very common. You almost always want to
  178. destroy the parent group instead, which will destroy all of the child widgets
  179. and groups in that group.
  180. \since FLTK 1.3, the widget's destructor removes the widget from its parent
  181. group, if it is member of a group.
  182. */
  183. virtual ~Fl_Widget();
  184. /** Draws the widget.
  185. Never call this function directly. FLTK will schedule redrawing whenever
  186. needed. If your widget must be redrawn as soon as possible, call redraw()
  187. instead.
  188. Override this function to draw your own widgets.
  189. If you ever need to call another widget's draw method <I>from within your
  190. own draw() method</I>, e.g. for an embedded scrollbar, you can do it
  191. (because draw() is virtual) like this:
  192. \code
  193. Fl_Widget *s = &scroll; // scroll is an embedded Fl_Scrollbar
  194. s->draw(); // calls Fl_Scrollbar::draw()
  195. \endcode
  196. */
  197. virtual void draw() = 0;
  198. /** Handles the specified event.
  199. You normally don't call this method directly, but instead let FLTK do
  200. it when the user interacts with the widget.
  201. When implemented in a widget, this function must return 0 if the
  202. widget does not use the event or 1 otherwise.
  203. Most of the time, you want to call the inherited handle() method in
  204. your overridden method so that you don't short-circuit events that you
  205. don't handle. In this last case you should return the callee retval.
  206. \param[in] event the kind of event received
  207. \retval 0 if the event was not used or understood
  208. \retval 1 if the event was used and can be deleted
  209. \see Fl_Event
  210. */
  211. virtual int handle(int event);
  212. /** Returns a pointer to the parent widget.
  213. Usually this is a Fl_Group or Fl_Window.
  214. \retval NULL if the widget has no parent
  215. \see Fl_Group::add(Fl_Widget*)
  216. */
  217. Fl_Group* parent() const {return parent_;}
  218. /** Internal use only - "for hacks only".
  219. It is \em \b STRONGLY recommended not to use this method, because it
  220. short-circuits Fl_Group's normal widget adding and removing methods,
  221. if the widget is already a child widget of another Fl_Group.
  222. Use Fl_Group::add(Fl_Widget*) and/or Fl_Group::remove(Fl_Widget*) instead.
  223. */
  224. void parent(Fl_Group* p) {parent_ = p;} // for hacks only, use Fl_Group::add()
  225. /** Gets the widget type.
  226. Returns the widget type value, which is used for Forms compatibility
  227. and to simulate RTTI.
  228. \todo Explain "simulate RTTI" (currently only used to decide if a widget
  229. is a window, i.e. type()>=FL_WINDOW ?). Is type() really used in a way
  230. that ensures "Forms compatibility" ?
  231. */
  232. uchar type() const {return type_;}
  233. /** Sets the widget type.
  234. This is used for Forms compatibility.
  235. */
  236. void type(uchar t) {type_ = t;}
  237. /** Gets the widget position in its window.
  238. \return the x position relative to the window
  239. */
  240. int x() const {return x_;}
  241. /** Gets the widget position in its window.
  242. \return the y position relative to the window
  243. */
  244. int y() const {return y_;}
  245. /** Gets the widget width.
  246. \return the width of the widget in pixels.
  247. */
  248. int w() const {return w_;}
  249. /** Gets the widget height.
  250. \return the height of the widget in pixels.
  251. */
  252. int h() const {return h_;}
  253. /** Changes the size or position of the widget.
  254. This is a virtual function so that the widget may implement its
  255. own handling of resizing. The default version does \e not
  256. call the redraw() method, but instead relies on the parent widget
  257. to do so because the parent may know a faster way to update the
  258. display, such as scrolling from the old position.
  259. Some window managers under X11 call resize() a lot more often
  260. than needed. Please verify that the position or size of a widget
  261. did actually change before doing any extensive calculations.
  262. position(X, Y) is a shortcut for resize(X, Y, w(), h()),
  263. and size(W, H) is a shortcut for resize(x(), y(), W, H).
  264. \param[in] x, y new position relative to the parent window
  265. \param[in] w, h new size
  266. \see position(int,int), size(int,int)
  267. */
  268. virtual void resize(int x, int y, int w, int h);
  269. /** Internal use only. */
  270. int damage_resize(int,int,int,int);
  271. /** Repositions the window or widget.
  272. position(X, Y) is a shortcut for resize(X, Y, w(), h()).
  273. \param[in] X, Y new position relative to the parent window
  274. \see resize(int,int,int,int), size(int,int)
  275. */
  276. void position(int X,int Y) {resize(X,Y,w_,h_);}
  277. /** Changes the size of the widget.
  278. size(W, H) is a shortcut for resize(x(), y(), W, H).
  279. \param[in] W, H new size
  280. \see position(int,int), resize(int,int,int,int)
  281. */
  282. void size(int W,int H) {resize(x_,y_,W,H);}
  283. /** Gets the label alignment.
  284. \return label alignment
  285. \see label(), align(Fl_Align), Fl_Align
  286. */
  287. Fl_Align align() const {return label_.align_;}
  288. /** Sets the label alignment.
  289. This controls how the label is displayed next to or inside the widget.
  290. The default value is FL_ALIGN_CENTER, which centers the label inside
  291. the widget.
  292. \param[in] alignment new label alignment
  293. \see align(), Fl_Align
  294. */
  295. void align(Fl_Align alignment) {label_.align_ = alignment;}
  296. /** Gets the box type of the widget.
  297. \return the current box type
  298. \see box(Fl_Boxtype), Fl_Boxtype
  299. */
  300. Fl_Boxtype box() const {return (Fl_Boxtype)box_;}
  301. /** Sets the box type for the widget.
  302. This identifies a routine that draws the background of the widget.
  303. See Fl_Boxtype for the available types. The default depends on the
  304. widget, but is usually FL_NO_BOX or FL_UP_BOX.
  305. \param[in] new_box the new box type
  306. \see box(), Fl_Boxtype
  307. */
  308. void box(Fl_Boxtype new_box) {box_ = new_box;}
  309. /** Gets the background color of the widget.
  310. \return current background color
  311. \see color(Fl_Color), color(Fl_Color, Fl_Color)
  312. */
  313. Fl_Color color() const {return color_;}
  314. /** Sets the background color of the widget.
  315. The color is passed to the box routine. The color is either an index into
  316. an internal table of RGB colors or an RGB color value generated using
  317. fl_rgb_color().
  318. The default for most widgets is FL_BACKGROUND_COLOR. Use Fl::set_color()
  319. to redefine colors in the color map.
  320. \param[in] bg background color
  321. \see color(), color(Fl_Color, Fl_Color), selection_color(Fl_Color)
  322. */
  323. void color(Fl_Color bg) {color_ = bg;}
  324. /** Gets the selection color.
  325. \return the current selection color
  326. \see selection_color(Fl_Color), color(Fl_Color, Fl_Color)
  327. */
  328. Fl_Color selection_color() const {return color2_;}
  329. /** Sets the selection color.
  330. The selection color is defined for Forms compatibility and is usually
  331. used to color the widget when it is selected, although some widgets
  332. use this color for other purposes. You can set both colors at once
  333. with color(Fl_Color bg, Fl_Color sel).
  334. \param[in] a the new selection color
  335. \see selection_color(), color(Fl_Color, Fl_Color)
  336. */
  337. void selection_color(Fl_Color a) {color2_ = a;}
  338. /** Sets the background and selection color of the widget.
  339. The two color form sets both the background and selection colors.
  340. \param[in] bg background color
  341. \param[in] sel selection color
  342. \see color(unsigned), selection_color(unsigned)
  343. */
  344. void color(Fl_Color bg, Fl_Color sel) {color_=bg; color2_=sel;}
  345. /** Gets the current label text.
  346. \return a pointer to the current label text
  347. \see label(const char *), copy_label(const char *)
  348. */
  349. const char* label() const {return label_.value;}
  350. /** Sets the current label pointer.
  351. The label is shown somewhere on or next to the widget. The passed pointer
  352. is stored unchanged in the widget (the string is \em not copied), so if
  353. you need to set the label to a formatted value, make sure the buffer is
  354. static, global, or allocated. The copy_label() method can be used
  355. to make a copy of the label string automatically.
  356. \param[in] text pointer to new label text
  357. \see copy_label()
  358. */
  359. void label(const char* text);
  360. /** Sets the current label.
  361. Unlike label(), this method allocates a copy of the label
  362. string instead of using the original string pointer.
  363. The internal copy will automatically be freed whenever you assign
  364. a new label or when the widget is destroyed.
  365. \param[in] new_label the new label text
  366. \see label()
  367. */
  368. void copy_label(const char *new_label);
  369. /** Shortcut to set the label text and type in one call.
  370. \see label(const char *), labeltype(Fl_Labeltype)
  371. */
  372. void label(Fl_Labeltype a, const char* b) {label_.type = a; label_.value = b;}
  373. /** Gets the label type.
  374. \return the current label type.
  375. \see Fl_Labeltype
  376. */
  377. Fl_Labeltype labeltype() const {return (Fl_Labeltype)label_.type;}
  378. /** Sets the label type.
  379. The label type identifies the function that draws the label of the widget.
  380. This is generally used for special effects such as embossing or for using
  381. the label() pointer as another form of data such as an icon. The value
  382. FL_NORMAL_LABEL prints the label as plain text.
  383. \param[in] a new label type
  384. \see Fl_Labeltype
  385. */
  386. void labeltype(Fl_Labeltype a) {label_.type = a;}
  387. /** Gets the label color.
  388. The default color is FL_FOREGROUND_COLOR.
  389. \return the current label color
  390. */
  391. Fl_Color labelcolor() const {return label_.color;}
  392. /** Sets the label color.
  393. The default color is FL_FOREGROUND_COLOR.
  394. \param[in] c the new label color
  395. */
  396. void labelcolor(Fl_Color c) {label_.color=c;}
  397. /** Gets the font to use.
  398. Fonts are identified by indexes into a table. The default value
  399. uses a Helvetica typeface (Arial for Microsoft&reg; Windows&reg;).
  400. The function Fl::set_font() can define new typefaces.
  401. \return current font used by the label
  402. \see Fl_Font
  403. */
  404. Fl_Font labelfont() const {return label_.font;}
  405. /** Sets the font to use.
  406. Fonts are identified by indexes into a table. The default value
  407. uses a Helvetica typeface (Arial for Microsoft&reg; Windows&reg;).
  408. The function Fl::set_font() can define new typefaces.
  409. \param[in] f the new font for the label
  410. \see Fl_Font
  411. */
  412. void labelfont(Fl_Font f) {label_.font=f;}
  413. /** Gets the font size in pixels.
  414. The default size is 14 pixels.
  415. \return the current font size
  416. */
  417. Fl_Fontsize labelsize() const {return label_.size;}
  418. /** Sets the font size in pixels.
  419. \param[in] pix the new font size
  420. \see Fl_Fontsize labelsize()
  421. */
  422. void labelsize(Fl_Fontsize pix) {label_.size=pix;}
  423. /** Gets the image that is used as part of the widget label.
  424. This image is used when drawing the widget in the active state.
  425. \return the current image
  426. */
  427. Fl_Image* image() {return label_.image;}
  428. const Fl_Image* image() const {return label_.image;}
  429. /** Sets the image to use as part of the widget label.
  430. This image is used when drawing the widget in the active state.
  431. \param[in] img the new image for the label
  432. */
  433. void image(Fl_Image* img) {label_.image=img;}
  434. /** Sets the image to use as part of the widget label.
  435. This image is used when drawing the widget in the active state.
  436. \param[in] img the new image for the label
  437. */
  438. void image(Fl_Image& img) {label_.image=&img;}
  439. /** Gets the image that is used as part of the widget label.
  440. This image is used when drawing the widget in the inactive state.
  441. \return the current image for the deactivated widget
  442. */
  443. Fl_Image* deimage() {return label_.deimage;}
  444. const Fl_Image* deimage() const {return label_.deimage;}
  445. /** Sets the image to use as part of the widget label.
  446. This image is used when drawing the widget in the inactive state.
  447. \param[in] img the new image for the deactivated widget
  448. */
  449. void deimage(Fl_Image* img) {label_.deimage=img;}
  450. /** Sets the image to use as part of the widget label.
  451. This image is used when drawing the widget in the inactive state.
  452. \param[in] img the new image for the deactivated widget
  453. */
  454. void deimage(Fl_Image& img) {label_.deimage=&img;}
  455. /** Gets the current tooltip text.
  456. \return a pointer to the tooltip text or NULL
  457. \see tooltip(const char*), copy_tooltip(const char*)
  458. */
  459. const char *tooltip() const {return tooltip_;}
  460. void tooltip(const char *text); // see Fl_Tooltip
  461. void copy_tooltip(const char *text); // see Fl_Tooltip
  462. /** Gets the current callback function for the widget.
  463. Each widget has a single callback.
  464. \return current callback
  465. */
  466. Fl_Callback_p callback() const {return callback_;}
  467. /** Sets the current callback function for the widget.
  468. Each widget has a single callback.
  469. \param[in] cb new callback
  470. \param[in] p user data
  471. */
  472. void callback(Fl_Callback* cb, void* p) {callback_=cb; user_data_=p;}
  473. /** Sets the current callback function for the widget.
  474. Each widget has a single callback.
  475. \param[in] cb new callback
  476. */
  477. void callback(Fl_Callback* cb) {callback_=cb;}
  478. /** Sets the current callback function for the widget.
  479. Each widget has a single callback.
  480. \param[in] cb new callback
  481. */
  482. void callback(Fl_Callback0*cb) {callback_=(Fl_Callback*)cb;}
  483. /** Sets the current callback function for the widget.
  484. Each widget has a single callback.
  485. \param[in] cb new callback
  486. \param[in] p user data
  487. */
  488. void callback(Fl_Callback1*cb, long p=0) {callback_=(Fl_Callback*)cb; user_data_=(void*)p;}
  489. /** Gets the user data for this widget.
  490. Gets the current user data (void *) argument that is passed to the callback function.
  491. \return user data as a pointer
  492. */
  493. void* user_data() const {return user_data_;}
  494. /** Sets the user data for this widget.
  495. Sets the new user data (void *) argument that is passed to the callback function.
  496. \param[in] v new user data
  497. */
  498. void user_data(void* v) {user_data_ = v;}
  499. /** Gets the current user data (long) argument that is passed to the callback function.
  500. */
  501. long argument() const {return (long)(fl_intptr_t)user_data_;}
  502. /** Sets the current user data (long) argument that is passed to the callback function.
  503. \todo The user data value must be implemented using \em intptr_t or similar
  504. to avoid 64-bit machine incompatibilities.
  505. */
  506. void argument(long v) {user_data_ = (void*)v;}
  507. /** Returns the conditions under which the callback is called.
  508. You can set the flags with when(uchar), the default value is
  509. FL_WHEN_RELEASE.
  510. \return set of flags
  511. \see when(uchar)
  512. */
  513. Fl_When when() const {return (Fl_When)when_;}
  514. /** Sets the flags used to decide when a callback is called.
  515. This controls when callbacks are done. The following values are useful,
  516. the default value is FL_WHEN_RELEASE:
  517. \li 0: The callback is not done, but changed() is turned on.
  518. \li FL_WHEN_CHANGED: The callback is done each time the text is
  519. changed by the user.
  520. \li FL_WHEN_RELEASE: The callback will be done when this widget loses
  521. the focus, including when the window is unmapped. This is a useful
  522. value for text fields in a panel where doing the callback on every
  523. change is wasteful. However the callback will also happen if the
  524. mouse is moved out of the window, which means it should not do
  525. anything visible (like pop up an error message).
  526. You might do better setting this to zero, and scanning all the
  527. items for changed() when the OK button on a panel is pressed.
  528. \li FL_WHEN_ENTER_KEY: If the user types the Enter key, the entire
  529. text is selected, and the callback is done if the text has changed.
  530. Normally the Enter key will navigate to the next field (or insert
  531. a newline for a Fl_Multiline_Input) - this changes the behavior.
  532. \li FL_WHEN_ENTER_KEY|FL_WHEN_NOT_CHANGED: The Enter key will do the
  533. callback even if the text has not changed. Useful for command fields.
  534. Fl_Widget::when() is a set of bitflags used by subclasses of
  535. Fl_Widget to decide when to do the callback.
  536. If the value is zero then the callback is never done. Other values
  537. are described in the individual widgets. This field is in the base
  538. class so that you can scan a panel and do_callback() on all the ones
  539. that don't do their own callbacks in response to an "OK" button.
  540. \param[in] i set of flags
  541. */
  542. void when(uchar i) {when_ = i;}
  543. /** Returns whether a widget is visible.
  544. \retval 0 if the widget is not drawn and hence invisible.
  545. \see show(), hide(), visible_r()
  546. */
  547. unsigned int visible() const {return !(flags_&INVISIBLE);}
  548. /** Returns whether a widget and all its parents are visible.
  549. \retval 0 if the widget or any of its parents are invisible.
  550. \see show(), hide(), visible()
  551. */
  552. int visible_r() const;
  553. /** Makes a widget visible.
  554. An invisible widget never gets redrawn and does not get keyboard
  555. or mouse events, but can receive a few other events like FL_SHOW.
  556. The visible() method returns true if the widget is set to be
  557. visible. The visible_r() method returns true if the widget and
  558. all of its parents are visible. A widget is only visible if
  559. visible() is true on it <I>and all of its parents</I>.
  560. Changing it will send FL_SHOW or FL_HIDE events to the widget.
  561. <I>Do not change it if the parent is not visible, as this
  562. will send false FL_SHOW or FL_HIDE events to the widget</I>.
  563. redraw() is called if necessary on this or the parent.
  564. \see hide(), visible(), visible_r()
  565. */
  566. virtual void show();
  567. /** Makes a widget invisible.
  568. \see show(), visible(), visible_r()
  569. */
  570. virtual void hide();
  571. /** Makes the widget visible.
  572. You must still redraw the parent widget to see a change in the
  573. window. Normally you want to use the show() method instead.
  574. */
  575. void set_visible() {flags_ &= ~INVISIBLE;}
  576. /** Hides the widget.
  577. You must still redraw the parent to see a change in the window.
  578. Normally you want to use the hide() method instead.
  579. */
  580. void clear_visible() {flags_ |= INVISIBLE;}
  581. /** Returns whether the widget is active.
  582. \retval 0 if the widget is inactive
  583. \see active_r(), activate(), deactivate()
  584. */
  585. unsigned int active() const {return !(flags_&INACTIVE);}
  586. /** Returns whether the widget and all of its parents are active.
  587. \retval 0 if this or any of the parent widgets are inactive
  588. \see active(), activate(), deactivate()
  589. */
  590. int active_r() const;
  591. /** Activates the widget.
  592. Changing this value will send FL_ACTIVATE to the widget if
  593. active_r() is true.
  594. \see active(), active_r(), deactivate()
  595. */
  596. void activate();
  597. /** Deactivates the widget.
  598. Inactive widgets will be drawn "grayed out", e.g. with less contrast
  599. than the active widget. Inactive widgets will not receive any keyboard
  600. or mouse button events. Other events (including FL_ENTER, FL_MOVE,
  601. FL_LEAVE, FL_SHORTCUT, and others) will still be sent. A widget is
  602. only active if active() is true on it <I>and all of its parents</I>.
  603. Changing this value will send FL_DEACTIVATE to the widget if
  604. active_r() is true.
  605. Currently you cannot deactivate Fl_Window widgets.
  606. \see activate(), active(), active_r()
  607. */
  608. void deactivate();
  609. /** Returns if a widget is used for output only.
  610. output() means the same as !active() except it does not change how the
  611. widget is drawn. The widget will not receive any events. This is useful
  612. for making scrollbars or buttons that work as displays rather than input
  613. devices.
  614. \retval 0 if the widget is used for input and output
  615. \see set_output(), clear_output()
  616. */
  617. unsigned int output() const {return (flags_&OUTPUT);}
  618. /** Sets a widget to output only.
  619. \see output(), clear_output()
  620. */
  621. void set_output() {flags_ |= OUTPUT;}
  622. /** Sets a widget to accept input.
  623. \see set_output(), output()
  624. */
  625. void clear_output() {flags_ &= ~OUTPUT;}
  626. /** Returns if the widget is able to take events.
  627. This is the same as (active() && !output() && visible())
  628. but is faster.
  629. \retval 0 if the widget takes no events
  630. */
  631. unsigned int takesevents() const {return !(flags_&(INACTIVE|INVISIBLE|OUTPUT));}
  632. /**
  633. Checks if the widget value changed since the last callback.
  634. "Changed" is a flag that is turned on when the user changes the value
  635. stored in the widget. This is only used by subclasses of Fl_Widget that
  636. store values, but is in the base class so it is easier to scan all the
  637. widgets in a panel and do_callback() on the changed ones in response
  638. to an "OK" button.
  639. Most widgets turn this flag off when they do the callback, and when
  640. the program sets the stored value.
  641. \retval 0 if the value did not change
  642. \see set_changed(), clear_changed()
  643. */
  644. unsigned int changed() const {return flags_&CHANGED;}
  645. /** Marks the value of the widget as changed.
  646. \see changed(), clear_changed()
  647. */
  648. void set_changed() {flags_ |= CHANGED;}
  649. /** Marks the value of the widget as unchanged.
  650. \see changed(), set_changed()
  651. */
  652. void clear_changed() {flags_ &= ~CHANGED;}
  653. /** Gives the widget the keyboard focus.
  654. Tries to make this widget be the Fl::focus() widget, by first sending
  655. it an FL_FOCUS event, and if it returns non-zero, setting
  656. Fl::focus() to this widget. You should use this method to
  657. assign the focus to a widget.
  658. \return true if the widget accepted the focus.
  659. */
  660. int take_focus();
  661. /** Enables keyboard focus navigation with this widget.
  662. Note, however, that this will not necessarily mean that the widget
  663. will accept focus, but for widgets that can accept focus, this method
  664. enables it if it has been disabled.
  665. \see visible_focus(), clear_visible_focus(), visible_focus(int)
  666. */
  667. void set_visible_focus() { flags_ |= VISIBLE_FOCUS; }
  668. /** Disables keyboard focus navigation with this widget.
  669. Normally, all widgets participate in keyboard focus navigation.
  670. \see set_visible_focus(), visible_focus(), visible_focus(int)
  671. */
  672. void clear_visible_focus() { flags_ &= ~VISIBLE_FOCUS; }
  673. /** Modifies keyboard focus navigation.
  674. \param[in] v set or clear visible focus
  675. \see set_visible_focus(), clear_visible_focus(), visible_focus()
  676. */
  677. void visible_focus(int v) { if (v) set_visible_focus(); else clear_visible_focus(); }
  678. /** Checks whether this widget has a visible focus.
  679. \retval 0 if this widget has no visible focus.
  680. \see visible_focus(int), set_visible_focus(), clear_visible_focus()
  681. */
  682. unsigned int visible_focus() { return flags_ & VISIBLE_FOCUS; }
  683. /** Sets the default callback for all widgets.
  684. Sets the default callback, which puts a pointer to the widget on the queue
  685. returned by Fl::readqueue(). You may want to call this from your own callback.
  686. \param[in] cb the new callback
  687. \param[in] d user data associated with that callback
  688. \see callback(), do_callback(), Fl::readqueue()
  689. */
  690. static void default_callback(Fl_Widget *cb, void *d);
  691. /** Calls the widget callback.
  692. Causes a widget to invoke its callback function with default arguments.
  693. \see callback()
  694. */
  695. void do_callback() {do_callback(this,user_data_);}
  696. /** Calls the widget callback.
  697. Causes a widget to invoke its callback function with arbitrary arguments.
  698. \param[in] o call the callback with \p o as the widget argument
  699. \param[in] arg call the callback with \p arg as the user data argument
  700. \see callback()
  701. */
  702. void do_callback(Fl_Widget* o,long arg) {do_callback(o,(void*)arg);}
  703. // Causes a widget to invoke its callback function with arbitrary arguments.
  704. // Documentation and implementation in Fl_Widget.cxx
  705. void do_callback(Fl_Widget* o,void* arg=0);
  706. /* Internal use only. */
  707. int test_shortcut();
  708. /* Internal use only. */
  709. static unsigned int label_shortcut(const char *t);
  710. /* Internal use only. */
  711. static int test_shortcut(const char*, const bool require_alt = false);
  712. /** Checks if w is a child of this widget.
  713. \param[in] w potential child widget
  714. \return Returns 1 if \p w is a child of this widget, or is
  715. equal to this widget. Returns 0 if \p w is NULL.
  716. */
  717. int contains(const Fl_Widget *w) const ;
  718. /** Checks if this widget is a child of w.
  719. Returns 1 if this widget is a child of \p w, or is
  720. equal to \p w. Returns 0 if \p w is NULL.
  721. \param[in] w the possible parent widget.
  722. \see contains()
  723. */
  724. int inside(const Fl_Widget* w) const {return w ? w->contains(this) : 0;}
  725. /** Schedules the drawing of the widget.
  726. Marks the widget as needing its draw() routine called.
  727. */
  728. void redraw();
  729. /** Schedules the drawing of the label.
  730. Marks the widget or the parent as needing a redraw for the label area
  731. of a widget.
  732. */
  733. void redraw_label();
  734. /** Returns non-zero if draw() needs to be called.
  735. The damage value is actually a bit field that the widget
  736. subclass can use to figure out what parts to draw.
  737. \return a bitmap of flags describing the kind of damage to the widget
  738. \see damage(uchar), clear_damage(uchar)
  739. */
  740. uchar damage() const {return damage_;}
  741. /** Clears or sets the damage flags.
  742. Damage flags are cleared when parts of the widget drawing is repaired.
  743. The optional argument \p c specifies the bits that <b>are set</b>
  744. after the call (default: 0) and \b not the bits that are cleared!
  745. \note Therefore it is possible to set damage bits with this method, but
  746. this should be avoided. Use damage(uchar) instead.
  747. \param[in] c new bitmask of damage flags (default: 0)
  748. \see damage(uchar), damage()
  749. */
  750. void clear_damage(uchar c = 0) {damage_ = c;}
  751. /** Sets the damage bits for the widget.
  752. Setting damage bits will schedule the widget for the next redraw.
  753. \param[in] c bitmask of flags to set
  754. \see damage(), clear_damage(uchar)
  755. */
  756. void damage(uchar c);
  757. /** Sets the damage bits for an area inside the widget.
  758. Setting damage bits will schedule the widget for the next redraw.
  759. \param[in] c bitmask of flags to set
  760. \param[in] x, y, w, h size of damaged area
  761. \see damage(), clear_damage(uchar)
  762. */
  763. void damage(uchar c, int x, int y, int w, int h);
  764. void draw_label(int, int, int, int, Fl_Align) const;
  765. /** Sets width ww and height hh accordingly with the label size.
  766. Labels with images will return w() and h() of the image.
  767. */
  768. void measure_label(int& ww, int& hh) const {label_.measure(ww, hh);}
  769. /** Returns a pointer to the primary Fl_Window widget.
  770. \retval NULL if no window is associated with this widget.
  771. \note for an Fl_Window widget, this returns its <I>parent</I> window
  772. (if any), not <I>this</I> window.
  773. */
  774. Fl_Window* window() const ;
  775. /** Returns an Fl_Group pointer if this widget is an Fl_Group.
  776. Use this method if you have a widget (pointer) and need to
  777. know whether this widget is derived from Fl_Group. If it returns
  778. non-NULL, then the widget in question is derived from Fl_Group,
  779. and you can use the returned pointer to access its children
  780. or other Fl_Group-specific methods.
  781. Example:
  782. \code
  783. void my_callback (Fl_Widget *w, void *) {
  784. Fl_Group *g = w->as_group();
  785. if (g)
  786. printf ("This group has %d children\n",g->children());
  787. else
  788. printf ("This widget is not a group!\n");
  789. }
  790. \endcode
  791. \retval NULL if this widget is not derived from Fl_Group.
  792. \note This method is provided to avoid dynamic_cast.
  793. \see Fl_Widget::as_window(), Fl_Widget::as_gl_window()
  794. */
  795. virtual Fl_Group* as_group() {return 0;}
  796. /** Returns an Fl_Window pointer if this widget is an Fl_Window.
  797. Use this method if you have a widget (pointer) and need to
  798. know whether this widget is derived from Fl_Window. If it returns
  799. non-NULL, then the widget in question is derived from Fl_Window,
  800. and you can use the returned pointer to access its children
  801. or other Fl_Window-specific methods.
  802. \retval NULL if this widget is not derived from Fl_Window.
  803. \note This method is provided to avoid dynamic_cast.
  804. \see Fl_Widget::as_group(), Fl_Widget::as_gl_window()
  805. */
  806. virtual Fl_Window* as_window() {return 0;}
  807. /** Returns an Fl_Gl_Window pointer if this widget is an Fl_Gl_Window.
  808. Use this method if you have a widget (pointer) and need to
  809. know whether this widget is derived from Fl_Gl_Window. If it returns
  810. non-NULL, then the widget in question is derived from Fl_Gl_Window.
  811. \retval NULL if this widget is not derived from Fl_Gl_Window.
  812. \note This method is provided to avoid dynamic_cast.
  813. \see Fl_Widget::as_group(), Fl_Widget::as_window()
  814. */
  815. virtual class Fl_Gl_Window* as_gl_window() {return 0;}
  816. /** For back compatibility only.
  817. \deprecated Use selection_color() instead.
  818. */
  819. Fl_Color color2() const {return (Fl_Color)color2_;}
  820. /** For back compatibility only.
  821. \deprecated Use selection_color(unsigned) instead.
  822. */
  823. void color2(unsigned a) {color2_ = a;}
  824. };
  825. /**
  826. Reserved type numbers (necessary for my cheapo RTTI) start here.
  827. Grep the header files for "RESERVED_TYPE" to find the next available
  828. number.
  829. */
  830. #define FL_RESERVED_TYPE 100
  831. #endif
  832. //
  833. // End of "$Id: Fl_Widget.H 8623 2011-04-24 17:09:41Z AlbrechtS $".
  834. //