source: trunk/src/helpers/winh.c@ 165

Last change on this file since 165 was 165, checked in by umoeller, 23 years ago

Bunch of fixes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 174.1 KB
Line 
1
2/*
3 *@@sourcefile winh.c:
4 * contains Presentation Manager helper functions that are
5 * independent of a single application, i.e. these can be
6 * used w/out the rest of the XWorkplace source in any PM
7 * program.
8 *
9 * Usage: All PM programs.
10 *
11 * Function prefixes (new with V0.81):
12 * -- winh* Win (Presentation Manager) helper functions
13 *
14 * Note: Version numbering in this file relates to XWorkplace version
15 * numbering.
16 *
17 *@@header "helpers\winh.h"
18 */
19
20/*
21 * Copyright (C) 1997-2000 Ulrich M”ller.
22 * This file is part of the "XWorkplace helpers" source package.
23 * This is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published
25 * by the Free Software Foundation, in version 2 as it comes in the
26 * "COPYING" file of the XWorkplace main distribution.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 */
32
33#define OS2EMX_PLAIN_CHAR
34 // this is needed for "os2emx.h"; if this is defined,
35 // emx will define PSZ as _signed_ char, otherwise
36 // as unsigned char
37
38#define INCL_DOSPROCESS
39#define INCL_DOSMODULEMGR
40#define INCL_DOSSEMAPHORES
41#define INCL_DOSDEVICES
42#define INCL_DOSDEVIOCTL
43#define INCL_DOSSESMGR
44#define INCL_DOSERRORS
45
46#define INCL_WINWINDOWMGR
47#define INCL_WINMESSAGEMGR
48#define INCL_WINFRAMEMGR
49#define INCL_WININPUT
50#define INCL_WINDIALOGS
51#define INCL_WINPOINTERS
52#define INCL_WINRECTANGLES
53#define INCL_WINSHELLDATA
54#define INCL_WINTIMER
55#define INCL_WINSYS
56#define INCL_WINHELP
57#define INCL_WINPROGRAMLIST
58#define INCL_WINSWITCHLIST
59#define INCL_WINBUTTONS
60#define INCL_WINSTATICS
61#define INCL_WINMENUS
62#define INCL_WINENTRYFIELDS
63#define INCL_WINSCROLLBARS
64#define INCL_WINLISTBOXES
65#define INCL_WINSTDSPIN
66#define INCL_WINSTDSLIDER
67#define INCL_WINCIRCULARSLIDER
68#define INCL_WINSTDFILE
69
70#define INCL_SPL
71#define INCL_SPLDOSPRINT
72#define INCL_SPLERRORS
73
74#define INCL_GPIBITMAPS
75#define INCL_GPIPRIMITIVES
76#include <os2.h>
77
78#include <stdlib.h>
79#include <string.h>
80#include <stdio.h>
81#include <stdarg.h>
82
83#include "setup.h" // code generation and debugging options
84
85#include "helpers\dosh.h"
86#include "helpers\winh.h"
87#include "helpers\prfh.h"
88#include "helpers\gpih.h"
89#include "helpers\standards.h"
90#include "helpers\stringh.h"
91#include "helpers\undoc.h"
92#include "helpers\xstring.h" // extended string helpers
93
94/*
95 *@@category: Helpers\PM helpers\Wrappers
96 */
97
98/* ******************************************************************
99 *
100 * Wrappers
101 *
102 ********************************************************************/
103
104#ifdef WINH_STANDARDWRAPPERS
105
106 /*
107 *@@ winhSendMsg:
108 * wrapper for WinSendMsg.
109 *
110 * If WINH_STANDARDWRAPPERS is #defined before
111 * including win.h, all WinSendMsg calls are
112 * redefined to use this wrapper instead. This
113 * reduces the amount of external fixups required
114 * for loading the module.
115 *
116 *@@added V0.9.12 (2001-05-18) [umoeller]
117 */
118
119 MRESULT winhSendMsg(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
120 {
121 // put the call in brackets so the macro won't apply here
122 return ((WinSendMsg)(hwnd, msg, mp1, mp2));
123 }
124
125 /*
126 *@@ winhSendDlgItemMsg:
127 * wrapper for WinSendDlgItemMsg.
128 *
129 * If WINH_STANDARDWRAPPERS is #defined before
130 * including win.h, all WinSendMsg calls are
131 * redefined to use this wrapper instead. This
132 * reduces the amount of external fixups required
133 * for loading the module.
134 *
135 *@@added V0.9.13 (2001-06-27) [umoeller]
136 */
137
138 MRESULT winhSendDlgItemMsg(HWND hwnd, ULONG id, ULONG msg, MPARAM mp1, MPARAM mp2)
139 {
140 return ((WinSendDlgItemMsg)(hwnd, id, msg, mp1, mp2));
141 }
142
143 /*
144 *@@ winhPostMsg:
145 * wrapper for WinPostMsg.
146 *
147 * If WINH_STANDARDWRAPPERS is #defined before
148 * including win.h, all WinSendMsg calls are
149 * redefined to use this wrapper instead. This
150 * reduces the amount of external fixups required
151 * for loading the module.
152 *
153 *@@added V0.9.12 (2001-05-18) [umoeller]
154 */
155
156 BOOL winhPostMsg(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
157 {
158 // put the call in brackets so the macro won't apply here
159 return ((WinPostMsg)(hwnd, msg, mp1, mp2));
160 }
161
162 /*
163 *@@ winhWindowFromID:
164 *
165 *@@added V0.9.12 (2001-05-18) [umoeller]
166 */
167
168 HWND winhWindowFromID(HWND hwnd, ULONG id)
169 {
170 // put the call in brackets so the macro won't apply here
171 return ((WinWindowFromID)(hwnd, id));
172 }
173
174 /*
175 *@@ winhQueryWindow:
176 *
177 *@@added V0.9.12 (2001-05-18) [umoeller]
178 */
179
180 HWND winhQueryWindow(HWND hwnd, LONG lCode)
181 {
182 // put the call in brackets so the macro won't apply here
183 return ((WinQueryWindow)(hwnd, lCode));
184 }
185
186 /*
187 *@@ winhQueryWindowPtr:
188 *
189 *@@added V0.9.13 (2001-06-21) [umoeller]
190 */
191
192 PVOID winhQueryWindowPtr(HWND hwnd, LONG index)
193 {
194 // put the call in brackets so the macro won't apply here
195 return ((WinQueryWindowPtr)(hwnd, index));
196 }
197
198 /*
199 *@@ winhSetWindowText2:
200 *
201 *@@added V0.9.13 (2001-06-21) [umoeller]
202 */
203
204 BOOL winhSetWindowText2(HWND hwnd, const char *pcsz)
205 {
206 // put the call in brackets so the macro won't apply here
207 return (WinSetWindowText)(hwnd, (PSZ)pcsz);
208 }
209
210 /*
211 *@@ winhSetDlgItemText:
212 *
213 *@@added V0.9.13 (2001-06-21) [umoeller]
214 */
215
216 BOOL winhSetDlgItemText(HWND hwnd, ULONG id, const char *pcsz)
217 {
218 // put the call in brackets so the macro won't apply here
219 return (WinSetDlgItemText)(hwnd, id, (PSZ)pcsz);
220 }
221
222 /*
223 *@@ winhRequestMutexSem:
224 *
225 *@@added V0.9.16 (2002-01-26) [umoeller]
226 */
227
228 APIRET winhRequestMutexSem(HMTX hmtx, ULONG ulTimeout)
229 {
230 // put the call in brackets so the macro won't apply here
231 return (WinRequestMutexSem)(hmtx, ulTimeout);
232 }
233
234#endif // WINH_STANDARDWRAPPERS
235
236/*
237 *@@category: Helpers\PM helpers\Rectangle helpers
238 */
239
240/* ******************************************************************
241 *
242 * Rectangle helpers
243 *
244 ********************************************************************/
245
246/*
247 *@@ winhOffsetRect:
248 * like WinOffsetRect, but doesn't require
249 * an anchor block to be passed in. Why
250 * the original would need an anchor block
251 * for this awfully complicated task is
252 * a mystery to me anyway.
253 *
254 *@@added V0.9.9 (2001-03-13) [umoeller]
255 */
256
257VOID winhOffsetRect(PRECTL prcl,
258 LONG lx,
259 LONG ly)
260{
261 prcl->xLeft += lx;
262 prcl->xRight += lx;
263 prcl->yBottom += ly;
264 prcl->yTop += ly;
265}
266
267/*
268 *@@category: Helpers\PM helpers\Generics
269 */
270
271/* ******************************************************************
272 *
273 * Generics
274 *
275 ********************************************************************/
276
277/*
278 *@@ winhQueryWindowStyle:
279 *
280 *@@added V0.9.13 (2001-07-02) [umoeller]
281 */
282
283ULONG winhQueryWindowStyle(HWND hwnd)
284{
285 return (WinQueryWindowULong(hwnd, QWL_STYLE));
286}
287
288/*
289 *@@ winhEnableDlgItem:
290 *
291 *@@added V0.9.12 (2001-05-18) [umoeller]
292 */
293
294BOOL winhEnableDlgItem(HWND hwndDlg,
295 SHORT id,
296 BOOL fEnable)
297{
298 return (WinEnableWindow(WinWindowFromID(hwndDlg, id), fEnable));
299}
300
301/*
302 *@@ winhIsDlgItemEnabled:
303 *
304 *@@added V0.9.12 (2001-05-18) [umoeller]
305 */
306
307BOOL winhIsDlgItemEnabled(HWND hwndDlg,
308 SHORT id)
309{
310 return (WinIsWindowEnabled(WinWindowFromID(hwndDlg, id)));
311}
312
313
314/*
315 *@@category: Helpers\PM helpers\Menu helpers
316 */
317
318/* ******************************************************************
319 *
320 * Menu helpers
321 *
322 ********************************************************************/
323
324/*
325 *@@ winhQueryMenuItem:
326 * wrapper around MM_QUERYITEM.
327 *
328 *@@added V0.9.12 (2001-05-18) [umoeller]
329 */
330
331BOOL winhQueryMenuItem(HWND hwndMenu,
332 USHORT usItemID,
333 BOOL fSearchSubmenus,
334 PMENUITEM pmi) // out: MENUITEM data
335{
336 return ((BOOL)WinSendMsg(hwndMenu,
337 MM_QUERYITEM,
338 MPFROM2SHORT(usItemID, fSearchSubmenus),
339 (MPARAM)pmi));
340}
341
342/*
343 *@@ winhInsertMenuItem:
344 * this inserts one one menu item into a given menu.
345 *
346 * Returns the return value of the MM_INSERTITEM msg:
347 * -- MIT_MEMERROR: space allocation for menu item failed
348 * -- MIT_ERROR: other error
349 * -- other: zero-based index of new item in menu.
350 */
351
352SHORT winhInsertMenuItem(HWND hwndMenu, // in: menu to insert item into
353 SHORT iPosition, // in: zero-based index of where to
354 // insert or MIT_END
355 SHORT sItemId, // in: ID of new menu item
356 const char *pcszItemTitle, // in: title of new menu item
357 SHORT afStyle,
358 // in: MIS_* style flags.
359 // Valid menu item styles are:
360 // -- MIS_SUBMENU
361 // -- MIS_SEPARATOR
362 // -- MIS_BITMAP: the display object is a bit map.
363 // -- MIS_TEXT: the display object is a text string.
364 // -- MIS_BUTTONSEPARATOR:
365 // The item is a menu button. Any menu can have zero,
366 // one, or two items of this type. These are the last
367 // items in a menu and are automatically displayed after
368 // a separator bar. The user cannot move the cursor to
369 // these items, but can select them with the pointing
370 // device or with the appropriate key.
371 // -- MIS_BREAK: the item begins a new row or column.
372 // -- MIS_BREAKSEPARATOR:
373 // Same as MIS_BREAK, except that it draws a separator
374 // between rows or columns of a pull-down menu.
375 // This style can only be used within a submenu.
376 // -- MIS_SYSCOMMAND:
377 // menu posts a WM_SYSCOMMAND message rather than a
378 // WM_COMMAND message.
379 // -- MIS_OWNERDRAW:
380 // WM_DRAWITEM and WM_MEASUREITEM notification messages
381 // are sent to the owner to draw the item or determine its size.
382 // -- MIS_HELP:
383 // menu posts a WM_HELP message rather than a
384 // WM_COMMAND message.
385 // -- MIS_STATIC
386 // This type of item exists for information purposes only.
387 // It cannot be selected with the pointing device or
388 // keyboard.
389 SHORT afAttr)
390 // in: MIA_* attribute flags
391 // Valid menu item attributes (afAttr) are:
392 // -- MIA_HILITED: if and only if, the item is selected.
393 // -- MIA_CHECKED: a check mark appears next to the item (submenu only).
394 // -- MIA_DISABLED: item is disabled and cannot be selected.
395 // The item is drawn in a disabled state (gray).
396 // -- MIA_FRAMED: a frame is drawn around the item (top-level menu only).
397 // -- MIA_NODISMISS:
398 // if the item is selected, the submenu remains down. A menu
399 // with this attribute is not hidden until the application
400 // or user explicitly does so, for example by selecting either
401 // another menu on the action bar or by pressing the escape key.
402{
403 MENUITEM mi;
404 SHORT src = MIT_ERROR;
405
406 mi.iPosition = iPosition;
407 mi.afStyle = afStyle;
408 mi.afAttribute = afAttr;
409 mi.id = sItemId;
410 mi.hwndSubMenu = 0;
411 mi.hItem = 0;
412 src = SHORT1FROMMR(WinSendMsg(hwndMenu,
413 MM_INSERTITEM,
414 (MPARAM)&mi,
415 (MPARAM)pcszItemTitle));
416 return (src);
417}
418
419/*
420 *@@ winhInsertSubmenu:
421 * this inserts a submenu into a given menu and, if
422 * sItemId != 0, inserts one item into this new submenu also.
423 *
424 * See winhInsertMenuItem for valid menu item styles and
425 * attributes.
426 *
427 * Returns the HWND of the new submenu.
428 */
429
430HWND winhInsertSubmenu(HWND hwndMenu, // in: menu to add submenu to
431 ULONG iPosition, // in: index where to add submenu or MIT_END
432 SHORT sMenuId, // in: menu ID of new submenu
433 const char *pcszSubmenuTitle, // in: title of new submenu
434 USHORT afMenuStyle, // in: MIS* style flags for submenu;
435 // MIS_SUBMENU will always be added
436 SHORT sItemId, // in: ID of first item to add to submenu;
437 // if 0, no first item is inserted
438 const char *pcszItemTitle, // in: title of this item
439 // (if sItemID != 0)
440 USHORT afItemStyle, // in: style flags for this item, e.g. MIS_TEXT
441 // (this is ignored if sItemID == 0)
442 USHORT afAttribute) // in: attributes for this item, e.g. MIA_DISABLED
443 // (this is ignored if sItemID == 0)
444{
445 MENUITEM mi;
446 SHORT src = MIT_ERROR;
447 HWND hwndNewMenu;
448
449 // create new, empty menu
450 hwndNewMenu = WinCreateMenu(hwndMenu,
451 NULL); // no menu template
452 if (hwndNewMenu)
453 {
454 // add "submenu item" to this empty menu;
455 // for some reason, PM always needs submenus
456 // to be a menu item
457 mi.iPosition = iPosition;
458 mi.afStyle = afMenuStyle | MIS_SUBMENU;
459 mi.afAttribute = 0;
460 mi.id = sMenuId;
461 mi.hwndSubMenu = hwndNewMenu;
462 mi.hItem = 0;
463 src = SHORT1FROMMR(WinSendMsg(hwndMenu, MM_INSERTITEM, (MPARAM)&mi, (MPARAM)pcszSubmenuTitle));
464 if ( (src != MIT_MEMERROR)
465 && (src != MIT_ERROR)
466 )
467 {
468 // set the new menu's ID to the same as the
469 // submenu item
470 WinSetWindowUShort(hwndNewMenu, QWS_ID, sMenuId);
471
472 if (sItemId)
473 {
474 // item id given: insert first menu item also
475 mi.iPosition = 0;
476 mi.afStyle = afItemStyle;
477 mi.afAttribute = afAttribute;
478 mi.id = sItemId;
479 mi.hwndSubMenu = 0;
480 mi.hItem = 0;
481 WinSendMsg(hwndNewMenu,
482 MM_INSERTITEM,
483 (MPARAM)&mi,
484 (MPARAM)pcszItemTitle);
485 }
486 }
487 }
488 return (hwndNewMenu);
489}
490
491/*
492 *@@ winhSetMenuCondCascade:
493 * sets the "conditional cascade" style
494 * on the specified submenu.
495 *
496 * This style must always be enabled manually
497 * because the resource compiler won't handle it.
498 *
499 * Note: Pass in the _submenu_ window handle,
500 * not the one of the parent. With lDefaultItem,
501 * specify the item ID in the submenu which is
502 * to be checked as the default item.
503 *
504 *@@added V0.9.12 (2001-05-22) [umoeller]
505 */
506
507BOOL winhSetMenuCondCascade(HWND hwndMenu, // in: submenu handle
508 LONG lDefaultItem) // in: item ID of new default item
509{
510 // stolen from the Warp Toolkit WPS Guide
511 ULONG ulStyle = WinQueryWindowULong(hwndMenu, QWL_STYLE);
512 ulStyle |= MS_CONDITIONALCASCADE;
513 WinSetWindowULong(hwndMenu, QWL_STYLE, ulStyle);
514
515 // make the first item in the subfolder
516 // the default of cascading submenu */
517 return (BOOL)(WinSendMsg(hwndMenu,
518 MM_SETDEFAULTITEMID,
519 (MPARAM)lDefaultItem,
520 0));
521}
522
523/*
524 *@@ winhInsertMenuSeparator:
525 * this inserts a separator into a given menu at
526 * the given position (which may be MIT_END);
527 * returns the position at which the item was
528 * inserted.
529 */
530
531SHORT winhInsertMenuSeparator(HWND hMenu, // in: menu to add separator to
532 SHORT iPosition, // in: index where to add separator or MIT_END
533 SHORT sId) // in: separator menu ID (doesn't really matter)
534{
535 MENUITEM mi;
536 mi.iPosition = iPosition;
537 mi.afStyle = MIS_SEPARATOR; // append separator
538 mi.afAttribute = 0;
539 mi.id = sId;
540 mi.hwndSubMenu = 0;
541 mi.hItem = 0;
542
543 return (SHORT1FROMMR(WinSendMsg(hMenu,
544 MM_INSERTITEM,
545 (MPARAM)&mi,
546 (MPARAM)"")));
547}
548
549/*
550 *@@ winhCopyMenuItem:
551 * copies a menu item from hmenuSource to hmenuTarget.
552 *
553 * This creates a full duplicate. If usID specifies
554 * a submenu, the entire submenu is copied as well
555 * (this will then recurse).
556 *
557 * NOTE: Copying submenus will work only if each item
558 * in the submenu has a unique menu ID. This is due
559 * to the dumb implementation of menus in PM where
560 * it is impossible to query menu items without
561 * knowing their ID.
562 *
563 *@@added V0.9.9 (2001-03-09) [umoeller]
564 */
565
566BOOL winhCopyMenuItem(HWND hmenuTarget,
567 HWND hmenuSource,
568 USHORT usID,
569 SHORT sTargetPosition) // in: position to insert at or MIT_END
570{
571 BOOL brc = FALSE;
572 MENUITEM mi = {0};
573 if (WinSendMsg(hmenuSource,
574 MM_QUERYITEM,
575 MPFROM2SHORT(usID,
576 FALSE),
577 (MPARAM)&mi))
578 {
579 // found in source:
580 // is it a separator?
581 if (mi.afStyle & MIS_SEPARATOR)
582 winhInsertMenuSeparator(hmenuTarget,
583 sTargetPosition,
584 usID);
585 else
586 {
587 // no separator:
588 // get item text
589 PSZ pszSource = winhQueryMenuItemText(hmenuSource,
590 usID);
591 if (pszSource)
592 {
593 if ( (mi.afStyle & MIS_SUBMENU)
594 && (mi.hwndSubMenu)
595 )
596 {
597 // this is the top of a submenu:
598 HWND hwndSubMenu = winhInsertSubmenu(hmenuTarget,
599 sTargetPosition,
600 mi.id,
601 pszSource,
602 mi.afStyle,
603 0,
604 NULL,
605 0,
606 0);
607 if (hwndSubMenu)
608 {
609 // now copy all the items in the submenu
610 SHORT cMenuItems = SHORT1FROMMR(WinSendMsg(mi.hwndSubMenu,
611 MM_QUERYITEMCOUNT,
612 0, 0));
613 // loop through all entries in the original submenu
614 ULONG i;
615 for (i = 0;
616 i < cMenuItems;
617 i++)
618 {
619 // CHAR szItemText[100];
620 SHORT id = SHORT1FROMMR(WinSendMsg(mi.hwndSubMenu,
621 MM_ITEMIDFROMPOSITION,
622 MPFROMSHORT(i),
623 0));
624 // recurse
625 winhCopyMenuItem(hwndSubMenu,
626 mi.hwndSubMenu,
627 id,
628 MIT_END);
629 }
630
631 // now check... was the original submenu
632 // "conditional cascade"?
633 if (WinQueryWindowULong(mi.hwndSubMenu,
634 QWL_STYLE)
635 & MS_CONDITIONALCASCADE)
636 // yes:
637 {
638 // get the original default item
639 SHORT sDefID = SHORT1FROMMR(WinSendMsg(mi.hwndSubMenu,
640 MM_QUERYDEFAULTITEMID,
641 0, 0));
642 // set "conditional cascade style" on target too
643 WinSetWindowBits(hwndSubMenu,
644 QWL_STYLE,
645 MS_CONDITIONALCASCADE,
646 MS_CONDITIONALCASCADE);
647 // and set default item id
648 WinSendMsg(hwndSubMenu,
649 MM_SETDEFAULTITEMID,
650 (MPARAM)sDefID,
651 0);
652 }
653 } // end if (hwndSubmenu)
654 } // end if ( (mi.afStyle & MIS_SUBMENU)
655 else
656 {
657 // no submenu:
658 // just copy that item
659 SHORT s;
660 mi.iPosition = sTargetPosition;
661 s = SHORT1FROMMR(WinSendMsg(hmenuTarget,
662 MM_INSERTITEM,
663 MPFROMP(&mi),
664 MPFROMP(pszSource)));
665 if (s != MIT_MEMERROR && s != MIT_ERROR)
666 brc = TRUE;
667 }
668
669 free(pszSource);
670 } // end if (pszSource)
671 } // end else if (mi.afStyle & MIS_SEPARATOR)
672 } // end if (WinSendMsg(hmenuSource, MM_QUERYITEM,...
673
674 return (brc);
675}
676
677/*
678 *@@ winhMergeIntoSubMenu:
679 * creates a new submenu in hmenuTarget with the
680 * specified title at the specified position
681 * and copies the entire contents of hmenuSource
682 * into that.
683 *
684 * Returns the window handle of the new submenu
685 * or NULLHANDLE on errors.
686 *
687 * NOTE: Copying submenus will work only if each item
688 * in the submenu has a unique menu ID. This is due
689 * to the dumb implementation of menus in PM where
690 * it is impossible to query menu items without
691 * knowing their ID.
692 *
693 *@@added V0.9.9 (2001-03-09) [umoeller]
694 */
695
696HWND winhMergeIntoSubMenu(HWND hmenuTarget, // in: menu where to create submenu
697 SHORT sTargetPosition, // in: position to insert at or MIT_END
698 const char *pcszTitle, // in: title of new submenu
699 SHORT sID, // in: ID of new submenu
700 HWND hmenuSource) // in: menu to merge
701{
702 HWND hwndNewSubmenu = WinCreateMenu(hmenuTarget, NULL);
703 if (hwndNewSubmenu)
704 {
705 MENUITEM mi = {0};
706 SHORT src = 0;
707 // SHORT s = 0;
708 mi.iPosition = MIT_END;
709 mi.afStyle = MIS_TEXT | MIS_SUBMENU;
710 mi.id = 2000;
711 mi.hwndSubMenu = hwndNewSubmenu;
712
713 WinSetWindowUShort(hwndNewSubmenu, QWS_ID, sID);
714
715 // insert new submenu into hmenuTarget
716 src = SHORT1FROMMR(WinSendMsg(hmenuTarget,
717 MM_INSERTITEM,
718 (MPARAM)&mi,
719 (MPARAM)pcszTitle));
720 if ( (src != MIT_MEMERROR)
721 && (src != MIT_ERROR)
722 )
723 {
724 int i;
725 SHORT cMenuItems = SHORT1FROMMR(WinSendMsg(hmenuSource,
726 MM_QUERYITEMCOUNT,
727 0, 0));
728
729 // loop through all entries in the original menu
730 for (i = 0; i < cMenuItems; i++)
731 {
732 SHORT id = SHORT1FROMMR(WinSendMsg(hmenuSource,
733 MM_ITEMIDFROMPOSITION,
734 MPFROMSHORT(i),
735 0));
736 winhCopyMenuItem(hwndNewSubmenu,
737 hmenuSource,
738 id,
739 MIT_END);
740 }
741 }
742 else
743 {
744 // error:
745 WinDestroyWindow(hwndNewSubmenu);
746 hwndNewSubmenu = NULLHANDLE;
747 }
748 }
749
750 return (hwndNewSubmenu);
751}
752
753/*
754 *@@ winhQueryMenuItemText:
755 * this returns a menu item text as a PSZ
756 * to a newly allocated buffer or NULL if
757 * not found.
758 *
759 * Returns NULL on error. Use free()
760 * to free the return value.
761 *
762 * This uses MM_QUERYITEMTEXT internally.
763 * PMREF doesn't say anything about this,
764 * but from my testing this always recurses
765 * into submenus.
766 *
767 * Use the WinSetMenuItemText macro to
768 * set the menu item text.
769 */
770
771PSZ winhQueryMenuItemText(HWND hwndMenu,
772 USHORT usItemID) // in: menu item ID (not index)
773{
774 PSZ prc = NULL;
775
776 SHORT sLength = SHORT1FROMMR(WinSendMsg(hwndMenu,
777 MM_QUERYITEMTEXTLENGTH,
778 (MPARAM)(ULONG)usItemID,
779 (MPARAM)NULL));
780 if (sLength)
781 {
782 prc = (PSZ)malloc(sLength + 1);
783 WinSendMsg(hwndMenu,
784 MM_QUERYITEMTEXT,
785 MPFROM2SHORT(usItemID, sLength + 1),
786 (MPARAM)prc);
787 }
788
789 return (prc);
790}
791
792/*
793 *@@ winhAppend2MenuItemText:
794 *
795 *@@added V0.9.2 (2000-03-08) [umoeller]
796 */
797
798BOOL winhAppend2MenuItemText(HWND hwndMenu,
799 USHORT usItemID, // in: menu item ID (not index)
800 const char *pcszAppend, // in: text to append
801 BOOL fTab) // in: if TRUE, add \t before pcszAppend
802{
803 BOOL brc = FALSE;
804 CHAR szItemText[400];
805 if (WinSendMsg(hwndMenu,
806 MM_QUERYITEMTEXT,
807 MPFROM2SHORT(usItemID,
808 sizeof(szItemText)),
809 (MPARAM)szItemText))
810 {
811 // text copied:
812 if (fTab)
813 {
814 if (strchr(szItemText, '\t'))
815 // we already have a tab:
816 strcat(szItemText, " ");
817 else
818 strcat(szItemText, "\t");
819 }
820 strcat(szItemText, pcszAppend);
821
822 brc = (BOOL)WinSendMsg(hwndMenu,
823 MM_SETITEMTEXT,
824 MPFROMSHORT(usItemID),
825 (MPARAM)szItemText);
826 }
827
828 return (brc);
829}
830
831/*
832 *@@ winhMenuRemoveEllipse:
833 * removes a "..." substring from a menu item
834 * title, if found. This is useful if confirmations
835 * have been turned off for a certain menu item, which
836 * should be reflected in the menu.
837 */
838
839VOID winhMenuRemoveEllipse(HWND hwndMenu,
840 USHORT usItemId) // in: item to remove "..." from
841{
842 CHAR szBuf[255];
843 CHAR *p;
844 WinSendMsg(hwndMenu,
845 MM_QUERYITEMTEXT,
846 MPFROM2SHORT(usItemId, sizeof(szBuf)-1),
847 (MPARAM)&szBuf);
848 if ((p = strstr(szBuf, "...")))
849 strcpy(p, p+3);
850 WinSendMsg(hwndMenu,
851 MM_SETITEMTEXT,
852 MPFROMSHORT(usItemId),
853 (MPARAM)&szBuf);
854}
855
856/*
857 *@@ winhQueryItemUnderMouse:
858 * this queries the menu item which corresponds
859 * to the given mouse coordinates.
860 * Returns the ID of the menu item and stores its
861 * rectangle in *prtlItem; returns (-1) upon errors.
862 */
863
864SHORT winhQueryItemUnderMouse(HWND hwndMenu, // in: menu handle
865 POINTL *pptlMouse, // in: mouse coordinates
866 RECTL *prtlItem) // out: rectangle of menu item
867{
868 SHORT s, sItemId, sItemCount;
869 HAB habDesktop = WinQueryAnchorBlock(HWND_DESKTOP);
870
871 sItemCount = SHORT1FROMMR(WinSendMsg(hwndMenu, MM_QUERYITEMCOUNT, MPNULL, MPNULL));
872
873 for (s = 0;
874 s <= sItemCount;
875 s++)
876 {
877 sItemId = SHORT1FROMMR(WinSendMsg(hwndMenu,
878 MM_ITEMIDFROMPOSITION,
879 (MPARAM)(ULONG)s, MPNULL));
880 WinSendMsg(hwndMenu,
881 MM_QUERYITEMRECT,
882 MPFROM2SHORT(sItemId, FALSE),
883 (MPARAM)prtlItem);
884 if (WinPtInRect(habDesktop, prtlItem, pptlMouse))
885 return (sItemId);
886 }
887 /* sItemId = (SHORT)WinSendMsg(hwndMenu, MM_ITEMIDFROMPOSITION, (MPARAM)(sItemCount-1), MPNULL);
888 return (sItemId); */
889 return (-1); // error: no valid menu item
890}
891
892/*
893 *@@category: Helpers\PM helpers\Slider helpers
894 */
895
896/* ******************************************************************
897 *
898 * Slider helpers
899 *
900 ********************************************************************/
901
902/*
903 *@@ winhReplaceWithLinearSlider:
904 * this destroys the control with the ID ulID in hwndDlg
905 * and creates a linear slider at the same position with the
906 * same ID (effectively replacing it).
907 *
908 * This is needed because the IBM dialog editor (DLGEDIT.EXE)
909 * keeps crashing when creating sliders. So the way to do
910 * this easily is to create some other control with DLGEDIT
911 * where the slider should be later and call this function
912 * on that control when the dialog is initialized.
913 *
914 * You need to specify _one_ of the following with ulSliderStyle:
915 * -- SLS_HORIZONTAL: horizontal slider (default)
916 * -- SLS_VERTICAL: vertical slider
917 *
918 * plus _one_ additional common slider style for positioning:
919 * -- for horizontal sliders: SLS_BOTTOM, SLS_CENTER, or SLS_TOP
920 * -- for vertical sliders: SLS_LEFT, SLS_CENTER, or SLS_RIGHT
921 *
922 * Additional common slider styles are:
923 * -- SLS_PRIMARYSCALE1: determines the location of the scale
924 * on the slider shaft by using increment
925 * and spacing specified for scale 1 as
926 * the incremental value for positioning
927 * the slider arm. Scale 1 is displayed
928 * above the slider shaft of a horizontal
929 * slider and to the right of the slider
930 * shaft of a vertical slider. This is
931 * the default for a slider.
932 * -- SLS_PRIMARYSCALE2: not supported by this function
933 * -- SLS_READONLY: creates a read-only slider, which
934 * presents information to the user but
935 * allows no interaction with the user.
936 * -- SLS_RIBBONSTRIP: fills, as the slider arm moves, the
937 * slider shaft between the home position
938 * and the slider arm with a color value
939 * different from slider shaft color,
940 * similar to mercury in a thermometer.
941 * -- SLS_OWNERDRAW: notifies the application whenever the
942 * slider shaft, the ribbon strip, the
943 * slider arm, and the slider background
944 * are to be drawn.
945 * -- SLS_SNAPTOINCREMENT: causes the slider arm, when positioned
946 * between two values, to be positioned
947 * to the nearest value and redrawn at
948 * that position.
949 *
950 * Additionally, for horizontal sliders:
951 * -- SLS_BUTTONSLEFT: specifies that the optional slider
952 * buttons are to be used and places them
953 * to the left of the slider shaft. The
954 * buttons move the slider arm by one
955 * position, left or right, in the
956 * direction selected.
957 * -- SLS_BUTTONSRIGHT: specifies that the optional slider
958 * buttons are to be used and places them
959 * to the right of the slider shaft. The
960 * buttons move the slider arm by one
961 * position, left or right, in the
962 * direction selected.
963 * -- SLS_HOMELEFT: specifies the slider arm's home
964 * position. The left edge is used as the
965 * base value for incrementing (default).
966 * -- SLS_HOMERIGHT: specifies the slider arm's home
967 * position. The right edge is used as
968 * the base value for incrementing.
969 *
970 * Instead, for vertical sliders:
971 * -- SLS_BUTTONSBOTTOM: specifies that the optional slider
972 * buttons are to be used and places them
973 * at the bottom of the slider shaft. The
974 * buttons move the slider arm by one
975 * position, up or down, in the direction
976 * selected.
977 * -- SLS_BUTTONSTOP: specifies that the optional slider
978 * buttons are to be used and places them
979 * at the top of the slider shaft. The
980 * buttons move the slider arm by one
981 * position, up or down, in the direction
982 * selected.
983 * -- SLS_HOMEBOTTOM: specifies the slider arm's home
984 * position. The bottom of the slider is
985 * used as the base value for
986 * incrementing.
987 * -- SLS_HOMETOP: specifies the slider arm's home
988 * position. The top of the slider is
989 * used as the base value for
990 * incrementing.
991 *
992 * Notes: This function automatically adds WS_PARENTCLIP,
993 * WS_TABSTOP, and WS_SYNCPAINT to the specified styles.
994 * For the WS_TABSTOP style, hwndInsertAfter is important.
995 * If you specify HWND_TOP, your window will be the first
996 * in the tab stop list.
997 *
998 * It also shows the slider after having done all the
999 * processing in here by calling WinShowWindow.
1000 *
1001 * Also, we only provide support for scale 1 here, so
1002 * do not specify SLS_PRIMARYSCALE2 with ulSliderStyle,
1003 * and we have the slider calculate all the spacings.
1004 *
1005 * This returns the HWND of the slider or NULLHANDLE upon
1006 * errors.
1007 *
1008 *@@added V0.9.0 [umoeller]
1009 */
1010
1011HWND winhReplaceWithLinearSlider(HWND hwndParent, // in: parent of old control and slider
1012 HWND hwndOwner, // in: owner of old control and slider
1013 HWND hwndInsertAfter, // in: the control after which the slider should
1014 // come up, or HWND_TOP, or HWND_BOTTOM
1015 ULONG ulID, // in: ID of old control and slider
1016 ULONG ulSliderStyle, // in: SLS_* styles
1017 ULONG ulTickCount) // in: number of ticks (scale 1)
1018{
1019 HWND hwndSlider = NULLHANDLE;
1020 HWND hwndKill = WinWindowFromID(hwndParent, ulID);
1021 if (hwndKill)
1022 {
1023 SWP swpControl;
1024 if (WinQueryWindowPos(hwndKill, &swpControl))
1025 {
1026 SLDCDATA slcd;
1027
1028 // destroy the old control
1029 WinDestroyWindow(hwndKill);
1030
1031 // initialize slider control data
1032 slcd.cbSize = sizeof(SLDCDATA);
1033 slcd.usScale1Increments = ulTickCount;
1034 slcd.usScale1Spacing = 0; // have slider calculate it
1035 slcd.usScale2Increments = 0;
1036 slcd.usScale2Spacing = 0;
1037
1038 // create a slider with the same ID at the same
1039 // position
1040 hwndSlider = WinCreateWindow(hwndParent,
1041 WC_SLIDER,
1042 NULL, // no window text
1043 ulSliderStyle
1044 | WS_PARENTCLIP
1045 | WS_SYNCPAINT
1046 | WS_TABSTOP,
1047 swpControl.x,
1048 swpControl.y,
1049 swpControl.cx,
1050 swpControl.cy,
1051 hwndOwner,
1052 hwndInsertAfter,
1053 ulID, // same ID as destroyed control
1054 &slcd, // slider control data
1055 NULL); // presparams
1056
1057 WinSendMsg(hwndSlider,
1058 SLM_SETTICKSIZE,
1059 MPFROM2SHORT(SMA_SETALLTICKS,
1060 6), // 15 pixels high
1061 NULL);
1062
1063 WinShowWindow(hwndSlider, TRUE);
1064 }
1065 }
1066
1067 return (hwndSlider);
1068}
1069
1070/*
1071 *@@ winhSetSliderTicks:
1072 * this adds ticks to the given linear slider,
1073 * which are ulPixels pixels high. A useful
1074 * value for this is 4.
1075 *
1076 * This queries the slider for the primary
1077 * scale values. Only the primary scale is
1078 * supported.
1079 *
1080 * This function goes sets the ticks twice,
1081 * once with mpEveryOther1 and ulPixels1,
1082 * and then a second time with mpEveryOther2
1083 * and ulPixels2. This allows you to quickly
1084 * give, say, every tenth item a taller tick.
1085 *
1086 * For every set, if mpEveryOther is 0, this sets
1087 * all ticks on the primary slider scale.
1088 *
1089 * If mpEveryOther is != 0, SHORT1FROMMP
1090 * specifies the first tick to set, and
1091 * SHORT2FROMMP specifies every other tick
1092 * to set from there. For example:
1093 *
1094 + MPFROM2SHORT(9, 10)
1095 *
1096 * would set tick 9, 19, 29, and so forth.
1097 *
1098 * If both mpEveryOther and ulPixels are -1,
1099 * that set is skipped.
1100 *
1101 * Example: Considering a slider with a
1102 * primary scale from 0 to 30, using
1103 *
1104 + winhSetSliderTicks(hwndSlider,
1105 + 0, // every tick
1106 + 3, // to three pixels
1107 + MPFROM2SHORT(9, 10) // then every tenth
1108 + 6); // to six pixels.
1109 *
1110 * Returns FALSE upon errors.
1111 *
1112 *@@added V0.9.1 (99-12-04) [umoeller]
1113 *@@changed V0.9.7 (2001-01-18) [umoeller]: added second set
1114 */
1115
1116BOOL winhSetSliderTicks(HWND hwndSlider, // in: linear slider
1117 MPARAM mpEveryOther1, // in: set 1
1118 ULONG ulPixels1,
1119 MPARAM mpEveryOther2, // in: set 2
1120 ULONG ulPixels2)
1121{
1122 BOOL brc = FALSE;
1123
1124 ULONG ulSet;
1125 MPARAM mpEveryOther = mpEveryOther1;
1126 ULONG ulPixels = ulPixels1;
1127
1128 // do this twice
1129 for (ulSet = 0;
1130 ulSet < 2;
1131 ulSet++)
1132 {
1133 if (mpEveryOther == 0)
1134 {
1135 // set all ticks:
1136 brc = (BOOL)WinSendMsg(hwndSlider,
1137 SLM_SETTICKSIZE,
1138 MPFROM2SHORT(SMA_SETALLTICKS,
1139 ulPixels),
1140 NULL);
1141 }
1142 else if ( (mpEveryOther != (MPARAM)-1) && (ulPixels != -1) )
1143 {
1144 SLDCDATA slcd;
1145 WNDPARAMS wp;
1146 memset(&wp, 0, sizeof(WNDPARAMS));
1147 wp.fsStatus = WPM_CTLDATA;
1148 wp.cbCtlData = sizeof(slcd);
1149 wp.pCtlData = &slcd;
1150 // get primary scale data from the slider
1151 if (WinSendMsg(hwndSlider,
1152 WM_QUERYWINDOWPARAMS,
1153 (MPARAM)&wp,
1154 0))
1155 {
1156 USHORT usStart = SHORT1FROMMP(mpEveryOther),
1157 usEveryOther = SHORT2FROMMP(mpEveryOther);
1158
1159 USHORT usScale1Max = slcd.usScale1Increments,
1160 us;
1161
1162 brc = TRUE;
1163
1164 for (us = usStart; us < usScale1Max; us += usEveryOther)
1165 {
1166 if (!(BOOL)WinSendMsg(hwndSlider,
1167 SLM_SETTICKSIZE,
1168 MPFROM2SHORT(us,
1169 ulPixels),
1170 NULL))
1171 {
1172 brc = FALSE;
1173 break;
1174 }
1175 }
1176 }
1177 }
1178
1179 // for the second loop, use second value set
1180 mpEveryOther = mpEveryOther2;
1181 ulPixels = ulPixels2;
1182 // we only loop twice
1183 } // end for (ulSet = 0; ulSet < 2;
1184
1185 return (brc);
1186}
1187
1188/*
1189 *@@ winhReplaceWithCircularSlider:
1190 * this destroys the control with the ID ulID in hwndDlg
1191 * and creates a linear slider at the same position with the
1192 * same ID (effectively replacing it).
1193 *
1194 * This is needed because the IBM dialog editor (DLGEDIT.EXE)
1195 * cannot create circular sliders. So the way to do this
1196 * easily is to create some other control with DLGEDIT
1197 * where the slider should be later and call this function
1198 * on that control when the dialog is initialized.
1199 *
1200 * You need to specify the following with ulSliderStyle:
1201 * -- CSS_CIRCULARVALUE: draws a circular thumb, rather than a line,
1202 * for the value indicator.
1203 * -- CSS_MIDPOINT: makes the mid-point tick mark larger.
1204 * -- CSS_NOBUTTON: does not display value buttons. Per default, the
1205 * slider displays "-" and "+" buttons to the bottom left
1206 * and bottom right of the knob. (BTW, these bitmaps can be
1207 * changed using CSM_SETBITMAPDATA.)
1208 * -- CSS_NONUMBER: does not display the value on the dial.
1209 * -- CSS_NOTEXT: does not display title text under the dial.
1210 * Otherwise, the text in the pszTitle parameter
1211 * will be used.
1212 * -- CSS_NOTICKS (only listed in pmstddlg.h, not in PMREF):
1213 * obviously, this prevents tick marks from being drawn.
1214 * -- CSS_POINTSELECT: permits the values on the circular slider
1215 * to change immediately when dragged.
1216 * Direct manipulation is performed by using a mouse to
1217 * click on and drag the circular slider. There are two
1218 * modes of direct manipulation for the circular slider:
1219 * <BR><B>1)</B> The default direct manipulation mode is to scroll to
1220 * the value indicated by the position of the mouse.
1221 * This could be important if you used a circular slider
1222 * for a volume control, for example. Increasing the volume
1223 * from 0% to 100% too quickly could result in damage to
1224 * both the user's ears and the equipment.
1225 * <BR><B>2)</B>The other mode of direct manipulation permits
1226 * the value on the circular slider to change immediately when dragged.
1227 * This mode is enabled using the CSS_POINTSELECT style bit. When this
1228 * style is used, the value of the dial can be changed by tracking
1229 * the value with the mouse, which changes values quickly.
1230 * -- CSS_PROPORTIONALTICKS: allow the length of the tick marks to be calculated
1231 * as a percentage of the radius (for small sliders).
1232 * -- CSS_360: permits the scroll range to extend 360 degrees.
1233 * CSS_360 forces the CSS_NONUMBER style on. This is necessary
1234 * to keep the value indicator from corrupting the number value.
1235 *
1236 * FYI: The most commonly known circular slider in OS/2, the one in the
1237 * default "Sound" object, has a style of 0x9002018a, meaning
1238 * CSS_NOTEXT | CSS_POINTSELECT | CSS_NOTICKS.
1239 *
1240 * Notes: This function automatically adds WS_PARENTCLIP,
1241 * WS_TABSTOP, and WS_SYNCPAINT to the specified styles.
1242 * For the WS_TABSTOP style, hwndInsertAfter is important.
1243 * If you specify HWND_TOP, your window will be the first
1244 * in the tab stop list.
1245 *
1246 * It also shows the slider after having done all the
1247 * processing in here by calling WinShowWindow.
1248 *
1249 * This returns the HWND of the slider or NULLHANDLE upon
1250 * errors.
1251 *
1252 *@@added V0.9.0 [umoeller]
1253 */
1254
1255HWND winhReplaceWithCircularSlider(HWND hwndParent, // in: parent of old control and slider
1256 HWND hwndOwner, // in: owner of old control and slider
1257 HWND hwndInsertAfter, // in: the control after which the slider should
1258 // come up, or HWND_TOP, or HWND_BOTTOM
1259 ULONG ulID, // in: ID of old control and slider
1260 ULONG ulSliderStyle, // in: SLS_* styles
1261 SHORT sMin, // in: minimum value (e.g. 0)
1262 SHORT sMax, // in: maximum value (e.g. 100)
1263 USHORT usIncrement, // in: minimum increment (e.g. 1)
1264 USHORT usTicksEvery) // in: ticks ever x values (e.g. 20)
1265{
1266 HWND hwndSlider = NULLHANDLE;
1267 HWND hwndKill = WinWindowFromID(hwndParent, ulID);
1268 if (hwndKill)
1269 {
1270 SWP swpControl;
1271 if (WinQueryWindowPos(hwndKill, &swpControl))
1272 {
1273 // destroy the old control
1274 WinDestroyWindow(hwndKill);
1275
1276 // WinRegisterCircularSlider();
1277
1278 // create a slider with the same ID at the same
1279 // position
1280 hwndSlider = WinCreateWindow(hwndParent,
1281 WC_CIRCULARSLIDER,
1282 "dummy", // no window text
1283 ulSliderStyle
1284 // | WS_PARENTCLIP
1285 // | WS_SYNCPAINT
1286 | WS_TABSTOP,
1287 swpControl.x,
1288 swpControl.y,
1289 swpControl.cx,
1290 swpControl.cy,
1291 hwndOwner,
1292 hwndInsertAfter,
1293 ulID, // same ID as destroyed control
1294 NULL, // control data
1295 NULL); // presparams
1296
1297 if (hwndSlider)
1298 {
1299 // set slider range
1300 WinSendMsg(hwndSlider,
1301 CSM_SETRANGE,
1302 (MPARAM)(ULONG)sMin,
1303 (MPARAM)(ULONG)sMax);
1304
1305 // set slider increments
1306 WinSendMsg(hwndSlider,
1307 CSM_SETINCREMENT,
1308 (MPARAM)(ULONG)usIncrement,
1309 (MPARAM)(ULONG)usTicksEvery);
1310
1311 // set slider value
1312 WinSendMsg(hwndSlider,
1313 CSM_SETVALUE,
1314 (MPARAM)0,
1315 (MPARAM)0);
1316
1317 // for some reason, the slider always has
1318 // WS_CLIPSIBLINGS set, even though we don't
1319 // set this; we must unset this now, or
1320 // the slider won't draw itself (%&$&%"$&%!!!)
1321 WinSetWindowBits(hwndSlider,
1322 QWL_STYLE,
1323 0, // unset bit
1324 WS_CLIPSIBLINGS);
1325
1326 WinShowWindow(hwndSlider, TRUE);
1327 }
1328 }
1329 }
1330
1331 return (hwndSlider);
1332}
1333
1334/*
1335 *@@category: Helpers\PM helpers\Spin button helpers
1336 */
1337
1338/* ******************************************************************
1339 *
1340 * Spin button helpers
1341 *
1342 ********************************************************************/
1343
1344/*
1345 *@@ winhSetDlgItemSpinData:
1346 * sets a spin button's limits and data within a dialog window.
1347 * This only works for decimal spin buttons.
1348 */
1349
1350VOID winhSetDlgItemSpinData(HWND hwndDlg, // in: dlg window
1351 ULONG idSpinButton, // in: item ID of spin button
1352 ULONG min, // in: minimum allowed value
1353 ULONG max, // in: maximum allowed value
1354 ULONG current) // in: new current value
1355{
1356 HWND hwndSpinButton = WinWindowFromID(hwndDlg, idSpinButton);
1357 if (hwndSpinButton)
1358 {
1359 WinSendMsg(hwndSpinButton,
1360 SPBM_SETLIMITS, // Set limits message
1361 (MPARAM)max, // Spin Button maximum setting
1362 (MPARAM)min); // Spin Button minimum setting
1363
1364 WinSendMsg(hwndSpinButton,
1365 SPBM_SETCURRENTVALUE, // Set current value message
1366 (MPARAM)current,
1367 (MPARAM)NULL);
1368 }
1369}
1370
1371/*
1372 *@@ winhAdjustDlgItemSpinData:
1373 * this can be called on a spin button control to
1374 * have its current data snap to a grid. This only
1375 * works for LONG integer values.
1376 *
1377 * For example, if you specify 100 for the grid and call
1378 * this func after you have received SPBN_UP/DOWNARROW,
1379 * the spin button's value will always in/decrease
1380 * so that the spin button's value is a multiple of 100.
1381 *
1382 * By contrast, if (lGrid < 0), this will not really
1383 * snap the value to a multiple of -lGrid, but instead
1384 * in/decrease the value by -lGrid. The value will not
1385 * necessarily be a multiple of the grid. (0.9.14)
1386 *
1387 * This returns the "snapped" value to which the spin
1388 * button was set.
1389 *
1390 * If you specify lGrid == 0, this returns the spin
1391 * button's value only without snapping (V0.9.0).
1392 *
1393 *@@changed V0.9.0 [umoeller]: added check for lGrid == 0 (caused division by zero previously)
1394 *@@changed V0.9.14 (2001-08-03) [umoeller]: added fixes for age-old problems with wrap around
1395 *@@changed V0.9.14 (2001-08-03) [umoeller]: added lGrid < 0 mode
1396 */
1397
1398LONG winhAdjustDlgItemSpinData(HWND hwndDlg, // in: dlg window
1399 USHORT usItemID, // in: item ID of spin button
1400 LONG lGrid, // in: grid
1401 USHORT usNotifyCode) // in: SPBN_UP* or *DOWNARROW of WM_CONTROL message
1402{
1403 HWND hwndSpin = WinWindowFromID(hwndDlg, usItemID);
1404 LONG lBottom, lTop, lValue;
1405
1406 // get value, which has already increased /
1407 // decreased by 1
1408 WinSendMsg(hwndSpin,
1409 SPBM_QUERYVALUE,
1410 (MPARAM)&lValue,
1411 MPFROM2SHORT(0, SPBQ_ALWAYSUPDATE));
1412
1413 if ((lGrid)
1414 && ( (usNotifyCode == SPBN_UPARROW)
1415 || (usNotifyCode == SPBN_DOWNARROW)
1416 )
1417 )
1418 {
1419 // only if the up/down buttons were pressed,
1420 // snap to the nearest grid; if the user
1421 // manually enters something (SPBN_CHANGE),
1422 // we'll accept that value
1423 LONG lChanged = (usNotifyCode == SPBN_UPARROW)
1424 // if the spin button went up, subtract 1
1425 ? -1
1426 : +1;
1427 LONG lPrev = lValue + lChanged;
1428
1429 // if grid is negative, it is assumed to
1430 // not be a "real" grid but jump in those
1431 // steps only
1432 if (lGrid < 0)
1433 {
1434 // add /subtract grid
1435 if (usNotifyCode == SPBN_UPARROW)
1436 lValue = lPrev - lGrid;
1437 else
1438 lValue = lPrev + lGrid;
1439
1440 // lValue = (lValue / lGrid) * lGrid;
1441 }
1442 else
1443 {
1444 // add /subtract grid
1445 if (usNotifyCode == SPBN_UPARROW)
1446 lValue = lPrev + lGrid;
1447 else
1448 lValue = lPrev - lGrid;
1449
1450 lValue = (lValue / lGrid) * lGrid;
1451 }
1452
1453 // balance with spin button limits
1454 WinSendMsg(hwndSpin,
1455 SPBM_QUERYLIMITS,
1456 (MPARAM)&lTop,
1457 (MPARAM)&lBottom);
1458 if (lValue < lBottom)
1459 lValue = lTop;
1460 else if (lValue > lTop)
1461 lValue = lBottom;
1462
1463 WinSendMsg(hwndSpin,
1464 SPBM_SETCURRENTVALUE,
1465 (MPARAM)(lValue),
1466 MPNULL);
1467 }
1468 return (lValue);
1469}
1470
1471/*
1472 *@@category: Helpers\PM helpers\List box helpers
1473 */
1474
1475/* ******************************************************************
1476 *
1477 * List box helpers
1478 *
1479 ********************************************************************/
1480
1481/*
1482 *@@ winhQueryLboxItemText:
1483 * returns the text of the specified
1484 * list box item in a newly allocated
1485 * buffer.
1486 *
1487 * Returns NULL on error. Use fre()
1488 * to free the return value.
1489 *
1490 *@@added V0.9.1 (99-12-14) [umoeller]
1491 */
1492
1493PSZ winhQueryLboxItemText(HWND hwndListbox,
1494 SHORT sIndex)
1495{
1496 PSZ pszReturn = 0;
1497 SHORT cbText = SHORT1FROMMR(WinSendMsg(hwndListbox,
1498 LM_QUERYITEMTEXTLENGTH,
1499 (MPARAM)(ULONG)sIndex,
1500 0));
1501 if ((cbText) && (cbText != LIT_ERROR))
1502 {
1503 pszReturn = (PSZ)malloc(cbText + 1); // add zero terminator
1504 WinSendMsg(hwndListbox,
1505 LM_QUERYITEMTEXT,
1506 MPFROM2SHORT(sIndex,
1507 cbText + 1),
1508 (MPARAM)pszReturn);
1509 }
1510
1511 return (pszReturn);
1512}
1513
1514/*
1515 *@@ winhMoveLboxItem:
1516 * this moves one list box item from one
1517 * list box to another, including the
1518 * item text and the item "handle"
1519 * (see LM_QUERYITEMHANDLE).
1520 *
1521 * sTargetIndex can either be a regular
1522 * item index or one of the following
1523 * (as in LM_INSERTITEM):
1524 * -- LIT_END
1525 * -- LIT_SORTASCENDING
1526 * -- LIT_SORTDESCENDING
1527 *
1528 * If (fSelectTarget == TRUE), the new
1529 * item is also selected in the target
1530 * list box.
1531 *
1532 * Returns FALSE if moving failed. In
1533 * that case, the list boxes are unchanged.
1534 *
1535 *@@added V0.9.1 (99-12-14) [umoeller]
1536 */
1537
1538BOOL winhMoveLboxItem(HWND hwndSource,
1539 SHORT sSourceIndex,
1540 HWND hwndTarget,
1541 SHORT sTargetIndex,
1542 BOOL fSelectTarget)
1543{
1544 BOOL brc = FALSE;
1545
1546 PSZ pszItemText = winhQueryLboxItemText(hwndSource, sSourceIndex);
1547 if (pszItemText)
1548 {
1549 ULONG ulItemHandle = winhQueryLboxItemHandle(hwndSource,
1550 sSourceIndex);
1551 // probably 0, if not used
1552 LONG lTargetIndex = WinInsertLboxItem(hwndTarget,
1553 sTargetIndex,
1554 pszItemText);
1555 if ( (lTargetIndex != LIT_ERROR)
1556 && (lTargetIndex != LIT_MEMERROR)
1557 )
1558 {
1559 // successfully inserted:
1560 winhSetLboxItemHandle(hwndTarget, lTargetIndex, ulItemHandle);
1561 if (fSelectTarget)
1562 winhSetLboxSelectedItem(hwndTarget, lTargetIndex, TRUE);
1563
1564 // remove source
1565 WinDeleteLboxItem(hwndSource,
1566 sSourceIndex);
1567
1568 brc = TRUE;
1569 }
1570
1571 free(pszItemText);
1572 }
1573
1574 return (brc);
1575}
1576
1577/*
1578 *@@ winhLboxSelectAll:
1579 * this selects or deselects all items in the
1580 * given list box, depending on fSelect.
1581 *
1582 * Returns the number of items in the list box.
1583 */
1584
1585ULONG winhLboxSelectAll(HWND hwndListBox, // in: list box
1586 BOOL fSelect) // in: TRUE = select, FALSE = deselect
1587{
1588 LONG lItemCount = WinQueryLboxCount(hwndListBox);
1589 ULONG ul;
1590
1591 for (ul = 0; ul < lItemCount; ul++)
1592 {
1593 WinSendMsg(hwndListBox,
1594 LM_SELECTITEM,
1595 (MPARAM)ul, // index
1596 (MPARAM)fSelect);
1597 }
1598
1599 return (lItemCount);
1600}
1601
1602/*
1603 *@@ winhLboxFindItemFromHandle:
1604 * finds the list box item with the specified
1605 * handle.
1606 *
1607 * Of course this only makes sense if each item
1608 * has a unique handle indeed.
1609 *
1610 * Returns the index of the item found or -1.
1611 *
1612 *@@added V0.9.12 (2001-05-18) [umoeller]
1613 */
1614
1615ULONG winhLboxFindItemFromHandle(HWND hwndListBox,
1616 ULONG ulHandle)
1617{
1618 LONG cItems;
1619 if (cItems = WinQueryLboxCount(hwndListBox))
1620 {
1621 ULONG ul;
1622 for (ul = 0;
1623 ul < cItems;
1624 ul++)
1625 {
1626 if (ulHandle == winhQueryLboxItemHandle(hwndListBox,
1627 ul))
1628 return (ul);
1629 }
1630 }
1631
1632 return (-1);
1633}
1634
1635/*
1636 *@@category: Helpers\PM helpers\Scroll bar helpers
1637 */
1638
1639/* ******************************************************************
1640 *
1641 * Scroll bar helpers
1642 *
1643 ********************************************************************/
1644
1645/*
1646 *@@ winhUpdateScrollBar:
1647 * updates the given scroll bar according to the given
1648 * values. This updates the scroll bar's thumb size,
1649 * extension, and position, all in one shot.
1650 *
1651 * This function usually gets called when the window is
1652 * created and later when the window is resized.
1653 *
1654 * This simplifies the typical functionality of a scroll
1655 * bar in a client window which is to be scrolled. I am
1656 * wondering why IBM never included such a function, since
1657 * it is so damn basic and still writing it cost me a whole
1658 * day.
1659 *
1660 * Terminology:
1661 *
1662 * -- "window": the actual window with scroll bars which displays
1663 * a subrectangle of the available data. With a typical PM
1664 * application, this will be your client window.
1665 *
1666 * The width or height of this must be passed in ulWinPels.
1667 *
1668 * -- "viewport": the entire data to be displayed, of which the
1669 * "window" can only display a subrectangle, if the viewport
1670 * is larger than the window.
1671 *
1672 * The width or height of this must be passed in ulViewportPels.
1673 * This can be smaller than ulWinPels (if the window is larger
1674 * than the data) or the same or larger than ulWinPels
1675 * (if the window is too small to show all the data).
1676 *
1677 * -- "window offset": the offset of the current window within
1678 * the viewport.
1679 *
1680 * For horizontal scroll bars, this is the X coordinate,
1681 * counting from the left of the window (0 means leftmost).
1682 *
1683 * For vertical scroll bars, this is counted from the _top_
1684 * of the viewport (0 means topmost, as opposed to OS/2
1685 * window coordinates!). This is because for vertical scroll
1686 * bars controls, higher values move the thumb _down_. Yes
1687 * indeed, this conflicts with PM's coordinate system.
1688 *
1689 * The window offset is therefore always positive.
1690 *
1691 * The scroll bar gets disabled if the entire viewport is visible,
1692 * that is, if ulViewportPels <= ulWinPels. In that case
1693 * FALSE is returned. If (fAutoHide == TRUE), the scroll
1694 * bar is not only disabled, but also hidden from the display.
1695 * In that case, you will need to reformat your output because
1696 * your viewport becomes larger without the scroll bar.
1697 *
1698 * This function will set the range of the scroll bar to 0 up
1699 * to a value depending on the viewport size. For vertical scroll
1700 * bars, 0 means topmost (which is kinda sick with the OS/2
1701 * coordinate system), for horizontal scroll bars, 0 means leftmost.
1702 *
1703 * The maximum value of the scroll bar will be
1704 *
1705 + (ulViewportPels - ulWinPels) / usScrollUnitPels
1706 *
1707 * The thumb size of the scroll bar will also be adjusted
1708 * based on the viewport and window size, as it should be.
1709 *
1710 *@@added V0.9.1 (2000-02-14) [umoeller]
1711 *@@changed V0.9.3 (2000-04-30) [umoeller]: fixed pels/unit confusion
1712 *@@changed V0.9.3 (2000-05-08) [umoeller]: now handling scroll units automatically
1713 */
1714
1715BOOL winhUpdateScrollBar(HWND hwndScrollBar, // in: scroll bar (vertical or horizontal)
1716 ULONG ulWinPels, // in: vertical or horizontal dimension of
1717 // visible window part (in pixels),
1718 // excluding the scroll bar!
1719 ULONG ulViewportPels, // in: dimension of total data part, of
1720 // which ulWinPels is a sub-dimension
1721 // (in pixels);
1722 // if <= ulWinPels, the scrollbar will be
1723 // disabled
1724 ULONG ulCurPelsOfs, // in: current offset of visible part
1725 // (in pixels)
1726 BOOL fAutoHide) // in: hide scroll bar if disabled
1727{
1728 BOOL brc = FALSE;
1729
1730 // _Pmpf(("Entering winhUpdateScrollBar"));
1731
1732 // for large viewports, adjust scroll bar units
1733 USHORT usScrollUnitPels = 1;
1734 if (ulViewportPels > 10000)
1735 usScrollUnitPels = 100;
1736
1737 if (ulViewportPels > ulWinPels)
1738 {
1739 // scrollbar needed:
1740 USHORT usThumbDivisorUnits = usScrollUnitPels;
1741 USHORT lMaxAllowedUnitOfs;
1742 // _Pmpf(("winhUpdateScrollBar: ulViewportPels > ulWinPels, enabling scroller"));
1743 // divisor for thumb size (below)
1744 if (ulViewportPels > 10000)
1745 // for very large viewports, we need to
1746 // raise the divisor, because we only
1747 // have a USHORT
1748 usThumbDivisorUnits = usScrollUnitPels * 100;
1749
1750 // viewport is larger than window:
1751 WinEnableWindow(hwndScrollBar, TRUE);
1752 if (fAutoHide)
1753 WinShowWindow(hwndScrollBar, TRUE);
1754
1755 // calculate limit
1756 lMaxAllowedUnitOfs = ((ulViewportPels - ulWinPels + usScrollUnitPels)
1757 // scroll unit is 10
1758 / usScrollUnitPels);
1759
1760 // _Pmpf((" usCurUnitOfs: %d", ulCurUnitOfs));
1761 // _Pmpf((" usMaxUnits: %d", lMaxAllowedUnitOfs));
1762
1763 // set thumb position and limit
1764 WinSendMsg(hwndScrollBar,
1765 SBM_SETSCROLLBAR,
1766 (MPARAM)(ulCurPelsOfs), // / usThumbDivisorUnits), // position: 0 means top
1767 MPFROM2SHORT(0, // minimum
1768 lMaxAllowedUnitOfs)); // maximum
1769
1770 // set thumb size based on ulWinPels and
1771 // ulViewportPels
1772 WinSendMsg(hwndScrollBar,
1773 SBM_SETTHUMBSIZE,
1774 MPFROM2SHORT( ulWinPels / usThumbDivisorUnits, // visible
1775 ulViewportPels / usThumbDivisorUnits), // total
1776 0);
1777 brc = TRUE;
1778 }
1779 else
1780 {
1781 // _Pmpf(("winhUpdateScrollBar: ulViewportPels <= ulWinPels"));
1782 // entire viewport is visible:
1783 WinEnableWindow(hwndScrollBar, FALSE);
1784 if (fAutoHide)
1785 WinShowWindow(hwndScrollBar, FALSE);
1786 }
1787
1788 // _Pmpf(("End of winhUpdateScrollBar"));
1789
1790 return (brc);
1791}
1792
1793/*
1794 *@@ winhHandleScrollMsg:
1795 * this helper handles a WM_VSCROLL or WM_HSCROLL
1796 * message posted to a client window when the user
1797 * has worked on a client scroll bar. Calling this
1798 * function is ALL you need to do to handle those
1799 * two messages.
1800 *
1801 * This is most useful in conjunction with winhUpdateScrollBar.
1802 * See that function for the terminology also.
1803 *
1804 * This function calculates the new scrollbar position
1805 * (from the mp2 value, which can be line up/down,
1806 * page up/down, or slider track) and calls WinScrollWindow
1807 * accordingly. The window part which became invalid
1808 * because of the scrolling is automatically invalidated
1809 * (using WinInvalidateRect), so expect a WM_PAINT after
1810 * calling this function.
1811 *
1812 * This function assumes that the scrollbar operates
1813 * on values starting from zero. The maximum value
1814 * of the scroll bar is:
1815 *
1816 + ulViewportPels - (prcl2Scroll->yTop - prcl2Scroll->yBottom)
1817 *
1818 * This function also automatically changes the scroll bar
1819 * units, should you have a viewport size which doesn't fit
1820 * into the SHORT's that the scroll bar uses internally. As
1821 * a result, this function handles a the complete range of
1822 * a ULONG for the viewport.
1823 *
1824 * Replace "bottom" and "top" with "right" and "left" for
1825 * horizontal scrollbars in the above formula.
1826 *
1827 *@@added V0.9.1 (2000-02-13) [umoeller]
1828 *@@changed V0.9.3 (2000-04-30) [umoeller]: changed prototype, fixed pels/unit confusion
1829 *@@changed V0.9.3 (2000-05-08) [umoeller]: now handling scroll units automatically
1830 *@@changed V0.9.7 (2001-01-17) [umoeller]: changed PLONG to PULONG
1831 */
1832
1833BOOL winhHandleScrollMsg(HWND hwnd2Scroll, // in: client window to scroll
1834 HWND hwndScrollBar, // in: vertical or horizontal scroll bar window
1835 PULONG pulCurPelsOfs, // in/out: current viewport offset;
1836 // this is updated with the proper scroll units
1837 PRECTL prcl2Scroll, // in: hwnd2Scroll rectangle to scroll
1838 // (in window coordinates);
1839 // this is passed to WinScrollWindow,
1840 // which considers this inclusive!
1841 LONG ulViewportPels, // in: total viewport dimension,
1842 // into which *pulCurPelsOfs is an offset
1843 USHORT usLineStepPels, // in: pixels to scroll line-wise
1844 // (scroll bar buttons pressed)
1845 ULONG msg, // in: either WM_VSCROLL or WM_HSCROLL
1846 MPARAM mp2) // in: complete mp2 of WM_VSCROLL/WM_HSCROLL;
1847 // this has two SHORT's (usPos and usCmd),
1848 // see PMREF for details
1849{
1850 ULONG ulOldPelsOfs = *pulCurPelsOfs;
1851 USHORT usPosUnits = SHORT1FROMMP(mp2), // in scroll units
1852 usCmd = SHORT2FROMMP(mp2);
1853 LONG lMaxAllowedUnitOfs;
1854 ULONG ulWinPels;
1855
1856 // for large viewports, adjust scroll bar units
1857 USHORT usScrollUnitPels = 1;
1858 if (ulViewportPels > 10000)
1859 usScrollUnitPels = 100;
1860
1861 // calculate window size (vertical or horizontal)
1862 if (msg == WM_VSCROLL)
1863 ulWinPels = (prcl2Scroll->yTop - prcl2Scroll->yBottom);
1864 else
1865 ulWinPels = (prcl2Scroll->xRight - prcl2Scroll->xLeft);
1866
1867 lMaxAllowedUnitOfs = ((LONG)ulViewportPels - ulWinPels) / usScrollUnitPels;
1868
1869 // _Pmpf(("Entering winhHandleScrollMsg"));
1870
1871 switch (usCmd)
1872 {
1873 case SB_LINEUP:
1874 if (*pulCurPelsOfs > usLineStepPels)
1875 *pulCurPelsOfs -= usLineStepPels; // * usScrollUnitPels);
1876 else
1877 *pulCurPelsOfs = 0;
1878 break;
1879
1880 case SB_LINEDOWN:
1881 *pulCurPelsOfs += usLineStepPels; // * usScrollUnitPels);
1882 break;
1883
1884 case SB_PAGEUP:
1885 if (*pulCurPelsOfs > ulWinPels)
1886 *pulCurPelsOfs -= ulWinPels; // convert to units
1887 else
1888 *pulCurPelsOfs = 0;
1889 break;
1890
1891 case SB_PAGEDOWN:
1892 *pulCurPelsOfs += ulWinPels; // convert to units
1893 break;
1894
1895 case SB_SLIDERTRACK:
1896 *pulCurPelsOfs = (usPosUnits * usScrollUnitPels);
1897 // _Pmpf((" SB_SLIDERTRACK: usUnits = %d", usPosUnits));
1898 break;
1899
1900 case SB_SLIDERPOSITION:
1901 *pulCurPelsOfs = (usPosUnits * usScrollUnitPels);
1902 break;
1903 }
1904
1905 // are we close to the lower limit?
1906 /* if (*plCurUnitOfs < usLineStepUnits) // usScrollUnit)
1907 *plCurUnitOfs = 0;
1908 // are we close to the upper limit?
1909 else if (*plCurUnitOfs + usLineStepUnits > lMaxUnitOfs)
1910 {
1911 _Pmpf((" !!! limiting: %d to %d", *plCurUnitOfs, lMaxUnitOfs));
1912 *plCurUnitOfs = lMaxUnitOfs;
1913 } */
1914
1915 /* if (*plCurPelsOfs < 0)
1916 *plCurPelsOfs = 0; */ // checked above
1917 if (*pulCurPelsOfs > (lMaxAllowedUnitOfs * usScrollUnitPels))
1918 {
1919 *pulCurPelsOfs = (lMaxAllowedUnitOfs * usScrollUnitPels);
1920 }
1921 if ( (*pulCurPelsOfs != ulOldPelsOfs)
1922 || (*pulCurPelsOfs == 0)
1923 || (*pulCurPelsOfs == (lMaxAllowedUnitOfs * usScrollUnitPels))
1924 )
1925 {
1926 RECTL rcl2Scroll,
1927 rcl2Update;
1928
1929 // changed:
1930 WinSendMsg(hwndScrollBar,
1931 SBM_SETPOS,
1932 (MPARAM)(*pulCurPelsOfs / usScrollUnitPels), // / usScrollUnit),
1933 0);
1934 // scroll window rectangle:
1935 rcl2Scroll.xLeft = prcl2Scroll->xLeft;
1936 rcl2Scroll.xRight = prcl2Scroll->xRight;
1937 rcl2Scroll.yBottom = prcl2Scroll->yBottom;
1938 rcl2Scroll.yTop = prcl2Scroll->yTop;
1939
1940 if (msg == WM_VSCROLL)
1941 WinScrollWindow(hwnd2Scroll,
1942 0,
1943 (*pulCurPelsOfs - ulOldPelsOfs) // scroll units changed
1944 , // * usScrollUnitPels, // convert to pels
1945 &rcl2Scroll, // rcl to scroll
1946 prcl2Scroll, // clipping rect
1947 NULLHANDLE, // no region
1948 &rcl2Update,
1949 0);
1950 else
1951 WinScrollWindow(hwnd2Scroll,
1952 -(LONG)(*pulCurPelsOfs - ulOldPelsOfs) // scroll units changed
1953 , // * usScrollUnitPels,
1954 0,
1955 &rcl2Scroll, // rcl to scroll
1956 prcl2Scroll, // clipping rect
1957 NULLHANDLE, // no region
1958 &rcl2Update,
1959 0);
1960
1961 // WinScrollWindow has stored the invalid window
1962 // rectangle which needs to be repainted in rcl2Update:
1963 WinInvalidateRect(hwnd2Scroll, &rcl2Update, FALSE);
1964 }
1965
1966 // _Pmpf(("End of winhHandleScrollMsg"));
1967
1968 return (TRUE);
1969}
1970
1971/*
1972 *@@ winhProcessScrollChars:
1973 * helper for processing WM_CHAR messages for
1974 * client windows with scroll bars.
1975 *
1976 * If your window has scroll bars, you normally
1977 * need to process a number of keystrokes to be
1978 * able to scroll the window contents. This is
1979 * tiresome to code, so here is a helper.
1980 *
1981 * When receiving WM_CHAR, call this function.
1982 * If this returns TRUE, the keystroke has been
1983 * a scroll keystroke, and the window has been
1984 * updated (by sending WM_VSCROLL or WM_HSCROLL
1985 * to hwndClient). Otherwise, you should process
1986 * the keystroke as usual because it's not a
1987 * scroll keystroke.
1988 *
1989 * The following keystrokes are processed here:
1990 *
1991 * -- "cursor up, down, right, left": scroll one
1992 * line in the proper direction.
1993 * -- "page up, down": scroll one page up or down.
1994 * -- "Home": scroll leftmost.
1995 * -- "Ctrl+ Home": scroll topmost.
1996 * -- "End": scroll rightmost.
1997 * -- "Ctrl+ End": scroll bottommost.
1998 * -- "Ctrl + page up, down": scroll one screen left or right.
1999 *
2000 * This is CUA behavior.
2001 *
2002 * Returns TRUE if the message has been
2003 * processed.
2004 *
2005 *@@added V0.9.3 (2000-04-29) [umoeller]
2006 *@@changed V0.9.9 (2001-02-01) [lafaix]: Ctrl+PgUp/Dn now do one screen left/right
2007 */
2008
2009BOOL winhProcessScrollChars(HWND hwndClient, // in: client window
2010 HWND hwndVScroll, // in: vertical scroll bar
2011 HWND hwndHScroll, // in: horizontal scroll bar
2012 MPARAM mp1, // in: WM_CHAR mp1
2013 MPARAM mp2, // in: WM_CHAR mp2
2014 ULONG ulVertMax, // in: maximum viewport cy
2015 ULONG ulHorzMax) // in: maximum viewport cx
2016{
2017 BOOL fProcessed = FALSE;
2018 USHORT usFlags = SHORT1FROMMP(mp1);
2019 // USHORT usch = SHORT1FROMMP(mp2);
2020 USHORT usvk = SHORT2FROMMP(mp2);
2021
2022 // _Pmpf(("Entering winhProcessScrollChars"));
2023
2024 if (usFlags & KC_VIRTUALKEY)
2025 {
2026 ULONG ulMsg = 0;
2027 SHORT sPos = 0;
2028 SHORT usCmd = 0;
2029 fProcessed = TRUE;
2030
2031 switch (usvk)
2032 {
2033 case VK_UP:
2034 ulMsg = WM_VSCROLL;
2035 usCmd = SB_LINEUP;
2036 break;
2037
2038 case VK_DOWN:
2039 ulMsg = WM_VSCROLL;
2040 usCmd = SB_LINEDOWN;
2041 break;
2042
2043 case VK_RIGHT:
2044 ulMsg = WM_HSCROLL;
2045 usCmd = SB_LINERIGHT;
2046 break;
2047
2048 case VK_LEFT:
2049 ulMsg = WM_HSCROLL;
2050 usCmd = SB_LINELEFT;
2051 break;
2052
2053 case VK_PAGEUP:
2054 if (usFlags & KC_CTRL)
2055 ulMsg = WM_HSCROLL;
2056 else
2057 ulMsg = WM_VSCROLL;
2058 usCmd = SB_PAGEUP;
2059 break;
2060
2061 case VK_PAGEDOWN:
2062 if (usFlags & KC_CTRL)
2063 ulMsg = WM_HSCROLL;
2064 else
2065 ulMsg = WM_VSCROLL;
2066 usCmd = SB_PAGEDOWN;
2067 break;
2068
2069 case VK_HOME:
2070 if (usFlags & KC_CTRL)
2071 // vertical:
2072 ulMsg = WM_VSCROLL;
2073 else
2074 ulMsg = WM_HSCROLL;
2075
2076 sPos = 0;
2077 usCmd = SB_SLIDERPOSITION;
2078 break;
2079
2080 case VK_END:
2081 if (usFlags & KC_CTRL)
2082 {
2083 // vertical:
2084 ulMsg = WM_VSCROLL;
2085 sPos = ulVertMax;
2086 }
2087 else
2088 {
2089 ulMsg = WM_HSCROLL;
2090 sPos = ulHorzMax;
2091 }
2092
2093 usCmd = SB_SLIDERPOSITION;
2094 break;
2095
2096 default:
2097 // other:
2098 fProcessed = FALSE;
2099 }
2100
2101 if ( ((usFlags & KC_KEYUP) == 0)
2102 && (ulMsg)
2103 )
2104 {
2105 HWND hwndScrollBar = ((ulMsg == WM_VSCROLL)
2106 ? hwndVScroll
2107 : hwndHScroll);
2108 if (WinIsWindowEnabled(hwndScrollBar))
2109 {
2110 USHORT usID = WinQueryWindowUShort(hwndScrollBar,
2111 QWS_ID);
2112 WinSendMsg(hwndClient,
2113 ulMsg,
2114 MPFROMSHORT(usID),
2115 MPFROM2SHORT(sPos,
2116 usCmd));
2117 }
2118 }
2119 }
2120
2121 // _Pmpf(("End of winhProcessScrollChars"));
2122
2123 return (fProcessed);
2124}
2125
2126/*
2127 *@@category: Helpers\PM helpers\Window positioning
2128 */
2129
2130/* ******************************************************************
2131 *
2132 * Window positioning helpers
2133 *
2134 ********************************************************************/
2135
2136/*
2137 *@@ winhSaveWindowPos:
2138 * saves the position of a certain window. As opposed
2139 * to the barely documented WinStoreWindowPos API, this
2140 * one only saves one regular SWP structure for the given
2141 * window, as returned by WinQueryWindowPos for hwnd.
2142 *
2143 * If the window is currently maximized or minimized,
2144 * we won't store the current window size and position
2145 * (which wouldn't make much sense), but retrieve the
2146 * "restored" window position from the window words
2147 * instead.
2148 *
2149 * The window should still be visible on the screen
2150 * when calling this function. Do not call it in WM_DESTROY,
2151 * because then the SWP data is no longer valid.
2152 *
2153 * This returns TRUE if saving was successful.
2154 *
2155 *@@changed V0.9.1 (99-12-19) [umoeller]: added minimize/maximize support
2156 */
2157
2158BOOL winhSaveWindowPos(HWND hwnd, // in: window to save
2159 HINI hIni, // in: INI file (or HINI_USER/SYSTEM)
2160 const char *pcszApp, // in: INI application name
2161 const char *pcszKey) // in: INI key name
2162{
2163 BOOL brc = FALSE;
2164 SWP swp;
2165 if (WinQueryWindowPos(hwnd, &swp))
2166 {
2167 if (swp.fl & (SWP_MAXIMIZE | SWP_MINIMIZE))
2168 {
2169 // window currently maximized or minimized:
2170 // retrieve "restore" position from window words
2171 swp.x = WinQueryWindowUShort(hwnd, QWS_XRESTORE);
2172 swp.y = WinQueryWindowUShort(hwnd, QWS_YRESTORE);
2173 swp.cx = WinQueryWindowUShort(hwnd, QWS_CXRESTORE);
2174 swp.cy = WinQueryWindowUShort(hwnd, QWS_CYRESTORE);
2175 }
2176
2177 brc = PrfWriteProfileData(hIni, (PSZ)pcszApp, (PSZ)pcszKey, &swp, sizeof(swp));
2178 }
2179 return (brc);
2180}
2181
2182/*
2183 *@@ winhRestoreWindowPos:
2184 * this will retrieve a window position which was
2185 * previously stored using winhSaveWindowPos.
2186 *
2187 * The window should not be visible to avoid flickering.
2188 * "fl" must contain the SWP_flags as in WinSetWindowPos.
2189 *
2190 * Note that only the following may be used:
2191 * -- SWP_MOVE reposition the window
2192 * -- SWP_SIZE also resize the window to
2193 * the stored position; this might
2194 * lead to problems with different
2195 * video resolutions, so be careful.
2196 * -- SWP_SHOW make window visible too
2197 * -- SWP_NOREDRAW changes are not redrawn
2198 * -- SWP_NOADJUST do not send a WM_ADJUSTWINDOWPOS message
2199 * before moving or sizing
2200 * -- SWP_ACTIVATE activate window (make topmost)
2201 * -- SWP_DEACTIVATE deactivate window (make bottommost)
2202 *
2203 * Do not specify any other SWP_* flags.
2204 *
2205 * If SWP_SIZE is not set, the window will be moved only.
2206 *
2207 * This returns TRUE if INI data was found.
2208 *
2209 * This function automatically checks for whether the
2210 * window would be positioned outside the visible screen
2211 * area and will adjust coordinates accordingly. This can
2212 * happen when changing video resolutions.
2213 *
2214 *@@changed V0.9.7 (2000-12-20) [umoeller]: fixed invalid params if INI key not found
2215 */
2216
2217BOOL winhRestoreWindowPos(HWND hwnd, // in: window to restore
2218 HINI hIni, // in: INI file (or HINI_USER/SYSTEM)
2219 const char *pcszApp, // in: INI application name
2220 const char *pcszKey, // in: INI key name
2221 ULONG fl) // in: "fl" parameter for WinSetWindowPos
2222{
2223 BOOL brc = FALSE;
2224 SWP swp;
2225 ULONG cbswp = sizeof(swp);
2226 ULONG fl2 = (fl & ~SWP_ZORDER);
2227
2228 if (PrfQueryProfileData(hIni, (PSZ)pcszApp, (PSZ)pcszKey, &swp, &cbswp))
2229 {
2230 ULONG ulScreenCX = WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
2231 ULONG ulScreenCY = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
2232
2233 brc = TRUE;
2234
2235 if ((fl & SWP_SIZE) == 0)
2236 {
2237 // if no resize, we need to get the current position
2238 SWP swpNow;
2239 brc = WinQueryWindowPos(hwnd, &swpNow);
2240 swp.cx = swpNow.cx;
2241 swp.cy = swpNow.cy;
2242 }
2243
2244 if (brc)
2245 {
2246 // check for full visibility
2247 if ( (swp.x + swp.cx) > ulScreenCX)
2248 swp.x = ulScreenCX - swp.cx;
2249 if ( (swp.y + swp.cy) > ulScreenCY)
2250 swp.y = ulScreenCY - swp.cy;
2251 }
2252
2253 brc = TRUE;
2254
2255 }
2256 else
2257 {
2258 // window pos not found in INI: unset SWP_MOVE etc.
2259 WinQueryWindowPos(hwnd, &swp);
2260 fl2 &= ~(SWP_MOVE | SWP_SIZE);
2261 }
2262
2263 WinSetWindowPos(hwnd,
2264 NULLHANDLE, // insert-behind window
2265 swp.x,
2266 swp.y,
2267 swp.cx,
2268 swp.cy,
2269 fl2); // SWP_* flags
2270
2271 return (brc);
2272}
2273
2274/*
2275 *@@ winhAdjustControls:
2276 * helper function for dynamically adjusting a window's
2277 * controls when the window is resized.
2278 *
2279 * This is most useful with dialogs loaded from resources
2280 * which should be sizeable. Normally, when the dialog
2281 * frame is resized, the controls stick to their positions,
2282 * and for dialogs with many controls, programming the
2283 * changes can be tiresome.
2284 *
2285 * Enter this function. ;-) Basically, this takes a
2286 * static array of MPARAM's as input (plus one dynamic
2287 * storage area for the window positions).
2288 *
2289 * This function must get called in three contexts:
2290 * during WM_INITDLG, during WM_WINDOWPOSCHANGED, and
2291 * during WM_DESTROY, with varying parameters.
2292 *
2293 * In detail, there are four things you need to do to make
2294 * this work:
2295 *
2296 * 1) Set up a static array (as a global variable) of
2297 * MPARAM's, one for each control in your array.
2298 * Each MPARAM will have the control's ID and the
2299 * XAC_* flags (winh.h) how the control shall be moved.
2300 * Use MPFROM2SHORT to easily create this. Example:
2301 *
2302 + MPARAM ampControlFlags[] =
2303 + { MPFROM2SHORT(ID_CONTROL_1, XAC_MOVEX),
2304 + MPFROM2SHORT(ID_CONTROL_2, XAC_SIZEY),
2305 + ...
2306 + }
2307 *
2308 * This can safely be declared as a global variable
2309 * because this data will only be read and never
2310 * changed by this function.
2311 *
2312 * 2) In WM_INITDLG of your dialog function, set up
2313 * an XADJUSTCTRLS structure, preferrably in your
2314 * window words (QWL_USER).
2315 *
2316 * ZERO THAT STRUCTURE (memset(&xac, 0, sizeof(XADJUSTCTRLS),
2317 * or this func will not work (because it will intialize
2318 * things on the first WM_WINDOWPOSCHANGED).
2319 *
2320 * 3) Intercept WM_WINDOWPOSCHANGED:
2321 *
2322 + case WM_WINDOWPOSCHANGED:
2323 + {
2324 + // this msg is passed two SWP structs:
2325 + // one for the old, one for the new data
2326 + // (from PM docs)
2327 + PSWP pswpNew = PVOIDFROMMP(mp1);
2328 + PSWP pswpOld = pswpNew + 1;
2329 +
2330 + // resizing?
2331 + if (pswpNew->fl & SWP_SIZE)
2332 + {
2333 + PXADJUSTCTRLS pxac = ... // get it from your window words
2334 +
2335 + winhAdjustControls(hwndDlg, // dialog
2336 + ampControlFlags, // MPARAMs array
2337 + sizeof(ampControlFlags) / sizeof(MPARAM),
2338 + // items count
2339 + pswpNew, // mp1
2340 + pxac); // storage area
2341 + }
2342 + mrc = WinDefDlgProc(hwnd, msg, mp1, mp2); ...
2343 *
2344 * 4) In WM_DESTROY, call this function again with pmpFlags,
2345 * pswpNew, and pswpNew set to NULL. This will clean up the
2346 * data which has been allocated internally (pointed to from
2347 * the XADJUSTCTRLS structure).
2348 * Don't forget to free your storage for XADJUSTCTLRS
2349 * _itself_, that's the job of the caller.
2350 *
2351 * This might sound complicated, but it's a lot easier than
2352 * having to write dozens of WinSetWindowPos calls oneself.
2353 *
2354 *@@added V0.9.0 [umoeller]
2355 *@@changed V0.9.19 (2002-04-13) [umoeller]: added correlation for entry field repositioning, this was always off
2356 */
2357
2358BOOL winhAdjustControls(HWND hwndDlg, // in: dialog (req.)
2359 const MPARAM *pmpFlags, // in: init flags or NULL for cleanup
2360 ULONG ulCount, // in: item count (req.)
2361 PSWP pswpNew, // in: pswpNew from WM_WINDOWPOSCHANGED or NULL for cleanup
2362 PXADJUSTCTRLS pxac) // in: adjust-controls storage area (req.)
2363{
2364 BOOL brc = FALSE;
2365 ULONG ul = 0;
2366
2367 /* if (!WinIsWindowVisible(hwndDlg))
2368 return (FALSE); */
2369
2370 if ((pmpFlags) && (pxac))
2371 {
2372 PSWP pswpThis;
2373 const MPARAM *pmpThis;
2374 LONG ldcx, ldcy;
2375 ULONG cWindows = 0;
2376
2377 // V0.9.19 (2002-04-13) [umoeller]
2378 LONG cxMarginEF = 3 * WinQuerySysValue(HWND_DESKTOP, SV_CXBORDER);
2379 LONG cyMarginEF = 3 * WinQuerySysValue(HWND_DESKTOP, SV_CYBORDER);
2380
2381 // setup mode:
2382 if (pxac->fInitialized == FALSE)
2383 {
2384 // first call: get all the SWP's
2385 WinQueryWindowPos(hwndDlg, &pxac->swpMain);
2386 // _Pmpf(("winhAdjustControls: queried main cx = %d, cy = %d",
2387 // pxac->swpMain.cx, pxac->swpMain.cy));
2388
2389 pxac->paswp = (PSWP)malloc(sizeof(SWP) * ulCount);
2390
2391 pswpThis = pxac->paswp;
2392 pmpThis = pmpFlags;
2393
2394 for (ul = 0;
2395 ul < ulCount;
2396 ul++)
2397 {
2398 HWND hwndThis;
2399 CHAR szClass[10];
2400 if (hwndThis = WinWindowFromID(hwndDlg, SHORT1FROMMP(*pmpThis)))
2401 {
2402 WinQueryWindowPos(hwndThis, pswpThis);
2403
2404 // correlate the stupid repositioning of entry fields
2405 // V0.9.19 (2002-04-13) [umoeller]
2406 if ( (WinQueryClassName(hwndThis, sizeof(szClass), szClass)
2407 && (!strcmp(szClass, "#6"))
2408 && (WinQueryWindowULong(hwndThis, QWL_STYLE) & ES_MARGIN))
2409 )
2410 {
2411 pswpThis->x += cxMarginEF;
2412 pswpThis->y += cyMarginEF;
2413 pswpThis->cx -= 2 * cxMarginEF;
2414 pswpThis->cy -= 2 * cyMarginEF;
2415 }
2416
2417 cWindows++;
2418 }
2419
2420 pswpThis++;
2421 pmpThis++;
2422 }
2423
2424 pxac->fInitialized = TRUE;
2425 // _Pmpf(("winhAdjustControls: queried %d controls", cWindows));
2426 }
2427
2428 if (pswpNew)
2429 {
2430 // compute width and height delta
2431 ldcx = (pswpNew->cx - pxac->swpMain.cx);
2432 ldcy = (pswpNew->cy - pxac->swpMain.cy);
2433
2434 // _Pmpf(("winhAdjustControls: new cx = %d, cy = %d",
2435 // pswpNew->cx, pswpNew->cy));
2436
2437 // now adjust the controls
2438 cWindows = 0;
2439 pswpThis = pxac->paswp;
2440 pmpThis = pmpFlags;
2441 for (ul = 0;
2442 ul < ulCount;
2443 ul++)
2444 {
2445 HWND hwndThis;
2446 if (hwndThis = WinWindowFromID(hwndDlg, SHORT1FROMMP(*pmpThis)))
2447 {
2448 LONG x = pswpThis->x,
2449 y = pswpThis->y,
2450 cx = pswpThis->cx,
2451 cy = pswpThis->cy;
2452
2453 ULONG ulSwpFlags = 0;
2454 // get flags for this control
2455 USHORT usFlags = SHORT2FROMMP(*pmpThis);
2456
2457 if (usFlags & XAC_MOVEX)
2458 {
2459 x += ldcx;
2460 ulSwpFlags |= SWP_MOVE;
2461 }
2462 if (usFlags & XAC_MOVEY)
2463 {
2464 y += ldcy;
2465 ulSwpFlags |= SWP_MOVE;
2466 }
2467 if (usFlags & XAC_SIZEX)
2468 {
2469 cx += ldcx;
2470 ulSwpFlags |= SWP_SIZE;
2471 }
2472 if (usFlags & XAC_SIZEY)
2473 {
2474 cy += ldcy;
2475 ulSwpFlags |= SWP_SIZE;
2476 }
2477
2478 if (ulSwpFlags)
2479 {
2480 WinSetWindowPos(hwndThis,
2481 NULLHANDLE, // hwndInsertBehind
2482 x, y, cx, cy,
2483 ulSwpFlags);
2484 cWindows++;
2485 brc = TRUE;
2486 }
2487 }
2488
2489 pswpThis++;
2490 pmpThis++;
2491 }
2492
2493 // _Pmpf(("winhAdjustControls: set %d windows", cWindows));
2494 }
2495 }
2496 else
2497 {
2498 // pxac == NULL:
2499 // cleanup mode
2500 if (pxac->paswp)
2501 free(pxac->paswp);
2502 }
2503
2504 return (brc);
2505}
2506
2507/*
2508 *@@ winhCenterWindow:
2509 * centers a window within its parent window. If that's
2510 * the PM desktop, it will be centered according to the
2511 * whole screen.
2512 * For dialog boxes, use WinCenteredDlgBox as a one-shot
2513 * function.
2514 *
2515 * Note: When calling this function, the window should
2516 * not be visible to avoid flickering.
2517 * This func does not show the window either, so call
2518 * WinShowWindow afterwards.
2519 */
2520
2521void winhCenterWindow(HWND hwnd)
2522{
2523 RECTL rclParent;
2524 RECTL rclWindow;
2525
2526 WinQueryWindowRect(hwnd, &rclWindow);
2527 WinQueryWindowRect(WinQueryWindow(hwnd, QW_PARENT), &rclParent);
2528
2529 rclWindow.xLeft = (rclParent.xRight - rclWindow.xRight) / 2;
2530 rclWindow.yBottom = (rclParent.yTop - rclWindow.yTop ) / 2;
2531
2532 WinSetWindowPos(hwnd, NULLHANDLE, rclWindow.xLeft, rclWindow.yBottom,
2533 0, 0, SWP_MOVE);
2534}
2535
2536/*
2537 *@@ winhCenteredDlgBox:
2538 * just like WinDlgBox, but the dlg box is centered on the screen;
2539 * you should mark the dlg template as not visible in the dlg
2540 * editor, or display will flicker.
2541 * As opposed to winhCenterWindow, this _does_ show the window.
2542 */
2543
2544ULONG winhCenteredDlgBox(HWND hwndParent,
2545 HWND hwndOwner,
2546 PFNWP pfnDlgProc,
2547 HMODULE hmod,
2548 ULONG idDlg,
2549 PVOID pCreateParams)
2550{
2551 ULONG ulReply;
2552 HWND hwndDlg = WinLoadDlg(hwndParent,
2553 hwndOwner,
2554 pfnDlgProc,
2555 hmod,
2556 idDlg,
2557 pCreateParams);
2558 winhCenterWindow(hwndDlg);
2559 ulReply = WinProcessDlg(hwndDlg);
2560 WinDestroyWindow(hwndDlg);
2561 return (ulReply);
2562}
2563
2564/*
2565 *@@ winhPlaceBesides:
2566 * attempts to place hwnd somewhere besides
2567 * hwndRelative.
2568 *
2569 * fl is presently ignored, but should be
2570 * PLF_SMART for future extensions.
2571 *
2572 * Works only if hwnd is a desktop child.
2573 *
2574 *@@added V0.9.19 (2002-04-17) [umoeller]
2575 */
2576
2577BOOL winhPlaceBesides(HWND hwnd,
2578 HWND hwndRelative,
2579 ULONG fl)
2580{
2581 BOOL brc = FALSE;
2582
2583 SWP swpRel,
2584 swpThis;
2585 LONG xNew, yNew;
2586
2587 if ( (WinQueryWindowPos(hwndRelative, &swpRel))
2588 && (WinQueryWindowPos(hwnd, &swpThis))
2589 )
2590 {
2591 HWND hwndRelParent,
2592 hwndThisParent;
2593 POINTL ptlRel = {swpRel.x, swpRel.y};
2594 if ( (hwndRelParent = WinQueryWindow(hwndRelative, QW_PARENT))
2595 && (hwndThisParent = WinQueryWindow(hwnd, QW_PARENT))
2596 && (hwndRelParent != hwndThisParent)
2597 )
2598 {
2599 WinMapWindowPoints(hwndRelParent,
2600 hwndThisParent,
2601 &ptlRel,
2602 1);
2603 }
2604
2605 // place right first
2606 xNew = ptlRel.x + swpRel.cx;
2607 // center vertically
2608 yNew = ptlRel.y + ((swpRel.cy - swpThis.cy) / 2);
2609
2610 if (xNew + swpThis.cy > WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN))
2611 {
2612 // place left then
2613 xNew = ptlRel.x - swpThis.cx;
2614
2615 if (xNew < 0)
2616 {
2617 // center then
2618 winhCenterWindow(hwnd);
2619 brc = TRUE;
2620 }
2621 }
2622
2623 if (!brc)
2624 brc = WinSetWindowPos(hwnd,
2625 0,
2626 xNew,
2627 yNew,
2628 0,
2629 0,
2630 SWP_MOVE);
2631 }
2632
2633 return brc;
2634}
2635
2636/*
2637 *@@ winhFindWindowBelow:
2638 * finds the window with the same parent
2639 * which sits right below hwndFind in the
2640 * window Z-order.
2641 *
2642 *@@added V0.9.7 (2000-12-04) [umoeller]
2643 */
2644
2645HWND winhFindWindowBelow(HWND hwndFind)
2646{
2647 HWND hwnd = NULLHANDLE,
2648 hwndParent = WinQueryWindow(hwndFind, QW_PARENT);
2649
2650 if (hwndParent)
2651 {
2652 HENUM henum = WinBeginEnumWindows(hwndParent);
2653 HWND hwndThis;
2654 while (hwndThis = WinGetNextWindow(henum))
2655 {
2656 SWP swp;
2657 WinQueryWindowPos(hwndThis, &swp);
2658 if (swp.hwndInsertBehind == hwndFind)
2659 {
2660 hwnd = hwndThis;
2661 break;
2662 }
2663 }
2664 WinEndEnumWindows(henum);
2665 }
2666
2667 return (hwnd);
2668}
2669
2670/*
2671 *@@category: Helpers\PM helpers\Presentation parameters
2672 */
2673
2674/* ******************************************************************
2675 *
2676 * Presparams helpers
2677 *
2678 ********************************************************************/
2679
2680/*
2681 *@@ winhQueryWindowFont:
2682 * returns the window font presentation parameter
2683 * in a newly allocated buffer.
2684 *
2685 * Returns NULL on error. Use free()
2686 * to free the return value.
2687 *
2688 *@@added V0.9.1 (2000-02-14) [umoeller]
2689 */
2690
2691PSZ winhQueryWindowFont(HWND hwnd)
2692{
2693 CHAR szNewFont[100] = "";
2694 WinQueryPresParam(hwnd,
2695 PP_FONTNAMESIZE,
2696 0,
2697 NULL,
2698 (ULONG)sizeof(szNewFont),
2699 (PVOID)&szNewFont,
2700 QPF_NOINHERIT);
2701 if (szNewFont[0] != 0)
2702 return (strdup(szNewFont));
2703
2704 return (NULL);
2705}
2706
2707/*
2708 *@@ winhSetWindowFont:
2709 * this sets a window's font by invoking
2710 * WinSetPresParam on it.
2711 *
2712 * If (pszFont == NULL), a default font will be set
2713 * (on Warp 4, "9.WarpSans", on Warp 3, "8.Helv").
2714 *
2715 * winh.h also defines the winhSetDlgItemFont macro.
2716 *
2717 * Returns TRUE if successful or FALSE otherwise.
2718 *
2719 *@@added V0.9.0 [umoeller]
2720 */
2721
2722BOOL winhSetWindowFont(HWND hwnd,
2723 const char *pcszFont)
2724{
2725 CHAR szFont[256];
2726
2727 if (pcszFont == NULL)
2728 {
2729 if (doshIsWarp4())
2730 strhncpy0(szFont, "9.WarpSans", sizeof(szFont));
2731 else
2732 strhncpy0(szFont, "8.Helv", sizeof(szFont));
2733 }
2734 else
2735 strhncpy0(szFont, pcszFont, sizeof(szFont));
2736
2737 return (WinSetPresParam(hwnd,
2738 PP_FONTNAMESIZE,
2739 strlen(szFont)+1,
2740 szFont));
2741}
2742
2743/*
2744 *@@ winhSetControlsFont:
2745 * this sets the font for all the controls of hwndDlg
2746 * which have a control ID in the range of usIDMin to
2747 * usIDMax. "Unused" IDs (i.e. -1) will also be set.
2748 *
2749 * If (pszFont == NULL), a default font will be set
2750 * (on Warp 4, "9.WarpSans", on Warp 3, "8.Helv").
2751 *
2752 * Returns the no. of controls set.
2753 *
2754 *@@added V0.9.0 [umoeller]
2755 */
2756
2757ULONG winhSetControlsFont(HWND hwndDlg, // in: dlg to set
2758 SHORT usIDMin, // in: minimum control ID to be set (inclusive)
2759 SHORT usIDMax, // in: maximum control ID to be set (inclusive)
2760 const char *pcszFont) // in: font to use (e.g. "9.WarpSans") or NULL
2761{
2762 ULONG ulrc = 0;
2763 HENUM henum;
2764 HWND hwndItem;
2765 CHAR szFont[256];
2766 ULONG cbFont;
2767
2768 if (pcszFont == NULL)
2769 {
2770 if (doshIsWarp4())
2771 strhncpy0(szFont, "9.WarpSans", sizeof(szFont));
2772 else
2773 strhncpy0(szFont, "8.Helv", sizeof(szFont));
2774 }
2775 else
2776 strhncpy0(szFont, pcszFont, sizeof(szFont));
2777 cbFont = strlen(szFont)+1;
2778
2779 // set font for all the dialog controls
2780 henum = WinBeginEnumWindows(hwndDlg);
2781 while ((hwndItem = WinGetNextWindow(henum)))
2782 {
2783 SHORT sID = WinQueryWindowUShort(hwndItem, QWS_ID);
2784 if ( (sID == -1)
2785 || ((sID >= usIDMin) && (sID <= usIDMax))
2786 )
2787 if (WinSetPresParam(hwndItem,
2788 PP_FONTNAMESIZE,
2789 cbFont,
2790 szFont))
2791 // successful:
2792 ulrc++;
2793 }
2794 WinEndEnumWindows(henum);
2795 return (ulrc);
2796}
2797
2798/*
2799 *@@ winhStorePresParam:
2800 * this appends a new presentation parameter to an
2801 * array of presentation parameters which can be
2802 * passed to WinCreateWindow. This is preferred
2803 * over setting the presparams using WinSetPresParams,
2804 * because that call will cause a lot of messages.
2805 *
2806 * On the first call, pppp _must_ be NULL. This
2807 * will allocate memory for storing the given
2808 * data as necessary and modify *pppp to point
2809 * to the new array.
2810 *
2811 * On subsequent calls with the same pppp, memory
2812 * will be reallocated, the old data will be copied,
2813 * and the new given data will be appended.
2814 *
2815 * Use free() on your PPRESPARAMS pointer (whose
2816 * address was passed) after WinCreateWindow.
2817 *
2818 * See winhQueryPresColor for typical presparams
2819 * used in OS/2.
2820 *
2821 * Example:
2822 *
2823 + PPRESPARAMS ppp = NULL;
2824 + CHAR szFont[] = "9.WarpSans";
2825 + LONG lColor = CLR_WHITE;
2826 + winhStorePresParam(&ppp, PP_FONTNAMESIZE, sizeof(szFont), szFont);
2827 + winhStorePresParam(&ppp, PP_BACKGROUNDCOLOR, sizeof(lColor), &lColor);
2828 + WinCreateWindow(...., ppp);
2829 + free(ppp);
2830 *
2831 *@@added V0.9.0 [umoeller]
2832 */
2833
2834BOOL winhStorePresParam(PPRESPARAMS *pppp, // in: data pointer (modified)
2835 ULONG ulAttrType, // in: PP_* index
2836 ULONG cbData, // in: sizeof(*pData), e.g. sizeof(LONG)
2837 PVOID pData) // in: presparam data (e.g. a PLONG to a color)
2838{
2839 BOOL brc = FALSE;
2840 if (pppp)
2841 {
2842 ULONG cbOld = 0,
2843 cbNew;
2844 PBYTE pbTemp = 0;
2845 PPRESPARAMS pppTemp = 0;
2846 PPARAM pppCopyTo = 0;
2847
2848 if (*pppp != NULL)
2849 // subsequent calls:
2850 cbOld = (**pppp).cb;
2851
2852 cbNew = sizeof(ULONG) // PRESPARAMS.cb
2853 + cbOld // old count, which does not include PRESPARAMS.cb
2854 + sizeof(ULONG) // PRESPARAMS.aparam[0].id
2855 + sizeof(ULONG) // PRESPARAMS.aparam[0].cb
2856 + cbData; // PRESPARAMS.aparam[0].ab[]
2857
2858 pbTemp = (PBYTE)malloc(cbNew);
2859 if (pbTemp)
2860 {
2861 pppTemp = (PPRESPARAMS)pbTemp;
2862
2863 if (*pppp != NULL)
2864 {
2865 // copy old data
2866 memcpy(pbTemp, *pppp, cbOld + sizeof(ULONG)); // including PRESPARAMS.cb
2867 pppCopyTo = (PPARAM)(pbTemp // new buffer
2868 + sizeof(ULONG) // skipping PRESPARAMS.cb
2869 + cbOld); // old PARAM array
2870 }
2871 else
2872 // first call:
2873 pppCopyTo = pppTemp->aparam;
2874
2875 pppTemp->cb = cbNew - sizeof(ULONG); // excluding PRESPARAMS.cb
2876 pppCopyTo->id = ulAttrType;
2877 pppCopyTo->cb = cbData; // byte count of PARAM.ab[]
2878 memcpy(pppCopyTo->ab, pData, cbData);
2879
2880 free(*pppp);
2881 *pppp = pppTemp;
2882
2883 brc = TRUE;
2884 }
2885 }
2886 return (brc);
2887}
2888
2889/*
2890 *@@ winhQueryPresColor:
2891 * returns the specified color. This is queried in the
2892 * following order:
2893 *
2894 * 1) hwnd's pres params are searched for ulPP
2895 * (which should be a PP_* index);
2896 * 2) if (fInherit == TRUE), the parent windows
2897 * are searched also;
2898 * 3) if this fails or (fInherit == FALSE), WinQuerySysColor
2899 * is called to get lSysColor (which should be a SYSCLR_*
2900 * index), if lSysColor != -1;
2901 * 4) if (lSysColor == -1), -1 is returned.
2902 *
2903 * The return value is always an RGB LONG, _not_ a color index.
2904 * This is even true for the returned system colors, which are
2905 * converted to RGB.
2906 *
2907 * If you do any painting with this value, you should switch
2908 * the HPS you're using to RGB mode (use gpihSwitchToRGB for that).
2909 *
2910 * Some useful ulPP / lSysColor pairs
2911 * (default values as in PMREF):
2912 *
2913 + -- PP_FOREGROUNDCOLOR SYSCLR_WINDOWTEXT (for most controls also)
2914 + SYSCLR_WINDOWSTATICTEXT (for static controls)
2915 + Foreground color (default: black)
2916 + -- PP_BACKGROUNDCOLOR SYSCLR_BACKGROUND
2917 + SYSCLR_DIALOGBACKGROUND
2918 + SYSCLR_FIELDBACKGROUND (for disabled scrollbars)
2919 + SYSCLR_WINDOW (application surface -- empty clients)
2920 + Background color (default: light gray)
2921 + -- PP_ACTIVETEXTFGNDCOLOR
2922 + -- PP_HILITEFOREGROUNDCOLOR SYSCLR_HILITEFOREGROUND
2923 + Highlighted foreground color, for example for selected menu
2924 + (def.: white)
2925 + -- PP_ACTIVETEXTBGNDCOLOR
2926 + -- PP_HILITEBACKGROUNDCOLOR SYSCLR_HILITEBACKGROUND
2927 + Highlighted background color (def.: dark gray)
2928 + -- PP_INACTIVETEXTFGNDCOLOR
2929 + -- PP_DISABLEDFOREGROUNDCOLOR SYSCLR_MENUDISABLEDTEXT
2930 + Disabled foreground color (dark gray)
2931 + -- PP_INACTIVETEXTBGNDCOLOR
2932 + -- PP_DISABLEDBACKGROUNDCOLOR
2933 + Disabled background color
2934 + -- PP_BORDERCOLOR SYSCLR_WINDOWFRAME
2935 + SYSCLR_INACTIVEBORDER
2936 + Border color (around pushbuttons, in addition to
2937 + the 3D colors)
2938 + -- PP_ACTIVECOLOR SYSCLR_ACTIVETITLE
2939 + Active color
2940 + -- PP_INACTIVECOLOR SYSCLR_INACTIVETITLE
2941 + Inactive color
2942 *
2943 * For menus:
2944 + -- PP_MENUBACKGROUNDCOLOR SYSCLR_MENU
2945 + -- PP_MENUFOREGROUNDCOLOR SYSCLR_MENUTEXT
2946 + -- PP_MENUHILITEBGNDCOLOR SYSCLR_MENUHILITEBGND
2947 + -- PP_MENUHILITEFGNDCOLOR SYSCLR_MENUHILITE
2948 + -- ?? SYSCLR_MENUDISABLEDTEXT
2949 +
2950 * For containers (according to the API ref. at EDM/2):
2951 + -- PP_FOREGROUNDCOLOR SYSCLR_WINDOWTEXT
2952 + -- PP_BACKGROUNDCOLOR SYSCLR_WINDOW
2953 + -- PP_HILITEFOREGROUNDCOLOR SYSCLR_HILITEFOREGROUND
2954 + -- PP_HILITEBACKGROUNDCOLOR SYSCLR_HILITEBACKGROUND
2955 + -- PP_BORDERCOLOR
2956 + (used for separator lines, eg. in Details view)
2957 + -- PP_ICONTEXTBACKGROUNDCOLOR
2958 + (column titles in Details view?!?)
2959 +
2960 * For listboxes / entryfields / MLE's:
2961 + -- PP_BACKGROUNDCOLOR SYSCLR_ENTRYFIELD
2962 *
2963 * PMREF has more of these.
2964 *
2965 *@@changed V0.9.0 [umoeller]: removed INI key query, using SYSCLR_* instead; function prototype changed
2966 *@@changed V0.9.0 [umoeller]: added fInherit parameter
2967 *@@changed V0.9.7 (2000-12-02) [umoeller]: added lSysColor == -1 support
2968 */
2969
2970LONG winhQueryPresColor(HWND hwnd, // in: window to query
2971 ULONG ulPP, // in: PP_* index
2972 BOOL fInherit, // in: search parent windows too?
2973 LONG lSysColor) // in: SYSCLR_* index or -1
2974{
2975 ULONG ul,
2976 attrFound,
2977 abValue[32];
2978
2979 if (ulPP != (ULONG)-1)
2980 if ((ul = WinQueryPresParam(hwnd,
2981 ulPP,
2982 0,
2983 &attrFound,
2984 (ULONG)sizeof(abValue),
2985 (PVOID)&abValue,
2986 (fInherit)
2987 ? 0
2988 : QPF_NOINHERIT)))
2989 return (abValue[0]);
2990
2991 // not found: get system color
2992 if (lSysColor != -1)
2993 return (WinQuerySysColor(HWND_DESKTOP, lSysColor, 0));
2994
2995 return -1;
2996}
2997
2998/*
2999 *@@ winhSetPresColor:
3000 * sets a color presparam. ulIndex specifies
3001 * the presparam to be set and would normally
3002 * be either PP_BACKGROUNDCOLOR or PP_FOREGROUNDCOLOR.
3003 *
3004 *@@added V0.9.16 (2001-10-15) [umoeller]
3005 */
3006
3007BOOL winhSetPresColor(HWND hwnd,
3008 ULONG ulIndex,
3009 LONG lColor)
3010{
3011 return (WinSetPresParam(hwnd,
3012 ulIndex,
3013 sizeof(LONG),
3014 &lColor));
3015}
3016
3017/*
3018 *@@category: Helpers\PM helpers\Help (IPF)
3019 */
3020
3021/* ******************************************************************
3022 *
3023 * Help instance helpers
3024 *
3025 ********************************************************************/
3026
3027/*
3028 *@@ winhCreateHelp:
3029 * creates a help instance and connects it with the
3030 * given frame window.
3031 *
3032 * If (pszFileName == NULL), we'll retrieve the
3033 * executable's fully qualified file name and
3034 * replace the extension with .HLP simply. This
3035 * avoids the typical "Help not found" errors if
3036 * the program isn't started in its own directory.
3037 *
3038 * If you have created a help table in memory, specify it
3039 * with pHelpTable. To load a help table from the resources,
3040 * specify hmod (or NULLHANDLE) and set pHelpTable to the
3041 * following:
3042 +
3043 + (PHELPTABLE)MAKELONG(usTableID, 0xffff)
3044 *
3045 * Returns the help window handle or NULLHANDLE on errors.
3046 *
3047 * Based on an EDM/2 code snippet.
3048 *
3049 *@@added V0.9.4 (2000-07-03) [umoeller]
3050 */
3051
3052HWND winhCreateHelp(HWND hwndFrame, // in: app's frame window handle; can be NULLHANDLE
3053 const char *pcszFileName, // in: help file name or NULL
3054 HMODULE hmod, // in: module with help table or NULLHANDLE (current)
3055 PHELPTABLE pHelpTable, // in: help table or resource ID
3056 const char *pcszWindowTitle) // in: help window title or NULL
3057{
3058 HELPINIT hi;
3059 PSZ pszExt;
3060 CHAR szName[CCHMAXPATH];
3061 HWND hwndHelp;
3062
3063 if (pcszFileName == NULL)
3064 {
3065 PPIB ppib;
3066 PTIB ptib;
3067 DosGetInfoBlocks(&ptib, &ppib);
3068 DosQueryModuleName(ppib->pib_hmte, sizeof(szName), szName);
3069
3070 pszExt = strrchr(szName, '.');
3071 if (pszExt)
3072 strcpy(pszExt, ".hlp");
3073 else
3074 strcat(szName, ".hlp");
3075
3076 pcszFileName = szName;
3077 }
3078
3079 hi.cb = sizeof(HELPINIT);
3080 hi.ulReturnCode = 0;
3081 hi.pszTutorialName = NULL;
3082 hi.phtHelpTable = pHelpTable;
3083 hi.hmodHelpTableModule = hmod;
3084 hi.hmodAccelActionBarModule = NULLHANDLE;
3085 hi.idAccelTable = 0;
3086 hi.idActionBar = 0;
3087 hi.pszHelpWindowTitle = (PSZ)pcszWindowTitle;
3088 hi.fShowPanelId = CMIC_HIDE_PANEL_ID;
3089 hi.pszHelpLibraryName = (PSZ)pcszFileName;
3090
3091 hwndHelp = WinCreateHelpInstance(WinQueryAnchorBlock(hwndFrame),
3092 &hi);
3093 if ((hwndFrame) && (hwndHelp))
3094 {
3095 WinAssociateHelpInstance(hwndHelp, hwndFrame);
3096 }
3097
3098 return (hwndHelp);
3099}
3100
3101/*
3102 *@@ winhDisplayHelpPanel:
3103 * displays the specified help panel ID.
3104 *
3105 * If (ulHelpPanel == 0), this displays the
3106 * standard OS/2 "Using help" panel.
3107 *
3108 * Returns zero on success or one of the
3109 * help manager error codes on failure.
3110 * See HM_ERROR for those.
3111 *
3112 *@@added V0.9.7 (2001-01-21) [umoeller]
3113 */
3114
3115ULONG winhDisplayHelpPanel(HWND hwndHelpInstance, // in: from winhCreateHelp
3116 ULONG ulHelpPanel) // in: help panel ID
3117{
3118 return (ULONG)(WinSendMsg(hwndHelpInstance,
3119 HM_DISPLAY_HELP,
3120 (MPARAM)ulHelpPanel,
3121 (MPARAM)( (ulHelpPanel != 0)
3122 ? HM_RESOURCEID
3123 : 0)));
3124}
3125
3126/*
3127 *@@ winhDestroyHelp:
3128 * destroys the help instance created by winhCreateHelp.
3129 *
3130 * Based on an EDM/2 code snippet.
3131 *
3132 *@@added V0.9.4 (2000-07-03) [umoeller]
3133 */
3134
3135void winhDestroyHelp(HWND hwndHelp,
3136 HWND hwndFrame) // can be NULLHANDLE if not used with winhCreateHelp
3137{
3138 if (hwndHelp)
3139 {
3140 if (hwndFrame)
3141 WinAssociateHelpInstance(NULLHANDLE, hwndFrame);
3142 WinDestroyHelpInstance(hwndHelp);
3143 }
3144}
3145
3146/*
3147 *@@category: Helpers\PM helpers\Application control
3148 */
3149
3150/* ******************************************************************
3151 *
3152 * Application control
3153 *
3154 ********************************************************************/
3155
3156/*
3157 *@@ winhAnotherInstance:
3158 * this tests whether another instance of the same
3159 * application is already running.
3160 *
3161 * To identify instances of the same application, the
3162 * application must call this function during startup
3163 * with the unique name of an OS/2 semaphore. As with
3164 * all OS/2 semaphores, the semaphore name must begin
3165 * with "\\SEM32\\". The semaphore isn't really used
3166 * except for testing for its existence, since that
3167 * name is unique among all processes.
3168 *
3169 * If another instance is found, TRUE is returned. If
3170 * (fSwitch == TRUE), that instance is switched to,
3171 * using the tasklist.
3172 *
3173 * If no other instance is found, FALSE is returned only.
3174 *
3175 * Based on an EDM/2 code snippet.
3176 *
3177 *@@added V0.9.0 (99-10-22) [umoeller]
3178 */
3179
3180BOOL winhAnotherInstance(const char *pcszSemName, // in: semaphore ID
3181 BOOL fSwitch) // in: if TRUE, switch to first instance if running
3182{
3183 HMTX hmtx;
3184
3185 if (DosCreateMutexSem((PSZ)pcszSemName,
3186 &hmtx,
3187 DC_SEM_SHARED,
3188 TRUE)
3189 == NO_ERROR)
3190 // semapore created: this doesn't happen if the semaphore
3191 // exists already, so no other instance is running
3192 return (FALSE);
3193
3194 // else: instance running
3195 hmtx = NULLHANDLE;
3196
3197 // switch to other instance?
3198 if (fSwitch)
3199 {
3200 // yes: query mutex creator
3201 if (DosOpenMutexSem((PSZ)pcszSemName,
3202 &hmtx)
3203 == NO_ERROR)
3204 {
3205 PID pid = 0;
3206 TID tid = 0; // unused
3207 ULONG ulCount; // unused
3208
3209 if (DosQueryMutexSem(hmtx, &pid, &tid, &ulCount) == NO_ERROR)
3210 {
3211 HSWITCH hswitch = WinQuerySwitchHandle(NULLHANDLE, pid);
3212 if (hswitch != NULLHANDLE)
3213 WinSwitchToProgram(hswitch);
3214 }
3215
3216 DosCloseMutexSem(hmtx);
3217 }
3218 }
3219
3220 return (TRUE); // another instance exists
3221}
3222
3223/*
3224 *@@ winhAddToTasklist:
3225 * this adds the specified window to the tasklist
3226 * with hIcon as its program icon (which is also
3227 * set for the main window). This is useful for
3228 * the old "dialog as main window" trick.
3229 *
3230 * Returns the HSWITCH of the added entry.
3231 */
3232
3233HSWITCH winhAddToTasklist(HWND hwnd, // in: window to add
3234 HPOINTER hIcon) // in: icon for main window
3235{
3236 SWCNTRL swctl;
3237 HSWITCH hswitch = 0;
3238 swctl.hwnd = hwnd; // window handle
3239 swctl.hwndIcon = hIcon; // icon handle
3240 swctl.hprog = NULLHANDLE; // program handle (use default)
3241 WinQueryWindowProcess(hwnd, &(swctl.idProcess), NULL);
3242 // process identifier
3243 swctl.idSession = 0; // session identifier ?
3244 swctl.uchVisibility = SWL_VISIBLE; // visibility
3245 swctl.fbJump = SWL_JUMPABLE; // jump indicator
3246 // get window title from window titlebar
3247 if (hwnd)
3248 WinQueryWindowText(hwnd, sizeof(swctl.szSwtitle), swctl.szSwtitle);
3249 swctl.bProgType = PROG_DEFAULT; // program type
3250 hswitch = WinAddSwitchEntry(&swctl);
3251
3252 // give the main window the icon
3253 if ((hwnd) && (hIcon))
3254 WinSendMsg(hwnd,
3255 WM_SETICON,
3256 (MPARAM)hIcon,
3257 NULL);
3258
3259 return (hswitch);
3260}
3261
3262/*
3263 *@@category: Helpers\PM helpers\Miscellaneous
3264 */
3265
3266/* ******************************************************************
3267 *
3268 * Miscellaneous
3269 *
3270 ********************************************************************/
3271
3272/*
3273 *@@ winhMyAnchorBlock:
3274 * returns the proper anchor block (HAB)
3275 * for the calling thread.
3276 *
3277 * Many Win* functions require an HAB to be
3278 * passed in. While many of them will work
3279 * when passing in NULLHANDLE, some (such as
3280 * WinGetMsg) won't. If you don't know the
3281 * anchor block of the calling thread, use
3282 * this function.
3283 *
3284 * This creates a temporary object window to
3285 * find out the anchor block. This is quite
3286 * expensive so only use this if there's no
3287 * other way to find out.
3288 *
3289 *@@added V0.9.11 (2001-04-20) [umoeller]
3290 */
3291
3292HAB winhMyAnchorBlock(VOID)
3293{
3294 HAB hab = NULLHANDLE;
3295 HWND hwnd;
3296 if (hwnd = winhCreateObjectWindow(WC_BUTTON, NULL))
3297 {
3298 hab = WinQueryAnchorBlock(hwnd);
3299 WinDestroyWindow(hwnd);
3300 }
3301
3302 return (hab);
3303}
3304
3305/*
3306 *@@ winhFree:
3307 * frees a block of memory allocated by the
3308 * winh* functions.
3309 *
3310 * Since the winh* functions use malloc(),
3311 * you can also use free() directly on such
3312 * blocks. However, you must use winhFree
3313 * if the winh* functions are in a module
3314 * with a different C runtime.
3315 *
3316 *@@added V0.9.7 (2000-12-06) [umoeller]
3317 */
3318
3319VOID winhFree(PVOID p)
3320{
3321 if (p)
3322 free(p);
3323}
3324
3325/*
3326 *@@ winhSleep:
3327 * sleeps at least the specified amount of time,
3328 * without blocking the message queue.
3329 *
3330 * NOTE: This function is a bit expensive because
3331 * it creates a temporary object window. If you
3332 * need to sleep several times, you should rather
3333 * use a private timer.
3334 *
3335 *@@added V0.9.4 (2000-07-11) [umoeller]
3336 *@@changed V0.9.9 (2001-03-11) [umoeller]: rewritten
3337 */
3338
3339VOID winhSleep(ULONG ulSleep) // in: sleep time in milliseconds
3340{
3341 HWND hwnd;
3342 if (hwnd = winhCreateObjectWindow(WC_STATIC, NULL))
3343 {
3344 QMSG qmsg;
3345 HAB hab = WinQueryAnchorBlock(hwnd);
3346 if (WinStartTimer(hab,
3347 hwnd,
3348 1,
3349 ulSleep))
3350 {
3351 while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
3352 {
3353 if ( (qmsg.hwnd == hwnd)
3354 && (qmsg.msg == WM_TIMER)
3355 && (qmsg.mp1 == (MPARAM)1) // timer ID
3356 )
3357 break;
3358
3359 WinDispatchMsg(hab, &qmsg);
3360 }
3361 WinStopTimer(hab,
3362 hwnd,
3363 1);
3364 }
3365 else
3366 // timer creation failed:
3367 DosSleep(ulSleep);
3368
3369 WinDestroyWindow(hwnd);
3370 }
3371 else
3372 DosSleep(ulSleep);
3373}
3374
3375/*
3376 *@@ winhFileDlg:
3377 * one-short function for opening an "Open" file
3378 * dialog.
3379 *
3380 * On input, pszFile specifies the directory and
3381 * file specification (e.g. "F:\*.txt").
3382 *
3383 * Returns TRUE if the user pressed OK. In that
3384 * case, the fully qualified filename is written
3385 * into pszFile again.
3386 *
3387 * Returns FALSE if the user pressed Cancel.
3388 *
3389 * Notes about flFlags:
3390 *
3391 * -- WINH_FOD_SAVEDLG: display a "Save As" dialog.
3392 * Otherwise an "Open" dialog is displayed.
3393 *
3394 * -- WINH_FOD_INILOADDIR: load a directory from the
3395 * specified INI key and switch the dlg to it.
3396 * In that case, on input, pszFile must only
3397 * contain the file filter without any path
3398 * specification, because that is loaded from
3399 * the INI key. If the INI key does not exist,
3400 * the current process directory will be used.
3401 *
3402 * -- WINH_FOD_INISAVEDIR: if the user presses OK,
3403 * the directory of the selected file is written
3404 * to the specified INI key so that it can be
3405 * reused later. This flag is independent of
3406 * WINH_FOD_INISAVEDIR: you can specify none,
3407 * one, or both of them.
3408 *
3409 *@@added V0.9.3 (2000-04-29) [umoeller]
3410 *@@changed V0.9.12 (2001-05-21) [umoeller]: this failed if INI data had root dir, fixed
3411 */
3412
3413BOOL winhFileDlg(HWND hwndOwner, // in: owner for file dlg
3414 PSZ pszFile, // in: file mask; out: fully q'd filename
3415 // (should be CCHMAXPATH in size)
3416 ULONG flFlags, // in: any combination of the following:
3417 // -- WINH_FOD_SAVEDLG: save dlg; else open dlg
3418 // -- WINH_FOD_INILOADDIR: load FOD path from INI
3419 // -- WINH_FOD_INISAVEDIR: store FOD path to INI on OK
3420 HINI hini, // in: INI file to load/store last path from (can be HINI_USER)
3421 const char *pcszApplication, // in: INI application to load/store last path from
3422 const char *pcszKey) // in: INI key to load/store last path from
3423{
3424 FILEDLG fd;
3425 FILESTATUS3 fs3;
3426
3427 memset(&fd, 0, sizeof(FILEDLG));
3428 fd.cbSize = sizeof(FILEDLG);
3429 fd.fl = FDS_CENTER;
3430
3431 if (flFlags & WINH_FOD_SAVEDLG)
3432 fd.fl |= FDS_SAVEAS_DIALOG;
3433 else
3434 fd.fl |= FDS_OPEN_DIALOG;
3435
3436 if ( (hini)
3437 && (flFlags & WINH_FOD_INILOADDIR)
3438 && (PrfQueryProfileString(hini,
3439 (PSZ)pcszApplication,
3440 (PSZ)pcszKey,
3441 "", // default string V0.9.9 (2001-02-10) [umoeller]
3442 fd.szFullFile,
3443 sizeof(fd.szFullFile)-10)
3444 > 2)
3445 // added these checks V0.9.12 (2001-05-21) [umoeller]
3446 && (!DosQueryPathInfo(fd.szFullFile,
3447 FIL_STANDARD,
3448 &fs3,
3449 sizeof(fs3)))
3450 && (fs3.attrFile & FILE_DIRECTORY)
3451 )
3452 {
3453 // found: append "\*"
3454 strcat(fd.szFullFile, "\\");
3455 strcat(fd.szFullFile, pszFile);
3456 }
3457 else
3458 // default: copy pszFile
3459 strcpy(fd.szFullFile, pszFile);
3460 // fixed V0.9.12 (2001-05-21) [umoeller]
3461
3462 if ( WinFileDlg(HWND_DESKTOP, // parent
3463 hwndOwner, // owner
3464 &fd)
3465 && (fd.lReturn == DID_OK)
3466 )
3467 {
3468 // save path back?
3469 if ( (hini)
3470 && (flFlags & WINH_FOD_INISAVEDIR)
3471 )
3472 {
3473 // get the directory that was used
3474 PSZ p = strrchr(fd.szFullFile, '\\');
3475 if (p)
3476 {
3477 // contains directory:
3478 // copy to OS2.INI
3479 PSZ pszDir = strhSubstr(fd.szFullFile, p);
3480 if (pszDir)
3481 {
3482 PrfWriteProfileString(hini,
3483 (PSZ)pcszApplication,
3484 (PSZ)pcszKey,
3485 pszDir);
3486 free(pszDir);
3487 }
3488 }
3489 }
3490
3491 strcpy(pszFile, fd.szFullFile);
3492
3493 return (TRUE);
3494 }
3495
3496 return (FALSE);
3497}
3498
3499/*
3500 *@@ winhSetWaitPointer:
3501 * this sets the mouse pointer to "Wait".
3502 * Returns the previous pointer (HPOINTER),
3503 * which should be stored somewhere to be
3504 * restored later. Example:
3505 + HPOINTER hptrOld = winhSetWaitPointer();
3506 + ...
3507 + WinSetPointer(HWND_DESKTOP, hptrOld);
3508 */
3509
3510HPOINTER winhSetWaitPointer(VOID)
3511{
3512 HPOINTER hptr = WinQueryPointer(HWND_DESKTOP);
3513 WinSetPointer(HWND_DESKTOP,
3514 WinQuerySysPointer(HWND_DESKTOP,
3515 SPTR_WAIT,
3516 FALSE)); // no copy
3517 return (hptr);
3518}
3519
3520/*
3521 *@@ winhQueryWindowText:
3522 * this returns the window text of the specified
3523 * HWND in a newly allocated buffer.
3524 *
3525 * Returns NULL on error. Use free()
3526 * to free the return value.
3527 */
3528
3529PSZ winhQueryWindowText(HWND hwnd)
3530{
3531 PSZ pszText = NULL;
3532 ULONG cbText = WinQueryWindowTextLength(hwnd);
3533 // additional null character
3534 if (cbText)
3535 {
3536 pszText = (PSZ)malloc(cbText + 1);
3537 if (pszText)
3538 WinQueryWindowText(hwnd,
3539 cbText + 1,
3540 pszText);
3541 }
3542 return (pszText);
3543}
3544
3545/*
3546 *@@ winhSetWindowText:
3547 * like WinSetWindowText, but this one accepts
3548 * printf-like arguments.
3549 *
3550 * Note that the total string is limited to
3551 * 1000 characters.
3552 *
3553 *@@added V0.9.16 (2001-10-08) [umoeller]
3554 */
3555
3556BOOL winhSetWindowText(HWND hwnd,
3557 const char *pcszFormat,
3558 ...)
3559{
3560 CHAR szBuf[1000];
3561 va_list args;
3562 int i;
3563 va_start(args, pcszFormat);
3564 i = vsprintf(szBuf, pcszFormat, args);
3565 va_end(args);
3566
3567 return (WinSetWindowText(hwnd,
3568 szBuf));
3569}
3570
3571/*
3572 *@@ winhReplaceWindowText:
3573 * this is a combination of winhQueryWindowText
3574 * and strhFindReplace to replace substrings in a window.
3575 *
3576 * This is useful for filling in placeholders
3577 * a la "%1" in control windows, e.g. static
3578 * texts.
3579 *
3580 * This replaces only the first occurence of
3581 * pszSearch.
3582 *
3583 * Returns TRUE only if the window exists and
3584 * the search string was replaced.
3585 *
3586 *@@added V0.9.0 [umoeller]
3587 */
3588
3589BOOL winhReplaceWindowText(HWND hwnd, // in: window whose text is to be modified
3590 const char *pcszSearch, // in: search string (e.g. "%1")
3591 const char *pcszReplaceWith) // in: replacement string for pszSearch
3592{
3593 BOOL brc = FALSE;
3594 PSZ pszText = winhQueryWindowText(hwnd);
3595 if (pszText)
3596 {
3597 ULONG ulOfs = 0;
3598 if (strhFindReplace(&pszText, &ulOfs, pcszSearch, pcszReplaceWith) > 0)
3599 {
3600 WinSetWindowText(hwnd, pszText);
3601 brc = TRUE;
3602 }
3603 free(pszText);
3604 }
3605 return (brc);
3606}
3607
3608/*
3609 *@@ winhEnableDlgItems:
3610 * this enables/disables a whole range of controls
3611 * in a window by enumerating the child windows
3612 * until usIDFirst is found. If so, that subwindow
3613 * is enabled/disabled and all the following windows
3614 * in the enumeration also, until usIDLast is found.
3615 *
3616 * Note that this affects _all_ controls following
3617 * the usIDFirst window, no matter what ID they have
3618 * (even if "-1"), until usIDLast is found.
3619 *
3620 * Returns the no. of controls which were enabled/disabled
3621 * (null if none).
3622 *
3623 *@@added V0.9.0 [umoeller]
3624 *@@changed V0.9.1 (99-12-20) [umoeller]: renamed from winhEnableDlgItems
3625 */
3626
3627ULONG winhEnableControls(HWND hwndDlg, // in: dialog window
3628 USHORT usIDFirst, // in: first affected control ID
3629 USHORT usIDLast, // in: last affected control ID (inclusive)
3630 BOOL fEnable)
3631{
3632 HENUM henum1 = NULLHANDLE;
3633 HWND hwndThis = NULLHANDLE;
3634 ULONG ulCount = 0;
3635
3636 henum1 = WinBeginEnumWindows(hwndDlg);
3637 while ((hwndThis = WinGetNextWindow(henum1)) != NULLHANDLE)
3638 {
3639 USHORT usIDCheckFirst = WinQueryWindowUShort(hwndThis, QWS_ID),
3640 usIDCheckLast;
3641 if (usIDCheckFirst == usIDFirst)
3642 {
3643 WinEnableWindow(hwndThis, fEnable);
3644 ulCount++;
3645
3646 while ((hwndThis = WinGetNextWindow(henum1)) != NULLHANDLE)
3647 {
3648 WinEnableWindow(hwndThis, fEnable);
3649 ulCount++;
3650 usIDCheckLast = WinQueryWindowUShort(hwndThis, QWS_ID);
3651 if (usIDCheckLast == usIDLast)
3652 break;
3653 }
3654
3655 break; // outer loop
3656 }
3657 }
3658 WinEndEnumWindows(henum1);
3659 return (ulCount);
3660}
3661
3662/*
3663 *@@ winhCreateStdWindow:
3664 * much like WinCreateStdWindow, but this one
3665 * allows you to have the standard window
3666 * positioned automatically, using a given
3667 * SWP structure (*pswpFrame).
3668 *
3669 * The frame is created with the specified parent
3670 * (usually HWND_DESKTOP), but no owner.
3671 *
3672 * The client window is created with the frame as
3673 * its parent and owner and gets an ID of FID_CLIENT.
3674 *
3675 * Alternatively, you can set pswpFrame to NULL
3676 * and specify FCF_SHELLPOSITION with flFrameCreateFlags.
3677 * If you want the window to be shown, specify
3678 * SWP_SHOW (and maybe SWP_ACTIVATE) in *pswpFrame.
3679 *
3680 *@@added V0.9.0 [umoeller]
3681 *@@changed V0.9.5 (2000-08-13) [umoeller]: flStyleClient never worked, fixed
3682 *@@changed V0.9.7 (2000-12-08) [umoeller]: fixed client calc for invisible window
3683 */
3684
3685HWND winhCreateStdWindow(HWND hwndFrameParent, // in: normally HWND_DESKTOP
3686 PSWP pswpFrame, // in: frame wnd pos (ptr can be NULL)
3687 ULONG flFrameCreateFlags, // in: FCF_* flags
3688 ULONG ulFrameStyle, // in: WS_* flags (e.g. WS_VISIBLE, WS_ANIMATE)
3689 const char *pcszFrameTitle, // in: frame title (title bar)
3690 ULONG ulResourcesID, // in: according to FCF_* flags
3691 const char *pcszClassClient, // in: client class name
3692 ULONG flStyleClient, // in: client style
3693 ULONG ulID, // in: frame window ID
3694 PVOID pClientCtlData, // in: pCtlData structure pointer for client
3695 PHWND phwndClient) // out: created client wnd
3696{
3697 FRAMECDATA fcdata;
3698 HWND hwndFrame;
3699 RECTL rclClient;
3700
3701 fcdata.cb = sizeof(FRAMECDATA);
3702 fcdata.flCreateFlags = flFrameCreateFlags;
3703 fcdata.hmodResources = (HMODULE)NULL;
3704 fcdata.idResources = ulResourcesID;
3705
3706 /* Create the frame and client windows. */
3707 hwndFrame = WinCreateWindow(hwndFrameParent,
3708 WC_FRAME,
3709 (PSZ)pcszFrameTitle,
3710 ulFrameStyle,
3711 0,0,0,0, // size and position = 0
3712 NULLHANDLE, // no owner
3713 HWND_TOP, // z-order
3714 ulID, // frame window ID
3715 &fcdata, // frame class data
3716 NULL); // no presparams
3717
3718 if (hwndFrame)
3719 {
3720 *phwndClient = WinCreateWindow(hwndFrame, // parent
3721 (PSZ)pcszClassClient, // class
3722 NULL, // no title
3723 flStyleClient, // style
3724 0,0,0,0, // size and position = 0
3725 hwndFrame, // owner
3726 HWND_BOTTOM, // bottom z-order
3727 FID_CLIENT, // frame window ID
3728 pClientCtlData, // class data
3729 NULL); // no presparams
3730
3731 if (*phwndClient)
3732 {
3733 if (pswpFrame)
3734 {
3735 // position frame
3736 WinSetWindowPos(hwndFrame,
3737 pswpFrame->hwndInsertBehind,
3738 pswpFrame->x,
3739 pswpFrame->y,
3740 pswpFrame->cx,
3741 pswpFrame->cy,
3742 pswpFrame->fl);
3743
3744 // position client
3745 // WinQueryWindowRect(hwndFrame, &rclClient);
3746 // doesn't work because it might be invisible V0.9.7 (2000-12-08) [umoeller]
3747 rclClient.xLeft = 0;
3748 rclClient.yBottom = 0;
3749 rclClient.xRight = pswpFrame->cx;
3750 rclClient.yTop = pswpFrame->cy;
3751 WinCalcFrameRect(hwndFrame,
3752 &rclClient,
3753 TRUE); // calc client from frame
3754 WinSetWindowPos(*phwndClient,
3755 HWND_TOP,
3756 rclClient.xLeft,
3757 rclClient.yBottom,
3758 rclClient.xRight - rclClient.xLeft,
3759 rclClient.yTop - rclClient.yBottom,
3760 SWP_MOVE | SWP_SIZE | SWP_SHOW);
3761 }
3762 }
3763 }
3764 return (hwndFrame);
3765}
3766
3767/*
3768 *@@ winhCreateObjectWindow:
3769 * creates an object window of the specified
3770 * window class, which you should have registered
3771 * before calling this. pvCreateParam will be
3772 * given to the window on WM_CREATE.
3773 *
3774 * Returns the HWND of the object window or
3775 * NULLHANDLE on errors.
3776 *
3777 *@@added V0.9.3 (2000-04-17) [umoeller]
3778 *@@changed V0.9.7 (2001-01-17) [umoeller]: made this a function from a macro
3779 */
3780
3781HWND winhCreateObjectWindow(const char *pcszWindowClass, // in: PM window class name
3782 PVOID pvCreateParam) // in: create param
3783{
3784 return (WinCreateWindow(HWND_OBJECT,
3785 (PSZ)pcszWindowClass,
3786 (PSZ)"",
3787 0,
3788 0,0,0,0,
3789 0,
3790 HWND_BOTTOM,
3791 0,
3792 pvCreateParam,
3793 NULL));
3794}
3795
3796/*
3797 *@@ winhCreateControl:
3798 * creates a control with a size and position of 0.
3799 *
3800 *@@added V0.9.9 (2001-03-13) [umoeller]
3801 */
3802
3803HWND winhCreateControl(HWND hwndParentAndOwner, // in: owner and parent window
3804 const char *pcszClass, // in: window class (e.g. WC_BUTTON)
3805 const char *pcszText, // in: window title
3806 ULONG ulStyle, // in: control style
3807 ULONG ulID) // in: control ID
3808{
3809 return (WinCreateWindow(hwndParentAndOwner,
3810 (PSZ)pcszClass,
3811 (PSZ)pcszText,
3812 ulStyle,
3813 0, 0, 0, 0,
3814 hwndParentAndOwner,
3815 HWND_TOP,
3816 ulID,
3817 NULL,
3818 NULL));
3819}
3820
3821/*
3822 *@@ winhRepaintWindows:
3823 * this repaints all children of hwndParent.
3824 * If this is passed as HWND_DESKTOP, the
3825 * whole screen is repainted.
3826 *
3827 *@@changed V0.9.7 (2000-12-13) [umoeller]: hwndParent was never respected, fixed
3828 */
3829
3830VOID winhRepaintWindows(HWND hwndParent)
3831{
3832 HWND hwndTop;
3833 HENUM henum = WinBeginEnumWindows(hwndParent);
3834 while ((hwndTop = WinGetNextWindow(henum)))
3835 if (WinIsWindowShowing(hwndTop))
3836 WinInvalidateRect(hwndTop, NULL, TRUE);
3837 WinEndEnumWindows(henum);
3838}
3839
3840/*
3841 *@@ winhFindMsgQueue:
3842 * returns the message queue which matches
3843 * the given process and thread IDs. Since,
3844 * per IBM definition, every thread may only
3845 * have one MQ, this should be unique.
3846 *
3847 *@@added V0.9.2 (2000-03-08) [umoeller]
3848 */
3849
3850HMQ winhFindMsgQueue(PID pid, // in: process ID
3851 TID tid, // in: thread ID
3852 HAB* phab) // out: anchor block
3853{
3854 HWND hwndThis = 0,
3855 rc = 0;
3856 HENUM henum = WinBeginEnumWindows(HWND_OBJECT);
3857 while ((hwndThis = WinGetNextWindow(henum)))
3858 {
3859 CHAR szClass[200];
3860 if (WinQueryClassName(hwndThis, sizeof(szClass), szClass))
3861 {
3862 if (strcmp(szClass, "#32767") == 0)
3863 {
3864 // message queue window:
3865 PID pidWin = 0;
3866 TID tidWin = 0;
3867 WinQueryWindowProcess(hwndThis,
3868 &pidWin,
3869 &tidWin);
3870 if ( (pidWin == pid)
3871 && (tidWin == tid)
3872 )
3873 {
3874 // get HMQ from window words
3875 rc = WinQueryWindowULong(hwndThis, QWL_HMQ);
3876 if (rc)
3877 if (phab)
3878 *phab = WinQueryAnchorBlock(hwndThis);
3879 break;
3880 }
3881 }
3882 }
3883 }
3884 WinEndEnumWindows(henum);
3885
3886 return (rc);
3887}
3888
3889/*
3890 *@@ winhFindHardErrorWindow:
3891 * this searches all children of HWND_OBJECT
3892 * for the PM hard error windows, which are
3893 * invisible most of the time. When a hard
3894 * error occurs, that window is made a child
3895 * of HWND_DESKTOP instead.
3896 *
3897 * Stolen from ProgramCommander/2 (C) Roman Stangl.
3898 *
3899 *@@added V0.9.3 (2000-04-27) [umoeller]
3900 */
3901
3902VOID winhFindPMErrorWindows(HWND *phwndHardError, // out: hard error window
3903 HWND *phwndSysError) // out: system error window
3904{
3905 PID pidObject; // HWND_OBJECT's process and thread id
3906 TID tidObject;
3907 PID pidObjectChild; // HWND_OBJECT's child window process and thread id
3908 TID tidObjectChild;
3909 HENUM henumObject; // HWND_OBJECT enumeration handle
3910 HWND hwndObjectChild; // Window handle of current HWND_OBJECT child
3911 UCHAR ucClassName[32]; // Window class e.g. #1 for WC_FRAME
3912 CLASSINFO classinfoWindow; // Class info of current HWND_OBJECT child
3913
3914 *phwndHardError = NULLHANDLE;
3915 *phwndSysError = NULLHANDLE;
3916
3917 // query HWND_OBJECT's window process
3918 WinQueryWindowProcess(WinQueryObjectWindow(HWND_DESKTOP), &pidObject, &tidObject);
3919 // enumerate all child windows of HWND_OBJECT
3920 henumObject = WinBeginEnumWindows(HWND_OBJECT);
3921 while ((hwndObjectChild = WinGetNextWindow(henumObject)) != NULLHANDLE)
3922 {
3923 // see if the current HWND_OBJECT child window runs in the
3924 // process of HWND_OBJECT (PM)
3925 WinQueryWindowProcess(hwndObjectChild, &pidObjectChild, &tidObjectChild);
3926 if (pidObject == pidObjectChild)
3927 {
3928 // get the child window's data
3929 WinQueryClassName(hwndObjectChild,
3930 sizeof(ucClassName),
3931 (PCH)ucClassName);
3932 WinQueryClassInfo(WinQueryAnchorBlock(hwndObjectChild),
3933 (PSZ)ucClassName,
3934 &classinfoWindow);
3935 if ( (!strcmp((PSZ)ucClassName, "#1")
3936 || (classinfoWindow.flClassStyle & CS_FRAME))
3937 )
3938 {
3939 // if the child window is a frame window and running in
3940 // HWND_OBJECT's (PM's) window process, it must be the
3941 // PM Hard Error or System Error window
3942 WinQueryClassName(WinWindowFromID(hwndObjectChild,
3943 FID_CLIENT),
3944 sizeof(ucClassName),
3945 (PSZ)ucClassName);
3946 if (!strcmp((PSZ)ucClassName, "PM Hard Error"))
3947 {
3948 *phwndHardError = hwndObjectChild;
3949 if (*phwndSysError)
3950 // we found the other one already:
3951 // stop searching, we got both
3952 break;
3953 }
3954 else
3955 {
3956 printf("Utility: Found System Error %08X\n", (int)hwndObjectChild);
3957 *phwndSysError = hwndObjectChild;
3958 if (*phwndHardError)
3959 // we found the other one already:
3960 // stop searching, we got both
3961 break;
3962 }
3963 }
3964 } // end if (pidObject == pidObjectChild)
3965 } // end while ((hwndObjectChild = WinGetNextWindow(henumObject)) != NULLHANDLE)
3966 WinEndEnumWindows(henumObject);
3967}
3968
3969/*
3970 *@@ winhCreateFakeDesktop:
3971 * this routine creates and displays a frameless window over
3972 * the whole screen in the color of PM's Desktop to fool the
3973 * user that all windows have been closed (which in fact might
3974 * not be the case).
3975 *
3976 * This window's background color is set to the Desktop's
3977 * (PM's one, not the WPS's one).
3978 *
3979 * Returns the HWND of this window.
3980 */
3981
3982HWND winhCreateFakeDesktop(HWND hwndSibling)
3983{
3984 // presparam for background
3985 typedef struct _BACKGROUND
3986 {
3987 ULONG cb; // length of the aparam parameter, in bytes
3988 ULONG id; // attribute type identity
3989 ULONG cb2; // byte count of the ab parameter
3990 RGB rgb; // attribute value
3991 } BACKGROUND;
3992
3993 BACKGROUND background;
3994 LONG lDesktopColor;
3995
3996 // create fake desktop window = empty window with
3997 // the size of full screen
3998 lDesktopColor = WinQuerySysColor(HWND_DESKTOP,
3999 SYSCLR_BACKGROUND,
4000 0);
4001 background.cb = sizeof(background.id)
4002 + sizeof(background.cb)
4003 + sizeof(background.rgb);
4004 background.id = PP_BACKGROUNDCOLOR;
4005 background.cb2 = sizeof(RGB);
4006 background.rgb.bBlue = (CHAR1FROMMP(lDesktopColor));
4007 background.rgb.bGreen= (CHAR2FROMMP(lDesktopColor));
4008 background.rgb.bRed = (CHAR3FROMMP(lDesktopColor));
4009
4010 return (WinCreateWindow(HWND_DESKTOP, // parent window
4011 WC_FRAME, // class name
4012 "", // window text
4013 WS_VISIBLE, // window style
4014 0, 0, // position and size
4015 WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN),
4016 WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN),
4017 NULLHANDLE, // owner window
4018 hwndSibling, // sibling window
4019 1, // window id
4020 NULL, // control data
4021 &background)); // presentation parms
4022}
4023
4024/*
4025 *@@ winhAssertWarp4Notebook:
4026 * this takes hwndDlg as a notebook dialog page and
4027 * goes thru all its controls. If a control with an
4028 * ID <= udIdThreshold is found, this is assumed to
4029 * be a button which is to be given the BS_NOTEBOOKBUTTON
4030 * style. You should therefore give all your button
4031 * controls which should be moved such an ID.
4032 *
4033 * Note that this function will now automatically
4034 * find out the lowest y coordinate that was used
4035 * for a non-notebook button and move all controls
4036 * down accordingly. As a result, ulDownUnit must
4037 * no longer be specified (V0.9.19).
4038 *
4039 * This function is useful if you wish to create
4040 * notebook pages using dlgedit.exe which are compatible
4041 * with both Warp 3 and Warp 4. This should be executed
4042 * in WM_INITDLG of the notebook dlg function if the app
4043 * has determined that it is running on Warp 4.
4044 *
4045 *@@changed V0.9.16 (2002-02-02) [umoeller]: fixed entry fields
4046 *@@changed V0.9.19 (2002-04-24) [umoeller]: removed ulDownUnits
4047 *@@changed V0.9.19 (2002-05-02) [umoeller]: fix for combobox
4048 */
4049
4050BOOL winhAssertWarp4Notebook(HWND hwndDlg,
4051 USHORT usIdThreshold) // in: ID threshold
4052{
4053 BOOL brc = FALSE;
4054
4055 if (doshIsWarp4())
4056 {
4057 LONG yLowest = 10000;
4058 HWND hwndItem;
4059 HENUM henum = 0;
4060 PSWP paswp,
4061 pswpThis;
4062 ULONG cWindows = 0,
4063 ul;
4064
4065 BOOL fIsVisible;
4066
4067 if (fIsVisible = WinIsWindowVisible(hwndDlg))
4068 // avoid flicker
4069 WinEnableWindowUpdate(hwndDlg, FALSE);
4070
4071 if (paswp = (PSWP)malloc(sizeof(SWP) * 100))
4072 {
4073 pswpThis = paswp;
4074
4075 // loop 1: set notebook buttons, find lowest y used
4076 henum = WinBeginEnumWindows(hwndDlg);
4077 while ((hwndItem = WinGetNextWindow(henum)))
4078 {
4079 USHORT usId = WinQueryWindowUShort(hwndItem, QWS_ID);
4080 // _Pmpf(("hwndItem: 0x%lX, ID: 0x%lX", hwndItem, usId));
4081 if (usId <= usIdThreshold)
4082 {
4083 // pushbutton to change:
4084 WinSetWindowBits(hwndItem,
4085 QWL_STYLE,
4086 BS_NOTEBOOKBUTTON, BS_NOTEBOOKBUTTON);
4087 brc = TRUE;
4088 }
4089 else
4090 {
4091 // no pushbutton to change:
4092 CHAR szClass[10];
4093
4094 WinQueryWindowPos(hwndItem, pswpThis);
4095
4096 // special handling for entry fields
4097 // V0.9.16 (2002-02-02) [umoeller]
4098 WinQueryClassName(hwndItem, sizeof(szClass), szClass);
4099 if (!strcmp(szClass, "#6"))
4100 {
4101 pswpThis->x += 3 * WinQuerySysValue(HWND_DESKTOP, SV_CXBORDER);
4102 pswpThis->y += 3 * WinQuerySysValue(HWND_DESKTOP, SV_CYBORDER);
4103 }
4104
4105 // check lowest y
4106 if ( (pswpThis->y < yLowest)
4107 // ignore combobox, this will distort everything
4108 // AGAIN ... sigh V0.9.19 (2002-05-02) [umoeller]
4109 && (strcmp(szClass, "#2"))
4110 )
4111 yLowest = pswpThis->y ;
4112
4113 ++pswpThis;
4114 if (++cWindows == 100)
4115 break;
4116 }
4117 } // end while ((hwndItem = WinGetNextWindow(henum)))
4118 WinEndEnumWindows(henum);
4119
4120 // now adjust window positions
4121 pswpThis = paswp;
4122 for (ul = 0;
4123 ul < cWindows;
4124 ++ul, ++pswpThis)
4125 {
4126 pswpThis->y -= (yLowest - 8);
4127 // 8 is magic to match the lower border of the
4128 // standard WPS notebook pages V0.9.19 (2002-04-24) [umoeller]
4129 pswpThis->fl = SWP_MOVE;
4130 }
4131
4132 WinSetMultWindowPos(WinQueryAnchorBlock(hwndDlg),
4133 paswp,
4134 cWindows);
4135
4136 free(paswp);
4137 }
4138
4139 if (fIsVisible)
4140 WinShowWindow(hwndDlg, TRUE);
4141 }
4142
4143 return (brc);
4144}
4145
4146/*
4147 *@@ winhDrawFormattedText:
4148 * this func takes a rectangle and draws pszText into
4149 * it, breaking the words as neccessary. The line spacing
4150 * is determined from the font currently selected in hps.
4151 *
4152 * As opposed to WinDrawText, this can draw several lines
4153 * at once, and format the _complete_ text according to the
4154 * flCmd parameter, which is like with WinDrawText.
4155 *
4156 * After this function returns, *prcl is modified like this:
4157 *
4158 * -- yTop and yBottom contain the upper and lower boundaries
4159 * which were needed to draw the text. This depends on
4160 * whether DT_TOP etc. were specified.
4161 * To get the height of the rectangle used, calculate the
4162 * delta between yTop and yBottom.
4163 *
4164 * -- xLeft and xRight are modified to contain the outmost
4165 * left and right coordinates which were needed to draw
4166 * the text. This will be set to the longest line which
4167 * was encountered.
4168 *
4169 * You can specify DT_QUERYEXTENT with flDraw to only have
4170 * these text boundaries calculated without actually drawing.
4171 *
4172 * This returns the number of lines drawn.
4173 *
4174 * Note that this calls WinDrawText with DT_TEXTATTRS set,
4175 * that is, the current text primitive attributes will be
4176 * used (fonts and colors).
4177 *
4178 *@@changed V0.9.0 [umoeller]: prcl.xLeft and xRight are now updated too upon return
4179 */
4180
4181ULONG winhDrawFormattedText(HPS hps, // in: presentation space; its settings
4182 // are used, but not altered
4183 PRECTL prcl, // in/out: rectangle to use for drawing
4184 // (modified)
4185 const char *pcszText, // in: text to draw (zero-terminated)
4186 ULONG flCmd) // in: flags like in WinDrawText; I have
4187 // only tested DT_TOP and DT_LEFT though.
4188 // DT_WORDBREAK | DT_TEXTATTRS are always
4189 // set.
4190 // You can specify DT_QUERYEXTENT to only
4191 // have prcl calculated without drawing.
4192{
4193 PSZ p = (PSZ)pcszText;
4194 LONG lDrawn = 1,
4195 lTotalDrawn = 0,
4196 lLineCount = 0,
4197 lOrigYTop = prcl->yTop;
4198 ULONG ulTextLen = strlen(pcszText),
4199 ulCharHeight,
4200 flCmd2,
4201 xLeftmost = prcl->xRight,
4202 xRightmost = prcl->xLeft;
4203 RECTL rcl2;
4204
4205 flCmd2 = flCmd | DT_WORDBREAK | DT_TEXTATTRS;
4206
4207 ulCharHeight = gpihQueryLineSpacing(hps);
4208
4209 while ( (lDrawn)
4210 && (lTotalDrawn < ulTextLen)
4211 )
4212 {
4213 memcpy(&rcl2, prcl, sizeof(rcl2));
4214 lDrawn = WinDrawText(hps,
4215 ulTextLen-lTotalDrawn,
4216 p,
4217 &rcl2,
4218 0, 0, // colors
4219 flCmd2);
4220
4221 // update char counters
4222 p += lDrawn;
4223 lTotalDrawn += lDrawn;
4224
4225 // update x extents
4226 if (rcl2.xLeft < xLeftmost)
4227 xLeftmost = rcl2.xLeft;
4228 if (rcl2.xRight > xRightmost)
4229 xRightmost = rcl2.xRight;
4230
4231 // update y for next line
4232 prcl->yTop -= ulCharHeight;
4233
4234 // increase line count
4235 lLineCount++;
4236 }
4237 prcl->xLeft = xLeftmost;
4238 prcl->xRight = xRightmost;
4239 prcl->yBottom = prcl->yTop;
4240 prcl->yTop = lOrigYTop;
4241
4242 return (lLineCount);
4243}
4244
4245/*
4246 *@@ winhQuerySwitchList:
4247 * returns the switch list in a newly
4248 * allocated buffer. This does the
4249 * regular double WinQuerySwitchList
4250 * call to first get the no. of items
4251 * and then get the items.
4252 *
4253 * The no. of items can be found in
4254 * the returned SWBLOCK.cwsentry.
4255 *
4256 * Returns NULL on errors. Use
4257 * free() to free the return value.
4258 *
4259 *@@added V0.9.7 (2000-12-06) [umoeller]
4260 */
4261
4262PSWBLOCK winhQuerySwitchList(HAB hab)
4263{
4264 ULONG cItems = WinQuerySwitchList(hab, NULL, 0);
4265 ULONG ulBufSize = (cItems * sizeof(SWENTRY)) + sizeof(HSWITCH);
4266 PSWBLOCK pSwBlock = (PSWBLOCK)malloc(ulBufSize);
4267 if (pSwBlock)
4268 {
4269 cItems = WinQuerySwitchList(hab, pSwBlock, ulBufSize);
4270 if (!cItems)
4271 {
4272 free(pSwBlock);
4273 pSwBlock = NULL;
4274 }
4275 }
4276
4277 return (pSwBlock);
4278}
4279
4280typedef HSWITCH APIENTRY WINHSWITCHFROMHAPP(HAPP happ);
4281
4282/*
4283 *@@ winhHSWITCHfromHAPP:
4284 * resolves and calls the undocumented
4285 * WinHSWITCHfromHAPP API.
4286 *
4287 *@@added V0.9.19 (2002-04-17) [umoeller]
4288 */
4289
4290HSWITCH winhHSWITCHfromHAPP(HAPP happ)
4291{
4292 static WINHSWITCHFROMHAPP *pWinHSWITCHfromHAPP = NULL;
4293
4294 if (!pWinHSWITCHfromHAPP)
4295 // first call: import WinHSWITCHfromHAPP
4296 // WinHSWITCHfromHAPP PMMERGE.5199
4297 doshQueryProcAddr("PMMERGE",
4298 5199,
4299 (PFN*)&pWinHSWITCHfromHAPP);
4300
4301 if (pWinHSWITCHfromHAPP)
4302 return pWinHSWITCHfromHAPP(happ);
4303
4304 return NULLHANDLE;
4305}
4306
4307/*
4308 *@@ winhQueryTasklistWindow:
4309 * returns the window handle of the PM task list.
4310 *
4311 *@@added V0.9.7 (2000-12-07) [umoeller]
4312 */
4313
4314HWND winhQueryTasklistWindow(VOID)
4315{
4316 SWBLOCK swblock;
4317 // HWND hwndTasklist = winhQueryTasklistWindow();
4318 // the tasklist has entry #0 in the SWBLOCK
4319 WinQuerySwitchList(NULLHANDLE, &swblock, sizeof(SWBLOCK));
4320 return (swblock.aswentry[0].swctl.hwnd);
4321}
4322
4323/*
4324 *@@ winhKillTasklist:
4325 * this will destroy the Tasklist (window list) window.
4326 * Note: you will only be able to get it back after a
4327 * reboot, not a WPS restart. Only for use at shutdown and such.
4328 * This trick by Uri J. Stern at
4329 * http://zebra.asta.fh-weingarten.de/os2/Snippets/Howt8881.HTML
4330 */
4331
4332VOID winhKillTasklist(VOID)
4333{
4334 HWND hwndTasklist = winhQueryTasklistWindow();
4335 WinPostMsg(hwndTasklist,
4336 0x0454, // undocumented msg for killing tasklist
4337 NULL, NULL);
4338}
4339
4340// the following must be added for EMX (99-10-22) [umoeller]
4341#ifndef NERR_BufTooSmall
4342 #define NERR_BASE 2100
4343 #define NERR_BufTooSmall (NERR_BASE+23)
4344 // the API return buffer is too small
4345#endif
4346
4347/*
4348 *@@ winhQueryPendingSpoolJobs:
4349 * returns the number of pending print jobs in the spooler
4350 * or 0 if none. Useful for testing before shutdown.
4351 */
4352
4353ULONG winhQueryPendingSpoolJobs(VOID)
4354{
4355 // BOOL rcPending = FALSE;
4356 ULONG ulTotalJobCount = 0;
4357
4358 SPLERR splerr;
4359 USHORT jobCount;
4360 ULONG cbBuf;
4361 ULONG cTotal;
4362 ULONG cReturned;
4363 ULONG cbNeeded;
4364 ULONG ulLevel;
4365 ULONG i,j;
4366 PSZ pszComputerName;
4367 PBYTE pBuf = NULL;
4368 PPRQINFO3 prq;
4369 PPRJINFO2 prj2;
4370
4371 ulLevel = 4L;
4372 pszComputerName = (PSZ)NULL;
4373 splerr = SplEnumQueue(pszComputerName, ulLevel, pBuf, 0L, // cbBuf
4374 &cReturned, &cTotal,
4375 &cbNeeded, NULL);
4376 if ( (splerr == ERROR_MORE_DATA)
4377 || (splerr == NERR_BufTooSmall)
4378 )
4379 {
4380 if (!DosAllocMem((PPVOID)&pBuf,
4381 cbNeeded,
4382 PAG_READ | PAG_WRITE | PAG_COMMIT))
4383 {
4384 cbBuf = cbNeeded;
4385 splerr = SplEnumQueue(pszComputerName, ulLevel, pBuf, cbBuf,
4386 &cReturned, &cTotal,
4387 &cbNeeded, NULL);
4388 if (splerr == NO_ERROR)
4389 {
4390 // set pointer to point to the beginning of the buffer
4391 prq = (PPRQINFO3)pBuf;
4392
4393 // cReturned has the count of the number of PRQINFO3 structures
4394 for (i = 0;
4395 i < cReturned;
4396 i++)
4397 {
4398 // save the count of jobs; there are this many PRJINFO2
4399 // structures following the PRQINFO3 structure
4400 jobCount = prq->cJobs;
4401 // _Pmpf(( "Job count in this queue is %d",jobCount ));
4402
4403 // increment the pointer past the PRQINFO3 structure
4404 prq++;
4405
4406 // set a pointer to point to the first PRJINFO2 structure
4407 prj2=(PPRJINFO2)prq;
4408 for (j = 0;
4409 j < jobCount;
4410 j++)
4411 {
4412 // increment the pointer to point to the next structure
4413 prj2++;
4414 // increase the job count, which we'll return
4415 ulTotalJobCount++;
4416
4417 } // endfor jobCount
4418
4419 // after doing all the job structures, prj2 points to the next
4420 // queue structure; set the pointer for a PRQINFO3 structure
4421 prq = (PPRQINFO3)prj2;
4422 } //endfor cReturned
4423 } // endif NO_ERROR
4424 DosFreeMem(pBuf);
4425 }
4426 } // end if Q level given
4427
4428 return (ulTotalJobCount);
4429}
4430
4431/*
4432 *@@ winhSetNumLock:
4433 * this sets the NumLock key on or off, depending
4434 * on fState.
4435 *
4436 * Based on code from WarpEnhancer, (C) Achim Hasenmller.
4437 *
4438 *@@added V0.9.1 (99-12-18) [umoeller]
4439 */
4440
4441VOID winhSetNumLock(BOOL fState)
4442{
4443 // BOOL fRestoreKBD = FALSE; // Assume we're not going to close Kbd
4444 BYTE KeyStateTable[256];
4445 ULONG ulActionTaken; // Used by DosOpen
4446 HFILE hKbd;
4447
4448 // read keyboard state table
4449 if (WinSetKeyboardStateTable(HWND_DESKTOP, &KeyStateTable[0],
4450 FALSE))
4451 {
4452 // first set the PM state
4453 if (fState)
4454 KeyStateTable[VK_NUMLOCK] |= 0x01; // Turn numlock on
4455 else
4456 KeyStateTable[VK_NUMLOCK] &= 0xFE; // Turn numlock off
4457
4458 // set keyboard state table with new state values
4459 WinSetKeyboardStateTable(HWND_DESKTOP, &KeyStateTable[0], TRUE);
4460 }
4461
4462 // now set the OS/2 keyboard state
4463
4464 // try to open OS/2 keyboard driver
4465 if (!DosOpen("KBD$",
4466 &hKbd, &ulActionTaken,
4467 0, // cbFile
4468 FILE_NORMAL,
4469 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
4470 OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
4471 NULL))
4472 {
4473 SHIFTSTATE ShiftState;
4474 ULONG DataLen = sizeof(SHIFTSTATE);
4475
4476 memset(&ShiftState, '\0', DataLen);
4477 DosDevIOCtl(hKbd, IOCTL_KEYBOARD, KBD_GETSHIFTSTATE,
4478 NULL, 0L, NULL,
4479 &ShiftState, DataLen, &DataLen);
4480
4481 if (fState)
4482 ShiftState.fsState |= 0x0020; // turn NumLock on
4483 else
4484 ShiftState.fsState &= 0xFFDF; // turn NumLock off
4485
4486 DosDevIOCtl(hKbd, IOCTL_KEYBOARD, KBD_SETSHIFTSTATE,
4487 &ShiftState, DataLen, &DataLen,
4488 NULL, 0L, NULL);
4489 // now close OS/2 keyboard driver
4490 DosClose(hKbd);
4491 }
4492 return;
4493}
4494
4495/*
4496 *@@category: Helpers\PM helpers\Extended frame windows
4497 */
4498
4499/* ******************************************************************
4500 *
4501 * Extended frame
4502 *
4503 ********************************************************************/
4504
4505/*
4506 *@@ fnwpSubclExtFrame:
4507 * subclassed frame window proc.
4508 *
4509 *@@added V0.9.16 (2001-09-29) [umoeller]
4510 */
4511
4512MRESULT EXPENTRY fnwpSubclExtFrame(HWND hwndFrame, ULONG msg, MPARAM mp1, MPARAM mp2)
4513{
4514 MRESULT mrc = 0;
4515
4516 PEXTFRAMEDATA pData = (PEXTFRAMEDATA)WinQueryWindowPtr(hwndFrame, QWL_USER);
4517
4518 switch (msg)
4519 {
4520 case WM_QUERYFRAMECTLCOUNT:
4521 {
4522 // query the standard frame controls count
4523 ULONG ulrc = (ULONG)pData->pfnwpOrig(hwndFrame, msg, mp1, mp2);
4524
4525 // if we have a status bar, increment the count
4526 ulrc++;
4527
4528 mrc = (MPARAM)ulrc;
4529 }
4530 break;
4531
4532 case WM_FORMATFRAME:
4533 {
4534 // query the number of standard frame controls
4535 ULONG ulCount = (ULONG)pData->pfnwpOrig(hwndFrame, msg, mp1, mp2);
4536
4537 // we have a status bar:
4538 // format the frame
4539 ULONG ul;
4540 PSWP swpArr = (PSWP)mp1;
4541
4542 for (ul = 0; ul < ulCount; ul++)
4543 {
4544 if (WinQueryWindowUShort( swpArr[ul].hwnd, QWS_ID ) == 0x8008 )
4545 // FID_CLIENT
4546 {
4547 POINTL ptlBorderSizes;
4548 ULONG ulStatusBarHeight = 20;
4549 WinSendMsg(hwndFrame,
4550 WM_QUERYBORDERSIZE,
4551 (MPARAM)&ptlBorderSizes,
4552 0);
4553
4554 // first initialize the _new_ SWP for the status bar.
4555 // Since the SWP array for the std frame controls is
4556 // zero-based, and the standard frame controls occupy
4557 // indices 0 thru ulCount-1 (where ulCount is the total
4558 // count), we use ulCount for our static text control.
4559 swpArr[ulCount].fl = SWP_MOVE | SWP_SIZE | SWP_NOADJUST | SWP_ZORDER;
4560 swpArr[ulCount].x = ptlBorderSizes.x;
4561 swpArr[ulCount].y = ptlBorderSizes.y;
4562 swpArr[ulCount].cx = swpArr[ul].cx; // same as cnr's width
4563 swpArr[ulCount].cy = ulStatusBarHeight;
4564 swpArr[ulCount].hwndInsertBehind = HWND_BOTTOM; // HWND_TOP;
4565 swpArr[ulCount].hwnd = WinWindowFromID(hwndFrame, FID_STATUSBAR);
4566
4567 // adjust the origin and height of the container to
4568 // accomodate our static text control
4569 swpArr[ul].y += swpArr[ulCount].cy;
4570 swpArr[ul].cy -= swpArr[ulCount].cy;
4571 }
4572 }
4573
4574 // increment the number of frame controls
4575 // to include our status bar
4576 mrc = (MRESULT)(ulCount + 1);
4577 }
4578 break;
4579
4580 case WM_CALCFRAMERECT:
4581 {
4582 mrc = pData->pfnwpOrig(hwndFrame, msg, mp1, mp2);
4583
4584 // we have a status bar: calculate its rectangle
4585 // CalcFrameRect(mp1, mp2);
4586 }
4587 break;
4588
4589 case WM_DESTROY:
4590 WinSubclassWindow(hwndFrame, pData->pfnwpOrig);
4591 free(pData);
4592 WinSetWindowPtr(hwndFrame, QWL_USER, NULL);
4593 break;
4594
4595 default:
4596 mrc = pData->pfnwpOrig(hwndFrame, msg, mp1, mp2);
4597 }
4598
4599 return (mrc);
4600}
4601
4602/*
4603 *@@ winhCreateStatusBar:
4604 * creates a status bar for a frame window.
4605 *
4606 * Normally there's no need to call this manually,
4607 * this gets called by winhCreateExtStdWindow
4608 * automatically.
4609 *
4610 *@@added V0.9.16 (2001-09-29) [umoeller]
4611 */
4612
4613HWND winhCreateStatusBar(HWND hwndFrame,
4614 HWND hwndOwner,
4615 const char *pcszText, // in: initial status bar text
4616 const char *pcszFont, // in: font to use for status bar
4617 LONG lColor) // in: foreground color for status bar
4618{
4619 // create status bar
4620 HWND hwndReturn = NULLHANDLE;
4621 PPRESPARAMS ppp = NULL;
4622 winhStorePresParam(&ppp, PP_FONTNAMESIZE, strlen(pcszFont)+1, (PVOID)pcszFont);
4623 lColor = WinQuerySysColor(HWND_DESKTOP, SYSCLR_DIALOGBACKGROUND, 0);
4624 winhStorePresParam(&ppp, PP_BACKGROUNDCOLOR, sizeof(lColor), &lColor);
4625 lColor = CLR_BLACK;
4626 winhStorePresParam(&ppp, PP_FOREGROUNDCOLOR, sizeof(lColor), &lColor);
4627 hwndReturn = WinCreateWindow(hwndFrame,
4628 WC_STATIC,
4629 (PSZ)pcszText,
4630 SS_TEXT | DT_VCENTER | WS_VISIBLE,
4631 0, 0, 0, 0,
4632 hwndOwner,
4633 HWND_TOP,
4634 FID_STATUSBAR,
4635 NULL,
4636 ppp);
4637 free(ppp);
4638 return (hwndReturn);
4639}
4640
4641/*
4642 *@@ winhCreateExtStdWindow:
4643 * creates an extended frame window.
4644 *
4645 * pData must point to an EXTFRAMECDATA structure
4646 * which contains a copy of the parameters to be
4647 * passed to winhCreateStdWindow. In addition,
4648 * this contains the flExtFlags field, which allows
4649 * you to automatically create a status bar for
4650 * the window.
4651 *
4652 * Note that we subclass the frame here and require
4653 * QWL_USER for that. The frame's QWL_USER points
4654 * to an EXTFRAMEDATA structure whose pUser parameter
4655 * you may use for additional data, if you want to
4656 * do further subclassing.
4657 *
4658 *@@added V0.9.16 (2001-09-29) [umoeller]
4659 */
4660
4661HWND winhCreateExtStdWindow(PEXTFRAMECDATA pData, // in: extended frame data
4662 PHWND phwndClient) // out: created client wnd
4663{
4664 HWND hwndFrame;
4665
4666 if (hwndFrame = winhCreateStdWindow(HWND_DESKTOP,
4667 pData->pswpFrame,
4668 pData->flFrameCreateFlags,
4669 pData->ulFrameStyle,
4670 pData->pcszFrameTitle,
4671 pData->ulResourcesID,
4672 pData->pcszClassClient,
4673 pData->flStyleClient,
4674 pData->ulID,
4675 pData->pClientCtlData,
4676 phwndClient))
4677 {
4678 if (pData->flExtFlags & XFCF_STATUSBAR)
4679 {
4680 // create status bar as child of the frame
4681 HWND hwndStatusBar = winhCreateStatusBar(hwndFrame,
4682 hwndFrame,
4683 "",
4684 "9.WarpSans",
4685 CLR_BLACK);
4686
4687 // subclass frame for supporting status bar and msgs
4688 PEXTFRAMEDATA pFrameData;
4689 if (pFrameData = NEW(EXTFRAMEDATA))
4690 {
4691 ZERO(pFrameData),
4692 memcpy(&pFrameData->CData, pData, sizeof(pFrameData->CData));
4693 if (pFrameData->pfnwpOrig = WinSubclassWindow(hwndFrame,
4694 fnwpSubclExtFrame))
4695 {
4696 WinSetWindowPtr(hwndFrame, QWL_USER, pFrameData);
4697 }
4698 else
4699 free(pFrameData);
4700 }
4701 }
4702 }
4703
4704 return (hwndFrame);
4705}
4706
4707/*
4708 *@@category: Helpers\PM helpers\Workplace Shell\WPS class list
4709 */
4710
4711/* ******************************************************************
4712 *
4713 * WPS Class List helpers
4714 *
4715 ********************************************************************/
4716
4717/*
4718 *@@ winhQueryWPSClassList:
4719 * this returns the WPS class list in a newly
4720 * allocated buffer. This is just a shortcut to
4721 * the usual double WinEnumObjectClasses call.
4722 *
4723 * The return value is actually of the POBJCLASS type,
4724 * so you better cast this manually. We declare this
4725 * this as PBYTE though because POBJCLASS requires
4726 * INCL_WINWORKPLACE.
4727 * See WinEnumObjectClasses() for details.
4728 *
4729 * Returns NULL on error. Use free()
4730 * to free the return value.
4731 *
4732 *@@added V0.9.0 [umoeller]
4733 */
4734
4735PBYTE winhQueryWPSClassList(VOID)
4736{
4737 ULONG ulSize;
4738 POBJCLASS pObjClass = 0;
4739
4740 // get WPS class list size
4741 if (WinEnumObjectClasses(NULL, &ulSize))
4742 {
4743 // allocate buffer
4744 pObjClass = (POBJCLASS)malloc(ulSize+1);
4745 // and load the classes into it
4746 WinEnumObjectClasses(pObjClass, &ulSize);
4747 }
4748
4749 return ((PBYTE)pObjClass);
4750}
4751
4752/*
4753 *@@ winhQueryWPSClass:
4754 * this returns the POBJCLASS item if pszClass is registered
4755 * with the WPS or NULL if the class could not be found.
4756 *
4757 * The return value is actually of the POBJCLASS type,
4758 * so you better cast this manually. We declare this
4759 * this as PBYTE though because POBJCLASS requires
4760 * INCL_WINWORKPLACE.
4761 *
4762 * This takes as input the return value of winhQueryWPSClassList,
4763 * which you must call first.
4764 *
4765 * <B>Usage:</B>
4766 + PBYTE pClassList = winhQueryWPSClassList(),
4767 + pWPFolder;
4768 + if (pClassList)
4769 + {
4770 + if (pWPFolder = winhQueryWPSClass(pClassList, "WPFolder"))
4771 + ...
4772 + free(pClassList);
4773 + }
4774 *
4775 *@@added V0.9.0 [umoeller]
4776 */
4777
4778PBYTE winhQueryWPSClass(PBYTE pObjClass, // in: buffer returned by
4779 // winhQueryWPSClassList
4780 const char *pszClass) // in: class name to query
4781{
4782 PBYTE pbReturn = 0;
4783
4784 POBJCLASS pocThis = (POBJCLASS)pObjClass;
4785 // now go thru the WPS class list
4786 while (pocThis)
4787 {
4788 if (strcmp(pocThis->pszClassName, pszClass) == 0)
4789 {
4790 pbReturn = (PBYTE)pocThis;
4791 break;
4792 }
4793 // next class
4794 pocThis = pocThis->pNext;
4795 } // end while (pocThis)
4796
4797 return (pbReturn);
4798}
4799
4800/*
4801 *@@ winhRegisterClass:
4802 * this works just like WinRegisterObjectClass,
4803 * except that it returns a more meaningful
4804 * error code than just FALSE in case registering
4805 * fails.
4806 *
4807 * This returns NO_ERROR if the class was successfully
4808 * registered (WinRegisterObjectClass returned TRUE).
4809 *
4810 * Otherwise, we do a DosLoadModule if maybe the DLL
4811 * couldn't be loaded in the first place. If DosLoadModule
4812 * did not return NO_ERROR, this function returns that
4813 * return code, which can be:
4814 *
4815 * -- 2 ERROR_FILE_NOT_FOUND: pcszModule does not exist
4816 * -- 2 ERROR_FILE_NOT_FOUND
4817 * -- 3 ERROR_PATH_NOT_FOUND
4818 * -- 4 ERROR_TOO_MANY_OPEN_FILES
4819 * -- 5 ERROR_ACCESS_DENIED
4820 * -- 8 ERROR_NOT_ENOUGH_MEMORY
4821 * -- 11 ERROR_BAD_FORMAT
4822 * -- 26 ERROR_NOT_DOS_DISK (unknown media type)
4823 * -- 32 ERROR_SHARING_VIOLATION
4824 * -- 33 ERROR_LOCK_VIOLATION
4825 * -- 36 ERROR_SHARING_BUFFER_EXCEEDED
4826 * -- 95 ERROR_INTERRUPT (interrupted system call)
4827 * -- 108 ERROR_DRIVE_LOCKED (by another process)
4828 * -- 123 ERROR_INVALID_NAME (illegal character or FS name not valid)
4829 * -- 127 ERROR_PROC_NOT_FOUND (DosQueryProcAddr error)
4830 * -- 180 ERROR_INVALID_SEGMENT_NUMBER
4831 * -- 182 ERROR_INVALID_ORDINAL
4832 * -- 190 ERROR_INVALID_MODULETYPE (probably an application)
4833 * -- 191 ERROR_INVALID_EXE_SIGNATURE (probably not LX DLL)
4834 * -- 192 ERROR_EXE_MARKED_INVALID (by linker)
4835 * -- 194 ERROR_ITERATED_DATA_EXCEEDS_64K (in a DLL segment)
4836 * -- 195 ERROR_INVALID_MINALLOCSIZE
4837 * -- 196 ERROR_DYNLINK_FROM_INVALID_RING
4838 * -- 198 ERROR_INVALID_SEGDPL
4839 * -- 199 ERROR_AUTODATASEG_EXCEEDS_64K
4840 * -- 201 ERROR_RELOCSRC_CHAIN_EXCEEDS_SEGLIMIT
4841 * -- 206 ERROR_FILENAME_EXCED_RANGE (not matching 8+3 spec)
4842 * -- 295 ERROR_INIT_ROUTINE_FAILED (DLL init routine failed)
4843 *
4844 * In all these cases, pszBuf may contain a meaningful
4845 * error message from DosLoadModule, especially if an import
4846 * could not be resolved.
4847 *
4848 * Still worse, if DosLoadModule returned NO_ERROR, we
4849 * probably have some SOM internal error. A probable
4850 * reason is that the parent class of pcszClassName
4851 * is not installed, but that's WPS/SOM internal
4852 * and cannot be queried from outside the WPS context.
4853 *
4854 * In that case, ERROR_OPEN_FAILED (110) is returned.
4855 * That one sounded good to me. ;-)
4856 */
4857
4858APIRET winhRegisterClass(const char* pcszClassName, // in: e.g. "XFolder"
4859 const char* pcszModule, // in: e.g. "C:\XFOLDER\XFLDR.DLL"
4860 PSZ pszBuf, // out: error message from DosLoadModule
4861 ULONG cbBuf) // in: sizeof(*pszBuf), passed to DosLoadModule
4862{
4863 APIRET arc = NO_ERROR;
4864
4865 if (!WinRegisterObjectClass((PSZ)pcszClassName, (PSZ)pcszModule))
4866 {
4867 // failed: do more error checking then, try DosLoadModule
4868 HMODULE hmod = NULLHANDLE;
4869 arc = DosLoadModule(pszBuf, cbBuf,
4870 (PSZ)pcszModule,
4871 &hmod);
4872 if (arc == NO_ERROR)
4873 {
4874 // DosLoadModule succeeded:
4875 // some SOM error then
4876 DosFreeModule(hmod);
4877 arc = ERROR_OPEN_FAILED;
4878 }
4879 }
4880 // else: ulrc still 0 (== no error)
4881
4882 return (arc);
4883}
4884
4885/*
4886 *@@ winhIsClassRegistered:
4887 * quick one-shot function which checks if
4888 * a class is currently registered. Calls
4889 * winhQueryWPSClassList and winhQueryWPSClass
4890 * in turn.
4891 *
4892 *@@added V0.9.2 (2000-02-26) [umoeller]
4893 */
4894
4895BOOL winhIsClassRegistered(const char *pcszClass)
4896{
4897 BOOL brc = FALSE;
4898 PBYTE pClassList = winhQueryWPSClassList();
4899 if (pClassList)
4900 {
4901 if (winhQueryWPSClass(pClassList, pcszClass))
4902 brc = TRUE;
4903 free(pClassList);
4904 }
4905
4906 return (brc);
4907}
4908
4909/*
4910 *@@category: Helpers\PM helpers\Workplace Shell
4911 */
4912
4913/*
4914 *@@ winhResetWPS:
4915 * restarts the WPS using PrfReset. Returns
4916 * one of the following:
4917 *
4918 * -- 0: no error.
4919 * -- 1: PrfReset failed.
4920 * -- 2 or 4: PrfQueryProfile failed.
4921 * -- 3: malloc() failed.
4922 *
4923 *@@added V0.9.4 (2000-07-01) [umoeller]
4924 */
4925
4926ULONG winhResetWPS(HAB hab)
4927{
4928 ULONG ulrc = 0;
4929 // find out current profile names
4930 PRFPROFILE Profiles;
4931 Profiles.cchUserName = Profiles.cchSysName = 0;
4932 // first query their file name lengths
4933 if (PrfQueryProfile(hab, &Profiles))
4934 {
4935 // allocate memory for filenames
4936 Profiles.pszUserName = (PSZ)malloc(Profiles.cchUserName);
4937 Profiles.pszSysName = (PSZ)malloc(Profiles.cchSysName);
4938
4939 if (Profiles.pszSysName)
4940 {
4941 // get filenames
4942 if (PrfQueryProfile(hab, &Profiles))
4943 {
4944
4945 // "change" INIs to these filenames:
4946 // THIS WILL RESET THE WPS
4947 if (PrfReset(hab, &Profiles) == FALSE)
4948 ulrc = 1;
4949 free(Profiles.pszSysName);
4950 free(Profiles.pszUserName);
4951 }
4952 else
4953 ulrc = 2;
4954 }
4955 else
4956 ulrc = 3;
4957 }
4958 else
4959 ulrc = 4;
4960
4961 return (ulrc);
4962}
Note: See TracBrowser for help on using the repository browser.