Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/ui/ui-drv/win32/winmenu.c
blob: d04f941232a58fd65f0edb645f53a6e579e2f723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <config.h>
/*#include <filter.h>
   #include <ui_helper.h> */
#include <xerror.h>
#include <ui.h>
#include <xmenu.h>
#include "ui_win32.h"
#define IDMUL 64
#define MAIN 0
#define POPUP 1
#define WSUBMENU 2
struct menurecord {
    int type;
    int id;
    struct uih_context *c;
    CONST menuitem *item;
    struct menurecord *nextrecord;
    struct menurecord *prevrecord;
    HMENU menu;
    struct menurecord *submenu;
    struct menurecord *nextsubmenu;
} *firstrecord;
struct menurecord rootmenu;

static void win32_appendmenu(struct uih_context *c,
			     struct menurecord *menu);
HMENU win32_createrootmenu()
{
    /*printf("Createrootmenu\n"); */
    rootmenu.id = 0;
    rootmenu.menu = CreateMenu();
    rootmenu.nextrecord = firstrecord;
    rootmenu.prevrecord = NULL;
    firstrecord = &rootmenu;
    rootmenu.nextsubmenu = NULL;
    rootmenu.submenu = NULL;
    /*printf("Createrootmenu OK\n"); */
    return rootmenu.menu;
};

static struct menurecord *win32_createmenu(struct uih_context *c,
					   CONST char *name, int type)
{
    struct menurecord *m = calloc(sizeof(*m), 1);
    static int id = 1;
    /*printf("Createmenu %s\n",name); */
    m->id = id++;
    m->type = type;
    if (type != POPUP)
	m->menu = CreateMenu();
    else
	m->menu = CreatePopupMenu();
    m->c = c;
    m->item = menu_findcommand(name);
    m->nextrecord = firstrecord;
    m->prevrecord = NULL;
    firstrecord = m;
    m->nextsubmenu = NULL;
    m->submenu = NULL;
    win32_appendmenu(c, m);
    /*printf("Createmenu OK\n"); */
    return m;
};

static void
win32_appendmenu(struct uih_context *c, struct menurecord *menu)
{
    int i, y, hotkeyed;
    CONST menuitem *item;
    char out[256];
    char used[256];
    memset(used, 0, 256);
    /*printf("Appendmenu %s\n",menu->item->shortname); */
    for (i = 0; (item = menu_item(menu->item->shortname, i)) != NULL; i++) {
	struct menurecord *submenu = NULL;
	int flags = MF_ENABLED | MF_STRING;
	if (item->type == MENU_SEPARATOR)
	    flags |= MF_SEPARATOR;
	if (item->type == MENU_SUBMENU) {
	    submenu = win32_createmenu(c, item->shortname, WSUBMENU);
	    flags |= MF_POPUP;
	    submenu->nextsubmenu = menu->submenu;
	    menu->submenu = submenu;
	}
	if (item->flags & (MENUFLAG_RADIO | MENUFLAG_CHECKBOX) &&
	    menu_enabled(item, c))
	    flags |= MF_CHECKED;
	/*printf("   %s\n",item->name); */

	hotkeyed = 0;
	for (y = 0; item->name[y]; y++) {
	    if (!hotkeyed) {
		unsigned char c = tolower(item->name[y]);
		if (((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))
		    && !used[c])
		    used[c] = 1, hotkeyed = 1, out[y] = '&';
	    }
	    out[y + hotkeyed] = item->name[y];
	}
	out[y + hotkeyed] = 0;
	if (item->type == MENU_CUSTOMDIALOG || item->type == MENU_DIALOG)
	    strcat(out, "...");
	if (menu->type != MAIN && item->key) {
	    strcat(out, "\t");
	    strcat(out, item->key);
	}
	AppendMenu(menu->menu, flags,
		   (submenu !=
		    NULL ? (UINT) submenu->menu : (UINT) (menu->id *
							  IDMUL + i)),
		   out);

    }
    /*printf("Appendmenu OK\n"); */
}

static void win32_freestructures(struct menurecord *menu)
{
    struct menurecord *sub, *nextsub;
    /*printf("Freestructures\n"); */
    sub = menu->submenu;
    while (sub != NULL) {
	nextsub = sub->nextsubmenu;
	win32_freestructures(sub);
	sub = nextsub;
    }
    if (menu->nextrecord)
	menu->nextrecord->prevrecord = menu->prevrecord;
    if (menu->prevrecord)
	menu->prevrecord->nextrecord = menu->nextrecord;
    else
	firstrecord = menu->nextrecord;
    free(menu);
}

void win32_pressed(int id)
{
    struct menurecord *menu = firstrecord;
    /*printf("Pressed\n"); */
    while (menu) {
	if (id / IDMUL == menu->id) {
	    ui_menuactivate(menu_item(menu->item->shortname, id % IDMUL),
			    NULL);
	    return;
	}
	menu = menu->nextrecord;
    }
    x_error("Unknown menu");
}

/* first destroy the old contents */
void win32_dorootmenu(struct uih_context *uih, CONST char *name)
{
    struct menurecord *sub, *nextsub;
    /*printf("dorootmenu %s\n",name); */
    /* Delete the old entries */
    while (DeleteMenu(rootmenu.menu, 0, MF_BYPOSITION));
    sub = rootmenu.submenu;
    rootmenu.c = uih;
    while (sub != NULL) {
	nextsub = sub->nextsubmenu;
	win32_freestructures(sub);
	sub = nextsub;
    }
    rootmenu.item = menu_findcommand(name);
    rootmenu.submenu = NULL;
    win32_appendmenu(uih, &rootmenu);
    SetMenu(hWnd, rootmenu.menu);
    /*printf("OK\n"); */
}

/* Check all created menus to see if there is the changed item and do changes
   as neccesary.

   This implementation is rather ugly and slow. Try if it is fast enought
   and change it otherwise */
void win32_enabledisable(struct uih_context *uih, CONST char *name)
{
    CONST struct menuitem *chgitem = menu_findcommand(name);
    CONST struct menuitem *item;
    struct menurecord *menu = firstrecord;
    int i;
    int checked = menu_enabled(chgitem, uih);
    while (menu) {
	if (menu->item != NULL)
	    for (i = 0;
		 (item = menu_item(menu->item->shortname, i)) != NULL;
		 i++) {
		if (item == chgitem) {
		    if (chgitem->flags & MENUFLAG_RADIO && checked) {
			int y;
			for (y = 0;
			     (item =
			      menu_item(menu->item->shortname, y)) != NULL;
			     y++)
			    if (item->flags & MENUFLAG_RADIO)
				CheckMenuItem(menu->menu,
					      menu->id * IDMUL + y,
					      MF_BYCOMMAND | MF_UNCHECKED);
		    }
		    CheckMenuItem(menu->menu, menu->id * IDMUL + i,
				  MF_BYCOMMAND | (checked ? MF_CHECKED :
						  MF_UNCHECKED));
		}
	    }
	menu = menu->nextrecord;
    }
}

void win32_menu(struct uih_context *c, CONST char *name)
{

    POINT p;
    struct menurecord *m = firstrecord;

    /*Delete records about all popups, since they are closed now */
    while (m) {
	if (m->type == POPUP) {
	    DestroyMenu(m->menu), win32_freestructures(m);
	    break;
	}
	m = m->nextrecord;
    }
    m = win32_createmenu(c, name, POPUP);
    GetCursorPos(&p);
    /*printf("menu %s %i %i\n",name,p.x,p.y); */
    TrackPopupMenu(m->menu, 0, p.x, p.y, 0, hWnd, 0);
}

void win32_uninitializewindows()
{
    while (firstrecord) {
	struct menurecord *r = firstrecord;
	while (r->type == WSUBMENU)
	    r = r->nextrecord;
	DestroyMenu(r->menu);
	win32_freestructures(r);
    }
}