Version bump.
[geany-mirror.git] / src / geanyentryaction.c
blob8283bfed68b84114c1912b9fff044356e5888e30
1 /*
2 * geanyentryaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2008-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either vergeany 2 of the License, or
10 * (at your option) any later vergeany.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /* GtkAction subclass to provide a GtkEntry in a toolbar.
23 * This class is missing the action_create_menu_item() function and so can't be
24 * used for creating menu items. */
27 #include "geany.h"
28 #include "support.h"
29 #include "ui_utils.h"
30 #include "geanyentryaction.h"
31 #include <ctype.h>
34 typedef struct _GeanyEntryActionPrivate GeanyEntryActionPrivate;
36 #define GEANY_ENTRY_ACTION_GET_PRIVATE(obj) (GEANY_ENTRY_ACTION(obj)->priv)
39 struct _GeanyEntryActionPrivate
41 GtkWidget *entry;
42 gboolean numeric;
45 enum
47 ENTRY_ACTIVATE,
48 ENTRY_CHANGED,
50 LAST_SIGNAL
52 static guint signals[LAST_SIGNAL];
55 G_DEFINE_TYPE(GeanyEntryAction, geany_entry_action, GTK_TYPE_ACTION)
58 static GtkWidget *geany_entry_action_create_tool_item(GtkAction *action)
60 GtkWidget *toolitem;
61 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
63 priv->entry = gtk_entry_new();
64 if (priv->numeric)
65 gtk_entry_set_width_chars(GTK_ENTRY(priv->entry), 9);
67 ui_entry_add_clear_icon(GTK_ENTRY(priv->entry));
69 gtk_widget_show(priv->entry);
71 toolitem = g_object_new(GTK_TYPE_TOOL_ITEM, NULL);
72 gtk_container_add(GTK_CONTAINER(toolitem), priv->entry);
74 return toolitem;
78 static void delegate_entry_activate_cb(GtkEntry *entry, GeanyEntryAction *action)
80 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
81 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
83 g_signal_emit(action, signals[ENTRY_ACTIVATE], 0, text);
87 static void delegate_entry_changed_cb(GtkEditable *editable, GeanyEntryAction *action)
89 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
90 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
92 g_signal_emit(action, signals[ENTRY_CHANGED], 0, text);
96 static void geany_entry_action_connect_proxy(GtkAction *action, GtkWidget *widget)
98 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
100 if (priv->numeric)
101 g_signal_connect(priv->entry, "insert-text",
102 G_CALLBACK(ui_editable_insert_text_callback), NULL);
103 g_signal_connect(priv->entry, "changed", G_CALLBACK(delegate_entry_changed_cb), action);
104 g_signal_connect(priv->entry, "activate", G_CALLBACK(delegate_entry_activate_cb), action);
106 GTK_ACTION_CLASS(geany_entry_action_parent_class)->connect_proxy(action, widget);
110 static void geany_entry_action_class_init(GeanyEntryActionClass *klass)
112 GtkActionClass *action_class = GTK_ACTION_CLASS(klass);
114 action_class->connect_proxy = geany_entry_action_connect_proxy;
115 action_class->create_tool_item = geany_entry_action_create_tool_item;
116 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
118 g_type_class_add_private(klass, sizeof(GeanyEntryActionPrivate));
120 signals[ENTRY_CHANGED] = g_signal_new("entry-changed",
121 G_TYPE_FROM_CLASS(klass),
122 (GSignalFlags) 0,
125 NULL,
126 g_cclosure_marshal_VOID__STRING,
127 G_TYPE_NONE, 1, G_TYPE_STRING);
128 signals[ENTRY_ACTIVATE] = g_signal_new("entry-activate",
129 G_TYPE_FROM_CLASS(klass),
130 (GSignalFlags) 0,
133 NULL,
134 g_cclosure_marshal_VOID__STRING,
135 G_TYPE_NONE, 1, G_TYPE_STRING);
139 static void geany_entry_action_init(GeanyEntryAction *action)
141 GeanyEntryActionPrivate *priv;
143 action->priv = G_TYPE_INSTANCE_GET_PRIVATE(action,
144 GEANY_ENTRY_ACTION_TYPE, GeanyEntryActionPrivate);
146 priv = action->priv;
147 priv->entry = NULL;
148 priv->numeric = FALSE;
152 GtkAction *geany_entry_action_new(const gchar *name, const gchar *label,
153 const gchar *tooltip, gboolean numeric)
155 GtkAction *action = g_object_new(GEANY_ENTRY_ACTION_TYPE,
156 "name", name,
157 "label", label,
158 "tooltip", tooltip,
159 NULL);
160 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
162 priv->numeric = numeric;
164 return action;