]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QPreambleDialog.C
fix up build (also need patch sent to list), bring back doc/par dialogs,
[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/forkedcall.h"
22 #include "support/filetools.h"
23 #include "gettext.h" 
24  
25 #include "QtLyXView.h"
26 #include "ControlPreamble.h" 
27
28 using std::getline;
29
30 QPreambleDialog::QPreambleDialog(QPreamble * form)
31         : QPreambleDialogBase(0, 0, false, 0),
32         form_(form)
33 {
34         connect(okPB, SIGNAL(clicked()),
35                 form, SLOT(slotOK()));
36         connect(applyPB, SIGNAL(clicked()),
37                 form, SLOT(slotApply()));
38         connect(closePB, SIGNAL(clicked()),
39                 form, SLOT(slotClose()));
40 }
41
42  
43 void QPreambleDialog::closeEvent(QCloseEvent * e)
44 {
45         form_->slotWMHide();
46         e->accept();
47 }
48
49
50 void QPreambleDialog::change_adaptor()
51 {
52         form_->changed();
53 }
54
55  
56 void QPreambleDialog::editClicked()
57 {
58         // find an editor
59         string editor = GetEnv("EDITOR");
60         if (editor.empty()) {
61                 static string lastentry = "";
62                 editor = QInputDialog::getText(
63                         _("Enter editor program"), _("Editor"), QLineEdit::Normal, 
64                         lastentry.c_str()).latin1();
65                 if (editor.empty())
66                         return;
67                 lastentry = editor; 
68         }
69
70         string const text(preambleLE->text().latin1());
71         string const filename(lyx::tempName("", "preamble"));
72         std::ofstream file(filename.c_str());
73
74         // FIXME ?
75         if (!file)
76                 return;
77
78         file << text;
79
80         file.close();
81
82         editor += " " + filename;
83  
84         Forkedcall call;
85
86         // FIXME: make async
87         if (call.startscript(Forkedcall::Wait, editor)) {
88                 lyx::unlink(filename);
89                 return;
90         }
91
92         std::ifstream in(filename.c_str());
93
94         if (!in) {
95                 lyx::unlink(filename);
96                 return;
97         }
98
99         string newtext;
100         string line;
101  
102         while (getline(in, line)) {
103                 newtext += line + "\n"; 
104         }
105
106         in.close();
107         lyx::unlink(filename);
108         preambleLE->setText(newtext.c_str());
109 }