]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QPreambleDialog.C
added pre-amble dialog with icky $EDITOR support
[lyx.git] / src / frontends / qt2 / QPreambleDialog.C
1 /**
2  * \file QPreambleDialog.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10 #include <fstream>
11
12 #include <qpushbutton.h>
13 #include <qmultilineedit.h>
14 #include <qinputdialog.h>
15
16 #include "QPreambleDialog.h"
17 #include "Dialogs.h"
18 #include "QPreamble.h"
19
20 #include "support/lyxlib.h"
21 #include "support/syscall.h"
22 #include "support/filetools.h"
23 #include "gettext.h" 
24  
25 #include "QtLyXView.h"
26 #include "ControlPreamble.h" 
27
28 QPreambleDialog::QPreambleDialog(QPreamble * form)
29         : QPreambleDialogBase(0, 0, false, 0),
30         form_(form)
31 {
32         connect(okPB, SIGNAL(clicked()),
33                 form, SLOT(slotOK()));
34         connect(applyPB, SIGNAL(clicked()),
35                 form, SLOT(slotApply()));
36         connect(closePB, SIGNAL(clicked()),
37                 form, SLOT(slotClose()));
38 }
39
40  
41 void QPreambleDialog::closeEvent(QCloseEvent * e)
42 {
43         form_->slotWMHide();
44         e->accept();
45 }
46
47
48 void QPreambleDialog::change_adaptor()
49 {
50         form_->changed();
51 }
52
53  
54 void QPreambleDialog::editClicked()
55 {
56         // find an editor
57         string editor = GetEnv("EDITOR");
58         if (editor.empty()) {
59                 static string lastentry = "";
60                 editor = QInputDialog::getText(
61                         _("Enter editor program"), _("Editor"), lastentry.c_str()).latin1();
62                 if (editor.empty())
63                         return;
64                 lastentry = editor; 
65         }
66
67         string const text(preambleLE->text().latin1());
68         string const filename(lyx::tempName("", "preamble"));
69         std::ofstream file(filename.c_str());
70
71         // FIXME ?
72         if (!file)
73                 return;
74
75         file << text;
76
77         file.close();
78
79         editor += " " + filename;
80  
81         // FIXME: synchronous, ugh. Make async when moved to controllers ?
82         Systemcalls sys(Systemcalls::Wait, editor);
83
84         std::ifstream in(filename.c_str());
85
86         if (!in)
87                 return;
88
89         string newtext;
90         string line;
91  
92         while (getline(in, line)) {
93                 newtext += line + "\n"; 
94         }
95
96         in.close();
97         lyx::unlink(filename);
98         preambleLE->setText(newtext.c_str());
99 }