summaryrefslogtreecommitdiff
path: root/iup/src/gtk/iupgtk_menu.c
blob: 772f4cfbe377fe26320fd39ea27ee0fc6369f742 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/** \file
 * \brief Menu Resources
 *
 * See Copyright Notice in "iup.h"
 */

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#ifdef HILDON
#include <hildon/hildon-window.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdarg.h>

#include "iup.h"
#include "iupcbs.h"

#include "iup_object.h"
#include "iup_childtree.h"
#include "iup_attrib.h"
#include "iup_dialog.h"
#include "iup_str.h"
#include "iup_label.h"
#include "iup_drv.h"
#include "iup_drvfont.h"
#include "iup_image.h"
#include "iup_menu.h"

#include "iupgtk_drv.h"


typedef struct _ImenuPos
{
  int x, y;
} ImenuPos;

static void gtkMenuPositionFunc(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, ImenuPos *menupos)
{
  *x = menupos->x;
  *y = menupos->y;
  *push_in = FALSE;
  (void)menu;
}

int iupdrvMenuPopup(Ihandle* ih, int x, int y)
{
  ImenuPos menupos;
  menupos.x = x;
  menupos.y = y;
  gtk_menu_popup((GtkMenu*)ih->handle, NULL, NULL, (GtkMenuPositionFunc)gtkMenuPositionFunc,
                 (gpointer)&menupos, 0, gtk_get_current_event_time());
  gtk_main();
  return IUP_NOERROR;
}

int iupdrvMenuGetMenuBarSize(Ihandle* ih)
{
  int ch;
  iupdrvFontGetCharSize(ih, NULL, &ch);
#ifdef WIN32
  return 3 + ch + 3;
#else
  return 4 + ch + 4;
#endif
}

static void gtkItemUpdateImage(Ihandle* ih, const char* value, const char* image, const char* impress)
{
  GdkPixbuf* pixbuf;

  if (!impress || !iupStrBoolean(value))
    pixbuf = iupImageGetImage(image, ih, 0);
  else
    pixbuf = iupImageGetImage(impress, ih, 0);

  if (pixbuf)
  {
    GtkWidget* image_label = gtk_image_menu_item_get_image((GtkImageMenuItem*)ih->handle);
    if (!image_label)
    {
      image_label = gtk_image_new();
      gtk_image_menu_item_set_image((GtkImageMenuItem*)ih->handle, image_label);
    }

    if (pixbuf != gtk_image_get_pixbuf((GtkImage*)image_label))
      gtk_image_set_from_pixbuf((GtkImage*)image_label, pixbuf);
  }
  else
    gtk_image_menu_item_set_image((GtkImageMenuItem*)ih->handle, NULL);
}


/*******************************************************************************************/


static void gtkMenuMap(GtkWidget *widget, Ihandle* ih)
{
  Icallback cb = IupGetCallback(ih, "OPEN_CB");
  if (!cb && ih->parent) cb = (Icallback)IupGetCallback(ih->parent, "OPEN_CB");  /* check also in the Submenu */
  if (cb) cb(ih);

  (void)widget;
}

static void gtkMenuUnMap(GtkWidget *widget, Ihandle* ih)
{
  Icallback cb = IupGetCallback(ih, "MENUCLOSE_CB");
  if (!cb && ih->parent) cb = (Icallback)IupGetCallback(ih->parent, "MENUCLOSE_CB");  /* check also in the Submenu */
  if (cb) cb(ih);

  (void)widget;
}

static void gtkPopupMenuUnMap(GtkWidget *widget, Ihandle* ih)
{
  gtkMenuUnMap(widget, ih);

  /* quit the popup loop */
  gtk_main_quit();
}

static void gtkItemSelect(GtkWidget *widget, Ihandle* ih)
{
  Icallback cb = IupGetCallback(ih, "HIGHLIGHT_CB");
  if (cb)
    cb(ih);

  cb = IupGetCallback(ih, "HELP_CB");
  if (cb)
    gtk_menu_set_active((GtkMenu*)ih->parent->handle, IupGetChildPos(ih->parent, ih));

  (void)widget;
}

static void gtkItemActivate(GtkWidget *widget, Ihandle* ih)
{
  Icallback cb;

  if (GTK_IS_CHECK_MENU_ITEM(ih->handle) && !iupAttribGetBoolean(ih, "AUTOTOGGLE") && !iupAttribGetBoolean(ih->parent, "RADIO"))
  {
    /* GTK by default will do autotoggle */
    g_signal_handlers_block_by_func(G_OBJECT(ih->handle), G_CALLBACK(gtkItemActivate), ih);
    gtk_check_menu_item_set_active((GtkCheckMenuItem*)ih->handle, !gtk_check_menu_item_get_active((GtkCheckMenuItem*)ih->handle));
    g_signal_handlers_unblock_by_func(G_OBJECT(ih->handle), G_CALLBACK(gtkItemActivate), ih);
  }

  if (GTK_IS_IMAGE_MENU_ITEM(ih->handle))
  {
    if (iupAttribGetBoolean(ih, "AUTOTOGGLE"))
    {
      if (iupAttribGetBoolean(ih, "VALUE"))
        iupAttribSetStr(ih, "VALUE", "OFF");
      else
        iupAttribSetStr(ih, "VALUE", "ON");

      gtkItemUpdateImage(ih, iupAttribGet(ih, "VALUE"), iupAttribGet(ih, "IMAGE"), iupAttribGet(ih, "IMPRESS"));
    }
  }

  cb = IupGetCallback(ih, "ACTION");
  if (cb && cb(ih)==IUP_CLOSE)
    IupExitLoop();

  (void)widget;
}

static gboolean gtkMenuKeyPressEvent(GtkWidget *widget, GdkEventKey *evt, Ihandle *ih)
{
  if (evt->keyval == GDK_F1)
  {
    Ihandle* child;
    GtkWidget* active = gtk_menu_get_active((GtkMenu*)widget);
    for (child=ih->firstchild; child; child=child->brother)
    {
      if (child->handle == active)
        iupgtkShowHelp(NULL, NULL, child);
    }
  }

  (void)widget;
  (void)evt;
  return FALSE;
}


/*******************************************************************************************/


static int gtkMenuMapMethod(Ihandle* ih)
{
  if (iupMenuIsMenuBar(ih))
  {
    /* top level menu used for MENU attribute in IupDialog (a menu bar) */
#ifdef HILDON
    Ihandle *pih;
    ih->handle = gtk_menu_new();
    if (!ih->handle)
      return IUP_ERROR;

    pih = iupChildTreeGetNativeParent(ih);
    hildon_window_set_menu(HILDON_WINDOW(pih->handle), GTK_MENU(ih->handle));
#else
    ih->handle = gtk_menu_bar_new();
    if (!ih->handle)
      return IUP_ERROR;

    iupgtkBaseAddToParent(ih);
#endif
  }
  else
  {
    ih->handle = gtk_menu_new();
    if (!ih->handle)
      return IUP_ERROR;

    if (ih->parent)
    {
      /* parent is a submenu */
      gtk_menu_item_set_submenu((GtkMenuItem*)ih->parent->handle, ih->handle);

      g_signal_connect(G_OBJECT(ih->handle), "map", G_CALLBACK(gtkMenuMap), ih);
      g_signal_connect(G_OBJECT(ih->handle), "unmap", G_CALLBACK(gtkMenuUnMap), ih);
    }
    else
    {
      /* top level menu used for IupPopup */
      iupAttribSetStr(ih, "_IUPGTK_POPUP_MENU", "1");

      g_signal_connect(G_OBJECT(ih->handle), "map", G_CALLBACK(gtkMenuMap), ih);
      g_signal_connect(G_OBJECT(ih->handle), "unmap", G_CALLBACK(gtkPopupMenuUnMap), ih);
    }
  }

  gtk_widget_add_events(ih->handle, GDK_KEY_PRESS_MASK);
  g_signal_connect(G_OBJECT(ih->handle), "key-press-event", G_CALLBACK(gtkMenuKeyPressEvent), ih);

  ih->serial = iupMenuGetChildId(ih); 
  gtk_widget_show(ih->handle);

  return IUP_NOERROR;
}

static void gtkMenuUnMapMethod(Ihandle* ih)
{
  if (iupMenuIsMenuBar(ih))
    ih->parent = NULL;

  iupdrvBaseUnMapMethod(ih);
}

void iupdrvMenuInitClass(Iclass* ic)
{
  /* Driver Dependent Class functions */
  ic->Map = gtkMenuMapMethod;
  ic->UnMap = gtkMenuUnMapMethod;

  /* Used by iupdrvMenuGetMenuBarSize */
  iupClassRegisterAttribute(ic, "STANDARDFONT", NULL, NULL, IUPAF_SAMEASSYSTEM, "DEFAULTFONT", IUPAF_DEFAULT);  /* use inheritance to retrieve standard fonts */

  iupClassRegisterAttribute(ic, "BGCOLOR", NULL, iupdrvBaseSetBgColorAttrib, NULL, NULL, IUPAF_DEFAULT);
}


/*******************************************************************************************/

static int gtkItemSetTitleImageAttrib(Ihandle* ih, const char* value)
{
  if (GTK_IS_IMAGE_MENU_ITEM(ih->handle))
  {
    gtkItemUpdateImage(ih, NULL, value, NULL);
    return 1;
  }
  else
    return 0;
}

static int gtkItemSetImageAttrib(Ihandle* ih, const char* value)
{
  if (GTK_IS_IMAGE_MENU_ITEM(ih->handle))
  {
    gtkItemUpdateImage(ih, iupAttribGet(ih, "VALUE"), value, iupAttribGet(ih, "IMPRESS"));
    return 1;
  }
  else
    return 0;
}

static int gtkItemSetImpressAttrib(Ihandle* ih, const char* value)
{
  if (GTK_IS_IMAGE_MENU_ITEM(ih->handle))
  {
    gtkItemUpdateImage(ih, iupAttribGet(ih, "VALUE"), iupAttribGet(ih, "IMAGE"), value);
    return 1;
  }
  else
    return 0;
}

static int gtkItemSetTitleAttrib(Ihandle* ih, const char* value)
{
  char *str;
  GtkWidget* label;

  if (!value)
  {
    str = "     ";
    value = str;
  }
  else
    str = iupMenuProcessTitle(ih, value);

  label = gtk_bin_get_child((GtkBin*)ih->handle);

  iupgtkSetMnemonicTitle(ih, (GtkLabel*)label, str);

  if (str != value) free(str);
  return 1;
}

static int gtkItemSetValueAttrib(Ihandle* ih, const char* value)
{
  if (GTK_IS_CHECK_MENU_ITEM(ih->handle))
  {
    if (iupAttribGetBoolean(ih->parent, "RADIO"))
      value = "ON";

    g_signal_handlers_block_by_func(G_OBJECT(ih->handle), G_CALLBACK(gtkItemActivate), ih);
    gtk_check_menu_item_set_active((GtkCheckMenuItem*)ih->handle, iupStrBoolean(value));
    g_signal_handlers_unblock_by_func(G_OBJECT(ih->handle), G_CALLBACK(gtkItemActivate), ih);
    return 0;
  }
  else if (GTK_IS_IMAGE_MENU_ITEM(ih->handle))
  {
    gtkItemUpdateImage(ih, value, iupAttribGet(ih, "IMAGE"), iupAttribGet(ih, "IMPRESS"));
    return 1;
  }
  else
    return 0;
}

static char* gtkItemGetValueAttrib(Ihandle* ih)
{
  if (GTK_IS_CHECK_MENU_ITEM(ih->handle) && gtk_check_menu_item_get_active((GtkCheckMenuItem*)ih->handle))
    return "ON";
  else
    return "OFF";
}

static int gtkItemMapMethod(Ihandle* ih)
{
  int pos;

  if (!ih->parent)
    return IUP_ERROR;

#ifndef HILDON
  if (iupMenuIsMenuBar(ih->parent))
    ih->handle = gtk_menu_item_new_with_label("");
  else
#endif
  {
    if (iupAttribGet(ih, "IMAGE")||iupAttribGet(ih, "TITLEIMAGE"))
      ih->handle = gtk_image_menu_item_new_with_label("");
    else if (iupAttribGetBoolean(ih->parent, "RADIO"))
    {
      GtkRadioMenuItem* last_tg = (GtkRadioMenuItem*)iupAttribGet(ih->parent, "_IUPGTK_LASTRADIOITEM");
      if (last_tg)
        ih->handle = gtk_radio_menu_item_new_with_label_from_widget(last_tg, "");
      else
        ih->handle = gtk_radio_menu_item_new_with_label(NULL, "");
      iupAttribSetStr(ih->parent, "_IUPGTK_LASTRADIOITEM", (char*)ih->handle);
    }
    else
    {
      char* hidemark = iupAttribGetStr(ih, "HIDEMARK");
      if (!hidemark && gtk_check_version(2, 14, 0) == NULL)
      {
        /* force HIDEMARK if VALUE is defined before Map, after GTK 2.14 */
        if (!iupAttribGet(ih, "VALUE")) 
          hidemark = "YES";
      }

      if (iupStrBoolean(hidemark))
        ih->handle = gtk_menu_item_new_with_label("");
      else
        ih->handle = gtk_check_menu_item_new_with_label("");
    }
  }

  if (!ih->handle)
    return IUP_ERROR;

  ih->serial = iupMenuGetChildId(ih); 

  g_signal_connect(G_OBJECT(ih->handle), "select", G_CALLBACK(gtkItemSelect), ih);
  g_signal_connect(G_OBJECT(ih->handle), "activate", G_CALLBACK(gtkItemActivate), ih);

  pos = IupGetChildPos(ih->parent, ih);
  gtk_menu_shell_insert((GtkMenuShell*)ih->parent->handle, ih->handle, pos);
  gtk_widget_show(ih->handle);

  iupUpdateStandardFontAttrib(ih);

  return IUP_NOERROR;
}

void iupdrvItemInitClass(Iclass* ic)
{
  /* Driver Dependent Class functions */
  ic->Map = gtkItemMapMethod;
  ic->UnMap = iupdrvBaseUnMapMethod;

  /* Common */
  iupClassRegisterAttribute(ic, "STANDARDFONT", NULL, iupdrvSetStandardFontAttrib, IUPAF_SAMEASSYSTEM, "DEFAULTFONT", IUPAF_NOT_MAPPED);  /* use inheritance to retrieve standard fonts */

  /* Visual */
  iupClassRegisterAttribute(ic, "ACTIVE", iupBaseGetActiveAttrib, iupBaseSetActiveAttrib, IUPAF_SAMEASSYSTEM, "YES", IUPAF_DEFAULT);
  iupClassRegisterAttribute(ic, "BGCOLOR", NULL, iupdrvBaseSetBgColorAttrib, IUPAF_SAMEASSYSTEM, "DLGBGCOLOR", IUPAF_DEFAULT);

  /* IupItem only */
  iupClassRegisterAttribute(ic, "VALUE", gtkItemGetValueAttrib, gtkItemSetValueAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "TITLE", NULL, gtkItemSetTitleAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "TITLEIMAGE", NULL, gtkItemSetTitleImageAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "IMAGE", NULL, gtkItemSetImageAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "IMPRESS", NULL, gtkItemSetImpressAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);

  /* IupItem GTK and Motif only */
  iupClassRegisterAttribute(ic, "HIDEMARK", NULL, NULL, NULL, NULL, IUPAF_NOT_MAPPED);
}


/*******************************************************************************************/


static int gtkSubmenuSetImageAttrib(Ihandle* ih, const char* value)
{
  if (GTK_IS_IMAGE_MENU_ITEM(ih->handle))
  {
    gtkItemUpdateImage(ih, NULL, value, NULL);
    return 1;
  }
  else
    return 0;
}

static int motSubmenuMapMethod(Ihandle* ih)
{
  int pos;

  if (!ih->parent)
    return IUP_ERROR;

#ifndef HILDON
  if (iupMenuIsMenuBar(ih->parent))
    ih->handle = gtk_menu_item_new_with_label("");
  else
#endif
    ih->handle = gtk_image_menu_item_new_with_label("");

  if (!ih->handle)
    return IUP_ERROR;

  ih->serial = iupMenuGetChildId(ih); 

  pos = IupGetChildPos(ih->parent, ih);
  gtk_menu_shell_insert((GtkMenuShell*)ih->parent->handle, ih->handle, pos);
  gtk_widget_show(ih->handle);

  g_signal_connect(G_OBJECT(ih->handle), "select", G_CALLBACK(gtkItemSelect), ih);

  iupUpdateStandardFontAttrib(ih);

  return IUP_NOERROR;
}

void iupdrvSubmenuInitClass(Iclass* ic)
{
  /* Driver Dependent Class functions */
  ic->Map = motSubmenuMapMethod;
  ic->UnMap = iupdrvBaseUnMapMethod;

  /* Common */
  iupClassRegisterAttribute(ic, "STANDARDFONT", NULL, iupdrvSetStandardFontAttrib, IUPAF_SAMEASSYSTEM, "DEFAULTFONT", IUPAF_NOT_MAPPED);  /* use inheritance to retrieve standard fonts */

  /* Visual */
  iupClassRegisterAttribute(ic, "ACTIVE", iupBaseGetActiveAttrib, iupBaseSetActiveAttrib, IUPAF_SAMEASSYSTEM, "YES", IUPAF_DEFAULT);
  iupClassRegisterAttribute(ic, "BGCOLOR", NULL, iupdrvBaseSetBgColorAttrib, IUPAF_SAMEASSYSTEM, "DLGBGCOLOR", IUPAF_DEFAULT);

  /* IupSubmenu only */
  iupClassRegisterAttribute(ic, "TITLE", NULL, gtkItemSetTitleAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "IMAGE", NULL, gtkSubmenuSetImageAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
}


/*******************************************************************************************/


static int gtkSeparatorMapMethod(Ihandle* ih)
{
  int pos;

  if (!ih->parent)
    return IUP_ERROR;

  ih->handle = gtk_separator_menu_item_new();
  if (!ih->handle)
    return IUP_ERROR;

  ih->serial = iupMenuGetChildId(ih); 

  pos = IupGetChildPos(ih->parent, ih);
  gtk_menu_shell_insert((GtkMenuShell*)ih->parent->handle, ih->handle, pos);
  gtk_widget_show(ih->handle);

  return IUP_NOERROR;
}

void iupdrvSeparatorInitClass(Iclass* ic)
{
  /* Driver Dependent Class functions */
  ic->Map = gtkSeparatorMapMethod;
  ic->UnMap = iupdrvBaseUnMapMethod;
}