]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormGraphics.C
ed4469a3ad4eaf2c867be501489082fa2cdcb4fb
[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 FILENAME_MAXCHARS = 1024;
40
41 string defaultUnit("cm");
42
43 /// Given input and choice widgets, create a LyXLength
44 LyXLength getLyXLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
45 {
46         return LyXLength(getLengthFromWidgets(input, choice));
47 }
48
49 } // namespace anon
50
51
52 typedef FormCB<ControlGraphics, FormDB<FD_graphics> > base_class;
53
54 FormGraphics::FormGraphics()
55         : base_class(_("Graphics"), false)
56 {}
57
58
59 void FormGraphics::redraw()
60 {
61         if (form() && form()->visible)
62                 fl_redraw_form(form());
63         else
64                 return;
65         FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
66         if (outer_form && outer_form->visible)
67                 fl_redraw_form(outer_form);
68 }
69
70
71 void FormGraphics::build()
72 {
73         dialog_.reset(build_graphics(this));
74
75         // Allow the base class to control messages
76         setMessageWidget(dialog_->text_warning);
77
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);
83
84         // the file section
85         file_.reset(build_graphics_file(this));
86
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);
91
92         setPrehandler(file_->input_filename);
93         setPrehandler(file_->input_lyxscale);
94         setPrehandler(file_->input_width);
95         setPrehandler(file_->input_height);
96
97         fl_set_input_maxchars(file_->input_filename, FILENAME_MAXCHARS);
98         fl_set_input_filter(file_->input_lyxscale, fl_unsigned_int_filter);
99
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);
103
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());
107
108         bc().addReadOnly(file_->button_browse);   
109         bc().addReadOnly(file_->check_aspectratio);
110         bc().addReadOnly(file_->check_draft);
111         bc().addReadOnly(file_->check_nounzip);
112
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);
118
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);
123
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);
135
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);
140
141         str = _("Show image only as a rectangle of the original size.");
142         tooltips().init(file_->check_draft, str);
143
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);
150
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);
155
156         setPrehandler(bbox_->input_bb_x0);
157         setPrehandler(bbox_->input_bb_y0);
158         setPrehandler(bbox_->input_bb_x1);
159         setPrehandler(bbox_->input_bb_y1);
160
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);
165
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);
177
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);
182
183         str = _("Enable this checkbox when the image should be clipped to the "
184                 "bounding box values.");
185         tooltips().init(bbox_->check_clip, str);
186
187         // the extra section
188         extra_.reset(build_graphics_extra(this));
189
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);
193
194         fl_set_input_filter(extra_->input_rotate_angle, fl_float_filter);
195
196         setPrehandler(extra_->input_rotate_angle);
197         setPrehandler(extra_->input_subcaption);
198         setPrehandler(extra_->input_special);
199
200         bc().addReadOnly(extra_->check_subcaption);
201
202         using namespace frnt;
203         vector<RotationOriginPair> origindata = getRotationOriginData();
204
205         // Store the identifiers for later
206         origins_ = getSecond(origindata);
207
208         string const choice = " " + getStringFromVector(getFirst(origindata), " | ") + " ";
209         fl_addto_choice(extra_->choice_origin, choice.c_str());
210
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);
224
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);
229
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;
240         }
241 }
242
243
244 void FormGraphics::apply()
245 {
246         // Create the parameters structure and fill the data from the dialog.
247         InsetGraphicsParams & igp = controller().params();
248
249         // the file section
250         igp.filename = getString(file_->input_filename);
251
252         igp.lyxscale = strToInt(getString(file_->input_lyxscale));
253         if (igp.lyxscale == 0) igp.lyxscale = 100;
254         
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;
260                 case 1:
261                 default: igp.display = grfx::DefaultDisplay;
262         }
263
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();
269         }
270         else {
271                 igp.scale = 0;
272                 igp.width = getLyXLengthFromWidgets(file_->input_width,
273                                                     file_->choice_width);
274         }
275         igp.height = getLyXLengthFromWidgets(file_->input_height,
276                                              file_->choice_height);
277         igp.keepAspectRatio = fl_get_button(file_->check_aspectratio);
278
279         igp.draft = fl_get_button(file_->check_draft);
280         igp.noUnzip = fl_get_button(file_->check_nounzip);
281
282         // the bb section
283         if (!controller().bbChanged)    // different to the original one?
284                 igp.bb = string();      // don't write anything
285         else {
286                 string bb;
287                 if (getString(bbox_->input_bb_x0).empty())
288                         bb = "0 ";
289                 else
290                         bb = getLengthFromWidgets(bbox_->input_bb_x0,
291                                                   bbox_->choice_bb_units)+" ";
292                 if (getString(bbox_->input_bb_y0).empty())
293                         bb += "0 ";
294                 else
295                         bb += (getLengthFromWidgets(bbox_->input_bb_y0,
296                                                     bbox_->choice_bb_units)+" ");
297                 if (getString(bbox_->input_bb_x1).empty())
298                         bb += "0 ";
299                 else
300                         bb += (getLengthFromWidgets(bbox_->input_bb_x1,
301                                                     bbox_->choice_bb_units)+" ");
302                 if (getString(bbox_->input_bb_y1).empty())
303                         bb += "0 ";
304                 else
305                         bb += (getLengthFromWidgets(bbox_->input_bb_y1,
306                                                     bbox_->choice_bb_units)+" ");
307                 igp.bb = bb;
308         }
309         igp.clip = fl_get_button(bbox_->check_clip);
310
311         // the extra section
312         igp.rotateAngle = strToDbl(getString(extra_->input_rotate_angle));
313         
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());
318
319         int const origin_pos = fl_get_choice(extra_->choice_origin);
320         igp.rotateOrigin = origins_[origin_pos-1];
321
322         igp.subcaption = fl_get_button(extra_->check_subcaption);
323         igp.subcaptionText = getString(extra_->input_subcaption);
324
325         igp.special = getString(extra_->input_special);
326 }
327
328
329 void FormGraphics::update() {
330         // Update dialog with details from inset
331         InsetGraphicsParams & igp = controller().params();
332
333         // the file section
334         fl_set_input(file_->input_filename, igp.filename.c_str());
335         fl_set_input(file_->input_lyxscale, tostr(igp.lyxscale).c_str());
336
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);
344         }
345
346         // disable height input in case of scaling
347         setEnabled(file_->input_height, !igp.scale);
348         setEnabled(file_->choice_height, !igp.scale);
349
350         // set width input fields according to scaling or width/height input
351         if (igp.scale) {
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);
355         }
356         else {
357                 fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
358                 updateWidgetsFromLength(file_->input_width,
359                                         file_->choice_width, igp.width, defaultUnit);
360         }
361
362         updateWidgetsFromLength(file_->input_height,
363                                 file_->choice_height, igp.height, defaultUnit);
364         
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);
368
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);
374
375         // the bb section
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);
380
381
382         // the extra section
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);
387         else
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());
395
396         // open dialog in the file-tab, whenever filename is empty
397         if (igp.filename.empty()) fl_set_folder(dialog_->tabfolder, file_->form);
398 }
399
400
401 void FormGraphics::updateBB(string const & filename, string const & bb_inset)
402 {
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);
410                 if (!bb.empty()) {
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());
421
422                 } else {
423                         // no bb from file
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());
428                 }
429                 // "bp"
430                 fl_set_choice(bbox_->choice_bb_units, 1);
431
432         } else {
433                 // get the values from the inset
434                 lyxerr[Debug::GRAPHICS] << "update:: igp has BoundingBox"
435                                         << endl;
436                 controller().bbChanged = true;
437                 LyXLength anyLength;
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");
450         }
451 }
452
453 namespace {
454
455 bool isValid(FL_OBJECT * ob)
456 {
457         string const input = getString(ob);
458         return input.empty() || isValidLength(input) || isStrDbl(input);
459 }
460
461 } // namespace anon
462
463
464
465 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
466 {
467         // the file section
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());
475                 }
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);
490                 
491                 // allow only integer intput for scaling; float otherwise
492                 if (scaling)
493                         fl_set_input_filter(file_->input_width, fl_unsigned_int_filter);
494                 else
495                         fl_set_input_filter(file_->input_width, fl_unsigned_float_filter);
496
497
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);
501         // the bb section
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);
511                         if (!bb.empty()) {
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");
517                         }
518                         controller().bbChanged = false;
519                 } else {
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");
525                 }
526         // the extra section
527         } else if (ob == extra_->check_subcaption) {
528                 setEnabled(extra_->input_subcaption,
529                            fl_get_button(extra_->check_subcaption));
530
531         }
532
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) {
538                 if (isValid(ob))
539                         clearMessage();
540                 else {
541                         postWarning(_("Invalid Length!"));
542                         return ButtonPolicy::SMI_INVALID;
543                 }
544         }
545
546         return ButtonPolicy::SMI_VALID;
547 }