1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * ======================================================
10 * \file FormGraphics.C
11 * \author Baruch Even, baruch.even@writeme.com
17 #pragma implementation
21 #include "ControlGraphics.h"
22 #include "FormGraphics.h"
23 #include "form_graphics.h"
25 #include "xforms_helpers.h"
26 #include "input_validators.h"
27 #include "debug.h" // for lyxerr
28 #include "support/lstrings.h" // for strToDbl & tostr
29 #include "support/FileInfo.h"
30 #include "support/filetools.h" // for MakeAbsPath etc
31 #include "insets/insetgraphicsParams.h"
32 #include "lyxrc.h" // for lyxrc.display_graphics
38 // Bound the number of input characters
39 int const SCALE_MAXDIGITS = 3;
40 int const WIDTH_MAXDIGITS = 10;
41 int const HEIGHT_MAXDIGITS = 10;
42 int const ROTATE_MAXCHARS = 4;
43 int const FILENAME_MAXCHARS = 1024;
48 typedef FormCB<ControlGraphics, FormDB<FD_form_graphics> > base_class;
50 FormGraphics::FormGraphics(ControlGraphics & c)
51 : base_class(c, _("Graphics"))
55 void FormGraphics::redraw()
57 if(form() && form()->visible)
58 fl_redraw_form(form());
62 FL_FORM * outer_form = fl_get_active_folder(dialog_->tabFolder);
63 if (outer_form && outer_form->visible)
64 fl_redraw_form(outer_form);
68 void FormGraphics::build()
70 dialog_.reset(build_graphics());
72 // Manage the ok, apply, restore and cancel/close buttons
73 bc().setOK(dialog_->button_ok);
74 // bc().setApply(dialog_->button_apply);
75 bc().setCancel(dialog_->button_cancel);
76 bc().setRestore(dialog_->button_restore);
79 file_.reset(build_file());
81 fl_set_input_return (file_->input_filename, FL_RETURN_CHANGED);
82 fl_set_input_return (file_->input_subcaption, FL_RETURN_CHANGED);
83 fl_set_input_return (file_->input_lyxwidth, FL_RETURN_CHANGED);
84 fl_set_input_return (file_->input_lyxheight, FL_RETURN_CHANGED);
85 fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
87 fl_addto_choice(file_->choice_width_lyxwidth, choice_Length_All.c_str());
88 fl_addto_choice(file_->choice_width_lyxheight, choice_Length_All.c_str());
89 fl_addto_choice(file_->choice_display,
90 _(" Default | Monochrome | Grayscale | Color | Do not display "));
91 //FIXME: This does not work (always set to monochrome by default. Why? JSpitzm)
92 fl_set_choice(file_->choice_display, 1);
94 bc().addReadOnly(file_->button_browse);
95 bc().addReadOnly(file_->check_subcaption);
96 bc().addReadOnly(file_->choice_display);
99 size_.reset(build_size());
101 fl_set_input_return (size_->input_scale, FL_RETURN_CHANGED);
102 fl_set_input_return (size_->input_width, FL_RETURN_CHANGED);
103 fl_set_input_return (size_->input_height, FL_RETURN_CHANGED);
105 fl_set_input_maxchars(size_->input_scale, SCALE_MAXDIGITS);
106 fl_set_input_maxchars(size_->input_width, WIDTH_MAXDIGITS);
107 fl_set_input_maxchars(size_->input_height, HEIGHT_MAXDIGITS);
109 fl_set_input_filter(size_->input_scale, fl_unsigned_float_filter);
111 fl_addto_choice(size_->choice_width_units, choice_Length_All.c_str());
112 fl_addto_choice(size_->choice_height_units, choice_Length_All.c_str());
114 bc().addReadOnly(size_->button_default);
115 bc().addReadOnly(size_->button_wh);
116 bc().addReadOnly(size_->button_scale);
117 bc().addReadOnly(size_->check_aspectratio);
119 // the bounding box selection
120 bbox_.reset(build_bbox());
121 fl_set_input_return (bbox_->input_bb_x0, FL_RETURN_CHANGED);
122 fl_set_input_return (bbox_->input_bb_y0, FL_RETURN_CHANGED);
123 fl_set_input_return (bbox_->input_bb_x1, FL_RETURN_CHANGED);
124 fl_set_input_return (bbox_->input_bb_y1, FL_RETURN_CHANGED);
126 bc().addReadOnly(bbox_->button_getBB);
127 bc().addReadOnly(bbox_->button_clip);
128 bc().addReadOnly(bbox_->button_draft);
130 string const bb_units = "pt|cm|in";
131 fl_addto_choice(bbox_->choice_bb_x0, bb_units.c_str());
132 fl_addto_choice(bbox_->choice_bb_y0, bb_units.c_str());
133 fl_addto_choice(bbox_->choice_bb_x1, bb_units.c_str());
134 fl_addto_choice(bbox_->choice_bb_y1, bb_units.c_str());
137 // the rotate section
138 special_.reset(build_special());
140 fl_set_input_return (special_->input_rotate_angle, FL_RETURN_CHANGED);
142 string const choice_origin =
143 "default|" // not important
144 "leftTop|leftBottom|leftBaseline|" // lt lb lB
146 "centerTop|centerBottom|centerBaseline|" // ct cb cB
147 "rightTop|rightBottom|rightBaseline|" // rt rb rB
148 "referencePoint"; // special
149 fl_addto_choice(special_->choice_origin, choice_origin.c_str());
151 fl_set_input_return (special_->input_special, FL_RETURN_CHANGED);
152 fl_set_input_maxchars(special_->input_rotate_angle, ROTATE_MAXCHARS);
153 fl_set_input_filter(special_->input_rotate_angle, fl_float_filter);
155 // add the different tabfolders
156 fl_addto_tabfolder(dialog_->tabFolder, _("File"), file_->form);
157 fl_addto_tabfolder(dialog_->tabFolder, _("Size"), size_->form);
158 fl_addto_tabfolder(dialog_->tabFolder, _("Bounding Box"), bbox_->form);
159 fl_addto_tabfolder(dialog_->tabFolder, _("Extras"), special_->form);
163 void FormGraphics::apply()
165 // Create the parameters structure and fill the data from the dialog.
166 InsetGraphicsParams & igp = controller().params();
167 igp.filename = getStringFromInput(file_->input_filename);
168 if (!controller().bbChanged) // different to the original one?
169 igp.bb = string(); // don't write anything
172 if (getStringFromInput(bbox_->input_bb_x0).empty())
175 bb = getLengthFromWidgets(bbox_->input_bb_x0,
176 bbox_->choice_bb_x0)+" ";
177 if (getStringFromInput(bbox_->input_bb_y0).empty())
180 bb += (getLengthFromWidgets(bbox_->input_bb_y0,
181 bbox_->choice_bb_y0)+" ");
182 if (getStringFromInput(bbox_->input_bb_x1).empty())
185 bb += (getLengthFromWidgets(bbox_->input_bb_x1,
186 bbox_->choice_bb_x1)+" ");
187 if (getStringFromInput(bbox_->input_bb_y1).empty())
190 bb += (getLengthFromWidgets(bbox_->input_bb_y1,
191 bbox_->choice_bb_y1)+" ");
194 igp.draft = fl_get_button(bbox_->button_draft);
195 igp.clip = fl_get_button(bbox_->button_clip);
196 igp.subcaption = fl_get_button(file_->check_subcaption);
197 igp.subcaptionText = getStringFromInput(file_->input_subcaption);
198 // use preferences settings if choice is set to default
199 if (fl_get_choice(file_->choice_display) == 1) {
200 if (lyxrc.display_graphics == "mono")
201 igp.display = InsetGraphicsParams::MONOCHROME;
202 else if (lyxrc.display_graphics == "gray")
203 igp.display = InsetGraphicsParams::GRAYSCALE;
204 else if (lyxrc.display_graphics == "color")
205 igp.display = InsetGraphicsParams::COLOR;
206 else if (lyxrc.display_graphics == "no")
207 igp.display = InsetGraphicsParams::NONE;
208 } else if (fl_get_choice(file_->choice_display) == 2) {
209 igp.display = InsetGraphicsParams::MONOCHROME;
210 } else if (fl_get_choice(file_->choice_display) == 3) {
211 igp.display = InsetGraphicsParams::GRAYSCALE;
212 } else if (fl_get_choice(file_->choice_display) == 4) {
213 igp.display = InsetGraphicsParams::COLOR;
214 } else if (fl_get_choice(file_->choice_display) == 5) {
215 igp.display = InsetGraphicsParams::NONE;
217 if (fl_get_button(size_->button_default))
218 igp.size_type = InsetGraphicsParams::DEFAULT_SIZE;
219 else if (fl_get_button(size_->button_wh))
220 igp.size_type = InsetGraphicsParams::WH;
222 igp.size_type = InsetGraphicsParams::SCALE;
223 igp.width = LyXLength(getLengthFromWidgets(size_->input_width,
224 size_->choice_width_units));
225 igp.height = LyXLength(getLengthFromWidgets(size_->input_height,
226 size_->choice_height_units));
227 igp.scale = strToInt(getStringFromInput(size_->input_scale));
228 igp.keepAspectRatio = fl_get_button(size_->check_aspectratio);
229 igp.lyxwidth = LyXLength(getLengthFromWidgets(file_->input_lyxwidth,
230 file_->choice_width_lyxwidth));
231 igp.lyxheight = LyXLength(getLengthFromWidgets(file_->input_lyxheight,
232 file_->choice_width_lyxheight));
235 strToDbl(getStringFromInput(special_->input_rotate_angle));
236 while (igp.rotateAngle < 0.0 || igp.rotateAngle > 360.0) {
237 if (igp.rotateAngle < 0.0) {
238 igp.rotateAngle += 360.0;
239 } else if (igp.rotateAngle > 360.0) {
240 igp.rotateAngle -= 360.0;
243 if (fl_get_choice(special_->choice_origin) > 0)
244 igp.rotateOrigin = fl_get_choice_text(special_->choice_origin);
246 igp.rotateOrigin = string();
247 igp.special = getStringFromInput(special_->input_special);
252 void FormGraphics::update()
253 { string unit = "cm";
254 if (lyxrc.default_papersize < 3)
256 string const defaultUnit = string(unit);
257 // Update dialog with details from inset
258 InsetGraphicsParams & igp = controller().params();
259 fl_set_input(file_->input_filename, igp.filename.c_str());
260 // set the bounding box values, if exists. First we need the whole
261 // path, because the controller knows nothing about the doc-dir
262 lyxerr << "GraphicsUpdate::BoundingBox = " << igp.bb << "\n";
263 controller().bbChanged = false;
264 if (igp.bb.empty()) {
265 string const fileWithAbsPath = MakeAbsPath(igp.filename, OnlyPath(igp.filename));
266 string bb = controller().readBB(fileWithAbsPath);
267 lyxerr << "file::BoundingBox = " << bb << "\n";
269 // get the values from the file
270 // in this case we always have the point-unit
271 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
272 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
273 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
274 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
276 } else { // get the values from the inset
277 controller().bbChanged = true;
279 anyLength = LyXLength(token(igp.bb,' ',0));
280 updateWidgetsFromLength(bbox_->input_bb_x0,
281 bbox_->choice_bb_x0,anyLength,"pt");
282 anyLength = LyXLength(token(igp.bb,' ',1));
283 updateWidgetsFromLength(bbox_->input_bb_y0,
284 bbox_->choice_bb_y0,anyLength,"pt");
285 anyLength = LyXLength(token(igp.bb,' ',2));
286 updateWidgetsFromLength(bbox_->input_bb_x1,
287 bbox_->choice_bb_x1,anyLength,"pt");
288 anyLength = LyXLength(token(igp.bb,' ',3));
289 updateWidgetsFromLength(bbox_->input_bb_y1,
290 bbox_->choice_bb_y1,anyLength,"pt");
292 // Update the draft and clip mode
293 fl_set_button(bbox_->button_draft, igp.draft);
294 fl_set_button(bbox_->button_clip, igp.clip);
295 // Update the subcaption check button and input field
296 fl_set_button(file_->check_subcaption, igp.subcaption);
297 fl_set_input(file_->input_subcaption, igp.subcaptionText.c_str());
298 setEnabled(file_->input_subcaption,
299 fl_get_button(file_->check_subcaption));
301 switch (igp.display) {
302 case InsetGraphicsParams::NONE: { // dont't display
303 fl_set_choice(file_->choice_display, 5);
306 case InsetGraphicsParams::MONOCHROME: {
307 fl_set_choice(file_->choice_display, 2);
310 case InsetGraphicsParams::GRAYSCALE: {
311 fl_set_choice(file_->choice_display, 3);
314 case InsetGraphicsParams::COLOR: {
315 fl_set_choice(file_->choice_display, 4);
319 updateWidgetsFromLength(size_->input_width,
320 size_->choice_width_units,igp.width,defaultUnit);
321 updateWidgetsFromLength(size_->input_height,
322 size_->choice_height_units,igp.height,defaultUnit);
323 fl_set_input(size_->input_scale, tostr(igp.scale).c_str());
324 switch (igp.size_type) {
325 case InsetGraphicsParams::DEFAULT_SIZE: {
326 fl_set_button(size_->button_default,1);
327 setEnabled(size_->input_width, 0);
328 setEnabled(size_->choice_width_units, 0);
329 setEnabled(size_->input_height, 0);
330 setEnabled(size_->choice_height_units, 0);
331 setEnabled(size_->input_scale, 0);
334 case InsetGraphicsParams::WH: {
335 fl_set_button(size_->button_wh, 1);
336 setEnabled(size_->input_width, 1);
337 setEnabled(size_->choice_width_units, 1);
338 setEnabled(size_->input_height, 1);
339 setEnabled(size_->choice_height_units, 1);
340 setEnabled(size_->input_scale, 0);
343 case InsetGraphicsParams::SCALE: {
344 fl_set_button(size_->button_scale, 1);
345 setEnabled(size_->input_width, 0);
346 setEnabled(size_->choice_width_units, 0);
347 setEnabled(size_->input_height, 0);
348 setEnabled(size_->choice_height_units, 0);
349 setEnabled(size_->input_scale, 1);
353 fl_set_button(size_->check_aspectratio, igp.keepAspectRatio);
354 // now the lyx-internally viewsize
355 updateWidgetsFromLength(file_->input_lyxwidth,
356 file_->choice_width_lyxwidth, igp.lyxwidth,defaultUnit);
357 updateWidgetsFromLength(file_->input_lyxheight,
358 file_->choice_width_lyxheight, igp.lyxheight,defaultUnit);
359 // Update the rotate angle and special commands
360 fl_set_input(special_->input_rotate_angle,
361 tostr(igp.rotateAngle).c_str());
362 if (igp.rotateOrigin.empty())
363 fl_set_choice(special_->choice_origin,0);
365 fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
366 fl_set_input(special_->input_special, igp.special.c_str());
372 bool isValid(FL_OBJECT * ob)
374 string const input = getStringFromInput(ob);
375 return input.empty() || isValidLength(input) || isStrDbl(input);
382 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
384 if (ob == file_->button_browse) {
385 // Get the filename from the dialog
386 string const in_name = getStringFromInput(file_->input_filename);
387 string const out_name = controller().Browse(in_name);
388 if (out_name != in_name && !out_name.empty()) {
389 fl_set_input(file_->input_filename, out_name.c_str());
391 } else if (!controller().bbChanged &&
392 ((ob == bbox_->input_bb_x0) || (ob == bbox_->input_bb_y0) ||
393 (ob == bbox_->input_bb_x1) || (ob == bbox_->input_bb_y1) ||
394 (ob == bbox_->choice_bb_x0) || (ob == bbox_->choice_bb_y0) ||
395 (ob == bbox_->choice_bb_x1) || (ob == bbox_->choice_bb_y1))) {
396 controller().bbChanged = true;
397 } else if (ob == size_->button_default) {
398 setEnabled(size_->input_width, 0);
399 setEnabled(size_->choice_width_units, 0);
400 setEnabled(size_->input_height, 0);
401 setEnabled(size_->choice_height_units, 0);
402 setEnabled(size_->input_scale, 0);
403 } else if (ob == size_->button_wh) {
404 setEnabled(size_->input_width, 1);
405 setEnabled(size_->choice_width_units, 1);
406 setEnabled(size_->input_height, 1);
407 setEnabled(size_->choice_height_units, 1);
408 setEnabled(size_->input_scale, 0);
409 } else if (ob == size_->button_scale) {
410 setEnabled(size_->input_width, 0);
411 setEnabled(size_->choice_width_units, 0);
412 setEnabled(size_->input_height, 0);
413 setEnabled(size_->choice_height_units, 0);
414 setEnabled(size_->input_scale, 1);
415 } else if (ob == file_->check_subcaption) {
416 setEnabled(file_->input_subcaption,
417 fl_get_button(file_->check_subcaption));
418 } else if (ob == bbox_->button_getBB) {
419 string const filename = getStringFromInput(file_->input_filename);
420 if (!filename.empty()) {
421 string const fileWithAbsPath = MakeAbsPath(filename, OnlyPath(filename));
422 string bb = controller().readBB(fileWithAbsPath);
423 lyxerr << "getBB::BoundingBox = " << bb << "\n";
425 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
426 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
427 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
428 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
430 controller().bbChanged = false;
432 } else if (ob == dialog_->button_help) {
436 // check if the input is valid
437 bool invalid = !isValid(bbox_->input_bb_x0);
438 invalid = invalid || !isValid(bbox_->input_bb_x1);
439 invalid = invalid || !isValid(bbox_->input_bb_y0);
440 invalid = invalid || !isValid(bbox_->input_bb_y1);
441 invalid = invalid || !isValid(size_->input_width);
442 invalid = invalid || !isValid(size_->input_height);
443 invalid = invalid || !isValid(file_->input_lyxwidth);
444 invalid = invalid || !isValid(file_->input_lyxheight);
446 // deactivate OK/ Apply buttons and
447 // spit out warnings if invalid
448 if (ob == bbox_->input_bb_x0
449 || ob == bbox_->input_bb_x1
450 || ob == bbox_->input_bb_y0
451 || ob == bbox_->input_bb_y1
452 || ob == size_->input_width
453 || ob == size_->input_height
454 || ob == file_->input_lyxwidth
455 || ob == file_->input_lyxheight) {
457 fl_set_object_label(dialog_->text_warning,
458 _("Warning: Invalid Length!"));
459 fl_show_object(dialog_->text_warning);
460 return ButtonPolicy::SMI_INVALID;
462 fl_hide_object(dialog_->text_warning);
470 ButtonPolicy::SMInput FormGraphics::checkInput()
472 // Put verifications that the dialog shows some sane values,
473 // if not disallow clicking on ok/apply.
474 // Possibly use a label in the bottom of the dialog to give the reason.
475 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
476 // We verify now that there is a filename, it exists, it's a file
477 // and it's readable.
478 string filename = getStringFromInput(file_->input_filename);
479 FileInfo file(filename);
480 if (filename.empty() || !file.isOK() || !file.exist()
481 || !file.isRegular() || !file.readable()
483 activate = ButtonPolicy::SMI_INVALID;