3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Baruch Even, baruch.even@writeme.com
7 * \author Herbert Voss, voss@perce.de
13 #pragma implementation
17 #include "ControlGraphics.h"
18 #include "FormGraphics.h"
19 #include "forms/form_graphics.h"
23 #include "xforms_helpers.h"
24 #include "helper_funcs.h"
25 #include "input_validators.h"
26 #include "debug.h" // for lyxerr
27 #include "support/lstrings.h" // for strToDbl & tostr
28 #include "support/filetools.h" // for MakeAbsPath etc
29 #include "insets/insetgraphicsParams.h"
30 #include "lyxrc.h" // for lyxrc.display_graphics
31 #include FORMS_H_LOCATION
38 // Bound the number of input characters
39 int const FILENAME_MAXCHARS = 1024;
41 string defaultUnit("cm");
43 /// Given input and choice widgets, create a LyXLength
44 LyXLength getLyXLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
46 return LyXLength(getLengthFromWidgets(input, choice));
52 typedef FormCB<ControlGraphics, FormDB<FD_graphics> > base_class;
54 FormGraphics::FormGraphics()
55 : base_class(_("Graphics"), false)
59 void FormGraphics::redraw()
61 if (form() && form()->visible)
62 fl_redraw_form(form());
65 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
66 if (outer_form && outer_form->visible)
67 fl_redraw_form(outer_form);
71 void FormGraphics::build()
73 dialog_.reset(build_graphics(this));
75 // Allow the base class to control messages
76 setMessageWidget(dialog_->text_warning);
78 // Manage the ok, apply, restore and cancel/close buttons
79 bc().setOK(dialog_->button_ok);
80 bc().setApply(dialog_->button_apply);
81 bc().setCancel(dialog_->button_close);
82 bc().setRestore(dialog_->button_restore);
85 file_.reset(build_graphics_file(this));
87 fl_set_input_return (file_->input_filename, FL_RETURN_CHANGED);
88 fl_set_input_return (file_->input_lyxscale, FL_RETURN_CHANGED);
89 fl_set_input_return (file_->input_width, FL_RETURN_CHANGED);
90 fl_set_input_return (file_->input_height, FL_RETURN_CHANGED);
92 setPrehandler(file_->input_filename);
93 setPrehandler(file_->input_lyxscale);
94 setPrehandler(file_->input_width);
95 setPrehandler(file_->input_height);
97 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
98 fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
100 // width default is scaling, thus unsigned integer input
101 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
102 fl_set_input_filter(file_->input_height, fl_unsigned_float_filter);
104 fl_addto_choice(file_->choice_display, _("Default|Monochrome|Grayscale|Color|Do not display"));
105 fl_addto_choice(file_->choice_width, (_("Scale%%|") + choice_Length_All).c_str());
106 fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
108 bc().addReadOnly(file_->button_browse);
109 bc().addReadOnly(file_->check_aspectratio);
110 bc().addReadOnly(file_->check_draft);
111 bc().addReadOnly(file_->check_nounzip);
113 // set up the tooltips for the filesection
114 string str = _("The file you want to insert.");
115 tooltips().init(file_->input_filename, str);
116 str = _("Browse the directories.");
117 tooltips().init(file_->button_browse, str);
119 str = _("Scale the image to inserted percentage value");
120 tooltips().init(file_->input_lyxscale, str);
121 str = _("Select display mode for this image.");
122 tooltips().init(file_->choice_display, str);
124 str = _("Set the image width to the inserted value.");
125 tooltips().init(file_->input_width, str);
126 str = _("Select unit for width; Scale% for scaling whole image");
127 tooltips().init(file_->choice_width, str);
128 str = _("Set the image height to the inserted value.");
129 tooltips().init(file_->input_height, str);
130 str = _("Select unit for height");
131 tooltips().init(file_->choice_height, str);
132 str = _("Do not distort the image. "
133 "Keep image within \"width\" by \"height\" and obey aspect ratio.");
134 tooltips().init(file_->check_aspectratio, str);
136 str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
137 "This is useful when LaTeX should unzip the file. Needs an additional file "
138 "like \"file.eps.bb\" which holds the values for the bounding box.");
139 tooltips().init(file_->check_nounzip, str);
141 str = _("Show image only as a rectangle of the original size.");
142 tooltips().init(file_->check_draft, str);
144 // the bounding box selection
145 bbox_.reset(build_graphics_bbox(this));
146 fl_set_input_return (bbox_->input_bb_x0, FL_RETURN_CHANGED);
147 fl_set_input_return (bbox_->input_bb_y0, FL_RETURN_CHANGED);
148 fl_set_input_return (bbox_->input_bb_x1, FL_RETURN_CHANGED);
149 fl_set_input_return (bbox_->input_bb_y1, FL_RETURN_CHANGED);
151 fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
152 fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
153 fl_set_input_filter(bbox_->input_bb_x1, fl_unsigned_float_filter);
154 fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
156 setPrehandler(bbox_->input_bb_x0);
157 setPrehandler(bbox_->input_bb_y0);
158 setPrehandler(bbox_->input_bb_x1);
159 setPrehandler(bbox_->input_bb_y1);
161 string const bb_units = "bp|cm|mm|in";
162 fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
163 bc().addReadOnly(bbox_->button_getBB);
164 bc().addReadOnly(bbox_->check_clip);
166 // set up the tooltips for the bounding-box-section
167 str = _("The lower left x-value of the bounding box");
168 tooltips().init(bbox_->input_bb_x0, str);
169 str = _("The lower left y-value of the bounding box");
170 tooltips().init(bbox_->input_bb_y0, str);
171 str = _("The upper right x-value of the bounding box");
172 tooltips().init(bbox_->input_bb_x1, str);
173 str = _("The upper right y-value of the bounding box");
174 tooltips().init(bbox_->input_bb_y1, str);
175 str = _("Select unit for the bounding box values");
176 tooltips().init(bbox_->choice_bb_units, str);
178 str = _("Read the image coordinates new from file. If it's an (e)ps-file "
179 "then the bounding box is read otherwise the imagesize in pixels. "
180 "The default unit is \"bp\", the PostScript's b(ig) p(oint).");
181 tooltips().init(bbox_->button_getBB, str);
183 str = _("Enable this checkbox when the image should be clipped to the "
184 "bounding box values.");
185 tooltips().init(bbox_->check_clip, str);
188 extra_.reset(build_graphics_extra(this));
190 fl_set_input_return (extra_->input_rotate_angle, FL_RETURN_CHANGED);
191 fl_set_input_return (extra_->input_subcaption, FL_RETURN_CHANGED);
192 fl_set_input_return (extra_->input_special, FL_RETURN_CHANGED);
194 fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
196 setPrehandler(extra_->input_rotate_angle);
197 setPrehandler(extra_->input_subcaption);
198 setPrehandler(extra_->input_special);
200 bc().addReadOnly(extra_->check_subcaption);
202 using namespace frnt;
203 vector<RotationOriginPair> origindata = getRotationOriginData();
205 // Store the identifiers for later
206 origins_ = getSecond(origindata);
208 string const choice = " " + getStringFromVector(getFirst(origindata), " | ") + " ";
209 fl_addto_choice(extra_->choice_origin, choice.c_str());
211 // set up the tooltips for the extra section
212 str = _("Insert the rotation angle in degrees. "
213 "Positive value rotates anti-clockwise, negative value clockwise");
214 tooltips().init(extra_->input_rotate_angle, str);
215 str = _("Insert the point of origin for rotation ");
216 tooltips().init(extra_->choice_origin, str);
217 str = _("Enables use of subfigure with its own caption.");
218 tooltips().init(extra_->check_subcaption, str);
219 str = _("Insert the optional subfigure caption");
220 tooltips().init(extra_->input_subcaption, str);
221 str = _("Add any additional latex option, which is defined in the "
222 "graphicx-package and not mentioned in the gui's tabfolders.");
223 tooltips().init(extra_->input_special, str);
225 // add the different tabfolders
226 fl_addto_tabfolder(dialog_->tabfolder, _("File"), file_->form);
227 fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box"), bbox_->form);
228 fl_addto_tabfolder(dialog_->tabfolder, _("Extra"), extra_->form);
230 // set the right default unit
231 switch (lyxrc.default_papersize) {
232 case BufferParams::PAPER_DEFAULT: break;
233 case BufferParams::PAPER_USLETTER:
234 case BufferParams::PAPER_LEGALPAPER:
235 case BufferParams::PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
236 case BufferParams::PAPER_A3PAPER:
237 case BufferParams::PAPER_A4PAPER:
238 case BufferParams::PAPER_A5PAPER:
239 case BufferParams::PAPER_B5PAPER: defaultUnit = "cm"; break;
244 void FormGraphics::apply()
246 // Create the parameters structure and fill the data from the dialog.
247 InsetGraphicsParams & igp = controller().params();
250 igp.filename = getString(file_->input_filename);
252 igp.lyxscale = strToInt(getString(file_->input_lyxscale));
253 if (igp.lyxscale == 0) igp.lyxscale = 100;
255 switch (fl_get_choice(file_->choice_display)) {
256 case 5: igp.display = grfx::NoDisplay; break;
257 case 4: igp.display = grfx::ColorDisplay; break;
258 case 3: igp.display = grfx::GrayscaleDisplay; break;
259 case 2: igp.display = grfx::MonochromeDisplay; break;
261 default: igp.display = grfx::DefaultDisplay;
264 // first item in choice_width means scaling
265 if (fl_get_choice(file_->choice_width) == 1) {
266 igp.scale = strToInt(getString(file_->input_width));
267 if (igp.scale == 0) igp.scale = 100;
268 igp.width = LyXLength();
272 igp.width = getLyXLengthFromWidgets(file_->input_width,
273 file_->choice_width);
275 igp.height = getLyXLengthFromWidgets(file_->input_height,
276 file_->choice_height);
277 igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
279 igp.draft = fl_get_button(file_->check_draft);
280 igp.noUnzip = fl_get_button(file_->check_nounzip);
283 if (!controller().bbChanged) // different to the original one?
284 igp.bb = string(); // don't write anything
287 if (getString(bbox_->input_bb_x0).empty())
290 bb = getLengthFromWidgets(bbox_->input_bb_x0,
291 bbox_->choice_bb_units)+" ";
292 if (getString(bbox_->input_bb_y0).empty())
295 bb += (getLengthFromWidgets(bbox_->input_bb_y0,
296 bbox_->choice_bb_units)+" ");
297 if (getString(bbox_->input_bb_x1).empty())
300 bb += (getLengthFromWidgets(bbox_->input_bb_x1,
301 bbox_->choice_bb_units)+" ");
302 if (getString(bbox_->input_bb_y1).empty())
305 bb += (getLengthFromWidgets(bbox_->input_bb_y1,
306 bbox_->choice_bb_units)+" ");
309 igp.clip = fl_get_button(bbox_->check_clip);
312 igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
314 // map angle into -360 (clock-wise) to +360 (counter clock-wise)
315 while (igp.rotateAngle <= -360.0) igp.rotateAngle += 360.0;
316 while (igp.rotateAngle >= 360.0) igp.rotateAngle -= 360.0;
317 fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
319 int const origin_pos = fl_get_choice(extra_->choice_origin);
320 igp.rotateOrigin = origins_[origin_pos-1];
322 igp.subcaption = fl_get_button(extra_->check_subcaption);
323 igp.subcaptionText = getString(extra_->input_subcaption);
325 igp.special = getString(extra_->input_special);
329 void FormGraphics::update() {
330 // Update dialog with details from inset
331 InsetGraphicsParams & igp = controller().params();
334 fl_set_input(file_->input_filename, igp.filename.c_str());
335 fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
337 switch (igp.display) {
338 case grfx::NoDisplay: fl_set_choice(file_->choice_display, 5); break;
339 case grfx::ColorDisplay: fl_set_choice(file_->choice_display, 4); break;
340 case grfx::GrayscaleDisplay: fl_set_choice(file_->choice_display, 3); break;
341 case grfx::MonochromeDisplay: fl_set_choice(file_->choice_display, 2); break;
342 case grfx::DefaultDisplay:
343 default: fl_set_choice(file_->choice_display, 1);
346 // disable height input in case of scaling
347 setEnabled(file_->input_height, !igp.scale);
348 setEnabled(file_->choice_height, !igp.scale);
350 // set width input fields according to scaling or width/height input
352 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
353 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
354 fl_set_choice(file_->choice_width, 1);
357 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
358 updateWidgetsFromLength(file_->input_width,
359 file_->choice_width, igp.width, defaultUnit);
362 updateWidgetsFromLength(file_->input_height,
363 file_->choice_height, igp.height, defaultUnit);
365 fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
366 fl_set_button(file_->check_draft, igp.draft);
367 fl_set_button(file_->check_nounzip, igp.noUnzip);
369 // disable aspectratio button in case of scaling or one of width/height is empty
370 bool const disable_aspectRatio = igp.scale ||
371 getString(file_->input_width).empty() ||
372 getString(file_->input_height).empty();
373 setEnabled(file_->check_aspectratio, !disable_aspectRatio);
376 // set the bounding box values, if exists. First we need the whole
377 // path, because the controller knows nothing about the doc-dir
378 updateBB(igp.filename, igp.bb);
379 fl_set_button(bbox_->check_clip, igp.clip);
383 fl_set_input(extra_->input_rotate_angle,
384 tostr(igp.rotateAngle).c_str());
385 if (igp.rotateOrigin.empty())
386 fl_set_choice(extra_->choice_origin, 1);
388 fl_set_choice(extra_->choice_origin,
389 1 + int(findPos(origins_, igp.rotateOrigin)) );
390 fl_set_button(extra_->check_subcaption, igp.subcaption);
391 fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
392 setEnabled(extra_->input_subcaption,
393 fl_get_button(extra_->check_subcaption));
394 fl_set_input(extra_->input_special, igp.special.c_str());
396 // open dialog in the file-tab, whenever filename is empty
397 if (igp.filename.empty()) fl_set_folder(dialog_->tabfolder, file_->form);
401 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
403 // Update dialog with details from inset
404 // set the bounding box values, if exists. First we need the whole
405 // path, because the controller knows nothing about the doc-dir
406 controller().bbChanged = false;
407 if (bb_inset.empty()) {
408 lyxerr[Debug::GRAPHICS] << "update:: no BoundingBox" << endl;
409 string const bb = controller().readBB(filename);
411 // get the values from the file
412 // in this case we always have the point-unit
413 fl_set_input(bbox_->input_bb_x0,
414 token(bb,' ',0).c_str());
415 fl_set_input(bbox_->input_bb_y0,
416 token(bb,' ',1).c_str());
417 fl_set_input(bbox_->input_bb_x1,
418 token(bb,' ',2).c_str());
419 fl_set_input(bbox_->input_bb_y1,
420 token(bb,' ',3).c_str());
424 fl_set_input(bbox_->input_bb_x0, bb.c_str());
425 fl_set_input(bbox_->input_bb_y0, bb.c_str());
426 fl_set_input(bbox_->input_bb_x1, bb.c_str());
427 fl_set_input(bbox_->input_bb_y1, bb.c_str());
430 fl_set_choice(bbox_->choice_bb_units, 1);
433 // get the values from the inset
434 lyxerr[Debug::GRAPHICS] << "update:: igp has BoundingBox"
436 controller().bbChanged = true;
438 anyLength = LyXLength(token(bb_inset,' ',0));
439 updateWidgetsFromLength(bbox_->input_bb_x0,
440 bbox_->choice_bb_units,anyLength,"bp");
441 anyLength = LyXLength(token(bb_inset,' ',1));
442 updateWidgetsFromLength(bbox_->input_bb_y0,
443 bbox_->choice_bb_units,anyLength,"bp");
444 anyLength = LyXLength(token(bb_inset,' ',2));
445 updateWidgetsFromLength(bbox_->input_bb_x1,
446 bbox_->choice_bb_units,anyLength,"bp");
447 anyLength = LyXLength(token(bb_inset,' ',3));
448 updateWidgetsFromLength(bbox_->input_bb_y1,
449 bbox_->choice_bb_units,anyLength,"bp");
455 bool isValid(FL_OBJECT * ob)
457 string const input = getString(ob);
458 return input.empty() || isValidLength(input) || isStrDbl(input);
465 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
468 if (ob == file_->button_browse) {
469 // Get the filename from the dialog
470 string const in_name = getString(file_->input_filename);
471 string const out_name = controller().Browse(in_name);
472 lyxerr[Debug::GRAPHICS] << "[FormGraphics]out_name: " << out_name << endl;
473 if (out_name != in_name && !out_name.empty()) {
474 fl_set_input(file_->input_filename, out_name.c_str());
476 if (controller().isFilenameValid(out_name) &&
477 !controller().bbChanged)
478 updateBB(out_name, string());
479 } else if (ob == file_->input_width || ob == file_->input_height) {
480 // disable aspectratio button in case of scaling or one of width/height is empty
481 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
482 getString(file_->input_width).empty() ||
483 getString(file_->input_height).empty();
484 setEnabled(file_->check_aspectratio, !disable);
485 } else if (ob == file_->choice_width) {
486 // disable height input in case of scaling
487 bool const scaling = fl_get_choice(file_->choice_width) == 1;
488 setEnabled(file_->input_height, !scaling);
489 setEnabled(file_->choice_height, !scaling);
491 // allow only integer intput for scaling; float otherwise
493 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
495 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
498 // disable aspectratio button in case of scaling or height input is empty
499 bool const disable_aspectratio = scaling || getString(file_->input_height).empty();
500 setEnabled(file_->check_aspectratio, !disable_aspectratio);
502 } else if (!controller().bbChanged &&
503 (ob == bbox_->check_clip || ob == bbox_->choice_bb_units ||
504 ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
505 ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
506 controller().bbChanged = true;
507 } else if (ob == bbox_->button_getBB) {
508 string const filename = getString(file_->input_filename);
509 if (!filename.empty()) {
510 string bb = controller().readBB(filename);
512 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
513 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
514 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
515 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
516 fl_set_choice_text(bbox_->choice_bb_units, "bp");
518 controller().bbChanged = false;
520 fl_set_input(bbox_->input_bb_x0, "");
521 fl_set_input(bbox_->input_bb_y0, "");
522 fl_set_input(bbox_->input_bb_x1, "");
523 fl_set_input(bbox_->input_bb_y1, "");
524 fl_set_choice_text(bbox_->choice_bb_units, "bp");
527 } else if (ob == extra_->check_subcaption) {
528 setEnabled(extra_->input_subcaption,
529 fl_get_button(extra_->check_subcaption));
533 // deactivate OK/ Apply buttons and
534 // spit out warnings if invalid
535 if (ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_x1 ||
536 ob == bbox_->input_bb_y0 || ob == bbox_->input_bb_y1 ||
537 ob == file_->input_width || ob == file_->input_height) {
541 postWarning(_("Invalid Length!"));
542 return ButtonPolicy::SMI_INVALID;
546 return ButtonPolicy::SMI_VALID;