Usermode/AxWin3 - Implementing more of the widget code
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / wm.c
1 /*
2  * Acess2 Window Manager v3
3  * - By John Hodge (thePowersGang)
4  *
5  * wm.c
6  * - Window manager core
7  */
8 #include <common.h>
9 #include <wm_renderer.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <video.h>
13 #include <wm_messages.h>
14
15 // === GLOBALS ===
16 tWMRenderer     *gpWM_Renderers;
17 tWindow *gpWM_RootWindow;
18
19 // === CODE ===
20 void WM_Initialise(void)
21 {
22         WM_CreateWindow(NULL, 0x0088FF, "Background");
23         gpWM_RootWindow->W = giScreenWidth;
24         gpWM_RootWindow->H = giScreenHeight;
25         gpWM_RootWindow->Flags = WINFLAG_SHOW;
26 }
27
28 void WM_RegisterRenderer(tWMRenderer *Renderer)
29 {
30         // TODO: Catch out duplicates
31         Renderer->Next = gpWM_Renderers;
32         gpWM_Renderers = Renderer;
33 }
34
35 // --- Manipulation
36 tWindow *WM_CreateWindow(tWindow *Parent, int RendererArg, const char *RendererName)
37 {
38         tWMRenderer     *renderer;
39         tWindow *ret;
40         
41         // - Get Renderer
42         for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
43         {
44                 if(strcmp(RendererName, renderer->Name) == 0)
45                         break;
46         }
47         if(renderer == NULL)
48                 return NULL;
49
50         if(!Parent)
51                 Parent = gpWM_RootWindow;
52
53         // - Call create window function
54         ret = renderer->CreateWindow(RendererArg);
55         ret->Parent = Parent;
56         ret->Renderer = renderer;
57         ret->Flags = WINFLAG_CLEAN;     // Note, not acutally clean, but it makes invaidate work
58
59         // Append to parent
60         if(Parent)
61         {
62                 if(Parent->LastChild)
63                         Parent->LastChild->NextSibling = ret;
64                 else
65                         Parent->FirstChild = ret;
66                 ret->PrevSibling = Parent->LastChild;
67                 Parent->LastChild = ret;
68         }
69         else
70         {
71                 gpWM_RootWindow = ret;
72         }
73
74         // - Return!
75         return ret;
76 }
77
78 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
79 {
80         tWindow *ret;
81         
82         ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
83         ret->RendererInfo = ret + 1;    // Get end of tWindow
84         
85         return ret;
86 }
87
88 void WM_ShowWindow(tWindow *Window, int bShow)
89 {
90         // TODO: Message window
91         if(bShow)
92                 Window->Flags |= WINFLAG_SHOW;
93         else
94                 Window->Flags &= ~WINFLAG_SHOW;
95         WM_Invalidate(Window);
96 }
97
98 int WM_MoveWindow(tWindow *Window, int X, int Y)
99 {
100         // Clip coordinates
101         if(X + Window->W < 0)   X = -Window->W + 1;
102         if(Y + Window->H < 0)   Y = -Window->H + 1;
103         if(X >= giScreenWidth)  X = giScreenWidth - 1;
104         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
105         
106         Window->X = X;  Window->Y = Y;
107
108         WM_Invalidate(Window);
109
110         return 0;
111 }
112
113 int WM_ResizeWindow(tWindow *Window, int W, int H)
114 {
115         if(W <= 0 || H <= 0 )   return 1;
116         if(Window->X + W < 0)   Window->X = -W + 1;
117         if(Window->Y + H < 0)   Window->Y = -H + 1;
118         
119         Window->W = W;  Window->H = H;
120
121         WM_Invalidate(Window);
122
123         {
124                 struct sWndMsg_Resize   msg;
125                 msg.W = W;
126                 msg.H = H;
127                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
128         }
129         
130         return 0;
131 }
132
133 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, void *Data)
134 {
135         if(Dest == NULL)        return -2;
136         if(Length > 0 && Data == NULL)  return -1;
137         
138         // ->HandleMessage returns 1 when the message was not handled
139         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
140         {
141                 // TODO: Catch errors from ->HandleMessage
142                 return 0;
143         }
144         
145         // TODO: Pass on to user
146         _SysDebug("WM_SendMessage: TODO - Implement sending to client application");
147         
148         return 1;
149 }
150
151 void WM_Invalidate(tWindow *Window)
152 {
153         // Don't invalidate twice (speedup)
154         if( !(Window->Flags & WINFLAG_CLEAN) )  return;
155
156 //      _SysDebug("Invalidating %p");
157         
158         // Mark for re-render
159         Window->Flags &= ~WINFLAG_CLEAN;
160
161         // Mark up the tree that a child window has changed     
162         while( (Window = Window->Parent) )
163         {
164 //              _SysDebug("Childclean removed from %p", Window);
165                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
166         }
167 }
168
169 // --- Rendering / Update
170 void WM_int_UpdateWindow(tWindow *Window)
171 {
172         tWindow *child;
173
174         // Ignore hidden windows
175         if( !(Window->Flags & WINFLAG_SHOW) )
176                 return ;
177         
178         // Render
179         if( !(Window->Flags & WINFLAG_CLEAN) )
180         {
181                 Window->Renderer->Redraw(Window);
182                 Window->Flags |= WINFLAG_CLEAN;
183         }
184         
185         // Process children
186         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
187         {
188                 for( child = Window->FirstChild; child; child = child->NextSibling )
189                 {
190                         WM_int_UpdateWindow(child);
191                 }
192                 Window->Flags |= WINFLAG_CHILDCLEAN;
193         }
194         
195 }
196
197 void WM_int_BlitWindow(tWindow *Window)
198 {
199         tWindow *child;
200
201         // Ignore hidden windows
202         if( !(Window->Flags & WINFLAG_SHOW) )
203                 return ;
204         
205         Video_Blit(Window->RenderBuffer, Window->X, Window->Y, Window->W, Window->H);
206         
207         for( child = Window->FirstChild; child; child = child->NextSibling )
208         {
209                 WM_int_BlitWindow(child);
210         }
211 }
212
213 void WM_Update(void)
214 {
215         // Don't redraw if nothing has changed
216         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
217                 return ;        
218
219         // - Iterate through visible windows, updating them as needed
220         WM_int_UpdateWindow(gpWM_RootWindow);
221         
222         // - Draw windows from back to front to the render buffer
223         WM_int_BlitWindow(gpWM_RootWindow);
224
225         Video_Update();
226 }
227

UCC git Repository :: git.ucc.asn.au