PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/fltk/src/Fl_Group.cxx

http://luafltk.googlecode.com/
C++ | 828 lines | 545 code | 78 blank | 205 comment | 218 complexity | e023b90158e09a0759dd87a59364de5e MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, 0BSD
  1. //
  2. // "$Id: Fl_Group.cxx 7469 2010-04-07 23:17:33Z matt $"
  3. //
  4. // Group widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2009 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. // The Fl_Group is the only defined container type in FLTK.
  28. // Fl_Window itself is a subclass of this, and most of the event
  29. // handling is designed so windows themselves work correctly.
  30. #include <stdio.h>
  31. #include <FL/Fl.H>
  32. #include <FL/Fl_Group.H>
  33. #include <FL/Fl_Window.H>
  34. #include <FL/fl_draw.H>
  35. #include <stdlib.h>
  36. Fl_Group* Fl_Group::current_;
  37. // Hack: A single child is stored in the pointer to the array, while
  38. // multiple children are stored in an allocated array:
  39. /**
  40. Returns a pointer to the array of children. <I>This pointer is only
  41. valid until the next time a child is added or removed.</I>
  42. */
  43. Fl_Widget*const* Fl_Group::array() const {
  44. return children_ <= 1 ? (Fl_Widget**)(&array_) : array_;
  45. }
  46. /**
  47. Searches the child array for the widget and returns the index. Returns children()
  48. if the widget is NULL or not found.
  49. */
  50. int Fl_Group::find(const Fl_Widget* o) const {
  51. Fl_Widget*const* a = array();
  52. int i; for (i=0; i < children_; i++) if (*a++ == o) break;
  53. return i;
  54. }
  55. // Metrowerks CodeWarrior and others can't export the static
  56. // class member: current_, so these methods can't be inlined...
  57. /**
  58. Sets the current group so you can build the widget
  59. tree by just constructing the widgets.
  60. begin() is automatically called by the constructor for Fl_Group (and thus for
  61. Fl_Window as well). begin() <I>is exactly the same as</I> current(this).
  62. <I>Don't forget to end() the group or window!</I>
  63. */
  64. void Fl_Group::begin() {current_ = this;}
  65. /**
  66. <I>Exactly the same as</I> current(this->parent()). Any new widgets
  67. added to the widget tree will be added to the parent of the group.
  68. */
  69. void Fl_Group::end() {current_ = parent();}
  70. /**
  71. Returns the currently active group.
  72. The Fl_Widget constructor automatically does current()->add(widget) if this
  73. is not null. To prevent new widgets from being added to a group, call
  74. Fl_Group::current(0).
  75. */
  76. Fl_Group *Fl_Group::current() {return current_;}
  77. /**
  78. See static Fl_Group *Fl_Group::current()
  79. */
  80. void Fl_Group::current(Fl_Group *g) {current_ = g;}
  81. extern Fl_Widget* fl_oldfocus; // set by Fl::focus
  82. // For back-compatibility, we must adjust all events sent to child
  83. // windows so they are relative to that window.
  84. static int send(Fl_Widget* o, int event) {
  85. if (o->type() < FL_WINDOW) return o->handle(event);
  86. switch ( event )
  87. {
  88. case FL_DND_ENTER: /* FALLTHROUGH */
  89. case FL_DND_DRAG:
  90. // figure out correct type of event:
  91. event = (o->contains(Fl::belowmouse())) ? FL_DND_DRAG : FL_DND_ENTER;
  92. }
  93. int save_x = Fl::e_x; Fl::e_x -= o->x();
  94. int save_y = Fl::e_y; Fl::e_y -= o->y();
  95. int ret = o->handle(event);
  96. Fl::e_y = save_y;
  97. Fl::e_x = save_x;
  98. switch ( event )
  99. {
  100. case FL_ENTER: /* FALLTHROUGH */
  101. case FL_DND_ENTER:
  102. // Successful completion of FL_ENTER means the widget is now the
  103. // belowmouse widget, but only call Fl::belowmouse if the child
  104. // widget did not do so:
  105. if (!o->contains(Fl::belowmouse())) Fl::belowmouse(o);
  106. break;
  107. }
  108. return ret;
  109. }
  110. // translate the current keystroke into up/down/left/right for navigation:
  111. #define ctrl(x) (x^0x40)
  112. static int navkey() {
  113. switch (Fl::event_key()) {
  114. case 0: // not an FL_KEYBOARD/FL_SHORTCUT event
  115. break;
  116. case FL_Tab:
  117. if (!Fl::event_state(FL_SHIFT)) return FL_Right;
  118. case 0xfe20: // XK_ISO_Left_Tab
  119. return FL_Left;
  120. case FL_Right:
  121. return FL_Right;
  122. case FL_Left:
  123. return FL_Left;
  124. case FL_Up:
  125. return FL_Up;
  126. case FL_Down:
  127. return FL_Down;
  128. }
  129. return 0;
  130. }
  131. int Fl_Group::handle(int event) {
  132. if(user_handler_) {
  133. int result = (*user_handler_)(this, event);
  134. if (result) return result;
  135. }
  136. Fl_Widget*const* a = array();
  137. int i;
  138. Fl_Widget* o;
  139. switch (event) {
  140. case FL_FOCUS:
  141. switch (navkey()) {
  142. default:
  143. if (savedfocus_ && savedfocus_->take_focus()) return 1;
  144. case FL_Right:
  145. case FL_Down:
  146. for (i = children(); i--;) if ((*a++)->take_focus()) return 1;
  147. break;
  148. case FL_Left:
  149. case FL_Up:
  150. for (i = children(); i--;) if (a[i]->take_focus()) return 1;
  151. break;
  152. }
  153. return 0;
  154. case FL_UNFOCUS:
  155. savedfocus_ = fl_oldfocus;
  156. return 0;
  157. case FL_KEYBOARD:
  158. return navigation(navkey());
  159. case FL_SHORTCUT:
  160. for (i = children(); i--;) {
  161. o = a[i];
  162. if (o->takesevents() && Fl::event_inside(o) && send(o,FL_SHORTCUT))
  163. return 1;
  164. }
  165. for (i = children(); i--;) {
  166. o = a[i];
  167. if (o->takesevents() && !Fl::event_inside(o) && send(o,FL_SHORTCUT))
  168. return 1;
  169. }
  170. if ((Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter))
  171. //return navigation(FL_Down);
  172. return navigation(FL_Right);
  173. return 0;
  174. case FL_ENTER:
  175. case FL_MOVE:
  176. for (i = children(); i--;) {
  177. o = a[i];
  178. if (o->visible() && Fl::event_inside(o)) {
  179. if (o->contains(Fl::belowmouse())) {
  180. return send(o,FL_MOVE);
  181. } else {
  182. Fl::belowmouse(o);
  183. if (send(o,FL_ENTER)) return 1;
  184. }
  185. }
  186. }
  187. Fl::belowmouse(this);
  188. return 1;
  189. case FL_DND_ENTER:
  190. case FL_DND_DRAG:
  191. for (i = children(); i--;) {
  192. o = a[i];
  193. if (o->takesevents() && Fl::event_inside(o)) {
  194. if (o->contains(Fl::belowmouse())) {
  195. return send(o,FL_DND_DRAG);
  196. } else if (send(o,FL_DND_ENTER)) {
  197. if (!o->contains(Fl::belowmouse())) Fl::belowmouse(o);
  198. return 1;
  199. }
  200. }
  201. }
  202. Fl::belowmouse(this);
  203. return 0;
  204. case FL_PUSH:
  205. for (i = children(); i--;) {
  206. o = a[i];
  207. if (o->takesevents() && Fl::event_inside(o)) {
  208. Fl_Widget_Tracker wp(o);
  209. if (send(o,FL_PUSH)) {
  210. if (Fl::pushed() && wp.exists() && !o->contains(Fl::pushed())) Fl::pushed(o);
  211. return 1;
  212. }
  213. }
  214. }
  215. return 0;
  216. case FL_RELEASE:
  217. case FL_DRAG:
  218. o = Fl::pushed();
  219. if (o == this) return 0;
  220. else if (o) send(o,event);
  221. else {
  222. for (i = children(); i--;) {
  223. o = a[i];
  224. if (o->takesevents() && Fl::event_inside(o)) {
  225. if (send(o,event)) return 1;
  226. }
  227. }
  228. }
  229. return 0;
  230. case FL_MOUSEWHEEL:
  231. for (i = children(); i--;) {
  232. o = a[i];
  233. if (o->takesevents() && Fl::event_inside(o) && send(o,FL_MOUSEWHEEL))
  234. return 1;
  235. }
  236. for (i = children(); i--;) {
  237. o = a[i];
  238. if (o->takesevents() && !Fl::event_inside(o) && send(o,FL_MOUSEWHEEL))
  239. return 1;
  240. }
  241. return 0;
  242. case FL_DEACTIVATE:
  243. case FL_ACTIVATE:
  244. for (i = children(); i--;) {
  245. o = *a++;
  246. if (o->active()) o->handle(event);
  247. }
  248. return 1;
  249. case FL_SHOW:
  250. case FL_HIDE:
  251. for (i = children(); i--;) {
  252. o = *a++;
  253. if (event == FL_HIDE && o == Fl::focus()) {
  254. // Give up input focus...
  255. int old_event = Fl::e_number;
  256. o->handle(Fl::e_number = FL_UNFOCUS);
  257. Fl::e_number = old_event;
  258. Fl::focus(0);
  259. }
  260. if (o->visible()) o->handle(event);
  261. }
  262. return 1;
  263. default:
  264. // For all other events, try to give to each child, starting at focus:
  265. for (i = 0; i < children(); i ++)
  266. if (Fl::focus_ == a[i]) break;
  267. if (i >= children()) i = 0;
  268. if (children()) {
  269. for (int j = i;;) {
  270. if (a[j]->takesevents()) if (send(a[j], event)) return 1;
  271. j++;
  272. if (j >= children()) j = 0;
  273. if (j == i) break;
  274. }
  275. }
  276. return 0;
  277. }
  278. }
  279. //void Fl_Group::focus(Fl_Widget *o) {Fl::focus(o); o->handle(FL_FOCUS);}
  280. #if 0
  281. const char *nameof(Fl_Widget *o) {
  282. if (!o) return "NULL";
  283. if (!o->label()) return "<no label>";
  284. return o->label();
  285. }
  286. #endif
  287. // try to move the focus in response to a keystroke:
  288. int Fl_Group::navigation(int key) {
  289. if (children() <= 1) return 0;
  290. int i;
  291. for (i = 0; ; i++) {
  292. if (i >= children_) return 0;
  293. if (array_[i]->contains(Fl::focus())) break;
  294. }
  295. Fl_Widget *previous = array_[i];
  296. for (;;) {
  297. switch (key) {
  298. case FL_Right:
  299. case FL_Down:
  300. i++;
  301. if (i >= children_) {
  302. if (parent()) return 0;
  303. i = 0;
  304. }
  305. break;
  306. case FL_Left:
  307. case FL_Up:
  308. if (i) i--;
  309. else {
  310. if (parent()) return 0;
  311. i = children_-1;
  312. }
  313. break;
  314. default:
  315. return 0;
  316. }
  317. Fl_Widget* o = array_[i];
  318. if (o == previous) return 0;
  319. switch (key) {
  320. case FL_Down:
  321. case FL_Up:
  322. // for up/down, the widgets have to overlap horizontally:
  323. if (o->x() >= previous->x()+previous->w() ||
  324. o->x()+o->w() <= previous->x()) continue;
  325. }
  326. if (o->take_focus()) return 1;
  327. }
  328. }
  329. ////////////////////////////////////////////////////////////////
  330. Fl_Group::Fl_Group(int X,int Y,int W,int H,const char *l)
  331. : Fl_Widget(X,Y,W,H,l) {
  332. align(FL_ALIGN_TOP);
  333. set_flag(GROUP_TYPE);
  334. children_ = 0;
  335. array_ = 0;
  336. savedfocus_ = 0;
  337. resizable_ = this;
  338. sizes_ = 0; // this is allocated when first resize() is done
  339. // Subclasses may want to construct child objects as part of their
  340. // constructor, so make sure they are add()'d to this object.
  341. // But you must end() the object!
  342. user_handler_ = NULL;
  343. begin();
  344. }
  345. /**
  346. Deletes all child widgets from memory recursively.
  347. This method differs from the remove() method in that it
  348. affects all child widgets and deletes them from memory.
  349. */
  350. void Fl_Group::clear() {
  351. savedfocus_ = 0;
  352. resizable_ = this;
  353. init_sizes();
  354. // okay, now it is safe to destroy the children:
  355. while (children_) {
  356. Fl_Widget* o = child(0); // *first* child widget
  357. if (o->parent() == this) { // should always be true
  358. remove(o); // remove child widget first
  359. delete o; // then delete it
  360. } else { // this should never happen !
  361. #ifdef DEBUG_CLEAR
  362. printf ("Fl_Group::clear() widget:%p, parent: %p != this (%p)\n",
  363. o, o->parent(), this); fflush(stdout);
  364. #endif // DEBUG_CLEAR
  365. remove(o); // remove it
  366. }
  367. }
  368. }
  369. /**
  370. The destructor <I>also deletes all the children</I>. This allows a
  371. whole tree to be deleted at once, without having to keep a pointer to
  372. all the children in the user code.
  373. It is allowed that the Fl_Group and all of its children are automatic
  374. (local) variables, but you must declare the Fl_Group \e first, so that
  375. it is destroyed last.
  376. If you add static or automatic (local) variables to an Fl_Group, then it
  377. is your responsibility to remove (or delete) all such static or automatic
  378. child widgets \e \b before destroying the group - otherwise the child
  379. widgets' destructors would be called twice!
  380. */
  381. Fl_Group::~Fl_Group() {
  382. clear();
  383. }
  384. /**
  385. The widget is removed from its current group (if any) and then
  386. inserted into this group. It is put at index n - or at the end,
  387. if n >= children(). This can also be used to rearrange
  388. the widgets inside a group.
  389. */
  390. void Fl_Group::insert(Fl_Widget &o, int index) {
  391. if (o.parent()) {
  392. Fl_Group* g = o.parent();
  393. int n = g->find(o);
  394. if (g == this) {
  395. if (index > n) index--;
  396. if (index == n) return;
  397. }
  398. g->remove(o);
  399. }
  400. o.parent_ = this;
  401. if (children_ == 0) { // use array pointer to point at single child
  402. array_ = (Fl_Widget**)&o;
  403. } else if (children_ == 1) { // go from 1 to 2 children
  404. Fl_Widget* t = (Fl_Widget*)array_;
  405. array_ = (Fl_Widget**)malloc(2*sizeof(Fl_Widget*));
  406. if (index) {array_[0] = t; array_[1] = &o;}
  407. else {array_[0] = &o; array_[1] = t;}
  408. } else {
  409. if (!(children_ & (children_-1))) // double number of children
  410. array_ = (Fl_Widget**)realloc((void*)array_,
  411. 2*children_*sizeof(Fl_Widget*));
  412. int j; for (j = children_; j > index; j--) array_[j] = array_[j-1];
  413. array_[j] = &o;
  414. }
  415. children_++;
  416. init_sizes();
  417. }
  418. /**
  419. The widget is removed from its current group (if any) and then added
  420. to the end of this group.
  421. */
  422. void Fl_Group::add(Fl_Widget &o) {insert(o, children_);}
  423. /**
  424. Removes a widget from the group but does not delete it.
  425. This method does nothing if the widget is not a child of the group.
  426. This method differs from the clear() method in that it only affects
  427. a single widget and does not delete it from memory.
  428. */
  429. void Fl_Group::remove(Fl_Widget &o) {
  430. if (!children_) return;
  431. int i = find(o);
  432. if (i >= children_) return;
  433. if (&o == savedfocus_) savedfocus_ = 0;
  434. if (o.parent_ == this) { // this should always be true
  435. o.parent_ = 0;
  436. }
  437. #ifdef DEBUG_REMOVE
  438. else { // this should never happen !
  439. printf ("Fl_Group::remove(): widget %p, parent_ (%p) != this (%p)\n",
  440. &o, o.parent_, this);
  441. }
  442. #endif // DEBUG_REMOVE
  443. // remove the widget from the group
  444. children_--;
  445. if (children_ == 1) { // go from 2 to 1 child
  446. Fl_Widget *t = array_[!i];
  447. free((void*)array_);
  448. array_ = (Fl_Widget**)t;
  449. } else if (children_ > 1) { // delete from array
  450. for (; i < children_; i++) array_[i] = array_[i+1];
  451. }
  452. init_sizes();
  453. }
  454. ////////////////////////////////////////////////////////////////
  455. // Rather lame kludge here, I need to detect windows and ignore the
  456. // changes to X,Y, since all children are relative to X,Y. That
  457. // is why I check type():
  458. // sizes array stores the initial positions of widgets as
  459. // left,right,top,bottom quads. The first quad is the group, the
  460. // second is the resizable (clipped to the group), and the
  461. // rest are the children. This is a convenient order for the
  462. // algorithm. If you change this be sure to fix Fl_Tile which
  463. // also uses this array!
  464. /**
  465. Resets the internal array of widget sizes and positions.
  466. The Fl_Group widget keeps track of the original widget sizes and
  467. positions when resizing occurs so that if you resize a window back to its
  468. original size the widgets will be in the correct places. If you rearrange
  469. the widgets in your group, call this method to register the new arrangement
  470. with the Fl_Group that contains them.
  471. If you add or remove widgets, this will be done automatically.
  472. \note The internal array of widget sizes and positions will be allocated and
  473. filled when the next resize() occurs.
  474. \sa sizes()
  475. */
  476. void Fl_Group::init_sizes() {
  477. delete[] sizes_; sizes_ = 0;
  478. }
  479. /**
  480. Returns the internal array of widget sizes and positions.
  481. If the sizes() array does not exist, it will be allocated and filled
  482. with the current widget sizes and positions.
  483. \note You should never need to use this method directly, unless you have
  484. special needs to rearrange the children of a Fl_Group. Fl_Tile uses
  485. this to rearrange its widget positions.
  486. \sa init_sizes()
  487. \todo Should the internal representation of the sizes() array be documented?
  488. */
  489. st_widget_sizes* Fl_Group::sizes() {
  490. if (!sizes_) {
  491. //int* p = sizes_ = new int[4*(children_+2)];
  492. st_widget_sizes* p = sizes_ = new st_widget_sizes[(children_+2)];
  493. // first thing in sizes array is the group's size:
  494. if (type() < FL_WINDOW) {p->x = x(); p->y = y();} else {p->x = p->y = 0;}
  495. p->w = p->x+w(); p->h = p->y+h();
  496. p->labelsize = labelsize();
  497. p->textsize = textsize();
  498. // next is the resizable's size:
  499. ++p;
  500. p->x = sizes_->x; // init to the group's size
  501. p->w = sizes_->w;
  502. p->y = sizes_->y;
  503. p->h = sizes_->h;
  504. p->labelsize = sizes_->labelsize;
  505. p->textsize = sizes_->textsize;
  506. Fl_Widget* r = resizable();
  507. if (r && r != this) { // then clip the resizable to it
  508. int t;
  509. t = r->x(); if (t > sizes_->x) p->x = t;
  510. t +=r->w(); if (t < sizes_->w) p->w = t;
  511. t = r->y(); if (t > sizes_->y) p->y = t;
  512. t +=r->h(); if (t < sizes_->h) p->h = t;
  513. p->labelsize = r->labelsize();
  514. p->textsize = r->textsize();
  515. }
  516. // next is all the children's sizes:
  517. ++p;
  518. Fl_Widget*const* a = array();
  519. for (int i=children_; i--;) {
  520. Fl_Widget* o = *a++;
  521. p->x = o->x();
  522. p->w = o->x()+o->w();
  523. p->y = o->y();
  524. p->h = o->y()+o->h();
  525. p->labelsize = o->labelsize();
  526. p->textsize = o->textsize();
  527. ++p;
  528. }
  529. }
  530. return sizes_;
  531. }
  532. /**
  533. Resizes the Fl_Group widget and all of its children.
  534. The Fl_Group widget first resizes itself, and then it moves and resizes
  535. all its children according to the rules documented for
  536. Fl_Group::resizable(Fl_Widget*)
  537. \sa Fl_Group::resizable(Fl_Widget*)
  538. \sa Fl_Group::resizable()
  539. \sa Fl_Widget::resize(int,int,int,int)
  540. */
  541. void Fl_Group::resize(int X, int Y, int W, int H) {
  542. int dx = X-x();
  543. int dy = Y-y();
  544. int dw = W-w();
  545. int dh = H-h();
  546. st_widget_sizes *p = sizes(); // save initial sizes and positions
  547. Fl_Widget::resize(X,Y,W,H); // make new xywh values visible for children
  548. if (!resizable() || dw==0 && dh==0 ) {
  549. if (type() < FL_WINDOW) {
  550. Fl_Widget*const* a = array();
  551. for (int i=children_; i--;) {
  552. Fl_Widget* o = *a++;
  553. o->resize(o->x()+dx, o->y()+dy, o->w(), o->h());
  554. }
  555. }
  556. } else if (children_) {
  557. // get changes in size/position from the initial size:
  558. dx = X - p->x;
  559. dw = W - (p->w - p->x);
  560. dy = Y - p->y;
  561. dh = H - (p->h - p->y);
  562. if (type() >= FL_WINDOW) dx = dy = 0;
  563. ++p;
  564. // get initial size of resizable():
  565. int IX = p->x;
  566. int IR = p->w;
  567. int IY = p->y;
  568. int IB = p->h;
  569. ++p;
  570. Fl_Widget*const* a = array();
  571. for (int i=children_; i--;) {
  572. Fl_Widget* o = *a++;
  573. #if 1
  574. int XX = p->x;
  575. if (XX >= IR) XX += dw;
  576. else if (XX > IX) XX = IX+((XX-IX)*(IR+dw-IX)+(IR-IX)/2)/(IR-IX);
  577. int R = p->w;
  578. if (R >= IR) R += dw;
  579. else if (R > IX) R = IX+((R-IX)*(IR+dw-IX)+(IR-IX)/2)/(IR-IX);
  580. int YY = p->y;
  581. if (YY >= IB) YY += dh;
  582. else if (YY > IY) YY = IY+((YY-IY)*(IB+dh-IY)+(IB-IY)/2)/(IB-IY);
  583. int B = p->h;
  584. if (B >= IB) B += dh;
  585. else if (B > IY) B = IY+((B-IY)*(IB+dh-IY)+(IB-IY)/2)/(IB-IY);
  586. #else // much simpler code from Francois Ostiguy:
  587. int XX = p->x;
  588. if (XX >= IR) XX += dw;
  589. else if (XX > IX) XX += dw * (XX-IX)/(IR-IX);
  590. int R = p->w;
  591. if (R >= IR) R += dw;
  592. else if (R > IX) R = R + dw * (R-IX)/(IR-IX);
  593. int YY = p->y;
  594. if (YY >= IB) YY += dh;
  595. else if (YY > IY) YY = YY + dh*(YY-IY)/(IB-IY);
  596. int B = p->h;
  597. if (B >= IB) B += dh;
  598. else if (B > IY) B = B + dh*(B-IY)/(IB-IY);
  599. #endif
  600. o->resize(XX+dx, YY+dy, R-XX, B-YY);
  601. ++p;
  602. }
  603. }
  604. }
  605. /**
  606. Draws all children of the group.
  607. This is useful, if you derived a widget from Fl_Group and want to draw a special
  608. border or background. You can call draw_children() from the derived draw() method
  609. after drawing the box, border, or background.
  610. */
  611. void Fl_Group::draw_children() {
  612. Fl_Widget*const* a = array();
  613. if (clip_children()) {
  614. fl_push_clip(x() + Fl::box_dx(box()),
  615. y() + Fl::box_dy(box()),
  616. w() - Fl::box_dw(box()),
  617. h() - Fl::box_dh(box()));
  618. }
  619. if (damage() & ~FL_DAMAGE_CHILD) { // redraw the entire thing:
  620. for (int i=children_; i--;) {
  621. Fl_Widget& o = **a++;
  622. draw_child(o);
  623. draw_outside_label(o);
  624. }
  625. } else { // only redraw the children that need it:
  626. for (int i=children_; i--;) update_child(**a++);
  627. }
  628. if (clip_children()) fl_pop_clip();
  629. }
  630. void Fl_Group::draw() {
  631. if (damage() & ~FL_DAMAGE_CHILD) { // redraw the entire thing:
  632. draw_box();
  633. draw_label();
  634. }
  635. draw_children();
  636. }
  637. /**
  638. Draws a child only if it needs it.
  639. This draws a child widget, if it is not clipped \em and if any damage() bits
  640. are set. The damage bits are cleared after drawing.
  641. \sa Fl_Group::draw_child(Fl_Widget& widget) const
  642. */
  643. void Fl_Group::update_child(Fl_Widget& widget) const {
  644. if (widget.damage() && widget.visible() && widget.type() < FL_WINDOW &&
  645. fl_not_clipped(widget.x(), widget.y(), widget.w(), widget.h())) {
  646. widget.draw();
  647. widget.clear_damage();
  648. }
  649. }
  650. /**
  651. Forces a child to redraw.
  652. This draws a child widget, if it is not clipped.
  653. The damage bits are cleared after drawing.
  654. */
  655. void Fl_Group::draw_child(Fl_Widget& widget) const {
  656. if (widget.visible() && widget.type() < FL_WINDOW &&
  657. fl_not_clipped(widget.x(), widget.y(), widget.w(), widget.h())) {
  658. widget.clear_damage(FL_DAMAGE_ALL);
  659. widget.draw();
  660. widget.clear_damage();
  661. }
  662. }
  663. extern char fl_draw_shortcut;
  664. /** Parents normally call this to draw outside labels of child widgets. */
  665. void Fl_Group::draw_outside_label(const Fl_Widget& widget) const {
  666. if (!widget.visible()) return;
  667. // skip any labels that are inside the widget:
  668. if (!(widget.align()&15) || (widget.align() & FL_ALIGN_INSIDE)) return;
  669. // invent a box that is outside the widget:
  670. int a = widget.align();
  671. int X = widget.x();
  672. int Y = widget.y();
  673. int W = widget.w();
  674. int H = widget.h();
  675. if ( (a & 0x0f) == FL_ALIGN_LEFT_TOP ) {
  676. a = (a &~0x0f ) | FL_ALIGN_TOP_RIGHT;
  677. X = x();
  678. W = widget.x()-X-3;
  679. } else if ( (a & 0x0f) == FL_ALIGN_LEFT_BOTTOM ) {
  680. a = (a &~0x0f ) | FL_ALIGN_BOTTOM_RIGHT;
  681. X = x();
  682. W = widget.x()-X-3;
  683. } else if ( (a & 0x0f) == FL_ALIGN_RIGHT_TOP ) {
  684. a = (a &~0x0f ) | FL_ALIGN_TOP_LEFT;
  685. X = X+W+3;
  686. W = x()+this->w()-X;
  687. } else if ( (a & 0x0f) == FL_ALIGN_RIGHT_BOTTOM ) {
  688. a = (a &~0x0f ) | FL_ALIGN_BOTTOM_LEFT;
  689. X = X+W+3;
  690. W = x()+this->w()-X;
  691. } else if (a & FL_ALIGN_TOP) {
  692. a ^= (FL_ALIGN_BOTTOM|FL_ALIGN_TOP);
  693. Y = y();
  694. H = widget.y()-Y;
  695. } else if (a & FL_ALIGN_BOTTOM) {
  696. a ^= (FL_ALIGN_BOTTOM|FL_ALIGN_TOP);
  697. Y = Y+H;
  698. H = y()+h()-Y;
  699. } else if (a & FL_ALIGN_LEFT) {
  700. a ^= (FL_ALIGN_LEFT|FL_ALIGN_RIGHT);
  701. X = x();
  702. W = widget.x()-X-3;
  703. } else if (a & FL_ALIGN_RIGHT) {
  704. a ^= (FL_ALIGN_LEFT|FL_ALIGN_RIGHT);
  705. X = X+W+3;
  706. W = x()+this->w()-X;
  707. }
  708. widget.draw_label(X,Y,W,H,(Fl_Align)a);
  709. }
  710. void Fl_Group::resizefont(float font_scale){
  711. st_widget_sizes *p = sizes();
  712. //element at 0 is self = this window
  713. for(int i=1, iMax=children(); i < iMax; i++)
  714. {
  715. Fl_Widget *wdg = child(i);
  716. if(wdg->inherits_from("Fl_Group"))
  717. ((Fl_Group*)wdg)->resizefont(font_scale);
  718. else
  719. {
  720. st_widget_sizes *tmp = p+i;
  721. wdg->labelsize( tmp->labelsize * font_scale);
  722. if (tmp->textsize)
  723. wdg->textsize( tmp->textsize * font_scale);
  724. }
  725. }
  726. }
  727. //
  728. // End of "$Id: Fl_Group.cxx 7469 2010-04-07 23:17:33Z matt $".
  729. //