]> Shamusworld >> Repos - apple2/blob - apple2/src/gui/draggablewindow.cpp
860e35d4fc2e8bf5f21443d2601f123608065377
[apple2] / apple2 / src / gui / draggablewindow.cpp
1 //
2 // DRAGGABLEWINDOW.CPP
3 //
4 // Graphical User Interface draggable window class
5 // by James L. Hammons
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  03/01/2006  Created this file
12 //
13 // STILL TO DO:
14 //
15 // - Check for parent's extents and clip movement against those extents
16 //
17
18 #include "draggablewindow.h"
19 #include "button.h"
20 #include "guimisc.h"                                                            // Various support functions
21 #include <algorithm>
22
23 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
24 #define MASK_R 0xFF000000
25 #define MASK_G 0x00FF0000
26 #define MASK_B 0x0000FF00
27 #define MASK_A 0x000000FF
28 #else
29 #define MASK_R 0x000000FF
30 #define MASK_G 0x0000FF00
31 #define MASK_B 0x00FF0000
32 #define MASK_A 0xFF000000
33 #endif
34
35 using namespace std;                                                            // For STL stuff
36
37 #define BACKGROUND_IMG_TEST
38
39 //
40 // DraggableWindow class implementation
41 //
42 // NOTE: FG/BG colors are hard-wired
43 //
44
45 DraggableWindow::DraggableWindow(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
46         void (* f)(Element *)/*= NULL*/):
47         Element(x, y, w, h, 0x4D, 0xFF, 0x84, 0xFF, 0x1F, 0x84, 0x84, 0xFF), handler(f),
48         clicked(false),
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))
56 {
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);
60
61 #ifdef BACKGROUND_IMG_TEST
62 uint16 imgWidth = (floppyDiskImg[0] << 8) | floppyDiskImg[1];
63 uint16 imgHeight = (floppyDiskImg[2] << 8) | floppyDiskImg[3];
64 img = SDL_CreateRGBSurfaceFrom(&floppyDiskImg[4], imgWidth, imgHeight, 32, imgWidth * 4,
65         MASK_R, MASK_G, MASK_B, MASK_A);
66 #endif
67
68         CreateBackstore();
69         Draw(); // Can we do this in the constructor??? Mebbe.
70 }
71
72 DraggableWindow::~DraggableWindow()
73 {
74         for(uint32 i=0; i<list.size(); i++)
75                 if (list[i])
76                         delete list[i];
77
78 #ifdef BACKGROUND_IMG_TEST
79 SDL_FreeSurface(img);
80 #endif
81
82         SDL_FreeSurface(cbUp);
83         SDL_FreeSurface(cbDown);
84         SDL_FreeSurface(cbHover);
85 }
86
87 void DraggableWindow::HandleKey(SDLKey key)
88 {
89         if (key == SDLK_ESCAPE)
90         {
91                 SDL_Event event;
92                 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
93                 SDL_PushEvent(&event);
94         }
95
96         // Handle the items this window contains...
97         for(uint32 i=0; i<list.size(); i++)
98                 list[i]->HandleKey(key);
99 }
100
101 void DraggableWindow::HandleMouseMove(uint32 x, uint32 y)
102 {
103         if (clicked)
104         {
105 //Need to check whether or not we've run into the extents of the screen... !!! FIX !!!
106                 RestoreScreenFromBackstore();
107                 extents.x = x - offset.x;
108                 extents.y = y - offset.y;
109                 SDL_BlitSurface(screen, &extents, backstore, NULL);
110                 Draw();
111
112                 return;
113         }
114
115         // Handle the items this window contains...
116         for(uint32 i=0; i<list.size(); i++)
117                 // Make coords relative to upper right corner of this window...
118                 list[i]->HandleMouseMove(x - extents.x, y - extents.y);
119 }
120
121 void DraggableWindow::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
122 {
123         clicked = false;
124
125         if (mouseDown && Inside(x, y))
126         {
127                 clicked = true;
128                 offset.x = x - extents.x;
129                 offset.y = y - extents.y;
130         }
131
132         // Handle the items this window contains...
133         for(uint32 i=0; i<list.size(); i++)
134         {
135                 // Make coords relative to upper right corner of this window...
136                 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
137
138                 if (list[i]->Inside(x - extents.x, y - extents.y))
139                         clicked = false;
140         }
141 }
142
143 void DraggableWindow::Draw(void)
144 {
145         // These are *always* top level and parentless, so no need to traverse up through
146         // the parent chain...
147 //Perhaps we can make these parentable, put the parent traversal in the base class?
148 //Prolly.
149 #ifdef BACKGROUND_IMG_TEST
150         SDL_Rect src, dst;
151         src.x = 0, src.y = 0, src.w = extents.w, src.h = extents.h;
152         dst.x = extents.x, dst.y = extents.y;
153         SDL_BlitSurface(img, &src, screen, &dst);
154
155         extern char textChar2e[];
156         uint8 * fontAddr = (uint8 *)textChar2e + ((128 + 32) * 7 * 8);
157         SetNewFont(Font(fontAddr, 7, 8));
158         DrawStringOpaque(screen, extents.x + 8, extents.y +  6, 0xFF000000, 0xFFFFFFFF, "Ultima III - Boo");
159         DrawStringOpaque(screen, extents.x + 8, extents.y + 14, 0xFF000000, 0xFFFFFFFF, "0123456789012345");
160         DrawStringOpaque(screen, extents.x + 8, extents.y + 22, 0xFF000000, 0xFFFFFFFF, "1234567890123456");
161         DrawStringOpaque(screen, extents.x + 8, extents.y + 30, 0xFF000000, 0xFFFFFFFF, "2345678901234567");
162         RestoreOldFont();
163 #else
164         SDL_FillRect(screen, &extents, bgColor);
165 #endif
166
167         // Handle the items this window contains...
168         for(uint32 i=0; i<list.size(); i++)
169                 list[i]->Draw();
170
171 //Prolly don't need this since the close button will do this for us...
172         needToRefreshScreen = true;
173 }
174
175 void DraggableWindow::Notify(Element * e)
176 {
177         if (e == closeButton)
178         {
179                 SDL_Event event;
180                 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
181                 SDL_PushEvent(&event);
182         }
183 }
184
185 void DraggableWindow::AddElement(Element * e)
186 {
187         list.push_back(e);
188 }