2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
4 * Copyright (c) 2000-2003 by Alfons Hoogervorst <alfons@proteus.demon.nl>
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 "addressbook.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 * Reference to address index.
69 static AddressIndex *_addressIndex_ = NULL;
72 * address_entry - structure which refers to the original address entry in the
82 * completion_entry - structure used to complete addresses, with a reference
83 * the the real address information.
87 gchar *string; /* string to complete */
88 address_entry *ref; /* address the string belongs to */
91 /*******************************************************************************/
93 static gint g_ref_count; /* list ref count */
94 static GList *g_completion_list; /* list of strings to be checked */
95 static GList *g_address_list; /* address storage */
96 static GCompletion *g_completion; /* completion object */
98 /* To allow for continuing completion we have to keep track of the state
99 * using the following variables. No need to create a context object. */
101 static gint g_completion_count; /* nr of addresses incl. the prefix */
102 static gint g_completion_next; /* next prev address */
103 static GSList *g_completion_addresses; /* unique addresses found in the
105 static gchar *g_completion_prefix; /* last prefix. (this is cached here
106 * because the prefix passed to g_completion
107 * is g_strdown()'ed */
109 /*******************************************************************************/
112 * Function used by GTK to find the string data to be used for completion.
113 * \param data Pointer to data being processed.
115 static gchar *completion_func(gpointer data)
117 g_return_val_if_fail(data != NULL, NULL);
119 return ((completion_entry *)data)->string;
123 * Initialize all completion index data.
125 static void init_all(void)
127 g_completion = g_completion_new(completion_func);
128 g_return_if_fail(g_completion != NULL);
132 * Free up all completion index data.
134 static void free_all(void)
138 walk = g_list_first(g_completion_list);
139 for (; walk != NULL; walk = g_list_next(walk)) {
140 completion_entry *ce = (completion_entry *) walk->data;
144 g_list_free(g_completion_list);
145 g_completion_list = NULL;
147 walk = g_address_list;
148 for (; walk != NULL; walk = g_list_next(walk)) {
149 address_entry *ae = (address_entry *) walk->data;
154 g_list_free(g_address_list);
155 g_address_list = NULL;
157 g_completion_free(g_completion);
162 * Append specified address entry to the index.
163 * \param str Index string value.
164 * \param ae Entry containing address data.
166 static void add_address1(const char *str, address_entry *ae)
168 completion_entry *ce1;
169 ce1 = g_new0(completion_entry, 1),
170 ce1->string = g_strdup(str);
171 /* GCompletion list is case sensitive */
172 g_strdown(ce1->string);
175 g_completion_list = g_list_prepend(g_completion_list, ce1);
179 * Adds address to the completion list. This function looks complicated, but
180 * it's only allocation checks. Each value will be included in the index.
181 * \param name Recipient name.
182 * \param address EMail address.
183 * \param alias Alias to append.
184 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
187 static gint add_address(const gchar *name, const gchar *address, const gchar *alias)
191 if (!name || !address) return -1;
193 ae = g_new0(address_entry, 1);
195 g_return_val_if_fail(ae != NULL, -1);
197 ae->name = g_strdup(name);
198 ae->address = g_strdup(address);
200 g_address_list = g_list_prepend(g_address_list, ae);
202 add_address1(name, ae);
203 add_address1(address, ae);
204 add_address1(alias, ae);
210 * Read address book, creating all entries in the completion index.
212 static void read_address_book(void) {
213 addrindex_load_completion( _addressIndex_, add_address );
214 g_address_list = g_list_reverse(g_address_list);
215 g_completion_list = g_list_reverse(g_completion_list);
219 * Test whether there is a completion pending.
220 * \return <code>TRUE</code> if pending.
222 static gboolean is_completion_pending(void)
224 /* check if completion pending, i.e. we might satisfy a request for the next
225 * or previous address */
226 return g_completion_count;
230 * Clear the completion cache.
232 static void clear_completion_cache(void)
234 if (is_completion_pending()) {
235 if (g_completion_prefix)
236 g_free(g_completion_prefix);
238 if (g_completion_addresses) {
239 g_slist_free(g_completion_addresses);
240 g_completion_addresses = NULL;
243 g_completion_count = g_completion_next = 0;
248 * Prepare completion index. This function should be called prior to attempting
249 * address completion.
250 * \return The number of addresses in the completion list.
252 gint start_address_completion(void)
254 clear_completion_cache();
257 /* open the address book */
259 /* merge the completion entry list into g_completion */
260 if (g_completion_list)
261 g_completion_add_items(g_completion, g_completion_list);
264 debug_print("start_address_completion ref count %d\n", g_ref_count);
266 return g_list_length(g_completion_list);
270 * Retrieve a possible address (or a part) from an entry box. To make life
271 * easier, we only look at the last valid address component; address
272 * completion only works at the last string component in the entry box.
274 * \param entry Address entry field.
275 * \param start_pos Address of start position of address.
276 * \return Possible address.
278 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
280 const gchar *edit_text;
284 wchar_t rfc_mail_sep;
288 gboolean in_quote = FALSE;
289 gboolean in_bracket = FALSE;
292 if (mbtowc(&rfc_mail_sep, ",", 1) < 0) return NULL;
293 if (mbtowc("e, "\"", 1) < 0) return NULL;
294 if (mbtowc(<, "<", 1) < 0) return NULL;
295 if (mbtowc(>, ">", 1) < 0) return NULL;
297 edit_text = gtk_entry_get_text(entry);
298 if (edit_text == NULL) return NULL;
300 wtext = strdup_mbstowcs(edit_text);
301 g_return_val_if_fail(wtext != NULL, NULL);
303 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
305 /* scan for a separator. doesn't matter if walk points at null byte. */
306 for (wp = wtext + cur_pos; wp > wtext; wp--) {
309 else if (!in_quote) {
310 if (!in_bracket && *wp == rfc_mail_sep)
319 /* have something valid */
320 if (wcslen(wp) == 0) {
325 #define IS_VALID_CHAR(x) \
326 (iswalnum(x) || (x) == quote || (x) == lt || ((x) > 0x7f))
328 /* now scan back until we hit a valid character */
329 for (; *wp && !IS_VALID_CHAR(*wp); wp++)
334 if (wcslen(wp) == 0) {
339 if (start_pos) *start_pos = wp - wtext;
341 str = strdup_wcstombs(wp);
348 * Replace an incompleted address with a completed one.
349 * \param entry Address entry field.
350 * \param newtext New text.
351 * \param start_pos Insertion point in entry field.
353 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
356 if (!newtext) return;
357 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
358 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
360 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
364 * Attempt to complete an address, and returns the number of addresses found.
365 * Use <code>get_complete_address()</code> to get an entry from the index.
367 * \param str Search string to find.
368 * \return Zero if no match was found, otherwise the number of addresses; the
369 * original prefix (search string) will appear at index 0.
371 guint complete_address(const gchar *str)
376 completion_entry *ce;
378 g_return_val_if_fail(str != NULL, 0);
380 Xstrdup_a(d, str, return 0);
382 clear_completion_cache();
383 g_completion_prefix = g_strdup(str);
385 /* g_completion is case sensitive */
387 result = g_completion_complete(g_completion, d, NULL);
389 count = g_list_length(result);
391 /* create list with unique addresses */
392 for (cpl = 0, result = g_list_first(result);
394 result = g_list_next(result)) {
395 ce = (completion_entry *)(result->data);
396 if (NULL == g_slist_find(g_completion_addresses,
399 g_completion_addresses =
400 g_slist_append(g_completion_addresses,
404 count = cpl + 1; /* index 0 is the original prefix */
405 g_completion_next = 1; /* we start at the first completed one */
407 g_free(g_completion_prefix);
408 g_completion_prefix = NULL;
411 g_completion_count = count;
416 * Return a complete address from the index.
417 * \param index Index of entry that was found (by the previous call to
418 * <code>complete_address()</code>
419 * \return Completed address string; this should be freed when done.
421 gchar *get_complete_address(gint index)
423 const address_entry *p;
424 gchar *address = NULL;
426 if (index < g_completion_count) {
428 address = g_strdup(g_completion_prefix);
430 /* get something from the unique addresses */
431 p = (address_entry *)g_slist_nth_data
432 (g_completion_addresses, index - 1);
434 if (!p->name || p->name[0] == '\0')
435 address = g_strdup_printf(p->address);
436 else if (strchr_with_skip_quote(p->name, '"', ','))
437 address = g_strdup_printf
438 ("\"%s\" <%s>", p->name, p->address);
440 address = g_strdup_printf
441 ("%s <%s>", p->name, p->address);
450 * Return the next complete address match from the completion index.
451 * \return Completed address string; this should be freed when done.
453 static gchar *get_next_complete_address(void)
455 if (is_completion_pending()) {
458 res = get_complete_address(g_completion_next);
459 g_completion_next += 1;
460 if (g_completion_next >= g_completion_count)
461 g_completion_next = 0;
469 * Return a count of the completed matches in the completion index.
470 * \return Number of matched entries.
472 static guint get_completion_count(void)
474 if (is_completion_pending())
475 return g_completion_count;
481 * Invalidate address completion index. This function should be called whenever
482 * the address book changes. This forces data to be read into the completion
484 * \return Number of entries in index.
486 gint invalidate_address_completion(void)
489 /* simply the same as start_address_completion() */
490 debug_print("Invalidation request for address completion\n");
494 g_completion_add_items(g_completion, g_completion_list);
495 clear_completion_cache();
498 return g_list_length(g_completion_list);
502 * Finished with completion index. This function should be called after
503 * matching addresses.
504 * \return Reference count.
506 gint end_address_completion(void)
508 clear_completion_cache();
510 if (0 == --g_ref_count)
513 debug_print("end_address_completion ref count %d\n", g_ref_count);
519 * Define the structure of the completion window.
521 typedef struct _CompletionWindow CompletionWindow;
522 struct _CompletionWindow {
533 static CompletionWindow *_compWindow_ = NULL;
536 * Mutex to protect callback from multiple threads.
538 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
541 * Completion queue list.
543 static GList *_displayQueue_ = NULL;
548 static gint _queryID_ = 0;
551 * Completion idle ID.
553 static gint _completionIdleID_ = 0;
556 * address completion entry ui. the ui (completion list was inspired by galeon's
557 * auto completion list). remaining things powered by sylpheed's completion engine.
560 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
562 static void address_completion_mainwindow_set_focus (GtkWindow *window,
565 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
568 static gboolean address_completion_complete_address_in_entry
571 static void address_completion_create_completion_window (GtkEntry *entry);
573 static void completion_window_select_row(GtkCList *clist,
577 CompletionWindow *compWin );
579 static gboolean completion_window_button_press
581 GdkEventButton *event,
582 CompletionWindow *compWin );
584 static gboolean completion_window_key_press
587 CompletionWindow *compWin );
588 static void address_completion_create_completion_window( GtkEntry *entry_ );
591 * Create a completion window object.
592 * \return Initialized completion window.
594 static CompletionWindow *addrcompl_create_window( void ) {
595 CompletionWindow *cw;
597 cw = g_new0( CompletionWindow, 1 );
599 cw->searchTerm = NULL;
608 * Destroy completion window.
609 * \param cw Window to destroy.
611 static void addrcompl_destroy_window( CompletionWindow *cw ) {
612 /* Remove idler function... or application may not terminate */
613 if( _completionIdleID_ != 0 ) {
614 gtk_idle_remove( _completionIdleID_ );
615 _completionIdleID_ = 0;
618 /* Now destroy window */
620 /* Clear references to widgets */
626 gtk_widget_hide( cw->window );
627 gtk_widget_destroy( cw->window );
634 * Free up completion window.
635 * \param cw Window to free.
637 static void addrcompl_free_window( CompletionWindow *cw ) {
639 addrcompl_destroy_window( cw );
641 g_free( cw->searchTerm );
642 cw->searchTerm = NULL;
644 /* Clear references */
653 * Advance selection to previous/next item in list.
654 * \param clist List to process.
655 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
658 static void completion_window_advance_selection(GtkCList *clist, gboolean forward)
662 g_return_if_fail(clist != NULL);
664 if( clist->selection ) {
665 row = GPOINTER_TO_INT(clist->selection->data);
666 row = forward ? ( row + 1 ) : ( row - 1 );
667 gtk_clist_freeze(clist);
668 gtk_clist_select_row(clist, row, 0);
669 gtk_clist_thaw(clist);
674 /* completion_window_accept_selection() - accepts the current selection in the
675 * clist, and destroys the window */
676 static void completion_window_accept_selection(GtkWidget **window,
680 gchar *address = NULL, *text = NULL;
681 gint cursor_pos, row;
683 g_return_if_fail(window != NULL);
684 g_return_if_fail(*window != NULL);
685 g_return_if_fail(clist != NULL);
686 g_return_if_fail(entry != NULL);
687 g_return_if_fail(clist->selection != NULL);
689 /* FIXME: I believe it's acceptable to access the selection member directly */
690 row = GPOINTER_TO_INT(clist->selection->data);
692 /* we just need the cursor position */
693 address = get_address_from_edit(entry, &cursor_pos);
695 gtk_clist_get_text(clist, row, 0, &text);
696 replace_address_in_edit(entry, text, cursor_pos);
698 clear_completion_cache();
699 gtk_widget_destroy(*window);
705 * Resize window to accommodate maximum number of address entries.
706 * \param cw Completion window.
708 static void addrcompl_resize_window( CompletionWindow *cw ) {
710 gint x, y, width, height, depth;
712 /* Get current geometry of window */
713 gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
715 gtk_widget_size_request( cw->clist, &r );
716 gtk_widget_set_usize( cw->window, width, r.height );
717 gtk_widget_show_all( cw->window );
718 gtk_widget_size_request( cw->clist, &r );
720 /* Adjust window height to available screen space */
721 if( ( y + r.height ) > gdk_screen_height() ) {
722 gtk_window_set_policy( GTK_WINDOW( cw->window ), TRUE, FALSE, FALSE );
723 gtk_widget_set_usize( cw->window, width, gdk_screen_height() - y );
728 * Add an address the completion window address list.
729 * \param cw Completion window.
730 * \param address Address to add.
732 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
733 gchar *text[] = { NULL, NULL };
735 /* printf( "\t\tAdding :%s\n", address ); */
737 gtk_clist_append( GTK_CLIST(cw->clist), text );
741 addrcompl_resize_window( cw );
742 gtk_grab_add( cw->window );
744 if( cw->listCount == 1 ) {
745 /* Select first row for now */
746 gtk_clist_select_row( GTK_CLIST(cw->clist), 0, 0);
748 else if( cw->listCount == 2 ) {
749 /* Move off first row */
750 gtk_clist_select_row( GTK_CLIST(cw->clist), 1, 0);
755 * Completion idle function. This function is called by the main (UI) thread
756 * during UI idle time while an address search is in progress. Items from the
757 * display queue are processed and appended to the address list.
759 * \param data Target completion window to receive email addresses.
760 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
762 static gboolean addrcompl_idle( gpointer data ) {
765 CompletionWindow *cw;
767 /* Process all entries in display queue */
768 pthread_mutex_lock( & _completionMutex_ );
769 if( _displayQueue_ ) {
771 node = _displayQueue_;
773 address = node->data;
774 /* printf( "address ::: %s :::\n", address ); */
775 addrcompl_add_entry( cw, address );
777 node = g_list_next( node );
779 g_list_free( _displayQueue_ );
780 _displayQueue_ = NULL;
782 pthread_mutex_unlock( & _completionMutex_ );
788 * Callback entry point. The background thread (if any) appends the address
789 * list to the display queue.
790 * \param queryID Query ID of search request.
791 * \param listEMail List of zero of more email objects that met search
793 * \param target Target object to received data.
795 static gint addrcompl_callback(
796 gint queryID, GList *listEMail, gpointer target )
801 /* printf( "addrcompl_callback::queryID=%d\n", queryID ); */
802 pthread_mutex_lock( & _completionMutex_ );
803 if( queryID == _queryID_ ) {
804 /* Append contents to end of display queue */
807 ItemEMail *email = node->data;
809 address = addritem_format_email( email );
810 /* printf( "\temail/address ::%s::\n", address ); */
811 _displayQueue_ = g_list_append( _displayQueue_, address );
813 node = g_list_next( node );
816 pthread_mutex_unlock( & _completionMutex_ );
817 /* printf( "addrcompl_callback...done\n" ); */
823 * Clear the display queue.
825 static void addrcompl_clear_queue( void ) {
826 /* Clear out display queue */
827 pthread_mutex_lock( & _completionMutex_ );
829 g_list_free( _displayQueue_ );
830 _displayQueue_ = NULL;
832 pthread_mutex_unlock( & _completionMutex_ );
836 * Add a single address entry into the display queue.
837 * \param address Address to append.
839 static void addrcompl_add_queue( gchar *address ) {
840 pthread_mutex_lock( & _completionMutex_ );
841 _displayQueue_ = g_list_append( _displayQueue_, address );
842 pthread_mutex_unlock( & _completionMutex_ );
846 * Load list with entries from local completion index.
847 * \param cw Completion window.
849 static void addrcompl_load_local( CompletionWindow *cw ) {
852 for (count = 0; count < get_completion_count(); count++) {
855 address = get_complete_address( count );
856 /* printf( "\taddress ::%s::\n", address ); */
858 /* Append contents to end of display queue */
859 addrcompl_add_queue( address );
866 static void addrcompl_start_search( void ) {
869 searchTerm = g_strdup( _compWindow_->searchTerm );
871 /* Setup the search */
872 _queryID_ = addrindex_setup_search(
873 _addressIndex_, searchTerm, _compWindow_, addrcompl_callback );
874 g_free( searchTerm );
875 /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
877 /* Load local stuff */
878 addrcompl_load_local( _compWindow_ );
880 /* Sit back and wait until something happens */
882 gtk_idle_add( ( GtkFunction ) addrcompl_idle, _compWindow_ );
883 /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
885 addrindex_start_search( _addressIndex_, _queryID_ );
889 * Apply the current selection in the list to the entry field. Focus is also
890 * moved to the next widget so that Tab key works correctly.
891 * \param clist List to process.
892 * \param entry Address entry field.
894 static void completion_window_apply_selection(GtkCList *clist, GtkEntry *entry)
896 gchar *address = NULL, *text = NULL;
897 gint cursor_pos, row;
900 g_return_if_fail(clist != NULL);
901 g_return_if_fail(entry != NULL);
902 g_return_if_fail(clist->selection != NULL);
904 /* First remove the idler */
905 if( _completionIdleID_ != 0 ) {
906 gtk_idle_remove( _completionIdleID_ );
907 _completionIdleID_ = 0;
910 /* Process selected item */
911 row = GPOINTER_TO_INT(clist->selection->data);
913 address = get_address_from_edit(entry, &cursor_pos);
915 gtk_clist_get_text(clist, row, 0, &text);
916 replace_address_in_edit(entry, text, cursor_pos);
918 /* Move focus to next widget */
919 parent = GTK_WIDGET(entry)->parent;
921 gtk_container_focus( GTK_CONTAINER(parent), GTK_DIR_TAB_FORWARD );
926 * Start address completion. Should be called when creating the main window
927 * containing address completion entries.
928 * \param mainwindow Main window.
930 void address_completion_start(GtkWidget *mainwindow)
932 start_address_completion();
934 /* register focus change hook */
935 gtk_signal_connect(GTK_OBJECT(mainwindow), "set_focus",
936 GTK_SIGNAL_FUNC(address_completion_mainwindow_set_focus),
941 * Need unique data to make unregistering signal handler possible for the auto
944 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
947 * Register specified entry widget for address completion.
948 * \param entry Address entry field.
950 void address_completion_register_entry(GtkEntry *entry)
952 g_return_if_fail(entry != NULL);
953 g_return_if_fail(GTK_IS_ENTRY(entry));
955 /* add hooked property */
956 gtk_object_set_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
958 /* add keypress event */
959 gtk_signal_connect_full(GTK_OBJECT(entry), "key_press_event",
960 GTK_SIGNAL_FUNC(address_completion_entry_key_pressed),
962 COMPLETION_UNIQUE_DATA,
969 * Unregister specified entry widget from address completion operations.
970 * \param entry Address entry field.
972 void address_completion_unregister_entry(GtkEntry *entry)
974 GtkObject *entry_obj;
976 g_return_if_fail(entry != NULL);
977 g_return_if_fail(GTK_IS_ENTRY(entry));
979 entry_obj = gtk_object_get_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
980 g_return_if_fail(entry_obj);
981 g_return_if_fail(entry_obj == GTK_OBJECT(entry));
983 /* has the hooked property? */
984 gtk_object_set_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
986 /* remove the hook */
987 gtk_signal_disconnect_by_func(GTK_OBJECT(entry),
988 GTK_SIGNAL_FUNC(address_completion_entry_key_pressed),
989 COMPLETION_UNIQUE_DATA);
993 * End address completion. Should be called when main window with address
994 * completion entries terminates. NOTE: this function assumes that it is
995 * called upon destruction of the window.
996 * \param mainwindow Main window.
998 void address_completion_end(GtkWidget *mainwindow)
1000 /* if address_completion_end() is really called on closing the window,
1001 * we don't need to unregister the set_focus_cb */
1002 end_address_completion();
1005 /* if focus changes to another entry, then clear completion cache */
1006 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1011 clear_completion_cache();
1015 * Listener that watches for tab or other keystroke in address entry field.
1016 * \param entry Address entry field.
1017 * \param ev Event object.
1018 * \param data User data.
1019 * \return <i>TRUE</i>.
1021 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1025 if (ev->keyval == GDK_Tab) {
1026 addrcompl_clear_queue();
1028 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1029 /* route a void character to the default handler */
1030 /* this is a dirty hack; we're actually changing a key
1031 * reported by the system. */
1032 ev->keyval = GDK_AudibleBell_Enable;
1033 ev->state &= ~GDK_SHIFT_MASK;
1034 gtk_signal_emit_stop_by_name(GTK_OBJECT(entry),
1038 address_completion_create_completion_window(entry);
1040 /* Start remote queries */
1041 addrcompl_start_search();
1046 } else if (ev->keyval == GDK_Shift_L
1047 || ev->keyval == GDK_Shift_R
1048 || ev->keyval == GDK_Control_L
1049 || ev->keyval == GDK_Control_R
1050 || ev->keyval == GDK_Caps_Lock
1051 || ev->keyval == GDK_Shift_Lock
1052 || ev->keyval == GDK_Meta_L
1053 || ev->keyval == GDK_Meta_R
1054 || ev->keyval == GDK_Alt_L
1055 || ev->keyval == GDK_Alt_R) {
1056 /* these buttons should not clear the cache... */
1058 clear_completion_cache();
1063 * Initialize search term for address completion.
1064 * \param entry Address entry field.
1066 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1069 gint ncount, cursor_pos;
1070 gchar *searchTerm, *new = NULL;
1072 g_return_val_if_fail(entry != NULL, FALSE);
1074 if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
1076 /* get an address component from the cursor */
1077 searchTerm = get_address_from_edit( entry, &cursor_pos );
1078 if( ! searchTerm ) return FALSE;
1079 /* printf( "search for :::%s:::\n", searchTerm ); */
1081 /* Clear any existing search */
1082 if( _compWindow_->searchTerm ) {
1083 g_free( _compWindow_->searchTerm );
1085 _compWindow_->searchTerm = g_strdup( searchTerm );
1087 /* Perform search on local completion index */
1088 ncount = complete_address( searchTerm );
1090 new = get_next_complete_address();
1094 /* Make sure that drop-down appears uniform! */
1096 addrcompl_add_queue( g_strdup( searchTerm ) );
1098 g_free( searchTerm );
1104 * Create new address completion window for specified entry.
1105 * \param entry_ Entry widget to associate with window.
1107 static void address_completion_create_completion_window( GtkEntry *entry_ )
1109 gint x, y, height, width, depth;
1110 GtkWidget *scroll, *clist;
1113 GtkWidget *entry = GTK_WIDGET(entry_);
1115 /* Create new window and list */
1116 window = gtk_window_new(GTK_WINDOW_POPUP);
1117 clist = gtk_clist_new(1);
1119 /* Destroy any existing window */
1120 addrcompl_destroy_window( _compWindow_ );
1122 /* Create new object */
1123 _compWindow_->window = window;
1124 _compWindow_->entry = entry;
1125 _compWindow_->clist = clist;
1126 _compWindow_->listCount = 0;
1128 scroll = gtk_scrolled_window_new(NULL, NULL);
1129 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1130 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1131 gtk_container_add(GTK_CONTAINER(window), scroll);
1132 gtk_container_add(GTK_CONTAINER(scroll), clist);
1133 gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_SINGLE);
1135 /* Use entry widget to create initial window */
1136 gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
1137 gdk_window_get_deskrelative_origin (entry->window, &x, &y);
1139 gtk_widget_set_uposition(window, x, y);
1141 /* Resize window to fit initial (empty) address list */
1142 gtk_widget_size_request( clist, &r );
1143 gtk_widget_set_usize( window, width, r.height );
1144 gtk_widget_show_all( window );
1145 gtk_widget_size_request( clist, &r );
1147 /* Setup handlers */
1148 gtk_signal_connect(GTK_OBJECT(clist), "select_row",
1149 GTK_SIGNAL_FUNC(completion_window_select_row),
1151 gtk_signal_connect(GTK_OBJECT(window),
1152 "button-press-event",
1153 GTK_SIGNAL_FUNC(completion_window_button_press),
1155 gtk_signal_connect(GTK_OBJECT(window),
1157 GTK_SIGNAL_FUNC(completion_window_key_press),
1159 gdk_pointer_grab(window->window, TRUE,
1160 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1161 GDK_BUTTON_RELEASE_MASK,
1162 NULL, NULL, GDK_CURRENT_TIME);
1163 gtk_grab_add( window );
1165 /* this gets rid of the irritating focus rectangle that doesn't
1166 * follow the selection */
1167 GTK_WIDGET_UNSET_FLAGS(clist, GTK_CAN_FOCUS);
1171 * Respond to select row event in clist object. selection sends completed
1172 * address to entry. Note: event is NULL if selected by anything else than a
1174 * \param widget Window object.
1175 * \param event Event.
1176 * \param compWind Reference to completion window.
1178 static void completion_window_select_row(GtkCList *clist, gint row, gint col,
1180 CompletionWindow *compWin )
1184 g_return_if_fail(compWin != NULL);
1186 entry = GTK_ENTRY(compWin->entry);
1187 g_return_if_fail(entry != NULL);
1189 /* Don't update unless user actually selects ! */
1190 if (!event || event->type != GDK_BUTTON_RELEASE)
1193 /* User selected address by releasing the mouse in drop-down list*/
1194 completion_window_apply_selection( clist, entry );
1196 clear_completion_cache();
1197 addrcompl_destroy_window( _compWindow_ );
1201 * Respond to button press in completion window. Check if mouse click is
1202 * anywhere outside the completion window. In that case the completion
1203 * window is destroyed, and the original searchTerm is restored.
1205 * \param widget Window object.
1206 * \param event Event.
1207 * \param compWin Reference to completion window.
1209 static gboolean completion_window_button_press(GtkWidget *widget,
1210 GdkEventButton *event,
1211 CompletionWindow *compWin )
1213 GtkWidget *event_widget, *entry;
1216 gboolean restore = TRUE;
1218 g_return_val_if_fail(compWin != NULL, FALSE);
1220 entry = compWin->entry;
1221 g_return_val_if_fail(entry != NULL, FALSE);
1223 /* Test where mouse was clicked */
1224 event_widget = gtk_get_event_widget((GdkEvent *)event);
1225 if (event_widget != widget) {
1226 while (event_widget) {
1227 if (event_widget == widget)
1229 else if (event_widget == entry) {
1233 event_widget = event_widget->parent;
1238 /* Clicked outside of completion window - restore */
1239 searchTerm = _compWindow_->searchTerm;
1240 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1241 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1244 clear_completion_cache();
1245 addrcompl_destroy_window( _compWindow_ );
1251 * Respond to key press in completion window.
1252 * \param widget Window object.
1253 * \param event Event.
1254 * \param compWind Reference to completion window.
1256 static gboolean completion_window_key_press(GtkWidget *widget,
1258 CompletionWindow *compWin )
1260 GdkEventKey tmp_event;
1267 g_return_val_if_fail(compWin != NULL, FALSE);
1269 entry = compWin->entry;
1270 clist = compWin->clist;
1271 g_return_val_if_fail(entry != NULL, FALSE);
1273 /* allow keyboard navigation in the alternatives clist */
1274 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
1275 event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
1276 completion_window_advance_selection
1278 event->keyval == GDK_Down ||
1279 event->keyval == GDK_Page_Down ? TRUE : FALSE);
1284 /* also make tab / shift tab go to next previous completion entry. we're
1285 * changing the key value */
1286 if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
1287 event->keyval = (event->state & GDK_SHIFT_MASK)
1288 ? GDK_Up : GDK_Down;
1289 /* need to reset shift state if going up */
1290 if (event->state & GDK_SHIFT_MASK)
1291 event->state &= ~GDK_SHIFT_MASK;
1292 completion_window_advance_selection(GTK_CLIST(clist),
1293 event->keyval == GDK_Down ? TRUE : FALSE);
1298 /* make tab move to next field */
1299 if( event->keyval == GDK_Tab ) {
1300 /* Reference to parent */
1301 parent = GTK_WIDGET(entry)->parent;
1303 /* Discard the window */
1304 clear_completion_cache();
1305 addrcompl_destroy_window( _compWindow_ );
1307 /* Move focus to next widget */
1309 gtk_container_focus( GTK_CONTAINER(parent), GTK_DIR_TAB_FORWARD );
1314 /* make backtab move to previous field */
1315 if( event->keyval == GDK_ISO_Left_Tab ) {
1316 /* Reference to parent */
1317 parent = GTK_WIDGET(entry)->parent;
1319 /* Discard the window */
1320 clear_completion_cache();
1321 addrcompl_destroy_window( _compWindow_ );
1323 /* Move focus to previous widget */
1325 gtk_container_focus( GTK_CONTAINER(parent), GTK_DIR_TAB_BACKWARD );
1330 /* look for presses that accept the selection */
1331 if (event->keyval == GDK_Return || event->keyval == GDK_space) {
1332 /* User selected address with a key press */
1334 /* Display selected address in entry field */
1335 completion_window_apply_selection(
1336 GTK_CLIST(clist), GTK_ENTRY(entry) );
1338 /* Discard the window */
1339 clear_completion_cache();
1340 addrcompl_destroy_window( _compWindow_ );
1344 /* key state keys should never be handled */
1345 if (event->keyval == GDK_Shift_L
1346 || event->keyval == GDK_Shift_R
1347 || event->keyval == GDK_Control_L
1348 || event->keyval == GDK_Control_R
1349 || event->keyval == GDK_Caps_Lock
1350 || event->keyval == GDK_Shift_Lock
1351 || event->keyval == GDK_Meta_L
1352 || event->keyval == GDK_Meta_R
1353 || event->keyval == GDK_Alt_L
1354 || event->keyval == GDK_Alt_R) {
1358 /* some other key, let's restore the searchTerm (orignal text) */
1359 searchTerm = _compWindow_->searchTerm;
1360 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1361 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1363 /* make sure anything we typed comes in the edit box */
1364 tmp_event.type = event->type;
1365 tmp_event.window = entry->window;
1366 tmp_event.send_event = TRUE;
1367 tmp_event.time = event->time;
1368 tmp_event.state = event->state;
1369 tmp_event.keyval = event->keyval;
1370 tmp_event.length = event->length;
1371 tmp_event.string = event->string;
1372 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1374 /* and close the completion window */
1375 clear_completion_cache();
1376 addrcompl_destroy_window( _compWindow_ );
1382 * ============================================================================
1383 * Publically accessible functions.
1384 * ============================================================================
1388 * Setup completion object.
1389 * \param addrIndex Address index object.
1391 void addrcompl_initialize( AddressIndex *addrIndex ) {
1392 g_return_if_fail( addrIndex != NULL );
1393 _addressIndex_ = addrIndex;
1395 /* printf( "addrcompl_initialize...\n" ); */
1396 if( ! _compWindow_ ) {
1397 _compWindow_ = addrcompl_create_window();
1400 _completionIdleID_ = 0;
1401 /* printf( "addrcompl_initialize...done\n" ); */
1405 * Teardown completion object.
1407 void addrcompl_teardown( void ) {
1408 /* printf( "addrcompl_teardown...\n" ); */
1409 addrcompl_free_window( _compWindow_ );
1410 _compWindow_ = NULL;
1411 if( _displayQueue_ ) {
1412 g_list_free( _displayQueue_ );
1414 _displayQueue_ = NULL;
1415 _completionIdleID_ = 0;
1416 _addressIndex_ = NULL;
1417 /* printf( "addrcompl_teardown...done\n" ); */