Fl_Tabs.H 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // "$Id: Fl_Tabs.H 12270 2017-06-21 20:00:28Z AlbrechtS $"
  3. //
  4. // Tab header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2017 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. /* \file
  19. Fl_Tabs widget . */
  20. #ifndef Fl_Tabs_H
  21. #define Fl_Tabs_H
  22. #include "Fl_Group.H"
  23. /**
  24. The Fl_Tabs widget is the "file card tabs"
  25. interface that allows you to put lots and lots of buttons and
  26. switches in a panel, as popularized by many toolkits.
  27. \image html tabs.png
  28. \image latex tabs.png "Fl_Tabs" width=8cm
  29. Clicking the tab makes a child visible() by calling
  30. show() on it, and all other children are made invisible
  31. by calling hide() on them. Usually the children are Fl_Group widgets
  32. containing several widgets themselves.
  33. Each child makes a card, and its label() is printed
  34. on the card tab, including the label font and style. The
  35. selection color of that child is used to color the tab, while
  36. the color of the child determines the background color of the pane.
  37. The size of the tabs is controlled by the bounding box of the
  38. children (there should be some space between the children and
  39. the edge of the Fl_Tabs), and the tabs may be placed
  40. "inverted" on the bottom - this is determined by which
  41. gap is larger. It is easiest to lay this out in fluid, using the
  42. fluid browser to select each child group and resize them until
  43. the tabs look the way you want them to.
  44. The background area behind and to the right of the tabs is
  45. "transparent", exposing the background detail of the parent. The
  46. value of Fl_Tabs::box() does not affect this area. So if Fl_Tabs is
  47. resized by itself without the parent, force the appropriate parent
  48. (visible behind the tabs) to redraw() to prevent artifacts.
  49. See "Resizing Caveats" below on how to keep tab heights constant.
  50. See "Callback's Use Of when()" on how to control the details
  51. of how clicks invoke the callback().
  52. A typical use of the Fl_Tabs widget:
  53. \par
  54. \code
  55. // Typical use of Fl_Tabs
  56. Fl_Tabs *tabs = new Fl_Tabs(10,10,300,200);
  57. {
  58. Fl_Group *grp1 = new Fl_Group(20,30,280,170,"Tab1");
  59. {
  60. ..widgets that go in tab#1..
  61. }
  62. grp1->end();
  63. Fl_Group *grp2 = new Fl_Group(20,30,280,170,"Tab2");
  64. {
  65. ..widgets that go in tab#2..
  66. }
  67. grp2->end();
  68. }
  69. tabs->end();
  70. \endcode
  71. \b Default \b Appearance
  72. The appearance of each "tab" is taken from the label() and color() of the
  73. child group corresponding to that "tab" and panel. Where the "tabs" appear
  74. depends on the position and size of the child groups that make up the
  75. panels within the Fl_Tab, i.e. whether there is more space above or
  76. below them. The height of the "tabs" depends on how much free space
  77. is available.
  78. \image html tabs_default.png "Fl_Tabs Default Appearance"
  79. \image latex tabs_default.png "Fl_Tabs Default Appearance" width=8cm
  80. \b Highlighting \b The \b Selected \b Tab
  81. The selected "tab" can be highlighted further by setting the
  82. selection_color() of the Fl_Tab itself, e.g.
  83. \par
  84. \code
  85. ..
  86. tabs = new Fl_Tabs(..);
  87. tabs->selection_color(FL_DARK3);
  88. ..
  89. \endcode
  90. The result of the above looks like:
  91. \image html tabs_selection.png "Highlighting the selected tab"
  92. \image latex tabs_selection.png "Highlighting the selected tab" width=8cm
  93. \b Uniform \b Tab \b and \b Panel \b Appearance
  94. In order to have uniform tab and panel appearance, not only must the color()
  95. and selection_color() for each child group be set, but also the
  96. selection_color() of the Fl_Tab itself any time a new "tab" is selected.
  97. This can be achieved within the Fl_Tab callback, e.g.
  98. \par
  99. \code
  100. void MyTabCallback(Fl_Widget *w, void*) {
  101. Fl_Tabs *tabs = (Fl_Tabs*)w;
  102. // When tab changed, make sure it has same color as its group
  103. tabs->selection_color( (tab->value())->color() );
  104. }
  105. ..
  106. int main(..) {
  107. // Define tabs widget
  108. tabs = new Fl_Tabs(..);
  109. tabs->callback(MyTabCallback);
  110. // Create three tabs each colored differently
  111. grp1 = new Fl_Group(.. "One");
  112. grp1->color(9);
  113. grp1->selection_color(9);
  114. grp1->end();
  115. grp2 = new Fl_Group(.. "Two");
  116. grp2->color(10);
  117. grp2->selection_color(10);
  118. grp2->end();
  119. grp3 = new Fl_Group(.. "Three");
  120. grp3->color(14);
  121. grp3->selection_color(14);
  122. grp3->end();
  123. ..
  124. // Make sure default tab has same color as its group
  125. tabs->selection_color( (tab->value())->color() );
  126. ..
  127. return Fl::run();
  128. }
  129. \endcode
  130. The result of the above looks like:
  131. \image html tabs_uniform.png "Fl_Tabs with uniform colors"
  132. \image latex tabs_uniform.png "Fl_Tabs with uniform colors" width=8cm
  133. \b Resizing \b Caveats
  134. When Fl_Tabs is resized vertically, the default behavior scales the
  135. tab's height as well as its children. To keep the tab height constant
  136. during resizing, set the tab widget's resizable() to one of the tab's
  137. child groups, i.e.
  138. \par
  139. \code
  140. tabs = new Fl_Tabs(..);
  141. grp1 = new Fl_Group(..);
  142. ..
  143. grp2 = new Fl_Group(..);
  144. ..
  145. tabs->end();
  146. tabs->resizable(grp1); // keeps tab height constant
  147. \endcode
  148. \par Callback's Use Of when()
  149. As of FLTK 1.3.3, Fl_Tabs() supports the following flags for when():
  150. - \ref FL_WHEN_NEVER -- callback never invoked (all flags off)
  151. - \ref FL_WHEN_CHANGED -- if flag set, invokes callback when a tab has been changed (on click or keyboard navigation)
  152. - \ref FL_WHEN_NOT_CHANGED -- if flag set, invokes callback when the tabs remain unchanged (on click or keyboard navigation)
  153. - \ref FL_WHEN_RELEASE -- if flag set, invokes callback on RELEASE of mouse button or keyboard navigation
  154. Notes:
  155. -# The above flags can be logically OR-ed (|) or added (+) to combine behaviors.
  156. -# The default value for when() is \ref FL_WHEN_RELEASE (inherited from Fl_Widget).
  157. -# If \ref FL_WHEN_RELEASE is the \em only flag specified,
  158. the behavior will be as if (\ref FL_WHEN_RELEASE|\ref FL_WHEN_CHANGED) was specified.
  159. -# The value of changed() will be valid during the callback.
  160. -# If both \ref FL_WHEN_CHANGED and \ref FL_WHEN_NOT_CHANGED are specified,
  161. the callback is invoked whether the tab has been changed or not.
  162. The changed() method can be used to determine the cause.
  163. -# \ref FL_WHEN_NOT_CHANGED can happen if someone clicks on an already selected tab,
  164. or if a keyboard navigation attempt results in no change to the tabs,
  165. such as using the arrow keys while at the left or right end of the tabs.
  166. */
  167. class FL_EXPORT Fl_Tabs : public Fl_Group {
  168. Fl_Widget *push_;
  169. protected:
  170. int *tab_pos; // array of x-offsets of tabs per child + 1
  171. int *tab_width; // array of widths of tabs per child + 1
  172. int tab_count; // array size
  173. Fl_Align tab_align_; // tab label alignment
  174. virtual void redraw_tabs();
  175. virtual int tab_positions(); // allocate and calculate tab positions
  176. virtual void clear_tab_positions();
  177. virtual void draw_tab(int x1, int x2, int W, int H, Fl_Widget* o, int sel=0);
  178. virtual int tab_height();
  179. void draw();
  180. public:
  181. Fl_Tabs(int X, int Y, int W, int H, const char *L = 0);
  182. virtual ~Fl_Tabs();
  183. int handle(int);
  184. Fl_Widget *value();
  185. int value(Fl_Widget *);
  186. /**
  187. Focus the tabs header (group labels).
  188. */
  189. int header_focus();
  190. /**
  191. Returns the tab group for the tab the user has currently down-clicked on
  192. and remains over until FL_RELEASE. Otherwise, returns NULL.
  193. While the user is down-clicked on a tab, the return value is the tab group
  194. for that tab. But as soon as the user releases, or drags off the tab with
  195. the button still down, the return value will be NULL.
  196. \see push(Fl_Widget*).
  197. */
  198. Fl_Widget *push() const {return push_;}
  199. int push(Fl_Widget *);
  200. // Returns the widget of the tab the user clicked on at event_x/event_y.
  201. virtual Fl_Widget *which(int event_x, int event_y);
  202. // Returns the position and size available to be used by its children.
  203. void client_area(int &rx, int &ry, int &rw, int &rh, int tabh=0);
  204. /**
  205. Sets the tab label alignment.
  206. The default is FL_ALIGN_CENTER so tab labels are centered, but since
  207. the label space is measured (per label) to fit the labels, there
  208. wouldn't be any difference if labels were aligned left or right.
  209. If you want to show an image (icon) next to the group's label you can
  210. set a different label alignment. FL_ALIGN_IMAGE_NEXT_TO_TEXT is the
  211. recommended alignment to show the icon left of the text.
  212. */
  213. void tab_align(Fl_Align a) {tab_align_ = a;}
  214. /**
  215. Gets the tab label alignment.
  216. \see tab_align(Fl_Align)
  217. */
  218. Fl_Align tab_align() const {return tab_align_;}
  219. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Tabs, Fl_Group)
  220. };
  221. #endif
  222. //
  223. // End of "$Id: Fl_Tabs.H 12270 2017-06-21 20:00:28Z AlbrechtS $".
  224. //