2 // DRAGGABLEWINDOW2.CPP
4 // Graphical User Interface draggable window class
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 03/17/2006 Created this file
12 // JLH 03/17/2006 Added clipping against parent extents
16 // - Check for parent's extents and clip movement against those extents [DONE]
19 #include "draggablewindow2.h"
20 #include "guimisc.h" // Various support functions
25 //#define DESTRUCTOR_TESTING
26 #define BACKGROUND_IMG_TEST
27 #define USE_COVERAGE_LISTS
29 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
30 #define MASK_R 0xFF000000
31 #define MASK_G 0x00FF0000
32 #define MASK_B 0x0000FF00
33 #define MASK_A 0x000000FF
35 #define MASK_R 0x000000FF
36 #define MASK_G 0x0000FF00
37 #define MASK_B 0x00FF0000
38 #define MASK_A 0xFF000000
42 // DraggableWindow class implementation
44 // NOTE: FG/BG colors are hard-wired
47 DraggableWindow2::DraggableWindow2(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
48 void (* f)(Element *)/*= NULL*/):
49 Window(x, y, w, h, f), clicked(false)
51 #ifdef BACKGROUND_IMG_TEST
52 uint16 imgWidth = (floppyDiskImg[0] << 8) | floppyDiskImg[1];
53 uint16 imgHeight = (floppyDiskImg[2] << 8) | floppyDiskImg[3];
54 img = SDL_CreateRGBSurfaceFrom(&floppyDiskImg[4], imgWidth, imgHeight, 32, imgWidth * 4,
55 MASK_R, MASK_G, MASK_B, MASK_A);
56 label = SDL_CreateRGBSurface(SDL_SWSURFACE, 16*7, 32, 32,
57 MASK_R, MASK_G, MASK_B, MASK_A);
59 //Prolly should draw this in the constructor...
61 extern char textChar2e[];
62 uint8 * fontAddr = (uint8 *)textChar2e + ((128 + 32) * 7 * 8);
63 SetNewFont(Font(fontAddr, 7, 8));
64 DrawStringOpaque(label, 0, 0, 0xFF000000, 0xFFFFFFFF, "Ultima III - Boo");
65 DrawStringOpaque(label, 0, 8, 0xFF000000, 0xFFFFFFFF, "0123456789012345");
66 DrawStringOpaque(label, 0, 16, 0xFF000000, 0xFFFFFFFF, "1234567890123456");
67 DrawStringOpaque(label, 0, 24, 0xFF000000, 0xFFFFFFFF, "2345678901234567");
72 //Bleh. We only have to do this here because Window's constructor is doing it too. !!! FIX !!!
73 //Mebbe put this shiaut into Element? What about stuff that doesn't draw itself right away?
74 //Perhaps we should just hit needToRefreshScreen or some such--but will that trigger a redraw?
75 RestoreScreenFromBackstore();
76 Draw(); // Can we do this in the constructor??? Mebbe.
79 DraggableWindow2::~DraggableWindow2()
81 #ifdef DESTRUCTOR_TESTING
82 printf("Inside ~DraggableWindow2()...\n");
85 #ifdef BACKGROUND_IMG_TEST
87 SDL_FreeSurface(label);
91 void DraggableWindow2::HandleMouseMove(uint32 x, uint32 y)
95 //Need to check whether or not we've run into the extents of the screen... !!! FIX !!!
97 int32 newX = x - offset.x;
98 int32 newY = y - offset.y;
99 SDL_Rect clip = GetParentRect();
103 else if (newX > (clip.w - extents.w))
104 newX = clip.w - extents.w;
108 else if (newY > (clip.h - extents.h))
109 newY = clip.h - extents.h;
111 RestoreScreenFromBackstore();
114 SaveScreenToBackstore();
115 // SDL_BlitSurface(screen, &extents, backstore, NULL);
121 // Handle the items this window contains...
122 // for(uint32 i=0; i<list.size(); i++)
123 // Make coords relative to upper right corner of this window...
124 // list[i]->HandleMouseMove(x - extents.x, y - extents.y);
125 Window::HandleMouseMove(x, y);
128 void DraggableWindow2::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
132 if (mouseDown && Inside(x, y))
135 offset.x = x - extents.x;
136 offset.y = y - extents.y;
139 // Handle the items this window contains...
140 for(uint32 i=0; i<list.size(); i++)
142 // Make coords relative to upper right corner of this window...
143 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
145 if (list[i]->Inside(x - extents.x, y - extents.y))
150 void DraggableWindow2::Draw(void)
152 #ifdef USE_COVERAGE_LISTS
153 // These are *always* top level and parentless, so no need to traverse up through
154 // the parent chain...
155 for(std::list<SDL_Rect>::iterator i=coverList.begin(); i!=coverList.end(); i++)
158 src.x = (*i).x - extents.x, src.y = (*i).y - extents.y, src.w = (*i).w, src.h = (*i).h;
159 dst.x = (*i).x, dst.y = (*i).y;
160 SDL_BlitSurface(img, &src, screen, &dst);
163 //This doesn't get clipped at all... !!! FIX !!!
165 src.x = 0, src.y = 0, src.w = label->w, src.h = label->h;
166 dst.x = extents.x + 8, dst.y = extents.y + 6;
167 SDL_BlitSurface(label, &src, screen, &dst);
169 // Handle the items this window contains...
170 for(uint32 i=0; i<list.size(); i++)
173 // These are *always* top level and parentless, so no need to traverse up through
174 // the parent chain...
175 //Perhaps we can make these parentable, put the parent traversal in the base class?
177 #ifdef BACKGROUND_IMG_TEST
179 src.x = 0, src.y = 0, src.w = extents.w, src.h = extents.h;
180 dst.x = extents.x, dst.y = extents.y;
181 SDL_BlitSurface(img, &src, screen, &dst);
183 extern char textChar2e[];
184 uint8 * fontAddr = (uint8 *)textChar2e + ((128 + 32) * 7 * 8);
185 SetNewFont(Font(fontAddr, 7, 8));
186 DrawStringOpaque(screen, extents.x + 8, extents.y + 6, 0xFF000000, 0xFFFFFFFF, "Ultima III - Boo");
187 DrawStringOpaque(screen, extents.x + 8, extents.y + 14, 0xFF000000, 0xFFFFFFFF, "0123456789012345");
188 DrawStringOpaque(screen, extents.x + 8, extents.y + 22, 0xFF000000, 0xFFFFFFFF, "1234567890123456");
189 DrawStringOpaque(screen, extents.x + 8, extents.y + 30, 0xFF000000, 0xFFFFFFFF, "2345678901234567");
192 SDL_FillRect(screen, &extents, bgColor);
195 // Handle the items this window contains...
196 for(uint32 i=0; i<list.size(); i++)
200 //Prolly don't need this since the close button will do this for us...
201 needToRefreshScreen = true;