aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Gnome.cc321
-rw-r--r--src/Gnome.hh96
-rw-r--r--src/Observer.cc33
-rw-r--r--src/Observer.hh39
-rw-r--r--src/Subject.cc80
-rw-r--r--src/Subject.hh54
6 files changed, 623 insertions, 0 deletions
diff --git a/src/Gnome.cc b/src/Gnome.cc
new file mode 100644
index 0000000..9a5d9cb
--- /dev/null
+++ b/src/Gnome.cc
@@ -0,0 +1,321 @@
1// Gnome.cc for fluxbox
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@fluxbox.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Gnome.cc,v 1.1 2002/09/07 20:32:44 fluxgen Exp $
23
24#include "Gnome.hh"
25#include "Window.hh"
26#include "Screen.hh"
27
28#include <iostream>
29using namespace std;
30
31Gnome::Gnome() {
32 createAtoms();
33}
34
35Gnome::~Gnome() {
36 // destroy gnome windows
37 while (!m_gnomewindows.empty()) {
38 XDestroyWindow(BaseDisplay::getXDisplay(), m_gnomewindows.back());
39 m_gnomewindows.pop_back();
40 }
41}
42
43void Gnome::initForScreen(const BScreen &screen) {
44 Display *disp = BaseDisplay::getXDisplay();
45 // create the GNOME window
46 Window gnome_win = XCreateSimpleWindow(disp,
47 screen.getRootWindow(), 0, 0, 5, 5, 0, 0, 0);
48 // supported WM check
49 XChangeProperty(disp, screen.getRootWindow(),
50 m_gnome_wm_supporting_wm_check,
51 XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &gnome_win, 1);
52
53 XChangeProperty(disp, gnome_win,
54 m_gnome_wm_supporting_wm_check,
55 XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &gnome_win, 1);
56
57 Atom gnomeatomlist[] = {
58 m_gnome_wm_supporting_wm_check,
59 m_gnome_wm_win_workspace_names,
60 m_gnome_wm_win_client_list,
61 m_gnome_wm_win_state,
62 m_gnome_wm_win_hints
63// m_gnome_wm_win_layer not supported yet
64 };
65
66 //list atoms that we support
67 XChangeProperty(disp, screen.getRootWindow(),
68 m_gnome_wm_prot, XA_ATOM, 32, PropModeReplace,
69 (unsigned char *)gnomeatomlist, (sizeof gnomeatomlist)/sizeof gnomeatomlist[0]);
70
71 m_gnomewindows.push_back(gnome_win);
72
73 updateClientList(screen);
74 updateWorkspaceNames(screen);
75 updateWorkspaceCount(screen);
76 updateCurrentWorkspace(screen);
77
78}
79
80void Gnome::updateClientList(const BScreen &screen) {
81 size_t num=0;
82
83 BScreen::Workspaces::const_iterator workspace_it = screen.getWorkspacesList().begin();
84 BScreen::Workspaces::const_iterator workspace_it_end = screen.getWorkspacesList().end();
85 for (; workspace_it != workspace_it_end; ++workspace_it) {
86 num += (*workspace_it)->getWindowList().size();
87 }
88 //int num = getCurrentWorkspace()->getWindowList().size();
89
90 Window *wl = new (nothrow) Window[num];
91 if (wl == 0) {
92 cerr<<"Fatal: Out of memory, can't allocate for gnome client list"<<endl;
93 return;
94 }
95 //start the iterator from begining
96 workspace_it = screen.getWorkspacesList().begin();
97 int win=0;
98 for (; workspace_it != workspace_it_end; ++workspace_it) {
99
100 // Fill in array of window ID's
101 Workspace::Windows::const_iterator it = (*workspace_it)->getWindowList().begin();
102 Workspace::Windows::const_iterator it_end = (*workspace_it)->getWindowList().end();
103 for (; it != it_end; ++it) {
104 // TODO!
105 //check if the window don't want to be visible in the list
106 //if (! ( (*it)->getGnomeHints() & WIN_STATE_HIDDEN) ) {
107 wl[win++] = (*it)->getClientWindow();
108 //}
109 }
110 }
111 //number of windows to show in client list
112 num = win;
113 XChangeProperty(BaseDisplay::getXDisplay(),
114 screen.getRootWindow(),
115 m_gnome_wm_win_client_list,
116 XA_CARDINAL, 32,
117 PropModeReplace, (unsigned char *)wl, num);
118
119 delete wl;
120}
121
122void Gnome::updateWorkspaceNames(const BScreen &screen) {
123 XTextProperty text;
124 int number_of_desks = screen.getWorkspaceNames().size();
125
126 char s[1024];
127 char *names[number_of_desks];
128
129 for (int i = 0; i < number_of_desks; i++) {
130 sprintf(s, "Desktop %i", i);
131 names[i] = new char[strlen(s) + 1];
132 strcpy(names[i], s);
133 }
134
135 if (XStringListToTextProperty(names, number_of_desks, &text)) {
136 XSetTextProperty(BaseDisplay::getXDisplay(), screen.getRootWindow(),
137 &text, m_gnome_wm_win_workspace_names);
138 XFree(text.value);
139 }
140
141 for (int i = 0; i < number_of_desks; i++)
142 delete [] names[i];
143}
144
145void Gnome::updateCurrentWorkspace(const BScreen &screen) {
146 int workspace = screen.getCurrentWorkspaceID();
147 XChangeProperty(BaseDisplay::getXDisplay(),
148 screen.getRootWindow(),
149 m_gnome_wm_win_workspace, XA_CARDINAL, 32, PropModeReplace,
150 (unsigned char *)&workspace, 1);
151
152 updateClientList(screen); // make sure the client list is updated too
153}
154
155void Gnome::updateWorkspaceCount(const BScreen &screen) {
156 int numworkspaces = screen.getCount();
157 XChangeProperty(BaseDisplay::getXDisplay(), screen.getRootWindow(),
158 m_gnome_wm_win_workspace_count, XA_CARDINAL, 32, PropModeReplace,
159 (unsigned char *)&numworkspaces, 1);
160}
161
162void Gnome::updateState(FluxboxWindow *win) {
163 //translate to gnome win state
164 int state=0;
165 if (win->isStuck())
166 state |= WIN_STATE_STICKY;
167 if (win->isIconic())
168 state |= WIN_STATE_MINIMIZED;
169 if (win->isShaded())
170 state |= WIN_STATE_SHADED;
171
172 XChangeProperty(BaseDisplay::getXDisplay(), win->getClientWindow(),
173 m_gnome_wm_win_state,
174 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&state, 1);
175}
176
177void Gnome::updateHints(FluxboxWindow *win) {
178 //TODO
179
180}
181
182bool Gnome::checkClientMessage(const XClientMessageEvent &ce, BScreen *screen, FluxboxWindow *win) {
183 if (ce.message_type == m_gnome_wm_win_workspace) {
184#ifdef DEBUG
185 cerr<<__FILE__<<"("<<__LINE__<<"): Got workspace atom="<<ce.data.l[0]<<endl;
186#endif//!DEBUG
187 if ( win !=0 && // the message sent to client window?
188 win->getScreen() && ce.data.l[0] >= 0 &&
189 ce.data.l[0] < (signed)win->getScreen()->getCount()) {
190 win->getScreen()->changeWorkspaceID(ce.data.l[0]);
191
192 } else if (screen!=0 && //the message sent to root window?
193 ce.data.l[0] >= 0 &&
194 ce.data.l[0] < (signed)screen->getCount())
195 screen->changeWorkspaceID(ce.data.l[0]);
196 return true;
197 } else if (win == 0)
198 return false;
199
200
201 if (ce.message_type == m_gnome_wm_win_state) {
202#ifdef DEBUG
203 cerr<<__FILE__<<"("<<__LINE__<<"): _WIN_STATE"<<endl;
204#endif // DEBUG
205
206#ifdef DEBUG
207 cerr<<__FILE__<<"("<<__LINE__<<"): Mask of members to change:"<<
208 hex<<ce.data.l[0]<<dec<<endl; // mask_of_members_to_change
209 cerr<<"New members:"<<ce.data.l[1]<<endl;
210#endif // DEBUG
211
212 //get new states
213 int flag = ce.data.l[0] & ce.data.l[1];
214 //don't update this when when we set new state
215 disableUpdate();
216 // convert to Fluxbox state
217 setState(win, flag);
218 // enable update of atom states
219 enableUpdate();
220
221 } else if (ce.message_type == m_gnome_wm_win_hints) {
222#ifdef DEBUG
223 cerr<<__FILE__<<"("<<__LINE__<<"): _WIN_HINTS"<<endl;
224#endif // DEBUG
225
226 } else
227 return false; //the gnome atom wasn't found or not supported
228
229 return true; // we handled the atom
230}
231
232void Gnome::setState(FluxboxWindow *win, int state) {
233#ifdef DEBUG
234 cerr<<"Gnome: state=0x"<<hex<<state<<dec<<endl;
235#endif // DEBUG
236
237 if (state & WIN_STATE_STICKY) {
238#ifdef DEBUG
239 cerr<<"Gnome state: Sticky"<<endl;
240#endif // DEBUG
241 if (!win->isStuck())
242 win->stick();
243 } else if (win->isStuck())
244 win->stick();
245
246 if (state & WIN_STATE_MINIMIZED) {
247#ifdef DEBUG
248 cerr<<"Gnome state: Minimized"<<endl;
249#endif // DEBUG
250 if (win->isIconic())
251 win->iconify();
252 } else if (win->isIconic())
253 win->deiconify(true, true);
254
255 if (state & WIN_STATE_SHADED) {
256#ifdef DEBUG
257 cerr<<"Gnome state: Shade"<<endl;
258#endif // DEBUG
259 if (!win->isShaded())
260 win->shade();
261 } else if (win->isShaded())
262 win->shade();
263
264 /* TODO
265 if (state & WIN_STATE_MAXIMIZED_VERT)
266 cerr<<"Maximize Vert"<<endl;
267 if (state & WIN_STATE_MAXIMIZED_HORIZ)
268 cerr<<"Maximize Horiz"<<endl;
269 if (state & WIN_STATE_HIDDEN)
270 cerr<<"Hidden"<<endl;
271 if (state & WIN_STATE_HID_WORKSPACE)
272 cerr<<"HID Workspace"<<endl;
273 if (state & WIN_STATE_HID_TRANSIENT)
274 cerr<<"HID Transient"<<endl;
275 if (state & WIN_STATE_FIXED_POSITION)
276 cerr<<"Fixed Position"<<endl;
277 if (state & WIN_STATE_ARRANGE_IGNORE)
278 cerr<<"Arrange Ignore"<<endl;
279 */
280}
281
282void Gnome::setLayer(GnomeLayer layer) {
283 FluxboxWindow::WinLayer winlayer;
284
285 switch (layer) {
286 case WIN_LAYER_DESKTOP:
287 winlayer = FluxboxWindow::LAYER_BOTTOM;
288 break;
289 case WIN_LAYER_BELOW:
290 winlayer = FluxboxWindow::LAYER_BELOW;
291 break;
292 case WIN_LAYER_NORMAL:
293 winlayer = FluxboxWindow::LAYER_NORMAL;
294 break;
295 case WIN_LAYER_ONTOP:
296 case WIN_LAYER_DOCK:
297 case WIN_LAYER_ABOVE_DOCK:
298 case WIN_LAYER_MENU:
299 winlayer = FluxboxWindow::LAYER_TOP;
300 break;
301 default:
302 winlayer = FluxboxWindow::LAYER_NORMAL;
303 break;
304 }
305}
306
307void Gnome::createAtoms() {
308 Display *disp = BaseDisplay::getXDisplay();
309 m_gnome_wm_win_layer = XInternAtom(disp, "_WIN_LAYER", False);
310 m_gnome_wm_win_state = XInternAtom(disp, "_WIN_STATE", False);
311 m_gnome_wm_win_hints = XInternAtom(disp, "_WIN_HINTS", False);
312 m_gnome_wm_win_app_state = XInternAtom(disp, "_WIN_APP_STATE", False);
313 m_gnome_wm_win_expanded_size = XInternAtom(disp, "_WIN_EXPANDED_SIZE", False);
314 m_gnome_wm_win_icons = XInternAtom(disp, "_WIN_ICONS", False);
315 m_gnome_wm_win_workspace = XInternAtom(disp, "_WIN_WORKSPACE", False);
316 m_gnome_wm_win_workspace_count = XInternAtom(disp, "_WIN_WORKSPACE_COUNT", False);
317 m_gnome_wm_win_workspace_names = XInternAtom(disp, "_WIN_WORKSPACE_NAMES", False);
318 m_gnome_wm_win_client_list = XInternAtom(disp, "_WIN_CLIENT_LIST", False);
319 m_gnome_wm_prot = XInternAtom(disp, "_WIN_PROTOCOLS", False);
320 m_gnome_wm_supporting_wm_check = XInternAtom(disp, "_WIN_SUPPORTING_WM_CHECK", False);
321}
diff --git a/src/Gnome.hh b/src/Gnome.hh
new file mode 100644
index 0000000..abdd8bf
--- /dev/null
+++ b/src/Gnome.hh
@@ -0,0 +1,96 @@
1// Gnome.hh for fluxbox
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@fluxbox.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Gnome.hh,v 1.1 2002/09/07 20:32:44 fluxgen Exp $
23
24#ifndef GNOME_HH
25#define GNOME_HH
26
27#include "AtomHandler.hh"
28
29#include <X11/Xatom.h>
30#include <vector>
31
32class Gnome:public AtomHandler {
33public:
34 enum GnomeLayer {
35 WIN_LAYER_DESKTOP = 0,
36 WIN_LAYER_BELOW = 2,
37 WIN_LAYER_NORMAL = 4,
38 WIN_LAYER_ONTOP = 6,
39 WIN_LAYER_DOCK = 8,
40 WIN_LAYER_ABOVE_DOCK = 10,
41 WIN_LAYER_MENU = 12
42 };
43
44 enum GnomeState {
45 WIN_STATE_STICKY = (1<<0), // everyone knows sticky
46 WIN_STATE_MINIMIZED = (1<<1), // Reserved - definition is unclear
47 WIN_STATE_MAXIMIZED_VERT = (1<<2), // window in maximized V state
48 WIN_STATE_MAXIMIZED_HORIZ = (1<<3), // window in maximized H state
49 WIN_STATE_HIDDEN = (1<<4), // not on taskbar but window visible
50 WIN_STATE_SHADED = (1<<5), // shaded (MacOS / Afterstep style)
51 WIN_STATE_HID_WORKSPACE = (1<<6), // not on current desktop
52 WIN_STATE_HID_TRANSIENT = (1<<7), // owner of transient is hidden
53 WIN_STATE_FIXED_POSITION = (1<<8), // window is fixed in position even
54 WIN_STATE_ARRANGE_IGNORE = (1<<9) // ignore for auto arranging
55 };
56
57 enum GnomeHints {
58 WIN_HINTS_SKIP_FOCUS = (1<<0), // skip this window
59 WIN_HINTS_SKIP_WINLIST = (1<<1), // do not show in window list
60 WIN_HINTS_SKIP_TASKBAR = (1<<2), // do not show on taskbar
61 WIN_HINTS_GROUP_TRANSIENT = (1<<3), // Reserved - definition is unclear
62 WIN_HINTS_FOCUS_ON_CLICK = (1<<4) // app only accepts focus if clicked
63 };
64
65 Gnome();
66 ~Gnome();
67 void initForScreen(const BScreen &screen);
68
69 void updateClientList(const BScreen &screen);
70 void updateWorkspaceNames(const BScreen &screen);
71 void updateCurrentWorkspace(const BScreen &screen);
72 void updateWorkspaceCount(const BScreen &screen);
73
74 void updateState(FluxboxWindow *win);
75 void updateHints(FluxboxWindow *win);
76 void updateWorkspace(FluxboxWindow *win);
77
78
79 bool checkClientMessage(const XClientMessageEvent &ce, BScreen *screen, FluxboxWindow *win);
80
81private:
82 void setLayer(GnomeLayer layer);
83 void setState(FluxboxWindow *win, int state);
84 void setLayer(int layer);
85 void createAtoms();
86 Atom m_gnome_wm_win_layer, m_gnome_wm_win_state, m_gnome_wm_win_hints,
87 m_gnome_wm_win_app_state, m_gnome_wm_win_expanded_size,
88 m_gnome_wm_win_icons, m_gnome_wm_win_workspace,
89 m_gnome_wm_win_workspace_count, m_gnome_wm_win_workspace_names,
90 m_gnome_wm_win_client_list;
91 Atom m_gnome_wm_prot;
92 Atom m_gnome_wm_supporting_wm_check;
93 std::vector<Window> m_gnomewindows;
94};
95
96#endif // GNOME_HH
diff --git a/src/Observer.cc b/src/Observer.cc
new file mode 100644
index 0000000..1a3d553
--- /dev/null
+++ b/src/Observer.cc
@@ -0,0 +1,33 @@
1// Observer.cc for FbTk
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@fluxbox.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Observer.cc,v 1.1 2002/09/07 20:30:45 fluxgen Exp $
23
24#include "Observer.hh"
25#include "Subject.hh"
26
27namespace FbTk {
28
29Observer::~Observer() {
30 Subject::removeObserver(this); // make sure no subject has this observer attached
31}
32
33};
diff --git a/src/Observer.hh b/src/Observer.hh
new file mode 100644
index 0000000..8a93e1d
--- /dev/null
+++ b/src/Observer.hh
@@ -0,0 +1,39 @@
1// Observer.hh for FbTk
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@fluxbox.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Observer.hh,v 1.1 2002/09/07 20:30:45 fluxgen Exp $
23
24#ifndef FBTK_OBSERVER_HH
25#define FBTK_OBSERVER_HH
26
27namespace FbTk {
28
29class Subject;
30
31class Observer {
32public:
33 virtual ~Observer();
34 virtual void update(Subject *changedSubj) = 0;
35};
36
37}; // end namespace FBTK
38
39#endif // FBTK_OBSERVER_HH
diff --git a/src/Subject.cc b/src/Subject.cc
new file mode 100644
index 0000000..3e9618b
--- /dev/null
+++ b/src/Subject.cc
@@ -0,0 +1,80 @@
1// Subject.cc for FbTk
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@fluxbox.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Subject.cc,v 1.1 2002/09/07 20:30:45 fluxgen Exp $
23
24#include "Subject.hh"
25#include "Observer.hh"
26
27#include <algorithm>
28#include <functional>
29
30namespace FbTk {
31
32Subject::SubjectList Subject::s_subjectlist;
33
34Subject::Subject() {
35 s_subjectlist.push_back(this);
36}
37
38Subject::~Subject() {
39 SubjectList::iterator it = s_subjectlist.begin();
40 SubjectList::iterator it_end = s_subjectlist.end();
41 for (; it != it_end; ++it) {
42 if (this == (*it)) {
43 s_subjectlist.erase(it);
44 break;
45 }
46 }
47}
48
49void Subject::attach(Observer *obj) {
50 m_observerlist.push_back(obj);
51 // no need to have more than one instance of an observer
52 std::unique(m_observerlist.begin(), m_observerlist.end());
53}
54
55void Subject::detach(Observer *obj) {
56 ObserverList::iterator it = m_observerlist.begin();
57 ObserverList::iterator it_end = m_observerlist.end();
58 for (; it != it_end; ++it) {
59 if (obj == (*it)) {
60 m_observerlist.erase(it);
61 break;
62 }
63 }
64}
65
66void Subject::notify() {
67 ObserverList::iterator it = m_observerlist.begin();
68 for (; it != m_observerlist.end(); ++it) {
69 (*it)->update(this);
70 }
71}
72
73void Subject::removeObserver(Observer *obj) {
74 SubjectList::iterator it = s_subjectlist.begin();
75 for(; it != s_subjectlist.end(); ++it) {
76 (*it)->detach(obj);
77 }
78}
79
80}; // end namespace FbTk
diff --git a/src/Subject.hh b/src/Subject.hh
new file mode 100644
index 0000000..95c64f2
--- /dev/null
+++ b/src/Subject.hh
@@ -0,0 +1,54 @@
1// Subject.hh for FbTk
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@fluxbox.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Subject.hh,v 1.1 2002/09/07 20:30:45 fluxgen Exp $
23
24#ifndef FBTK_SUBJECT_HH
25#define FBTK_SUBJECT_HH
26
27#include <vector>
28
29namespace FbTk {
30
31class Observer;
32
33class Subject {
34public:
35 Subject();
36 virtual ~Subject();
37 /// attach an observer
38 void attach(Observer *obs);
39 /// detach an observer
40 void detach(Observer *obs);
41 /// notify all attached observers
42 void notify();
43 static void removeObserver(Observer *obs);
44private:
45 typedef std::vector<Observer *> ObserverList;
46 ObserverList m_observerlist;
47
48 typedef std::vector<Subject *> SubjectList;
49 static SubjectList s_subjectlist;
50};
51
52}; // end namespace FbTk
53
54#endif // FBTK_SUBJECT_HH