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::build()
46 dialog_.reset(build_graphics());
48 // This is the place to add settings of the dialog that did not go
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,
55 fl_set_input_return (dialog_->input_height,
57 fl_set_input_return (dialog_->input_filename,
59 fl_set_input_return (dialog_->input_rotate_angle,
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);
68 // Set input filter on width and height to make them accept only
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);
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);
79 // Add the widgets of the width radio buttons to their group
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);
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);
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);
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);
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);
135 void FormGraphics::apply()
137 // Create the parameters structure and fill the data from the dialog.
138 InsetGraphicsParams & igp = controller().params();
140 igp.filename = fl_get_input(dialog_->input_filename);
142 igp.display = static_cast < InsetGraphicsParams::DisplayType >
143 (displayButtons.getButton());
145 igp.widthResize = static_cast < InsetGraphicsParams::Resize >
146 (widthButtons.getButton());
147 igp.widthSize = strToDbl(fl_get_input(dialog_->input_width));
149 igp.heightResize = static_cast < InsetGraphicsParams::Resize >
150 (heightButtons.getButton());
151 igp.heightSize = strToDbl(fl_get_input(dialog_->input_height));
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);
161 igp.subcaption = fl_get_button(dialog_->check_subcaption);
162 igp.subcaptionText = fl_get_input(dialog_->input_subcaption);
168 void FormGraphics::update()
170 // Update dialog with details from inset
171 InsetGraphicsParams & igp = controller().params();
173 // Update the filename input field
174 fl_set_input(dialog_->input_filename,
175 igp.filename.c_str());
177 // Update the display depth radio buttons
178 displayButtons.setButton(igp.display);
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());
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());
190 // Update the rotate angle
191 fl_set_input(dialog_->input_rotate_angle,
192 tostr(igp.rotateAngle).c_str());
194 // Update the subcaption check button and input field
195 fl_set_button(dialog_->check_subcaption,
197 fl_set_input(dialog_->input_subcaption,
198 igp.subcaptionText.c_str());
200 // Now make sure that the buttons are set correctly.
205 ButtonPolicy::SMInput FormGraphics::input(FL_OBJECT *, long data)
207 State cb = static_cast<State>( data );
209 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
213 activate = checkInput();
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);
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);
229 lyxerr << "Advanced Options button depressed, "
230 << "show advanced options dialog"
241 ButtonPolicy::SMInput FormGraphics::checkInput()
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.
247 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
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
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.
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);
268 activate = ButtonPolicy::SMI_NOOP;
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) {
274 activate = ButtonPolicy::SMI_NOOP;
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) {
281 activate = ButtonPolicy::SMI_NOOP;