]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImplementation.C
* qt_helpers.h:
[lyx.git] / src / frontends / qt4 / GuiImplementation.C
1 // -*- C++ -*-
2 /**
3  * \file GuiImplementation.C
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
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 // This include must be declared before everything else because
16 // of boost/Qt/LyX clash...
17 #include "GuiView.h"
18
19 #include "GuiImplementation.h"
20 #include "GuiWorkArea.h"
21
22 #include "BufferView.h"
23 #include "bufferlist.h"
24 #include "funcrequest.h"
25 #include "lyxfunc.h"
26
27 using boost::shared_ptr;
28
29 namespace lyx {
30 namespace frontend {
31
32
33 GuiImplementation::GuiImplementation(): max_view_id_(0), max_wa_id_(0)
34 {
35 }
36
37
38 int GuiImplementation::newView()
39 {
40         size_t const id = max_view_id_;
41         ++max_view_id_;
42
43         views_[id] = new GuiView(id);
44         view_ids_.push_back(id);
45
46         return id;
47 }
48
49
50 LyXView& GuiImplementation::view(int id)
51 {
52         BOOST_ASSERT(views_.find(id) != views_.end());
53
54         return *views_[id];
55 }
56
57
58 bool GuiImplementation::closeAll()
59 {
60         // ATM never used
61         if (!theBufferList().quitWriteAll())
62                 return false;
63
64         // In order to know if it is the last opened window,
65         // GuiView::closeEvent() check for (view_ids_.size() == 1)
66         // We deny this check by setting the vector size to zero.
67         // But we still need the vector, hence the temporary copy.
68         std::vector<int> view_ids_tmp = view_ids_;
69         view_ids_.clear();
70
71         for (size_t i = 0; i < view_ids_tmp.size(); ++i) {
72                 // LFUN_LYX_QUIT has already been triggered so we need
73                 // to disable the lastWindowClosed() signal before closing
74                 // the last window.
75                 views_[view_ids_tmp[i]]->setAttribute(Qt::WA_QuitOnClose, false);
76                 views_[view_ids_tmp[i]]->close();
77                 // The view_ids_ vector is reconstructed in the closeEvent; so
78                 // let's clear that out again!
79                 view_ids_.clear();
80         }
81
82         views_.clear();
83         view_ids_.clear();
84         work_areas_.clear();
85
86         return true;
87 }
88
89
90 void GuiImplementation::unregisterView(GuiView * view)
91 {
92         std::map<int, GuiView *>::iterator I;
93
94         for (I = views_.begin(); I != views_.end(); ++I) {
95                 if (I->second == view) {
96                         std::vector<int> const & wa_ids = view->workAreaIds();
97                         for (size_t i = 0; i < wa_ids.size(); ++i)
98                                 work_areas_.erase(wa_ids[i]);
99
100                         views_.erase(I->first);
101                         break;
102                 }
103         }
104
105         buildViewIds();
106
107         if (views_.empty()) {
108                 theLyXFunc().setLyXView(0);
109                 dispatch(FuncRequest(LFUN_LYX_QUIT));
110                 return;
111         }
112
113         theLyXFunc().setLyXView(views_.begin()->second);
114 }
115
116
117 void GuiImplementation::buildViewIds()
118 {
119         view_ids_.clear();
120         std::map<int, GuiView *>::const_iterator I;
121         for (I = views_.begin(); I != views_.end(); ++I)
122                 view_ids_.push_back(I->first);
123 }
124
125
126 int GuiImplementation::newWorkArea(unsigned int w, unsigned int h, int view_id)
127 {
128         size_t const id = max_wa_id_;
129         ++max_wa_id_;
130
131         GuiView * view = views_[view_id];
132
133         work_areas_[id] = new GuiWorkArea(w, h, id, *view);
134
135         // FIXME BufferView creation should be independant of WorkArea creation
136         buffer_views_[id].reset(new BufferView);
137         work_areas_[id]->setBufferView(buffer_views_[id].get());
138
139         view->setWorkArea(work_areas_[id]);
140         view->initTab(work_areas_[id]);
141         work_areas_[id]->setFocus();
142
143         return id;
144 }
145
146
147 WorkArea& GuiImplementation::workArea(int id)
148 {
149         BOOST_ASSERT(work_areas_.find(id) != work_areas_.end());
150
151         return *work_areas_[id];
152 }
153
154
155 } // namespace frontend
156 } // namespace lyx
157
158 #include "GuiImplementation_moc.cpp"