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