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

Last change on this file since 111 was 111, 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: 49.1 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 *@@changed V0.9.16 (2001-10-15) [umoeller]: now centering icon in static properly
680 *@@changed V0.9.16 (2001-10-15) [umoeller]: this always used the presparam colors of the parent instead of its own ones
681 */
682
683MRESULT EXPENTRY ctl_fnwpBitmapStatic(HWND hwndStatic, ULONG msg, MPARAM mp1, MPARAM mp2)
684{
685 PANIMATIONDATA pa = (PANIMATIONDATA)WinQueryWindowULong(hwndStatic, QWL_USER);
686 // animation data which was stored in window words
687
688 PFNWP OldStaticProc = NULL;
689 MRESULT mrc = NULL;
690
691 if (pa)
692 {
693 OldStaticProc = pa->OldStaticProc;
694
695 switch(msg)
696 {
697 /*
698 * WM_TIMER:
699 * this timer is started by the ctl* funcs
700 * below. Proceed to the next animation step.
701 *
702 * Note: this timer is only used with
703 * ANF_ICON, and even then, it
704 * is not necessarily running.
705 */
706
707 case WM_TIMER:
708 {
709 pa->usAniCurrent++;
710 if (pa->usAniCurrent >= pa->usAniCount)
711 pa->usAniCurrent = 0;
712
713 WinSendMsg(hwndStatic,
714 SM_SETHANDLE,
715 (MPARAM)pa->ahptrAniIcons[pa->usAniCurrent],
716 (MPARAM)NULL);
717 break; }
718
719 /*
720 * SM_SETHANDLE:
721 *
722 */
723
724 case SM_SETHANDLE:
725 {
726 HDC hdcMem;
727 HPS hpsMem;
728 SIZEL szlPage;
729
730 LONG lBkgndColor
731 /* = winhQueryPresColor(WinQueryWindow(hwndStatic, QW_PARENT),
732 PP_BACKGROUNDCOLOR,
733 FALSE,
734 SYSCLR_DIALOGBACKGROUND); */
735 // fixed this... V0.9.16 (2001-10-15) [umoeller]
736 = winhQueryPresColor(hwndStatic,
737 PP_BACKGROUNDCOLOR,
738 TRUE,
739 SYSCLR_DIALOGBACKGROUND);
740
741 HPS hps = WinGetPS(hwndStatic);
742
743 // if we have created bitmaps previously, delete them
744 if (pa->hbm)
745 {
746 GpiDeleteBitmap(pa->hbm);
747 pa->hbm = NULLHANDLE;
748 }
749 if (pa->hbmHalftoned)
750 {
751 GpiDeleteBitmap(pa->hbmHalftoned);
752 pa->hbmHalftoned = NULLHANDLE;
753 }
754
755 // switch to RGB mode
756 gpihSwitchToRGB(hps);
757
758 // create a memory PS
759 szlPage.cx = pa->rclIcon.xRight - pa->rclIcon.xLeft;
760 szlPage.cy = pa->rclIcon.yTop - pa->rclIcon.yBottom;
761 if (gpihCreateMemPS(pa->hab,
762 &szlPage,
763 &hdcMem,
764 &hpsMem))
765 {
766 // switch the memory PS to RGB mode too
767 gpihSwitchToRGB(hpsMem);
768
769 // create a suitable bitmap w/ the size of the
770 // static control
771 if ((pa->hbm = gpihCreateBitmap(hpsMem,
772 szlPage.cx,
773 szlPage.cy)))
774 {
775 // associate the bit map with the memory PS
776 if (GpiSetBitmap(hpsMem, pa->hbm) != HBM_ERROR)
777 {
778 // fill the bitmap with the current static
779 // background color
780 POINTL ptl = {0, 0};
781 GpiMove(hpsMem, &ptl);
782 ptl.x = pa->rclIcon.xRight;
783 ptl.y = pa->rclIcon.yTop;
784 GpiSetColor(hpsMem,
785 lBkgndColor);
786 GpiBox(hpsMem,
787 DRO_FILL, // interior only
788 &ptl,
789 0, 0); // no corner rounding
790
791 /*
792 * ANF_ICON:
793 *
794 */
795
796 if (pa->ulFlags & ANF_ICON)
797 {
798 // store new icon in our own structure
799 pa->hptr = (HPOINTER)mp1;
800
801 if (pa->hptr)
802 {
803 // center the icon in the bitmap
804 // V0.9.16 (2001-10-15) [umoeller]
805 POINTL ptlOfs;
806 ptlOfs.x = ( (pa->rclIcon.xRight - pa->rclIcon.xLeft)
807 - pa->lIconSize
808 ) / 2;
809 ptlOfs.y = ( (pa->rclIcon.yTop - pa->rclIcon.yBottom)
810 - pa->lIconSize
811 ) / 2;
812
813 // paint icon into bitmap
814 gpihIcon2Bitmap(hpsMem,
815 pa->hptr,
816 lBkgndColor,
817 &ptlOfs,
818 pa->lIconSize);
819 }
820
821 } // end if (pa->ulFlags & ANF_ICON)
822
823 /*
824 * ANF_BITMAP:
825 *
826 */
827
828 else if (pa->ulFlags & ANF_BITMAP)
829 {
830 // store passed bitmap
831 HBITMAP hbmSource = (HBITMAP)mp1;
832
833 if (hbmSource)
834 gpihStretchBitmap(hpsMem, // target
835 hbmSource, // source
836 NULL, // use size of bitmap
837 &pa->rclIcon,
838 ((pa->ulFlags & ANF_PROPORTIONAL)
839 != 0));
840
841 } // end if (pa->ulFlags & ANF_BITMAP)
842
843 } // end if (GpiSetBitmap(...
844 } // if (pa->hbm = gpihCreateBitmap(hpsMem, &(pa->rclIcon)))
845
846 // in any case, clean up now
847 GpiDestroyPS(hpsMem);
848 DevCloseDC(hdcMem);
849 } // end if (gpihCreateMemPS(hab, &hdcMem, &hpsMem))
850
851 WinReleasePS(hps);
852
853 // enforce WM_PAINT
854 WinInvalidateRect(hwndStatic, NULL, FALSE);
855
856 break; }
857
858 /*
859 * WM_PAINT:
860 * "normal" paint; this only arrives here if the
861 * icon needs to be repainted. We simply paint
862 * the bitmap we created in WM_SETHANDLE.
863 */
864
865 case WM_PAINT:
866 {
867 RECTL rcl;
868 HPS hps = WinBeginPaint(hwndStatic, NULLHANDLE, &rcl);
869 POINTL ptl = {0, 0};
870
871 if (pa->hbm)
872 {
873 if (WinQueryWindowULong(hwndStatic, QWL_STYLE)
874 & WS_DISABLED)
875 {
876 // if the control is currently disabled,
877 // draw in half-toned (grayed)
878
879 LONG lBkgndColor = winhQueryPresColor(
880 WinQueryWindow(hwndStatic, QW_PARENT),
881 PP_BACKGROUNDCOLOR,
882 FALSE,
883 SYSCLR_DIALOGBACKGROUND);
884
885 // 1) check if we created the half-tone
886 // bitmap already (WinDrawBitmap doesn't
887 // work with half-toned color bitmaps, so
888 // here's yet another thing we have to do
889 // all alone)
890 if (pa->hbmHalftoned == NULLHANDLE)
891 pa->hbmHalftoned = gpihCreateHalftonedBitmap(pa->hab,
892 pa->hbm,
893 lBkgndColor);
894
895 if (pa->hbmHalftoned)
896 WinDrawBitmap(hps,
897 pa->hbmHalftoned,
898 NULL, // whole bmp
899 &ptl, // lower left corner
900 0, 0, // no colors
901 DBM_NORMAL);
902 }
903 else
904 {
905 // not disabled: draw regular bitmap
906 // we created previously
907 // draw the bitmap that we created previously
908 WinDrawBitmap(hps,
909 pa->hbm,
910 NULL, // whole bmp
911 &ptl, // lower left corner
912 0, 0, // no colors
913 DBM_NORMAL);
914 }
915 }
916
917 WinEndPaint(hps);
918 break; }
919
920 /*
921 * WM_DESTROY:
922 * clean up.
923 */
924
925 case WM_DESTROY:
926 {
927 // undo subclassing in case more WM_TIMERs come in
928 WinSubclassWindow(hwndStatic, OldStaticProc);
929
930 if (pa->hbm)
931 GpiDeleteBitmap(pa->hbm);
932 if (pa->hbmHalftoned)
933 GpiDeleteBitmap(pa->hbm);
934
935 // clean up ANIMATIONDATA struct
936 WinSetWindowULong(hwndStatic, QWL_USER, (ULONG)NULL);
937 free(pa);
938
939 mrc = OldStaticProc(hwndStatic, msg, mp1, mp2);
940 break; }
941
942 default:
943 mrc = OldStaticProc(hwndStatic, msg, mp1, mp2);
944 }
945 }
946 return (mrc);
947}
948
949/* ******************************************************************
950 *
951 * Icon animation
952 *
953 ********************************************************************/
954
955/*
956 *@@ CreateAnimationData:
957 *
958 *@@added V0.9.16 (2001-10-15) [umoeller]
959 */
960
961PANIMATIONDATA CreateAnimationData(HWND hwndStatic,
962 USHORT cAnimations)
963{
964 PANIMATIONDATA pa = NULL;
965
966 if (cAnimations >= 1)
967 {
968 // create the ANIMATIONDATA structure,
969 // initialize some fields,
970 // and store it in QWL_USER of the static control
971 ULONG cbStruct = sizeof(ANIMATIONDATA)
972 + ((cAnimations - 1) * sizeof(HPOINTER));
973
974 if (pa = (PANIMATIONDATA)malloc(cbStruct))
975 {
976 memset(pa, 0, cbStruct);
977
978 WinSetWindowULong(hwndStatic, QWL_USER, (ULONG)pa);
979
980 pa->hab = WinQueryAnchorBlock(hwndStatic);
981 WinQueryWindowRect(hwndStatic, &pa->rclIcon);
982 pa->OldStaticProc = WinSubclassWindow(hwndStatic, ctl_fnwpBitmapStatic);
983 pa->lIconSize = WinQuerySysValue(HWND_DESKTOP, SV_CXICON);
984 }
985 }
986
987 return (pa);
988}
989
990/*
991 *@@ ctlPrepareStaticIcon:
992 * turns a static control into one which properly
993 * displays transparent icons when given a
994 * SM_SETHANDLE msg.
995 * This is done by subclassing the static control
996 * with ctl_fnwpBitmapStatic.
997 *
998 * This function gets called automatically by
999 * ctlPrepareAnimation, so you need not call it
1000 * independently for animations. See the notes there
1001 * how this can be done.
1002 *
1003 * You can, however, call this function if you
1004 * have some static control with transparent icons
1005 * which is not animated but changes sometimes
1006 * (because you have the same repaint problems there).
1007 *
1008 * This func does _not_ start an animation yet.
1009 *
1010 * To change the icon being displayed, send the control
1011 * a SM_SETHANDLE msg with the icon handle in (HPOINTER)mp1.
1012 *
1013 * Returns a PANIMATIONDATA if subclassing succeeded or
1014 * NULL upon errors. If you only call this function, you
1015 * do not need this structure; it is needed by
1016 * ctlPrepareAnimation though.
1017 *
1018 * The subclassed static icon func above automatically
1019 * cleans up resources, so you don't have to worry about
1020 * that either.
1021 *
1022 *@@changed V0.9.0 [umoeller]: adjusted for ctl_fnwpBitmapStatic changes
1023 */
1024
1025PANIMATIONDATA ctlPrepareStaticIcon(HWND hwndStatic,
1026 USHORT usAnimCount) // needed for allocating extra memory,
1027 // this must be at least 1
1028{
1029 PANIMATIONDATA pa;
1030
1031 if (pa = CreateAnimationData(hwndStatic,
1032 usAnimCount))
1033 {
1034 // switch static to icon mode
1035 pa->ulFlags = ANF_ICON;
1036 }
1037
1038 return (pa);
1039}
1040
1041/*
1042 *@@ ctlPrepareAnimation:
1043 * this function turns a regular static icon
1044 * control an animated one. This is the one-shot
1045 * function for animating static icon controls.
1046 *
1047 * It calls ctlPrepareStaticIcon first, then uses
1048 * the passed parameters to prepare an animation:
1049 *
1050 * pahptr must point to an array of HPOINTERs
1051 * which must contain usAnimCount icon handles.
1052 * If (fStartAnimation == TRUE), the animation
1053 * is started already. Otherwise you'll have
1054 * to call ctlStartAnimation yourself.
1055 *
1056 * To create an animated icon, simply create a static
1057 * control with the dialog editor (can be any static,
1058 * e.g. a text control). Then do the following in your code:
1059 *
1060 * 1) load the dlg box template (using WinLoadDlg);
1061 *
1062 * 2) load all the icons for the animation in an array of
1063 * HPOINTERs (check xsdLoadAnimation in shutdown.c for an
1064 * example);
1065 *
1066 * 3) call ctlPrepareAnimation, e.g.:
1067 + ctlPrepareAnimation(WinWindowFromID(hwndDlg, ID_ICON), // get icon hwnd
1068 + 8, // no. of icons for the anim
1069 + &ahptr[0], // ptr to first icon in the array
1070 + 150, // delay
1071 + TRUE); // start animation now
1072 *
1073 * 4) call WinProcessDlg(hwndDlg);
1074 *
1075 * 5) stop the animation, e.g.:
1076 + ctlStopAnimation(WinWindowFromID(hwndDlg, ID_ICON));
1077 *
1078 * 6) destroy the dlg window;
1079 *
1080 * 7) free all the HPOINTERS loaded above (check xsdFreeAnimation
1081 * in shutdown.c for an example).
1082 *
1083 * When the icon control is destroyed, the
1084 * subclassed window proc (ctl_fnwpBitmapStatic)
1085 * automatically cleans up resources. However,
1086 * the icons in *pahptr are not freed.
1087 */
1088
1089BOOL ctlPrepareAnimation(HWND hwndStatic, // icon hwnd
1090 USHORT usAnimCount, // no. of anim steps
1091 HPOINTER *pahptr, // array of HPOINTERs
1092 ULONG ulDelay, // delay per anim step (in ms)
1093 BOOL fStartAnimation) // TRUE: start animation now
1094{
1095 PANIMATIONDATA paNew = ctlPrepareStaticIcon(hwndStatic, usAnimCount);
1096 if (paNew)
1097 {
1098 paNew->ulDelay = ulDelay;
1099 // paNew->OldStaticProc already set
1100 paNew->hptr = NULLHANDLE;
1101 // paNew->rclIcon already set
1102 paNew->usAniCurrent = 0;
1103 paNew->usAniCount = usAnimCount;
1104 memcpy(&paNew->ahptrAniIcons,
1105 pahptr,
1106 (usAnimCount * sizeof(HPOINTER)));
1107
1108 if (fStartAnimation)
1109 {
1110 WinStartTimer(WinQueryAnchorBlock(hwndStatic), hwndStatic,
1111 1, ulDelay);
1112 WinPostMsg(hwndStatic, WM_TIMER, NULL, NULL);
1113 }
1114 }
1115 return (paNew != NULL);
1116}
1117
1118/*
1119 *@@ ctlStartAnimation:
1120 * starts an animation that not currently running. You
1121 * must prepare the animation by calling ctlPrepareAnimation
1122 * first.
1123 */
1124
1125BOOL ctlStartAnimation(HWND hwndStatic)
1126{
1127 BOOL brc = FALSE;
1128 PANIMATIONDATA pa = (PANIMATIONDATA)WinQueryWindowULong(hwndStatic, QWL_USER);
1129 if (pa)
1130 if (WinStartTimer(WinQueryAnchorBlock(hwndStatic), hwndStatic,
1131 1, pa->ulDelay))
1132 {
1133 brc = TRUE;
1134 WinPostMsg(hwndStatic, WM_TIMER, NULL, NULL);
1135 }
1136 return (brc);
1137}
1138
1139/*
1140 *@@ ctlStopAnimation:
1141 * stops an animation that is currently running.
1142 * This does not free any resources.
1143 */
1144
1145BOOL ctlStopAnimation(HWND hwndStatic)
1146{
1147 return (WinStopTimer(WinQueryAnchorBlock(hwndStatic), hwndStatic, 1));
1148}
1149
1150/*
1151 *@@category: Helpers\PM helpers\Window classes\Stretched bitmaps
1152 * See comctl.c and ctlPrepareStretchedBitmap.
1153 */
1154
1155/* ******************************************************************
1156 *
1157 * Bitmap functions
1158 *
1159 ********************************************************************/
1160
1161/*
1162 *@@ ctlPrepareStretchedBitmap:
1163 * turns a static control into a bitmap control
1164 * which stretches the bitmap to the size of the
1165 * control when given an SM_SETHANDLE msg.
1166 *
1167 * If (fPreserveProportions == TRUE), the control
1168 * will size the bitmap proportionally only. Otherwise,
1169 * it will always stretch the bitmap to the whole
1170 * size of the static control (possibly distorting
1171 * the proportions). See gpihStretchBitmap for
1172 * details.
1173 *
1174 * This function subclasses the static control
1175 * with ctl_fnwpBitmapStatic, setting the flags as
1176 * necessary. However, this does not set the bitmap
1177 * to display yet.
1178 * To have a bitmap displayed, send the control
1179 * a SM_SETHANDLE message with the bitmap to be
1180 * displayed in (HBITMAP)mp1 after having used this
1181 * function.
1182 *
1183 * ctl_fnwpBitmapStatic will then create a private
1184 * copy of that bitmap, stretched to its own size.
1185 * You can therefore delete the "source" bitmap
1186 * after sending SM_SETHANDLE.
1187 *
1188 * You can also send SM_SETHANDLE several times.
1189 * The control will then readjust itself and delete
1190 * its old bitmap.
1191 * But note that GpiWCBitBlt is invoked on that new
1192 * bitmap with every new message, so this is not
1193 * terribly fast.
1194 *
1195 * Also note that you should not resize the static
1196 * control after creation, because the bitmap will
1197 * _not_ be adjusted.
1198 *
1199 * This returns a PANIMATIONDATA if subclassing succeeded or
1200 * NULL upon errors. You do not need to save this structure;
1201 * it is cleaned up automatically when the control is destroyed.
1202 *
1203 *@@added V0.9.0 [umoeller]
1204 *@@changed V0.9.16 (2001-10-15) [umoeller]: some cleanup
1205 */
1206
1207PANIMATIONDATA ctlPrepareStretchedBitmap(HWND hwndStatic,
1208 BOOL fPreserveProportions)
1209{
1210 PANIMATIONDATA pa;
1211
1212 if (pa = CreateAnimationData(hwndStatic, 1))
1213 {
1214 // switch static to bitmap mode
1215 pa->ulFlags = ANF_BITMAP;
1216 if (fPreserveProportions)
1217 pa->ulFlags |= ANF_PROPORTIONAL;
1218 }
1219
1220 return (pa);
1221}
1222
1223/*
1224 *@@category: Helpers\PM helpers\Window classes\Hotkey entry field
1225 * See comctl.c and ctl_fnwpObjectHotkeyEntryField.
1226 */
1227
1228/* ******************************************************************
1229 *
1230 * Hotkey entry field
1231 *
1232 ********************************************************************/
1233
1234/*
1235 *@@ ctl_fnwpHotkeyEntryField:
1236 * this is the window proc for a subclassed entry
1237 * field which displays a description of a keyboard
1238 * combination upon receiving WM_CHAR.
1239 *
1240 * Use ctlMakeHotkeyEntryField to turn any existing
1241 * entry field into such an "hotkey entry field".
1242 *
1243 * The entry field is deleted on every WM_CHAR that
1244 * comes in.
1245 *
1246 * For this to work, this window proc needs the
1247 * cooperation of its owner. Whenever a WM_CHAR
1248 * message is received by the entry field which
1249 * looks like a meaningful key or key combination
1250 * (some filtering is done by this func), then
1251 * the owner of the entry field receives a WM_CONTROL
1252 * message with the new EN_HOTKEY notification code
1253 * (see comctl.h for details).
1254 *
1255 * It is then the responsibility of the owner to
1256 * check whether the HOTKEYNOTIFY structure pointed
1257 * to by WM_CONTROL's mp2 is a meaningful keyboard
1258 * combination.
1259 *
1260 * If not, the owner must return FALSE, and the
1261 * entry field's contents are deleted.
1262 *
1263 * If yes however, the owner must fill the szDescription
1264 * field with a description of the keyboard combination
1265 * (e.g. "Shift+Alt+C") and return TRUE. The entry field
1266 * will then be set to that description.
1267 *
1268 *@@added V0.9.1 (99-12-19) [umoeller]
1269 */
1270
1271MRESULT EXPENTRY ctl_fnwpObjectHotkeyEntryField(HWND hwndEdit, ULONG msg, MPARAM mp1, MPARAM mp2)
1272{
1273 MRESULT mrc = (MPARAM)FALSE; // WM_CHAR not-processed flag
1274
1275 // get object window data; this was stored in QWL_USER by
1276 // WM_INITDLG of obj_fnwpSettingsObjDetails
1277 // PXFOBJWINDATA pWinData = (PXFOBJWINDATA)WinQueryWindowULong(hwndEdit, QWL_USER);
1278 PFNWP pfnwpOrig = (PFNWP)WinQueryWindowULong(hwndEdit, QWL_USER);
1279
1280 switch (msg)
1281 {
1282 case WM_CHAR:
1283 {
1284 /*
1285 #define KC_CHAR 0x0001
1286 #define KC_VIRTUALKEY 0x0002
1287 #define KC_SCANCODE 0x0004
1288 #define KC_SHIFT 0x0008
1289 #define KC_CTRL 0x0010
1290 #define KC_ALT 0x0020
1291 #define KC_KEYUP 0x0040
1292 #define KC_PREVDOWN 0x0080
1293 #define KC_LONEKEY 0x0100
1294 #define KC_DEADKEY 0x0200
1295 #define KC_COMPOSITE 0x0400
1296 #define KC_INVALIDCOMP 0x0800
1297 */
1298
1299 /*
1300 Examples: usFlags usKeyCode
1301 F3 02 22
1302 Ctrl+F4 12 23
1303 Ctrl+Shift+F4 1a 23
1304 Ctrl 12 0a
1305 Alt 22 0b
1306 Shift 0a 09
1307 */
1308
1309 // USHORT usCommand;
1310 // USHORT usKeyCode;
1311 USHORT usFlags = SHORT1FROMMP(mp1);
1312 // UCHAR ucRepeat = CHAR3FROMMP(mp1);
1313 UCHAR ucScanCode = CHAR4FROMMP(mp1);
1314 USHORT usch = SHORT1FROMMP(mp2);
1315 USHORT usvk = SHORT2FROMMP(mp2);
1316
1317 if (
1318 // process only key-down messages
1319 ((usFlags & KC_KEYUP) == 0)
1320 // check flags:
1321 // filter out those ugly composite key (accents etc.)
1322 && ((usFlags & (KC_DEADKEY | KC_COMPOSITE | KC_INVALIDCOMP)) == 0)
1323 )
1324 {
1325 HOTKEYNOTIFY hkn;
1326 ULONG flReturned;
1327 HWND hwndOwner = WinQueryWindow(hwndEdit, QW_OWNER);
1328
1329 usFlags &= (KC_VIRTUALKEY | KC_CTRL | KC_ALT | KC_SHIFT);
1330
1331 hkn.usFlags = usFlags;
1332 hkn.usvk = usvk;
1333 hkn.usch = usch;
1334 hkn.ucScanCode = ucScanCode;
1335
1336 if (usFlags & KC_VIRTUALKEY)
1337 hkn.usKeyCode = usvk;
1338 else
1339 hkn.usKeyCode = usch;
1340
1341 hkn.szDescription[0] = 0;
1342
1343 flReturned = (ULONG)WinSendMsg(hwndOwner,
1344 WM_CONTROL,
1345 MPFROM2SHORT(WinQueryWindowUShort(hwndEdit,
1346 QWS_ID),
1347 EN_HOTKEY), // new notification code
1348 (MPARAM)&hkn);
1349 if (flReturned & HEFL_SETTEXT)
1350 WinSetWindowText(hwndEdit, hkn.szDescription);
1351 else
1352 WinSetWindowText(hwndEdit, "");
1353
1354 if (flReturned & HEFL_FORWARD2OWNER)
1355 WinPostMsg(hwndOwner,
1356 WM_CHAR,
1357 mp1, mp2);
1358
1359 mrc = (MPARAM)TRUE; // WM_CHAR processed flag;
1360 }
1361 break; }
1362
1363 default:
1364 mrc = pfnwpOrig(hwndEdit, msg, mp1, mp2);
1365 }
1366 return (mrc);
1367}
1368
1369/*
1370 *@@ ctlMakeHotkeyEntryField:
1371 * this turns any entry field into a "hotkey entry field"
1372 * by subclassing it with ctl_fnwpObjectHotkeyEntryField.
1373 * See remarks there for details.
1374 *
1375 * Returns FALSE upon errors, TRUE otherwise.
1376 *
1377 *@@added V0.9.1 (99-12-19) [umoeller]
1378 */
1379
1380BOOL ctlMakeHotkeyEntryField(HWND hwndHotkeyEntryField)
1381{
1382 PFNWP pfnwpOrig = WinSubclassWindow(hwndHotkeyEntryField,
1383 ctl_fnwpObjectHotkeyEntryField);
1384 if (pfnwpOrig)
1385 {
1386 WinSetWindowPtr(hwndHotkeyEntryField, QWL_USER, (PVOID)pfnwpOrig);
1387 return (TRUE);
1388 }
1389
1390 return (FALSE);
1391}
1392
Note: See TracBrowser for help on using the repository browser.