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