AxWin2 - More fiddling, almost ready now :)
[tpg/acess2.git] / Usermode / Applications / axwin2_src / WM / wm.c
1 /*
2  * Acess GUI (AxWin) Version 2
3  * By John Hodge (thePowersGang)
4  * 
5  * Window Manager and Widget Control
6  */
7 #include "common.h"
8 #include <stdlib.h>
9 #include <string.h>
10 #include "wm.h"
11 #include <acess/sys.h>  // _SysDebug
12
13 // === IMPORTS ===
14 extern void     Video_GetTextDims(tFont *Font, const char *Text, int *W, int *H);
15
16 // === PROTOTYPES ===
17 tApplication    *AxWin_RegisterClient(tIPC_Type *IPCType, void *Ident, const char *Name);
18 void    AxWin_DeregisterClient(tApplication *App);
19 tApplication    *AxWin_GetClient(tIPC_Type *Method, void *Ident);
20 tElement        *AxWin_CreateElement(tElement *Parent, int Type, int Flags, const char *DebugName);
21 void    AxWin_DeleteElement(tElement *Element);
22 void    AxWin_SetFlags(tElement *Element, int Flags);
23 void    AxWin_SetSize(tElement *Element, int Size);
24 void    AxWin_SetText(tElement *Element, const char *Text);
25
26 // === GLOBALS ===
27 // - TODO: Handle windows by having multiple root elements
28 tElement        gWM_RootElement = {
29         .DebugName = "ROOT"
30 };
31 tApplication    *gWM_Applications;
32
33 // --- Element type flags
34 struct {
35         void    (*Init)(tElement *This);
36         void    (*Delete)(tElement *This);
37         void    (*UpdateFlags)(tElement *This);
38         void    (*UpdateSize)(tElement *This);
39         void    (*UpdateText)(tElement *This);
40 }       gaWM_WidgetTypes[MAX_ELETYPES] = {
41         {NULL, NULL, NULL, NULL, NULL}, // NULL
42         {NULL, NULL, NULL, NULL, NULL}, // Window
43         {NULL, NULL, NULL, NULL, NULL}  // Box
44 };
45 const int       ciWM_NumWidgetTypes = sizeof(gaWM_WidgetTypes)/sizeof(gaWM_WidgetTypes[0]);
46
47 // === CODE ===
48 tApplication *AxWin_RegisterClient(tIPC_Type *Method, void *Ident, const char *Name)
49 {
50          int    identlen = Method->GetIdentSize(Ident);
51         // Structure, empty string, Name, Ident
52         tApplication    *ret = calloc( 1, sizeof(tApplication) + 1 + strlen(Name) + 1 + identlen );
53         
54         // DebugName is empty
55         
56         // Name/Title
57         ret->Name = &ret->MetaElement.DebugName[1];
58         strcpy(ret->Name, Name);
59         // Ident
60         ret->Ident = ret->Name + strlen(Name) + 1;
61         memcpy(ret->Ident, Ident, identlen);
62         // IPC Type
63         ret->IPCType = Method;
64
65         // Element index
66         ret->MaxElementIndex = DEFAULT_ELEMENTS_PER_APP;
67         ret->EleIndex = calloc( 1, ret->MaxElementIndex * sizeof(*ret->EleIndex) );
68
69         // Add to global list
70         ret->Next = gWM_Applications;
71         gWM_Applications = ret;
72
73         // TODO: Inform listeners of the new application
74
75         return ret;
76 }
77
78 void AxWin_DeregisterClient(tApplication *App)
79 {
80         // TODO: Complete implementing DeregisterClient
81         tElement        *win, *next;
82
83         for( win = App->MetaElement.FirstChild; win; win = next )
84         {
85                 next = win->NextSibling;
86                 AxWin_DeleteElement(win);
87         }
88
89         // TODO: Inform listeners of deleted application
90         
91         // Remove from list
92         {
93                 tApplication    *app, *prev = NULL;
94                 for( app = gWM_Applications; app; app = app->Next )
95                 {
96                         if( app == App )        break;
97                         prev = app;
98                 }
99                 
100                 if( app )
101                 {
102                         if(prev)
103                                 prev->Next = App->Next;
104                         else
105                                 gWM_Applications = App->Next;
106                 }
107         }
108         
109         free(App);
110 }
111
112 /**
113  * \brief Get an application handle from a client identifier
114  */
115 tApplication *AxWin_GetClient(tIPC_Type *Method, void *Ident)
116 {
117         // TODO: Faster and smarter technique
118         tApplication    *app;
119         for( app = gWM_Applications; app; app = app->Next )
120         {
121                 if( app->IPCType != Method )    continue;
122                 if( Method->CompareIdent( app->Ident, Ident ) != 0 ) continue;
123                 return app;
124         }
125         return NULL;
126 }
127
128 tElement *AxWin_CreateAppWindow(tApplication *App, const char *Name)
129 {
130         tElement        *ret;
131         
132         ret = AxWin_CreateElement(&App->MetaElement, ELETYPE_WINDOW, 0, NULL);
133         ret->Text = strdup(Name);
134         return ret;
135 }
136
137 // --- Widget Creation and Control ---
138 tAxWin_Element *AxWin_CreateElement(tElement *Parent, int Type, int Flags, const char *DebugName)
139 {
140         tElement        *ret;
141         const char      *dbgName = DebugName ? DebugName : "";
142         
143         ret = calloc(sizeof(tElement)+strlen(dbgName)+1, 1);
144         if(!ret)        return NULL;
145         
146         // Prepare
147         ret->Type = Type;
148         strcpy(ret->DebugName, dbgName);
149         if(Parent == NULL)      Parent = &gWM_RootElement;
150         ret->Parent = Parent;
151         ret->Flags = Flags;
152         
153         // Append to parent's list
154         if(Parent->LastChild)
155                 Parent->LastChild->NextSibling = ret;
156         Parent->LastChild = ret;
157         if(!Parent->FirstChild) Parent->FirstChild = ret;
158         
159         ret->PaddingL = 2;
160         ret->PaddingR = 2;
161         ret->PaddingT = 2;
162         ret->PaddingB = 2;
163         
164         if( Type < ciWM_NumWidgetTypes && gaWM_WidgetTypes[Type].Init )
165                 gaWM_WidgetTypes[Type].Init(ret);
166         
167         WM_UpdateMinDims(ret->Parent);
168         
169         return ret;
170 }
171
172 /**
173  * \brief Delete an element
174  */
175 void AxWin_DeleteElement(tElement *Element)
176 {
177         tElement        *child, *next;
178         
179         for(child = Element->FirstChild; child; child = next)
180         {
181                 next = child->NextSibling;
182                 AxWin_DeleteElement(child);
183         }
184
185         // TODO: Implement AxWin_DeleteElement
186         // TODO: Clean up related data.
187         if( Element->Type < ciWM_NumWidgetTypes && gaWM_WidgetTypes[Element->Type].Delete )
188                 gaWM_WidgetTypes[Element->Type].Delete(Element);
189
190         if(Element->Owner)
191                 Element->Owner->EleIndex[ Element->ApplicationID ] = NULL;
192
193         Element->Type = 0;
194         Element->Owner = 0;
195         Element->Flags = 0;
196
197         free(Element);
198 }
199
200 /**
201  * \brief Alter an element's flags 
202  */
203 void AxWin_SetFlags(tElement *Element, int Flags)
204 {
205         // Permissions are handled in the message handler
206         if(!Element) {
207                 gWM_RootElement.Flags = Flags;
208                 return ;
209         }
210         
211         Element->Flags = Flags;
212         return ;
213 }
214
215 /**
216  * \brief Set the fixed lenghthways size of an element
217  */
218 void AxWin_SetSize(tElement *Element, int Size)
219 {
220         if(!Element)    return ;
221         Element->FixedWith = Size;
222         return ;
223 }
224
225 /**
226  * \brief Set the text field of an element
227  * \note Used for the image path on ELETYPE_IMAGE
228  */
229 void AxWin_SetText(tElement *Element, const char *Text)
230 {
231         if(!Element)    return ;
232         if(Element->Text)       free(Element->Text);
233         Element->Text = strdup(Text);
234         
235         switch(Element->Type)
236         {
237         case ELETYPE_IMAGE:
238                 if(Element->Data)       free(Element->Data);
239                 Element->Data = Image_Load( Element->Text );
240                 if(!Element->Data) {
241                         Element->Flags &= ~ELEFLAG_FIXEDSIZE;
242                         return ;
243                 }
244                 
245                 //Element->Flags |= ELEFLAG_FIXEDSIZE;
246                 Element->CachedW = ((tImage*)Element->Data)->Width;
247                 Element->CachedH = ((tImage*)Element->Data)->Height;
248                 
249                 if(Element->Parent && Element->Parent->Flags & ELEFLAG_VERTICAL) {
250                         Element->MinCross = ((tImage*)Element->Data)->Width;
251                         Element->MinWith = ((tImage*)Element->Data)->Height;
252                 }
253                 else {
254                         Element->MinWith = ((tImage*)Element->Data)->Width;
255                         Element->MinCross = ((tImage*)Element->Data)->Height;
256                 }
257                 break;
258         
259         case ELETYPE_TEXT:
260                 {
261                  int    w=0, h=0;
262                 Video_GetTextDims(NULL, Element->Text, &w, &h);
263                 if(Element->Parent && Element->Parent->Flags & ELEFLAG_VERTICAL) {
264                         Element->MinCross = w;
265                         Element->MinWith = h;
266                 }
267                 else {
268                         Element->MinWith = w;
269                         Element->MinCross = h;
270                 }
271                 }
272                 break;
273         default:        // Any other, no special case
274                 break ; 
275         }
276         
277         return ;
278 }

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