]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
layout as string
[lyx.git] / src / LyXView.C
1
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2001 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "LyXView.h"
19 #include "minibuffer.h"
20 #include "debug.h"
21 #include "intl.h"
22 #include "lyxrc.h"
23 #include "lyxtext.h"
24 #include "buffer.h"
25 #include "MenuBackend.h"
26 #include "lyx_gui_misc.h"       // [update,Close,Redraw]AllBufferRelatedDialogs
27 #include "bufferview_funcs.h" // CurrentState()
28 #include "gettext.h"
29 #include "lyxfunc.h"
30 #include "BufferView.h"
31 #include "lyxtextclasslist.h"
32
33 #include "frontends/Dialogs.h"
34 #include "frontends/Toolbar.h"
35 #include "frontends/Timeout.h"
36 #include "frontends/Menubar.h"
37
38 #include "support/filetools.h"        // OnlyFilename()
39
40 #include <sys/time.h>
41 #include <unistd.h>
42
43 using std::endl;
44
45 extern void AutoSave(BufferView *);
46 extern void QuitLyX();
47
48 string current_layout;
49
50
51 LyXView::LyXView()
52 {
53         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
54         lyxfunc = new LyXFunc(this);
55
56         intl = new Intl;
57
58         // Give the timeout some default sensible value.
59         autosave_timeout = new Timeout(5000);
60
61         dialogs_ = new Dialogs(this);
62         dialogs_->hideBufferDependent
63                 .connect(SigC::slot(&CloseAllBufferRelatedDialogs));
64         Dialogs::redrawGUI.connect(SigC::slot(this, &LyXView::redraw));
65         Dialogs::redrawGUI.connect(SigC::slot(&RedrawAllBufferRelatedDialogs));
66 }
67
68
69 LyXView::~LyXView()
70 {
71         delete menubar;
72         delete toolbar;
73         delete bufferview;
74         delete minibuffer;
75         delete lyxfunc;
76         delete intl;
77         delete autosave_timeout;
78         delete dialogs_;
79 }
80
81
82 void LyXView::resize() 
83 {
84         view()->resize();
85 }
86
87
88 /// returns the buffer currently shown in the main form.
89 Buffer * LyXView::buffer() const
90 {
91         return bufferview->buffer();
92 }
93
94
95 BufferView * LyXView::view() const
96 {
97         return bufferview;
98 }
99
100
101 Toolbar * LyXView::getToolbar() const
102 {
103         return toolbar;
104 }
105
106
107 void LyXView::setLayout(string const & layout)
108 {
109         toolbar->setLayout(layout);
110 }
111
112
113 void LyXView::updateToolbar()
114 {
115         toolbar->update();
116 }
117
118
119 LyXFunc * LyXView::getLyXFunc() const
120 {
121         return lyxfunc;
122 }
123
124
125 MiniBuffer * LyXView::getMiniBuffer() const
126 {
127         return minibuffer;
128 }
129
130
131 void LyXView::message(string const & str)
132 {
133         minibuffer->message(str);
134 }
135
136
137 void LyXView::messagePush(string const & str)
138 {
139         minibuffer->messagePush(str);
140 }
141
142
143 void LyXView::messagePop()
144 {
145         minibuffer->messagePop();
146 }
147
148
149 Menubar * LyXView::getMenubar() const
150 {
151         return menubar;
152 }
153
154
155 void LyXView::updateMenubar() 
156 {
157         if ((!view() || !view()->buffer())
158             && menubackend.hasMenu("main_nobuffer"))
159                 menubar->set("main_nobuffer");
160         else
161                 menubar->set("main");
162         menubar->update();
163 }
164
165
166 Intl * LyXView::getIntl() const
167 {
168         return intl;
169 }
170
171
172 // Callback for autosave timer
173 void LyXView::AutoSave()
174 {
175         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
176         if (view()->available())
177                 ::AutoSave(view());
178 }
179
180
181 /// Reset autosave timer
182 void LyXView::resetAutosaveTimer()
183 {
184         if (lyxrc.autosave)
185                 autosave_timeout->restart();
186 }
187
188
189 void LyXView::invalidateLayoutChoice()
190 {
191         last_textclass = -1;
192 }
193
194
195 void LyXView::updateLayoutChoice()
196 {
197         // This has a side-effect that the layouts are not showed when no
198         // document is loaded.
199         if (!view() || !view()->buffer()) {
200                 toolbar->clearLayoutList();
201                 return; 
202         }
203
204         // Update the layout display
205         if (last_textclass != int(buffer()->params.textclass)) {
206                 toolbar->updateLayoutList(true);
207                 last_textclass = int(buffer()->params.textclass);
208                 current_layout = textclasslist[last_textclass].defaultLayoutName();
209         } else {
210                 toolbar->updateLayoutList(false);
211         }
212
213         string const & layout =
214                 bufferview->getLyXText()->cursor.par()->layout();
215
216         if (layout != current_layout) {
217                 toolbar->setLayout(layout);
218                 current_layout = layout;
219         }
220 }
221
222
223 // Updates the title of the window with the filename of the current document
224 void LyXView::updateWindowTitle()
225 {
226         static string last_title = "LyX";
227         string title = "LyX";
228         string icon_title = "LyX";
229
230         if (view()->available()) {
231                 string const cur_title = buffer()->fileName();
232                 if (!cur_title.empty()) {
233                         title += ": " + MakeDisplayPath(cur_title, 30);
234                         if (!buffer()->isLyxClean())
235                                 title += _(" (Changed)");
236                         if (buffer()->isReadonly())
237                                 title += _(" (read only)");
238                         /* Show only the filename if it's available. */
239                         icon_title = OnlyFilename(cur_title);
240                 }
241         }
242         if (title != last_title) {
243                 setWindowTitle(title, icon_title);
244                 last_title = title;
245         }
246 }
247
248
249 void LyXView::showState()
250 {
251         message(currentState(view()));
252 }