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
22 //#define DESTRUCTOR_TESTING
24 // Rendering experiment...
26 //#define USE_COVERAGE_LISTS
28 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
29 #define MASK_R 0xFF000000
30 #define MASK_G 0x00FF0000
31 #define MASK_B 0x0000FF00
32 #define MASK_A 0x000000FF
34 #define MASK_R 0x000000FF
35 #define MASK_G 0x0000FF00
36 #define MASK_B 0x00FF0000
37 #define MASK_A 0xFF000000
41 // Window class implementation
43 // NOTE: FG/BG colors are hard-wired
46 Window::Window(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
47 void (* f)(Element *)/*= NULL*/):
48 Element(x, y, w, h, 0x4D, 0xFF, 0x84, 0xFF, 0x1F, 0x84, 0x84, 0xFF), handler(f),
49 cbWidth((closeBox[0] << 8) | closeBox[1]), cbHeight((closeBox[2] << 8) | closeBox[3]),
50 cbUp(SDL_CreateRGBSurfaceFrom(&closeBox[4], cbWidth, cbHeight, 32, cbWidth * 4,
51 MASK_R, MASK_G, MASK_B, MASK_A)),
52 cbDown(SDL_CreateRGBSurfaceFrom(&closeBoxDown[4], cbWidth, cbHeight, 32, cbWidth * 4,
53 MASK_R, MASK_G, MASK_B, MASK_A)),
54 cbHover(SDL_CreateRGBSurfaceFrom(&closeBoxHover[4], cbWidth, cbHeight, 32, cbWidth * 4,
55 MASK_R, MASK_G, MASK_B, MASK_A))
57 //Could probably move this into the initializer list as well...
58 // closeButton = new Button(w - (cbWidth + 1), 1, cbUp, cbHover, cbDown, this);
59 // list.push_back(closeButton);
62 Draw(); // Can we do this in the constructor??? Mebbe.
67 #ifdef DESTRUCTOR_TESTING
68 printf("Inside ~Window()...\n");
70 for(uint32 i=0; i<list.size(); i++)
74 SDL_FreeSurface(cbUp);
75 SDL_FreeSurface(cbDown);
76 SDL_FreeSurface(cbHover);
79 void Window::HandleKey(SDLKey key)
81 if (key == SDLK_ESCAPE)
84 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
85 SDL_PushEvent(&event);
88 // Handle the items this window contains...
89 for(uint32 i=0; i<list.size(); i++)
90 list[i]->HandleKey(key);
93 void Window::HandleMouseMove(uint32 x, uint32 y)
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]->HandleMouseMove(x - extents.x, y - extents.y);
101 void Window::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
104 // Handle the items this window contains...
105 for(uint32 i=0; i<list.size(); i++)
106 // Make coords relative to upper right corner of this window...
107 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
108 #else //? This works in draggablewindow2...
109 // Handle the items this window contains...
110 for(uint32 i=0; i<list.size(); i++)
112 // Make coords relative to upper right corner of this window...
113 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
115 if (list[i]->Inside(x - extents.x, y - extents.y))
121 void Window::Draw(void)
123 #ifdef USE_COVERAGE_LISTS
124 // These are *always* top level and parentless, so no need to traverse up through
125 // the parent chain...
126 for(std::list<SDL_Rect>::iterator i=coverList.begin(); i!=coverList.end(); i++)
127 SDL_FillRect(screen, &(*i), bgColor);
129 // Handle the items this window contains...
130 for(uint32 i=0; i<list.size(); i++)
133 // These are *always* top level and parentless, so no need to traverse up through
134 // the parent chain...
135 SDL_FillRect(screen, &extents, bgColor);
137 // Handle the items this window contains...
138 for(uint32 i=0; i<list.size(); i++)
142 //Prolly don't need this since the close button will do this for us...
143 //Close button isn't mandatory anymore...
144 needToRefreshScreen = true;
147 // This is only called if a close button has been added
148 void Window::Notify(Element * e)
150 if (e == closeButton)
153 event.type = SDL_USEREVENT;
154 event.user.code = WINDOW_CLOSE;
155 event.user.data1 = (void *)this;
156 SDL_PushEvent(&event);
160 void Window::AddElement(Element * e)
165 void Window::AddCloseButton(void)
167 // Only allow this to happen once!
168 if (closeButton == NULL)
170 closeButton = new Button(extents.w - (cbWidth + 1), 1, cbUp, cbHover, cbDown, this);
171 list.push_back(closeButton);