Usermode/AxWin3 - Cleaning up bugs, adding image loading and text printing
[tpg/acess2.git] / Usermode / Applications / axwin3_src / libaxwin3.so_src / r_widget.c
1 /*
2  * AxWin3 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Entrypoint and setup
7  */
8 #include <axwin3/axwin.h>
9 #include <axwin3/widget.h>
10 #include "include/internal.h"
11 #include <stdlib.h>
12 #include <widget_messages.h>
13 #include <string.h>
14
15 // === STRUCTURES ===
16 struct sAxWin3_Widget
17 {
18         tHWND   Window;
19         uint32_t        ID;
20         tAxWin3_Widget_Callback Callback;
21 };
22
23 typedef struct
24 {
25          int    nElements;
26         tAxWin3_Widget  **Elements;
27          int    FirstFreeID;
28         tAxWin3_Widget  RootElement;
29         // Callbacks for each element
30 } tWidgetWindowInfo;
31
32 // === CODE ===
33 int AxWin3_Widget_MessageHandler(tHWND Window, int Size, void *Data)
34 {
35         return 0;
36 }
37
38 tHWND AxWin3_Widget_CreateWindow(tHWND Parent, int W, int H, int RootEleFlags)
39 {
40         tHWND   ret;
41         tWidgetWindowInfo       *info;
42         
43         ret = AxWin3_CreateWindow(
44                 Parent, "Widget", RootEleFlags,
45                 sizeof(*info) + sizeof(tAxWin3_Widget), AxWin3_Widget_MessageHandler
46                 );
47         info = AxWin3_int_GetDataPtr(ret);
48
49         info->RootElement.Window = ret;
50         info->RootElement.ID = -1;
51
52         AxWin3_ResizeWindow(ret, W, H);
53         
54         return ret;
55 }
56
57 void AxWin3_Widget_DestroyWindow(tHWND Window)
58 {
59         // Free all element structures
60         
61         // Free array
62         
63         // Request window to be destroyed (will clean up any data stored in tHWND)
64 }
65
66 tAxWin3_Widget *AxWin3_Widget_GetRoot(tHWND Window)
67 {
68         tWidgetWindowInfo       *info = AxWin3_int_GetDataPtr(Window);
69         return &info->RootElement;
70 }
71
72 tAxWin3_Widget *AxWin3_Widget_AddWidget(tAxWin3_Widget *Parent, int Type, int Flags, const char *DebugName)
73 {
74          int    newID;
75         tWidgetWindowInfo       *info;
76         tAxWin3_Widget  *ret;
77
78         if(!Parent)     return NULL;
79
80         info = AxWin3_int_GetDataPtr(Parent->Window);
81         
82         // Assign ID
83         // BUG BUG BUG - Double Allocations!
84         // TODO: Atomicity
85         for( newID = info->FirstFreeID; newID < info->nElements; newID ++ )
86         {
87                 if( info->Elements[newID] == NULL )
88                         break;
89         }
90         if( newID == info->nElements )
91         {
92                 info->nElements ++;
93                 info->Elements = realloc(info->Elements, sizeof(*info->Elements)*info->nElements);
94                 newID = info->nElements - 1;
95                 _SysDebug("Expanded and allocated %i", newID);
96         }
97         else
98                 _SysDebug("Allocated %i", newID);
99         info->Elements[newID] = (void*)-1;
100         
101         // Create new widget structure
102         ret = malloc(sizeof(tAxWin3_Widget));
103         ret->Window = Parent->Window;
104         ret->ID = newID;
105         ret->Callback = NULL;
106
107         info->Elements[newID] = ret;
108
109         // Send create widget message
110         {
111                 char    tmp[sizeof(tWidgetMsg_Create)+1];
112                 tWidgetMsg_Create       *msg = (void*)tmp;
113                 msg->Parent = Parent->ID;
114                 msg->NewID = newID;
115                 msg->Type = Type;
116                 msg->Flags = Flags;
117                 msg->DebugName[0] = '\0';
118                 AxWin3_SendMessage(ret->Window, ret->Window, MSG_WIDGET_CREATE, sizeof(tmp), tmp);
119         }
120
121         return ret;
122 }
123
124 void AxWin3_Widget_DelWidget(tAxWin3_Widget *Widget)
125 {
126         tWidgetMsg_Delete       msg;
127         tWidgetWindowInfo       *info = AxWin3_int_GetDataPtr(Widget->Window);
128         
129         msg.WidgetID = Widget->ID;
130         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_DELETE, sizeof(msg), &msg);
131         
132         info->Elements[Widget->ID] = NULL;
133         if(Widget->ID < info->FirstFreeID)
134                 info->FirstFreeID = Widget->ID;
135         free(Widget);
136 }
137
138 void AxWin3_Widget_SetFlags(tAxWin3_Widget *Widget, int FlagSet, int FlagMask)
139 {
140         tWidgetMsg_SetFlags     msg;
141         msg.WidgetID = Widget->ID;
142         msg.Value = FlagSet;
143         msg.Mask = FlagMask;
144         
145         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETFLAGS, sizeof(msg), &msg);
146 }
147
148 void AxWin3_Widget_SetSize(tAxWin3_Widget *Widget, int Size)
149 {
150         tWidgetMsg_SetSize      msg;
151         
152         msg.WidgetID = Widget->ID;
153         msg.Value = Size;
154         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETSIZE, sizeof(msg), &msg);
155 }
156
157 void AxWin3_Widget_SetText(tAxWin3_Widget *Widget, const char *Text)
158 {
159         char    buf[sizeof(tWidgetMsg_SetText) + strlen(Text) + 1];
160         tWidgetMsg_SetText      *msg = (void*)buf;
161         
162         msg->WidgetID = Widget->ID;
163         strcpy(msg->Text, Text);
164         
165         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETTEXT, sizeof(buf), buf);
166 }
167
168 void AxWin3_Widget_SetColour(tAxWin3_Widget *Widget, int Index, tAxWin3_Colour Colour)
169 {
170         tWidgetMsg_SetColour    msg;
171         
172         msg.WidgetID = Widget->ID;
173         msg.Index = Index;
174         msg.Colour = Colour;
175         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETCOLOUR, sizeof(msg), &msg);
176 }

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