1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* gtk-combo-text.c - A combo box for selecting from a list.
* Copyright 2000, 2001, Ximian, Inc.
*
* Authors:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License, version 2, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <config.h>
#include <ctype.h>
#include <string.h>
#include <gtk/gtk.h>
#include "e-util/e-util.h"
#include "gal-combo-text.h"
#define PARENT_TYPE GAL_COMBO_BOX_TYPE
static GtkObjectClass *gal_combo_text_parent_class;
static gboolean cb_pop_down (GtkWidget *w, GtkWidget *pop_down,
gpointer dummy);
static void list_unselect_cb (GtkWidget *list, GtkWidget *child,
gpointer data);
static void update_list_selection (GalComboText *ct, const gchar *text);
static void
gal_combo_text_destroy (GtkObject *object)
{
GalComboText *ct = GAL_COMBO_TEXT (object);
if (ct->elements != NULL) {
g_hash_table_destroy (ct->elements);
ct->elements = NULL;
}
if (ct->list != NULL) {
g_signal_handlers_disconnect_matched (ct,
G_SIGNAL_MATCH_FUNC,
0, 0, NULL,
cb_pop_down, NULL);
g_signal_handlers_disconnect_matched (ct->list,
G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
0, 0, NULL,
list_unselect_cb, ct);
ct->list = NULL;
}
(*gal_combo_text_parent_class->destroy) (object);
}
static void
gal_combo_text_class_init (GtkObjectClass *object_class)
{
object_class->destroy = &gal_combo_text_destroy;
gal_combo_text_parent_class = g_type_class_ref (PARENT_TYPE);
}
static void
gal_combo_text_init (GalComboText *object)
{
}
E_MAKE_TYPE (gal_combo_text,
"GalComboText",
GalComboText,
gal_combo_text_class_init,
gal_combo_text_init,
PARENT_TYPE)
static gint
strcase_equal (gconstpointer v, gconstpointer v2)
{
return g_ascii_strcasecmp ((const gchar*) v, (const gchar*)v2) == 0;
}
static guint
strcase_hash (gconstpointer v)
{
const unsigned char *s = (const unsigned char *)v;
const unsigned char *p;
guint h = 0, g;
for(p = s; *p != '\0'; p += 1) {
h = ( h << 4 ) + g_ascii_tolower (*p);
if ( ( g = h & 0xf0000000 ) ) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
}
/**
* gal_combo_text_set_case_sensitive
* @combo_text: ComboText widget
* @val: make case sensitive if TRUE
*
* Specifies whether the text entered into the GtkEntry field and the text
* in the list items is case sensitive. Because the values are stored in a
* hash, it is not legal to change case sensitivity when the list contains
* elements.
*
* Returns: The function returns -1 if request could not be honored. On
* success, it returns 0.
*/
gint
gal_combo_text_set_case_sensitive (GalComboText *combo, gboolean val)
{
if (combo->elements
&& g_hash_table_size (combo->elements) > 0
&& val != combo->case_sensitive)
return -1;
else {
combo->case_sensitive = val;
if (val != combo->case_sensitive) {
GHashFunc hashfunc;
GCompareFunc comparefunc;
g_hash_table_destroy (combo->elements);
if (combo->case_sensitive) {
hashfunc = g_str_hash;
comparefunc = g_str_equal;
} else {
hashfunc = strcase_hash;
comparefunc = strcase_equal;
}
combo->elements = g_hash_table_new (hashfunc,
comparefunc);
}
return 0;
}
}
static void
entry_activate_cb (GtkWidget *entry, gpointer data)
{
GalComboText *combo = GAL_COMBO_TEXT (data);
update_list_selection (combo,
gtk_entry_get_text (GTK_ENTRY (combo->entry)));
}
static void
list_select_cb (GtkWidget *list, GtkWidget *child, gpointer data)
{
GalComboText *combo = GAL_COMBO_TEXT (data);
GtkEntry *entry = GTK_ENTRY (combo->entry);
gchar *value = (gchar*) g_object_get_data (G_OBJECT (child), "value");
g_return_if_fail (entry && value);
if (combo->cached_entry == child)
combo->cached_entry = NULL;
gtk_entry_set_text (entry, value);
gtk_signal_handler_block_by_func (GTK_OBJECT (entry),
GTK_SIGNAL_FUNC (entry_activate_cb),
(gpointer) combo);
g_signal_emit_by_name (entry, "activate");
gtk_signal_handler_unblock_by_func (GTK_OBJECT (entry),
GTK_SIGNAL_FUNC (entry_activate_cb),
(gpointer) combo);
gal_combo_box_popup_hide (GAL_COMBO_BOX (data));
}
static void
list_unselect_cb (GtkWidget *list, GtkWidget *child, gpointer data)
{
if (GTK_WIDGET_VISIBLE (list)) /* Undo interactive unselect */
gtk_list_select_child (GTK_LIST (list), child);
}
static void
cb_toggle (GtkWidget *child, gpointer data)
{
GalComboText *ct = GAL_COMBO_TEXT (data);
gtk_list_select_child (GTK_LIST (ct->list), child);
}
void
gal_combo_text_select_item (GalComboText *ct, int elem)
{
gtk_list_select_item (GTK_LIST(ct->list), elem);
}
static void
update_list_selection (GalComboText *ct, const gchar *text)
{
gpointer candidate;
GtkWidget *child;
gtk_signal_handler_block_by_func (GTK_OBJECT (ct->list),
GTK_SIGNAL_FUNC (list_select_cb),
(gpointer) ct);
gtk_signal_handler_block_by_func (GTK_OBJECT (ct->list),
GTK_SIGNAL_FUNC (list_unselect_cb),
(gpointer) ct);
gtk_list_unselect_all (GTK_LIST (ct->list));
candidate = g_hash_table_lookup (ct->elements, (gconstpointer) text);
if (candidate && GTK_IS_WIDGET (candidate)) {
child = GTK_WIDGET (candidate);
gtk_list_select_child (GTK_LIST (ct->list), child);
gtk_widget_grab_focus (child);
}
gtk_signal_handler_unblock_by_func (GTK_OBJECT (ct->list),
GTK_SIGNAL_FUNC (list_select_cb),
(gpointer) ct);
gtk_signal_handler_unblock_by_func (GTK_OBJECT (ct->list),
GTK_SIGNAL_FUNC (list_unselect_cb),
(gpointer) ct);
}
void
gal_combo_text_set_text (GalComboText *ct, const gchar *text)
{
gtk_entry_set_text (GTK_ENTRY (ct->entry), text);
update_list_selection (ct, text);
}
/*
* We can't just cache the old widget state on entry: If the pointer is
* dragged, we receive two enter-notify-events, and the original cached
* value would be overwritten with the GTK_STATE_ACTIVE we just set.
*
* However, we know that the gtklist only uses GTK_STATE_SELECTED and
* GTK_STATE_NORMAL. We're OK if we only cache those two.
*/
static gboolean
cb_enter (GtkWidget *w, GdkEventCrossing *event,
gpointer user)
{
GalComboText *ct = user;
GtkStateType state = GTK_WIDGET_STATE (w);
if (state == GTK_STATE_NORMAL || state == GTK_STATE_SELECTED) {
ct->cached_entry = w;
ct->cache_mouse_state = state;
}
if (state != GTK_STATE_SELECTED)
gtk_widget_set_state (w, GTK_STATE_ACTIVE);
return TRUE;
}
static gboolean
cb_exit (GtkWidget *w, GdkEventCrossing *event,
gpointer user)
{
GalComboText *ct = user;
if (ct->cached_entry == w)
gtk_widget_set_state (w, ct->cache_mouse_state);
return TRUE;
}
static gboolean
cb_pop_down (GtkWidget *w, GtkWidget *pop_down, gpointer dummy)
{
GalComboText *ct = GAL_COMBO_TEXT (w);
if (ct->cached_entry)
gtk_widget_set_state (ct->cached_entry, ct->cache_mouse_state);
ct->cached_entry = NULL;
return FALSE;
}
typedef struct {
GalComboText *ct;
gchar *value;
} WeakRefClosure;
static void
cb_remove_from_hash (gpointer data, GObject *where_object_was)
{
WeakRefClosure *closure = data;
if (closure->ct->elements)
g_hash_table_remove (closure->ct->elements, closure->value);
g_free (closure->value);
g_free (closure);
}
void
gal_combo_text_add_item (GalComboText *ct,
const gchar *item,
const gchar *value)
{
WeakRefClosure *weak_ref_closure;
GtkWidget *listitem;
gchar *value_copy;
g_return_if_fail (item);
if (!value)
value = item;
value_copy = g_strdup (value);
listitem = gtk_list_item_new_with_label (item);
gtk_widget_show (listitem);
g_object_set_data_full (G_OBJECT (listitem), "value",
value_copy, g_free);
g_signal_connect (listitem, "enter-notify-event",
G_CALLBACK (cb_enter),
(gpointer) ct);
g_signal_connect (listitem, "leave-notify-event",
G_CALLBACK (cb_exit),
(gpointer) ct);
g_signal_connect (listitem, "toggle",
G_CALLBACK (cb_toggle),
(gpointer) ct);
gtk_container_add (GTK_CONTAINER (ct->list),
listitem);
g_hash_table_insert (ct->elements, (gpointer)value_copy,
(gpointer) listitem);
weak_ref_closure = g_new (WeakRefClosure, 1);
weak_ref_closure->ct = ct;
weak_ref_closure->value = g_strdup (value_copy);
g_object_weak_ref (G_OBJECT (listitem),
cb_remove_from_hash,
weak_ref_closure);
}
static void
cb_list_mapped (GtkWidget *widget, gpointer user_data)
{
GtkList *list = GTK_LIST (widget);
if (g_list_length (list->selection) > 0)
gtk_widget_grab_focus (GTK_WIDGET ((list->selection->data)));
}
void
gal_combo_text_construct (GalComboText *ct, gboolean const is_scrolled)
{
GtkWidget *entry, *list, *scroll, *display_widget;
ct->case_sensitive = FALSE;
ct->elements = g_hash_table_new (&strcase_hash,
&strcase_equal);
/* Probably irrelevant, but lets be careful */
ct->cache_mouse_state = GTK_STATE_NORMAL;
ct->cached_entry = NULL;
entry = ct->entry = gtk_entry_new ();
list = ct->list = gtk_list_new ();
if (is_scrolled) {
display_widget = scroll = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scroll),
GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_add_with_viewport (
GTK_SCROLLED_WINDOW(scroll), list);
gtk_container_set_focus_hadjustment (
GTK_CONTAINER (list),
gtk_scrolled_window_get_hadjustment (
GTK_SCROLLED_WINDOW (scroll)));
gtk_container_set_focus_vadjustment (
GTK_CONTAINER (list),
gtk_scrolled_window_get_vadjustment (
GTK_SCROLLED_WINDOW (scroll)));
gtk_widget_set_usize (scroll, 0, 200); /* MAGIC NUMBER */
} else
display_widget = list;
g_signal_connect (entry, "activate",
G_CALLBACK (entry_activate_cb),
ct);
g_signal_connect (list, "select-child",
G_CALLBACK (list_select_cb),
ct);
g_signal_connect (list, "unselect-child",
G_CALLBACK (list_unselect_cb),
ct);
g_signal_connect (list, "map",
G_CALLBACK (cb_list_mapped), NULL);
gtk_widget_show (display_widget);
gtk_widget_show (entry);
gal_combo_box_construct (GAL_COMBO_BOX (ct), entry, display_widget);
g_signal_connect (ct, "pop_down_done",
G_CALLBACK (cb_pop_down), NULL);
}
GtkWidget*
gal_combo_text_new (gboolean const is_scrolled)
{
GalComboText *ct;
ct = g_object_new (GAL_COMBO_TEXT_TYPE, NULL);
gal_combo_text_construct (ct, is_scrolled);
return GTK_WIDGET (ct);
}
|