]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImplementation.cpp
Rename .C ==> .cpp for files in src/frontends/qt4, part two
[lyx.git] / src / frontends / qt4 / GuiImplementation.cpp
1 // -*- C++ -*-
2 /**
3  * \file GuiImplementation.cpp
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 #include <QApplication>
28
29 using boost::shared_ptr;
30
31
32 namespace
33 {
34         template<class T>
35         void updateIds(std::map<int, T*> const & stdmap, std::vector<int> & ids) 
36         {
37                 ids.clear();
38                 typename std::map<int, T*>::const_iterator it;
39                 for (it = stdmap.begin(); it != stdmap.end(); ++it)
40                         ids.push_back(it->first);
41         }
42 }
43
44
45 namespace lyx {
46 namespace frontend {
47
48
49 GuiImplementation::GuiImplementation()
50 {
51         view_ids_.clear();
52         work_area_ids_.clear();
53 }
54
55
56 LyXView& GuiImplementation::createRegisteredView()
57 {
58         updateIds(views_, view_ids_);
59         int id = 0;
60         while (views_.find(id) != views_.end())
61                 id++;
62         views_.insert(std::pair<int, GuiView *>(id, new GuiView(id)));
63         updateIds(views_, view_ids_);
64         return *views_[id];
65 }
66
67
68 bool GuiImplementation::unregisterView(int id)
69 {
70         updateIds(views_, view_ids_);
71         BOOST_ASSERT(views_.find(id) != views_.end());
72         BOOST_ASSERT(views_[id]);
73
74         std::map<int, GuiView *>::iterator it;
75         for (it = views_.begin(); it != views_.end(); ++it) {
76                 if (it->first == id) {
77                         std::vector<int> const & wa_ids = it->second->workAreaIds();
78                         for (size_t i = 0; i < wa_ids.size(); ++i)
79                                 work_areas_.erase(wa_ids[i]);
80                         views_.erase(id);
81                         break;
82                 }
83         }
84         updateIds(views_, view_ids_);
85         return true;
86 }
87
88
89 bool GuiImplementation::closeAllViews()
90 {
91         updateIds(views_, view_ids_);
92         if (views_.empty())
93         {
94                 // quit in CloseEvent will not be triggert
95                 qApp->quit();
96                 return true;
97         }
98
99         std::map<int, GuiView*> const cmap = views_;
100         std::map<int, GuiView*>::const_iterator it;
101         for (it = cmap.begin(); it != cmap.end(); ++it)
102         {
103                 // TODO: return false when close event was ignored
104                 //       e.g. quitWriteAll()->'Cancel'
105                 //       maybe we need something like 'bool closeView()'
106                 it->second->close(); 
107                 // unregisterd by the CloseEvent
108         }
109
110         views_.clear();
111         work_areas_.clear();
112         view_ids_.clear();
113         work_area_ids_.clear();
114         return true;
115 }
116
117
118 LyXView& GuiImplementation::view(int id) const
119 {
120         BOOST_ASSERT(views_.find(id) != views_.end());
121         return *views_.find(id)->second;
122 }
123
124
125 std::vector<int> const & GuiImplementation::workAreaIds()
126 {
127         updateIds(work_areas_, work_area_ids_);
128         return work_area_ids_;
129 }
130
131
132 int GuiImplementation::newWorkArea(unsigned int w, unsigned int h, int view_id)
133 {
134         updateIds(views_, view_ids_);
135         int id = 0;
136         while (work_areas_.find(id) != work_areas_.end())
137                 id++;
138
139         GuiView * view = views_[view_id];
140
141         work_areas_.insert(std::pair<int, GuiWorkArea *>
142                                         (id, new GuiWorkArea(w, h, id, *view)));
143
144         // FIXME BufferView creation should be independant of WorkArea creation
145         buffer_views_[id].reset(new BufferView);
146         work_areas_[id]->setBufferView(buffer_views_[id].get());
147
148         view->setWorkArea(work_areas_[id]);
149         view->initTab(work_areas_[id]);
150
151         return id;
152 }
153
154
155 WorkArea& GuiImplementation::workArea(int id)
156 {
157         BOOST_ASSERT(work_areas_.find(id) != work_areas_.end());
158         return *work_areas_[id];
159 }
160
161
162 } // namespace frontend
163 } // namespace lyx
164
165 #include "GuiImplementation_moc.cpp"