2 * AxWin3 Interface Library
3 * - By John Hodge (thePowersGang)
6 * - Widget window type interface
8 #include <axwin3/axwin.h>
9 #include <axwin3/widget.h>
10 #include "include/internal.h"
12 #include <widget_messages.h>
21 tAxWin3_Widget_FireCb Fire;
22 tAxWin3_Widget_KeyUpDownCb KeyUpDown;
23 tAxWin3_Widget_KeyFireCb KeyFire;
24 tAxWin3_Widget_MouseMoveCb MouseMove;
25 tAxWin3_Widget_MouseBtnCb MouseButton;
31 tAxWin3_Widget **Elements;
33 tAxWin3_Widget RootElement;
34 // Callbacks for each element
38 tAxWin3_Widget *AxWin3_Widget_int_GetElementByID(tHWND Window, uint32_t ID)
40 tWidgetWindowInfo *info;
41 if(!Window) return NULL;
43 info = AxWin3_int_GetDataPtr(Window);
44 if(ID >= info->nElements) return NULL;
46 return info->Elements[ID];
49 uint32_t AxWin3_Widget_int_AllocateID(tWidgetWindowInfo *Info)
52 // BUG BUG BUG - Double Allocations! (citation needed)
54 for( newID = Info->FirstFreeID; newID < Info->nElements; newID ++ )
56 if( Info->Elements[newID] == NULL )
59 if( newID == Info->nElements )
61 const int size_step = 4;
63 Info->Elements = realloc(Info->Elements, sizeof(*Info->Elements)*Info->nElements);
64 newID = Info->nElements - 4;
65 memset( &Info->Elements[newID+1], 0, (size_step-1)*sizeof(Info->Elements));
66 _SysDebug("Widget: Expanded to %i and allocated %i", Info->nElements, newID);
69 _SysDebug("Widget: Allocated %i", newID);
70 Info->Elements[newID] = (void*)-1;
76 int AxWin3_Widget_MessageHandler(tHWND Window, int MessageID, int Size, void *Data)
78 tAxWin3_Widget *widget;
82 case MSG_WIDGET_FIRE: {
83 tWidgetMsg_Fire *msg = Data;
84 if(Size < sizeof(*msg)) return -1;
85 widget = AxWin3_Widget_int_GetElementByID(Window, msg->WidgetID);
86 if(widget->Fire) widget->Fire(widget);
94 tHWND AxWin3_Widget_CreateWindow(tHWND Parent, int W, int H, int RootEleFlags)
97 tWidgetWindowInfo *info;
99 ret = AxWin3_CreateWindow(
100 Parent, "Widget", RootEleFlags,
101 sizeof(*info) + sizeof(tAxWin3_Widget), AxWin3_Widget_MessageHandler
103 info = AxWin3_int_GetDataPtr(ret);
105 info->RootElement.Window = ret;
106 info->RootElement.ID = -1;
108 AxWin3_ResizeWindow(ret, W, H);
113 void AxWin3_Widget_DestroyWindow(tHWND Window)
115 // Free all element structures
119 // Request window to be destroyed (will clean up any data stored in tHWND)
122 tAxWin3_Widget *AxWin3_Widget_GetRoot(tHWND Window)
124 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Window);
125 return &info->RootElement;
128 tAxWin3_Widget *AxWin3_Widget_AddWidget(tAxWin3_Widget *Parent, int Type, int Flags, const char *DebugName)
131 tWidgetWindowInfo *info;
134 if(!Parent) return NULL;
136 info = AxWin3_int_GetDataPtr(Parent->Window);
138 newID = AxWin3_Widget_int_AllocateID(info);
140 // Create new widget structure
141 ret = calloc(sizeof(tAxWin3_Widget), 1);
142 ret->Window = Parent->Window;
145 info->Elements[newID] = ret;
147 // Send create widget message
149 char tmp[sizeof(tWidgetIPC_Create)+1];
150 tWidgetIPC_Create *msg = (void*)tmp;
151 msg->Parent = Parent->ID;
155 msg->DebugName[0] = '\0';
156 AxWin3_SendIPC(ret->Window, IPC_WIDGET_CREATE, sizeof(tmp), tmp);
162 tAxWin3_Widget *AxWin3_Widget_AddWidget_SubWindow(tAxWin3_Widget *Parent, tHWND Window, int Flags, const char *DbgName)
164 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Parent->Window);
165 int newID = AxWin3_Widget_int_AllocateID(info);
167 tAxWin3_Widget *ret = calloc(sizeof(tAxWin3_Widget), 1);
168 ret->Window = Parent->Window;
170 info->Elements[newID] = ret;
174 char tmp[sizeof(tWidgetIPC_CreateSubWin)+1];
175 tWidgetIPC_CreateSubWin *msg = (void*)tmp;
176 msg->Parent = Parent->ID;
178 msg->Type = ELETYPE_SUBWIN;
179 msg->Flags = Flags; // TODO: Flags
180 msg->WindowHandle = AxWin3_int_GetWindowID(Window);
181 msg->DebugName[0] = '\0';
182 AxWin3_SendIPC(ret->Window, IPC_WIDGET_CREATESUBWIN, sizeof(tmp), tmp);
189 void AxWin3_Widget_DelWidget(tAxWin3_Widget *Widget)
191 tWidgetIPC_Delete msg;
192 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Widget->Window);
194 msg.WidgetID = Widget->ID;
195 AxWin3_SendIPC(Widget->Window, IPC_WIDGET_DELETE, sizeof(msg), &msg);
197 info->Elements[Widget->ID] = NULL;
198 if(Widget->ID < info->FirstFreeID)
199 info->FirstFreeID = Widget->ID;
204 void AxWin3_Widget_SetFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_FireCb Callback)
207 Widget->Fire = Callback;
210 void AxWin3_Widget_SetKeyHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyUpDownCb Callback)
213 Widget->KeyUpDown = Callback;
216 void AxWin3_Widget_SetKeyFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyFireCb Callback)
219 Widget->KeyFire = Callback;
222 void AxWin3_Widget_SetMouseMoveHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseMoveCb Callback)
225 Widget->MouseMove = Callback;
228 void AxWin3_Widget_SetMouseButtonHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseBtnCb Callback)
231 Widget->MouseButton = Callback;
235 void AxWin3_Widget_SetFlags(tAxWin3_Widget *Widget, int FlagSet, int FlagMask)
237 tWidgetIPC_SetFlags msg;
238 msg.WidgetID = Widget->ID;
242 AxWin3_SendIPC(Widget->Window, IPC_WIDGET_SETFLAGS, sizeof(msg), &msg);
245 void AxWin3_Widget_SetSize(tAxWin3_Widget *Widget, int Size)
247 tWidgetIPC_SetSize msg;
249 msg.WidgetID = Widget->ID;
251 AxWin3_SendIPC(Widget->Window, IPC_WIDGET_SETSIZE, sizeof(msg), &msg);
254 void AxWin3_Widget_SetText(tAxWin3_Widget *Widget, const char *Text)
256 char buf[sizeof(tWidgetIPC_SetText) + strlen(Text) + 1];
257 tWidgetIPC_SetText *msg = (void*)buf;
259 msg->WidgetID = Widget->ID;
260 strcpy(msg->Text, Text);
262 AxWin3_SendIPC(Widget->Window, IPC_WIDGET_SETTEXT, sizeof(buf), buf);
265 char *AxWin3_Widget_GetText(tAxWin3_Widget *Widget)
267 char buf[sizeof(tWidgetIPC_SetText)];
268 tWidgetIPC_SetText *msg = (void*)buf;
271 msg->WidgetID = Widget->ID;
273 AxWin3_SendIPC(Widget->Window, IPC_WIDGET_GETTEXT, sizeof(buf), buf);
275 msg = AxWin3_WaitIPCMessage(Widget->Window, IPC_WIDGET_GETTEXT, &retmsg_size);
276 if( retmsg_size < sizeof(*msg) ) {
281 char *ret = strndup(msg->Text, retmsg_size - sizeof(*msg));
286 void AxWin3_Widget_SetColour(tAxWin3_Widget *Widget, int Index, tAxWin3_Colour Colour)
288 tWidgetIPC_SetColour msg;
290 msg.WidgetID = Widget->ID;
293 AxWin3_SendIPC(Widget->Window, IPC_WIDGET_SETCOLOUR, sizeof(msg), &msg);