source: trunk/src/helpers/comctl.c@ 91

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

Misc changes

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 47.9 KB
Line 
1
2/*
3 *@@sourcefile comctl.c:
4 * contains various window procedures for implementing
5 * common controls. The source file name has nothing to do
6 * with the Windoze DLL of the same name.
7 *
8 * Usage: All PM programs.
9 *
10 * Function prefixes (new with V0.81):
11 * -- ctl* common control helper functions
12 *
13 * The functionality of this code is accessed with the use
14 * of a special helper function. Sometimes that function
15 * registers a new window class, sometimes a static control
16 * needs to be subclassed. See the respective functions for
17 * details.
18 *
19 * In detail, we have:
20 *
21 * -- a "menu button" control, which displays a menu when
22 * pressed (see ctlMakeMenuButton for details);
23 *
24 * -- progress bar support (see ctlProgressBarFromStatic for
25 * details);
26 *
27 * -- a "chart" control for displaying pie charts (all new
28 * with V0.9.0; see ctlChartFromStatic for details);
29 *
30 * -- split windows support (all new with V0.9.0; see
31 * ctlCreateSplitWindow for details);
32 *
33 * -- a subclassed static control for enhanced bitmap and
34 * and icon display (see ctl_fnwpBitmapStatic for details).
35 * This used to be in animate.c and has been enhanced
36 * with V0.9.0;
37 *
38 * -- a "tooltip" control, which shows fly-over ("bubble")
39 * help over any window, including controls. This is
40 * largely API-compatible with the Win95 tooltip control.
41 * See ctl_fnwpTooltip for details;
42 *
43 * -- a "checkbox container" control, which is a subclassed
44 * container which uses checkboxes as record icons.
45 * See ctlMakeCheckboxContainer for details.
46 *
47 * Note: Version numbering in this file relates to XWorkplace version
48 * numbering.
49 *
50 *@@header "helpers\comctl.h"
51 */
52
53/*
54 * Copyright (C) 1997-2000 Ulrich M”ller.
55 * This file is part of the "XWorkplace helpers" source package.
56 * This is free software; you can redistribute it and/or modify
57 * it under the terms of the GNU General Public License as published
58 * by the Free Software Foundation, in version 2 as it comes in the
59 * "COPYING" file of the XWorkplace main distribution.
60 * This program is distributed in the hope that it will be useful,
61 * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 * GNU General Public License for more details.
64 */
65
66#define OS2EMX_PLAIN_CHAR
67 // this is needed for "os2emx.h"; if this is defined,
68 // emx will define PSZ as _signed_ char, otherwise
69 // as unsigned char
70
71#define INCL_DOSEXCEPTIONS
72#define INCL_DOSPROCESS
73#define INCL_DOSSEMAPHORES
74#define INCL_DOSERRORS
75
76#define INCL_WINWINDOWMGR
77#define INCL_WINFRAMEMGR
78#define INCL_WINMESSAGEMGR
79#define INCL_WININPUT
80#define INCL_WINPOINTERS
81#define INCL_WINTRACKRECT
82#define INCL_WINTIMER
83#define INCL_WINSYS
84
85#define INCL_WINRECTANGLES /// xxx temporary
86
87#define INCL_WINMENUS
88#define INCL_WINSTATICS
89#define INCL_WINBUTTONS
90#define INCL_WINSTDCNR
91
92#define INCL_GPIPRIMITIVES
93#define INCL_GPILOGCOLORTABLE
94#define INCL_GPILCIDS
95#define INCL_GPIPATHS
96#define INCL_GPIREGIONS
97#define INCL_GPIBITMAPS // added V0.9.1 (2000-01-04) [umoeller]: needed for EMX headers
98#include <os2.h>
99
100#include <stdlib.h>
101#include <stdio.h>
102#include <string.h>
103#include <setjmp.h> // needed for except.h
104#include <assert.h> // needed for except.h
105
106#include "setup.h" // code generation and debugging options
107
108#include "helpers\cnrh.h"
109#include "helpers\except.h" // exception handling
110#include "helpers\gpih.h"
111#include "helpers\linklist.h"
112#include "helpers\winh.h"
113
114#include "helpers\comctl.h"
115
116#pragma hdrstop
117
118/* ******************************************************************
119 *
120 * "XButton" control
121 *
122 ********************************************************************/
123
124/*
125 *@@ ctlPaintXButton:
126 * paints an X-button control. Can be called externally
127 * for just painting a button even if this is not really
128 * a window.
129 *
130 * WARNING: this is work in progress and will change into
131 * the future. Eventually this will turn into a full
132 * button control replacement.
133 *
134 *@@added V0.9.13 (2001-06-21) [umoeller]
135 */
136
137VOID ctlPaintXButton(HPS hps, // in: presentation space (RGB mode)
138 ULONG fl, // in: XBF_* flags
139 PXBUTTONDATA pxbd) // in: button data
140{
141 ULONG ulBorder = 0,
142 cx,
143 cy,
144 ulOfs = 0;
145 LONG lLeft,
146 lRight;
147 RECTL rclWin;
148 memcpy(&rclWin, &pxbd->rcl, sizeof(RECTL));
149
150 if (0 == (fl & XBF_FLAT))
151 ulBorder = 2;
152
153 gpihSwitchToRGB(hps);
154
155 if (fl & XBF_PRESSED)
156 {
157 // paint button "down":
158 lLeft = pxbd->lcol3DDark;
159 lRight = pxbd->lcol3DLight;
160 // add offset for icon painting at the bottom
161 ulOfs += 1;
162 if (ulBorder == 0)
163 ulBorder = 1;
164 }
165 else
166 {
167 lLeft = pxbd->lcol3DLight;
168 lRight = pxbd->lcol3DDark;
169 }
170
171 if (ulBorder)
172 {
173 // button border:
174
175 // now paint button frame
176 rclWin.xRight--;
177 rclWin.yTop--;
178 gpihDraw3DFrame(hps,
179 &rclWin, // inclusive
180 ulBorder,
181 lLeft,
182 lRight);
183
184 // now paint button middle
185 rclWin.xLeft += ulBorder;
186 rclWin.yBottom += ulBorder;
187 rclWin.xRight -= ulBorder - 1; // make exclusive again
188 rclWin.yTop -= ulBorder - 1; // make exclusive again
189 }
190
191 if (fl & XBF_BACKGROUND)
192 WinFillRect(hps,
193 &rclWin, // exclusive
194 pxbd->lMiddle);
195
196 // get icon
197 if (pxbd->hptr)
198 {
199 // calculate x and y to be centered in rectangle
200 POINTL ptl;
201
202 cx = rclWin.xRight - rclWin.xLeft;
203 cy = rclWin.yTop - rclWin.yBottom;
204
205 ptl.x = rclWin.xLeft + ((cx - pxbd->cxMiniIcon) / 2);
206 ptl.y = rclWin.yBottom + ((cy - pxbd->cxMiniIcon) / 2);
207
208 if (fl & XBF_INUSE)
209 {
210 // caller wants in-use (hatched) emphasis:
211 // draw a box then
212 POINTL ptl2;
213 ptl2.x = ptl.x - 2;
214 ptl2.y = ptl.y - 2;
215 GpiMove(hps,
216 &ptl);
217 GpiSetPattern(hps, PATSYM_DIAG1);
218 ptl2.x = ptl.x + pxbd->cxMiniIcon + 1; // inclusive!
219 ptl2.y = ptl.y + pxbd->cxMiniIcon + 1; // inclusive!
220 GpiBox(hps,
221 DRO_FILL,
222 &ptl2,
223 0,
224 0);
225 }
226
227 // now paint icon
228 GpiIntersectClipRectangle(hps, &rclWin); // exclusive!
229 WinDrawPointer(hps,
230 // center this in remaining rectl
231 ptl.x + ulOfs,
232 ptl.y - ulOfs,
233 pxbd->hptr,
234 DP_MINI);
235 }
236}
237
238/*
239 *@@category: Helpers\PM helpers\Window classes\Menu buttons
240 * See comctl.c and ctlMakeMenuButton.
241 */
242
243/* ******************************************************************
244 *
245 * "Menu button" control
246 *
247 ********************************************************************/
248
249/*
250 *@@ MENUBUTTONDATA:
251 * internal data for "menu button"
252 *
253 *@@added V0.9.0 [umoeller]
254 */
255
256typedef struct _MENUBUTTONDATA
257{
258 PFNWP pfnwpButtonOriginal;
259 HMODULE hmodMenu;
260 ULONG idMenu;
261 BOOL fMouseCaptured, // TRUE if WinSetCapture
262 fMouseButton1Down, // TRUE in between WM_BUTTON1DOWN and WM_BUTTON1UP
263 fButtonSunk; // toggle state of the button
264 HWND hwndMenu;
265} MENUBUTTONDATA, *PMENUBUTTONDATA;
266
267/*
268 *@@ ctlDisplayButtonMenu:
269 * displays the specified menu above the button.
270 *
271 *@@added V0.9.7 (2000-11-29) [umoeller]
272 */
273
274VOID ctlDisplayButtonMenu(HWND hwndButton,
275 HWND hwndMenu)
276{
277 SWP swpButton;
278 POINTL ptlMenu;
279 WinQueryWindowPos(hwndButton, &swpButton);
280 ptlMenu.x = swpButton.x;
281 ptlMenu.y = swpButton.y;
282
283 // ptlMenu now has button coordinates
284 // relative to the button's parent;
285 // convert this to screen coordinates:
286 WinMapWindowPoints(WinQueryWindow(hwndButton, QW_PARENT),
287 HWND_DESKTOP,
288 &ptlMenu,
289 1);
290
291 // now show the menu on top of the button
292 WinPopupMenu(HWND_DESKTOP, // menu parent
293 hwndButton, // owner
294 hwndMenu,
295 (SHORT)(ptlMenu.x),
296 (SHORT)(ptlMenu.y + swpButton.cy - 1),
297 0, // ID
298 PU_NONE
299 | PU_MOUSEBUTTON1
300 | PU_KEYBOARD
301 | PU_HCONSTRAIN
302 | PU_VCONSTRAIN);
303}
304
305/*
306 *@@ ctl_fnwpSubclassedMenuButton:
307 * subclassed window proc for "menu button".
308 * See ctlMakeMenuButton for details.
309 *
310 *@@added V0.9.0 [umoeller]
311 *@@changed V0.9.2 (2000-02-28) [umoeller]: menu was displayed even if button was disabled; fixed
312 *@@changed V0.9.14 (2001-07-31) [umoeller]: fixed WM_MENUEND submenu quirk
313 */
314
315MRESULT EXPENTRY ctl_fnwpSubclassedMenuButton(HWND hwndButton, ULONG msg, MPARAM mp1, MPARAM mp2)
316{
317 MRESULT mrc = 0;
318 PMENUBUTTONDATA pmbd = (PMENUBUTTONDATA)WinQueryWindowULong(hwndButton, QWL_USER);
319
320 switch (msg)
321 {
322 /*
323 * WM_BUTTON1DOWN:
324 * WM_BUTTON1UP:
325 * these show/hide the menu.
326 *
327 * Showing the menu follows these steps:
328 * a) first WM_BUTTON1DOWN hilites the button;
329 * b) first WM_BUTTON1UP shows the menu.
330 *
331 * When the button is pressed again, the open
332 * menu loses focus, which results in WM_MENUEND
333 * and destroys the window automatically.
334 */
335
336 case WM_BUTTON1DOWN:
337 case WM_BUTTON1DBLCLK:
338 // only do this if the button is enabled
339 // V0.9.2 (2000-02-28) [umoeller]
340 if (WinIsWindowEnabled(hwndButton))
341 {
342 // _Pmpf(("WM_BUTTON1DOWN"));
343 // since we're not passing the message
344 // to WinDefWndProc, we need to give
345 // ourselves the focus
346 WinSetFocus(HWND_DESKTOP, hwndButton);
347
348 if (!pmbd->fMouseCaptured)
349 {
350 // capture mouse events while the
351 // mouse button is down
352 WinSetCapture(HWND_DESKTOP, hwndButton);
353 pmbd->fMouseCaptured = TRUE;
354 }
355
356 pmbd->fMouseButton1Down = TRUE;
357
358 if (!pmbd->fButtonSunk)
359 {
360 // button not hilited yet (first click):
361 // do it now
362 // _Pmpf((" Sinking menu WM_BUTTON1DOWN "));
363 pmbd->fButtonSunk = TRUE;
364 WinSendMsg(hwndButton,
365 BM_SETHILITE,
366 (MPARAM)TRUE,
367 (MPARAM)0);
368 }
369
370 // else: the menu has just been destroyed
371 // (WM_MENUEND below)
372 }
373
374 mrc = (MPARAM)TRUE; // message processed
375 break;
376
377 /*
378 * WM_BUTTON1UP:
379 *
380 */
381
382 case WM_BUTTON1UP:
383 // only do this if the button is enabled
384 // V0.9.2 (2000-02-28) [umoeller]
385 if (WinIsWindowEnabled(hwndButton))
386 {
387 // _Pmpf(("WM_BUTTON1UP sunk %d hwndMenu 0x%lX", pmbd->fButtonSunk, pmbd->hwndMenu));
388
389 // un-capture the mouse first
390 if (pmbd->fMouseCaptured)
391 {
392 WinSetCapture(HWND_DESKTOP, NULLHANDLE);
393 pmbd->fMouseCaptured = FALSE;
394 }
395
396 pmbd->fMouseButton1Down = FALSE;
397
398 if ( (pmbd->fButtonSunk) // set by WM_BUTTON1DOWN above
399 && (pmbd->hwndMenu) // menu currently showing
400 )
401 {
402 // button currently depressed:
403 // un-hilite button
404 // _Pmpf((" Unsinking menu WM_BUTTON1UP 1"));
405 pmbd->fButtonSunk = FALSE;
406 WinSendMsg(hwndButton,
407 BM_SETHILITE,
408 (MPARAM)FALSE,
409 (MPARAM)0);
410 }
411 else
412 {
413 // first button up:
414 // show menu
415
416 if (pmbd->idMenu)
417 {
418 // _Pmpf((" Loading menu hmod %lX id %lX", pmbd->hmodMenu, pmbd->idMenu));
419 // menu specified: load from
420 // specified resources
421 pmbd->hwndMenu = WinLoadMenu(hwndButton,
422 pmbd->hmodMenu,
423 pmbd->idMenu);
424 }
425 else
426 {
427 HWND hwndOwner = WinQueryWindow(hwndButton, QW_OWNER);
428 // _Pmpf((" Querying menu WM_COMMAND from owner 0x%lX", hwndOwner));
429 // send WM_COMMAND to owner
430 pmbd->hwndMenu
431 = (HWND)WinSendMsg(hwndOwner,
432 WM_COMMAND,
433 (MPARAM)(ULONG)WinQueryWindowUShort(hwndButton,
434 QWS_ID),
435 (MPARAM)0);
436 }
437
438 // _Pmpf((" Loaded menu, hwnd: 0x%lX", pmbd->hwndMenu));
439
440 if (pmbd->hwndMenu)
441 {
442 // menu successfully loaded:
443 // find out where to put it
444 ctlDisplayButtonMenu(hwndButton,
445 pmbd->hwndMenu);
446 } // end if (pmbd->hwndMenu)
447 else
448 {
449 // menu not loaded:
450 // _Pmpf((" Unsinking menu WM_BUTTON1UP 2"));
451 pmbd->fButtonSunk = FALSE;
452 WinSendMsg(hwndButton,
453 BM_SETHILITE,
454 (MPARAM)FALSE,
455 (MPARAM)0);
456 }
457 }
458 }
459
460 mrc = (MPARAM)TRUE; // message processed
461 break;
462
463 /*
464 * WM_BUTTON1CLICK:
465 * swallow this
466 */
467
468 case WM_BUTTON1CLICK:
469 mrc = (MPARAM)TRUE;
470 break;
471
472 /*
473 * WM_SETFOCUS:
474 * swallow this, the button keeps painting
475 * itself otherwise
476 */
477
478 case WM_SETFOCUS:
479 break;
480
481 /*
482 * WM_MENUEND:
483 * menu is destroyed; we get this
484 * because we're the owner
485 */
486
487 case WM_MENUEND:
488 {
489 if ((HWND)mp2 == pmbd->hwndMenu) // V0.9.14 (2001-07-31) [umoeller]
490 {
491 BOOL fUnHilite = TRUE;
492 // _Pmpf(("WM_MENUEND"));
493 // At this point, the menu has been
494 // destroyed already.
495 // Since WM_BUTTON1UP handles the
496 // default case that the user presses
497 // the menu button a second time while
498 // the menu is open, we only need
499 // to handle the case that the user
500 // c) presses some key on the menu (ESC, menu selection) or
501 // a) selects a menu item (which
502 // dismisses the menu) or
503 // b) clicks anywhere else.
504
505 // Case a) if mouse button 1 is not currently down
506 if ((WinGetKeyState(HWND_DESKTOP, VK_BUTTON1) & 0x8000) == 0)
507 // button 1 _not_ down:
508 // must be keyboard
509 fUnHilite = TRUE;
510 else
511 // button 1 _is_ down:
512 // query window under mouse pointer
513 ;
514
515 if (fUnHilite)
516 {
517 // _Pmpf((" Unsinking menu WM_MENUEND"));
518 pmbd->fButtonSunk = FALSE;
519 WinSendMsg(hwndButton,
520 BM_SETHILITE,
521 (MPARAM)FALSE,
522 (MPARAM)0);
523 }
524 pmbd->hwndMenu = NULLHANDLE;
525 } // end if ((HWND)mp1 == pmbd->pmbd->hwndMenu) // V0.9.14 (2001-07-31) [umoeller]
526 break; }
527
528 /*
529 * WM_COMMAND:
530 * this must be from the menu, so
531 * forward this to the button's owner
532 */
533
534 case WM_COMMAND:
535 WinPostMsg(WinQueryWindow(hwndButton, QW_OWNER),
536 msg,
537 mp1,
538 mp2);
539 break;
540
541 /*
542 * WM_DESTROY:
543 * clean up allocated data
544 */
545
546 case WM_DESTROY:
547 mrc = (*(pmbd->pfnwpButtonOriginal))(hwndButton, msg, mp1, mp2);
548 free(pmbd);
549 break;
550
551 default:
552 mrc = (*(pmbd->pfnwpButtonOriginal))(hwndButton, msg, mp1, mp2);
553 }
554
555 return (mrc);
556}
557
558/*
559 *@@ ctlMakeMenuButton:
560 * this turns the specified button into a "menu button",
561 * which shows a popup menu when the button is depressed.
562 * This is done by subclassing the menu button with
563 * ctl_fnwpSubclassedMenuButton.
564 *
565 * Simply call this function upon any button, and it'll
566 * turn in to a menu button.
567 *
568 * When the user presses the button, the specified menu
569 * is loaded from the resources. The button will then
570 * be set as the owner, but it will forward all WM_COMMAND
571 * messages from the menu to its own owner (probably your
572 * dialog), so you can handle WM_COMMAND messages just as
573 * if the menu was owned by your dialog.
574 *
575 * Alternatively, if you don't want to load a menu from
576 * the resources, you can specify idMenu == 0. In that case,
577 * when the menu button is pressed, it sends (!) a WM_COMMAND
578 * message to its owner with its ID in mp1 (as usual). The
579 * difference is that the return value from that message will
580 * be interpreted as a menu handle (HWND) to use for the button
581 * menu.
582 *
583 * The subclassed button also handles menu destruction etc.
584 * by itself.
585 *
586 *@@added V0.9.0 [umoeller]
587 */
588
589BOOL ctlMakeMenuButton(HWND hwndButton, // in: button to subclass
590 HMODULE hmodMenu, // in: resource module (can be NULLHANDLE for
591 // current EXE)
592 ULONG idMenu) // in: resource menu ID (or 0)
593{
594 BOOL brc = FALSE;
595 PMENUBUTTONDATA pmbd = (PMENUBUTTONDATA)malloc(sizeof(MENUBUTTONDATA));
596 if (pmbd)
597 {
598 memset(pmbd, 0, sizeof(MENUBUTTONDATA));
599 pmbd->pfnwpButtonOriginal = WinSubclassWindow(hwndButton,
600 ctl_fnwpSubclassedMenuButton);
601 if (pmbd->pfnwpButtonOriginal)
602 {
603 pmbd->hmodMenu = hmodMenu;
604 pmbd->idMenu = idMenu;
605 WinSetWindowULong(hwndButton, QWL_USER, (ULONG)pmbd);
606 brc = TRUE;
607 }
608 else
609 free(pmbd);
610 }
611 return (brc);
612}
613
614/*
615 *@@category: Helpers\PM helpers\Window classes\Static bitmaps
616 * See comctl.c and ctl_fnwpBitmapStatic.
617 */
618
619/* ******************************************************************
620 *
621 * Subclassed Static Bitmap Control
622 *
623 ********************************************************************/
624
625/*
626 *@@ ctl_fnwpBitmapStatic:
627 * subclassed wnd proc for both static controls which
628 * should display icons and stretched bitmaps.
629 *
630 * This is not a stand-alone window procedure, but must only
631 * be used with static controls subclassed by the functions
632 * listed below.
633 *
634 * This is now (V0.9.0) used in two contexts:
635 * -- when subclassed from ctlPrepareStaticIcon,
636 * this displays transparent icons properly
637 * (for regular icons or icon animations);
638 * -- when subclassed from ctlPrepareStretchedBitmap,
639 * this displays a stretched bitmap.
640 *
641 * The behavior depends on ANIMATIONDATA.ulFlags,
642 * which is set by both of these functions (either to
643 * ANF_ICON or ANF_BITMAP).
644 *
645 * Only one msg is of interest to the "user" application
646 * of this control, which is SM_SETHANDLE,
647 * the normal message sent to a static icon control to change
648 * its icon or bitmap.
649 * The new handle (HPOINTER or HBITMAP) is
650 * in mp1; mp2 must be null.
651 *
652 * Depending on ANIMATIONDATA.ulFlags, we do the following:
653 *
654 * -- ANF_ICON: Unfortunately, the
655 * standard static control paints garbage if
656 * the icons contain transparent areas, and the
657 * display often flickers too.
658 * We improve this by creating one bitmap from
659 * the icon that we were given which we can then
660 * simply copy to the screen in one step in
661 * WM_PAINT.
662 *
663 * -- ANF_BITMAP: we create a bitmap which is
664 * stretched to the size of the static control,
665 * using gpihStretchBitmap.
666 *
667 * In any case, a new bitmap is produced internally and
668 * stored so that it can easily be painted when WM_PAINT
669 * comes in. The source icon/bitmap can therefore
670 * be safely destroyed by the caller after sending
671 * SM_SETHANDLE. This bitmap is automatically deleted when
672 * the window is destroyed.
673 *
674 *@@changed V0.9.0 [umoeller]: function renamed
675 *@@changed V0.9.0 [umoeller]: added support for stretched bitmaps
676 *@@changed V0.9.0 [umoeller]: added halftoned display for WS_DISABLED
677 *@@changed V0.9.0 [umoeller]: exported gpihIcon2Bitmap function to gpih.c
678 *@@changed V0.9.0 [umoeller]: fixed paint errors when SM_SETHANDLE had NULL argument in mp1
679 */
680
681MRESULT EXPENTRY ctl_fnwpBitmapStatic(HWND hwndStatic, ULONG msg, MPARAM mp1, MPARAM mp2)
682{
683 PANIMATIONDATA pa = (PANIMATIONDATA)WinQueryWindowULong(hwndStatic, QWL_USER);
684 // animation data which was stored in window words
685 PFNWP OldStaticProc = NULL;
686 MRESULT mrc = NULL;
687
688 if (pa)
689 {
690 OldStaticProc = pa->OldStaticProc;
691
692 switch(msg)
693 {
694 /*
695 * WM_TIMER:
696 * this timer is started by the ctl* funcs
697 * below. Proceed to the next animation step.
698 *
699 * Note: this timer is only used with
700 * ANF_ICON, and even then, it
701 * is not necessarily running.
702 */
703
704 case WM_TIMER:
705 {
706 pa->usAniCurrent++;
707 if (pa->usAniCurrent >= pa->usAniCount)
708 pa->usAniCurrent = 0;
709
710 WinSendMsg(hwndStatic,
711 SM_SETHANDLE,
712 (MPARAM)pa->ahptrAniIcons[pa->usAniCurrent],
713 (MPARAM)NULL);
714 break; }
715
716 /*
717 * SM_SETHANDLE:
718 *
719 */
720
721 case SM_SETHANDLE:
722 {
723 HDC hdcMem;
724 HPS hpsMem;
725 SIZEL szlPage;
726
727 LONG lBkgndColor
728 = winhQueryPresColor(WinQueryWindow(hwndStatic, QW_PARENT),
729 PP_BACKGROUNDCOLOR,
730 FALSE,
731 SYSCLR_DIALOGBACKGROUND);
732
733 HPS hps = WinGetPS(hwndStatic);
734
735 // if we have created bitmaps previously, delete them
736 if (pa->hbm)
737 {
738 GpiDeleteBitmap(pa->hbm);
739 pa->hbm = NULLHANDLE;
740 }
741 if (pa->hbmHalftoned)
742 {
743 GpiDeleteBitmap(pa->hbmHalftoned);
744 pa->hbmHalftoned = NULLHANDLE;
745 }
746
747 // switch to RGB mode
748 gpihSwitchToRGB(hps);
749
750 // create a memory PS
751 szlPage.cx = pa->rclIcon.xRight - pa->rclIcon.xLeft;
752 szlPage.cy = pa->rclIcon.yTop - pa->rclIcon.yBottom;
753 if (gpihCreateMemPS(pa->hab,
754 &szlPage,
755 &hdcMem,
756 &hpsMem))
757 {
758 // switch the memory PS to RGB mode too
759 gpihSwitchToRGB(hpsMem);
760
761 // create a suitable bitmap w/ the size of the
762 // static control
763 if ((pa->hbm = gpihCreateBitmap(hpsMem,
764 szlPage.cx,
765 szlPage.cy)))
766 {
767 // associate the bit map with the memory PS
768 if (GpiSetBitmap(hpsMem, pa->hbm) != HBM_ERROR)
769 {
770 // fill the bitmap with the current static
771 // background color
772 POINTL ptl = {0, 0};
773 GpiMove(hpsMem, &ptl);
774 ptl.x = pa->rclIcon.xRight;
775 ptl.y = pa->rclIcon.yTop;
776 GpiSetColor(hpsMem,
777 lBkgndColor);
778 GpiBox(hpsMem,
779 DRO_FILL, // interior only
780 &ptl,
781 0, 0); // no corner rounding
782
783 /*
784 * ANF_ICON:
785 *
786 */
787
788 if (pa->ulFlags & ANF_ICON)
789 {
790 // store new icon in our own structure
791 pa->hptr = (HPOINTER)mp1;
792
793 if (pa->hptr)
794 // paint icon into bitmap
795 gpihIcon2Bitmap(hpsMem,
796 pa->hptr,
797 lBkgndColor,
798 WinQuerySysValue(HWND_DESKTOP, SV_CXICON));
799
800 } // end if (pa->ulFlags & ANF_ICON)
801
802 /*
803 * ANF_BITMAP:
804 *
805 */
806
807 else if (pa->ulFlags & ANF_BITMAP)
808 {
809 // store passed bitmap
810 HBITMAP hbmSource = (HBITMAP)mp1;
811
812 if (hbmSource)
813 gpihStretchBitmap(hpsMem, // target
814 hbmSource, // source
815 NULL, // use size of bitmap
816 &pa->rclIcon,
817 ((pa->ulFlags & ANF_PROPORTIONAL)
818 != 0));
819
820 } // end if (pa->ulFlags & ANF_BITMAP)
821
822 } // end if (GpiSetBitmap(...
823 } // if (pa->hbm = gpihCreateBitmap(hpsMem, &(pa->rclIcon)))
824
825 // in any case, clean up now
826 GpiDestroyPS(hpsMem);
827 DevCloseDC(hdcMem);
828 } // end if (gpihCreateMemPS(hab, &hdcMem, &hpsMem))
829
830 WinReleasePS(hps);
831
832 // enforce WM_PAINT
833 WinInvalidateRect(hwndStatic, NULL, FALSE);
834
835 break; }
836
837 /*
838 * WM_PAINT:
839 * "normal" paint; this only arrives here if the
840 * icon needs to be repainted. We simply paint
841 * the bitmap we created in WM_SETHANDLE.
842 */
843
844 case WM_PAINT:
845 {
846 RECTL rcl;
847 HPS hps = WinBeginPaint(hwndStatic, NULLHANDLE, &rcl);
848 POINTL ptl = {0, 0};
849
850 if (pa->hbm)
851 {
852 if (WinQueryWindowULong(hwndStatic, QWL_STYLE)
853 & WS_DISABLED)
854 {
855 // if the control is currently disabled,
856 // draw in half-toned (grayed)
857
858 LONG lBkgndColor = winhQueryPresColor(
859 WinQueryWindow(hwndStatic, QW_PARENT),
860 PP_BACKGROUNDCOLOR,
861 FALSE,
862 SYSCLR_DIALOGBACKGROUND);
863
864 // 1) check if we created the half-tone
865 // bitmap already (WinDrawBitmap doesn't
866 // work with half-toned color bitmaps, so
867 // here's yet another thing we have to do
868 // all alone)
869 if (pa->hbmHalftoned == NULLHANDLE)
870 pa->hbmHalftoned = gpihCreateHalftonedBitmap(pa->hab,
871 pa->hbm,
872 lBkgndColor);
873
874 if (pa->hbmHalftoned)
875 WinDrawBitmap(hps,
876 pa->hbmHalftoned,
877 NULL, // whole bmp
878 &ptl, // lower left corner
879 0, 0, // no colors
880 DBM_NORMAL);
881 }
882 else
883 {
884 // not disabled: draw regular bitmap
885 // we created previously
886 // draw the bitmap that we created previously
887 WinDrawBitmap(hps,
888 pa->hbm,
889 NULL, // whole bmp
890 &ptl, // lower left corner
891 0, 0, // no colors
892 DBM_NORMAL);
893 }
894 }
895
896 WinEndPaint(hps);
897 break; }
898
899 /*
900 * WM_DESTROY:
901 * clean up.
902 */
903
904 case WM_DESTROY:
905 {
906 // undo subclassing in case more WM_TIMERs come in
907 WinSubclassWindow(hwndStatic, OldStaticProc);
908
909 if (pa->hbm)
910 GpiDeleteBitmap(pa->hbm);
911 if (pa->hbmHalftoned)
912 GpiDeleteBitmap(pa->hbm);
913
914 // clean up ANIMATIONDATA struct
915 WinSetWindowULong(hwndStatic, QWL_USER, (ULONG)NULL);
916 free(pa);
917
918 mrc = OldStaticProc(hwndStatic, msg, mp1, mp2);
919 break; }
920
921 default:
922 mrc = OldStaticProc(hwndStatic, msg, mp1, mp2);
923 }
924 }
925 return (mrc);
926}
927
928/* ******************************************************************
929 *
930 * Icon animation
931 *
932 ********************************************************************/
933
934/*
935 *@@ ctlPrepareStaticIcon:
936 * turns a static control into one which properly
937 * displays transparent icons when given a
938 * SM_SETHANDLE msg.
939 * This is done by subclassing the static control
940 * with ctl_fnwpBitmapStatic.
941 *
942 * This function gets called automatically by
943 * ctlPrepareAnimation, so you need not call it
944 * independently for animations. See the notes there
945 * how this can be done.
946 *
947 * You can, however, call this function if you
948 * have some static control with transparent icons
949 * which is not animated but changes sometimes
950 * (because you have the same repaint problems there).
951 *
952 * This func does _not_ start an animation yet.
953 *
954 * To change the icon being displayed, send the control
955 * a SM_SETHANDLE msg with the icon handle in (HPOINTER)mp1.
956 *
957 * Returns a PANIMATIONDATA if subclassing succeeded or
958 * NULL upon errors. If you only call this function, you
959 * do not need this structure; it is needed by
960 * ctlPrepareAnimation though.
961 *
962 * The subclassed static icon func above automatically
963 * cleans up resources, so you don't have to worry about
964 * that either.
965 *
966 *@@changed V0.9.0 [umoeller]: adjusted for ctl_fnwpBitmapStatic changes
967 */
968
969PANIMATIONDATA ctlPrepareStaticIcon(HWND hwndStatic,
970 USHORT usAnimCount) // needed for allocating extra memory,
971 // this must be at least 1
972{
973 PANIMATIONDATA pa = NULL;
974 PFNWP OldStaticProc = WinSubclassWindow(hwndStatic, ctl_fnwpBitmapStatic);
975 if (OldStaticProc)
976 {
977 // create the ANIMATIONDATA structure,
978 // initialize some fields,
979 // and store it in QWL_USER of the static control
980 ULONG cbStruct = sizeof(ANIMATIONDATA)
981 + ((usAnimCount-1) * sizeof(HPOINTER));
982 pa = (PANIMATIONDATA)malloc(cbStruct);
983 memset(pa, 0, cbStruct);
984
985 // switch static to icon mode
986 pa->ulFlags = ANF_ICON;
987 pa->OldStaticProc = OldStaticProc;
988 WinQueryWindowRect(hwndStatic, &(pa->rclIcon));
989 pa->hab = WinQueryAnchorBlock(hwndStatic);
990
991 WinSetWindowULong(hwndStatic, QWL_USER, (ULONG)pa);
992 }
993 return (pa);
994}
995
996/*
997 *@@ ctlPrepareAnimation:
998 * this function turns a regular static icon
999 * control an animated one. This is the one-shot
1000 * function for animating static icon controls.
1001 *
1002 * It calls ctlPrepareStaticIcon first, then uses
1003 * the passed parameters to prepare an animation:
1004 *
1005 * pahptr must point to an array of HPOINTERs
1006 * which must contain usAnimCount icon handles.
1007 * If (fStartAnimation == TRUE), the animation
1008 * is started already. Otherwise you'll have
1009 * to call ctlStartAnimation yourself.
1010 *
1011 * To create an animated icon, simply create a static
1012 * control with the dialog editor (can be any static,
1013 * e.g. a text control). Then do the following in your code:
1014 *
1015 * 1) load the dlg box template (using WinLoadDlg);
1016 *
1017 * 2) load all the icons for the animation in an array of
1018 * HPOINTERs (check xsdLoadAnimation in shutdown.c for an
1019 * example);
1020 *
1021 * 3) call ctlPrepareAnimation, e.g.:
1022 + ctlPrepareAnimation(WinWindowFromID(hwndDlg, ID_ICON), // get icon hwnd
1023 + 8, // no. of icons for the anim
1024 + &ahptr[0], // ptr to first icon in the array
1025 + 150, // delay
1026 + TRUE); // start animation now
1027 *
1028 * 4) call WinProcessDlg(hwndDlg);
1029 *
1030 * 5) stop the animation, e.g.:
1031 + ctlStopAnimation(WinWindowFromID(hwndDlg, ID_ICON));
1032 *
1033 * 6) destroy the dlg window;
1034 *
1035 * 7) free all the HPOINTERS loaded above (check xsdFreeAnimation
1036 * in shutdown.c for an example).
1037 *
1038 * When the icon control is destroyed, the
1039 * subclassed window proc (ctl_fnwpBitmapStatic)
1040 * automatically cleans up resources. However,
1041 * the icons in *pahptr are not freed.
1042 */
1043
1044BOOL ctlPrepareAnimation(HWND hwndStatic, // icon hwnd
1045 USHORT usAnimCount, // no. of anim steps
1046 HPOINTER *pahptr, // array of HPOINTERs
1047 ULONG ulDelay, // delay per anim step (in ms)
1048 BOOL fStartAnimation) // TRUE: start animation now
1049{
1050 PANIMATIONDATA paNew = ctlPrepareStaticIcon(hwndStatic, usAnimCount);
1051 if (paNew)
1052 {
1053 paNew->ulDelay = ulDelay;
1054 // paNew->OldStaticProc already set
1055 paNew->hptr = NULLHANDLE;
1056 // paNew->rclIcon already set
1057 paNew->usAniCurrent = 0;
1058 paNew->usAniCount = usAnimCount;
1059 memcpy(&(paNew->ahptrAniIcons), pahptr,
1060 (usAnimCount * sizeof(HPOINTER)));
1061
1062 if (fStartAnimation) {
1063 WinStartTimer(WinQueryAnchorBlock(hwndStatic), hwndStatic,
1064 1, ulDelay);
1065 WinPostMsg(hwndStatic, WM_TIMER, NULL, NULL);
1066 }
1067 }
1068 return (paNew != NULL);
1069}
1070
1071/*
1072 *@@ ctlStartAnimation:
1073 * starts an animation that not currently running. You
1074 * must prepare the animation by calling ctlPrepareAnimation
1075 * first.
1076 */
1077
1078BOOL ctlStartAnimation(HWND hwndStatic)
1079{
1080 BOOL brc = FALSE;
1081 PANIMATIONDATA pa = (PANIMATIONDATA)WinQueryWindowULong(hwndStatic, QWL_USER);
1082 if (pa)
1083 if (WinStartTimer(WinQueryAnchorBlock(hwndStatic), hwndStatic,
1084 1, pa->ulDelay))
1085 {
1086 brc = TRUE;
1087 WinPostMsg(hwndStatic, WM_TIMER, NULL, NULL);
1088 }
1089 return (brc);
1090}
1091
1092/*
1093 *@@ ctlStopAnimation:
1094 * stops an animation that is currently running.
1095 * This does not free any resources.
1096 */
1097
1098BOOL ctlStopAnimation(HWND hwndStatic)
1099{
1100 return (WinStopTimer(WinQueryAnchorBlock(hwndStatic), hwndStatic, 1));
1101}
1102
1103/*
1104 *@@category: Helpers\PM helpers\Window classes\Stretched bitmaps
1105 * See comctl.c and ctlPrepareStretchedBitmap.
1106 */
1107
1108/* ******************************************************************
1109 *
1110 * Bitmap functions
1111 *
1112 ********************************************************************/
1113
1114/*
1115 *@@ ctlPrepareStretchedBitmap:
1116 * turns a static control into a bitmap control
1117 * which stretches the bitmap to the size of the
1118 * control when given an SM_SETHANDLE msg.
1119 *
1120 * If (fPreserveProportions == TRUE), the control
1121 * will size the bitmap proportionally only. Otherwise,
1122 * it will always stretch the bitmap to the whole
1123 * size of the static control (possibly distorting
1124 * the proportions). See gpihStretchBitmap for
1125 * details.
1126 *
1127 * This function subclasses the static control
1128 * with ctl_fnwpBitmapStatic, setting the flags as
1129 * necessary. However, this does not set the bitmap
1130 * to display yet.
1131 * To have a bitmap displayed, send the control
1132 * a SM_SETHANDLE message with the bitmap to be
1133 * displayed in (HBITMAP)mp1 after having used this
1134 * function.
1135 *
1136 * ctl_fnwpBitmapStatic will then create a private
1137 * copy of that bitmap, stretched to its own size.
1138 * You can therefore delete the "source" bitmap
1139 * after sending SM_SETHANDLE.
1140 *
1141 * You can also send SM_SETHANDLE several times.
1142 * The control will then readjust itself and delete
1143 * its old bitmap.
1144 * But note that GpiWCBitBlt is invoked on that new
1145 * bitmap with every new message, so this is not
1146 * terribly fast.
1147 *
1148 * Also note that you should not resize the static
1149 * control after creation, because the bitmap will
1150 * _not_ be adjusted.
1151 *
1152 * This returns a PANIMATIONDATA if subclassing succeeded or
1153 * NULL upon errors. You do not need to save this structure;
1154 * it is cleaned up automatically when the control is destroyed.
1155 *
1156 *@@added V0.9.0 [umoeller]
1157 */
1158
1159PANIMATIONDATA ctlPrepareStretchedBitmap(HWND hwndStatic,
1160 BOOL fPreserveProportions)
1161{
1162 PANIMATIONDATA pa = NULL;
1163 PFNWP OldStaticProc = WinSubclassWindow(hwndStatic, ctl_fnwpBitmapStatic);
1164 if (OldStaticProc)
1165 {
1166 // create the ANIMATIONDATA structure,
1167 // initialize some fields,
1168 // and store it in QWL_USER of the static control
1169 ULONG cbStruct = sizeof(ANIMATIONDATA);
1170 pa = (PANIMATIONDATA)malloc(cbStruct);
1171 memset(pa, 0, cbStruct);
1172
1173 // switch static to bitmap mode
1174 pa->ulFlags = ANF_BITMAP;
1175 if (fPreserveProportions)
1176 pa->ulFlags |= ANF_PROPORTIONAL;
1177
1178 pa->OldStaticProc = OldStaticProc;
1179 WinQueryWindowRect(hwndStatic, &(pa->rclIcon));
1180 pa->hab = WinQueryAnchorBlock(hwndStatic);
1181
1182 WinSetWindowULong(hwndStatic, QWL_USER, (ULONG)pa);
1183 }
1184 return (pa);
1185}
1186
1187/*
1188 *@@category: Helpers\PM helpers\Window classes\Hotkey entry field
1189 * See comctl.c and ctl_fnwpObjectHotkeyEntryField.
1190 */
1191
1192/* ******************************************************************
1193 *
1194 * Hotkey entry field
1195 *
1196 ********************************************************************/
1197
1198/*
1199 *@@ ctl_fnwpHotkeyEntryField:
1200 * this is the window proc for a subclassed entry
1201 * field which displays a description of a keyboard
1202 * combination upon receiving WM_CHAR.
1203 *
1204 * Use ctlMakeHotkeyEntryField to turn any existing
1205 * entry field into such an "hotkey entry field".
1206 *
1207 * The entry field is deleted on every WM_CHAR that
1208 * comes in.
1209 *
1210 * For this to work, this window proc needs the
1211 * cooperation of its owner. Whenever a WM_CHAR
1212 * message is received by the entry field which
1213 * looks like a meaningful key or key combination
1214 * (some filtering is done by this func), then
1215 * the owner of the entry field receives a WM_CONTROL
1216 * message with the new EN_HOTKEY notification code
1217 * (see comctl.h for details).
1218 *
1219 * It is then the responsibility of the owner to
1220 * check whether the HOTKEYNOTIFY structure pointed
1221 * to by WM_CONTROL's mp2 is a meaningful keyboard
1222 * combination.
1223 *
1224 * If not, the owner must return FALSE, and the
1225 * entry field's contents are deleted.
1226 *
1227 * If yes however, the owner must fill the szDescription
1228 * field with a description of the keyboard combination
1229 * (e.g. "Shift+Alt+C") and return TRUE. The entry field
1230 * will then be set to that description.
1231 *
1232 *@@added V0.9.1 (99-12-19) [umoeller]
1233 */
1234
1235MRESULT EXPENTRY ctl_fnwpObjectHotkeyEntryField(HWND hwndEdit, ULONG msg, MPARAM mp1, MPARAM mp2)
1236{
1237 MRESULT mrc = (MPARAM)FALSE; // WM_CHAR not-processed flag
1238
1239 // get object window data; this was stored in QWL_USER by
1240 // WM_INITDLG of obj_fnwpSettingsObjDetails
1241 // PXFOBJWINDATA pWinData = (PXFOBJWINDATA)WinQueryWindowULong(hwndEdit, QWL_USER);
1242 PFNWP pfnwpOrig = (PFNWP)WinQueryWindowULong(hwndEdit, QWL_USER);
1243
1244 switch (msg)
1245 {
1246 case WM_CHAR:
1247 {
1248 /*
1249 #define KC_CHAR 0x0001
1250 #define KC_VIRTUALKEY 0x0002
1251 #define KC_SCANCODE 0x0004
1252 #define KC_SHIFT 0x0008
1253 #define KC_CTRL 0x0010
1254 #define KC_ALT 0x0020
1255 #define KC_KEYUP 0x0040
1256 #define KC_PREVDOWN 0x0080
1257 #define KC_LONEKEY 0x0100
1258 #define KC_DEADKEY 0x0200
1259 #define KC_COMPOSITE 0x0400
1260 #define KC_INVALIDCOMP 0x0800
1261 */
1262
1263 /*
1264 Examples: usFlags usKeyCode
1265 F3 02 22
1266 Ctrl+F4 12 23
1267 Ctrl+Shift+F4 1a 23
1268 Ctrl 12 0a
1269 Alt 22 0b
1270 Shift 0a 09
1271 */
1272
1273 // USHORT usCommand;
1274 // USHORT usKeyCode;
1275 USHORT usFlags = SHORT1FROMMP(mp1);
1276 // UCHAR ucRepeat = CHAR3FROMMP(mp1);
1277 UCHAR ucScanCode = CHAR4FROMMP(mp1);
1278 USHORT usch = SHORT1FROMMP(mp2);
1279 USHORT usvk = SHORT2FROMMP(mp2);
1280
1281 if (
1282 // process only key-down messages
1283 ((usFlags & KC_KEYUP) == 0)
1284 // check flags:
1285 // filter out those ugly composite key (accents etc.)
1286 && ((usFlags & (KC_DEADKEY | KC_COMPOSITE | KC_INVALIDCOMP)) == 0)
1287 )
1288 {
1289 HOTKEYNOTIFY hkn;
1290 ULONG flReturned;
1291 HWND hwndOwner = WinQueryWindow(hwndEdit, QW_OWNER);
1292
1293 usFlags &= (KC_VIRTUALKEY | KC_CTRL | KC_ALT | KC_SHIFT);
1294
1295 hkn.usFlags = usFlags;
1296 hkn.usvk = usvk;
1297 hkn.usch = usch;
1298 hkn.ucScanCode = ucScanCode;
1299
1300 if (usFlags & KC_VIRTUALKEY)
1301 hkn.usKeyCode = usvk;
1302 else
1303 hkn.usKeyCode = usch;
1304
1305 hkn.szDescription[0] = 0;
1306
1307 flReturned = (ULONG)WinSendMsg(hwndOwner,
1308 WM_CONTROL,
1309 MPFROM2SHORT(WinQueryWindowUShort(hwndEdit,
1310 QWS_ID),
1311 EN_HOTKEY), // new notification code
1312 (MPARAM)&hkn);
1313 if (flReturned & HEFL_SETTEXT)
1314 WinSetWindowText(hwndEdit, hkn.szDescription);
1315 else
1316 WinSetWindowText(hwndEdit, "");
1317
1318 if (flReturned & HEFL_FORWARD2OWNER)
1319 WinPostMsg(hwndOwner,
1320 WM_CHAR,
1321 mp1, mp2);
1322
1323 mrc = (MPARAM)TRUE; // WM_CHAR processed flag;
1324 }
1325 break; }
1326
1327 default:
1328 mrc = pfnwpOrig(hwndEdit, msg, mp1, mp2);
1329 }
1330 return (mrc);
1331}
1332
1333/*
1334 *@@ ctlMakeHotkeyEntryField:
1335 * this turns any entry field into a "hotkey entry field"
1336 * by subclassing it with ctl_fnwpObjectHotkeyEntryField.
1337 * See remarks there for details.
1338 *
1339 * Returns FALSE upon errors, TRUE otherwise.
1340 *
1341 *@@added V0.9.1 (99-12-19) [umoeller]
1342 */
1343
1344BOOL ctlMakeHotkeyEntryField(HWND hwndHotkeyEntryField)
1345{
1346 PFNWP pfnwpOrig = WinSubclassWindow(hwndHotkeyEntryField,
1347 ctl_fnwpObjectHotkeyEntryField);
1348 if (pfnwpOrig)
1349 {
1350 WinSetWindowPtr(hwndHotkeyEntryField, QWL_USER, (PVOID)pfnwpOrig);
1351 return (TRUE);
1352 }
1353
1354 return (FALSE);
1355}
1356
Note: See TracBrowser for help on using the repository browser.