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