Disabling a Window (Rectangle)

From EDM2
Jump to: navigation, search

Written by Roman Stangl

The problem

If you create your own window controls, you can of course logically disable your controls with the OS/2 PM API WinDisableWindow(), however it would be nice if you would draw your controls similar to standard OS/2 controls. For example, the right-scrollbutton may be drawn differently, as shown in Figure 1.a.

  • Enabled style: Styleenabled.gif
  • Disabled style: Styledisabled.gif

Figure 1.a: Window Styles.

The following function DisableWindowRect() is an example of how you can draw your control in a disabled style.

A DisableWindowRect() function

As your control would draw into a rectangle inside a presentation space, you pass to DisableWindowRect() your control's rectangle to disable the whole control, or a subrectangle to just disable a part of your window. The latter is useful when you draw a window that seems to consist of multiple controls, but to save resources (e.g. window handles) you draw all that controls into the single window's presentation space.

Now, let's concentrate on the actual implementation of DisableWindowRect() which is shown in Figure 2.a.

 /*------------------------------------------------------------------------
  * This procedure disables a rectangle by drawing a halftoned background
  * colored box into the rectangle.
  * Req:
  *      hpsDestination  The presentation space to blit icon into
  *      prectlDestination
  *                      The target rectangle to draw halftoned box into
  *      ulBackgroundColor
  *                      The background color to be used
  * Ret:
  *      ulRc .......... Return code: NO_ERROR no error, PMERR_* otherwise
  * Ref:
 \*------------------------------------------------------------------------
 ULONG DisableWindowRect(HPS hpsDestination,
                         RECTL *prectlDestination,
                         ULONG ulBackgroundColor)
 {
   GpiSetPattern(hpsDestination, PATSYM_HALFTONE);
   GpiSetColor(hpsDestination, ulBackgroundColor);
   GpiMove(hpsDestination, (POINTL *)(&prectlDestination->xLeft));
   GpiBox(hpsDestination, DRO_OUTLINEFILL,
          (POINTL *)(&prectlDestination->xRight), 0, 0);
   GpiSetPattern(hpsDestination, PATSYM_SOLID);
   return(NO_ERROR);
 }

Figure 2.a: DisableWindowRect().

You may notice the casting. As a RECTANGLE structure consists of 2 POINTL structures, the first for the lower left and the second for the upper right edge of the rectangle, you can save some temporary variables by applying above casts.

Figure 2.b shows how you would invoke DisableWindowRect(). The sample invocation passes the presentation space your control will be drawn into, the rectangle you want to draw in a disabled style, and the background color active in your presentation space.

DisableWindowRect(pHP->hpsSession, prectlScrollLeft, SYSCLR_DIALOGBACKGROUND);

Figure 2.b: DisableWindowRect call.

The rectangle will be filled with a halftoned box of the background color, that is every odd pixel will be reset to the background color.

Credits

Some ideas to solve that problem have been published in the OS/2 development fora on the IBMPC conference disk.