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/convert.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 = getString(file_->input_width);
334 if (igp.scale.empty()
335 || float_equal(strToDbl(igp.scale), 0.0, 0.05)
336 || float_equal(strToDbl(igp.scale), 100.0, 0.05)) {
337 igp.scale = string();
339 igp.width = LyXLength();
341 igp.scale = string();
342 igp.width = LyXLength(getLengthFromWidgets(file_->input_width,
343 file_->choice_width));
345 igp.height = LyXLength(getLengthFromWidgets(file_->input_height,
346 file_->choice_height));
347 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
349 igp.draft = fl_get_button(file_->check_draft);
350 igp.noUnzip = fl_get_button(file_->check_nounzip);
353 if (!controller().bbChanged) {
354 // don't write anything
357 // allow length + unit input only for x1 input field
359 if (!getString(bbox_->input_bb_x1).empty()) {
360 x1_str = getLengthFromWidgets(bbox_->input_bb_x1,
361 bbox_->choice_bb_units);
362 LyXLength x1 = LyXLength(x1_str);
363 x1_str = x1.asString();
380 fl_set_choice_text(bbox_->choice_bb_units, unit.c_str());
384 if (getString(bbox_->input_bb_x0).empty())
387 bb = getLengthFromWidgets(bbox_->input_bb_x0,
388 bbox_->choice_bb_units);
392 if (getString(bbox_->input_bb_y0).empty())
395 bb += getLengthFromWidgets(bbox_->input_bb_y0,
396 bbox_->choice_bb_units);
398 bb += ' ' + x1_str + ' ';
400 if (getString(bbox_->input_bb_y1).empty())
403 bb += getLengthFromWidgets(bbox_->input_bb_y1,
404 bbox_->choice_bb_units);
408 igp.clip = fl_get_button(bbox_->check_clip);
411 igp.rotateAngle = getString(extra_->input_rotate_angle);
413 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
414 float rotAngle = strToDbl(igp.rotateAngle);
415 if (std::abs(rotAngle) > 360.0) {
416 rotAngle -= 360.0 * floor(rotAngle / 360.0);
417 igp.rotateAngle = convert<string>(rotAngle);
420 fl_set_input(extra_->input_rotate_angle, igp.rotateAngle.c_str());
422 int const origin_pos = fl_get_choice(extra_->choice_origin);
423 if (origin_pos == 0) {
424 igp.rotateOrigin.erase();
426 igp.rotateOrigin = origins_[origin_pos - 1];
429 igp.subcaption = fl_get_button(extra_->check_subcaption);
430 igp.subcaptionText = getString(extra_->input_subcaption);
432 igp.special = getString(extra_->input_special);
436 void FormGraphics::update() {
437 // Update dialog with details from inset
438 InsetGraphicsParams & igp = controller().params();
442 igp.filename.outputFilename(kernel().bufferFilepath());
443 fl_set_input(file_->input_filename, name.c_str());
444 fl_set_input(file_->input_lyxscale, convert<string>(igp.lyxscale).c_str());
446 switch (igp.display) {
447 case graphics::NoDisplay:
448 fl_set_choice(file_->choice_display, 5);
450 case graphics::ColorDisplay:
451 fl_set_choice(file_->choice_display, 4);
453 case graphics::GrayscaleDisplay:
454 fl_set_choice(file_->choice_display, 3);
456 case graphics::MonochromeDisplay:
457 fl_set_choice(file_->choice_display, 2);
459 case graphics::DefaultDisplay:
460 fl_set_choice(file_->choice_display, 1);
463 // set width input fields according to scaling or width/height input
464 if (!igp.scale.empty() && !float_equal(strToDbl(igp.scale), 0.0, 0.05)) {
465 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
466 fl_set_input_maxchars(file_->input_width, 0);
467 fl_set_input(file_->input_width, igp.scale.c_str());
468 fl_set_choice(file_->choice_width, 1);
470 fl_set_input_filter(file_->input_width, NULL);
471 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
472 updateWidgetsFromLength(file_->input_width, file_->choice_width,
473 igp.width, defaultUnit);
476 updateWidgetsFromLength(file_->input_height, file_->choice_height,
477 igp.height, defaultUnit);
479 // disable height input in case of scaling
480 bool const disable_height = (!igp.scale.empty()
481 && !float_equal(strToDbl(igp.scale), 0.0, 0.05));
482 setEnabled(file_->input_height, !disable_height);
483 setEnabled(file_->choice_height, !disable_height);
485 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
486 fl_set_button(file_->check_draft, igp.draft);
487 fl_set_button(file_->check_nounzip, igp.noUnzip);
489 // disable aspectratio button in case of scaling or one of width/height
491 bool const disable_aspectRatio = disable_height ||
492 getString(file_->input_width).empty() ||
493 getString(file_->input_height).empty();
494 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
497 // set the bounding box values, if exists. First we need the whole
498 // path, because the controller knows nothing about the doc-dir
499 updateBB(igp.filename.absFilename(), igp.bb);
500 fl_set_button(bbox_->check_clip, igp.clip);
503 fl_set_input(extra_->input_rotate_angle, igp.rotateAngle.c_str());
506 if (igp.rotateOrigin.empty()) {
509 origin_pos = 1 + findPos(origins_, igp.rotateOrigin);
511 fl_set_choice(extra_->choice_origin, origin_pos);
513 fl_set_button(extra_->check_subcaption, igp.subcaption);
514 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
515 setEnabled(extra_->input_subcaption,
516 fl_get_button(extra_->check_subcaption));
517 fl_set_input(extra_->input_special, igp.special.c_str());
519 // open dialog in the file-tab, whenever filename is empty
520 if (igp.filename.empty()) {
521 fl_set_folder(dialog_->tabfolder, file_->form);
526 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
528 // Update dialog with details from inset
529 // set the bounding box values, if exists. First we need the whole
530 // path, because the controller knows nothing about the doc-dir
531 controller().bbChanged = false;
532 if (bb_inset.empty()) {
533 lyxerr[Debug::GRAPHICS]
534 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
535 string const bb = controller().readBB(filename);
537 // get the values from the file
538 // in this case we always have the point-unit
539 fl_set_input(bbox_->input_bb_x0,
540 token(bb,' ',0).c_str());
541 fl_set_input(bbox_->input_bb_y0,
542 token(bb,' ',1).c_str());
543 fl_set_input(bbox_->input_bb_x1,
544 token(bb,' ',2).c_str());
545 fl_set_input(bbox_->input_bb_y1,
546 token(bb,' ',3).c_str());
550 fl_set_input(bbox_->input_bb_x0, bb.c_str());
551 fl_set_input(bbox_->input_bb_y0, bb.c_str());
552 fl_set_input(bbox_->input_bb_x1, bb.c_str());
553 fl_set_input(bbox_->input_bb_y1, bb.c_str());
556 fl_set_choice(bbox_->choice_bb_units, 1);
559 // get the values from the inset
560 lyxerr[Debug::GRAPHICS]
561 << "FormGraphics::updateBB(): igp has BoundingBox"
562 << " ["<< bb_inset << "]" << endl;
563 controller().bbChanged = true;
566 anyLength = LyXLength(token(bb_inset,' ',0));
567 updateWidgetsFromLength(bbox_->input_bb_x0,
568 bbox_->choice_bb_units,anyLength,"bp");
569 anyLength = LyXLength(token(bb_inset,' ',1));
570 updateWidgetsFromLength(bbox_->input_bb_y0,
571 bbox_->choice_bb_units,anyLength,"bp");
572 anyLength = LyXLength(token(bb_inset,' ',2));
573 updateWidgetsFromLength(bbox_->input_bb_x1,
574 bbox_->choice_bb_units,anyLength,"bp");
575 anyLength = LyXLength(token(bb_inset,' ',3));
576 updateWidgetsFromLength(bbox_->input_bb_y1,
577 bbox_->choice_bb_units,anyLength,"bp");
582 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
584 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
587 if (ob == file_->button_browse) {
588 activate = ButtonPolicy::SMI_NOOP;
590 // Get the filename from the dialog
591 string const in_name = getString(file_->input_filename);
592 string const out_name = controller().browse(in_name);
593 lyxerr[Debug::GRAPHICS]
594 << "[FormGraphics]out_name: " << out_name << endl;
595 if (out_name != in_name && !out_name.empty()) {
596 fl_set_input(file_->input_filename, out_name.c_str());
598 if (controller().isFilenameValid(out_name) &&
599 !controller().bbChanged) {
600 updateBB(out_name, string());
602 } else if (ob == file_->input_width || ob == file_->input_height) {
603 // disable aspectratio button in case of scaling or one of
604 // width/height is empty
605 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
606 getString(file_->input_width).empty() ||
607 getString(file_->input_height).empty();
608 setEnabled(file_->check_aspectratio, !disable);
609 } else if (ob == file_->choice_width) {
610 // disable height input in case of scaling
611 bool const scaling = fl_get_choice(file_->choice_width) == 1;
612 setEnabled(file_->input_height, !scaling);
613 setEnabled(file_->choice_height, !scaling);
615 // allow only integer intput for scaling; float otherwise
617 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
618 fl_set_input_maxchars(file_->input_width, 0);
620 fl_set_input_filter(file_->input_width, NULL);
621 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
624 // disable aspectratio button in case of scaling or height
626 bool const disable_aspectratio =
627 scaling || getString(file_->input_height).empty();
628 setEnabled(file_->check_aspectratio, !disable_aspectratio);
630 } else if (!controller().bbChanged &&
631 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
632 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
633 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
634 controller().bbChanged = true;
635 } else if (ob == bbox_->button_getBB) {
636 string const filename = getString(file_->input_filename);
637 if (!filename.empty()) {
638 string bb = controller().readBB(filename);
640 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
641 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
642 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
643 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
644 fl_set_choice_text(bbox_->choice_bb_units, "bp");
646 controller().bbChanged = false;
648 fl_set_input(bbox_->input_bb_x0, "");
649 fl_set_input(bbox_->input_bb_y0, "");
650 fl_set_input(bbox_->input_bb_x1, "");
651 fl_set_input(bbox_->input_bb_y1, "");
652 fl_set_choice_text(bbox_->choice_bb_units, "bp");
655 } else if (ob == extra_->check_subcaption) {
656 setEnabled(extra_->input_subcaption,
657 fl_get_button(extra_->check_subcaption));
659 } else if (ob == file_->button_edit) {
660 activate = ButtonPolicy::SMI_NOOP;
661 controller().editGraphics();
668 } // namespace frontend