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

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

Misc changes.

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