]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
Handle Qt-style file filters like
[lyx.git] / src / frontends / LyXView.C
1 /**
2  * \file LyXView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "LyXView.h"
15 #include "Dialogs.h"
16 #include "Timeout.h"
17 #include "Toolbar.h"
18 #include "Menubar.h"
19
20 #include "buffer.h"
21 #include "bufferparams.h"
22 #include "BufferView.h"
23 #include "bufferview_funcs.h"
24 #include "debug.h"
25 #include "funcrequest.h"
26 #include "gettext.h"
27 #include "intl.h"
28 #include "lyx_cb.h"
29 #include "lyxfunc.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "MenuBackend.h"
33 #include "paragraph.h"
34
35 #include "controllers/ControlCommandBuffer.h"
36
37 #include "mathed/math_cursor.h"
38
39 #include "support/filetools.h" // OnlyFilename()
40
41 #include <boost/bind.hpp>
42
43 #include <sys/time.h>
44 #include <unistd.h>
45
46 using lyx::support::MakeDisplayPath;
47 using lyx::support::OnlyFilename;
48
49 using std::endl;
50 using std::string;
51
52
53 string current_layout;
54
55
56 LyXView::LyXView()
57         : intl_(new Intl),
58           autosave_timeout_(new Timeout(5000)),
59           lyxfunc_(new LyXFunc(this)),
60           dialogs_(new Dialogs(*this)),
61           controlcommand_(new ControlCommandBuffer(*this))
62 {
63         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
64 }
65
66
67 LyXView::~LyXView()
68 {
69 }
70
71
72 void LyXView::init()
73 {
74         updateLayoutChoice();
75         updateMenubar();
76
77         // Start autosave timer
78         if (lyxrc.autosave) {
79                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
80                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
81                 autosave_timeout_->start();
82         }
83
84         intl_->InitKeyMapper(lyxrc.use_kbmap);
85 }
86
87
88 Buffer * LyXView::buffer() const
89 {
90         return bufferview_->buffer();
91 }
92
93
94 boost::shared_ptr<BufferView> const & LyXView::view() const
95 {
96         return bufferview_;
97 }
98
99
100 void LyXView::setLayout(string const & layout)
101 {
102         toolbar_->setLayout(layout);
103 }
104
105
106 void LyXView::updateToolbar()
107 {
108         bool const math = mathcursor;
109         bool const table =
110                 !getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).disabled();
111         toolbar_->update(math, table);
112 }
113
114
115 void LyXView::updateMenubar()
116 {
117         menubar_->update();
118 }
119
120
121 void LyXView::autoSave()
122 {
123         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
124
125         if (view()->available()) {
126                 ::AutoSave(view().get());
127         }
128 }
129
130
131 void LyXView::resetAutosaveTimer()
132 {
133         if (lyxrc.autosave)
134                 autosave_timeout_->restart();
135 }
136
137
138 void LyXView::updateLayoutChoice()
139 {
140         // don't show any layouts without a buffer
141         if (!view()->buffer()) {
142                 toolbar_->clearLayoutList();
143                 return;
144         }
145
146         // update the layout display
147         if (toolbar_->updateLayoutList(buffer()->params().textclass)) {
148                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
149         }
150
151         string const & layout =
152                 bufferview_->getLyXText()->cursorPar()->layout()->name();
153
154         if (layout != current_layout) {
155                 toolbar_->setLayout(layout);
156                 current_layout = layout;
157         }
158 }
159
160
161 void LyXView::updateWindowTitle()
162 {
163         static string last_title = "LyX";
164         string maximize_title = "LyX";
165         string minimize_title = "LyX";
166
167         if (view()->available()) {
168                 string const cur_title = buffer()->fileName();
169                 if (!cur_title.empty()) {
170                         maximize_title += ": " + MakeDisplayPath(cur_title, 30);
171                         minimize_title = OnlyFilename(cur_title);
172                         if (!buffer()->isClean()) {
173                                 maximize_title += _(" (changed)");
174                                 minimize_title += '*';
175                         }
176                         if (buffer()->isReadonly())
177                                 maximize_title += _(" (read only)");
178                 }
179         }
180
181         if (maximize_title != last_title) {
182                 setWindowTitle(maximize_title, minimize_title);
183                 last_title = maximize_title;
184         }
185 }
186
187
188 void LyXView::dispatch(FuncRequest const & req)
189 {
190         // substitute the correct BufferView here
191         FuncRequest r = req;
192         r.setView(view().get());
193         getLyXFunc().dispatch(r);
194 }
195
196
197 Buffer const * const LyXView::updateInset(InsetOld const * inset) const
198 {
199         Buffer const * buffer_ptr = 0;
200         if (inset) {
201                 buffer_ptr = bufferview_->buffer();
202                 bufferview_->update();
203         }
204         return buffer_ptr;
205 }