2020-06-17 22:08:17 +00:00
|
|
|
/* Entry/Completion
|
2004-01-07 22:19:51 +00:00
|
|
|
*
|
|
|
|
* GtkEntryCompletion provides a mechanism for adding support for
|
|
|
|
* completion in GtkEntry.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-06-21 17:46:54 +00:00
|
|
|
#include <glib/gi18n.h>
|
2004-01-07 22:19:51 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
/* Creates a tree model containing the completions */
|
2020-03-07 14:01:21 +00:00
|
|
|
static GtkTreeModel *
|
2004-01-07 22:19:51 +00:00
|
|
|
create_completion_model (void)
|
|
|
|
{
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkTreeIter iter;
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2004-01-07 22:19:51 +00:00
|
|
|
store = gtk_list_store_new (1, G_TYPE_STRING);
|
|
|
|
|
|
|
|
/* Append one word */
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "GNOME", -1);
|
|
|
|
|
|
|
|
/* Append another word */
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "total", -1);
|
|
|
|
|
|
|
|
/* And another word */
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter, 0, "totally", -1);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2004-01-07 22:19:51 +00:00
|
|
|
return GTK_TREE_MODEL (store);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_entry_completion (GtkWidget *do_widget)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
static GtkWidget *window = NULL;
|
2004-01-07 22:19:51 +00:00
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *entry;
|
|
|
|
GtkEntryCompletion *completion;
|
|
|
|
GtkTreeModel *completion_model;
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2004-01-07 22:19:51 +00:00
|
|
|
if (!window)
|
2011-09-02 03:55:47 +00:00
|
|
|
{
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2020-06-17 22:08:17 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Completion");
|
2011-09-02 03:55:47 +00:00
|
|
|
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (vbox, 5);
|
|
|
|
gtk_widget_set_margin_end (vbox, 5);
|
|
|
|
gtk_widget_set_margin_top (vbox, 5);
|
|
|
|
gtk_widget_set_margin_bottom (vbox, 5);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), vbox);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
|
|
|
label = gtk_label_new (NULL);
|
2020-06-17 22:08:17 +00:00
|
|
|
gtk_label_set_markup (GTK_LABEL (label), "Try writing <b>total</b> or <b>gnome</b> for example.");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), label);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
|
|
|
/* Create our entry */
|
|
|
|
entry = gtk_entry_new ();
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), entry);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* Create a tree model and use it as the completion model */
|
|
|
|
completion_model = create_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);
|
|
|
|
}
|
2004-01-07 22:19:51 +00:00
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2004-01-07 22:19:51 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2004-01-07 22:19:51 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|