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/convert.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;
57 LyXLength::UNIT defaultUnit()
59 LyXLength::UNIT default_unit = LyXLength::CM;
60 switch (lyxrc.default_papersize) {
63 case PAPER_USEXECUTIVE:
64 default_unit = LyXLength::IN;
73 void setDisplay(FL_OBJECT * displayCB, FL_OBJECT * showCO, FL_OBJECT * scaleED,
74 external::DisplayType display, unsigned int scale,
77 BOOST_ASSERT(displayCB && displayCB->objclass == FL_CHECKBUTTON);
78 BOOST_ASSERT(showCO && showCO->objclass == FL_CHOICE);
79 BOOST_ASSERT(scaleED && scaleED->objclass == FL_INPUT);
83 case external::DefaultDisplay:
86 case external::MonochromeDisplay:
89 case external::GrayscaleDisplay:
92 case external::ColorDisplay:
95 case external::PreviewDisplay:
98 case external::NoDisplay:
103 fl_set_choice(showCO, item);
105 bool const no_display = display == external::NoDisplay;
106 setEnabled(showCO, !no_display && !read_only);
108 fl_set_button(displayCB, !no_display);
110 fl_set_input(scaleED, convert<string>(scale).c_str());
111 setEnabled(scaleED, !no_display && !read_only);
115 void getDisplay(external::DisplayType & display,
116 unsigned int & scale,
117 FL_OBJECT * displayCB,
121 BOOST_ASSERT(displayCB && displayCB->objclass == FL_CHECKBUTTON);
122 BOOST_ASSERT(showCO && showCO->objclass == FL_CHOICE);
123 BOOST_ASSERT(scaleED && scaleED->objclass == FL_INPUT);
125 switch (fl_get_choice(showCO)) {
127 display = external::DefaultDisplay;
130 display = external::MonochromeDisplay;
133 display = external::GrayscaleDisplay;
136 display = external::ColorDisplay;
139 display = external::PreviewDisplay;
143 if (!fl_get_button(displayCB))
144 display = external::NoDisplay;
146 scale = convert<int>(getString(scaleED));
150 void setRotation(FL_OBJECT * angleED, FL_OBJECT * originCO,
151 external::RotationData const & data)
153 BOOST_ASSERT(angleED && angleED->objclass == FL_INPUT);
154 BOOST_ASSERT(originCO && originCO->objclass == FL_CHOICE);
156 fl_set_choice(originCO, 1 + int(data.origin()));
157 fl_set_input(angleED, data.angle.c_str());
161 void getRotation(external::RotationData & data,
162 FL_OBJECT * angleED, FL_OBJECT * originCO)
164 BOOST_ASSERT(angleED && angleED->objclass == FL_INPUT);
165 BOOST_ASSERT(originCO && originCO->objclass == FL_CHOICE);
167 typedef external::RotationData::OriginType OriginType;
169 data.origin(static_cast<OriginType>(fl_get_choice(originCO) - 1));
170 data.angle = getString(angleED);
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)
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);
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!
195 fl_set_input(widthED, scale.c_str());
196 fl_set_choice(widthUnitCO, 1);
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);
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));
211 setEnabled(heightED, !using_scale);
212 setEnabled(heightUnitCO, !using_scale);
214 fl_set_button(aspectratioCB, data.keepAspectRatio);
216 bool const disable_aspectRatio = using_scale ||
217 data.width.zero() || data.height.zero();
218 setEnabled(aspectratioCB, !disable_aspectRatio);
222 void getSize(external::ResizeData & data,
223 FL_OBJECT * widthED, FL_OBJECT * widthUnitCO,
224 FL_OBJECT * heightED, FL_OBJECT * heightUnitCO,
225 FL_OBJECT * aspectratioCB)
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);
234 string const width = getString(widthED);
236 if (fl_get_choice(widthUnitCO) > 1) {
237 // Subtract one, because scale is 1.
238 int const unit = fl_get_choice(widthUnitCO) - 1;
241 if (isValidLength(width, &w))
243 else if (isStrDbl(width))
244 data.width = LyXLength(convert<double>(width),
245 static_cast<LyXLength::UNIT>(unit));
247 data.width = LyXLength();
251 // scaling instead of a width
253 data.width = LyXLength();
256 data.height = LyXLength(getLengthFromWidgets(heightED, heightUnitCO));
258 data.keepAspectRatio = fl_get_button(aspectratioCB);
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)
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);
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());
282 void getCrop(external::ClipData & data,
284 FL_OBJECT * xlED, FL_OBJECT * ybED,
285 FL_OBJECT * xrED, FL_OBJECT * ytED,
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);
294 data.clip = fl_get_button(clipCB);
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));
306 void getExtra(external::ExtraData & data,
307 FormExternal::MapType const & extra)
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));
319 typedef FormController<ControlExternal, FormView<FD_external> > base_class;
321 FormExternal::FormExternal(Dialog & parent)
322 : base_class(parent, _("External Material")),
327 void FormExternal::build()
329 dialog_.reset(build_external(this));
330 file_.reset(build_external_file(this));
331 lyxview_.reset(build_external_lyxview(this));
332 rotate_.reset(build_external_rotate(this));
333 scale_.reset(build_external_scale(this));
334 crop_.reset(build_external_crop(this));
335 options_.reset(build_external_options(this));
337 bcview().setOK(dialog_->button_ok);
338 bcview().setApply(dialog_->button_apply);
339 bcview().setCancel(dialog_->button_close);
341 bcview().addReadOnly(file_->input_file);
342 bcview().addReadOnly(file_->button_browse);
343 bcview().addReadOnly(file_->button_edit);
344 bcview().addReadOnly(file_->choice_template);
345 bcview().addReadOnly(file_->check_draft);
347 bcview().addReadOnly(lyxview_->check_show);
348 bcview().addReadOnly(lyxview_->choice_show);
349 bcview().addReadOnly(lyxview_->input_displayscale);
351 bcview().addReadOnly(rotate_->input_angle);
352 bcview().addReadOnly(rotate_->choice_origin);
354 bcview().addReadOnly(scale_->input_width);
355 bcview().addReadOnly(scale_->choice_width);
356 bcview().addReadOnly(scale_->input_height);
357 bcview().addReadOnly(scale_->choice_height);
358 bcview().addReadOnly(scale_->check_aspectratio);
360 bcview().addReadOnly(crop_->check_bbox);
361 bcview().addReadOnly(crop_->button_get_bbox);
362 bcview().addReadOnly(crop_->input_xr);
363 bcview().addReadOnly(crop_->input_yt);
364 bcview().addReadOnly(crop_->input_xl);
365 bcview().addReadOnly(crop_->input_yb);
367 bcview().addReadOnly(options_->choice_option);
368 bcview().addReadOnly(options_->input_option);
370 file_checker_ = &addCheckedPath(bcview(), true, file_->input_file);
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 file_checker_->setChecker(kernel().docType(), lyxrc);
472 fl_set_folder_bynumber(dialog_->tabfolder, 1);
473 InsetExternalParams const & params = controller().params();
475 string const buffer_path = kernel().bufferFilepath();
476 string const name = params.filename.outputFilename(buffer_path);
477 fl_set_input(file_->input_file, name.c_str());
479 int ID = controller().getTemplateNumber(params.templatename());
481 fl_set_choice(file_->choice_template, ID+1);
485 fl_set_button(file_->check_draft, params.draft);
487 setDisplay(lyxview_->check_show, lyxview_->choice_show,
488 lyxview_->input_displayscale,
489 params.display, params.lyxscale,
490 kernel().isBufferReadonly());
492 setRotation(rotate_->input_angle, rotate_->choice_origin,
493 params.rotationdata);
495 setSize(scale_->input_width, scale_->choice_width,
496 scale_->input_height, scale_->choice_height,
497 scale_->check_aspectratio,
500 setCrop(crop_->check_bbox,
501 crop_->input_xl, crop_->input_yb,
502 crop_->input_xr, crop_->input_yt,
504 controller().bbChanged(!params.clipdata.bbox.empty());
508 void FormExternal::updateComboChange()
510 int const choice = fl_get_choice(file_->choice_template) - 1;
511 external::Template templ = controller().getTemplate(choice);
513 // Update the help text
514 string const txt = formatted(templ.helpText,
515 file_->browser_template->w - 20);
516 fl_clear_browser(file_->browser_template);
517 fl_addto_browser(file_->browser_template, txt.c_str());
518 fl_set_browser_topline(file_->browser_template, 0);
520 // Ascertain which (if any) transformations the template supports
521 // and disable tabs hosting unsupported transforms.
522 typedef vector<external::TransformID> TransformIDs;
523 TransformIDs const transformIds = templ.transformIds;
524 TransformIDs::const_iterator tr_begin = transformIds.begin();
525 TransformIDs::const_iterator const tr_end = transformIds.end();
534 bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
535 setEnabled(tabmap_[ROTATETAB], found);
537 found = find(tr_begin, tr_end, external::Resize) != tr_end;
538 setEnabled(tabmap_[SCALETAB], found);
540 found = find(tr_begin, tr_end, external::Clip) != tr_end;
541 setEnabled(tabmap_[CROPTAB], found);
543 found = find(tr_begin, tr_end, external::Extra) != tr_end;
544 setEnabled(tabmap_[OPTIONSTAB], found);
549 // Ascertain whether the template has any formats supporting
550 // the 'Extra' option
551 FL_OBJECT * const ob_input = options_->input_option;
552 FL_OBJECT * const ob_choice = options_->choice_option;
554 fl_set_input(ob_input, "");
555 fl_clear_choice(ob_choice);
557 external::Template::Formats::const_iterator it = templ.formats.begin();
558 external::Template::Formats::const_iterator end = templ.formats.end();
559 for (; it != end; ++it) {
560 if (it->second.option_transformers.find(external::Extra) ==
561 it->second.option_transformers.end())
563 string const format = it->first;
564 string const opt = controller().params().extradata.get(format);
565 fl_addto_choice(ob_choice, format.c_str());
566 extra_[format] = opt;
569 bool const enabled = fl_get_choice_maxitems(ob_choice) > 0;
571 setEnabled(tabmap_[OPTIONSTAB], enabled);
572 setEnabled(ob_input, enabled && !kernel().isBufferReadonly());
573 setEnabled(ob_choice, enabled);
576 fl_set_choice(ob_choice, 1);
577 string const format = fl_get_choice_text(ob_choice);
578 fl_set_input(ob_input, extra_[format].c_str());
583 void FormExternal::apply()
585 InsetExternalParams params = controller().params();
587 string const buffer_path = kernel().bufferFilepath();
588 params.filename.set(getString(file_->input_file), buffer_path);
590 int const choice = fl_get_choice(file_->choice_template) - 1;
591 params.settemplate(controller().getTemplate(choice).lyxName);
593 params.draft = fl_get_button(file_->check_draft);
595 getDisplay(params.display, params.lyxscale,
596 lyxview_->check_show, lyxview_->choice_show,
597 lyxview_->input_displayscale);
599 if (isActive(tabmap_[ROTATETAB]))
600 getRotation(params.rotationdata,
601 rotate_->input_angle, rotate_->choice_origin);
603 if (isActive(tabmap_[SCALETAB]))
604 getSize(params.resizedata,
605 scale_->input_width, scale_->choice_width,
606 scale_->input_height, scale_->choice_height,
607 scale_->check_aspectratio);
609 if (isActive(tabmap_[CROPTAB]))
610 getCrop(params.clipdata,
612 crop_->input_xl, crop_->input_yb,
613 crop_->input_xr, crop_->input_yt,
614 controller().bbChanged());
616 if (isActive(tabmap_[OPTIONSTAB]))
617 getExtra(params.extradata, extra_);
619 controller().setParams(params);
623 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
625 ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
627 if (ob == file_->choice_template) {
629 // set to the chosen template
632 } else if (ob == file_->button_browse) {
634 string const in_name = fl_get_input(file_->input_file);
636 int const choice = fl_get_choice(file_->choice_template) - 1;
637 string const template_name =
638 controller().getTemplate(choice).lyxName;
639 string const out_name =
640 controller().browse(in_name, template_name);
641 fl_set_input(file_->input_file, out_name.c_str());
643 } else if (ob == file_->button_edit) {
644 controller().editExternal();
645 result = ButtonPolicy::SMI_NOOP;
647 } else if (ob == lyxview_->check_show) {
649 bool const checked = fl_get_button(ob);
650 setEnabled(lyxview_->choice_show, checked);
651 setEnabled(lyxview_->input_displayscale, checked);
653 } else if (ob == crop_->button_get_bbox) {
657 } else if (ob == scale_->input_width ||
658 ob == scale_->input_height) {
660 setEnabled(scale_->check_aspectratio,
661 activateAspectratio());
663 } else if (ob == scale_->choice_width) {
667 } else if (ob == crop_->input_xr ||
668 ob == crop_->input_yt ||
669 ob == crop_->input_xl ||
670 ob == crop_->input_yb) {
672 controller().bbChanged(true);
674 } else if (ob == options_->input_option) {
676 string const format =
677 fl_get_choice_text(options_->choice_option);
678 extra_[format] = getString(options_->input_option);
680 } else if (ob == options_->choice_option) {
682 string const format =
683 fl_get_choice_text(options_->choice_option);
684 fl_set_input(options_->input_option, extra_[format].c_str());
685 result = ButtonPolicy::SMI_NOOP;
692 bool FormExternal::activateAspectratio() const
694 if (fl_get_choice(scale_->choice_width) == 1)
697 string const wstr = getString(scale_->input_width);
700 bool const wIsDbl = isStrDbl(wstr);
701 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
704 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
707 string const hstr = getString(scale_->input_height);
710 bool const hIsDbl = isStrDbl(hstr);
711 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
713 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
720 void FormExternal::getBB()
722 fl_set_input(crop_->input_xl, "0");
723 fl_set_input(crop_->input_yb, "0");
724 fl_set_input(crop_->input_xr, "0");
725 fl_set_input(crop_->input_yt, "0");
727 string const filename = getString(file_->input_file);
728 if (filename.empty())
731 string const bb = controller().readBB(filename);
735 fl_set_input(crop_->input_xl, token(bb, ' ', 0).c_str());
736 fl_set_input(crop_->input_yb, token(bb, ' ', 1).c_str());
737 fl_set_input(crop_->input_xr, token(bb, ' ', 2).c_str());
738 fl_set_input(crop_->input_yt, token(bb, ' ', 3).c_str());
740 controller().bbChanged(false);
744 void FormExternal::widthUnitChanged()
746 if (fl_get_choice(scale_->choice_width) == 1)
749 bool useHeight = fl_get_choice(scale_->choice_width) > 1;
752 // widthED->setValidator(unsignedLengthValidator(widthED));
754 // widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
756 setEnabled(scale_->input_height, useHeight);
757 setEnabled(scale_->choice_height, useHeight);
760 } // namespace frontend