Usermode/AxWin3 - Reworked widget minimum sizes, fixed bugs
[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 #include <decorator.h>
15
16 // === IMPORTS ===
17 extern void     IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int Msg, int Len, const void *Data);
18
19 // === GLOBALS ===
20 tWMRenderer     *gpWM_Renderers;
21 tWindow *gpWM_RootWindow;
22 //! Window which will recieve the next keyboard event
23 tWindow *gpWM_FocusedWindow;
24
25 // === CODE ===
26 void WM_Initialise(void)
27 {
28         WM_CreateWindow(NULL, NULL, 0, 0x0088FF, "Background");
29         gpWM_RootWindow->W = giScreenWidth;
30         gpWM_RootWindow->H = giScreenHeight;
31         gpWM_RootWindow->Flags = WINFLAG_SHOW|WINFLAG_NODECORATE;
32 }
33
34 void WM_RegisterRenderer(tWMRenderer *Renderer)
35 {
36         // TODO: Catch out duplicates
37         Renderer->Next = gpWM_Renderers;
38         gpWM_Renderers = Renderer;
39 }
40
41 // --- Manipulation
42 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
43 {
44         tWMRenderer     *renderer;
45         tWindow *ret;
46         
47         // - Get Renderer
48         for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
49         {
50                 if(strcmp(RendererName, renderer->Name) == 0)
51                         break;
52         }
53         if(renderer == NULL)
54                 return NULL;
55
56         if(!Parent)
57                 Parent = gpWM_RootWindow;
58
59         // - Call create window function
60         ret = renderer->CreateWindow(RendererArg);
61         ret->Client = Client;
62         ret->ID = ID;
63         ret->Parent = Parent;
64         ret->Renderer = renderer;
65         ret->Flags |= WINFLAG_CLEAN;    // Needed to stop invaildate early exiting
66
67         // Append to parent
68         if(Parent)
69         {
70                 if(Parent->LastChild)
71                         Parent->LastChild->NextSibling = ret;
72                 else
73                         Parent->FirstChild = ret;
74                 ret->PrevSibling = Parent->LastChild;
75                 Parent->LastChild = ret;
76                 ret->NextSibling = NULL;
77         }
78         else
79         {
80                 gpWM_RootWindow = ret;
81         }
82
83         // Don't decorate child windows by default
84         if(Parent != gpWM_RootWindow)
85         {
86                 ret->Flags |= WINFLAG_NODECORATE;
87         }
88         
89         // - Return!
90         return ret;
91 }
92
93 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
94 {
95         tWindow *ret;
96         
97         ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
98         ret->RendererInfo = ret + 1;    // Get end of tWindow
99         
100         return ret;
101 }
102
103 void WM_SetWindowTitle(tWindow *Window, const char *Title)
104 {
105         if(Window->Title)
106                 free(Window->Title);
107         Window->Title = strdup(Title);
108         _SysDebug("Window %p title set to '%s'", Window, Title);
109 }
110
111 void WM_RaiseWindow(tWindow *Window)
112 {
113         tWindow *parent = Window->Parent;
114         if(!Window->Parent)     return ;
115         
116         // Remove from list
117         if(Window->PrevSibling)
118                 Window->PrevSibling->NextSibling = Window->NextSibling;
119         if(Window->NextSibling)
120                 Window->NextSibling->PrevSibling = Window->PrevSibling;
121         if(parent->FirstChild == Window)
122                 parent->FirstChild = Window->NextSibling;
123         if(parent->LastChild == Window)
124                 parent->LastChild = Window->PrevSibling;
125
126         // Append to end
127         if(parent->LastChild)
128                 parent->LastChild->NextSibling = Window;
129         else
130                 parent->FirstChild = Window;
131         Window->PrevSibling = parent->LastChild;
132         Window->NextSibling = NULL;
133         parent->LastChild = Window;
134 }
135
136 void WM_FocusWindow(tWindow *Destination)
137 {
138         struct sWndMsg_Bool     _msg;
139         
140         if( gpWM_FocusedWindow == Destination )
141                 return ;
142         if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
143                 return ;
144         
145         _msg.Val = 0;
146         WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
147         _msg.Val = 1;
148         WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
149         
150         gpWM_FocusedWindow = Destination;
151 }
152
153
154 void WM_ShowWindow(tWindow *Window, int bShow)
155 {
156         // Message window
157         struct sWndMsg_Bool     _msg;
158         
159         if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
160                 return ;
161
162         _msg.Val = !!bShow;
163         WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
164         
165         if(bShow)
166                 Window->Flags |= WINFLAG_SHOW;
167         else {
168                 Window->Flags &= ~WINFLAG_SHOW;
169                 if( Window == gpWM_FocusedWindow )
170                         WM_FocusWindow(Window->Parent);
171         }
172         // Just a little memory saving for large hidden windows
173         if(Window->RenderBuffer) {
174                 free(Window->RenderBuffer);
175                 Window->RenderBuffer = NULL;
176         }
177         
178         WM_Invalidate(Window);
179 }
180
181 void WM_DecorateWindow(tWindow *Window, int bDecorate)
182 {
183         if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
184                 return ;
185         
186         if(bDecorate)
187                 Window->Flags &= ~WINFLAG_NODECORATE;
188         else
189                 Window->Flags |= WINFLAG_NODECORATE;
190         
191         // Needed because the window size changes
192         if(Window->RenderBuffer) {
193                 free(Window->RenderBuffer);
194                 Window->RenderBuffer = NULL;
195         }
196         
197         WM_Invalidate(Window);
198 }
199
200 int WM_MoveWindow(tWindow *Window, int X, int Y)
201 {
202         // Clip coordinates
203         if(X + Window->W < 0)   X = -Window->W + 1;
204         if(Y + Window->H < 0)   Y = -Window->H + 1;
205         if(X >= giScreenWidth)  X = giScreenWidth - 1;
206         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
207         
208         Window->X = X;  Window->Y = Y;
209
210         WM_Invalidate(Window);
211
212         return 0;
213 }
214
215 int WM_ResizeWindow(tWindow *Window, int W, int H)
216 {
217         if(W <= 0 || H <= 0 )   return 1;
218         if(Window->X + W < 0)   Window->X = -W + 1;
219         if(Window->Y + H < 0)   Window->Y = -H + 1;
220
221         Window->W = W;  Window->H = H;
222
223         if(Window->RenderBuffer) {
224                 free(Window->RenderBuffer);
225                 Window->RenderBuffer = NULL;
226         }
227         WM_Invalidate(Window);
228
229         {
230                 struct sWndMsg_Resize   msg;
231                 msg.W = W;
232                 msg.H = H;
233                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
234         }
235         
236         return 0;
237 }
238
239 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
240 {
241         if(Dest == NULL)        return -2;
242         if(Length > 0 && Data == NULL)  return -1;
243
244         if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
245         {
246                 // TODO: Catch errors from ->HandleMessage
247                 return 0;
248         }
249         
250         // ->HandleMessage returns 1 when the message was not handled
251         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
252         {
253                 // TODO: Catch errors from ->HandleMessage
254                 return 0;
255         }
256
257         // TODO: Implement message masking
258
259         if(Dest->Client)
260         {
261                 uint32_t        src_id;
262                 if(!Source)
263                         src_id = -1;
264                 else if(Source->Client != Dest->Client) {
265                         // TODO: Support different client source windows
266                         _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
267                         return -1;
268                 }
269                 else {
270                         src_id = Source->ID;
271                 }
272                 
273                 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
274         }       
275
276         return 1;
277 }
278
279 void WM_Invalidate(tWindow *Window)
280 {
281         _SysDebug("Invalidating %p", Window);
282         // Don't invalidate twice (speedup)
283 //      if( !(Window->Flags & WINFLAG_CLEAN) )  return;
284
285         // Mark for re-render
286         Window->Flags &= ~WINFLAG_CLEAN;
287
288         // Mark up the tree that a child window has changed     
289         while( (Window = Window->Parent) )
290                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
291 }
292
293 // --- Rendering / Update
294 void WM_int_UpdateWindow(tWindow *Window)
295 {
296         tWindow *child;
297
298         // Ignore hidden windows
299         if( !(Window->Flags & WINFLAG_SHOW) )
300                 return ;
301         
302         // Render
303         if( !(Window->Flags & WINFLAG_CLEAN) )
304         {
305                 // Calculate RealW/RealH
306                 if( !(Window->Flags & WINFLAG_NODECORATE) )
307                 {
308                         _SysDebug("Applying decorations to %p", Window);
309                         Decorator_UpdateBorderSize(Window);
310                         Window->RealW = Window->BorderL + Window->W + Window->BorderR;
311                         Window->RealH = Window->BorderT + Window->H + Window->BorderB;
312                         Decorator_Redraw(Window);
313                 }
314                 else
315                 {
316                         Window->BorderL = 0;
317                         Window->BorderR = 0;
318                         Window->BorderT = 0;
319                         Window->BorderB = 0;
320                         Window->RealW = Window->W;
321                         Window->RealH = Window->H;
322                 }
323                 
324                 Window->Renderer->Redraw(Window);
325                 Window->Flags |= WINFLAG_CLEAN;
326         }
327         
328         // Process children
329         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
330         {
331                 for( child = Window->FirstChild; child; child = child->NextSibling )
332                 {
333                         WM_int_UpdateWindow(child);
334                 }
335                 Window->Flags |= WINFLAG_CHILDCLEAN;
336         }
337         
338 }
339
340 void WM_int_BlitWindow(tWindow *Window)
341 {
342         tWindow *child;
343
344         // Ignore hidden windows
345         if( !(Window->Flags & WINFLAG_SHOW) )
346                 return ;
347
348         _SysDebug("Blit %p to (%i,%i) %ix%i", Window, Window->X, Window->Y, Window->RealW, Window->RealH);
349         Video_Blit(Window->RenderBuffer, Window->X, Window->Y, Window->RealW, Window->RealH);
350         
351         for( child = Window->FirstChild; child; child = child->NextSibling )
352         {
353                 WM_int_BlitWindow(child);
354         }
355 }
356
357 void WM_Update(void)
358 {
359         // Don't redraw if nothing has changed
360         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
361                 return ;        
362
363         // - Iterate through visible windows, updating them as needed
364         WM_int_UpdateWindow(gpWM_RootWindow);
365         
366         // - Draw windows from back to front to the render buffer
367         WM_int_BlitWindow(gpWM_RootWindow);
368
369         Video_Update();
370 }
371

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