2 * \file xforms_resize.C
3 * Copyright 2000-2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
12 #pragma implementation
15 #include "xforms_resize.h"
20 #include FORMS_H_LOCATION
26 // Returns the FL_OBJECT * of class FL_TABFOLDER if present in the form.
28 FL_OBJECT * find_tabfolder(FL_FORM * form);
30 // Takes an FL_OBJECT * folder of class FL_TABFOLDER and calculates how much it
31 // should be scaled to display all the tabs.
32 // Returns 1 if the tabs are alll fully visible.
33 // Nested tabfolders are not considered.
34 double scale_to_fit_tabs(FL_OBJECT * folder);
36 // Returns the leaves making up a tabfolder.
37 vector<FL_FORM *> const leaves_in_tabfolder(FL_OBJECT * folder);
39 // The labels of each tab in the folder.
40 vector<string> const tab_names(FL_OBJECT * folder);
45 double scale_to_fit_tabs(FL_FORM * form)
50 FL_OBJECT * folder = find_tabfolder(form);
54 double scale = scale_to_fit_tabs(folder);
56 vector<FL_FORM *> const leaves = leaves_in_tabfolder(folder);
57 vector<FL_FORM *>::const_iterator it = leaves.begin();
58 vector<FL_FORM *>::const_iterator end = leaves.end();
60 for (; it != end; ++it) {
61 folder = find_tabfolder(*it);
63 scale = std::max(scale, scale_to_fit_tabs(folder));
70 void scale_form(FL_FORM * form, double scale_factor)
75 fl_scale_form(form, scale_factor, 1);
77 FL_OBJECT * folder = find_tabfolder(form);
81 vector<FL_FORM *> const leaves = leaves_in_tabfolder(folder);
82 vector<FL_FORM *>::const_iterator it = leaves.begin();
83 vector<FL_FORM *>::const_iterator end = leaves.end();
85 for (; it != end; ++it) {
86 fl_scale_form(*it, scale_factor, 1);
93 FL_OBJECT * find_tabfolder(FL_FORM * form)
95 for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
96 if (ob->objclass == FL_TABFOLDER)
104 // Return the scaling factor needed to render all the tab labels.
105 double scale_to_fit_tabs(FL_OBJECT * folder)
107 if (folder->objclass != FL_TABFOLDER)
110 // The problem here is that we can access the names of a tabfolder's
111 // tabs directly (through fl_get_folder_name(folder)), but we
112 // can't access directly the tabs themselves. Nonetheless, they are
113 // made visible as buttons on the parent form, so we can obtain
114 // the style info by a judicious camparison of names. This style info
115 // (label font size, style) is needed to ascertain how much space
116 // the label takes up.
118 // The tabfolder makes the tabs (buttons) visible to the parent form,
119 // so to identify them, we need their names.
120 vector<string> const names = tab_names(folder);
124 FL_FORM * parent = folder->form;
128 // Ascertain the style parameters of the tabs.
129 int label_style = FL_BOLD_STYLE;
130 int label_size = FL_NORMAL_SIZE;
132 // Hard-coded within xforms' fl_create_tabfolder in tabfolder.c.
133 int const tab_padding = 12;
135 for (FL_OBJECT * ob = parent->first; ob; ob = ob->next) {
139 vector<string>::const_iterator it = names.begin();
140 vector<string>::const_iterator end = names.end();
141 it = std::find(it, end, ob->label);
146 label_style = ob->lstyle;
147 label_size = ob->lsize;
149 if (ob->boxtype == FL_UP_BOX || ob->boxtype == FL_DOWN_BOX)
150 box_width = FL_abs(ob->bw);
152 // achieved all we set out to achieve, so break.
155 // Hard coded in xforms' get_tabsize in tabfolder.c as
156 // (2 + fudge) * box_width, where fudge is always 1.
157 int const box_width_factor = 3 * box_width;
159 // Loop over the names and calculate how much space is needed to display
163 vector<string>::const_iterator it = names.begin();
164 vector<string>::const_iterator end = names.end();
166 for (; it != end; ++it) {
169 fl_get_string_dimension(label_style, label_size,
170 it->c_str(), int(it->size()),
173 // This is the minimum width the object must be to contain
175 length += sw + tab_padding + box_width_factor;
178 // Compare this length to the width of the tabfolder
179 double const scale = double(length) / double(folder->w);
180 return std::max(double(1), scale);
184 // A nasty hack for older xforms versions
185 int get_tabfolder_numfolders(FL_OBJECT * folder)
187 #if FL_VERSION > 0 || FL_REVISION > 88
188 return fl_get_tabfolder_numfolders(folder);
190 if (folder->objclass != FL_TABFOLDER)
193 fl_freeze_form(folder->form);
194 int const saved_folder_id = fl_get_folder_number(folder);
197 FL_FORM const * old_leaf = 0;
199 int const id = num_folders + 1;
200 fl_set_folder_bynumber(folder, id);
201 FL_FORM const * const leaf = fl_get_folder(folder);
202 if (!leaf || leaf == old_leaf) {
203 // unable to increment succesfully.
210 fl_set_folder_bynumber(folder, saved_folder_id);
211 fl_unfreeze_form(folder->form);
218 // Returns the leaves making up a tabfolder.
219 vector<FL_FORM *> const leaves_in_tabfolder(FL_OBJECT * folder)
221 if (folder->objclass != FL_TABFOLDER)
222 return vector<FL_FORM *>();
224 fl_freeze_form(folder->form);
225 int const saved_folder_id = fl_get_folder_number(folder);
227 int const size = get_tabfolder_numfolders(folder);
228 vector<FL_FORM *> leaves(size);
230 for (int i = 0; i < size; ++i) {
231 fl_set_folder_bynumber(folder, i+1);
232 leaves[i] = fl_get_folder(folder);
235 fl_set_folder_bynumber(folder, saved_folder_id);
236 fl_unfreeze_form(folder->form);
242 // The labels of each tab in the folder.
243 vector<string> const tab_names(FL_OBJECT * folder)
245 if (folder->objclass != FL_TABFOLDER)
246 return vector<string>();
248 fl_freeze_form(folder->form);
249 int const saved_folder_id = fl_get_folder_number(folder);
251 int const size = get_tabfolder_numfolders(folder);
252 vector<string> names(size);
254 for (int i = 0; i < size; ++i) {
255 fl_set_folder_bynumber(folder, i+1);
257 char const * const name = fl_get_folder_name(folder);
262 fl_set_folder_bynumber(folder, saved_folder_id);
263 fl_unfreeze_form(folder->form);