]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.C
f1611262deef44042de4e246d3e16c7a0e9171ae
[lyx.git] / src / frontends / qt4 / GuiView.C
1 /**
2  * \file GuiView.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  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiView.h"
16
17 #include "GuiImplementation.h"
18 #include "GuiWorkArea.h"
19 #include "QLMenubar.h"
20 #include "QLToolbar.h"
21 #include "QCommandBuffer.h"
22 #include "qt_helpers.h"
23
24 #include "frontends/Application.h"
25 #include "frontends/Gui.h"
26 #include "frontends/WorkArea.h"
27
28 #include "support/filetools.h"
29 #include "support/convert.h"
30 #include "support/lstrings.h"
31
32 #include "BufferView.h"
33 #include "bufferlist.h"
34 #include "debug.h"
35 #include "funcrequest.h"
36 #include "lyx_cb.h"
37 #include "lyxrc.h"
38 #include "lyx_main.h"
39 #include "session.h"
40 #include "lyxfunc.h"
41 #include "MenuBackend.h"
42
43 #include <QAction>
44 #include <QApplication>
45 #include <QCloseEvent>
46 #include <QPixmap>
47 #include <QStatusBar>
48 #include <QToolBar>
49
50 #include <boost/bind.hpp>
51
52 using std::endl;
53 using std::string;
54 using std::vector;
55
56 namespace lyx {
57
58 using support::subst;
59 using support::libFileSearch;
60
61 namespace frontend {
62
63 namespace {
64
65 int const statusbar_timer_value = 3000;
66
67 } // namespace anon
68
69
70 GuiView::GuiView(int id)
71         : QMainWindow(), LyXView(id), commandbuffer_(0)
72 {
73         setAttribute(Qt::WA_DeleteOnClose, true);
74         setAttribute(Qt::WA_QuitOnClose, true);
75
76 //      setToolButtonStyle(Qt::ToolButtonIconOnly);
77 //      setIconSize(QSize(12,12));
78
79 //      bufferview_.reset(new BufferView(this, width, height));
80
81 #ifndef Q_WS_MACX
82         //  assign an icon to main form. We do not do it under Qt/Mac,
83         //  since the icon is provided in the application bundle.
84         string const iconname = libFileSearch("images", "lyx", "xpm");
85         if (!iconname.empty())
86                 setWindowIcon(QPixmap(toqstr(iconname)));
87 #endif
88 }
89
90
91 GuiView::~GuiView()
92 {
93 }
94
95
96 void GuiView::close()
97 {
98         QMainWindow::close();
99 }
100
101
102 void GuiView::init()
103 {
104         menubar_.reset(new QLMenubar(this, menubackend));
105         QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
106                 this, SLOT(updateMenu(QAction *)));
107
108         getToolbars().init();
109
110         statusBar()->setSizeGripEnabled(false);
111
112         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
113                 this, SLOT(update_view_state_qt()));
114
115         // make sure the buttons are disabled if needed
116         updateToolbars();
117         updateLayoutChoice();
118         updateMenubar();
119 }
120
121
122 void GuiView::saveGeometry()
123 {
124         // FIXME:
125         // change the ifdef to 'geometry = normalGeometry();' only
126         // when Trolltech has fixed the broken normalGeometry on X11:
127         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
128         // Then also the moveEvent, resizeEvent, and the
129         // code for floatingGeometry_ can be removed;
130         // adjust GuiView::setGeometry()
131 #ifdef Q_OS_WIN32
132         QRect geometry = normalGeometry();
133 #else
134         updateFloatingGeometry();
135         QRect geometry = floatingGeometry_;
136 #endif
137
138         // save windows size and position
139         Session & session = LyX::ref().session();
140         session.saveSessionInfo("WindowWidth", convert<string>(geometry.width()));
141         session.saveSessionInfo("WindowHeight", convert<string>(geometry.height()));
142         session.saveSessionInfo("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
143         if (lyxrc.geometry_xysaved) {
144                 session.saveSessionInfo("WindowPosX", convert<string>(geometry.x()));
145                 session.saveSessionInfo("WindowPosY", convert<string>(geometry.y()));
146         }
147 }
148                                                   
149 void GuiView::setGeometry(unsigned int width,
150                                                                   unsigned int height,
151                                                                   int posx, int posy,
152                                                                   bool maximize)
153 {
154         // only true when the -geometry option was NOT used
155         if (width != 0 && height != 0) {
156                 if (posx != -1 && posy != -1) {
157 #ifdef Q_WS_WIN
158                         // FIXME: use only setGeoemtry when Trolltech has
159                         // fixed the qt4/X11 bug
160                         QMainWindow::setGeometry(posx, posy,width, height);
161 #else
162                         resize(width, height);
163                         move(posx, posy);
164 #endif
165                 } else {
166                         resize(width, height);
167                 }
168
169                 if (maximize)
170                         setWindowState(Qt::WindowMaximized);
171         }
172         
173         show();
174 }
175
176
177 void GuiView::updateMenu(QAction * /*action*/)
178 {
179         menubar_->update();
180 }
181
182
183 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
184 {
185         QMainWindow::setWindowTitle(toqstr(t));
186         QMainWindow::setWindowIconText(toqstr(it));
187 }
188
189
190 void GuiView::addCommandBuffer(QToolBar * toolbar)
191 {
192         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
193         focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
194         toolbar->addWidget(commandbuffer_);
195 }
196
197
198 void GuiView::message(docstring const & str)
199 {
200         statusBar()->showMessage(toqstr(str));
201         statusbar_timer_.stop();
202         statusbar_timer_.start(statusbar_timer_value);
203 }
204
205
206 void GuiView::clearMessage()
207 {
208         update_view_state_qt();
209 }
210
211
212 void GuiView::focus_command_widget()
213 {
214         if (commandbuffer_)
215                 commandbuffer_->focus_command();
216 }
217
218
219 void GuiView::update_view_state_qt()
220 {
221         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
222         statusbar_timer_.stop();
223 }
224
225
226 void GuiView::updateStatusBar()
227 {
228         // let the user see the explicit message
229         if (statusbar_timer_.isActive())
230                 return;
231
232         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
233 }
234
235
236 void GuiView::activated(FuncRequest const & func)
237 {
238         dispatch(func);
239 }
240
241
242 bool GuiView::hasFocus() const
243 {
244         return qApp->activeWindow() == this;
245 }
246
247
248 void  GuiView::updateFloatingGeometry()
249 {
250         if (!isMaximized())
251                 floatingGeometry_ = QRect(x(), y(), width(), height());
252 }
253
254
255 void GuiView::resizeEvent(QResizeEvent *)
256 {
257         updateFloatingGeometry();
258 }
259
260
261 void GuiView::moveEvent(QMoveEvent *)
262 {
263         updateFloatingGeometry();
264 }
265
266
267 void GuiView::closeEvent(QCloseEvent * close_event)
268 {
269         GuiImplementation & gui 
270                 = static_cast<GuiImplementation &>(theApp->gui());
271
272         vector<int> const & view_ids = gui.viewIds();
273
274         if (view_ids.size() == 1 && !theBufferList().quitWriteAll()) {
275                 close_event->ignore();
276                 return;
277         }
278
279         saveGeometry();
280         gui.unregisterView(this);
281 }
282
283
284 void GuiView::show()
285 {
286         QMainWindow::setWindowTitle(qt_("LyX"));
287         QMainWindow::show();
288         updateFloatingGeometry();
289 }
290
291
292 void GuiView::busy(bool yes)
293 {
294         static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
295
296         if (yes) {
297                 work_area_->stopBlinkingCursor();
298                 QApplication::setOverrideCursor(Qt::WaitCursor);
299         }
300         else {
301                 work_area_->startBlinkingCursor();
302                 QApplication::restoreOverrideCursor();
303         }
304 }
305
306
307 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
308 {
309         QLToolbar * Tb = new QLToolbar(tbb, *this);
310         static QLToolbar * lastTb = 0;
311
312         if (tbb.flags & ToolbarBackend::TOP) {
313                         addToolBar(Qt::TopToolBarArea, Tb);
314                         addToolBarBreak(Qt::TopToolBarArea);
315         }
316         if (tbb.flags & ToolbarBackend::BOTTOM) {
317                 addToolBar(Qt::BottomToolBarArea, Tb);
318                 if (lastTb)
319                         insertToolBarBreak(lastTb);
320                 lastTb = Tb;
321         }
322         if (tbb.flags & ToolbarBackend::LEFT) {
323                 addToolBar(Qt::LeftToolBarArea, Tb);
324         }
325         if (tbb.flags & ToolbarBackend::RIGHT) {
326                 addToolBar(Qt::RightToolBarArea, Tb);
327         }
328
329         return Toolbars::ToolbarPtr(Tb);
330 }
331
332 } // namespace frontend
333 } // namespace lyx
334
335 #include "GuiView_moc.cpp"