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"
38 using lyx::support::bformat;
39 using lyx::support::float_equal;
40 using lyx::support::getStringFromVector;
41 using lyx::support::strToDbl;
42 using lyx::support::strToInt;
43 using lyx::support::token;
53 // Bound the number of input characters
54 int const SIZE_MAXDIGITS = 10;
55 int const FILENAME_MAXCHARS = 1024;
57 string defaultUnit("cm");
59 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL < 2)
60 bool const scalableTabfolders = false;
62 bool const scalableTabfolders = true;
68 typedef FormController<ControlGraphics, FormView<FD_graphics> > base_class;
70 FormGraphics::FormGraphics(Dialog & parent)
71 : base_class(parent, _("Graphics"), scalableTabfolders)
75 void FormGraphics::redraw()
77 if (form() && form()->visible)
78 fl_redraw_form(form());
81 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
82 if (outer_form && outer_form->visible)
83 fl_redraw_form(outer_form);
87 void FormGraphics::build()
89 dialog_.reset(build_graphics(this));
91 // Manage the ok, apply, restore and cancel/close buttons
92 bcview().setOK(dialog_->button_ok);
93 bcview().setApply(dialog_->button_apply);
94 bcview().setCancel(dialog_->button_close);
95 bcview().setRestore(dialog_->button_restore);
98 file_.reset(build_graphics_file(this));
100 // Disable for read-only documents.
101 bcview().addReadOnly(file_->button_browse);
102 bcview().addReadOnly(file_->check_aspectratio);
103 bcview().addReadOnly(file_->check_draft);
104 bcview().addReadOnly(file_->check_nounzip);
106 // Check validity of "length + unit" input.
107 addCheckedGlueLength(bcview(), file_->input_width);
108 addCheckedGlueLength(bcview(), file_->input_height);
110 // Trigger an input event for cut&paste with middle mouse button.
111 setPrehandler(file_->input_filename);
112 setPrehandler(file_->input_lyxscale);
113 setPrehandler(file_->input_width);
114 setPrehandler(file_->input_height);
116 // Activate ok/apply immediately upon input.
117 fl_set_input_return(file_->input_filename, FL_RETURN_CHANGED);
118 fl_set_input_return(file_->input_lyxscale, FL_RETURN_CHANGED);
119 fl_set_input_return(file_->input_width, FL_RETURN_CHANGED);
120 fl_set_input_return(file_->input_height, FL_RETURN_CHANGED);
122 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
123 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
125 // Width default is scaling: use unsigned float filter.
126 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
127 fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
129 string const display_List =
130 _("Default|Monochrome|Grayscale|Color|Do not display");
131 fl_addto_choice(file_->choice_display, display_List.c_str());
133 string const width_list = bformat(_("Scale%%%%|%1$s"), choice_Length_All);
134 fl_addto_choice(file_->choice_width, width_list.c_str());
136 fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
138 // set up the tooltips for the filesection
139 string str = _("The file you want to insert.");
140 tooltips().init(file_->input_filename, str);
141 str = _("Browse the directories.");
142 tooltips().init(file_->button_browse, str);
144 str = _("Scale the image to inserted percentage value.");
145 tooltips().init(file_->input_lyxscale, str);
146 str = _("Select display mode for this image.");
147 tooltips().init(file_->choice_display, str);
149 str = _("Set the image width to the inserted value.");
150 tooltips().init(file_->input_width, str);
151 // xgettext:no-c-format
152 str = _("Select unit for width; Scale% for scaling whole image.");
153 tooltips().init(file_->choice_width, str);
154 str = _("Set the image height to the inserted value.");
155 tooltips().init(file_->input_height, str);
156 str = _("Select unit for height.");
157 tooltips().init(file_->choice_height, str);
158 str = _("Do not distort the image. "
159 "Keep image within \"width\" by \"height\" and obey "
161 tooltips().init(file_->check_aspectratio, str);
162 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
163 "Useful when LaTeX should unzip the file. Needs an additional "
164 "file like \"file.eps.bb\" which holds the values for "
165 "the bounding box.");
166 tooltips().init(file_->check_nounzip, str);
167 str = _("Show image only as a rectangle of the original size.");
168 tooltips().init(file_->check_draft, str);
170 // the bounding box selection
171 bbox_.reset(build_graphics_bbox(this));
173 // disable for read-only documents
174 bcview().addReadOnly(bbox_->button_getBB);
175 bcview().addReadOnly(bbox_->check_clip);
177 // check validity of "length + unit" input
178 addCheckedLyXLength(bcview(), bbox_->input_bb_x1, bbox_->text_X);
180 // trigger an input event for cut&paste with middle mouse button.
181 setPrehandler(bbox_->input_bb_x0);
182 setPrehandler(bbox_->input_bb_y0);
183 setPrehandler(bbox_->input_bb_x1);
184 setPrehandler(bbox_->input_bb_y1);
186 // for activate ok/apply immediately upon input
187 fl_set_input_return(bbox_->input_bb_x0, FL_RETURN_CHANGED);
188 fl_set_input_return(bbox_->input_bb_y0, FL_RETURN_CHANGED);
189 fl_set_input_return(bbox_->input_bb_x1, FL_RETURN_CHANGED);
190 fl_set_input_return(bbox_->input_bb_y1, FL_RETURN_CHANGED);
192 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
193 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
194 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
196 string const bb_units = getStringFromVector(frnt::getBBUnits(), "|");
197 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
199 // set up the tooltips for the bounding-box-section
200 str = _("The lower left x-value of the bounding box.");
201 tooltips().init(bbox_->input_bb_x0, str);
202 str = _("The lower left y-value of the bounding box.");
203 tooltips().init(bbox_->input_bb_y0, str);
204 str = _("The upper right x-value of the bounding box; "
205 "only this input field allows length + unit, e.g. 5cm "
206 "and sets the unit for the other input fields.");
207 tooltips().init(bbox_->input_bb_x1, str);
208 str = _("The upper right y-value of the bounding box.");
209 tooltips().init(bbox_->input_bb_y1, str);
210 str = _("Select unit for the bounding box values.");
211 tooltips().init(bbox_->choice_bb_units, str);
213 str = _("Read the image coordinates new from file. For (e)ps-file "
214 "the bounding box is read, otherwise the imagesize in pixels. "
215 "Default unit is \"bp\", the PostScript's b(ig) p(oint).");
216 tooltips().init(bbox_->button_getBB, str);
218 str = _("Clip image to the bounding box values.");
219 tooltips().init(bbox_->check_clip, str);
222 extra_.reset(build_graphics_extra(this));
224 // disable for read-only documents
225 bcview().addReadOnly(extra_->input_rotate_angle);
226 bcview().addReadOnly(extra_->choice_origin);
227 bcview().addReadOnly(extra_->check_subcaption);
228 bcview().addReadOnly(extra_->input_special);
230 // trigger an input event for cut&paste with middle mouse button.
231 setPrehandler(extra_->input_rotate_angle);
232 setPrehandler(extra_->input_subcaption);
233 setPrehandler(extra_->input_special);
235 fl_set_input_return(extra_->input_rotate_angle, FL_RETURN_CHANGED);
236 fl_set_input_return(extra_->input_subcaption, FL_RETURN_CHANGED);
237 fl_set_input_return(extra_->input_special, FL_RETURN_CHANGED);
239 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
241 using namespace frnt;
242 vector<RotationOriginPair> origindata = getRotationOriginData();
244 // Store the identifiers for later
245 origins_ = getSecond(origindata);
247 string const choice = getStringFromVector(getFirst(origindata), "|");
248 fl_addto_choice(extra_->choice_origin, choice.c_str());
250 // set up the tooltips for the extra section
251 str = _("Insert the rotation angle in degrees. "
252 "Positive value rotates anti-clockwise, "
253 "negative value clockwise.");
254 tooltips().init(extra_->input_rotate_angle, str);
255 str = _("Insert the point of origin for rotation.");
256 tooltips().init(extra_->choice_origin, str);
257 str = _("Enables use of subfigure with its own caption.");
258 tooltips().init(extra_->check_subcaption, str);
259 str = _("Insert the optional subfigure caption.");
260 tooltips().init(extra_->input_subcaption, str);
261 str = _("Add any additional LaTeX option, which is defined in the "
262 "graphicx-package and not mentioned in the gui's tabfolders.");
263 tooltips().init(extra_->input_special, str);
265 // Enable the tabfolder to be rescaled correctly.
266 if (scalableTabfolders)
267 fl_set_tabfolder_autofit(dialog_->tabfolder, FL_FIT);
270 fl_addto_tabfolder(dialog_->tabfolder, _("File").c_str(),
272 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box").c_str(),
274 fl_addto_tabfolder(dialog_->tabfolder, _("Extra").c_str(),
277 // set the right default unit
278 switch (lyxrc.default_papersize) {
279 case PAPER_DEFAULT: break;
281 case PAPER_LEGALPAPER:
282 case PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
286 case PAPER_B5PAPER: defaultUnit = "cm"; break;
291 void FormGraphics::apply()
293 // Create the parameters structure and fill the data from the dialog.
294 InsetGraphicsParams & igp = controller().params();
297 igp.filename.set(getString(file_->input_filename),
298 kernel().bufferFilepath());
300 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
301 if (igp.lyxscale == 0) {
305 switch (fl_get_choice(file_->choice_display)) {
307 igp.display = lyx::graphics::NoDisplay;
310 igp.display = lyx::graphics::ColorDisplay;
313 igp.display = lyx::graphics::GrayscaleDisplay;
316 igp.display = lyx::graphics::MonochromeDisplay;
319 igp.display = lyx::graphics::DefaultDisplay;
322 // first item in choice_width means scaling
323 if (fl_get_choice(file_->choice_width) == 1) {
324 igp.scale = strToDbl(getString(file_->input_width));
325 if (float_equal(igp.scale, 0.0, 0.05)) {
328 igp.width = LyXLength();
331 igp.width = LyXLength(getLengthFromWidgets(file_->input_width,
332 file_->choice_width));
334 igp.height = LyXLength(getLengthFromWidgets(file_->input_height,
335 file_->choice_height));
336 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
338 igp.draft = fl_get_button(file_->check_draft);
339 igp.noUnzip = fl_get_button(file_->check_nounzip);
342 if (!controller().bbChanged) {
343 // don't write anything
346 // allow length + unit input only for x1 input field
348 if (!getString(bbox_->input_bb_x1).empty()) {
349 x1_str = getLengthFromWidgets(bbox_->input_bb_x1,
350 bbox_->choice_bb_units);
351 LyXLength x1 = LyXLength(x1_str);
352 x1_str = x1.asString();
369 fl_set_choice_text(bbox_->choice_bb_units, unit.c_str());
373 if (getString(bbox_->input_bb_x0).empty())
376 bb = getLengthFromWidgets(bbox_->input_bb_x0,
377 bbox_->choice_bb_units);
381 if (getString(bbox_->input_bb_y0).empty())
384 bb += getLengthFromWidgets(bbox_->input_bb_y0,
385 bbox_->choice_bb_units);
387 bb += ' ' + x1_str + ' ';
389 if (getString(bbox_->input_bb_y1).empty())
392 bb += getLengthFromWidgets(bbox_->input_bb_y1,
393 bbox_->choice_bb_units);
397 igp.clip = fl_get_button(bbox_->check_clip);
400 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
402 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
403 while (igp.rotateAngle <= -360.0) {
404 igp.rotateAngle += 360.0;
406 while (igp.rotateAngle >= 360.0) {
407 igp.rotateAngle -= 360.0;
409 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
411 int const origin_pos = fl_get_choice(extra_->choice_origin);
412 if (origin_pos == 0) {
413 igp.rotateOrigin.erase();
415 igp.rotateOrigin = origins_[origin_pos - 1];
418 igp.subcaption = fl_get_button(extra_->check_subcaption);
419 igp.subcaptionText = getString(extra_->input_subcaption);
421 igp.special = getString(extra_->input_special);
425 void FormGraphics::update() {
426 // Update dialog with details from inset
427 InsetGraphicsParams & igp = controller().params();
431 igp.filename.outputFilename(kernel().bufferFilepath());
432 fl_set_input(file_->input_filename, name.c_str());
433 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
435 switch (igp.display) {
436 case lyx::graphics::NoDisplay:
437 fl_set_choice(file_->choice_display, 5);
439 case lyx::graphics::ColorDisplay:
440 fl_set_choice(file_->choice_display, 4);
442 case lyx::graphics::GrayscaleDisplay:
443 fl_set_choice(file_->choice_display, 3);
445 case lyx::graphics::MonochromeDisplay:
446 fl_set_choice(file_->choice_display, 2);
448 case lyx::graphics::DefaultDisplay:
449 fl_set_choice(file_->choice_display, 1);
452 // set width input fields according to scaling or width/height input
453 if (!float_equal(igp.scale, 0.0, 0.05)) {
454 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
455 fl_set_input_maxchars(file_->input_width, 0);
456 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
457 fl_set_choice(file_->choice_width, 1);
459 fl_set_input_filter(file_->input_width, NULL);
460 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
461 updateWidgetsFromLength(file_->input_width, file_->choice_width,
462 igp.width, defaultUnit);
465 updateWidgetsFromLength(file_->input_height, file_->choice_height,
466 igp.height, defaultUnit);
468 // disable height input in case of scaling
469 bool const disable_height = !float_equal(igp.scale, 0.0, 0.05);
470 setEnabled(file_->input_height, !disable_height);
471 setEnabled(file_->choice_height, !disable_height);
473 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
474 fl_set_button(file_->check_draft, igp.draft);
475 fl_set_button(file_->check_nounzip, igp.noUnzip);
477 // disable aspectratio button in case of scaling or one of width/height
479 bool const disable_aspectRatio = disable_height ||
480 getString(file_->input_width).empty() ||
481 getString(file_->input_height).empty();
482 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
485 // set the bounding box values, if exists. First we need the whole
486 // path, because the controller knows nothing about the doc-dir
487 updateBB(igp.filename.absFilename(), igp.bb);
488 fl_set_button(bbox_->check_clip, igp.clip);
491 fl_set_input(extra_->input_rotate_angle,
492 tostr(igp.rotateAngle).c_str());
495 if (igp.rotateOrigin.empty()) {
498 origin_pos = 1 + findPos(origins_, igp.rotateOrigin);
500 fl_set_choice(extra_->choice_origin, origin_pos);
502 fl_set_button(extra_->check_subcaption, igp.subcaption);
503 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
504 setEnabled(extra_->input_subcaption,
505 fl_get_button(extra_->check_subcaption));
506 fl_set_input(extra_->input_special, igp.special.c_str());
508 // open dialog in the file-tab, whenever filename is empty
509 if (igp.filename.empty()) {
510 fl_set_folder(dialog_->tabfolder, file_->form);
515 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
517 // Update dialog with details from inset
518 // set the bounding box values, if exists. First we need the whole
519 // path, because the controller knows nothing about the doc-dir
520 controller().bbChanged = false;
521 if (bb_inset.empty()) {
522 lyxerr[Debug::GRAPHICS]
523 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
524 string const bb = controller().readBB(filename);
526 // get the values from the file
527 // in this case we always have the point-unit
528 fl_set_input(bbox_->input_bb_x0,
529 token(bb,' ',0).c_str());
530 fl_set_input(bbox_->input_bb_y0,
531 token(bb,' ',1).c_str());
532 fl_set_input(bbox_->input_bb_x1,
533 token(bb,' ',2).c_str());
534 fl_set_input(bbox_->input_bb_y1,
535 token(bb,' ',3).c_str());
539 fl_set_input(bbox_->input_bb_x0, bb.c_str());
540 fl_set_input(bbox_->input_bb_y0, bb.c_str());
541 fl_set_input(bbox_->input_bb_x1, bb.c_str());
542 fl_set_input(bbox_->input_bb_y1, bb.c_str());
545 fl_set_choice(bbox_->choice_bb_units, 1);
548 // get the values from the inset
549 lyxerr[Debug::GRAPHICS]
550 << "FormGraphics::updateBB(): igp has BoundingBox"
551 << " ["<< bb_inset << "]" << endl;
552 controller().bbChanged = true;
555 anyLength = LyXLength(token(bb_inset,' ',0));
556 updateWidgetsFromLength(bbox_->input_bb_x0,
557 bbox_->choice_bb_units,anyLength,"bp");
558 anyLength = LyXLength(token(bb_inset,' ',1));
559 updateWidgetsFromLength(bbox_->input_bb_y0,
560 bbox_->choice_bb_units,anyLength,"bp");
561 anyLength = LyXLength(token(bb_inset,' ',2));
562 updateWidgetsFromLength(bbox_->input_bb_x1,
563 bbox_->choice_bb_units,anyLength,"bp");
564 anyLength = LyXLength(token(bb_inset,' ',3));
565 updateWidgetsFromLength(bbox_->input_bb_y1,
566 bbox_->choice_bb_units,anyLength,"bp");
571 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
574 if (ob == file_->button_browse) {
575 // Get the filename from the dialog
576 string const in_name = getString(file_->input_filename);
577 string const out_name = controller().browse(in_name);
578 lyxerr[Debug::GRAPHICS]
579 << "[FormGraphics]out_name: " << out_name << endl;
580 if (out_name != in_name && !out_name.empty()) {
581 fl_set_input(file_->input_filename, out_name.c_str());
583 if (controller().isFilenameValid(out_name) &&
584 !controller().bbChanged) {
585 updateBB(out_name, string());
587 } else if (ob == file_->input_width || ob == file_->input_height) {
588 // disable aspectratio button in case of scaling or one of
589 // width/height is empty
590 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
591 getString(file_->input_width).empty() ||
592 getString(file_->input_height).empty();
593 setEnabled(file_->check_aspectratio, !disable);
594 } else if (ob == file_->choice_width) {
595 // disable height input in case of scaling
596 bool const scaling = fl_get_choice(file_->choice_width) == 1;
597 setEnabled(file_->input_height, !scaling);
598 setEnabled(file_->choice_height, !scaling);
600 // allow only integer intput for scaling; float otherwise
602 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
603 fl_set_input_maxchars(file_->input_width, 0);
605 fl_set_input_filter(file_->input_width, NULL);
606 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
609 // disable aspectratio button in case of scaling or height
611 bool const disable_aspectratio =
612 scaling || getString(file_->input_height).empty();
613 setEnabled(file_->check_aspectratio, !disable_aspectratio);
615 } else if (!controller().bbChanged &&
616 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
617 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
618 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
619 controller().bbChanged = true;
620 } else if (ob == bbox_->button_getBB) {
621 string const filename = getString(file_->input_filename);
622 if (!filename.empty()) {
623 string bb = controller().readBB(filename);
625 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
626 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
627 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
628 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
629 fl_set_choice_text(bbox_->choice_bb_units, "bp");
631 controller().bbChanged = false;
633 fl_set_input(bbox_->input_bb_x0, "");
634 fl_set_input(bbox_->input_bb_y0, "");
635 fl_set_input(bbox_->input_bb_x1, "");
636 fl_set_input(bbox_->input_bb_y1, "");
637 fl_set_choice_text(bbox_->choice_bb_units, "bp");
640 } else if (ob == extra_->check_subcaption) {
641 setEnabled(extra_->input_subcaption,
642 fl_get_button(extra_->check_subcaption));
646 return ButtonPolicy::SMI_VALID;