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

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

Bugfix for SetPos bugfix, Edit controls now work, experimental fix in Edit control for VPBuddy

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