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

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

Window size + paint fix + dialog fixes

File size: 21.7 KB
Line 
1/* $Id: windlg.cpp,v 1.10 1999-10-31 01:14:44 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 * MapDialogRect32 (USER32.382)
201 */
202BOOL WIN32API MapDialogRect(HWND hwndDlg, LPRECT rect)
203{
204 Win32Dialog *dialog;
205
206 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwndDlg);
207 if(!dialog || !dialog->IsDialog()) {
208 dprintf(("MapDialogRect, window %x not found", hwndDlg));
209 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
210 return 0;
211 }
212 dprintf(("USER32: MapDialogRect\n"));
213 return dialog->MapDialogRect(rect);
214}
215//******************************************************************************
216//******************************************************************************
217BOOL WIN32API IsDlgButtonChecked( HWND hwnd, UINT id)
218{
219 dprintf(("USER32: IsDlgButtonChecked\n"));
220
221 return (BOOL)SendDlgItemMessageA(hwnd, id,BM_GETCHECK,0,0);
222}
223//******************************************************************************
224//******************************************************************************
225HWND WIN32API GetNextDlgTabItem(HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious)
226{
227 Win32BaseWindow *window;
228
229 window = (Win32BaseWindow*)Win32BaseWindow::GetWindowFromHandle(hwndDlg);
230 if(!window) {
231 dprintf(("GetNextDlgTabItem, window %x not found", hwndDlg));
232 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
233 return 0;
234 }
235 dprintf(("USER32: GetNextDlgTabItem\n"));
236 return window->getNextDlgTabItem(hwndCtrl, fPrevious);
237}
238//******************************************************************************
239//Can be used for any parent-child pair
240//******************************************************************************
241HWND WIN32API GetDlgItem(HWND hwnd, int id)
242{
243 Win32BaseWindow *dlgcontrol, *window;
244
245 window = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
246 if(!window) {
247 dprintf(("GetDlgItem, window %x not found", hwnd));
248 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
249 return 0;
250 }
251 dlgcontrol = window->FindWindowById(id);
252 if(dlgcontrol) {
253 dprintf(("USER32: GetDlgItem %x %d returned %x\n", hwnd, id, dlgcontrol->getWindowHandle()));
254 return dlgcontrol->getWindowHandle();
255 }
256 dprintf(("USER32: GetDlgItem %x %d NOT FOUND!\n", hwnd, id));
257 return 0;
258}
259//******************************************************************************
260//******************************************************************************
261int WIN32API GetDlgCtrlID(HWND hwnd)
262{
263 Win32BaseWindow *dlgcontrol;
264
265 dlgcontrol = Win32BaseWindow::GetWindowFromHandle(hwnd);
266 if(!dlgcontrol) {
267 dprintf(("GetDlgCtrlID, control %x not found", hwnd));
268 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
269 return 0;
270 }
271 dprintf(("USER32: GetDlgCtrlID\n"));
272 return dlgcontrol->getWindowId();
273}
274//******************************************************************************
275//******************************************************************************
276BOOL WIN32API EndDialog(HWND hwnd, int retval)
277{
278 Win32Dialog *dialog;
279
280 dialog = (Win32Dialog *)Win32BaseWindow::GetWindowFromHandle(hwnd);
281 if(!dialog || !dialog->IsDialog()) {
282 dprintf(("GetDlgItem, window %x not found", hwnd));
283 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
284 return 0;
285 }
286 dprintf(("USER32: EndDialog\n"));
287 return dialog->endDialog(retval);
288}
289//******************************************************************************
290//******************************************************************************
291BOOL WIN32API CheckDlgButton( HWND hwnd, int id, UINT check)
292{
293 dprintf(("USER32: CheckDlgButton\n"));
294 //CB: set button state
295 return (BOOL)SendDlgItemMessageA(hwnd, id, BM_SETCHECK, check,0);
296}
297//******************************************************************************
298//******************************************************************************
299UINT WIN32API GetDlgItemTextA(HWND hwnd, int id, LPSTR lpszName, UINT cch)
300{
301 return SendDlgItemMessageA( hwnd, id, WM_GETTEXT, cch, (LPARAM)lpszName);
302}
303//*****************************************************************************
304//*****************************************************************************
305UINT WIN32API GetDlgItemTextW(HWND hwnd, int id, LPWSTR lpszName, UINT cch)
306{
307 return SendDlgItemMessageW( hwnd, id, WM_GETTEXT, cch, (LPARAM)lpszName);
308}
309//******************************************************************************
310//******************************************************************************
311BOOL WIN32API SetDlgItemInt( HWND hwnd, int idControl, UINT uValue, BOOL fSigned)
312{
313 char str[20];
314
315 dprintf(("USER32: SetDlgItemInt\n"));
316
317 if (fSigned)
318 sprintf( str, "%d", (INT)uValue );
319 else sprintf( str, "%u", uValue );
320
321 return SendDlgItemMessageA( hwnd, idControl, WM_SETTEXT, 0, (LPARAM)str );
322}
323//******************************************************************************
324//******************************************************************************
325BOOL WIN32API SetDlgItemTextA( HWND hwnd, int id, LPCSTR lpszName)
326{
327 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpszName );
328}
329//******************************************************************************
330//******************************************************************************
331BOOL WIN32API SetDlgItemTextW( HWND hwnd, int id, LPCWSTR lpszName)
332{
333 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpszName );
334}
335//******************************************************************************
336//******************************************************************************
337UINT WIN32API GetDlgItemInt(HWND hwnd, INT id, BOOL *translated, BOOL fSigned)
338{
339 char str[30];
340 char * endptr;
341 long result = 0;
342
343 dprintf(("USER32: GetDlgItemInt\n"));
344 if (translated) *translated = FALSE;
345
346 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
347 return 0;
348
349 if (fSigned)
350 {
351 result = strtol( str, &endptr, 10 );
352 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
353 return 0;
354 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
355 return 0;
356 }
357 else
358 {
359 result = strtoul( str, &endptr, 10 );
360 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
361 return 0;
362 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
363 }
364 if (translated) *translated = TRUE;
365 return (UINT)result;
366}
367//******************************************************************************
368//******************************************************************************
369HWND WIN32API GetNextDlgGroupItem( HWND hwnd, HWND hwndCtrl, BOOL fPrevious)
370{
371 Win32BaseWindow *window;
372
373 window = Win32BaseWindow::GetWindowFromHandle(hwnd);
374 if(!window) {
375 dprintf(("GetNextDlgGroupItem, window %x not found", hwnd));
376 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
377 return 0;
378 }
379 dprintf(("USER32: GetNextDlgGroupItem\n"));
380 return window->getNextDlgGroupItem(hwndCtrl, fPrevious);
381}
382/***********************************************************************
383 * GetDialogBaseUnits (USER.243) (USER32.233)
384 */
385DWORD WIN32API GetDialogBaseUnits(void)
386{
387 return Win32Dialog::GetDialogBaseUnits();
388}
389//******************************************************************************
390//******************************************************************************
391INT WIN32API DlgDirListA(HWND hDlg, LPSTR spec, INT idLBox, INT idStatic, UINT attrib )
392{
393 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
394}
395//******************************************************************************
396//******************************************************************************
397int WIN32API DlgDirListW(HWND hDlg, LPWSTR spec, INT idLBox, INT idStatic, UINT attrib )
398{
399 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
400}
401//******************************************************************************
402//******************************************************************************
403INT WIN32API DlgDirListComboBoxA(HWND hDlg, LPSTR spec, INT idCBox, INT idStatic, UINT attrib )
404{
405 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
406}
407//******************************************************************************
408//******************************************************************************
409INT WIN32API DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox, INT idStatic, UINT attrib )
410{
411 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );
412}
413//******************************************************************************
414//******************************************************************************
415BOOL WIN32API DlgDirSelectComboBoxExA(HWND hwnd, LPSTR str, INT len, INT id )
416{
417 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, TRUE );
418}
419//******************************************************************************
420//******************************************************************************
421BOOL WIN32API DlgDirSelectComboBoxExW(HWND hwnd, LPWSTR str, INT len, INT id)
422{
423 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, TRUE );
424}
425//******************************************************************************
426//******************************************************************************
427BOOL WIN32API DlgDirSelectExA(HWND hwnd, LPSTR str, INT len, INT id)
428{
429 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, FALSE );
430}
431//******************************************************************************
432//******************************************************************************
433BOOL WIN32API DlgDirSelectExW(HWND hwnd, LPWSTR str, INT len, INT id)
434{
435 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, FALSE );
436}
437/**********************************************************************
438 * DIALOG_DlgDirSelect
439 *
440 * Helper function for DlgDirSelect*
441 */
442static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
443 INT id, BOOL win32, BOOL unicode,
444 BOOL combo )
445{
446 char *buffer, *ptr;
447 INT item, size;
448 BOOL ret;
449 HWND listbox = GetDlgItem( hwnd, id );
450
451 if (!listbox) return FALSE;
452
453 item = SendMessageA(listbox, combo ? CB_GETCURSEL
454 : LB_GETCURSEL, 0, 0 );
455 if (item == LB_ERR) return FALSE;
456 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN
457 : LB_GETTEXTLEN, 0, 0 );
458 if (size == LB_ERR) return FALSE;
459
460 if (!(buffer = (char *)malloc( size+1 ))) return FALSE;
461
462 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT,
463 item, (LPARAM)buffer );
464
465 if ((ret = (buffer[0] == '['))) /* drive or directory */
466 {
467 if (buffer[1] == '-') /* drive */
468 {
469 buffer[3] = ':';
470 buffer[4] = 0;
471 ptr = buffer + 2;
472 }
473 else
474 {
475 buffer[strlen(buffer)-1] = '\\';
476 ptr = buffer + 1;
477 }
478 }
479 else ptr = buffer;
480
481 if (unicode) lstrcpynAtoW( (LPWSTR)str, ptr, len );
482 else lstrcpynA( str, ptr, len );
483
484 free( buffer );
485 return ret;
486}
487
488
489/**********************************************************************
490 * DIALOG_DlgDirList
491 *
492 * Helper function for DlgDirList*
493 */
494static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
495 INT idStatic, UINT attrib, BOOL combo )
496{
497 int drive;
498 HWND hwnd;
499 LPSTR orig_spec = spec;
500
501#define SENDMSG(msg,wparam,lparam) \
502 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
503 : SendMessageA( hwnd, msg, wparam, lparam ))
504
505 if (spec && spec[0] && (spec[1] == ':'))
506 {
507 drive = _toupper( spec[0] ) - 'A';
508 spec += 2;
509 if (!DRIVE_SetCurrentDrive( drive )) return FALSE;
510 }
511 else drive = DRIVE_GetCurrentDrive();
512
513 /* If the path exists and is a directory, chdir to it */
514 if (!spec || !spec[0] || DRIVE_Chdir( drive, spec )) spec = "*.*";
515 else
516 {
517 char *p, *p2;
518 p = spec;
519 if ((p2 = strrchr( p, '\\' ))) p = p2;
520 if ((p2 = strrchr( p, '/' ))) p = p2;
521 if (p != spec)
522 {
523 char sep = *p;
524 *p = 0;
525 if (!DRIVE_Chdir( drive, spec ))
526 {
527 *p = sep; /* Restore the original spec */
528 return FALSE;
529 }
530 spec = p + 1;
531 }
532 }
533
534 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
535 {
536 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
537 if (attrib & DDL_DIRECTORY)
538 {
539 if (!(attrib & DDL_EXCLUSIVE))
540 {
541 if (SENDMSG( combo ? CB_DIR : LB_DIR,
542 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
543 (LPARAM)spec ) == LB_ERR)
544 return FALSE;
545 }
546 if (SENDMSG( combo ? CB_DIR : LB_DIR,
547 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
548 (LPARAM)"*.*" ) == LB_ERR)
549 return FALSE;
550 }
551 else
552 {
553 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
554 (LPARAM)spec ) == LB_ERR)
555 return FALSE;
556 }
557 }
558
559 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
560 {
561 char temp[512], curpath[512];
562 int drive = DRIVE_GetCurrentDrive();
563 strcpy( temp, "A:\\" );
564 temp[0] += drive;
565 lstrcpynA( temp + 3, DRIVE_GetDosCwd(curpath, drive), sizeof(temp)-3 );
566 CharLowerA( temp );
567 /* Can't use PostMessage() here, because the string is on the stack */
568 SetDlgItemTextA( hDlg, idStatic, temp );
569 }
570
571 if (orig_spec && (spec != orig_spec))
572 {
573 /* Update the original file spec */
574 char *p = spec;
575 while ((*orig_spec++ = *p++));
576 }
577
578 return TRUE;
579#undef SENDMSG
580}
581
582
583/**********************************************************************
584 * DIALOG_DlgDirListW
585 *
586 * Helper function for DlgDirList*32W
587 */
588static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
589 INT idStatic, UINT attrib, BOOL combo )
590{
591 if (spec)
592 {
593 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
594 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
595 attrib, combo );
596 lstrcpyAtoW( spec, specA );
597 HeapFree( GetProcessHeap(), 0, specA );
598 return ret;
599 }
600 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
601}
602//******************************************************************************
603//******************************************************************************
604
Note: See TracBrowser for help on using the repository browser.