]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiExternal.cpp
PrefShortcuts: ShortcutEdit, adapted from Edwin's patch
[lyx.git] / src / frontends / qt4 / GuiExternal.cpp
1 /**
2  * \file GuiExternal.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Angus Leeming
8  * \author Asger Alstrup
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiExternal.h"
16
17 #include "GuiExternal.h"
18 #include "frontend_helpers.h"
19 #include "FuncRequest.h"
20 #include "gettext.h"
21 #include "lengthcommon.h"
22 #include "LyXRC.h"
23
24 #include "insets/ExternalSupport.h"
25 #include "insets/ExternalTemplate.h"
26 #include "insets/InsetExternal.h"
27
28 #include "graphics/GraphicsCache.h"
29 #include "graphics/GraphicsCacheItem.h"
30 #include "graphics/GraphicsImage.h"
31
32 #include "support/convert.h"
33 #include "support/FileFilterList.h"
34 #include "support/filetools.h"
35 #include "support/lstrings.h"
36 #include "support/lyxlib.h"
37 #include "support/os.h"
38
39 #include "LengthCombo.h"
40 #include "qt_helpers.h"
41 #include "Validator.h"
42
43 #include <QCloseEvent>
44 #include <QCheckBox>
45 #include <QLineEdit>
46 #include <QPushButton>
47 #include <QTabWidget>
48 #include <QTextBrowser>
49
50 using std::advance;
51 using std::vector;
52 using std::string;
53
54
55 namespace lyx {
56 namespace frontend {
57
58 using support::FileFilterList;
59 using support::FileName;
60 using support::float_equal;
61 using support::isStrDbl;
62 using support::makeAbsPath;
63 using support::readBB_from_PSFile;
64 using support::token;
65 using support::trim;
66 using support::os::internal_path;
67
68 using namespace external;
69
70 namespace {
71
72 RotationDataType origins_array[] = {
73         RotationData::DEFAULT,
74         RotationData::TOPLEFT,
75         RotationData::BOTTOMLEFT,
76         RotationData::BASELINELEFT,
77         RotationData::CENTER,
78         RotationData::TOPCENTER,
79         RotationData::BOTTOMCENTER,
80         RotationData::BASELINECENTER,
81         RotationData::TOPRIGHT,
82         RotationData::BOTTOMRIGHT,
83         RotationData::BASELINERIGHT
84 };
85
86
87 size_type const origins_array_size =
88 sizeof(origins_array) / sizeof(origins_array[0]);
89
90 vector<external::RotationDataType> const
91 all_origins(origins_array, origins_array + origins_array_size);
92
93 // These are the strings, corresponding to the above, that the GUI should
94 // use. Note that they can/should be translated.
95 char const * const origin_gui_strs[] = {
96         N_("Default"),
97         N_("Top left"), N_("Bottom left"), N_("Baseline left"),
98         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
99         N_("Top right"), N_("Bottom right"), N_("Baseline right")
100 };
101
102 } // namespace anon
103
104
105 GuiExternal::GuiExternal(LyXView & lv)
106         : GuiDialog(lv, "external"), bbChanged_(false)
107 {
108         setupUi(this);
109         setViewTitle(_("External Material"));
110
111         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
112         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
113         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
114
115         connect(displayCB, SIGNAL(toggled(bool)),
116                 showCO, SLOT(setEnabled(bool)));
117         connect(displayCB, SIGNAL(toggled(bool)),
118                 displayscaleED, SLOT(setEnabled(bool)));
119         connect(showCO, SIGNAL(activated(QString)),
120                 this, SLOT(change_adaptor()));
121         connect(originCO, SIGNAL(activated(int)),
122                 this, SLOT(change_adaptor()));
123         connect(aspectratioCB, SIGNAL(stateChanged(int)),
124                 this, SLOT(change_adaptor()));
125         connect(browsePB, SIGNAL(clicked()),
126                 this, SLOT(browseClicked()));
127         connect(editPB, SIGNAL(clicked()),
128                 this, SLOT(editClicked()));
129         connect(externalCO, SIGNAL(activated(QString)),
130                 this, SLOT(templateChanged()));
131         connect(extraED, SIGNAL(textChanged(QString)),
132                 this, SLOT(extraChanged(QString)));
133         connect(extraFormatCO, SIGNAL(activated(QString)),
134                 this, SLOT(formatChanged(QString)));
135         connect(widthUnitCO, SIGNAL(activated(int)),
136                 this, SLOT(widthUnitChanged()));
137         connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
138                 this, SLOT(change_adaptor()));
139         connect(displayCB, SIGNAL(stateChanged(int)),
140                 this, SLOT(change_adaptor()));
141         connect(displayscaleED, SIGNAL(textChanged(QString)),
142                 this, SLOT(change_adaptor()));
143         connect(angleED, SIGNAL(textChanged(QString)),
144                 this, SLOT(change_adaptor()));
145         connect(widthED, SIGNAL(textChanged(QString)),
146                 this, SLOT(sizeChanged()));
147         connect(heightED, SIGNAL(textChanged(QString)),
148                 this, SLOT(sizeChanged()));
149         connect(fileED, SIGNAL(textChanged(QString)),
150                 this, SLOT(change_adaptor()));
151         connect(clipCB, SIGNAL(stateChanged(int)),
152                 this, SLOT(change_adaptor()));
153         connect(getbbPB, SIGNAL(clicked()), this, SLOT(getbbClicked()));
154         connect(xrED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
155         connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
156         connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
157         connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
158         connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
159
160         QIntValidator * validator = new QIntValidator(displayscaleED);
161         validator->setBottom(1);
162         displayscaleED->setValidator(validator);
163
164         angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
165
166         xlED->setValidator(new QIntValidator(xlED));
167         ybED->setValidator(new QIntValidator(ybED));
168         xrED->setValidator(new QIntValidator(xrED));
169         ytED->setValidator(new QIntValidator(ytED));
170
171         widthED->setValidator(unsignedLengthValidator(widthED));
172         heightED->setValidator(unsignedLengthValidator(heightED));
173
174         setFocusProxy(fileED);
175
176         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
177
178         bc().setOK(okPB);
179         bc().setApply(applyPB);
180         bc().setCancel(closePB);
181
182         bc().addReadOnly(fileED);
183         bc().addReadOnly(browsePB);
184         bc().addReadOnly(editPB);
185         bc().addReadOnly(externalCO);
186         bc().addReadOnly(draftCB);
187         bc().addReadOnly(displayscaleED);
188         bc().addReadOnly(showCO);
189         bc().addReadOnly(displayCB);
190         bc().addReadOnly(angleED);
191         bc().addReadOnly(originCO);
192         bc().addReadOnly(heightUnitCO);
193         bc().addReadOnly(heightED);
194         bc().addReadOnly(aspectratioCB);
195         bc().addReadOnly(widthUnitCO);
196         bc().addReadOnly(widthED);
197         bc().addReadOnly(clipCB);
198         bc().addReadOnly(getbbPB);
199         bc().addReadOnly(ytED);
200         bc().addReadOnly(xlED);
201         bc().addReadOnly(xrED);
202         bc().addReadOnly(ybED);
203         bc().addReadOnly(extraFormatCO);
204         bc().addReadOnly(extraED);
205
206         bc().addCheckedLineEdit(angleED, angleLA);
207         bc().addCheckedLineEdit(displayscaleED, scaleLA);
208         bc().addCheckedLineEdit(heightED, heightLA);
209         bc().addCheckedLineEdit(widthED, widthLA);
210         bc().addCheckedLineEdit(xlED, lbLA);
211         bc().addCheckedLineEdit(ybED, lbLA);
212         bc().addCheckedLineEdit(xrED, rtLA);
213         bc().addCheckedLineEdit(ytED, rtLA);
214         bc().addCheckedLineEdit(fileED, fileLA);
215
216         std::vector<string> templates = getTemplates();
217
218         for (std::vector<string>::const_iterator cit = templates.begin();
219                 cit != templates.end(); ++cit) {
220                 externalCO->addItem(qt_(*cit));
221         }
222
223         // Fill the origins combo
224         for (size_t i = 0; i != all_origins.size(); ++i)
225                 originCO->addItem(qt_(origin_gui_strs[i]));
226
227         // Fill the width combo
228         widthUnitCO->addItem(qt_("Scale%"));
229         for (int i = 0; i < num_units; i++)
230                 widthUnitCO->addItem(qt_(unit_name_gui[i]));
231 }
232
233
234 bool GuiExternal::activateAspectratio() const
235 {
236         if (widthUnitCO->currentIndex() == 0)
237                 return false;
238
239         string const wstr = fromqstr(widthED->text());
240         if (wstr.empty())
241                 return false;
242         bool const wIsDbl = isStrDbl(wstr);
243         if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
244                 return false;
245         Length l;
246         if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
247                 return false;
248
249         string const hstr = fromqstr(heightED->text());
250         if (hstr.empty())
251                 return false;
252         bool const hIsDbl = isStrDbl(hstr);
253         if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
254                 return false;
255         if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
256                 return false;
257
258         return true;
259 }
260
261
262 void GuiExternal::bbChanged()
263 {
264         bbChanged_ = true;
265         changed();
266 }
267
268
269 void GuiExternal::browseClicked()
270 {
271         int const choice =  externalCO->currentIndex();
272         docstring const template_name = from_utf8(getTemplate(choice).lyxName);
273         docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
274         if (!str.empty()) {
275                 fileED->setText(toqstr(str));
276                 changed();
277         }
278 }
279
280
281 void GuiExternal::change_adaptor()
282 {
283         changed();
284 }
285
286
287 void GuiExternal::closeEvent(QCloseEvent * e)
288 {
289         slotClose();
290         e->accept();
291 }
292
293
294 void GuiExternal::editClicked()
295 {
296         editExternal();
297 }
298
299
300
301 void GuiExternal::extraChanged(const QString& text)
302 {
303         std::string const format = fromqstr(extraFormatCO->currentText());
304         extra_[format] = text;
305         changed();
306 }
307
308
309 void GuiExternal::formatChanged(const QString& format)
310 {
311         extraED->setText(extra_[fromqstr(format)]);
312 }
313
314
315 void GuiExternal::getbbClicked()
316 {
317         getBB();
318 }
319
320
321 void GuiExternal::sizeChanged()
322 {
323         aspectratioCB->setEnabled(activateAspectratio());
324         changed();
325 }
326
327
328 void GuiExternal::templateChanged()
329 {
330         updateTemplate();
331         changed();
332 }
333
334
335 void GuiExternal::widthUnitChanged()
336 {
337         bool useHeight = (widthUnitCO->currentIndex() > 0);
338
339         if (useHeight)
340                 widthED->setValidator(unsignedLengthValidator(widthED));
341         else
342                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
343
344         heightED->setEnabled(useHeight);
345         heightUnitCO->setEnabled(useHeight);
346         changed();
347 }
348
349
350 static Length::UNIT defaultUnit()
351 {
352         Length::UNIT default_unit = Length::CM;
353         switch (lyxrc.default_papersize) {
354         case PAPER_USLETTER:
355         case PAPER_USLEGAL:
356         case PAPER_USEXECUTIVE:
357                 default_unit = Length::IN;
358                 break;
359         default:
360                 break;
361         }
362         return default_unit;
363 }
364
365
366 static void setDisplay(
367         QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
368         external::DisplayType display, unsigned int scale, bool read_only)
369 {
370         int item = 0;
371         switch (display) {
372         case external::DefaultDisplay:
373                 item = 0;
374                 break;
375         case external::MonochromeDisplay:
376                 item = 1;
377                 break;
378         case external::GrayscaleDisplay:
379                 item = 2;
380                 break;
381         case external::ColorDisplay:
382                 item = 3;
383                 break;
384         case external::PreviewDisplay:
385                 item = 4;
386                 break;
387         case external::NoDisplay:
388                 item = 0;
389                 break;
390         }
391
392         showCO.setCurrentIndex(item);
393         bool const no_display = display == external::NoDisplay;
394         showCO.setEnabled(!no_display && !read_only);
395         displayCB.setChecked(!no_display);
396         scaleED.setEnabled(!no_display && !read_only);
397         scaleED.setText(QString::number(scale));
398 }
399
400
401 static void getDisplay(external::DisplayType & display,
402                 unsigned int & scale,
403                 QCheckBox const & displayCB,
404                 QComboBox const & showCO,
405                 QLineEdit const & scaleED)
406 {
407         switch (showCO.currentIndex()) {
408         case 0:
409                 display = external::DefaultDisplay;
410                 break;
411         case 1:
412                 display = external::MonochromeDisplay;
413                 break;
414         case 2:
415                 display = external::GrayscaleDisplay;
416                 break;
417         case 3:
418                 display = external::ColorDisplay;
419                 break;
420         case 4:
421                 display = external::PreviewDisplay;
422                 break;
423         }
424
425         if (!displayCB.isChecked())
426                 display = external::NoDisplay;
427
428         scale = scaleED.text().toInt();
429 }
430
431
432 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
433         external::RotationData const & data)
434 {
435         originCO.setCurrentIndex(int(data.origin()));
436         angleED.setText(toqstr(data.angle));
437 }
438
439
440 static void getRotation(external::RotationData & data,
441         QLineEdit const & angleED, QComboBox const & originCO)
442 {
443         typedef external::RotationData::OriginType OriginType;
444
445         data.origin(static_cast<OriginType>(originCO.currentIndex()));
446         data.angle = fromqstr(angleED.text());
447 }
448
449
450 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
451         QLineEdit & heightED, LengthCombo & heightUnitCO,
452         QCheckBox & aspectratioCB,
453         external::ResizeData const & data)
454 {
455         bool using_scale = data.usingScale();
456         std::string scale = data.scale;
457         if (data.no_resize()) {
458                 // Everything is zero, so default to this!
459                 using_scale = true;
460                 scale = "100";
461         }
462
463         if (using_scale) {
464                 widthED.setText(toqstr(scale));
465                 widthUnitCO.setCurrentIndex(0);
466         } else {
467                 widthED.setText(QString::number(data.width.value()));
468                 // Because 'Scale' is position 0...
469                 // Note also that width cannot be zero here, so
470                 // we don't need to worry about the default unit.
471                 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
472         }
473
474         string const h = data.height.zero() ? string() : data.height.asString();
475         Length::UNIT default_unit = data.width.zero() ?
476                 defaultUnit() : data.width.unit();
477         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
478
479         heightED.setEnabled(!using_scale);
480         heightUnitCO.setEnabled(!using_scale);
481
482         aspectratioCB.setChecked(data.keepAspectRatio);
483
484         bool const disable_aspectRatio = using_scale ||
485                 data.width.zero() || data.height.zero();
486         aspectratioCB.setEnabled(!disable_aspectRatio);
487 }
488
489
490 static void getSize(external::ResizeData & data,
491         QLineEdit const & widthED, QComboBox const & widthUnitCO,
492         QLineEdit const & heightED, LengthCombo const & heightUnitCO,
493         QCheckBox const & aspectratioCB)
494 {
495         string const width = fromqstr(widthED.text());
496
497         if (widthUnitCO.currentIndex() > 0) {
498                 // Subtract one, because scale is 0.
499                 int const unit = widthUnitCO.currentIndex() - 1;
500
501                 Length w;
502                 if (isValidLength(width, &w))
503                         data.width = w;
504                 else if (isStrDbl(width))
505                         data.width = Length(convert<double>(width),
506                                            static_cast<Length::UNIT>(unit));
507                 else
508                         data.width = Length();
509
510                 data.scale = string();
511
512         } else {
513                 // scaling instead of a width
514                 data.scale = width;
515                 data.width = Length();
516         }
517
518         data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
519
520         data.keepAspectRatio = aspectratioCB.isChecked();
521 }
522
523
524 void setCrop(QCheckBox & clipCB,
525         QLineEdit & xlED, QLineEdit & ybED,
526         QLineEdit & xrED, QLineEdit & ytED,
527         external::ClipData const & data)
528 {
529         clipCB.setChecked(data.clip);
530         graphics::BoundingBox const & bbox = data.bbox;
531         xlED.setText(QString::number(bbox.xl));
532         ybED.setText(QString::number(bbox.yb));
533         xrED.setText(QString::number(bbox.xr));
534         ytED.setText(QString::number(bbox.yt));
535 }
536
537
538 static void getCrop(external::ClipData & data,
539         QCheckBox const & clipCB,
540         QLineEdit const & xlED, QLineEdit const & ybED,
541         QLineEdit const & xrED, QLineEdit const & ytED,
542         bool bb_changed)
543 {
544         data.clip = clipCB.isChecked();
545
546         if (!bb_changed)
547                 return;
548
549         data.bbox.xl = xlED.text().toInt();
550         data.bbox.yb = ybED.text().toInt();
551         data.bbox.xr = xrED.text().toInt();
552         data.bbox.yt = ytED.text().toInt();
553 }
554
555
556 static void getExtra(external::ExtraData & data,
557               GuiExternal::MapType const & extra)
558 {
559         typedef GuiExternal::MapType MapType;
560         MapType::const_iterator it = extra.begin();
561         MapType::const_iterator const end = extra.end();
562         for (; it != end; ++it)
563                 data.set(it->first, trim(fromqstr(it->second)));
564 }
565
566
567 void GuiExternal::updateContents()
568 {
569         tab->setCurrentIndex(0);
570
571         string const name =
572                 params_.filename.outputFilename(bufferFilepath());
573         fileED->setText(toqstr(name));
574
575         externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
576         updateTemplate();
577
578         draftCB->setChecked(params_.draft);
579
580         setDisplay(*displayCB, *showCO, *displayscaleED,
581                    params_.display, params_.lyxscale, isBufferReadonly());
582
583         setRotation(*angleED, *originCO, params_.rotationdata);
584
585         setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
586                 *aspectratioCB, params_.resizedata);
587
588         setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
589         bbChanged_ = !params_.clipdata.bbox.empty();
590
591         isValid();
592 }
593
594
595 void GuiExternal::updateTemplate()
596 {
597         external::Template templ = getTemplate(externalCO->currentIndex());
598         externalTB->setPlainText(qt_(templ.helpText));
599
600         // Ascertain which (if any) transformations the template supports
601         // and disable tabs hosting unsupported transforms.
602         typedef vector<external::TransformID> TransformIDs;
603         TransformIDs const transformIds = templ.transformIds;
604         TransformIDs::const_iterator tr_begin = transformIds.begin();
605         TransformIDs::const_iterator const tr_end = transformIds.end();
606
607         bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
608         tab->setTabEnabled(tab->indexOf(rotatetab), found);
609         found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
610         tab->setTabEnabled(tab->indexOf(scaletab), found);
611
612         found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
613         tab->setTabEnabled(tab->indexOf(croptab), found);
614
615         found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
616         tab->setTabEnabled(tab->indexOf(optionstab), found);
617
618         if (!found)
619                 return;
620
621         // Ascertain whether the template has any formats supporting
622         // the 'Extra' option
623         extra_.clear();
624         extraED->clear();
625         extraFormatCO->clear();
626
627         external::Template::Formats::const_iterator it  = templ.formats.begin();
628         external::Template::Formats::const_iterator end = templ.formats.end();
629         for (; it != end; ++it) {
630                 if (it->second.option_transformers.find(external::Extra) ==
631                     it->second.option_transformers.end())
632                         continue;
633                 string const format = it->first;
634                 string const opt = params_.extradata.get(format);
635                 extraFormatCO->addItem(toqstr(format));
636                 extra_[format] = toqstr(opt);
637         }
638
639         bool const enabled = extraFormatCO->count()  > 0;
640
641         tab->setTabEnabled(
642                 tab->indexOf(optionstab), enabled);
643         extraED->setEnabled(enabled && !isBufferReadonly());
644         extraFormatCO->setEnabled(enabled);
645
646         if (enabled) {
647                 extraFormatCO->setCurrentIndex(0);
648                 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
649         }
650 }
651
652
653 void GuiExternal::applyView()
654 {
655         params_.filename.set(internal_path(fromqstr(fileED->text())),
656                             bufferFilepath());
657
658         params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
659
660         params_.draft = draftCB->isChecked();
661
662         getDisplay(params_.display, params_.lyxscale,
663                    *displayCB, *showCO,
664                    *displayscaleED);
665
666         if (tab->isTabEnabled(tab->indexOf(rotatetab)))
667                 getRotation(params_.rotationdata, *angleED, *originCO);
668
669         if (tab->isTabEnabled(tab->indexOf(scaletab)))
670                 getSize(params_.resizedata, *widthED, *widthUnitCO,
671                         *heightED, *heightUnitCO, *aspectratioCB);
672
673         if (tab->isTabEnabled(tab->indexOf(croptab)))
674                 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
675                         *xrED, *ytED, bbChanged_);
676
677         if (tab->isTabEnabled(tab->indexOf(optionstab)))
678                 getExtra(params_.extradata, extra_);
679 }
680
681
682 void GuiExternal::getBB()
683 {
684         xlED->setText("0");
685         ybED->setText("0");
686         xrED->setText("0");
687         ytED->setText("0");
688
689         string const filename = fromqstr(fileED->text());
690         if (filename.empty())
691                 return;
692
693         string const bb = readBB(filename);
694         if (bb.empty())
695                 return;
696
697         xlED->setText(toqstr(token(bb, ' ', 0)));
698         ybED->setText(toqstr(token(bb, ' ', 1)));
699         xrED->setText(toqstr(token(bb, ' ', 2)));
700         ytED->setText(toqstr(token(bb, ' ', 3)));
701
702         bbChanged_ = false;
703 }
704
705
706 bool GuiExternal::initialiseParams(string const & data)
707 {
708         InsetExternalMailer::string2params(data, buffer(), params_);
709         return true;
710 }
711
712
713 void GuiExternal::clearParams()
714 {
715         params_ = InsetExternalParams();
716 }
717
718
719 void GuiExternal::dispatchParams()
720 {
721         string const lfun = InsetExternalMailer::params2string(params_, buffer());
722         dispatch(FuncRequest(getLfun(), lfun));
723 }
724
725
726 void GuiExternal::editExternal()
727 {
728         applyView();
729         string const lfun = InsetExternalMailer::params2string(params_, buffer());
730         dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
731 }
732
733
734 vector<string> const GuiExternal::getTemplates() const
735 {
736         vector<string> result;
737
738         external::TemplateManager::Templates::const_iterator i1, i2;
739         i1 = external::TemplateManager::get().getTemplates().begin();
740         i2 = external::TemplateManager::get().getTemplates().end();
741
742         for (; i1 != i2; ++i1) {
743                 result.push_back(i1->second.lyxName);
744         }
745         return result;
746 }
747
748
749 int GuiExternal::getTemplateNumber(string const & name) const
750 {
751         external::TemplateManager::Templates::const_iterator i1, i2;
752         i1 = external::TemplateManager::get().getTemplates().begin();
753         i2 = external::TemplateManager::get().getTemplates().end();
754         for (int i = 0; i1 != i2; ++i1, ++i) {
755                 if (i1->second.lyxName == name)
756                         return i;
757         }
758
759         // we can get here if a LyX document has a template not installed
760         // on this machine.
761         return -1;
762 }
763
764
765 external::Template GuiExternal::getTemplate(int i) const
766 {
767         external::TemplateManager::Templates::const_iterator i1
768                 = external::TemplateManager::get().getTemplates().begin();
769
770         advance(i1, i);
771
772         return i1->second;
773 }
774
775
776 string const
777 GuiExternal::getTemplateFilters(string const & template_name) const
778 {
779         /// Determine the template file extension
780         external::TemplateManager const & etm =
781                 external::TemplateManager::get();
782         external::Template const * const et_ptr =
783                 etm.getTemplateByName(template_name);
784
785         if (et_ptr)
786                 return et_ptr->fileRegExp;
787
788         return string();
789 }
790
791
792 docstring const GuiExternal::browse(docstring const & input,
793                                      docstring const & template_name) const
794 {
795         docstring const title =  _("Select external file");
796
797         docstring const bufpath = lyx::from_utf8(bufferFilepath());
798         FileFilterList const filter =
799                 FileFilterList(lyx::from_utf8(getTemplateFilters(lyx::to_utf8(template_name))));
800
801         std::pair<docstring, docstring> dir1(_("Documents|#o#O"),
802                 lyx::from_utf8(lyxrc.document_path));
803
804         return browseRelFile(input, bufpath, title, filter, false, dir1);
805 }
806
807
808 string const GuiExternal::readBB(string const & file)
809 {
810         FileName const abs_file(makeAbsPath(file, bufferFilepath()));
811
812         // try to get it from the file, if possible. Zipped files are
813         // unzipped in the readBB_from_PSFile-Function
814         string const bb = readBB_from_PSFile(abs_file);
815         if (!bb.empty())
816                 return bb;
817
818         // we don't, so ask the Graphics Cache if it has loaded the file
819         int width = 0;
820         int height = 0;
821
822         graphics::Cache & gc = graphics::Cache::get();
823         if (gc.inCache(abs_file)) {
824                 graphics::Image const * image = gc.item(abs_file)->image();
825
826                 if (image) {
827                         width  = image->getWidth();
828                         height = image->getHeight();
829                 }
830         }
831
832         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
833 }
834
835
836 Dialog * createGuiExternal(LyXView & lv) { return new GuiExternal(lv); }
837
838
839 } // namespace frontend
840 } // namespace lyx
841
842 #include "GuiExternal_moc.cpp"