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

Last change on this file since 1248 was 1248, checked in by cbratschi, 26 years ago

pmframe finished

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