]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xforms_resize.C
Prevent crashes experienced by speakers of "long" languages like Norwegian
[lyx.git] / src / frontends / xforms / xforms_resize.C
1 /**
2  * \file xforms_resize.C
3  * Copyright 2000-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "xforms_resize.h"
16
17 #include "LString.h"
18 #include <algorithm>
19 #include <vector>
20
21 using std::vector;
22
23 namespace {
24
25 // Returns the FL_OBJECT * of class FL_TABFOLDER if present in the form.
26 // Else 0.
27 FL_OBJECT * find_tabfolder(FL_FORM * form);
28
29 // Takes an FL_OBJECT * folder of class FL_TABFOLDER and calculates how much it
30 // should be scaled to display all the tabs.
31 // Returns 1 if the tabs are alll fully visible.
32 // Nested tabfolders are not considered.
33 double scale_to_fit_tabs(FL_OBJECT * folder);
34
35 // Returns the leaves making up a tabfolder.
36 vector<FL_FORM *> const leaves_in_tabfolder(FL_OBJECT * folder);
37
38 // The labels of each tab in the folder.
39 vector<string> const tab_names(FL_OBJECT * folder);
40
41 } // namespace anon
42
43
44 double scale_to_fit_tabs(FL_FORM * form)
45 {
46         if (!form)
47                 return 1;
48
49         FL_OBJECT * folder = find_tabfolder(form);
50         if (!folder)
51                 return 1;
52
53         double scale = scale_to_fit_tabs(folder);
54
55         vector<FL_FORM *> const leaves = leaves_in_tabfolder(folder);
56         vector<FL_FORM *>::const_iterator it  = leaves.begin();
57         vector<FL_FORM *>::const_iterator end = leaves.end();
58
59         for (; it != end; ++it) {
60                 folder = find_tabfolder(*it);
61                 if (!folder)
62                         continue;
63                 double folder_scale = scale_to_fit_tabs(folder);
64                 scale = std::max(scale, folder_scale);
65         }
66
67         return scale;
68 }
69
70
71 void scale_form(FL_FORM * form, double scale_factor)
72 {
73         if (!form)
74                 return;
75
76         fl_scale_form(form, scale_factor, 1);
77
78         FL_OBJECT * folder = find_tabfolder(form);
79         if (!folder)
80                 return;
81
82         vector<FL_FORM *> const leaves = leaves_in_tabfolder(folder);
83         vector<FL_FORM *>::const_iterator it  = leaves.begin();
84         vector<FL_FORM *>::const_iterator end = leaves.end();
85
86         for (; it != end; ++it) {
87                 fl_scale_form(*it, scale_factor, 1);
88         }
89 }
90
91
92 namespace {
93
94 FL_OBJECT * find_tabfolder(FL_FORM * form)
95 {
96         for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
97                 if (ob->objclass == FL_TABFOLDER)
98                         return ob;
99         }
100
101         return 0;
102 }
103
104
105 // Return the scaling factor needed to render all the tab labels.
106 double scale_to_fit_tabs(FL_OBJECT * folder)
107 {
108         if (folder->objclass != FL_TABFOLDER)
109                 return 1;
110
111         // The problem here is that we can access the names of a tabfolder's
112         // tabs directly (through fl_get_folder_name(folder)), but we
113         // can't access directly the tabs themselves. Nonetheless, they are
114         // made visible as buttons on the parent form, so we can obtain
115         // the style info by a judicious camparison of names. This style info
116         // (label font size, style) is needed to ascertain how much space
117         // the label takes up.
118
119         // The tabfolder makes the tabs (buttons) visible to the parent form,
120         // so to identify them, we need their names.
121         vector<string> const names = tab_names(folder);
122         if (names.empty())
123                 return 1;
124
125         FL_FORM * parent = folder->form;
126         if (!parent)
127                 return 1;
128
129         // Ascertain the style parameters of the tabs.
130         int label_style = FL_BOLD_STYLE;
131         int label_size  = FL_NORMAL_SIZE;
132         int box_width   = 1;
133         // Hard-coded within xforms' fl_create_tabfolder in tabfolder.c.
134         int const tab_padding =  12;
135
136         for (FL_OBJECT * ob = parent->first; ob; ob = ob->next) {
137                 if (!ob->label)
138                         continue;
139
140                 vector<string>::const_iterator it  = names.begin();
141                 vector<string>::const_iterator end = names.end();
142                 it = std::find(it, end, ob->label);
143
144                 if (it == end)
145                         continue;
146
147                 label_style = ob->lstyle;
148                 label_size  = ob->lsize;
149                 
150                 if (ob->boxtype == FL_UP_BOX || ob->boxtype == FL_DOWN_BOX)
151                         box_width = FL_abs(ob->bw);
152
153                 // achieved all we set out to achieve, so break.
154                 break;
155         }
156         // Hard coded in xforms' get_tabsize in tabfolder.c as
157         // (2 + fudge) * box_width, where fudge is always 1.
158         int const box_width_factor = 3 * box_width;
159
160         // Loop over the names and calculate how much space is needed to display
161         // them.
162         int length = 0;
163
164         vector<string>::const_iterator it  = names.begin();
165         vector<string>::const_iterator end = names.end();
166
167         for (; it != end; ++it) {
168                 int sw, sh;
169
170                 fl_get_string_dimension(label_style, label_size,
171                                         it->c_str(), int(it->size()),
172                                         &sw, &sh);
173
174                 // This is the minimum width the object must be to contain
175                 // the label
176                 length += sw + tab_padding + box_width_factor;
177         }
178
179         // Compare this length to the width of the tabfolder
180         double scale = double(length) / double(folder->w);
181         return std::max(double(1), scale);
182 }
183
184
185 // Returns the leaves making up a tabfolder.
186 vector<FL_FORM *> const leaves_in_tabfolder(FL_OBJECT * folder)
187 {
188         if (folder->objclass != FL_TABFOLDER)
189                 return vector<FL_FORM *>();
190
191         fl_freeze_form(folder->form);
192         int const folder_id = fl_get_folder_number(folder);
193
194         int const size = fl_get_tabfolder_numfolders(folder);
195         vector<FL_FORM *> leaves(size);
196
197         for (int i = 0; i < size; ++i) {
198                 fl_set_folder_bynumber(folder, i+1);
199                 leaves[i] = fl_get_folder(folder);
200         }
201
202         fl_set_folder_bynumber(folder, folder_id);
203         fl_unfreeze_form(folder->form);
204
205         return leaves;
206 }
207
208
209 // The labels of each tab in the folder.
210 vector<string> const tab_names(FL_OBJECT * folder)
211 {
212         if (folder->objclass != FL_TABFOLDER)
213                 return vector<string>();
214
215         fl_freeze_form(folder->form);
216         int const folder_id = fl_get_folder_number(folder);
217
218         int const size = fl_get_tabfolder_numfolders(folder);
219         vector<string> names(size);
220
221         for (int i = 0; i < size; ++i) {
222                 fl_set_folder_bynumber(folder, i+1);
223
224                 const char * const name = fl_get_folder_name(folder);
225                 if (name)
226                         names[i] = name;
227         }
228
229         fl_set_folder_bynumber(folder, folder_id);
230         fl_unfreeze_form(folder->form);
231
232         return names;
233 }
234
235 } // namespace anon