| 1 /* |
|
| 2 * GNT - The GLib Ncurses Toolkit |
|
| 3 * |
|
| 4 * GNT is the legal property of its developers, whose names are too numerous |
|
| 5 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 6 * source distribution. |
|
| 7 * |
|
| 8 * This library is free software; you can redistribute it and/or modify |
|
| 9 * it under the terms of the GNU General Public License as published by |
|
| 10 * the Free Software Foundation; either version 2 of the License, or |
|
| 11 * (at your option) any later version. |
|
| 12 * |
|
| 13 * This program is distributed in the hope that it will be useful, |
|
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 16 * GNU General Public License for more details. |
|
| 17 * |
|
| 18 * You should have received a copy of the GNU General Public License |
|
| 19 * along with this program; if not, write to the Free Software |
|
| 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 21 */ |
|
| 22 |
|
| 23 #include "gntinternal.h" |
|
| 24 #include "gntmenu.h" |
|
| 25 #include "gntmenuitem.h" |
|
| 26 |
|
| 27 enum |
|
| 28 { |
|
| 29 SIG_ACTIVATE, |
|
| 30 SIGS |
|
| 31 }; |
|
| 32 static guint signals[SIGS] = { 0 }; |
|
| 33 |
|
| 34 static GObjectClass *parent_class = NULL; |
|
| 35 |
|
| 36 static void |
|
| 37 gnt_menuitem_destroy(GObject *obj) |
|
| 38 { |
|
| 39 GntMenuItem *item = GNT_MENU_ITEM(obj); |
|
| 40 g_free(item->text); |
|
| 41 item->text = NULL; |
|
| 42 if (item->submenu) |
|
| 43 gnt_widget_destroy(GNT_WIDGET(item->submenu)); |
|
| 44 g_free(item->priv.id); |
|
| 45 parent_class->dispose(obj); |
|
| 46 } |
|
| 47 |
|
| 48 static void |
|
| 49 gnt_menuitem_class_init(GntMenuItemClass *klass) |
|
| 50 { |
|
| 51 GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
|
| 52 parent_class = g_type_class_peek_parent(klass); |
|
| 53 |
|
| 54 obj_class->dispose = gnt_menuitem_destroy; |
|
| 55 |
|
| 56 signals[SIG_ACTIVATE] = |
|
| 57 g_signal_new("activate", |
|
| 58 G_TYPE_FROM_CLASS(klass), |
|
| 59 G_SIGNAL_RUN_LAST, |
|
| 60 0, NULL, NULL, NULL, |
|
| 61 G_TYPE_NONE, 0); |
|
| 62 } |
|
| 63 |
|
| 64 static void |
|
| 65 gnt_menuitem_init(GTypeInstance *instance, gpointer klass) |
|
| 66 { |
|
| 67 GntMenuItem *item = GNT_MENU_ITEM(instance); |
|
| 68 |
|
| 69 item->visible = TRUE; |
|
| 70 } |
|
| 71 |
|
| 72 /****************************************************************************** |
|
| 73 * GntMenuItem API |
|
| 74 *****************************************************************************/ |
|
| 75 GType |
|
| 76 gnt_menuitem_get_type(void) |
|
| 77 { |
|
| 78 static GType type = 0; |
|
| 79 |
|
| 80 if(type == 0) |
|
| 81 { |
|
| 82 static const GTypeInfo info = { |
|
| 83 sizeof(GntMenuItemClass), |
|
| 84 NULL, /* base_init */ |
|
| 85 NULL, /* base_finalize */ |
|
| 86 (GClassInitFunc)gnt_menuitem_class_init, |
|
| 87 NULL, /* class_finalize */ |
|
| 88 NULL, /* class_data */ |
|
| 89 sizeof(GntMenuItem), |
|
| 90 0, /* n_preallocs */ |
|
| 91 gnt_menuitem_init, /* instance_init */ |
|
| 92 NULL /* value_table */ |
|
| 93 }; |
|
| 94 |
|
| 95 type = g_type_register_static(G_TYPE_OBJECT, |
|
| 96 "GntMenuItem", |
|
| 97 &info, 0); |
|
| 98 } |
|
| 99 |
|
| 100 return type; |
|
| 101 } |
|
| 102 |
|
| 103 GntMenuItem *gnt_menuitem_new(const char *text) |
|
| 104 { |
|
| 105 GObject *item = g_object_new(GNT_TYPE_MENU_ITEM, NULL); |
|
| 106 GntMenuItem *menuitem = GNT_MENU_ITEM(item); |
|
| 107 |
|
| 108 menuitem->text = g_strdup(text); |
|
| 109 |
|
| 110 return menuitem; |
|
| 111 } |
|
| 112 |
|
| 113 void gnt_menuitem_set_callback(GntMenuItem *item, GntMenuItemCallback callback, gpointer data) |
|
| 114 { |
|
| 115 item->callback = callback; |
|
| 116 item->callbackdata = data; |
|
| 117 } |
|
| 118 |
|
| 119 void gnt_menuitem_set_submenu(GntMenuItem *item, GntMenu *menu) |
|
| 120 { |
|
| 121 if (item->submenu) |
|
| 122 gnt_widget_destroy(GNT_WIDGET(item->submenu)); |
|
| 123 item->submenu = menu; |
|
| 124 } |
|
| 125 |
|
| 126 GntMenu *gnt_menuitem_get_submenu(GntMenuItem *item) |
|
| 127 { |
|
| 128 return item->submenu; |
|
| 129 } |
|
| 130 |
|
| 131 void gnt_menuitem_set_trigger(GntMenuItem *item, char trigger) |
|
| 132 { |
|
| 133 item->priv.trigger = trigger; |
|
| 134 } |
|
| 135 |
|
| 136 char gnt_menuitem_get_trigger(GntMenuItem *item) |
|
| 137 { |
|
| 138 return item->priv.trigger; |
|
| 139 } |
|
| 140 |
|
| 141 void gnt_menuitem_set_id(GntMenuItem *item, const char *id) |
|
| 142 { |
|
| 143 g_free(item->priv.id); |
|
| 144 item->priv.id = g_strdup(id); |
|
| 145 } |
|
| 146 |
|
| 147 const char * gnt_menuitem_get_id(GntMenuItem *item) |
|
| 148 { |
|
| 149 return item->priv.id; |
|
| 150 } |
|
| 151 |
|
| 152 gboolean gnt_menuitem_activate(GntMenuItem *item) |
|
| 153 { |
|
| 154 g_signal_emit(item, signals[SIG_ACTIVATE], 0); |
|
| 155 if (item->callback) { |
|
| 156 item->callback(item, item->callbackdata); |
|
| 157 return TRUE; |
|
| 158 } |
|
| 159 return FALSE; |
|
| 160 } |
|
| 161 |
|
| 162 void |
|
| 163 gnt_menuitem_set_visible(GntMenuItem *item, gboolean visible) |
|
| 164 { |
|
| 165 item->visible = visible; |
|
| 166 } |
|
| 167 |
|
| 168 gboolean |
|
| 169 gnt_menuitem_is_visible(GntMenuItem *item) |
|
| 170 { |
|
| 171 g_return_val_if_fail(GNT_IS_MENU_ITEM(item), FALSE); |
|
| 172 |
|
| 173 return item->visible; |
|
| 174 } |
|
| 175 |
|
| 176 void |
|
| 177 gnt_menuitem_set_text(GntMenuItem *item, const gchar *text) |
|
| 178 { |
|
| 179 g_free(item->text); |
|
| 180 item->text = g_strdup(text); |
|
| 181 } |
|