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"
26 /* This is hacked straight out of the xforms source.
27 It is fl_adjust_form_size without the last few lines that do the
30 double get_scaling_factor(FL_FORM * form)
33 float xfactor, yfactor, max_factor, factor;
35 float xm = 0.5f, ym = 0.5f;
38 // if (fl_no_connection)
41 max_factor = factor = 1.0f;
42 for (ob = form->first; ob; ob = ob->next)
44 if ((ob->align == FL_ALIGN_CENTER || (ob->align & FL_ALIGN_INSIDE) ||
45 ob->objclass == FL_INPUT) &&
46 !ob->is_child && *(ob->label) && ob->label[0] != '@' &&
47 ob->boxtype != FL_NO_BOX &&
48 (ob->boxtype != FL_FLAT_BOX || ob->objclass == FL_MENU))
50 fl_get_string_dimension(ob->lstyle, ob->lsize, ob->label,
51 strlen(ob->label), &sw, &sh);
53 bw = (ob->boxtype == FL_UP_BOX || ob->boxtype == FL_DOWN_BOX) ?
56 if (ob->objclass == FL_BUTTON &&
57 (ob->type == FL_RETURN_BUTTON || ob->type == FL_MENU_BUTTON))
58 sw += (int)FL_min(0.6f * ob->h, 0.6f * ob->w) - 1;
60 if (ob->objclass == FL_BUTTON && ob->type == FL_LIGHTBUTTON)
61 sw += FL_LIGHTBUTTON_MINSIZE + 1;
63 if (sw <= (ob->w - 2 * (bw + xm)) && sh <= (ob->h - 2 * (bw + ym)))
66 if ((osize = ob->w - 2 * (int)(bw + xm)) <= 0)
68 xfactor = (float) sw / (float)osize;
70 if ((osize = ob->h - 2 * (int)(bw + ym)) <= 0)
72 yfactor = (float) sh / osize;
74 if (ob->objclass == FL_INPUT)
77 yfactor = (sh + 1.6f) / osize;
80 if ((factor = FL_max(xfactor, yfactor)) > max_factor)
87 if (max_factor <= 1.0f)
90 max_factor = 0.01f * (int) (max_factor * 100.0f);
92 if (max_factor > 1.25f)
99 double get_tabfolder_scale_to_fit(FL_OBJECT * folder)
101 BOOST_ASSERT(folder && folder->objclass == FL_TABFOLDER);
103 fl_freeze_form(folder->form);
104 int const saved_folder_id = fl_get_folder_number(folder);
107 int const size = fl_get_tabfolder_numfolders(folder);
108 for (int i = 0; i < size; ++i) {
109 fl_set_folder_bynumber(folder, i+1);
110 FL_FORM * leaf = fl_get_folder(folder);
111 factor = std::max(factor, get_scale_to_fit(leaf));
114 fl_set_folder_bynumber(folder, saved_folder_id);
115 fl_unfreeze_form(folder->form);
121 void scale_tabfolder_horizontally(FL_OBJECT * folder, double factor)
123 BOOST_ASSERT(folder && folder->objclass == FL_TABFOLDER);
125 fl_freeze_form(folder->form);
126 int const saved_folder_id = fl_get_folder_number(folder);
128 int const size = fl_get_tabfolder_numfolders(folder);
129 for (int i = 0; i < size; ++i) {
130 fl_set_folder_bynumber(folder, i+1);
131 FL_FORM * leaf = fl_get_folder(folder);
132 scale_form_horizontally(leaf, factor);
135 fl_set_folder_bynumber(folder, saved_folder_id);
136 fl_unfreeze_form(folder->form);
142 double get_scale_to_fit(FL_FORM * form)
146 double factor = get_scaling_factor(form);
147 for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
148 if (ob->objclass == FL_TABFOLDER)
149 factor = std::max(factor,
150 get_tabfolder_scale_to_fit(ob));
156 void scale_form_horizontally(FL_FORM * form, double factor)
163 fl_scale_form(form, factor, 1);
165 for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
166 if (ob->objclass == FL_TABFOLDER)
167 scale_tabfolder_horizontally(ob, factor);
171 } // namespace frontend