source: trunk/src/helpers/dialog.c@ 79

Last change on this file since 79 was 79, checked in by umoeller, 24 years ago

Fixes to dialogs and other stuff.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 57.0 KB
Line 
1
2/*
3 *@@sourcefile dialog.c:
4 * contains PM helper functions to create and
5 * auto-format dialogs from control arrays in memory.
6 *
7 * See dlghCreateDlg for details.
8 *
9 * Usage: All PM programs.
10 *
11 * Function prefixes (new with V0.81):
12 * -- dlg* Dialog functions
13 *
14 * Note: Version numbering in this file relates to XWorkplace version
15 * numbering.
16 *
17 *@@added V0.9.9 (2001-04-01) [umoeller]
18 *@@header "helpers\dialog.h"
19 */
20
21/*
22 * Copyright (C) 2001 Ulrich M”ller.
23 * This file is part of the "XWorkplace helpers" source package.
24 * This is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published
26 * by the Free Software Foundation, in version 2 as it comes in the
27 * "COPYING" file of the XWorkplace main distribution.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 */
33
34#define OS2EMX_PLAIN_CHAR
35 // this is needed for "os2emx.h"; if this is defined,
36 // emx will define PSZ as _signed_ char, otherwise
37 // as unsigned char
38
39#define INCL_DOSERRORS
40
41#define INCL_WINWINDOWMGR
42#define INCL_WINFRAMEMGR
43#define INCL_WINDIALOGS
44#define INCL_WININPUT
45#define INCL_WINBUTTONS
46#define INCL_WINSTATICS
47#define INCL_WINSYS
48
49#define INCL_GPIPRIMITIVES
50#define INCL_GPIBITMAPS
51#define INCL_GPILCIDS
52#include <os2.h>
53
54#include <stdlib.h>
55#include <string.h>
56#include <stdio.h>
57
58#include "setup.h" // code generation and debugging options
59
60#include "helpers\comctl.h"
61#include "helpers\dialog.h"
62#include "helpers\gpih.h"
63#include "helpers\linklist.h"
64#include "helpers\standards.h"
65#include "helpers\stringh.h"
66#include "helpers\winh.h"
67
68/*
69 *@@category: Helpers\PM helpers\Dialog templates
70 */
71
72/* ******************************************************************
73 *
74 * Private declarations
75 *
76 ********************************************************************/
77
78/*
79 *@@ DLGPRIVATE:
80 * private data to the dlg manager, allocated
81 * by dlghCreateDlg. This is what is really
82 * used, even though the prototype only
83 * declares DIALOGDATA.
84 */
85
86typedef struct _DLGPRIVATE
87{
88 // public data
89 HWND hwndDlg;
90
91 // definition data (private)
92 LINKLIST llTables;
93
94 HWND hwndFirstFocus;
95
96 POINTL ptlTotalOfs;
97
98 LINKLIST llControls; // linked list of all PCOLUMNDEF structs,
99 // in the order in which windows were
100 // created
101
102 const char *pcszControlsFont; // from dlghCreateDlg
103
104 HPS hps;
105 /* const char *pcszFontLast;
106 LONG lcidLast; */
107} DLGPRIVATE, *PDLGPRIVATE;
108
109typedef struct _COLUMNDEF *PCOLUMNDEF;
110typedef struct _ROWDEF *PROWDEF;
111typedef struct _TABLEDEF *PTABLEDEF;
112
113/*
114 *@@ CONTROLPOS:
115 * control position. We don't want to use SWP.
116 */
117
118typedef struct _CONTROLPOS
119{
120 LONG x,
121 y,
122 cx,
123 cy;
124} CONTROLPOS, *PCONTROLPOS;
125
126/*
127 *@@ COLUMNDEF:
128 * representation of a table column.
129 * This is stored in a linked list in ROWDEF.
130 *
131 * A table column represents either a PM control
132 * window or another table, which may therefore
133 * be nested.
134 */
135
136typedef struct _COLUMNDEF
137{
138 PROWDEF pOwningRow; // row whose linked list this column belongs to
139
140 BOOL fIsNestedTable; // if TRUE, pvDefinition points to a nested TABLEDEF;
141 // if FALSE, pvDefinition points to a CONTROLDEF as
142 // specified by the user
143
144 PVOID pvDefinition; // either a PTABLEDEF or a PCONTROLDEF
145
146 CONTROLPOS cpControl, // real pos and size of control
147 cpColumn; // pos and size of column; can be wider, spacings applied
148
149 HWND hwndControl; // created control; NULLHANDLE for tables always
150
151} COLUMNDEF;
152
153/*
154 *@@ ROWDEF:
155 *
156 */
157
158typedef struct _ROWDEF
159{
160 PTABLEDEF pOwningTable; // table whose linked list this row belongs to
161
162 LINKLIST llColumns; // contains COLUMNDEF structs, no auto-free
163
164 ULONG flRowFormat; // one of:
165 // -- ROW_VALIGN_BOTTOM 0x0000
166 // -- ROW_VALIGN_CENTER 0x0001
167 // -- ROW_VALIGN_TOP 0x0002
168
169 CONTROLPOS cpRow;
170
171} ROWDEF;
172
173/*
174 *@@ TABLEDEF:
175 *
176 */
177
178typedef struct _TABLEDEF
179{
180 LINKLIST llRows; // contains ROWDEF structs, no auto-free
181
182 PCONTROLDEF pCtlDef; // if != NULL, we create a PM control around the table
183
184 CONTROLPOS cpTable;
185
186} TABLEDEF;
187
188/*
189 *@@ PROCESSMODE:
190 *
191 */
192
193typedef enum _PROCESSMODE
194{
195 PROCESS_CALC_SIZES, // step 1
196 PROCESS_CALC_POSITIONS, // step 3
197 PROCESS_CREATE_CONTROLS // step 4
198} PROCESSMODE;
199
200/* ******************************************************************
201 *
202 * Worker routines
203 *
204 ********************************************************************/
205
206#define PM_GROUP_SPACING_X 20
207#define PM_GROUP_SPACING_TOP 30
208
209VOID ProcessTable(PTABLEDEF pTableDef,
210 const CONTROLPOS *pcpTable,
211 PROCESSMODE ProcessMode,
212 PDLGPRIVATE pDlgData);
213
214/*
215 *@@ CalcAutoSizeText:
216 *
217 *@@changed V0.9.12 (2001-05-31) [umoeller]: fixed various things with statics
218 *@@changed V0.9.12 (2001-05-31) [umoeller]: fixed broken fonts
219 */
220
221VOID CalcAutoSizeText(PCONTROLDEF pControlDef,
222 BOOL fMultiLine, // in: if TRUE, multiple lines
223 PSIZEL pszlAuto, // out: computed size
224 PDLGPRIVATE pDlgData)
225{
226 BOOL fFind = FALSE;
227 RECTL rclText;
228 LONG lcid = 0;
229
230 const char *pcszFontThis = pControlDef->pcszFont;
231 // can be NULL,
232 // or CTL_COMMON_FONT
233
234 if (pcszFontThis == CTL_COMMON_FONT)
235 pcszFontThis = pDlgData->pcszControlsFont;
236
237 if (!pDlgData->hps)
238 pDlgData->hps = WinGetPS(pDlgData->hwndDlg);
239
240 if (pcszFontThis)
241 {
242 FONTMETRICS fm;
243 LONG lPointSize = 0;
244
245 // create new font
246 lcid = gpihFindPresFont(NULLHANDLE, // no window yet
247 FALSE,
248 pDlgData->hps,
249 pcszFontThis,
250 &fm,
251 &lPointSize);
252 GpiSetCharSet(pDlgData->hps, lcid);
253 if (fm.fsDefn & FM_DEFN_OUTLINE)
254 gpihSetPointSize(pDlgData->hps, lPointSize);
255
256 pszlAuto->cy = fm.lMaxBaselineExt + fm.lExternalLeading;
257 }
258
259 // ok, we FINALLY have a font now...
260 // get the control string and see how much space it needs
261 if (pControlDef->pcszText)
262 {
263 // do we have multiple lines?
264 if (fMultiLine)
265 {
266 RECTL rcl = {0, 0, 0, 0};
267 if (pControlDef->szlControlProposed.cx != -1)
268 rcl.xRight = pControlDef->szlControlProposed.cx; // V0.9.12 (2001-05-31) [umoeller]
269 else
270 rcl.xRight = winhQueryScreenCX() * 2 / 3;
271 if (pControlDef->szlControlProposed.cy != -1)
272 rcl.yTop = pControlDef->szlControlProposed.cy; // V0.9.12 (2001-05-31) [umoeller]
273 else
274 rcl.yTop = winhQueryScreenCY() * 2 / 3;
275
276 winhDrawFormattedText(pDlgData->hps,
277 &rcl,
278 pControlDef->pcszText,
279 DT_LEFT | DT_TOP | DT_WORDBREAK | DT_QUERYEXTENT);
280 pszlAuto->cx = rcl.xRight - rcl.xLeft;
281 pszlAuto->cy = rcl.yTop - rcl.yBottom;
282 }
283 else
284 {
285 POINTL aptl[TXTBOX_COUNT];
286 GpiQueryTextBox(pDlgData->hps,
287 strlen(pControlDef->pcszText),
288 (PCH)pControlDef->pcszText,
289 TXTBOX_COUNT,
290 aptl);
291 pszlAuto->cx = aptl[TXTBOX_TOPRIGHT].x - aptl[TXTBOX_BOTTOMLEFT].x;
292 }
293 }
294
295 /* if (lcid)
296 {
297 GpiSetCharSet(pDlgData->hps, LCID_DEFAULT);
298 GpiDeleteSetId(pDlgData->hps, lcid);
299 } */
300
301}
302
303/*
304 *@@ CalcAutoSize:
305 *
306 *@@changed V0.9.12 (2001-05-31) [umoeller]: fixed various things with statics
307 */
308
309VOID CalcAutoSize(PCONTROLDEF pControlDef,
310 PSIZEL pszlAuto, // out: computed size
311 PDLGPRIVATE pDlgData)
312{
313 // dumb defaults
314 pszlAuto->cx = 100;
315 pszlAuto->cy = 30;
316
317 switch ((ULONG)pControlDef->pcszClass)
318 {
319 case 0xffff0003L: // WC_BUTTON:
320 CalcAutoSizeText(pControlDef,
321 FALSE, // no multiline
322 pszlAuto,
323 pDlgData);
324 if (pControlDef->flStyle & ( BS_AUTOCHECKBOX
325 | BS_AUTORADIOBUTTON
326 | BS_AUTO3STATE
327 | BS_3STATE
328 | BS_CHECKBOX
329 | BS_RADIOBUTTON))
330 pszlAuto->cx += 20; // @@todo
331 else if (pControlDef->flStyle & BS_BITMAP)
332 ;
333 else if (pControlDef->flStyle & (BS_ICON | BS_MINIICON))
334 ;
335 // we can't test for BS_PUSHBUTTON because that's 0x0000
336 else if (!(pControlDef->flStyle & BS_USERBUTTON))
337 {
338 pszlAuto->cx += (2 * WinQuerySysValue(HWND_DESKTOP, SV_CXBORDER) + 15);
339 pszlAuto->cy += (2 * WinQuerySysValue(HWND_DESKTOP, SV_CYBORDER) + 15);
340 }
341 break;
342
343 case 0xffff0005L: // WC_STATIC:
344 if ((pControlDef->flStyle & 0x0F) == SS_TEXT)
345 CalcAutoSizeText(pControlDef,
346 ((pControlDef->flStyle & DT_WORDBREAK) != 0),
347 pszlAuto,
348 pDlgData);
349 else if ((pControlDef->flStyle & 0x0F) == SS_BITMAP)
350 {
351 HBITMAP hbm = (HBITMAP)pControlDef->pcszText;
352 if (hbm)
353 {
354 BITMAPINFOHEADER2 bmih2;
355 ZERO(&bmih2);
356 bmih2.cbFix = sizeof(bmih2);
357 if (GpiQueryBitmapInfoHeader(hbm,
358 &bmih2))
359 {
360 pszlAuto->cx = bmih2.cx;
361 pszlAuto->cy = bmih2.cy;
362 }
363 }
364 }
365 else if ((pControlDef->flStyle & 0x0F) == SS_ICON)
366 {
367 pszlAuto->cx = WinQuerySysValue(HWND_DESKTOP, SV_CXICON);
368 pszlAuto->cy = WinQuerySysValue(HWND_DESKTOP, SV_CYICON);
369 }
370 break;
371 }
372}
373
374/*
375 *@@ ProcessColumn:
376 * processes a column, which per definition is either
377 * a control or a nested subtable.
378 *
379 * A column is part of a row, which in turn is part
380 * of a table. There can be several columns in a row,
381 * and several rows in a table.
382 *
383 * Since tables may be specified as columns, it is
384 * possible to produce complex dialog layouts by
385 * nesting tables.
386 *
387 * This does the following:
388 *
389 * -- PROCESS_CALC_SIZES: size is taken from control def,
390 * or for tables, this produces a recursive ProcessTable
391 * call.
392 * Preconditions: none.
393 *
394 * -- PROCESS_CALC_POSITIONS: position of each column
395 * is taken from *plX, which is increased by the
396 * column width by this call.
397 *
398 * Preconditions: Owning row must already have its
399 * y position properly set, or we can't compute
400 * ours. Besides, plX must point to the current X
401 * in the row and will be incremented by the columns
402 * size here.
403 *
404 * -- PROCESS_CREATE_CONTROLS: well, creates the controls.
405 *
406 * For tables, this recurses again. If the table has
407 * a string assigned, this also produces a group box
408 * after the recursion.
409 *
410 *@@changed V0.9.12 (2001-05-31) [umoeller]: added control data
411 *@@changed V0.9.12 (2001-05-31) [umoeller]: fixed font problems
412 */
413
414VOID ProcessColumn(PCOLUMNDEF pColumnDef,
415 PROWDEF pOwningRow, // in: current row from ProcessRow
416 PROCESSMODE ProcessMode, // in: processing mode (see ProcessAll)
417 PLONG plX, // in/out: PROCESS_CALC_POSITIONS only
418 PDLGPRIVATE pDlgData)
419{
420 pColumnDef->pOwningRow = pOwningRow;
421
422 switch (ProcessMode)
423 {
424 /*
425 * PROCESS_CALC_SIZES:
426 * step 1.
427 */
428
429 case PROCESS_CALC_SIZES:
430 {
431 ULONG ulXSpacing = 0,
432 ulYSpacing = 0;
433 if (pColumnDef->fIsNestedTable)
434 {
435 // nested table: recurse!!
436 PTABLEDEF pTableDef = (PTABLEDEF)pColumnDef->pvDefinition;
437 ProcessTable(pTableDef,
438 NULL,
439 ProcessMode,
440 pDlgData);
441
442 // store the size of the sub-table
443 pColumnDef->cpControl.cx = pTableDef->cpTable.cx;
444 pColumnDef->cpControl.cy = pTableDef->cpTable.cy;
445
446 // should we create a PM control around the table?
447 if (pTableDef->pCtlDef)
448 {
449 // yes: make this wider
450 ulXSpacing = (2 * PM_GROUP_SPACING_X);
451 ulYSpacing = (PM_GROUP_SPACING_X + PM_GROUP_SPACING_TOP);
452 }
453 }
454 else
455 {
456 // no nested table, but control:
457 PCONTROLDEF pControlDef = (PCONTROLDEF)pColumnDef->pvDefinition;
458 PSIZEL pszl = &pControlDef->szlControlProposed;
459 SIZEL szlAuto;
460
461 if ( (pszl->cx == -1)
462 || (pszl->cy == -1)
463 )
464 {
465 CalcAutoSize(pControlDef,
466 &szlAuto,
467 pDlgData);
468 }
469
470 if (pszl->cx == -1)
471 pColumnDef->cpControl.cx = szlAuto.cx;
472 else
473 pColumnDef->cpControl.cx = pszl->cx;
474
475 if (pszl->cy == -1)
476 pColumnDef->cpControl.cy = szlAuto.cy;
477 else
478 pColumnDef->cpControl.cy = pszl->cy;
479
480 // @@todo hack sizes
481
482 ulXSpacing = ulYSpacing = (2 * pControlDef->ulSpacing);
483 }
484
485 pColumnDef->cpColumn.cx = pColumnDef->cpControl.cx
486 + ulXSpacing;
487 pColumnDef->cpColumn.cy = pColumnDef->cpControl.cy
488 + ulYSpacing;
489 break; }
490
491 /*
492 * PROCESS_CALC_POSITIONS:
493 * step 2.
494 */
495
496 case PROCESS_CALC_POSITIONS:
497 {
498 // calculate column position: this includes spacing
499 ULONG ulSpacing = 0;
500
501 // column position = *plX on ProcessRow stack
502 pColumnDef->cpColumn.x = *plX;
503 pColumnDef->cpColumn.y = pOwningRow->cpRow.y;
504
505 // check vertical alignment of row;
506 // we might need to increase column y
507 switch (pOwningRow->flRowFormat & ROW_VALIGN_MASK)
508 {
509 // case ROW_VALIGN_BOTTOM: // do nothing
510
511 case ROW_VALIGN_CENTER:
512 if (pColumnDef->cpColumn.cy < pOwningRow->cpRow.cy)
513 pColumnDef->cpColumn.y
514 += ( (pOwningRow->cpRow.cy - pColumnDef->cpColumn.cy)
515 / 2);
516 break;
517
518 case ROW_VALIGN_TOP:
519 if (pColumnDef->cpColumn.cy < pOwningRow->cpRow.cy)
520 pColumnDef->cpColumn.y
521 += (pOwningRow->cpRow.cy - pColumnDef->cpColumn.cy);
522 break;
523 }
524
525 if (pColumnDef->fIsNestedTable)
526 {
527 PTABLEDEF pTableDef = (PTABLEDEF)pColumnDef->pvDefinition;
528 // should we create a PM control around the table?
529 if (pTableDef->pCtlDef)
530 // yes:
531 ulSpacing = PM_GROUP_SPACING_X;
532 }
533 else
534 {
535 // no nested table, but control:
536 PCONTROLDEF pControlDef = (PCONTROLDEF)pColumnDef->pvDefinition;
537 ulSpacing = pControlDef->ulSpacing;
538 }
539
540 // increase plX by column width
541 *plX += pColumnDef->cpColumn.cx;
542
543 // calculate CONTROL pos from COLUMN pos by applying spacing
544 pColumnDef->cpControl.x = pColumnDef->cpColumn.x
545 + ulSpacing;
546 pColumnDef->cpControl.y = pColumnDef->cpColumn.y
547 + ulSpacing;
548
549 if (pColumnDef->fIsNestedTable)
550 {
551 // nested table:
552 PTABLEDEF pTableDef = (PTABLEDEF)pColumnDef->pvDefinition;
553
554 // recurse!! to create windows for the sub-table
555 ProcessTable(pTableDef,
556 &pColumnDef->cpControl, // start pos for new table
557 ProcessMode,
558 pDlgData);
559 }
560 break; }
561
562 /*
563 * PROCESS_CREATE_CONTROLS:
564 * step 3.
565 */
566
567 case PROCESS_CREATE_CONTROLS:
568 {
569 PCONTROLPOS pcp = NULL;
570 PCONTROLDEF pControlDef = NULL;
571 const char *pcszTitle = NULL;
572 ULONG flStyle = 0;
573 LHANDLE lHandleSet = NULLHANDLE;
574 ULONG flOld = 0;
575
576 if (pColumnDef->fIsNestedTable)
577 {
578 // nested table:
579 PTABLEDEF pTableDef = (PTABLEDEF)pColumnDef->pvDefinition;
580
581 // recurse!!
582 ProcessTable(pTableDef,
583 NULL,
584 ProcessMode,
585 pDlgData);
586
587 // should we create a PM control around the table?
588 // (do this AFTER the other controls from recursing,
589 // otherwise the stupid container doesn't show up)
590 if (pTableDef->pCtlDef)
591 {
592 // yes:
593 pcp = &pColumnDef->cpColumn; // !! not control
594 pControlDef = pTableDef->pCtlDef;
595 pcszTitle = pControlDef->pcszText;
596 flStyle = pControlDef->flStyle;
597 }
598 }
599 else
600 {
601 // no nested table, but control:
602 pControlDef = (PCONTROLDEF)pColumnDef->pvDefinition;
603 pcp = &pColumnDef->cpControl;
604 pcszTitle = pControlDef->pcszText;
605 flStyle = pControlDef->flStyle;
606
607 // change the title if this is a static with SS_BITMAP;
608 // we have used a HBITMAP in there!
609 if ( ((ULONG)pControlDef->pcszClass == 0xffff0005L) // WC_STATIC:
610 && ( ((flStyle & 0x0F) == SS_BITMAP)
611 || ((flStyle & 0x0F) == SS_ICON)
612 )
613 )
614 {
615 // change style flag to not use SS_BITMAP nor SS_ICON;
616 // control creation fails otherwise (stupid, stupid PM)
617 flOld = flStyle;
618 flStyle = ((flStyle & ~0x0F) | SS_FGNDFRAME);
619 pcszTitle = "";
620 lHandleSet = (LHANDLE)pControlDef->pcszText;
621 }
622 }
623
624 if (pcp && pControlDef)
625 {
626 // create something:
627 // PPRESPARAMS ppp = NULL;
628
629 const char *pcszFont = pControlDef->pcszFont;
630 // can be NULL, or CTL_COMMON_FONT
631 if (pcszFont == CTL_COMMON_FONT)
632 pcszFont = pDlgData->pcszControlsFont;
633
634 /* if (pcszFont)
635 winhStorePresParam(&ppp,
636 PP_FONTNAMESIZE,
637 strlen(pcszFont),
638 (PVOID)pcszFont); */
639
640 pColumnDef->hwndControl
641 = WinCreateWindow(pDlgData->hwndDlg, // parent
642 (PSZ)pControlDef->pcszClass,
643 (pcszTitle) // hacked
644 ? (PSZ)pcszTitle
645 : "",
646 flStyle, // hacked
647 pcp->x + pDlgData->ptlTotalOfs.x,
648 pcp->y + pDlgData->ptlTotalOfs.y,
649 pcp->cx,
650 pcp->cy,
651 pDlgData->hwndDlg, // owner
652 HWND_BOTTOM,
653 pControlDef->usID,
654 pControlDef->pvCtlData,
655 NULL); // ppp);
656
657 if ((pColumnDef->hwndControl) && (lHandleSet))
658 {
659 // subclass the damn static
660 if ((flOld & 0x0F) == SS_ICON)
661 // this was a static:
662 ctlPrepareStaticIcon(pColumnDef->hwndControl,
663 1);
664 else
665 // this was a bitmap:
666 ctlPrepareStretchedBitmap(pColumnDef->hwndControl,
667 TRUE);
668
669 WinSendMsg(pColumnDef->hwndControl,
670 SM_SETHANDLE,
671 (MPARAM)lHandleSet,
672 0);
673 }
674 else
675 if (pcszFont)
676 // we must set the font explicitly here...
677 // doesn't always work with WinCreateWindow
678 // presparams parameter, for some reason
679 // V0.9.12 (2001-05-31) [umoeller]
680 winhSetWindowFont(pColumnDef->hwndControl,
681 pcszFont);
682
683 if (pColumnDef->hwndControl)
684 {
685 lstAppendItem(&pDlgData->llControls,
686 pColumnDef);
687
688 // if this is the first control with WS_TABSTOP,
689 // we give it the focus later
690 if ( (flStyle & WS_TABSTOP)
691 && (!pDlgData->hwndFirstFocus)
692 )
693 pDlgData->hwndFirstFocus = pColumnDef->hwndControl;
694 }
695 }
696 break; }
697 }
698
699}
700
701/*
702 *@@ ProcessRow:
703 * level-3 procedure (called from ProcessTable),
704 * which in turn calls ProcessColumn for each column
705 * in the row.
706 *
707 * See ProcessAll for the meaning of ProcessMode.
708 */
709
710VOID ProcessRow(PROWDEF pRowDef,
711 PTABLEDEF pOwningTable, // in: current table from ProcessTable
712 PROCESSMODE ProcessMode, // in: processing mode (see ProcessAll)
713 PLONG plY, // in/out: current y position (decremented)
714 PDLGPRIVATE pDlgData)
715{
716 ULONG ul;
717 LONG lX;
718 PLISTNODE pNode;
719
720 pRowDef->pOwningTable = pOwningTable;
721
722 if (ProcessMode == PROCESS_CALC_SIZES)
723 {
724 pRowDef->cpRow.cx = 0;
725 pRowDef->cpRow.cy = 0;
726 }
727 else if (ProcessMode == PROCESS_CALC_POSITIONS)
728 {
729 // set up x and y so that the columns can
730 // base on that
731 pRowDef->cpRow.x = pOwningTable->cpTable.x;
732 // decrease y by row height
733 *plY -= pRowDef->cpRow.cy;
734 // and use that for our bottom position
735 pRowDef->cpRow.y = *plY;
736
737 // set lX to left of row; used by column calls below
738 lX = pRowDef->cpRow.x;
739 }
740
741 FOR_ALL_NODES(&pRowDef->llColumns, pNode)
742 {
743 PCOLUMNDEF pColumnDefThis = (PCOLUMNDEF)pNode->pItemData;
744
745 ProcessColumn(pColumnDefThis, pRowDef, ProcessMode, &lX, pDlgData);
746
747 if (ProcessMode == PROCESS_CALC_SIZES)
748 {
749 // row width = sum of all columns
750 pRowDef->cpRow.cx += pColumnDefThis->cpColumn.cx;
751
752 // row height = maximum height of a column
753 if (pRowDef->cpRow.cy < pColumnDefThis->cpColumn.cy)
754 pRowDef->cpRow.cy = pColumnDefThis->cpColumn.cy;
755 }
756 }
757}
758
759/*
760 *@@ ProcessTable:
761 * level-2 procedure (called from ProcessAll),
762 * which in turn calls ProcessRow for each row
763 * in the table (which in turn calls ProcessColumn
764 * for each column in the row).
765 *
766 * See ProcessAll for the meaning of ProcessMode.
767 *
768 * This routine is a bit sick because it can even be
769 * called recursively from ProcessColumn (!) if a
770 * nested table is found in a COLUMNDEF.
771 *
772 * With PROCESS_CALC_POSITIONS, pptl must specify
773 * the lower left corner of the table. For the
774 * root call, this will be {0, 0}; for nested calls,
775 * this must be the lower left corner of the column
776 * to which the nested table belongs.
777 *
778 */
779
780VOID ProcessTable(PTABLEDEF pTableDef,
781 const CONTROLPOS *pcpTable, // in: table position with PROCESS_CALC_POSITIONS
782 PROCESSMODE ProcessMode, // in: processing mode (see ProcessAll)
783 PDLGPRIVATE pDlgData)
784{
785 ULONG ul;
786 LONG lY;
787 PLISTNODE pNode;
788
789 if (ProcessMode == PROCESS_CALC_SIZES)
790 {
791 pTableDef->cpTable.cx = 0;
792 pTableDef->cpTable.cy = 0;
793 }
794 else if (ProcessMode == PROCESS_CALC_POSITIONS)
795 {
796 pTableDef->cpTable.x = pcpTable->x;
797 pTableDef->cpTable.y = pcpTable->y;
798
799 // start the rows on top
800 lY = pcpTable->y + pTableDef->cpTable.cy;
801 }
802
803 FOR_ALL_NODES(&pTableDef->llRows, pNode)
804 {
805 PROWDEF pRowDefThis = (PROWDEF)pNode->pItemData;
806
807 ProcessRow(pRowDefThis, pTableDef, ProcessMode, &lY, pDlgData);
808
809 if (ProcessMode == PROCESS_CALC_SIZES)
810 {
811 // table width = maximum width of a row
812 if (pTableDef->cpTable.cx < pRowDefThis->cpRow.cx)
813 pTableDef->cpTable.cx = pRowDefThis->cpRow.cx;
814
815 // table height = sum of all rows
816 pTableDef->cpTable.cy += pRowDefThis->cpRow.cy;
817 }
818 }
819}
820
821/*
822 *@@ ProcessAll:
823 * level-1 procedure, which in turn calls ProcessTable
824 * for each root-level table found (which in turn
825 * calls ProcessRow for each row in the table, which
826 * in turn calls ProcessColumn for each column in
827 * the row).
828 *
829 * The first trick to formatting is that ProcessAll will
830 * get three times, thus going down the entire tree three
831 * times, with ProcessMode being set to one of the
832 * following for each call (in this order):
833 *
834 * -- PROCESS_CALC_SIZES: calculates the sizes
835 * of all tables, rows, columns, and controls.
836 *
837 * After this first call, we know all the sizes
838 * only and then then calculate the positions.
839 *
840 * -- PROCESS_CALC_POSITIONS: calculates the positions
841 * based on the sizes calculated before.
842 *
843 * -- PROCESS_CREATE_CONTROLS: creates the controls with the
844 * positions and sizes calculated before.
845 *
846 * The second trick is the precondition that tables may
847 * nest by allowing a table definition instead of a
848 * control definition in a column. This way we can
849 * recurse from columns back into tables and thus
850 * know the size and position of a nested table column
851 * just as if it were a regular control.
852 */
853
854VOID ProcessAll(PDLGPRIVATE pDlgData,
855 PSIZEL pszlClient,
856 PROCESSMODE ProcessMode)
857{
858 ULONG ul;
859 PLISTNODE pNode;
860 CONTROLPOS cpTable;
861 ZERO(&cpTable);
862
863 switch (ProcessMode)
864 {
865 case PROCESS_CALC_SIZES:
866 pszlClient->cx = 0;
867 pszlClient->cy = 0;
868 break;
869
870 case PROCESS_CALC_POSITIONS:
871 // start with the table on top
872 cpTable.y = pszlClient->cy;
873 break;
874 }
875
876 FOR_ALL_NODES(&pDlgData->llTables, pNode)
877 {
878 PTABLEDEF pTableDefThis = (PTABLEDEF)pNode->pItemData;
879
880 if (ProcessMode == PROCESS_CALC_POSITIONS)
881 {
882 cpTable.x = 0;
883 cpTable.y -= pTableDefThis->cpTable.cy;
884 }
885
886 ProcessTable(pTableDefThis,
887 &cpTable, // start pos
888 ProcessMode,
889 pDlgData);
890
891 if (ProcessMode == PROCESS_CALC_SIZES)
892 {
893 pszlClient->cx += pTableDefThis->cpTable.cx;
894 pszlClient->cy += pTableDefThis->cpTable.cy;
895 }
896 }
897}
898
899/*
900 *@@ CreateColumn:
901 *
902 */
903
904APIRET CreateColumn(PROWDEF pCurrentRow,
905 BOOL fIsNestedTable,
906 PVOID pvDefinition, // in: either PTABLEDEF or PCONTROLDEF
907 PCOLUMNDEF *ppColumnDef) // out: new COLUMNDEF
908{
909 APIRET arc = NO_ERROR;
910
911 if (!pCurrentRow)
912 arc = DLGERR_CONTROL_BEFORE_ROW;
913 else
914 {
915 // append the control def
916 if (!pvDefinition)
917 arc = DLGERR_NULL_CTL_DEF;
918 else
919 {
920 // create column and store ctl def
921 PCOLUMNDEF pColumnDef = NEW(COLUMNDEF);
922 if (!pColumnDef)
923 arc = ERROR_NOT_ENOUGH_MEMORY;
924 else
925 {
926 memset(pColumnDef, 0, sizeof(COLUMNDEF));
927 pColumnDef->pOwningRow = pCurrentRow;
928 pColumnDef->fIsNestedTable = fIsNestedTable;
929 pColumnDef->pvDefinition = pvDefinition;
930
931 *ppColumnDef = pColumnDef;
932 }
933 }
934 }
935
936 return (arc);
937}
938
939/* ******************************************************************
940 *
941 * Public APIs
942 *
943 ********************************************************************/
944
945/*
946 *@@ STACKITEM:
947 *
948 */
949
950typedef struct _STACKITEM
951{
952 PTABLEDEF pLastTable;
953 PROWDEF pLastRow;
954
955} STACKITEM, *PSTACKITEM;
956
957/*
958 *@@ dlghCreateDlg:
959 * replacement for WinCreateDlg for creating a dialog
960 * from a settings array in memory, which is formatted
961 * automatically.
962 *
963 * This does NOT use regular dialog templates from
964 * module resources. Instead, you pass in an array
965 * of _DLGHITEM structures, which define the controls
966 * and how they are to be formatted.
967 *
968 * A regular standard dialog would use something like
969 *
970 + FCF_TITLEBAR | FCF_SYSMENU | FCF_DLGBORDER | FCF_NOBYTEALIGN
971 *
972 * for flCreateFlags. To make the dlg sizeable, specify
973 * FCF_SIZEBORDER instead of FCF_DLGBORDER.
974 *
975 * <B>Usage:</B>
976 *
977 * Like WinLoadDlg, this creates a standard WC_FRAME and
978 * subclasses it with fnwpMyDlgProc. It then sends WM_INITDLG
979 * to the dialog with pCreateParams in mp2. You can use
980 * WinProcessDlg as usual. In your dlg proc, use WinDefDlgProc
981 * as usual.
982 *
983 * There is NO run-time overhead after creation; after this
984 * function returns, the dialog is a standard dialog as if
985 * loaded from WinLoadDlg.
986 *
987 * The array of _DLGHITEM structures defines how the
988 * dialog is set up. All this is ONLY used by this function
989 * and NOT needed after the dialog has been created.
990 *
991 * In _DLGHITEM, the "Type" field determines what this
992 * structure defines. A number of handy macros have been
993 * defined to make this easier and to provide type-checking
994 * at compile time.
995 *
996 * The macros are:
997 *
998 * -- START_TABLE starts a new table. The tables may nest,
999 * but must each be properly terminated with END_TABLE.
1000 *
1001 * -- START_ROW(fl) starts a new row in a table.
1002 *
1003 * fl specifies formatting flags for the row. This
1004 * can be one of ROW_VALIGN_BOTTOM, ROW_VALIGN_CENTER,
1005 * ROW_VALIGN_TOP and affects all items in the control.
1006 *
1007 * -- START_GROUP_TABLE(pDef) starts a group. This
1008 * behaves exacly like START_TABLE, but in addition,
1009 * it produces a control around the table. Useful
1010 * for group boxes. pDef must point to a _CONTROLDEF,
1011 * whose size parameter is ignored.
1012 *
1013 * This must also be terminated with END_TABLE.
1014 *
1015 * -- CONTROL_DEF(pDef) defines a control in a table row.
1016 * pDef must point to a _CONTROLDEF structure.
1017 *
1018 * The main difference (and advantage) of this
1019 * function is that there is NO information in
1020 * _CONTROLDEF about a control's _position_.
1021 * Instead, the structure only contains the _size_
1022 * of the control. All positions are computed by
1023 * this function, depending on the position of the
1024 * control and its nesting within the various tables.
1025 *
1026 * If you specify SZL_AUTOSIZE, the size of the
1027 * control is even computed automatically. Presently,
1028 * this only works for statics with SS_TEXT and
1029 * SS_BITMAP.
1030 *
1031 * Unless separated with TYPE_START_NEW_ROW items,
1032 * subsequent control items will be considered
1033 * to be in the same row (== positioned next to
1034 * each other).
1035 *
1036 * There are a few rules, whose violation will produce
1037 * an error:
1038 *
1039 * -- The entire array must be enclosed in a table
1040 * (the "root" table).
1041 *
1042 * -- After START_TABLE or START_GROUP_TABLE, there must
1043 * always be a START_ROW first.
1044 *
1045 * To create a dialog, set up arrays like the following:
1046 *
1047 + // control definitions referenced by DlgTemplate:
1048 + CONTROLDEF
1049 + (1) GroupDef = {
1050 + WC_STATIC, "", ....,
1051 + { 0, 0 }, // size, ignored for groups
1052 + 5 // spacing
1053 + },
1054 + (2) CnrDef = {
1055 + WC_CONTAINER, "", ....,
1056 + { 50, 50 }, // size
1057 + 5 // spacing
1058 + },
1059 + (3) Static = {
1060 + WC_STATIC, "Static below cnr", ...,
1061 + { SZL_AUTOSIZE, SZL_AUTOSIZE }, // size
1062 + 5 // spacing
1063 + },
1064 + (4) OKButton = {
1065 + WC_STATIC, "~OK", ...,
1066 + { 100, 30 }, // size
1067 + 5 // spacing
1068 + },
1069 + (5) CancelButton = {
1070 + WC_STATIC, "~Cancel", ...,
1071 + { 100, 30 }, // size
1072 + 5 // spacing
1073 + };
1074 +
1075 + DLGHITEM DlgTemplate[] =
1076 + {
1077 + START_TABLE, // root table, must exist
1078 + START_ROW(0), // row 1 in the root table
1079 + // create group on top
1080 + (1) START_GROUP_TABLE(&Group),
1081 + START_ROW(0),
1082 + (2) CONTROL_DEF(&CnrDef),
1083 + START_ROW(0),
1084 + (3) CONTROL_DEF(&Static),
1085 + END_TABLE, // end of group
1086 + START_ROW(0), // row 2 in the root table
1087 + // two buttons next to each other
1088 + (4) CONTROL_DEF(&OKButton),
1089 + (5) CONTROL_DEF(&CancelButton),
1090 + END_TABLE
1091 + }
1092 *
1093 * This will produce a dlg like this:
1094 *
1095 + ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
1096 + º º
1097 + º ÚÄ Group (1) ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ º
1098 + º ³ ³ º
1099 + º ³ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ º
1100 + º ³ ³ ³ ³ º
1101 + º ³ ³ Cnr inside group (2) ³ ³ º
1102 + º ³ ³ ³ ³ º
1103 + º ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ³ º
1104 + º ³ Static below cnr (3) ³ º
1105 + º ³ ³ º
1106 + º ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ º
1107 + º ÚÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ º
1108 + º ³ OK (4) ³ ³ Cancel (5) ³ º
1109 + º ÀÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ º
1110 + º º
1111 + ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ
1112 *
1113 * <B>Errors:</B>
1114 *
1115 * This does not return a HWND, but an APIRET. This will be
1116 * one of the following:
1117 *
1118 * -- NO_ERROR: only in that case, the phwndDlg ptr
1119 * receives the HWND of the new dialog, which can
1120 * then be given to WinProcessDlg. Don't forget
1121 * WinDestroyWindow.
1122 *
1123 * -- ERROR_NOT_ENOUGH_MEMORY
1124 *
1125 * -- DLGERR_ROW_BEFORE_TABLE: a row definition appeared
1126 * outside a table definition.
1127 *
1128 * -- DLGERR_CONTROL_BEFORE_ROW: a control definition
1129 * appeared right after a table definition. You must
1130 * specify a row first.
1131 *
1132 * -- DLGERR_NULL_CTL_DEF: TYPE_END_TABLE was specified,
1133 * but the CONTROLDEF ptr was NULL.
1134 *
1135 * -- DLGERR_CANNOT_CREATE_FRAME: unable to create the
1136 * WC_FRAME window. Maybe an invalid owner was specified.
1137 *
1138 * -- DLGERR_INVALID_CODE: invalid "Type" field in
1139 * DLGHITEM.
1140 *
1141 * -- DLGERR_TABLE_NOT_CLOSED, DLGERR_TOO_MANY_TABLES_CLOSED:
1142 * improper nesting of TYPE_START_NEW_TABLE and
1143 * TYPE_END_TABLE fields.
1144 *
1145 * <B>Example:</B>
1146 *
1147 * The typical calling sequence would be:
1148 *
1149 + HWND hwndDlg = NULLHANDLE;
1150 + if (NO_ERROR == dlghCreateDlg(&hwndDlg,
1151 + hwndOwner,
1152 + FCF_DLGBORDER | FCF_NOBYTEALIGN,
1153 + fnwpMyDlgProc,
1154 + "My Dlg Title",
1155 + G_aMyDialogTemplate,
1156 + ARRAYITEMSIZE(G_aMyDialogTemplate),
1157 + NULL))
1158 + {
1159 + ULONG idReturn = WinProcessDlg(pDlgData->hwndDlg);
1160 + WinDestroyWindow(hwndDlg);
1161 + }
1162 *
1163 *
1164 */
1165
1166APIRET dlghCreateDlg(HWND *phwndDlg, // out: new dialog
1167 HWND hwndOwner,
1168 ULONG flCreateFlags, // in: standard FCF_* frame flags
1169 PFNWP pfnwpDialogProc,
1170 const char *pcszDlgTitle,
1171 PDLGHITEM paDlgItems, // in: definition array
1172 ULONG cDlgItems, // in: array item count (NOT array size)
1173 PVOID pCreateParams, // in: for mp2 of WM_INITDLG
1174 const char *pcszControlsFont) // in: font for ctls with CTL_COMMON_FONT
1175{
1176 APIRET arc = NO_ERROR;
1177
1178 #define SPACING 10
1179
1180 PTABLEDEF pCurrentTable = NULL;
1181 PROWDEF pCurrentRow = NULL;
1182 ULONG ul;
1183 LINKLIST llStack;
1184
1185 PDLGPRIVATE pDlgData = NEW(DLGPRIVATE);
1186
1187 if (!pDlgData)
1188 return (ERROR_NOT_ENOUGH_MEMORY);
1189
1190 ZERO(pDlgData);
1191 lstInit(&pDlgData->llTables, FALSE);
1192 lstInit(&pDlgData->llControls, FALSE);
1193
1194 pDlgData->pcszControlsFont = pcszControlsFont;
1195
1196 /*
1197 * 1) parse the table and create structures from it
1198 *
1199 */
1200
1201 lstInit(&llStack, TRUE); // this is our stack for nested table definitions
1202
1203 for (ul = 0;
1204 ul < cDlgItems;
1205 ul++)
1206 {
1207 PDLGHITEM pItemThis = &paDlgItems[ul];
1208
1209 switch (pItemThis->Type)
1210 {
1211 /*
1212 * TYPE_START_NEW_TABLE:
1213 *
1214 */
1215
1216 case TYPE_START_NEW_TABLE:
1217 {
1218 // root table or nested?
1219 BOOL fIsRoot = (pCurrentTable == NULL);
1220
1221 // push the current table on the stack
1222 PSTACKITEM pStackItem = NEW(STACKITEM);
1223 if (pStackItem)
1224 {
1225 pStackItem->pLastTable = pCurrentTable;
1226 pStackItem->pLastRow = pCurrentRow;
1227 lstPush(&llStack, pStackItem);
1228 }
1229
1230 // create new table
1231 pCurrentTable = NEW(TABLEDEF);
1232 if (!pCurrentTable)
1233 arc = ERROR_NOT_ENOUGH_MEMORY;
1234 else
1235 {
1236 ZERO(pCurrentTable);
1237
1238 lstInit(&pCurrentTable->llRows, FALSE);
1239
1240 if (pItemThis->ulData)
1241 // control specified: store it (this will become a PM group)
1242 pCurrentTable->pCtlDef = (PCONTROLDEF)pItemThis->ulData;
1243
1244 if (fIsRoot)
1245 // root table:
1246 // append to dialog data list
1247 lstAppendItem(&pDlgData->llTables, pCurrentTable);
1248 else
1249 {
1250 // nested table:
1251 // create "table" column for this
1252 PCOLUMNDEF pColumnDef;
1253 arc = CreateColumn(pCurrentRow,
1254 TRUE, // nested table
1255 pCurrentTable,
1256 &pColumnDef);
1257 if (!arc)
1258 lstAppendItem(&pCurrentRow->llColumns, pColumnDef);
1259 }
1260 }
1261
1262 pCurrentRow = NULL;
1263 break; }
1264
1265 /*
1266 * TYPE_START_NEW_ROW:
1267 *
1268 */
1269
1270 case TYPE_START_NEW_ROW:
1271 {
1272 if (!pCurrentTable)
1273 arc = DLGERR_ROW_BEFORE_TABLE;
1274 else
1275 {
1276 // create new row
1277 pCurrentRow = NEW(ROWDEF);
1278 if (!pCurrentRow)
1279 arc = ERROR_NOT_ENOUGH_MEMORY;
1280 else
1281 {
1282 ZERO(pCurrentRow);
1283
1284 pCurrentRow->pOwningTable = pCurrentTable;
1285 lstInit(&pCurrentRow->llColumns, FALSE);
1286
1287 pCurrentRow->flRowFormat = pItemThis->ulData;
1288
1289 lstAppendItem(&pCurrentTable->llRows, pCurrentRow);
1290 }
1291 }
1292 break; }
1293
1294 /*
1295 * TYPE_CONTROL_DEF:
1296 *
1297 */
1298
1299 case TYPE_CONTROL_DEF:
1300 {
1301 PCOLUMNDEF pColumnDef;
1302 arc = CreateColumn(pCurrentRow,
1303 FALSE, // no nested table
1304 (PVOID)pItemThis->ulData,
1305 &pColumnDef);
1306 if (!arc)
1307 lstAppendItem(&pCurrentRow->llColumns, pColumnDef);
1308 break; }
1309
1310 /*
1311 * TYPE_END_TABLE:
1312 *
1313 */
1314
1315 case TYPE_END_TABLE:
1316 {
1317 PLISTNODE pNode = lstPop(&llStack);
1318 if (!pNode)
1319 // nothing on the stack:
1320 arc = DLGERR_TOO_MANY_TABLES_CLOSED;
1321 else
1322 {
1323 PSTACKITEM pStackItem = (PSTACKITEM)pNode->pItemData;
1324 pCurrentTable = pStackItem->pLastTable;
1325 pCurrentRow = pStackItem->pLastRow;
1326
1327 lstRemoveNode(&llStack, pNode);
1328 }
1329 break; }
1330
1331 default:
1332 arc = DLGERR_INVALID_CODE;
1333 }
1334
1335 if (arc)
1336 break;
1337 }
1338
1339 if (arc == NO_ERROR)
1340 if (lstCountItems(&llStack))
1341 arc = DLGERR_TABLE_NOT_CLOSED;
1342
1343 lstClear(&llStack);
1344
1345 if (arc == NO_ERROR)
1346 {
1347 /*
1348 * 2) create empty dialog frame
1349 *
1350 */
1351
1352 FRAMECDATA fcData = {0};
1353 ULONG flStyle = 0;
1354
1355 #define FCF_DIALOGBOX 0x40000000L
1356
1357 fcData.cb = sizeof(FRAMECDATA);
1358 fcData.flCreateFlags = flCreateFlags | FCF_DIALOGBOX;
1359
1360 if (flCreateFlags & FCF_SIZEBORDER)
1361 // dialog has size border:
1362 // add "clip siblings" style
1363 flStyle |= WS_CLIPSIBLINGS;
1364
1365 pDlgData->hwndDlg = WinCreateWindow(HWND_DESKTOP,
1366 WC_FRAME,
1367 (PSZ)pcszDlgTitle,
1368 flStyle, // style; invisible for now
1369 0, 0, 0, 0,
1370 hwndOwner,
1371 HWND_TOP,
1372 0, // ID
1373 &fcData,
1374 NULL); // presparams
1375
1376 if (!pDlgData->hwndDlg)
1377 arc = DLGERR_CANNOT_CREATE_FRAME;
1378 else
1379 {
1380 SIZEL szlClient = {0};
1381 RECTL rclClient;
1382 HWND hwndFocusItem = NULLHANDLE;
1383
1384 /*
1385 * 3) compute size of client after computing all control sizes
1386 *
1387 */
1388
1389 ProcessAll(pDlgData,
1390 &szlClient,
1391 PROCESS_CALC_SIZES);
1392
1393 if (pDlgData->hps)
1394 WinReleasePS(pDlgData->hps);
1395
1396 WinSubclassWindow(pDlgData->hwndDlg, pfnwpDialogProc);
1397
1398 // calculate the frame size from the client size
1399 rclClient.xLeft = 10;
1400 rclClient.yBottom = 10;
1401 rclClient.xRight = szlClient.cx + 2 * SPACING;
1402 rclClient.yTop = szlClient.cy + 2 * SPACING;
1403 WinCalcFrameRect(pDlgData->hwndDlg,
1404 &rclClient,
1405 FALSE); // frame from client
1406
1407 WinSetWindowPos(pDlgData->hwndDlg,
1408 0,
1409 10,
1410 10,
1411 rclClient.xRight,
1412 rclClient.yTop,
1413 SWP_MOVE | SWP_SIZE | SWP_NOADJUST);
1414
1415 /*
1416 * 4) compute positions of all controls
1417 *
1418 */
1419
1420 ProcessAll(pDlgData,
1421 &szlClient,
1422 PROCESS_CALC_POSITIONS);
1423
1424 /*
1425 * 5) create control windows, finally
1426 *
1427 */
1428
1429 pDlgData->ptlTotalOfs.x = SPACING;
1430 pDlgData->ptlTotalOfs.y = SPACING;
1431
1432 ProcessAll(pDlgData,
1433 &szlClient,
1434 PROCESS_CREATE_CONTROLS);
1435
1436 /*
1437 * 6) WM_INITDLG, set focus
1438 *
1439 */
1440
1441 hwndFocusItem = (pDlgData->hwndFirstFocus)
1442 ? pDlgData->hwndFirstFocus
1443 : pDlgData->hwndDlg;
1444 if (!WinSendMsg(pDlgData->hwndDlg,
1445 WM_INITDLG,
1446 (MPARAM)hwndFocusItem,
1447 (MPARAM)pCreateParams))
1448 {
1449 // if WM_INITDLG returns FALSE, this means
1450 // the dlg proc has not changed the focus;
1451 // we must then set the focus here
1452 WinSetFocus(HWND_DESKTOP, hwndFocusItem);
1453 }
1454 }
1455 }
1456
1457 if (pDlgData)
1458 {
1459 PLISTNODE pTableNode;
1460
1461 if (arc)
1462 {
1463 // error: clean up
1464 if (pDlgData->hwndDlg)
1465 WinDestroyWindow(pDlgData->hwndDlg);
1466 }
1467 else
1468 // no error: output dialog
1469 *phwndDlg = pDlgData->hwndDlg;
1470
1471 // in any case, clean up our mess:
1472
1473 // clean up the tables
1474 FOR_ALL_NODES(&pDlgData->llTables, pTableNode)
1475 {
1476 PTABLEDEF pTable = (PTABLEDEF)pTableNode->pItemData;
1477
1478 // for each table, clean up the rows
1479 PLISTNODE pRowNode;
1480 FOR_ALL_NODES(&pTable->llRows, pRowNode)
1481 {
1482 PROWDEF pRow = (PROWDEF)pRowNode->pItemData;
1483
1484 // for each row, clean up the columns
1485 PLISTNODE pColumnNode;
1486 FOR_ALL_NODES(&pRow->llColumns, pColumnNode)
1487 {
1488 PCOLUMNDEF pColumn = (PCOLUMNDEF)pColumnNode->pItemData;
1489 free(pColumn);
1490 }
1491 lstClear(&pRow->llColumns);
1492
1493 free(pRow);
1494 }
1495 lstClear(&pTable->llRows);
1496
1497 free(pTable);
1498 }
1499
1500 lstClear(&pDlgData->llTables);
1501 lstClear(&pDlgData->llControls);
1502
1503 free(pDlgData);
1504 }
1505
1506 return (arc);
1507}
1508
1509/*
1510 *@@ dlghSetPrevFocus:
1511 * "backward" function for rotating the focus
1512 * in a dialog when the "shift+tab" keys get
1513 * pressed.
1514 *
1515 * pllWindows must be a linked list with the
1516 * plain HWND window handles of the focussable
1517 * controls in the dialog.
1518 */
1519
1520VOID dlghSetPrevFocus(PVOID pvllWindows)
1521{
1522 PLINKLIST pllWindows = (PLINKLIST)pvllWindows;
1523
1524 // check current focus
1525 HWND hwndFocus = WinQueryFocus(HWND_DESKTOP);
1526
1527 PLISTNODE pNode = lstNodeFromItem(pllWindows, (PVOID)hwndFocus);
1528
1529 BOOL fRestart = FALSE;
1530
1531 while (pNode)
1532 {
1533 CHAR szClass[100];
1534
1535 // previos node
1536 pNode = pNode->pPrevious;
1537
1538 if ( (!pNode) // no next, or not found:
1539 && (!fRestart) // avoid infinite looping if bad list
1540 )
1541 {
1542 pNode = lstQueryLastNode(pllWindows);
1543 fRestart = TRUE;
1544 }
1545
1546 if (pNode)
1547 {
1548 // check if this is a focusable control
1549 if (WinQueryClassName((HWND)pNode->pItemData,
1550 sizeof(szClass),
1551 szClass))
1552 {
1553 if ( (strcmp(szClass, "#5")) // not static
1554 )
1555 break;
1556 // else: go for next then
1557 }
1558 }
1559 }
1560
1561 if (pNode)
1562 {
1563 WinSetFocus(HWND_DESKTOP,
1564 (HWND)pNode->pItemData);
1565 }
1566}
1567
1568/*
1569 *@@ dlghSetNextFocus:
1570 * "forward" function for rotating the focus
1571 * in a dialog when the "ab" key gets pressed.
1572 *
1573 * pllWindows must be a linked list with the
1574 * plain HWND window handles of the focussable
1575 * controls in the dialog.
1576 */
1577
1578VOID dlghSetNextFocus(PVOID pvllWindows)
1579{
1580 PLINKLIST pllWindows = (PLINKLIST)pvllWindows;
1581
1582 // check current focus
1583 HWND hwndFocus = WinQueryFocus(HWND_DESKTOP);
1584
1585 PLISTNODE pNode = lstNodeFromItem(pllWindows, (PVOID)hwndFocus);
1586
1587 BOOL fRestart = FALSE;
1588
1589 while (pNode)
1590 {
1591 CHAR szClass[100];
1592
1593 // next focus in node
1594 pNode = pNode->pNext;
1595
1596 if ( (!pNode) // no next, or not found:
1597 && (!fRestart) // avoid infinite looping if bad list
1598 )
1599 {
1600 pNode = lstQueryFirstNode(pllWindows);
1601 fRestart = TRUE;
1602 }
1603
1604 if (pNode)
1605 {
1606 // check if this is a focusable control
1607 if (WinQueryClassName((HWND)pNode->pItemData,
1608 sizeof(szClass),
1609 szClass))
1610 {
1611 if ( (strcmp(szClass, "#5")) // not static
1612 )
1613 break;
1614 // else: go for next then
1615 }
1616 }
1617 }
1618
1619 if (pNode)
1620 {
1621 WinSetFocus(HWND_DESKTOP,
1622 (HWND)pNode->pItemData);
1623 }
1624}
1625
1626/*
1627 *@@ MatchMnemonic:
1628 * returns TRUE if the specified control matches
1629 *
1630 *
1631 *@@added V0.9.9 (2001-03-17) [umoeller]
1632 */
1633
1634/*
1635 *@@ dlghProcessMnemonic:
1636 * finds the control which matches usch
1637 * and gives it the focus. If this is a
1638 * static control, the next control in the
1639 * list is given focus instead. (Standard
1640 * dialog behavior.)
1641 *
1642 * Pass in usch from WM_CHAR. It is assumed
1643 * that the caller has already tested for
1644 * the "alt" key to be depressed.
1645 *
1646 *@@added V0.9.9 (2001-03-17) [umoeller]
1647 */
1648
1649HWND dlghProcessMnemonic(PVOID pvllWindows,
1650 USHORT usch)
1651{
1652 PLINKLIST pllWindows = (PLINKLIST)pvllWindows;
1653
1654 HWND hwndFound = NULLHANDLE;
1655 PLISTNODE pNode = lstQueryFirstNode(pllWindows);
1656 CHAR szClass[100];
1657
1658 while (pNode)
1659 {
1660 HWND hwnd = (HWND)pNode->pItemData;
1661
1662 if (WinSendMsg(hwnd,
1663 WM_MATCHMNEMONIC,
1664 (MPARAM)usch,
1665 0))
1666 {
1667 // according to the docs, only buttons and static
1668 // return TRUE to that msg;
1669 // if this is a static, give focus to the next
1670 // control
1671
1672 // _Pmpf((__FUNCTION__ ": hwnd 0x%lX", hwnd));
1673
1674 // check if this is a focusable control
1675 if (WinQueryClassName(hwnd,
1676 sizeof(szClass),
1677 szClass))
1678 {
1679 if (!strcmp(szClass, "#3"))
1680 // it's a button: click it
1681 WinSendMsg(hwnd, BM_CLICK, (MPARAM)TRUE, 0);
1682 else if (!strcmp(szClass, "#5"))
1683 {
1684 // it's a static: give focus to following control
1685 pNode = pNode->pNext;
1686 if (pNode)
1687 WinSetFocus(HWND_DESKTOP, (HWND)pNode->pItemData);
1688 }
1689 }
1690 else
1691 // any other control (are there any?): give them focus
1692 WinSetFocus(HWND_DESKTOP, hwnd);
1693
1694 // in any case, stop
1695 hwndFound = hwnd;
1696 break;
1697 }
1698
1699 pNode = pNode->pNext;
1700 }
1701
1702 return (hwndFound);
1703}
1704
1705/*
1706 *@@ dlghEnter:
1707 * presses the first button with BS_DEFAULT.
1708 */
1709
1710BOOL dlghEnter(PVOID pvllWindows)
1711{
1712 PLINKLIST pllWindows = (PLINKLIST)pvllWindows;
1713
1714 PLISTNODE pNode = lstQueryFirstNode(pllWindows);
1715 CHAR szClass[100];
1716 while (pNode)
1717 {
1718 HWND hwnd = (HWND)pNode->pItemData;
1719 if (WinQueryClassName(hwnd,
1720 sizeof(szClass),
1721 szClass))
1722 {
1723 if (!strcmp(szClass, "#3")) // button
1724 {
1725 // _Pmpf((__FUNCTION__ ": found button"));
1726 if ( (WinQueryWindowULong(hwnd, QWL_STYLE) & (BS_PUSHBUTTON | BS_DEFAULT))
1727 == (BS_PUSHBUTTON | BS_DEFAULT)
1728 )
1729 {
1730 // _Pmpf((" is default!"));
1731 WinPostMsg(hwnd,
1732 BM_CLICK,
1733 (MPARAM)TRUE, // upclick
1734 0);
1735 return (TRUE);
1736 }
1737 }
1738 }
1739
1740 pNode = pNode->pNext;
1741 }
1742
1743 return (FALSE);
1744}
1745
1746
Note: See TracBrowser for help on using the repository browser.