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