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
12 * \author Herbert Voss, voss@perce.de
18 #pragma implementation
22 #include "ControlGraphics.h"
23 #include "FormGraphics.h"
24 #include "form_graphics.h"
26 #include "xforms_helpers.h"
27 #include "input_validators.h"
28 #include "debug.h" // for lyxerr
29 #include "support/lstrings.h" // for strToDbl & tostr
30 #include "support/FileInfo.h"
31 #include "support/filetools.h" // for MakeAbsPath etc
32 #include "insets/insetgraphicsParams.h"
33 #include "lyxrc.h" // for lyxrc.display_graphics
39 // Bound the number of input characters
40 int const SCALE_MAXDIGITS = 3;
41 int const WIDTH_MAXDIGITS = 10;
42 int const HEIGHT_MAXDIGITS = 10;
43 int const ROTATE_MAXCHARS = 4;
44 int const FILENAME_MAXCHARS = 1024;
49 typedef FormCB<ControlGraphics, FormDB<FD_form_graphics> > base_class;
51 FormGraphics::FormGraphics(ControlGraphics & c)
52 : base_class(c, _("Graphics"), false)
56 void FormGraphics::redraw()
58 if(form() && form()->visible)
59 fl_redraw_form(form());
62 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabFolder);
63 if (outer_form && outer_form->visible)
64 fl_redraw_form(outer_form);
68 void FormGraphics::build()
70 dialog_.reset(build_graphics());
72 // Manage the ok, apply, restore and cancel/close buttons
73 bc().setOK(dialog_->button_ok);
74 bc().setApply(dialog_->button_apply);
75 bc().setCancel(dialog_->button_cancel);
76 bc().setRestore(dialog_->button_restore);
79 file_.reset(build_file());
81 fl_set_input_return (file_->input_filename, FL_RETURN_CHANGED);
82 fl_set_input_return (file_->input_subcaption, FL_RETURN_CHANGED);
83 fl_set_input_return (file_->input_rotate_angle, FL_RETURN_CHANGED);
84 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
85 fl_set_input_maxchars(file_->input_rotate_angle, ROTATE_MAXCHARS);
86 fl_set_input_filter(file_->input_rotate_angle, fl_float_filter);
88 string const choice_origin =
90 "leftTop|leftBottom|leftBaseline|" // lt lb lB
91 "centerTop|centerBottom|centerBaseline|" // ct cb cB
92 "rightTop|rightBottom|rightBaseline"; // rt rb rB
93 fl_addto_choice(file_->choice_origin, choice_origin.c_str());
95 bc().addReadOnly(file_->button_browse);
96 bc().addReadOnly(file_->check_subcaption);
97 bc().addReadOnly(file_->check_rotate);
98 bc().addReadOnly(file_->button_clip);
99 bc().addReadOnly(file_->button_draft);
101 // the lyxview section
102 lyxview_.reset(build_lyxview());
104 fl_set_input_return (lyxview_->input_lyxwidth, FL_RETURN_CHANGED);
105 fl_set_input_return (lyxview_->input_lyxheight, FL_RETURN_CHANGED);
106 fl_set_input_return (lyxview_->input_lyxscale, FL_RETURN_CHANGED);
108 fl_addto_choice(lyxview_->choice_width_lyxwidth, choice_Length_WithUnit.c_str());
109 fl_addto_choice(lyxview_->choice_width_lyxheight, choice_Length_WithUnit.c_str());
111 bc().addReadOnly(lyxview_->radio_pref);
112 bc().addReadOnly(lyxview_->radio_mono);
113 bc().addReadOnly(lyxview_->radio_gray);
114 bc().addReadOnly(lyxview_->radio_color);
115 bc().addReadOnly(lyxview_->radio_nodisplay);
118 size_.reset(build_size());
120 fl_set_input_return (size_->input_scale, FL_RETURN_CHANGED);
121 fl_set_input_return (size_->input_width, FL_RETURN_CHANGED);
122 fl_set_input_return (size_->input_height, FL_RETURN_CHANGED);
124 fl_set_input_maxchars(size_->input_scale, SCALE_MAXDIGITS);
125 fl_set_input_maxchars(size_->input_width, WIDTH_MAXDIGITS);
126 fl_set_input_maxchars(size_->input_height, HEIGHT_MAXDIGITS);
128 fl_set_input_filter(size_->input_scale, fl_unsigned_float_filter);
130 fl_addto_choice(size_->choice_width_units, choice_Length_All.c_str());
131 fl_addto_choice(size_->choice_height_units, choice_Length_All.c_str());
133 bc().addReadOnly(size_->button_asis);
134 bc().addReadOnly(size_->button_wh);
135 bc().addReadOnly(size_->button_scale);
136 bc().addReadOnly(size_->check_aspectratio);
138 // the bounding box selection
139 bbox_.reset(build_bbox());
140 fl_set_input_return (bbox_->input_bb_x0, FL_RETURN_CHANGED);
141 fl_set_input_return (bbox_->input_bb_y0, FL_RETURN_CHANGED);
142 fl_set_input_return (bbox_->input_bb_x1, FL_RETURN_CHANGED);
143 fl_set_input_return (bbox_->input_bb_y1, FL_RETURN_CHANGED);
145 string const bb_units = "pt|cm|in";
146 fl_addto_choice(bbox_->choice_bb_x0, bb_units.c_str());
147 fl_addto_choice(bbox_->choice_bb_y0, bb_units.c_str());
148 fl_addto_choice(bbox_->choice_bb_x1, bb_units.c_str());
149 fl_addto_choice(bbox_->choice_bb_y1, bb_units.c_str());
150 bc().addReadOnly(bbox_->button_getBB);
152 // the rotate section
153 special_.reset(build_special());
155 fl_set_input_return (special_->input_special, FL_RETURN_CHANGED);
157 // add the different tabfolders
158 fl_addto_tabfolder(dialog_->tabFolder, _("File"), file_->form);
159 fl_addto_tabfolder(dialog_->tabFolder, _("LyX View"), lyxview_->form);
160 fl_addto_tabfolder(dialog_->tabFolder, _("LaTeX Size"), size_->form);
161 fl_addto_tabfolder(dialog_->tabFolder, _("Bounding Box"), bbox_->form);
162 fl_addto_tabfolder(dialog_->tabFolder, _("Extras"), special_->form);
166 void FormGraphics::apply()
168 // Create the parameters structure and fill the data from the dialog.
169 InsetGraphicsParams & igp = controller().params();
172 igp.filename = getStringFromInput(file_->input_filename);
173 igp.subcaption = fl_get_button(file_->check_subcaption);
174 igp.subcaptionText = getStringFromInput(file_->input_subcaption);
175 igp.rotate = fl_get_button(file_->check_rotate);
177 strToDbl(getStringFromInput(file_->input_rotate_angle));
178 while (igp.rotateAngle < 0.0 || igp.rotateAngle > 360.0) {
179 if (igp.rotateAngle < 0.0) {
180 igp.rotateAngle += 360.0;
181 } else if (igp.rotateAngle > 360.0) {
182 igp.rotateAngle -= 360.0;
185 if (fl_get_choice(file_->choice_origin) > 0)
186 igp.rotateOrigin = fl_get_choice_text(file_->choice_origin);
188 igp.rotateOrigin = string();
189 igp.draft = fl_get_button(file_->button_draft);
190 igp.clip = fl_get_button(file_->button_clip);
192 // the lyxview section
193 if (fl_get_button(lyxview_->radio_pref))
194 igp.display = InsetGraphicsParams::DEFAULT;
195 else if (fl_get_button(lyxview_->radio_mono))
196 igp.display = InsetGraphicsParams::MONOCHROME;
197 else if (fl_get_button(lyxview_->radio_gray))
198 igp.display = InsetGraphicsParams::GRAYSCALE;
199 else if (fl_get_button(lyxview_->radio_color))
200 igp.display = InsetGraphicsParams::COLOR;
201 else if (fl_get_button(lyxview_->radio_nodisplay))
202 igp.display = InsetGraphicsParams::NONE;
204 if (fl_get_button(lyxview_->button_lyxasis))
205 igp.lyxsize_type = InsetGraphicsParams::DEFAULT_SIZE;
206 else if (fl_get_button(lyxview_->button_lyxwh))
207 igp.lyxsize_type = InsetGraphicsParams::WH;
209 igp.lyxsize_type = InsetGraphicsParams::SCALE;
210 igp.lyxwidth = LyXLength(getLengthFromWidgets(lyxview_->input_lyxwidth,
211 lyxview_->choice_width_lyxwidth));
212 igp.lyxheight = LyXLength(getLengthFromWidgets(lyxview_->input_lyxheight,
213 lyxview_->choice_width_lyxheight));
214 igp.lyxscale = strToInt(getStringFromInput(lyxview_->input_lyxscale));
217 if (fl_get_button(size_->button_asis))
218 igp.size_type = InsetGraphicsParams::DEFAULT_SIZE;
219 else if (fl_get_button(size_->button_wh))
220 igp.size_type = InsetGraphicsParams::WH;
222 igp.size_type = InsetGraphicsParams::SCALE;
223 igp.width = LyXLength(getLengthFromWidgets(size_->input_width,
224 size_->choice_width_units));
225 igp.height = LyXLength(getLengthFromWidgets(size_->input_height,
226 size_->choice_height_units));
227 igp.scale = strToInt(getStringFromInput(size_->input_scale));
228 igp.keepAspectRatio = fl_get_button(size_->check_aspectratio);
231 if (!controller().bbChanged) // different to the original one?
232 igp.bb = string(); // don't write anything
235 if (getStringFromInput(bbox_->input_bb_x0).empty())
238 bb = getLengthFromWidgets(bbox_->input_bb_x0,
239 bbox_->choice_bb_x0)+" ";
240 if (getStringFromInput(bbox_->input_bb_y0).empty())
243 bb += (getLengthFromWidgets(bbox_->input_bb_y0,
244 bbox_->choice_bb_y0)+" ");
245 if (getStringFromInput(bbox_->input_bb_x1).empty())
248 bb += (getLengthFromWidgets(bbox_->input_bb_x1,
249 bbox_->choice_bb_x1)+" ");
250 if (getStringFromInput(bbox_->input_bb_y1).empty())
253 bb += (getLengthFromWidgets(bbox_->input_bb_y1,
254 bbox_->choice_bb_y1)+" ");
258 // the special section
259 igp.special = getStringFromInput(special_->input_special);
263 void FormGraphics::update() {
265 if (lyxrc.default_papersize < 3)
267 string const defaultUnit = string(unit);
268 // Update dialog with details from inset
269 InsetGraphicsParams & igp = controller().params();
272 fl_set_input(file_->input_filename, igp.filename.c_str());
273 fl_set_button(file_->check_subcaption, igp.subcaption);
274 fl_set_input(file_->input_subcaption, igp.subcaptionText.c_str());
275 setEnabled(file_->input_subcaption,
276 fl_get_button(file_->check_subcaption));
277 fl_set_button(file_->check_rotate, igp.rotate);
278 fl_set_input(file_->input_rotate_angle,
279 tostr(igp.rotateAngle).c_str());
280 if (igp.rotateOrigin.empty())
281 fl_set_choice(file_->choice_origin,0);
283 fl_set_choice_text(file_->choice_origin,igp.rotateOrigin.c_str());
284 setEnabled(file_->input_rotate_angle,
285 fl_get_button(file_->check_rotate));
286 setEnabled(file_->choice_origin,
287 fl_get_button(file_->check_rotate));
288 fl_set_button(file_->button_draft, igp.draft);
289 fl_set_button(file_->button_clip, igp.clip);
291 // the lyxview section
292 switch (igp.display) {
293 case InsetGraphicsParams::DEFAULT:
294 fl_set_button(lyxview_->radio_pref, 1);
296 case InsetGraphicsParams::MONOCHROME:
297 fl_set_button(lyxview_->radio_mono, 1);
299 case InsetGraphicsParams::GRAYSCALE:
300 fl_set_button(lyxview_->radio_gray, 1);
302 case InsetGraphicsParams::COLOR:
303 fl_set_button(lyxview_->radio_color, 1);
305 case InsetGraphicsParams::NONE:
306 fl_set_button(lyxview_->radio_nodisplay, 1);
309 updateWidgetsFromLength(lyxview_->input_lyxwidth,
310 lyxview_->choice_width_lyxwidth, igp.lyxwidth,defaultUnit);
311 updateWidgetsFromLength(lyxview_->input_lyxheight,
312 lyxview_->choice_width_lyxheight, igp.lyxheight,defaultUnit);
313 fl_set_input(lyxview_->input_lyxscale, tostr(igp.lyxscale).c_str());
314 switch (igp.lyxsize_type) {
315 case InsetGraphicsParams::DEFAULT_SIZE: {
316 fl_set_button(lyxview_->button_lyxasis,1);
317 setEnabled(lyxview_->input_lyxwidth, 0);
318 setEnabled(lyxview_->choice_width_lyxwidth, 0);
319 setEnabled(lyxview_->input_lyxheight, 0);
320 setEnabled(lyxview_->choice_width_lyxheight, 0);
321 setEnabled(lyxview_->input_lyxscale, 0);
324 case InsetGraphicsParams::WH: {
325 fl_set_button(lyxview_->button_lyxwh, 1);
326 setEnabled(lyxview_->input_lyxwidth, 1);
327 setEnabled(lyxview_->choice_width_lyxwidth, 1);
328 setEnabled(lyxview_->input_lyxheight, 1);
329 setEnabled(lyxview_->choice_width_lyxheight, 1);
330 setEnabled(lyxview_->input_lyxscale, 0);
333 case InsetGraphicsParams::SCALE: {
334 fl_set_button(lyxview_->button_lyxscale, 1);
335 setEnabled(lyxview_->input_lyxwidth, 0);
336 setEnabled(lyxview_->choice_width_lyxwidth, 0);
337 setEnabled(lyxview_->input_lyxheight, 0);
338 setEnabled(lyxview_->choice_width_lyxheight, 0);
339 setEnabled(lyxview_->input_lyxscale, 1);
345 // Update the draft and clip mode
346 updateWidgetsFromLength(size_->input_width,
347 size_->choice_width_units,igp.width,defaultUnit);
348 updateWidgetsFromLength(size_->input_height,
349 size_->choice_height_units,igp.height,defaultUnit);
350 fl_set_input(size_->input_scale, tostr(igp.scale).c_str());
351 switch (igp.size_type) {
352 case InsetGraphicsParams::DEFAULT_SIZE: {
353 fl_set_button(size_->button_asis,1);
354 setEnabled(size_->input_width, 0);
355 setEnabled(size_->choice_width_units, 0);
356 setEnabled(size_->input_height, 0);
357 setEnabled(size_->choice_height_units, 0);
358 setEnabled(size_->check_aspectratio, 0);
359 setEnabled(size_->input_scale, 0);
362 case InsetGraphicsParams::WH: {
363 fl_set_button(size_->button_wh, 1);
364 setEnabled(size_->input_width, 1);
365 setEnabled(size_->choice_width_units, 1);
366 setEnabled(size_->input_height, 1);
367 setEnabled(size_->choice_height_units, 1);
368 setEnabled(size_->check_aspectratio, 1);
369 setEnabled(size_->input_scale, 0);
372 case InsetGraphicsParams::SCALE: {
373 fl_set_button(size_->button_scale, 1);
374 setEnabled(size_->input_width, 0);
375 setEnabled(size_->choice_width_units, 0);
376 setEnabled(size_->input_height, 0);
377 setEnabled(size_->choice_height_units, 0);
378 setEnabled(size_->check_aspectratio, 0);
379 setEnabled(size_->input_scale, 1);
383 fl_set_button(size_->check_aspectratio, igp.keepAspectRatio);
386 // set the bounding box values, if exists. First we need the whole
387 // path, because the controller knows nothing about the doc-dir
388 lyxerr << "GraphicsUpdate::BoundingBox = " << igp.bb << "\n";
389 controller().bbChanged = false;
390 if (igp.bb.empty()) {
391 string const fileWithAbsPath = MakeAbsPath(igp.filename, OnlyPath(igp.filename));
392 string bb = controller().readBB(fileWithAbsPath);
393 lyxerr << "file::BoundingBox = " << bb << "\n";
395 // get the values from the file
396 // in this case we always have the point-unit
397 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
398 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
399 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
400 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
402 } else { // get the values from the inset
403 controller().bbChanged = true;
405 anyLength = LyXLength(token(igp.bb,' ',0));
406 updateWidgetsFromLength(bbox_->input_bb_x0,
407 bbox_->choice_bb_x0,anyLength,"pt");
408 anyLength = LyXLength(token(igp.bb,' ',1));
409 updateWidgetsFromLength(bbox_->input_bb_y0,
410 bbox_->choice_bb_y0,anyLength,"pt");
411 anyLength = LyXLength(token(igp.bb,' ',2));
412 updateWidgetsFromLength(bbox_->input_bb_x1,
413 bbox_->choice_bb_x1,anyLength,"pt");
414 anyLength = LyXLength(token(igp.bb,' ',3));
415 updateWidgetsFromLength(bbox_->input_bb_y1,
416 bbox_->choice_bb_y1,anyLength,"pt");
419 // the special section
420 fl_set_input(special_->input_special, igp.special.c_str());
426 bool isValid(FL_OBJECT * ob)
428 string const input = getStringFromInput(ob);
429 return input.empty() || isValidLength(input) || isStrDbl(input);
436 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
439 if (ob == file_->button_browse) {
440 // Get the filename from the dialog
441 string const in_name = getStringFromInput(file_->input_filename);
442 string const out_name = controller().Browse(in_name);
443 if (out_name != in_name && !out_name.empty()) {
444 fl_set_input(file_->input_filename, out_name.c_str());
446 } else if (ob == file_->check_subcaption) {
447 setEnabled(file_->input_subcaption,
448 fl_get_button(file_->check_subcaption));
449 } else if (ob == file_->check_rotate) {
450 setEnabled(file_->input_rotate_angle,
451 fl_get_button(file_->check_rotate));
452 setEnabled(file_->choice_origin,
453 fl_get_button(file_->check_rotate));
455 // the lyxview section
456 } else if (ob == lyxview_->button_lyxasis) {
457 setEnabled(lyxview_->input_lyxwidth, 0);
458 setEnabled(lyxview_->choice_width_lyxwidth, 0);
459 setEnabled(lyxview_->input_lyxheight, 0);
460 setEnabled(lyxview_->choice_width_lyxheight, 0);
461 setEnabled(lyxview_->input_lyxscale, 0);
462 } else if (ob == lyxview_->button_lyxwh) {
463 setEnabled(lyxview_->input_lyxwidth, 1);
464 setEnabled(lyxview_->choice_width_lyxwidth, 1);
465 setEnabled(lyxview_->input_lyxheight, 1);
466 setEnabled(lyxview_->choice_width_lyxheight, 1);
467 setEnabled(lyxview_->input_lyxscale, 0);
468 } else if (ob == lyxview_->button_lyxscale) {
469 setEnabled(lyxview_->input_lyxwidth, 0);
470 setEnabled(lyxview_->choice_width_lyxwidth, 0);
471 setEnabled(lyxview_->input_lyxheight, 0);
472 setEnabled(lyxview_->choice_width_lyxheight, 0);
473 setEnabled(lyxview_->input_lyxscale, 1);
476 } else if (!controller().bbChanged &&
477 (ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
478 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1 ||
479 ob == bbox_->choice_bb_x0 || ob == bbox_->choice_bb_y0 ||
480 ob == bbox_->choice_bb_x1 || ob == bbox_->choice_bb_y1)) {
481 controller().bbChanged = true;
482 } else if (ob == bbox_->button_getBB) {
483 string const filename = getStringFromInput(file_->input_filename);
484 if (!filename.empty()) {
485 string const fileWithAbsPath = MakeAbsPath(filename, OnlyPath(filename));
486 string bb = controller().readBB(fileWithAbsPath);
487 lyxerr << "getBB::BoundingBox = " << bb << "\n";
489 updateWidgetsFromLengthString(bbox_->input_bb_x0,
491 token(bb,' ',0), "pt");
492 updateWidgetsFromLengthString(bbox_->input_bb_y0,
494 token(bb,' ',1), "pt");
495 updateWidgetsFromLengthString(bbox_->input_bb_x1,
497 token(bb,' ',2), "pt");
498 updateWidgetsFromLengthString(bbox_->input_bb_y1,
500 token(bb,' ',3), "pt");
502 controller().bbChanged = false;
506 } else if (ob == size_->button_asis) {
507 setEnabled(size_->input_width, 0);
508 setEnabled(size_->choice_width_units, 0);
509 setEnabled(size_->input_height, 0);
510 setEnabled(size_->choice_height_units, 0);
511 setEnabled(size_->check_aspectratio, 0);
512 setEnabled(size_->input_scale, 0);
513 } else if (ob == size_->button_wh) {
514 setEnabled(size_->input_width, 1);
515 setEnabled(size_->choice_width_units, 1);
516 setEnabled(size_->input_height, 1);
517 setEnabled(size_->choice_height_units, 1);
518 setEnabled(size_->check_aspectratio, 1);
519 setEnabled(size_->input_scale, 0);
520 } else if (ob == size_->button_scale) {
521 setEnabled(size_->input_width, 0);
522 setEnabled(size_->choice_width_units, 0);
523 setEnabled(size_->input_height, 0);
524 setEnabled(size_->choice_height_units, 0);
525 setEnabled(size_->check_aspectratio, 0);
526 setEnabled(size_->input_scale, 1);
529 // check if the input is valid
530 bool invalid = !isValid(bbox_->input_bb_x0);
531 invalid = invalid || !isValid(bbox_->input_bb_x1);
532 invalid = invalid || !isValid(bbox_->input_bb_y0);
533 invalid = invalid || !isValid(bbox_->input_bb_y1);
534 invalid = invalid || !isValid(size_->input_width);
535 invalid = invalid || !isValid(size_->input_height);
536 invalid = invalid || !isValid(lyxview_->input_lyxwidth);
537 invalid = invalid || !isValid(lyxview_->input_lyxheight);
539 // deactivate OK/ Apply buttons and
540 // spit out warnings if invalid
541 if (ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_x1 ||
542 ob == bbox_->input_bb_y0 || ob == bbox_->input_bb_y1 ||
543 ob == size_->input_width || ob == size_->input_height ||
544 ob == lyxview_->input_lyxwidth || ob == lyxview_->input_lyxheight) {
546 fl_set_object_label(dialog_->text_warning,
547 _("Warning: Invalid Length!"));
548 fl_show_object(dialog_->text_warning);
549 return ButtonPolicy::SMI_INVALID;
551 fl_hide_object(dialog_->text_warning);
555 return ButtonPolicy::SMI_VALID;