Usermode/AxWin3 - Implementing more of the widget code
[tpg/acess2.git] / Usermode / Applications / axwin3_src / libaxwin3.so_src / wm.c
1 /*
2  * AxWin3 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * wm.c
6  * - Core window management functions
7  */
8 #include <axwin3/axwin.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "include/internal.h"
12 #include "include/ipc.h"
13
14 #define WINDOWS_PER_ALLOC       (63)
15
16 typedef struct sWindowBlock     tWindowBlock;
17
18 typedef struct sAxWin3_Window   tWindow;
19
20 // === STRUCTURES ===
21 struct sWindowBlock
22 {
23         tWindowBlock    *Next;
24         tWindow *Windows[WINDOWS_PER_ALLOC];
25 };
26
27 // === GLOBALS ===
28  int    giAxWin3_LowestFreeWinID;
29  int    giAxWin3_HighestUsedWinID;
30 tWindowBlock    gAxWin3_WindowList;
31
32 // === CODE ===
33 tWindow *AxWin3_int_CreateWindowStruct(uint32_t ServerID, int ExtraBytes)
34 {
35         tWindow *ret;
36         
37         ret = calloc(sizeof(tWindow) + ExtraBytes, 1);
38         ret->ServerID = ServerID;
39
40         return ret;
41 }
42
43 tWindow *AxWin3_int_AllocateWindowInfo(int DataBytes, int *WinID)
44 {
45          int    idx, newWinID;
46         tWindowBlock *block, *prev;
47         tWindow *ret;   
48
49         block = &gAxWin3_WindowList;
50         newWinID = giAxWin3_LowestFreeWinID;
51         for( idx = 0; block; newWinID ++ )
52         {
53                 if( block->Windows[idx] == NULL )
54                         break;
55                 idx ++;
56                 if(idx == WINDOWS_PER_ALLOC) {
57                         prev = block;
58                         block = block->Next;
59                         idx = 0;
60                 }
61         }
62         
63         if( !block )
64         {
65                 block = calloc(sizeof(tWindowBlock), 1);
66                 prev->Next = block;
67                 idx = 0;
68         }
69         
70         ret = block->Windows[idx] = AxWin3_int_CreateWindowStruct(newWinID, DataBytes);
71         if(newWinID > giAxWin3_HighestUsedWinID)
72                 giAxWin3_HighestUsedWinID = newWinID;
73         if(newWinID == giAxWin3_LowestFreeWinID)
74                 giAxWin3_HighestUsedWinID ++;
75
76         if(WinID)       *WinID = newWinID;
77
78         return ret;
79 }
80
81 tHWND AxWin3_CreateWindow(
82         tHWND Parent, const char *Renderer, int RendererArg,
83         int DataBytes, tAxWin3_WindowMessageHandler MessageHandler
84         )
85 {
86         tWindow *ret;
87          int    newWinID;
88          int    dataSize = sizeof(tIPCMsg_CreateWin) + strlen(Renderer) + 1;
89         tAxWin_IPCMessage       *msg;
90         tIPCMsg_CreateWin       *create_win;
91
92         // Allocate a window ID
93         ret = AxWin3_int_AllocateWindowInfo(DataBytes, &newWinID);
94         if(!ret)        return NULL;
95         ret->Handler = MessageHandler;
96
97         // Create message
98         msg = AxWin3_int_AllocateIPCMessage(Parent, IPCMSG_CREATEWIN, 0, dataSize);
99         create_win = (void*)msg->Data;  
100         create_win->NewWinID = newWinID;
101         create_win->RendererArg = RendererArg;
102         strcpy(create_win->Renderer, Renderer);
103
104         // Send and clean up
105         AxWin3_int_SendIPCMessage(msg);
106         free(msg);
107
108         // TODO: Detect and handle possible errors
109
110         // Return success
111         return ret;
112 }
113
114 void AxWin3_DestroyWindow(tHWND Window)
115 {
116         tAxWin_IPCMessage       *msg;
117         
118         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_DESTROYWIN, 0, 0);
119         AxWin3_int_SendIPCMessage(msg);
120         free(msg);
121 }
122
123 void *AxWin3_int_GetDataPtr(tHWND Window)
124 {
125         return Window->Data;
126 }
127
128 void AxWin3_SendMessage(tHWND Window, tHWND Destination, int Message, int Length, void *Data)
129 {
130         tAxWin_IPCMessage       *msg;
131         tIPCMsg_SendMsg *info;
132         
133         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SENDMSG, 0, sizeof(*info)+Length);
134         info = (void*)msg->Data;
135         info->Dest = AxWin3_int_GetWindowID(Destination);
136         info->ID = Message;
137         info->Length = Length;
138         memcpy(info->Data, Data, Length);
139         
140         AxWin3_int_SendIPCMessage(msg);
141         free(msg);
142 }
143
144 void AxWin3_ShowWindow(tHWND Window, int bShow)
145 {
146         tAxWin_IPCMessage       *msg;
147         tIPCMsg_ShowWindow      *info;
148
149         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SHOWWINDOW, 0, sizeof(*info));
150         info = (void*)msg->Data;
151         info->bShow = !!bShow;
152         
153         AxWin3_int_SendIPCMessage(msg);
154         
155         free(msg);
156 }
157
158 void AxWin3_SetWindowPos(tHWND Window, short X, short Y, short W, short H)
159 {
160         tAxWin_IPCMessage       *msg;
161         tIPCMsg_SetWindowPos    *info;
162         
163         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
164         info = (void*)msg->Data;
165
166         info->bSetPos = 1;
167         info->bSetDims = 1;
168         info->X = X;    info->Y = Y;
169         info->W = W;    info->H = H;
170
171         AxWin3_int_SendIPCMessage(msg); 
172         free(msg);
173 }
174
175 void AxWin3_MoveWindow(tHWND Window, short X, short Y)
176 {
177         tAxWin_IPCMessage       *msg;
178         tIPCMsg_SetWindowPos    *info;
179         
180         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
181         info = (void*)msg->Data;
182
183         info->bSetPos = 1;
184         info->bSetDims = 0;
185         info->X = X;
186         info->Y = Y;
187         
188         AxWin3_int_SendIPCMessage(msg);
189         
190         free(msg);
191 }
192
193 void AxWin3_ResizeWindow(tHWND Window, short W, short H)
194 {
195         tAxWin_IPCMessage       *msg;
196         tIPCMsg_SetWindowPos    *info;
197         
198         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
199         info = (void*)msg->Data;
200
201         info->bSetPos = 0;
202         info->bSetDims = 1;
203         info->W = W;
204         info->H = H;
205         
206         AxWin3_int_SendIPCMessage(msg);
207         
208         free(msg);
209 }
210

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