Release 941017
[wine/gsoc-2012-control.git] / controls / combo.c
blob0dc66fb4ed0292dfca40d9ca16eb1900f8f05b96
1 /*
2 * Interface code to COMBOBOX widget
4 * Copyright Martin Ayotte, 1993
6 */
8 static char Copyright[] = "Copyright Martin Ayotte, 1993";
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
15 #include "windows.h"
16 #include "combo.h"
17 #include "heap.h"
18 #include "win.h"
19 #include "prototypes.h"
20 #include "stddebug.h"
21 /* #define DEBUG_COMBO */
22 /* #undef DEBUG_COMBO */
23 #include "debug.h"
26 HBITMAP hComboBit = 0;
28 LPHEADCOMBO ComboGetStorageHeader(HWND hwnd);
29 int CreateComboStruct(HWND hwnd);
32 /***********************************************************************
33 * ComboWndProc
35 LONG ComboBoxWndProc( HWND hwnd, WORD message, WORD wParam, LONG lParam )
37 RECT rect;
38 int y, count;
39 int width, height;
40 WND *wndPtr;
41 LPHEADCOMBO lphc;
42 HDC hDC;
43 BITMAP bm;
44 char str[128];
45 PAINTSTRUCT paintstruct;
46 int style;
47 switch(message)
49 case WM_CREATE:
50 wndPtr = WIN_FindWndPtr(hwnd);
51 if (wndPtr == NULL) return 0;
52 dprintf_combo(stddeb,"Combo WM_CREATE %p !\n", lphc);
53 if (hComboBit == (HBITMAP)NULL)
54 hComboBit = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_COMBO));
55 GetObject(hComboBit, sizeof(BITMAP), (LPSTR)&bm);
56 wndPtr->dwStyle &= 0xFFFFFFFFL ^ (WS_VSCROLL | WS_HSCROLL);
57 GetWindowRect(hwnd, &rect);
58 width = rect.right - rect.left;
59 height = rect.bottom - rect.top;
60 SetWindowPos(hwnd, 0, 0, 0, width + bm.bmHeight, bm.bmHeight,
61 SWP_NOMOVE | SWP_NOZORDER);
62 CreateComboStruct(hwnd);
63 lphc = ComboGetStorageHeader(hwnd);
64 if (lphc == NULL) return 0;
65 if (wndPtr->dwStyle & CBS_SIMPLE)
66 /* lphc->hWndEdit = CreateWindow("EDIT", "", */
67 lphc->hWndEdit = CreateWindow("STATIC", "",
68 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | SS_LEFT,
69 0, 0, width - bm.bmHeight, bm.bmHeight,
70 hwnd, 1, wndPtr->hInstance, 0L);
71 else
72 lphc->hWndEdit = CreateWindow("STATIC", "",
73 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | SS_LEFT,
74 0, 0, width - bm.bmHeight, bm.bmHeight,
75 hwnd, 1, wndPtr->hInstance, 0L);
76 style=0;
77 if ((wndPtr->dwStyle & CBS_HASSTRINGS) == CBS_HASSTRINGS)
78 style |= LBS_HASSTRINGS;
79 if ((wndPtr->dwStyle & CBS_OWNERDRAWFIXED) ==CBS_OWNERDRAWFIXED)
80 style |= LBS_OWNERDRAWFIXED;
81 if ((wndPtr->dwStyle & CBS_OWNERDRAWVARIABLE)==CBS_OWNERDRAWVARIABLE)
82 style |= LBS_OWNERDRAWVARIABLE;
83 lphc->hWndLBox = CreateWindow("LISTBOX", "",
84 WS_POPUP | WS_BORDER | WS_VSCROLL | LBS_NOTIFY | style,
85 rect.left, rect.top + bm.bmHeight,
86 width, height, wndPtr->hwndParent, 0,
87 wndPtr->hInstance, (LPSTR)MAKELONG(0, hwnd));
88 ShowWindow(lphc->hWndLBox, SW_HIDE);
89 dprintf_combo(stddeb,"Combo Creation LBox=%X!\n",
90 lphc->hWndLBox);
91 return 0;
92 case WM_DESTROY:
93 lphc = ComboGetStorageHeader(hwnd);
94 if (lphc == 0) return 0;
96 DestroyWindow(lphc->hWndEdit);
98 DestroyWindow(lphc->hWndLBox);
99 free(lphc);
101 *((LPHEADCOMBO *)&wndPtr->wExtra[1]) = 0;
102 printf("Combo WM_DESTROY after clearing wExtra !\n");
104 dprintf_combo(stddeb,"Combo WM_DESTROY %p !\n", lphc);
105 return DefWindowProc( hwnd, message, wParam, lParam );
106 case WM_SHOWWINDOW:
107 dprintf_combo(stddeb,"ComboBox WM_SHOWWINDOW hWnd=%04X !\n",
108 hwnd);
109 if (!(wParam == 0 && lParam == 0L)) {
110 InvalidateRect(hwnd, NULL, TRUE);
112 break;
114 case WM_COMMAND:
115 wndPtr = WIN_FindWndPtr(hwnd);
116 lphc = ComboGetStorageHeader(hwnd);
117 if (lphc == NULL) return 0;
118 if (LOWORD(lParam) == lphc->hWndLBox) {
119 switch(HIWORD(lParam)) {
120 case LBN_SELCHANGE:
121 lphc->dwState = lphc->dwState & (CB_SHOWDROPDOWN ^ 0xFFFFFFFFL);
122 ShowWindow(lphc->hWndLBox, SW_HIDE);
123 y = SendMessage(lphc->hWndLBox, LB_GETCURSEL, 0, 0L);
124 if (y != LB_ERR) {
125 SendMessage(lphc->hWndLBox, LB_GETTEXT, (WORD)y, (LPARAM)str);
126 SendMessage(lphc->hWndEdit, WM_SETTEXT, (WORD)y, (LPARAM)str);
128 SendMessage(GetParent(hwnd), WM_COMMAND, wndPtr->wIDmenu,
129 MAKELONG(hwnd, CBN_SELCHANGE));
130 break;
131 case LBN_DBLCLK:
132 SendMessage(GetParent(hwnd), WM_COMMAND, wndPtr->wIDmenu,
133 MAKELONG(hwnd, CBN_DBLCLK));
134 break;
137 break;
138 case WM_LBUTTONDOWN:
139 dprintf_combo(stddeb,"Combo WM_LBUTTONDOWN wParam=%x lParam=%lX !\n", wParam, lParam);
140 GetClientRect(hwnd, &rect);
141 rect.left = rect.right - (rect.bottom - rect.top);
142 hDC = GetDC(hwnd);
143 InflateRect(&rect, -1, -1);
144 GRAPH_DrawReliefRect(hDC, &rect, 1, 1);
145 ReleaseDC(hwnd, hDC);
146 wndPtr = WIN_FindWndPtr(hwnd);
147 lphc = ComboGetStorageHeader(hwnd);
148 if (lphc == NULL) return 0;
149 lphc->dwState = lphc->dwState ^ CB_SHOWDROPDOWN;
150 if ((lphc->dwState & CB_SHOWDROPDOWN) == CB_SHOWDROPDOWN) {
151 ShowWindow(lphc->hWndLBox, SW_SHOW);
152 SetFocus(lphc->hWndLBox);
154 else {
155 dprintf_combo(stddeb,"before Combo Restore Focus !\n");
156 SetFocus(lphc->hWndEdit);
157 dprintf_combo(stddeb,"before Combo List Hide !\n");
158 ShowWindow(lphc->hWndLBox, SW_HIDE);
159 dprintf_combo(stddeb,"before Combo List GetCurSel !\n");
160 y = SendMessage(lphc->hWndLBox, LB_GETCURSEL, 0, 0L);
161 if (y != LB_ERR) {
162 dprintf_combo(stddeb,"before Combo List GetText !\n");
163 SendMessage(lphc->hWndLBox, LB_GETTEXT, (WORD)y, (LPARAM)str);
164 SendMessage(lphc->hWndEdit, WM_SETTEXT, (WORD)y, (LPARAM)str);
166 dprintf_combo(stddeb,"End of Combo List Hide !\n");
168 break;
169 case WM_LBUTTONUP:
170 dprintf_combo(stddeb,"Combo WM_LBUTTONUP wParam=%x lParam=%lX !\n", wParam, lParam);
171 GetClientRect(hwnd, &rect);
172 rect.left = rect.right - (rect.bottom - rect.top);
173 hDC = GetDC(hwnd);
174 InflateRect(&rect, -1, -1);
175 GRAPH_DrawReliefRect(hDC, &rect, 1, 0);
176 ReleaseDC(hwnd, hDC);
177 break;
178 case WM_KEYDOWN:
179 wndPtr = WIN_FindWndPtr(hwnd);
180 lphc = ComboGetStorageHeader(hwnd);
181 if (lphc == NULL) return 0;
182 y = SendMessage(lphc->hWndLBox, LB_GETCURSEL, 0, 0L);
183 count = SendMessage(lphc->hWndLBox, LB_GETCOUNT, 0, 0L);
184 dprintf_combo(stddeb,"COMBOBOX // GetKeyState(VK_MENU)=%d\n", GetKeyState(VK_MENU));
185 if (GetKeyState(VK_MENU) < 0) {
186 lphc->dwState = lphc->dwState ^ CB_SHOWDROPDOWN;
187 if ((lphc->dwState & CB_SHOWDROPDOWN) == CB_SHOWDROPDOWN) {
188 ShowWindow(lphc->hWndLBox, SW_SHOW);
189 SetFocus(lphc->hWndLBox);
191 else {
192 ShowWindow(lphc->hWndLBox, SW_HIDE);
193 y = SendMessage(lphc->hWndLBox, LB_GETCURSEL, 0, 0L);
194 if (y != LB_ERR) {
195 SendMessage(lphc->hWndLBox, LB_GETTEXT, (WORD)y, (LPARAM)str);
196 SendMessage(lphc->hWndEdit, WM_SETTEXT, (WORD)y, (LPARAM)str);
200 else {
201 switch(wParam) {
202 case VK_HOME:
203 y = 0;
204 break;
205 case VK_END:
206 y = count - 1;
207 break;
208 case VK_UP:
209 y--;
210 break;
211 case VK_DOWN:
212 y++;
213 break;
215 if (y < 0) y = 0;
216 if (y >= count) y = count - 1;
217 SendMessage(lphc->hWndLBox, LB_SETCURSEL, y, 0L);
218 SendMessage(lphc->hWndLBox, LB_GETTEXT, (WORD)y, (LPARAM)str);
219 SendMessage(lphc->hWndEdit, WM_SETTEXT, (WORD)y, (LPARAM)str);
220 SendMessage(GetParent(hwnd), WM_COMMAND, wndPtr->wIDmenu,
221 MAKELONG(hwnd, CBN_SELCHANGE));
223 break;
224 case WM_MEASUREITEM:
225 dprintf_combo(stddeb,"ComboBoxWndProc WM_MEASUREITEM !\n");
226 return(SendMessage(GetParent(hwnd), WM_MEASUREITEM, wParam, lParam));
227 case WM_CTLCOLOR:
228 return(SendMessage(GetParent(hwnd), WM_CTLCOLOR, wParam, lParam));
229 case WM_DRAWITEM:
230 return(SendMessage(GetParent(hwnd), WM_DRAWITEM, wParam, lParam));
231 case WM_PAINT:
232 GetClientRect(hwnd, &rect);
233 hDC = BeginPaint(hwnd, &paintstruct);
234 if (hComboBit != 0) {
235 GetObject(hComboBit, sizeof(BITMAP), (LPSTR)&bm);
236 GRAPH_DrawBitmap( hDC, hComboBit,
237 rect.right - bm.bmWidth, 0,
238 0, 0, bm.bmWidth, bm.bmHeight,
239 SRCCOPY );
241 EndPaint(hwnd, &paintstruct);
242 lphc = ComboGetStorageHeader(hwnd);
243 if (lphc == NULL) return 0;
244 InvalidateRect(lphc->hWndEdit, NULL, TRUE);
245 UpdateWindow(lphc->hWndEdit);
246 if ((lphc->dwState & CB_SHOWDROPDOWN) == CB_SHOWDROPDOWN) {
247 InvalidateRect(lphc->hWndLBox, NULL, TRUE);
248 UpdateWindow(lphc->hWndLBox);
250 break;
251 case WM_SETFOCUS:
252 lphc = ComboGetStorageHeader(hwnd);
253 if (lphc == NULL) return 0;
254 SetFocus(lphc->hWndEdit);
255 break;
256 case WM_KILLFOCUS:
257 lphc = ComboGetStorageHeader(hwnd);
258 if (lphc == NULL) return 0;
259 ShowWindow(lphc->hWndLBox, SW_HIDE);
260 y = SendMessage(lphc->hWndLBox, LB_GETCURSEL, 0, 0L);
261 if (y != LB_ERR) {
262 SendMessage(lphc->hWndLBox, LB_GETTEXT, (WORD)y, (LPARAM)str);
263 SendMessage(lphc->hWndEdit, WM_SETTEXT, (WORD)y, (LPARAM)str);
265 break;
266 case CB_ADDSTRING:
267 dprintf_combo(stddeb,"CB_ADDSTRING '%s' !\n", (LPSTR)lParam);
268 lphc = ComboGetStorageHeader(hwnd);
269 if (lphc == NULL) return 0;
270 return(SendMessage(lphc->hWndLBox, LB_ADDSTRING, wParam, lParam));
271 case CB_GETLBTEXT:
272 dprintf_combo(stddeb,"CB_GETLBTEXT #%u !\n", wParam);
273 lphc = ComboGetStorageHeader(hwnd);
274 if (lphc == NULL) return 0;
275 return(SendMessage(lphc->hWndLBox, LB_GETTEXT, wParam, lParam));
276 case CB_GETLBTEXTLEN:
277 dprintf_combo(stddeb,"CB_GETLBTEXTLEN !\n");
278 lphc = ComboGetStorageHeader(hwnd);
279 if (lphc == NULL) return 0;
280 return(SendMessage(lphc->hWndLBox, LB_GETTEXTLEN, wParam, lParam));
281 case CB_INSERTSTRING:
282 dprintf_combo(stddeb,"CB_INSERTSTRING '%s' !\n",(LPSTR)lParam);
283 lphc = ComboGetStorageHeader(hwnd);
284 if (lphc == NULL) return 0;
285 return(SendMessage(lphc->hWndLBox, LB_INSERTSTRING, wParam, lParam));
286 case CB_DELETESTRING:
287 dprintf_combo(stddeb,"CB_DELETESTRING #%u !\n", wParam);
288 lphc = ComboGetStorageHeader(hwnd);
289 if (lphc == NULL) return 0;
290 return(SendMessage(lphc->hWndLBox, LB_DELETESTRING, wParam, 0L));
291 case CB_RESETCONTENT:
292 dprintf_combo(stddeb,"CB_RESETCONTENT !\n");
293 lphc = ComboGetStorageHeader(hwnd);
294 if (lphc == NULL) return 0;
295 return(SendMessage(lphc->hWndLBox, LB_RESETCONTENT, 0, 0L));
296 case CB_DIR:
297 dprintf_combo(stddeb,"ComboBox CB_DIR !\n");
298 lphc = ComboGetStorageHeader(hwnd);
299 if (lphc == NULL) return 0;
300 return(SendMessage(lphc->hWndLBox, LB_DIR, wParam, lParam));
301 case CB_FINDSTRING:
302 lphc = ComboGetStorageHeader(hwnd);
303 return(SendMessage(lphc->hWndLBox, LB_FINDSTRING, wParam, lParam));
304 case CB_GETCOUNT:
305 lphc = ComboGetStorageHeader(hwnd);
306 if (lphc == NULL) return 0;
307 return(SendMessage(lphc->hWndLBox, LB_GETCOUNT, 0, 0L));
308 case CB_GETCURSEL:
309 dprintf_combo(stddeb,"ComboBox CB_GETCURSEL !\n");
310 lphc = ComboGetStorageHeader(hwnd);
311 if (lphc == NULL) return 0;
312 return(SendMessage(lphc->hWndLBox, LB_GETCURSEL, 0, 0L));
313 case CB_SETCURSEL:
314 dprintf_combo(stddeb,"ComboBox CB_SETCURSEL wParam=%X !\n", wParam);
315 lphc = ComboGetStorageHeader(hwnd);
316 if (lphc == NULL) return 0;
317 return(SendMessage(lphc->hWndLBox, LB_SETCURSEL, wParam, 0L));
318 case CB_GETEDITSEL:
319 dprintf_combo(stddeb,"ComboBox CB_GETEDITSEL !\n");
320 lphc = ComboGetStorageHeader(hwnd);
321 if (lphc == NULL) return 0;
322 /* return(SendMessage(lphc->hWndEdit, EM_GETSEL, 0, 0L)); */
323 break;
324 case CB_SETEDITSEL:
325 dprintf_combo(stddeb,"ComboBox CB_SETEDITSEL lParam=%lX !\n",
326 lParam);
327 lphc = ComboGetStorageHeader(hwnd);
328 if (lphc == NULL) return 0;
329 /* return(SendMessage(lphc->hWndEdit, EM_SETSEL, 0, lParam)); */
330 break;
331 case CB_SELECTSTRING:
332 dprintf_combo(stddeb,"ComboBox CB_SELECTSTRING !\n");
333 lphc = ComboGetStorageHeader(hwnd);
334 if (lphc == NULL) return 0;
335 break;
336 case CB_SHOWDROPDOWN:
337 dprintf_combo(stddeb,"ComboBox CB_SHOWDROPDOWN !\n");
338 lphc = ComboGetStorageHeader(hwnd);
339 if (lphc == NULL) return 0;
340 lphc->dwState = lphc->dwState | CB_SHOWDROPDOWN;
341 if (wParam != 0) {
342 ShowWindow(lphc->hWndLBox, SW_SHOW);
344 else {
345 lphc->dwState = lphc->dwState ^ CB_SHOWDROPDOWN;
346 ShowWindow(lphc->hWndLBox, SW_HIDE);
347 SendMessage(GetParent(hwnd), WM_COMMAND, wndPtr->wIDmenu,
348 MAKELONG(hwnd, CBN_DROPDOWN));
350 break;
351 case CB_GETITEMDATA:
352 dprintf_combo(stddeb,"ComboBox CB_GETITEMDATA wParam=%X !\n", wParam);
353 lphc = ComboGetStorageHeader(hwnd);
354 if (lphc == NULL) return 0;
355 return(SendMessage(lphc->hWndLBox, LB_GETITEMDATA, wParam, 0L));
356 break;
357 case CB_SETITEMDATA:
358 dprintf_combo(stddeb,"ComboBox CB_SETITEMDATA wParam=%04X lParam=%08lX!\n", wParam, lParam);
359 lphc = ComboGetStorageHeader(hwnd);
360 if (lphc == NULL) return 0;
361 return(SendMessage(lphc->hWndLBox, LB_SETITEMDATA, wParam, lParam));
362 break;
363 case CB_LIMITTEXT:
364 dprintf_combo(stddeb,"ComboBox CB_LIMITTEXT !\n");
365 lphc = ComboGetStorageHeader(hwnd);
366 if (lphc == NULL) return 0;
367 /* return(SendMessage(lphc->hWndEdit, EM_LIMITTEXT, wParam, 0L)); */
368 break;
370 default:
371 return DefWindowProc( hwnd, message, wParam, lParam );
373 return 0;
378 LPHEADCOMBO ComboGetStorageHeader(HWND hwnd)
380 WND *wndPtr;
381 LPHEADCOMBO lphc;
382 wndPtr = WIN_FindWndPtr(hwnd);
383 if (wndPtr == 0) {
384 fprintf(stderr,"Bad Window handle on ComboBox !\n");
385 return 0;
387 lphc = *((LPHEADCOMBO *)&wndPtr->wExtra[1]);
388 return lphc;
393 int CreateComboStruct(HWND hwnd)
395 WND *wndPtr;
396 LPHEADCOMBO lphc;
397 wndPtr = WIN_FindWndPtr(hwnd);
398 if (wndPtr == 0) {
399 fprintf(stderr,"Bad Window handle on ComboBox !\n");
400 return 0;
402 lphc = (LPHEADCOMBO)malloc(sizeof(HEADCOMBO));
403 *((LPHEADCOMBO *)&wndPtr->wExtra[1]) = lphc;
404 lphc->dwState = 0;
405 return TRUE;
410 /************************************************************************
411 * DlgDirSelectComboBox [USER.194]
413 BOOL DlgDirSelectComboBox(HWND hDlg, LPSTR lpStr, int nIDLBox)
415 fprintf(stdnimp,"DlgDirSelectComboBox(%04X, '%s', %d) \n",
416 hDlg, lpStr, nIDLBox);
420 /************************************************************************
421 * DlgDirListComboBox [USER.195]
423 int DlgDirListComboBox(HWND hDlg, LPSTR lpPathSpec,
424 int nIDLBox, int nIDStat, WORD wType)
426 HWND hWnd;
427 LPHEADCOMBO lphc;
428 dprintf_combo(stddeb,"DlgDirListComboBox(%04X, '%s', %d, %d, %04X) \n",
429 hDlg, lpPathSpec, nIDLBox, nIDStat, wType);
430 hWnd = GetDlgItem(hDlg, nIDLBox);
431 lphc = ComboGetStorageHeader(hWnd);
432 if (lphc == NULL) return 0;
433 SendMessage(lphc->hWndLBox, LB_RESETCONTENT, 0, 0L);
434 return SendMessage(lphc->hWndLBox, LB_DIR, wType, (DWORD)lpPathSpec);