]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormGraphics.C
Add fl_set_input_return to input_paperoption.
[lyx.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 "input_validators.h"
26 #include "debug.h" // for lyxerr
27 #include "support/lstrings.h"  // for strToDbl & tostr
28 #include "support/FileInfo.h"  // for FileInfo
29 #include "insets/insetgraphicsParams.h"
30
31 using std::endl;
32
33 typedef FormCB<ControlGraphics, FormDB<FD_form_graphics> > base_class;
34
35 FormGraphics::FormGraphics(ControlGraphics & c)
36         : base_class(c, _("Graphics")),
37           // The buttons c-tor values are the number of buttons we use
38           // This is only to reduce memory waste.
39           widthButtons(5), heightButtons(4), displayButtons(4),
40           last_image_path(".")
41 {}
42
43
44 void FormGraphics::build()
45 {
46         dialog_.reset(build_graphics());
47
48         // This is the place to add settings of the dialog that did not go
49         // to the .fd file.
50
51         // Set the input widgets to issue a callback to input() whenever
52         // they change, so we can verify their content.
53         fl_set_input_return (dialog_->input_width,
54                               FL_RETURN_CHANGED);
55         fl_set_input_return (dialog_->input_height,
56                               FL_RETURN_CHANGED);
57         fl_set_input_return (dialog_->input_filename,
58                               FL_RETURN_CHANGED);
59         fl_set_input_return (dialog_->input_rotate_angle,
60                               FL_RETURN_CHANGED);
61
62         // Set the maximum characters that can be written in the input texts.
63         fl_set_input_maxchars(dialog_->input_width, WIDTH_MAXDIGITS);
64         fl_set_input_maxchars(dialog_->input_height, HEIGHT_MAXDIGITS);
65         fl_set_input_maxchars(dialog_->input_filename, FILENAME_MAXCHARS);
66         fl_set_input_maxchars(dialog_->input_rotate_angle, ROTATE_MAXCHARS);
67
68         // Set input filter on width and height to make them accept only
69         // unsigned numbers.
70         fl_set_input_filter(dialog_->input_width,
71                             fl_unsigned_int_filter);
72         fl_set_input_filter(dialog_->input_height,
73                             fl_unsigned_int_filter);
74
75         // Set input filter on rotate_angle to make it accept only
76         // floating point numbers.
77         fl_set_input_filter(dialog_->input_rotate_angle, fl_float_filter);
78
79         // Add the widgets of the width radio buttons to their group
80         widthButtons.reset();
81         widthButtons.registerRadioButton(dialog_->radio_width_default,
82                                          InsetGraphicsParams::DEFAULT_SIZE);
83         widthButtons.registerRadioButton(dialog_->radio_width_cm,
84                                          InsetGraphicsParams::CM);
85         widthButtons.registerRadioButton(dialog_->radio_width_inch,
86                                          InsetGraphicsParams::INCH);
87         widthButtons.registerRadioButton(dialog_->radio_width_percent_page,
88                                          InsetGraphicsParams::PERCENT_PAGE);
89         widthButtons.registerRadioButton(dialog_->radio_width_percent_column,
90                                          InsetGraphicsParams::PERCENT_COLUMN);
91
92         // Add the widgets of the height radio buttons to their group
93         heightButtons.reset();
94         heightButtons.registerRadioButton(dialog_->radio_height_default,
95                                           InsetGraphicsParams::DEFAULT_SIZE);
96         heightButtons.registerRadioButton(dialog_->radio_height_cm,
97                                           InsetGraphicsParams::CM);
98         heightButtons.registerRadioButton(dialog_->radio_height_inch,
99                                           InsetGraphicsParams::INCH);
100         heightButtons.registerRadioButton(dialog_->radio_height_percent_page,
101                                           InsetGraphicsParams::PERCENT_PAGE);
102         heightButtons.registerRadioButton(dialog_->radio_height_scale,
103                                           InsetGraphicsParams::SCALE);
104
105         // Add the widgets of the display radio buttons to their group
106         displayButtons.reset();
107         displayButtons.registerRadioButton(dialog_->radio_display_color,
108                                            InsetGraphicsParams::COLOR);
109         displayButtons.registerRadioButton(dialog_->radio_display_grayscale,
110                                            InsetGraphicsParams::GRAYSCALE);
111         displayButtons.registerRadioButton(dialog_->radio_display_monochrome,
112                                            InsetGraphicsParams::MONOCHROME);
113         displayButtons.registerRadioButton(dialog_->radio_no_display,
114                                            InsetGraphicsParams::NONE);
115
116         // Manage the ok, apply, restore and cancel/close buttons
117         bc().setOK(dialog_->button_ok);
118         bc().setApply(dialog_->button_apply);
119         bc().setCancel(dialog_->button_cancel);
120         bc().setRestore(dialog_->button_restore);
121
122         bc().addReadOnly(dialog_->input_filename);
123         bc().addReadOnly(dialog_->button_browse);
124         bc().addReadOnly(dialog_->input_width);
125         bc().addReadOnly(dialog_->input_height);
126         bc().addReadOnly(dialog_->radio_button_group_width);
127         bc().addReadOnly(dialog_->radio_button_group_height);
128         bc().addReadOnly(dialog_->radio_button_group_display);
129         bc().addReadOnly(dialog_->input_rotate_angle);
130         bc().addReadOnly(dialog_->input_subcaption);
131         bc().addReadOnly(dialog_->check_subcaption);
132 }
133
134
135 void FormGraphics::apply()
136 {
137         // Create the parameters structure and fill the data from the dialog.
138         InsetGraphicsParams & igp = controller().params();
139
140         igp.filename = fl_get_input(dialog_->input_filename);
141
142         igp.display = static_cast < InsetGraphicsParams::DisplayType >
143                       (displayButtons.getButton());
144
145         igp.widthResize = static_cast < InsetGraphicsParams::Resize >
146                           (widthButtons.getButton());
147         igp.widthSize = strToDbl(fl_get_input(dialog_->input_width));
148
149         igp.heightResize = static_cast < InsetGraphicsParams::Resize >
150                            (heightButtons.getButton());
151         igp.heightSize = strToDbl(fl_get_input(dialog_->input_height));
152
153         igp.rotateAngle = strToDbl(fl_get_input(dialog_->input_rotate_angle));
154         /* // Need to redo it for floats, but I'm lazy now - BE 20010725
155         if (igp.rotateAngle >= 360)
156                 igp.rotateAngle = igp.rotateAngle % 360;
157         if (igp.rotateAngle <= -360)
158                 igp.rotateAngle = - (( -igp.rotateAngle) % 360);
159         */
160
161         igp.subcaption = fl_get_button(dialog_->check_subcaption);
162         igp.subcaptionText = fl_get_input(dialog_->input_subcaption);
163
164         igp.testInvariant();
165 }
166
167
168 void FormGraphics::update()
169 {
170         // Update dialog with details from inset
171         InsetGraphicsParams & igp = controller().params();
172
173         // Update the filename input field
174         fl_set_input(dialog_->input_filename,
175                      igp.filename.c_str());
176
177         // Update the display depth radio buttons
178         displayButtons.setButton(igp.display);
179
180         // Update the width radio buttons and input field
181         widthButtons.setButton(igp.widthResize);
182         fl_set_input(dialog_->input_width,
183                      tostr(igp.widthSize).c_str());
184
185         // Update the height radio buttons and input field
186         heightButtons.setButton(igp.heightResize);
187         fl_set_input(dialog_->input_height,
188                      tostr(igp.heightSize).c_str());
189
190         // Update the rotate angle
191         fl_set_input(dialog_->input_rotate_angle,
192                      tostr(igp.rotateAngle).c_str());
193
194         // Update the subcaption check button and input field
195         fl_set_button(dialog_->check_subcaption,
196                       igp.subcaption);
197         fl_set_input(dialog_->input_subcaption,
198                      igp.subcaptionText.c_str());
199
200         // Now make sure that the buttons are set correctly.
201         input(0, 0);
202 }
203
204
205 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT *, long data)
206 {
207         State cb = static_cast<State>( data );
208
209         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
210
211         switch (cb) {
212         case CHECKINPUT:
213                 activate = checkInput();
214                 break;
215         case BROWSE:
216         {
217                 // Get the filename from the dialog
218                 string const in_name = fl_get_input(dialog_->input_filename);
219                 string const out_name = controller().Browse(in_name);
220
221                 if (out_name != in_name && !out_name.empty()) {
222                         fl_set_input(dialog_->input_filename, out_name.c_str());
223                         activate = input(0, CHECKINPUT);
224                 }
225                 break;
226         }
227
228         case ADVANCEDINPUT:
229                 lyxerr << "Advanced Options button depressed, "
230                        << "show advanced options dialog"
231                        << endl;
232                 break;
233         default:
234                 break;
235         }
236         
237         return activate;
238 }
239
240
241 ButtonPolicy::SMInput FormGraphics::checkInput()
242 {
243         // Put verifications that the dialog shows some sane values,
244         // if not disallow clicking on ok/apply.
245         // Possibly use a label in the bottom of the dialog to give the reason.
246
247         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
248
249         // Things that we check (meaning they are incorrect states):
250         // 1. No filename specified.
251         // 2. Width radio button is not Default and width text is not a number.
252         // 3. Height radio button is not Default and height text is a not a number
253
254         // Note: radio button default means that the user asks for the image
255         // to be included as is with no size change, in this case we don't need
256         // any width or height.
257
258         // We verify now that there is a filename, it exists, it's a file
259         // and it's readable.
260         string filename = fl_get_input(dialog_->input_filename);
261         FileInfo file(filename);
262         if (filename.empty()
263                 || !file.isOK()
264                 || !file.exist()
265                 || !file.isRegular()
266                 || !file.readable()
267            )
268                 activate = ButtonPolicy::SMI_NOOP;
269
270         // Width radio button not default and no number.
271         if (!fl_get_button(dialog_->radio_width_default)
272                 && strToDbl(fl_get_input(dialog_->input_width)) <= 0.0) {
273
274                 activate = ButtonPolicy::SMI_NOOP;
275         }
276
277         // Height radio button not default and no number.
278         if (!fl_get_button(dialog_->radio_height_default)
279                 && strToDbl(fl_get_input(dialog_->input_height)) <= 0.0) {
280
281                 activate = ButtonPolicy::SMI_NOOP;
282         }
283
284         return activate;
285 }