3 * See the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
18 #include "ControlGraphics.h"
19 #include "FormGraphics.h"
20 #include "forms/form_graphics.h"
24 #include "xforms_helpers.h"
25 #include "helper_funcs.h"
26 #include "input_validators.h"
27 #include "debug.h" // for lyxerr
28 #include "support/lstrings.h" // for strToDbl & tostr
29 #include "support/filetools.h" // for MakeAbsPath etc
30 #include "insets/insetgraphicsParams.h"
31 #include "lyxrc.h" // for lyxrc.display_graphics
32 #include FORMS_H_LOCATION
39 // Bound the number of input characters
40 int const SIZE_MAXDIGITS = 10;
41 int const FILENAME_MAXCHARS = 1024;
43 string defaultUnit("cm");
45 /// Given input and choice widgets, create a LyXLength
46 LyXLength getLyXLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
48 return LyXLength(getLengthFromWidgets(input, choice));
54 typedef FormCB<ControlGraphics, FormDB<FD_graphics> > base_class;
56 FormGraphics::FormGraphics()
57 : base_class(_("Graphics"), false)
61 void FormGraphics::redraw()
63 if (form() && form()->visible)
64 fl_redraw_form(form());
67 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
68 if (outer_form && outer_form->visible)
69 fl_redraw_form(outer_form);
73 void FormGraphics::build()
75 dialog_.reset(build_graphics(this));
77 // Allow the base class to control messages
78 setMessageWidget(dialog_->text_warning);
80 // Manage the ok, apply, restore and cancel/close buttons
81 bc().setOK(dialog_->button_ok);
82 bc().setApply(dialog_->button_apply);
83 bc().setCancel(dialog_->button_close);
84 bc().setRestore(dialog_->button_restore);
87 file_.reset(build_graphics_file(this));
89 fl_set_input_return (file_->input_filename, FL_RETURN_CHANGED);
90 fl_set_input_return (file_->input_lyxscale, FL_RETURN_CHANGED);
91 fl_set_input_return (file_->input_width, FL_RETURN_CHANGED);
92 fl_set_input_return (file_->input_height, FL_RETURN_CHANGED);
94 setPrehandler(file_->input_filename);
95 setPrehandler(file_->input_lyxscale);
96 setPrehandler(file_->input_width);
97 setPrehandler(file_->input_height);
99 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
100 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
102 // width default is scaling, thus unsigned integer input
103 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
104 fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
107 string const display_List = _("Default|Monochrome|Grayscale|Color|Do not display");
108 fl_addto_choice(file_->choice_display, display_List.c_str());
110 string const width_list = _("Scale%%|") + choice_Length_All;
111 fl_addto_choice(file_->choice_width, width_list.c_str());
113 fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
115 bc().addReadOnly(file_->button_browse);
116 bc().addReadOnly(file_->check_aspectratio);
117 bc().addReadOnly(file_->check_draft);
118 bc().addReadOnly(file_->check_nounzip);
120 // set up the tooltips for the filesection
121 string str = _("The file you want to insert.");
122 tooltips().init(file_->input_filename, str);
123 str = _("Browse the directories.");
124 tooltips().init(file_->button_browse, str);
126 str = _("Scale the image to inserted percentage value");
127 tooltips().init(file_->input_lyxscale, str);
128 str = _("Select display mode for this image.");
129 tooltips().init(file_->choice_display, str);
131 str = _("Set the image width to the inserted value.");
132 tooltips().init(file_->input_width, str);
133 str = _("Select unit for width; Scale% for scaling whole image");
134 tooltips().init(file_->choice_width, str);
135 str = _("Set the image height to the inserted value.");
136 tooltips().init(file_->input_height, str);
137 str = _("Select unit for height");
138 tooltips().init(file_->choice_height, str);
139 str = _("Do not distort the image. "
140 "Keep image within \"width\" by \"height\" and obey aspect ratio.");
141 tooltips().init(file_->check_aspectratio, str);
143 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
144 "This is useful when LaTeX should unzip the file. Needs an additional file "
145 "like \"file.eps.bb\" which holds the values for the bounding box.");
146 tooltips().init(file_->check_nounzip, str);
148 str = _("Show image only as a rectangle of the original size.");
149 tooltips().init(file_->check_draft, str);
151 // the bounding box selection
152 bbox_.reset(build_graphics_bbox(this));
153 fl_set_input_return (bbox_->input_bb_x0, FL_RETURN_CHANGED);
154 fl_set_input_return (bbox_->input_bb_y0, FL_RETURN_CHANGED);
155 fl_set_input_return (bbox_->input_bb_x1, FL_RETURN_CHANGED);
156 fl_set_input_return (bbox_->input_bb_y1, FL_RETURN_CHANGED);
158 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
159 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
160 fl_set_input_filter(bbox_->input_bb_x1, fl_unsigned_float_filter);
161 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
163 setPrehandler(bbox_->input_bb_x0);
164 setPrehandler(bbox_->input_bb_y0);
165 setPrehandler(bbox_->input_bb_x1);
166 setPrehandler(bbox_->input_bb_y1);
168 string const bb_units = "bp|cm|mm|in";
169 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
170 bc().addReadOnly(bbox_->button_getBB);
171 bc().addReadOnly(bbox_->check_clip);
173 // set up the tooltips for the bounding-box-section
174 str = _("The lower left x-value of the bounding box");
175 tooltips().init(bbox_->input_bb_x0, str);
176 str = _("The lower left y-value of the bounding box");
177 tooltips().init(bbox_->input_bb_y0, str);
178 str = _("The upper right x-value of the bounding box");
179 tooltips().init(bbox_->input_bb_x1, str);
180 str = _("The upper right y-value of the bounding box");
181 tooltips().init(bbox_->input_bb_y1, str);
182 str = _("Select unit for the bounding box values");
183 tooltips().init(bbox_->choice_bb_units, str);
185 str = _("Read the image coordinates new from file. If it's an (e)ps-file "
186 "then the bounding box is read otherwise the imagesize in pixels. "
187 "The default unit is \"bp\", the PostScript's b(ig) p(oint).");
188 tooltips().init(bbox_->button_getBB, str);
190 str = _("Enable this checkbox when the image should be clipped to the "
191 "bounding box values.");
192 tooltips().init(bbox_->check_clip, str);
195 extra_.reset(build_graphics_extra(this));
197 fl_set_input_return (extra_->input_rotate_angle, FL_RETURN_CHANGED);
198 fl_set_input_return (extra_->input_subcaption, FL_RETURN_CHANGED);
199 fl_set_input_return (extra_->input_special, FL_RETURN_CHANGED);
201 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
203 setPrehandler(extra_->input_rotate_angle);
204 setPrehandler(extra_->input_subcaption);
205 setPrehandler(extra_->input_special);
207 bc().addReadOnly(extra_->check_subcaption);
209 using namespace frnt;
210 vector<RotationOriginPair> origindata = getRotationOriginData();
212 // Store the identifiers for later
213 origins_ = getSecond(origindata);
215 string const choice = " " + getStringFromVector(getFirst(origindata), " | ") + " ";
216 fl_addto_choice(extra_->choice_origin, choice.c_str());
218 // set up the tooltips for the extra section
219 str = _("Insert the rotation angle in degrees. "
220 "Positive value rotates anti-clockwise, negative value clockwise");
221 tooltips().init(extra_->input_rotate_angle, str);
222 str = _("Insert the point of origin for rotation ");
223 tooltips().init(extra_->choice_origin, str);
224 str = _("Enables use of subfigure with its own caption.");
225 tooltips().init(extra_->check_subcaption, str);
226 str = _("Insert the optional subfigure caption");
227 tooltips().init(extra_->input_subcaption, str);
228 str = _("Add any additional latex option, which is defined in the "
229 "graphicx-package and not mentioned in the gui's tabfolders.");
230 tooltips().init(extra_->input_special, str);
232 // add the different tabfolders
233 fl_addto_tabfolder(dialog_->tabfolder, _("File"), file_->form);
234 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box"), bbox_->form);
235 fl_addto_tabfolder(dialog_->tabfolder, _("Extra"), extra_->form);
237 // set the right default unit
238 switch (lyxrc.default_papersize) {
239 case BufferParams::PAPER_DEFAULT: break;
240 case BufferParams::PAPER_USLETTER:
241 case BufferParams::PAPER_LEGALPAPER:
242 case BufferParams::PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
243 case BufferParams::PAPER_A3PAPER:
244 case BufferParams::PAPER_A4PAPER:
245 case BufferParams::PAPER_A5PAPER:
246 case BufferParams::PAPER_B5PAPER: defaultUnit = "cm"; break;
251 void FormGraphics::apply()
253 // Create the parameters structure and fill the data from the dialog.
254 InsetGraphicsParams & igp = controller().params();
257 igp.filename = getString(file_->input_filename);
259 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
260 if (igp.lyxscale == 0) {
264 switch (fl_get_choice(file_->choice_display)) {
265 case 5: igp.display = grfx::NoDisplay; break;
266 case 4: igp.display = grfx::ColorDisplay; break;
267 case 3: igp.display = grfx::GrayscaleDisplay; break;
268 case 2: igp.display = grfx::MonochromeDisplay; break;
270 default: igp.display = grfx::DefaultDisplay;
273 // first item in choice_width means scaling
274 if (fl_get_choice(file_->choice_width) == 1) {
275 igp.scale = strToInt(getString(file_->input_width));
276 if (igp.scale == 0) {
279 igp.width = LyXLength();
282 igp.width = getLyXLengthFromWidgets(file_->input_width,
283 file_->choice_width);
285 igp.height = getLyXLengthFromWidgets(file_->input_height,
286 file_->choice_height);
287 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
289 igp.draft = fl_get_button(file_->check_draft);
290 igp.noUnzip = fl_get_button(file_->check_nounzip);
293 if (!controller().bbChanged) { // different to the original one?
294 igp.bb = string(); // don't write anything
297 if (getString(bbox_->input_bb_x0).empty())
300 bb = getLengthFromWidgets(bbox_->input_bb_x0,
301 bbox_->choice_bb_units)+" ";
302 if (getString(bbox_->input_bb_y0).empty())
305 bb += (getLengthFromWidgets(bbox_->input_bb_y0,
306 bbox_->choice_bb_units)+" ");
307 if (getString(bbox_->input_bb_x1).empty())
310 bb += (getLengthFromWidgets(bbox_->input_bb_x1,
311 bbox_->choice_bb_units)+" ");
312 if (getString(bbox_->input_bb_y1).empty())
315 bb += (getLengthFromWidgets(bbox_->input_bb_y1,
316 bbox_->choice_bb_units)+" ");
319 igp.clip = fl_get_button(bbox_->check_clip);
322 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
324 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
325 while (igp.rotateAngle <= -360.0) {
326 igp.rotateAngle += 360.0;
328 while (igp.rotateAngle >= 360.0) {
329 igp.rotateAngle -= 360.0;
331 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
333 int const origin_pos = fl_get_choice(extra_->choice_origin);
334 igp.rotateOrigin = origins_[origin_pos-1];
336 igp.subcaption = fl_get_button(extra_->check_subcaption);
337 igp.subcaptionText = getString(extra_->input_subcaption);
339 igp.special = getString(extra_->input_special);
343 void FormGraphics::update() {
344 // Update dialog with details from inset
345 InsetGraphicsParams & igp = controller().params();
348 fl_set_input(file_->input_filename, igp.filename.c_str());
349 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
351 switch (igp.display) {
352 case grfx::NoDisplay: fl_set_choice(file_->choice_display, 5); break;
353 case grfx::ColorDisplay: fl_set_choice(file_->choice_display, 4); break;
354 case grfx::GrayscaleDisplay: fl_set_choice(file_->choice_display, 3); break;
355 case grfx::MonochromeDisplay: fl_set_choice(file_->choice_display, 2); break;
356 case grfx::DefaultDisplay:
357 default: fl_set_choice(file_->choice_display, 1);
360 // disable height input in case of scaling
361 setEnabled(file_->input_height, !igp.scale);
362 setEnabled(file_->choice_height, !igp.scale);
364 // set width input fields according to scaling or width/height input
366 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
367 fl_set_input_maxchars(file_->input_width, 0);
368 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
369 fl_set_choice(file_->choice_width, 1);
371 fl_set_input_filter(file_->input_width, NULL);
372 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
373 updateWidgetsFromLength(file_->input_width,
374 file_->choice_width, igp.width, defaultUnit);
377 updateWidgetsFromLength(file_->input_height,
378 file_->choice_height, igp.height, defaultUnit);
380 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
381 fl_set_button(file_->check_draft, igp.draft);
382 fl_set_button(file_->check_nounzip, igp.noUnzip);
384 // disable aspectratio button in case of scaling or one of width/height is empty
385 bool const disable_aspectRatio = igp.scale ||
386 getString(file_->input_width).empty() ||
387 getString(file_->input_height).empty();
388 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
391 // set the bounding box values, if exists. First we need the whole
392 // path, because the controller knows nothing about the doc-dir
393 updateBB(igp.filename, igp.bb);
394 fl_set_button(bbox_->check_clip, igp.clip);
398 fl_set_input(extra_->input_rotate_angle,
399 tostr(igp.rotateAngle).c_str());
401 int const origin_pos = findPos(origins_, igp.rotateOrigin);
402 fl_set_choice(extra_->choice_origin, 1 + origin_pos);
404 fl_set_button(extra_->check_subcaption, igp.subcaption);
405 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
406 setEnabled(extra_->input_subcaption,
407 fl_get_button(extra_->check_subcaption));
408 fl_set_input(extra_->input_special, igp.special.c_str());
410 // open dialog in the file-tab, whenever filename is empty
411 if (igp.filename.empty()) {
412 fl_set_folder(dialog_->tabfolder, file_->form);
417 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
419 // Update dialog with details from inset
420 // set the bounding box values, if exists. First we need the whole
421 // path, because the controller knows nothing about the doc-dir
422 controller().bbChanged = false;
423 if (bb_inset.empty()) {
424 lyxerr[Debug::GRAPHICS] << "FormGraphics::updateBB() [no BoundingBox]" << endl;
425 string const bb = controller().readBB(filename);
427 // get the values from the file
428 // in this case we always have the point-unit
429 fl_set_input(bbox_->input_bb_x0,
430 token(bb,' ',0).c_str());
431 fl_set_input(bbox_->input_bb_y0,
432 token(bb,' ',1).c_str());
433 fl_set_input(bbox_->input_bb_x1,
434 token(bb,' ',2).c_str());
435 fl_set_input(bbox_->input_bb_y1,
436 token(bb,' ',3).c_str());
440 fl_set_input(bbox_->input_bb_x0, bb.c_str());
441 fl_set_input(bbox_->input_bb_y0, bb.c_str());
442 fl_set_input(bbox_->input_bb_x1, bb.c_str());
443 fl_set_input(bbox_->input_bb_y1, bb.c_str());
446 fl_set_choice(bbox_->choice_bb_units, 1);
449 // get the values from the inset
450 lyxerr[Debug::GRAPHICS] << "FormGraphics::updateBB(): igp has BoundingBox"
451 << " ["<< bb_inset << "]"
453 controller().bbChanged = true;
456 anyLength = LyXLength(token(bb_inset,' ',0));
457 updateWidgetsFromLength(bbox_->input_bb_x0,
458 bbox_->choice_bb_units,anyLength,"bp");
459 anyLength = LyXLength(token(bb_inset,' ',1));
460 updateWidgetsFromLength(bbox_->input_bb_y0,
461 bbox_->choice_bb_units,anyLength,"bp");
462 anyLength = LyXLength(token(bb_inset,' ',2));
463 updateWidgetsFromLength(bbox_->input_bb_x1,
464 bbox_->choice_bb_units,anyLength,"bp");
465 anyLength = LyXLength(token(bb_inset,' ',3));
466 updateWidgetsFromLength(bbox_->input_bb_y1,
467 bbox_->choice_bb_units,anyLength,"bp");
474 bool isValid(FL_OBJECT * ob)
476 string const input = getString(ob);
477 return input.empty() || isValidLength(input) || isStrDbl(input);
484 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
487 if (ob == file_->button_browse) {
488 // Get the filename from the dialog
489 string const in_name = getString(file_->input_filename);
490 string const out_name = controller().Browse(in_name);
491 lyxerr[Debug::GRAPHICS] << "[FormGraphics]out_name: " << out_name << endl;
492 if (out_name != in_name && !out_name.empty()) {
493 fl_set_input(file_->input_filename, out_name.c_str());
495 if (controller().isFilenameValid(out_name) &&
496 !controller().bbChanged) {
497 updateBB(out_name, string());
499 } else if (ob == file_->input_width || ob == file_->input_height) {
500 // disable aspectratio button in case of scaling or one of width/height is empty
501 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
502 getString(file_->input_width).empty() ||
503 getString(file_->input_height).empty();
504 setEnabled(file_->check_aspectratio, !disable);
505 } else if (ob == file_->choice_width) {
506 // disable height input in case of scaling
507 bool const scaling = fl_get_choice(file_->choice_width) == 1;
508 setEnabled(file_->input_height, !scaling);
509 setEnabled(file_->choice_height, !scaling);
511 // allow only integer intput for scaling; float otherwise
513 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
514 fl_set_input_maxchars(file_->input_width, 0);
516 fl_set_input_filter(file_->input_width, NULL);
517 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
520 // disable aspectratio button in case of scaling or height input is empty
521 bool const disable_aspectratio = scaling || getString(file_->input_height).empty();
522 setEnabled(file_->check_aspectratio, !disable_aspectratio);
524 } else if (!controller().bbChanged &&
525 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
526 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
527 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
528 controller().bbChanged = true;
529 } else if (ob == bbox_->button_getBB) {
530 string const filename = getString(file_->input_filename);
531 if (!filename.empty()) {
532 string bb = controller().readBB(filename);
534 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
535 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
536 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
537 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
538 fl_set_choice_text(bbox_->choice_bb_units, "bp");
540 controller().bbChanged = false;
542 fl_set_input(bbox_->input_bb_x0, "");
543 fl_set_input(bbox_->input_bb_y0, "");
544 fl_set_input(bbox_->input_bb_x1, "");
545 fl_set_input(bbox_->input_bb_y1, "");
546 fl_set_choice_text(bbox_->choice_bb_units, "bp");
549 } else if (ob == extra_->check_subcaption) {
550 setEnabled(extra_->input_subcaption,
551 fl_get_button(extra_->check_subcaption));
555 // check if the input is valid
556 bool const invalid = !isValid(file_->input_width) || !isValid(file_->input_height);
558 // deactivate OK / Apply buttons and spit out warnings if invalid
560 postWarning(_("Invalid Length in Output size!"));
561 return ButtonPolicy::SMI_INVALID;
564 return ButtonPolicy::SMI_VALID;