1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * ======================================================
10 * \file FormGraphics.C
11 * \author Baruch Even, baruch.even@writeme.com
17 #pragma implementation
21 #include "ControlGraphics.h"
22 #include "FormGraphics.h"
23 #include "form_graphics.h"
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"
33 typedef FormCB<ControlGraphics, FormDB<FD_form_graphics> > base_class;
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),
44 void FormGraphics::hide()
46 // Remove all associations for the radio buttons
48 heightButtons.reset();
49 displayButtons.reset();
55 void FormGraphics::build()
57 dialog_.reset(build_graphics());
59 // This is the place to add settings of the dialog that did not go
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,
66 fl_set_input_return (dialog_->input_height,
68 fl_set_input_return (dialog_->input_filename,
70 // fl_set_input_return(dialog_->input_rotate_angle,
71 // FL_RETURN_CHANGED);
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);
79 // Set input filter on width and height to make them accept only
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);
87 // Add the widgets of the width radio buttons to their group
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);
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);
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);
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);
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);
142 void FormGraphics::apply()
144 // Create the parameters structure and fill the data from the dialog.
145 InsetGraphicsParams & igp = controller().params();
147 igp.filename = fl_get_input(dialog_->input_filename);
149 igp.display = static_cast < InsetGraphicsParams::DisplayType >
150 (displayButtons.getButton());
152 igp.widthResize = static_cast < InsetGraphicsParams::Resize >
153 (widthButtons.getButton());
154 igp.widthSize = strToDbl(fl_get_input(dialog_->input_width));
156 igp.heightResize = static_cast < InsetGraphicsParams::Resize >
157 (heightButtons.getButton());
158 igp.heightSize = strToDbl(fl_get_input(dialog_->input_height));
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);
166 igp.subcaption = fl_get_button(dialog_->check_subcaption);
167 igp.subcaptionText = fl_get_input(dialog_->input_subcaption);
169 igp.inlineFigure = fl_get_button(dialog_->check_inline);
175 void FormGraphics::update()
177 // Update dialog with details from inset
178 InsetGraphicsParams & igp = controller().params();
180 // Update the filename input field
181 fl_set_input(dialog_->input_filename,
182 igp.filename.c_str());
184 // Update the display depth radio buttons
185 displayButtons.setButton(igp.display);
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());
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());
197 // Update the rotate angle
198 fl_set_input(dialog_->input_rotate_angle,
199 tostr(igp.rotateAngle).c_str());
201 // Update the subcaption check button and input field
202 fl_set_button(dialog_->check_subcaption,
204 fl_set_input(dialog_->input_subcaption,
205 igp.subcaptionText.c_str());
207 // Update the inline figure check button
208 fl_set_button(dialog_->check_inline,
211 // Now make sure that the buttons are set correctly.
216 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT *, long data)
218 State cb = static_cast<State>( data );
220 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
224 activate = checkInput();
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);
232 if (out_name != in_name && !out_name.empty()) {
233 fl_set_input(dialog_->input_filename, out_name.c_str());
240 lyxerr << "Advanced Options button depressed, "
241 << "show advanced options dialog"
252 ButtonPolicy::SMInput FormGraphics::checkInput()
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.
258 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
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
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.
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);
279 activate = ButtonPolicy::SMI_NOOP;
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) {
285 activate = ButtonPolicy::SMI_NOOP;
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) {
292 activate = ButtonPolicy::SMI_NOOP;