Enforce filetype extension for user tag files
[geany-mirror.git] / src / geanyentryaction.c
blobb4986335b573316593d0878264ea37ecd64c6299
1 /*
2 * geanyentryaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2008-2011 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 version 2 of the License, or
10 * (at your option) any later version.
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;
43 gboolean connected;
46 enum
48 ENTRY_ACTIVATE,
49 ENTRY_ACTIVATE_BACKWARD,
50 ENTRY_CHANGED,
52 LAST_SIGNAL
54 static guint signals[LAST_SIGNAL];
57 G_DEFINE_TYPE(GeanyEntryAction, geany_entry_action, GTK_TYPE_ACTION)
60 static GtkWidget *geany_entry_action_create_tool_item(GtkAction *action)
62 GtkWidget *toolitem;
63 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
65 priv->entry = gtk_entry_new();
66 if (priv->numeric)
67 gtk_entry_set_width_chars(GTK_ENTRY(priv->entry), 9);
69 ui_entry_add_clear_icon(GTK_ENTRY(priv->entry));
70 ui_entry_add_activate_backward_signal(GTK_ENTRY(priv->entry));
72 gtk_widget_show(priv->entry);
74 toolitem = g_object_new(GTK_TYPE_TOOL_ITEM, NULL);
75 gtk_container_add(GTK_CONTAINER(toolitem), priv->entry);
77 return toolitem;
81 static void delegate_entry_activate_cb(GtkEntry *entry, GeanyEntryAction *action)
83 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
84 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
86 g_signal_emit(action, signals[ENTRY_ACTIVATE], 0, text);
90 static void delegate_entry_activate_backward_cb(GtkEntry *entry, GeanyEntryAction *action)
92 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
93 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
95 g_signal_emit(action, signals[ENTRY_ACTIVATE_BACKWARD], 0, text);
99 static void delegate_entry_changed_cb(GtkEditable *editable, GeanyEntryAction *action)
101 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
102 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
104 g_signal_emit(action, signals[ENTRY_CHANGED], 0, text);
108 static void geany_entry_action_connect_proxy(GtkAction *action, GtkWidget *widget)
110 GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
112 /* make sure not to connect handlers twice */
113 if (! priv->connected)
115 if (priv->numeric)
116 g_signal_connect(priv->entry, "insert-text",
117 G_CALLBACK(ui_editable_insert_text_callback), NULL);
118 g_signal_connect(priv->entry, "changed", G_CALLBACK(delegate_entry_changed_cb), action);
119 g_signal_connect(priv->entry, "activate", G_CALLBACK(delegate_entry_activate_cb), action);
120 g_signal_connect(priv->entry, "activate-backward",
121 G_CALLBACK(delegate_entry_activate_backward_cb), action);
123 priv->connected = TRUE;
126 GTK_ACTION_CLASS(geany_entry_action_parent_class)->connect_proxy(action, widget);
130 static void geany_entry_action_class_init(GeanyEntryActionClass *klass)
132 GtkActionClass *action_class = GTK_ACTION_CLASS(klass);
134 action_class->connect_proxy = geany_entry_action_connect_proxy;
135 action_class->create_tool_item = geany_entry_action_create_tool_item;
136 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
138 g_type_class_add_private(klass, sizeof(GeanyEntryActionPrivate));
140 signals[ENTRY_CHANGED] = g_signal_new("entry-changed",
141 G_TYPE_FROM_CLASS(klass),
142 G_SIGNAL_RUN_LAST,
144 NULL,
145 NULL,
146 g_cclosure_marshal_VOID__STRING,
147 G_TYPE_NONE, 1, G_TYPE_STRING);
148 signals[ENTRY_ACTIVATE] = g_signal_new("entry-activate",
149 G_TYPE_FROM_CLASS(klass),
150 G_SIGNAL_RUN_LAST,
152 NULL,
153 NULL,
154 g_cclosure_marshal_VOID__STRING,
155 G_TYPE_NONE, 1, G_TYPE_STRING);
156 signals[ENTRY_ACTIVATE_BACKWARD] = g_signal_new("entry-activate-backward",
157 G_TYPE_FROM_CLASS(klass),
158 G_SIGNAL_RUN_LAST,
160 NULL,
161 NULL,
162 g_cclosure_marshal_VOID__STRING,
163 G_TYPE_NONE, 1, G_TYPE_STRING);
167 static void geany_entry_action_init(GeanyEntryAction *action)
169 GeanyEntryActionPrivate *priv;
171 action->priv = G_TYPE_INSTANCE_GET_PRIVATE(action,
172 GEANY_ENTRY_ACTION_TYPE, GeanyEntryActionPrivate);
174 priv = action->priv;
175 priv->entry = NULL;
176 priv->numeric = FALSE;
177 priv->connected = 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;