2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
4 * Copyright (C) 2000-2004 by Alfons Hoogervorst & The Sylpheed Claws Team.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkmain.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkentry.h>
32 #include <gtk/gtkeditable.h>
33 #include <gtk/gtkclist.h>
34 #include <gtk/gtkscrolledwindow.h>
38 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
43 #include "addrindex.h"
44 #include "addr_compl.h"
51 * The address book is read into memory. We set up an address list
52 * containing all address book entries. Next we make the completion
53 * list, which contains all the completable strings, and store a
54 * reference to the address entry it belongs to.
55 * After calling the g_completion_complete(), we get a reference
56 * to a valid email address.
58 * Completion is very simplified. We never complete on another prefix,
59 * i.e. we neglect the next smallest possible prefix for the current
60 * completion cache. This is simply done so we might break up the
61 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
62 * something like alfons, proteus, demon, nl; and then completing on
63 * any of those words).
67 * address_entry - structure which refers to the original address entry in the
77 * completion_entry - structure used to complete addresses, with a reference
78 * the the real address information.
82 gchar *string; /* string to complete */
83 address_entry *ref; /* address the string belongs to */
86 /*******************************************************************************/
88 static gint g_ref_count; /* list ref count */
89 static GList *g_completion_list; /* list of strings to be checked */
90 static GList *g_address_list; /* address storage */
91 static GCompletion *g_completion; /* completion object */
93 /* To allow for continuing completion we have to keep track of the state
94 * using the following variables. No need to create a context object. */
96 static gint g_completion_count; /* nr of addresses incl. the prefix */
97 static gint g_completion_next; /* next prev address */
98 static GSList *g_completion_addresses; /* unique addresses found in the
100 static gchar *g_completion_prefix; /* last prefix. (this is cached here
101 * because the prefix passed to g_completion
102 * is g_strdown()'ed */
104 /*******************************************************************************/
107 * Function used by GTK to find the string data to be used for completion.
108 * \param data Pointer to data being processed.
110 static gchar *completion_func(gpointer data)
112 g_return_val_if_fail(data != NULL, NULL);
114 return ((completion_entry *)data)->string;
118 * Initialize all completion index data.
120 static void init_all(void)
122 g_completion = g_completion_new(completion_func);
123 g_return_if_fail(g_completion != NULL);
127 * Free up all completion index data.
129 static void free_all(void)
133 walk = g_list_first(g_completion_list);
134 for (; walk != NULL; walk = g_list_next(walk)) {
135 completion_entry *ce = (completion_entry *) walk->data;
139 g_list_free(g_completion_list);
140 g_completion_list = NULL;
142 walk = g_address_list;
143 for (; walk != NULL; walk = g_list_next(walk)) {
144 address_entry *ae = (address_entry *) walk->data;
149 g_list_free(g_address_list);
150 g_address_list = NULL;
152 g_completion_free(g_completion);
157 * Append specified address entry to the index.
158 * \param str Index string value.
159 * \param ae Entry containing address data.
161 static void add_address1(const char *str, address_entry *ae)
163 completion_entry *ce1;
164 ce1 = g_new0(completion_entry, 1),
165 ce1->string = g_utf8_strdown(str, -1);
166 /* GCompletion list is case sensitive */
167 g_strdown(ce1->string);
170 g_completion_list = g_list_prepend(g_completion_list, ce1);
174 * Adds address to the completion list. This function looks complicated, but
175 * it's only allocation checks. Each value will be included in the index.
176 * \param name Recipient name.
177 * \param address EMail address.
178 * \param alias Alias to append.
179 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
182 static gint add_address(const gchar *name, const gchar *address,
183 const gchar *nick, const gchar *alias)
187 if (!name || !address) return -1;
189 ae = g_new0(address_entry, 1);
191 g_return_val_if_fail(ae != NULL, -1);
193 ae->name = g_strdup(name);
194 ae->address = g_strdup(address);
196 g_address_list = g_list_prepend(g_address_list, ae);
198 add_address1(name, ae);
199 add_address1(address, ae);
202 add_address1(nick, ae);
204 if ( alias != NULL ) {
205 add_address1(alias, ae);
212 * Read address book, creating all entries in the completion index.
214 static void read_address_book(void) {
215 addrindex_load_completion( add_address );
216 g_address_list = g_list_reverse(g_address_list);
217 g_completion_list = g_list_reverse(g_completion_list);
221 * Test whether there is a completion pending.
222 * \return <code>TRUE</code> if pending.
224 static gboolean is_completion_pending(void)
226 /* check if completion pending, i.e. we might satisfy a request for the next
227 * or previous address */
228 return g_completion_count;
232 * Clear the completion cache.
234 static void clear_completion_cache(void)
236 if (is_completion_pending()) {
237 if (g_completion_prefix)
238 g_free(g_completion_prefix);
240 if (g_completion_addresses) {
241 g_slist_free(g_completion_addresses);
242 g_completion_addresses = NULL;
245 g_completion_count = g_completion_next = 0;
250 * Prepare completion index. This function should be called prior to attempting
251 * address completion.
252 * \return The number of addresses in the completion list.
254 gint start_address_completion(void)
256 clear_completion_cache();
259 /* open the address book */
261 /* merge the completion entry list into g_completion */
262 if (g_completion_list)
263 g_completion_add_items(g_completion, g_completion_list);
266 debug_print("start_address_completion ref count %d\n", g_ref_count);
268 return g_list_length(g_completion_list);
272 * Retrieve a possible address (or a part) from an entry box. To make life
273 * easier, we only look at the last valid address component; address
274 * completion only works at the last string component in the entry box.
276 * \param entry Address entry field.
277 * \param start_pos Address of start position of address.
278 * \return Possible address.
280 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
282 const gchar *edit_text, *p;
284 gboolean in_quote = FALSE;
285 gboolean in_bracket = FALSE;
288 edit_text = gtk_entry_get_text(entry);
289 if (edit_text == NULL) return NULL;
291 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
293 /* scan for a separator. doesn't matter if walk points at null byte. */
294 for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
296 p = g_utf8_prev_char(p)) {
299 } else if (!in_quote) {
300 if (!in_bracket && *p == ',') {
302 } else if (*p == '<')
309 /* have something valid */
310 if (g_utf8_strlen(p, -1) == 0)
313 #define IS_VALID_CHAR(x) \
314 (isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
316 /* now scan back until we hit a valid character */
317 for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
322 if (g_utf8_strlen(p, -1) == 0)
325 if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
333 * Replace an incompleted address with a completed one.
334 * \param entry Address entry field.
335 * \param newtext New text.
336 * \param start_pos Insertion point in entry field.
338 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
341 if (!newtext) return;
342 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
343 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
345 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
349 * Attempt to complete an address, and returns the number of addresses found.
350 * Use <code>get_complete_address()</code> to get an entry from the index.
352 * \param str Search string to find.
353 * \return Zero if no match was found, otherwise the number of addresses; the
354 * original prefix (search string) will appear at index 0.
356 guint complete_address(const gchar *str)
361 completion_entry *ce;
363 g_return_val_if_fail(str != NULL, 0);
365 /* g_completion is case sensitive */
366 d = g_utf8_strdown(str, -1);
368 clear_completion_cache();
369 g_completion_prefix = g_strdup(str);
371 result = g_completion_complete(g_completion, d, NULL);
373 count = g_list_length(result);
375 /* create list with unique addresses */
376 for (cpl = 0, result = g_list_first(result);
378 result = g_list_next(result)) {
379 ce = (completion_entry *)(result->data);
380 if (NULL == g_slist_find(g_completion_addresses,
383 g_completion_addresses =
384 g_slist_append(g_completion_addresses,
388 count = cpl + 1; /* index 0 is the original prefix */
389 g_completion_next = 1; /* we start at the first completed one */
391 g_free(g_completion_prefix);
392 g_completion_prefix = NULL;
395 g_completion_count = count;
403 * Return a complete address from the index.
404 * \param index Index of entry that was found (by the previous call to
405 * <code>complete_address()</code>
406 * \return Completed address string; this should be freed when done.
408 gchar *get_complete_address(gint index)
410 const address_entry *p;
411 gchar *address = NULL;
413 if (index < g_completion_count) {
415 address = g_strdup(g_completion_prefix);
417 /* get something from the unique addresses */
418 p = (address_entry *)g_slist_nth_data
419 (g_completion_addresses, index - 1);
421 if (!p->name || p->name[0] == '\0')
422 address = g_strdup_printf(p->address);
423 else if (strchr_with_skip_quote(p->name, '"', ','))
424 address = g_strdup_printf
425 ("\"%s\" <%s>", p->name, p->address);
427 address = g_strdup_printf
428 ("%s <%s>", p->name, p->address);
437 * Return the next complete address match from the completion index.
438 * \return Completed address string; this should be freed when done.
440 static gchar *get_next_complete_address(void)
442 if (is_completion_pending()) {
445 res = get_complete_address(g_completion_next);
446 g_completion_next += 1;
447 if (g_completion_next >= g_completion_count)
448 g_completion_next = 0;
456 * Return a count of the completed matches in the completion index.
457 * \return Number of matched entries.
459 static guint get_completion_count(void)
461 if (is_completion_pending())
462 return g_completion_count;
468 * Invalidate address completion index. This function should be called whenever
469 * the address book changes. This forces data to be read into the completion
471 * \return Number of entries in index.
473 gint invalidate_address_completion(void)
476 /* simply the same as start_address_completion() */
477 debug_print("Invalidation request for address completion\n");
481 g_completion_add_items(g_completion, g_completion_list);
482 clear_completion_cache();
485 return g_list_length(g_completion_list);
489 * Finished with completion index. This function should be called after
490 * matching addresses.
491 * \return Reference count.
493 gint end_address_completion(void)
495 clear_completion_cache();
497 if (0 == --g_ref_count)
500 debug_print("end_address_completion ref count %d\n", g_ref_count);
506 * Define the structure of the completion window.
508 typedef struct _CompletionWindow CompletionWindow;
509 struct _CompletionWindow {
520 static CompletionWindow *_compWindow_ = NULL;
523 * Mutex to protect callback from multiple threads.
525 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
528 * Completion queue list.
530 static GList *_displayQueue_ = NULL;
535 static gint _queryID_ = 0;
538 * Completion idle ID.
540 static guint _completionIdleID_ = 0;
543 * address completion entry ui. the ui (completion list was inspired by galeon's
544 * auto completion list). remaining things powered by sylpheed's completion engine.
547 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
549 static void address_completion_mainwindow_set_focus (GtkWindow *window,
552 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
555 static gboolean address_completion_complete_address_in_entry
558 static void address_completion_create_completion_window (GtkEntry *entry);
560 static void completion_window_select_row(GtkCList *clist,
564 CompletionWindow *compWin );
566 static gboolean completion_window_button_press
568 GdkEventButton *event,
569 CompletionWindow *compWin );
571 static gboolean completion_window_key_press
574 CompletionWindow *compWin );
575 static void address_completion_create_completion_window( GtkEntry *entry_ );
578 * Create a completion window object.
579 * \return Initialized completion window.
581 static CompletionWindow *addrcompl_create_window( void ) {
582 CompletionWindow *cw;
584 cw = g_new0( CompletionWindow, 1 );
586 cw->searchTerm = NULL;
595 * Destroy completion window.
596 * \param cw Window to destroy.
598 static void addrcompl_destroy_window( CompletionWindow *cw ) {
599 /* Stop all searches currently in progress */
600 addrindex_stop_search( _queryID_ );
602 /* Remove idler function... or application may not terminate */
603 if( _completionIdleID_ != 0 ) {
604 gtk_idle_remove( _completionIdleID_ );
605 _completionIdleID_ = 0;
608 /* Now destroy window */
610 /* Clear references to widgets */
616 gtk_widget_hide( cw->window );
617 gtk_widget_destroy( cw->window );
624 * Free up completion window.
625 * \param cw Window to free.
627 static void addrcompl_free_window( CompletionWindow *cw ) {
629 addrcompl_destroy_window( cw );
631 g_free( cw->searchTerm );
632 cw->searchTerm = NULL;
634 /* Clear references */
643 * Advance selection to previous/next item in list.
644 * \param clist List to process.
645 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
648 static void completion_window_advance_selection(GtkCList *clist, gboolean forward)
652 g_return_if_fail(clist != NULL);
654 if( clist->selection ) {
655 row = GPOINTER_TO_INT(clist->selection->data);
656 row = forward ? ( row + 1 ) : ( row - 1 );
657 gtk_clist_freeze(clist);
658 gtk_clist_select_row(clist, row, 0);
659 gtk_clist_thaw(clist);
664 /* completion_window_accept_selection() - accepts the current selection in the
665 * clist, and destroys the window */
666 static void completion_window_accept_selection(GtkWidget **window,
670 gchar *address = NULL, *text = NULL;
671 gint cursor_pos, row;
673 g_return_if_fail(window != NULL);
674 g_return_if_fail(*window != NULL);
675 g_return_if_fail(clist != NULL);
676 g_return_if_fail(entry != NULL);
677 g_return_if_fail(clist->selection != NULL);
679 /* FIXME: I believe it's acceptable to access the selection member directly */
680 row = GPOINTER_TO_INT(clist->selection->data);
682 /* we just need the cursor position */
683 address = get_address_from_edit(entry, &cursor_pos);
685 gtk_clist_get_text(clist, row, 0, &text);
686 replace_address_in_edit(entry, text, cursor_pos);
688 clear_completion_cache();
689 gtk_widget_destroy(*window);
695 * Resize window to accommodate maximum number of address entries.
696 * \param cw Completion window.
698 static void addrcompl_resize_window( CompletionWindow *cw ) {
700 gint x, y, width, height, depth;
702 /* Get current geometry of window */
703 gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
705 gtk_widget_hide_all( cw->window );
706 gtk_widget_show_all( cw->window );
707 gtk_widget_size_request( cw->clist, &r );
709 /* Adjust window height to available screen space */
710 if( ( y + r.height ) > gdk_screen_height() ) {
711 gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
712 gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
714 gtk_widget_set_size_request(cw->window, width, r.height);
718 * Add an address the completion window address list.
719 * \param cw Completion window.
720 * \param address Address to add.
722 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
723 gchar *text[] = { NULL, NULL };
725 /* printf( "\t\tAdding :%s\n", address ); */
727 gtk_clist_append( GTK_CLIST(cw->clist), text );
731 addrcompl_resize_window( cw );
732 gtk_grab_add( cw->window );
734 if( cw->listCount == 1 ) {
735 /* Select first row for now */
736 gtk_clist_select_row( GTK_CLIST(cw->clist), 0, 0);
738 else if( cw->listCount == 2 ) {
739 /* Move off first row */
740 gtk_clist_select_row( GTK_CLIST(cw->clist), 1, 0);
745 * Completion idle function. This function is called by the main (UI) thread
746 * during UI idle time while an address search is in progress. Items from the
747 * display queue are processed and appended to the address list.
749 * \param data Target completion window to receive email addresses.
750 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
752 static gboolean addrcompl_idle( gpointer data ) {
756 /* Process all entries in display queue */
757 pthread_mutex_lock( & _completionMutex_ );
758 if( _displayQueue_ ) {
759 node = _displayQueue_;
761 address = node->data;
762 /* printf( "address ::: %s :::\n", address ); */
763 addrcompl_add_entry( _compWindow_, address );
765 node = g_list_next( node );
767 g_list_free( _displayQueue_ );
768 _displayQueue_ = NULL;
770 pthread_mutex_unlock( & _completionMutex_ );
776 * Callback entry point. The background thread (if any) appends the address
777 * list to the display queue.
778 * \param sender Sender of query.
779 * \param queryID Query ID of search request.
780 * \param listEMail List of zero of more email objects that met search
782 * \param data Query data.
784 static gint addrcompl_callback_entry(
785 gpointer sender, gint queryID, GList *listEMail, gpointer data )
790 /* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
791 pthread_mutex_lock( & _completionMutex_ );
792 if( queryID == _queryID_ ) {
793 /* Append contents to end of display queue */
796 ItemEMail *email = node->data;
798 address = addritem_format_email( email );
799 /* printf( "\temail/address ::%s::\n", address ); */
800 _displayQueue_ = g_list_append( _displayQueue_, address );
801 node = g_list_next( node );
804 g_list_free( listEMail );
805 pthread_mutex_unlock( & _completionMutex_ );
811 * Clear the display queue.
813 static void addrcompl_clear_queue( void ) {
814 /* Clear out display queue */
815 pthread_mutex_lock( & _completionMutex_ );
817 g_list_free( _displayQueue_ );
818 _displayQueue_ = NULL;
820 pthread_mutex_unlock( & _completionMutex_ );
824 * Add a single address entry into the display queue.
825 * \param address Address to append.
827 static void addrcompl_add_queue( gchar *address ) {
828 pthread_mutex_lock( & _completionMutex_ );
829 _displayQueue_ = g_list_append( _displayQueue_, address );
830 pthread_mutex_unlock( & _completionMutex_ );
834 * Load list with entries from local completion index.
836 static void addrcompl_load_local( void ) {
839 for (count = 0; count < get_completion_count(); count++) {
842 address = get_complete_address( count );
843 /* printf( "\taddress ::%s::\n", address ); */
845 /* Append contents to end of display queue */
846 addrcompl_add_queue( address );
853 static void addrcompl_start_search( void ) {
856 searchTerm = g_strdup( _compWindow_->searchTerm );
858 /* Setup the search */
859 _queryID_ = addrindex_setup_search(
860 searchTerm, NULL, addrcompl_callback_entry );
861 g_free( searchTerm );
862 /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
864 /* Load local stuff */
865 addrcompl_load_local();
867 /* Sit back and wait until something happens */
869 gtk_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
870 /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
872 addrindex_start_search( _queryID_ );
876 * Apply the current selection in the list to the entry field. Focus is also
877 * moved to the next widget so that Tab key works correctly.
878 * \param clist List to process.
879 * \param entry Address entry field.
881 static void completion_window_apply_selection(GtkCList *clist, GtkEntry *entry)
883 gchar *address = NULL, *text = NULL;
884 gint cursor_pos, row;
887 g_return_if_fail(clist != NULL);
888 g_return_if_fail(entry != NULL);
889 g_return_if_fail(clist->selection != NULL);
891 /* First remove the idler */
892 if( _completionIdleID_ != 0 ) {
893 gtk_idle_remove( _completionIdleID_ );
894 _completionIdleID_ = 0;
897 /* Process selected item */
898 row = GPOINTER_TO_INT(clist->selection->data);
900 address = get_address_from_edit(entry, &cursor_pos);
902 gtk_clist_get_text(clist, row, 0, &text);
903 replace_address_in_edit(entry, text, cursor_pos);
905 /* Move focus to next widget */
906 parent = GTK_WIDGET(entry)->parent;
908 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
913 * Start address completion. Should be called when creating the main window
914 * containing address completion entries.
915 * \param mainwindow Main window.
917 void address_completion_start(GtkWidget *mainwindow)
919 start_address_completion();
921 /* register focus change hook */
922 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
923 G_CALLBACK(address_completion_mainwindow_set_focus),
928 * Need unique data to make unregistering signal handler possible for the auto
931 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
934 * Register specified entry widget for address completion.
935 * \param entry Address entry field.
937 void address_completion_register_entry(GtkEntry *entry)
939 g_return_if_fail(entry != NULL);
940 g_return_if_fail(GTK_IS_ENTRY(entry));
942 /* add hooked property */
943 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
945 /* add keypress event */
946 g_signal_connect_closure
947 (G_OBJECT(entry), "key_press_event",
948 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
949 COMPLETION_UNIQUE_DATA,
955 * Unregister specified entry widget from address completion operations.
956 * \param entry Address entry field.
958 void address_completion_unregister_entry(GtkEntry *entry)
960 GtkObject *entry_obj;
962 g_return_if_fail(entry != NULL);
963 g_return_if_fail(GTK_IS_ENTRY(entry));
965 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
966 g_return_if_fail(entry_obj);
967 g_return_if_fail(entry_obj == G_OBJECT(entry));
969 /* has the hooked property? */
970 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
972 /* remove the hook */
973 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
974 G_CALLBACK(address_completion_entry_key_pressed),
975 COMPLETION_UNIQUE_DATA);
979 * End address completion. Should be called when main window with address
980 * completion entries terminates. NOTE: this function assumes that it is
981 * called upon destruction of the window.
982 * \param mainwindow Main window.
984 void address_completion_end(GtkWidget *mainwindow)
986 /* if address_completion_end() is really called on closing the window,
987 * we don't need to unregister the set_focus_cb */
988 end_address_completion();
991 /* if focus changes to another entry, then clear completion cache */
992 static void address_completion_mainwindow_set_focus(GtkWindow *window,
997 if (widget && GTK_IS_ENTRY(widget) &&
998 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
999 clear_completion_cache();
1004 * Listener that watches for tab or other keystroke in address entry field.
1005 * \param entry Address entry field.
1006 * \param ev Event object.
1007 * \param data User data.
1008 * \return <i>TRUE</i>.
1010 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1014 if (ev->keyval == GDK_Tab) {
1015 addrcompl_clear_queue();
1017 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1018 /* route a void character to the default handler */
1019 /* this is a dirty hack; we're actually changing a key
1020 * reported by the system. */
1021 ev->keyval = GDK_AudibleBell_Enable;
1022 ev->state &= ~GDK_SHIFT_MASK;
1025 address_completion_create_completion_window(entry);
1027 /* Start remote queries */
1028 addrcompl_start_search();
1035 } else if (ev->keyval == GDK_Shift_L
1036 || ev->keyval == GDK_Shift_R
1037 || ev->keyval == GDK_Control_L
1038 || ev->keyval == GDK_Control_R
1039 || ev->keyval == GDK_Caps_Lock
1040 || ev->keyval == GDK_Shift_Lock
1041 || ev->keyval == GDK_Meta_L
1042 || ev->keyval == GDK_Meta_R
1043 || ev->keyval == GDK_Alt_L
1044 || ev->keyval == GDK_Alt_R) {
1045 /* these buttons should not clear the cache... */
1047 clear_completion_cache();
1052 * Initialize search term for address completion.
1053 * \param entry Address entry field.
1055 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1058 gint ncount, cursor_pos;
1059 gchar *searchTerm, *new = NULL;
1061 g_return_val_if_fail(entry != NULL, FALSE);
1063 if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
1065 /* get an address component from the cursor */
1066 searchTerm = get_address_from_edit( entry, &cursor_pos );
1067 if( ! searchTerm ) return FALSE;
1068 /* printf( "search for :::%s:::\n", searchTerm ); */
1070 /* Clear any existing search */
1071 if( _compWindow_->searchTerm ) {
1072 g_free( _compWindow_->searchTerm );
1074 _compWindow_->searchTerm = g_strdup( searchTerm );
1076 /* Perform search on local completion index */
1077 ncount = complete_address( searchTerm );
1079 new = get_next_complete_address();
1083 /* Select the address if there is only one match */
1085 /* Display selected address in entry field */
1086 gchar *addr = get_complete_address(1);
1089 replace_address_in_edit(entry, addr, cursor_pos);
1093 /* Discard the window */
1094 clear_completion_cache();
1096 /* Make sure that drop-down appears uniform! */
1097 else if( ncount == 0 ) {
1098 addrcompl_add_queue( g_strdup( searchTerm ) );
1100 g_free( searchTerm );
1106 * Create new address completion window for specified entry.
1107 * \param entry_ Entry widget to associate with window.
1109 static void address_completion_create_completion_window( GtkEntry *entry_ )
1111 gint x, y, height, width, depth;
1112 GtkWidget *scroll, *clist;
1115 GtkWidget *entry = GTK_WIDGET(entry_);
1117 /* Create new window and list */
1118 window = gtk_window_new(GTK_WINDOW_POPUP);
1119 clist = gtk_clist_new(1);
1121 /* Destroy any existing window */
1122 addrcompl_destroy_window( _compWindow_ );
1124 /* Create new object */
1125 _compWindow_->window = window;
1126 _compWindow_->entry = entry;
1127 _compWindow_->clist = clist;
1128 _compWindow_->listCount = 0;
1130 scroll = gtk_scrolled_window_new(NULL, NULL);
1131 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1132 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1133 gtk_container_add(GTK_CONTAINER(window), scroll);
1134 gtk_container_add(GTK_CONTAINER(scroll), clist);
1135 gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_SINGLE);
1137 /* Use entry widget to create initial window */
1138 gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
1139 gdk_window_get_deskrelative_origin (entry->window, &x, &y);
1141 gtk_window_move(GTK_WINDOW(window), x, y);
1143 /* Resize window to fit initial (empty) address list */
1144 gtk_widget_size_request( clist, &r );
1145 gtk_widget_set_size_request( window, width, r.height );
1146 gtk_widget_show_all( window );
1147 gtk_widget_size_request( clist, &r );
1149 /* Setup handlers */
1150 g_signal_connect(G_OBJECT(clist), "select_row",
1151 G_CALLBACK(completion_window_select_row),
1153 g_signal_connect(G_OBJECT(window),
1154 "button-press-event",
1155 G_CALLBACK(completion_window_button_press),
1157 g_signal_connect(G_OBJECT(window),
1159 G_CALLBACK(completion_window_key_press),
1161 gdk_pointer_grab(window->window, TRUE,
1162 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1163 GDK_BUTTON_RELEASE_MASK,
1164 NULL, NULL, GDK_CURRENT_TIME);
1165 gtk_grab_add( window );
1167 /* this gets rid of the irritating focus rectangle that doesn't
1168 * follow the selection */
1169 GTK_WIDGET_UNSET_FLAGS(clist, GTK_CAN_FOCUS);
1173 * Respond to select row event in clist object. selection sends completed
1174 * address to entry. Note: event is NULL if selected by anything else than a
1176 * \param widget Window object.
1177 * \param event Event.
1178 * \param compWind Reference to completion window.
1180 static void completion_window_select_row(GtkCList *clist, gint row, gint col,
1182 CompletionWindow *compWin )
1186 g_return_if_fail(compWin != NULL);
1188 entry = GTK_ENTRY(compWin->entry);
1189 g_return_if_fail(entry != NULL);
1191 /* Don't update unless user actually selects ! */
1192 if (!event || event->type != GDK_BUTTON_RELEASE)
1195 /* User selected address by releasing the mouse in drop-down list*/
1196 completion_window_apply_selection( clist, entry );
1198 clear_completion_cache();
1199 addrcompl_destroy_window( _compWindow_ );
1203 * Respond to button press in completion window. Check if mouse click is
1204 * anywhere outside the completion window. In that case the completion
1205 * window is destroyed, and the original searchTerm is restored.
1207 * \param widget Window object.
1208 * \param event Event.
1209 * \param compWin Reference to completion window.
1211 static gboolean completion_window_button_press(GtkWidget *widget,
1212 GdkEventButton *event,
1213 CompletionWindow *compWin )
1215 GtkWidget *event_widget, *entry;
1218 gboolean restore = TRUE;
1220 g_return_val_if_fail(compWin != NULL, FALSE);
1222 entry = compWin->entry;
1223 g_return_val_if_fail(entry != NULL, FALSE);
1225 /* Test where mouse was clicked */
1226 event_widget = gtk_get_event_widget((GdkEvent *)event);
1227 if (event_widget != widget) {
1228 while (event_widget) {
1229 if (event_widget == widget)
1231 else if (event_widget == entry) {
1235 event_widget = event_widget->parent;
1240 /* Clicked outside of completion window - restore */
1241 searchTerm = _compWindow_->searchTerm;
1242 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1243 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1246 clear_completion_cache();
1247 addrcompl_destroy_window( _compWindow_ );
1253 * Respond to key press in completion window.
1254 * \param widget Window object.
1255 * \param event Event.
1256 * \param compWind Reference to completion window.
1258 static gboolean completion_window_key_press(GtkWidget *widget,
1260 CompletionWindow *compWin )
1262 GdkEventKey tmp_event;
1269 g_return_val_if_fail(compWin != NULL, FALSE);
1271 entry = compWin->entry;
1272 clist = compWin->clist;
1273 g_return_val_if_fail(entry != NULL, FALSE);
1275 /* allow keyboard navigation in the alternatives clist */
1276 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
1277 event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
1278 completion_window_advance_selection
1280 event->keyval == GDK_Down ||
1281 event->keyval == GDK_Page_Down ? TRUE : FALSE);
1286 /* also make tab / shift tab go to next previous completion entry. we're
1287 * changing the key value */
1288 if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
1289 event->keyval = (event->state & GDK_SHIFT_MASK)
1290 ? GDK_Up : GDK_Down;
1291 /* need to reset shift state if going up */
1292 if (event->state & GDK_SHIFT_MASK)
1293 event->state &= ~GDK_SHIFT_MASK;
1294 completion_window_advance_selection(GTK_CLIST(clist),
1295 event->keyval == GDK_Down ? TRUE : FALSE);
1300 /* make tab move to next field */
1301 if( event->keyval == GDK_Tab ) {
1302 /* Reference to parent */
1303 parent = GTK_WIDGET(entry)->parent;
1305 /* Discard the window */
1306 clear_completion_cache();
1307 addrcompl_destroy_window( _compWindow_ );
1309 /* Move focus to next widget */
1311 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1316 /* make backtab move to previous field */
1317 if( event->keyval == GDK_ISO_Left_Tab ) {
1318 /* Reference to parent */
1319 parent = GTK_WIDGET(entry)->parent;
1321 /* Discard the window */
1322 clear_completion_cache();
1323 addrcompl_destroy_window( _compWindow_ );
1325 /* Move focus to previous widget */
1327 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1332 /* look for presses that accept the selection */
1333 if (event->keyval == GDK_Return || event->keyval == GDK_space) {
1334 /* User selected address with a key press */
1336 /* Display selected address in entry field */
1337 completion_window_apply_selection(
1338 GTK_CLIST(clist), GTK_ENTRY(entry) );
1340 /* Discard the window */
1341 clear_completion_cache();
1342 addrcompl_destroy_window( _compWindow_ );
1346 /* key state keys should never be handled */
1347 if (event->keyval == GDK_Shift_L
1348 || event->keyval == GDK_Shift_R
1349 || event->keyval == GDK_Control_L
1350 || event->keyval == GDK_Control_R
1351 || event->keyval == GDK_Caps_Lock
1352 || event->keyval == GDK_Shift_Lock
1353 || event->keyval == GDK_Meta_L
1354 || event->keyval == GDK_Meta_R
1355 || event->keyval == GDK_Alt_L
1356 || event->keyval == GDK_Alt_R) {
1360 /* some other key, let's restore the searchTerm (orignal text) */
1361 searchTerm = _compWindow_->searchTerm;
1362 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1363 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1365 /* make sure anything we typed comes in the edit box */
1366 tmp_event.type = event->type;
1367 tmp_event.window = entry->window;
1368 tmp_event.send_event = TRUE;
1369 tmp_event.time = event->time;
1370 tmp_event.state = event->state;
1371 tmp_event.keyval = event->keyval;
1372 tmp_event.length = event->length;
1373 tmp_event.string = event->string;
1374 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1376 /* and close the completion window */
1377 clear_completion_cache();
1378 addrcompl_destroy_window( _compWindow_ );
1384 * ============================================================================
1385 * Publically accessible functions.
1386 * ============================================================================
1390 * Setup completion object.
1392 void addrcompl_initialize( void ) {
1393 /* printf( "addrcompl_initialize...\n" ); */
1394 if( ! _compWindow_ ) {
1395 _compWindow_ = addrcompl_create_window();
1398 _completionIdleID_ = 0;
1399 /* printf( "addrcompl_initialize...done\n" ); */
1403 * Teardown completion object.
1405 void addrcompl_teardown( void ) {
1406 /* printf( "addrcompl_teardown...\n" ); */
1407 addrcompl_free_window( _compWindow_ );
1408 _compWindow_ = NULL;
1409 if( _displayQueue_ ) {
1410 g_list_free( _displayQueue_ );
1412 _displayQueue_ = NULL;
1413 _completionIdleID_ = 0;
1414 /* printf( "addrcompl_teardown...done\n" ); */