3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "FormExternal.h"
16 #include "forms/form_external.h"
18 #include "checkedwidgets.h"
19 #include "input_validators.h"
21 #include "xforms_helpers.h"
24 #include "controllers/ControlExternal.h"
26 #include "lengthcommon.h"
29 #include "insets/ExternalTemplate.h"
30 #include "insets/insetexternal.h"
32 #include "support/lstrings.h"
33 #include "support/lyxlib.h"
34 #include "support/tostr.h"
36 #include "lyx_forms.h"
38 namespace external = lyx::external;
46 using support::bformat;
47 using support::float_equal;
48 using support::getStringFromVector;
49 using support::isStrDbl;
50 using support::strToDbl;
51 using support::strToInt;
59 LyXLength::UNIT defaultUnit()
61 LyXLength::UNIT default_unit = LyXLength::CM;
62 switch (lyxrc.default_papersize) {
64 case PAPER_LEGALPAPER:
65 case PAPER_EXECUTIVEPAPER:
66 default_unit = LyXLength::IN;
75 void setDisplay(FL_OBJECT * displayCB, FL_OBJECT * showCO, FL_OBJECT * scaleED,
76 external::DisplayType display, unsigned int scale,
79 BOOST_ASSERT(displayCB && displayCB->objclass == FL_CHECKBUTTON);
80 BOOST_ASSERT(showCO && showCO->objclass == FL_CHOICE);
81 BOOST_ASSERT(scaleED && scaleED->objclass == FL_INPUT);
85 case external::DefaultDisplay:
88 case external::MonochromeDisplay:
91 case external::GrayscaleDisplay:
94 case external::ColorDisplay:
97 case external::PreviewDisplay:
100 case external::NoDisplay:
105 fl_set_choice(showCO, item);
107 bool const no_display = display == external::NoDisplay;
108 setEnabled(showCO, !no_display && !read_only);
110 fl_set_button(displayCB, !no_display);
112 fl_set_input(scaleED, tostr(scale).c_str());
113 setEnabled(scaleED, !no_display && !read_only);
117 void getDisplay(external::DisplayType & display,
118 unsigned int & scale,
119 FL_OBJECT * displayCB,
123 BOOST_ASSERT(displayCB && displayCB->objclass == FL_CHECKBUTTON);
124 BOOST_ASSERT(showCO && showCO->objclass == FL_CHOICE);
125 BOOST_ASSERT(scaleED && scaleED->objclass == FL_INPUT);
127 switch (fl_get_choice(showCO)) {
129 display = external::DefaultDisplay;
132 display = external::MonochromeDisplay;
135 display = external::GrayscaleDisplay;
138 display = external::ColorDisplay;
141 display = external::PreviewDisplay;
145 if (!fl_get_button(displayCB))
146 display = external::NoDisplay;
148 scale = strToInt(getString(scaleED));
152 void setRotation(FL_OBJECT * angleED, FL_OBJECT * originCO,
153 external::RotationData const & data)
155 BOOST_ASSERT(angleED && angleED->objclass == FL_INPUT);
156 BOOST_ASSERT(originCO && originCO->objclass == FL_CHOICE);
158 fl_set_choice(originCO, 1 + int(data.origin()));
159 fl_set_input(angleED, tostr(data.angle()).c_str());
163 void getRotation(external::RotationData & data,
164 FL_OBJECT * angleED, FL_OBJECT * originCO)
166 BOOST_ASSERT(angleED && angleED->objclass == FL_INPUT);
167 BOOST_ASSERT(originCO && originCO->objclass == FL_CHOICE);
169 typedef external::RotationData::OriginType OriginType;
171 data.origin(static_cast<OriginType>(fl_get_choice(originCO) - 1));
172 data.angle(strToDbl(getString(angleED)));
176 void setSize(FL_OBJECT * widthED, FL_OBJECT * widthUnitCO,
177 FL_OBJECT * heightED, FL_OBJECT * heightUnitCO,
178 FL_OBJECT * aspectratioCB,
179 external::ResizeData const & data)
181 BOOST_ASSERT(widthED && widthED->objclass == FL_INPUT);
182 BOOST_ASSERT(widthUnitCO && widthUnitCO->objclass == FL_CHOICE);
183 BOOST_ASSERT(heightED && heightED->objclass == FL_INPUT);
184 BOOST_ASSERT(heightUnitCO && heightUnitCO->objclass == FL_CHOICE);
185 BOOST_ASSERT(aspectratioCB &&
186 aspectratioCB->objclass == FL_CHECKBUTTON);
188 bool using_scale = data.usingScale();
189 double scale = data.scale;
190 if (data.no_resize()) {
191 // Everything is zero, so default to this!
197 fl_set_input(widthED, tostr(scale).c_str());
198 fl_set_choice(widthUnitCO, 1);
200 fl_set_input(widthED, tostr(data.width.value()).c_str());
201 // Because 'Scale' is position 1...
202 // Note also that width cannot be zero here, so
203 // we don't need to worry about the default unit.
204 fl_set_choice(widthUnitCO, data.width.unit() + 2);
207 string const h = data.height.zero() ? string() : data.height.asString();
208 LyXLength::UNIT default_unit = data.width.zero() ?
209 defaultUnit() : data.width.unit();
210 updateWidgetsFromLengthString(heightED, heightUnitCO,
211 h, stringFromUnit(default_unit));
213 setEnabled(heightED, !using_scale);
214 setEnabled(heightUnitCO, !using_scale);
216 fl_set_button(aspectratioCB, data.keepAspectRatio);
218 bool const disable_aspectRatio = using_scale ||
219 data.width.zero() || data.height.zero();
220 setEnabled(aspectratioCB, !disable_aspectRatio);
224 void getSize(external::ResizeData & data,
225 FL_OBJECT * widthED, FL_OBJECT * widthUnitCO,
226 FL_OBJECT * heightED, FL_OBJECT * heightUnitCO,
227 FL_OBJECT * aspectratioCB)
229 BOOST_ASSERT(widthED && widthED->objclass == FL_INPUT);
230 BOOST_ASSERT(widthUnitCO && widthUnitCO->objclass == FL_CHOICE);
231 BOOST_ASSERT(heightED && heightED->objclass == FL_INPUT);
232 BOOST_ASSERT(heightUnitCO && heightUnitCO->objclass == FL_CHOICE);
233 BOOST_ASSERT(aspectratioCB &&
234 aspectratioCB->objclass == FL_CHECKBUTTON);
236 string const width = getString(widthED);
238 if (fl_get_choice(widthUnitCO) > 1) {
239 // Subtract one, because scale is 1.
240 int const unit = fl_get_choice(widthUnitCO) - 1;
243 if (isValidLength(width, &w))
245 else if (isStrDbl(width))
246 data.width = LyXLength(strToDbl(width),
247 static_cast<LyXLength::UNIT>(unit));
249 data.width = LyXLength();
254 // scaling instead of a width
255 data.scale = strToDbl(width);
256 data.width = LyXLength();
259 data.height = LyXLength(getLengthFromWidgets(heightED, heightUnitCO));
261 data.keepAspectRatio = fl_get_button(aspectratioCB);
265 void setCrop(FL_OBJECT * clipCB,
266 FL_OBJECT * xlED, FL_OBJECT * ybED,
267 FL_OBJECT * xrED, FL_OBJECT * ytED,
268 external::ClipData const & data)
270 BOOST_ASSERT(clipCB && clipCB->objclass == FL_CHECKBUTTON);
271 BOOST_ASSERT(xlED && xlED->objclass == FL_INPUT);
272 BOOST_ASSERT(ybED && ybED->objclass == FL_INPUT);
273 BOOST_ASSERT(xrED && xrED->objclass == FL_INPUT);
274 BOOST_ASSERT(ytED && ytED->objclass == FL_INPUT);
276 fl_set_button(clipCB, data.clip);
277 graphics::BoundingBox const & bbox = data.bbox;
278 fl_set_input(xlED, tostr(bbox.xl).c_str());
279 fl_set_input(ybED, tostr(bbox.yb).c_str());
280 fl_set_input(xrED, tostr(bbox.xr).c_str());
281 fl_set_input(ytED, tostr(bbox.yt).c_str());
285 void getCrop(external::ClipData & data,
287 FL_OBJECT * xlED, FL_OBJECT * ybED,
288 FL_OBJECT * xrED, FL_OBJECT * ytED,
291 BOOST_ASSERT(clipCB && clipCB->objclass == FL_CHECKBUTTON);
292 BOOST_ASSERT(xlED && xlED->objclass == FL_INPUT);
293 BOOST_ASSERT(ybED && ybED->objclass == FL_INPUT);
294 BOOST_ASSERT(xrED && xrED->objclass == FL_INPUT);
295 BOOST_ASSERT(ytED && ytED->objclass == FL_INPUT);
297 data.clip = fl_get_button(clipCB);
302 data.bbox.xl = strToInt(getString(xlED));
303 data.bbox.yb = strToInt(getString(ybED));
304 data.bbox.xr = strToInt(getString(xrED));
305 data.bbox.yt = strToInt(getString(ytED));
309 void getExtra(external::ExtraData & data,
310 FormExternal::MapType const & extra)
312 typedef FormExternal::MapType MapType;
313 MapType::const_iterator it = extra.begin();
314 MapType::const_iterator const end = extra.end();
315 for (; it != end; ++it)
316 data.set(it->first, trim(it->second));
322 typedef FormController<ControlExternal, FormView<FD_external> > base_class;
324 FormExternal::FormExternal(Dialog & parent)
325 : base_class(parent, _("External Material"))
329 void FormExternal::build()
331 dialog_.reset(build_external(this));
332 file_.reset(build_external_file(this));
333 lyxview_.reset(build_external_lyxview(this));
334 rotate_.reset(build_external_rotate(this));
335 scale_.reset(build_external_scale(this));
336 crop_.reset(build_external_crop(this));
337 options_.reset(build_external_options(this));
339 bcview().setOK(dialog_->button_ok);
340 bcview().setApply(dialog_->button_apply);
341 bcview().setCancel(dialog_->button_close);
343 bcview().addReadOnly(file_->input_file);
344 bcview().addReadOnly(file_->button_browse);
345 bcview().addReadOnly(file_->button_edit);
346 bcview().addReadOnly(file_->choice_template);
347 bcview().addReadOnly(file_->check_draft);
349 bcview().addReadOnly(lyxview_->check_show);
350 bcview().addReadOnly(lyxview_->choice_show);
351 bcview().addReadOnly(lyxview_->input_displayscale);
353 bcview().addReadOnly(rotate_->input_angle);
354 bcview().addReadOnly(rotate_->choice_origin);
356 bcview().addReadOnly(scale_->input_width);
357 bcview().addReadOnly(scale_->choice_width);
358 bcview().addReadOnly(scale_->input_height);
359 bcview().addReadOnly(scale_->choice_height);
360 bcview().addReadOnly(scale_->check_aspectratio);
362 bcview().addReadOnly(crop_->check_bbox);
363 bcview().addReadOnly(crop_->button_get_bbox);
364 bcview().addReadOnly(crop_->input_xr);
365 bcview().addReadOnly(crop_->input_yt);
366 bcview().addReadOnly(crop_->input_xl);
367 bcview().addReadOnly(crop_->input_yb);
369 bcview().addReadOnly(options_->choice_option);
370 bcview().addReadOnly(options_->input_option);
373 // addCheckedPositiveFloat(bcview(), scale_->input_width);
374 // As I haven't written addCheckedPositiveFloat, we default to
375 // always checking that it is a valide LyXLength, even when
376 // I'm 'scaling'. No harm done, just not as strict as it might be.
377 addCheckedLyXLength(bcview(), scale_->input_width);
378 addCheckedLyXLength(bcview(), scale_->input_height);
380 // addCheckedPositiveFloat(bcview(), input_displayscale);
381 fl_set_input_filter(lyxview_->input_displayscale,
382 fl_unsigned_int_filter);
384 fl_set_input_filter(crop_->input_xr, fl_unsigned_int_filter);
385 fl_set_input_filter(crop_->input_yt, fl_unsigned_int_filter);
386 fl_set_input_filter(crop_->input_xl, fl_unsigned_int_filter);
387 fl_set_input_filter(crop_->input_yb, fl_unsigned_int_filter);
389 fl_set_input_return(file_->input_file, FL_RETURN_CHANGED);
390 fl_set_input_return(lyxview_->input_displayscale, FL_RETURN_CHANGED);
391 fl_set_input_return(rotate_->input_angle, FL_RETURN_CHANGED);
392 fl_set_input_return(scale_->input_width, FL_RETURN_CHANGED);
393 fl_set_input_return(scale_->input_height, FL_RETURN_CHANGED);
394 fl_set_input_return(crop_->input_xr, FL_RETURN_CHANGED);
395 fl_set_input_return(crop_->input_yt, FL_RETURN_CHANGED);
396 fl_set_input_return(crop_->input_xl, FL_RETURN_CHANGED);
397 fl_set_input_return(crop_->input_yb, FL_RETURN_CHANGED);
398 fl_set_input_return(options_->input_option, FL_RETURN_CHANGED);
400 // Trigger an input event for cut&paste with middle mouse button.
401 setPrehandler(file_->input_file);
402 setPrehandler(lyxview_->input_displayscale);
403 setPrehandler(rotate_->input_angle);
404 setPrehandler(scale_->input_width);
405 setPrehandler(scale_->input_height);
406 setPrehandler(crop_->input_xr);
407 setPrehandler(crop_->input_yt);
408 setPrehandler(crop_->input_xl);
409 setPrehandler(crop_->input_yb);
410 setPrehandler(options_->input_option);
412 string const choice =
413 ' ' + getStringFromVector(controller().getTemplates(), " | ") +
415 fl_addto_choice(file_->choice_template, choice.c_str());
417 string const display_list =
418 _("Default|Monochrome|Grayscale|Color|Preview");
419 fl_addto_choice(lyxview_->choice_show, display_list.c_str());
421 // Fill the origins combo
422 typedef vector<external::RotationDataType> Origins;
423 Origins const & all_origins = external::all_origins();
424 for (Origins::size_type i = 0; i != all_origins.size(); ++i)
425 fl_addto_choice(rotate_->choice_origin,
426 external::origin_gui_str(i).c_str());
428 string const height_list = buildChoiceLengthString();
429 string const width_list = bformat(_("Scale%%%%|%1$s"), height_list);
431 fl_addto_choice(scale_->choice_width, width_list.c_str());
432 fl_addto_choice(scale_->choice_height, height_list.c_str());
434 // Set up the tooltips.
435 string str = _("The file you want to insert.");
436 tooltips().init(file_->input_file, str);
437 str = _("Browse the directories.");
438 tooltips().init(file_->button_browse, str);
440 str = _("Scale the image to inserted percentage value.");
441 tooltips().init(lyxview_->input_displayscale, str);
442 str = _("Select display mode for this image.");
443 tooltips().init(options_->choice_option, str);
447 fl_addto_tabfolder(dialog_->tabfolder, _("File").c_str(),
450 tabmap_[LYXVIEWTAB] =
451 fl_addto_tabfolder(dialog_->tabfolder, _("LyX View").c_str(),
454 fl_addto_tabfolder(dialog_->tabfolder, _("Rotate").c_str(),
457 fl_addto_tabfolder(dialog_->tabfolder, _("Scale").c_str(),
460 fl_addto_tabfolder(dialog_->tabfolder, _("Crop").c_str(),
462 tabmap_[OPTIONSTAB] =
463 fl_addto_tabfolder(dialog_->tabfolder, _("Options").c_str(),
468 void FormExternal::update()
470 fl_set_folder_bynumber(dialog_->tabfolder, 1);
471 InsetExternalParams const & params = controller().params();
473 string const buffer_path = kernel().bufferFilepath();
474 string const name = params.filename.outputFilename(buffer_path);
475 fl_set_input(file_->input_file, name.c_str());
477 int ID = controller().getTemplateNumber(params.templatename());
479 fl_set_choice(file_->choice_template, ID+1);
483 fl_set_button(file_->check_draft, params.draft);
485 setDisplay(lyxview_->check_show, lyxview_->choice_show,
486 lyxview_->input_displayscale,
487 params.display, params.lyxscale,
488 kernel().isBufferReadonly());
490 setRotation(rotate_->input_angle, rotate_->choice_origin,
491 params.rotationdata);
493 setSize(scale_->input_width, scale_->choice_width,
494 scale_->input_height, scale_->choice_height,
495 scale_->check_aspectratio,
498 setCrop(crop_->check_bbox,
499 crop_->input_xl, crop_->input_yb,
500 crop_->input_xr, crop_->input_yt,
502 controller().bbChanged(!params.clipdata.bbox.empty());
506 void FormExternal::updateComboChange()
508 int const choice = fl_get_choice(file_->choice_template) - 1;
509 external::Template templ = controller().getTemplate(choice);
511 // Update the help text
512 string const txt = formatted(templ.helpText,
513 file_->browser_template->w - 20);
514 fl_clear_browser(file_->browser_template);
515 fl_addto_browser(file_->browser_template, txt.c_str());
516 fl_set_browser_topline(file_->browser_template, 0);
518 // Ascertain which (if any) transformations the template supports
519 // and disable tabs hosting unsupported transforms.
520 typedef vector<external::TransformID> TransformIDs;
521 TransformIDs const transformIds = templ.transformIds;
522 TransformIDs::const_iterator tr_begin = transformIds.begin();
523 TransformIDs::const_iterator const tr_end = transformIds.end();
532 bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
533 setEnabled(tabmap_[ROTATETAB], found);
535 found = find(tr_begin, tr_end, external::Resize) != tr_end;
536 setEnabled(tabmap_[SCALETAB], found);
538 found = find(tr_begin, tr_end, external::Clip) != tr_end;
539 setEnabled(tabmap_[CROPTAB], found);
541 found = find(tr_begin, tr_end, external::Extra) != tr_end;
542 setEnabled(tabmap_[OPTIONSTAB], found);
547 // Ascertain whether the template has any formats supporting
548 // the 'Extra' option
549 FL_OBJECT * const ob_input = options_->input_option;
550 FL_OBJECT * const ob_choice = options_->choice_option;
552 fl_set_input(ob_input, "");
553 fl_clear_choice(ob_choice);
555 external::Template::Formats::const_iterator it = templ.formats.begin();
556 external::Template::Formats::const_iterator end = templ.formats.end();
557 for (; it != end; ++it) {
558 if (it->second.option_transformers.find(external::Extra) ==
559 it->second.option_transformers.end())
561 string const format = it->first;
562 string const opt = controller().params().extradata.get(format);
563 fl_addto_choice(ob_choice, format.c_str());
564 extra_[format] = opt;
567 bool const enabled = fl_get_choice_maxitems(ob_choice) > 0;
569 setEnabled(tabmap_[OPTIONSTAB], enabled);
570 setEnabled(ob_input, enabled && !kernel().isBufferReadonly());
571 setEnabled(ob_choice, enabled);
574 fl_set_choice(ob_choice, 1);
575 string const format = fl_get_choice_text(ob_choice);
576 fl_set_input(ob_input, extra_[format].c_str());
581 void FormExternal::apply()
583 InsetExternalParams params = controller().params();
585 string const buffer_path = kernel().bufferFilepath();
586 params.filename.set(getString(file_->input_file), buffer_path);
588 int const choice = fl_get_choice(file_->choice_template) - 1;
589 params.settemplate(controller().getTemplate(choice).lyxName);
591 params.draft = fl_get_button(file_->check_draft);
593 getDisplay(params.display, params.lyxscale,
594 lyxview_->check_show, lyxview_->choice_show,
595 lyxview_->input_displayscale);
597 if (isActive(tabmap_[ROTATETAB]))
598 getRotation(params.rotationdata,
599 rotate_->input_angle, rotate_->choice_origin);
601 if (isActive(tabmap_[SCALETAB]))
602 getSize(params.resizedata,
603 scale_->input_width, scale_->choice_width,
604 scale_->input_height, scale_->choice_height,
605 scale_->check_aspectratio);
607 if (isActive(tabmap_[CROPTAB]))
608 getCrop(params.clipdata,
610 crop_->input_xl, crop_->input_yb,
611 crop_->input_xr, crop_->input_yt,
612 controller().bbChanged());
614 if (isActive(tabmap_[OPTIONSTAB]))
615 getExtra(params.extradata, extra_);
617 controller().setParams(params);
621 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
623 ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
625 if (ob == file_->choice_template) {
627 // set to the chosen template
630 } else if (ob == file_->button_browse) {
632 string const in_name = fl_get_input(file_->input_file);
634 int const choice = fl_get_choice(file_->choice_template) - 1;
635 string const template_name =
636 controller().getTemplate(choice).lyxName;
637 string const out_name =
638 controller().browse(in_name, template_name);
639 fl_set_input(file_->input_file, out_name.c_str());
641 } else if (ob == file_->button_edit) {
642 controller().editExternal();
643 result = ButtonPolicy::SMI_NOOP;
645 } else if (ob == lyxview_->check_show) {
647 bool const checked = fl_get_button(ob);
648 setEnabled(lyxview_->choice_show, checked);
649 setEnabled(lyxview_->input_displayscale, checked);
651 } else if (ob == crop_->button_get_bbox) {
655 } else if (ob == scale_->input_width ||
656 ob == scale_->input_height) {
658 setEnabled(scale_->check_aspectratio,
659 activateAspectratio());
661 } else if (ob == scale_->choice_width) {
665 } else if (ob == crop_->input_xr ||
666 ob == crop_->input_yt ||
667 ob == crop_->input_xl ||
668 ob == crop_->input_yb) {
670 controller().bbChanged(true);
672 } else if (ob == options_->input_option) {
674 string const format =
675 fl_get_choice_text(options_->choice_option);
676 extra_[format] = getString(options_->input_option);
678 } else if (ob == options_->choice_option) {
680 string const format =
681 fl_get_choice_text(options_->choice_option);
682 fl_set_input(options_->input_option, extra_[format].c_str());
683 result = ButtonPolicy::SMI_NOOP;
690 bool FormExternal::activateAspectratio() const
692 if (fl_get_choice(scale_->choice_width) == 1)
695 string const wstr = getString(scale_->input_width);
698 bool const wIsDbl = isStrDbl(wstr);
699 if (wIsDbl && float_equal(strToDbl(wstr), 0.0, 0.05))
702 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
705 string const hstr = getString(scale_->input_height);
708 bool const hIsDbl = isStrDbl(hstr);
709 if (hIsDbl && float_equal(strToDbl(hstr), 0.0, 0.05))
711 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
718 void FormExternal::getBB()
720 fl_set_input(crop_->input_xl, "0");
721 fl_set_input(crop_->input_yb, "0");
722 fl_set_input(crop_->input_xr, "0");
723 fl_set_input(crop_->input_yt, "0");
725 string const filename = getString(file_->input_file);
726 if (filename.empty())
729 string const bb = controller().readBB(filename);
733 fl_set_input(crop_->input_xl, token(bb, ' ', 0).c_str());
734 fl_set_input(crop_->input_yb, token(bb, ' ', 1).c_str());
735 fl_set_input(crop_->input_xr, token(bb, ' ', 2).c_str());
736 fl_set_input(crop_->input_yt, token(bb, ' ', 3).c_str());
738 controller().bbChanged(false);
742 void FormExternal::widthUnitChanged()
744 if (fl_get_choice(scale_->choice_width) == 1)
747 bool useHeight = fl_get_choice(scale_->choice_width) > 1;
750 // widthED->setValidator(unsignedLengthValidator(widthED));
752 // widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
754 setEnabled(scale_->input_height, useHeight);
755 setEnabled(scale_->choice_height, useHeight);
758 } // namespace frontend