source: trunk/src/user32/windlg.cpp@ 1346

Last change on this file since 1346 was 1346, checked in by sandervl, 26 years ago

GetDlgItem fix + printfs for extra MFC messages

File size: 21.3 KB
Line 
1/* $Id: windlg.cpp,v 1.7 1999-10-17 20:18:46 sandervl Exp $ */
2/*
3 * Win32 dialog apis for OS/2
4 *
5 * Copyright 1999 Sander van Leeuwen
6 *
7 * Parts based on Wine code (990815; window\dialog.c)
8 *
9 * Copyright 1993, 1994, 1996 Alexandre Julliard
10 *
11 * Project Odin Software License can be found in LICENSE.TXT
12 *
13 */
14#include <ctype.h>
15#include <wchar.h>
16#include <os2win.h>
17#include <misc.h>
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <limits.h>
22#include <errno.h>
23#include "win32wbase.h"
24#include "win32dlg.h"
25#include <heapstring.h>
26#include <win\drive.h>
27
28static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox, INT idStatic, UINT attrib, BOOL combo );
29static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox, INT idStatic, UINT attrib, BOOL combo );
30static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len, INT id, BOOL win32, BOOL unicode, BOOL combo );
31
32//******************************************************************************
33//******************************************************************************
34HWND WIN32API CreateDialogParamA(HINSTANCE hInst, LPCSTR lpszTemplate,
35 HWND hwndOwner, DLGPROC dlgproc,
36 LPARAM lParamInit)
37{
38 HANDLE hrsrc = FindResourceA( hInst, lpszTemplate, RT_DIALOGA );
39
40 if (!hrsrc)
41 return 0;
42
43 return CreateDialogIndirectParamA(hInst, (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc),
44 hwndOwner, dlgproc, lParamInit);
45}
46//******************************************************************************
47//******************************************************************************
48HWND WIN32API CreateDialogParamW(HINSTANCE hInst, LPCWSTR lpszTemplate,
49 HWND hwndOwner, DLGPROC dlgproc,
50 LPARAM lParamInit)
51{
52 HANDLE hrsrc = FindResourceW( hInst, lpszTemplate, RT_DIALOGW );
53
54 if (!hrsrc)
55 return 0;
56
57 return CreateDialogIndirectParamW(hInst, (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc),
58 hwndOwner, dlgproc, lParamInit);
59}
60//******************************************************************************
61//******************************************************************************
62HWND WIN32API CreateDialogIndirectParamA(HINSTANCE hInst,
63 LPCDLGTEMPLATEA dlgtemplate,
64 HWND hwndParent, DLGPROC dlgproc,
65 LPARAM lParamInit)
66{
67 Win32Dialog *dialog;
68
69 dprintf(("CreateDialogIndirectParamA: %x %x %x %x %x", hInst, dlgtemplate, hwndParent, dlgproc, lParamInit));
70
71 if (!dlgtemplate) return 0;
72
73 dialog = new Win32Dialog(hInst, (LPCSTR)dlgtemplate, hwndParent, dlgproc, lParamInit, FALSE);
74
75 if(dialog == NULL)
76 {
77 dprintf(("Win32Dialog creation failed!!"));
78 return 0;
79 }
80 if(GetLastError() != 0)
81 {
82 dprintf(("Win32Dialog error found!!"));
83 delete dialog;
84 return 0;
85 }
86 return dialog->getWindowHandle();
87}
88//******************************************************************************
89//******************************************************************************
90HWND WIN32API CreateDialogIndirectParamW(HINSTANCE hInst,
91 LPCDLGTEMPLATEW dlgtemplate,
92 HWND hwndParent, DLGPROC dlgproc,
93 LPARAM lParamInit)
94{
95 Win32Dialog *dialog;
96
97 dprintf(("CreateDialogIndirectParamW: %x %x %x %x %x", hInst, dlgtemplate, hwndParent, dlgproc, lParamInit));
98
99 if (!dlgtemplate) return 0;
100
101 dialog = new Win32Dialog(hInst, (LPCSTR)dlgtemplate, hwndParent, dlgproc, lParamInit, TRUE);
102
103 if(dialog == NULL)
104 {
105 dprintf(("Win32Dialog creation failed!!"));
106 return 0;
107 }
108 if(GetLastError() != 0)
109 {
110 dprintf(("Win32Dialog error found!!"));
111 delete dialog;
112 return 0;
113 }
114 return dialog->getWindowHandle();
115}
116//******************************************************************************
117//******************************************************************************
118INT WIN32API DialogBoxIndirectParamA(HINSTANCE hInst,
119 LPCDLGTEMPLATEA dlgtemplate,
120 HWND hwndParent, DLGPROC dlgproc,
121 LPARAM lParamInit)
122{
123 HWND hwnd = CreateDialogIndirectParamA(hInst, dlgtemplate, hwndParent, dlgproc,
124 lParamInit);
125 if (hwnd)
126 {
127 Win32Dialog *dialog;
128
129 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
130 if(!dialog || !dialog->IsDialog()) {
131 dprintf(("DialogBoxIndirectParamA, dialog %x not found", hwnd));
132 return 0;
133 }
134 return dialog->doDialogBox();
135 }
136 return -1;
137}
138//******************************************************************************
139//******************************************************************************
140INT WIN32API DialogBoxIndirectParamW(HINSTANCE hInst, LPCDLGTEMPLATEW dlgtemplate,
141 HWND hwndParent, DLGPROC dlgproc,
142 LPARAM lParamInit)
143{
144 HWND hwnd = CreateDialogIndirectParamW(hInst, dlgtemplate, hwndParent, dlgproc,
145 lParamInit);
146 if (hwnd)
147 {
148 Win32Dialog *dialog;
149
150 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
151 if(!dialog || !dialog->IsDialog()) {
152 dprintf(("DialogBoxIndirectParamW, dialog %x not found", hwnd));
153 return 0;
154 }
155 return dialog->doDialogBox();
156 }
157 return -1;
158}
159//******************************************************************************
160//******************************************************************************
161int WIN32API DialogBoxParamA(HINSTANCE hInst, LPCSTR lpszTemplate, HWND hwndOwner,
162 DLGPROC dlgproc, LPARAM lParamInit)
163{
164 HWND hwnd = CreateDialogParamA( hInst, lpszTemplate, hwndOwner, dlgproc, lParamInit);
165
166 if (hwnd)
167 {
168 Win32Dialog *dialog;
169
170 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
171 if(!dialog || !dialog->IsDialog()) {
172 dprintf(("DialogBoxParamA, dialog %x not found", hwnd));
173 return 0;
174 }
175 return dialog->doDialogBox();
176 }
177 return -1;
178}
179//******************************************************************************
180//******************************************************************************
181int WIN32API DialogBoxParamW(HINSTANCE hInst, LPCWSTR lpszTemplate, HWND hwndOwner,
182 DLGPROC dlgproc, LPARAM lParamInit)
183{
184 HWND hwnd = CreateDialogParamW( hInst, lpszTemplate, hwndOwner, dlgproc, lParamInit);
185
186 if (hwnd)
187 {
188 Win32Dialog *dialog;
189
190 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
191 if(!dialog || !dialog->IsDialog()) {
192 dprintf(("DialogBoxParamW, dialog %x not found", hwnd));
193 return 0;
194 }
195 return dialog->doDialogBox();
196 }
197 return -1;
198}
199//******************************************************************************
200//******************************************************************************
201BOOL WIN32API IsDlgButtonChecked( HWND hwnd, UINT id)
202{
203 dprintf(("USER32: IsDlgButtonChecked\n"));
204 //CB: get button state
205 return (BOOL)SendDlgItemMessageA(hwnd, id,BM_GETCHECK,0,0);
206}
207//******************************************************************************
208//******************************************************************************
209HWND WIN32API GetNextDlgTabItem(HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious)
210{
211 Win32Dialog *dialog;
212
213 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwndDlg);
214 if(!dialog || !dialog->IsDialog()) {
215 dprintf(("GetNextDlgTabItem, window %x not found", hwndDlg));
216 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
217 return 0;
218 }
219 dprintf(("USER32: GetNextDlgTabItem\n"));
220 return dialog->getNextDlgTabItem(hwndCtrl, fPrevious);
221}
222//******************************************************************************
223//Can be used for any parent-child pair
224//******************************************************************************
225HWND WIN32API GetDlgItem(HWND hwnd, int id)
226{
227 Win32BaseWindow *dlgcontrol, *window;
228
229 window = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
230 if(!window) {
231 dprintf(("GetDlgItem, window %x not found", hwnd));
232 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
233 return 0;
234 }
235 dlgcontrol = window->FindWindowById(id);
236 if(dlgcontrol) {
237 dprintf(("USER32: GetDlgItem %x %d returned %x\n", hwnd, id, dlgcontrol->getWindowHandle()));
238 return dlgcontrol->getWindowHandle();
239 }
240 dprintf(("USER32: GetDlgItem %x %d NOT FOUND!\n", hwnd, id));
241 return 0;
242}
243//******************************************************************************
244//******************************************************************************
245int WIN32API GetDlgCtrlID(HWND hwnd)
246{
247 Win32BaseWindow *dlgcontrol;
248
249 dlgcontrol = Win32BaseWindow::GetWindowFromHandle(hwnd);
250 if(!dlgcontrol) {
251 dprintf(("GetDlgCtrlID, control %x not found", hwnd));
252 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
253 return 0;
254 }
255 dprintf(("USER32: GetDlgCtrlID\n"));
256 return dlgcontrol->getWindowId();
257}
258//******************************************************************************
259//******************************************************************************
260BOOL WIN32API EndDialog(HWND hwnd, int retval)
261{
262 Win32Dialog *dialog;
263
264 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
265 if(!dialog || !dialog->IsDialog()) {
266 dprintf(("GetDlgItem, window %x not found", hwnd));
267 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
268 return 0;
269 }
270 dprintf(("USER32: EndDialog\n"));
271 return dialog->endDialog(retval);
272}
273//******************************************************************************
274//******************************************************************************
275BOOL WIN32API CheckDlgButton( HWND hwnd, int id, UINT check)
276{
277 dprintf(("USER32: CheckDlgButton\n"));
278 //CB: set button state
279 return (BOOL)SendDlgItemMessageA(hwnd, id, BM_SETCHECK, check,0);
280}
281//******************************************************************************
282//******************************************************************************
283UINT WIN32API GetDlgItemTextA(HWND hwnd, int id, LPSTR lpszName, UINT cch)
284{
285 return SendDlgItemMessageA( hwnd, id, WM_GETTEXT, cch, (LPARAM)lpszName);
286}
287//*****************************************************************************
288//*****************************************************************************
289UINT WIN32API GetDlgItemTextW(HWND hwnd, int id, LPWSTR lpszName, UINT cch)
290{
291 return SendDlgItemMessageW( hwnd, id, WM_GETTEXT, cch, (LPARAM)lpszName);
292}
293//******************************************************************************
294//******************************************************************************
295BOOL WIN32API SetDlgItemInt( HWND hwnd, int idControl, UINT uValue, BOOL fSigned)
296{
297 char str[20];
298
299 dprintf(("USER32: SetDlgItemInt\n"));
300
301 if (fSigned)
302 sprintf( str, "%d", (INT)uValue );
303 else sprintf( str, "%u", uValue );
304
305 return SendDlgItemMessageA( hwnd, idControl, WM_SETTEXT, 0, (LPARAM)str );
306}
307//******************************************************************************
308//******************************************************************************
309BOOL WIN32API SetDlgItemTextA( HWND hwnd, int id, LPCSTR lpszName)
310{
311 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpszName );
312}
313//******************************************************************************
314//******************************************************************************
315BOOL WIN32API SetDlgItemTextW( HWND hwnd, int id, LPCWSTR lpszName)
316{
317 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpszName );
318}
319//******************************************************************************
320//******************************************************************************
321UINT WIN32API GetDlgItemInt(HWND hwnd, INT id, BOOL *translated, BOOL fSigned)
322{
323 char str[30];
324 char * endptr;
325 long result = 0;
326
327 dprintf(("USER32: GetDlgItemInt\n"));
328 if (translated) *translated = FALSE;
329
330 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
331 return 0;
332
333 if (fSigned)
334 {
335 result = strtol( str, &endptr, 10 );
336 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
337 return 0;
338 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
339 return 0;
340 }
341 else
342 {
343 result = strtoul( str, &endptr, 10 );
344 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
345 return 0;
346 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
347 }
348 if (translated) *translated = TRUE;
349 return (UINT)result;
350}
351//******************************************************************************
352//******************************************************************************
353HWND WIN32API GetNextDlgGroupItem( HWND hwnd, HWND hwndCtrl, BOOL fPrevious)
354{
355 Win32Dialog *dialog;
356
357 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
358 if(!dialog || !dialog->IsDialog()) {
359 dprintf(("GetNextDlgGroupItem, window %x not found", hwnd));
360 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
361 return 0;
362 }
363 dprintf(("USER32: GetNextDlgGroupItem\n"));
364 return dialog->getNextDlgGroupItem(hwndCtrl, fPrevious);
365}
366/***********************************************************************
367 * GetDialogBaseUnits (USER.243) (USER32.233)
368 */
369DWORD WIN32API GetDialogBaseUnits(void)
370{
371 return Win32Dialog::GetDialogBaseUnits();
372}
373//******************************************************************************
374//******************************************************************************
375INT WIN32API DlgDirListA(HWND hDlg, LPSTR spec, INT idLBox, INT idStatic, UINT attrib )
376{
377 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
378}
379//******************************************************************************
380//******************************************************************************
381int WIN32API DlgDirListW(HWND hDlg, LPWSTR spec, INT idLBox, INT idStatic, UINT attrib )
382{
383 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
384}
385//******************************************************************************
386//******************************************************************************
387INT WIN32API DlgDirListComboBoxA(HWND hDlg, LPSTR spec, INT idCBox, INT idStatic, UINT attrib )
388{
389 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
390}
391//******************************************************************************
392//******************************************************************************
393INT WIN32API DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox, INT idStatic, UINT attrib )
394{
395 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );
396}
397//******************************************************************************
398//******************************************************************************
399BOOL WIN32API DlgDirSelectComboBoxExA(HWND hwnd, LPSTR str, INT len, INT id )
400{
401 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, TRUE );
402}
403//******************************************************************************
404//******************************************************************************
405BOOL WIN32API DlgDirSelectComboBoxExW(HWND hwnd, LPWSTR str, INT len, INT id)
406{
407 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, TRUE );
408}
409//******************************************************************************
410//******************************************************************************
411BOOL WIN32API DlgDirSelectExA(HWND hwnd, LPSTR str, INT len, INT id)
412{
413 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, FALSE );
414}
415//******************************************************************************
416//******************************************************************************
417BOOL WIN32API DlgDirSelectExW(HWND hwnd, LPWSTR str, INT len, INT id)
418{
419 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, FALSE );
420}
421/**********************************************************************
422 * DIALOG_DlgDirSelect
423 *
424 * Helper function for DlgDirSelect*
425 */
426static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
427 INT id, BOOL win32, BOOL unicode,
428 BOOL combo )
429{
430 char *buffer, *ptr;
431 INT item, size;
432 BOOL ret;
433 HWND listbox = GetDlgItem( hwnd, id );
434
435 if (!listbox) return FALSE;
436
437 item = SendMessageA(listbox, combo ? CB_GETCURSEL
438 : LB_GETCURSEL, 0, 0 );
439 if (item == LB_ERR) return FALSE;
440 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN
441 : LB_GETTEXTLEN, 0, 0 );
442 if (size == LB_ERR) return FALSE;
443
444 if (!(buffer = (char *)malloc( size+1 ))) return FALSE;
445
446 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT,
447 item, (LPARAM)buffer );
448
449 if ((ret = (buffer[0] == '['))) /* drive or directory */
450 {
451 if (buffer[1] == '-') /* drive */
452 {
453 buffer[3] = ':';
454 buffer[4] = 0;
455 ptr = buffer + 2;
456 }
457 else
458 {
459 buffer[strlen(buffer)-1] = '\\';
460 ptr = buffer + 1;
461 }
462 }
463 else ptr = buffer;
464
465 if (unicode) lstrcpynAtoW( (LPWSTR)str, ptr, len );
466 else lstrcpynA( str, ptr, len );
467
468 free( buffer );
469 return ret;
470}
471
472
473/**********************************************************************
474 * DIALOG_DlgDirList
475 *
476 * Helper function for DlgDirList*
477 */
478static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
479 INT idStatic, UINT attrib, BOOL combo )
480{
481 int drive;
482 HWND hwnd;
483 LPSTR orig_spec = spec;
484
485#define SENDMSG(msg,wparam,lparam) \
486 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
487 : SendMessageA( hwnd, msg, wparam, lparam ))
488
489 if (spec && spec[0] && (spec[1] == ':'))
490 {
491 drive = _toupper( spec[0] ) - 'A';
492 spec += 2;
493 if (!DRIVE_SetCurrentDrive( drive )) return FALSE;
494 }
495 else drive = DRIVE_GetCurrentDrive();
496
497 /* If the path exists and is a directory, chdir to it */
498 if (!spec || !spec[0] || DRIVE_Chdir( drive, spec )) spec = "*.*";
499 else
500 {
501 char *p, *p2;
502 p = spec;
503 if ((p2 = strrchr( p, '\\' ))) p = p2;
504 if ((p2 = strrchr( p, '/' ))) p = p2;
505 if (p != spec)
506 {
507 char sep = *p;
508 *p = 0;
509 if (!DRIVE_Chdir( drive, spec ))
510 {
511 *p = sep; /* Restore the original spec */
512 return FALSE;
513 }
514 spec = p + 1;
515 }
516 }
517
518 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
519 {
520 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
521 if (attrib & DDL_DIRECTORY)
522 {
523 if (!(attrib & DDL_EXCLUSIVE))
524 {
525 if (SENDMSG( combo ? CB_DIR : LB_DIR,
526 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
527 (LPARAM)spec ) == LB_ERR)
528 return FALSE;
529 }
530 if (SENDMSG( combo ? CB_DIR : LB_DIR,
531 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
532 (LPARAM)"*.*" ) == LB_ERR)
533 return FALSE;
534 }
535 else
536 {
537 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
538 (LPARAM)spec ) == LB_ERR)
539 return FALSE;
540 }
541 }
542
543 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
544 {
545 char temp[512], curpath[512];
546 int drive = DRIVE_GetCurrentDrive();
547 strcpy( temp, "A:\\" );
548 temp[0] += drive;
549 lstrcpynA( temp + 3, DRIVE_GetDosCwd(curpath, drive), sizeof(temp)-3 );
550 CharLowerA( temp );
551 /* Can't use PostMessage() here, because the string is on the stack */
552 SetDlgItemTextA( hDlg, idStatic, temp );
553 }
554
555 if (orig_spec && (spec != orig_spec))
556 {
557 /* Update the original file spec */
558 char *p = spec;
559 while ((*orig_spec++ = *p++));
560 }
561
562 return TRUE;
563#undef SENDMSG
564}
565
566
567/**********************************************************************
568 * DIALOG_DlgDirListW
569 *
570 * Helper function for DlgDirList*32W
571 */
572static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
573 INT idStatic, UINT attrib, BOOL combo )
574{
575 if (spec)
576 {
577 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
578 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
579 attrib, combo );
580 lstrcpyAtoW( spec, specA );
581 HeapFree( GetProcessHeap(), 0, specA );
582 return ret;
583 }
584 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
585}
586//******************************************************************************
587//******************************************************************************
588
Note: See TracBrowser for help on using the repository browser.