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