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