]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormGraphics.C
c040b3bda4c345f9f00aae0bc0cb4134c36f796e
[features.git] / src / frontends / xforms / FormGraphics.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file FormGraphics.C
11  * \author Baruch Even, baruch.even@writeme.com
12  */
13
14 #include <config.h> 
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif 
19
20 #include "xformsBC.h"
21 #include "ControlGraphics.h"
22 #include "FormGraphics.h"
23 #include "form_graphics.h"
24
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
33
34 using std::endl;
35
36 namespace {
37
38 // Bound the number of input characters
39 int const SCALE_MAXDIGITS = 3;
40 int const WIDTH_MAXDIGITS = 3;
41 int const HEIGHT_MAXDIGITS = 3;
42 int const ROTATE_MAXCHARS = 4;
43 int const FILENAME_MAXCHARS = 1024;
44  
45 } // namespace anon
46
47
48 typedef FormCB<ControlGraphics, FormDB<FD_form_graphics> > base_class;
49
50 FormGraphics::FormGraphics(ControlGraphics & c)
51         : base_class(c, _("Graphics"))
52 {}
53
54
55 void FormGraphics::redraw()
56 {
57         if(form() && form()->visible)
58                 fl_redraw_form(form());
59         else
60                 return;
61
62         FL_FORM * outer_form = fl_get_active_folder(dialog_->tabFolder);
63         if (outer_form && outer_form->visible)
64                 fl_redraw_form(outer_form);
65 }
66
67
68 void FormGraphics::build()
69 {
70         dialog_.reset(build_graphics());
71
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);
77
78         // the file section
79         file_.reset(build_file());
80
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);
86
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);
93
94         bc().addReadOnly(file_->button_browse);
95         bc().addReadOnly(file_->check_subcaption);
96         bc().addReadOnly(file_->choice_display);
97
98         // the size section
99         size_.reset(build_size());
100
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);
104
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);
108
109         fl_set_input_filter(size_->input_scale,  fl_unsigned_float_filter);
110
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());
113
114         bc().addReadOnly(size_->button_default);
115         bc().addReadOnly(size_->button_wh);
116         bc().addReadOnly(size_->button_scale);
117         bc().addReadOnly(size_->check_aspectratio);
118
119         // the bounding box selection
120         bbox_.reset(build_bbox());
121         fl_set_input_return (bbox_->input_bbx0, FL_RETURN_CHANGED);
122         fl_set_input_return (bbox_->input_bby0, FL_RETURN_CHANGED);
123         fl_set_input_return (bbox_->input_bbx1, FL_RETURN_CHANGED);
124         fl_set_input_return (bbox_->input_bby1, FL_RETURN_CHANGED);
125
126         bc().addReadOnly(bbox_->button_getBB);
127         bc().addReadOnly(bbox_->button_clip);
128         bc().addReadOnly(bbox_->button_draft);
129
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());
135
136
137         // the rotate section
138         special_.reset(build_special());
139
140         fl_set_input_return (special_->input_rotate_angle, FL_RETURN_CHANGED);
141
142         string const choice_origin = 
143             "default|"                                  // not important
144             "leftTop|leftBottom|leftBaseline|"          // lt lb lB
145             "center|"                                   // c
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());
150
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);
154
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);
160 }
161
162
163 void FormGraphics::apply()
164 {
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     
170         else {
171             string bb;
172             if (getStringFromInput(bbox_->input_bbx0).empty())
173                 bb = "0 ";
174             else
175                 bb = getLengthFromWidgets(bbox_->input_bbx0,
176                         bbox_->choice_bb_x0)+" ";
177             if (getStringFromInput(bbox_->input_bby0).empty())
178                 bb += "0 ";
179             else
180                 bb += (getLengthFromWidgets(bbox_->input_bby0,
181                         bbox_->choice_bb_y0)+" ");
182             if (getStringFromInput(bbox_->input_bbx1).empty())
183                 bb += "0 ";
184             else
185                 bb += (getLengthFromWidgets(bbox_->input_bbx1,
186                         bbox_->choice_bb_x1)+" ");
187             if (getStringFromInput(bbox_->input_bby1).empty())
188                 bb += "0 ";
189             else
190                 bb += (getLengthFromWidgets(bbox_->input_bby1,
191                         bbox_->choice_bb_y1)+" ");
192             igp.bb = bb;
193         }
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;
216         }
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;
221         else
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));
233
234         igp.rotateAngle =
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;
241                 }
242         }
243         if (fl_get_choice(special_->choice_origin) > 0)
244             igp.rotateOrigin = fl_get_choice_text(special_->choice_origin);
245         else
246             igp.rotateOrigin = string();
247         igp.special = getStringFromInput(special_->input_special);
248         igp.testInvariant();
249 }
250
251
252 void FormGraphics::update()
253 {       string unit = "cm";
254         if (lyxrc.default_papersize < 3)
255             unit = "in";
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";
268             if (!bb.empty()) {          
269                 // get the values from the file
270                 // in this case we always have the point-unit
271                 fl_set_input(bbox_->input_bbx0, token(bb,' ',0).c_str());
272                 fl_set_input(bbox_->input_bby0, token(bb,' ',1).c_str());
273                 fl_set_input(bbox_->input_bbx1, token(bb,' ',2).c_str());
274                 fl_set_input(bbox_->input_bby1, token(bb,' ',3).c_str());
275             }
276         } else {                                // get the values from the inset
277             controller().bbChanged = true;
278             LyXLength anyLength;
279             anyLength = LyXLength(token(igp.bb,' ',0));
280             updateWidgetsFromLength(bbox_->input_bbx0,
281                         bbox_->choice_bb_x0,anyLength,"pt");
282             anyLength = LyXLength(token(igp.bb,' ',1));
283             updateWidgetsFromLength(bbox_->input_bby0,
284                         bbox_->choice_bb_y0,anyLength,"pt");
285             anyLength = LyXLength(token(igp.bb,' ',2));
286             updateWidgetsFromLength(bbox_->input_bbx1,
287                         bbox_->choice_bb_x1,anyLength,"pt");
288             anyLength = LyXLength(token(igp.bb,' ',3));
289             updateWidgetsFromLength(bbox_->input_bby1,
290                         bbox_->choice_bb_y1,anyLength,"pt");
291         }
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));
300
301         switch (igp.display) {
302             case InsetGraphicsParams::NONE: {   // dont't display
303                 fl_set_choice(file_->choice_display, 5);
304                 break;
305             }
306             case InsetGraphicsParams::MONOCHROME: {
307                 fl_set_choice(file_->choice_display, 2);
308                 break;
309             }
310             case InsetGraphicsParams::GRAYSCALE: {
311                 fl_set_choice(file_->choice_display, 3);
312                 break;
313             }
314             case InsetGraphicsParams::COLOR: {
315                 fl_set_choice(file_->choice_display, 4);
316                 break;
317             }
318         }
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);
332                 break;
333             }
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);
341                 break;
342             }
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);
350                 break;
351             }
352         }
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);
364         else
365             fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
366         fl_set_input(special_->input_special, igp.special.c_str());
367 }
368
369
370 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
371 {
372         if (ob == file_->button_browse) {
373                 // Get the filename from the dialog
374                 string const in_name = getStringFromInput(file_->input_filename);
375                 string const out_name = controller().Browse(in_name);
376                 if (out_name != in_name && !out_name.empty()) {
377                         fl_set_input(file_->input_filename, out_name.c_str());
378                 }
379         } else if (!controller().bbChanged && 
380                     ((ob == bbox_->input_bbx0) || (ob == bbox_->input_bby0) ||
381                     (ob == bbox_->input_bbx1) || (ob == bbox_->input_bby1) ||
382                     (ob == bbox_->choice_bb_x0) || (ob == bbox_->choice_bb_y0) ||
383                     (ob == bbox_->choice_bb_x1) || (ob == bbox_->choice_bb_y1))) {
384             controller().bbChanged = true; 
385         } else if (ob == size_->button_default) {
386                 setEnabled(size_->input_width, 0);
387                 setEnabled(size_->choice_width_units, 0);
388                 setEnabled(size_->input_height, 0);
389                 setEnabled(size_->choice_height_units, 0);
390                 setEnabled(size_->input_scale, 0);
391         } else if (ob == size_->button_wh) {
392                 setEnabled(size_->input_width, 1);
393                 setEnabled(size_->choice_width_units, 1);
394                 setEnabled(size_->input_height, 1);
395                 setEnabled(size_->choice_height_units, 1);
396                 setEnabled(size_->input_scale, 0);
397         } else if (ob == size_->button_scale) {
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, 1);
403         } else if (ob == file_->check_subcaption) {
404                 setEnabled(file_->input_subcaption,
405                            fl_get_button(file_->check_subcaption));
406         } else if (ob == bbox_->button_getBB) {
407             string const filename = getStringFromInput(file_->input_filename);
408             if (!filename.empty()) {
409                 string const fileWithAbsPath = MakeAbsPath(filename, OnlyPath(filename));       
410                 string bb = controller().readBB(fileWithAbsPath);
411                 lyxerr << "getBB::BoundingBox = " << bb << "\n";
412                 if (!bb.empty()) {              
413                     fl_set_input(bbox_->input_bbx0, token(bb,' ',0).c_str());
414                     fl_set_input(bbox_->input_bby0, token(bb,' ',1).c_str());
415                     fl_set_input(bbox_->input_bbx1, token(bb,' ',2).c_str());
416                     fl_set_input(bbox_->input_bby1, token(bb,' ',3).c_str());
417                 }
418                 controller().bbChanged = false;
419             }
420         } else if (ob == dialog_->button_help) {
421             controller().help();
422         }
423                 // Put verifications that the dialog shows some sane values,
424         // if not disallow clicking on ok/apply.
425         // Possibly use a label in the bottom of the dialog to give the reason.
426         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
427
428         string input;
429         bool invalid = false;
430
431         // check if the input is valid
432         input = fl_get_input(bbox_->input_bbx0);
433         invalid = !input.empty() && !isValidLength(input) && !isStrDbl(input);
434         input = fl_get_input(bbox_->input_bbx1);
435         invalid = invalid || (!input.empty() && !isValidLength(input) && !isStrDbl(input));
436         input = fl_get_input(bbox_->input_bby0);
437         invalid = invalid || (!input.empty() && !isValidLength(input) && !isStrDbl(input));
438         input = fl_get_input(bbox_->input_bby1);
439         invalid = invalid || (!input.empty() && !isValidLength(input) && !isStrDbl(input));
440         input = fl_get_input(size_->input_width);
441         invalid = invalid || !input.empty() && (!isValidLength(input) && !isStrDbl(input));
442         input = fl_get_input(size_->input_height);
443         invalid = invalid || !input.empty() && (!isValidLength(input) && !isStrDbl(input));
444         input = fl_get_input(file_->input_lyxwidth);
445         invalid = invalid || (!input.empty() && !isValidLength(input) && !isStrDbl(input));
446         input = fl_get_input(file_->input_lyxheight);
447         invalid = invalid || (!input.empty() && !isValidLength(input) && !isStrDbl(input));
448
449         // deactivate OK/ Apply buttons and
450         // spit out warnings if invalid
451         if (ob == bbox_->input_bbx0
452                         || ob == bbox_->input_bbx1
453                         || ob == bbox_->input_bby0
454                         || ob == bbox_->input_bby1
455                         || ob == size_->input_width
456                         || ob == size_->input_height
457                         || ob == file_->input_lyxwidth
458                         || ob == file_->input_lyxheight) {
459                 if (invalid) {
460                         fl_set_object_label(dialog_->text_warning,
461                                 _("Warning: Invalid Length!"));
462                         fl_show_object(dialog_->text_warning);
463                         activate = ButtonPolicy::SMI_INVALID;
464                 } else {
465                         fl_hide_object(dialog_->text_warning);
466                         activate = ButtonPolicy::SMI_VALID;
467                 }
468                 return activate;
469         }
470         return checkInput();
471 }
472
473
474 ButtonPolicy::SMInput FormGraphics::checkInput()
475 {
476         // Put verifications that the dialog shows some sane values,
477         // if not disallow clicking on ok/apply.
478         // Possibly use a label in the bottom of the dialog to give the reason.
479         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
480         // We verify now that there is a filename, it exists, it's a file
481         // and it's readable.
482         string filename = getStringFromInput(file_->input_filename);
483         FileInfo file(filename);
484         if (filename.empty() || !file.isOK() || !file.exist()
485                             || !file.isRegular() || !file.readable()
486            )
487                 activate = ButtonPolicy::SMI_INVALID;
488
489         return activate;
490 }
491