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