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;
60 // Bound the number of input characters
61 int const SIZE_MAXDIGITS = 10;
62 int const FILENAME_MAXCHARS = 1024;
64 string defaultUnit("cm");
66 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL < 2)
67 bool const scalableTabfolders = false;
69 bool const scalableTabfolders = true;
75 typedef FormController<ControlGraphics, FormView<FD_graphics> > base_class;
77 FormGraphics::FormGraphics(Dialog & parent)
78 : base_class(parent, _("Graphics"), scalableTabfolders)
82 void FormGraphics::redraw()
84 if (form() && form()->visible)
85 fl_redraw_form(form());
88 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
89 if (outer_form && outer_form->visible)
90 fl_redraw_form(outer_form);
94 void FormGraphics::build()
96 dialog_.reset(build_graphics(this));
98 // Manage the ok, apply, restore and cancel/close buttons
99 bcview().setOK(dialog_->button_ok);
100 bcview().setApply(dialog_->button_apply);
101 bcview().setCancel(dialog_->button_close);
102 bcview().setRestore(dialog_->button_restore);
105 file_.reset(build_graphics_file(this));
107 // Disable for read-only documents.
108 bcview().addReadOnly(file_->button_browse);
109 bcview().addReadOnly(file_->check_aspectratio);
110 bcview().addReadOnly(file_->check_draft);
111 bcview().addReadOnly(file_->check_nounzip);
113 // Check validity of "length + unit" input.
114 addCheckedGlueLength(bcview(), file_->input_width);
115 addCheckedGlueLength(bcview(), file_->input_height);
117 // Trigger an input event for cut&paste with middle mouse button.
118 setPrehandler(file_->input_filename);
119 setPrehandler(file_->input_lyxscale);
120 setPrehandler(file_->input_width);
121 setPrehandler(file_->input_height);
123 // Activate ok/apply immediately upon input.
124 fl_set_input_return(file_->input_filename, FL_RETURN_CHANGED);
125 fl_set_input_return(file_->input_lyxscale, FL_RETURN_CHANGED);
126 fl_set_input_return(file_->input_width, FL_RETURN_CHANGED);
127 fl_set_input_return(file_->input_height, FL_RETURN_CHANGED);
129 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
130 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
132 // Width default is scaling: use unsigned float filter.
133 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
134 fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
136 string const display_List =
137 _("Default|Monochrome|Grayscale|Color|Do not display");
138 fl_addto_choice(file_->choice_display, display_List.c_str());
140 string const height_list = buildChoiceLengthString();
141 string const width_list = bformat(_("Scale%%%%|%1$s"), height_list);
143 fl_addto_choice(file_->choice_width, width_list.c_str());
144 fl_addto_choice(file_->choice_height, height_list.c_str());
146 // set up the tooltips for the filesection
147 string str = _("The file you want to insert.");
148 tooltips().init(file_->input_filename, str);
149 str = _("Browse the directories.");
150 tooltips().init(file_->button_browse, str);
152 str = _("Scale the image to inserted percentage value.");
153 tooltips().init(file_->input_lyxscale, str);
154 str = _("Select display mode for this image.");
155 tooltips().init(file_->choice_display, str);
157 str = _("Set the image width to the inserted value.");
158 tooltips().init(file_->input_width, str);
159 // xgettext:no-c-format
160 str = _("Select unit for width; Scale% for scaling whole image.");
161 tooltips().init(file_->choice_width, str);
162 str = _("Set the image height to the inserted value.");
163 tooltips().init(file_->input_height, str);
164 str = _("Select unit for height.");
165 tooltips().init(file_->choice_height, str);
166 str = _("Do not distort the image. "
167 "Keep image within \"width\" by \"height\" and obey "
169 tooltips().init(file_->check_aspectratio, str);
170 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
171 "Useful when LaTeX should unzip the file. Needs an additional "
172 "file like \"file.eps.bb\" which holds the values for "
173 "the bounding box.");
174 tooltips().init(file_->check_nounzip, str);
175 str = _("Show image only as a rectangle of the original size.");
176 tooltips().init(file_->check_draft, str);
178 // the bounding box selection
179 bbox_.reset(build_graphics_bbox(this));
181 // disable for read-only documents
182 bcview().addReadOnly(bbox_->button_getBB);
183 bcview().addReadOnly(bbox_->check_clip);
185 // check validity of "length + unit" input
186 addCheckedLyXLength(bcview(), bbox_->input_bb_x1, bbox_->text_X);
188 // trigger an input event for cut&paste with middle mouse button.
189 setPrehandler(bbox_->input_bb_x0);
190 setPrehandler(bbox_->input_bb_y0);
191 setPrehandler(bbox_->input_bb_x1);
192 setPrehandler(bbox_->input_bb_y1);
194 // for activate ok/apply immediately upon input
195 fl_set_input_return(bbox_->input_bb_x0, FL_RETURN_CHANGED);
196 fl_set_input_return(bbox_->input_bb_y0, FL_RETURN_CHANGED);
197 fl_set_input_return(bbox_->input_bb_x1, FL_RETURN_CHANGED);
198 fl_set_input_return(bbox_->input_bb_y1, FL_RETURN_CHANGED);
200 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
201 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
202 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
204 string const bb_units = getStringFromVector(getBBUnits(), "|");
205 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
207 // set up the tooltips for the bounding-box-section
208 str = _("The lower left x-value of the bounding box.");
209 tooltips().init(bbox_->input_bb_x0, str);
210 str = _("The lower left y-value of the bounding box.");
211 tooltips().init(bbox_->input_bb_y0, str);
212 str = _("The upper right x-value of the bounding box; "
213 "only this input field allows length + unit, e.g. 5cm "
214 "and sets the unit for the other input fields.");
215 tooltips().init(bbox_->input_bb_x1, str);
216 str = _("The upper right y-value of the bounding box.");
217 tooltips().init(bbox_->input_bb_y1, str);
218 str = _("Select unit for the bounding box values.");
219 tooltips().init(bbox_->choice_bb_units, str);
221 str = _("Read the image coordinates new from file. For (e)ps-file "
222 "the bounding box is read, otherwise the imagesize in pixels. "
223 "Default unit is \"bp\", the PostScript's b(ig) p(oint).");
224 tooltips().init(bbox_->button_getBB, str);
226 str = _("Clip image to the bounding box values.");
227 tooltips().init(bbox_->check_clip, str);
230 extra_.reset(build_graphics_extra(this));
232 // disable for read-only documents
233 bcview().addReadOnly(extra_->input_rotate_angle);
234 bcview().addReadOnly(extra_->choice_origin);
235 bcview().addReadOnly(extra_->check_subcaption);
236 bcview().addReadOnly(extra_->input_special);
238 // trigger an input event for cut&paste with middle mouse button.
239 setPrehandler(extra_->input_rotate_angle);
240 setPrehandler(extra_->input_subcaption);
241 setPrehandler(extra_->input_special);
243 fl_set_input_return(extra_->input_rotate_angle, FL_RETURN_CHANGED);
244 fl_set_input_return(extra_->input_subcaption, FL_RETURN_CHANGED);
245 fl_set_input_return(extra_->input_special, FL_RETURN_CHANGED);
247 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
249 vector<RotationOriginPair> origindata = getRotationOriginData();
251 // Store the identifiers for later
252 origins_ = getSecond(origindata);
254 string const choice = getStringFromVector(getFirst(origindata), "|");
255 fl_addto_choice(extra_->choice_origin, choice.c_str());
257 // set up the tooltips for the extra section
258 str = _("Insert the rotation angle in degrees. "
259 "Positive value rotates anti-clockwise, "
260 "negative value clockwise.");
261 tooltips().init(extra_->input_rotate_angle, str);
262 str = _("Insert the point of origin for rotation.");
263 tooltips().init(extra_->choice_origin, str);
264 str = _("Enables use of subfigure with its own caption.");
265 tooltips().init(extra_->check_subcaption, str);
266 str = _("Insert the optional subfigure caption.");
267 tooltips().init(extra_->input_subcaption, str);
268 str = _("Add any additional LaTeX option, which is defined in the "
269 "graphicx-package and not mentioned in the gui's tabfolders.");
270 tooltips().init(extra_->input_special, str);
272 // Enable the tabfolder to be rescaled correctly.
273 if (scalableTabfolders)
274 fl_set_tabfolder_autofit(dialog_->tabfolder, FL_FIT);
277 fl_addto_tabfolder(dialog_->tabfolder, _("File").c_str(),
279 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box").c_str(),
281 fl_addto_tabfolder(dialog_->tabfolder, _("Extra").c_str(),
284 // set the right default unit
285 switch (lyxrc.default_papersize) {
286 case PAPER_DEFAULT: break;
288 case PAPER_LEGALPAPER:
289 case PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
293 case PAPER_B5PAPER: defaultUnit = "cm"; break;
298 void FormGraphics::apply()
300 // Create the parameters structure and fill the data from the dialog.
301 InsetGraphicsParams & igp = controller().params();
304 igp.filename.set(getString(file_->input_filename),
305 kernel().bufferFilepath());
307 igp.lyxscale = convert<int>(getString(file_->input_lyxscale));
308 if (igp.lyxscale == 0) {
312 switch (fl_get_choice(file_->choice_display)) {
314 igp.display = graphics::NoDisplay;
317 igp.display = graphics::ColorDisplay;
320 igp.display = graphics::GrayscaleDisplay;
323 igp.display = graphics::MonochromeDisplay;
326 igp.display = graphics::DefaultDisplay;
329 // first item in choice_width means scaling
330 if (fl_get_choice(file_->choice_width) == 1) {
331 igp.scale = getString(file_->input_width);
332 if (igp.scale.empty()
333 || float_equal(convert<double>(igp.scale), 0.0, 0.05)
334 || float_equal(convert<double>(igp.scale), 100.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 = getString(extra_->input_rotate_angle);
411 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
412 double rotAngle = convert<double>(igp.rotateAngle);
413 if (std::abs(rotAngle) > 360.0) {
414 rotAngle -= 360.0 * floor(rotAngle / 360.0);
415 igp.rotateAngle = convert<string>(rotAngle);
418 fl_set_input(extra_->input_rotate_angle, igp.rotateAngle.c_str());
420 int const origin_pos = fl_get_choice(extra_->choice_origin);
421 if (origin_pos == 0) {
422 igp.rotateOrigin.erase();
424 igp.rotateOrigin = origins_[origin_pos - 1];
427 igp.subcaption = fl_get_button(extra_->check_subcaption);
428 igp.subcaptionText = getString(extra_->input_subcaption);
430 igp.special = getString(extra_->input_special);
434 void FormGraphics::update() {
435 // Update dialog with details from inset
436 InsetGraphicsParams & igp = controller().params();
440 igp.filename.outputFilename(kernel().bufferFilepath());
441 fl_set_input(file_->input_filename, name.c_str());
442 fl_set_input(file_->input_lyxscale, convert<string>(igp.lyxscale).c_str());
444 switch (igp.display) {
445 case graphics::NoDisplay:
446 fl_set_choice(file_->choice_display, 5);
448 case graphics::ColorDisplay:
449 fl_set_choice(file_->choice_display, 4);
451 case graphics::GrayscaleDisplay:
452 fl_set_choice(file_->choice_display, 3);
454 case graphics::MonochromeDisplay:
455 fl_set_choice(file_->choice_display, 2);
457 case graphics::DefaultDisplay:
458 fl_set_choice(file_->choice_display, 1);
461 // set width input fields according to scaling or width/height input
462 if (!igp.scale.empty() && !float_equal(convert<double>(igp.scale), 0.0, 0.05)) {
463 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
464 fl_set_input_maxchars(file_->input_width, 0);
465 fl_set_input(file_->input_width, igp.scale.c_str());
466 fl_set_choice(file_->choice_width, 1);
468 fl_set_input_filter(file_->input_width, NULL);
469 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
470 updateWidgetsFromLength(file_->input_width, file_->choice_width,
471 igp.width, defaultUnit);
474 updateWidgetsFromLength(file_->input_height, file_->choice_height,
475 igp.height, defaultUnit);
477 // disable height input in case of scaling
478 bool const disable_height = (!igp.scale.empty()
479 && !float_equal(convert<double>(igp.scale), 0.0, 0.05));
480 setEnabled(file_->input_height, !disable_height);
481 setEnabled(file_->choice_height, !disable_height);
483 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
484 fl_set_button(file_->check_draft, igp.draft);
485 fl_set_button(file_->check_nounzip, igp.noUnzip);
487 // disable aspectratio button in case of scaling or one of width/height
489 bool const disable_aspectRatio = disable_height ||
490 getString(file_->input_width).empty() ||
491 getString(file_->input_height).empty();
492 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
495 // set the bounding box values, if exists. First we need the whole
496 // path, because the controller knows nothing about the doc-dir
497 updateBB(igp.filename.absFilename(), igp.bb);
498 fl_set_button(bbox_->check_clip, igp.clip);
501 fl_set_input(extra_->input_rotate_angle, igp.rotateAngle.c_str());
504 if (!igp.rotateOrigin.empty()) {
505 origin_pos += findPos(origins_, igp.rotateOrigin);
507 fl_set_choice(extra_->choice_origin, origin_pos);
509 fl_set_button(extra_->check_subcaption, igp.subcaption);
510 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
511 setEnabled(extra_->input_subcaption,
512 fl_get_button(extra_->check_subcaption));
513 fl_set_input(extra_->input_special, igp.special.c_str());
515 // open dialog in the file-tab, whenever filename is empty
516 if (igp.filename.empty()) {
517 fl_set_folder(dialog_->tabfolder, file_->form);
522 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
524 // Update dialog with details from inset
525 // set the bounding box values, if exists. First we need the whole
526 // path, because the controller knows nothing about the doc-dir
527 controller().bbChanged = false;
528 if (bb_inset.empty()) {
529 lyxerr[Debug::GRAPHICS]
530 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
531 string const bb = controller().readBB(filename);
533 // get the values from the file
534 // in this case we always have the point-unit
535 fl_set_input(bbox_->input_bb_x0,
536 token(bb,' ',0).c_str());
537 fl_set_input(bbox_->input_bb_y0,
538 token(bb,' ',1).c_str());
539 fl_set_input(bbox_->input_bb_x1,
540 token(bb,' ',2).c_str());
541 fl_set_input(bbox_->input_bb_y1,
542 token(bb,' ',3).c_str());
546 fl_set_input(bbox_->input_bb_x0, bb.c_str());
547 fl_set_input(bbox_->input_bb_y0, bb.c_str());
548 fl_set_input(bbox_->input_bb_x1, bb.c_str());
549 fl_set_input(bbox_->input_bb_y1, bb.c_str());
552 fl_set_choice(bbox_->choice_bb_units, 1);
555 // get the values from the inset
556 lyxerr[Debug::GRAPHICS]
557 << "FormGraphics::updateBB(): igp has BoundingBox"
558 << " ["<< bb_inset << "]" << endl;
559 controller().bbChanged = true;
562 anyLength = LyXLength(token(bb_inset,' ',0));
563 updateWidgetsFromLength(bbox_->input_bb_x0,
564 bbox_->choice_bb_units,anyLength,"bp");
565 anyLength = LyXLength(token(bb_inset,' ',1));
566 updateWidgetsFromLength(bbox_->input_bb_y0,
567 bbox_->choice_bb_units,anyLength,"bp");
568 anyLength = LyXLength(token(bb_inset,' ',2));
569 updateWidgetsFromLength(bbox_->input_bb_x1,
570 bbox_->choice_bb_units,anyLength,"bp");
571 anyLength = LyXLength(token(bb_inset,' ',3));
572 updateWidgetsFromLength(bbox_->input_bb_y1,
573 bbox_->choice_bb_units,anyLength,"bp");
578 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
580 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
583 if (ob == file_->button_browse) {
584 activate = ButtonPolicy::SMI_NOOP;
586 // Get the filename from the dialog
587 string const in_name = getString(file_->input_filename);
588 string const out_name = controller().browse(in_name);
589 lyxerr[Debug::GRAPHICS]
590 << "[FormGraphics]out_name: " << out_name << endl;
591 if (out_name != in_name && !out_name.empty()) {
592 fl_set_input(file_->input_filename, out_name.c_str());
594 if (controller().isFilenameValid(out_name) &&
595 !controller().bbChanged) {
596 updateBB(out_name, string());
598 } else if (ob == file_->input_width || ob == file_->input_height) {
599 // disable aspectratio button in case of scaling or one of
600 // width/height is empty
601 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
602 getString(file_->input_width).empty() ||
603 getString(file_->input_height).empty();
604 setEnabled(file_->check_aspectratio, !disable);
605 } else if (ob == file_->choice_width) {
606 // disable height input in case of scaling
607 bool const scaling = fl_get_choice(file_->choice_width) == 1;
608 setEnabled(file_->input_height, !scaling);
609 setEnabled(file_->choice_height, !scaling);
611 // allow only integer intput for scaling; float otherwise
613 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
614 fl_set_input_maxchars(file_->input_width, 0);
616 fl_set_input_filter(file_->input_width, NULL);
617 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
620 // disable aspectratio button in case of scaling or height
622 bool const disable_aspectratio =
623 scaling || getString(file_->input_height).empty();
624 setEnabled(file_->check_aspectratio, !disable_aspectratio);
626 } else if (!controller().bbChanged &&
627 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
628 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
629 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
630 controller().bbChanged = true;
631 } else if (ob == bbox_->button_getBB) {
632 string const filename = getString(file_->input_filename);
633 if (!filename.empty()) {
634 string bb = controller().readBB(filename);
636 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
637 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
638 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
639 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
640 fl_set_choice_text(bbox_->choice_bb_units, "bp");
642 controller().bbChanged = false;
644 fl_set_input(bbox_->input_bb_x0, "");
645 fl_set_input(bbox_->input_bb_y0, "");
646 fl_set_input(bbox_->input_bb_x1, "");
647 fl_set_input(bbox_->input_bb_y1, "");
648 fl_set_choice_text(bbox_->choice_bb_units, "bp");
651 } else if (ob == extra_->check_subcaption) {
652 setEnabled(extra_->input_subcaption,
653 fl_get_button(extra_->check_subcaption));
655 } else if (ob == file_->button_edit) {
656 activate = ButtonPolicy::SMI_NOOP;
657 controller().editGraphics();
664 } // namespace frontend