]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormGraphics.C
81a51d3eb9e0536ba62f3ef954946716027b1669
[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_bbx0, FL_RETURN_CHANGED);
84         fl_set_input_return (file_->input_bby0, FL_RETURN_CHANGED);
85         fl_set_input_return (file_->input_bbx1, FL_RETURN_CHANGED);
86         fl_set_input_return (file_->input_bby1, FL_RETURN_CHANGED);
87         fl_set_input_maxchars(file_->input_filename,  FILENAME_MAXCHARS);
88
89         string const bb_units = "pt|cm|in";
90         fl_addto_choice(file_->choice_bb_x0, bb_units.c_str());
91         fl_addto_choice(file_->choice_bb_y0, bb_units.c_str());
92         fl_addto_choice(file_->choice_bb_x1, bb_units.c_str());
93         fl_addto_choice(file_->choice_bb_y1, bb_units.c_str());
94
95         bc().addReadOnly(file_->button_browse);
96         bc().addReadOnly(file_->check_subcaption);
97         bc().addReadOnly(file_->button_getBB);
98         bc().addReadOnly(file_->button_clip);
99         bc().addReadOnly(file_->button_draft);
100
101         // the size section
102         size_.reset(build_size());
103
104         fl_set_input_return (size_->input_scale, FL_RETURN_CHANGED);
105         fl_set_input_return (size_->input_width, FL_RETURN_CHANGED);
106         fl_set_input_return (size_->input_height, FL_RETURN_CHANGED);
107         fl_set_input_return (size_->input_lyxwidth, FL_RETURN_CHANGED);
108         fl_set_input_return (size_->input_lyxheight, FL_RETURN_CHANGED);
109
110         fl_set_input_maxchars(size_->input_scale, SCALE_MAXDIGITS);
111         fl_set_input_maxchars(size_->input_width, WIDTH_MAXDIGITS);
112         fl_set_input_maxchars(size_->input_height, HEIGHT_MAXDIGITS);
113
114         fl_set_input_filter(size_->input_scale,  fl_unsigned_float_filter);
115         fl_set_input_filter(size_->input_width,  fl_unsigned_float_filter);
116         fl_set_input_filter(size_->input_height, fl_unsigned_float_filter);
117
118         fl_addto_choice(size_->choice_width_units, choice_Length_All.c_str());
119         fl_addto_choice(size_->choice_height_units, choice_Length_All.c_str());
120         fl_addto_choice(size_->choice_width_lyxwidth, choice_Length_All.c_str());
121         fl_addto_choice(size_->choice_width_lyxheight, choice_Length_All.c_str());
122
123         bc().addReadOnly(size_->button_default);
124         bc().addReadOnly(size_->button_wh);
125         bc().addReadOnly(size_->button_scale);
126         bc().addReadOnly(size_->check_aspectratio);
127         bc().addReadOnly(size_->radio_check_display);
128         bc().addReadOnly(size_->radio_display_mono);
129         bc().addReadOnly(size_->radio_display_gray);
130         bc().addReadOnly(size_->radio_display_color);
131
132         // the rotate section
133         special_.reset(build_special());
134
135         fl_set_input_return (special_->input_rotate_angle, FL_RETURN_CHANGED);
136
137         string const choice_origin = 
138             "default|"                                  // not important
139             "leftTop|leftBottom|leftBaseline|"          // lt lb lB
140             "center|"                                   // c
141             "centerTop|centerBottom|centerBaseline|"    // ct cb cB
142             "rightTop|rightBottom|rightBaseline|"       // rt rb rB
143             "referencePoint";                           // special
144         fl_addto_choice(special_->choice_origin, choice_origin.c_str());
145
146         fl_set_input_return (special_->input_special, FL_RETURN_CHANGED);
147         fl_set_input_maxchars(special_->input_rotate_angle, ROTATE_MAXCHARS);
148         fl_set_input_filter(special_->input_rotate_angle, fl_float_filter);
149
150         // add the different tabfolders
151         fl_addto_tabfolder(dialog_->tabFolder, _("Filename"), file_->form);
152         fl_addto_tabfolder(dialog_->tabFolder, _("Image size"), size_->form);
153         fl_addto_tabfolder(dialog_->tabFolder, _("Special"), special_->form);
154 }
155
156
157 void FormGraphics::apply()
158 {
159         // Create the parameters structure and fill the data from the dialog.
160         InsetGraphicsParams & igp = controller().params();
161         igp.filename = getStringFromInput(file_->input_filename);
162         if (!controller().bbChanged)            // different to the original one?
163             igp.bb = string();                  // don't write anything     
164         else {
165             string bb;
166             if (getStringFromInput(file_->input_bbx0).empty())
167                 bb = "0 ";
168             else
169                 bb = getLengthFromWidgets(file_->input_bbx0,file_->choice_bb_x0)+" ";
170             if (getStringFromInput(file_->input_bby0).empty())
171                 bb += "0 ";
172             else
173                 bb += (getLengthFromWidgets(file_->input_bby0,file_->choice_bb_y0)+" ");
174             if (getStringFromInput(file_->input_bbx1).empty())
175                 bb += "0 ";
176             else
177                 bb += (getLengthFromWidgets(file_->input_bbx1,file_->choice_bb_x1)+" ");
178             if (getStringFromInput(file_->input_bby1).empty())
179                 bb += "0 ";
180             else
181                 bb += (getLengthFromWidgets(file_->input_bby1,file_->choice_bb_y1)+" ");
182             igp.bb = bb;
183         }
184         igp.draft = fl_get_button(file_->button_draft);
185         igp.clip = fl_get_button(file_->button_clip);
186         igp.subcaption = fl_get_button(file_->check_subcaption);
187         igp.subcaptionText = getStringFromInput(file_->input_subcaption);
188         if (fl_get_button(size_->radio_check_display)) {
189                 igp.display = InsetGraphicsParams::NONE;
190         } else if (fl_get_button(size_->radio_display_mono)) {
191                 igp.display = InsetGraphicsParams::MONOCHROME;
192         } else if (fl_get_button(size_->radio_display_gray)) {
193                 igp.display = InsetGraphicsParams::GRAYSCALE;
194         } else if (fl_get_button(size_->radio_display_color)) {
195                 igp.display = InsetGraphicsParams::COLOR;
196         }
197         if (fl_get_button(size_->button_default))
198             igp.size_type = InsetGraphicsParams::DEFAULT_SIZE;
199         else if (fl_get_button(size_->button_wh))
200             igp.size_type = InsetGraphicsParams::WH;
201         else
202             igp.size_type = InsetGraphicsParams::SCALE;
203         igp.width = LyXLength(getLengthFromWidgets(size_->input_width,size_->choice_width_units));
204         igp.height = LyXLength(getLengthFromWidgets(size_->input_height,size_->choice_height_units));
205         igp.scale = strToInt(getStringFromInput(size_->input_scale));
206         igp.keepAspectRatio = fl_get_button(size_->check_aspectratio);
207         igp.lyxwidth = LyXLength(getLengthFromWidgets(size_->input_lyxwidth,size_->choice_width_lyxwidth));
208         igp.lyxheight = LyXLength(getLengthFromWidgets(size_->input_lyxheight,size_->choice_width_lyxheight));
209
210         igp.rotateAngle =
211                 strToDbl(getStringFromInput(special_->input_rotate_angle));
212         while (igp.rotateAngle < 0.0 || igp.rotateAngle > 360.0) {
213                 if (igp.rotateAngle < 0.0) {
214                         igp.rotateAngle += 360.0;
215                 } else if (igp.rotateAngle > 360.0) {
216                         igp.rotateAngle -= 360.0;
217                 }
218         }
219         if (fl_get_choice(special_->choice_origin) > 0)
220             igp.rotateOrigin = fl_get_choice_text(special_->choice_origin);
221         else
222             igp.rotateOrigin = string();
223         igp.special = getStringFromInput(special_->input_special);
224         igp.testInvariant();
225 }
226
227
228 void FormGraphics::update()
229 {       string unit = "cm";
230         if (lyxrc.default_papersize < 3)
231             unit = "in";
232         string const defaultUnit = string(unit); 
233         // Update dialog with details from inset
234         InsetGraphicsParams & igp = controller().params();
235         fl_set_input(file_->input_filename, igp.filename.c_str());
236         // set the bounding box values, if exists. First we need the whole
237         // path, because the controller knows nothing about the doc-dir
238         lyxerr << "GraphicsUpdate::BoundingBox = " << igp.bb << "\n";
239         controller().bbChanged = false;
240         if (igp.bb.empty()) {
241             string const fileWithAbsPath = MakeAbsPath(igp.filename, OnlyPath(igp.filename));   
242             string bb = controller().readBB(fileWithAbsPath);
243             lyxerr << "file::BoundingBox = " << bb << "\n";
244             if (!bb.empty()) {          
245                 // get the values from the file
246                 // in this case we always have the point-unit
247                 fl_set_input(file_->input_bbx0, token(bb,' ',0).c_str());
248                 fl_set_input(file_->input_bby0, token(bb,' ',1).c_str());
249                 fl_set_input(file_->input_bbx1, token(bb,' ',2).c_str());
250                 fl_set_input(file_->input_bby1, token(bb,' ',3).c_str());
251             }
252         } else {                                // get the values from the inset
253             controller().bbChanged = true;
254             LyXLength anyLength;
255             anyLength = LyXLength(token(igp.bb,' ',0));
256             updateWidgetsFromLength(file_->input_bbx0,file_->choice_bb_x0,anyLength,"pt");
257             anyLength = LyXLength(token(igp.bb,' ',1));
258             updateWidgetsFromLength(file_->input_bby0,file_->choice_bb_y0,anyLength,"pt");
259             anyLength = LyXLength(token(igp.bb,' ',2));
260             updateWidgetsFromLength(file_->input_bbx1,file_->choice_bb_x1,anyLength,"pt");
261             anyLength = LyXLength(token(igp.bb,' ',3));
262             updateWidgetsFromLength(file_->input_bby1,file_->choice_bb_y1,anyLength,"pt");
263         }
264         // Update the draft and clip mode
265         fl_set_button(file_->button_draft, igp.draft);
266         fl_set_button(file_->button_clip, igp.clip);
267         // Update the subcaption check button and input field
268         fl_set_button(file_->check_subcaption, igp.subcaption);
269         fl_set_input(file_->input_subcaption, igp.subcaptionText.c_str());
270         setEnabled(file_->input_subcaption,
271                    fl_get_button(file_->check_subcaption));
272         switch (igp.display) {
273             case InsetGraphicsParams::NONE: {   // dont't display
274                 fl_set_button(size_->radio_check_display, 1);
275                 break;
276             }
277             case InsetGraphicsParams::MONOCHROME: {
278                 fl_set_button(size_->radio_display_mono, 1);
279                 break;
280             }
281             case InsetGraphicsParams::GRAYSCALE: {
282                 fl_set_button(size_->radio_display_gray, 1);
283                 break;
284             }
285             case InsetGraphicsParams::COLOR: {
286                 fl_set_button(size_->radio_display_color, 1);
287                 break;
288             }
289         }
290         updateWidgetsFromLength(size_->input_width,size_->choice_width_units,igp.width,defaultUnit);
291         updateWidgetsFromLength(size_->input_height,size_->choice_height_units,igp.height,defaultUnit);
292         fl_set_input(size_->input_scale, tostr(igp.scale).c_str());
293         switch (igp.size_type) {
294             case InsetGraphicsParams::DEFAULT_SIZE: {
295                 fl_set_button(size_->button_default,1);
296                 setEnabled(size_->input_width, 0);
297                 setEnabled(size_->choice_width_units, 0);
298                 setEnabled(size_->input_height, 0);
299                 setEnabled(size_->choice_height_units, 0);
300                 setEnabled(size_->input_scale, 0);
301                 break;
302             }
303             case InsetGraphicsParams::WH: {
304                 fl_set_button(size_->button_wh, 1);
305                 setEnabled(size_->input_width, 1);
306                 setEnabled(size_->choice_width_units, 1);
307                 setEnabled(size_->input_height, 1);
308                 setEnabled(size_->choice_height_units, 1);
309                 setEnabled(size_->input_scale, 0);
310                 break;
311             }
312             case InsetGraphicsParams::SCALE: {
313                 fl_set_button(size_->button_scale, 1);
314                 setEnabled(size_->input_width, 0);
315                 setEnabled(size_->choice_width_units, 0);
316                 setEnabled(size_->input_height, 0);
317                 setEnabled(size_->choice_height_units, 0);
318                 setEnabled(size_->input_scale, 1);
319                 break;
320             }
321         }
322         fl_set_button(size_->check_aspectratio,igp.keepAspectRatio);
323         // now the lyx-internally viewsize
324         updateWidgetsFromLength(size_->input_lyxwidth,size_->choice_width_lyxwidth,igp.lyxwidth,defaultUnit);
325         updateWidgetsFromLength(size_->input_lyxheight,size_->choice_width_lyxheight,igp.lyxheight,defaultUnit);
326         // Update the rotate angle and special commands
327         fl_set_input(special_->input_rotate_angle,
328                      tostr(igp.rotateAngle).c_str());
329         if (igp.rotateOrigin.empty())
330             fl_set_choice(special_->choice_origin,0);
331         else
332             fl_set_choice_text(special_->choice_origin,igp.rotateOrigin.c_str());
333         fl_set_input(special_->input_special, igp.special.c_str());
334 }
335
336
337 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT * ob, long)
338 {
339         if (ob == file_->button_browse) {
340                 // Get the filename from the dialog
341                 string const in_name = getStringFromInput(file_->input_filename);
342                 string const out_name = controller().Browse(in_name);
343                 if (out_name != in_name && !out_name.empty()) {
344                         fl_set_input(file_->input_filename, out_name.c_str());
345                 }
346         } else if (!controller().bbChanged && 
347                     ((ob == file_->input_bbx0) || (ob == file_->input_bby0) || 
348                     (ob == file_->input_bbx1) || (ob == file_->input_bby1) || 
349                     (ob == file_->choice_bb_x0) || (ob == file_->choice_bb_y0) || 
350                     (ob == file_->choice_bb_x1) || (ob == file_->choice_bb_y1))) {
351             controller().bbChanged = true; 
352         } else if (ob == size_->button_default) {
353                 setEnabled(size_->input_width, 0);
354                 setEnabled(size_->choice_width_units, 0);
355                 setEnabled(size_->input_height, 0);
356                 setEnabled(size_->choice_height_units, 0);
357                 setEnabled(size_->input_scale, 0);
358         } else if (ob == size_->button_wh) {
359                 setEnabled(size_->input_width, 1);
360                 setEnabled(size_->choice_width_units, 1);
361                 setEnabled(size_->input_height, 1);
362                 setEnabled(size_->choice_height_units, 1);
363                 setEnabled(size_->input_scale, 0);
364         } else if (ob == size_->button_scale) {
365                 setEnabled(size_->input_width, 0);
366                 setEnabled(size_->choice_width_units, 0);
367                 setEnabled(size_->input_height, 0);
368                 setEnabled(size_->choice_height_units, 0);
369                 setEnabled(size_->input_scale, 1);
370         } else if (ob == file_->check_subcaption) {
371                 setEnabled(file_->input_subcaption,
372                            fl_get_button(file_->check_subcaption));
373         } else if (ob == file_->button_getBB) {
374             string const filename = getStringFromInput(file_->input_filename);
375             if (!filename.empty()) {
376                 string const fileWithAbsPath = MakeAbsPath(filename, OnlyPath(filename));       
377                 string bb = controller().readBB(fileWithAbsPath);
378                 lyxerr << "getBB::BoundingBox = " << bb << "\n";
379                 if (!bb.empty()) {              
380                     fl_set_input(file_->input_bbx0, token(bb,' ',0).c_str());
381                     fl_set_input(file_->input_bby0, token(bb,' ',1).c_str());
382                     fl_set_input(file_->input_bbx1, token(bb,' ',2).c_str());
383                     fl_set_input(file_->input_bby1, token(bb,' ',3).c_str());
384                 }
385                 controller().bbChanged = false;
386             }
387         } else if (ob == dialog_->button_help) {
388             controller().help();
389         }
390         return checkInput();
391 }
392
393
394 ButtonPolicy::SMInput FormGraphics::checkInput()
395 {
396         // Put verifications that the dialog shows some sane values,
397         // if not disallow clicking on ok/apply.
398         // Possibly use a label in the bottom of the dialog to give the reason.
399         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
400         // We verify now that there is a filename, it exists, it's a file
401         // and it's readable.
402         string filename = getStringFromInput(file_->input_filename);
403         FileInfo file(filename);
404         if (filename.empty() || !file.isOK() || !file.exist()
405                             || !file.isRegular() || !file.readable()
406            )
407                 activate = ButtonPolicy::SMI_INVALID;
408
409         return activate;
410 }
411