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");
58 typedef FormController<ControlGraphics, FormView<FD_graphics> > base_class;
60 FormGraphics::FormGraphics(Dialog & parent)
61 : base_class(parent, _("Graphics"), false)
65 void FormGraphics::redraw()
67 if (form() && form()->visible)
68 fl_redraw_form(form());
71 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
72 if (outer_form && outer_form->visible)
73 fl_redraw_form(outer_form);
77 void FormGraphics::build()
79 dialog_.reset(build_graphics(this));
81 // Manage the ok, apply, restore and cancel/close buttons
82 bcview().setOK(dialog_->button_ok);
83 bcview().setApply(dialog_->button_apply);
84 bcview().setCancel(dialog_->button_close);
85 bcview().setRestore(dialog_->button_restore);
88 file_.reset(build_graphics_file(this));
90 // disable for read-only documents
91 bcview().addReadOnly(file_->button_browse);
92 bcview().addReadOnly(file_->check_aspectratio);
93 bcview().addReadOnly(file_->check_draft);
94 bcview().addReadOnly(file_->check_nounzip);
96 // check validity of "length + unit" input
97 addCheckedGlueLength(bcview(), file_->input_width);
98 addCheckedGlueLength(bcview(), file_->input_height);
100 // trigger an input event for cut&paste with middle mouse button.
101 setPrehandler(file_->input_filename);
102 setPrehandler(file_->input_lyxscale);
103 setPrehandler(file_->input_width);
104 setPrehandler(file_->input_height);
106 // for activate ok/apply immediately upon input
107 fl_set_input_return(file_->input_filename, FL_RETURN_CHANGED);
108 fl_set_input_return(file_->input_lyxscale, FL_RETURN_CHANGED);
109 fl_set_input_return(file_->input_width, FL_RETURN_CHANGED);
110 fl_set_input_return(file_->input_height, FL_RETURN_CHANGED);
112 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
113 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
115 // width default is scaling: use unsigned float filter
116 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
117 fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
119 string const display_List =
120 _("Default|Monochrome|Grayscale|Color|Do not display");
121 fl_addto_choice(file_->choice_display, display_List.c_str());
124 string const width_list = boost::io::str(boost::format(_("Scale%%%%|%1$s")) % choice_Length_All);
126 // xgettext:no-c-format
127 string const width_list = _("Scale%%|") + choice_Length_All;
129 fl_addto_choice(file_->choice_width, width_list.c_str());
131 fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
133 // set up the tooltips for the filesection
134 string str = _("The file you want to insert.");
135 tooltips().init(file_->input_filename, str);
136 str = _("Browse the directories.");
137 tooltips().init(file_->button_browse, str);
139 str = _("Scale the image to inserted percentage value.");
140 tooltips().init(file_->input_lyxscale, str);
141 str = _("Select display mode for this image.");
142 tooltips().init(file_->choice_display, str);
144 str = _("Set the image width to the inserted value.");
145 tooltips().init(file_->input_width, str);
146 // xgettext:no-c-format
147 str = _("Select unit for width; Scale% for scaling whole image.");
148 tooltips().init(file_->choice_width, str);
149 str = _("Set the image height to the inserted value.");
150 tooltips().init(file_->input_height, str);
151 str = _("Select unit for height.");
152 tooltips().init(file_->choice_height, str);
153 str = _("Do not distort the image. "
154 "Keep image within \"width\" by \"height\" and obey "
156 tooltips().init(file_->check_aspectratio, str);
157 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
158 "Useful when LaTeX should unzip the file. Needs an additional "
159 "file like \"file.eps.bb\" which holds the values for "
160 "the bounding box.");
161 tooltips().init(file_->check_nounzip, str);
162 str = _("Show image only as a rectangle of the original size.");
163 tooltips().init(file_->check_draft, str);
165 // the bounding box selection
166 bbox_.reset(build_graphics_bbox(this));
168 // disable for read-only documents
169 bcview().addReadOnly(bbox_->button_getBB);
170 bcview().addReadOnly(bbox_->check_clip);
172 // check validity of "length + unit" input
173 addCheckedLyXLength(bcview(), bbox_->input_bb_x1, bbox_->text_X);
175 // trigger an input event for cut&paste with middle mouse button.
176 setPrehandler(bbox_->input_bb_x0);
177 setPrehandler(bbox_->input_bb_y0);
178 setPrehandler(bbox_->input_bb_x1);
179 setPrehandler(bbox_->input_bb_y1);
181 // for activate ok/apply immediately upon input
182 fl_set_input_return(bbox_->input_bb_x0, FL_RETURN_CHANGED);
183 fl_set_input_return(bbox_->input_bb_y0, FL_RETURN_CHANGED);
184 fl_set_input_return(bbox_->input_bb_x1, FL_RETURN_CHANGED);
185 fl_set_input_return(bbox_->input_bb_y1, FL_RETURN_CHANGED);
187 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
188 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
189 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
191 string const bb_units = getStringFromVector(frnt::getBBUnits(), "|");
192 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
194 // set up the tooltips for the bounding-box-section
195 str = _("The lower left x-value of the bounding box.");
196 tooltips().init(bbox_->input_bb_x0, str);
197 str = _("The lower left y-value of the bounding box.");
198 tooltips().init(bbox_->input_bb_y0, str);
199 str = _("The upper right x-value of the bounding box; "
200 "only this input field allows length + unit, e.g. 5cm "
201 "and sets the unit for the other input fields.");
202 tooltips().init(bbox_->input_bb_x1, str);
203 str = _("The upper right y-value of the bounding box.");
204 tooltips().init(bbox_->input_bb_y1, str);
205 str = _("Select unit for the bounding box values.");
206 tooltips().init(bbox_->choice_bb_units, str);
208 str = _("Read the image coordinates new from file. For (e)ps-file "
209 "the bounding box is read, otherwise the imagesize in pixels. "
210 "Default unit is \"bp\", the PostScript's b(ig) p(oint).");
211 tooltips().init(bbox_->button_getBB, str);
213 str = _("Clip image to the bounding box values.");
214 tooltips().init(bbox_->check_clip, str);
217 extra_.reset(build_graphics_extra(this));
219 // disable for read-only documents
220 bcview().addReadOnly(extra_->input_rotate_angle);
221 bcview().addReadOnly(extra_->choice_origin);
222 bcview().addReadOnly(extra_->check_subcaption);
223 bcview().addReadOnly(extra_->input_special);
225 // trigger an input event for cut&paste with middle mouse button.
226 setPrehandler(extra_->input_rotate_angle);
227 setPrehandler(extra_->input_subcaption);
228 setPrehandler(extra_->input_special);
230 fl_set_input_return(extra_->input_rotate_angle, FL_RETURN_CHANGED);
231 fl_set_input_return(extra_->input_subcaption, FL_RETURN_CHANGED);
232 fl_set_input_return(extra_->input_special, FL_RETURN_CHANGED);
234 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
236 using namespace frnt;
237 vector<RotationOriginPair> origindata = getRotationOriginData();
239 // Store the identifiers for later
240 origins_ = getSecond(origindata);
242 string const choice = getStringFromVector(getFirst(origindata), "|");
243 fl_addto_choice(extra_->choice_origin, choice.c_str());
245 // set up the tooltips for the extra section
246 str = _("Insert the rotation angle in degrees. "
247 "Positive value rotates anti-clockwise, "
248 "negative value clockwise.");
249 tooltips().init(extra_->input_rotate_angle, str);
250 str = _("Insert the point of origin for rotation.");
251 tooltips().init(extra_->choice_origin, str);
252 str = _("Enables use of subfigure with its own caption.");
253 tooltips().init(extra_->check_subcaption, str);
254 str = _("Insert the optional subfigure caption.");
255 tooltips().init(extra_->input_subcaption, str);
256 str = _("Add any additional LaTeX option, which is defined in the "
257 "graphicx-package and not mentioned in the gui's tabfolders.");
258 tooltips().init(extra_->input_special, str);
260 // add the different tabfolders
261 fl_addto_tabfolder(dialog_->tabfolder, _("File"), file_->form);
262 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box"), bbox_->form);
263 fl_addto_tabfolder(dialog_->tabfolder, _("Extra"), extra_->form);
265 // work-around xforms bug re update of folder->x, folder->y coords.
266 setPrehandler(dialog_->tabfolder);
268 // set the right default unit
269 switch (lyxrc.default_papersize) {
270 case BufferParams::PAPER_DEFAULT: break;
271 case BufferParams::PAPER_USLETTER:
272 case BufferParams::PAPER_LEGALPAPER:
273 case BufferParams::PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
274 case BufferParams::PAPER_A3PAPER:
275 case BufferParams::PAPER_A4PAPER:
276 case BufferParams::PAPER_A5PAPER:
277 case BufferParams::PAPER_B5PAPER: defaultUnit = "cm"; break;
282 void FormGraphics::apply()
284 // Create the parameters structure and fill the data from the dialog.
285 InsetGraphicsParams & igp = controller().params();
288 igp.filename = getString(file_->input_filename);
290 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
291 if (igp.lyxscale == 0) {
295 switch (fl_get_choice(file_->choice_display)) {
297 igp.display = grfx::NoDisplay;
300 igp.display = grfx::ColorDisplay;
303 igp.display = grfx::GrayscaleDisplay;
306 igp.display = grfx::MonochromeDisplay;
309 igp.display = grfx::DefaultDisplay;
312 // first item in choice_width means scaling
313 if (fl_get_choice(file_->choice_width) == 1) {
314 igp.scale = strToDbl(getString(file_->input_width));
315 if (lyx::float_equal(igp.scale, 0.0, 0.05)) {
318 igp.width = LyXLength();
321 igp.width = LyXLength(getLengthFromWidgets(file_->input_width,
322 file_->choice_width));
324 igp.height = LyXLength(getLengthFromWidgets(file_->input_height,
325 file_->choice_height));
326 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
328 igp.draft = fl_get_button(file_->check_draft);
329 igp.noUnzip = fl_get_button(file_->check_nounzip);
332 if (!controller().bbChanged) {
333 // don't write anything
336 // allow length + unit input only for x1 input field
338 if (!getString(bbox_->input_bb_x1).empty()) {
339 x1_str = getLengthFromWidgets(bbox_->input_bb_x1,
340 bbox_->choice_bb_units);
341 LyXLength x1 = LyXLength(x1_str);
342 x1_str = x1.asString();
359 fl_set_choice_text(bbox_->choice_bb_units, unit.c_str());
363 if (getString(bbox_->input_bb_x0).empty())
366 bb = getLengthFromWidgets(bbox_->input_bb_x0,
367 bbox_->choice_bb_units);
371 if (getString(bbox_->input_bb_y0).empty())
374 bb += getLengthFromWidgets(bbox_->input_bb_y0,
375 bbox_->choice_bb_units);
377 bb += ' ' + x1_str + ' ';
379 if (getString(bbox_->input_bb_y1).empty())
382 bb += getLengthFromWidgets(bbox_->input_bb_y1,
383 bbox_->choice_bb_units);
387 igp.clip = fl_get_button(bbox_->check_clip);
390 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
392 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
393 while (igp.rotateAngle <= -360.0) {
394 igp.rotateAngle += 360.0;
396 while (igp.rotateAngle >= 360.0) {
397 igp.rotateAngle -= 360.0;
399 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
401 int const origin_pos = fl_get_choice(extra_->choice_origin);
402 if (origin_pos == 0) {
403 igp.rotateOrigin.erase();
405 igp.rotateOrigin = origins_[origin_pos - 1];
408 igp.subcaption = fl_get_button(extra_->check_subcaption);
409 igp.subcaptionText = getString(extra_->input_subcaption);
411 igp.special = getString(extra_->input_special);
415 void FormGraphics::update() {
416 // Update dialog with details from inset
417 InsetGraphicsParams & igp = controller().params();
420 fl_set_input(file_->input_filename, igp.filename.c_str());
421 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
423 switch (igp.display) {
424 case grfx::NoDisplay:
425 fl_set_choice(file_->choice_display, 5);
427 case grfx::ColorDisplay:
428 fl_set_choice(file_->choice_display, 4);
430 case grfx::GrayscaleDisplay:
431 fl_set_choice(file_->choice_display, 3);
433 case grfx::MonochromeDisplay:
434 fl_set_choice(file_->choice_display, 2);
436 case grfx::DefaultDisplay:
437 fl_set_choice(file_->choice_display, 1);
440 // set width input fields according to scaling or width/height input
441 if (!lyx::float_equal(igp.scale, 0.0, 0.05)) {
442 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
443 fl_set_input_maxchars(file_->input_width, 0);
444 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
445 fl_set_choice(file_->choice_width, 1);
447 fl_set_input_filter(file_->input_width, NULL);
448 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
449 updateWidgetsFromLength(file_->input_width, file_->choice_width,
450 igp.width, defaultUnit);
453 updateWidgetsFromLength(file_->input_height, file_->choice_height,
454 igp.height, defaultUnit);
456 // disable height input in case of scaling
457 bool const disable_height = !lyx::float_equal(igp.scale, 0.0, 0.05);
458 setEnabled(file_->input_height, !disable_height);
459 setEnabled(file_->choice_height, !disable_height);
461 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
462 fl_set_button(file_->check_draft, igp.draft);
463 fl_set_button(file_->check_nounzip, igp.noUnzip);
465 // disable aspectratio button in case of scaling or one of width/height
467 bool const disable_aspectRatio = disable_height ||
468 getString(file_->input_width).empty() ||
469 getString(file_->input_height).empty();
470 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
473 // set the bounding box values, if exists. First we need the whole
474 // path, because the controller knows nothing about the doc-dir
475 updateBB(igp.filename, igp.bb);
476 fl_set_button(bbox_->check_clip, igp.clip);
479 fl_set_input(extra_->input_rotate_angle,
480 tostr(igp.rotateAngle).c_str());
483 if (igp.rotateOrigin.empty()) {
486 origin_pos = 1 + findPos(origins_, igp.rotateOrigin);
488 fl_set_choice(extra_->choice_origin, origin_pos);
490 fl_set_button(extra_->check_subcaption, igp.subcaption);
491 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
492 setEnabled(extra_->input_subcaption,
493 fl_get_button(extra_->check_subcaption));
494 fl_set_input(extra_->input_special, igp.special.c_str());
496 // open dialog in the file-tab, whenever filename is empty
497 if (igp.filename.empty()) {
498 fl_set_folder(dialog_->tabfolder, file_->form);
503 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
505 // Update dialog with details from inset
506 // set the bounding box values, if exists. First we need the whole
507 // path, because the controller knows nothing about the doc-dir
508 controller().bbChanged = false;
509 if (bb_inset.empty()) {
510 lyxerr[Debug::GRAPHICS]
511 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
512 string const bb = controller().readBB(filename);
514 // get the values from the file
515 // in this case we always have the point-unit
516 fl_set_input(bbox_->input_bb_x0,
517 token(bb,' ',0).c_str());
518 fl_set_input(bbox_->input_bb_y0,
519 token(bb,' ',1).c_str());
520 fl_set_input(bbox_->input_bb_x1,
521 token(bb,' ',2).c_str());
522 fl_set_input(bbox_->input_bb_y1,
523 token(bb,' ',3).c_str());
527 fl_set_input(bbox_->input_bb_x0, bb.c_str());
528 fl_set_input(bbox_->input_bb_y0, bb.c_str());
529 fl_set_input(bbox_->input_bb_x1, bb.c_str());
530 fl_set_input(bbox_->input_bb_y1, bb.c_str());
533 fl_set_choice(bbox_->choice_bb_units, 1);
536 // get the values from the inset
537 lyxerr[Debug::GRAPHICS]
538 << "FormGraphics::updateBB(): igp has BoundingBox"
539 << " ["<< bb_inset << "]" << endl;
540 controller().bbChanged = true;
543 anyLength = LyXLength(token(bb_inset,' ',0));
544 updateWidgetsFromLength(bbox_->input_bb_x0,
545 bbox_->choice_bb_units,anyLength,"bp");
546 anyLength = LyXLength(token(bb_inset,' ',1));
547 updateWidgetsFromLength(bbox_->input_bb_y0,
548 bbox_->choice_bb_units,anyLength,"bp");
549 anyLength = LyXLength(token(bb_inset,' ',2));
550 updateWidgetsFromLength(bbox_->input_bb_x1,
551 bbox_->choice_bb_units,anyLength,"bp");
552 anyLength = LyXLength(token(bb_inset,' ',3));
553 updateWidgetsFromLength(bbox_->input_bb_y1,
554 bbox_->choice_bb_units,anyLength,"bp");
559 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
562 if (ob == file_->button_browse) {
563 // Get the filename from the dialog
564 string const in_name = getString(file_->input_filename);
565 string const out_name = controller().Browse(in_name);
566 lyxerr[Debug::GRAPHICS]
567 << "[FormGraphics]out_name: " << out_name << endl;
568 if (out_name != in_name && !out_name.empty()) {
569 fl_set_input(file_->input_filename, out_name.c_str());
571 if (controller().isFilenameValid(out_name) &&
572 !controller().bbChanged) {
573 updateBB(out_name, string());
575 } else if (ob == file_->input_width || ob == file_->input_height) {
576 // disable aspectratio button in case of scaling or one of
577 // width/height is empty
578 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
579 getString(file_->input_width).empty() ||
580 getString(file_->input_height).empty();
581 setEnabled(file_->check_aspectratio, !disable);
582 } else if (ob == file_->choice_width) {
583 // disable height input in case of scaling
584 bool const scaling = fl_get_choice(file_->choice_width) == 1;
585 setEnabled(file_->input_height, !scaling);
586 setEnabled(file_->choice_height, !scaling);
588 // allow only integer intput for scaling; float otherwise
590 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
591 fl_set_input_maxchars(file_->input_width, 0);
593 fl_set_input_filter(file_->input_width, NULL);
594 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
597 // disable aspectratio button in case of scaling or height
599 bool const disable_aspectratio =
600 scaling || getString(file_->input_height).empty();
601 setEnabled(file_->check_aspectratio, !disable_aspectratio);
603 } else if (!controller().bbChanged &&
604 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
605 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
606 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
607 controller().bbChanged = true;
608 } else if (ob == bbox_->button_getBB) {
609 string const filename = getString(file_->input_filename);
610 if (!filename.empty()) {
611 string bb = controller().readBB(filename);
613 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
614 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
615 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
616 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
617 fl_set_choice_text(bbox_->choice_bb_units, "bp");
619 controller().bbChanged = false;
621 fl_set_input(bbox_->input_bb_x0, "");
622 fl_set_input(bbox_->input_bb_y0, "");
623 fl_set_input(bbox_->input_bb_x1, "");
624 fl_set_input(bbox_->input_bb_y1, "");
625 fl_set_choice_text(bbox_->choice_bb_units, "bp");
628 } else if (ob == extra_->check_subcaption) {
629 setEnabled(extra_->input_subcaption,
630 fl_get_button(extra_->check_subcaption));
634 return ButtonPolicy::SMI_VALID;