2 * \file xforms_resize.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
14 #include "xforms_resize.h"
16 #include <boost/assert.hpp>
18 #include <algorithm> // std::max. Use FL_max in .c code...
19 #include "lyx_forms.h"
24 /* This is hacked straight out of the xforms source.
25 It is fl_adjust_form_size without the last few lines that do the
28 double get_scaling_factor(FL_FORM * form)
31 float xfactor, yfactor, max_factor, factor;
33 float xm = 0.5f, ym = 0.5f;
36 // if (fl_no_connection)
39 max_factor = factor = 1.0f;
40 for (ob = form->first; ob; ob = ob->next)
42 if ((ob->align == FL_ALIGN_CENTER || (ob->align & FL_ALIGN_INSIDE) ||
43 ob->objclass == FL_INPUT) &&
44 !ob->is_child && *(ob->label) && ob->label[0] != '@' &&
45 ob->boxtype != FL_NO_BOX &&
46 (ob->boxtype != FL_FLAT_BOX || ob->objclass == FL_MENU))
48 fl_get_string_dimension(ob->lstyle, ob->lsize, ob->label,
49 strlen(ob->label), &sw, &sh);
51 bw = (ob->boxtype == FL_UP_BOX || ob->boxtype == FL_DOWN_BOX) ?
54 if (ob->objclass == FL_BUTTON &&
55 (ob->type == FL_RETURN_BUTTON || ob->type == FL_MENU_BUTTON))
56 sw += (int)FL_min(0.6f * ob->h, 0.6f * ob->w) - 1;
58 if (ob->objclass == FL_BUTTON && ob->type == FL_LIGHTBUTTON)
59 sw += FL_LIGHTBUTTON_MINSIZE + 1;
61 if (sw <= (ob->w - 2 * (bw + xm)) && sh <= (ob->h - 2 * (bw + ym)))
64 if ((osize = ob->w - 2 * (int)(bw + xm)) <= 0)
66 xfactor = (float) sw / (float)osize;
68 if ((osize = ob->h - 2 * (int)(bw + ym)) <= 0)
70 yfactor = (float) sh / osize;
72 if (ob->objclass == FL_INPUT)
75 yfactor = (sh + 1.6f) / osize;
78 if ((factor = FL_max(xfactor, yfactor)) > max_factor)
85 if (max_factor <= 1.0f)
88 max_factor = 0.01f * (int) (max_factor * 100.0f);
90 if (max_factor > 1.25f)
97 double get_tabfolder_scale_to_fit(FL_OBJECT * folder)
99 BOOST_ASSERT(folder && folder->objclass == FL_TABFOLDER);
101 fl_freeze_form(folder->form);
102 int const saved_folder_id = fl_get_folder_number(folder);
105 int const size = fl_get_tabfolder_numfolders(folder);
106 for (int i = 0; i < size; ++i) {
107 fl_set_folder_bynumber(folder, i+1);
108 FL_FORM * leaf = fl_get_folder(folder);
109 factor = std::max(factor, get_scale_to_fit(leaf));
112 fl_set_folder_bynumber(folder, saved_folder_id);
113 fl_unfreeze_form(folder->form);
119 void scale_tabfolder_horizontally(FL_OBJECT * folder, double factor)
121 BOOST_ASSERT(folder && folder->objclass == FL_TABFOLDER);
123 fl_freeze_form(folder->form);
124 int const saved_folder_id = fl_get_folder_number(folder);
126 int const size = fl_get_tabfolder_numfolders(folder);
127 for (int i = 0; i < size; ++i) {
128 fl_set_folder_bynumber(folder, i+1);
129 FL_FORM * leaf = fl_get_folder(folder);
130 scale_form_horizontally(leaf, factor);
133 fl_set_folder_bynumber(folder, saved_folder_id);
134 fl_unfreeze_form(folder->form);
140 double get_scale_to_fit(FL_FORM * form)
144 double factor = get_scaling_factor(form);
145 for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
146 if (ob->objclass == FL_TABFOLDER)
147 factor = std::max(factor,
148 get_tabfolder_scale_to_fit(ob));
154 void scale_form_horizontally(FL_FORM * form, double factor)
161 fl_scale_form(form, factor, 1);
163 for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
164 if (ob->objclass == FL_TABFOLDER)
165 scale_tabfolder_horizontally(ob, factor);