2 * geanyentryaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2008-2009 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. */
30 #include "geanyentryaction.h"
34 typedef struct _GeanyEntryActionPrivate GeanyEntryActionPrivate
;
36 #define GEANY_ENTRY_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
37 GEANY_ENTRY_ACTION_TYPE, GeanyEntryActionPrivate))
40 struct _GeanyEntryActionPrivate
53 static guint signals
[LAST_SIGNAL
];
56 G_DEFINE_TYPE(GeanyEntryAction
, geany_entry_action
, GTK_TYPE_ACTION
);
59 static GtkWidget
*geany_entry_action_create_tool_item(GtkAction
*action
)
62 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
64 priv
->entry
= gtk_entry_new();
66 gtk_entry_set_width_chars(GTK_ENTRY(priv
->entry
), 9);
68 ui_entry_add_clear_icon(GTK_ENTRY(priv
->entry
));
70 gtk_widget_show(priv
->entry
);
72 toolitem
= g_object_new(GTK_TYPE_TOOL_ITEM
, NULL
);
73 gtk_container_add(GTK_CONTAINER(toolitem
), priv
->entry
);
79 static void delegate_entry_activate_cb(GtkEntry
*entry
, GeanyEntryAction
*action
)
81 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
82 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
84 g_signal_emit(action
, signals
[ENTRY_ACTIVATE
], 0, text
);
88 static void delegate_entry_changed_cb(GtkEditable
*editable
, GeanyEntryAction
*action
)
90 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
91 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
93 g_signal_emit(action
, signals
[ENTRY_CHANGED
], 0, text
);
97 static void entry_insert_text_cb(GtkEditable
*editable
, gchar
*new_text
, gint new_text_len
,
98 gint
*position
, GeanyEntryAction
*action
)
100 /* don't insert any text when it is not a digit */
101 if (! isdigit(*new_text
))
102 g_signal_stop_emission_by_name(editable
, "insert-text");
106 static void geany_entry_action_connect_proxy(GtkAction
*action
, GtkWidget
*widget
)
108 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
111 g_signal_connect(priv
->entry
, "insert-text", G_CALLBACK(entry_insert_text_cb
), action
);
112 g_signal_connect(priv
->entry
, "changed", G_CALLBACK(delegate_entry_changed_cb
), action
);
113 g_signal_connect(priv
->entry
, "activate", G_CALLBACK(delegate_entry_activate_cb
), action
);
115 GTK_ACTION_CLASS(geany_entry_action_parent_class
)->connect_proxy(action
, widget
);
119 static void geany_entry_action_class_init(GeanyEntryActionClass
*klass
)
121 GtkActionClass
*action_class
= GTK_ACTION_CLASS(klass
);
123 action_class
->connect_proxy
= geany_entry_action_connect_proxy
;
124 action_class
->create_tool_item
= geany_entry_action_create_tool_item
;
125 action_class
->toolbar_item_type
= GTK_TYPE_MENU_TOOL_BUTTON
;
127 g_type_class_add_private(klass
, sizeof(GeanyEntryActionPrivate
));
129 signals
[ENTRY_CHANGED
] = g_signal_new("entry-changed",
130 G_TYPE_FROM_CLASS(klass
),
135 g_cclosure_marshal_VOID__STRING
,
136 G_TYPE_NONE
, 1, G_TYPE_STRING
);
137 signals
[ENTRY_ACTIVATE
] = g_signal_new("entry-activate",
138 G_TYPE_FROM_CLASS(klass
),
143 g_cclosure_marshal_VOID__STRING
,
144 G_TYPE_NONE
, 1, G_TYPE_STRING
);
148 static void geany_entry_action_init(GeanyEntryAction
*action
)
150 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
153 priv
->numeric
= FALSE
;
157 GtkAction
*geany_entry_action_new(const gchar
*name
, const gchar
*label
,
158 const gchar
*tooltip
, gboolean numeric
)
160 GtkAction
*action
= g_object_new(GEANY_ENTRY_ACTION_TYPE
,
165 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
167 priv
->numeric
= numeric
;