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
16 #pragma implementation
20 #include "ControlGraphics.h"
21 #include "FormGraphics.h"
22 #include "forms/form_graphics.h"
24 #include "checkedwidgets.h"
25 #include "input_validators.h"
27 #include "xforms_helpers.h"
29 #include "debug.h" // for lyxerr
30 #include "lyxrc.h" // for lyxrc.display_graphics
32 #include "insets/insetgraphicsParams.h"
34 #include "controllers/helper_funcs.h" // for getStringFromVector
36 #include "frontends/Alert.h"
38 #include "support/lstrings.h" // for strToDbl & tostr
39 #include "support/filetools.h" // for MakeAbsPath etc
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 FormCB<ControlGraphics, FormDB<FD_graphics> > base_class;
60 FormGraphics::FormGraphics()
61 : base_class(_("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 bc().setOK(dialog_->button_ok);
83 bc().setApply(dialog_->button_apply);
84 bc().setCancel(dialog_->button_close);
85 bc().setRestore(dialog_->button_restore);
88 file_.reset(build_graphics_file(this));
90 // disable for read-only documents
91 bc().addReadOnly(file_->button_browse);
92 bc().addReadOnly(file_->check_aspectratio);
93 bc().addReadOnly(file_->check_draft);
94 bc().addReadOnly(file_->check_nounzip);
96 // check validity of "length + unit" input
97 addCheckedGlueLength(bc(), file_->input_width);
98 addCheckedGlueLength(bc(), 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_float_filter);
115 // width default is scaling, thus unsigned integer input
116 fl_set_input_filter(file_->input_width, fl_unsigned_int_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());
123 string const width_list = _("Scale%%|") + choice_Length_All;
124 fl_addto_choice(file_->choice_width, width_list.c_str());
126 fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
128 // set up the tooltips for the filesection
129 string str = _("The file you want to insert");
130 tooltips().init(file_->input_filename, str);
131 str = _("Browse the directories");
132 tooltips().init(file_->button_browse, str);
134 str = _("Scale the image to inserted percentage value");
135 tooltips().init(file_->input_lyxscale, str);
136 str = _("Select display mode for this image");
137 tooltips().init(file_->choice_display, str);
139 str = _("Set the image width to the inserted value");
140 tooltips().init(file_->input_width, str);
141 str = _("Select unit for width; Scale% for scaling whole image");
142 tooltips().init(file_->choice_width, str);
143 str = _("Set the image height to the inserted value");
144 tooltips().init(file_->input_height, str);
145 str = _("Select unit for height");
146 tooltips().init(file_->choice_height, str);
147 str = _("Do not distort the image. "
148 "Keep image within \"width\" by \"height\" and obey "
150 tooltips().init(file_->check_aspectratio, str);
152 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
153 "Useful when LaTeX should unzip the file. Needs an additional file "
154 "like \"file.eps.bb\" which holds the values for the bounding box");
155 tooltips().init(file_->check_nounzip, str);
156 str = _("Show image only as a rectangle of the original size");
157 tooltips().init(file_->check_draft, str);
159 // the bounding box selection
160 bbox_.reset(build_graphics_bbox(this));
162 // disable for read-only documents
163 bc().addReadOnly(bbox_->button_getBB);
164 bc().addReadOnly(bbox_->check_clip);
166 // check validity of "length + unit" input
167 addCheckedLyXLength(bc(), bbox_->input_bb_x1, bbox_->text_X);
169 // trigger an input event for cut&paste with middle mouse button.
170 setPrehandler(bbox_->input_bb_x0);
171 setPrehandler(bbox_->input_bb_y0);
172 setPrehandler(bbox_->input_bb_x1);
173 setPrehandler(bbox_->input_bb_y1);
175 // for activate ok/apply immediately upon input
176 fl_set_input_return(bbox_->input_bb_x0, FL_RETURN_CHANGED);
177 fl_set_input_return(bbox_->input_bb_y0, FL_RETURN_CHANGED);
178 fl_set_input_return(bbox_->input_bb_x1, FL_RETURN_CHANGED);
179 fl_set_input_return(bbox_->input_bb_y1, FL_RETURN_CHANGED);
181 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
182 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
183 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
185 string const bb_units = "bp|cm|mm|in";
186 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
188 // set up the tooltips for the bounding-box-section
189 str = _("The lower left x-value of the bounding box");
190 tooltips().init(bbox_->input_bb_x0, str);
191 str = _("The lower left y-value of the bounding box");
192 tooltips().init(bbox_->input_bb_y0, str);
193 str = _("The upper right x-value of the bounding box; "
194 "only this input field allows length + unit, e.g. 5cm "
195 "and sets the unit for the other input fields");
196 tooltips().init(bbox_->input_bb_x1, str);
197 str = _("The upper right y-value of the bounding box");
198 tooltips().init(bbox_->input_bb_y1, str);
199 str = _("Select unit for the bounding box values");
200 tooltips().init(bbox_->choice_bb_units, str);
202 str = _("Read the image coordinates new from file. For (e)ps-file "
203 "the bounding box is read, otherwise the imagesize in pixels. "
204 "Default unit is \"bp\", the PostScript's b(ig) p(oint)");
205 tooltips().init(bbox_->button_getBB, str);
207 str = _("Clip image to the bounding box values");
208 tooltips().init(bbox_->check_clip, str);
211 extra_.reset(build_graphics_extra(this));
213 // disable for read-only documents
214 bc().addReadOnly(extra_->input_rotate_angle);
215 bc().addReadOnly(extra_->choice_origin);
216 bc().addReadOnly(extra_->check_subcaption);
217 bc().addReadOnly(extra_->input_special);
219 // trigger an input event for cut&paste with middle mouse button.
220 setPrehandler(extra_->input_rotate_angle);
221 setPrehandler(extra_->input_subcaption);
222 setPrehandler(extra_->input_special);
224 fl_set_input_return(extra_->input_rotate_angle, FL_RETURN_CHANGED);
225 fl_set_input_return(extra_->input_subcaption, FL_RETURN_CHANGED);
226 fl_set_input_return(extra_->input_special, FL_RETURN_CHANGED);
228 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
230 using namespace frnt;
231 vector<RotationOriginPair> origindata = getRotationOriginData();
233 // Store the identifiers for later
234 origins_ = getSecond(origindata);
236 string const choice = "Default|" + getStringFromVector(getFirst(origindata), "|");
237 fl_addto_choice(extra_->choice_origin, choice.c_str());
239 // set up the tooltips for the extra section
240 str = _("Insert the rotation angle in degrees. "
241 "Positive value rotates anti-clockwise, "
242 "negative value clockwise");
243 tooltips().init(extra_->input_rotate_angle, str);
244 str = _("Insert the point of origin for rotation");
245 tooltips().init(extra_->choice_origin, str);
246 str = _("Enables use of subfigure with its own caption");
247 tooltips().init(extra_->check_subcaption, str);
248 str = _("Insert the optional subfigure caption");
249 tooltips().init(extra_->input_subcaption, str);
250 str = _("Add any additional latex option, which is defined in the "
251 "graphicx-package and not mentioned in the gui's tabfolders");
252 tooltips().init(extra_->input_special, str);
254 // add the different tabfolders
255 fl_addto_tabfolder(dialog_->tabfolder, _("File"), file_->form);
256 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box"), bbox_->form);
257 fl_addto_tabfolder(dialog_->tabfolder, _("Extra"), extra_->form);
259 // work-around xforms bug re update of folder->x, folder->y coords.
260 setPrehandler(dialog_->tabfolder);
262 // set the right default unit
263 switch (lyxrc.default_papersize) {
264 case BufferParams::PAPER_DEFAULT: break;
265 case BufferParams::PAPER_USLETTER:
266 case BufferParams::PAPER_LEGALPAPER:
267 case BufferParams::PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
268 case BufferParams::PAPER_A3PAPER:
269 case BufferParams::PAPER_A4PAPER:
270 case BufferParams::PAPER_A5PAPER:
271 case BufferParams::PAPER_B5PAPER: defaultUnit = "cm"; break;
276 void FormGraphics::apply()
278 // Create the parameters structure and fill the data from the dialog.
279 InsetGraphicsParams & igp = controller().params();
282 igp.filename = getString(file_->input_filename);
284 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
285 if (igp.lyxscale == 0) {
289 switch (fl_get_choice(file_->choice_display)) {
291 igp.display = grfx::NoDisplay;
294 igp.display = grfx::ColorDisplay;
297 igp.display = grfx::GrayscaleDisplay;
300 igp.display = grfx::MonochromeDisplay;
303 igp.display = grfx::DefaultDisplay;
306 // first item in choice_width means scaling
307 if (fl_get_choice(file_->choice_width) == 1) {
308 igp.scale = strToDbl(getString(file_->input_width));
309 if (igp.scale == 0.0) {
312 igp.width = LyXLength();
315 igp.width = LyXLength(getLengthFromWidgets(file_->input_width,
316 file_->choice_width));
318 igp.height = LyXLength(getLengthFromWidgets(file_->input_height,
319 file_->choice_height));
320 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
322 igp.draft = fl_get_button(file_->check_draft);
323 igp.noUnzip = fl_get_button(file_->check_nounzip);
326 if (!controller().bbChanged) {
327 // don't write anything
330 // allow length + unit input only for x1 input field
332 if (!getString(bbox_->input_bb_x1).empty()) {
333 x1_str = getLengthFromWidgets(bbox_->input_bb_x1,
334 bbox_->choice_bb_units);
335 LyXLength x1 = LyXLength(x1_str);
336 x1_str = x1.asString();
353 fl_set_choice_text(bbox_->choice_bb_units, unit.c_str());
357 if (getString(bbox_->input_bb_x0).empty())
360 bb = getLengthFromWidgets(bbox_->input_bb_x0,
361 bbox_->choice_bb_units);
365 if (getString(bbox_->input_bb_y0).empty())
368 bb += getLengthFromWidgets(bbox_->input_bb_y0,
369 bbox_->choice_bb_units);
371 bb += " " + x1_str + " ";
373 if (getString(bbox_->input_bb_y1).empty())
376 bb += getLengthFromWidgets(bbox_->input_bb_y1,
377 bbox_->choice_bb_units);
381 igp.clip = fl_get_button(bbox_->check_clip);
384 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
386 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
387 while (igp.rotateAngle <= -360.0) {
388 igp.rotateAngle += 360.0;
390 while (igp.rotateAngle >= 360.0) {
391 igp.rotateAngle -= 360.0;
393 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
395 int const origin_pos = fl_get_choice(extra_->choice_origin);
396 if (origin_pos == 1) {
397 igp.rotateOrigin.erase();
399 igp.rotateOrigin = origins_[origin_pos - 2];
402 igp.subcaption = fl_get_button(extra_->check_subcaption);
403 igp.subcaptionText = getString(extra_->input_subcaption);
405 igp.special = getString(extra_->input_special);
409 void FormGraphics::update() {
410 // Update dialog with details from inset
411 InsetGraphicsParams & igp = controller().params();
414 fl_set_input(file_->input_filename, igp.filename.c_str());
415 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
417 switch (igp.display) {
418 case grfx::NoDisplay:
419 fl_set_choice(file_->choice_display, 5);
421 case grfx::ColorDisplay:
422 fl_set_choice(file_->choice_display, 4);
424 case grfx::GrayscaleDisplay:
425 fl_set_choice(file_->choice_display, 3);
427 case grfx::MonochromeDisplay:
428 fl_set_choice(file_->choice_display, 2);
430 case grfx::DefaultDisplay:
431 fl_set_choice(file_->choice_display, 1);
434 // set width input fields according to scaling or width/height input
436 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
437 fl_set_input_maxchars(file_->input_width, 0);
438 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
439 fl_set_choice(file_->choice_width, 1);
441 fl_set_input_filter(file_->input_width, NULL);
442 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
443 updateWidgetsFromLength(file_->input_width, file_->choice_width,
444 igp.width, defaultUnit);
447 updateWidgetsFromLength(file_->input_height, file_->choice_height,
448 igp.height, defaultUnit);
450 // disable height input in case of scaling
451 bool const disable_height = igp.scale != 0.0;
452 setEnabled(file_->input_height, !disable_height);
453 setEnabled(file_->choice_height, !disable_height);
455 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
456 fl_set_button(file_->check_draft, igp.draft);
457 fl_set_button(file_->check_nounzip, igp.noUnzip);
459 // disable aspectratio button in case of scaling or one of width/height
461 bool const disable_aspectRatio = disable_height ||
462 getString(file_->input_width).empty() ||
463 getString(file_->input_height).empty();
464 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
467 // set the bounding box values, if exists. First we need the whole
468 // path, because the controller knows nothing about the doc-dir
469 updateBB(igp.filename, igp.bb);
470 fl_set_button(bbox_->check_clip, igp.clip);
473 fl_set_input(extra_->input_rotate_angle,
474 tostr(igp.rotateAngle).c_str());
477 if (igp.rotateOrigin.empty()) {
480 origin_pos = 2 + findPos(origins_, igp.rotateOrigin);
482 fl_set_choice(extra_->choice_origin, origin_pos);
484 fl_set_button(extra_->check_subcaption, igp.subcaption);
485 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
486 setEnabled(extra_->input_subcaption,
487 fl_get_button(extra_->check_subcaption));
488 fl_set_input(extra_->input_special, igp.special.c_str());
490 // open dialog in the file-tab, whenever filename is empty
491 if (igp.filename.empty()) {
492 fl_set_folder(dialog_->tabfolder, file_->form);
497 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
499 // Update dialog with details from inset
500 // set the bounding box values, if exists. First we need the whole
501 // path, because the controller knows nothing about the doc-dir
502 controller().bbChanged = false;
503 if (bb_inset.empty()) {
504 lyxerr[Debug::GRAPHICS]
505 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
506 string const bb = controller().readBB(filename);
508 // get the values from the file
509 // in this case we always have the point-unit
510 fl_set_input(bbox_->input_bb_x0,
511 token(bb,' ',0).c_str());
512 fl_set_input(bbox_->input_bb_y0,
513 token(bb,' ',1).c_str());
514 fl_set_input(bbox_->input_bb_x1,
515 token(bb,' ',2).c_str());
516 fl_set_input(bbox_->input_bb_y1,
517 token(bb,' ',3).c_str());
521 fl_set_input(bbox_->input_bb_x0, bb.c_str());
522 fl_set_input(bbox_->input_bb_y0, bb.c_str());
523 fl_set_input(bbox_->input_bb_x1, bb.c_str());
524 fl_set_input(bbox_->input_bb_y1, bb.c_str());
527 fl_set_choice(bbox_->choice_bb_units, 1);
530 // get the values from the inset
531 lyxerr[Debug::GRAPHICS]
532 << "FormGraphics::updateBB(): igp has BoundingBox"
533 << " ["<< bb_inset << "]" << endl;
534 controller().bbChanged = true;
537 anyLength = LyXLength(token(bb_inset,' ',0));
538 updateWidgetsFromLength(bbox_->input_bb_x0,
539 bbox_->choice_bb_units,anyLength,"bp");
540 anyLength = LyXLength(token(bb_inset,' ',1));
541 updateWidgetsFromLength(bbox_->input_bb_y0,
542 bbox_->choice_bb_units,anyLength,"bp");
543 anyLength = LyXLength(token(bb_inset,' ',2));
544 updateWidgetsFromLength(bbox_->input_bb_x1,
545 bbox_->choice_bb_units,anyLength,"bp");
546 anyLength = LyXLength(token(bb_inset,' ',3));
547 updateWidgetsFromLength(bbox_->input_bb_y1,
548 bbox_->choice_bb_units,anyLength,"bp");
553 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
556 if (ob == file_->button_browse) {
557 // Get the filename from the dialog
558 string const in_name = getString(file_->input_filename);
559 string const out_name = controller().Browse(in_name);
560 lyxerr[Debug::GRAPHICS]
561 << "[FormGraphics]out_name: " << out_name << endl;
562 if (out_name != in_name && !out_name.empty()) {
563 fl_set_input(file_->input_filename, out_name.c_str());
565 if (controller().isFilenameValid(out_name) &&
566 !controller().bbChanged) {
567 updateBB(out_name, string());
569 } else if (ob == file_->input_width || ob == file_->input_height) {
570 // disable aspectratio button in case of scaling or one of
571 // width/height is empty
572 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
573 getString(file_->input_width).empty() ||
574 getString(file_->input_height).empty();
575 setEnabled(file_->check_aspectratio, !disable);
576 } else if (ob == file_->choice_width) {
577 // disable height input in case of scaling
578 bool const scaling = fl_get_choice(file_->choice_width) == 1;
579 setEnabled(file_->input_height, !scaling);
580 setEnabled(file_->choice_height, !scaling);
582 // allow only integer intput for scaling; float otherwise
584 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
585 fl_set_input_maxchars(file_->input_width, 0);
587 fl_set_input_filter(file_->input_width, NULL);
588 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
591 // disable aspectratio button in case of scaling or height
593 bool const disable_aspectratio =
594 scaling || getString(file_->input_height).empty();
595 setEnabled(file_->check_aspectratio, !disable_aspectratio);
597 } else if (!controller().bbChanged &&
598 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
599 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
600 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
601 controller().bbChanged = true;
602 } else if (ob == bbox_->button_getBB) {
603 string const filename = getString(file_->input_filename);
604 if (!filename.empty()) {
605 string bb = controller().readBB(filename);
607 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
608 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
609 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
610 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
611 fl_set_choice_text(bbox_->choice_bb_units, "bp");
613 controller().bbChanged = false;
615 fl_set_input(bbox_->input_bb_x0, "");
616 fl_set_input(bbox_->input_bb_y0, "");
617 fl_set_input(bbox_->input_bb_x1, "");
618 fl_set_input(bbox_->input_bb_y1, "");
619 fl_set_choice_text(bbox_->choice_bb_units, "bp");
622 } else if (ob == extra_->check_subcaption) {
623 setEnabled(extra_->input_subcaption,
624 fl_get_button(extra_->check_subcaption));
628 return ButtonPolicy::SMI_VALID;