]> Shamusworld >> Repos - apple2/blob - src/gui/draggablewindow2.cpp
25df4d0e95039a6349cafc92dc710f23fbd6dacb
[apple2] / src / gui / draggablewindow2.cpp
1 //
2 // DRAGGABLEWINDOW2.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/17/2006  Created this file
12 // JLH  03/17/2006  Added clipping against parent extents
13 //
14 // STILL TO DO:
15 //
16 // - Check for parent's extents and clip movement against those extents [DONE]
17 //
18
19 #include "draggablewindow2.h"
20 #include "guimisc.h"                                                            // Various support functions
21 #include <algorithm>
22
23 // Debugging support
24
25 //#define DESTRUCTOR_TESTING
26 #define BACKGROUND_IMG_TEST
27 #define USE_COVERAGE_LISTS
28
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
34 #else
35 #define MASK_R 0x000000FF
36 #define MASK_G 0x0000FF00
37 #define MASK_B 0x00FF0000
38 #define MASK_A 0xFF000000
39 #endif
40
41 //
42 // DraggableWindow class implementation
43 //
44 // NOTE: FG/BG colors are hard-wired
45 //
46
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)
50 {
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);
58
59 //Prolly should draw this in the constructor...
60 //Now is! :-D
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");
68         RestoreOldFont();
69 #endif
70
71 //      CreateBackstore();
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.
77 }
78
79 DraggableWindow2::~DraggableWindow2()
80 {
81 #ifdef DESTRUCTOR_TESTING
82 printf("Inside ~DraggableWindow2()...\n");
83 #endif
84
85 #ifdef BACKGROUND_IMG_TEST
86         SDL_FreeSurface(img);
87         SDL_FreeSurface(label);
88 #endif
89 }
90
91 void DraggableWindow2::HandleMouseMove(uint32 x, uint32 y)
92 {
93         if (clicked)
94         {
95 //Need to check whether or not we've run into the extents of the screen... !!! FIX !!!
96 //[DONE]
97                 int32 newX = x - offset.x;
98                 int32 newY = y - offset.y;
99                 SDL_Rect clip = GetParentRect();
100
101                 if (newX < clip.x)
102                         newX = clip.x;
103                 else if (newX > (clip.w - extents.w))
104                         newX = clip.w - extents.w;
105
106                 if (newY < clip.y)
107                         newY = clip.y;
108                 else if (newY > (clip.h - extents.h))
109                         newY = clip.h - extents.h;
110
111                 RestoreScreenFromBackstore();
112                 extents.x = newX;
113                 extents.y = newY;
114                 SaveScreenToBackstore();
115 //              SDL_BlitSurface(screen, &extents, backstore, NULL);
116                 Draw();
117
118                 return;
119         }
120
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);
126 }
127
128 void DraggableWindow2::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
129 {
130         clicked = false;
131
132         if (mouseDown && Inside(x, y))
133         {
134                 clicked = true;
135                 offset.x = x - extents.x;
136                 offset.y = y - extents.y;
137         }
138
139         // Handle the items this window contains...
140         for(uint32 i=0; i<list.size(); i++)
141         {
142                 // Make coords relative to upper right corner of this window...
143                 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
144
145                 if (list[i]->Inside(x - extents.x, y - extents.y))
146                         clicked = false;
147         }
148 }
149
150 void DraggableWindow2::Draw(void)
151 {
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++)
156         {
157                 SDL_Rect src, dst;
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);
161         }
162
163 //This doesn't get clipped at all... !!! FIX !!!
164         SDL_Rect src, dst;
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);
168
169         // Handle the items this window contains...
170         for(uint32 i=0; i<list.size(); i++)
171                 list[i]->Draw();
172 #else
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?
176 //Prolly.
177 #ifdef BACKGROUND_IMG_TEST
178         SDL_Rect src, dst;
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);
182
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");
190         RestoreOldFont();
191 #else
192         SDL_FillRect(screen, &extents, bgColor);
193 #endif
194
195         // Handle the items this window contains...
196         for(uint32 i=0; i<list.size(); i++)
197                 list[i]->Draw();
198 #endif
199
200 //Prolly don't need this since the close button will do this for us...
201         needToRefreshScreen = true;
202 }