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.

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