fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Widget.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Base widget class for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 #include <FL/Fl.H>
20 #include <FL/Fl_Widget.H>
21 #include <FL/Fl_Group.H>
22 #include <FL/Fl_Tooltip.H>
23 #include <FL/fl_draw.H>
24 #include <stdlib.h>
25 #include "flstring.h"
26 
27 
29 // for compatibility with Forms, all widgets without callbacks are
30 // inserted into a "queue" when they are activated, and the forms
31 // compatibility interaction functions (fl_do_events, etc.) will
32 // read one widget at a time from this queue and return it:
33 
34 const int QUEUE_SIZE = 20;
35 
37 static int obj_head, obj_tail;
38 
39 void Fl_Widget::default_callback(Fl_Widget *o, void * /*v*/) {
40 #if 0
41  // This is necessary for strict forms compatibility but is confusing.
42  // Use the parent's callback if this widget does not have one.
43  for (Fl_Widget *p = o->parent(); p; p = p->parent())
44  if (p->callback() != default_callback) {
45  p->do_callback(o,v);
46  return;
47  }
48 #endif
49  obj_queue[obj_head++] = o;
50  if (obj_head >= QUEUE_SIZE) obj_head = 0;
51  if (obj_head == obj_tail) {
52  obj_tail++;
53  if (obj_tail >= QUEUE_SIZE) obj_tail = 0;
54  }
55 }
77  if (obj_tail==obj_head) return 0;
79  if (obj_tail >= QUEUE_SIZE) obj_tail = 0;
80  return o;
81 }
82 /*
83  This static internal function removes all pending callbacks for a
84  specific widget from the default callback queue (Fl::readqueue()).
85  It is only called from Fl_Widget's destructor if the widget
86  doesn't have an own callback.
87  Note: There's no need to have this in the Fl:: namespace.
88 */
89 static void cleanup_readqueue(Fl_Widget *w) {
90 
91  if (obj_tail==obj_head) return;
92 
93  // Read the entire queue and copy over all valid entries.
94  // The new head will be determined after the last copied entry.
95 
96  int old_head = obj_head; // save newest entry
97  int entry = obj_tail; // oldest entry
98  obj_head = obj_tail; // new queue start
99  for (;;) {
100  Fl_Widget *o = obj_queue[entry++];
101  if (entry >= QUEUE_SIZE) entry = 0;
102  if (o != w) { // valid entry
103  obj_queue[obj_head++] = o;
104  if (obj_head >= QUEUE_SIZE) obj_head = 0;
105  } // valid entry
106  if (entry == old_head) break;
107  }
108  return;
109 }
111 
113  return 0;
114 }
115 
118 
119 Fl_Widget::Fl_Widget(int X, int Y, int W, int H, const char* L) {
120 
121  x_ = X; y_ = Y; w_ = W; h_ = H;
122 
123  label_.value = L;
124  label_.image = 0;
125  label_.deimage = 0;
131  tooltip_ = 0;
133  user_data_ = 0;
134  type_ = 0;
136  damage_ = 0;
137  box_ = FL_NO_BOX;
138  color_ = FL_GRAY;
139  color2_ = FL_GRAY;
141 
142  parent_ = 0;
143  if (Fl_Group::current()) Fl_Group::current()->add(this);
144  if (!fl_graphics_driver) {
145  // Make sure fl_graphics_driver is initialized. Important if we are called by a static initializer.
147  }
148 }
149 
150 void Fl_Widget::resize(int X, int Y, int W, int H) {
151  x_ = X; y_ = Y; w_ = W; h_ = H;
152 }
153 
154 // this is useful for parent widgets to call to resize children:
155 int Fl_Widget::damage_resize(int X, int Y, int W, int H) {
156  if (x() == X && y() == Y && w() == W && h() == H) return 0;
157  resize(X, Y, W, H);
158  redraw();
159  return 1;
160 }
161 
163  if (!takesevents()) return 0;
164  if (!visible_focus()) return 0;
165  if (!handle(FL_FOCUS)) return 0; // see if it wants it
166  if (contains(Fl::focus())) return 1; // it called Fl::focus for us
167  Fl::focus(this);
168  return 1;
169 }
170 
171 extern void fl_throw_focus(Fl_Widget*); // in Fl_x.cxx
172 
180  if (flags() & COPIED_LABEL) free((void *)(label_.value));
181  if (flags() & COPIED_TOOLTIP) free((void *)(tooltip_));
182  // remove from parent group
183  if (parent_) parent_->remove(this);
184 #ifdef DEBUG_DELETE
185  if (parent_) { // this should never happen
186  printf("*** Fl_Widget: parent_->remove(this) failed [%p,%p]\n",parent_,this);
187  }
188 #endif // DEBUG_DELETE
189  parent_ = 0; // Don't throw focus to a parent widget.
190  fl_throw_focus(this);
191  // remove stale entries from default callback queue (Fl::readqueue())
193 }
194 
196 void
197 Fl_Widget::draw_focus(Fl_Boxtype B, int X, int Y, int W, int H) const {
198  if (!Fl::visible_focus()) return;
199  switch (B) {
200  case FL_DOWN_BOX:
201  case FL_DOWN_FRAME:
202  case FL_THIN_DOWN_BOX:
203  case FL_THIN_DOWN_FRAME:
204  X ++;
205  Y ++;
206  default:
207  break;
208  }
209 
211 
212 #if defined(USE_X11) || defined(__APPLE_QUARTZ__)
214  fl_rect(X + Fl::box_dx(B), Y + Fl::box_dy(B),
215  W - Fl::box_dw(B) - 1, H - Fl::box_dh(B) - 1);
217 #elif defined(WIN32)
218  // Windows 95/98/ME do not implement the dotted line style, so draw
219  // every other pixel around the focus area...
220  //
221  // Also, QuickDraw (MacOS) does not support line styles specifically,
222  // and the hack we use in fl_line_style() will not draw horizontal lines
223  // on odd-numbered rows...
224  int i, xx, yy;
225 
226  X += Fl::box_dx(B);
227  Y += Fl::box_dy(B);
228  W -= Fl::box_dw(B) + 2;
229  H -= Fl::box_dh(B) + 2;
230 
231  for (xx = 0, i = 1; xx < W; xx ++, i ++) if (i & 1) fl_point(X + xx, Y);
232  for (yy = 0; yy < H; yy ++, i ++) if (i & 1) fl_point(X + W, Y + yy);
233  for (xx = W; xx > 0; xx --, i ++) if (i & 1) fl_point(X + xx, Y + H);
234  for (yy = H; yy > 0; yy --, i ++) if (i & 1) fl_point(X, Y + yy);
235 #else
236 # error unsupported platform
237 #endif // WIN32
238 }
239 
240 
242  if (!active()) {
244  if (active_r()) {
245  redraw();
246  redraw_label();
248  if (inside(Fl::focus())) Fl::focus()->take_focus();
249  }
250  }
251 }
252 
254  if (active_r()) {
256  redraw();
257  redraw_label();
259  fl_throw_focus(this);
260  } else {
262  }
263 }
264 
265 int Fl_Widget::active_r() const {
266  for (const Fl_Widget* o = this; o; o = o->parent())
267  if (!o->active()) return 0;
268  return 1;
269 }
270 
272  if (!visible()) {
274  if (visible_r()) {
275  redraw();
276  redraw_label();
277  handle(FL_SHOW);
278  if (inside(Fl::focus())) Fl::focus()->take_focus();
279  }
280  }
281 }
282 
284  if (visible_r()) {
286  for (Fl_Widget *p = parent(); p; p = p->parent())
287  if (p->box() || !p->parent()) {p->redraw(); break;}
288  handle(FL_HIDE);
289  fl_throw_focus(this);
290  } else {
292  }
293 }
294 
295 int Fl_Widget::visible_r() const {
296  for (const Fl_Widget* o = this; o; o = o->parent())
297  if (!o->visible()) return 0;
298  return 1;
299 }
300 
301 // return true if widget is inside (or equal to) this:
302 // Returns false for null widgets.
303 int Fl_Widget::contains(const Fl_Widget *o) const {
304  for (; o; o = o->parent_) if (o == this) return 1;
305  return 0;
306 }
307 
308 
309 void
310 Fl_Widget::label(const char *a) {
311  if (flags() & COPIED_LABEL) {
312  // reassigning a copied label remains the same copied label
313  if (label_.value == a)
314  return;
315  free((void *)(label_.value));
317  }
318  label_.value=a;
319  redraw_label();
320 }
321 
322 
323 void
324 Fl_Widget::copy_label(const char *a) {
325  // reassigning a copied label remains the same copied label
326  if ((flags() & COPIED_LABEL) && (label_.value == a))
327  return;
328  if (a) {
329  label(strdup(a));
331  } else {
332  label(0);
333  }
334 }
335 
344 void
346  if (!callback_) return;
347  Fl_Widget_Tracker wp(this);
348  callback_(o,arg);
349  if (wp.deleted()) return;
351  clear_changed();
352 }
353 
354 //
355 // End of "$Id$".
356 //
Fl_Widget.H
Fl_Widget::y
int y() const
Definition: Fl_Widget.H:289
Fl.H
Fl_Widget::activate
void activate()
Definition: Fl_Widget.cxx:241
Fl_Widget::x_
int x_
Definition: Fl_Widget.H:107
FL_HIDE
Definition: Enumerations.H:369
Fl_Widget::redraw_label
void redraw_label()
Definition: Fl.cxx:1786
Fl_Widget::h_
int h_
Definition: Fl_Widget.H:107
Fl_Widget::~Fl_Widget
virtual ~Fl_Widget()
Definition: Fl_Widget.cxx:178
Fl_Widget::takesevents
unsigned int takesevents() const
Definition: Fl_Widget.H:764
Fl_Widget::show
virtual void show()
Definition: Fl_Widget.cxx:271
Fl::box_dx
static int box_dx(Fl_Boxtype)
Definition: fl_boxtype.cxx:360
Fl_Boxtype
Fl_Boxtype
Definition: Enumerations.H:603
fl_color
void fl_color(Fl_Color c)
Definition: fl_draw.H:52
Fl::readqueue
static Fl_Widget * readqueue()
Definition: Fl_Widget.cxx:76
Fl_Widget::clear_changed
void clear_changed()
Definition: Fl_Widget.H:791
FL_WHEN_RELEASE
Do the callback when the button or key is released and the value changes.
Definition: Enumerations.H:443
FL_SOLID
line style: ___________
Definition: fl_draw.H:179
Fl_Label::type
uchar type
Definition: Fl_Widget.H:81
free
void free()
H
static int H
Definition: Fl_Tooltip.cxx:76
cleanup_readqueue
static void cleanup_readqueue(Fl_Widget *w)
Definition: Fl_Widget.cxx:89
Fl_Widget::callback_
Fl_Callback * callback_
Definition: Fl_Widget.H:105
fl_point
void fl_point(int x, int y)
Definition: fl_draw.H:147
Fl_Widget::set_flag
void set_flag(unsigned int c)
Definition: Fl_Widget.H:149
Fl_Widget::x
int x() const
Definition: Fl_Widget.H:284
FL_DOT
line style: . . . . . .
Definition: fl_draw.H:181
Fl_Widget::color_
Fl_Color color_
Definition: Fl_Widget.H:110
Fl_Label::color
Fl_Color color
Definition: Fl_Widget.H:77
Fl_Widget::do_callback
void do_callback()
Definition: Fl_Widget.H:861
fl_contrast
Fl_Color fl_contrast(Fl_Color fg, Fl_Color bg)
Definition: fl_color.cxx:435
Fl_Label::image
Fl_Image * image
Definition: Fl_Widget.H:69
obj_head
static int obj_head
Definition: Fl_Widget.cxx:37
FL_BLACK
const Fl_Color FL_BLACK
Definition: Enumerations.H:956
Fl_Widget_Tracker::deleted
int deleted()
Definition: Fl.H:1396
Fl_Widget::COPIED_LABEL
the widget label is internally copied, its destruction is handled by the widget
Definition: Fl_Widget.H:166
Fl_Widget::flags_
unsigned int flags_
Definition: Fl_Widget.H:109
Fl_Widget::label_
Fl_Label label_
Definition: Fl_Widget.H:108
Fl_Display_Device::display_device
static Fl_Display_Device * display_device()
Definition: Fl_Device.cxx:83
Fl::box_dh
static int box_dh(Fl_Boxtype)
Definition: fl_boxtype.cxx:397
QUEUE_SIZE
const int QUEUE_SIZE
Definition: Fl_Widget.cxx:34
Fl_Widget::w
int w() const
Definition: Fl_Widget.H:294
Fl_Label::deimage
Fl_Image * deimage
Definition: Fl_Widget.H:71
FL_FOREGROUND_COLOR
const Fl_Color FL_FOREGROUND_COLOR
the default foreground color (0) used for labels and text
Definition: Enumerations.H:937
Fl::box_dw
static int box_dw(Fl_Boxtype)
Definition: fl_boxtype.cxx:391
obj_queue
static Fl_Widget * obj_queue[QUEUE_SIZE]
Definition: Fl_Widget.cxx:36
Fl_Widget::color
Fl_Color color() const
Definition: Fl_Widget.H:378
Fl_Widget::copy_label
void copy_label(const char *new_label)
Definition: Fl_Widget.cxx:324
FL_NORMAL_SIZE
Fl_Fontsize FL_NORMAL_SIZE
normal font size
Definition: Fl_Widget.cxx:117
p
static menustate * p
Definition: Fl_Menu.cxx:606
Fl::box_dy
static int box_dy(Fl_Boxtype)
Definition: fl_boxtype.cxx:385
FL_DEACTIVATE
Definition: Enumerations.H:356
Fl_Widget::color2_
Fl_Color color2_
Definition: Fl_Widget.H:111
Fl_Widget::parent_
Fl_Group * parent_
Definition: Fl_Widget.H:104
fl_draw.H
utility header to pull drawing functions together
Fl_Widget::flags
unsigned int flags() const
Definition: Fl_Widget.H:147
FL_HELVETICA
const Fl_Font FL_HELVETICA
Helvetica (or Arial) normal (0)
Definition: Enumerations.H:879
FL_DOWN_BOX
see figure 1
Definition: Enumerations.H:608
arg
static int arg(int argc, char **argv, int &i)
Definition: fluid.cxx:1723
Fl_Widget::active_r
int active_r() const
Definition: Fl_Widget.cxx:265
Fl_Widget::COPIED_TOOLTIP
the widget tooltip is internally copied, its destruction is handled by the widget
Definition: Fl_Widget.H:173
Fl_Label::size
Fl_Fontsize size
Definition: Fl_Widget.H:75
Fl_Widget::y_
int y_
Definition: Fl_Widget.H:107
FL_FOCUS
Definition: Enumerations.H:283
Fl_Widget::visible
unsigned int visible() const
Definition: Fl_Widget.H:660
Fl_Widget::hide
virtual void hide()
Definition: Fl_Widget.cxx:283
fl_line_style
void fl_line_style(int style, int width=0, char *dashes=0)
Definition: fl_draw.H:177
Fl_Fontsize
int Fl_Fontsize
Definition: Enumerations.H:906
Fl_Widget::type_
uchar type_
Definition: Fl_Widget.H:112
Fl_Widget::redraw
void redraw()
Definition: Fl.cxx:1782
Fl_Widget
Definition: Fl_Widget.H:101
Fl_Widget::deactivate
void deactivate()
Definition: Fl_Widget.cxx:253
Fl_Group::current
static Fl_Group * current()
Definition: Fl_Group.cxx:84
fl_graphics_driver
FL_EXPORT Fl_Graphics_Driver * fl_graphics_driver
Points to the driver that currently receives all graphics requests.
Definition: Fl_Device.cxx:50
FL_NO_BOX
nothing is drawn at all, this box is invisible
Definition: Enumerations.H:605
Fl::focus
static Fl_Widget * focus()
Definition: Fl.H:840
Fl_Group::add
void add(Fl_Widget &)
Definition: Fl_Group.cxx:491
Fl_Widget::h
int h() const
Definition: Fl_Widget.H:299
Fl_Widget_Tracker
Definition: Fl.H:1371
Fl_Widget::label
const char * label() const
Definition: Fl_Widget.H:421
Fl_Tooltip.H
FL_SHOW
Definition: Enumerations.H:377
Fl_Widget::handle
virtual int handle(int event)
Definition: Fl_Widget.cxx:112
Fl_Widget::tooltip_
const char * tooltip_
Definition: Fl_Widget.H:117
Fl_Widget::active
unsigned int active() const
Definition: Fl_Widget.H:708
Fl_Widget::default_callback
static void default_callback(Fl_Widget *cb, void *d)
Definition: Fl_Widget.cxx:39
Fl_Widget::visible_r
int visible_r() const
Definition: Fl_Widget.cxx:295
Fl_Widget::damage_
uchar damage_
Definition: Fl_Widget.H:113
Fl_Widget::w_
int w_
Definition: Fl_Widget.H:107
FL_ALIGN_CENTER
const Fl_Align FL_ALIGN_CENTER
Definition: Enumerations.H:830
Fl_Widget::VISIBLE_FOCUS
accepts keyboard focus navigation if the widget can have the focus
Definition: Fl_Widget.H:165
Fl_Group.H
Fl_Label::value
const char * value
Definition: Fl_Widget.H:67
Fl_Widget::user_data_
void * user_data_
Definition: Fl_Widget.H:106
Fl::visible_focus
static int visible_focus()
Definition: Fl.H:1197
FL_NORMAL_LABEL
draws the text (0)
Definition: Enumerations.H:764
Fl_Widget::visible_focus
unsigned int visible_focus()
Definition: Fl_Widget.H:838
Fl_Widget::contains
int contains(const Fl_Widget *w) const
Definition: Fl_Widget.cxx:303
Fl_Label::font
Fl_Font font
Definition: Fl_Widget.H:73
FL_GRAY
#define FL_GRAY
Definition: Enumerations.H:978
Fl::clear_widget_pointer
static void clear_widget_pointer(Fl_Widget const *w)
Definition: Fl.cxx:2139
Fl_Widget::parent
Fl_Group * parent() const
Definition: Fl_Widget.H:254
FL_ACTIVATE
Definition: Enumerations.H:361
Y
static int Y
Definition: Fl_Tooltip.cxx:76
flstring.h
obj_tail
static int obj_tail
Definition: Fl_Widget.cxx:37
Fl_Widget::inside
int inside(const Fl_Widget *wgt) const
Definition: Fl_Widget.H:898
Fl_Group::remove
void remove(int index)
Definition: Fl_Group.cxx:503
Fl_Widget::resize
virtual void resize(int x, int y, int w, int h)
Definition: Fl_Widget.cxx:150
Fl_Widget::when_
uchar when_
Definition: Fl_Widget.H:115
Fl_Widget::Fl_Widget
Fl_Widget(const Fl_Widget &)
Fl_Widget::take_focus
int take_focus()
Definition: Fl_Widget.cxx:162
Fl_Widget::damage_resize
int damage_resize(int, int, int, int)
Definition: Fl_Widget.cxx:155
Fl_Widget::box_
uchar box_
Definition: Fl_Widget.H:114
FL_THIN_DOWN_FRAME
see figure 1
Definition: Enumerations.H:614
Fl_Widget::INACTIVE
the widget can't receive focus, and is disabled but potentially visible
Definition: Fl_Widget.H:156
Fl_Widget::INVISIBLE
the widget is not drawn, but can receive a few special events
Definition: Fl_Widget.H:157
Fl_Widget::clear_flag
void clear_flag(unsigned int c)
Definition: Fl_Widget.H:151
fl_throw_focus
void fl_throw_focus(Fl_Widget *)
Definition: Fl.cxx:1149
Fl_Label::align_
Fl_Align align_
Definition: Fl_Widget.H:79
Fl_Widget::draw_focus
void draw_focus()
Definition: Fl_Widget.H:186
FL_DOWN_FRAME
see figure 1
Definition: Enumerations.H:610
fl_rect
void fl_rect(int x, int y, int w, int h)
Definition: fl_draw.H:201
FL_THIN_DOWN_BOX
see figure 1
Definition: Enumerations.H:612