root / src / addr_compl.c @ 532
History | View | Annotate | Download (26.9 kB)
| 1 | 1 | hiro | /*
|
|---|---|---|---|
| 2 | 1 | hiro | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | 1 | hiro | * |
| 4 | 92 | hiro | * Copyright (C) 2000-2005 by Alfons Hoogervorst & The Sylpheed Claws Team. |
| 5 | 1 | hiro | * |
| 6 | 1 | hiro | * This program is free software; you can redistribute it and/or modify |
| 7 | 1 | hiro | * it under the terms of the GNU General Public License as published by |
| 8 | 1 | hiro | * the Free Software Foundation; either version 2 of the License, or |
| 9 | 1 | hiro | * (at your option) any later version. |
| 10 | 1 | hiro | * |
| 11 | 1 | hiro | * This program is distributed in the hope that it will be useful, |
| 12 | 1 | hiro | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | 1 | hiro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | 1 | hiro | * GNU General Public License for more details. |
| 15 | 1 | hiro | * |
| 16 | 1 | hiro | * You should have received a copy of the GNU General Public License |
| 17 | 1 | hiro | * along with this program; if not, write to the Free Software |
| 18 | 1 | hiro | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | 1 | hiro | */ |
| 20 | 1 | hiro | |
| 21 | 1 | hiro | #ifdef HAVE_CONFIG_H
|
| 22 | 1 | hiro | # include "config.h" |
| 23 | 1 | hiro | #endif
|
| 24 | 1 | hiro | #include "defs.h" |
| 25 | 1 | hiro | |
| 26 | 1 | hiro | #include <glib.h> |
| 27 | 92 | hiro | #include <glib/gi18n.h> |
| 28 | 1 | hiro | #include <gdk/gdkkeysyms.h> |
| 29 | 1 | hiro | #include <gtk/gtkmain.h> |
| 30 | 1 | hiro | #include <gtk/gtkwindow.h> |
| 31 | 1 | hiro | #include <gtk/gtkentry.h> |
| 32 | 1 | hiro | #include <gtk/gtkeditable.h> |
| 33 | 1 | hiro | #include <gtk/gtkclist.h> |
| 34 | 1 | hiro | #include <gtk/gtkscrolledwindow.h> |
| 35 | 1 | hiro | |
| 36 | 1 | hiro | #include <string.h> |
| 37 | 1 | hiro | #include <ctype.h> |
| 38 | 1 | hiro | #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
|
| 39 | 1 | hiro | # include <wchar.h> |
| 40 | 1 | hiro | # include <wctype.h> |
| 41 | 1 | hiro | #endif
|
| 42 | 1 | hiro | |
| 43 | 1 | hiro | #include "xml.h" |
| 44 | 1 | hiro | #include "addr_compl.h" |
| 45 | 1 | hiro | #include "utils.h" |
| 46 | 1 | hiro | #include "addressbook.h" |
| 47 | 1 | hiro | #include "main.h" |
| 48 | 1 | hiro | |
| 49 | 1 | hiro | /* How it works:
|
| 50 | 1 | hiro | * |
| 51 | 1 | hiro | * The address book is read into memory. We set up an address list |
| 52 | 1 | hiro | * containing all address book entries. Next we make the completion |
| 53 | 1 | hiro | * list, which contains all the completable strings, and store a |
| 54 | 1 | hiro | * reference to the address entry it belongs to. |
| 55 | 1 | hiro | * After calling the g_completion_complete(), we get a reference |
| 56 | 1 | hiro | * to a valid email address. |
| 57 | 1 | hiro | * |
| 58 | 1 | hiro | * Completion is very simplified. We never complete on another prefix, |
| 59 | 1 | hiro | * i.e. we neglect the next smallest possible prefix for the current |
| 60 | 1 | hiro | * completion cache. This is simply done so we might break up the |
| 61 | 1 | hiro | * addresses a little more (e.g. break up alfons@proteus.demon.nl into |
| 62 | 1 | hiro | * something like alfons, proteus, demon, nl; and then completing on |
| 63 | 1 | hiro | * any of those words). |
| 64 | 1 | hiro | */ |
| 65 | 1 | hiro | |
| 66 | 1 | hiro | /* address_entry - structure which refers to the original address entry in the
|
| 67 | 1 | hiro | * address book |
| 68 | 1 | hiro | */ |
| 69 | 1 | hiro | typedef struct |
| 70 | 1 | hiro | {
|
| 71 | 1 | hiro | gchar *name; |
| 72 | 1 | hiro | gchar *address; |
| 73 | 1 | hiro | } address_entry; |
| 74 | 1 | hiro | |
| 75 | 1 | hiro | /* completion_entry - structure used to complete addresses, with a reference
|
| 76 | 1 | hiro | * the the real address information. |
| 77 | 1 | hiro | */ |
| 78 | 1 | hiro | typedef struct |
| 79 | 1 | hiro | {
|
| 80 | 1 | hiro | gchar *string; /* string to complete */
|
| 81 | 1 | hiro | address_entry *ref; /* address the string belongs to */
|
| 82 | 1 | hiro | } completion_entry; |
| 83 | 1 | hiro | |
| 84 | 1 | hiro | /*******************************************************************************/
|
| 85 | 1 | hiro | |
| 86 | 1 | hiro | static gint g_ref_count; /* list ref count */ |
| 87 | 1 | hiro | static GList *g_completion_list; /* list of strings to be checked */ |
| 88 | 1 | hiro | static GList *g_address_list; /* address storage */ |
| 89 | 1 | hiro | static GCompletion *g_completion; /* completion object */ |
| 90 | 1 | hiro | |
| 91 | 1 | hiro | /* To allow for continuing completion we have to keep track of the state
|
| 92 | 1 | hiro | * using the following variables. No need to create a context object. */ |
| 93 | 1 | hiro | |
| 94 | 1 | hiro | static gint g_completion_count; /* nr of addresses incl. the prefix */ |
| 95 | 1 | hiro | static gint g_completion_next; /* next prev address */ |
| 96 | 1 | hiro | static GSList *g_completion_addresses; /* unique addresses found in the |
| 97 | 1 | hiro | completion cache. */ |
| 98 | 1 | hiro | static gchar *g_completion_prefix; /* last prefix. (this is cached here |
| 99 | 1 | hiro | * because the prefix passed to g_completion |
| 100 | 1 | hiro | * is g_strdown()'ed */ |
| 101 | 1 | hiro | |
| 102 | 1 | hiro | /*******************************************************************************/
|
| 103 | 1 | hiro | |
| 104 | 1 | hiro | /* completion_func() - used by GTK to find the string data to be used for
|
| 105 | 1 | hiro | * completion |
| 106 | 1 | hiro | */ |
| 107 | 1 | hiro | static gchar *completion_func(gpointer data)
|
| 108 | 1 | hiro | {
|
| 109 | 1 | hiro | g_return_val_if_fail(data != NULL, NULL); |
| 110 | 1 | hiro | |
| 111 | 1 | hiro | return ((completion_entry *)data)->string;
|
| 112 | 1 | hiro | } |
| 113 | 1 | hiro | |
| 114 | 1 | hiro | static void init_all(void) |
| 115 | 1 | hiro | {
|
| 116 | 1 | hiro | g_completion = g_completion_new(completion_func); |
| 117 | 1 | hiro | g_return_if_fail(g_completion != NULL);
|
| 118 | 1 | hiro | } |
| 119 | 1 | hiro | |
| 120 | 1 | hiro | static void free_all(void) |
| 121 | 1 | hiro | {
|
| 122 | 1 | hiro | GList *walk; |
| 123 | 1 | hiro | |
| 124 | 1 | hiro | walk = g_list_first(g_completion_list); |
| 125 | 1 | hiro | for (; walk != NULL; walk = g_list_next(walk)) { |
| 126 | 1 | hiro | completion_entry *ce = (completion_entry *) walk->data; |
| 127 | 1 | hiro | g_free(ce->string); |
| 128 | 1 | hiro | g_free(walk->data); |
| 129 | 1 | hiro | } |
| 130 | 1 | hiro | g_list_free(g_completion_list); |
| 131 | 1 | hiro | g_completion_list = NULL;
|
| 132 | 1 | hiro | |
| 133 | 1 | hiro | walk = g_address_list; |
| 134 | 1 | hiro | for (; walk != NULL; walk = g_list_next(walk)) { |
| 135 | 1 | hiro | address_entry *ae = (address_entry *) walk->data; |
| 136 | 1 | hiro | g_free(ae->name); |
| 137 | 1 | hiro | g_free(ae->address); |
| 138 | 1 | hiro | g_free(walk->data); |
| 139 | 1 | hiro | } |
| 140 | 1 | hiro | g_list_free(g_address_list); |
| 141 | 1 | hiro | g_address_list = NULL;
|
| 142 | 1 | hiro | |
| 143 | 1 | hiro | g_completion_free(g_completion); |
| 144 | 1 | hiro | g_completion = NULL;
|
| 145 | 1 | hiro | } |
| 146 | 1 | hiro | |
| 147 | 1 | hiro | /* add_address() - adds address to the completion list. this function looks
|
| 148 | 1 | hiro | * complicated, but it's only allocation checks. |
| 149 | 1 | hiro | */ |
| 150 | 1 | hiro | static gint add_address(const gchar *name, const gchar *address) |
| 151 | 1 | hiro | {
|
| 152 | 1 | hiro | address_entry *ae; |
| 153 | 1 | hiro | completion_entry *ce1; |
| 154 | 1 | hiro | completion_entry *ce2; |
| 155 | 1 | hiro | |
| 156 | 1 | hiro | if (!name || !address) return -1; |
| 157 | 1 | hiro | |
| 158 | 1 | hiro | ae = g_new0(address_entry, 1);
|
| 159 | 1 | hiro | ce1 = g_new0(completion_entry, 1),
|
| 160 | 1 | hiro | ce2 = g_new0(completion_entry, 1);
|
| 161 | 1 | hiro | |
| 162 | 1 | hiro | g_return_val_if_fail(ae != NULL, -1); |
| 163 | 1 | hiro | g_return_val_if_fail(ce1 != NULL && ce2 != NULL, -1); |
| 164 | 1 | hiro | |
| 165 | 1 | hiro | ae->name = g_strdup(name); |
| 166 | 1 | hiro | ae->address = g_strdup(address); |
| 167 | 1 | hiro | |
| 168 | 1 | hiro | /* GCompletion list is case sensitive */
|
| 169 | 1 | hiro | ce1->string = g_utf8_strdown(name, -1);
|
| 170 | 1 | hiro | ce2->string = g_utf8_strdown(address, -1);
|
| 171 | 1 | hiro | |
| 172 | 1 | hiro | ce1->ref = ce2->ref = ae; |
| 173 | 1 | hiro | |
| 174 | 1 | hiro | g_completion_list = g_list_append(g_completion_list, ce1); |
| 175 | 1 | hiro | g_completion_list = g_list_append(g_completion_list, ce2); |
| 176 | 1 | hiro | g_address_list = g_list_append(g_address_list, ae); |
| 177 | 1 | hiro | |
| 178 | 1 | hiro | return 0; |
| 179 | 1 | hiro | } |
| 180 | 1 | hiro | |
| 181 | 1 | hiro | /* read_address_book()
|
| 182 | 1 | hiro | */ |
| 183 | 1 | hiro | static void read_address_book(void) { |
| 184 | 1 | hiro | addressbook_load_completion( add_address ); |
| 185 | 1 | hiro | } |
| 186 | 1 | hiro | |
| 187 | 1 | hiro | /* start_address_completion() - returns the number of addresses
|
| 188 | 1 | hiro | * that should be matched for completion. |
| 189 | 1 | hiro | */ |
| 190 | 1 | hiro | gint start_address_completion(void)
|
| 191 | 1 | hiro | {
|
| 192 | 1 | hiro | clear_completion_cache(); |
| 193 | 1 | hiro | if (!g_ref_count) {
|
| 194 | 1 | hiro | init_all(); |
| 195 | 1 | hiro | /* open the address book */
|
| 196 | 1 | hiro | read_address_book(); |
| 197 | 1 | hiro | /* merge the completion entry list into g_completion */
|
| 198 | 1 | hiro | if (g_completion_list)
|
| 199 | 1 | hiro | g_completion_add_items(g_completion, g_completion_list); |
| 200 | 1 | hiro | } |
| 201 | 1 | hiro | g_ref_count++; |
| 202 | 1 | hiro | debug_print("start_address_completion ref count %d\n", g_ref_count);
|
| 203 | 1 | hiro | |
| 204 | 1 | hiro | return g_list_length(g_completion_list);
|
| 205 | 1 | hiro | } |
| 206 | 1 | hiro | |
| 207 | 1 | hiro | /* get_address_from_edit() - returns a possible address (or a part)
|
| 208 | 1 | hiro | * from an entry box. To make life easier, we only look at the last valid address |
| 209 | 1 | hiro | * component; address completion only works at the last string component in |
| 210 | 1 | hiro | * the entry box. |
| 211 | 1 | hiro | */ |
| 212 | 1 | hiro | gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos) |
| 213 | 1 | hiro | {
|
| 214 | 1 | hiro | const gchar *edit_text, *p;
|
| 215 | 1 | hiro | gint cur_pos; |
| 216 | 1 | hiro | gboolean in_quote = FALSE; |
| 217 | 1 | hiro | gboolean in_bracket = FALSE; |
| 218 | 1 | hiro | gchar *str; |
| 219 | 1 | hiro | |
| 220 | 1 | hiro | edit_text = gtk_entry_get_text(entry); |
| 221 | 1 | hiro | if (edit_text == NULL) return NULL; |
| 222 | 1 | hiro | |
| 223 | 1 | hiro | cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry)); |
| 224 | 1 | hiro | |
| 225 | 1 | hiro | /* scan for a separator. doesn't matter if walk points at null byte. */
|
| 226 | 1 | hiro | for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
|
| 227 | 1 | hiro | p > edit_text; |
| 228 | 1 | hiro | p = g_utf8_prev_char(p)) {
|
| 229 | 1 | hiro | if (*p == '"') |
| 230 | 1 | hiro | in_quote ^= TRUE; |
| 231 | 1 | hiro | else if (!in_quote) { |
| 232 | 1 | hiro | if (!in_bracket && *p == ',') |
| 233 | 1 | hiro | break;
|
| 234 | 1 | hiro | else if (*p == '>') |
| 235 | 1 | hiro | in_bracket = TRUE; |
| 236 | 1 | hiro | else if (*p == '<') |
| 237 | 1 | hiro | in_bracket = FALSE; |
| 238 | 1 | hiro | } |
| 239 | 1 | hiro | } |
| 240 | 1 | hiro | |
| 241 | 1 | hiro | /* have something valid */
|
| 242 | 1 | hiro | if (g_utf8_strlen(p, -1) == 0) |
| 243 | 1 | hiro | return NULL; |
| 244 | 1 | hiro | |
| 245 | 1 | hiro | #define IS_VALID_CHAR(x) \
|
| 246 | 457 | hiro | (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || ((guchar)(x) > 0x7f)) |
| 247 | 1 | hiro | |
| 248 | 1 | hiro | /* now scan back until we hit a valid character */
|
| 249 | 1 | hiro | for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
|
| 250 | 1 | hiro | ; |
| 251 | 1 | hiro | |
| 252 | 1 | hiro | #undef IS_VALID_CHAR
|
| 253 | 1 | hiro | |
| 254 | 1 | hiro | if (g_utf8_strlen(p, -1) == 0) |
| 255 | 1 | hiro | return NULL; |
| 256 | 1 | hiro | |
| 257 | 1 | hiro | if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
|
| 258 | 1 | hiro | |
| 259 | 1 | hiro | str = g_strdup(p); |
| 260 | 1 | hiro | |
| 261 | 1 | hiro | return str;
|
| 262 | 1 | hiro | } |
| 263 | 1 | hiro | |
| 264 | 1 | hiro | /* replace_address_in_edit() - replaces an incompleted address with a completed one.
|
| 265 | 1 | hiro | */ |
| 266 | 1 | hiro | void replace_address_in_edit(GtkEntry *entry, const gchar *newtext, |
| 267 | 1 | hiro | gint start_pos) |
| 268 | 1 | hiro | {
|
| 269 | 1 | hiro | if (!newtext) return; |
| 270 | 1 | hiro | |
| 271 | 1 | hiro | gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
|
| 272 | 1 | hiro | gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext), |
| 273 | 1 | hiro | &start_pos); |
| 274 | 1 | hiro | gtk_editable_set_position(GTK_EDITABLE(entry), -1);
|
| 275 | 1 | hiro | } |
| 276 | 1 | hiro | |
| 277 | 1 | hiro | /* complete_address() - tries to complete an addres, and returns the
|
| 278 | 1 | hiro | * number of addresses found. use get_complete_address() to get one. |
| 279 | 1 | hiro | * returns zero if no match was found, otherwise the number of addresses, |
| 280 | 1 | hiro | * with the original prefix at index 0. |
| 281 | 1 | hiro | */ |
| 282 | 1 | hiro | guint complete_address(const gchar *str)
|
| 283 | 1 | hiro | {
|
| 284 | 1 | hiro | GList *result; |
| 285 | 1 | hiro | gchar *d; |
| 286 | 1 | hiro | guint count, cpl; |
| 287 | 1 | hiro | completion_entry *ce; |
| 288 | 1 | hiro | |
| 289 | 1 | hiro | g_return_val_if_fail(str != NULL, 0); |
| 290 | 1 | hiro | |
| 291 | 1 | hiro | clear_completion_cache(); |
| 292 | 1 | hiro | g_completion_prefix = g_strdup(str); |
| 293 | 1 | hiro | |
| 294 | 1 | hiro | /* g_completion is case sensitive */
|
| 295 | 1 | hiro | d = g_utf8_strdown(str, -1);
|
| 296 | 1 | hiro | result = g_completion_complete(g_completion, d, NULL);
|
| 297 | 1 | hiro | |
| 298 | 1 | hiro | count = g_list_length(result); |
| 299 | 1 | hiro | if (count) {
|
| 300 | 1 | hiro | /* create list with unique addresses */
|
| 301 | 1 | hiro | for (cpl = 0, result = g_list_first(result); |
| 302 | 1 | hiro | result != NULL;
|
| 303 | 1 | hiro | result = g_list_next(result)) {
|
| 304 | 1 | hiro | ce = (completion_entry *)(result->data); |
| 305 | 1 | hiro | if (NULL == g_slist_find(g_completion_addresses, |
| 306 | 1 | hiro | ce->ref)) {
|
| 307 | 1 | hiro | cpl++; |
| 308 | 1 | hiro | g_completion_addresses = |
| 309 | 1 | hiro | g_slist_append(g_completion_addresses, |
| 310 | 1 | hiro | ce->ref); |
| 311 | 1 | hiro | } |
| 312 | 1 | hiro | } |
| 313 | 1 | hiro | count = cpl + 1; /* index 0 is the original prefix */ |
| 314 | 1 | hiro | g_completion_next = 1; /* we start at the first completed one */ |
| 315 | 1 | hiro | } else {
|
| 316 | 1 | hiro | g_free(g_completion_prefix); |
| 317 | 1 | hiro | g_completion_prefix = NULL;
|
| 318 | 1 | hiro | } |
| 319 | 1 | hiro | |
| 320 | 1 | hiro | g_completion_count = count; |
| 321 | 1 | hiro | |
| 322 | 1 | hiro | g_free(d); |
| 323 | 1 | hiro | |
| 324 | 1 | hiro | return count;
|
| 325 | 1 | hiro | } |
| 326 | 1 | hiro | |
| 327 | 1 | hiro | /* get_complete_address() - returns a complete address. the returned
|
| 328 | 1 | hiro | * string should be freed |
| 329 | 1 | hiro | */ |
| 330 | 1 | hiro | gchar *get_complete_address(gint index) |
| 331 | 1 | hiro | {
|
| 332 | 1 | hiro | const address_entry *p;
|
| 333 | 1 | hiro | gchar *address = NULL;
|
| 334 | 1 | hiro | |
| 335 | 1 | hiro | if (index < g_completion_count) {
|
| 336 | 1 | hiro | if (index == 0) |
| 337 | 1 | hiro | address = g_strdup(g_completion_prefix); |
| 338 | 1 | hiro | else {
|
| 339 | 1 | hiro | /* get something from the unique addresses */
|
| 340 | 1 | hiro | p = (address_entry *)g_slist_nth_data |
| 341 | 1 | hiro | (g_completion_addresses, index - 1);
|
| 342 | 1 | hiro | if (p != NULL) { |
| 343 | 1 | hiro | if (!p->name || p->name[0] == '\0') |
| 344 | 1 | hiro | address = g_strdup_printf(p->address); |
| 345 | 1 | hiro | else if (p->name[0] != '"' && |
| 346 | 1 | hiro | strpbrk(p->name, ",.[]<>") != NULL) |
| 347 | 1 | hiro | address = g_strdup_printf |
| 348 | 1 | hiro | ("\"%s\" <%s>", p->name, p->address);
|
| 349 | 1 | hiro | else
|
| 350 | 1 | hiro | address = g_strdup_printf |
| 351 | 1 | hiro | ("%s <%s>", p->name, p->address);
|
| 352 | 1 | hiro | } |
| 353 | 1 | hiro | } |
| 354 | 1 | hiro | } |
| 355 | 1 | hiro | |
| 356 | 1 | hiro | return address;
|
| 357 | 1 | hiro | } |
| 358 | 1 | hiro | |
| 359 | 1 | hiro | gchar *get_next_complete_address(void)
|
| 360 | 1 | hiro | {
|
| 361 | 1 | hiro | if (is_completion_pending()) {
|
| 362 | 1 | hiro | gchar *res; |
| 363 | 1 | hiro | |
| 364 | 1 | hiro | res = get_complete_address(g_completion_next); |
| 365 | 1 | hiro | g_completion_next += 1;
|
| 366 | 1 | hiro | if (g_completion_next >= g_completion_count)
|
| 367 | 1 | hiro | g_completion_next = 0;
|
| 368 | 1 | hiro | |
| 369 | 1 | hiro | return res;
|
| 370 | 1 | hiro | } else
|
| 371 | 1 | hiro | return NULL; |
| 372 | 1 | hiro | } |
| 373 | 1 | hiro | |
| 374 | 1 | hiro | gchar *get_prev_complete_address(void)
|
| 375 | 1 | hiro | {
|
| 376 | 1 | hiro | if (is_completion_pending()) {
|
| 377 | 1 | hiro | int n = g_completion_next - 2; |
| 378 | 1 | hiro | |
| 379 | 1 | hiro | /* real previous */
|
| 380 | 1 | hiro | n = (n + (g_completion_count * 5)) % g_completion_count;
|
| 381 | 1 | hiro | |
| 382 | 1 | hiro | /* real next */
|
| 383 | 1 | hiro | g_completion_next = n + 1;
|
| 384 | 1 | hiro | if (g_completion_next >= g_completion_count)
|
| 385 | 1 | hiro | g_completion_next = 0;
|
| 386 | 1 | hiro | return get_complete_address(n);
|
| 387 | 1 | hiro | } else
|
| 388 | 1 | hiro | return NULL; |
| 389 | 1 | hiro | } |
| 390 | 1 | hiro | |
| 391 | 1 | hiro | guint get_completion_count(void)
|
| 392 | 1 | hiro | {
|
| 393 | 1 | hiro | if (is_completion_pending())
|
| 394 | 1 | hiro | return g_completion_count;
|
| 395 | 1 | hiro | else
|
| 396 | 1 | hiro | return 0; |
| 397 | 1 | hiro | } |
| 398 | 1 | hiro | |
| 399 | 1 | hiro | /* should clear up anything after complete_address() */
|
| 400 | 1 | hiro | void clear_completion_cache(void) |
| 401 | 1 | hiro | {
|
| 402 | 1 | hiro | if (is_completion_pending()) {
|
| 403 | 1 | hiro | if (g_completion_prefix)
|
| 404 | 1 | hiro | g_free(g_completion_prefix); |
| 405 | 1 | hiro | |
| 406 | 1 | hiro | if (g_completion_addresses) {
|
| 407 | 1 | hiro | g_slist_free(g_completion_addresses); |
| 408 | 1 | hiro | g_completion_addresses = NULL;
|
| 409 | 1 | hiro | } |
| 410 | 1 | hiro | |
| 411 | 1 | hiro | g_completion_count = g_completion_next = 0;
|
| 412 | 1 | hiro | } |
| 413 | 1 | hiro | } |
| 414 | 1 | hiro | |
| 415 | 1 | hiro | gboolean is_completion_pending(void)
|
| 416 | 1 | hiro | {
|
| 417 | 1 | hiro | /* check if completion pending, i.e. we might satisfy a request for the next
|
| 418 | 1 | hiro | * or previous address */ |
| 419 | 1 | hiro | return g_completion_count;
|
| 420 | 1 | hiro | } |
| 421 | 1 | hiro | |
| 422 | 1 | hiro | /* invalidate_address_completion() - should be called if address book
|
| 423 | 1 | hiro | * changed; |
| 424 | 1 | hiro | */ |
| 425 | 1 | hiro | gint invalidate_address_completion(void)
|
| 426 | 1 | hiro | {
|
| 427 | 1 | hiro | if (g_ref_count) {
|
| 428 | 1 | hiro | /* simply the same as start_address_completion() */
|
| 429 | 1 | hiro | debug_print("Invalidation request for address completion\n");
|
| 430 | 1 | hiro | free_all(); |
| 431 | 1 | hiro | init_all(); |
| 432 | 1 | hiro | read_address_book(); |
| 433 | 1 | hiro | if (g_completion_list)
|
| 434 | 1 | hiro | g_completion_add_items(g_completion, g_completion_list); |
| 435 | 1 | hiro | clear_completion_cache(); |
| 436 | 1 | hiro | } |
| 437 | 1 | hiro | |
| 438 | 1 | hiro | return g_list_length(g_completion_list);
|
| 439 | 1 | hiro | } |
| 440 | 1 | hiro | |
| 441 | 1 | hiro | gint end_address_completion(void)
|
| 442 | 1 | hiro | {
|
| 443 | 1 | hiro | clear_completion_cache(); |
| 444 | 1 | hiro | |
| 445 | 1 | hiro | if (0 == --g_ref_count) |
| 446 | 1 | hiro | free_all(); |
| 447 | 1 | hiro | |
| 448 | 1 | hiro | debug_print("end_address_completion ref count %d\n", g_ref_count);
|
| 449 | 1 | hiro | |
| 450 | 1 | hiro | return g_ref_count;
|
| 451 | 1 | hiro | } |
| 452 | 1 | hiro | |
| 453 | 1 | hiro | |
| 454 | 1 | hiro | /* address completion entry ui. the ui (completion list was inspired by galeon's
|
| 455 | 1 | hiro | * auto completion list). remaining things powered by sylpheed's completion engine. |
| 456 | 1 | hiro | */ |
| 457 | 1 | hiro | |
| 458 | 1 | hiro | #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */ |
| 459 | 1 | hiro | #define WINDOW_DATA_COMPL_ENTRY "compl_entry" /* used to store entry for compl. window */ |
| 460 | 1 | hiro | #define WINDOW_DATA_COMPL_CLIST "compl_clist" /* used to store clist for compl. window */ |
| 461 | 1 | hiro | |
| 462 | 1 | hiro | static void address_completion_mainwindow_set_focus (GtkWindow *window, |
| 463 | 1 | hiro | GtkWidget *widget, |
| 464 | 1 | hiro | gpointer data); |
| 465 | 1 | hiro | static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
|
| 466 | 1 | hiro | GdkEventKey *ev, |
| 467 | 1 | hiro | gpointer data); |
| 468 | 1 | hiro | static gboolean address_completion_complete_address_in_entry
|
| 469 | 1 | hiro | (GtkEntry *entry, |
| 470 | 1 | hiro | gboolean next); |
| 471 | 1 | hiro | static void address_completion_create_completion_window (GtkEntry *entry); |
| 472 | 1 | hiro | |
| 473 | 1 | hiro | static void completion_window_select_row(GtkCList *clist, |
| 474 | 1 | hiro | gint row, |
| 475 | 1 | hiro | gint col, |
| 476 | 1 | hiro | GdkEvent *event, |
| 477 | 1 | hiro | GtkWidget **completion_window); |
| 478 | 1 | hiro | static gboolean completion_window_button_press
|
| 479 | 1 | hiro | (GtkWidget *widget, |
| 480 | 1 | hiro | GdkEventButton *event, |
| 481 | 1 | hiro | GtkWidget **completion_window); |
| 482 | 1 | hiro | static gboolean completion_window_key_press
|
| 483 | 1 | hiro | (GtkWidget *widget, |
| 484 | 1 | hiro | GdkEventKey *event, |
| 485 | 1 | hiro | GtkWidget **completion_window); |
| 486 | 1 | hiro | |
| 487 | 1 | hiro | |
| 488 | 1 | hiro | static void completion_window_advance_to_row(GtkCList *clist, gint row) |
| 489 | 1 | hiro | {
|
| 490 | 1 | hiro | g_return_if_fail(row < g_completion_count); |
| 491 | 1 | hiro | gtk_clist_select_row(clist, row, 0);
|
| 492 | 1 | hiro | } |
| 493 | 1 | hiro | |
| 494 | 1 | hiro | static void completion_window_advance_selection(GtkCList *clist, gboolean forward) |
| 495 | 1 | hiro | {
|
| 496 | 1 | hiro | int row;
|
| 497 | 1 | hiro | |
| 498 | 1 | hiro | g_return_if_fail(clist != NULL);
|
| 499 | 1 | hiro | g_return_if_fail(clist->selection != NULL);
|
| 500 | 1 | hiro | |
| 501 | 1 | hiro | row = GPOINTER_TO_INT(clist->selection->data); |
| 502 | 1 | hiro | |
| 503 | 1 | hiro | row = forward ? (row + 1) % g_completion_count :
|
| 504 | 1 | hiro | (row - 1) < 0 ? g_completion_count - 1 : row - 1; |
| 505 | 1 | hiro | |
| 506 | 1 | hiro | gtk_clist_freeze(clist); |
| 507 | 1 | hiro | completion_window_advance_to_row(clist, row); |
| 508 | 1 | hiro | gtk_clist_thaw(clist); |
| 509 | 1 | hiro | } |
| 510 | 1 | hiro | |
| 511 | 1 | hiro | #if 0
|
| 512 | 1 | hiro | /* completion_window_accept_selection() - accepts the current selection in the |
| 513 | 1 | hiro | * clist, and destroys the window */ |
| 514 | 1 | hiro | static void completion_window_accept_selection(GtkWidget **window, |
| 515 | 1 | hiro | GtkCList *clist, |
| 516 | 1 | hiro | GtkEntry *entry) |
| 517 | 1 | hiro | {
|
| 518 | 1 | hiro | gchar *address = NULL, *text = NULL; |
| 519 | 1 | hiro | gint cursor_pos, row; |
| 520 | 1 | hiro | |
| 521 | 1 | hiro | g_return_if_fail(window != NULL); |
| 522 | 1 | hiro | g_return_if_fail(*window != NULL); |
| 523 | 1 | hiro | g_return_if_fail(clist != NULL); |
| 524 | 1 | hiro | g_return_if_fail(entry != NULL); |
| 525 | 1 | hiro | g_return_if_fail(clist->selection != NULL); |
| 526 | 1 | hiro | |
| 527 | 1 | hiro | /* FIXME: I believe it's acceptable to access the selection member directly */ |
| 528 | 1 | hiro | row = GPOINTER_TO_INT(clist->selection->data); |
| 529 | 1 | hiro | |
| 530 | 1 | hiro | /* we just need the cursor position */ |
| 531 | 1 | hiro | address = get_address_from_edit(entry, &cursor_pos); |
| 532 | 1 | hiro | g_free(address); |
| 533 | 1 | hiro | gtk_clist_get_text(clist, row, 0, &text); |
| 534 | 1 | hiro | replace_address_in_edit(entry, text, cursor_pos); |
| 535 | 1 | hiro | |
| 536 | 1 | hiro | clear_completion_cache(); |
| 537 | 1 | hiro | gtk_widget_destroy(*window); |
| 538 | 1 | hiro | *window = NULL; |
| 539 | 1 | hiro | } |
| 540 | 1 | hiro | #endif |
| 541 | 1 | hiro | |
| 542 | 1 | hiro | /* completion_window_apply_selection() - apply the current selection in the
|
| 543 | 1 | hiro | * clist */ |
| 544 | 1 | hiro | static void completion_window_apply_selection(GtkCList *clist, GtkEntry *entry) |
| 545 | 1 | hiro | {
|
| 546 | 1 | hiro | gchar *address = NULL, *text = NULL; |
| 547 | 1 | hiro | gint cursor_pos, row; |
| 548 | 1 | hiro | |
| 549 | 1 | hiro | g_return_if_fail(clist != NULL);
|
| 550 | 1 | hiro | g_return_if_fail(entry != NULL);
|
| 551 | 1 | hiro | g_return_if_fail(clist->selection != NULL);
|
| 552 | 1 | hiro | |
| 553 | 1 | hiro | row = GPOINTER_TO_INT(clist->selection->data); |
| 554 | 1 | hiro | |
| 555 | 1 | hiro | address = get_address_from_edit(entry, &cursor_pos); |
| 556 | 1 | hiro | g_free(address); |
| 557 | 1 | hiro | gtk_clist_get_text(clist, row, 0, &text);
|
| 558 | 1 | hiro | replace_address_in_edit(entry, text, cursor_pos); |
| 559 | 1 | hiro | } |
| 560 | 1 | hiro | |
| 561 | 1 | hiro | /* should be called when creating the main window containing address
|
| 562 | 1 | hiro | * completion entries */ |
| 563 | 1 | hiro | void address_completion_start(GtkWidget *mainwindow)
|
| 564 | 1 | hiro | {
|
| 565 | 1 | hiro | start_address_completion(); |
| 566 | 1 | hiro | |
| 567 | 1 | hiro | /* register focus change hook */
|
| 568 | 1 | hiro | g_signal_connect(G_OBJECT(mainwindow), "set_focus",
|
| 569 | 1 | hiro | G_CALLBACK(address_completion_mainwindow_set_focus), |
| 570 | 1 | hiro | mainwindow); |
| 571 | 1 | hiro | } |
| 572 | 1 | hiro | |
| 573 | 1 | hiro | /* Need unique data to make unregistering signal handler possible for the auto
|
| 574 | 1 | hiro | * completed entry */ |
| 575 | 1 | hiro | #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa)) |
| 576 | 1 | hiro | |
| 577 | 1 | hiro | void address_completion_register_entry(GtkEntry *entry)
|
| 578 | 1 | hiro | {
|
| 579 | 1 | hiro | g_return_if_fail(entry != NULL);
|
| 580 | 1 | hiro | g_return_if_fail(GTK_IS_ENTRY(entry)); |
| 581 | 1 | hiro | |
| 582 | 1 | hiro | /* add hooked property */
|
| 583 | 1 | hiro | g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry); |
| 584 | 1 | hiro | |
| 585 | 1 | hiro | /* add keypress event */
|
| 586 | 1 | hiro | g_signal_connect_closure |
| 587 | 1 | hiro | (G_OBJECT(entry), "key_press_event",
|
| 588 | 1 | hiro | g_cclosure_new |
| 589 | 1 | hiro | (G_CALLBACK(address_completion_entry_key_pressed), |
| 590 | 1 | hiro | COMPLETION_UNIQUE_DATA, NULL),
|
| 591 | 1 | hiro | FALSE); |
| 592 | 1 | hiro | } |
| 593 | 1 | hiro | |
| 594 | 1 | hiro | void address_completion_unregister_entry(GtkEntry *entry)
|
| 595 | 1 | hiro | {
|
| 596 | 1 | hiro | GObject *entry_obj; |
| 597 | 1 | hiro | |
| 598 | 1 | hiro | g_return_if_fail(entry != NULL);
|
| 599 | 1 | hiro | g_return_if_fail(GTK_IS_ENTRY(entry)); |
| 600 | 1 | hiro | |
| 601 | 1 | hiro | entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK); |
| 602 | 1 | hiro | g_return_if_fail(entry_obj); |
| 603 | 1 | hiro | g_return_if_fail(entry_obj == G_OBJECT(entry)); |
| 604 | 1 | hiro | |
| 605 | 1 | hiro | /* has the hooked property? */
|
| 606 | 1 | hiro | g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
|
| 607 | 1 | hiro | |
| 608 | 1 | hiro | /* remove the hook */
|
| 609 | 1 | hiro | g_signal_handlers_disconnect_by_func |
| 610 | 1 | hiro | (G_OBJECT(entry), |
| 611 | 1 | hiro | G_CALLBACK(address_completion_entry_key_pressed), |
| 612 | 1 | hiro | COMPLETION_UNIQUE_DATA); |
| 613 | 1 | hiro | } |
| 614 | 1 | hiro | |
| 615 | 1 | hiro | /* should be called when main window with address completion entries
|
| 616 | 1 | hiro | * terminates. |
| 617 | 1 | hiro | * NOTE: this function assumes that it is called upon destruction of |
| 618 | 1 | hiro | * the window */ |
| 619 | 1 | hiro | void address_completion_end(GtkWidget *mainwindow)
|
| 620 | 1 | hiro | {
|
| 621 | 1 | hiro | /* if address_completion_end() is really called on closing the window,
|
| 622 | 1 | hiro | * we don't need to unregister the set_focus_cb */ |
| 623 | 1 | hiro | end_address_completion(); |
| 624 | 1 | hiro | } |
| 625 | 1 | hiro | |
| 626 | 1 | hiro | /* if focus changes to another entry, then clear completion cache */
|
| 627 | 1 | hiro | static void address_completion_mainwindow_set_focus(GtkWindow *window, |
| 628 | 1 | hiro | GtkWidget *widget, |
| 629 | 1 | hiro | gpointer data) |
| 630 | 1 | hiro | {
|
| 631 | 1 | hiro | if (widget && GTK_IS_ENTRY(widget) &&
|
| 632 | 1 | hiro | g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) |
| 633 | 1 | hiro | clear_completion_cache(); |
| 634 | 1 | hiro | } |
| 635 | 1 | hiro | |
| 636 | 1 | hiro | /* watch for tabs in one of the address entries. if no tab then clear the
|
| 637 | 1 | hiro | * completion cache */ |
| 638 | 1 | hiro | static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
|
| 639 | 1 | hiro | GdkEventKey *ev, |
| 640 | 1 | hiro | gpointer data) |
| 641 | 1 | hiro | {
|
| 642 | 1 | hiro | if (ev->keyval == GDK_Tab) {
|
| 643 | 1 | hiro | if (address_completion_complete_address_in_entry(entry, TRUE)) {
|
| 644 | 1 | hiro | address_completion_create_completion_window(entry); |
| 645 | 1 | hiro | /* route a void character to the default handler */
|
| 646 | 1 | hiro | /* this is a dirty hack; we're actually changing a key
|
| 647 | 1 | hiro | * reported by the system. */ |
| 648 | 1 | hiro | ev->keyval = GDK_AudibleBell_Enable; |
| 649 | 1 | hiro | ev->state &= ~GDK_SHIFT_MASK; |
| 650 | 1 | hiro | //gtk_signal_emit_stop_by_name(G_OBJECT(entry),
|
| 651 | 1 | hiro | // "key_press_event");
|
| 652 | 1 | hiro | return TRUE; // |
| 653 | 1 | hiro | } else {
|
| 654 | 1 | hiro | /* old behaviour */
|
| 655 | 1 | hiro | } |
| 656 | 1 | hiro | } else if (ev->keyval == GDK_Shift_L |
| 657 | 1 | hiro | || ev->keyval == GDK_Shift_R |
| 658 | 1 | hiro | || ev->keyval == GDK_Control_L |
| 659 | 1 | hiro | || ev->keyval == GDK_Control_R |
| 660 | 1 | hiro | || ev->keyval == GDK_Caps_Lock |
| 661 | 1 | hiro | || ev->keyval == GDK_Shift_Lock |
| 662 | 1 | hiro | || ev->keyval == GDK_Meta_L |
| 663 | 1 | hiro | || ev->keyval == GDK_Meta_R |
| 664 | 1 | hiro | || ev->keyval == GDK_Alt_L |
| 665 | 1 | hiro | || ev->keyval == GDK_Alt_R) {
|
| 666 | 1 | hiro | /* these buttons should not clear the cache... */
|
| 667 | 1 | hiro | } else
|
| 668 | 1 | hiro | clear_completion_cache(); |
| 669 | 1 | hiro | |
| 670 | 1 | hiro | return FALSE;
|
| 671 | 1 | hiro | } |
| 672 | 1 | hiro | |
| 673 | 1 | hiro | /* initialize the completion cache and put first completed string
|
| 674 | 1 | hiro | * in entry. this function used to do back cycling but this is not |
| 675 | 1 | hiro | * currently used. since the address completion behaviour has been |
| 676 | 1 | hiro | * changed regularly, we keep the feature in case someone changes |
| 677 | 1 | hiro | * his / her mind again. :) */ |
| 678 | 1 | hiro | static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
|
| 679 | 1 | hiro | gboolean next) |
| 680 | 1 | hiro | {
|
| 681 | 1 | hiro | gint ncount, cursor_pos; |
| 682 | 1 | hiro | gchar *address, *new = NULL;
|
| 683 | 1 | hiro | gboolean completed = FALSE; |
| 684 | 1 | hiro | |
| 685 | 1 | hiro | g_return_val_if_fail(entry != NULL, FALSE);
|
| 686 | 1 | hiro | |
| 687 | 1 | hiro | if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE; |
| 688 | 1 | hiro | |
| 689 | 1 | hiro | /* get an address component from the cursor */
|
| 690 | 1 | hiro | address = get_address_from_edit(entry, &cursor_pos); |
| 691 | 1 | hiro | if (!address) return FALSE; |
| 692 | 1 | hiro | |
| 693 | 1 | hiro | /* still something in the cache */
|
| 694 | 1 | hiro | if (is_completion_pending()) {
|
| 695 | 1 | hiro | new = next ? get_next_complete_address() : |
| 696 | 1 | hiro | get_prev_complete_address(); |
| 697 | 1 | hiro | } else {
|
| 698 | 1 | hiro | if (0 < (ncount = complete_address(address))) |
| 699 | 1 | hiro | new = get_next_complete_address(); |
| 700 | 1 | hiro | } |
| 701 | 1 | hiro | |
| 702 | 1 | hiro | if (new) {
|
| 703 | 1 | hiro | /* prevent "change" signal */
|
| 704 | 1 | hiro | /* replace_address_in_edit(entry, new, cursor_pos); */
|
| 705 | 1 | hiro | g_free(new); |
| 706 | 1 | hiro | completed = TRUE; |
| 707 | 1 | hiro | } |
| 708 | 1 | hiro | |
| 709 | 1 | hiro | g_free(address); |
| 710 | 1 | hiro | |
| 711 | 1 | hiro | return completed;
|
| 712 | 1 | hiro | } |
| 713 | 1 | hiro | |
| 714 | 1 | hiro | static void address_completion_create_completion_window(GtkEntry *entry_) |
| 715 | 1 | hiro | {
|
| 716 | 1 | hiro | static GtkWidget *completion_window;
|
| 717 | 1 | hiro | gint x, y, height, width, depth; |
| 718 | 1 | hiro | GtkWidget *scroll, *clist; |
| 719 | 1 | hiro | GtkRequisition r; |
| 720 | 1 | hiro | guint count = 0;
|
| 721 | 1 | hiro | GtkWidget *entry = GTK_WIDGET(entry_); |
| 722 | 1 | hiro | |
| 723 | 1 | hiro | if (completion_window) {
|
| 724 | 1 | hiro | gtk_widget_destroy(completion_window); |
| 725 | 1 | hiro | completion_window = NULL;
|
| 726 | 1 | hiro | } |
| 727 | 1 | hiro | |
| 728 | 1 | hiro | scroll = gtk_scrolled_window_new(NULL, NULL); |
| 729 | 1 | hiro | clist = gtk_clist_new(1);
|
| 730 | 1 | hiro | gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_SINGLE); |
| 731 | 1 | hiro | |
| 732 | 1 | hiro | completion_window = gtk_window_new(GTK_WINDOW_POPUP); |
| 733 | 1 | hiro | |
| 734 | 1 | hiro | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), |
| 735 | 1 | hiro | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
| 736 | 1 | hiro | gtk_container_add(GTK_CONTAINER(completion_window), scroll); |
| 737 | 1 | hiro | gtk_container_add(GTK_CONTAINER(scroll), clist); |
| 738 | 1 | hiro | |
| 739 | 1 | hiro | /* set the unique data so we can always get back the entry and
|
| 740 | 1 | hiro | * clist window to which this completion window has been attached */ |
| 741 | 1 | hiro | g_object_set_data(G_OBJECT(completion_window), |
| 742 | 1 | hiro | WINDOW_DATA_COMPL_ENTRY, entry_); |
| 743 | 1 | hiro | g_object_set_data(G_OBJECT(completion_window), |
| 744 | 1 | hiro | WINDOW_DATA_COMPL_CLIST, clist); |
| 745 | 1 | hiro | |
| 746 | 1 | hiro | g_signal_connect(G_OBJECT(clist), "select_row",
|
| 747 | 1 | hiro | G_CALLBACK(completion_window_select_row), |
| 748 | 1 | hiro | &completion_window); |
| 749 | 1 | hiro | |
| 750 | 1 | hiro | for (count = 0; count < get_completion_count(); count++) { |
| 751 | 1 | hiro | gchar *text[] = {NULL, NULL};
|
| 752 | 1 | hiro | |
| 753 | 1 | hiro | text[0] = get_complete_address(count);
|
| 754 | 1 | hiro | gtk_clist_append(GTK_CLIST(clist), text); |
| 755 | 1 | hiro | g_free(text[0]);
|
| 756 | 1 | hiro | } |
| 757 | 1 | hiro | |
| 758 | 1 | hiro | gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth); |
| 759 | 1 | hiro | gdk_window_get_deskrelative_origin (entry->window, &x, &y); |
| 760 | 1 | hiro | y += height; |
| 761 | 1 | hiro | gtk_window_move(GTK_WINDOW(completion_window), x, y); |
| 762 | 1 | hiro | |
| 763 | 1 | hiro | gtk_widget_size_request(clist, &r); |
| 764 | 1 | hiro | gtk_widget_set_size_request(completion_window, width, r.height); |
| 765 | 1 | hiro | gtk_widget_show_all(completion_window); |
| 766 | 1 | hiro | gtk_widget_size_request(clist, &r); |
| 767 | 1 | hiro | |
| 768 | 1 | hiro | if ((y + r.height) > gdk_screen_height()) {
|
| 769 | 1 | hiro | gtk_window_set_policy(GTK_WINDOW(completion_window), |
| 770 | 1 | hiro | TRUE, FALSE, FALSE); |
| 771 | 1 | hiro | gtk_widget_set_size_request(completion_window, width, |
| 772 | 1 | hiro | gdk_screen_height () - y); |
| 773 | 1 | hiro | } |
| 774 | 1 | hiro | |
| 775 | 1 | hiro | g_signal_connect(G_OBJECT(completion_window), |
| 776 | 1 | hiro | "button-press-event",
|
| 777 | 1 | hiro | G_CALLBACK(completion_window_button_press), |
| 778 | 1 | hiro | &completion_window); |
| 779 | 1 | hiro | g_signal_connect(G_OBJECT(completion_window), |
| 780 | 1 | hiro | "key-press-event",
|
| 781 | 1 | hiro | G_CALLBACK(completion_window_key_press), |
| 782 | 1 | hiro | &completion_window); |
| 783 | 1 | hiro | gdk_pointer_grab(completion_window->window, TRUE, |
| 784 | 1 | hiro | GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | |
| 785 | 1 | hiro | GDK_BUTTON_RELEASE_MASK, |
| 786 | 1 | hiro | NULL, NULL, GDK_CURRENT_TIME); |
| 787 | 1 | hiro | gtk_grab_add(completion_window); |
| 788 | 1 | hiro | |
| 789 | 1 | hiro | /* this gets rid of the irritating focus rectangle that doesn't
|
| 790 | 1 | hiro | * follow the selection */ |
| 791 | 1 | hiro | GTK_WIDGET_UNSET_FLAGS(clist, GTK_CAN_FOCUS); |
| 792 | 1 | hiro | gtk_clist_select_row(GTK_CLIST(clist), 1, 0); |
| 793 | 1 | hiro | } |
| 794 | 1 | hiro | |
| 795 | 1 | hiro | |
| 796 | 1 | hiro | /* row selection sends completed address to entry.
|
| 797 | 1 | hiro | * note: event is NULL if selected by anything else than a mouse button. */ |
| 798 | 1 | hiro | static void completion_window_select_row(GtkCList *clist, gint row, gint col, |
| 799 | 1 | hiro | GdkEvent *event, |
| 800 | 1 | hiro | GtkWidget **completion_window) |
| 801 | 1 | hiro | {
|
| 802 | 1 | hiro | GtkEntry *entry; |
| 803 | 1 | hiro | |
| 804 | 1 | hiro | g_return_if_fail(completion_window != NULL);
|
| 805 | 1 | hiro | g_return_if_fail(*completion_window != NULL);
|
| 806 | 1 | hiro | |
| 807 | 1 | hiro | entry = GTK_ENTRY(g_object_get_data(G_OBJECT(*completion_window), |
| 808 | 1 | hiro | WINDOW_DATA_COMPL_ENTRY)); |
| 809 | 1 | hiro | g_return_if_fail(entry != NULL);
|
| 810 | 1 | hiro | |
| 811 | 1 | hiro | completion_window_apply_selection(clist, entry); |
| 812 | 1 | hiro | |
| 813 | 1 | hiro | if (!event || event->type != GDK_BUTTON_RELEASE)
|
| 814 | 1 | hiro | return;
|
| 815 | 1 | hiro | |
| 816 | 1 | hiro | clear_completion_cache(); |
| 817 | 1 | hiro | gtk_widget_destroy(*completion_window); |
| 818 | 1 | hiro | *completion_window = NULL;
|
| 819 | 1 | hiro | } |
| 820 | 1 | hiro | |
| 821 | 1 | hiro | /* completion_window_button_press() - check is mouse click is anywhere
|
| 822 | 1 | hiro | * else (not in the completion window). in that case the completion |
| 823 | 1 | hiro | * window is destroyed, and the original prefix is restored */ |
| 824 | 1 | hiro | static gboolean completion_window_button_press(GtkWidget *widget,
|
| 825 | 1 | hiro | GdkEventButton *event, |
| 826 | 1 | hiro | GtkWidget **completion_window) |
| 827 | 1 | hiro | {
|
| 828 | 1 | hiro | GtkWidget *event_widget, *entry; |
| 829 | 1 | hiro | gchar *prefix; |
| 830 | 1 | hiro | gint cursor_pos; |
| 831 | 1 | hiro | gboolean restore = TRUE; |
| 832 | 1 | hiro | |
| 833 | 1 | hiro | g_return_val_if_fail(completion_window != NULL, FALSE);
|
| 834 | 1 | hiro | g_return_val_if_fail(*completion_window != NULL, FALSE);
|
| 835 | 1 | hiro | |
| 836 | 1 | hiro | entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window), |
| 837 | 1 | hiro | WINDOW_DATA_COMPL_ENTRY)); |
| 838 | 1 | hiro | g_return_val_if_fail(entry != NULL, FALSE);
|
| 839 | 1 | hiro | |
| 840 | 1 | hiro | event_widget = gtk_get_event_widget((GdkEvent *)event); |
| 841 | 1 | hiro | if (event_widget != widget) {
|
| 842 | 1 | hiro | while (event_widget) {
|
| 843 | 1 | hiro | if (event_widget == widget)
|
| 844 | 1 | hiro | return FALSE;
|
| 845 | 1 | hiro | else if (event_widget == entry) { |
| 846 | 1 | hiro | restore = FALSE; |
| 847 | 1 | hiro | break;
|
| 848 | 1 | hiro | } |
| 849 | 1 | hiro | event_widget = event_widget->parent; |
| 850 | 1 | hiro | } |
| 851 | 1 | hiro | } |
| 852 | 1 | hiro | |
| 853 | 1 | hiro | if (restore) {
|
| 854 | 1 | hiro | prefix = get_complete_address(0);
|
| 855 | 1 | hiro | g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos)); |
| 856 | 1 | hiro | replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos); |
| 857 | 1 | hiro | g_free(prefix); |
| 858 | 1 | hiro | } |
| 859 | 1 | hiro | |
| 860 | 1 | hiro | clear_completion_cache(); |
| 861 | 1 | hiro | gtk_widget_destroy(*completion_window); |
| 862 | 1 | hiro | *completion_window = NULL;
|
| 863 | 1 | hiro | |
| 864 | 1 | hiro | return TRUE;
|
| 865 | 1 | hiro | } |
| 866 | 1 | hiro | |
| 867 | 1 | hiro | static gboolean completion_window_key_press(GtkWidget *widget,
|
| 868 | 1 | hiro | GdkEventKey *event, |
| 869 | 1 | hiro | GtkWidget **completion_window) |
| 870 | 1 | hiro | {
|
| 871 | 1 | hiro | GdkEventKey tmp_event; |
| 872 | 1 | hiro | GtkWidget *entry; |
| 873 | 1 | hiro | gchar *prefix; |
| 874 | 1 | hiro | gint cursor_pos; |
| 875 | 1 | hiro | GtkWidget *clist; |
| 876 | 1 | hiro | |
| 877 | 1 | hiro | g_return_val_if_fail(completion_window != NULL, FALSE);
|
| 878 | 1 | hiro | g_return_val_if_fail(*completion_window != NULL, FALSE);
|
| 879 | 1 | hiro | |
| 880 | 1 | hiro | entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window), |
| 881 | 1 | hiro | WINDOW_DATA_COMPL_ENTRY)); |
| 882 | 1 | hiro | clist = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window), |
| 883 | 1 | hiro | WINDOW_DATA_COMPL_CLIST)); |
| 884 | 1 | hiro | g_return_val_if_fail(entry != NULL, FALSE);
|
| 885 | 1 | hiro | |
| 886 | 1 | hiro | /* allow keyboard navigation in the alternatives clist */
|
| 887 | 1 | hiro | if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
|
| 888 | 1 | hiro | event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
|
| 889 | 1 | hiro | completion_window_advance_selection |
| 890 | 1 | hiro | (GTK_CLIST(clist), |
| 891 | 1 | hiro | event->keyval == GDK_Down || |
| 892 | 1 | hiro | event->keyval == GDK_Page_Down ? TRUE : FALSE); |
| 893 | 1 | hiro | return FALSE;
|
| 894 | 1 | hiro | } |
| 895 | 1 | hiro | |
| 896 | 1 | hiro | /* also make tab / shift tab go to next previous completion entry. we're
|
| 897 | 1 | hiro | * changing the key value */ |
| 898 | 1 | hiro | if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
|
| 899 | 1 | hiro | event->keyval = (event->state & GDK_SHIFT_MASK) |
| 900 | 1 | hiro | ? GDK_Up : GDK_Down; |
| 901 | 1 | hiro | /* need to reset shift state if going up */
|
| 902 | 1 | hiro | if (event->state & GDK_SHIFT_MASK)
|
| 903 | 1 | hiro | event->state &= ~GDK_SHIFT_MASK; |
| 904 | 1 | hiro | completion_window_advance_selection(GTK_CLIST(clist), |
| 905 | 1 | hiro | event->keyval == GDK_Down ? TRUE : FALSE); |
| 906 | 1 | hiro | return FALSE;
|
| 907 | 1 | hiro | } |
| 908 | 1 | hiro | |
| 909 | 1 | hiro | /* look for presses that accept the selection */
|
| 910 | 1 | hiro | if (event->keyval == GDK_Return || event->keyval == GDK_space) {
|
| 911 | 1 | hiro | clear_completion_cache(); |
| 912 | 1 | hiro | gtk_widget_destroy(*completion_window); |
| 913 | 1 | hiro | *completion_window = NULL;
|
| 914 | 1 | hiro | return FALSE;
|
| 915 | 1 | hiro | } |
| 916 | 1 | hiro | |
| 917 | 1 | hiro | /* key state keys should never be handled */
|
| 918 | 1 | hiro | if (event->keyval == GDK_Shift_L
|
| 919 | 1 | hiro | || event->keyval == GDK_Shift_R |
| 920 | 1 | hiro | || event->keyval == GDK_Control_L |
| 921 | 1 | hiro | || event->keyval == GDK_Control_R |
| 922 | 1 | hiro | || event->keyval == GDK_Caps_Lock |
| 923 | 1 | hiro | || event->keyval == GDK_Shift_Lock |
| 924 | 1 | hiro | || event->keyval == GDK_Meta_L |
| 925 | 1 | hiro | || event->keyval == GDK_Meta_R |
| 926 | 1 | hiro | || event->keyval == GDK_Alt_L |
| 927 | 1 | hiro | || event->keyval == GDK_Alt_R) {
|
| 928 | 1 | hiro | return FALSE;
|
| 929 | 1 | hiro | } |
| 930 | 1 | hiro | |
| 931 | 1 | hiro | /* other key, let's restore the prefix (orignal text) */
|
| 932 | 1 | hiro | prefix = get_complete_address(0);
|
| 933 | 1 | hiro | g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos)); |
| 934 | 1 | hiro | replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos); |
| 935 | 1 | hiro | g_free(prefix); |
| 936 | 1 | hiro | clear_completion_cache(); |
| 937 | 1 | hiro | |
| 938 | 1 | hiro | /* make sure anything we typed comes in the edit box */
|
| 939 | 1 | hiro | tmp_event.type = event->type; |
| 940 | 1 | hiro | tmp_event.window = entry->window; |
| 941 | 1 | hiro | tmp_event.send_event = TRUE; |
| 942 | 1 | hiro | tmp_event.time = event->time; |
| 943 | 1 | hiro | tmp_event.state = event->state; |
| 944 | 1 | hiro | tmp_event.keyval = event->keyval; |
| 945 | 1 | hiro | tmp_event.length = event->length; |
| 946 | 1 | hiro | tmp_event.string = event->string; |
| 947 | 1 | hiro | gtk_widget_event(entry, (GdkEvent *)&tmp_event); |
| 948 | 1 | hiro | |
| 949 | 1 | hiro | /* and close the completion window */
|
| 950 | 1 | hiro | gtk_widget_destroy(*completion_window); |
| 951 | 1 | hiro | *completion_window = NULL;
|
| 952 | 1 | hiro | |
| 953 | 1 | hiro | return TRUE;
|
| 954 | 1 | hiro | } |