blob: 9e5c2d47c2a53817b27602183e867a78a069e972 [file] [log] [blame]
Pierre Ossmand463b572011-05-16 12:04:43 +00001/* 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
19#ifndef __FLTK_LAYOUT_H__
20#define __FLTK_LAYOUT_H__
21
22#include <FL/fl_draw.H>
Pierre Ossman245c8022015-02-25 11:27:49 +010023#include <FL/Fl_Menu_.H>
Pierre Ossmand463b572011-05-16 12:04:43 +000024
25/* Calculates the width of a string as printed by FLTK (pixels) */
26static inline int gui_str_len(const char *str)
27{
28 float len;
29
30 fl_font(FL_HELVETICA, FL_NORMAL_SIZE);
31
32 len = fl_width(str);
33
34 return (int)(len + 0.5f);
35}
36
Pierre Ossmanc628ba42011-05-23 12:21:21 +000037/* Escapes all @ in text as those have special meaning in labels */
Pierre Ossmand7f84502015-03-04 09:52:48 +010038static inline size_t fltk_escape(const char *in, char *out, size_t maxlen)
Pierre Ossmanc628ba42011-05-23 12:21:21 +000039{
Pierre Ossmand7f84502015-03-04 09:52:48 +010040 size_t len;
Pierre Ossmanc628ba42011-05-23 12:21:21 +000041
42 len = 0;
43
44 while (*in != '\0') {
45 if (*in == '@') {
46 if (maxlen >= 3) {
47 *out++ = '@';
48 *out++ = '@';
49 maxlen -= 2;
50 }
51
52 len += 2;
53 } else {
54 if (maxlen >= 2) {
55 *out++ = *in;
56 maxlen--;
57 }
58
59 len += 1;
60 }
61
62 in++;
63 }
64
65 if (maxlen)
66 *out = '\0';
67
68 return len;
69}
70
Pierre Ossman245c8022015-02-25 11:27:49 +010071/* Filter out unsafe characters for menu entries */
Pierre Ossmand7f84502015-03-04 09:52:48 +010072static inline size_t fltk_menu_escape(const char *in, char *out, size_t maxlen)
Pierre Ossman245c8022015-02-25 11:27:49 +010073{
Pierre Ossmand7f84502015-03-04 09:52:48 +010074 size_t len;
Pierre Ossman245c8022015-02-25 11:27:49 +010075
76 len = 0;
77
78 while (*in != '\0') {
79 if (*in == '/') {
80 if (maxlen >= 3) {
81 *out++ = '\\';
82 *out++ = '/';
83 maxlen -= 2;
84 }
85
86 len += 2;
87 } else {
88 if (maxlen >= 2) {
89 *out++ = *in;
90 maxlen--;
91 }
92
93 len += 1;
94 }
95
96 in++;
97 }
98
99 if (maxlen)
100 *out = '\0';
101
102 return len;
103}
104
105/* Helper to add menu entries safely */
106static inline void fltk_menu_add(Fl_Menu_ *menu, const char *text,
107 int shortcut, Fl_Callback *cb,
108 void *data = 0, int flags = 0)
109{
110 char buffer[1024];
111
112 if (fltk_menu_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
113 return;
114
115 menu->add(buffer, shortcut, cb, data, flags);
116}
117
Pierre Ossmand463b572011-05-16 12:04:43 +0000118/**** MARGINS ****/
119
120#define OUTER_MARGIN 10
121#define INNER_MARGIN 10
122
123/* Tighter grouping of related fields */
124#define TIGHT_MARGIN 5
125
126/**** ADJUSTMENTS ****/
127#define INDENT 20
128
129/**** FLTK WIDGETS ****/
130
131/* Fl_Tabs */
132#define TABS_HEIGHT 30
133
134/* Fl_Input */
135#define INPUT_LABEL_OFFSET FL_NORMAL_SIZE
136#define INPUT_HEIGHT 25
137
138/* Fl_Button */
139#define BUTTON_WIDTH 115
140#define BUTTON_HEIGHT 27
141
142/* Fl_Round_Button */
143#define RADIO_MIN_WIDTH (FL_NORMAL_SIZE + 5)
144#define RADIO_HEIGHT (FL_NORMAL_SIZE + 7)
145
146/* Fl_Check_Button */
147#define CHECK_MIN_WIDTH RADIO_MIN_WIDTH
148#define CHECK_HEIGHT RADIO_HEIGHT
149
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000150/* Fl_Choice */
151
152#define CHOICE_HEIGHT INPUT_HEIGHT
153
Pierre Ossmand463b572011-05-16 12:04:43 +0000154/* Fl_Group */
155#define GROUP_LABEL_OFFSET FL_NORMAL_SIZE
156#define GROUP_MARGIN 12
157
158/**** HELPERS FOR DYNAMIC TEXT ****/
159
160/* Extra space to add after any text line */
161#define TEXT_PADDING 2
162
163/* Use this when the text extends to the right (e.g. checkboxes) */
164#define LBLRIGHT(x, y, w, h, str) \
165 (x), (y), (w) + gui_str_len(str) + TEXT_PADDING, (h), (str)
166
167/* Use this when the space for the label is taken from the left (e.g. input) */
168#define LBLLEFT(x, y, w, h, str) \
169 (x) + (gui_str_len(str) + TEXT_PADDING), (y), \
170 (w) - (gui_str_len(str) + TEXT_PADDING), (h), (str)
171
172#endif