2 * geanyentryaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2008-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 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"
37 typedef struct _GeanyEntryActionPrivate GeanyEntryActionPrivate
;
39 #define GEANY_ENTRY_ACTION_GET_PRIVATE(obj) (GEANY_ENTRY_ACTION(obj)->priv)
42 struct _GeanyEntryActionPrivate
52 ENTRY_ACTIVATE_BACKWARD
,
57 static guint signals
[LAST_SIGNAL
];
60 G_DEFINE_TYPE(GeanyEntryAction
, geany_entry_action
, GTK_TYPE_ACTION
)
63 static GtkWidget
*geany_entry_action_create_tool_item(GtkAction
*action
)
66 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
68 priv
->entry
= gtk_entry_new();
70 gtk_entry_set_width_chars(GTK_ENTRY(priv
->entry
), 9);
72 ui_entry_add_clear_icon(GTK_ENTRY(priv
->entry
));
73 ui_entry_add_activate_backward_signal(GTK_ENTRY(priv
->entry
));
75 gtk_widget_show(priv
->entry
);
77 toolitem
= g_object_new(GTK_TYPE_TOOL_ITEM
, NULL
);
78 gtk_container_add(GTK_CONTAINER(toolitem
), priv
->entry
);
84 static void delegate_entry_activate_cb(GtkEntry
*entry
, GeanyEntryAction
*action
)
86 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
87 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
89 g_signal_emit(action
, signals
[ENTRY_ACTIVATE
], 0, text
);
93 static void delegate_entry_activate_backward_cb(GtkEntry
*entry
, GeanyEntryAction
*action
)
95 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
96 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
98 g_signal_emit(action
, signals
[ENTRY_ACTIVATE_BACKWARD
], 0, text
);
102 static void delegate_entry_changed_cb(GtkEditable
*editable
, GeanyEntryAction
*action
)
104 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
105 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
107 g_signal_emit(action
, signals
[ENTRY_CHANGED
], 0, text
);
111 static void geany_entry_action_connect_proxy(GtkAction
*action
, GtkWidget
*widget
)
113 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
115 /* make sure not to connect handlers twice */
116 if (! priv
->connected
)
119 g_signal_connect(priv
->entry
, "insert-text",
120 G_CALLBACK(ui_editable_insert_text_callback
), NULL
);
121 g_signal_connect(priv
->entry
, "changed", G_CALLBACK(delegate_entry_changed_cb
), action
);
122 g_signal_connect(priv
->entry
, "activate", G_CALLBACK(delegate_entry_activate_cb
), action
);
123 g_signal_connect(priv
->entry
, "activate-backward",
124 G_CALLBACK(delegate_entry_activate_backward_cb
), action
);
126 priv
->connected
= TRUE
;
129 GTK_ACTION_CLASS(geany_entry_action_parent_class
)->connect_proxy(action
, widget
);
133 static void geany_entry_action_class_init(GeanyEntryActionClass
*klass
)
135 GtkActionClass
*action_class
= GTK_ACTION_CLASS(klass
);
137 action_class
->connect_proxy
= geany_entry_action_connect_proxy
;
138 action_class
->create_tool_item
= geany_entry_action_create_tool_item
;
139 action_class
->toolbar_item_type
= GTK_TYPE_MENU_TOOL_BUTTON
;
141 g_type_class_add_private(klass
, sizeof(GeanyEntryActionPrivate
));
143 signals
[ENTRY_CHANGED
] = g_signal_new("entry-changed",
144 G_TYPE_FROM_CLASS(klass
),
149 g_cclosure_marshal_VOID__STRING
,
150 G_TYPE_NONE
, 1, G_TYPE_STRING
);
151 signals
[ENTRY_ACTIVATE
] = g_signal_new("entry-activate",
152 G_TYPE_FROM_CLASS(klass
),
157 g_cclosure_marshal_VOID__STRING
,
158 G_TYPE_NONE
, 1, G_TYPE_STRING
);
159 signals
[ENTRY_ACTIVATE_BACKWARD
] = g_signal_new("entry-activate-backward",
160 G_TYPE_FROM_CLASS(klass
),
165 g_cclosure_marshal_VOID__STRING
,
166 G_TYPE_NONE
, 1, G_TYPE_STRING
);
170 static void geany_entry_action_init(GeanyEntryAction
*action
)
172 GeanyEntryActionPrivate
*priv
;
174 action
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(action
,
175 GEANY_ENTRY_ACTION_TYPE
, GeanyEntryActionPrivate
);
179 priv
->numeric
= FALSE
;
180 priv
->connected
= FALSE
;
184 GtkAction
*geany_entry_action_new(const gchar
*name
, const gchar
*label
,
185 const gchar
*tooltip
, gboolean numeric
)
187 GtkAction
*action
= g_object_new(GEANY_ENTRY_ACTION_TYPE
,
192 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
194 priv
->numeric
= numeric
;