~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/windows/identity/uilib/action.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-10-30 10:28:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20061030102853-x7v876vw4af46v0m
Tags: 1.4.4-3ubuntu1
* Merge with Debian; only Ubuntu change:
  - src/include/k5-thread.h: Define__USE_GNU when #include'ing pthread.h to
    fix FTBFS (from 1.4.3-9ubuntu1).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2004 Massachusetts Institute of Technology
 
2
 * Copyright (c) 2005 Massachusetts Institute of Technology
3
3
 *
4
4
 * Permission is hereby granted, free of charge, to any person
5
5
 * obtaining a copy of this software and associated documentation
26
26
 
27
27
#define NOEXPORT
28
28
#include<khuidefs.h>
 
29
#include<utils.h>
29
30
#include<assert.h>
30
31
 
 
32
#include<strsafe.h>
 
33
 
31
34
khui_action_ref khui_main_menu[] = {
32
 
    MENU_ACTION(KHUI_MENU_FILE),
33
 
    MENU_ACTION(KHUI_MENU_CRED),
34
 
    MENU_ACTION(KHUI_MENU_VIEW),
35
 
    MENU_ACTION(KHUI_MENU_OPTIONS),
36
 
    MENU_ACTION(KHUI_MENU_HELP),
 
35
    MENU_SUBMENU(KHUI_MENU_FILE),
 
36
    MENU_SUBMENU(KHUI_MENU_CRED),
 
37
    MENU_SUBMENU(KHUI_MENU_VIEW),
 
38
    MENU_SUBMENU(KHUI_MENU_OPTIONS),
 
39
    MENU_SUBMENU(KHUI_MENU_HELP),
37
40
    MENU_END()
38
41
};
39
42
 
48
51
    MENU_ACTION(KHUI_ACTION_NEW_CRED),
49
52
    MENU_SEP(),
50
53
    MENU_ACTION(KHUI_ACTION_RENEW_CRED),
 
54
    MENU_ACTION(KHUI_ACTION_IMPORT),
51
55
    MENU_ACTION(KHUI_ACTION_DESTROY_CRED),
52
56
    MENU_SEP(),
53
57
    MENU_ACTION(KHUI_ACTION_SET_DEF_ID),
 
58
#if 0
 
59
    /* not implemented yet */
54
60
    MENU_ACTION(KHUI_ACTION_SET_SRCH_ID),
 
61
#endif
55
62
    MENU_SEP(),
56
63
    MENU_ACTION(KHUI_ACTION_PASSWD_ID),
57
 
    MENU_SEP(),
58
 
    MENU_ACTION(KHUI_ACTION_IMPORT),
59
64
    MENU_END()
60
65
};
61
66
 
63
68
    MENU_ACTION(KHUI_ACTION_LAYOUT_ID),
64
69
    MENU_ACTION(KHUI_ACTION_LAYOUT_TYPE),
65
70
    MENU_ACTION(KHUI_ACTION_LAYOUT_LOC),
 
71
    MENU_ACTION(KHUI_ACTION_LAYOUT_CUST),
66
72
    MENU_END()
67
73
};
68
74
 
72
78
};
73
79
 
74
80
khui_action_ref khui_menu_view[] = {
75
 
    MENU_ACTION(KHUI_ACTION_CHOOSE_COLS),
76
 
    MENU_ACTION(KHUI_MENU_LAYOUT),
77
 
    MENU_ACTION(KHUI_MENU_TOOLBARS),
 
81
    MENU_SUBMENU(KHUI_MENU_COLUMNS),
 
82
    MENU_SUBMENU(KHUI_MENU_LAYOUT),
 
83
#if 0
 
84
    /* not implemented yet */
 
85
    MENU_SUBMENU(KHUI_MENU_TOOLBARS),
 
86
#endif
78
87
    MENU_SEP(),
 
88
#if 0
 
89
    /* not implemented yet */
79
90
    MENU_ACTION(KHUI_ACTION_DEBUG_WINDOW),
80
91
    MENU_SEP(),
 
92
#endif
81
93
    MENU_ACTION(KHUI_ACTION_VIEW_REFRESH),
82
94
    MENU_END()
83
95
};
84
96
 
85
97
khui_action_ref khui_menu_options[] = {
86
98
    MENU_ACTION(KHUI_ACTION_OPT_KHIM),
 
99
    MENU_ACTION(KHUI_ACTION_OPT_APPEAR),
87
100
    MENU_ACTION(KHUI_ACTION_OPT_IDENTS),
88
101
    MENU_ACTION(KHUI_ACTION_OPT_NOTIF),
 
102
    MENU_ACTION(KHUI_ACTION_OPT_PLUGINS),
 
103
    MENU_SEP(),
89
104
    MENU_END()
90
105
};
91
106
 
156
171
    MENU_END()
157
172
};
158
173
 
 
174
khui_action_ref khui_menu_cwheader_ctx[] = {
 
175
    MENU_SUBMENU(KHUI_MENU_COLUMNS),
 
176
    MENU_SUBMENU(KHUI_MENU_LAYOUT),
 
177
    MENU_END()
 
178
};
 
179
 
 
180
khui_action_ref khui_menu_columns[] = {
 
181
    MENU_END()
 
182
};
 
183
 
159
184
khui_action_ref khui_pmenu_tok_sel[] = {
160
185
    MENU_ACTION(KHUI_ACTION_RENEW_CRED),
161
186
    MENU_ACTION(KHUI_ACTION_DESTROY_CRED),
170
195
 
171
196
/* all stock menus and toolbars */
172
197
khui_menu_def khui_all_menus[] = {
173
 
    CONSTMENU(KHUI_MENU_MAIN, KHUI_MENUSTATE_CONSTANT, khui_main_menu),
174
 
    CONSTMENU(KHUI_MENU_FILE, KHUI_MENUSTATE_CONSTANT, khui_menu_file),
175
 
    CONSTMENU(KHUI_MENU_CRED, KHUI_MENUSTATE_CONSTANT, khui_menu_cred),
176
 
    CONSTMENU(KHUI_MENU_VIEW, KHUI_MENUSTATE_CONSTANT, khui_menu_view),
177
 
    CONSTMENU(KHUI_MENU_LAYOUT, KHUI_MENUSTATE_CONSTANT, khui_menu_layout),
178
 
    CONSTMENU(KHUI_MENU_TOOLBARS, KHUI_MENUSTATE_CONSTANT, khui_menu_toolbars),
179
 
    CONSTMENU(KHUI_MENU_OPTIONS, KHUI_MENUSTATE_CONSTANT, khui_menu_options),
180
 
    CONSTMENU(KHUI_MENU_HELP, KHUI_MENUSTATE_CONSTANT, khui_menu_help),
 
198
    CONSTMENU(KHUI_MENU_MAIN, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_main_menu),
 
199
    CONSTMENU(KHUI_MENU_FILE, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_file),
 
200
    CONSTMENU(KHUI_MENU_CRED, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_cred),
 
201
    CONSTMENU(KHUI_MENU_VIEW, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_view),
 
202
    CONSTMENU(KHUI_MENU_LAYOUT, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_layout),
 
203
    CONSTMENU(KHUI_MENU_TOOLBARS, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_toolbars),
 
204
    CONSTMENU(KHUI_MENU_OPTIONS, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_options),
 
205
    CONSTMENU(KHUI_MENU_HELP, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_help),
 
206
    CONSTMENU(KHUI_MENU_COLUMNS, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_menu_columns),
181
207
 
182
208
    /* toolbars */
183
 
    CONSTMENU(KHUI_TOOLBAR_STANDARD, KHUI_MENUSTATE_CONSTANT, khui_toolbar_standard),
 
209
    CONSTMENU(KHUI_TOOLBAR_STANDARD, KHUI_MENUSTATE_CONSTANT | KHUI_MENUSTATE_SYSTEM, khui_toolbar_standard),
184
210
 
185
211
    /* context menus */
186
212
    CONSTMENU(KHUI_MENU_IDENT_CTX, KHUI_MENUSTATE_CONSTANT, khui_menu_ident_ctx),
187
213
    CONSTMENU(KHUI_MENU_TOK_CTX, KHUI_MENUSTATE_CONSTANT, khui_menu_tok_ctx),
188
214
    CONSTMENU(KHUI_MENU_ICO_CTX_MIN, KHUI_MENUSTATE_CONSTANT, khui_menu_ico_ctx_min),
189
215
    CONSTMENU(KHUI_MENU_ICO_CTX_NORMAL, KHUI_MENUSTATE_CONSTANT, khui_menu_ico_ctx_normal),
 
216
    CONSTMENU(KHUI_MENU_CWHEADER_CTX, KHUI_MENUSTATE_CONSTANT, khui_menu_cwheader_ctx),
190
217
 
191
218
    /* pseudo menus */
192
219
    CONSTMENU(KHUI_PMENU_TOK_SEL, KHUI_MENUSTATE_CONSTANT, khui_pmenu_tok_sel),
194
221
};
195
222
 
196
223
int khui_n_all_menus = sizeof(khui_all_menus) / sizeof(khui_menu_def);
 
224
khui_menu_def ** khui_cust_menus = NULL;
 
225
int khui_nc_cust_menus = 0;
 
226
int khui_n_cust_menus = 0;
197
227
CRITICAL_SECTION cs_actions;
198
228
 
 
229
#define CACT_NC_ALLOC 32
 
230
 
 
231
khui_action ** khui_cust_actions = NULL;
 
232
int khui_nc_cust_actions = 0;
 
233
int khui_n_cust_actions = 0;
 
234
 
 
235
HWND khui_hwnd_main;                    /* main window, for notifying
 
236
                                           action launches and
 
237
                                           dispatching messages to the
 
238
                                           application. */
 
239
 
199
240
KHMEXP void KHMAPI 
200
241
khui_init_actions(void) {
201
242
    InitializeCriticalSection(&cs_actions);
206
247
    DeleteCriticalSection(&cs_actions);
207
248
}
208
249
 
 
250
KHMEXP khm_int32 KHMAPI
 
251
khui_action_create(const wchar_t * name,
 
252
                   const wchar_t * caption,
 
253
                   const wchar_t * tooltip,
 
254
                   void * userdata,
 
255
                   khm_int32 type,
 
256
                   khm_handle hsub) {
 
257
    khui_action * act;
 
258
    khm_int32 action = 0;
 
259
    int i;
 
260
    size_t s;
 
261
 
 
262
    if (!caption ||
 
263
        FAILED(StringCchLength(caption, KHUI_MAXCCH_SHORT_DESC, &s)) ||
 
264
        (tooltip && FAILED(StringCchLength(tooltip, KHUI_MAXCCH_SHORT_DESC, &s))) ||
 
265
        (type != KHUI_ACTIONTYPE_TRIGGER && type != KHUI_ACTIONTYPE_TOGGLE)) {
 
266
        return 0;
 
267
    }
 
268
 
 
269
    EnterCriticalSection(&cs_actions);
 
270
    if (name && (act = khui_find_named_action(name))) {
 
271
        /* named action already exists */
 
272
        action = act->cmd;
 
273
        goto _done;
 
274
    }
 
275
 
 
276
    for (i=0; i < khui_n_cust_actions; i++) {
 
277
        if (khui_cust_actions[i] == NULL ||
 
278
            (khui_cust_actions[i]->state & KHUI_ACTIONSTATE_DELETED))
 
279
            break;
 
280
    }
 
281
 
 
282
    if (i >= khui_n_cust_actions &&
 
283
        (khui_cust_actions == NULL ||
 
284
         khui_n_cust_actions + 1 > khui_nc_cust_actions)) {
 
285
 
 
286
        khui_nc_cust_actions = UBOUNDSS(khui_n_cust_actions + 1,
 
287
                                        CACT_NC_ALLOC,
 
288
                                        CACT_NC_ALLOC);
 
289
#ifdef DEBUG
 
290
        assert(khui_nc_cust_actions > khui_n_cust_actions + 1);
 
291
#endif
 
292
        khui_cust_actions = PREALLOC(khui_cust_actions,
 
293
                                     sizeof(*khui_cust_actions) * khui_nc_cust_actions);
 
294
#ifdef DEBUG
 
295
        assert(khui_cust_actions);
 
296
#endif
 
297
    }
 
298
 
 
299
    if (i >= khui_n_cust_actions) {
 
300
        i = khui_n_cust_actions ++;
 
301
        act = PMALLOC(sizeof(khui_action));
 
302
    } else {
 
303
        act = khui_cust_actions[i];
 
304
        if (act == NULL)
 
305
            act = PMALLOC(sizeof(khui_action));
 
306
    }
 
307
 
 
308
#ifdef DEBUG
 
309
    assert(act);
 
310
#endif
 
311
 
 
312
    khui_cust_actions[i] = act;
 
313
 
 
314
    ZeroMemory(act, sizeof(*act));
 
315
 
 
316
    act->cmd = KHUI_USERACTION_BASE + i;
 
317
    act->type = type;
 
318
    act->name = (name? PWCSDUP(name) : 0);
 
319
    act->caption = PWCSDUP(caption);
 
320
    act->tooltip = (tooltip? PWCSDUP(tooltip) : 0);
 
321
    act->listener = hsub;
 
322
    act->data = userdata;
 
323
    act->state = 0;
 
324
 
 
325
    action = act->cmd;
 
326
 
 
327
 _done:
 
328
    LeaveCriticalSection(&cs_actions);
 
329
 
 
330
    if (action)
 
331
        kmq_post_message(KMSG_ACT, KMSG_ACT_NEW, action, NULL);
 
332
 
 
333
    return action;
 
334
}
 
335
 
 
336
KHMEXP void * KHMAPI
 
337
khui_action_get_data(khm_int32 action) {
 
338
    khui_action * act;
 
339
 
 
340
    act = khui_find_action(action);
 
341
 
 
342
    if (act == NULL)
 
343
        return NULL;
 
344
    else
 
345
        return act->data;
 
346
}
 
347
 
 
348
KHMEXP void KHMAPI
 
349
khui_action_delete(khm_int32 action) {
 
350
    khui_action * act;
 
351
 
 
352
    act = khui_find_action(action);
 
353
 
 
354
    if (act == NULL)
 
355
        return;
 
356
 
 
357
    /* for the moment, even when the action is deleted, we don't free
 
358
       up the block of memory used by the khui_action structure.  When
 
359
       a new action is created, it will reuse deleted action
 
360
       structures. */
 
361
    EnterCriticalSection(&cs_actions);
 
362
    act->state |= KHUI_ACTIONSTATE_DELETED;
 
363
    if (act->name)
 
364
        PFREE(act->name);
 
365
    if (act->caption)
 
366
        PFREE(act->caption);
 
367
    if (act->tooltip)
 
368
        PFREE(act->tooltip);
 
369
    if (act->listener)
 
370
        kmq_delete_subscription(act->listener);
 
371
    act->name = NULL;
 
372
    act->caption = NULL;
 
373
    act->tooltip = NULL;
 
374
    act->listener = NULL;
 
375
    LeaveCriticalSection(&cs_actions);
 
376
 
 
377
    kmq_post_message(KMSG_ACT, KMSG_ACT_DELETE, action, NULL);
 
378
}
 
379
 
209
380
#define MENU_NC_ITEMS 8
210
381
 
211
382
KHMEXP khui_menu_def * KHMAPI 
212
 
khui_menu_create(int cmd)
 
383
khui_menu_create(khm_int32 action)
213
384
{
214
385
    khui_menu_def * d;
215
 
    d = malloc(sizeof(*d));
 
386
 
 
387
    d = PMALLOC(sizeof(*d));
216
388
    ZeroMemory(d, sizeof(*d));
217
389
 
218
 
    d->cmd = cmd;
 
390
    d->cmd = action;
219
391
    d->nc_items = MENU_NC_ITEMS;
220
 
    d->items = malloc(sizeof(*(d->items)) *  d->nc_items);
 
392
    d->items = PMALLOC(sizeof(*(d->items)) * d->nc_items);
221
393
 
222
394
    d->state = KHUI_MENUSTATE_ALLOCD;
223
395
 
 
396
    if (action) {
 
397
        int i;
 
398
        EnterCriticalSection(&cs_actions);
 
399
 
 
400
        for (i=0; i < khui_n_cust_menus; i++) {
 
401
            if (khui_cust_menus[i] == NULL)
 
402
                break;
 
403
        }
 
404
 
 
405
        if (i >= khui_n_cust_menus) {
 
406
 
 
407
            if (khui_n_cust_menus + 1 >= khui_nc_cust_menus) {
 
408
                khui_nc_cust_menus = UBOUNDSS(khui_n_cust_menus + 1,
 
409
                                              CACT_NC_ALLOC, CACT_NC_ALLOC);
 
410
                khui_cust_menus =
 
411
                    PREALLOC(khui_cust_menus,
 
412
                             sizeof(khui_cust_menus[0]) * khui_nc_cust_menus);
 
413
            }
 
414
 
 
415
            i = khui_n_cust_menus ++;
 
416
        }
 
417
 
 
418
        khui_cust_menus[i] = d;
 
419
 
 
420
        LeaveCriticalSection(&cs_actions);
 
421
    }
 
422
 
224
423
    return d;
225
424
}
226
425
 
 
426
KHMEXP void KHMAPI
 
427
khui_set_main_window(HWND hwnd) {
 
428
    khui_hwnd_main = hwnd;
 
429
}
 
430
 
 
431
KHMEXP void KHMAPI
 
432
khui_action_trigger(khm_int32 action, khui_action_context * ctx) {
 
433
    khui_action_context save;
 
434
 
 
435
    if (!khui_hwnd_main)
 
436
        return;
 
437
 
 
438
    if (ctx) {
 
439
        khui_context_get(&save);
 
440
 
 
441
        khui_context_set_indirect(ctx);
 
442
    }
 
443
 
 
444
    SendMessage(khui_hwnd_main, WM_COMMAND,
 
445
                MAKEWPARAM(action, 0), (LPARAM) 0);
 
446
 
 
447
    if (ctx) {
 
448
        khui_context_set_indirect(&save);
 
449
    }
 
450
}
 
451
 
227
452
KHMEXP khui_menu_def * KHMAPI 
228
453
khui_menu_dup(khui_menu_def * src)
229
454
{
233
458
 
234
459
    d = khui_menu_create(src->cmd);
235
460
 
236
 
    if(src->n_items == -1)
 
461
    if (!(src->state & KHUI_MENUSTATE_ALLOCD))
237
462
        n = khui_action_list_length(src->items);
238
463
    else
239
464
        n = src->n_items;
240
465
 
241
 
    for(i=0; i<n; i++) {
242
 
        if(src->items[i].flags & KHUI_ACTIONREF_PACTION) {
243
 
            khui_menu_add_paction(d, src->items[i].p_action, src->items[i].flags);
 
466
    for (i=0; i<n; i++) {
 
467
        if (src->items[i].flags & KHUI_ACTIONREF_PACTION) {
 
468
            khui_menu_insert_paction(d, -1, src->items[i].p_action, src->items[i].flags);
244
469
        } else {
245
 
            khui_menu_add_action(d, src->items[i].action);
 
470
            khui_menu_insert_action(d, -1, src->items[i].action, 0);
246
471
        }
247
472
    }
248
473
 
256
481
 
257
482
    /* non-allocated menus are assumed to have no pointers to other
258
483
       allocated blocks */
259
 
    if(!(d->state & KHUI_MENUSTATE_ALLOCD))
 
484
    if(!(d->state & KHUI_MENUSTATE_ALLOCD)) {
 
485
        /* we shouldn't have tried to delete a constant menu */
 
486
#ifdef DEBUG
 
487
        assert(FALSE);
 
488
#endif
260
489
        return;
 
490
    }
 
491
 
 
492
    EnterCriticalSection(&cs_actions);
 
493
    for (i=0; i < khui_n_cust_menus; i++) {
 
494
        if (khui_cust_menus[i] == d) {
 
495
            khui_cust_menus[i] = NULL;
 
496
            break;
 
497
        }
 
498
    }
 
499
    LeaveCriticalSection(&cs_actions);
261
500
 
262
501
    for(i=0; i< (int) d->n_items; i++) {
263
502
        if(d->items[i].flags & KHUI_ACTIONREF_FREE_PACTION)
264
 
            free(d->items[i].p_action);
 
503
            PFREE(d->items[i].p_action);
265
504
    }
266
505
 
267
506
    if(d->items)
268
 
        free(d->items);
269
 
    free(d);
 
507
        PFREE(d->items);
 
508
    PFREE(d);
270
509
}
271
510
 
272
 
static void khui_menu_assert_size(khui_menu_def * d, size_t n)
 
511
static void
 
512
menu_assert_size(khui_menu_def * d, size_t n)
273
513
{
 
514
 
 
515
    assert(d->state & KHUI_MENUSTATE_ALLOCD);
 
516
 
274
517
    if(n > (int) d->nc_items) {
275
518
        khui_action_ref * ni;
276
519
 
277
520
        d->nc_items = UBOUNDSS(n, MENU_NC_ITEMS, MENU_NC_ITEMS);
278
 
        ni = malloc(sizeof(*(d->items)) * d->nc_items);
 
521
        ni = PMALLOC(sizeof(*(d->items)) * d->nc_items);
279
522
        memcpy(ni, d->items, sizeof(*(d->items)) * d->n_items);
280
 
        free(d->items);
 
523
        PFREE(d->items);
281
524
        d->items = ni;
282
525
    }
283
526
}
284
527
 
285
 
KHMEXP void KHMAPI khui_menu_add_action(khui_menu_def * d, int id)
286
 
{
287
 
    khui_menu_assert_size(d, d->n_items + 1);
288
 
    d->items[d->n_items].flags = 0;
289
 
    d->items[d->n_items ++].action = id;
290
 
}
291
 
 
292
 
KHMEXP void KHMAPI khui_menu_add_paction(khui_menu_def * d, khui_action * act, int flags)
293
 
{
294
 
    khui_menu_assert_size(d, d->n_items + 1);
295
 
    d->items[d->n_items].flags = flags | KHUI_ACTIONREF_PACTION;
296
 
    d->items[d->n_items ++].p_action = act;
297
 
}
298
 
 
299
 
KHMEXP khui_menu_def * KHMAPI khui_find_menu(int id) {
 
528
static void
 
529
menu_const_to_allocd(khui_menu_def * d)
 
530
{
 
531
    khui_action_ref * olist;
 
532
    khui_action_ref * nlist;
 
533
    khm_size n;
 
534
 
 
535
    assert(!(d->state & KHUI_MENUSTATE_ALLOCD));
 
536
 
 
537
    olist = d->items;
 
538
    n = khui_action_list_length(d->items);
 
539
 
 
540
    d->nc_items = UBOUNDSS(n, MENU_NC_ITEMS, MENU_NC_ITEMS);
 
541
    nlist = PMALLOC(sizeof(d->items[0]) * d->nc_items);
 
542
    memcpy(nlist, olist, sizeof(d->items[0]) * n);
 
543
 
 
544
    d->items = nlist;
 
545
    d->n_items = n;
 
546
    d->state |= KHUI_MENUSTATE_ALLOCD;
 
547
}
 
548
 
 
549
KHMEXP void KHMAPI
 
550
khui_menu_insert_action(khui_menu_def * d, khm_size idx, khm_int32 action, khm_int32 flags)
 
551
{
 
552
    if (!(d->state & KHUI_MENUSTATE_ALLOCD))
 
553
        menu_const_to_allocd(d);
 
554
 
 
555
    assert(d->state & KHUI_MENUSTATE_ALLOCD);
 
556
    assert(action == KHUI_MENU_SEP || action > 0);
 
557
 
 
558
    if (idx < 0 || idx > d->n_items)
 
559
        idx = d->n_items;
 
560
 
 
561
    menu_assert_size(d, d->n_items + 1);
 
562
 
 
563
    if (idx < d->n_items) {
 
564
        memmove(&d->items[idx + 1], &d->items[idx], (d->n_items - idx) * sizeof(d->items[0]));
 
565
    }
 
566
 
 
567
    d->items[idx].flags = flags;
 
568
    d->items[idx].action = action;
 
569
    if (action == KHUI_MENU_SEP)
 
570
        d->items[idx].flags |= KHUI_ACTIONREF_SEP;
 
571
 
 
572
    d->n_items++;
 
573
}
 
574
 
 
575
KHMEXP void KHMAPI
 
576
khui_menu_insert_paction(khui_menu_def * d, khm_size idx, khui_action * paction, int flags)
 
577
{
 
578
 
 
579
    if (paction == NULL)
 
580
        return;
 
581
 
 
582
    if (!(d->state & KHUI_MENUSTATE_ALLOCD))
 
583
        menu_const_to_allocd(d);
 
584
 
 
585
    assert(d->state & KHUI_MENUSTATE_ALLOCD);
 
586
 
 
587
    if (idx < 0 || idx > d->n_items)
 
588
        idx = d->n_items;
 
589
 
 
590
    menu_assert_size(d, d->n_items + 1);
 
591
 
 
592
    if (idx < d->n_items) {
 
593
        memmove(&d->items[idx + 1], &d->items[idx], (d->n_items - idx) * sizeof(d->items[0]));
 
594
    }
 
595
 
 
596
    d->items[idx].flags = flags | KHUI_ACTIONREF_PACTION;
 
597
    d->items[idx].p_action = paction;
 
598
 
 
599
    d->n_items++;
 
600
}
 
601
 
 
602
KHMEXP void KHMAPI
 
603
khui_menu_remove_action(khui_menu_def * d, khm_size idx) {
 
604
 
 
605
    assert(d->state & KHUI_MENUSTATE_ALLOCD);
 
606
 
 
607
    if (idx < 0 || idx >= d->n_items)
 
608
        return;
 
609
 
 
610
    if (idx < d->n_items - 1) {
 
611
        memmove(&d->items[idx], &d->items[idx + 1],
 
612
                ((d->n_items - 1) - idx) * sizeof(d->items[0]));
 
613
    }
 
614
 
 
615
    d->n_items--;
 
616
}
 
617
 
 
618
KHMEXP khm_size KHMAPI
 
619
khui_menu_get_size(khui_menu_def * d) {
 
620
 
 
621
    if (d->state & KHUI_MENUSTATE_ALLOCD)
 
622
        return d->n_items;
 
623
    else
 
624
        return khui_action_list_length(d->items);
 
625
}
 
626
 
 
627
KHMEXP khui_action_ref *
 
628
khui_menu_get_action(khui_menu_def * d, khm_size idx) {
 
629
 
 
630
    khm_size n;
 
631
 
 
632
    if (d->state & KHUI_MENUSTATE_ALLOCD)
 
633
        n = d->n_items;
 
634
    else
 
635
        n = khui_action_list_length(d->items);
 
636
 
 
637
    if (idx < 0 || idx >= n)
 
638
        return NULL;
 
639
 
 
640
    return &d->items[idx];
 
641
}
 
642
 
 
643
KHMEXP khui_menu_def * KHMAPI
 
644
khui_find_menu(khm_int32 id) {
300
645
    khui_menu_def * d;
301
646
    int i;
302
647
 
303
 
    d = khui_all_menus;
304
 
    for(i=0;i<khui_n_all_menus;i++) {
305
 
        if(id == d[i].cmd)
306
 
            return &d[i];
 
648
    if (id < KHUI_USERACTION_BASE) {
 
649
        d = khui_all_menus;
 
650
        for(i=0;i<khui_n_all_menus;i++) {
 
651
            if(id == d[i].cmd)
 
652
                return &d[i];
 
653
        }
 
654
 
 
655
        return NULL;
 
656
    } else {
 
657
        d = NULL;
 
658
 
 
659
        EnterCriticalSection(&cs_actions);
 
660
        for (i=0; i < khui_n_cust_menus; i++) {
 
661
            if (khui_cust_menus[i] &&
 
662
                khui_cust_menus[i]->cmd == id) {
 
663
                d = khui_cust_menus[i];
 
664
                break;
 
665
            }
 
666
        }
 
667
        LeaveCriticalSection(&cs_actions);
 
668
 
 
669
        return d;
307
670
    }
308
 
 
309
 
    return NULL;
310
671
}
311
672
 
312
 
KHMEXP khui_action * KHMAPI khui_find_action(int id) {
 
673
KHMEXP khui_action * KHMAPI
 
674
khui_find_action(khm_int32 id) {
313
675
    khui_action * act;
314
676
    int i;
315
677
 
319
681
            return &act[i];
320
682
    }
321
683
 
322
 
    return NULL;
 
684
    act = NULL;
 
685
 
 
686
    EnterCriticalSection(&cs_actions);
 
687
    if (id >= KHUI_USERACTION_BASE &&
 
688
        (id - KHUI_USERACTION_BASE) < khui_n_cust_actions) {
 
689
        act = khui_cust_actions[id - KHUI_USERACTION_BASE];
 
690
#ifdef DEBUG
 
691
        assert(!act || act->cmd == id);
 
692
#endif
 
693
        if (act && act->state & KHUI_ACTIONSTATE_DELETED)
 
694
            act = NULL;
 
695
    }
 
696
    LeaveCriticalSection(&cs_actions);
 
697
 
 
698
    return act;
323
699
}
324
700
 
325
 
KHMEXP khui_action * KHMAPI khui_find_named_action(wchar_t * name) {
 
701
KHMEXP khui_action * KHMAPI
 
702
khui_find_named_action(const wchar_t * name) {
326
703
    int i;
327
704
    khui_action * act;
 
705
    khui_action ** pact;
328
706
 
329
707
    if(!name)
330
708
        return NULL;
337
715
            return &act[i];
338
716
    }
339
717
 
 
718
    pact = khui_cust_actions;
 
719
    for(i=0;i<khui_n_cust_actions;i++) {
 
720
        if(!pact[i] || !pact[i]->name)
 
721
            continue;
 
722
 
 
723
        if(!wcscmp(pact[i]->name, name)) {
 
724
            if (pact[i]->state & KHUI_ACTIONSTATE_DELETED)
 
725
                return NULL;
 
726
            else
 
727
                return pact[i];
 
728
        }
 
729
    }
 
730
 
340
731
    return NULL;
341
732
}
342
733
 
343
 
KHMEXP size_t KHMAPI khui_action_list_length(khui_action_ref * ref) {
 
734
KHMEXP size_t KHMAPI
 
735
khui_action_list_length(khui_action_ref * ref) {
344
736
    size_t c = 0;
345
 
    while(ref && ref->action != KHUI_MENU_END) {
 
737
    while(ref && ref->action != KHUI_MENU_END &&
 
738
          !(ref->flags & KHUI_ACTIONREF_END)) {
346
739
        c++;
347
740
        ref++;
348
741
    }
349
742
    return c;
350
743
}
351
744
 
352
 
KHMEXP void KHMAPI khui_check_radio_action(khui_menu_def * d, khm_int32 cmd)
 
745
KHMEXP void KHMAPI
 
746
khui_check_radio_action(khui_menu_def * d, khm_int32 cmd)
353
747
{
354
748
    khui_action_ref * r;
355
749
    khui_action * act;
356
750
 
357
751
    r = d->items;
358
 
    while(r && r->action != KHUI_MENU_END) {
 
752
    while(r && r->action != KHUI_MENU_END &&
 
753
          (!(d->state & KHUI_MENUSTATE_ALLOCD) || (r - d->items) < (int) d->n_items)) {
359
754
        if(r->flags & KHUI_ACTIONREF_PACTION) {
360
755
            act = r->p_action;
361
756
        } else {
374
769
    kmq_post_message(KMSG_ACT, KMSG_ACT_CHECK, 0, 0);
375
770
}
376
771
 
377
 
KHMEXP void KHMAPI khui_check_action(int cmd, khm_boolean check) {
 
772
KHMEXP void KHMAPI
 
773
khui_check_action(khm_int32 cmd, khm_boolean check) {
378
774
    khui_action * act;
379
775
 
380
776
    act = khui_find_action(cmd);
391
787
    kmq_post_message(KMSG_ACT, KMSG_ACT_CHECK, 0, 0);
392
788
}
393
789
 
394
 
KHMEXP void KHMAPI khui_enable_actions(khui_menu_def * d, khm_boolean enable)
 
790
KHMEXP void KHMAPI
 
791
khui_enable_actions(khui_menu_def * d, khm_boolean enable)
395
792
{
396
793
    khui_action_ref * r;
397
794
    int delta = FALSE;
398
795
    khui_action * act;
399
796
 
400
797
    r = d->items;
401
 
    while(r && r->action != KHUI_MENU_END) {
 
798
    while(r && r->action != KHUI_MENU_END &&
 
799
          (!(d->state & KHUI_MENUSTATE_ALLOCD) || (r - d->items) < (int) d->n_items)) {
402
800
        if(r->flags & KHUI_ACTIONREF_PACTION) {
403
801
            act = r->p_action;
404
802
        } else {
420
818
    }
421
819
 
422
820
    if(delta) {
423
 
        kmq_send_message(KMSG_ACT, KMSG_ACT_ENABLE, 0, 0);
 
821
        kmq_post_message(KMSG_ACT, KMSG_ACT_ENABLE, 0, 0);
424
822
    }
425
823
}
426
824
 
427
 
KHMEXP void KHMAPI khui_enable_action(int cmd, khm_boolean enable) {
 
825
KHMEXP void KHMAPI
 
826
khui_enable_action(khm_int32 cmd, khm_boolean enable) {
428
827
    khui_action * act;
429
828
 
430
829
    act = khui_find_action(cmd);
438
837
    } else
439
838
        return;
440
839
 
441
 
    kmq_send_message(KMSG_ACT, KMSG_ACT_ENABLE, 0, 0);
 
840
    kmq_post_message(KMSG_ACT, KMSG_ACT_ENABLE, 0, 0);
442
841
}
443
842
 
444
 
KHMEXP HACCEL KHMAPI khui_create_global_accel_table(void) {
 
843
KHMEXP HACCEL KHMAPI
 
844
khui_create_global_accel_table(void) {
445
845
    int i;
446
846
    ACCEL * accels;
447
847
    HACCEL ha;
448
848
 
449
 
    accels = malloc(sizeof(ACCEL) * khui_n_accel_global);
 
849
    accels = PMALLOC(sizeof(ACCEL) * khui_n_accel_global);
450
850
    for(i=0;i<khui_n_accel_global;i++) {
451
851
        accels[i].cmd = khui_accel_global[i].cmd;
452
852
        accels[i].fVirt = khui_accel_global[i].mod;
455
855
 
456
856
    ha = CreateAcceleratorTable(accels, khui_n_accel_global);
457
857
 
458
 
    free(accels);
 
858
    PFREE(accels);
459
859
 
460
860
    return ha;
461
861
}
462
862
 
463
863
KHMEXP khm_boolean KHMAPI 
464
 
khui_get_cmd_accel_string(int cmd, 
 
864
khui_get_cmd_accel_string(khm_int32 cmd, 
465
865
                          wchar_t * buf, 
466
 
                          size_t bufsiz) {
 
866
                          khm_size bufsiz) {
467
867
    int i;
468
868
    khui_accel_def * def;
469
869
 
484
884
    def = &khui_accel_global[i];
485
885
 
486
886
    if(def->mod & FALT) {
487
 
        if(FAILED(StringCbCat(buf, bufsiz, L"ALT+")))
 
887
        if(FAILED(StringCbCat(buf, bufsiz, L"Alt+")))
488
888
            return FALSE;
489
889
    }
490
890
 
491
891
 
492
892
    if(def->mod & FCONTROL) {
493
 
        if(FAILED(StringCbCat(buf, bufsiz, L"CTRL+")))
 
893
        if(FAILED(StringCbCat(buf, bufsiz, L"Ctrl+")))
494
894
            return FALSE;
495
895
    }
496
896
 
497
897
    if(def->mod & FSHIFT) {
498
 
        if(FAILED(StringCbCat(buf, bufsiz, L"SHIFT+")))
 
898
        if(FAILED(StringCbCat(buf, bufsiz, L"Shift+")))
499
899
            return FALSE;
500
900
    }
501
901
 
515
915
            ap = L"Enter";
516
916
            break;
517
917
 
 
918
        case VK_F1:
 
919
            ap = L"F1";
 
920
            break;
 
921
 
 
922
        case VK_F2:
 
923
            ap = L"F2";
 
924
            break;
 
925
 
 
926
        case VK_F3:
 
927
            ap = L"F3";
 
928
            break;
 
929
 
 
930
        case VK_F4:
 
931
            ap = L"F4";
 
932
            break;
 
933
 
518
934
        case VK_F5:
519
935
            ap = L"F5";
520
936
            break;
521
937
 
 
938
        case VK_F6:
 
939
            ap = L"F6";
 
940
            break;
 
941
 
 
942
        case VK_F7:
 
943
            ap = L"F7";
 
944
            break;
 
945
 
 
946
        case VK_F8:
 
947
            ap = L"F8";
 
948
            break;
 
949
 
 
950
        case VK_F9:
 
951
            ap = L"F9";
 
952
            break;
 
953
 
 
954
        case VK_F10:
 
955
            ap = L"F10";
 
956
            break;
 
957
 
522
958
        case VK_DELETE:
523
959
            ap = L"Del";
524
960
            break;
623
1059
        if (ctxdest->int_cb_buf < cb_total) {
624
1060
 
625
1061
            if (ctxdest->int_buf)
626
 
                free(ctxdest->int_buf);
 
1062
                PFREE(ctxdest->int_buf);
627
1063
 
628
1064
            ctxdest->int_cb_buf = cb_total;
629
 
            ctxdest->int_buf = malloc(cb_total);
 
1065
            ctxdest->int_buf = PMALLOC(cb_total);
630
1066
        }
631
1067
 
632
1068
#ifdef DEBUG
680
1116
       khui_context_release() to free the allocated buffer */
681
1117
#if 0
682
1118
    if (ctxsrc->vparam && ctxsrc->cb_vparam) {
683
 
        ctxdest->vparam = malloc(ctxsrc->cb_vparam);
 
1119
        ctxdest->vparam = PMALLOC(ctxsrc->cb_vparam);
684
1120
#ifdef DEBUG
685
1121
        assert(ctxdest->vparam);
686
1122
#endif
822
1258
    LeaveCriticalSection(&cs_actions);
823
1259
}
824
1260
 
 
1261
KHMEXP void KHMAPI
 
1262
khui_context_set_indirect(khui_action_context * ctx)
 
1263
{
 
1264
    EnterCriticalSection(&cs_actions);
 
1265
 
 
1266
    khuiint_context_release(&khui_ctx);
 
1267
 
 
1268
    khuiint_copy_context(&khui_ctx, ctx);
 
1269
 
 
1270
    khui_context_refresh();
 
1271
 
 
1272
    LeaveCriticalSection(&cs_actions);
 
1273
}
 
1274
 
825
1275
KHMEXP void KHMAPI 
826
1276
khui_context_refresh(void) {
827
1277
    khm_int32 flags;
891
1341
    }
892
1342
    ctx->credset = NULL;
893
1343
    if (ctx->int_buf)
894
 
        free(ctx->int_buf);
 
1344
        PFREE(ctx->int_buf);
895
1345
    ctx->int_buf = NULL;
896
1346
#if 0
897
1347
    if (ctx->vparam && ctx->cb_vparam > 0) {
898
 
        free(ctx->vparam);
 
1348
        PFREE(ctx->vparam);
899
1349
        ctx->vparam = NULL;
900
1350
    }
901
1351
    ctx->cb_vparam = 0;