claws-mail/src/addr_compl.c

1582 lines
42 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
*
* Copyright (C) 2000-2005 by Alfons Hoogervorst & The Sylpheed Claws Team.
2001-04-19 14:21:46 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "intl.h"
#include "defs.h"
#include <glib.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkeditable.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtktreeview.h>
#include <gtk/gtktreemodel.h>
#include <gtk/gtkliststore.h>
2001-04-19 14:21:46 +02:00
#include <string.h>
#include <ctype.h>
#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
# include <wchar.h>
# include <wctype.h>
#endif
2004-01-12 22:28:31 +01:00
#include "addrindex.h"
2001-04-19 14:21:46 +02:00
#include "addr_compl.h"
#include "utils.h"
2003-06-21 08:23:00 +02:00
#include <pthread.h>
2001-04-19 14:21:46 +02:00
/*!
*\brief For the GtkListStore
*/
enum {
ADDR_COMPL_ADDRESS,
N_ADDR_COMPL_COLUMNS
};
2003-06-21 08:23:00 +02:00
/*
* How it works:
2001-04-19 14:21:46 +02:00
*
* The address book is read into memory. We set up an address list
* containing all address book entries. Next we make the completion
* list, which contains all the completable strings, and store a
* reference to the address entry it belongs to.
* After calling the g_completion_complete(), we get a reference
* to a valid email address.
*
* Completion is very simplified. We never complete on another prefix,
* i.e. we neglect the next smallest possible prefix for the current
2001-04-19 14:21:46 +02:00
* completion cache. This is simply done so we might break up the
* addresses a little more (e.g. break up alfons@proteus.demon.nl into
* something like alfons, proteus, demon, nl; and then completing on
* any of those words).
*/
2003-06-21 08:23:00 +02:00
/**
* address_entry - structure which refers to the original address entry in the
* address book .
2001-04-19 14:21:46 +02:00
*/
typedef struct
{
gchar *name;
gchar *address;
} address_entry;
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
/**
* completion_entry - structure used to complete addresses, with a reference
* the the real address information.
2003-03-17 07:49:25 +01:00
*/
typedef struct
{
gchar *string; /* string to complete */
address_entry *ref; /* address the string belongs to */
} completion_entry;
2001-04-19 14:21:46 +02:00
/*******************************************************************************/
2001-04-19 14:21:46 +02:00
static gint g_ref_count; /* list ref count */
static GList *g_completion_list; /* list of strings to be checked */
static GList *g_address_list; /* address storage */
static GCompletion *g_completion; /* completion object */
2001-04-19 14:21:46 +02:00
/* To allow for continuing completion we have to keep track of the state
* using the following variables. No need to create a context object. */
2001-04-19 14:21:46 +02:00
static gint g_completion_count; /* nr of addresses incl. the prefix */
static gint g_completion_next; /* next prev address */
static GSList *g_completion_addresses; /* unique addresses found in the
completion cache. */
static gchar *g_completion_prefix; /* last prefix. (this is cached here
* because the prefix passed to g_completion
* is g_strdown()'ed */
/*******************************************************************************/
2001-04-19 14:21:46 +02:00
/*
* Define the structure of the completion window.
*/
typedef struct _CompletionWindow CompletionWindow;
struct _CompletionWindow {
gint listCount;
gchar *searchTerm;
GtkWidget *window;
GtkWidget *entry;
GtkWidget *list_view;
gboolean in_mouse; /*!< mouse press pending... */
gboolean destroying; /*!< destruction in progress */
};
static GtkListStore *addr_compl_create_store (void);
static void addr_compl_list_view_add_address (GtkWidget *list_view,
const gchar *address);
static GtkWidget *addr_compl_list_view_create (CompletionWindow *window);
static void addr_compl_create_list_view_columns (GtkWidget *list_view);
static gboolean list_view_button_press (GtkWidget *widget,
GdkEventButton *event,
CompletionWindow *window);
static gboolean list_view_button_release (GtkWidget *widget,
GdkEventButton *event,
CompletionWindow *window);
static gboolean addr_compl_selected (GtkTreeSelection *selector,
GtkTreeModel *model,
GtkTreePath *path,
gboolean currently_selected,
gpointer data);
static gboolean addr_compl_defer_select_destruct(CompletionWindow *window);
2003-06-21 08:23:00 +02:00
/**
* Function used by GTK to find the string data to be used for completion.
* \param data Pointer to data being processed.
2001-04-19 14:21:46 +02:00
*/
static gchar *completion_func(gpointer data)
{
g_return_val_if_fail(data != NULL, NULL);
2001-04-19 14:21:46 +02:00
return ((completion_entry *)data)->string;
}
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
/**
* Initialize all completion index data.
*/
static void init_all(void)
{
g_completion = g_completion_new(completion_func);
g_return_if_fail(g_completion != NULL);
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Free up all completion index data.
*/
static void free_all(void)
{
GList *walk;
walk = g_list_first(g_completion_list);
for (; walk != NULL; walk = g_list_next(walk)) {
completion_entry *ce = (completion_entry *) walk->data;
g_free(ce->string);
g_free(walk->data);
2001-04-19 14:21:46 +02:00
}
g_list_free(g_completion_list);
g_completion_list = NULL;
walk = g_address_list;
for (; walk != NULL; walk = g_list_next(walk)) {
address_entry *ae = (address_entry *) walk->data;
g_free(ae->name);
g_free(ae->address);
g_free(walk->data);
2003-03-17 07:49:25 +01:00
}
g_list_free(g_address_list);
g_address_list = NULL;
g_completion_free(g_completion);
g_completion = NULL;
}
2003-06-21 08:23:00 +02:00
/**
* Append specified address entry to the index.
* \param str Index string value.
* \param ae Entry containing address data.
*/
static void add_address1(const char *str, address_entry *ae)
{
completion_entry *ce1;
ce1 = g_new0(completion_entry, 1),
2003-10-05 12:10:30 +02:00
ce1->string = g_utf8_strdown(str, -1);
/* GCompletion list is case sensitive */
g_strdown(ce1->string);
ce1->ref = ae;
g_completion_list = g_list_prepend(g_completion_list, ce1);
}
2003-06-21 08:23:00 +02:00
/**
* Adds address to the completion list. This function looks complicated, but
* it's only allocation checks. Each value will be included in the index.
* \param name Recipient name.
* \param address EMail address.
* \param alias Alias to append.
* \return <code>0</code> if entry appended successfully, or <code>-1</code>
* if failure.
2001-04-19 14:21:46 +02:00
*/
2004-05-10 12:22:28 +02:00
static gint add_address(const gchar *name, const gchar *address,
const gchar *nick, const gchar *alias)
{
address_entry *ae;
if (!name || !address) return -1;
ae = g_new0(address_entry, 1);
g_return_val_if_fail(ae != NULL, -1);
ae->name = g_strdup(name);
ae->address = g_strdup(address);
2003-06-21 08:23:00 +02:00
g_address_list = g_list_prepend(g_address_list, ae);
2001-04-19 14:21:46 +02:00
add_address1(name, ae);
add_address1(address, ae);
2004-05-10 12:22:28 +02:00
if (nick != NULL)
add_address1(nick, ae);
if ( alias != NULL ) {
add_address1(alias, ae);
}
2001-04-19 14:21:46 +02:00
return 0;
}
2003-06-21 08:23:00 +02:00
/**
* Read address book, creating all entries in the completion index.
*/
static void read_address_book(void) {
2004-01-12 22:28:31 +01:00
addrindex_load_completion( add_address );
g_address_list = g_list_reverse(g_address_list);
g_completion_list = g_list_reverse(g_completion_list);
}
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
/**
* Test whether there is a completion pending.
* \return <code>TRUE</code> if pending.
*/
static gboolean is_completion_pending(void)
{
/* check if completion pending, i.e. we might satisfy a request for the next
* or previous address */
return g_completion_count;
}
/**
* Clear the completion cache.
*/
static void clear_completion_cache(void)
{
if (is_completion_pending()) {
if (g_completion_prefix)
g_free(g_completion_prefix);
if (g_completion_addresses) {
g_slist_free(g_completion_addresses);
g_completion_addresses = NULL;
}
g_completion_count = g_completion_next = 0;
}
}
/**
* Prepare completion index. This function should be called prior to attempting
* address completion.
* \return The number of addresses in the completion list.
*/
gint start_address_completion(void)
{
clear_completion_cache();
if (!g_ref_count) {
init_all();
/* open the address book */
read_address_book();
/* merge the completion entry list into g_completion */
if (g_completion_list)
g_completion_add_items(g_completion, g_completion_list);
2001-04-19 14:21:46 +02:00
}
g_ref_count++;
debug_print("start_address_completion ref count %d\n", g_ref_count);
return g_list_length(g_completion_list);
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Retrieve a possible address (or a part) from an entry box. To make life
* easier, we only look at the last valid address component; address
* completion only works at the last string component in the entry box.
*
* \param entry Address entry field.
* \param start_pos Address of start position of address.
* \return Possible address.
*/
static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
2001-04-19 14:21:46 +02:00
{
2003-10-05 12:10:30 +02:00
const gchar *edit_text, *p;
2001-04-19 14:21:46 +02:00
gint cur_pos;
2002-02-06 11:17:44 +01:00
gboolean in_quote = FALSE;
2002-05-18 17:22:17 +02:00
gboolean in_bracket = FALSE;
2001-04-19 14:21:46 +02:00
gchar *str;
edit_text = gtk_entry_get_text(entry);
if (edit_text == NULL) return NULL;
cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
/* scan for a separator. doesn't matter if walk points at null byte. */
2003-10-05 12:10:30 +02:00
for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
p > edit_text;
p = g_utf8_prev_char(p)) {
if (*p == '"') {
in_quote = TRUE;
} else if (!in_quote) {
if (!in_bracket && *p == ',') {
2002-05-18 17:22:17 +02:00
break;
2003-10-05 12:10:30 +02:00
} else if (*p == '<')
2002-05-18 17:22:17 +02:00
in_bracket = TRUE;
2003-10-05 12:10:30 +02:00
else if (*p == '>')
2002-05-18 17:22:17 +02:00
in_bracket = FALSE;
}
2002-02-06 11:17:44 +01:00
}
2001-04-19 14:21:46 +02:00
/* have something valid */
2003-10-05 12:10:30 +02:00
if (g_utf8_strlen(p, -1) == 0)
2001-04-19 14:21:46 +02:00
return NULL;
2002-02-07 11:14:25 +01:00
#define IS_VALID_CHAR(x) \
2003-10-05 12:10:30 +02:00
(isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
2001-04-19 14:21:46 +02:00
/* now scan back until we hit a valid character */
2003-10-05 12:10:30 +02:00
for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
2001-04-19 14:21:46 +02:00
;
#undef IS_VALID_CHAR
2003-10-05 12:10:30 +02:00
if (g_utf8_strlen(p, -1) == 0)
2001-04-19 14:21:46 +02:00
return NULL;
2003-10-05 12:10:30 +02:00
if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
2001-04-19 14:21:46 +02:00
2003-10-05 12:10:30 +02:00
str = g_strdup(p);
2001-04-19 14:21:46 +02:00
return str;
}
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
/**
* Replace an incompleted address with a completed one.
* \param entry Address entry field.
* \param newtext New text.
* \param start_pos Insertion point in entry field.
2001-04-19 14:21:46 +02:00
*/
2003-06-21 08:23:00 +02:00
static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
2001-04-19 14:21:46 +02:00
gint start_pos)
{
if (!newtext) return;
2001-04-19 14:21:46 +02:00
gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
&start_pos);
gtk_editable_set_position(GTK_EDITABLE(entry), -1);
}
2003-06-21 08:23:00 +02:00
/**
* Attempt to complete an address, and returns the number of addresses found.
* Use <code>get_complete_address()</code> to get an entry from the index.
*
* \param str Search string to find.
* \return Zero if no match was found, otherwise the number of addresses; the
* original prefix (search string) will appear at index 0.
2003-03-17 07:49:25 +01:00
*/
guint complete_address(const gchar *str)
{
GList *result;
gchar *d;
guint count, cpl;
completion_entry *ce;
g_return_val_if_fail(str != NULL, 0);
2003-10-05 12:10:30 +02:00
/* g_completion is case sensitive */
d = g_utf8_strdown(str, -1);
clear_completion_cache();
g_completion_prefix = g_strdup(str);
result = g_completion_complete(g_completion, d, NULL);
count = g_list_length(result);
if (count) {
/* create list with unique addresses */
for (cpl = 0, result = g_list_first(result);
result != NULL;
result = g_list_next(result)) {
ce = (completion_entry *)(result->data);
if (NULL == g_slist_find(g_completion_addresses,
ce->ref)) {
cpl++;
g_completion_addresses =
g_slist_append(g_completion_addresses,
ce->ref);
}
}
count = cpl + 1; /* index 0 is the original prefix */
g_completion_next = 1; /* we start at the first completed one */
} else {
g_free(g_completion_prefix);
g_completion_prefix = NULL;
2003-03-17 07:49:25 +01:00
}
g_completion_count = count;
2003-10-05 12:10:30 +02:00
g_free(d);
return count;
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Return a complete address from the index.
* \param index Index of entry that was found (by the previous call to
* <code>complete_address()</code>
* \return Completed address string; this should be freed when done.
2003-03-17 07:49:25 +01:00
*/
gchar *get_complete_address(gint index)
{
const address_entry *p;
gchar *address = NULL;
if (index < g_completion_count) {
if (index == 0)
address = g_strdup(g_completion_prefix);
else {
/* get something from the unique addresses */
p = (address_entry *)g_slist_nth_data
(g_completion_addresses, index - 1);
if (p != NULL) {
if (!p->name || p->name[0] == '\0')
address = g_strdup_printf(p->address);
2003-06-21 08:23:00 +02:00
else if (strchr_with_skip_quote(p->name, '"', ','))
address = g_strdup_printf
("\"%s\" <%s>", p->name, p->address);
else
address = g_strdup_printf
("%s <%s>", p->name, p->address);
}
}
}
2001-04-19 14:21:46 +02:00
return address;
}
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
/**
* Return the next complete address match from the completion index.
* \return Completed address string; this should be freed when done.
*/
static gchar *get_next_complete_address(void)
{
if (is_completion_pending()) {
gchar *res;
2001-04-19 14:21:46 +02:00
res = get_complete_address(g_completion_next);
g_completion_next += 1;
if (g_completion_next >= g_completion_count)
g_completion_next = 0;
return res;
} else
return NULL;
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Return a count of the completed matches in the completion index.
* \return Number of matched entries.
*/
static guint get_completion_count(void)
{
if (is_completion_pending())
return g_completion_count;
else
return 0;
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Invalidate address completion index. This function should be called whenever
* the address book changes. This forces data to be read into the completion
* data.
* \return Number of entries in index.
*/
gint invalidate_address_completion(void)
{
if (g_ref_count) {
/* simply the same as start_address_completion() */
debug_print("Invalidation request for address completion\n");
free_all();
init_all();
read_address_book();
2003-06-21 08:23:00 +02:00
g_completion_add_items(g_completion, g_completion_list);
clear_completion_cache();
}
2001-04-19 14:21:46 +02:00
return g_list_length(g_completion_list);
}
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
/**
* Finished with completion index. This function should be called after
* matching addresses.
* \return Reference count.
*/
gint end_address_completion(void)
{
clear_completion_cache();
2001-04-19 14:21:46 +02:00
if (0 == --g_ref_count)
free_all();
2001-04-19 14:21:46 +02:00
debug_print("end_address_completion ref count %d\n", g_ref_count);
2003-03-17 07:49:25 +01:00
return g_ref_count;
2003-03-17 07:49:25 +01:00
}
2003-06-21 08:23:00 +02:00
/**
* Completion window.
*/
static CompletionWindow *_compWindow_ = NULL;
/**
* Mutex to protect callback from multiple threads.
*/
static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
2003-06-21 08:23:00 +02:00
/**
* Completion queue list.
*/
static GList *_displayQueue_ = NULL;
/**
* Current query ID.
*/
static gint _queryID_ = 0;
/**
* Completion idle ID.
*/
2003-09-07 21:31:26 +02:00
static guint _completionIdleID_ = 0;
2003-06-21 08:23:00 +02:00
/*
* address completion entry ui. the ui (completion list was inspired by galeon's
2001-04-19 14:21:46 +02:00
* auto completion list). remaining things powered by sylpheed's completion engine.
*/
2003-06-21 08:23:00 +02:00
#define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
2001-04-19 14:21:46 +02:00
static void address_completion_mainwindow_set_focus (GtkWindow *window,
GtkWidget *widget,
gpointer data);
2001-04-19 14:21:46 +02:00
static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
GdkEventKey *ev,
gpointer data);
static gboolean address_completion_complete_address_in_entry
(GtkEntry *entry,
gboolean next);
2001-04-19 14:21:46 +02:00
static void address_completion_create_completion_window (GtkEntry *entry);
static gboolean completion_window_button_press
(GtkWidget *widget,
GdkEventButton *event,
2003-06-21 08:23:00 +02:00
CompletionWindow *compWin );
2001-04-19 14:21:46 +02:00
static gboolean completion_window_key_press
(GtkWidget *widget,
GdkEventKey *event,
2003-06-21 08:23:00 +02:00
CompletionWindow *compWin );
static void address_completion_create_completion_window( GtkEntry *entry_ );
/**
* Create a completion window object.
* \return Initialized completion window.
*/
static CompletionWindow *addrcompl_create_window( void ) {
CompletionWindow *cw;
cw = g_new0( CompletionWindow, 1 );
cw->listCount = 0;
cw->searchTerm = NULL;
cw->window = NULL;
cw->entry = NULL;
cw->list_view = NULL;
cw->in_mouse = FALSE;
cw->destroying = FALSE;
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
return cw;
}
/**
* Destroy completion window.
* \param cw Window to destroy.
*/
static void addrcompl_destroy_window( CompletionWindow *cw ) {
2004-01-12 22:28:31 +01:00
/* Stop all searches currently in progress */
addrindex_stop_search( _queryID_ );
2003-06-21 08:23:00 +02:00
/* Remove idler function... or application may not terminate */
if( _completionIdleID_ != 0 ) {
gtk_idle_remove( _completionIdleID_ );
_completionIdleID_ = 0;
}
/* Now destroy window */
if( cw ) {
/* Clear references to widgets */
cw->entry = NULL;
cw->list_view = NULL;
2003-06-21 08:23:00 +02:00
/* Free objects */
if( cw->window ) {
gtk_widget_hide( cw->window );
gtk_widget_destroy( cw->window );
}
cw->window = NULL;
cw->destroying = FALSE;
cw->in_mouse = FALSE;
2003-06-21 08:23:00 +02:00
}
2003-06-21 08:23:00 +02:00
}
/**
* Free up completion window.
* \param cw Window to free.
*/
static void addrcompl_free_window( CompletionWindow *cw ) {
if( cw ) {
addrcompl_destroy_window( cw );
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
g_free( cw->searchTerm );
cw->searchTerm = NULL;
/* Clear references */
cw->listCount = 0;
/* Free object */
g_free( cw );
}
}
/**
* Advance selection to previous/next item in list.
* \param list_view List to process.
2003-06-21 08:23:00 +02:00
* \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
* previous entry.
*/
static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
2001-04-19 14:21:46 +02:00
{
GtkTreeSelection *selection;
GtkTreeIter iter;
GtkTreeModel *model;
2001-04-19 14:21:46 +02:00
g_return_if_fail(list_view != NULL);
2001-04-19 14:21:46 +02:00
selection = gtk_tree_view_get_selection(list_view);
if (!gtk_tree_selection_get_selected(selection, &model, &iter))
return;
if (forward) {
forward = gtk_tree_model_iter_next(model, &iter);
if (forward)
gtk_tree_selection_select_iter(selection, &iter);
} else {
GtkTreePath *prev;
prev = gtk_tree_model_get_path(model, &iter);
if (!prev)
return;
if (gtk_tree_path_prev(prev))
gtk_tree_selection_select_path(selection, prev);
gtk_tree_path_free(prev);
2003-06-21 08:23:00 +02:00
}
2001-04-19 14:21:46 +02:00
}
2002-05-18 17:22:17 +02:00
#if 0
/* completion_window_accept_selection() - accepts the current selection in the
* clist, and destroys the window */
static void completion_window_accept_selection(GtkWidget **window,
GtkCList *clist,
GtkEntry *entry)
2002-05-18 17:22:17 +02:00
{
gchar *address = NULL, *text = NULL;
gint cursor_pos, row;
g_return_if_fail(window != NULL);
g_return_if_fail(*window != NULL);
2002-05-18 17:22:17 +02:00
g_return_if_fail(clist != NULL);
g_return_if_fail(entry != NULL);
g_return_if_fail(clist->selection != NULL);
/* FIXME: I believe it's acceptable to access the selection member directly */
row = GPOINTER_TO_INT(clist->selection->data);
/* we just need the cursor position */
address = get_address_from_edit(entry, &cursor_pos);
g_free(address);
gtk_clist_get_text(clist, row, 0, &text);
replace_address_in_edit(entry, text, cursor_pos);
clear_completion_cache();
gtk_widget_destroy(*window);
*window = NULL;
}
#endif
2003-06-21 08:23:00 +02:00
/**
* Resize window to accommodate maximum number of address entries.
* \param cw Completion window.
*/
static void addrcompl_resize_window( CompletionWindow *cw ) {
GtkRequisition r;
gint x, y, width, height, depth;
/* Get current geometry of window */
gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
gtk_widget_hide_all( cw->window );
2003-06-21 08:23:00 +02:00
gtk_widget_show_all( cw->window );
gtk_widget_size_request( cw->list_view, &r );
2003-06-21 08:23:00 +02:00
/* Adjust window height to available screen space */
if( ( y + r.height ) > gdk_screen_height() ) {
gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
2003-10-05 12:10:30 +02:00
gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
} else
gtk_widget_set_size_request(cw->window, width, r.height);
2003-06-21 08:23:00 +02:00
}
/**
* Add an address the completion window address list.
* \param cw Completion window.
* \param address Address to add.
*/
static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
GtkListStore *store;
GtkTreeIter iter;
GtkTreeSelection *selection;
store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
gtk_list_store_append(store, &iter);
2003-06-21 08:23:00 +02:00
/* printf( "\t\tAdding :%s\n", address ); */
gtk_list_store_set(store, &iter, ADDR_COMPL_ADDRESS, address, -1);
2003-06-21 08:23:00 +02:00
cw->listCount++;
/* Resize window */
addrcompl_resize_window( cw );
gtk_grab_add( cw->window );
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
2003-06-21 08:23:00 +02:00
if( cw->listCount == 1 ) {
/* Select first row for now */
gtk_tree_selection_select_iter(selection, &iter);
2003-06-21 08:23:00 +02:00
}
else if( cw->listCount == 2 ) {
gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
2003-06-21 08:23:00 +02:00
/* Move off first row */
gtk_tree_selection_select_iter(selection, &iter);
2003-06-21 08:23:00 +02:00
}
}
/**
* Completion idle function. This function is called by the main (UI) thread
* during UI idle time while an address search is in progress. Items from the
* display queue are processed and appended to the address list.
*
* \param data Target completion window to receive email addresses.
* \return <i>TRUE</i> to ensure that idle event do not get ignored.
*/
static gboolean addrcompl_idle( gpointer data ) {
GList *node;
gchar *address;
/* Process all entries in display queue */
pthread_mutex_lock( & _completionMutex_ );
if( _displayQueue_ ) {
node = _displayQueue_;
while( node ) {
address = node->data;
/* printf( "address ::: %s :::\n", address ); */
2004-01-12 22:28:31 +01:00
addrcompl_add_entry( _compWindow_, address );
2003-06-21 08:23:00 +02:00
g_free( address );
node = g_list_next( node );
}
g_list_free( _displayQueue_ );
_displayQueue_ = NULL;
}
pthread_mutex_unlock( & _completionMutex_ );
return TRUE;
}
/**
* Callback entry point. The background thread (if any) appends the address
* list to the display queue.
2004-01-12 22:28:31 +01:00
* \param sender Sender of query.
2003-06-21 08:23:00 +02:00
* \param queryID Query ID of search request.
* \param listEMail List of zero of more email objects that met search
* criteria.
2004-01-12 22:28:31 +01:00
* \param data Query data.
2003-06-21 08:23:00 +02:00
*/
2004-01-12 22:28:31 +01:00
static gint addrcompl_callback_entry(
gpointer sender, gint queryID, GList *listEMail, gpointer data )
2003-06-21 08:23:00 +02:00
{
GList *node;
gchar *address;
2004-01-12 22:28:31 +01:00
/* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
2003-06-21 08:23:00 +02:00
pthread_mutex_lock( & _completionMutex_ );
2004-01-12 22:28:31 +01:00
if( queryID == _queryID_ ) {
/* Append contents to end of display queue */
node = listEMail;
while( node ) {
ItemEMail *email = node->data;
address = addritem_format_email( email );
/* printf( "\temail/address ::%s::\n", address ); */
_displayQueue_ = g_list_append( _displayQueue_, address );
node = g_list_next( node );
2003-06-21 08:23:00 +02:00
}
}
2004-01-12 22:28:31 +01:00
g_list_free( listEMail );
2003-06-21 08:23:00 +02:00
pthread_mutex_unlock( & _completionMutex_ );
return 0;
2003-06-21 08:23:00 +02:00
}
/**
* Clear the display queue.
*/
static void addrcompl_clear_queue( void ) {
/* Clear out display queue */
pthread_mutex_lock( & _completionMutex_ );
g_list_free( _displayQueue_ );
_displayQueue_ = NULL;
pthread_mutex_unlock( & _completionMutex_ );
}
/**
* Add a single address entry into the display queue.
* \param address Address to append.
*/
static void addrcompl_add_queue( gchar *address ) {
pthread_mutex_lock( & _completionMutex_ );
_displayQueue_ = g_list_append( _displayQueue_, address );
pthread_mutex_unlock( & _completionMutex_ );
}
/**
* Load list with entries from local completion index.
*/
2004-01-12 22:28:31 +01:00
static void addrcompl_load_local( void ) {
2003-06-21 08:23:00 +02:00
guint count = 0;
for (count = 0; count < get_completion_count(); count++) {
gchar *address;
address = get_complete_address( count );
/* printf( "\taddress ::%s::\n", address ); */
/* Append contents to end of display queue */
addrcompl_add_queue( address );
}
}
/**
* Start the search.
*/
static void addrcompl_start_search( void ) {
gchar *searchTerm;
searchTerm = g_strdup( _compWindow_->searchTerm );
/* Setup the search */
_queryID_ = addrindex_setup_search(
2004-01-12 22:28:31 +01:00
searchTerm, NULL, addrcompl_callback_entry );
2003-06-21 08:23:00 +02:00
g_free( searchTerm );
/* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
/* Load local stuff */
2004-01-12 22:28:31 +01:00
addrcompl_load_local();
2003-06-21 08:23:00 +02:00
/* Sit back and wait until something happens */
_completionIdleID_ =
2004-01-12 22:28:31 +01:00
gtk_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
2003-06-21 08:23:00 +02:00
/* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
2004-01-12 22:28:31 +01:00
addrindex_start_search( _queryID_ );
2003-06-21 08:23:00 +02:00
}
/**
* Apply the current selection in the list to the entry field. Focus is also
* moved to the next widget so that Tab key works correctly.
* \param list_view List to process.
2003-06-21 08:23:00 +02:00
* \param entry Address entry field.
*/
static void completion_window_apply_selection(GtkTreeView *list_view, GtkEntry *entry)
{
gchar *address = NULL, *text = NULL;
gint cursor_pos;
2003-06-21 08:23:00 +02:00
GtkWidget *parent;
GtkTreeSelection *selection;
GtkTreeModel *model;
GtkTreeIter iter;
g_return_if_fail(list_view != NULL);
g_return_if_fail(entry != NULL);
selection = gtk_tree_view_get_selection(list_view);
if (! gtk_tree_selection_get_selected(selection, &model, &iter))
return;
2003-03-17 07:49:25 +01:00
2003-06-21 08:23:00 +02:00
/* First remove the idler */
if( _completionIdleID_ != 0 ) {
gtk_idle_remove( _completionIdleID_ );
_completionIdleID_ = 0;
}
/* Process selected item */
gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text, -1);
2002-05-18 17:22:17 +02:00
address = get_address_from_edit(entry, &cursor_pos);
g_free(address);
replace_address_in_edit(entry, text, cursor_pos);
g_free(text);
2003-06-21 08:23:00 +02:00
/* Move focus to next widget */
parent = GTK_WIDGET(entry)->parent;
if( parent ) {
2003-10-05 12:10:30 +02:00
gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
2003-06-21 08:23:00 +02:00
}
}
2003-06-21 08:23:00 +02:00
/**
* Start address completion. Should be called when creating the main window
* containing address completion entries.
* \param mainwindow Main window.
*/
void address_completion_start(GtkWidget *mainwindow)
{
start_address_completion();
2001-04-19 14:21:46 +02:00
/* register focus change hook */
2003-10-05 12:10:30 +02:00
g_signal_connect(G_OBJECT(mainwindow), "set_focus",
G_CALLBACK(address_completion_mainwindow_set_focus),
mainwindow);
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Need unique data to make unregistering signal handler possible for the auto
* completed entry.
*/
#define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
2003-06-21 08:23:00 +02:00
/**
* Register specified entry widget for address completion.
* \param entry Address entry field.
*/
void address_completion_register_entry(GtkEntry *entry)
{
g_return_if_fail(entry != NULL);
g_return_if_fail(GTK_IS_ENTRY(entry));
/* add hooked property */
2003-10-05 12:10:30 +02:00
g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
/* add keypress event */
2003-10-05 12:10:30 +02:00
g_signal_connect_closure
(G_OBJECT(entry), "key_press_event",
2003-10-05 12:10:30 +02:00
g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
COMPLETION_UNIQUE_DATA,
2003-10-05 12:10:30 +02:00
NULL),
FALSE); /* magic */
}
2003-06-21 08:23:00 +02:00
/**
* Unregister specified entry widget from address completion operations.
* \param entry Address entry field.
*/
void address_completion_unregister_entry(GtkEntry *entry)
{
GtkObject *entry_obj;
g_return_if_fail(entry != NULL);
g_return_if_fail(GTK_IS_ENTRY(entry));
2003-10-05 12:10:30 +02:00
entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
g_return_if_fail(entry_obj);
g_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
/* has the hooked property? */
2003-10-05 12:10:30 +02:00
g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
/* remove the hook */
2003-10-05 12:10:30 +02:00
g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
G_CALLBACK(address_completion_entry_key_pressed),
COMPLETION_UNIQUE_DATA);
}
2003-06-21 08:23:00 +02:00
/**
* End address completion. Should be called when main window with address
* completion entries terminates. NOTE: this function assumes that it is
* called upon destruction of the window.
* \param mainwindow Main window.
*/
void address_completion_end(GtkWidget *mainwindow)
{
/* if address_completion_end() is really called on closing the window,
* we don't need to unregister the set_focus_cb */
end_address_completion();
}
/* if focus changes to another entry, then clear completion cache */
static void address_completion_mainwindow_set_focus(GtkWindow *window,
GtkWidget *widget,
gpointer data)
{
2003-10-05 12:10:30 +02:00
if (widget && GTK_IS_ENTRY(widget) &&
g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
clear_completion_cache();
2003-10-05 12:10:30 +02:00
}
}
2003-06-21 08:23:00 +02:00
/**
* Listener that watches for tab or other keystroke in address entry field.
* \param entry Address entry field.
* \param ev Event object.
* \param data User data.
* \return <i>TRUE</i>.
*/
2001-04-19 14:21:46 +02:00
static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
GdkEventKey *ev,
gpointer data)
{
if (ev->keyval == GDK_Tab) {
2003-06-21 08:23:00 +02:00
addrcompl_clear_queue();
if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
2001-04-19 14:21:46 +02:00
/* route a void character to the default handler */
/* this is a dirty hack; we're actually changing a key
* reported by the system. */
ev->keyval = GDK_AudibleBell_Enable;
ev->state &= ~GDK_SHIFT_MASK;
2003-06-21 08:23:00 +02:00
/* Create window */
address_completion_create_completion_window(entry);
/* Start remote queries */
addrcompl_start_search();
2003-10-05 12:10:30 +02:00
return TRUE;
2003-06-21 08:23:00 +02:00
}
else {
/* old behaviour */
2003-03-17 07:49:25 +01:00
}
} else if (ev->keyval == GDK_Shift_L
|| ev->keyval == GDK_Shift_R
|| ev->keyval == GDK_Control_L
|| ev->keyval == GDK_Control_R
|| ev->keyval == GDK_Caps_Lock
|| ev->keyval == GDK_Shift_Lock
|| ev->keyval == GDK_Meta_L
|| ev->keyval == GDK_Meta_R
|| ev->keyval == GDK_Alt_L
|| ev->keyval == GDK_Alt_R) {
/* these buttons should not clear the cache... */
} else
clear_completion_cache();
2003-10-05 12:10:30 +02:00
return FALSE;
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Initialize search term for address completion.
* \param entry Address entry field.
*/
static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
gboolean next)
2001-04-19 14:21:46 +02:00
{
gint ncount, cursor_pos;
2003-06-21 08:23:00 +02:00
gchar *searchTerm, *new = NULL;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(entry != NULL, FALSE);
if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
/* get an address component from the cursor */
2003-06-21 08:23:00 +02:00
searchTerm = get_address_from_edit( entry, &cursor_pos );
if( ! searchTerm ) return FALSE;
/* printf( "search for :::%s:::\n", searchTerm ); */
2003-06-21 08:23:00 +02:00
/* Clear any existing search */
if( _compWindow_->searchTerm ) {
g_free( _compWindow_->searchTerm );
}
2003-06-21 08:23:00 +02:00
_compWindow_->searchTerm = g_strdup( searchTerm );
2002-05-18 17:22:17 +02:00
2003-06-21 08:23:00 +02:00
/* Perform search on local completion index */
ncount = complete_address( searchTerm );
if( 0 < ncount ) {
new = get_next_complete_address();
g_free( new );
2002-05-18 17:22:17 +02:00
}
2001-04-19 14:21:46 +02:00
2004-05-10 12:22:28 +02:00
/* Select the address if there is only one match */
if (ncount == 2) {
/* Display selected address in entry field */
gchar *addr = get_complete_address(1);
if (addr) {
replace_address_in_edit(entry, addr, cursor_pos);
g_free(addr);
}
/* Discard the window */
clear_completion_cache();
}
2003-06-21 08:23:00 +02:00
/* Make sure that drop-down appears uniform! */
2004-05-10 12:22:28 +02:00
else if( ncount == 0 ) {
2003-06-21 08:23:00 +02:00
addrcompl_add_queue( g_strdup( searchTerm ) );
}
g_free( searchTerm );
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
return TRUE;
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Create new address completion window for specified entry.
* \param entry_ Entry widget to associate with window.
*/
static void address_completion_create_completion_window( GtkEntry *entry_ )
2001-04-19 14:21:46 +02:00
{
gint x, y, height, width, depth;
GtkWidget *scroll, *list_view;
2001-04-19 14:21:46 +02:00
GtkRequisition r;
2003-06-21 08:23:00 +02:00
GtkWidget *window;
2001-04-19 14:21:46 +02:00
GtkWidget *entry = GTK_WIDGET(entry_);
2003-06-21 08:23:00 +02:00
/* Create new window and list */
window = gtk_window_new(GTK_WINDOW_POPUP);
list_view = addr_compl_list_view_create(_compWindow_);
2003-06-21 08:23:00 +02:00
/* Destroy any existing window */
addrcompl_destroy_window( _compWindow_ );
/* Create new object */
_compWindow_->window = window;
_compWindow_->entry = entry;
_compWindow_->list_view = list_view;
2003-06-21 08:23:00 +02:00
_compWindow_->listCount = 0;
_compWindow_->in_mouse = FALSE;
2003-06-21 08:23:00 +02:00
scroll = gtk_scrolled_window_new(NULL, NULL);
2001-04-19 14:21:46 +02:00
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
2003-06-21 08:23:00 +02:00
gtk_container_add(GTK_CONTAINER(window), scroll);
gtk_container_add(GTK_CONTAINER(scroll), list_view);
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
/* Use entry widget to create initial window */
2001-04-19 14:21:46 +02:00
gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
gdk_window_get_deskrelative_origin (entry->window, &x, &y);
y += height;
2003-10-05 12:10:30 +02:00
gtk_window_move(GTK_WINDOW(window), x, y);
2003-06-21 08:23:00 +02:00
/* Resize window to fit initial (empty) address list */
gtk_widget_size_request( list_view, &r );
2003-10-05 12:10:30 +02:00
gtk_widget_set_size_request( window, width, r.height );
2003-06-21 08:23:00 +02:00
gtk_widget_show_all( window );
gtk_widget_size_request( list_view, &r );
2003-03-17 07:49:25 +01:00
2003-06-21 08:23:00 +02:00
/* Setup handlers */
g_signal_connect(G_OBJECT(list_view), "button_press_event",
G_CALLBACK(list_view_button_press),
_compWindow_);
g_signal_connect(G_OBJECT(list_view), "button_release_event",
G_CALLBACK(list_view_button_release),
_compWindow_);
2003-10-05 12:10:30 +02:00
g_signal_connect(G_OBJECT(window),
"button-press-event",
G_CALLBACK(completion_window_button_press),
_compWindow_ );
g_signal_connect(G_OBJECT(window),
"key-press-event",
G_CALLBACK(completion_window_key_press),
_compWindow_ );
2003-06-21 08:23:00 +02:00
gdk_pointer_grab(window->window, TRUE,
2001-04-19 14:21:46 +02:00
GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK,
NULL, NULL, GDK_CURRENT_TIME);
2003-06-21 08:23:00 +02:00
gtk_grab_add( window );
2001-04-19 14:21:46 +02:00
/* XXX: GTK2 too???
*
* GTK1: this gets rid of the irritating focus rectangle that doesn't
2001-04-19 14:21:46 +02:00
* follow the selection */
GTK_WIDGET_UNSET_FLAGS(list_view, GTK_CAN_FOCUS);
2001-04-19 14:21:46 +02:00
}
2003-06-21 08:23:00 +02:00
/**
* Respond to button press in completion window. Check if mouse click is
* anywhere outside the completion window. In that case the completion
* window is destroyed, and the original searchTerm is restored.
*
* \param widget Window object.
* \param event Event.
* \param compWin Reference to completion window.
*/
2001-04-19 14:21:46 +02:00
static gboolean completion_window_button_press(GtkWidget *widget,
GdkEventButton *event,
2003-06-21 08:23:00 +02:00
CompletionWindow *compWin )
2001-04-19 14:21:46 +02:00
{
GtkWidget *event_widget, *entry;
2003-06-21 08:23:00 +02:00
gchar *searchTerm;
2001-04-19 14:21:46 +02:00
gint cursor_pos;
gboolean restore = TRUE;
2003-06-21 08:23:00 +02:00
g_return_val_if_fail(compWin != NULL, FALSE);
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
entry = compWin->entry;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(entry != NULL, FALSE);
2003-06-21 08:23:00 +02:00
/* Test where mouse was clicked */
2001-04-19 14:21:46 +02:00
event_widget = gtk_get_event_widget((GdkEvent *)event);
if (event_widget != widget) {
while (event_widget) {
if (event_widget == widget)
return FALSE;
else if (event_widget == entry) {
restore = FALSE;
break;
}
2003-06-21 08:23:00 +02:00
event_widget = event_widget->parent;
2001-04-19 14:21:46 +02:00
}
}
if (restore) {
2003-06-21 08:23:00 +02:00
/* Clicked outside of completion window - restore */
searchTerm = _compWindow_->searchTerm;
2001-04-19 14:21:46 +02:00
g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
2003-06-21 08:23:00 +02:00
replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
2001-04-19 14:21:46 +02:00
}
clear_completion_cache();
2003-06-21 08:23:00 +02:00
addrcompl_destroy_window( _compWindow_ );
2001-04-19 14:21:46 +02:00
return TRUE;
}
2003-06-21 08:23:00 +02:00
/**
* Respond to key press in completion window.
* \param widget Window object.
* \param event Event.
* \param compWind Reference to completion window.
*/
2001-04-19 14:21:46 +02:00
static gboolean completion_window_key_press(GtkWidget *widget,
GdkEventKey *event,
2003-06-21 08:23:00 +02:00
CompletionWindow *compWin )
2001-04-19 14:21:46 +02:00
{
GdkEventKey tmp_event;
GtkWidget *entry;
2003-06-21 08:23:00 +02:00
gchar *searchTerm;
2001-04-19 14:21:46 +02:00
gint cursor_pos;
GtkWidget *list_view;
2003-06-26 07:40:21 +02:00
GtkWidget *parent;
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
g_return_val_if_fail(compWin != NULL, FALSE);
2001-04-19 14:21:46 +02:00
2003-06-21 08:23:00 +02:00
entry = compWin->entry;
list_view = compWin->list_view;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(entry != NULL, FALSE);
/* allow keyboard navigation in the alternatives tree view */
2001-04-19 14:21:46 +02:00
if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
completion_window_advance_selection
(GTK_TREE_VIEW(list_view),
2001-04-19 14:21:46 +02:00
event->keyval == GDK_Down ||
event->keyval == GDK_Page_Down ? TRUE : FALSE);
return FALSE;
}
2003-06-26 07:40:21 +02:00
#if 0
2001-04-19 14:21:46 +02:00
/* also make tab / shift tab go to next previous completion entry. we're
* changing the key value */
if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
event->keyval = (event->state & GDK_SHIFT_MASK)
? GDK_Up : GDK_Down;
/* need to reset shift state if going up */
if (event->state & GDK_SHIFT_MASK)
event->state &= ~GDK_SHIFT_MASK;
completion_window_advance_selection(GTK_CLIST(clist),
event->keyval == GDK_Down ? TRUE : FALSE);
return FALSE;
}
2003-06-26 07:40:21 +02:00
#endif
/* make tab move to next field */
if( event->keyval == GDK_Tab ) {
/* Reference to parent */
parent = GTK_WIDGET(entry)->parent;
/* Discard the window */
clear_completion_cache();
addrcompl_destroy_window( _compWindow_ );
/* Move focus to next widget */
if( parent ) {
2003-10-05 12:10:30 +02:00
gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
2003-06-26 07:40:21 +02:00
}
return FALSE;
}
/* make backtab move to previous field */
if( event->keyval == GDK_ISO_Left_Tab ) {
/* Reference to parent */
parent = GTK_WIDGET(entry)->parent;
/* Discard the window */
clear_completion_cache();
addrcompl_destroy_window( _compWindow_ );
/* Move focus to previous widget */
if( parent ) {
2003-10-05 12:10:30 +02:00
gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
2003-06-26 07:40:21 +02:00
}
return FALSE;
}
2001-04-19 14:21:46 +02:00
/* look for presses that accept the selection */
if (event->keyval == GDK_Return || event->keyval == GDK_space) {
2003-06-21 08:23:00 +02:00
/* User selected address with a key press */
/* Display selected address in entry field */
completion_window_apply_selection(
GTK_TREE_VIEW(list_view), GTK_ENTRY(entry) );
2003-06-21 08:23:00 +02:00
/* Discard the window */
clear_completion_cache();
2003-06-21 08:23:00 +02:00
addrcompl_destroy_window( _compWindow_ );
2001-04-19 14:21:46 +02:00
return FALSE;
}
/* key state keys should never be handled */
if (event->keyval == GDK_Shift_L
|| event->keyval == GDK_Shift_R
|| event->keyval == GDK_Control_L
|| event->keyval == GDK_Control_R
|| event->keyval == GDK_Caps_Lock
|| event->keyval == GDK_Shift_Lock
|| event->keyval == GDK_Meta_L
|| event->keyval == GDK_Meta_R
|| event->keyval == GDK_Alt_L
|| event->keyval == GDK_Alt_R) {
return FALSE;
}
2003-06-21 08:23:00 +02:00
/* some other key, let's restore the searchTerm (orignal text) */
searchTerm = _compWindow_->searchTerm;
2001-04-19 14:21:46 +02:00
g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
2003-06-21 08:23:00 +02:00
replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
2001-04-19 14:21:46 +02:00
/* make sure anything we typed comes in the edit box */
tmp_event.type = event->type;
tmp_event.window = entry->window;
tmp_event.send_event = TRUE;
tmp_event.time = event->time;
tmp_event.state = event->state;
tmp_event.keyval = event->keyval;
tmp_event.length = event->length;
tmp_event.string = event->string;
gtk_widget_event(entry, (GdkEvent *)&tmp_event);
/* and close the completion window */
2003-06-21 08:23:00 +02:00
clear_completion_cache();
addrcompl_destroy_window( _compWindow_ );
2001-04-19 14:21:46 +02:00
return TRUE;
}
2003-06-21 08:23:00 +02:00
/*
* ============================================================================
* Publically accessible functions.
* ============================================================================
*/
/**
* Setup completion object.
*/
2004-01-12 22:28:31 +01:00
void addrcompl_initialize( void ) {
2003-06-21 08:23:00 +02:00
/* printf( "addrcompl_initialize...\n" ); */
if( ! _compWindow_ ) {
_compWindow_ = addrcompl_create_window();
}
_queryID_ = 0;
_completionIdleID_ = 0;
/* printf( "addrcompl_initialize...done\n" ); */
}
/**
* Teardown completion object.
*/
void addrcompl_teardown( void ) {
/* printf( "addrcompl_teardown...\n" ); */
addrcompl_free_window( _compWindow_ );
_compWindow_ = NULL;
if( _displayQueue_ ) {
g_list_free( _displayQueue_ );
}
_displayQueue_ = NULL;
_completionIdleID_ = 0;
/* printf( "addrcompl_teardown...done\n" ); */
}
/*
* tree view functions
*/
static GtkListStore *addr_compl_create_store(void)
{
return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
G_TYPE_STRING,
-1);
}
static void addr_compl_list_view_add_address(GtkWidget *list_view,
const gchar *address)
{
GtkTreeIter iter;
GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list_view)));
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
ADDR_COMPL_ADDRESS, address,
-1);
}
static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
{
GtkTreeView *list_view;
GtkTreeSelection *selector;
GtkTreeModel *model;
model = GTK_TREE_MODEL(addr_compl_create_store());
list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
g_object_unref(model);
gtk_tree_view_set_rules_hint(list_view, TRUE);
gtk_tree_view_set_headers_visible(list_view, FALSE);
selector = gtk_tree_view_get_selection(list_view);
gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
gtk_tree_selection_set_select_function(selector, addr_compl_selected,
window, NULL);
/* create the columns */
addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
return GTK_WIDGET(list_view);
}
static void addr_compl_create_list_view_columns(GtkWidget *list_view)
{
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes
("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
}
static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
CompletionWindow *window)
{
if (window && event && event->type == GDK_BUTTON_PRESS) {
window->in_mouse = TRUE;
}
return FALSE;
}
static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
CompletionWindow *window)
{
if (window && event && event->type == GDK_BUTTON_RELEASE) {
window->in_mouse = FALSE;
}
return FALSE;
}
static gboolean addr_compl_selected(GtkTreeSelection *selector,
GtkTreeModel *model,
GtkTreePath *path,
gboolean currently_selected,
gpointer data)
{
CompletionWindow *window = data;
if (currently_selected)
return TRUE;
if (!window->in_mouse)
return TRUE;
/* XXX: select the entry and kill window later... select is called before
* any other mouse events handlers including the tree view internal one;
* not using a time out would result in a crash. if this doesn't work
* safely, maybe we should set variables when receiving button presses
* in the tree view. */
if (!window->destroying) {
window->destroying = TRUE;
g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
}
return TRUE;
}
static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
{
GtkEntry *entry = GTK_ENTRY(window->entry);
completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
entry);
clear_completion_cache();
addrcompl_destroy_window(window);
return FALSE;
}
2003-06-21 08:23:00 +02:00
/*
* End of Source.
*/