]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiExternal.cpp
getting rid of superfluous lyx::support:: statements.
[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"), bbChanged_(false)
93 {
94         setupUi(this);
95         setViewTitle(_("External Material"));
96
97         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
98         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
99         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
100
101         connect(displayCB, SIGNAL(toggled(bool)),
102                 showCO, SLOT(setEnabled(bool)));
103         connect(displayCB, SIGNAL(toggled(bool)),
104                 displayscaleED, SLOT(setEnabled(bool)));
105         connect(showCO, SIGNAL(activated(QString)),
106                 this, SLOT(change_adaptor()));
107         connect(originCO, SIGNAL(activated(int)),
108                 this, SLOT(change_adaptor()));
109         connect(aspectratioCB, SIGNAL(stateChanged(int)),
110                 this, SLOT(change_adaptor()));
111         connect(browsePB, SIGNAL(clicked()),
112                 this, SLOT(browseClicked()));
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::closeEvent(QCloseEvent * e)
274 {
275         slotClose();
276         e->accept();
277 }
278
279
280 void GuiExternal::editClicked()
281 {
282         editExternal();
283 }
284
285
286
287 void GuiExternal::extraChanged(const QString& text)
288 {
289         string const format = fromqstr(extraFormatCO->currentText());
290         extra_[format] = text;
291         changed();
292 }
293
294
295 void GuiExternal::formatChanged(const QString& format)
296 {
297         extraED->setText(extra_[fromqstr(format)]);
298 }
299
300
301 void GuiExternal::getbbClicked()
302 {
303         getBB();
304 }
305
306
307 void GuiExternal::sizeChanged()
308 {
309         aspectratioCB->setEnabled(activateAspectratio());
310         changed();
311 }
312
313
314 void GuiExternal::templateChanged()
315 {
316         updateTemplate();
317         changed();
318 }
319
320
321 void GuiExternal::widthUnitChanged()
322 {
323         bool useHeight = (widthUnitCO->currentIndex() > 0);
324
325         if (useHeight)
326                 widthED->setValidator(unsignedLengthValidator(widthED));
327         else
328                 widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
329
330         heightED->setEnabled(useHeight);
331         heightUnitCO->setEnabled(useHeight);
332         changed();
333 }
334
335
336 static Length::UNIT defaultUnit()
337 {
338         Length::UNIT default_unit = Length::CM;
339         switch (lyxrc.default_papersize) {
340         case PAPER_USLETTER:
341         case PAPER_USLEGAL:
342         case PAPER_USEXECUTIVE:
343                 default_unit = Length::IN;
344                 break;
345         default:
346                 break;
347         }
348         return default_unit;
349 }
350
351
352 static void setDisplay(
353         QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
354         external::DisplayType display, unsigned int scale, bool read_only)
355 {
356         int item = 0;
357         switch (display) {
358         case external::DefaultDisplay:
359                 item = 0;
360                 break;
361         case external::MonochromeDisplay:
362                 item = 1;
363                 break;
364         case external::GrayscaleDisplay:
365                 item = 2;
366                 break;
367         case external::ColorDisplay:
368                 item = 3;
369                 break;
370         case external::PreviewDisplay:
371                 item = 4;
372                 break;
373         case external::NoDisplay:
374                 item = 0;
375                 break;
376         }
377
378         showCO.setCurrentIndex(item);
379         bool const no_display = display == external::NoDisplay;
380         showCO.setEnabled(!no_display && !read_only);
381         displayCB.setChecked(!no_display);
382         scaleED.setEnabled(!no_display && !read_only);
383         scaleED.setText(QString::number(scale));
384 }
385
386
387 static void getDisplay(external::DisplayType & display,
388                 unsigned int & scale,
389                 QCheckBox const & displayCB,
390                 QComboBox const & showCO,
391                 QLineEdit const & scaleED)
392 {
393         switch (showCO.currentIndex()) {
394         case 0:
395                 display = external::DefaultDisplay;
396                 break;
397         case 1:
398                 display = external::MonochromeDisplay;
399                 break;
400         case 2:
401                 display = external::GrayscaleDisplay;
402                 break;
403         case 3:
404                 display = external::ColorDisplay;
405                 break;
406         case 4:
407                 display = external::PreviewDisplay;
408                 break;
409         }
410
411         if (!displayCB.isChecked())
412                 display = external::NoDisplay;
413
414         scale = scaleED.text().toInt();
415 }
416
417
418 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
419         external::RotationData const & data)
420 {
421         originCO.setCurrentIndex(int(data.origin()));
422         angleED.setText(toqstr(data.angle));
423 }
424
425
426 static void getRotation(external::RotationData & data,
427         QLineEdit const & angleED, QComboBox const & originCO)
428 {
429         typedef external::RotationData::OriginType OriginType;
430
431         data.origin(static_cast<OriginType>(originCO.currentIndex()));
432         data.angle = fromqstr(angleED.text());
433 }
434
435
436 static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
437         QLineEdit & heightED, LengthCombo & heightUnitCO,
438         QCheckBox & aspectratioCB,
439         external::ResizeData const & data)
440 {
441         bool using_scale = data.usingScale();
442         string scale = data.scale;
443         if (data.no_resize()) {
444                 // Everything is zero, so default to this!
445                 using_scale = true;
446                 scale = "100";
447         }
448
449         if (using_scale) {
450                 widthED.setText(toqstr(scale));
451                 widthUnitCO.setCurrentIndex(0);
452         } else {
453                 widthED.setText(QString::number(data.width.value()));
454                 // Because 'Scale' is position 0...
455                 // Note also that width cannot be zero here, so
456                 // we don't need to worry about the default unit.
457                 widthUnitCO.setCurrentIndex(data.width.unit() + 1);
458         }
459
460         string const h = data.height.zero() ? string() : data.height.asString();
461         Length::UNIT default_unit = data.width.zero() ?
462                 defaultUnit() : data.width.unit();
463         lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
464
465         heightED.setEnabled(!using_scale);
466         heightUnitCO.setEnabled(!using_scale);
467
468         aspectratioCB.setChecked(data.keepAspectRatio);
469
470         bool const disable_aspectRatio = using_scale ||
471                 data.width.zero() || data.height.zero();
472         aspectratioCB.setEnabled(!disable_aspectRatio);
473 }
474
475
476 static void getSize(external::ResizeData & data,
477         QLineEdit const & widthED, QComboBox const & widthUnitCO,
478         QLineEdit const & heightED, LengthCombo const & heightUnitCO,
479         QCheckBox const & aspectratioCB)
480 {
481         string const width = fromqstr(widthED.text());
482
483         if (widthUnitCO.currentIndex() > 0) {
484                 // Subtract one, because scale is 0.
485                 int const unit = widthUnitCO.currentIndex() - 1;
486
487                 Length w;
488                 if (isValidLength(width, &w))
489                         data.width = w;
490                 else if (isStrDbl(width))
491                         data.width = Length(convert<double>(width),
492                                            static_cast<Length::UNIT>(unit));
493                 else
494                         data.width = Length();
495
496                 data.scale = string();
497
498         } else {
499                 // scaling instead of a width
500                 data.scale = width;
501                 data.width = Length();
502         }
503
504         data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
505
506         data.keepAspectRatio = aspectratioCB.isChecked();
507 }
508
509
510 void setCrop(QCheckBox & clipCB,
511         QLineEdit & xlED, QLineEdit & ybED,
512         QLineEdit & xrED, QLineEdit & ytED,
513         external::ClipData const & data)
514 {
515         clipCB.setChecked(data.clip);
516         graphics::BoundingBox const & bbox = data.bbox;
517         xlED.setText(QString::number(bbox.xl));
518         ybED.setText(QString::number(bbox.yb));
519         xrED.setText(QString::number(bbox.xr));
520         ytED.setText(QString::number(bbox.yt));
521 }
522
523
524 static void getCrop(external::ClipData & data,
525         QCheckBox const & clipCB,
526         QLineEdit const & xlED, QLineEdit const & ybED,
527         QLineEdit const & xrED, QLineEdit const & ytED,
528         bool bb_changed)
529 {
530         data.clip = clipCB.isChecked();
531
532         if (!bb_changed)
533                 return;
534
535         data.bbox.xl = xlED.text().toInt();
536         data.bbox.yb = ybED.text().toInt();
537         data.bbox.xr = xrED.text().toInt();
538         data.bbox.yt = ytED.text().toInt();
539 }
540
541
542 static void getExtra(external::ExtraData & data,
543               GuiExternal::MapType const & extra)
544 {
545         typedef GuiExternal::MapType MapType;
546         MapType::const_iterator it = extra.begin();
547         MapType::const_iterator const end = extra.end();
548         for (; it != end; ++it)
549                 data.set(it->first, trim(fromqstr(it->second)));
550 }
551
552
553 void GuiExternal::updateContents()
554 {
555         tab->setCurrentIndex(0);
556
557         string const name =
558                 params_.filename.outputFilename(bufferFilepath());
559         fileED->setText(toqstr(name));
560
561         externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
562         updateTemplate();
563
564         draftCB->setChecked(params_.draft);
565
566         setDisplay(*displayCB, *showCO, *displayscaleED,
567                    params_.display, params_.lyxscale, isBufferReadonly());
568
569         setRotation(*angleED, *originCO, params_.rotationdata);
570
571         setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
572                 *aspectratioCB, params_.resizedata);
573
574         setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
575         bbChanged_ = !params_.clipdata.bbox.empty();
576
577         isValid();
578 }
579
580
581 void GuiExternal::updateTemplate()
582 {
583         external::Template templ = getTemplate(externalCO->currentIndex());
584         externalTB->setPlainText(qt_(templ.helpText));
585
586         // Ascertain which (if any) transformations the template supports
587         // and disable tabs hosting unsupported transforms.
588         typedef vector<external::TransformID> TransformIDs;
589         TransformIDs const transformIds = templ.transformIds;
590         TransformIDs::const_iterator tr_begin = transformIds.begin();
591         TransformIDs::const_iterator const tr_end = transformIds.end();
592
593         bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
594         tab->setTabEnabled(tab->indexOf(rotatetab), found);
595         found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
596         tab->setTabEnabled(tab->indexOf(scaletab), found);
597
598         found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
599         tab->setTabEnabled(tab->indexOf(croptab), found);
600
601         found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
602         tab->setTabEnabled(tab->indexOf(optionstab), found);
603
604         if (!found)
605                 return;
606
607         // Ascertain whether the template has any formats supporting
608         // the 'Extra' option
609         extra_.clear();
610         extraED->clear();
611         extraFormatCO->clear();
612
613         external::Template::Formats::const_iterator it  = templ.formats.begin();
614         external::Template::Formats::const_iterator end = templ.formats.end();
615         for (; it != end; ++it) {
616                 if (it->second.option_transformers.find(external::Extra) ==
617                     it->second.option_transformers.end())
618                         continue;
619                 string const format = it->first;
620                 string const opt = params_.extradata.get(format);
621                 extraFormatCO->addItem(toqstr(format));
622                 extra_[format] = toqstr(opt);
623         }
624
625         bool const enabled = extraFormatCO->count()  > 0;
626
627         tab->setTabEnabled(
628                 tab->indexOf(optionstab), enabled);
629         extraED->setEnabled(enabled && !isBufferReadonly());
630         extraFormatCO->setEnabled(enabled);
631
632         if (enabled) {
633                 extraFormatCO->setCurrentIndex(0);
634                 extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
635         }
636 }
637
638
639 void GuiExternal::applyView()
640 {
641         params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
642
643         params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
644
645         params_.draft = draftCB->isChecked();
646
647         getDisplay(params_.display, params_.lyxscale,
648                    *displayCB, *showCO,
649                    *displayscaleED);
650
651         if (tab->isTabEnabled(tab->indexOf(rotatetab)))
652                 getRotation(params_.rotationdata, *angleED, *originCO);
653
654         if (tab->isTabEnabled(tab->indexOf(scaletab)))
655                 getSize(params_.resizedata, *widthED, *widthUnitCO,
656                         *heightED, *heightUnitCO, *aspectratioCB);
657
658         if (tab->isTabEnabled(tab->indexOf(croptab)))
659                 getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
660                         *xrED, *ytED, bbChanged_);
661
662         if (tab->isTabEnabled(tab->indexOf(optionstab)))
663                 getExtra(params_.extradata, extra_);
664 }
665
666
667 void GuiExternal::getBB()
668 {
669         xlED->setText("0");
670         ybED->setText("0");
671         xrED->setText("0");
672         ytED->setText("0");
673
674         string const filename = fromqstr(fileED->text());
675         if (filename.empty())
676                 return;
677
678         string const bb = readBB(filename);
679         if (bb.empty())
680                 return;
681
682         xlED->setText(toqstr(token(bb, ' ', 0)));
683         ybED->setText(toqstr(token(bb, ' ', 1)));
684         xrED->setText(toqstr(token(bb, ' ', 2)));
685         ytED->setText(toqstr(token(bb, ' ', 3)));
686
687         bbChanged_ = false;
688 }
689
690
691 bool GuiExternal::initialiseParams(string const & data)
692 {
693         InsetExternalMailer::string2params(data, buffer(), params_);
694         return true;
695 }
696
697
698 void GuiExternal::clearParams()
699 {
700         params_ = InsetExternalParams();
701 }
702
703
704 void GuiExternal::dispatchParams()
705 {
706         string const lfun = InsetExternalMailer::params2string(params_, buffer());
707         dispatch(FuncRequest(getLfun(), lfun));
708 }
709
710
711 void GuiExternal::editExternal()
712 {
713         applyView();
714         string const lfun = InsetExternalMailer::params2string(params_, buffer());
715         dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
716 }
717
718
719 vector<string> const GuiExternal::getTemplates() const
720 {
721         vector<string> result;
722
723         external::TemplateManager::Templates::const_iterator i1, i2;
724         i1 = external::TemplateManager::get().getTemplates().begin();
725         i2 = external::TemplateManager::get().getTemplates().end();
726
727         for (; i1 != i2; ++i1) {
728                 result.push_back(i1->second.lyxName);
729         }
730         return result;
731 }
732
733
734 int GuiExternal::getTemplateNumber(string const & name) const
735 {
736         external::TemplateManager::Templates::const_iterator i1, i2;
737         i1 = external::TemplateManager::get().getTemplates().begin();
738         i2 = external::TemplateManager::get().getTemplates().end();
739         for (int i = 0; i1 != i2; ++i1, ++i) {
740                 if (i1->second.lyxName == name)
741                         return i;
742         }
743
744         // we can get here if a LyX document has a template not installed
745         // on this machine.
746         return -1;
747 }
748
749
750 external::Template GuiExternal::getTemplate(int i) const
751 {
752         external::TemplateManager::Templates::const_iterator i1
753                 = external::TemplateManager::get().getTemplates().begin();
754
755         advance(i1, i);
756
757         return i1->second;
758 }
759
760
761 string const
762 GuiExternal::getTemplateFilters(string const & template_name) const
763 {
764         /// Determine the template file extension
765         external::TemplateManager const & etm =
766                 external::TemplateManager::get();
767         external::Template const * const et_ptr =
768                 etm.getTemplateByName(template_name);
769
770         if (et_ptr)
771                 return et_ptr->fileRegExp;
772
773         return string();
774 }
775
776
777 docstring const GuiExternal::browse(docstring const & input,
778                                      docstring const & template_name) const
779 {
780         docstring const title =  _("Select external file");
781
782         docstring const bufpath = from_utf8(bufferFilepath());
783         FileFilterList const filter =
784                 FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
785
786         docstring const label1 = _("Documents|#o#O");
787         docstring const dir1 = from_utf8(lyxrc.document_path);
788
789         return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
790 }
791
792
793 string const GuiExternal::readBB(string const & file)
794 {
795         FileName const abs_file(makeAbsPath(file, bufferFilepath()));
796
797         // try to get it from the file, if possible. Zipped files are
798         // unzipped in the readBB_from_PSFile-Function
799         string const bb = readBB_from_PSFile(abs_file);
800         if (!bb.empty())
801                 return bb;
802
803         // we don't, so ask the Graphics Cache if it has loaded the file
804         int width = 0;
805         int height = 0;
806
807         graphics::Cache & gc = graphics::Cache::get();
808         if (gc.inCache(abs_file)) {
809                 graphics::Image const * image = gc.item(abs_file)->image();
810
811                 if (image) {
812                         width  = image->width();
813                         height = image->height();
814                 }
815         }
816
817         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
818 }
819
820
821 Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
822
823
824 } // namespace frontend
825 } // namespace lyx
826
827 #include "GuiExternal_moc.cpp"