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