Usermode/init - Fixed early free of command, fixed %s on array
[tpg/acess2.git] / Usermode / Applications / axwin3_src / libaxwin3.so_src / r_menu.c
1 /*
2  * AxWin3 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * r_menu.c
6  * - Menu renderer interface
7  */
8 #include <stdlib.h>
9 #include <axwin3/axwin.h>
10 #include <axwin3/menu.h>
11 #include <menu_messages.h>
12 #include "include/internal.h"
13 #include <string.h>
14
15 // === TYPES ===
16 typedef struct sMenuWindowInfo  tMenuWindowInfo;
17
18 struct sAxWin3_MenuItem
19 {
20         tHWND   Window;
21          int    ID;
22         tAxWin3_Menu_Callback   Callback;
23         void    *CbPtr;
24         tHWND   SubMenu;
25 };
26
27 struct sMenuWindowInfo
28 {
29          int    nItems;
30         tAxWin3_MenuItem        *Items;
31 };
32
33 // === CODE ===
34 int AxWin3_Menu_int_Callback(tHWND Window, int Message, int Length, void *Data)
35 {
36         tMenuWindowInfo *info = AxWin3_int_GetDataPtr(Window);
37         switch(Message)
38         {
39         case MSG_MENU_SELECT: {
40                 tMenuMsg_Select *msg = Data;
41                 tAxWin3_MenuItem        *item;
42                 if(Length < sizeof(tMenuMsg_Select))    return -1;
43                 if(msg->ID >= info->nItems)     return -1;
44                 item = &info->Items[msg->ID];
45                 if(item->Callback)      item->Callback(item->CbPtr);
46                 return 1; }
47         }
48         return 0;
49 }
50
51 tHWND AxWin3_Menu_Create(tHWND Parent)
52 {
53         tHWND   ret;
54         tMenuWindowInfo *info;
55         
56         ret = AxWin3_CreateWindow(Parent, "Menu", 0, sizeof(*info), AxWin3_Menu_int_Callback);
57         if(!ret)        return ret;
58         
59         info = AxWin3_int_GetDataPtr(ret);
60         info->nItems = 0;
61         info->Items = NULL;
62         
63         return ret;
64 }
65
66 void AxWin3_Menu_ShowAt(tHWND Menu, int X, int Y)
67 {
68         AxWin3_MoveWindow(Menu, X, Y);
69         AxWin3_ShowWindow(Menu, 1);
70         AxWin3_FocusWindow(Menu);
71 }
72
73 tAxWin3_MenuItem *AxWin3_Menu_AddItem(
74         tHWND Menu, const char *Label,
75         tAxWin3_Menu_Callback Cb, void *Ptr,
76         int Flags, tHWND SubMenu
77         )
78 {
79         tMenuWindowInfo *info;
80         tAxWin3_MenuItem        *ret;
81         
82         info = AxWin3_int_GetDataPtr(Menu);
83         
84         info->nItems ++;
85         info->Items = realloc(info->Items, sizeof(*info->Items)*info->nItems);
86         if(!info->Items) {
87                 _SysDebug("ERROR: Realloc Failed");
88                 return NULL;
89         }
90         
91         ret = &info->Items[info->nItems-1];
92         ret->ID = info->nItems - 1;
93         ret->Window = Menu;
94         ret->Callback = Cb;
95         ret->CbPtr = Ptr;
96         ret->SubMenu = SubMenu; 
97
98         {
99                 tMenuIPC_AddItem        *req;
100                  int    data_size;
101                 if(!Label)      Label = "";
102                 data_size = sizeof(*req)+strlen(Label)+1;
103                 req = malloc(data_size);
104                 req->ID = ret->ID;
105                 req->Flags = Flags;
106                 req->SubMenuID = AxWin3_int_GetWindowID(SubMenu);
107                 strcpy(req->Label, Label);
108                 AxWin3_SendIPC(Menu, IPC_MENU_ADDITEM, data_size, req);
109                 free(req);
110         }
111         
112         return ret;
113 }

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