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