3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
10 * Full author contact details are available in file CREDITS.
15 #include "FormGraphics.h"
16 #include "ControlGraphics.h"
17 #include "forms/form_graphics.h"
19 #include "controllers/helper_funcs.h"
21 #include "checkedwidgets.h"
22 #include "input_validators.h"
24 #include "xforms_helpers.h"
27 #include "lyxrc.h" // for lyxrc.display_graphics
29 #include "insets/insetgraphicsParams.h"
31 #include "frontends/Alert.h"
33 #include "support/lyxlib.h" // for float_equal
34 #include "support/tostr.h"
36 #include "lyx_forms.h"
40 #ifndef CXX_GLOBAL_CSTD
51 using support::bformat;
52 using support::float_equal;
53 using support::getStringFromVector;
54 using support::strToDbl;
55 using support::strToInt;
62 // Bound the number of input characters
63 int const SIZE_MAXDIGITS = 10;
64 int const FILENAME_MAXCHARS = 1024;
66 string defaultUnit("cm");
68 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL < 2)
69 bool const scalableTabfolders = false;
71 bool const scalableTabfolders = true;
77 typedef FormController<ControlGraphics, FormView<FD_graphics> > base_class;
79 FormGraphics::FormGraphics(Dialog & parent)
80 : base_class(parent, _("Graphics"), scalableTabfolders)
84 void FormGraphics::redraw()
86 if (form() && form()->visible)
87 fl_redraw_form(form());
90 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
91 if (outer_form && outer_form->visible)
92 fl_redraw_form(outer_form);
96 void FormGraphics::build()
98 dialog_.reset(build_graphics(this));
100 // Manage the ok, apply, restore and cancel/close buttons
101 bcview().setOK(dialog_->button_ok);
102 bcview().setApply(dialog_->button_apply);
103 bcview().setCancel(dialog_->button_close);
104 bcview().setRestore(dialog_->button_restore);
107 file_.reset(build_graphics_file(this));
109 // Disable for read-only documents.
110 bcview().addReadOnly(file_->button_browse);
111 bcview().addReadOnly(file_->check_aspectratio);
112 bcview().addReadOnly(file_->check_draft);
113 bcview().addReadOnly(file_->check_nounzip);
115 // Check validity of "length + unit" input.
116 addCheckedGlueLength(bcview(), file_->input_width);
117 addCheckedGlueLength(bcview(), file_->input_height);
119 // Trigger an input event for cut&paste with middle mouse button.
120 setPrehandler(file_->input_filename);
121 setPrehandler(file_->input_lyxscale);
122 setPrehandler(file_->input_width);
123 setPrehandler(file_->input_height);
125 // Activate ok/apply immediately upon input.
126 fl_set_input_return(file_->input_filename, FL_RETURN_CHANGED);
127 fl_set_input_return(file_->input_lyxscale, FL_RETURN_CHANGED);
128 fl_set_input_return(file_->input_width, FL_RETURN_CHANGED);
129 fl_set_input_return(file_->input_height, FL_RETURN_CHANGED);
131 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
132 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
134 // Width default is scaling: use unsigned float filter.
135 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
136 fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
138 string const display_List =
139 _("Default|Monochrome|Grayscale|Color|Do not display");
140 fl_addto_choice(file_->choice_display, display_List.c_str());
142 string const height_list = buildChoiceLengthString();
143 string const width_list = bformat(_("Scale%%%%|%1$s"), height_list);
145 fl_addto_choice(file_->choice_width, width_list.c_str());
146 fl_addto_choice(file_->choice_height, height_list.c_str());
148 // set up the tooltips for the filesection
149 string str = _("The file you want to insert.");
150 tooltips().init(file_->input_filename, str);
151 str = _("Browse the directories.");
152 tooltips().init(file_->button_browse, str);
154 str = _("Scale the image to inserted percentage value.");
155 tooltips().init(file_->input_lyxscale, str);
156 str = _("Select display mode for this image.");
157 tooltips().init(file_->choice_display, str);
159 str = _("Set the image width to the inserted value.");
160 tooltips().init(file_->input_width, str);
161 // xgettext:no-c-format
162 str = _("Select unit for width; Scale% for scaling whole image.");
163 tooltips().init(file_->choice_width, str);
164 str = _("Set the image height to the inserted value.");
165 tooltips().init(file_->input_height, str);
166 str = _("Select unit for height.");
167 tooltips().init(file_->choice_height, str);
168 str = _("Do not distort the image. "
169 "Keep image within \"width\" by \"height\" and obey "
171 tooltips().init(file_->check_aspectratio, str);
172 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
173 "Useful when LaTeX should unzip the file. Needs an additional "
174 "file like \"file.eps.bb\" which holds the values for "
175 "the bounding box.");
176 tooltips().init(file_->check_nounzip, str);
177 str = _("Show image only as a rectangle of the original size.");
178 tooltips().init(file_->check_draft, str);
180 // the bounding box selection
181 bbox_.reset(build_graphics_bbox(this));
183 // disable for read-only documents
184 bcview().addReadOnly(bbox_->button_getBB);
185 bcview().addReadOnly(bbox_->check_clip);
187 // check validity of "length + unit" input
188 addCheckedLyXLength(bcview(), bbox_->input_bb_x1, bbox_->text_X);
190 // trigger an input event for cut&paste with middle mouse button.
191 setPrehandler(bbox_->input_bb_x0);
192 setPrehandler(bbox_->input_bb_y0);
193 setPrehandler(bbox_->input_bb_x1);
194 setPrehandler(bbox_->input_bb_y1);
196 // for activate ok/apply immediately upon input
197 fl_set_input_return(bbox_->input_bb_x0, FL_RETURN_CHANGED);
198 fl_set_input_return(bbox_->input_bb_y0, FL_RETURN_CHANGED);
199 fl_set_input_return(bbox_->input_bb_x1, FL_RETURN_CHANGED);
200 fl_set_input_return(bbox_->input_bb_y1, FL_RETURN_CHANGED);
202 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
203 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
204 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
206 string const bb_units = getStringFromVector(getBBUnits(), "|");
207 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
209 // set up the tooltips for the bounding-box-section
210 str = _("The lower left x-value of the bounding box.");
211 tooltips().init(bbox_->input_bb_x0, str);
212 str = _("The lower left y-value of the bounding box.");
213 tooltips().init(bbox_->input_bb_y0, str);
214 str = _("The upper right x-value of the bounding box; "
215 "only this input field allows length + unit, e.g. 5cm "
216 "and sets the unit for the other input fields.");
217 tooltips().init(bbox_->input_bb_x1, str);
218 str = _("The upper right y-value of the bounding box.");
219 tooltips().init(bbox_->input_bb_y1, str);
220 str = _("Select unit for the bounding box values.");
221 tooltips().init(bbox_->choice_bb_units, str);
223 str = _("Read the image coordinates new from file. For (e)ps-file "
224 "the bounding box is read, otherwise the imagesize in pixels. "
225 "Default unit is \"bp\", the PostScript's b(ig) p(oint).");
226 tooltips().init(bbox_->button_getBB, str);
228 str = _("Clip image to the bounding box values.");
229 tooltips().init(bbox_->check_clip, str);
232 extra_.reset(build_graphics_extra(this));
234 // disable for read-only documents
235 bcview().addReadOnly(extra_->input_rotate_angle);
236 bcview().addReadOnly(extra_->choice_origin);
237 bcview().addReadOnly(extra_->check_subcaption);
238 bcview().addReadOnly(extra_->input_special);
240 // trigger an input event for cut&paste with middle mouse button.
241 setPrehandler(extra_->input_rotate_angle);
242 setPrehandler(extra_->input_subcaption);
243 setPrehandler(extra_->input_special);
245 fl_set_input_return(extra_->input_rotate_angle, FL_RETURN_CHANGED);
246 fl_set_input_return(extra_->input_subcaption, FL_RETURN_CHANGED);
247 fl_set_input_return(extra_->input_special, FL_RETURN_CHANGED);
249 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
251 vector<RotationOriginPair> origindata = getRotationOriginData();
253 // Store the identifiers for later
254 origins_ = getSecond(origindata);
256 string const choice = getStringFromVector(getFirst(origindata), "|");
257 fl_addto_choice(extra_->choice_origin, choice.c_str());
259 // set up the tooltips for the extra section
260 str = _("Insert the rotation angle in degrees. "
261 "Positive value rotates anti-clockwise, "
262 "negative value clockwise.");
263 tooltips().init(extra_->input_rotate_angle, str);
264 str = _("Insert the point of origin for rotation.");
265 tooltips().init(extra_->choice_origin, str);
266 str = _("Enables use of subfigure with its own caption.");
267 tooltips().init(extra_->check_subcaption, str);
268 str = _("Insert the optional subfigure caption.");
269 tooltips().init(extra_->input_subcaption, str);
270 str = _("Add any additional LaTeX option, which is defined in the "
271 "graphicx-package and not mentioned in the gui's tabfolders.");
272 tooltips().init(extra_->input_special, str);
274 // Enable the tabfolder to be rescaled correctly.
275 if (scalableTabfolders)
276 fl_set_tabfolder_autofit(dialog_->tabfolder, FL_FIT);
279 fl_addto_tabfolder(dialog_->tabfolder, _("File").c_str(),
281 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box").c_str(),
283 fl_addto_tabfolder(dialog_->tabfolder, _("Extra").c_str(),
286 // set the right default unit
287 switch (lyxrc.default_papersize) {
288 case PAPER_DEFAULT: break;
290 case PAPER_LEGALPAPER:
291 case PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
295 case PAPER_B5PAPER: defaultUnit = "cm"; break;
300 void FormGraphics::apply()
302 // Create the parameters structure and fill the data from the dialog.
303 InsetGraphicsParams & igp = controller().params();
306 igp.filename.set(getString(file_->input_filename),
307 kernel().bufferFilepath());
309 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
310 if (igp.lyxscale == 0) {
314 switch (fl_get_choice(file_->choice_display)) {
316 igp.display = graphics::NoDisplay;
319 igp.display = graphics::ColorDisplay;
322 igp.display = graphics::GrayscaleDisplay;
325 igp.display = graphics::MonochromeDisplay;
328 igp.display = graphics::DefaultDisplay;
331 // first item in choice_width means scaling
332 if (fl_get_choice(file_->choice_width) == 1) {
333 igp.scale = strToDbl(getString(file_->input_width));
334 if (float_equal(igp.scale, 0.0, 0.05)) {
337 igp.width = LyXLength();
340 igp.width = LyXLength(getLengthFromWidgets(file_->input_width,
341 file_->choice_width));
343 igp.height = LyXLength(getLengthFromWidgets(file_->input_height,
344 file_->choice_height));
345 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
347 igp.draft = fl_get_button(file_->check_draft);
348 igp.noUnzip = fl_get_button(file_->check_nounzip);
351 if (!controller().bbChanged) {
352 // don't write anything
355 // allow length + unit input only for x1 input field
357 if (!getString(bbox_->input_bb_x1).empty()) {
358 x1_str = getLengthFromWidgets(bbox_->input_bb_x1,
359 bbox_->choice_bb_units);
360 LyXLength x1 = LyXLength(x1_str);
361 x1_str = x1.asString();
378 fl_set_choice_text(bbox_->choice_bb_units, unit.c_str());
382 if (getString(bbox_->input_bb_x0).empty())
385 bb = getLengthFromWidgets(bbox_->input_bb_x0,
386 bbox_->choice_bb_units);
390 if (getString(bbox_->input_bb_y0).empty())
393 bb += getLengthFromWidgets(bbox_->input_bb_y0,
394 bbox_->choice_bb_units);
396 bb += ' ' + x1_str + ' ';
398 if (getString(bbox_->input_bb_y1).empty())
401 bb += getLengthFromWidgets(bbox_->input_bb_y1,
402 bbox_->choice_bb_units);
406 igp.clip = fl_get_button(bbox_->check_clip);
409 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
411 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
412 if (std::abs(igp.rotateAngle) > 360.0)
413 igp.rotateAngle -= 360.0 * floor(igp.rotateAngle / 360.0);
415 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
417 int const origin_pos = fl_get_choice(extra_->choice_origin);
418 if (origin_pos == 0) {
419 igp.rotateOrigin.erase();
421 igp.rotateOrigin = origins_[origin_pos - 1];
424 igp.subcaption = fl_get_button(extra_->check_subcaption);
425 igp.subcaptionText = getString(extra_->input_subcaption);
427 igp.special = getString(extra_->input_special);
431 void FormGraphics::update() {
432 // Update dialog with details from inset
433 InsetGraphicsParams & igp = controller().params();
437 igp.filename.outputFilename(kernel().bufferFilepath());
438 fl_set_input(file_->input_filename, name.c_str());
439 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
441 switch (igp.display) {
442 case graphics::NoDisplay:
443 fl_set_choice(file_->choice_display, 5);
445 case graphics::ColorDisplay:
446 fl_set_choice(file_->choice_display, 4);
448 case graphics::GrayscaleDisplay:
449 fl_set_choice(file_->choice_display, 3);
451 case graphics::MonochromeDisplay:
452 fl_set_choice(file_->choice_display, 2);
454 case graphics::DefaultDisplay:
455 fl_set_choice(file_->choice_display, 1);
458 // set width input fields according to scaling or width/height input
459 if (!float_equal(igp.scale, 0.0, 0.05)) {
460 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
461 fl_set_input_maxchars(file_->input_width, 0);
462 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
463 fl_set_choice(file_->choice_width, 1);
465 fl_set_input_filter(file_->input_width, NULL);
466 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
467 updateWidgetsFromLength(file_->input_width, file_->choice_width,
468 igp.width, defaultUnit);
471 updateWidgetsFromLength(file_->input_height, file_->choice_height,
472 igp.height, defaultUnit);
474 // disable height input in case of scaling
475 bool const disable_height = !float_equal(igp.scale, 0.0, 0.05);
476 setEnabled(file_->input_height, !disable_height);
477 setEnabled(file_->choice_height, !disable_height);
479 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
480 fl_set_button(file_->check_draft, igp.draft);
481 fl_set_button(file_->check_nounzip, igp.noUnzip);
483 // disable aspectratio button in case of scaling or one of width/height
485 bool const disable_aspectRatio = disable_height ||
486 getString(file_->input_width).empty() ||
487 getString(file_->input_height).empty();
488 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
491 // set the bounding box values, if exists. First we need the whole
492 // path, because the controller knows nothing about the doc-dir
493 updateBB(igp.filename.absFilename(), igp.bb);
494 fl_set_button(bbox_->check_clip, igp.clip);
497 fl_set_input(extra_->input_rotate_angle,
498 tostr(igp.rotateAngle).c_str());
501 if (igp.rotateOrigin.empty()) {
504 origin_pos = 1 + findPos(origins_, igp.rotateOrigin);
506 fl_set_choice(extra_->choice_origin, origin_pos);
508 fl_set_button(extra_->check_subcaption, igp.subcaption);
509 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
510 setEnabled(extra_->input_subcaption,
511 fl_get_button(extra_->check_subcaption));
512 fl_set_input(extra_->input_special, igp.special.c_str());
514 // open dialog in the file-tab, whenever filename is empty
515 if (igp.filename.empty()) {
516 fl_set_folder(dialog_->tabfolder, file_->form);
521 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
523 // Update dialog with details from inset
524 // set the bounding box values, if exists. First we need the whole
525 // path, because the controller knows nothing about the doc-dir
526 controller().bbChanged = false;
527 if (bb_inset.empty()) {
528 lyxerr[Debug::GRAPHICS]
529 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
530 string const bb = controller().readBB(filename);
532 // get the values from the file
533 // in this case we always have the point-unit
534 fl_set_input(bbox_->input_bb_x0,
535 token(bb,' ',0).c_str());
536 fl_set_input(bbox_->input_bb_y0,
537 token(bb,' ',1).c_str());
538 fl_set_input(bbox_->input_bb_x1,
539 token(bb,' ',2).c_str());
540 fl_set_input(bbox_->input_bb_y1,
541 token(bb,' ',3).c_str());
545 fl_set_input(bbox_->input_bb_x0, bb.c_str());
546 fl_set_input(bbox_->input_bb_y0, bb.c_str());
547 fl_set_input(bbox_->input_bb_x1, bb.c_str());
548 fl_set_input(bbox_->input_bb_y1, bb.c_str());
551 fl_set_choice(bbox_->choice_bb_units, 1);
554 // get the values from the inset
555 lyxerr[Debug::GRAPHICS]
556 << "FormGraphics::updateBB(): igp has BoundingBox"
557 << " ["<< bb_inset << "]" << endl;
558 controller().bbChanged = true;
561 anyLength = LyXLength(token(bb_inset,' ',0));
562 updateWidgetsFromLength(bbox_->input_bb_x0,
563 bbox_->choice_bb_units,anyLength,"bp");
564 anyLength = LyXLength(token(bb_inset,' ',1));
565 updateWidgetsFromLength(bbox_->input_bb_y0,
566 bbox_->choice_bb_units,anyLength,"bp");
567 anyLength = LyXLength(token(bb_inset,' ',2));
568 updateWidgetsFromLength(bbox_->input_bb_x1,
569 bbox_->choice_bb_units,anyLength,"bp");
570 anyLength = LyXLength(token(bb_inset,' ',3));
571 updateWidgetsFromLength(bbox_->input_bb_y1,
572 bbox_->choice_bb_units,anyLength,"bp");
577 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
579 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
582 if (ob == file_->button_browse) {
583 activate = ButtonPolicy::SMI_NOOP;
585 // Get the filename from the dialog
586 string const in_name = getString(file_->input_filename);
587 string const out_name = controller().browse(in_name);
588 lyxerr[Debug::GRAPHICS]
589 << "[FormGraphics]out_name: " << out_name << endl;
590 if (out_name != in_name && !out_name.empty()) {
591 fl_set_input(file_->input_filename, out_name.c_str());
593 if (controller().isFilenameValid(out_name) &&
594 !controller().bbChanged) {
595 updateBB(out_name, string());
597 } else if (ob == file_->input_width || ob == file_->input_height) {
598 // disable aspectratio button in case of scaling or one of
599 // width/height is empty
600 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
601 getString(file_->input_width).empty() ||
602 getString(file_->input_height).empty();
603 setEnabled(file_->check_aspectratio, !disable);
604 } else if (ob == file_->choice_width) {
605 // disable height input in case of scaling
606 bool const scaling = fl_get_choice(file_->choice_width) == 1;
607 setEnabled(file_->input_height, !scaling);
608 setEnabled(file_->choice_height, !scaling);
610 // allow only integer intput for scaling; float otherwise
612 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
613 fl_set_input_maxchars(file_->input_width, 0);
615 fl_set_input_filter(file_->input_width, NULL);
616 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
619 // disable aspectratio button in case of scaling or height
621 bool const disable_aspectratio =
622 scaling || getString(file_->input_height).empty();
623 setEnabled(file_->check_aspectratio, !disable_aspectratio);
625 } else if (!controller().bbChanged &&
626 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
627 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
628 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
629 controller().bbChanged = true;
630 } else if (ob == bbox_->button_getBB) {
631 string const filename = getString(file_->input_filename);
632 if (!filename.empty()) {
633 string bb = controller().readBB(filename);
635 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
636 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
637 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
638 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
639 fl_set_choice_text(bbox_->choice_bb_units, "bp");
641 controller().bbChanged = false;
643 fl_set_input(bbox_->input_bb_x0, "");
644 fl_set_input(bbox_->input_bb_y0, "");
645 fl_set_input(bbox_->input_bb_x1, "");
646 fl_set_input(bbox_->input_bb_y1, "");
647 fl_set_choice_text(bbox_->choice_bb_units, "bp");
650 } else if (ob == extra_->check_subcaption) {
651 setEnabled(extra_->input_subcaption,
652 fl_get_button(extra_->check_subcaption));
654 } else if (ob == file_->button_edit) {
655 activate = ButtonPolicy::SMI_NOOP;
656 controller().editGraphics();
663 } // namespace frontend