4 // Graphical User Interface support
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 02/03/2006 Created this file
12 // JLH 03/13/2006 Added functions to allow shutting down GUI externally
13 // JLH 03/22/2006 Finalized basic multiple window support
17 // - Memory leak on quitting with a window active [DONE]
18 // - Multiple window handling [DONE]
22 #include "menu.h" // Element class methods are pulled in here...
30 //#define DEBUG_MAIN_LOOP
32 // New main screen buffering
33 // This works, but the colors are rendered incorrectly. Also, it seems that there's
34 // fullscreen blitting still going on--dragging the disk is fast at first but then
35 // gets painfully slow. Not sure what's going on there.
36 #define USE_NEW_MAINBUFFERING
38 //#ifdef DEBUG_MAIN_LOOP
43 Work flow: Draw floppy drive.
44 If disk in drive, MO shows eject graphic, otherwise show load graphic.
45 If hit 'new blank image':
46 If disk in drive, ask if want to save if modified
52 GUI::GUI(SDL_Surface * surface): menuItem(new MenuItems())
54 Element::SetScreen(surface);
55 // windowList.push_back(new Menu());
57 // Create drive windows, and config windows here...
58 windowList.push_back(new Window(30, 30, 200, 100));
59 windowList.push_back(new Window(30, 140, 200, 100));
60 windowList.push_back(new Button(30, 250, "Click!"));
61 windowList.push_back(new Text(30, 20, floppyDrive.GetImageName(0)));
62 windowList.push_back(new Text(30, 130, floppyDrive.GetImageName(1)));
67 // Clean up menuItem, if any
74 for(std::list<Element *>::iterator i=windowList.begin(); i!=windowList.end(); i++)
79 void GUI::AddMenuTitle(const char * title)
81 menuItem->title = title;
82 menuItem->item.clear();
85 void GUI::AddMenuItem(const char * item, Element * (* a)(void)/*= NULL*/, SDLKey k/*= SDLK_UNKNOWN*/)
87 menuItem->item.push_back(NameAction(item, a, k));
90 void GUI::CommitItemsToMenu(void)
92 //We could just do a simple check here to see if more than one item is in the list,
93 //and if so fail. Make it so you build the menu first before allowing any other action. [DONE]
95 //Right now, we just silently fail...
96 if (windowList.size() > 1)
98 WriteLog("GUI: Can't find menu--more than one item in windowList!\n");
102 ((Menu *)(*windowList.begin()))->Add(*menuItem);
110 std::list<Element *>::iterator i;
112 SDL_EnableKeyRepeat(150, 75);
114 // Initial update... [Now handled correctly in the constructor]
115 // Uh, still needed here, though... Only makes sense that it should
116 for(i=windowList.begin(); i!=windowList.end(); i++)
119 #ifndef USE_NEW_MAINBUFFERING
120 RenderScreenBuffer();
128 // if (SDL_PollEvent(&event))
129 if (SDL_WaitEvent(&event))
131 #ifdef DEBUG_MAIN_LOOP
132 WriteLog("An event was found!");
134 if (event.type == SDL_USEREVENT)
136 #ifdef DEBUG_MAIN_LOOP
137 WriteLog(" -- SDL_USEREVENT\n");
139 //Mebbe add another user event for screen refresh? Why not!
140 if (event.user.code == WINDOW_CLOSE)
142 for(i=windowList.begin(); i!=windowList.end(); i++)
144 if (*i == (Element *)event.user.data1)
152 else if (event.user.code == MENU_ITEM_CHOSEN)
154 // Confused? Let me enlighten... What we're doing here is casting
155 // data1 as a pointer to a function which returns a Element pointer and
156 // which takes no parameters (the "(Element *(*)(void))" part), then
157 // derefencing it (the "*" in front of that) in order to call the
158 // function that it points to. Clear as mud? Yeah, I hate function
159 // pointers too, but what else are you gonna do?
160 Element * window = (*(Element *(*)(void))event.user.data1)();
163 windowList.push_back(window);
165 while (SDL_PollEvent(&event)); // Flush the event queue...
167 event.type = SDL_MOUSEMOTION;
169 SDL_GetMouseState(&mx, &my);
170 event.motion.x = mx, event.motion.y = my;
171 SDL_PushEvent(&event); // & update mouse position...!
173 oldMouse.x = mouse.x, oldMouse.y = mouse.y;
174 mouse.x = mx, mouse.y = my; // This prevents "mouse flash"...
176 //There's a *small* problem with the following approach--if a window and a bunch of
177 //child widgets send this message, we'll get a bunch of unnecessary refresh events...
178 //This could be controlled by having the main window refresh itself intelligently...
180 //What we could do instead is set a variable in Element and check it after the fact
181 //to see whether or not a refresh is needed.
182 //[This is what we do now.]
184 //Dirty rectangle is also possible...
185 else if (event.user.code == SCREEN_REFRESH_NEEDED)
186 #ifndef USE_NEW_MAINBUFFERING
187 RenderScreenBuffer();
192 else if (event.type == SDL_ACTIVEEVENT)
194 //Need to do a screen refresh here...
195 if (event.active.state == SDL_APPMOUSEFOCUS)
196 showMouse = (event.active.gain ? true : false);
198 #ifndef USE_NEW_MAINBUFFERING
199 RenderScreenBuffer();
204 else if (event.type == SDL_KEYDOWN)
206 #ifdef DEBUG_MAIN_LOOP
207 WriteLog(" -- SDL_KEYDOWN\n");
209 if (event.key.keysym.sym == SDLK_F1)
212 //Not sure that this is the right way to handle this...
213 //Probably should only give this to the top level window...
214 // for(i=windowList.begin(); i!=windowList.end(); i++)
215 // (*i)->HandleKey(event.key.keysym.sym);
216 windowList.back()->HandleKey(event.key.keysym.sym);
218 else if (event.type == SDL_MOUSEMOTION)
220 #ifdef DEBUG_MAIN_LOOP
221 WriteLog(" -- SDL_MOUSEMOTION\n");
223 //This is for tracking a custom mouse cursor, which we're not doing--YET.
224 oldMouse.x = mouse.x, oldMouse.y = mouse.y;
225 mouse.x = event.motion.x, mouse.y = event.motion.y;
227 //Not sure that this is the right way to handle this...
228 //Right now, we should probably only do mouseover for the last item in the list...
230 //Though, it seems to screw other things up. Maybe it IS better to pass it to all windows?
231 //Or maybe to just the ones that aren't completely obscured?
232 //Probably. Right now, a disk's close button that should be obscured by one sitting on
233 //top of it gets redrawn. Not good. !!! FIX !!!
234 for(i=windowList.begin(); i!=windowList.end(); i++)
235 (*i)->HandleMouseMove(mouse.x, mouse.y);
236 // windowList.back()->HandleMouseMove(mouse.x, mouse.y);
238 else if (event.type == SDL_MOUSEBUTTONDOWN)
240 #ifdef DEBUG_MAIN_LOOP
241 WriteLog(" -- SDL_MOUSEBUTTONDOWN\n");
243 //Not sure that this is the right way to handle this...
244 // What we should do here is ensure that whatever has been clicked on gets moved to the
245 // highest priority--in our current data schema that would be the end of the list... !!! FIX !!!
250 We could do the following:
252 - Go through list and find which window has been clicked on (if any). If more
253 than one is clicked on, take the one highest in the Z order (closer to the end
256 - If item is highest in Z order, pass click through to window and exit.
258 - Otherwise, restore backing store on each window in reverse order.
260 - Remove item clicked on from the list. Put removed item at the end of the list.
262 - Go through list and pass click through to each window in the list. Also do a
263 blit to backing store and a Draw() for each window.
265 Could also do a check (if not clicked on highest Z window) to see which windows
266 it overlaps and just do restore/redraw for those that overlap. To wit:
268 - Create new list containing only those windows that overlap the clicking on window.
270 - Go through list and do a blit to backing store and a Draw() for each window.
272 - Go through list and pass click through to each window in the list.
278 for(i=windowList.begin(); i!=windowList.end(); i++)
279 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
281 // We use the 1st algorithm here, since it's simpler. If we need to, we can optimize
284 // Walk backward through the list and see if a window was hit.
285 // This will automagically return us the window with the highest Z.
287 std::list<Element *>::reverse_iterator ri;
288 std::list<Element *>::iterator hit;// = windowList.end();
290 for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
292 if ((*ri)->Inside(event.button.x, event.button.y))
294 // Here's a bit of STL weirdness: Converting from a reverse
295 // iterator to a regular iterator requires backing the iterator
296 // up a position after grabbing it's base() OR going forward
297 // one position with the reverse iterator before grabbing base().
298 // Ugly, but it gets the job done...
300 // Put it back where we found it, so the tests following this
307 // If we hit the highest in the list, then pass the event through
308 // to the window for handling. if we hit no windows, then pass the
309 // event to all windows. Otherwise, we need to shuffle windows.
311 //NOTE: We need to pass the click to all windows regardless of whether they're topmost or not...
312 if (ri == windowList.rbegin())
314 for(i=windowList.begin(); i!=windowList.end(); i++)
315 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
317 else if (ri == windowList.rend())
319 for(i=windowList.begin(); i!=windowList.end(); i++)
320 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
324 // - Otherwise, restore backing store on each window in reverse order.
325 for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
326 (*ri)->RestoreScreenFromBackstore();
327 // At this point, the screen has been restored...
329 // - Remove item clicked on from the list. Put removed item at the end of the list.
330 windowList.push_back(*hit);
331 windowList.erase(hit);
332 // - Go through list and pass click through to each window in the list. Also do a
333 // blit to backing store and a Draw() for each window.
334 for(i=windowList.begin(); i!= windowList.end(); i++)
336 // Grab bg into backstore
337 (*i)->SaveScreenToBackstore();
339 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
347 A slightly different way to handle this would be to loop through all windows, compare
348 all those above it to see if they obscure it; if so then subdivide it's update rectangle
349 to eliminate drawing the parts that aren't shown. The beauty of this approach is that
350 you don't have to care what order the windows are drawn in and you don't need to worry
351 about the order of restoring the backing store.
353 You *do* still need to determine the Z-order of the windows, in order to get the subdivisions
354 correct, but that's not too terrible.
356 Also, when doing a window drag, the coverage lists for all windows have to be regenerated.
358 std::list<Element *>::reverse_iterator ri;
359 bool movedWindow = false;
361 for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
363 if ((*ri)->Inside(event.button.x, event.button.y))
365 // Remove item clicked on from the list & put removed item at the
366 // end of the list, thus putting the window at the top of the Z
367 // order. But IFF window is not already topmost!
368 if (ri != windowList.rbegin())
370 windowList.push_back(*ri);
371 // Here's a bit of STL weirdness: Converting from a reverse
372 // iterator to a regular iterator requires backing the iterator
373 // up a position after grabbing it's base() OR going forward
374 // one position with the reverse iterator before grabbing base().
375 // Ugly, but it get the job done...
376 windowList.erase((++ri).base());
384 //Small problem here: we should only pass the *hit* to the topmost window and pass
385 //*misses* to everyone else... Otherwise, you can have overlapping draggable windows
386 //and be able to drag both by clicking on a point that intersects both...
387 //(though that may be an interesting way to handle things!)
388 //The thing is that you want to do it on purpose (like with a special grouping widget)
389 //instead of by accident. So, !!! FIX !!!
390 // Pass the click on to all windows
391 // for(i=windowList.begin(); i!=windowList.end(); i++)
392 // (*i)->HandleMouseButton(event.button.x, event.button.y, true);
393 windowList.back()->HandleMouseButton(event.button.x, event.button.y, true);
395 // // & bail if nothing changed...
399 // Check for overlap/build coverage lists [O((n^2)/2) algorithm!]
400 //One way to optimize this would be to only reset coverage lists from the point in
401 //the Z order where the previous window was.
402 for(i=windowList.begin(); i!=windowList.end(); i++)
404 //One other little quirk: Probably need to clear the backing store as well!
406 (*i)->ResetCoverageList();
408 // This looks odd, but it's just a consequence of iterator weirdness.
409 // Otherwise we could just stick a j+1 in the for loop below. :-P
410 std::list<Element *>::iterator j = i;
413 for(; j!=windowList.end(); j++)
414 (*i)->AdjustCoverageList((*j)->GetExtents());
416 // (*i)->HandleMouseButton(event.button.x, event.button.y, true);
421 else if (event.type == SDL_MOUSEBUTTONUP)
423 #ifdef DEBUG_MAIN_LOOP
424 WriteLog(" -- SDL_MOUSEBUTTONUP\n");
426 //Not sure that this is the right way to handle this...
427 for(i=windowList.begin(); i!=windowList.end(); i++)
428 (*i)->HandleMouseButton(event.button.x, event.button.y, false);
429 //I think we should only do topmost here...
431 // windowList.back()->HandleMouseButton(event.button.x, event.button.y, false);
433 #ifdef DEBUG_MAIN_LOOP
435 WriteLog(" -- Unknown event\n");
438 if (Element::ScreenNeedsRefreshing())
440 #ifndef USE_NEW_MAINBUFFERING
441 #ifdef DEBUG_MAIN_LOOP
442 WriteLog("Screen refresh called!\n");
444 RenderScreenBuffer();
445 Element::ScreenWasRefreshed();
448 Element::ScreenWasRefreshed();
452 //hm. Works, but slows things way down.
453 //Now we use WaitEvents() instead. Yay!
457 SDL_EnableKeyRepeat(0, 0);