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

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

Tons of changes from the last weeks.

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