Usermode/AxWin3 - Moved interface library to Applications tree
[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/ipc.h"
12 #include "include/internal.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 Flags,
83         int DataBytes, void **DataPtr,
84         tAxWin3_WindowMessageHandler MessageHandler
85         )
86 {
87         tWindow *ret;
88          int    newWinID;
89          int    dataSize = sizeof(tIPCMsg_CreateWin) + strlen(Renderer) + 1;
90         tAxWin_IPCMessage       *msg;
91         tIPCMsg_CreateWin       *create_win;
92
93         // Allocate a window ID
94         ret = AxWin3_int_AllocateWindowInfo(DataBytes, &newWinID);
95         if(!ret)        return NULL;
96         ret->Handler = MessageHandler;
97
98         // Create message
99         msg = AxWin3_int_AllocateIPCMessage(Parent, IPCMSG_CREATEWIN, 0, dataSize);
100         create_win = (void*)msg->Data;  
101         create_win->NewWinID = newWinID;
102         create_win->Flags = Flags;
103         strcpy(create_win->Renderer, Renderer);
104
105         // Send and clean up
106         AxWin3_int_SendIPCMessage(msg);
107         free(msg);
108
109         // TODO: Detect and handle possible errors
110
111         // Return success
112         if(DataPtr)     *DataPtr = ret->Data;
113         return ret;
114 }
115
116 void AxWin3_DestroyWindow(tHWND Window)
117 {
118         tAxWin_IPCMessage       *msg;
119         
120         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_DESTROYWIN, 0, 0);
121         AxWin3_int_SendIPCMessage(msg);
122         free(msg);
123 }
124
125 void AxWin3_ShowWindow(tHWND Window, int bShow)
126 {
127         tAxWin_IPCMessage       *msg;
128         tIPCMsg_ShowWindow      *info;
129
130         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SHOWWINDOW, 0, sizeof(*info));
131         info = (void*)msg->Data;
132         info->bShow = !!bShow;
133         
134         AxWin3_int_SendIPCMessage(msg);
135         
136         free(msg);
137 }
138
139 void AxWin3_SetWindowPos(tHWND Window, short X, short Y, short W, short H)
140 {
141         tAxWin_IPCMessage       *msg;
142         tIPCMsg_SetWindowPos    *info;
143         
144         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
145         info = (void*)msg->Data;
146
147         info->Fields = 0xF;
148         info->X = X;    info->Y = Y;
149         info->W = W;    info->H = H;
150
151         AxWin3_int_SendIPCMessage(msg); 
152         free(msg);
153 }
154
155 void AxWin3_MoveWindow(tHWND Window, short X, short Y)
156 {
157         tAxWin_IPCMessage       *msg;
158         tIPCMsg_SetWindowPos    *info;
159         
160         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
161         info = (void*)msg->Data;
162
163         info->Fields = 0x3;
164         info->X = X;
165         info->Y = Y;
166         
167         AxWin3_int_SendIPCMessage(msg);
168         
169         free(msg);
170 }
171
172 void AxWin3_ResizeWindow(tHWND Window, short W, short H)
173 {
174         tAxWin_IPCMessage       *msg;
175         tIPCMsg_SetWindowPos    *info;
176         
177         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
178         info = (void*)msg->Data;
179
180         info->Fields = 0xC;
181         info->W = W;
182         info->H = H;
183         
184         AxWin3_int_SendIPCMessage(msg);
185         
186         free(msg);
187 }
188

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