Fl_Layouter.H 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* BurrTools
  2. *
  3. * BurrTools is the legal property of its developers, whose
  4. * names are listed in the COPYRIGHT file, which is included
  5. * within the source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #ifndef __FL_LAYOUTER_H__
  20. #define __FL_LAYOUTER_H__
  21. #include <FL/Fl_Double_Window.H>
  22. #include <FL/Fl_Group.H>
  23. #include <FL/Fl_Tabs.H>
  24. class Fl_Box;
  25. class Fl_Button;
  26. class Fl_Check_Button;
  27. class Fl_Round_Button;
  28. class Fl_Radio_Button;
  29. class Fl_Slider;
  30. class Fl_Choice;
  31. class Fl_Input;
  32. class Fl_Output;
  33. class Fl_Int_Input;
  34. class Fl_Float_Input;
  35. class Fl_Value_Output;
  36. class Fl_Value_Input;
  37. class Fl_Value_Slider;
  38. class Fl_Roller;
  39. class Fl_Menu_Bar;
  40. class Fl_Progress;
  41. class Fl_Scroll;
  42. class Fl_Tabs;
  43. class Fl_Widget;
  44. class Fl_Int_Vector;
  45. // this class is used to calculate the minimal size of a widget
  46. class layoutMeasurer_c
  47. {
  48. public:
  49. /* the following function is for the layouter to find out how much space
  50. * the widget requires at least
  51. */
  52. virtual void getMinSize(const Fl_Widget * w, int *width, int *height) const = 0;
  53. };
  54. // a layout measurer class, where minsize returns a constant value
  55. class layoutMeasurerConst_c : public layoutMeasurer_c
  56. {
  57. private:
  58. int minw, minh;
  59. public:
  60. layoutMeasurerConst_c(int mw, int mh) : minw(mw), minh(mh) {}
  61. virtual void getMinSize(const Fl_Widget * /*w*/, int *width, int *height) const
  62. {
  63. *width = minw;
  64. *height = minh;
  65. }
  66. };
  67. // a layout measurer class, that measures the size of the label and adds an constant offset
  68. class layoutMeasurerPad_c : public layoutMeasurer_c
  69. {
  70. private:
  71. int padw, padh;
  72. public:
  73. layoutMeasurerPad_c(int pw, int ph) : padw(pw), padh(ph) {}
  74. virtual void getMinSize(const Fl_Widget * w, int *width, int *height) const;
  75. };
  76. // a layout measurer class, where minsize returns a value proportional to font size
  77. class layoutMeasurerFont_c : public layoutMeasurer_c
  78. {
  79. private:
  80. int minw, minh;
  81. public:
  82. layoutMeasurerFont_c(int mw, int mh) : minw(mw), minh(mh) {}
  83. virtual void getMinSize(const Fl_Widget * w, int *width, int *height) const
  84. {
  85. *width = minw;
  86. if(w->textsize() < minh) {
  87. *height = w->textsize() + 6;
  88. ((Fl_Widget *) w)->size(w->w(), minh);
  89. }
  90. else *height = minh;
  91. }
  92. };
  93. // this class is the group that actually layouts its subwidgets according to the layouting information
  94. // provided with each widget
  95. class layouter_c : public Fl_Group {
  96. private:
  97. bool minsizeValid;
  98. int mw, mh;
  99. public:
  100. void calcLayout(int task, Fl_Int_Vector *widths, Fl_Int_Vector *heights,
  101. Fl_Int_Vector *widgetsW, Fl_Int_Vector *widgetsH,
  102. int targetW = 0, int tagetH = 0) const;
  103. virtual void resize(int x, int y, int w, int h);
  104. void getMinSize(int *width, int *height) const;
  105. layouter_c(int x, int y, int w, int h) : Fl_Group(x, y, w, h), minsizeValid(false) {}
  106. void remove(Fl_Widget &w);
  107. void remove(Fl_Widget *w);
  108. void add(Fl_Widget &w);
  109. void add(Fl_Widget *w);
  110. };
  111. // the measurer class that should be attached to the layouter
  112. class layoutMeasurerLayouter_c : public layoutMeasurer_c
  113. {
  114. public:
  115. layoutMeasurerLayouter_c(void) {}
  116. virtual void getMinSize(const Fl_Widget * w, int *width, int *height) const
  117. {
  118. ((layouter_c*)w)->getMinSize(width, height);
  119. }
  120. };
  121. // a tabs class, that uses layoutable subwidgets
  122. class LFl_Tabs : public Fl_Tabs {
  123. public:
  124. LFl_Tabs(int x, int y, int w, int h);
  125. void getMinSize(int *width, int *height) const;
  126. void resize(int x, int y, int w, int h);
  127. };
  128. /* window class that alread contains a layouter, and all further added widgets
  129. * will automatically go into this layouter...
  130. */
  131. class LFl_Double_Window : public Fl_Double_Window {
  132. layouter_c * lay;
  133. bool res;
  134. bool placed;
  135. public:
  136. LFl_Double_Window(bool resizab) : Fl_Double_Window(10, 10), res(resizab), placed(false) {
  137. lay = new layouter_c(0, 0, 10, 10);
  138. resizable(lay);
  139. }
  140. void show(void);
  141. void resize(int x, int y, int w, int h) {
  142. placed = true;
  143. Fl_Double_Window::resize(x, y, w, h);
  144. }
  145. void begin() {
  146. lay->begin();
  147. }
  148. };
  149. // functions to access and change layouting information for a layoutable
  150. // widget. If the widget is not suitable for layouting these function do nothing
  151. // how is a widget supposed to behave, when the available space
  152. // is bigger than required
  153. typedef enum {
  154. STRETCH_MINUS, STRETCH_PLUS, STRETCH_MIDDLE, STRETCH, STRETCH_SQUARE
  155. } stretch_t;
  156. class Fl_Layout_Utils {
  157. public:
  158. /* Some functions that return a widget, that are suitable for layouting.
  159. * Those are mainly tool functions to make it easier to construct the widgets
  160. * the gevne values for x, y, w, h are no longer pixel positions but grid positions
  161. */
  162. Fl_Box * getNewBox(const char *txt, int x = 0, int y = 0, int w = 1, int h = 1);
  163. Fl_Box * getNewBox(int x = 0, int y = 0, int w = 1, int h = 1);
  164. layouter_c * getNewFrame(int x = 0, int y = 0, int w = 1, int h = 1);
  165. layouter_c * getNewLayouter(int x = 0, int y = 0, int w = 1, int h = 1);
  166. Fl_Button * getNewButton(const char * text, int x = 0, int y = 0, int w = 1, int h = 1, int padX=4, int padY=4);
  167. Fl_Check_Button * getNewCheckButton(const char * text, int x = 0, int y = 0, int w = 1, int h = 1);
  168. Fl_Round_Button * getNewRoundButton(const char *text, int x = 0, int y = 0, int w = 1, int h = 1);
  169. Fl_Round_Button * getNewRadioButton(const char *text, int x = 0, int y = 0, int w = 1, int h = 1);
  170. Fl_Slider * getNewSlider(int x = 0, int y = 0, int w = 1, int h = 1);
  171. Fl_Choice * getNewChoice(int x = 0, int y = 0, int w = 1, int h = 1);
  172. Fl_Input * getNewInput(int x = 0, int y = 0, int w = 1, int h = 1);
  173. Fl_Output * getNewOutput(int x = 0, int y = 0, int w = 1, int h = 1);
  174. Fl_Int_Input * getNewIntInput(int x = 0, int y = 0, int w = 1, int h = 1);
  175. Fl_Float_Input * getNewFloatInput(int x = 0, int y = 0, int w = 1, int h = 1);
  176. Fl_Value_Output * getNewValueOutput(int x = 0, int y = 0, int w = 1, int h = 1);
  177. Fl_Value_Input * getNewValueInput(int x = 0, int y = 0, int w = 1, int h = 1);
  178. Fl_Value_Slider * getNewValueSlider(int x = 0, int y = 0, int w = 1, int h = 1);
  179. Fl_Roller * getNewRoller(int x = 0, int y = 0, int w = 1, int h = 1);
  180. Fl_Menu_Bar * getNewMenuBar(int x, int y, int w, int h);
  181. Fl_Progress * getNewProgress(int x, int y, int w, int h);
  182. Fl_Scroll * getNewScroll(int x, int y, int w, int h);
  183. // functions to access layouting information attached to a widget
  184. // the get functions, when applied to a widget without layouting information
  185. // will simply return the default value
  186. // when setter functions are called on a widget without layouting information
  187. // nothing is done, this is because the layouting information is right now specialized on
  188. // the widget until Fltk includes a get minimal size functions into the widget class
  189. int getMinWidth(const Fl_Widget * w);
  190. int getMinHeight(const Fl_Widget * w);
  191. unsigned char getPitch(const Fl_Widget * w);
  192. unsigned char getWeightX(const Fl_Widget * w);
  193. unsigned char getWeightY(const Fl_Widget * w);
  194. stretch_t getStretchX(const Fl_Widget * w);
  195. stretch_t getStretchY(const Fl_Widget * w);
  196. void getGridValues(const Fl_Widget * wi, unsigned int *x, unsigned int *y, unsigned int *w, unsigned int *h);
  197. void setGridValues(const Fl_Widget * wi, unsigned int x, unsigned int y, unsigned int w, unsigned int h);
  198. void setLayoutParameter(const Fl_Widget * w, stretch_t stretchX, stretch_t stretchY, unsigned char pitch, unsigned char weightX, unsigned char weightY);
  199. void pitch(const Fl_Widget * w, unsigned char pitch);
  200. void stretchLeft(const Fl_Widget * w);
  201. void stretchRight(const Fl_Widget * w);
  202. void stretchTop(const Fl_Widget * w);
  203. void stretchBottom(const Fl_Widget * w);
  204. void stretchSquare(const Fl_Widget * w);
  205. void stretchHCenter(const Fl_Widget * w);
  206. void stretchVCenter(const Fl_Widget * w);
  207. void stretchCenter(const Fl_Widget * w);
  208. void stretch(const Fl_Widget * w);
  209. void weight(const Fl_Widget * w, unsigned char x, unsigned char y);
  210. void setMinimumSize(const Fl_Widget * w, unsigned int minWidth, unsigned int minHeight);
  211. void setMeasureClass(const Fl_Widget * w, layoutMeasurer_c * m);
  212. void getMinSize(const Fl_Widget * wi, int * w, int * h);
  213. bool hasLayoutInfo(const Fl_Widget * w);
  214. void removeLayoutInfo(const Fl_Widget * w);
  215. };
  216. #endif