Merge pull request #3648 from techee/nim_lexer
[geany-mirror.git] / src / geanyentryaction.c
blobd1670196421cd95af4bac0b9bea482b4398aaeb4
1 /*
2 * geanyentryaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* GtkAction subclass to provide a GtkEntry in a toolbar.
22 * This class is missing the action_create_menu_item() function and so can't be
23 * used for creating menu items. */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "geanyentryaction.h"
31 #include "ui_utils.h"
33 #include <ctype.h>
36 typedef struct _GeanyEntryActionPrivate GeanyEntryActionPrivate;
38 #define GEANY_ENTRY_ACTION_GET_PRIVATE(obj) (GEANY_ENTRY_ACTION(obj)->priv)
41 struct _GeanyEntryActionPrivate
43 GtkWidget *entry;
44 gboolean numeric;
47 enum
49 ENTRY_ACTIVATE,
50 ENTRY_ACTIVATE_BACKWARD,
51 ENTRY_CHANGED,
53 LAST_SIGNAL
55 static guint signals[LAST_SIGNAL];
58 G_DEFINE_TYPE(GeanyEntryAction, geany_entry_action, GTK_TYPE_ACTION)
61 static GtkWidget *geany_entry_action_create_tool_item(GtkAction *action)
63 GtkWidget *toolitem;
64 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
66 priv->entry = gtk_entry_new();
67 if (priv->numeric)
68 gtk_entry_set_width_chars(GTK_ENTRY(priv->entry), 9);
70 ui_entry_add_clear_icon(GTK_ENTRY(priv->entry));
71 ui_entry_add_activate_backward_signal(GTK_ENTRY(priv->entry));
73 gtk_widget_show(priv->entry);
75 toolitem = g_object_new(GTK_TYPE_TOOL_ITEM, NULL);
76 gtk_container_add(GTK_CONTAINER(toolitem), priv->entry);
78 return toolitem;
82 static void delegate_entry_activate_cb(GtkEntry *entry, GeanyEntryAction *action)
84 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
85 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
87 g_signal_emit(action, signals[ENTRY_ACTIVATE], 0, text);
91 static void delegate_entry_activate_backward_cb(GtkEntry *entry, GeanyEntryAction *action)
93 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
94 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
96 g_signal_emit(action, signals[ENTRY_ACTIVATE_BACKWARD], 0, text);
100 static void delegate_entry_changed_cb(GtkEditable *editable, GeanyEntryAction *action)
102 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
103 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
105 g_signal_emit(action, signals[ENTRY_CHANGED], 0, text);
109 static void geany_entry_action_connect_proxy(GtkAction *action, GtkWidget *widget)
111 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
113 /* make sure not to connect handlers twice */
114 if (! g_object_get_data(G_OBJECT(widget), "gea-connected"))
116 if (priv->numeric)
117 g_signal_connect(priv->entry, "insert-text",
118 G_CALLBACK(ui_editable_insert_text_callback), NULL);
119 g_signal_connect(priv->entry, "changed", G_CALLBACK(delegate_entry_changed_cb), action);
120 g_signal_connect(priv->entry, "activate", G_CALLBACK(delegate_entry_activate_cb), action);
121 g_signal_connect(priv->entry, "activate-backward",
122 G_CALLBACK(delegate_entry_activate_backward_cb), action);
124 g_object_set_data(G_OBJECT(widget), "gea-connected", action /* anything non-NULL */);
127 GTK_ACTION_CLASS(geany_entry_action_parent_class)->connect_proxy(action, widget);
131 static void geany_entry_action_class_init(GeanyEntryActionClass *klass)
133 GtkActionClass *action_class = GTK_ACTION_CLASS(klass);
135 action_class->connect_proxy = geany_entry_action_connect_proxy;
136 action_class->create_tool_item = geany_entry_action_create_tool_item;
137 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
139 g_type_class_add_private(klass, sizeof(GeanyEntryActionPrivate));
141 signals[ENTRY_CHANGED] = g_signal_new("entry-changed",
142 G_TYPE_FROM_CLASS(klass),
143 G_SIGNAL_RUN_LAST,
145 NULL,
146 NULL,
147 g_cclosure_marshal_VOID__STRING,
148 G_TYPE_NONE, 1, G_TYPE_STRING);
149 signals[ENTRY_ACTIVATE] = g_signal_new("entry-activate",
150 G_TYPE_FROM_CLASS(klass),
151 G_SIGNAL_RUN_LAST,
153 NULL,
154 NULL,
155 g_cclosure_marshal_VOID__STRING,
156 G_TYPE_NONE, 1, G_TYPE_STRING);
157 signals[ENTRY_ACTIVATE_BACKWARD] = g_signal_new("entry-activate-backward",
158 G_TYPE_FROM_CLASS(klass),
159 G_SIGNAL_RUN_LAST,
161 NULL,
162 NULL,
163 g_cclosure_marshal_VOID__STRING,
164 G_TYPE_NONE, 1, G_TYPE_STRING);
168 static void geany_entry_action_init(GeanyEntryAction *action)
170 GeanyEntryActionPrivate *priv;
172 action->priv = G_TYPE_INSTANCE_GET_PRIVATE(action,
173 GEANY_ENTRY_ACTION_TYPE, GeanyEntryActionPrivate);
175 priv = action->priv;
176 priv->entry = NULL;
177 priv->numeric = FALSE;
181 GtkAction *geany_entry_action_new(const gchar *name, const gchar *label,
182 const gchar *tooltip, gboolean numeric)
184 GtkAction *action = g_object_new(GEANY_ENTRY_ACTION_TYPE,
185 "name", name,
186 "label", label,
187 "tooltip", tooltip,
188 NULL);
189 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
191 priv->numeric = numeric;
193 return action;