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"
11 #include "include/ipc.h"
13 #include <widget_messages.h>
22 tAxWin3_Widget_FireCb Fire;
23 tAxWin3_Widget_KeyUpDownCb KeyUpDown;
24 tAxWin3_Widget_KeyFireCb KeyFire;
25 tAxWin3_Widget_MouseMoveCb MouseMove;
26 tAxWin3_Widget_MouseBtnCb MouseButton;
32 tAxWin3_Widget **Elements;
34 tAxWin3_Widget RootElement;
35 // Callbacks for each element
39 tAxWin3_Widget *AxWin3_Widget_int_GetElementByID(tHWND Window, uint32_t ID)
41 tWidgetWindowInfo *info;
42 if(!Window) return NULL;
44 info = AxWin3_int_GetDataPtr(Window);
45 if(ID >= info->nElements) return NULL;
47 return info->Elements[ID];
50 int AxWin3_Widget_MessageHandler(tHWND Window, int MessageID, int Size, void *Data)
52 tAxWin3_Widget *widget;
56 case MSG_WIDGET_FIRE: {
57 tWidgetMsg_Fire *msg = Data;
58 if(Size < sizeof(*msg)) return -1;
59 widget = AxWin3_Widget_int_GetElementByID(Window, msg->WidgetID);
60 if(widget->Fire) widget->Fire(widget);
68 tHWND AxWin3_Widget_CreateWindow(tHWND Parent, int W, int H, int RootEleFlags)
71 tWidgetWindowInfo *info;
73 ret = AxWin3_CreateWindow(
74 Parent, "Widget", RootEleFlags,
75 sizeof(*info) + sizeof(tAxWin3_Widget), AxWin3_Widget_MessageHandler
77 info = AxWin3_int_GetDataPtr(ret);
79 info->RootElement.Window = ret;
80 info->RootElement.ID = -1;
82 AxWin3_ResizeWindow(ret, W, H);
87 void AxWin3_Widget_DestroyWindow(tHWND Window)
89 // Free all element structures
93 // Request window to be destroyed (will clean up any data stored in tHWND)
96 tAxWin3_Widget *AxWin3_Widget_GetRoot(tHWND Window)
98 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Window);
99 return &info->RootElement;
102 tAxWin3_Widget *AxWin3_Widget_AddWidget(tAxWin3_Widget *Parent, int Type, int Flags, const char *DebugName)
105 tWidgetWindowInfo *info;
108 if(!Parent) return NULL;
110 info = AxWin3_int_GetDataPtr(Parent->Window);
113 // BUG BUG BUG - Double Allocations!
115 for( newID = info->FirstFreeID; newID < info->nElements; newID ++ )
117 if( info->Elements[newID] == NULL )
120 if( newID == info->nElements )
122 const int size_step = 4;
123 info->nElements += 4;
124 info->Elements = realloc(info->Elements, sizeof(*info->Elements)*info->nElements);
125 newID = info->nElements - 4;
126 memset( &info->Elements[newID+1], 0, (size_step-1)*sizeof(info->Elements));
127 _SysDebug("Expanded to %i and allocated %i", info->nElements, newID);
130 _SysDebug("Allocated %i", newID);
131 info->Elements[newID] = (void*)-1;
133 // Create new widget structure
134 ret = calloc(sizeof(tAxWin3_Widget), 1);
135 ret->Window = Parent->Window;
138 info->Elements[newID] = ret;
140 // Send create widget message
142 char tmp[sizeof(tWidgetMsg_Create)+1];
143 tWidgetMsg_Create *msg = (void*)tmp;
144 msg->Parent = Parent->ID;
148 msg->DebugName[0] = '\0';
149 AxWin3_SendMessage(ret->Window, ret->Window, MSG_WIDGET_CREATE, sizeof(tmp), tmp);
155 void AxWin3_Widget_DelWidget(tAxWin3_Widget *Widget)
157 tWidgetMsg_Delete msg;
158 tWidgetWindowInfo *info = AxWin3_int_GetDataPtr(Widget->Window);
160 msg.WidgetID = Widget->ID;
161 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_DELETE, sizeof(msg), &msg);
163 info->Elements[Widget->ID] = NULL;
164 if(Widget->ID < info->FirstFreeID)
165 info->FirstFreeID = Widget->ID;
170 void AxWin3_Widget_SetFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_FireCb Callback)
173 Widget->Fire = Callback;
176 void AxWin3_Widget_SetKeyHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyUpDownCb Callback)
179 Widget->KeyUpDown = Callback;
182 void AxWin3_Widget_SetKeyFireHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_KeyFireCb Callback)
185 Widget->KeyFire = Callback;
188 void AxWin3_Widget_SetMouseMoveHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseMoveCb Callback)
191 Widget->MouseMove = Callback;
194 void AxWin3_Widget_SetMouseButtonHandler(tAxWin3_Widget *Widget, tAxWin3_Widget_MouseBtnCb Callback)
197 Widget->MouseButton = Callback;
201 void AxWin3_Widget_SetFlags(tAxWin3_Widget *Widget, int FlagSet, int FlagMask)
203 tWidgetMsg_SetFlags msg;
204 msg.WidgetID = Widget->ID;
208 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETFLAGS, sizeof(msg), &msg);
211 void AxWin3_Widget_SetSize(tAxWin3_Widget *Widget, int Size)
213 tWidgetMsg_SetSize msg;
215 msg.WidgetID = Widget->ID;
217 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETSIZE, sizeof(msg), &msg);
220 void AxWin3_Widget_SetText(tAxWin3_Widget *Widget, const char *Text)
222 char buf[sizeof(tWidgetMsg_SetText) + strlen(Text) + 1];
223 tWidgetMsg_SetText *msg = (void*)buf;
225 msg->WidgetID = Widget->ID;
226 strcpy(msg->Text, Text);
228 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETTEXT, sizeof(buf), buf);
231 char *AxWin3_Widget_GetText(tAxWin3_Widget *Widget)
233 char buf[sizeof(tWidgetMsg_SetText)];
234 tWidgetMsg_SetText *msg = (void*)buf;
235 tAxWin_IPCMessage *retmsg;
238 msg->WidgetID = Widget->ID;
240 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_GETTEXT, sizeof(buf), buf);
242 retmsg = AxWin3_int_WaitIPCMessage(MSG_WIDGET_GETTEXT);
243 msg = (void*)retmsg->Data;
245 if( retmsg->Size < sizeof(*msg) ) {
250 ret = strndup(msg->Text, retmsg->Size - sizeof(*msg));
255 void AxWin3_Widget_SetColour(tAxWin3_Widget *Widget, int Index, tAxWin3_Colour Colour)
257 tWidgetMsg_SetColour msg;
259 msg.WidgetID = Widget->ID;
262 AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETCOLOUR, sizeof(msg), &msg);