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"
25 // Returns the FL_OBJECT * of class FL_TABFOLDER if present in the form.
27 FL_OBJECT * find_tabfolder(FL_FORM * form);
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);
35 // Returns the leaves making up a tabfolder.
36 vector<FL_FORM *> const leaves_in_tabfolder(FL_OBJECT * folder);
38 // The labels of each tab in the folder.
39 vector<string> const tab_names(FL_OBJECT * folder);
44 double scale_to_fit_tabs(FL_FORM * form)
49 FL_OBJECT * folder = find_tabfolder(form);
53 double scale = scale_to_fit_tabs(folder);
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();
59 for (; it != end; ++it) {
60 folder = find_tabfolder(*it);
63 double folder_scale = scale_to_fit_tabs(folder);
64 scale = std::max(scale, folder_scale);
71 void scale_form(FL_FORM * form, double scale_factor)
76 fl_scale_form(form, scale_factor, 1);
78 FL_OBJECT * folder = find_tabfolder(form);
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();
86 for (; it != end; ++it) {
87 fl_scale_form(*it, scale_factor, 1);
94 FL_OBJECT * find_tabfolder(FL_FORM * form)
96 for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
97 if (ob->objclass == FL_TABFOLDER)
105 // Return the scaling factor needed to render all the tab labels.
106 double scale_to_fit_tabs(FL_OBJECT * folder)
108 if (folder->objclass != FL_TABFOLDER)
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.
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);
125 FL_FORM * parent = folder->form;
129 // Ascertain the style parameters of the tabs.
130 int label_style = FL_BOLD_STYLE;
131 int label_size = FL_NORMAL_SIZE;
133 // Hard-coded within xforms' fl_create_tabfolder in tabfolder.c.
134 int const tab_padding = 12;
136 for (FL_OBJECT * ob = parent->first; ob; ob = ob->next) {
140 vector<string>::const_iterator it = names.begin();
141 vector<string>::const_iterator end = names.end();
142 it = std::find(it, end, ob->label);
147 label_style = ob->lstyle;
148 label_size = ob->lsize;
150 if (ob->boxtype == FL_UP_BOX || ob->boxtype == FL_DOWN_BOX)
151 box_width = FL_abs(ob->bw);
153 // achieved all we set out to achieve, so break.
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;
160 // Loop over the names and calculate how much space is needed to display
164 vector<string>::const_iterator it = names.begin();
165 vector<string>::const_iterator end = names.end();
167 for (; it != end; ++it) {
170 fl_get_string_dimension(label_style, label_size,
171 it->c_str(), int(it->size()),
174 // This is the minimum width the object must be to contain
176 length += sw + tab_padding + box_width_factor;
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);
185 // Returns the leaves making up a tabfolder.
186 vector<FL_FORM *> const leaves_in_tabfolder(FL_OBJECT * folder)
188 if (folder->objclass != FL_TABFOLDER)
189 return vector<FL_FORM *>();
191 fl_freeze_form(folder->form);
192 int const folder_id = fl_get_folder_number(folder);
194 int const size = fl_get_tabfolder_numfolders(folder);
195 vector<FL_FORM *> leaves(size);
197 for (int i = 0; i < size; ++i) {
198 fl_set_folder_bynumber(folder, i+1);
199 leaves[i] = fl_get_folder(folder);
202 fl_set_folder_bynumber(folder, folder_id);
203 fl_unfreeze_form(folder->form);
209 // The labels of each tab in the folder.
210 vector<string> const tab_names(FL_OBJECT * folder)
212 if (folder->objclass != FL_TABFOLDER)
213 return vector<string>();
215 fl_freeze_form(folder->form);
216 int const folder_id = fl_get_folder_number(folder);
218 int const size = fl_get_tabfolder_numfolders(folder);
219 vector<string> names(size);
221 for (int i = 0; i < size; ++i) {
222 fl_set_folder_bynumber(folder, i+1);
224 const char * const name = fl_get_folder_name(folder);
229 fl_set_folder_bynumber(folder, folder_id);
230 fl_unfreeze_form(folder->form);