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) {
62 case PAPER_LEGALPAPER:
63 case PAPER_EXECUTIVEPAPER:
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"))
326 void FormExternal::build()
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));
336 bcview().setOK(dialog_->button_ok);
337 bcview().setApply(dialog_->button_apply);
338 bcview().setCancel(dialog_->button_close);
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);
346 bcview().addReadOnly(lyxview_->check_show);
347 bcview().addReadOnly(lyxview_->choice_show);
348 bcview().addReadOnly(lyxview_->input_displayscale);
350 bcview().addReadOnly(rotate_->input_angle);
351 bcview().addReadOnly(rotate_->choice_origin);
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);
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);
366 bcview().addReadOnly(options_->choice_option);
367 bcview().addReadOnly(options_->input_option);
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);
377 // addCheckedPositiveFloat(bcview(), input_displayscale);
378 fl_set_input_filter(lyxview_->input_displayscale,
379 fl_unsigned_int_filter);
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);
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);
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);
409 string const choice =
410 ' ' + getStringFromVector(controller().getTemplates(), " | ") +
412 fl_addto_choice(file_->choice_template, choice.c_str());
414 string const display_list =
415 _("Default|Monochrome|Grayscale|Color|Preview");
416 fl_addto_choice(lyxview_->choice_show, display_list.c_str());
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());
425 string const height_list = buildChoiceLengthString();
426 string const width_list = bformat(_("Scale%%%%|%1$s"), height_list);
428 fl_addto_choice(scale_->choice_width, width_list.c_str());
429 fl_addto_choice(scale_->choice_height, height_list.c_str());
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);
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);
444 fl_addto_tabfolder(dialog_->tabfolder, _("File").c_str(),
447 tabmap_[LYXVIEWTAB] =
448 fl_addto_tabfolder(dialog_->tabfolder, _("LyX View").c_str(),
451 fl_addto_tabfolder(dialog_->tabfolder, _("Rotate").c_str(),
454 fl_addto_tabfolder(dialog_->tabfolder, _("Scale").c_str(),
457 fl_addto_tabfolder(dialog_->tabfolder, _("Crop").c_str(),
459 tabmap_[OPTIONSTAB] =
460 fl_addto_tabfolder(dialog_->tabfolder, _("Options").c_str(),
465 void FormExternal::update()
467 fl_set_folder_bynumber(dialog_->tabfolder, 1);
468 InsetExternalParams const & params = controller().params();
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());
474 int ID = controller().getTemplateNumber(params.templatename());
476 fl_set_choice(file_->choice_template, ID+1);
480 fl_set_button(file_->check_draft, params.draft);
482 setDisplay(lyxview_->check_show, lyxview_->choice_show,
483 lyxview_->input_displayscale,
484 params.display, params.lyxscale,
485 kernel().isBufferReadonly());
487 setRotation(rotate_->input_angle, rotate_->choice_origin,
488 params.rotationdata);
490 setSize(scale_->input_width, scale_->choice_width,
491 scale_->input_height, scale_->choice_height,
492 scale_->check_aspectratio,
495 setCrop(crop_->check_bbox,
496 crop_->input_xl, crop_->input_yb,
497 crop_->input_xr, crop_->input_yt,
499 controller().bbChanged(!params.clipdata.bbox.empty());
503 void FormExternal::updateComboChange()
505 int const choice = fl_get_choice(file_->choice_template) - 1;
506 external::Template templ = controller().getTemplate(choice);
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);
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();
529 bool found = find(tr_begin, tr_end, external::Rotate) != tr_end;
530 setEnabled(tabmap_[ROTATETAB], found);
532 found = find(tr_begin, tr_end, external::Resize) != tr_end;
533 setEnabled(tabmap_[SCALETAB], found);
535 found = find(tr_begin, tr_end, external::Clip) != tr_end;
536 setEnabled(tabmap_[CROPTAB], found);
538 found = find(tr_begin, tr_end, external::Extra) != tr_end;
539 setEnabled(tabmap_[OPTIONSTAB], found);
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;
549 fl_set_input(ob_input, "");
550 fl_clear_choice(ob_choice);
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())
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;
564 bool const enabled = fl_get_choice_maxitems(ob_choice) > 0;
566 setEnabled(tabmap_[OPTIONSTAB], enabled);
567 setEnabled(ob_input, enabled && !kernel().isBufferReadonly());
568 setEnabled(ob_choice, 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());
578 void FormExternal::apply()
580 InsetExternalParams params = controller().params();
582 string const buffer_path = kernel().bufferFilepath();
583 params.filename.set(getString(file_->input_file), buffer_path);
585 int const choice = fl_get_choice(file_->choice_template) - 1;
586 params.settemplate(controller().getTemplate(choice).lyxName);
588 params.draft = fl_get_button(file_->check_draft);
590 getDisplay(params.display, params.lyxscale,
591 lyxview_->check_show, lyxview_->choice_show,
592 lyxview_->input_displayscale);
594 if (isActive(tabmap_[ROTATETAB]))
595 getRotation(params.rotationdata,
596 rotate_->input_angle, rotate_->choice_origin);
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);
604 if (isActive(tabmap_[CROPTAB]))
605 getCrop(params.clipdata,
607 crop_->input_xl, crop_->input_yb,
608 crop_->input_xr, crop_->input_yt,
609 controller().bbChanged());
611 if (isActive(tabmap_[OPTIONSTAB]))
612 getExtra(params.extradata, extra_);
614 controller().setParams(params);
618 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
620 ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
622 if (ob == file_->choice_template) {
624 // set to the chosen template
627 } else if (ob == file_->button_browse) {
629 string const in_name = fl_get_input(file_->input_file);
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());
638 } else if (ob == file_->button_edit) {
639 controller().editExternal();
640 result = ButtonPolicy::SMI_NOOP;
642 } else if (ob == lyxview_->check_show) {
644 bool const checked = fl_get_button(ob);
645 setEnabled(lyxview_->choice_show, checked);
646 setEnabled(lyxview_->input_displayscale, checked);
648 } else if (ob == crop_->button_get_bbox) {
652 } else if (ob == scale_->input_width ||
653 ob == scale_->input_height) {
655 setEnabled(scale_->check_aspectratio,
656 activateAspectratio());
658 } else if (ob == scale_->choice_width) {
662 } else if (ob == crop_->input_xr ||
663 ob == crop_->input_yt ||
664 ob == crop_->input_xl ||
665 ob == crop_->input_yb) {
667 controller().bbChanged(true);
669 } else if (ob == options_->input_option) {
671 string const format =
672 fl_get_choice_text(options_->choice_option);
673 extra_[format] = getString(options_->input_option);
675 } else if (ob == options_->choice_option) {
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;
687 bool FormExternal::activateAspectratio() const
689 if (fl_get_choice(scale_->choice_width) == 1)
692 string const wstr = getString(scale_->input_width);
695 bool const wIsDbl = isStrDbl(wstr);
696 if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
699 if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
702 string const hstr = getString(scale_->input_height);
705 bool const hIsDbl = isStrDbl(hstr);
706 if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
708 if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
715 void FormExternal::getBB()
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");
722 string const filename = getString(file_->input_file);
723 if (filename.empty())
726 string const bb = controller().readBB(filename);
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());
735 controller().bbChanged(false);
739 void FormExternal::widthUnitChanged()
741 if (fl_get_choice(scale_->choice_width) == 1)
744 bool useHeight = fl_get_choice(scale_->choice_width) > 1;
747 // widthED->setValidator(unsignedLengthValidator(widthED));
749 // widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
751 setEnabled(scale_->input_height, useHeight);
752 setEnabled(scale_->choice_height, useHeight);
755 } // namespace frontend