]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormGraphics.C
befc213ac9da98478abac41e5f01c1394a70e53f
[features.git] / src / frontends / xforms / FormGraphics.C
1 /**
2  * \file FormGraphics.C
3  * Copyright 2000-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Baruch Even, baruch.even@writeme.com
7  * \author Herbert Voss, voss@perce.de
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "xformsBC.h"
17 #include "ControlGraphics.h"
18 #include "FormGraphics.h"
19 #include "forms/form_graphics.h"
20 #include "Alert.h"
21 #include "Tooltips.h"
22
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
32
33 using std::endl;
34 using std::vector;
35
36 namespace {
37
38 // Bound the number of input characters
39 int const SIZE_MAXDIGITS = 10;
40 int const FILENAME_MAXCHARS = 1024;
41
42 string defaultUnit("cm");
43
44 /// Given input and choice widgets, create a LyXLength
45 LyXLength getLyXLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
46 {
47         return LyXLength(getLengthFromWidgets(input, choice));
48 }
49
50 } // namespace anon
51
52
53 typedef FormCB<ControlGraphics, FormDB<FD_graphics> > base_class;
54
55 FormGraphics::FormGraphics()
56         : base_class(_("Graphics"), false)
57 {}
58
59
60 void FormGraphics::redraw()
61 {
62         if (form() && form()->visible)
63                 fl_redraw_form(form());
64         else
65                 return;
66         FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
67         if (outer_form && outer_form->visible)
68                 fl_redraw_form(outer_form);
69 }
70
71
72 void FormGraphics::build()
73 {
74         dialog_.reset(build_graphics(this));
75
76         // Allow the base class to control messages
77         setMessageWidget(dialog_->text_warning);
78
79         // Manage the ok, apply, restore and cancel/close buttons
80         bc().setOK(dialog_->button_ok);
81         bc().setApply(dialog_->button_apply);
82         bc().setCancel(dialog_->button_close);
83         bc().setRestore(dialog_->button_restore);
84
85         // the file section
86         file_.reset(build_graphics_file(this));
87
88         fl_set_input_return (file_->input_filename, FL_RETURN_CHANGED);
89         fl_set_input_return (file_->input_lyxscale, FL_RETURN_CHANGED);
90         fl_set_input_return (file_->input_width, FL_RETURN_CHANGED);
91         fl_set_input_return (file_->input_height, FL_RETURN_CHANGED);
92
93         setPrehandler(file_->input_filename);
94         setPrehandler(file_->input_lyxscale);
95         setPrehandler(file_->input_width);
96         setPrehandler(file_->input_height);
97
98         fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
99         fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
100
101          // width default is scaling, thus unsigned integer input
102         fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
103         fl_set_input_maxchars(file_->input_height, SIZE_MAXDIGITS);
104
105
106         string const display_List = _("Default|Monochrome|Grayscale|Color|Do not display");
107         fl_addto_choice(file_->choice_display, display_List.c_str());
108         
109         string const width_list = _("Scale%%|") + choice_Length_All;
110         fl_addto_choice(file_->choice_width, width_list.c_str());
111
112         fl_addto_choice(file_->choice_height, choice_Length_All.c_str());
113
114         bc().addReadOnly(file_->button_browse);   
115         bc().addReadOnly(file_->check_aspectratio);
116         bc().addReadOnly(file_->check_draft);
117         bc().addReadOnly(file_->check_nounzip);
118
119         // set up the tooltips for the filesection
120         string str = _("The file you want to insert.");
121         tooltips().init(file_->input_filename, str);
122         str = _("Browse the directories.");
123         tooltips().init(file_->button_browse, str);
124
125         str = _("Scale the image to inserted percentage value");
126         tooltips().init(file_->input_lyxscale, str);
127         str = _("Select display mode for this image.");
128         tooltips().init(file_->choice_display, str);
129
130         str = _("Set the image width to the inserted value.");
131         tooltips().init(file_->input_width, str);
132         str = _("Select unit for width; Scale% for scaling whole image");
133         tooltips().init(file_->choice_width, str);
134         str = _("Set the image height to the inserted value.");
135         tooltips().init(file_->input_height, str);
136         str = _("Select unit for height");
137         tooltips().init(file_->choice_height, str);
138         str = _("Do not distort the image. " 
139                 "Keep image within \"width\" by \"height\" and obey aspect ratio.");
140         tooltips().init(file_->check_aspectratio, str);
141
142         str = _("Pass a filename like \"file.eps.gz\" to the LaTeX output. "
143             "This is useful when LaTeX should unzip the file. Needs an additional file "
144             "like \"file.eps.bb\" which holds the values for the bounding box.");
145         tooltips().init(file_->check_nounzip, str);
146
147         str = _("Show image only as a rectangle of the original size.");
148         tooltips().init(file_->check_draft, str);
149
150         // the bounding box selection
151         bbox_.reset(build_graphics_bbox(this));
152         fl_set_input_return (bbox_->input_bb_x0, FL_RETURN_CHANGED);
153         fl_set_input_return (bbox_->input_bb_y0, FL_RETURN_CHANGED);
154         fl_set_input_return (bbox_->input_bb_x1, FL_RETURN_CHANGED);
155         fl_set_input_return (bbox_->input_bb_y1, FL_RETURN_CHANGED);
156
157         fl_set_input_filter(bbox_->input_bb_x0, fl_unsigned_float_filter);
158         fl_set_input_filter(bbox_->input_bb_y0, fl_unsigned_float_filter);
159         fl_set_input_filter(bbox_->input_bb_x1, fl_unsigned_float_filter);
160         fl_set_input_filter(bbox_->input_bb_y1, fl_unsigned_float_filter);
161
162         setPrehandler(bbox_->input_bb_x0);
163         setPrehandler(bbox_->input_bb_y0);
164         setPrehandler(bbox_->input_bb_x1);
165         setPrehandler(bbox_->input_bb_y1);
166
167         string const bb_units = "bp|cm|mm|in";
168         fl_addto_choice(bbox_->choice_bb_units, bb_units.c_str());
169         bc().addReadOnly(bbox_->button_getBB);
170         bc().addReadOnly(bbox_->check_clip);
171
172         // set up the tooltips for the bounding-box-section
173         str = _("The lower left x-value of the bounding box");
174         tooltips().init(bbox_->input_bb_x0, str);
175         str = _("The lower left y-value of the bounding box");
176         tooltips().init(bbox_->input_bb_y0, str);
177         str = _("The upper right x-value of the bounding box");
178         tooltips().init(bbox_->input_bb_x1, str);
179         str = _("The upper right y-value of the bounding box");
180         tooltips().init(bbox_->input_bb_y1, str);
181         str = _("Select unit for the bounding box values");
182         tooltips().init(bbox_->choice_bb_units, str);
183
184         str = _("Read the image coordinates new from file. If it's an (e)ps-file "
185                 "then the bounding box is read otherwise the imagesize in pixels. "
186                 "The default unit is \"bp\", the PostScript's b(ig) p(oint).");
187         tooltips().init(bbox_->button_getBB, str);
188
189         str = _("Enable this checkbox when the image should be clipped to the "
190                 "bounding box values.");
191         tooltips().init(bbox_->check_clip, str);
192
193         // the extra section
194         extra_.reset(build_graphics_extra(this));
195
196         fl_set_input_return (extra_->input_rotate_angle, FL_RETURN_CHANGED);
197         fl_set_input_return (extra_->input_subcaption, FL_RETURN_CHANGED);
198         fl_set_input_return (extra_->input_special, FL_RETURN_CHANGED);
199
200         fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
201
202         setPrehandler(extra_->input_rotate_angle);
203         setPrehandler(extra_->input_subcaption);
204         setPrehandler(extra_->input_special);
205
206         bc().addReadOnly(extra_->check_subcaption);
207
208         using namespace frnt;
209         vector<RotationOriginPair> origindata = getRotationOriginData();
210
211         // Store the identifiers for later
212         origins_ = getSecond(origindata);
213
214         string const choice = " " + getStringFromVector(getFirst(origindata), " | ") + " ";
215         fl_addto_choice(extra_->choice_origin, choice.c_str());
216
217         // set up the tooltips for the extra section
218         str = _("Insert the rotation angle in degrees. "
219                 "Positive value rotates anti-clockwise, negative value clockwise");
220         tooltips().init(extra_->input_rotate_angle, str);
221         str = _("Insert the point of origin for rotation ");
222         tooltips().init(extra_->choice_origin, str);
223         str = _("Enables use of subfigure with its own caption.");
224         tooltips().init(extra_->check_subcaption, str);
225         str = _("Insert the optional subfigure caption");
226         tooltips().init(extra_->input_subcaption, str);
227         str = _("Add any additional latex option, which is defined in the "
228                 "graphicx-package and not mentioned in the gui's tabfolders.");
229         tooltips().init(extra_->input_special, str);
230
231         // add the different tabfolders
232         fl_addto_tabfolder(dialog_->tabfolder, _("File"), file_->form);
233         fl_addto_tabfolder(dialog_->tabfolder, _("Bounding Box"), bbox_->form);
234         fl_addto_tabfolder(dialog_->tabfolder, _("Extra"), extra_->form);
235
236         // set the right default unit
237         switch (lyxrc.default_papersize) {
238         case BufferParams::PAPER_DEFAULT: break;
239         case BufferParams::PAPER_USLETTER:
240         case BufferParams::PAPER_LEGALPAPER:
241         case BufferParams::PAPER_EXECUTIVEPAPER: defaultUnit = "in"; break;
242         case BufferParams::PAPER_A3PAPER:
243         case BufferParams::PAPER_A4PAPER:
244         case BufferParams::PAPER_A5PAPER:
245         case BufferParams::PAPER_B5PAPER: defaultUnit = "cm"; break;
246         }
247 }
248
249
250 void FormGraphics::apply()
251 {
252         // Create the parameters structure and fill the data from the dialog.
253         InsetGraphicsParams & igp = controller().params();
254
255         // the file section
256         igp.filename = getString(file_->input_filename);
257
258         igp.lyxscale = strToInt(getString(file_->input_lyxscale));
259         if (igp.lyxscale == 0) {
260                 igp.lyxscale = 100;
261         }
262         
263         switch (fl_get_choice(file_->choice_display)) {
264                 case 5: igp.display = grfx::NoDisplay; break;
265                 case 4: igp.display = grfx::ColorDisplay; break;
266                 case 3: igp.display = grfx::GrayscaleDisplay; break;
267                 case 2: igp.display = grfx::MonochromeDisplay; break;
268                 case 1:
269                 default: igp.display = grfx::DefaultDisplay;
270         }
271
272         // first item in choice_width means scaling
273         if (fl_get_choice(file_->choice_width) == 1) {
274                 igp.scale = strToInt(getString(file_->input_width));
275                 if (igp.scale == 0) {
276                         igp.scale = 100;
277                 }
278                 igp.width = LyXLength();
279         } else {
280                 igp.scale = 0;
281                 igp.width = getLyXLengthFromWidgets(file_->input_width,
282                                                     file_->choice_width);
283         }
284         igp.height = getLyXLengthFromWidgets(file_->input_height,
285                                              file_->choice_height);
286         igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
287
288         igp.draft = fl_get_button(file_->check_draft);
289         igp.noUnzip = fl_get_button(file_->check_nounzip);
290
291         // the bb section
292         if (!controller().bbChanged) { // different to the original one?
293                 igp.bb = string();     // don't write anything
294         } else {
295                 string bb;
296                 if (getString(bbox_->input_bb_x0).empty())
297                         bb = "0 ";
298                 else
299                         bb = getLengthFromWidgets(bbox_->input_bb_x0,
300                                                   bbox_->choice_bb_units)+" ";
301                 if (getString(bbox_->input_bb_y0).empty())
302                         bb += "0 ";
303                 else
304                         bb += (getLengthFromWidgets(bbox_->input_bb_y0,
305                                                     bbox_->choice_bb_units)+" ");
306                 if (getString(bbox_->input_bb_x1).empty())
307                         bb += "0 ";
308                 else
309                         bb += (getLengthFromWidgets(bbox_->input_bb_x1,
310                                                     bbox_->choice_bb_units)+" ");
311                 if (getString(bbox_->input_bb_y1).empty())
312                         bb += "0 ";
313                 else
314                         bb += (getLengthFromWidgets(bbox_->input_bb_y1,
315                                                     bbox_->choice_bb_units)+" ");
316                 igp.bb = bb;
317         }
318         igp.clip = fl_get_button(bbox_->check_clip);
319
320         // the extra section
321         igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
322         
323         // map angle into -360 (clock-wise) to +360 (counter clock-wise)
324         while (igp.rotateAngle <= -360.0) {
325                 igp.rotateAngle += 360.0;
326         }
327         while (igp.rotateAngle >=  360.0) {
328                 igp.rotateAngle -= 360.0;
329         }
330         fl_set_input(extra_->input_rotate_angle, tostr(igp.rotateAngle).c_str());
331
332         int const origin_pos = fl_get_choice(extra_->choice_origin);
333         igp.rotateOrigin = origins_[origin_pos-1];
334
335         igp.subcaption = fl_get_button(extra_->check_subcaption);
336         igp.subcaptionText = getString(extra_->input_subcaption);
337
338         igp.special = getString(extra_->input_special);
339 }
340
341
342 void FormGraphics::update() {
343         // Update dialog with details from inset
344         InsetGraphicsParams & igp = controller().params();
345
346         // the file section
347         fl_set_input(file_->input_filename, igp.filename.c_str());
348         fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
349
350         switch (igp.display) {
351                 case grfx::NoDisplay:           fl_set_choice(file_->choice_display, 5); break;
352                 case grfx::ColorDisplay:        fl_set_choice(file_->choice_display, 4); break;
353                 case grfx::GrayscaleDisplay:    fl_set_choice(file_->choice_display, 3); break;
354                 case grfx::MonochromeDisplay:   fl_set_choice(file_->choice_display, 2); break;
355                 case grfx::DefaultDisplay:
356                 default:                        fl_set_choice(file_->choice_display, 1);
357         }
358
359         // disable height input in case of scaling
360         setEnabled(file_->input_height, !igp.scale);
361         setEnabled(file_->choice_height, !igp.scale);
362
363         // set width input fields according to scaling or width/height input
364         if (igp.scale) {
365                 fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
366                 fl_set_input_maxchars(file_->input_width, 0);
367                 fl_set_input(file_->input_width, tostr(igp.scale).c_str());
368                 fl_set_choice(file_->choice_width, 1);
369         } else {
370                 fl_set_input_filter(file_->input_width, NULL);
371                 fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
372                 updateWidgetsFromLength(file_->input_width,
373                                         file_->choice_width, igp.width, defaultUnit);
374         }
375
376         updateWidgetsFromLength(file_->input_height,
377                                 file_->choice_height, igp.height, defaultUnit);
378         
379         fl_set_button(file_->check_aspectratio, igp.keepAspectRatio);
380         fl_set_button(file_->check_draft, igp.draft);
381         fl_set_button(file_->check_nounzip, igp.noUnzip);
382
383         // disable aspectratio button in case of scaling or one of width/height is empty
384         bool const disable_aspectRatio = igp.scale ||
385                                 getString(file_->input_width).empty() ||
386                                 getString(file_->input_height).empty();
387         setEnabled(file_->check_aspectratio, !disable_aspectRatio);
388
389         // the bb section
390         // set the bounding box values, if exists. First we need the whole
391         // path, because the controller knows nothing about the doc-dir
392         updateBB(igp.filename, igp.bb);
393         fl_set_button(bbox_->check_clip, igp.clip);
394
395
396         // the extra section
397         fl_set_input(extra_->input_rotate_angle,
398                      tostr(igp.rotateAngle).c_str());
399
400         int const origin_pos = findPos(origins_, igp.rotateOrigin);
401         fl_set_choice(extra_->choice_origin, 1 + origin_pos);
402
403         fl_set_button(extra_->check_subcaption, igp.subcaption);
404         fl_set_input(extra_->input_subcaption, igp.subcaptionText.c_str());
405         setEnabled(extra_->input_subcaption,
406                    fl_get_button(extra_->check_subcaption));
407         fl_set_input(extra_->input_special, igp.special.c_str());
408
409         // open dialog in the file-tab, whenever filename is empty
410         if (igp.filename.empty()) {
411                 fl_set_folder(dialog_->tabfolder, file_->form);
412         }
413 }
414
415
416 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
417 {
418         // Update dialog with details from inset
419         // set the bounding box values, if exists. First we need the whole
420         // path, because the controller knows nothing about the doc-dir
421         controller().bbChanged = false;
422         if (bb_inset.empty()) {
423                 lyxerr[Debug::GRAPHICS] << "FormGraphics::updateBB() [no BoundingBox]" << endl;
424                 string const bb = controller().readBB(filename);
425                 if (!bb.empty()) {
426                         // get the values from the file
427                         // in this case we always have the point-unit
428                         fl_set_input(bbox_->input_bb_x0,
429                                      token(bb,' ',0).c_str());
430                         fl_set_input(bbox_->input_bb_y0,
431                                      token(bb,' ',1).c_str());
432                         fl_set_input(bbox_->input_bb_x1,
433                                      token(bb,' ',2).c_str());
434                         fl_set_input(bbox_->input_bb_y1,
435                                      token(bb,' ',3).c_str());
436
437                 } else {
438                         // no bb from file
439                         fl_set_input(bbox_->input_bb_x0, bb.c_str());
440                         fl_set_input(bbox_->input_bb_y0, bb.c_str());
441                         fl_set_input(bbox_->input_bb_x1, bb.c_str());
442                         fl_set_input(bbox_->input_bb_y1, bb.c_str());
443                 }
444                 // "bp"
445                 fl_set_choice(bbox_->choice_bb_units, 1);
446
447         } else {
448                 // get the values from the inset
449                 lyxerr[Debug::GRAPHICS] << "FormGraphics::updateBB(): igp has BoundingBox"
450                                         << " ["<< bb_inset << "]"
451                                         << endl;
452                 controller().bbChanged = true;
453
454                 LyXLength anyLength;
455                 anyLength = LyXLength(token(bb_inset,' ',0));
456                 updateWidgetsFromLength(bbox_->input_bb_x0,
457                                         bbox_->choice_bb_units,anyLength,"bp");
458                 anyLength = LyXLength(token(bb_inset,' ',1));
459                 updateWidgetsFromLength(bbox_->input_bb_y0,
460                                         bbox_->choice_bb_units,anyLength,"bp");
461                 anyLength = LyXLength(token(bb_inset,' ',2));
462                 updateWidgetsFromLength(bbox_->input_bb_x1,
463                                         bbox_->choice_bb_units,anyLength,"bp");
464                 anyLength = LyXLength(token(bb_inset,' ',3));
465                 updateWidgetsFromLength(bbox_->input_bb_y1,
466                                         bbox_->choice_bb_units,anyLength,"bp");
467         }
468 }
469
470
471 namespace {
472
473 bool isValid(FL_OBJECT * ob)
474 {
475         string const input = getString(ob);
476         return input.empty() || isValidLength(input) || isStrDbl(input);
477 }
478
479 } // namespace anon
480
481
482
483 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
484 {
485         // the file section
486         if (ob == file_->button_browse) {
487                 // Get the filename from the dialog
488                 string const in_name = getString(file_->input_filename);
489                 string const out_name = controller().Browse(in_name);
490                 lyxerr[Debug::GRAPHICS] << "[FormGraphics]out_name: " << out_name << endl;
491                 if (out_name != in_name && !out_name.empty()) {
492                         fl_set_input(file_->input_filename, out_name.c_str());
493                 }
494                 if (controller().isFilenameValid(out_name) &&
495                     !controller().bbChanged) {
496                         updateBB(out_name, string());
497                 }
498         } else if (ob == file_->input_width || ob == file_->input_height) {
499                 // disable aspectratio button in case of scaling or one of width/height is empty
500                 bool const disable = fl_get_choice(file_->choice_width) == 1 ||
501                                     getString(file_->input_width).empty() ||
502                                     getString(file_->input_height).empty();
503                 setEnabled(file_->check_aspectratio, !disable);
504         } else if (ob == file_->choice_width) {
505                 // disable height input in case of scaling
506                 bool const scaling = fl_get_choice(file_->choice_width) == 1;
507                 setEnabled(file_->input_height, !scaling);
508                 setEnabled(file_->choice_height, !scaling);
509                 
510                 // allow only integer intput for scaling; float otherwise
511                 if (scaling) {
512                         fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
513                         fl_set_input_maxchars(file_->input_width, 0);
514                 } else {
515                         fl_set_input_filter(file_->input_width, NULL);
516                         fl_set_input_maxchars(file_->input_width, SIZE_MAXDIGITS);
517                 }
518
519                 // disable aspectratio button in case of scaling or height input is empty
520                 bool const disable_aspectratio = scaling || getString(file_->input_height).empty();
521                 setEnabled(file_->check_aspectratio, !disable_aspectratio);
522         // the bb section
523         } else if (!controller().bbChanged &&
524                    (ob == bbox_->check_clip  || ob == bbox_->choice_bb_units ||
525                     ob == bbox_->input_bb_x0 || ob == bbox_->input_bb_y0 ||
526                     ob == bbox_->input_bb_x1 || ob == bbox_->input_bb_y1)) {
527                 controller().bbChanged = true;
528         } else if (ob == bbox_->button_getBB) {
529                 string const filename = getString(file_->input_filename);
530                 if (!filename.empty()) {
531                         string bb = controller().readBB(filename);
532                         if (!bb.empty()) {
533                                 fl_set_input(bbox_->input_bb_x0, token(bb,' ',0).c_str());
534                                 fl_set_input(bbox_->input_bb_y0, token(bb,' ',1).c_str());
535                                 fl_set_input(bbox_->input_bb_x1, token(bb,' ',2).c_str());
536                                 fl_set_input(bbox_->input_bb_y1, token(bb,' ',3).c_str());
537                                 fl_set_choice_text(bbox_->choice_bb_units, "bp");
538                         }
539                         controller().bbChanged = false;
540                 } else {
541                         fl_set_input(bbox_->input_bb_x0, "");
542                         fl_set_input(bbox_->input_bb_y0, "");
543                         fl_set_input(bbox_->input_bb_x1, "");
544                         fl_set_input(bbox_->input_bb_y1, "");
545                         fl_set_choice_text(bbox_->choice_bb_units, "bp");
546                 }
547         // the extra section
548         } else if (ob == extra_->check_subcaption) {
549                 setEnabled(extra_->input_subcaption,
550                            fl_get_button(extra_->check_subcaption));
551
552         }
553
554         // check if the input is valid
555         bool const invalid = !isValid(file_->input_width) || !isValid(file_->input_height);
556
557         // deactivate OK / Apply buttons and spit out warnings if invalid
558         if (invalid) {
559                 postWarning(_("Invalid Length in Output size!"));
560                 return ButtonPolicy::SMI_INVALID;
561         } else {
562                 clearMessage();
563                 return ButtonPolicy::SMI_VALID;
564         }
565 }