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

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

Misc helpers updates.

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