You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fltk_layout.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifndef __FLTK_LAYOUT_H__
  19. #define __FLTK_LAYOUT_H__
  20. #include <FL/fl_draw.H>
  21. #include <FL/Fl_Menu_.H>
  22. /* Calculates the width of a string as printed by FLTK (pixels) */
  23. static inline int gui_str_len(const char *str)
  24. {
  25. float len;
  26. fl_font(FL_HELVETICA, FL_NORMAL_SIZE);
  27. len = fl_width(str);
  28. return (int)(len + 0.5f);
  29. }
  30. /* Escapes all @ in text as those have special meaning in labels */
  31. static inline size_t fltk_escape(const char *in, char *out, size_t maxlen)
  32. {
  33. size_t len;
  34. len = 0;
  35. while (*in != '\0') {
  36. if (*in == '@') {
  37. if (maxlen >= 3) {
  38. *out++ = '@';
  39. *out++ = '@';
  40. maxlen -= 2;
  41. }
  42. len += 2;
  43. } else {
  44. if (maxlen >= 2) {
  45. *out++ = *in;
  46. maxlen--;
  47. }
  48. len += 1;
  49. }
  50. in++;
  51. }
  52. if (maxlen)
  53. *out = '\0';
  54. return len;
  55. }
  56. /* Filter out unsafe characters for menu entries */
  57. static inline size_t fltk_menu_escape(const char *in, char *out, size_t maxlen)
  58. {
  59. size_t len;
  60. len = 0;
  61. while (*in != '\0') {
  62. if (*in == '/') {
  63. if (maxlen >= 3) {
  64. *out++ = '\\';
  65. *out++ = '/';
  66. maxlen -= 2;
  67. }
  68. len += 2;
  69. } else {
  70. if (maxlen >= 2) {
  71. *out++ = *in;
  72. maxlen--;
  73. }
  74. len += 1;
  75. }
  76. in++;
  77. }
  78. if (maxlen)
  79. *out = '\0';
  80. return len;
  81. }
  82. /* Helper to add menu entries safely */
  83. static inline void fltk_menu_add(Fl_Menu_ *menu, const char *text,
  84. int shortcut, Fl_Callback *cb,
  85. void *data = 0, int flags = 0)
  86. {
  87. char buffer[1024];
  88. if (fltk_menu_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
  89. return;
  90. menu->add(buffer, shortcut, cb, data, flags);
  91. }
  92. /**** MARGINS ****/
  93. #define OUTER_MARGIN 10
  94. #define INNER_MARGIN 10
  95. /* Tighter grouping of related fields */
  96. #define TIGHT_MARGIN 5
  97. /**** ADJUSTMENTS ****/
  98. #define INDENT 20
  99. /**** FLTK WIDGETS ****/
  100. /* Fl_Tabs */
  101. #define TABS_HEIGHT 30
  102. /* Fl_Input */
  103. #define INPUT_LABEL_OFFSET FL_NORMAL_SIZE
  104. #define INPUT_HEIGHT 25
  105. /* Fl_Button */
  106. #define BUTTON_WIDTH 115
  107. #define BUTTON_HEIGHT 27
  108. /* Fl_Round_Button */
  109. #define RADIO_MIN_WIDTH (FL_NORMAL_SIZE + 5)
  110. #define RADIO_HEIGHT (FL_NORMAL_SIZE + 7)
  111. /* Fl_Check_Button */
  112. #define CHECK_MIN_WIDTH RADIO_MIN_WIDTH
  113. #define CHECK_HEIGHT RADIO_HEIGHT
  114. /* Fl_Choice */
  115. #define CHOICE_HEIGHT INPUT_HEIGHT
  116. /* Fl_Group */
  117. #define GROUP_LABEL_OFFSET FL_NORMAL_SIZE
  118. #define GROUP_MARGIN 12
  119. /**** HELPERS FOR DYNAMIC TEXT ****/
  120. /* Extra space to add after any text line */
  121. #define TEXT_PADDING 2
  122. /* Use this when the text extends to the right (e.g. checkboxes) */
  123. #define LBLRIGHT(x, y, w, h, str) \
  124. (x), (y), (w) + gui_str_len(str) + TEXT_PADDING, (h), (str)
  125. /* Use this when the space for the label is taken from the left (e.g. input) */
  126. #define LBLLEFT(x, y, w, h, str) \
  127. (x) + (gui_str_len(str) + TEXT_PADDING), (y), \
  128. (w) - (gui_str_len(str) + TEXT_PADDING), (h), (str)
  129. #endif