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