4 // Graphical User Interface draggable window class
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 03/01/2006 Created this file
15 // - Check for parent's extents and clip movement against those extents
18 #include "draggablewindow.h"
20 #include "guimisc.h" // Various support functions
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
29 #define MASK_R 0x000000FF
30 #define MASK_G 0x0000FF00
31 #define MASK_B 0x00FF0000
32 #define MASK_A 0xFF000000
35 using namespace std; // For STL stuff
37 #define BACKGROUND_IMG_TEST
40 // DraggableWindow class implementation
42 // NOTE: FG/BG colors are hard-wired
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),
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);
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);
69 Draw(); // Can we do this in the constructor??? Mebbe.
72 DraggableWindow::~DraggableWindow()
74 for(uint32 i=0; i<list.size(); i++)
78 #ifdef BACKGROUND_IMG_TEST
82 SDL_FreeSurface(cbUp);
83 SDL_FreeSurface(cbDown);
84 SDL_FreeSurface(cbHover);
87 void DraggableWindow::HandleKey(SDLKey key)
89 if (key == SDLK_ESCAPE)
92 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
93 SDL_PushEvent(&event);
96 // Handle the items this window contains...
97 for(uint32 i=0; i<list.size(); i++)
98 list[i]->HandleKey(key);
101 void DraggableWindow::HandleMouseMove(uint32 x, uint32 y)
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);
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);
121 void DraggableWindow::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
125 if (mouseDown && Inside(x, y))
128 offset.x = x - extents.x;
129 offset.y = y - extents.y;
132 // Handle the items this window contains...
133 for(uint32 i=0; i<list.size(); i++)
135 // Make coords relative to upper right corner of this window...
136 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
138 if (list[i]->Inside(x - extents.x, y - extents.y))
143 void DraggableWindow::Draw(void)
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?
149 #ifdef BACKGROUND_IMG_TEST
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);
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");
164 SDL_FillRect(screen, &extents, bgColor);
167 // Handle the items this window contains...
168 for(uint32 i=0; i<list.size(); i++)
171 //Prolly don't need this since the close button will do this for us...
172 needToRefreshScreen = true;
175 void DraggableWindow::Notify(Element * e)
177 if (e == closeButton)
180 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
181 SDL_PushEvent(&event);
185 void DraggableWindow::AddElement(Element * e)