4 // Graphical User Interface window class
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 02/03/2006 Created this file
12 // JLH 02/09/2006 Fixed various problems with the class implementation
13 // JLH 02/14/2006 Added window rendering
18 #include "guimisc.h" // Various support functions
21 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
22 #define MASK_R 0xFF000000
23 #define MASK_G 0x00FF0000
24 #define MASK_B 0x0000FF00
25 #define MASK_A 0x000000FF
27 #define MASK_R 0x000000FF
28 #define MASK_G 0x0000FF00
29 #define MASK_B 0x00FF0000
30 #define MASK_A 0xFF000000
33 using namespace std; // For STL stuff
36 // Window class implementation
38 // NOTE: FG/BG colors are hard-wired
41 Window::Window(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
42 void (* f)(Element *)/*= NULL*/):
43 Element(x, y, w, h, 0x4D, 0xFF, 0x84, 0xFF, 0x1F, 0x84, 0x84, 0xFF), handler(f),
44 cbWidth((closeBox[0] << 8) | closeBox[1]), cbHeight((closeBox[2] << 8) | closeBox[3]),
45 cbUp(SDL_CreateRGBSurfaceFrom(&closeBox[4], cbWidth, cbHeight, 32, cbWidth * 4,
46 MASK_R, MASK_G, MASK_B, MASK_A)),
47 cbDown(SDL_CreateRGBSurfaceFrom(&closeBoxDown[4], cbWidth, cbHeight, 32, cbWidth * 4,
48 MASK_R, MASK_G, MASK_B, MASK_A)),
49 cbHover(SDL_CreateRGBSurfaceFrom(&closeBoxHover[4], cbWidth, cbHeight, 32, cbWidth * 4,
50 MASK_R, MASK_G, MASK_B, MASK_A))
52 //Could probably move this into the initializer list as well...
53 closeButton = new Button(w - (cbWidth + 1), 1, cbUp, cbHover, cbDown, this);
54 list.push_back(closeButton);
57 Draw(); // Can we do this in the constructor??? Mebbe.
62 for(uint32 i=0; i<list.size(); i++)
66 SDL_FreeSurface(cbUp);
67 SDL_FreeSurface(cbDown);
68 SDL_FreeSurface(cbHover);
71 void Window::HandleKey(SDLKey key)
73 if (key == SDLK_ESCAPE)
76 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
77 SDL_PushEvent(&event);
80 // Handle the items this window contains...
81 for(uint32 i=0; i<list.size(); i++)
82 list[i]->HandleKey(key);
85 void Window::HandleMouseMove(uint32 x, uint32 y)
87 // Handle the items this window contains...
88 for(uint32 i=0; i<list.size(); i++)
89 // Make coords relative to upper right corner of this window...
90 list[i]->HandleMouseMove(x - extents.x, y - extents.y);
93 void Window::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
95 // Handle the items this window contains...
96 for(uint32 i=0; i<list.size(); i++)
97 // Make coords relative to upper right corner of this window...
98 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
101 void Window::Draw(void)
103 // These are *always* top level and parentless, so no need to traverse up through
104 // the parent chain...
105 SDL_FillRect(screen, &extents, bgColor);
107 // Handle the items this window contains...
108 for(uint32 i=0; i<list.size(); i++)
111 //Prolly don't need this since the close button will do this for us...
112 needToRefreshScreen = true;
115 void Window::Notify(Element * e)
117 if (e == closeButton)
120 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
121 SDL_PushEvent(&event);
125 void Window::AddElement(Element * e)