2 * AxWin3 Interface Library
3 * - By John Hodge (thePowersGang)
6 * - Entrypoint and setup
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 )
122 info->Elements = realloc(info->Elements, sizeof(*info->Elements)*info->nElements);
123 newID = info->nElements - 1;
124 _SysDebug("Expanded and allocated %i", newID);
127 _SysDebug("Allocated %i", newID);
128 info->Elements[newID] = (void*)-1;
130 // Create new widget structure
131 ret = calloc(sizeof(tAxWin3_Widget), 1);
132 ret->Window = Parent->Window;
135 info->Elements[newID] = ret;
137 // Send create widget message
139 char tmp[sizeof(tWidgetMsg_Create)+1];
140 tWidgetMsg_Create *msg = (void*)tmp;
141 msg->Parent = Parent->ID;
145 msg->DebugName[0] = '\0';
146 AxWin3_SendMessage(ret->Window, ret->Window, MSG_WIDGET_CREATE, sizeof(tmp), tmp);
152 void AxWin3_Widget_DelWidget(tAxWin3_Widget *Widget)
154 tWidgetMsg_Delete msg;
155 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Widget->Window);
157 msg.WidgetID = Widget->ID;
158 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_DELETE, sizeof(msg), &msg);
160 info->Elements[Widget->ID] = NULL;
161 if(Widget->ID < info->FirstFreeID)
162 info->FirstFreeID = Widget->ID;
167 void AxWin3_Widget_SetFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_FireCb Callback)
170 Widget->Fire = Callback;
173 void AxWin3_Widget_SetKeyHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyUpDownCb Callback)
176 Widget->KeyUpDown = Callback;
179 void AxWin3_Widget_SetKeyFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyFireCb Callback)
182 Widget->KeyFire = Callback;
185 void AxWin3_Widget_SetMouseMoveHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseMoveCb Callback)
188 Widget->MouseMove = Callback;
191 void AxWin3_Widget_SetMouseButtonHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseBtnCb Callback)
194 Widget->MouseButton = Callback;
198 void AxWin3_Widget_SetFlags(tAxWin3_Widget *Widget, int FlagSet, int FlagMask)
200 tWidgetMsg_SetFlags msg;
201 msg.WidgetID = Widget->ID;
205 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETFLAGS, sizeof(msg), &msg);
208 void AxWin3_Widget_SetSize(tAxWin3_Widget *Widget, int Size)
210 tWidgetMsg_SetSize msg;
212 msg.WidgetID = Widget->ID;
214 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETSIZE, sizeof(msg), &msg);
217 void AxWin3_Widget_SetText(tAxWin3_Widget *Widget, const char *Text)
219 char buf[sizeof(tWidgetMsg_SetText) + strlen(Text) + 1];
220 tWidgetMsg_SetText *msg = (void*)buf;
222 msg->WidgetID = Widget->ID;
223 strcpy(msg->Text, Text);
225 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETTEXT, sizeof(buf), buf);
228 void AxWin3_Widget_SetColour(tAxWin3_Widget *Widget, int Index, tAxWin3_Colour Colour)
230 tWidgetMsg_SetColour msg;
232 msg.WidgetID = Widget->ID;
235 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETCOLOUR, sizeof(msg), &msg);