0e39a049826169c2db35c0edacaea84c2fcca413
[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         // TODO: Atomicity
84         for( newID = info->FirstFreeID; newID < info->nElements; newID ++ )
85         {
86                 if( info->Elements[newID] == NULL )
87                         break;
88         }
89         if( info->nElements == 0 || info->Elements[newID] )
90         {
91                 info->nElements ++;
92                 info->Elements = realloc(info->Elements, sizeof(*info->Elements)*info->nElements);
93                 newID = info->nElements - 1;
94         }
95         info->Elements[newID] = (void*)-1;
96         
97         // Create new widget structure
98         ret = malloc(sizeof(tAxWin3_Widget));
99         ret->Window = Parent->Window;
100         ret->ID = newID;
101         ret->Callback = NULL;
102
103         info->Elements[newID] = ret;
104
105         // Send create widget message
106         {
107                 char    tmp[sizeof(tWidgetMsg_Create)+1];
108                 tWidgetMsg_Create       *msg = (void*)tmp;
109                 msg->Parent = Parent->ID;
110                 msg->NewID = newID;
111                 msg->DebugName[0] = '\0';
112                 AxWin3_SendMessage(ret->Window, ret->Window, MSG_WIDGET_CREATE, sizeof(tmp), tmp);
113         }
114
115         return ret;
116 }
117
118 void AxWin3_Widget_DelWidget(tAxWin3_Widget *Widget)
119 {
120         tWidgetMsg_Delete       msg;
121         tWidgetWindowInfo       *info = AxWin3_int_GetDataPtr(Widget->Window);
122         
123         msg.WidgetID = Widget->ID;
124         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_DELETE, sizeof(msg), &msg);
125         
126         info->Elements[Widget->ID] = NULL;
127         if(Widget->ID < info->FirstFreeID)
128                 info->FirstFreeID = Widget->ID;
129         free(Widget);
130 }
131
132 void AxWin3_Widget_SetSize(tAxWin3_Widget *Widget, int Size)
133 {
134         tWidgetMsg_SetSize      msg;
135         
136         msg.WidgetID = Widget->ID;
137         msg.Value = Size;
138         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETSIZE, sizeof(msg), &msg);
139 }
140
141 void AxWin3_Widget_SetText(tAxWin3_Widget *Widget, const char *Text)
142 {
143         char    buf[sizeof(tWidgetMsg_SetText) + strlen(Text) + 1];
144         tWidgetMsg_SetText      *msg = (void*)buf;
145         
146         msg->WidgetID = Widget->ID;
147         strcpy(msg->Text, Text);
148         
149         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETTEXT, sizeof(buf), buf);
150 }
151
152 void AxWin3_Widget_SetColour(tAxWin3_Widget *Widget, int Index, tAxWin3_Colour Colour)
153 {
154         tWidgetMsg_SetColour    msg;
155         
156         msg.WidgetID = Widget->ID;
157         msg.Index = Index;
158         msg.Colour = Colour;
159         AxWin3_SendMessage(Widget->Window, Widget->Window, MSG_WIDGET_SETCOLOUR, sizeof(msg), &msg);
160 }

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