| 1 |
/* Entry/Entry Completion |
| 2 |
* |
| 3 |
* GtkEntryCompletion provides a mechanism for adding support for |
| 4 |
* completion in GtkEntry. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <glib/gi18n.h> |
| 9 |
#include <gtk/gtk.h> |
| 10 |
|
| 11 |
/* Creates a tree model containing the completions */ |
| 12 |
GtkTreeModel * |
| 13 |
create_completion_model (void) |
| 14 |
{ |
| 15 |
GtkListStore *store; |
| 16 |
GtkTreeIter iter; |
| 17 |
|
| 18 |
store = gtk_list_store_new (1, G_TYPE_STRING); |
| 19 |
|
| 20 |
/* Append one word */ |
| 21 |
gtk_list_store_append (store, &iter); |
| 22 |
gtk_list_store_set (store, &iter, 0, "GNOME", -1); |
| 23 |
|
| 24 |
/* Append another word */ |
| 25 |
gtk_list_store_append (store, &iter); |
| 26 |
gtk_list_store_set (store, &iter, 0, "total", -1); |
| 27 |
|
| 28 |
/* And another word */ |
| 29 |
gtk_list_store_append (store, &iter); |
| 30 |
gtk_list_store_set (store, &iter, 0, "totally", -1); |
| 31 |
|
| 32 |
return GTK_TREE_MODEL (store); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
GtkWidget * |
| 37 |
do_entry_completion (GtkWidget *do_widget) |
| 38 |
{ |
| 39 |
static GtkWidget *window = NULL; |
| 40 |
GtkWidget *vbox; |
| 41 |
GtkWidget *label; |
| 42 |
GtkWidget *entry; |
| 43 |
GtkEntryCompletion *completion; |
| 44 |
GtkTreeModel *completion_model; |
| 45 |
|
| 46 |
if (!window) |
| 47 |
{ |
| 48 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 49 |
gtk_window_set_screen (GTK_WINDOW (window), |
| 50 |
gtk_widget_get_screen (do_widget)); |
| 51 |
gtk_window_set_title (GTK_WINDOW (window), "Entry Completion"); |
| 52 |
gtk_window_set_resizable (GTK_WINDOW (window), FALSE); |
| 53 |
|
| 54 |
g_signal_connect (window, "destroy", |
| 55 |
G_CALLBACK (gtk_widget_destroyed), &window); |
| 56 |
|
| 57 |
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); |
| 58 |
gtk_container_add (GTK_CONTAINER (window), vbox); |
| 59 |
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5); |
| 60 |
|
| 61 |
label = gtk_label_new (NULL); |
| 62 |
gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example."); |
| 63 |
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); |
| 64 |
|
| 65 |
/* Create our entry */ |
| 66 |
entry = gtk_entry_new (); |
| 67 |
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0); |
| 68 |
|
| 69 |
/* Create the completion object */ |
| 70 |
completion = gtk_entry_completion_new (); |
| 71 |
|
| 72 |
/* Assign the completion to the entry */ |
| 73 |
gtk_entry_set_completion (GTK_ENTRY (entry), completion); |
| 74 |
g_object_unref (completion); |
| 75 |
|
| 76 |
/* Create a tree model and use it as the completion model */ |
| 77 |
completion_model = create_completion_model (); |
| 78 |
gtk_entry_completion_set_model (completion, completion_model); |
| 79 |
g_object_unref (completion_model); |
| 80 |
|
| 81 |
/* Use model column 0 as the text column */ |
| 82 |
gtk_entry_completion_set_text_column (completion, 0); |
| 83 |
} |
| 84 |
|
| 85 |
if (!gtk_widget_get_visible (window)) |
| 86 |
gtk_widget_show_all (window); |
| 87 |
else |
| 88 |
gtk_widget_destroy (window); |
| 89 |
|
| 90 |
return window; |
| 91 |
} |