]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/window.cpp
Major refactoring of GUI: Phase I
[virtualjaguar] / src / gui / window.cpp
1 //
2 // Window class
3 //
4 // by James L. Hammons
5 //
6
7 #include "window.h"
8
9 #include "guimisc.h"
10
11 Window::Window(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
12         void (* f)(Element *)/*= NULL*/): Element(x, y, w, h),
13 //                      /*clicked(false), inside(false),*/ fgColor(0x4FF0), bgColor(0x1E10),
14 //4FF0 -> 010011 11111 10000 -> 0100 1101 1111 1111 1000 0100 -> 4D FF 84
15 //1E10 -> 000111 10000 10000 -> 0001 1111 1000 0100 1000 0100 -> 1F 84 84
16         /*clicked(false), inside(false),*/ fgColor(0xFF84FF4D), bgColor(0xFF84841F),
17         handler(f)
18 {
19         close = new Button(w - (CLOSEBOX_WIDTH + 1), 1, closeBox, closeBoxHover, closeBoxDown);
20         list.push_back(close);
21         close->SetNotificationElement(this);
22 }
23
24 Window::~Window()
25 {
26         for(uint32 i=0; i<list.size(); i++)
27                 if (list[i])
28                         delete list[i];
29 }
30
31 void Window::HandleKey(SDLKey key)
32 {
33         if (key == SDLK_ESCAPE)
34         {
35                 SDL_Event event;
36                 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
37                 SDL_PushEvent(&event);
38         }
39
40         // Handle the items this window contains...
41         for(uint32 i=0; i<list.size(); i++)
42                 // Make coords relative to upper right corner of this window...
43                 list[i]->HandleKey(key);
44 }
45
46 void Window::HandleMouseMove(uint32 x, uint32 y)
47 {
48         // Handle the items this window contains...
49         for(uint32 i=0; i<list.size(); i++)
50                 // Make coords relative to upper right corner of this window...
51                 list[i]->HandleMouseMove(x - extents.x, y - extents.y);
52 }
53
54 void Window::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
55 {
56         // Handle the items this window contains...
57         for(uint32 i=0; i<list.size(); i++)
58                 // Make coords relative to upper right corner of this window...
59                 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
60 }
61
62 void Window::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/)
63 {
64         uint32 addr = (extents.x + offsetX) + ((extents.y + offsetY) * pitch);
65
66         for(uint32 y=0; y<extents.h; y++)
67         {
68                 for(uint32 x=0; x<extents.w; x++)
69                 {
70                         // Doesn't clip in y axis! !!! FIX !!!
71                         if (extents.x + x < pitch)
72                                 screenBuffer[addr + x + (y * pitch)] = bgColor;
73                 }
74         }
75
76         // Handle the items this window contains...
77         for(uint32 i=0; i<list.size(); i++)
78                 list[i]->Draw(extents.x, extents.y);
79 }
80
81 void Window::AddElement(Element * e)
82 {
83         list.push_back(e);
84 }
85
86 void Window::Notify(Element * e)
87 {
88         if (e == close)
89         {
90                 SDL_Event event;
91                 event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
92                 SDL_PushEvent(&event);
93         }
94 }