Support more folding icon styles: arrows, +/- and no lines
[geany-mirror.git] / src / geanyentryaction.c
blob9b543957a186fe753a5d07961d6b10cb146743f9
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 entry_insert_text_cb(GtkEditable *editable, gchar *new_text, gint new_text_len,
97 gint *position, GeanyEntryAction *action)
99 /* don't insert any text when it is not a digit */
100 if (! isdigit(*new_text))
101 g_signal_stop_emission_by_name(editable, "insert-text");
105 static void geany_entry_action_connect_proxy(GtkAction *action, GtkWidget *widget)
107 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
109 if (priv->numeric)
110 g_signal_connect(priv->entry, "insert-text", G_CALLBACK(entry_insert_text_cb), action);
111 g_signal_connect(priv->entry, "changed", G_CALLBACK(delegate_entry_changed_cb), action);
112 g_signal_connect(priv->entry, "activate", G_CALLBACK(delegate_entry_activate_cb), action);
114 GTK_ACTION_CLASS(geany_entry_action_parent_class)->connect_proxy(action, widget);
118 static void geany_entry_action_class_init(GeanyEntryActionClass *klass)
120 GtkActionClass *action_class = GTK_ACTION_CLASS(klass);
122 action_class->connect_proxy = geany_entry_action_connect_proxy;
123 action_class->create_tool_item = geany_entry_action_create_tool_item;
124 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
126 g_type_class_add_private(klass, sizeof(GeanyEntryActionPrivate));
128 signals[ENTRY_CHANGED] = g_signal_new("entry-changed",
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);
136 signals[ENTRY_ACTIVATE] = g_signal_new("entry-activate",
137 G_TYPE_FROM_CLASS(klass),
138 (GSignalFlags) 0,
141 NULL,
142 g_cclosure_marshal_VOID__STRING,
143 G_TYPE_NONE, 1, G_TYPE_STRING);
147 static void geany_entry_action_init(GeanyEntryAction *action)
149 GeanyEntryActionPrivate *priv;
151 action->priv = G_TYPE_INSTANCE_GET_PRIVATE(action,
152 GEANY_ENTRY_ACTION_TYPE, GeanyEntryActionPrivate);
154 priv = action->priv;
155 priv->entry = NULL;
156 priv->numeric = FALSE;
160 GtkAction *geany_entry_action_new(const gchar *name, const gchar *label,
161 const gchar *tooltip, gboolean numeric)
163 GtkAction *action = g_object_new(GEANY_ENTRY_ACTION_TYPE,
164 "name", name,
165 "label", label,
166 "tooltip", tooltip,
167 NULL);
168 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
170 priv->numeric = numeric;
172 return action;