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