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