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

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

Major updates; timers, LVM, miscellaneous.

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