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/lyxlib.h" // for float_equal
40 #include "support/filetools.h" // for MakeAbsPath etc
42 #include FORMS_H_LOCATION
50 // Bound the number of input characters
51 int const SIZE_MAXDIGITS = 10;
52 int const FILENAME_MAXCHARS = 1024;
54 string defaultUnit("cm");
59 typedef FormCB<ControlGraphics, FormDB<FD_graphics> > base_class;
61 FormGraphics::FormGraphics()
62 : base_class(_("Graphics"), false)
66 void FormGraphics::redraw()
68 if (form() && form()->visible)
69 fl_redraw_form(form());
72 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
73 if (outer_form && outer_form->visible)
74 fl_redraw_form(outer_form);
78 void FormGraphics::build()
80 dialog_.reset(build_graphics(this));
82 // Manage the ok, apply, restore and cancel/close buttons
83 bc().setOK(dialog_->button_ok);
84 bc().setApply(dialog_->button_apply);
85 bc().setCancel(dialog_->button_close);
86 bc().setRestore(dialog_->button_restore);
89 file_.reset(build_graphics_file(this));
91 // disable for read-only documents
92 bc().addReadOnly(file_->button_browse);
93 bc().addReadOnly(file_->check_aspectratio);
94 bc().addReadOnly(file_->check_draft);
95 bc().addReadOnly(file_->check_nounzip);
97 // check validity of "length + unit" input
98 addCheckedGlueLength(bc(), file_->input_width);
99 addCheckedGlueLength(bc(), file_->input_height);
101 // trigger an input event for cut&paste with middle mouse button.
102 setPrehandler(file_->input_filename);
103 setPrehandler(file_->input_lyxscale);
104 setPrehandler(file_->input_width);
105 setPrehandler(file_->input_height);
107 // for activate ok/apply immediately upon input
108 fl_set_input_return(file_->input_filename, FL_RETURN_CHANGED);
109 fl_set_input_return(file_->input_lyxscale, FL_RETURN_CHANGED);
110 fl_set_input_return(file_->input_width, FL_RETURN_CHANGED);
111 fl_set_input_return(file_->input_height, FL_RETURN_CHANGED);
113 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
114 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
116 // width default is scaling: use unsigned float filter
117 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
118 fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
120 string const display_List =
121 _("Default|Monochrome|Grayscale|Color|Do not display");
122 fl_addto_choice(file_->choice_display, display_List.c_str());
124 string const width_list = _("Scale%%|") + choice_Length_All;
125 fl_addto_choice(file_->choice_width, width_list.c_str());
127 fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
129 // set up the tooltips for the filesection
130 string str = _("The file you want to insert.");
131 tooltips().init(file_->input_filename, str);
132 str = _("Browse the directories.");
133 tooltips().init(file_->button_browse, str);
135 str = _("Scale the image to inserted percentage value.");
136 tooltips().init(file_->input_lyxscale, str);
137 str = _("Select display mode for this image.");
138 tooltips().init(file_->choice_display, str);
140 str = _("Set the image width to the inserted value.");
141 tooltips().init(file_->input_width, str);
142 str = _("Select unit for width; Scale% for scaling whole image.");
143 tooltips().init(file_->choice_width, str);
144 str = _("Set the image height to the inserted value.");
145 tooltips().init(file_->input_height, str);
146 str = _("Select unit for height.");
147 tooltips().init(file_->choice_height, str);
148 str = _("Do not distort the image. "
149 "Keep image within \"width\" by \"height\" and obey "
151 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 "
154 "file like \"file.eps.bb\" which holds the values for "
155 "the bounding box.");
156 tooltips().init(file_->check_nounzip, str);
157 str = _("Show image only as a rectangle of the original size.");
158 tooltips().init(file_->check_draft, str);
160 // the bounding box selection
161 bbox_.reset(build_graphics_bbox(this));
163 // disable for read-only documents
164 bc().addReadOnly(bbox_->button_getBB);
165 bc().addReadOnly(bbox_->check_clip);
167 // check validity of "length + unit" input
168 addCheckedLyXLength(bc(), bbox_->input_bb_x1, bbox_->text_X);
170 // trigger an input event for cut&paste with middle mouse button.
171 setPrehandler(bbox_->input_bb_x0);
172 setPrehandler(bbox_->input_bb_y0);
173 setPrehandler(bbox_->input_bb_x1);
174 setPrehandler(bbox_->input_bb_y1);
176 // for activate ok/apply immediately upon input
177 fl_set_input_return(bbox_->input_bb_x0, FL_RETURN_CHANGED);
178 fl_set_input_return(bbox_->input_bb_y0, FL_RETURN_CHANGED);
179 fl_set_input_return(bbox_->input_bb_x1, FL_RETURN_CHANGED);
180 fl_set_input_return(bbox_->input_bb_y1, FL_RETURN_CHANGED);
182 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
183 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
184 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
186 string const bb_units = getStringFromVector(frnt::getBBUnits(), "|");
187 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
189 // set up the tooltips for the bounding-box-section
190 str = _("The lower left x-value of the bounding box.");
191 tooltips().init(bbox_->input_bb_x0, str);
192 str = _("The lower left y-value of the bounding box.");
193 tooltips().init(bbox_->input_bb_y0, str);
194 str = _("The upper right x-value of the bounding box; "
195 "only this input field allows length + unit, e.g. 5cm "
196 "and sets the unit for the other input fields.");
197 tooltips().init(bbox_->input_bb_x1, str);
198 str = _("The upper right y-value of the bounding box.");
199 tooltips().init(bbox_->input_bb_y1, str);
200 str = _("Select unit for the bounding box values.");
201 tooltips().init(bbox_->choice_bb_units, str);
203 str = _("Read the image coordinates new from file. For (e)ps-file "
204 "the bounding box is read, otherwise the imagesize in pixels. "
205 "Default unit is \"bp\", the PostScript's b(ig) p(oint).");
206 tooltips().init(bbox_->button_getBB, str);
208 str = _("Clip image to the bounding box values.");
209 tooltips().init(bbox_->check_clip, str);
212 extra_.reset(build_graphics_extra(this));
214 // disable for read-only documents
215 bc().addReadOnly(extra_->input_rotate_angle);
216 bc().addReadOnly(extra_->choice_origin);
217 bc().addReadOnly(extra_->check_subcaption);
218 bc().addReadOnly(extra_->input_special);
220 // trigger an input event for cut&paste with middle mouse button.
221 setPrehandler(extra_->input_rotate_angle);
222 setPrehandler(extra_->input_subcaption);
223 setPrehandler(extra_->input_special);
225 fl_set_input_return(extra_->input_rotate_angle, FL_RETURN_CHANGED);
226 fl_set_input_return(extra_->input_subcaption, FL_RETURN_CHANGED);
227 fl_set_input_return(extra_->input_special, FL_RETURN_CHANGED);
229 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
231 using namespace frnt;
232 vector<RotationOriginPair> origindata = getRotationOriginData();
234 // Store the identifiers for later
235 origins_ = getSecond(origindata);
237 string const choice = "Default|" + getStringFromVector(getFirst(origindata), "|");
238 fl_addto_choice(extra_->choice_origin, choice.c_str());
240 // set up the tooltips for the extra section
241 str = _("Insert the rotation angle in degrees. "
242 "Positive value rotates anti-clockwise, "
243 "negative value clockwise.");
244 tooltips().init(extra_->input_rotate_angle, str);
245 str = _("Insert the point of origin for rotation.");
246 tooltips().init(extra_->choice_origin, str);
247 str = _("Enables use of subfigure with its own caption.");
248 tooltips().init(extra_->check_subcaption, str);
249 str = _("Insert the optional subfigure caption.");
250 tooltips().init(extra_->input_subcaption, str);
251 str = _("Add any additional latex option, which is defined in the "
252 "graphicx-package and not mentioned in the gui's tabfolders.");
253 tooltips().init(extra_->input_special, str);
255 // add the different tabfolders
256 fl_addto_tabfolder(dialog_->tabfolder, _("File"), file_->form);
257 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box"), bbox_->form);
258 fl_addto_tabfolder(dialog_->tabfolder, _("Extra"), extra_->form);
260 // work-around xforms bug re update of folder->x, folder->y coords.
261 setPrehandler(dialog_->tabfolder);
263 // set the right default unit
264 switch (lyxrc.default_papersize) {
265 case BufferParams::PAPER_DEFAULT: break;
266 case BufferParams::PAPER_USLETTER:
267 case BufferParams::PAPER_LEGALPAPER:
268 case BufferParams::PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
269 case BufferParams::PAPER_A3PAPER:
270 case BufferParams::PAPER_A4PAPER:
271 case BufferParams::PAPER_A5PAPER:
272 case BufferParams::PAPER_B5PAPER: defaultUnit = "cm"; break;
277 void FormGraphics::apply()
279 // Create the parameters structure and fill the data from the dialog.
280 InsetGraphicsParams & igp = controller().params();
283 igp.filename = getString(file_->input_filename);
285 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
286 if (igp.lyxscale == 0) {
290 switch (fl_get_choice(file_->choice_display)) {
292 igp.display = grfx::NoDisplay;
295 igp.display = grfx::ColorDisplay;
298 igp.display = grfx::GrayscaleDisplay;
301 igp.display = grfx::MonochromeDisplay;
304 igp.display = grfx::DefaultDisplay;
307 // first item in choice_width means scaling
308 if (fl_get_choice(file_->choice_width) == 1) {
309 igp.scale = strToDbl(getString(file_->input_width));
310 if (lyx::float_equal(igp.scale, 0.0, 0.05)) {
313 igp.width = LyXLength();
316 igp.width = LyXLength(getLengthFromWidgets(file_->input_width,
317 file_->choice_width));
319 igp.height = LyXLength(getLengthFromWidgets(file_->input_height,
320 file_->choice_height));
321 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
323 igp.draft = fl_get_button(file_->check_draft);
324 igp.noUnzip = fl_get_button(file_->check_nounzip);
327 if (!controller().bbChanged) {
328 // don't write anything
331 // allow length + unit input only for x1 input field
333 if (!getString(bbox_->input_bb_x1).empty()) {
334 x1_str = getLengthFromWidgets(bbox_->input_bb_x1,
335 bbox_->choice_bb_units);
336 LyXLength x1 = LyXLength(x1_str);
337 x1_str = x1.asString();
354 fl_set_choice_text(bbox_->choice_bb_units, unit.c_str());
358 if (getString(bbox_->input_bb_x0).empty())
361 bb = getLengthFromWidgets(bbox_->input_bb_x0,
362 bbox_->choice_bb_units);
366 if (getString(bbox_->input_bb_y0).empty())
369 bb += getLengthFromWidgets(bbox_->input_bb_y0,
370 bbox_->choice_bb_units);
372 bb += " " + x1_str + " ";
374 if (getString(bbox_->input_bb_y1).empty())
377 bb += getLengthFromWidgets(bbox_->input_bb_y1,
378 bbox_->choice_bb_units);
382 igp.clip = fl_get_button(bbox_->check_clip);
385 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
387 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
388 while (igp.rotateAngle <= -360.0) {
389 igp.rotateAngle += 360.0;
391 while (igp.rotateAngle >= 360.0) {
392 igp.rotateAngle -= 360.0;
394 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
396 int const origin_pos = fl_get_choice(extra_->choice_origin);
397 if (origin_pos == 1) {
398 igp.rotateOrigin.erase();
400 igp.rotateOrigin = origins_[origin_pos - 2];
403 igp.subcaption = fl_get_button(extra_->check_subcaption);
404 igp.subcaptionText = getString(extra_->input_subcaption);
406 igp.special = getString(extra_->input_special);
410 void FormGraphics::update() {
411 // Update dialog with details from inset
412 InsetGraphicsParams & igp = controller().params();
415 fl_set_input(file_->input_filename, igp.filename.c_str());
416 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
418 switch (igp.display) {
419 case grfx::NoDisplay:
420 fl_set_choice(file_->choice_display, 5);
422 case grfx::ColorDisplay:
423 fl_set_choice(file_->choice_display, 4);
425 case grfx::GrayscaleDisplay:
426 fl_set_choice(file_->choice_display, 3);
428 case grfx::MonochromeDisplay:
429 fl_set_choice(file_->choice_display, 2);
431 case grfx::DefaultDisplay:
432 fl_set_choice(file_->choice_display, 1);
435 // set width input fields according to scaling or width/height input
436 if (!lyx::float_equal(igp.scale, 0.0, 0.05)) {
437 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
438 fl_set_input_maxchars(file_->input_width, 0);
439 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
440 fl_set_choice(file_->choice_width, 1);
442 fl_set_input_filter(file_->input_width, NULL);
443 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
444 updateWidgetsFromLength(file_->input_width, file_->choice_width,
445 igp.width, defaultUnit);
448 updateWidgetsFromLength(file_->input_height, file_->choice_height,
449 igp.height, defaultUnit);
451 // disable height input in case of scaling
452 bool const disable_height = !lyx::float_equal(igp.scale, 0.0, 0.05);
453 setEnabled(file_->input_height, !disable_height);
454 setEnabled(file_->choice_height, !disable_height);
456 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
457 fl_set_button(file_->check_draft, igp.draft);
458 fl_set_button(file_->check_nounzip, igp.noUnzip);
460 // disable aspectratio button in case of scaling or one of width/height
462 bool const disable_aspectRatio = disable_height ||
463 getString(file_->input_width).empty() ||
464 getString(file_->input_height).empty();
465 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
468 // set the bounding box values, if exists. First we need the whole
469 // path, because the controller knows nothing about the doc-dir
470 updateBB(igp.filename, igp.bb);
471 fl_set_button(bbox_->check_clip, igp.clip);
474 fl_set_input(extra_->input_rotate_angle,
475 tostr(igp.rotateAngle).c_str());
478 if (igp.rotateOrigin.empty()) {
481 origin_pos = 2 + findPos(origins_, igp.rotateOrigin);
483 fl_set_choice(extra_->choice_origin, origin_pos);
485 fl_set_button(extra_->check_subcaption, igp.subcaption);
486 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
487 setEnabled(extra_->input_subcaption,
488 fl_get_button(extra_->check_subcaption));
489 fl_set_input(extra_->input_special, igp.special.c_str());
491 // open dialog in the file-tab, whenever filename is empty
492 if (igp.filename.empty()) {
493 fl_set_folder(dialog_->tabfolder, file_->form);
498 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
500 // Update dialog with details from inset
501 // set the bounding box values, if exists. First we need the whole
502 // path, because the controller knows nothing about the doc-dir
503 controller().bbChanged = false;
504 if (bb_inset.empty()) {
505 lyxerr[Debug::GRAPHICS]
506 << "FormGraphics::updateBB() [no BoundingBox]" << endl;
507 string const bb = controller().readBB(filename);
509 // get the values from the file
510 // in this case we always have the point-unit
511 fl_set_input(bbox_->input_bb_x0,
512 token(bb,' ',0).c_str());
513 fl_set_input(bbox_->input_bb_y0,
514 token(bb,' ',1).c_str());
515 fl_set_input(bbox_->input_bb_x1,
516 token(bb,' ',2).c_str());
517 fl_set_input(bbox_->input_bb_y1,
518 token(bb,' ',3).c_str());
522 fl_set_input(bbox_->input_bb_x0, bb.c_str());
523 fl_set_input(bbox_->input_bb_y0, bb.c_str());
524 fl_set_input(bbox_->input_bb_x1, bb.c_str());
525 fl_set_input(bbox_->input_bb_y1, bb.c_str());
528 fl_set_choice(bbox_->choice_bb_units, 1);
531 // get the values from the inset
532 lyxerr[Debug::GRAPHICS]
533 << "FormGraphics::updateBB(): igp has BoundingBox"
534 << " ["<< bb_inset << "]" << endl;
535 controller().bbChanged = true;
538 anyLength = LyXLength(token(bb_inset,' ',0));
539 updateWidgetsFromLength(bbox_->input_bb_x0,
540 bbox_->choice_bb_units,anyLength,"bp");
541 anyLength = LyXLength(token(bb_inset,' ',1));
542 updateWidgetsFromLength(bbox_->input_bb_y0,
543 bbox_->choice_bb_units,anyLength,"bp");
544 anyLength = LyXLength(token(bb_inset,' ',2));
545 updateWidgetsFromLength(bbox_->input_bb_x1,
546 bbox_->choice_bb_units,anyLength,"bp");
547 anyLength = LyXLength(token(bb_inset,' ',3));
548 updateWidgetsFromLength(bbox_->input_bb_y1,
549 bbox_->choice_bb_units,anyLength,"bp");
554 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
557 if (ob == file_->button_browse) {
558 // Get the filename from the dialog
559 string const in_name = getString(file_->input_filename);
560 string const out_name = controller().Browse(in_name);
561 lyxerr[Debug::GRAPHICS]
562 << "[FormGraphics]out_name: " << out_name << endl;
563 if (out_name != in_name && !out_name.empty()) {
564 fl_set_input(file_->input_filename, out_name.c_str());
566 if (controller().isFilenameValid(out_name) &&
567 !controller().bbChanged) {
568 updateBB(out_name, string());
570 } else if (ob == file_->input_width || ob == file_->input_height) {
571 // disable aspectratio button in case of scaling or one of
572 // width/height is empty
573 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
574 getString(file_->input_width).empty() ||
575 getString(file_->input_height).empty();
576 setEnabled(file_->check_aspectratio, !disable);
577 } else if (ob == file_->choice_width) {
578 // disable height input in case of scaling
579 bool const scaling = fl_get_choice(file_->choice_width) == 1;
580 setEnabled(file_->input_height, !scaling);
581 setEnabled(file_->choice_height, !scaling);
583 // allow only integer intput for scaling; float otherwise
585 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
586 fl_set_input_maxchars(file_->input_width, 0);
588 fl_set_input_filter(file_->input_width, NULL);
589 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
592 // disable aspectratio button in case of scaling or height
594 bool const disable_aspectratio =
595 scaling || getString(file_->input_height).empty();
596 setEnabled(file_->check_aspectratio, !disable_aspectratio);
598 } else if (!controller().bbChanged &&
599 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
600 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
601 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
602 controller().bbChanged = true;
603 } else if (ob == bbox_->button_getBB) {
604 string const filename = getString(file_->input_filename);
605 if (!filename.empty()) {
606 string bb = controller().readBB(filename);
608 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
609 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
610 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
611 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
612 fl_set_choice_text(bbox_->choice_bb_units, "bp");
614 controller().bbChanged = false;
616 fl_set_input(bbox_->input_bb_x0, "");
617 fl_set_input(bbox_->input_bb_y0, "");
618 fl_set_input(bbox_->input_bb_x1, "");
619 fl_set_input(bbox_->input_bb_y1, "");
620 fl_set_choice_text(bbox_->choice_bb_units, "bp");
623 } else if (ob == extra_->check_subcaption) {
624 setEnabled(extra_->input_subcaption,
625 fl_get_button(extra_->check_subcaption));
629 return ButtonPolicy::SMI_VALID;