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
17 #include "ControlGraphics.h"
18 #include "FormGraphics.h"
19 #include "forms/form_graphics.h"
21 #include "checkedwidgets.h"
22 #include "input_validators.h"
24 #include "xforms_helpers.h"
26 #include "debug.h" // for lyxerr
27 #include "lyxrc.h" // for lyxrc.display_graphics
29 #include "insets/insetgraphicsParams.h"
31 #include "controllers/helper_funcs.h" // for getStringFromVector
33 #include "frontends/Alert.h"
35 #include "support/lstrings.h" // for strToDbl & tostr
36 #include "support/lyxlib.h" // for float_equal
37 #include "support/filetools.h" // for MakeAbsPath etc
39 #include "support/BoostFormat.h"
41 #include FORMS_H_LOCATION
49 // Bound the number of input characters
50 int const SIZE_MAXDIGITS = 10;
51 int const FILENAME_MAXCHARS = 1024;
53 string defaultUnit("cm");
55 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL < 2)
56 bool const scalableTabfolders = false;
58 bool const scalableTabfolders = true;
64 typedef FormController<ControlGraphics, FormView<FD_graphics> > base_class;
66 FormGraphics::FormGraphics(Dialog & parent)
67 : base_class(parent, _("Graphics"), scalableTabfolders)
71 void FormGraphics::redraw()
73 if (form() && form()->visible)
74 fl_redraw_form(form());
77 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
78 if (outer_form && outer_form->visible)
79 fl_redraw_form(outer_form);
83 void FormGraphics::build()
85 dialog_.reset(build_graphics(this));
87 // Manage the ok, apply, restore and cancel/close buttons
88 bcview().setOK(dialog_->button_ok);
89 bcview().setApply(dialog_->button_apply);
90 bcview().setCancel(dialog_->button_close);
91 bcview().setRestore(dialog_->button_restore);
94 file_.reset(build_graphics_file(this));
96 // disable for read-only documents
97 bcview().addReadOnly(file_->button_browse);
98 bcview().addReadOnly(file_->check_aspectratio);
99 bcview().addReadOnly(file_->check_draft);
100 bcview().addReadOnly(file_->check_nounzip);
102 // check validity of "length + unit" input
103 addCheckedGlueLength(bcview(), file_->input_width);
104 addCheckedGlueLength(bcview(), file_->input_height);
106 // trigger an input event for cut&paste with middle mouse button.
107 setPrehandler(file_->input_filename);
108 setPrehandler(file_->input_lyxscale);
109 setPrehandler(file_->input_width);
110 setPrehandler(file_->input_height);
112 // for activate ok/apply immediately upon input
113 fl_set_input_return(file_->input_filename, FL_RETURN_CHANGED);
114 fl_set_input_return(file_->input_lyxscale, FL_RETURN_CHANGED);
115 fl_set_input_return(file_->input_width, FL_RETURN_CHANGED);
116 fl_set_input_return(file_->input_height, FL_RETURN_CHANGED);
118 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
119 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
121 // width default is scaling: use unsigned float filter
122 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
123 fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
125 string const display_List =
126 _("Default|Monochrome|Grayscale|Color|Do not display");
127 fl_addto_choice(file_->choice_display, display_List.c_str());
129 string const width_list = bformat(_("Scale%%%%|%1$s"), choice_Length_All);
130 fl_addto_choice(file_->choice_width, width_list.c_str());
132 fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
134 // set up the tooltips for the filesection
135 string str = _("The file you want to insert.");
136 tooltips().init(file_->input_filename, str);
137 str = _("Browse the directories.");
138 tooltips().init(file_->button_browse, str);
140 str = _("Scale the image to inserted percentage value.");
141 tooltips().init(file_->input_lyxscale, str);
142 str = _("Select display mode for this image.");
143 tooltips().init(file_->choice_display, str);
145 str = _("Set the image width to the inserted value.");
146 tooltips().init(file_->input_width, str);
147 // xgettext:no-c-format
148 str = _("Select unit for width; Scale% for scaling whole image.");
149 tooltips().init(file_->choice_width, str);
150 str = _("Set the image height to the inserted value.");
151 tooltips().init(file_->input_height, str);
152 str = _("Select unit for height.");
153 tooltips().init(file_->choice_height, str);
154 str = _("Do not distort the image. "
155 "Keep image within \"width\" by \"height\" and obey "
157 tooltips().init(file_->check_aspectratio, str);
158 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
159 "Useful when LaTeX should unzip the file. Needs an additional "
160 "file like \"file.eps.bb\" which holds the values for "
161 "the bounding box.");
162 tooltips().init(file_->check_nounzip, str);
163 str = _("Show image only as a rectangle of the original size.");
164 tooltips().init(file_->check_draft, str);
166 // the bounding box selection
167 bbox_.reset(build_graphics_bbox(this));
169 // disable for read-only documents
170 bcview().addReadOnly(bbox_->button_getBB);
171 bcview().addReadOnly(bbox_->check_clip);
173 // check validity of "length + unit" input
174 addCheckedLyXLength(bcview(), bbox_->input_bb_x1, bbox_->text_X);
176 // trigger an input event for cut&paste with middle mouse button.
177 setPrehandler(bbox_->input_bb_x0);
178 setPrehandler(bbox_->input_bb_y0);
179 setPrehandler(bbox_->input_bb_x1);
180 setPrehandler(bbox_->input_bb_y1);
182 // for activate ok/apply immediately upon input
183 fl_set_input_return(bbox_->input_bb_x0, FL_RETURN_CHANGED);
184 fl_set_input_return(bbox_->input_bb_y0, FL_RETURN_CHANGED);
185 fl_set_input_return(bbox_->input_bb_x1, FL_RETURN_CHANGED);
186 fl_set_input_return(bbox_->input_bb_y1, FL_RETURN_CHANGED);
188 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
189 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
190 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
192 string const bb_units = getStringFromVector(frnt::getBBUnits(), "|");
193 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
195 // set up the tooltips for the bounding-box-section
196 str = _("The lower left x-value of the bounding box.");
197 tooltips().init(bbox_->input_bb_x0, str);
198 str = _("The lower left y-value of the bounding box.");
199 tooltips().init(bbox_->input_bb_y0, str);
200 str = _("The upper right x-value of the bounding box; "
201 "only this input field allows length + unit, e.g. 5cm "
202 "and sets the unit for the other input fields.");
203 tooltips().init(bbox_->input_bb_x1, str);
204 str = _("The upper right y-value of the bounding box.");
205 tooltips().init(bbox_->input_bb_y1, str);
206 str = _("Select unit for the bounding box values.");
207 tooltips().init(bbox_->choice_bb_units, str);
209 str = _("Read the image coordinates new from file. For (e)ps-file "
210 "the bounding box is read, otherwise the imagesize in pixels. "
211 "Default unit is \"bp\", the PostScript's b(ig) p(oint).");
212 tooltips().init(bbox_->button_getBB, str);
214 str = _("Clip image to the bounding box values.");
215 tooltips().init(bbox_->check_clip, str);
218 extra_.reset(build_graphics_extra(this));
220 // disable for read-only documents
221 bcview().addReadOnly(extra_->input_rotate_angle);
222 bcview().addReadOnly(extra_->choice_origin);
223 bcview().addReadOnly(extra_->check_subcaption);
224 bcview().addReadOnly(extra_->input_special);
226 // trigger an input event for cut&paste with middle mouse button.
227 setPrehandler(extra_->input_rotate_angle);
228 setPrehandler(extra_->input_subcaption);
229 setPrehandler(extra_->input_special);
231 fl_set_input_return(extra_->input_rotate_angle, FL_RETURN_CHANGED);
232 fl_set_input_return(extra_->input_subcaption, FL_RETURN_CHANGED);
233 fl_set_input_return(extra_->input_special, FL_RETURN_CHANGED);
235 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
237 using namespace frnt;
238 vector<RotationOriginPair> origindata = getRotationOriginData();
240 // Store the identifiers for later
241 origins_ = getSecond(origindata);
243 string const choice = getStringFromVector(getFirst(origindata), "|");
244 fl_addto_choice(extra_->choice_origin, choice.c_str());
246 // set up the tooltips for the extra section
247 str = _("Insert the rotation angle in degrees. "
248 "Positive value rotates anti-clockwise, "
249 "negative value clockwise.");
250 tooltips().init(extra_->input_rotate_angle, str);
251 str = _("Insert the point of origin for rotation.");
252 tooltips().init(extra_->choice_origin, str);
253 str = _("Enables use of subfigure with its own caption.");
254 tooltips().init(extra_->check_subcaption, str);
255 str = _("Insert the optional subfigure caption.");
256 tooltips().init(extra_->input_subcaption, str);
257 str = _("Add any additional LaTeX option, which is defined in the "
258 "graphicx-package and not mentioned in the gui's tabfolders.");
259 tooltips().init(extra_->input_special, str);
261 // Enable the tabfolder to be rescaled correctly.
262 if (scalableTabfolders)
263 fl_set_tabfolder_autofit(dialog_->tabfolder, FL_FIT);
266 fl_addto_tabfolder(dialog_->tabfolder, _("File"), file_->form);
267 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box"), bbox_->form);
268 fl_addto_tabfolder(dialog_->tabfolder, _("Extra"), extra_->form);
270 // set the right default unit
271 switch (lyxrc.default_papersize) {
272 case BufferParams::PAPER_DEFAULT: break;
273 case BufferParams::PAPER_USLETTER:
274 case BufferParams::PAPER_LEGALPAPER:
275 case BufferParams::PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
276 case BufferParams::PAPER_A3PAPER:
277 case BufferParams::PAPER_A4PAPER:
278 case BufferParams::PAPER_A5PAPER:
279 case BufferParams::PAPER_B5PAPER: defaultUnit = "cm"; break;
284 void FormGraphics::apply()
286 // Create the parameters structure and fill the data from the dialog.
287 InsetGraphicsParams & igp = controller().params();
290 igp.filename = getString(file_->input_filename);
292 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
293 if (igp.lyxscale == 0) {
297 switch (fl_get_choice(file_->choice_display)) {
299 igp.display = grfx::NoDisplay;
302 igp.display = grfx::ColorDisplay;
305 igp.display = grfx::GrayscaleDisplay;
308 igp.display = grfx::MonochromeDisplay;
311 igp.display = grfx::DefaultDisplay;
314 // first item in choice_width means scaling
315 if (fl_get_choice(file_->choice_width) == 1) {
316 igp.scale = strToDbl(getString(file_->input_width));
317 if (lyx::float_equal(igp.scale, 0.0, 0.05)) {
320 igp.width = LyXLength();
323 igp.width = LyXLength(getLengthFromWidgets(file_->input_width,
324 file_->choice_width));
326 igp.height = LyXLength(getLengthFromWidgets(file_->input_height,
327 file_->choice_height));
328 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
330 igp.draft = fl_get_button(file_->check_draft);
331 igp.noUnzip = fl_get_button(file_->check_nounzip);
334 if (!controller().bbChanged) {
335 // don't write anything
338 // allow length + unit input only for x1 input field
340 if (!getString(bbox_->input_bb_x1).empty()) {
341 x1_str = getLengthFromWidgets(bbox_->input_bb_x1,
342 bbox_->choice_bb_units);
343 LyXLength x1 = LyXLength(x1_str);
344 x1_str = x1.asString();
361 fl_set_choice_text(bbox_->choice_bb_units, unit.c_str());
365 if (getString(bbox_->input_bb_x0).empty())
368 bb = getLengthFromWidgets(bbox_->input_bb_x0,
369 bbox_->choice_bb_units);
373 if (getString(bbox_->input_bb_y0).empty())
376 bb += getLengthFromWidgets(bbox_->input_bb_y0,
377 bbox_->choice_bb_units);
379 bb += ' ' + x1_str + ' ';
381 if (getString(bbox_->input_bb_y1).empty())
384 bb += getLengthFromWidgets(bbox_->input_bb_y1,
385 bbox_->choice_bb_units);
389 igp.clip = fl_get_button(bbox_->check_clip);
392 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
394 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
395 while (igp.rotateAngle <= -360.0) {
396 igp.rotateAngle += 360.0;
398 while (igp.rotateAngle >= 360.0) {
399 igp.rotateAngle -= 360.0;
401 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
403 int const origin_pos = fl_get_choice(extra_->choice_origin);
404 if (origin_pos == 0) {
405 igp.rotateOrigin.erase();
407 igp.rotateOrigin = origins_[origin_pos - 1];
410 igp.subcaption = fl_get_button(extra_->check_subcaption);
411 igp.subcaptionText = getString(extra_->input_subcaption);
413 igp.special = getString(extra_->input_special);
417 void FormGraphics::update() {
418 // Update dialog with details from inset
419 InsetGraphicsParams & igp = controller().params();
422 fl_set_input(file_->input_filename, igp.filename.c_str());
423 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
425 switch (igp.display) {
426 case grfx::NoDisplay:
427 fl_set_choice(file_->choice_display, 5);
429 case grfx::ColorDisplay:
430 fl_set_choice(file_->choice_display, 4);
432 case grfx::GrayscaleDisplay:
433 fl_set_choice(file_->choice_display, 3);
435 case grfx::MonochromeDisplay:
436 fl_set_choice(file_->choice_display, 2);
438 case grfx::DefaultDisplay:
439 fl_set_choice(file_->choice_display, 1);
442 // set width input fields according to scaling or width/height input
443 if (!lyx::float_equal(igp.scale, 0.0, 0.05)) {
444 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
445 fl_set_input_maxchars(file_->input_width, 0);
446 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
447 fl_set_choice(file_->choice_width, 1);
449 fl_set_input_filter(file_->input_width, NULL);
450 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
451 updateWidgetsFromLength(file_->input_width, file_->choice_width,
452 igp.width, defaultUnit);
455 updateWidgetsFromLength(file_->input_height, file_->choice_height,
456 igp.height, defaultUnit);
458 // disable height input in case of scaling
459 bool const disable_height = !lyx::float_equal(igp.scale, 0.0, 0.05);
460 setEnabled(file_->input_height, !disable_height);
461 setEnabled(file_->choice_height, !disable_height);
463 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
464 fl_set_button(file_->check_draft, igp.draft);
465 fl_set_button(file_->check_nounzip, igp.noUnzip);
467 // disable aspectratio button in case of scaling or one of width/height
469 bool const disable_aspectRatio = disable_height ||
470 getString(file_->input_width).empty() ||
471 getString(file_->input_height).empty();
472 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
475 // set the bounding box values, if exists. First we need the whole
476 // path, because the controller knows nothing about the doc-dir
477 updateBB(igp.filename, igp.bb);
478 fl_set_button(bbox_->check_clip, igp.clip);
481 fl_set_input(extra_->input_rotate_angle,
482 tostr(igp.rotateAngle).c_str());
485 if (igp.rotateOrigin.empty()) {
488 origin_pos = 1 + findPos(origins_, igp.rotateOrigin);
490 fl_set_choice(extra_->choice_origin, origin_pos);
492 fl_set_button(extra_->check_subcaption, igp.subcaption);
493 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
494 setEnabled(extra_->input_subcaption,
495 fl_get_button(extra_->check_subcaption));
496 fl_set_input(extra_->input_special, igp.special.c_str());
498 // open dialog in the file-tab, whenever filename is empty
499 if (igp.filename.empty()) {
500 fl_set_folder(dialog_->tabfolder, file_->form);
505 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
507 // Update dialog with details from inset
508 // set the bounding box values, if exists. First we need the whole
509 // path, because the controller knows nothing about the doc-dir
510 controller().bbChanged = false;
511 if (bb_inset.empty()) {
512 lyxerr[Debug::GRAPHICS]
513 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
514 string const bb = controller().readBB(filename);
516 // get the values from the file
517 // in this case we always have the point-unit
518 fl_set_input(bbox_->input_bb_x0,
519 token(bb,' ',0).c_str());
520 fl_set_input(bbox_->input_bb_y0,
521 token(bb,' ',1).c_str());
522 fl_set_input(bbox_->input_bb_x1,
523 token(bb,' ',2).c_str());
524 fl_set_input(bbox_->input_bb_y1,
525 token(bb,' ',3).c_str());
529 fl_set_input(bbox_->input_bb_x0, bb.c_str());
530 fl_set_input(bbox_->input_bb_y0, bb.c_str());
531 fl_set_input(bbox_->input_bb_x1, bb.c_str());
532 fl_set_input(bbox_->input_bb_y1, bb.c_str());
535 fl_set_choice(bbox_->choice_bb_units, 1);
538 // get the values from the inset
539 lyxerr[Debug::GRAPHICS]
540 << "FormGraphics::updateBB(): igp has BoundingBox"
541 << " ["<< bb_inset << "]" << endl;
542 controller().bbChanged = true;
545 anyLength = LyXLength(token(bb_inset,' ',0));
546 updateWidgetsFromLength(bbox_->input_bb_x0,
547 bbox_->choice_bb_units,anyLength,"bp");
548 anyLength = LyXLength(token(bb_inset,' ',1));
549 updateWidgetsFromLength(bbox_->input_bb_y0,
550 bbox_->choice_bb_units,anyLength,"bp");
551 anyLength = LyXLength(token(bb_inset,' ',2));
552 updateWidgetsFromLength(bbox_->input_bb_x1,
553 bbox_->choice_bb_units,anyLength,"bp");
554 anyLength = LyXLength(token(bb_inset,' ',3));
555 updateWidgetsFromLength(bbox_->input_bb_y1,
556 bbox_->choice_bb_units,anyLength,"bp");
561 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
564 if (ob == file_->button_browse) {
565 // Get the filename from the dialog
566 string const in_name = getString(file_->input_filename);
567 string const out_name = controller().Browse(in_name);
568 lyxerr[Debug::GRAPHICS]
569 << "[FormGraphics]out_name: " << out_name << endl;
570 if (out_name != in_name && !out_name.empty()) {
571 fl_set_input(file_->input_filename, out_name.c_str());
573 if (controller().isFilenameValid(out_name) &&
574 !controller().bbChanged) {
575 updateBB(out_name, string());
577 } else if (ob == file_->input_width || ob == file_->input_height) {
578 // disable aspectratio button in case of scaling or one of
579 // width/height is empty
580 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
581 getString(file_->input_width).empty() ||
582 getString(file_->input_height).empty();
583 setEnabled(file_->check_aspectratio, !disable);
584 } else if (ob == file_->choice_width) {
585 // disable height input in case of scaling
586 bool const scaling = fl_get_choice(file_->choice_width) == 1;
587 setEnabled(file_->input_height, !scaling);
588 setEnabled(file_->choice_height, !scaling);
590 // allow only integer intput for scaling; float otherwise
592 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
593 fl_set_input_maxchars(file_->input_width, 0);
595 fl_set_input_filter(file_->input_width, NULL);
596 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
599 // disable aspectratio button in case of scaling or height
601 bool const disable_aspectratio =
602 scaling || getString(file_->input_height).empty();
603 setEnabled(file_->check_aspectratio, !disable_aspectratio);
605 } else if (!controller().bbChanged &&
606 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
607 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
608 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
609 controller().bbChanged = true;
610 } else if (ob == bbox_->button_getBB) {
611 string const filename = getString(file_->input_filename);
612 if (!filename.empty()) {
613 string bb = controller().readBB(filename);
615 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
616 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
617 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
618 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
619 fl_set_choice_text(bbox_->choice_bb_units, "bp");
621 controller().bbChanged = false;
623 fl_set_input(bbox_->input_bb_x0, "");
624 fl_set_input(bbox_->input_bb_y0, "");
625 fl_set_input(bbox_->input_bb_x1, "");
626 fl_set_input(bbox_->input_bb_y1, "");
627 fl_set_choice_text(bbox_->choice_bb_units, "bp");
630 } else if (ob == extra_->check_subcaption) {
631 setEnabled(extra_->input_subcaption,
632 fl_get_button(extra_->check_subcaption));
636 return ButtonPolicy::SMI_VALID;