Adonthell  0.4
win_manager.cc
1 /*
2  $Id: win_manager.cc,v 1.12 2004/10/25 06:55:01 ksterker Exp $
3 
4  (C) Copyright 2000/2001 Joel Vennin
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details
13 */
14 
15 /**
16  * @file win_theme.cc
17  *
18  * @author Joel Vennin
19  * @brief Implements the win_theme class.
20  */
21 
22 #include "types.h"
23 #include "image.h"
24 #include "win_types.h"
25 #include "win_manager.h"
26 
27 
28 // Pointer to the active window(s)
30 // List of loaded themes
31 hash_map<string, win_theme *> win_manager::theme;
32 // List of loaded fonts
33 hash_map<string, win_ttf *> win_manager::font;
34 // True type font to use
35 string win_manager::font_file;
36 
37 
39 {
40  // save a pointer to the parent window(s)
41  prev = active;
42 
43  // make the current window(s) active
44  active = this;
45 
46  // no window in focus at that point
47  wnd_focus = NULL;
48 
49  // we're not iterating over the window_list
50  current = wnd_list.end ();
51 }
52 
54 {
55  destroy ();
56 
57  // restore parent window(s)
58  active = prev;
59 }
60 
61 // Close and delete all windows
63 {
64  list<win_base *>::iterator i;
65 
66  for (i = wnd_list.begin(); i != wnd_list.end(); i++)
67  {
68  (*i)->set_manager (NULL);
69  // delete *i;
70  }
71 
72  wnd_list.clear ();
73  wnd_focus = NULL;
74 }
75 
76 void win_manager::init (const string & font)
77 {
78  font_file = font;
79  TTF_Init ();
80 }
81 
82 // Delete all fonts and themes
84 {
85  // Cleaning up themes
86  for (hash_map <string, win_theme *>::iterator it = theme.begin ();
87  it != theme.end (); it++)
88  delete it->second;
89  theme.clear ();
90 
91  // Cleaning up fonts
92  for (hash_map <string, win_ttf *>::iterator ifo = font.begin ();
93  ifo != font.end (); ifo++)
94  delete ifo->second;
95  font.clear ();
96 
97  TTF_Quit ();
98 }
99 
100 // add a window
102 {
103  wnd_list.push_back (tmp);
104  tmp->set_manager (this);
105 }
106 
107 /*
108 bool win_manager::exist(win_base *tmp)
109 {
110  for (list<win_base *>::iterator i = wnd_list.begin ();
111  i != wnd_list.end(); i++)
112  if (*i == tmp) return true;
113 
114  return false;
115 }
116 */
117 
118 // remove a window
120 {
121  // if the window has focus take it away
122  if (tmp->is_focus ())
123  {
124  tmp->set_focus (false);
125  wnd_focus = NULL;
126  }
127 
128  // be careful if the iterator points to the element
129  // we want to remove. This may happen if remove() is called
130  // from a window's update() method or from win_manager::update().
131  if (tmp == *current)
132  {
133  // make sure that the iterator remains valid
134  current++;
135  }
136 
137  // remove it from the window list
138  wnd_list.remove (tmp);
139  tmp->set_manager (NULL);
140 
141  // if no window has the focus, give it to the topmost window
142  if (!wnd_focus) set_focus (wnd_list.back ());
143 }
144 
145 // draw all windows
147 {
148  // first descent recursively down the list of parents
149  if (prev != NULL) prev->draw ();
150 
151  // on the way up, draw every window
152  for (current = wnd_list.begin (); current != wnd_list.end(); current++)
153  (*current)->draw ();
154 }
155 
156 // grab keyboard input
158 {
159  // only the window with the focus may recieve input
160  if (wnd_focus) wnd_focus->input_update ();
161 }
162 
163 // update the state of the topmost window(s)
165 {
166  for (current = wnd_list.begin (); current != wnd_list.end ();)
167  // a window signals that it wants to be closed by returning 0 here
168  if (!(*current)->update ())
169  {
170  // remove and delete it
171  win_base *tmp = *current;
172 
173  remove (tmp);
174  delete tmp;
175  }
176  else current++;
177 }
178 
179 // give the focus to a window
181 {
182  // but only if there are any windows at all
183  if (!wnd_list.empty ())
184  {
185  // remove focus from the old window
186  if (wnd_focus) wnd_focus->set_focus (false);
187 
188  // and give it to the new one
189  wnd_focus = tmp;
190  wnd_focus->set_focus (true);
191  }
192 }
193 
194 // load a theme from disk
195 void win_manager::add_theme (string name)
196 {
197  theme[name] = new win_theme ((char *) name.c_str ());
198 }
199 
200 // remove a theme
201 bool win_manager::remove_theme (string name)
202 {
203  hash_map <string, win_theme *>::iterator it = theme.find (name);
204  if (it == theme.end ()) return false;
205 
206  delete it->second;
207  theme.erase (it);
208  return true;
209 }
210 
211 // return a pointer to a theme
213 {
214  hash_map <string, win_theme *>::iterator it = theme.find (name);
215 
216  // try to load it if it's not in memory yet
217  if (it == theme.end ())
218  {
219  add_theme (name);
220  return get_theme (name);
221  }
222  else return it->second;
223 }
224 
225 // load a font from disk
226 void win_manager::add_font (string name)
227 {
228  font[name] = new win_ttf ((char *) name.c_str (), font_file);
229 }
230 
231 // remove a font
232 bool win_manager::remove_font (string name)
233 {
234  hash_map <string, win_ttf *>::iterator it = font.find (name);
235  if (it == font.end ()) return false;
236 
237  delete it->second;
238  font.erase (it);
239  return true;
240 }
241 
242 // return a pointer to a font
244 {
245  hash_map <string, win_ttf *>::iterator it = font.find (name);
246 
247  // try to load the font if it's not in memory yet
248  if (it == font.end ())
249  {
250  add_font (name);
251  return get_font (name);
252  }
253  else return it->second;
254 }
static win_font * get_font(string name)
Returns a pointer to a font.
Definition: win_manager.cc:243
Declares some basic types.
void update()
Update the state of all top level windows.
Definition: win_manager.cc:164
static win_manager * active
Pointer to the active, i.e.
Definition: win_manager.h:143
static win_theme * get_theme(string name)
Returns a pointer to a theme.
Definition: win_manager.cc:212
win_manager()
Standard constructor.
Definition: win_manager.cc:38
static void add_theme(string name)
Load a theme from disk.
Definition: win_manager.cc:195
bool is_focus() const
Test if win_* has focus on.
Definition: win_base.h:159
static void init(const string &font)
Empty for now.
Definition: win_manager.cc:76
static bool remove_theme(string name)
Delete a theme.
Definition: win_manager.cc:201
Declares the image class.
The window manager takes care of basic GUI functions, such as input focus, window state updates and d...
Definition: win_manager.h:61
virtual bool input_update()
Input Update process .
Definition: win_base.cc:102
void remove(win_base *wnd)
Remove a window from the window manager.
Definition: win_manager.cc:119
void set_focus(win_base *wnd)
Gives the input focus to wnd.
Definition: win_manager.cc:180
void set_focus(const bool b)
Set the focus parameter.
Definition: win_base.h:166
void draw()
Draws all windows.
Definition: win_manager.cc:146
static void cleanup()
Delete all themes and fonts currently loaded.
Definition: win_manager.cc:83
~win_manager()
Destructor.
Definition: win_manager.cc:53
static bool remove_font(string name)
Delete a font.
Definition: win_manager.cc:232
Declares the win_manager class.
void add(win_base *wnd)
Add a window to the window manager.
Definition: win_manager.cc:101
static void add_font(string name)
Load a font from disk.
Definition: win_manager.cc:226
Common properties for each win_base&#39;s object.
Definition: win_base.h:47
void input_update()
Checks for user input.
Definition: win_manager.cc:157
void destroy()
Closes and deletes all windows of the current level.
Definition: win_manager.cc:62