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

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

Fixes for V0.9.7.

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