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. */
29 #include "geanyentryaction.h"
36 typedef struct _GeanyEntryActionPrivate GeanyEntryActionPrivate
;
38 #define GEANY_ENTRY_ACTION_GET_PRIVATE(obj) (GEANY_ENTRY_ACTION(obj)->priv)
41 struct _GeanyEntryActionPrivate
51 ENTRY_ACTIVATE_BACKWARD
,
56 static guint signals
[LAST_SIGNAL
];
59 G_DEFINE_TYPE(GeanyEntryAction
, geany_entry_action
, GTK_TYPE_ACTION
)
62 static GtkWidget
*geany_entry_action_create_tool_item(GtkAction
*action
)
65 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
67 priv
->entry
= gtk_entry_new();
69 gtk_entry_set_width_chars(GTK_ENTRY(priv
->entry
), 9);
71 ui_entry_add_clear_icon(GTK_ENTRY(priv
->entry
));
72 ui_entry_add_activate_backward_signal(GTK_ENTRY(priv
->entry
));
74 gtk_widget_show(priv
->entry
);
76 toolitem
= g_object_new(GTK_TYPE_TOOL_ITEM
, NULL
);
77 gtk_container_add(GTK_CONTAINER(toolitem
), priv
->entry
);
83 static void delegate_entry_activate_cb(GtkEntry
*entry
, GeanyEntryAction
*action
)
85 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
86 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
88 g_signal_emit(action
, signals
[ENTRY_ACTIVATE
], 0, text
);
92 static void delegate_entry_activate_backward_cb(GtkEntry
*entry
, GeanyEntryAction
*action
)
94 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
95 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
97 g_signal_emit(action
, signals
[ENTRY_ACTIVATE_BACKWARD
], 0, text
);
101 static void delegate_entry_changed_cb(GtkEditable
*editable
, GeanyEntryAction
*action
)
103 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
104 const gchar
*text
= gtk_entry_get_text(GTK_ENTRY(priv
->entry
));
106 g_signal_emit(action
, signals
[ENTRY_CHANGED
], 0, text
);
110 static void geany_entry_action_connect_proxy(GtkAction
*action
, GtkWidget
*widget
)
112 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
114 /* make sure not to connect handlers twice */
115 if (! priv
->connected
)
118 g_signal_connect(priv
->entry
, "insert-text",
119 G_CALLBACK(ui_editable_insert_text_callback
), NULL
);
120 g_signal_connect(priv
->entry
, "changed", G_CALLBACK(delegate_entry_changed_cb
), action
);
121 g_signal_connect(priv
->entry
, "activate", G_CALLBACK(delegate_entry_activate_cb
), action
);
122 g_signal_connect(priv
->entry
, "activate-backward",
123 G_CALLBACK(delegate_entry_activate_backward_cb
), action
);
125 priv
->connected
= TRUE
;
128 GTK_ACTION_CLASS(geany_entry_action_parent_class
)->connect_proxy(action
, widget
);
132 static void geany_entry_action_class_init(GeanyEntryActionClass
*klass
)
134 GtkActionClass
*action_class
= GTK_ACTION_CLASS(klass
);
136 action_class
->connect_proxy
= geany_entry_action_connect_proxy
;
137 action_class
->create_tool_item
= geany_entry_action_create_tool_item
;
138 action_class
->toolbar_item_type
= GTK_TYPE_MENU_TOOL_BUTTON
;
140 g_type_class_add_private(klass
, sizeof(GeanyEntryActionPrivate
));
142 signals
[ENTRY_CHANGED
] = g_signal_new("entry-changed",
143 G_TYPE_FROM_CLASS(klass
),
148 g_cclosure_marshal_VOID__STRING
,
149 G_TYPE_NONE
, 1, G_TYPE_STRING
);
150 signals
[ENTRY_ACTIVATE
] = g_signal_new("entry-activate",
151 G_TYPE_FROM_CLASS(klass
),
156 g_cclosure_marshal_VOID__STRING
,
157 G_TYPE_NONE
, 1, G_TYPE_STRING
);
158 signals
[ENTRY_ACTIVATE_BACKWARD
] = g_signal_new("entry-activate-backward",
159 G_TYPE_FROM_CLASS(klass
),
164 g_cclosure_marshal_VOID__STRING
,
165 G_TYPE_NONE
, 1, G_TYPE_STRING
);
169 static void geany_entry_action_init(GeanyEntryAction
*action
)
171 GeanyEntryActionPrivate
*priv
;
173 action
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(action
,
174 GEANY_ENTRY_ACTION_TYPE
, GeanyEntryActionPrivate
);
178 priv
->numeric
= FALSE
;
179 priv
->connected
= FALSE
;
183 GtkAction
*geany_entry_action_new(const gchar
*name
, const gchar
*label
,
184 const gchar
*tooltip
, gboolean numeric
)
186 GtkAction
*action
= g_object_new(GEANY_ENTRY_ACTION_TYPE
,
191 GeanyEntryActionPrivate
*priv
= GEANY_ENTRY_ACTION_GET_PRIVATE(action
);
193 priv
->numeric
= numeric
;