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