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.

Fl_Widget.H 37KB

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