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 int AxWin3_Widget_MessageHandler(tHWND Window, int MessageID, int Size, void *Data)
51 tAxWin3_Widget *widget;
55 case MSG_WIDGET_FIRE: {
56 tWidgetMsg_Fire *msg = Data;
57 if(Size < sizeof(*msg)) return -1;
58 widget = AxWin3_Widget_int_GetElementByID(Window, msg->WidgetID);
59 if(widget->Fire) widget->Fire(widget);
67 tHWND AxWin3_Widget_CreateWindow(tHWND Parent, int W, int H, int RootEleFlags)
70 tWidgetWindowInfo *info;
72 ret = AxWin3_CreateWindow(
73 Parent, "Widget", RootEleFlags,
74 sizeof(*info) + sizeof(tAxWin3_Widget), AxWin3_Widget_MessageHandler
76 info = AxWin3_int_GetDataPtr(ret);
78 info->RootElement.Window = ret;
79 info->RootElement.ID = -1;
81 AxWin3_ResizeWindow(ret, W, H);
86 void AxWin3_Widget_DestroyWindow(tHWND Window)
88 // Free all element structures
92 // Request window to be destroyed (will clean up any data stored in tHWND)
95 tAxWin3_Widget *AxWin3_Widget_GetRoot(tHWND Window)
97 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Window);
98 return &info->RootElement;
101 tAxWin3_Widget *AxWin3_Widget_AddWidget(tAxWin3_Widget *Parent, int Type, int Flags, const char *DebugName)
104 tWidgetWindowInfo *info;
107 if(!Parent) return NULL;
109 info = AxWin3_int_GetDataPtr(Parent->Window);
112 // BUG BUG BUG - Double Allocations!
114 for( newID = info->FirstFreeID; newID < info->nElements; newID ++ )
116 if( info->Elements[newID] == NULL )
119 if( newID == info->nElements )
121 const int size_step = 4;
122 info->nElements += 4;
123 info->Elements = realloc(info->Elements, sizeof(*info->Elements)*info->nElements);
124 newID = info->nElements - 4;
125 memset( &info->Elements[newID+1], 0, (size_step-1)*sizeof(info->Elements));
126 _SysDebug("Expanded to %i and allocated %i", info->nElements, newID);
129 _SysDebug("Allocated %i", newID);
130 info->Elements[newID] = (void*)-1;
132 // Create new widget structure
133 ret = calloc(sizeof(tAxWin3_Widget), 1);
134 ret->Window = Parent->Window;
137 info->Elements[newID] = ret;
139 // Send create widget message
141 char tmp[sizeof(tWidgetMsg_Create)+1];
142 tWidgetMsg_Create *msg = (void*)tmp;
143 msg->Parent = Parent->ID;
147 msg->DebugName[0] = '\0';
148 AxWin3_SendMessage(ret->Window, ret->Window, MSG_WIDGET_CREATE, sizeof(tmp), tmp);
154 void AxWin3_Widget_DelWidget(tAxWin3_Widget *Widget)
156 tWidgetMsg_Delete msg;
157 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Widget->Window);
159 msg.WidgetID = Widget->ID;
160 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_DELETE, sizeof(msg), &msg);
162 info->Elements[Widget->ID] = NULL;
163 if(Widget->ID < info->FirstFreeID)
164 info->FirstFreeID = Widget->ID;
169 void AxWin3_Widget_SetFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_FireCb Callback)
172 Widget->Fire = Callback;
175 void AxWin3_Widget_SetKeyHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyUpDownCb Callback)
178 Widget->KeyUpDown = Callback;
181 void AxWin3_Widget_SetKeyFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyFireCb Callback)
184 Widget->KeyFire = Callback;
187 void AxWin3_Widget_SetMouseMoveHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseMoveCb Callback)
190 Widget->MouseMove = Callback;
193 void AxWin3_Widget_SetMouseButtonHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseBtnCb Callback)
196 Widget->MouseButton = Callback;
200 void AxWin3_Widget_SetFlags(tAxWin3_Widget *Widget, int FlagSet, int FlagMask)
202 tWidgetMsg_SetFlags msg;
203 msg.WidgetID = Widget->ID;
207 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETFLAGS, sizeof(msg), &msg);
210 void AxWin3_Widget_SetSize(tAxWin3_Widget *Widget, int Size)
212 tWidgetMsg_SetSize msg;
214 msg.WidgetID = Widget->ID;
216 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETSIZE, sizeof(msg), &msg);
219 void AxWin3_Widget_SetText(tAxWin3_Widget *Widget, const char *Text)
221 char buf[sizeof(tWidgetMsg_SetText) + strlen(Text) + 1];
222 tWidgetMsg_SetText *msg = (void*)buf;
224 msg->WidgetID = Widget->ID;
225 strcpy(msg->Text, Text);
227 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETTEXT, sizeof(buf), buf);
230 char *AxWin3_Widget_GetText(tAxWin3_Widget *Widget)
232 char buf[sizeof(tWidgetMsg_SetText)];
233 tWidgetMsg_SetText *msg = (void*)buf;
236 msg->WidgetID = Widget->ID;
238 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_GETTEXT, sizeof(buf), buf);
240 msg = AxWin3_WaitMessage(Widget->Window, MSG_WIDGET_GETTEXT, &retmsg_size);
241 if( retmsg_size < sizeof(*msg) ) {
246 char *ret = strndup(msg->Text, retmsg_size - sizeof(*msg));
251 void AxWin3_Widget_SetColour(tAxWin3_Widget *Widget, int Index, tAxWin3_Colour Colour)
253 tWidgetMsg_SetColour msg;
255 msg.WidgetID = Widget->ID;
258 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETCOLOUR, sizeof(msg), &msg);