]> Shamusworld >> Repos - apple2/blob - src/gui/gui.cpp
7c6e219782d001fbd2c7c7680964c0abb341b267
[apple2] / src / gui / gui.cpp
1 //
2 // GUI.CPP
3 //
4 // Graphical User Interface support
5 // by James L. Hammons
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
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
14 //
15 // STILL TO DO:
16 //
17 // - Memory leak on quitting with a window active [DONE]
18 // - Multiple window handling [DONE]
19 //
20
21 #include "gui.h"
22 #include "menu.h"                                                               // Element class methods are pulled in here...
23 #include "window.h"
24 #include "button.h"
25 #include "text.h"
26 #include "diskwindow.h"
27 #include "video.h"
28 #include "apple2.h"
29
30 // Debug support
31 //#define DEBUG_MAIN_LOOP
32
33 // New main screen buffering
34 // This works, but the colors are rendered incorrectly. Also, it seems that there's
35 // fullscreen blitting still going on--dragging the disk is fast at first but then
36 // gets painfully slow. Not sure what's going on there.
37 #define USE_NEW_MAINBUFFERING
38
39 //#ifdef DEBUG_MAIN_LOOP
40 #include "log.h"
41 //#endif
42
43 /*
44 Work flow: Draw floppy drive.
45 If disk in drive, MO shows eject graphic, otherwise show load graphic.
46 If hit 'new blank image':
47         If disk in drive, ask if want to save if modified
48         else, load it
49 If hit 'swap disks', swap disks.
50 */
51
52
53 GUI::GUI(SDL_Surface * surface): menuItem(new MenuItems())
54 {
55         Element::SetScreen(surface);
56 //      windowList.push_back(new Menu());
57
58 // Create drive windows, and config windows here...
59         windowList.push_back(new Window(30, 30, 200, 100));
60         windowList.push_back(new Window(30, 140, 200, 100));
61         windowList.push_back(new Button(30, 250, "Click!"));
62         windowList.push_back(new Text(30, 20, floppyDrive.GetImageName(0)));
63         windowList.push_back(new Text(30, 130, floppyDrive.GetImageName(1)));
64         windowList.push_back(new DiskWindow(&floppyDrive, 240, 20));
65 }
66
67 GUI::~GUI()
68 {
69         // Clean up menuItem, if any
70
71         if (menuItem)
72                 delete menuItem;
73
74         // Clean up the rest
75
76         for(std::list<Element *>::iterator i=windowList.begin(); i!=windowList.end(); i++)
77                 if (*i)
78                         delete *i;
79 }
80
81 void GUI::AddMenuTitle(const char * title)
82 {
83         menuItem->title = title;
84         menuItem->item.clear();
85 }
86
87 void GUI::AddMenuItem(const char * item, Element * (* a)(void)/*= NULL*/, SDLKey k/*= SDLK_UNKNOWN*/)
88 {
89         menuItem->item.push_back(NameAction(item, a, k));
90 }
91
92 void GUI::CommitItemsToMenu(void)
93 {
94 //We could just do a simple check here to see if more than one item is in the list,
95 //and if so fail. Make it so you build the menu first before allowing any other action. [DONE]
96
97 //Right now, we just silently fail...
98         if (windowList.size() > 1)
99         {
100                 WriteLog("GUI: Can't find menu--more than one item in windowList!\n");
101                 return;
102         }
103
104         ((Menu *)(*windowList.begin()))->Add(*menuItem);
105 }
106
107 void GUI::Run(void)
108 {
109         exitGUI = false;
110         showMouse = true;
111         SDL_Event event;
112         std::list<Element *>::iterator i;
113
114         SDL_EnableKeyRepeat(150, 75);
115
116         // Initial update... [Now handled correctly in the constructor]
117         // Uh, still needed here, though... Only makes sense that it should
118         for(i=windowList.begin(); i!=windowList.end(); i++)
119                 (*i)->Draw();
120
121 #ifndef USE_NEW_MAINBUFFERING
122         RenderScreenBuffer();
123 #else
124         FlipMainScreen();
125 #endif
126
127         // Main loop
128         while (!exitGUI)
129         {
130 //              if (SDL_PollEvent(&event))
131                 if (SDL_WaitEvent(&event))
132                 {
133 #ifdef DEBUG_MAIN_LOOP
134 WriteLog("An event was found!");
135 #endif
136                         if (event.type == SDL_USEREVENT)
137                         {
138 #ifdef DEBUG_MAIN_LOOP
139 WriteLog(" -- SDL_USEREVENT\n");
140 #endif
141 //Mebbe add another user event for screen refresh? Why not!
142                                 if (event.user.code == WINDOW_CLOSE)
143                                 {
144                                         for(i=windowList.begin(); i!=windowList.end(); i++)
145                                         {
146                                                 if (*i == (Element *)event.user.data1)
147                                                 {
148                                                         delete *i;
149                                                         windowList.erase(i);
150                                                         break;
151                                                 }
152                                         }
153                                 }
154                                 else if (event.user.code == MENU_ITEM_CHOSEN)
155                                 {
156                                         // Confused? Let me enlighten... What we're doing here is casting
157                                         // data1 as a pointer to a function which returns a Element pointer and
158                                         // which takes no parameters (the "(Element *(*)(void))" part), then
159                                         // derefencing it (the "*" in front of that) in order to call the
160                                         // function that it points to. Clear as mud? Yeah, I hate function
161                                         // pointers too, but what else are you gonna do?
162                                         Element * window = (*(Element *(*)(void))event.user.data1)();
163
164                                         if (window)
165                                                 windowList.push_back(window);
166
167                                         while (SDL_PollEvent(&event));  // Flush the event queue...
168
169                                         event.type = SDL_MOUSEMOTION;
170                                         int mx, my;
171                                         SDL_GetMouseState(&mx, &my);
172                                         event.motion.x = mx, event.motion.y = my;
173                                     SDL_PushEvent(&event);                      // & update mouse position...!
174
175                                         oldMouse.x = mouse.x, oldMouse.y = mouse.y;
176                                         mouse.x = mx, mouse.y = my;             // This prevents "mouse flash"...
177                                 }
178 //There's a *small* problem with the following approach--if a window and a bunch of
179 //child widgets send this message, we'll get a bunch of unnecessary refresh events...
180 //This could be controlled by having the main window refresh itself intelligently...
181
182 //What we could do instead is set a variable in Element and check it after the fact
183 //to see whether or not a refresh is needed.
184 //[This is what we do now.]
185
186 //Dirty rectangle is also possible...
187                                 else if (event.user.code == SCREEN_REFRESH_NEEDED)
188 #ifndef USE_NEW_MAINBUFFERING
189                                         RenderScreenBuffer();
190 #else
191                                         FlipMainScreen();
192 #endif
193                         }
194                         else if (event.type == SDL_ACTIVEEVENT)
195                         {
196 //Need to do a screen refresh here...
197                                 if (event.active.state == SDL_APPMOUSEFOCUS)
198                                         showMouse = (event.active.gain ? true : false);
199
200 #ifndef USE_NEW_MAINBUFFERING
201                                 RenderScreenBuffer();
202 #else
203                                         FlipMainScreen();
204 #endif
205                         }
206                         else if (event.type == SDL_KEYDOWN)
207                         {
208 #ifdef DEBUG_MAIN_LOOP
209 WriteLog(" -- SDL_KEYDOWN\n");
210 #endif
211                                 if (event.key.keysym.sym == SDLK_F1)
212                                         exitGUI = true;
213
214 //Not sure that this is the right way to handle this...
215 //Probably should only give this to the top level window...
216 //                              for(i=windowList.begin(); i!=windowList.end(); i++)
217 //                                      (*i)->HandleKey(event.key.keysym.sym);
218                                 windowList.back()->HandleKey(event.key.keysym.sym);
219                         }
220                         else if (event.type == SDL_MOUSEMOTION)
221                         {
222 #ifdef DEBUG_MAIN_LOOP
223 WriteLog(" -- SDL_MOUSEMOTION\n");
224 #endif
225 //This is for tracking a custom mouse cursor, which we're not doing--YET.
226                                 oldMouse.x = mouse.x, oldMouse.y = mouse.y;
227                                 mouse.x = event.motion.x, mouse.y = event.motion.y;
228
229 //Not sure that this is the right way to handle this...
230 //Right now, we should probably only do mouseover for the last item in the list...
231 //And now we do!
232 //Though, it seems to screw other things up. Maybe it IS better to pass it to all windows?
233 //Or maybe to just the ones that aren't completely obscured?
234 //Probably. Right now, a disk's close button that should be obscured by one sitting on
235 //top of it gets redrawn. Not good. !!! FIX !!!
236                                 for(i=windowList.begin(); i!=windowList.end(); i++)
237                                         (*i)->HandleMouseMove(mouse.x, mouse.y);
238 //                              windowList.back()->HandleMouseMove(mouse.x, mouse.y);
239                         }
240                         else if (event.type == SDL_MOUSEBUTTONDOWN)
241                         {
242 #ifdef DEBUG_MAIN_LOOP
243 WriteLog(" -- SDL_MOUSEBUTTONDOWN\n");
244 #endif
245 //Not sure that this is the right way to handle this...
246 // What we should do here is ensure that whatever has been clicked on gets moved to the
247 // highest priority--in our current data schema that would be the end of the list... !!! FIX !!!
248 //[DONE]
249
250 /*
251
252 We could do the following:
253
254 - Go through list and find which window has been clicked on (if any). If more
255   than one is clicked on, take the one highest in the Z order (closer to the end
256   of the list).
257
258 - If item is highest in Z order, pass click through to window and exit.
259
260 - Otherwise, restore backing store on each window in reverse order.
261
262 - Remove item clicked on from the list. Put removed item at the end of the list.
263
264 - Go through list and pass click through to each window in the list. Also do a
265   blit to backing store and a Draw() for each window.
266
267 Could also do a check (if not clicked on highest Z window) to see which windows
268 it overlaps and just do restore/redraw for those that overlap. To wit:
269
270 - Create new list containing only those windows that overlap the clicking on window.
271
272 - Go through list and do a blit to backing store and a Draw() for each window.
273
274 - Go through list and pass click through to each window in the list.
275
276 */
277
278 #if 0
279 #if 0
280                                 for(i=windowList.begin(); i!=windowList.end(); i++)
281                                         (*i)->HandleMouseButton(event.button.x, event.button.y, true);
282 #else
283 // We use the 1st algorithm here, since it's simpler. If we need to, we can optimize
284 // to the 2nd...
285
286                                 // Walk backward through the list and see if a window was hit.
287                                 // This will automagically return us the window with the highest Z.
288
289                                 std::list<Element *>::reverse_iterator ri;
290                                 std::list<Element *>::iterator hit;// = windowList.end();
291
292                                 for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
293                                 {
294                                         if ((*ri)->Inside(event.button.x, event.button.y))
295                                         {
296                                                 // Here's a bit of STL weirdness: Converting from a reverse
297                                                 // iterator to a regular iterator requires backing the iterator
298                                                 // up a position after grabbing it's base() OR going forward
299                                                 // one position with the reverse iterator before grabbing base().
300                                                 // Ugly, but it gets the job done...
301                                                 hit = (++ri).base();
302                                                 // Put it back where we found it, so the tests following this
303                                                 // don't fail...
304                                                 ri--;
305                                                 break;
306                                         }
307                                 }
308
309                                 // If we hit the highest in the list, then pass the event through
310                                 // to the window for handling. if we hit no windows, then pass the
311                                 // event to all windows. Otherwise, we need to shuffle windows.
312
313 //NOTE: We need to pass the click to all windows regardless of whether they're topmost or not...
314                                 if (ri == windowList.rbegin())
315                                 {
316                                         for(i=windowList.begin(); i!=windowList.end(); i++)
317                                                 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
318                                 }
319                                 else if (ri == windowList.rend())
320                                 {
321                                         for(i=windowList.begin(); i!=windowList.end(); i++)
322                                                 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
323                                 }
324                                 else
325                                 {
326 // - Otherwise, restore backing store on each window in reverse order.
327                                         for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
328                                                 (*ri)->RestoreScreenFromBackstore();
329                                         // At this point, the screen has been restored...
330
331 // - Remove item clicked on from the list. Put removed item at the end of the list.
332                                         windowList.push_back(*hit);
333                                         windowList.erase(hit);
334 // - Go through list and pass click through to each window in the list. Also do a
335 //  blit to backing store and a Draw() for each window.
336                                         for(i=windowList.begin(); i!= windowList.end(); i++)
337                                         {
338                                                 // Grab bg into backstore
339                                                 (*i)->SaveScreenToBackstore();
340                                                 // Pass click
341                                                 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
342                                                 // Draw?
343                                                 (*i)->Draw();
344                                         }
345                                 }
346 #endif
347 #endif
348 /*
349 A slightly different way to handle this would be to loop through all windows, compare
350 all those above it to see if they obscure it; if so then subdivide it's update rectangle
351 to eliminate drawing the parts that aren't shown. The beauty of this approach is that
352 you don't have to care what order the windows are drawn in and you don't need to worry
353 about the order of restoring the backing store.
354
355 You *do* still need to determine the Z-order of the windows, in order to get the subdivisions
356 correct, but that's not too terrible.
357
358 Also, when doing a window drag, the coverage lists for all windows have to be regenerated.
359 */
360                                 std::list<Element *>::reverse_iterator ri;
361                                 bool movedWindow = false;
362
363                                 for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
364                                 {
365                                         if ((*ri)->Inside(event.button.x, event.button.y))
366                                         {
367                                                 // Remove item clicked on from the list & put removed item at the
368                                                 // end of the list, thus putting the window at the top of the Z
369                                                 // order. But IFF window is not already topmost!
370                                                 if (ri != windowList.rbegin())
371                                                 {
372                                                         windowList.push_back(*ri);
373                                                         // Here's a bit of STL weirdness: Converting from a reverse
374                                                         // iterator to a regular iterator requires backing the iterator
375                                                         // up a position after grabbing it's base() OR going forward
376                                                         // one position with the reverse iterator before grabbing base().
377                                                         // Ugly, but it get the job done...
378                                                         windowList.erase((++ri).base());
379                                                         movedWindow = true;
380                                                 }
381
382                                                 break;
383                                         }
384                                 }
385
386 //Small problem here: we should only pass the *hit* to the topmost window and pass
387 //*misses* to everyone else... Otherwise, you can have overlapping draggable windows
388 //and be able to drag both by clicking on a point that intersects both...
389 //(though that may be an interesting way to handle things!)
390 //The thing is that you want to do it on purpose (like with a special grouping widget)
391 //instead of by accident. So, !!! FIX !!!
392                                 // Pass the click on to all windows
393 //                              for(i=windowList.begin(); i!=windowList.end(); i++)
394 //                                      (*i)->HandleMouseButton(event.button.x, event.button.y, true);
395                                 windowList.back()->HandleMouseButton(event.button.x, event.button.y, true);
396
397 //                              // & bail if nothing changed...
398                                 if (movedWindow)
399 //                                      return;
400 {
401                                 // Check for overlap/build coverage lists [O((n^2)/2) algorithm!]
402 //One way to optimize this would be to only reset coverage lists from the point in
403 //the Z order where the previous window was.
404                                 for(i=windowList.begin(); i!=windowList.end(); i++)
405                                 {
406 //One other little quirk: Probably need to clear the backing store as well!
407 //Not sure...
408                                         (*i)->ResetCoverageList();
409
410                                         // This looks odd, but it's just a consequence of iterator weirdness.
411                                         // Otherwise we could just stick a j+1 in the for loop below. :-P
412                                         std::list<Element *>::iterator j = i;
413                                         j++;
414
415                                         for(; j!=windowList.end(); j++)
416                                                 (*i)->AdjustCoverageList((*j)->GetExtents());
417
418 //                                      (*i)->HandleMouseButton(event.button.x, event.button.y, true);
419                                         (*i)->Draw();
420                                 }
421 }
422                         }
423                         else if (event.type == SDL_MOUSEBUTTONUP)
424                         {
425 #ifdef DEBUG_MAIN_LOOP
426 WriteLog(" -- SDL_MOUSEBUTTONUP\n");
427 #endif
428 //Not sure that this is the right way to handle this...
429                                 for(i=windowList.begin(); i!=windowList.end(); i++)
430                                         (*i)->HandleMouseButton(event.button.x, event.button.y, false);
431 //I think we should only do topmost here...
432 //Or should we???
433 //                              windowList.back()->HandleMouseButton(event.button.x, event.button.y, false);
434                         }
435 #ifdef DEBUG_MAIN_LOOP
436 else
437         WriteLog(" -- Unknown event\n");
438 #endif
439
440                         if (Element::ScreenNeedsRefreshing())
441                         {
442 #ifndef USE_NEW_MAINBUFFERING
443 #ifdef DEBUG_MAIN_LOOP
444 WriteLog("Screen refresh called!\n");
445 #endif
446                                 RenderScreenBuffer();
447                                 Element::ScreenWasRefreshed();
448 #else
449                                 FlipMainScreen();
450                                 Element::ScreenWasRefreshed();
451 #endif
452                         }
453                 }
454 //hm. Works, but slows things way down.
455 //Now we use WaitEvents() instead. Yay!
456 //SDL_Delay(10);
457         }
458
459         SDL_EnableKeyRepeat(0, 0);
460 //      return false;
461 }
462
463 void GUI::Stop(void)
464 {
465         exitGUI = true;
466 }