]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormExternal.C
the convert patch
[lyx.git] / src / frontends / xforms / FormExternal.C
1 /**
2  * \file FormExternal.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author John Levon
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "FormExternal.h"
16 #include "forms/form_external.h"
17
18 #include "checkedwidgets.h"
19 #include "input_validators.h"
20 #include "Tooltips.h"
21 #include "xforms_helpers.h"
22 #include "xformsBC.h"
23
24 #include "controllers/ControlExternal.h"
25
26 #include "lengthcommon.h"
27 #include "lyxrc.h"
28
29 #include "insets/ExternalTemplate.h"
30 #include "insets/insetexternal.h"
31
32 #include "support/lstrings.h"
33 #include "support/lyxlib.h"
34 #include "support/convert.h"
35
36 #include "lyx_forms.h"
37
38 namespace external = lyx::external;
39
40 using std::find;
41 using std::string;
42 using std::vector;
43
44 namespace lyx {
45
46 using support::bformat;
47 using support::float_equal;
48 using support::getStringFromVector;
49 using support::isStrDbl;
50 using support::token;
51 using support::trim;
52
53 namespace frontend {
54
55 namespace {
56
57 LyXLength::UNIT defaultUnit()
58 {
59         LyXLength::UNIT default_unit = LyXLength::CM;
60         switch (lyxrc.default_papersize) {
61         case PAPER_USLETTER:
62         case PAPER_LEGALPAPER:
63         case PAPER_EXECUTIVEPAPER:
64                 default_unit = LyXLength::IN;
65                 break;
66         default:
67                 break;
68         }
69         return default_unit;
70 }
71
72
73 void setDisplay(FL_OBJECT * displayCB, FL_OBJECT * showCO, FL_OBJECT * scaleED,
74                 external::DisplayType display, unsigned int scale,
75                 bool read_only)
76 {
77         BOOST_ASSERT(displayCB && displayCB->objclass == FL_CHECKBUTTON);
78         BOOST_ASSERT(showCO && showCO->objclass == FL_CHOICE);
79         BOOST_ASSERT(scaleED && scaleED->objclass == FL_INPUT);
80
81         int item = 1;
82         switch (display) {
83         case external::DefaultDisplay:
84                 item = 1;
85                 break;
86         case external::MonochromeDisplay:
87                 item = 2;
88                 break;
89         case external::GrayscaleDisplay:
90                 item = 3;
91                 break;
92         case external::ColorDisplay:
93                 item = 4;
94                 break;
95         case external::PreviewDisplay:
96                 item = 5;
97                 break;
98         case external::NoDisplay:
99                 item = 1;
100                 break;
101         }
102
103         fl_set_choice(showCO, item);
104
105         bool const no_display = display == external::NoDisplay;
106         setEnabled(showCO, !no_display && !read_only);
107
108         fl_set_button(displayCB, !no_display);
109
110         fl_set_input(scaleED, convert<string>(scale).c_str());
111         setEnabled(scaleED, !no_display && !read_only);
112 }
113
114
115 void getDisplay(external::DisplayType & display,
116                 unsigned int & scale,
117                 FL_OBJECT * displayCB,
118                 FL_OBJECT * showCO,
119                 FL_OBJECT * scaleED)
120 {
121         BOOST_ASSERT(displayCB && displayCB->objclass == FL_CHECKBUTTON);
122         BOOST_ASSERT(showCO && showCO->objclass == FL_CHOICE);
123         BOOST_ASSERT(scaleED && scaleED->objclass == FL_INPUT);
124
125         switch (fl_get_choice(showCO)) {
126         case 1:
127                 display = external::DefaultDisplay;
128                 break;
129         case 2:
130                 display = external::MonochromeDisplay;
131                 break;
132         case 3:
133                 display = external::GrayscaleDisplay;
134                 break;
135         case 4:
136                 display = external::ColorDisplay;
137                 break;
138         case 5:
139                 display = external::PreviewDisplay;
140                 break;
141         }
142
143         if (!fl_get_button(displayCB))
144                 display = external::NoDisplay;
145
146         scale = convert<int>(getString(scaleED));
147 }
148
149
150 void setRotation(FL_OBJECT * angleED, FL_OBJECT * originCO,
151                  external::RotationData const & data)
152 {
153         BOOST_ASSERT(angleED && angleED->objclass == FL_INPUT);
154         BOOST_ASSERT(originCO && originCO->objclass == FL_CHOICE);
155
156         fl_set_choice(originCO, 1 + int(data.origin()));
157         fl_set_input(angleED, data.angle.c_str());
158 }
159
160
161 void getRotation(external::RotationData & data,
162                  FL_OBJECT * angleED, FL_OBJECT * originCO)
163 {
164         BOOST_ASSERT(angleED && angleED->objclass == FL_INPUT);
165         BOOST_ASSERT(originCO && originCO->objclass == FL_CHOICE);
166
167         typedef external::RotationData::OriginType OriginType;
168
169         data.origin(static_cast<OriginType>(fl_get_choice(originCO) - 1));
170         data.angle = getString(angleED);
171 }
172
173
174 void setSize(FL_OBJECT * widthED, FL_OBJECT * widthUnitCO,
175              FL_OBJECT * heightED, FL_OBJECT * heightUnitCO,
176              FL_OBJECT * aspectratioCB,
177              external::ResizeData const & data)
178 {
179         BOOST_ASSERT(widthED && widthED->objclass == FL_INPUT);
180         BOOST_ASSERT(widthUnitCO && widthUnitCO->objclass == FL_CHOICE);
181         BOOST_ASSERT(heightED && heightED->objclass == FL_INPUT);
182         BOOST_ASSERT(heightUnitCO && heightUnitCO->objclass == FL_CHOICE);
183         BOOST_ASSERT(aspectratioCB &&
184                      aspectratioCB->objclass == FL_CHECKBUTTON);
185
186         bool using_scale = data.usingScale();
187         std::string scale =  data.scale;
188         if (data.no_resize()) {
189                 // Everything is zero, so default to this!
190                 using_scale = true;
191                 scale = "100";
192         }
193
194         if (using_scale) {
195                 fl_set_input(widthED, scale.c_str());
196                 fl_set_choice(widthUnitCO, 1);
197         } else {
198                 fl_set_input(widthED, convert<string>(data.width.value()).c_str());
199                 // Because 'Scale' is position 1...
200                 // Note also that width cannot be zero here, so
201                 // we don't need to worry about the default unit.
202                 fl_set_choice(widthUnitCO, data.width.unit() + 2);
203         }
204
205         string const h = data.height.zero() ? string() : data.height.asString();
206         LyXLength::UNIT default_unit = data.width.zero() ?
207                 defaultUnit() : data.width.unit();
208         updateWidgetsFromLengthString(heightED, heightUnitCO,
209                                       h, stringFromUnit(default_unit));
210
211         setEnabled(heightED, !using_scale);
212         setEnabled(heightUnitCO, !using_scale);
213
214         fl_set_button(aspectratioCB, data.keepAspectRatio);
215
216         bool const disable_aspectRatio = using_scale ||
217                 data.width.zero() || data.height.zero();
218         setEnabled(aspectratioCB, !disable_aspectRatio);
219 }
220
221
222 void getSize(external::ResizeData & data,
223              FL_OBJECT * widthED, FL_OBJECT * widthUnitCO,
224              FL_OBJECT * heightED, FL_OBJECT * heightUnitCO,
225              FL_OBJECT * aspectratioCB)
226 {
227         BOOST_ASSERT(widthED && widthED->objclass == FL_INPUT);
228         BOOST_ASSERT(widthUnitCO && widthUnitCO->objclass == FL_CHOICE);
229         BOOST_ASSERT(heightED && heightED->objclass == FL_INPUT);
230         BOOST_ASSERT(heightUnitCO && heightUnitCO->objclass == FL_CHOICE);
231         BOOST_ASSERT(aspectratioCB &&
232                      aspectratioCB->objclass == FL_CHECKBUTTON);
233
234         string const width = getString(widthED);
235
236         if (fl_get_choice(widthUnitCO) > 1) {
237                 // Subtract one, because scale is 1.
238                 int const unit = fl_get_choice(widthUnitCO) - 1;
239
240                 LyXLength w;
241                 if (isValidLength(width, &w))
242                         data.width = w;
243                 else if (isStrDbl(width))
244                         data.width = LyXLength(convert<double>(width),
245                                            static_cast<LyXLength::UNIT>(unit));
246                 else
247                         data.width = LyXLength();
248
249                 data.scale.erase();
250         } else {
251                 // scaling instead of a width
252                 data.scale = width;
253                 data.width = LyXLength();
254         }
255
256         data.height = LyXLength(getLengthFromWidgets(heightED, heightUnitCO));
257
258         data.keepAspectRatio = fl_get_button(aspectratioCB);
259 }
260
261
262 void setCrop(FL_OBJECT * clipCB,
263              FL_OBJECT * xlED, FL_OBJECT * ybED,
264              FL_OBJECT * xrED, FL_OBJECT * ytED,
265              external::ClipData const & data)
266 {
267         BOOST_ASSERT(clipCB && clipCB->objclass == FL_CHECKBUTTON);
268         BOOST_ASSERT(xlED && xlED->objclass == FL_INPUT);
269         BOOST_ASSERT(ybED && ybED->objclass == FL_INPUT);
270         BOOST_ASSERT(xrED && xrED->objclass == FL_INPUT);
271         BOOST_ASSERT(ytED && ytED->objclass == FL_INPUT);
272
273         fl_set_button(clipCB, data.clip);
274         graphics::BoundingBox const & bbox = data.bbox;
275         fl_set_input(xlED, convert<string>(bbox.xl).c_str());
276         fl_set_input(ybED, convert<string>(bbox.yb).c_str());
277         fl_set_input(xrED, convert<string>(bbox.xr).c_str());
278         fl_set_input(ytED, convert<string>(bbox.yt).c_str());
279 }
280
281
282 void getCrop(external::ClipData & data,
283              FL_OBJECT * clipCB,
284              FL_OBJECT * xlED, FL_OBJECT * ybED,
285              FL_OBJECT * xrED, FL_OBJECT * ytED,
286              bool bb_changed)
287 {
288         BOOST_ASSERT(clipCB && clipCB->objclass == FL_CHECKBUTTON);
289         BOOST_ASSERT(xlED && xlED->objclass == FL_INPUT);
290         BOOST_ASSERT(ybED && ybED->objclass == FL_INPUT);
291         BOOST_ASSERT(xrED && xrED->objclass == FL_INPUT);
292         BOOST_ASSERT(ytED && ytED->objclass == FL_INPUT);
293
294         data.clip = fl_get_button(clipCB);
295
296         if (!bb_changed)
297                 return;
298
299         data.bbox.xl = convert<int>(getString(xlED));
300         data.bbox.yb = convert<int>(getString(ybED));
301         data.bbox.xr = convert<int>(getString(xrED));
302         data.bbox.yt = convert<int>(getString(ytED));
303 }
304
305
306 void getExtra(external::ExtraData & data,
307               FormExternal::MapType const & extra)
308 {
309         typedef FormExternal::MapType MapType;
310         MapType::const_iterator it  = extra.begin();
311         MapType::const_iterator const end = extra.end();
312         for (; it != end; ++it)
313                 data.set(it->first, trim(it->second));
314 }
315
316 } // namespace anon
317
318
319 typedef FormController<ControlExternal, FormView<FD_external> > base_class;
320
321 FormExternal::FormExternal(Dialog & parent)
322         : base_class(parent, _("External Material"))
323 {}
324
325
326 void FormExternal::build()
327 {
328         dialog_.reset(build_external(this));
329         file_.reset(build_external_file(this));
330         lyxview_.reset(build_external_lyxview(this));
331         rotate_.reset(build_external_rotate(this));
332         scale_.reset(build_external_scale(this));
333         crop_.reset(build_external_crop(this));
334         options_.reset(build_external_options(this));
335
336         bcview().setOK(dialog_->button_ok);
337         bcview().setApply(dialog_->button_apply);
338         bcview().setCancel(dialog_->button_close);
339
340         bcview().addReadOnly(file_->input_file);
341         bcview().addReadOnly(file_->button_browse);
342         bcview().addReadOnly(file_->button_edit);
343         bcview().addReadOnly(file_->choice_template);
344         bcview().addReadOnly(file_->check_draft);
345
346         bcview().addReadOnly(lyxview_->check_show);
347         bcview().addReadOnly(lyxview_->choice_show);
348         bcview().addReadOnly(lyxview_->input_displayscale);
349
350         bcview().addReadOnly(rotate_->input_angle);
351         bcview().addReadOnly(rotate_->choice_origin);
352
353         bcview().addReadOnly(scale_->input_width);
354         bcview().addReadOnly(scale_->choice_width);
355         bcview().addReadOnly(scale_->input_height);
356         bcview().addReadOnly(scale_->choice_height);
357         bcview().addReadOnly(scale_->check_aspectratio);
358
359         bcview().addReadOnly(crop_->check_bbox);
360         bcview().addReadOnly(crop_->button_get_bbox);
361         bcview().addReadOnly(crop_->input_xr);
362         bcview().addReadOnly(crop_->input_yt);
363         bcview().addReadOnly(crop_->input_xl);
364         bcview().addReadOnly(crop_->input_yb);
365
366         bcview().addReadOnly(options_->choice_option);
367         bcview().addReadOnly(options_->input_option);
368
369         // initial setting
370 //      addCheckedPositiveFloat(bcview(), scale_->input_width);
371         // As I haven't written addCheckedPositiveFloat, we default to
372         // always checking that it is a valide LyXLength, even when
373         // I'm 'scaling'. No harm done, just not as strict as it might be.
374         addCheckedLyXLength(bcview(), scale_->input_width);
375         addCheckedLyXLength(bcview(), scale_->input_height);
376
377 //      addCheckedPositiveFloat(bcview(), input_displayscale);
378         fl_set_input_filter(lyxview_->input_displayscale,
379                             fl_unsigned_int_filter);
380
381         fl_set_input_filter(crop_->input_xr, fl_unsigned_int_filter);
382         fl_set_input_filter(crop_->input_yt, fl_unsigned_int_filter);
383         fl_set_input_filter(crop_->input_xl, fl_unsigned_int_filter);
384         fl_set_input_filter(crop_->input_yb, fl_unsigned_int_filter);
385
386         fl_set_input_return(file_->input_file,            FL_RETURN_CHANGED);
387         fl_set_input_return(lyxview_->input_displayscale, FL_RETURN_CHANGED);
388         fl_set_input_return(rotate_->input_angle,         FL_RETURN_CHANGED);
389         fl_set_input_return(scale_->input_width,          FL_RETURN_CHANGED);
390         fl_set_input_return(scale_->input_height,         FL_RETURN_CHANGED);
391         fl_set_input_return(crop_->input_xr,              FL_RETURN_CHANGED);
392         fl_set_input_return(crop_->input_yt,              FL_RETURN_CHANGED);
393         fl_set_input_return(crop_->input_xl,              FL_RETURN_CHANGED);
394         fl_set_input_return(crop_->input_yb,              FL_RETURN_CHANGED);
395         fl_set_input_return(options_->input_option,       FL_RETURN_CHANGED);
396
397         // Trigger an input event for cut&paste with middle mouse button.
398         setPrehandler(file_->input_file);
399         setPrehandler(lyxview_->input_displayscale);
400         setPrehandler(rotate_->input_angle);
401         setPrehandler(scale_->input_width);
402         setPrehandler(scale_->input_height);
403         setPrehandler(crop_->input_xr);
404         setPrehandler(crop_->input_yt);
405         setPrehandler(crop_->input_xl);
406         setPrehandler(crop_->input_yb);
407         setPrehandler(options_->input_option);
408
409         string const choice =
410                 ' ' + getStringFromVector(controller().getTemplates(), " | ") +
411                 ' ';
412         fl_addto_choice(file_->choice_template, choice.c_str());
413
414         string const display_list =
415                 _("Default|Monochrome|Grayscale|Color|Preview");
416         fl_addto_choice(lyxview_->choice_show, display_list.c_str());
417
418         // Fill the origins combo
419         typedef vector<external::RotationDataType> Origins;
420         Origins const & all_origins = external::all_origins();
421         for (Origins::size_type i = 0; i != all_origins.size(); ++i)
422                 fl_addto_choice(rotate_->choice_origin,
423                                 external::origin_gui_str(i).c_str());
424
425         string const height_list = buildChoiceLengthString();
426         string const width_list = bformat(_("Scale%%%%|%1$s"), height_list);
427
428         fl_addto_choice(scale_->choice_width, width_list.c_str());
429         fl_addto_choice(scale_->choice_height, height_list.c_str());
430
431         // Set up the tooltips.
432         string str = _("The file you want to insert.");
433         tooltips().init(file_->input_file, str);
434         str = _("Browse the directories.");
435         tooltips().init(file_->button_browse, str);
436
437         str = _("Scale the image to inserted percentage value.");
438         tooltips().init(lyxview_->input_displayscale, str);
439         str = _("Select display mode for this image.");
440         tooltips().init(options_->choice_option, str);
441
442         // Stack tabs
443         tabmap_[FILETAB] =
444                 fl_addto_tabfolder(dialog_->tabfolder, _("File").c_str(),
445                                    file_->form);
446
447         tabmap_[LYXVIEWTAB] =
448                 fl_addto_tabfolder(dialog_->tabfolder, _("LyX View").c_str(),
449                                    lyxview_->form);
450         tabmap_[ROTATETAB] =
451                 fl_addto_tabfolder(dialog_->tabfolder, _("Rotate").c_str(),
452                                    rotate_->form);
453         tabmap_[SCALETAB] =
454                 fl_addto_tabfolder(dialog_->tabfolder, _("Scale").c_str(),
455                                    scale_->form);
456         tabmap_[CROPTAB] =
457                 fl_addto_tabfolder(dialog_->tabfolder, _("Crop").c_str(),
458                                    crop_->form);
459         tabmap_[OPTIONSTAB] =
460                 fl_addto_tabfolder(dialog_->tabfolder, _("Options").c_str(),
461                                    options_->form);
462 }
463
464
465 void FormExternal::update()
466 {
467         fl_set_folder_bynumber(dialog_->tabfolder, 1);
468         InsetExternalParams const & params = controller().params();
469
470         string const buffer_path = kernel().bufferFilepath();
471         string const name = params.filename.outputFilename(buffer_path);
472         fl_set_input(file_->input_file, name.c_str());
473
474         int ID = controller().getTemplateNumber(params.templatename());
475         if (ID < 0) ID = 0;
476         fl_set_choice(file_->choice_template, ID+1);
477
478         updateComboChange();
479
480         fl_set_button(file_->check_draft, params.draft);
481
482         setDisplay(lyxview_->check_show, lyxview_->choice_show,
483                    lyxview_->input_displayscale,
484                    params.display, params.lyxscale,
485                    kernel().isBufferReadonly());
486
487         setRotation(rotate_->input_angle, rotate_->choice_origin,
488                     params.rotationdata);
489
490         setSize(scale_->input_width, scale_->choice_width,
491                 scale_->input_height, scale_->choice_height,
492                 scale_->check_aspectratio,
493                 params.resizedata);
494
495         setCrop(crop_->check_bbox,
496                 crop_->input_xl, crop_->input_yb,
497                 crop_->input_xr, crop_->input_yt,
498                 params.clipdata);
499         controller().bbChanged(!params.clipdata.bbox.empty());
500 }
501
502
503 void FormExternal::updateComboChange()
504 {
505         int const choice = fl_get_choice(file_->choice_template) - 1;
506         external::Template templ = controller().getTemplate(choice);
507
508         // Update the help text
509         string const txt = formatted(templ.helpText,
510                                      file_->browser_template->w - 20);
511         fl_clear_browser(file_->browser_template);
512         fl_addto_browser(file_->browser_template, txt.c_str());
513         fl_set_browser_topline(file_->browser_template, 0);
514
515         // Ascertain which (if any) transformations the template supports
516         // and disable tabs hosting unsupported transforms.
517         typedef vector<external::TransformID> TransformIDs;
518         TransformIDs const transformIds = templ.transformIds;
519         TransformIDs::const_iterator tr_begin = transformIds.begin();
520         TransformIDs::const_iterator const tr_end = transformIds.end();
521
522         tabmap_[FILETAB];
523         tabmap_[LYXVIEWTAB];
524         tabmap_[ROTATETAB];
525         tabmap_[SCALETAB];
526         tabmap_[CROPTAB];
527         tabmap_[OPTIONSTAB];
528
529         bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
530         setEnabled(tabmap_[ROTATETAB], found);
531
532         found = find(tr_begin, tr_end, external::Resize) != tr_end;
533         setEnabled(tabmap_[SCALETAB], found);
534
535         found = find(tr_begin, tr_end, external::Clip) != tr_end;
536         setEnabled(tabmap_[CROPTAB], found);
537
538         found = find(tr_begin, tr_end, external::Extra) != tr_end;
539         setEnabled(tabmap_[OPTIONSTAB], found);
540
541         if (!found)
542                 return;
543
544         // Ascertain whether the template has any formats supporting
545         // the 'Extra' option
546         FL_OBJECT * const ob_input  = options_->input_option;
547         FL_OBJECT * const ob_choice = options_->choice_option;
548         extra_.clear();
549         fl_set_input(ob_input, "");
550         fl_clear_choice(ob_choice);
551
552         external::Template::Formats::const_iterator it  = templ.formats.begin();
553         external::Template::Formats::const_iterator end = templ.formats.end();
554         for (; it != end; ++it) {
555                 if (it->second.option_transformers.find(external::Extra) ==
556                     it->second.option_transformers.end())
557                         continue;
558                 string const format = it->first;
559                 string const opt = controller().params().extradata.get(format);
560                 fl_addto_choice(ob_choice, format.c_str());
561                 extra_[format] = opt;
562         }
563
564         bool const enabled = fl_get_choice_maxitems(ob_choice) > 0;
565
566         setEnabled(tabmap_[OPTIONSTAB], enabled);
567         setEnabled(ob_input,  enabled && !kernel().isBufferReadonly());
568         setEnabled(ob_choice, enabled);
569
570         if (enabled) {
571                 fl_set_choice(ob_choice, 1);
572                 string const format = fl_get_choice_text(ob_choice);
573                 fl_set_input(ob_input, extra_[format].c_str());
574         }
575 }
576
577
578 void FormExternal::apply()
579 {
580         InsetExternalParams params = controller().params();
581
582         string const buffer_path = kernel().bufferFilepath();
583         params.filename.set(getString(file_->input_file), buffer_path);
584
585         int const choice = fl_get_choice(file_->choice_template) - 1;
586         params.settemplate(controller().getTemplate(choice).lyxName);
587
588         params.draft = fl_get_button(file_->check_draft);
589
590         getDisplay(params.display, params.lyxscale,
591                    lyxview_->check_show, lyxview_->choice_show,
592                    lyxview_->input_displayscale);
593
594         if (isActive(tabmap_[ROTATETAB]))
595                 getRotation(params.rotationdata,
596                             rotate_->input_angle, rotate_->choice_origin);
597
598         if (isActive(tabmap_[SCALETAB]))
599                 getSize(params.resizedata,
600                         scale_->input_width, scale_->choice_width,
601                         scale_->input_height, scale_->choice_height,
602                         scale_->check_aspectratio);
603
604         if (isActive(tabmap_[CROPTAB]))
605                 getCrop(params.clipdata,
606                         crop_->check_bbox,
607                         crop_->input_xl, crop_->input_yb,
608                         crop_->input_xr, crop_->input_yt,
609                         controller().bbChanged());
610
611         if (isActive(tabmap_[OPTIONSTAB]))
612                 getExtra(params.extradata, extra_);
613
614         controller().setParams(params);
615 }
616
617
618 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
619 {
620         ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
621
622         if (ob == file_->choice_template) {
623
624                 // set to the chosen template
625                 updateComboChange();
626
627         } else if (ob == file_->button_browse) {
628
629                 string const in_name  = fl_get_input(file_->input_file);
630
631                 int const choice = fl_get_choice(file_->choice_template) - 1;
632                 string const template_name =
633                         controller().getTemplate(choice).lyxName;
634                 string const out_name =
635                         controller().browse(in_name, template_name);
636                 fl_set_input(file_->input_file, out_name.c_str());
637
638         } else if (ob == file_->button_edit) {
639                 controller().editExternal();
640                 result = ButtonPolicy::SMI_NOOP;
641
642         } else if (ob == lyxview_->check_show) {
643
644                 bool const checked = fl_get_button(ob);
645                 setEnabled(lyxview_->choice_show, checked);
646                 setEnabled(lyxview_->input_displayscale, checked);
647
648         } else if (ob == crop_->button_get_bbox) {
649
650                 getBB();
651
652         } else if (ob == scale_->input_width ||
653                    ob == scale_->input_height) {
654
655                 setEnabled(scale_->check_aspectratio,
656                            activateAspectratio());
657
658         } else if (ob == scale_->choice_width) {
659
660                 widthUnitChanged();
661
662         } else if (ob == crop_->input_xr ||
663                    ob == crop_->input_yt ||
664                    ob == crop_->input_xl ||
665                    ob == crop_->input_yb) {
666
667                 controller().bbChanged(true);
668
669         } else if (ob == options_->input_option) {
670
671                 string const format =
672                         fl_get_choice_text(options_->choice_option);
673                 extra_[format] = getString(options_->input_option);
674
675         } else if (ob == options_->choice_option) {
676
677                 string const format =
678                         fl_get_choice_text(options_->choice_option);
679                 fl_set_input(options_->input_option, extra_[format].c_str());
680                 result = ButtonPolicy::SMI_NOOP;
681         }
682
683         return result;
684 }
685
686
687 bool FormExternal::activateAspectratio() const
688 {
689         if (fl_get_choice(scale_->choice_width) == 1)
690                 return false;
691
692         string const wstr = getString(scale_->input_width);
693         if (wstr.empty())
694                 return false;
695         bool const wIsDbl = isStrDbl(wstr);
696         if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
697                 return false;
698         LyXLength l;
699         if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
700                 return false;
701
702         string const hstr = getString(scale_->input_height);
703         if (hstr.empty())
704                 return false;
705         bool const hIsDbl = isStrDbl(hstr);
706         if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
707                 return false;
708         if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
709                 return false;
710
711         return true;
712 }
713
714
715 void FormExternal::getBB()
716 {
717         fl_set_input(crop_->input_xl, "0");
718         fl_set_input(crop_->input_yb, "0");
719         fl_set_input(crop_->input_xr, "0");
720         fl_set_input(crop_->input_yt, "0");
721
722         string const filename = getString(file_->input_file);
723         if (filename.empty())
724                 return;
725
726         string const bb = controller().readBB(filename);
727         if (bb.empty())
728                 return;
729
730         fl_set_input(crop_->input_xl, token(bb, ' ', 0).c_str());
731         fl_set_input(crop_->input_yb, token(bb, ' ', 1).c_str());
732         fl_set_input(crop_->input_xr, token(bb, ' ', 2).c_str());
733         fl_set_input(crop_->input_yt, token(bb, ' ', 3).c_str());
734
735         controller().bbChanged(false);
736 }
737
738
739 void FormExternal::widthUnitChanged()
740 {
741         if (fl_get_choice(scale_->choice_width) == 1)
742                 return;
743
744         bool useHeight = fl_get_choice(scale_->choice_width) > 1;
745
746 //      if (useHeight)
747 //              widthED->setValidator(unsignedLengthValidator(widthED));
748 //      else
749 //              widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
750
751         setEnabled(scale_->input_height, useHeight);
752         setEnabled(scale_->choice_height, useHeight);
753 }
754
755 } // namespace frontend
756 } // namespace lyx