1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * ======================================================
10 * \file FormGraphics.C
11 * \author Baruch Even, baruch.even@writeme.com
17 #pragma implementation
21 #include "ControlGraphics.h"
22 #include "FormGraphics.h"
23 #include "form_graphics.h"
25 #include "xforms_helpers.h"
26 #include "input_validators.h"
27 #include "debug.h" // for lyxerr
28 #include "support/lstrings.h" // for strToDbl & tostr
29 #include "support/FileInfo.h" // for FileInfo
30 #include "insets/insetgraphicsParams.h"
31 #include "lyxrc.h" // for lyxrc.display_graphics
35 static double const tol = 1.0e-08;
37 typedef FormCB<ControlGraphics, FormDB<FD_form_graphics> > base_class;
39 FormGraphics::FormGraphics(ControlGraphics & c)
40 : base_class(c, _("Graphics"))
44 void FormGraphics::build()
46 dialog_.reset(build_graphics());
48 // This is the place to add settings of the dialog that did not go
51 // Set the input widgets to issue a callback to input() whenever
52 // they change, so we can verify their content.
53 fl_set_input_return (dialog_->input_filename, FL_RETURN_CHANGED);
54 fl_set_input_return (dialog_->input_scale, FL_RETURN_CHANGED);
55 fl_set_input_return (dialog_->input_width, FL_RETURN_CHANGED);
56 fl_set_input_return (dialog_->input_height, FL_RETURN_CHANGED);
57 fl_set_input_return (dialog_->input_rotate_angle, FL_RETURN_CHANGED);
58 fl_set_input_return (dialog_->input_subcaption, FL_RETURN_CHANGED);
60 // Set the maximum characters that can be written in the input texts.
61 fl_set_input_maxchars(dialog_->input_scale, SCALE_MAXDIGITS);
62 fl_set_input_maxchars(dialog_->input_width, WIDTH_MAXDIGITS);
63 fl_set_input_maxchars(dialog_->input_height, HEIGHT_MAXDIGITS);
64 fl_set_input_maxchars(dialog_->input_filename, FILENAME_MAXCHARS);
65 fl_set_input_maxchars(dialog_->input_rotate_angle, ROTATE_MAXCHARS);
67 // Set input filter on width and height to make them accept only
69 fl_set_input_filter(dialog_->input_scale, fl_unsigned_float_filter);
70 fl_set_input_filter(dialog_->input_width, fl_unsigned_float_filter);
71 fl_set_input_filter(dialog_->input_height, fl_unsigned_float_filter);
73 // Set input filter on rotate_angle to make it accept only
74 // floating point numbers.
75 fl_set_input_filter(dialog_->input_rotate_angle, fl_float_filter);
77 // Create the contents of the choices
78 string const width = " cm | inch | page %% | column %% ";
79 fl_addto_choice(dialog_->choice_width_units, width.c_str());
81 string const height = " cm | inch | page %% ";
82 fl_addto_choice(dialog_->choice_height_units, height.c_str());
84 // Manage the ok, apply, restore and cancel/close buttons
85 bc().setOK(dialog_->button_ok);
86 bc().setApply(dialog_->button_apply);
87 bc().setCancel(dialog_->button_cancel);
88 bc().setRestore(dialog_->button_restore);
90 bc().addReadOnly(dialog_->input_filename);
91 bc().addReadOnly(dialog_->button_browse);
92 bc().addReadOnly(dialog_->check_display);
93 bc().addReadOnly(dialog_->input_scale);
94 bc().addReadOnly(dialog_->input_width);
95 bc().addReadOnly(dialog_->choice_width_units);
96 bc().addReadOnly(dialog_->input_height);
97 bc().addReadOnly(dialog_->choice_height_units);
98 bc().addReadOnly(dialog_->input_rotate_angle);
99 bc().addReadOnly(dialog_->input_subcaption);
100 bc().addReadOnly(dialog_->check_subcaption);
104 void FormGraphics::apply()
106 // Create the parameters structure and fill the data from the dialog.
107 InsetGraphicsParams & igp = controller().params();
109 igp.filename = fl_get_input(dialog_->input_filename);
111 if (lyxrc.display_graphics == "no") {
112 igp.display = InsetGraphicsParams::NONE;
115 if (fl_get_button(dialog_->check_display)) {
116 if (lyxrc.display_graphics == "mono") {
117 igp.display = InsetGraphicsParams::MONOCHROME;
118 } else if (lyxrc.display_graphics == "gray") {
119 igp.display = InsetGraphicsParams::GRAYSCALE;
120 } else if (lyxrc.display_graphics == "color") {
121 igp.display = InsetGraphicsParams::COLOR;
125 igp.display = InsetGraphicsParams::NONE;
130 strToDbl(strip(fl_get_input(dialog_->input_scale)));
133 strToDbl(strip(fl_get_input(dialog_->input_width)));
136 igp.widthResize = InsetGraphicsParams::DEFAULT_SIZE;
139 switch (fl_get_choice(dialog_->choice_width_units)) {
141 igp.widthResize = InsetGraphicsParams::INCH;
145 InsetGraphicsParams::PERCENT_PAGE;
149 InsetGraphicsParams::PERCENT_COLUMN;
152 igp.widthResize = InsetGraphicsParams::CM;
155 igp.widthSize = width;
158 double const height =
159 strToDbl(strip(fl_get_input(dialog_->input_height)));
162 igp.heightResize = InsetGraphicsParams::DEFAULT_SIZE;
163 igp.heightSize = 0.0;
165 switch (fl_get_choice(dialog_->choice_height_units)) {
167 igp.heightResize = InsetGraphicsParams::INCH;
171 InsetGraphicsParams::PERCENT_PAGE;
174 igp.heightResize = InsetGraphicsParams::CM;
177 igp.heightSize = height;
181 igp.widthResize = InsetGraphicsParams::DEFAULT_SIZE;
183 igp.heightResize = InsetGraphicsParams::SCALE;
184 igp.heightSize = scale;
188 strToDbl(strip(fl_get_input(dialog_->input_rotate_angle)));
189 while (igp.rotateAngle < 0.0 || igp.rotateAngle > 360.0) {
190 if (igp.rotateAngle < 0.0) {
191 igp.rotateAngle += 360.0;
192 } else if (igp.rotateAngle > 360.0) {
193 igp.rotateAngle -= 360.0;
197 igp.subcaption = fl_get_button(dialog_->check_subcaption);
198 igp.subcaptionText = fl_get_input(dialog_->input_subcaption);
204 void FormGraphics::update()
206 // Update dialog with details from inset
207 InsetGraphicsParams & igp = controller().params();
209 // Update the filename input field
210 fl_set_input(dialog_->input_filename,
211 igp.filename.c_str());
213 // To display or not to display
214 if (lyxrc.display_graphics == "no") {
215 fl_set_button(dialog_->check_display, 0);
217 if (igp.display == InsetGraphicsParams::NONE) {
218 fl_set_button(dialog_->check_display, 0);
220 fl_set_button(dialog_->check_display, 1);
224 setEnabled(dialog_->check_display, (lyxrc.display_graphics != "no"));
226 if (igp.heightResize == InsetGraphicsParams::SCALE) {
227 string number = tostr(igp.heightSize);
228 fl_set_input(dialog_->input_scale, number.c_str());
229 fl_set_input(dialog_->input_width, "");
230 fl_set_choice(dialog_->choice_width_units, 1);
231 fl_set_input(dialog_->input_height, "");
232 fl_set_choice(dialog_->choice_height_units, 1);
235 fl_set_input(dialog_->input_scale, "");
238 if (igp.widthResize != InsetGraphicsParams::DEFAULT_SIZE) {
239 number = tostr(igp.widthSize);
241 fl_set_input(dialog_->input_width, number.c_str());
244 switch (igp.widthResize) {
245 case InsetGraphicsParams::INCH:
248 case InsetGraphicsParams::PERCENT_PAGE:
251 case InsetGraphicsParams::PERCENT_COLUMN:
257 fl_set_choice(dialog_->choice_width_units, pos);
260 if (igp.heightResize != InsetGraphicsParams::DEFAULT_SIZE) {
261 number = tostr(igp.heightSize);
263 fl_set_input(dialog_->input_height, number.c_str());
266 switch (igp.heightResize) {
267 case InsetGraphicsParams::INCH:
270 case InsetGraphicsParams::PERCENT_PAGE:
276 fl_set_choice(dialog_->choice_height_units, pos);
279 // Update the rotate angle
280 fl_set_input(dialog_->input_rotate_angle,
281 tostr(igp.rotateAngle).c_str());
283 // Update the subcaption check button and input field
284 fl_set_button(dialog_->check_subcaption,
286 fl_set_input(dialog_->input_subcaption,
287 igp.subcaptionText.c_str());
289 setEnabled(dialog_->input_subcaption,
290 fl_get_button(dialog_->check_subcaption));
294 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
296 if (ob == dialog_->button_browse) {
297 // Get the filename from the dialog
298 string const in_name = fl_get_input(dialog_->input_filename);
299 string const out_name = controller().Browse(in_name);
301 if (out_name != in_name && !out_name.empty()) {
302 fl_set_input(dialog_->input_filename, out_name.c_str());
306 if (ob == dialog_->input_scale) {
308 strToDbl(strip(fl_get_input(dialog_->input_scale)));
310 fl_set_input(dialog_->input_width, "");
311 fl_set_choice(dialog_->choice_width_units, 1);
312 fl_set_input(dialog_->input_height, "");
313 fl_set_choice(dialog_->choice_height_units, 1);
317 if (ob == dialog_->input_width || ob == dialog_->input_height) {
319 strToDbl(strip(fl_get_input(dialog_->input_width)));
320 double const height =
321 strToDbl(strip(fl_get_input(dialog_->input_height)));
323 if (width > tol || height > tol) {
324 fl_set_input(dialog_->input_scale, "");
328 if (ob == dialog_->check_subcaption) {
329 setEnabled(dialog_->input_subcaption,
330 fl_get_button(dialog_->check_subcaption));
337 ButtonPolicy::SMInput FormGraphics::checkInput()
339 // Put verifications that the dialog shows some sane values,
340 // if not disallow clicking on ok/apply.
341 // Possibly use a label in the bottom of the dialog to give the reason.
343 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
345 // We verify now that there is a filename, it exists, it's a file
346 // and it's readable.
347 string filename = fl_get_input(dialog_->input_filename);
348 FileInfo file(filename);
355 activate = ButtonPolicy::SMI_INVALID;