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