2005-07-13 05:44:22 +00:00
|
|
|
/* testentrycompletion.c
|
|
|
|
* Copyright (C) 2004 Red Hat, Inc.
|
|
|
|
* Author: Matthias Clasen
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2005-07-13 05:44:22 +00:00
|
|
|
*/
|
2004-03-02 19:59:20 +00:00
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2004-03-08 00:08:11 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-03-02 19:59:20 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
/* Don't copy this bad example; inline RGB data is always a better
|
|
|
|
* idea than inline XPMs.
|
|
|
|
*/
|
|
|
|
static char *book_closed_xpm[] = {
|
|
|
|
"16 16 6 1",
|
|
|
|
" c None s None",
|
|
|
|
". c black",
|
|
|
|
"X c red",
|
|
|
|
"o c yellow",
|
|
|
|
"O c #808080",
|
|
|
|
"# c white",
|
|
|
|
" ",
|
|
|
|
" .. ",
|
|
|
|
" ..XX. ",
|
|
|
|
" ..XXXXX. ",
|
|
|
|
" ..XXXXXXXX. ",
|
|
|
|
".ooXXXXXXXXX. ",
|
|
|
|
"..ooXXXXXXXXX. ",
|
|
|
|
".X.ooXXXXXXXXX. ",
|
|
|
|
".XX.ooXXXXXX.. ",
|
|
|
|
" .XX.ooXXX..#O ",
|
|
|
|
" .XX.oo..##OO. ",
|
|
|
|
" .XX..##OO.. ",
|
|
|
|
" .X.#OO.. ",
|
|
|
|
" ..O.. ",
|
|
|
|
" .. ",
|
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/* Creates a tree model containing the completions */
|
|
|
|
GtkTreeModel *
|
|
|
|
create_simple_completion_model (void)
|
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
store = gtk_list_store_new (1, G_TYPE_STRING);
|
|
|
|
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "GNOME", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "gnominious", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "Gnomonic projection", -1);
|
|
|
|
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "total", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totally", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "toto", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "tottery", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totterer", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "Totten trust", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totipotent", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totipotency", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totemism", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totem pole", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "Totara", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totalizer", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totalizator", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totalitarianism", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "total parenteral nutrition", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "total hysterectomy", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "total eclipse", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "Totipresence", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "Totipalmi", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "zombie", -1);
|
2005-09-13 15:08:43 +00:00
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "a\303\246x", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "a\303\246y", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "a\303\246z", -1);
|
2004-03-02 19:59:20 +00:00
|
|
|
|
|
|
|
return GTK_TREE_MODEL (store);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates a tree model containing the completions */
|
|
|
|
GtkTreeModel *
|
|
|
|
create_completion_model (void)
|
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
|
|
|
pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)book_closed_xpm);
|
|
|
|
|
|
|
|
store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
|
|
|
|
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambient", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambidextrously", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambidexter", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambiguity", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "American Party", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "American mountain ash", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "amelioration", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Amelia Earhart", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Totten trust", -1);
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Laminated arch", -1);
|
|
|
|
|
|
|
|
return GTK_TREE_MODEL (store);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
match_func (GtkEntryCompletion *completion,
|
|
|
|
const gchar *key,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2004-03-08 00:08:11 +00:00
|
|
|
gchar *item = NULL;
|
|
|
|
GtkTreeModel *model;
|
|
|
|
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
model = gtk_entry_completion_get_model (completion);
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, iter, 1, &item, -1);
|
|
|
|
|
|
|
|
if (item != NULL)
|
|
|
|
{
|
|
|
|
g_print ("compare %s %s\n", key, item);
|
|
|
|
if (strncmp (key, item, strlen (key)) == 0)
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
g_free (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2004-03-02 19:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
activated_cb (GtkEntryCompletion *completion,
|
|
|
|
gint index,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_print ("action activated: %d\n", index);
|
|
|
|
}
|
|
|
|
|
2004-03-08 00:08:11 +00:00
|
|
|
static gint timer_count = 0;
|
|
|
|
|
|
|
|
static gchar *dynamic_completions[] = {
|
|
|
|
"GNOME",
|
|
|
|
"gnominious",
|
|
|
|
"Gnomonic projection",
|
|
|
|
"total",
|
|
|
|
"totally",
|
|
|
|
"toto",
|
|
|
|
"tottery",
|
|
|
|
"totterer",
|
|
|
|
"Totten trust",
|
|
|
|
"totipotent",
|
|
|
|
"totipotency",
|
|
|
|
"totemism",
|
|
|
|
"totem pole",
|
|
|
|
"Totara",
|
|
|
|
"totalizer",
|
|
|
|
"totalizator",
|
|
|
|
"totalitarianism",
|
|
|
|
"total parenteral nutrition",
|
|
|
|
"total hysterectomy",
|
|
|
|
"total eclipse",
|
|
|
|
"Totipresence",
|
|
|
|
"Totipalmi",
|
|
|
|
"zombie"
|
|
|
|
};
|
|
|
|
|
|
|
|
static gint
|
|
|
|
animation_timer (GtkEntryCompletion *completion)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gint n_completions = G_N_ELEMENTS (dynamic_completions);
|
|
|
|
gint n;
|
2004-10-26 15:53:32 +00:00
|
|
|
static GtkListStore *old_store = NULL;
|
2004-03-08 00:08:11 +00:00
|
|
|
GtkListStore *store = GTK_LIST_STORE (gtk_entry_completion_get_model (completion));
|
|
|
|
|
2004-10-26 15:53:32 +00:00
|
|
|
if (timer_count % 10 == 0)
|
2004-03-08 00:08:11 +00:00
|
|
|
{
|
2004-10-26 15:53:32 +00:00
|
|
|
if (!old_store)
|
|
|
|
{
|
|
|
|
g_print ("removing model!\n");
|
|
|
|
|
|
|
|
old_store = g_object_ref (gtk_entry_completion_get_model (completion));
|
|
|
|
gtk_entry_completion_set_model (completion, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_print ("readding model!\n");
|
|
|
|
|
2004-10-31 18:26:49 +00:00
|
|
|
gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (old_store));
|
2004-10-26 15:53:32 +00:00
|
|
|
g_object_unref (old_store);
|
|
|
|
old_store = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_count ++;
|
|
|
|
return TRUE;
|
2004-03-08 00:08:11 +00:00
|
|
|
}
|
2004-10-26 15:53:32 +00:00
|
|
|
|
|
|
|
if (!old_store)
|
2004-03-08 00:08:11 +00:00
|
|
|
{
|
2004-10-26 15:53:32 +00:00
|
|
|
if ((timer_count / n_completions) % 2 == 0)
|
|
|
|
{
|
|
|
|
n = timer_count % n_completions;
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, dynamic_completions[n], -1);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-10-31 18:26:49 +00:00
|
|
|
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter))
|
|
|
|
gtk_list_store_remove (store, &iter);
|
2004-10-26 15:53:32 +00:00
|
|
|
}
|
2004-03-08 00:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timer_count++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-05-10 17:55:49 +00:00
|
|
|
gboolean
|
|
|
|
match_selected_cb (GtkEntryCompletion *completion,
|
|
|
|
GtkTreeModel *model,
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
{
|
|
|
|
gchar *str;
|
|
|
|
GtkWidget *entry;
|
|
|
|
|
|
|
|
entry = gtk_entry_completion_get_entry (completion);
|
|
|
|
gtk_tree_model_get (GTK_TREE_MODEL (model), iter, 1, &str, -1);
|
|
|
|
gtk_entry_set_text (GTK_ENTRY (entry), str);
|
|
|
|
gtk_editable_set_position (GTK_EDITABLE (entry), -1);
|
|
|
|
g_free (str);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-02 19:59:20 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *entry;
|
|
|
|
GtkEntryCompletion *completion;
|
|
|
|
GtkTreeModel *completion_model;
|
|
|
|
GtkCellRenderer *cell;
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2004-03-02 19:59:20 +00:00
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
|
2004-03-02 19:59:20 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
label = gtk_label_new (NULL);
|
2004-04-29 14:12:20 +00:00
|
|
|
|
2004-03-02 19:59:20 +00:00
|
|
|
gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
|
2016-10-02 15:37:22 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE);
|
2004-03-02 19:59:20 +00:00
|
|
|
|
|
|
|
/* Create our first entry */
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
|
|
|
|
/* Create the completion object */
|
|
|
|
completion = gtk_entry_completion_new ();
|
2005-09-13 15:08:43 +00:00
|
|
|
gtk_entry_completion_set_inline_completion (completion, TRUE);
|
2004-03-02 19:59:20 +00:00
|
|
|
|
|
|
|
/* Assign the completion to the entry */
|
|
|
|
gtk_entry_set_completion (GTK_ENTRY (entry), completion);
|
|
|
|
g_object_unref (completion);
|
|
|
|
|
2014-05-22 12:30:34 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), entry);
|
2005-10-26 03:55:35 +00:00
|
|
|
|
2004-03-02 19:59:20 +00:00
|
|
|
/* Create a tree model and use it as the completion model */
|
|
|
|
completion_model = create_simple_completion_model ();
|
|
|
|
gtk_entry_completion_set_model (completion, completion_model);
|
|
|
|
g_object_unref (completion_model);
|
|
|
|
|
|
|
|
/* Use model column 0 as the text column */
|
|
|
|
gtk_entry_completion_set_text_column (completion, 0);
|
|
|
|
|
|
|
|
/* Create our second entry */
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
|
|
|
|
/* Create the completion object */
|
|
|
|
completion = gtk_entry_completion_new ();
|
|
|
|
|
|
|
|
/* Assign the completion to the entry */
|
|
|
|
gtk_entry_set_completion (GTK_ENTRY (entry), completion);
|
|
|
|
g_object_unref (completion);
|
|
|
|
|
2014-05-22 12:30:34 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), entry);
|
2005-10-26 03:55:35 +00:00
|
|
|
|
2004-03-02 19:59:20 +00:00
|
|
|
/* Create a tree model and use it as the completion model */
|
|
|
|
completion_model = create_completion_model ();
|
|
|
|
gtk_entry_completion_set_model (completion, completion_model);
|
|
|
|
gtk_entry_completion_set_minimum_key_length (completion, 2);
|
|
|
|
g_object_unref (completion_model);
|
|
|
|
|
|
|
|
/* Use model column 1 as the text column */
|
|
|
|
cell = gtk_cell_renderer_pixbuf_new ();
|
|
|
|
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
|
|
|
|
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell,
|
|
|
|
"pixbuf", 0, NULL);
|
|
|
|
|
|
|
|
cell = gtk_cell_renderer_text_new ();
|
|
|
|
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
|
|
|
|
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell,
|
|
|
|
"text", 1, NULL);
|
|
|
|
|
|
|
|
gtk_entry_completion_set_match_func (completion, match_func, NULL, NULL);
|
2004-11-19 23:30:47 +00:00
|
|
|
g_signal_connect (completion, "match-selected",
|
2004-05-10 17:55:49 +00:00
|
|
|
G_CALLBACK (match_selected_cb), NULL);
|
2004-03-02 19:59:20 +00:00
|
|
|
|
|
|
|
gtk_entry_completion_insert_action_text (completion, 100, "action!");
|
|
|
|
gtk_entry_completion_insert_action_text (completion, 101, "'nother action!");
|
2004-10-28 15:00:05 +00:00
|
|
|
g_signal_connect (completion, "action_activated", G_CALLBACK (activated_cb), NULL);
|
2004-03-02 19:59:20 +00:00
|
|
|
|
2004-03-08 00:08:11 +00:00
|
|
|
/* Create our third entry */
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
|
|
|
|
/* Create the completion object */
|
|
|
|
completion = gtk_entry_completion_new ();
|
|
|
|
|
|
|
|
/* Assign the completion to the entry */
|
|
|
|
gtk_entry_set_completion (GTK_ENTRY (entry), completion);
|
|
|
|
g_object_unref (completion);
|
|
|
|
|
2014-05-22 12:30:34 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), entry);
|
2005-10-26 03:55:35 +00:00
|
|
|
|
2004-03-08 00:08:11 +00:00
|
|
|
/* Create a tree model and use it as the completion model */
|
2004-04-29 14:12:20 +00:00
|
|
|
completion_model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
|
2004-03-08 00:08:11 +00:00
|
|
|
|
|
|
|
gtk_entry_completion_set_model (completion, completion_model);
|
|
|
|
g_object_unref (completion_model);
|
|
|
|
|
|
|
|
/* Use model column 0 as the text column */
|
|
|
|
gtk_entry_completion_set_text_column (completion, 0);
|
|
|
|
|
|
|
|
/* Fill the completion dynamically */
|
2006-12-22 19:10:43 +00:00
|
|
|
gdk_threads_add_timeout (1000, (GSourceFunc) animation_timer, completion);
|
2004-03-08 00:08:11 +00:00
|
|
|
|
2004-10-26 15:53:32 +00:00
|
|
|
/* Fourth entry */
|
2016-10-02 15:37:22 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Model-less entry completion"), FALSE, FALSE);
|
2004-10-26 15:53:32 +00:00
|
|
|
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
|
|
|
|
/* Create the completion object */
|
|
|
|
completion = gtk_entry_completion_new ();
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2004-10-26 15:53:32 +00:00
|
|
|
/* Assign the completion to the entry */
|
|
|
|
gtk_entry_set_completion (GTK_ENTRY (entry), completion);
|
|
|
|
g_object_unref (completion);
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2014-05-22 12:30:34 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), entry);
|
2005-10-26 03:55:35 +00:00
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2004-03-02 19:59:20 +00:00
|
|
|
|
|
|
|
gtk_main ();
|
2016-10-02 15:37:22 +00:00
|
|
|
|
2004-03-02 19:59:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|