1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
|
/*
* AutoComplete interfaces implementation.
*
* Copyright 2004 Maxime Bellengé <maxime.bellenge@laposte.net>
* Copyright 2018 Gabriel Ivăncescu <gabrielopcode@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
TODO:
- implement ACO_SEARCH style
- implement ACO_RTLREADING style
- implement ACO_WORD_FILTER style
*/
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define COBJMACROS
#include "wine/debug.h"
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "undocshell.h"
#include "shlwapi.h"
#include "winerror.h"
#include "objbase.h"
#include "pidl.h"
#include "shlobj.h"
#include "shldisp.h"
#include "debughlp.h"
#include "shell32_main.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(shell);
typedef struct
{
IAutoComplete2 IAutoComplete2_iface;
IAutoCompleteDropDown IAutoCompleteDropDown_iface;
LONG ref;
BOOL initialized;
BOOL enabled;
UINT enum_strs_num;
WCHAR **enum_strs;
HWND hwndEdit;
HWND hwndListBox;
WNDPROC wpOrigEditProc;
WNDPROC wpOrigLBoxProc;
WCHAR *txtbackup;
WCHAR *quickComplete;
IEnumString *enumstr;
IACList *aclist;
AUTOCOMPLETEOPTIONS options;
WCHAR no_fwd_char;
} IAutoCompleteImpl;
enum autoappend_flag
{
autoappend_flag_yes,
autoappend_flag_no,
autoappend_flag_displayempty
};
enum prefix_filtering
{
prefix_filtering_none = 0, /* no prefix filtering (raw search) */
prefix_filtering_protocol, /* filter common protocol (e.g. http://) */
prefix_filtering_all /* filter all common prefixes (protocol & www. ) */
};
static const WCHAR autocomplete_propertyW[] = {'W','i','n','e',' ','A','u','t','o',
'c','o','m','p','l','e','t','e',' ',
'c','o','n','t','r','o','l',0};
static inline IAutoCompleteImpl *impl_from_IAutoComplete2(IAutoComplete2 *iface)
{
return CONTAINING_RECORD(iface, IAutoCompleteImpl, IAutoComplete2_iface);
}
static inline IAutoCompleteImpl *impl_from_IAutoCompleteDropDown(IAutoCompleteDropDown *iface)
{
return CONTAINING_RECORD(iface, IAutoCompleteImpl, IAutoCompleteDropDown_iface);
}
static void set_text_and_selection(IAutoCompleteImpl *ac, HWND hwnd, WCHAR *text, WPARAM start, LPARAM end)
{
/* Send it directly to the edit control to match Windows behavior */
WNDPROC proc = ac->wpOrigEditProc;
if (CallWindowProcW(proc, hwnd, WM_SETTEXT, 0, (LPARAM)text))
CallWindowProcW(proc, hwnd, EM_SETSEL, start, end);
}
static inline WCHAR *filter_protocol(WCHAR *str)
{
static const WCHAR http[] = {'h','t','t','p'};
if (!strncmpW(str, http, ARRAY_SIZE(http)))
{
str += ARRAY_SIZE(http);
str += (*str == 's'); /* https */
if (str[0] == ':' && str[1] == '/' && str[2] == '/')
return str + 3;
}
return NULL;
}
static inline WCHAR *filter_www(WCHAR *str)
{
static const WCHAR www[] = {'w','w','w','.'};
if (!strncmpW(str, www, ARRAY_SIZE(www)))
return str + ARRAY_SIZE(www);
return NULL;
}
/*
Get the prefix filtering based on text, for example if text's prefix
is a protocol, then we return none because we actually filter nothing
*/
static enum prefix_filtering get_text_prefix_filtering(const WCHAR *text)
{
/* Convert to lowercase to perform case insensitive filtering,
using the longest possible prefix as the size of the buffer */
WCHAR buf[sizeof("https://")];
UINT i;
for (i = 0; i < ARRAY_SIZE(buf) - 1 && text[i]; i++)
buf[i] = tolowerW(text[i]);
buf[i] = '\0';
if (filter_protocol(buf)) return prefix_filtering_none;
if (filter_www(buf)) return prefix_filtering_protocol;
return prefix_filtering_all;
}
/*
Filter the prefix of str based on the value of pfx_filter
This is used in sorting, so it's more performance sensitive
*/
static WCHAR *filter_str_prefix(WCHAR *str, enum prefix_filtering pfx_filter)
{
WCHAR *p = str;
if (pfx_filter == prefix_filtering_none) return str;
if ((p = filter_protocol(str))) str = p;
if (pfx_filter == prefix_filtering_protocol) return str;
if ((p = filter_www(str))) str = p;
return str;
}
static inline int sort_strs_cmpfn_impl(WCHAR *a, WCHAR *b, enum prefix_filtering pfx_filter)
{
WCHAR *str1 = filter_str_prefix(a, pfx_filter);
WCHAR *str2 = filter_str_prefix(b, pfx_filter);
return strcmpiW(str1, str2);
}
static int sort_strs_cmpfn_none(const void *a, const void *b)
{
return sort_strs_cmpfn_impl(*(WCHAR* const*)a, *(WCHAR* const*)b, prefix_filtering_none);
}
static int sort_strs_cmpfn_protocol(const void *a, const void *b)
{
return sort_strs_cmpfn_impl(*(WCHAR* const*)a, *(WCHAR* const*)b, prefix_filtering_protocol);
}
static int sort_strs_cmpfn_all(const void *a, const void *b)
{
return sort_strs_cmpfn_impl(*(WCHAR* const*)a, *(WCHAR* const*)b, prefix_filtering_all);
}
static int (*const sort_strs_cmpfn[])(const void*, const void*) =
{
sort_strs_cmpfn_none,
sort_strs_cmpfn_protocol,
sort_strs_cmpfn_all
};
static void sort_strs(WCHAR **strs, UINT numstrs, enum prefix_filtering pfx_filter)
{
qsort(strs, numstrs, sizeof(*strs), sort_strs_cmpfn[pfx_filter]);
}
/*
Enumerate all of the strings and sort them in the internal list.
We don't free the enumerated strings (except on error) to avoid needless
copies, until the next reset (or the object itself is destroyed)
*/
static void enumerate_strings(IAutoCompleteImpl *ac, enum prefix_filtering pfx_filter)
{
UINT cur = 0, array_size = 1024;
LPOLESTR *strs = NULL, *tmp;
ULONG read;
do
{
if ((tmp = heap_realloc(strs, array_size * sizeof(*strs))) == NULL)
goto fail;
strs = tmp;
do
{
if (FAILED(IEnumString_Next(ac->enumstr, array_size - cur, &strs[cur], &read)))
read = 0;
} while (read != 0 && (cur += read) < array_size);
array_size *= 2;
} while (read != 0);
/* Allocate even if there were zero strings enumerated, to mark it non-NULL */
if ((tmp = heap_realloc(strs, cur * sizeof(*strs))))
{
strs = tmp;
if (cur > 0)
sort_strs(strs, cur, pfx_filter);
ac->enum_strs = strs;
ac->enum_strs_num = cur;
return;
}
fail:
while (cur--)
CoTaskMemFree(strs[cur]);
heap_free(strs);
}
static UINT find_matching_enum_str(IAutoCompleteImpl *ac, UINT start, WCHAR *text,
UINT len, enum prefix_filtering pfx_filter, int direction)
{
WCHAR **strs = ac->enum_strs;
UINT index = ~0, a = start, b = ac->enum_strs_num;
while (a < b)
{
UINT i = (a + b - 1) / 2;
int cmp = strncmpiW(text, filter_str_prefix(strs[i], pfx_filter), len);
if (cmp == 0)
{
index = i;
cmp = direction;
}
if (cmp <= 0) b = i;
else a = i + 1;
}
return index;
}
static void free_enum_strs(IAutoCompleteImpl *ac)
{
WCHAR **strs = ac->enum_strs;
if (strs)
{
UINT i = ac->enum_strs_num;
ac->enum_strs = NULL;
while (i--)
CoTaskMemFree(strs[i]);
heap_free(strs);
}
}
static void hide_listbox(IAutoCompleteImpl *ac, HWND hwnd, BOOL reset)
{
ShowWindow(hwnd, SW_HIDE);
SendMessageW(hwnd, LB_RESETCONTENT, 0, 0);
if (reset) free_enum_strs(ac);
}
static void show_listbox(IAutoCompleteImpl *ac)
{
RECT r;
UINT cnt, width, height;
GetWindowRect(ac->hwndEdit, &r);
SendMessageW(ac->hwndListBox, LB_CARETOFF, 0, 0);
/* Windows XP displays 7 lines at most, then it uses a scroll bar */
cnt = SendMessageW(ac->hwndListBox, LB_GETCOUNT, 0, 0);
height = SendMessageW(ac->hwndListBox, LB_GETITEMHEIGHT, 0, 0) * min(cnt + 1, 7);
width = r.right - r.left;
SetWindowPos(ac->hwndListBox, HWND_TOP, r.left, r.bottom + 1, width, height, SWP_SHOWWINDOW);
}
static size_t format_quick_complete(WCHAR *dst, const WCHAR *qc, const WCHAR *str, size_t str_len)
{
/* Replace the first %s directly without using snprintf, to avoid
exploits since the format string can be retrieved from the registry */
WCHAR *base = dst;
UINT args = 0;
while (*qc != '\0')
{
if (qc[0] == '%')
{
if (args < 1 && qc[1] == 's')
{
memcpy(dst, str, str_len * sizeof(WCHAR));
dst += str_len;
qc += 2;
args++;
continue;
}
qc += (qc[1] == '%');
}
*dst++ = *qc++;
}
*dst = '\0';
return dst - base;
}
static BOOL select_item_with_return_key(IAutoCompleteImpl *ac, HWND hwnd)
{
WCHAR *text;
HWND hwndListBox = ac->hwndListBox;
if (!(ac->options & ACO_AUTOSUGGEST))
return FALSE;
if (IsWindowVisible(hwndListBox))
{
INT sel = SendMessageW(hwndListBox, LB_GETCURSEL, 0, 0);
if (sel >= 0)
{
UINT len = SendMessageW(hwndListBox, LB_GETTEXTLEN, sel, 0);
if ((text = heap_alloc((len + 1) * sizeof(WCHAR))))
{
len = SendMessageW(hwndListBox, LB_GETTEXT, sel, (LPARAM)text);
set_text_and_selection(ac, hwnd, text, 0, len);
hide_listbox(ac, hwndListBox, TRUE);
ac->no_fwd_char = '\r'; /* RETURN char */
heap_free(text);
return TRUE;
}
}
}
hide_listbox(ac, hwndListBox, TRUE);
return FALSE;
}
static LRESULT change_selection(IAutoCompleteImpl *ac, HWND hwnd, UINT key)
{
INT count = SendMessageW(ac->hwndListBox, LB_GETCOUNT, 0, 0);
INT sel = SendMessageW(ac->hwndListBox, LB_GETCURSEL, 0, 0);
if (key == VK_PRIOR || key == VK_NEXT)
{
if (sel < 0)
sel = (key == VK_PRIOR) ? count - 1 : 0;
else
{
INT base = SendMessageW(ac->hwndListBox, LB_GETTOPINDEX, 0, 0);
INT pgsz = SendMessageW(ac->hwndListBox, LB_GETLISTBOXINFO, 0, 0);
pgsz = max(pgsz - 1, 1);
if (key == VK_PRIOR)
{
if (sel == 0)
sel = -1;
else
{
if (sel == base) base -= min(base, pgsz);
sel = base;
}
}
else
{
if (sel == count - 1)
sel = -1;
else
{
base += pgsz;
if (sel >= base) base += pgsz;
sel = min(base, count - 1);
}
}
}
}
else if (key == VK_UP || (key == VK_TAB && (GetKeyState(VK_SHIFT) & 0x8000)))
sel = ((sel - 1) < -1) ? count - 1 : sel - 1;
else
sel = ((sel + 1) >= count) ? -1 : sel + 1;
SendMessageW(ac->hwndListBox, LB_SETCURSEL, sel, 0);
if (sel >= 0)
{
WCHAR *msg;
UINT len = SendMessageW(ac->hwndListBox, LB_GETTEXTLEN, sel, 0);
if (!(msg = heap_alloc((len + 1) * sizeof(WCHAR))))
return 0;
len = SendMessageW(ac->hwndListBox, LB_GETTEXT, sel, (LPARAM)msg);
set_text_and_selection(ac, hwnd, msg, len, len);
heap_free(msg);
}
else
{
UINT len = strlenW(ac->txtbackup);
set_text_and_selection(ac, hwnd, ac->txtbackup, len, len);
}
return 0;
}
static BOOL do_aclist_expand(IAutoCompleteImpl *ac, WCHAR *txt, WCHAR *last_delim)
{
WCHAR c = last_delim[1];
free_enum_strs(ac);
IEnumString_Reset(ac->enumstr); /* call before expand */
last_delim[1] = '\0';
IACList_Expand(ac->aclist, txt);
last_delim[1] = c;
return TRUE;
}
static BOOL aclist_expand(IAutoCompleteImpl *ac, WCHAR *txt)
{
/* call IACList::Expand only when needed, if the
new txt and old_txt require different expansions */
static const WCHAR empty[] = { 0 };
const WCHAR *old_txt = ac->txtbackup;
WCHAR c, *p, *last_delim;
size_t i = 0;
/* '/' is allowed as a delim for unix paths */
static const WCHAR delims[] = { '\\', '/', 0 };
/* always expand if the enumerator was reset */
if (!ac->enum_strs) old_txt = empty;
/* skip the shared prefix */
while ((c = tolowerW(txt[i])) == tolowerW(old_txt[i]))
{
if (c == '\0') return FALSE;
i++;
}
/* they differ at this point, check for a delim further in txt */
for (last_delim = NULL, p = &txt[i]; (p = strpbrkW(p, delims)) != NULL; p++)
last_delim = p;
if (last_delim) return do_aclist_expand(ac, txt, last_delim);
/* txt has no delim after i, check for a delim further in old_txt */
if (strpbrkW(&old_txt[i], delims))
{
/* scan backwards to find the first delim before txt[i] (if any) */
while (i--)
if (strchrW(delims, txt[i]))
return do_aclist_expand(ac, txt, &txt[i]);
/* Windows doesn't expand without a delim, but it does reset */
free_enum_strs(ac);
}
return FALSE;
}
static void autoappend_str(IAutoCompleteImpl *ac, WCHAR *text, UINT len, WCHAR *str, HWND hwnd)
{
DWORD sel_start;
WCHAR *tmp;
size_t size;
/* Don't auto-append unless the caret is at the end */
SendMessageW(hwnd, EM_GETSEL, (WPARAM)&sel_start, 0);
if (sel_start != len)
return;
/* The character capitalization can be different,
so merge text and str into a new string */
size = len + strlenW(&str[len]) + 1;
if ((tmp = heap_alloc(size * sizeof(*tmp))))
{
memcpy(tmp, text, len * sizeof(*tmp));
memcpy(&tmp[len], &str[len], (size - len) * sizeof(*tmp));
}
else tmp = str;
set_text_and_selection(ac, hwnd, tmp, len, size - 1);
if (tmp != str)
heap_free(tmp);
}
static BOOL display_matching_strs(IAutoCompleteImpl *ac, WCHAR *text, UINT len,
enum prefix_filtering pfx_filter, HWND hwnd,
enum autoappend_flag flag)
{
/* Return FALSE if we need to hide the listbox */
WCHAR **str = ac->enum_strs;
UINT start, end;
if (!str) return !(ac->options & ACO_AUTOSUGGEST);
/* Windows seems to disable autoappend if ACO_NOPREFIXFILTERING is set */
if (!(ac->options & ACO_NOPREFIXFILTERING) && len)
{
start = find_matching_enum_str(ac, 0, text, len, pfx_filter, -1);
if (start == ~0)
return !(ac->options & ACO_AUTOSUGGEST);
if (flag == autoappend_flag_yes)
autoappend_str(ac, text, len, filter_str_prefix(str[start], pfx_filter), hwnd);
if (!(ac->options & ACO_AUTOSUGGEST))
return TRUE;
/* Find the index beyond the last string that matches */
end = find_matching_enum_str(ac, start + 1, text, len, pfx_filter, 1);
end = (end == ~0 ? start : end) + 1;
}
else
{
if (!(ac->options & ACO_AUTOSUGGEST))
return TRUE;
start = 0;
end = ac->enum_strs_num;
if (end == 0)
return FALSE;
}
SendMessageW(ac->hwndListBox, WM_SETREDRAW, FALSE, 0);
SendMessageW(ac->hwndListBox, LB_RESETCONTENT, 0, 0);
SendMessageW(ac->hwndListBox, LB_INITSTORAGE, end - start, 0);
for (; start < end; start++)
SendMessageW(ac->hwndListBox, LB_INSERTSTRING, -1, (LPARAM)str[start]);
show_listbox(ac);
SendMessageW(ac->hwndListBox, WM_SETREDRAW, TRUE, 0);
return TRUE;
}
static enum prefix_filtering setup_prefix_filtering(IAutoCompleteImpl *ac, const WCHAR *text)
{
enum prefix_filtering pfx_filter;
if (!(ac->options & ACO_FILTERPREFIXES)) return prefix_filtering_none;
pfx_filter = get_text_prefix_filtering(text);
if (!ac->enum_strs) return pfx_filter;
/* If the prefix filtering is different, re-sort the filtered strings */
if (pfx_filter != get_text_prefix_filtering(ac->txtbackup))
sort_strs(ac->enum_strs, ac->enum_strs_num, pfx_filter);
return pfx_filter;
}
static void autocomplete_text(IAutoCompleteImpl *ac, HWND hwnd, enum autoappend_flag flag)
{
WCHAR *text;
BOOL expanded = FALSE;
enum prefix_filtering pfx_filter;
UINT size, len = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0);
if (flag != autoappend_flag_displayempty && len == 0)
{
if (ac->options & ACO_AUTOSUGGEST)
hide_listbox(ac, ac->hwndListBox, FALSE);
free_enum_strs(ac);
return;
}
size = len + 1;
if (!(text = heap_alloc(size * sizeof(WCHAR))))
return;
len = SendMessageW(hwnd, WM_GETTEXT, size, (LPARAM)text);
if (len + 1 != size)
text = heap_realloc(text, (len + 1) * sizeof(WCHAR));
if (ac->aclist)
{
if (text[len - 1] == '\\' || text[len - 1] == '/')
flag = autoappend_flag_no;
expanded = aclist_expand(ac, text);
}
pfx_filter = setup_prefix_filtering(ac, text);
if (expanded || !ac->enum_strs)
{
if (!expanded) IEnumString_Reset(ac->enumstr);
enumerate_strings(ac, pfx_filter);
}
/* Set txtbackup to point to text itself (which must not be released),
and it must be done here since aclist_expand uses it to track changes */
heap_free(ac->txtbackup);
ac->txtbackup = text;
if (!display_matching_strs(ac, text, len, pfx_filter, hwnd, flag))
hide_listbox(ac, ac->hwndListBox, FALSE);
}
static void destroy_autocomplete_object(IAutoCompleteImpl *ac)
{
ac->hwndEdit = NULL;
free_enum_strs(ac);
if (ac->hwndListBox)
DestroyWindow(ac->hwndListBox);
IAutoComplete2_Release(&ac->IAutoComplete2_iface);
}
/*
Helper for ACEditSubclassProc
*/
static LRESULT ACEditSubclassProc_KeyDown(IAutoCompleteImpl *ac, HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case VK_ESCAPE:
/* When pressing ESC, Windows hides the auto-suggest listbox, if visible */
if ((ac->options & ACO_AUTOSUGGEST) && IsWindowVisible(ac->hwndListBox))
{
hide_listbox(ac, ac->hwndListBox, FALSE);
ac->no_fwd_char = 0x1B; /* ESC char */
return 0;
}
break;
case VK_RETURN:
/* If quickComplete is set and control is pressed, replace the string */
if (ac->quickComplete && (GetKeyState(VK_CONTROL) & 0x8000))
{
WCHAR *text, *buf;
size_t sz;
UINT len = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0);
ac->no_fwd_char = '\n'; /* CTRL+RETURN char */
if (!(text = heap_alloc((len + 1) * sizeof(WCHAR))))
return 0;
len = SendMessageW(hwnd, WM_GETTEXT, len + 1, (LPARAM)text);
sz = strlenW(ac->quickComplete) + 1 + len;
if ((buf = heap_alloc(sz * sizeof(WCHAR))))
{
len = format_quick_complete(buf, ac->quickComplete, text, len);
set_text_and_selection(ac, hwnd, buf, 0, len);
heap_free(buf);
}
if (ac->options & ACO_AUTOSUGGEST)
hide_listbox(ac, ac->hwndListBox, TRUE);
heap_free(text);
return 0;
}
if (select_item_with_return_key(ac, hwnd))
return 0;
break;
case VK_TAB:
if ((ac->options & (ACO_AUTOSUGGEST | ACO_USETAB)) == (ACO_AUTOSUGGEST | ACO_USETAB)
&& IsWindowVisible(ac->hwndListBox) && !(GetKeyState(VK_CONTROL) & 0x8000))
{
ac->no_fwd_char = '\t';
return change_selection(ac, hwnd, wParam);
}
break;
case VK_UP:
case VK_DOWN:
case VK_PRIOR:
case VK_NEXT:
/* Two cases here:
- if the listbox is not visible and ACO_UPDOWNKEYDROPSLIST is
set, display it with all the entries, without selecting any
- if the listbox is visible, change the selection
*/
if (!(ac->options & ACO_AUTOSUGGEST))
break;
if (!IsWindowVisible(ac->hwndListBox))
{
if (ac->options & ACO_UPDOWNKEYDROPSLIST)
{
autocomplete_text(ac, hwnd, autoappend_flag_displayempty);
return 0;
}
}
else
return change_selection(ac, hwnd, wParam);
break;
case VK_DELETE:
{
LRESULT ret = CallWindowProcW(ac->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
autocomplete_text(ac, hwnd, autoappend_flag_no);
return ret;
}
}
ac->no_fwd_char = '\0';
return CallWindowProcW(ac->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
}
/*
Window procedure for autocompletion
*/
static LRESULT APIENTRY ACEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
IAutoCompleteImpl *This = GetPropW(hwnd, autocomplete_propertyW);
LRESULT ret;
if (!This->enabled) return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
switch (uMsg)
{
case CB_SHOWDROPDOWN:
if (This->options & ACO_AUTOSUGGEST)
hide_listbox(This, This->hwndListBox, TRUE);
return 0;
case WM_KILLFOCUS:
if (This->options & ACO_AUTOSUGGEST)
{
if ((HWND)wParam == This->hwndListBox) break;
hide_listbox(This, This->hwndListBox, FALSE);
}
/* Reset the enumerator if it's not visible anymore */
if (!IsWindowVisible(hwnd)) free_enum_strs(This);
break;
case WM_WINDOWPOSCHANGED:
{
WINDOWPOS *pos = (WINDOWPOS*)lParam;
if ((pos->flags & (SWP_NOMOVE | SWP_NOSIZE)) != (SWP_NOMOVE | SWP_NOSIZE) &&
This->hwndListBox && IsWindowVisible(This->hwndListBox))
show_listbox(This);
break;
}
case WM_KEYDOWN:
return ACEditSubclassProc_KeyDown(This, hwnd, uMsg, wParam, lParam);
case WM_CHAR:
case WM_UNICHAR:
if (wParam == This->no_fwd_char) return 0;
This->no_fwd_char = '\0';
/* Don't autocomplete at all on most control characters */
if (iscntrlW(wParam) && !(wParam >= '\b' && wParam <= '\r'))
break;
ret = CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
autocomplete_text(This, hwnd, (This->options & ACO_AUTOAPPEND) && wParam >= ' '
? autoappend_flag_yes : autoappend_flag_no);
return ret;
case WM_SETTEXT:
if (This->options & ACO_AUTOSUGGEST)
hide_listbox(This, This->hwndListBox, TRUE);
break;
case WM_CUT:
case WM_CLEAR:
case WM_UNDO:
ret = CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
autocomplete_text(This, hwnd, autoappend_flag_no);
return ret;
case WM_PASTE:
ret = CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
autocomplete_text(This, hwnd, (This->options & ACO_AUTOAPPEND)
? autoappend_flag_yes : autoappend_flag_no);
return ret;
case WM_MOUSEWHEEL:
if ((This->options & ACO_AUTOSUGGEST) && IsWindowVisible(This->hwndListBox))
return SendMessageW(This->hwndListBox, WM_MOUSEWHEEL, wParam, lParam);
break;
case WM_SETFONT:
if (This->hwndListBox)
SendMessageW(This->hwndListBox, WM_SETFONT, wParam, lParam);
break;
case WM_DESTROY:
{
WNDPROC proc = This->wpOrigEditProc;
SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)proc);
RemovePropW(hwnd, autocomplete_propertyW);
destroy_autocomplete_object(This);
return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
}
}
return CallWindowProcW(This->wpOrigEditProc, hwnd, uMsg, wParam, lParam);
}
static LRESULT APIENTRY ACLBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
IAutoCompleteImpl *This = (IAutoCompleteImpl *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
WCHAR *msg;
int sel, len;
switch (uMsg) {
case WM_MOUSEMOVE:
sel = SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, lParam);
SendMessageW(hwnd, LB_SETCURSEL, sel, 0);
break;
case WM_LBUTTONDOWN:
sel = SendMessageW(hwnd, LB_GETCURSEL, 0, 0);
if (sel < 0)
break;
len = SendMessageW(hwnd, LB_GETTEXTLEN, sel, 0);
if (!(msg = heap_alloc((len + 1) * sizeof(WCHAR))))
break;
len = SendMessageW(hwnd, LB_GETTEXT, sel, (LPARAM)msg);
set_text_and_selection(This, This->hwndEdit, msg, 0, len);
hide_listbox(This, hwnd, TRUE);
heap_free(msg);
break;
default:
return CallWindowProcW(This->wpOrigLBoxProc, hwnd, uMsg, wParam, lParam);
}
return 0;
}
static void create_listbox(IAutoCompleteImpl *This)
{
/* FIXME : The listbox should be resizable with the mouse. WS_THICKFRAME looks ugly */
This->hwndListBox = CreateWindowExW(0, WC_LISTBOXW, NULL,
WS_BORDER | WS_CHILD | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
0, 0, 0, 0, GetParent(This->hwndEdit), NULL, shell32_hInstance, NULL);
if (This->hwndListBox) {
HFONT edit_font;
This->wpOrigLBoxProc = (WNDPROC) SetWindowLongPtrW( This->hwndListBox, GWLP_WNDPROC, (LONG_PTR) ACLBoxSubclassProc);
SetWindowLongPtrW( This->hwndListBox, GWLP_USERDATA, (LONG_PTR)This);
SetParent(This->hwndListBox, HWND_DESKTOP);
/* Use the same font as the edit control, as it gets destroyed before it anyway */
edit_font = (HFONT)SendMessageW(This->hwndEdit, WM_GETFONT, 0, 0);
if (edit_font)
SendMessageW(This->hwndListBox, WM_SETFONT, (WPARAM)edit_font, FALSE);
}
else
This->options &= ~ACO_AUTOSUGGEST;
}
/**************************************************************************
* AutoComplete_QueryInterface
*/
static HRESULT WINAPI IAutoComplete2_fnQueryInterface(
IAutoComplete2 * iface,
REFIID riid,
LPVOID *ppvObj)
{
IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
TRACE("(%p)->(IID:%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
*ppvObj = NULL;
if (IsEqualIID(riid, &IID_IUnknown) ||
IsEqualIID(riid, &IID_IAutoComplete) ||
IsEqualIID(riid, &IID_IAutoComplete2))
{
*ppvObj = &This->IAutoComplete2_iface;
}
else if (IsEqualIID(riid, &IID_IAutoCompleteDropDown))
{
*ppvObj = &This->IAutoCompleteDropDown_iface;
}
if (*ppvObj)
{
IUnknown_AddRef((IUnknown*)*ppvObj);
TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
return S_OK;
}
WARN("unsupported interface: %s\n", debugstr_guid(riid));
return E_NOINTERFACE;
}
/******************************************************************************
* IAutoComplete2_fnAddRef
*/
static ULONG WINAPI IAutoComplete2_fnAddRef(
IAutoComplete2 * iface)
{
IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
ULONG refCount = InterlockedIncrement(&This->ref);
TRACE("(%p)->(%u)\n", This, refCount - 1);
return refCount;
}
/******************************************************************************
* IAutoComplete2_fnRelease
*/
static ULONG WINAPI IAutoComplete2_fnRelease(
IAutoComplete2 * iface)
{
IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
ULONG refCount = InterlockedDecrement(&This->ref);
TRACE("(%p)->(%u)\n", This, refCount + 1);
if (!refCount) {
TRACE("destroying IAutoComplete(%p)\n", This);
heap_free(This->quickComplete);
heap_free(This->txtbackup);
if (This->enumstr)
IEnumString_Release(This->enumstr);
if (This->aclist)
IACList_Release(This->aclist);
heap_free(This);
}
return refCount;
}
/******************************************************************************
* IAutoComplete2_fnEnable
*/
static HRESULT WINAPI IAutoComplete2_fnEnable(
IAutoComplete2 * iface,
BOOL fEnable)
{
IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
HRESULT hr = S_OK;
TRACE("(%p)->(%s)\n", This, (fEnable)?"true":"false");
This->enabled = fEnable;
return hr;
}
/******************************************************************************
* IAutoComplete2_fnInit
*/
static HRESULT WINAPI IAutoComplete2_fnInit(
IAutoComplete2 * iface,
HWND hwndEdit,
IUnknown *punkACL,
LPCOLESTR pwzsRegKeyPath,
LPCOLESTR pwszQuickComplete)
{
IAutoCompleteImpl *prev, *This = impl_from_IAutoComplete2(iface);
TRACE("(%p)->(%p, %p, %s, %s)\n",
This, hwndEdit, punkACL, debugstr_w(pwzsRegKeyPath), debugstr_w(pwszQuickComplete));
if (This->options & ACO_SEARCH) FIXME(" ACO_SEARCH not supported\n");
if (This->options & ACO_RTLREADING) FIXME(" ACO_RTLREADING not supported\n");
if (This->options & ACO_WORD_FILTER) FIXME(" ACO_WORD_FILTER not supported\n");
if (!hwndEdit || !punkACL)
return E_INVALIDARG;
if (This->initialized)
{
WARN("Autocompletion object is already initialized\n");
/* This->hwndEdit is set to NULL when the edit window is destroyed. */
return This->hwndEdit ? E_FAIL : E_UNEXPECTED;
}
if (FAILED (IUnknown_QueryInterface (punkACL, &IID_IEnumString, (LPVOID*)&This->enumstr))) {
WARN("No IEnumString interface\n");
return E_NOINTERFACE;
}
/* Prevent txtbackup from ever being NULL to simplify aclist_expand */
if ((This->txtbackup = heap_alloc_zero(sizeof(WCHAR))) == NULL)
{
IEnumString_Release(This->enumstr);
This->enumstr = NULL;
return E_OUTOFMEMORY;
}
if (FAILED (IUnknown_QueryInterface (punkACL, &IID_IACList, (LPVOID*)&This->aclist)))
This->aclist = NULL;
This->initialized = TRUE;
This->hwndEdit = hwndEdit;
/* If another AutoComplete object was previously assigned to this edit control,
release it but keep the same callback on the control, to avoid an infinite
recursive loop in ACEditSubclassProc while the property is set to this object */
prev = GetPropW(hwndEdit, autocomplete_propertyW);
SetPropW(hwndEdit, autocomplete_propertyW, This);
if (prev && prev->initialized) {
This->wpOrigEditProc = prev->wpOrigEditProc;
destroy_autocomplete_object(prev);
}
else
This->wpOrigEditProc = (WNDPROC) SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC, (LONG_PTR) ACEditSubclassProc);
/* Keep at least one reference to the object until the edit window is destroyed */
IAutoComplete2_AddRef(&This->IAutoComplete2_iface);
if (This->options & ACO_AUTOSUGGEST)
create_listbox(This);
if (pwzsRegKeyPath)
{
static const HKEY roots[] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
WCHAR *key, *value;
DWORD type, sz;
BYTE *qc;
HKEY hKey;
LSTATUS res;
size_t len;
UINT i;
/* pwszRegKeyPath contains the key as well as the value, so split it */
value = strrchrW(pwzsRegKeyPath, '\\');
len = value - pwzsRegKeyPath;
if (value && (key = heap_alloc((len+1) * sizeof(*key))) != NULL)
{
memcpy(key, pwzsRegKeyPath, len * sizeof(*key));
key[len] = '\0';
value++;
for (i = 0; i < ARRAY_SIZE(roots); i++)
{
if (RegOpenKeyExW(roots[i], key, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
continue;
sz = MAX_PATH * sizeof(WCHAR);
while ((qc = heap_alloc(sz)) != NULL)
{
res = RegQueryValueExW(hKey, value, NULL, &type, qc, &sz);
if (res == ERROR_SUCCESS && type == REG_SZ)
{
This->quickComplete = heap_realloc(qc, sz);
i = ARRAY_SIZE(roots);
break;
}
heap_free(qc);
if (res != ERROR_MORE_DATA || type != REG_SZ)
break;
}
RegCloseKey(hKey);
}
heap_free(key);
}
}
if (!This->quickComplete && pwszQuickComplete)
{
size_t len = strlenW(pwszQuickComplete)+1;
if ((This->quickComplete = heap_alloc(len * sizeof(WCHAR))) != NULL)
memcpy(This->quickComplete, pwszQuickComplete, len * sizeof(WCHAR));
}
return S_OK;
}
/**************************************************************************
* IAutoComplete2_fnGetOptions
*/
static HRESULT WINAPI IAutoComplete2_fnGetOptions(
IAutoComplete2 * iface,
DWORD *pdwFlag)
{
IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
HRESULT hr = S_OK;
TRACE("(%p) -> (%p)\n", This, pdwFlag);
*pdwFlag = This->options;
return hr;
}
/**************************************************************************
* IAutoComplete2_fnSetOptions
*/
static HRESULT WINAPI IAutoComplete2_fnSetOptions(
IAutoComplete2 * iface,
DWORD dwFlag)
{
IAutoCompleteImpl *This = impl_from_IAutoComplete2(iface);
DWORD changed = This->options ^ dwFlag;
HRESULT hr = S_OK;
TRACE("(%p) -> (0x%x)\n", This, dwFlag);
This->options = dwFlag;
if ((This->options & ACO_AUTOSUGGEST) && This->hwndEdit && !This->hwndListBox)
create_listbox(This);
else if (!(This->options & ACO_AUTOSUGGEST) && This->hwndListBox)
hide_listbox(This, This->hwndListBox, TRUE);
/* If ACO_FILTERPREFIXES changed we might have to reset the enumerator */
if ((changed & ACO_FILTERPREFIXES) && This->txtbackup)
{
if (get_text_prefix_filtering(This->txtbackup) != prefix_filtering_none)
IAutoCompleteDropDown_ResetEnumerator(&This->IAutoCompleteDropDown_iface);
}
return hr;
}
/**************************************************************************
* IAutoComplete2 VTable
*/
static const IAutoComplete2Vtbl acvt =
{
IAutoComplete2_fnQueryInterface,
IAutoComplete2_fnAddRef,
IAutoComplete2_fnRelease,
IAutoComplete2_fnInit,
IAutoComplete2_fnEnable,
/* IAutoComplete2 */
IAutoComplete2_fnSetOptions,
IAutoComplete2_fnGetOptions,
};
static HRESULT WINAPI IAutoCompleteDropDown_fnQueryInterface(IAutoCompleteDropDown *iface,
REFIID riid, LPVOID *ppvObj)
{
IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
return IAutoComplete2_QueryInterface(&This->IAutoComplete2_iface, riid, ppvObj);
}
static ULONG WINAPI IAutoCompleteDropDown_fnAddRef(IAutoCompleteDropDown *iface)
{
IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
return IAutoComplete2_AddRef(&This->IAutoComplete2_iface);
}
static ULONG WINAPI IAutoCompleteDropDown_fnRelease(IAutoCompleteDropDown *iface)
{
IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
return IAutoComplete2_Release(&This->IAutoComplete2_iface);
}
/**************************************************************************
* IAutoCompleteDropDown_fnGetDropDownStatus
*/
static HRESULT WINAPI IAutoCompleteDropDown_fnGetDropDownStatus(
IAutoCompleteDropDown *iface,
DWORD *pdwFlags,
LPWSTR *ppwszString)
{
IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
BOOL dropped;
TRACE("(%p) -> (%p, %p)\n", This, pdwFlags, ppwszString);
dropped = IsWindowVisible(This->hwndListBox);
if (pdwFlags)
*pdwFlags = (dropped ? ACDD_VISIBLE : 0);
if (ppwszString) {
if (dropped) {
int sel;
sel = SendMessageW(This->hwndListBox, LB_GETCURSEL, 0, 0);
if (sel >= 0)
{
DWORD len;
len = SendMessageW(This->hwndListBox, LB_GETTEXTLEN, sel, 0);
*ppwszString = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
SendMessageW(This->hwndListBox, LB_GETTEXT, sel, (LPARAM)*ppwszString);
}
else
*ppwszString = NULL;
}
else
*ppwszString = NULL;
}
return S_OK;
}
/**************************************************************************
* IAutoCompleteDropDown_fnResetEnumarator
*/
static HRESULT WINAPI IAutoCompleteDropDown_fnResetEnumerator(
IAutoCompleteDropDown *iface)
{
IAutoCompleteImpl *This = impl_from_IAutoCompleteDropDown(iface);
TRACE("(%p)\n", This);
if (This->hwndEdit)
{
free_enum_strs(This);
if ((This->options & ACO_AUTOSUGGEST) && IsWindowVisible(This->hwndListBox))
autocomplete_text(This, This->hwndEdit, autoappend_flag_displayempty);
}
return S_OK;
}
/**************************************************************************
* IAutoCompleteDropDown VTable
*/
static const IAutoCompleteDropDownVtbl acdropdownvt =
{
IAutoCompleteDropDown_fnQueryInterface,
IAutoCompleteDropDown_fnAddRef,
IAutoCompleteDropDown_fnRelease,
IAutoCompleteDropDown_fnGetDropDownStatus,
IAutoCompleteDropDown_fnResetEnumerator,
};
/**************************************************************************
* IAutoComplete_Constructor
*/
HRESULT WINAPI IAutoComplete_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
{
IAutoCompleteImpl *lpac;
HRESULT hr;
if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
return CLASS_E_NOAGGREGATION;
lpac = heap_alloc_zero(sizeof(*lpac));
if (!lpac)
return E_OUTOFMEMORY;
lpac->ref = 1;
lpac->IAutoComplete2_iface.lpVtbl = &acvt;
lpac->IAutoCompleteDropDown_iface.lpVtbl = &acdropdownvt;
lpac->enabled = TRUE;
lpac->options = ACO_AUTOAPPEND;
hr = IAutoComplete2_QueryInterface(&lpac->IAutoComplete2_iface, riid, ppv);
IAutoComplete2_Release(&lpac->IAutoComplete2_iface);
TRACE("-- (%p)->\n",lpac);
return hr;
}
|