1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/msw/ownerdrw.cpp
3 // Purpose:     implementation of wxOwnerDrawn class
4 // Author:      Vadim Zeitlin
5 // Modified by: Marcin Malich
6 // Created:     13.11.97
7 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence:     wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13 
14 #ifdef __BORLANDC__
15     #pragma hdrstop
16 #endif
17 
18 #if wxUSE_OWNER_DRAWN
19 
20 #include "wx/ownerdrw.h"
21 #include "wx/msw/dc.h"
22 #include "wx/msw/private.h"
23 #include "wx/msw/private/dc.h"
24 #include "wx/msw/wrapcctl.h"            // for HIMAGELIST
25 
26 // ----------------------------------------------------------------------------
27 // constants for base class
28 // ----------------------------------------------------------------------------
29 
30 int wxOwnerDrawnBase::ms_defaultMargin = 3;
31 
32 // ============================================================================
33 // implementation of wxOwnerDrawn class
34 // ============================================================================
35 
36 // draw the item
OnDrawItem(wxDC & dc,const wxRect & rc,wxODAction,wxODStatus stat)37 bool wxOwnerDrawn::OnDrawItem(wxDC& dc, const wxRect& rc,
38                               wxODAction, wxODStatus stat)
39 {
40     // we do nothing if item isn't ownerdrawn
41     if ( !IsOwnerDrawn() )
42         return true;
43 
44     wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
45     HDC hdc = GetHdcOf(*impl);
46 
47     RECT rect;
48     wxCopyRectToRECT(rc, rect);
49 
50     {
51         // set the font and colors
52         wxFont font;
53         GetFontToUse(font);
54 
55         wxColour colText, colBack;
56         GetColourToUse(stat, colText, colBack);
57 
58         SelectInHDC selFont(hdc, GetHfontOf(font));
59 
60         wxMSWImpl::wxTextColoursChanger textCol(hdc, colText, colBack);
61         wxMSWImpl::wxBkModeChanger bkMode(hdc, wxBRUSHSTYLE_TRANSPARENT);
62 
63 
64         AutoHBRUSH hbr(wxColourToPalRGB(colBack));
65         SelectInHDC selBrush(hdc, hbr);
66 
67         ::FillRect(hdc, &rect, hbr);
68 
69         // using native API because it recognizes '&'
70 
71         wxString text = GetName();
72 
73         SIZE sizeRect;
74         ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &sizeRect);
75 
76         int flags = DST_PREFIXTEXT;
77         if ( (stat & wxODDisabled) && !(stat & wxODSelected) )
78             flags |= DSS_DISABLED;
79 
80         if ( (stat & wxODHidePrefix) )
81             flags |= DSS_HIDEPREFIX;
82 
83         int x = rc.x + GetMarginWidth();
84         int y = rc.y + (rc.GetHeight() - sizeRect.cy) / 2;
85         int cx = rc.GetWidth() - GetMarginWidth();
86         int cy = sizeRect.cy;
87 
88         ::DrawState(hdc, NULL, NULL, wxMSW_CONV_LPARAM(text),
89                     text.length(), x, y, cx, cy, flags);
90 
91     } // reset to default the font, colors and brush
92 
93     if (stat & wxODHasFocus)
94         ::DrawFocusRect(hdc, &rect);
95 
96     return true;
97 }
98 
99 // ----------------------------------------------------------------------------
100 // global helper functions implemented here
101 // ----------------------------------------------------------------------------
102 
wxDrawStateBitmap(HDC hDC,HBITMAP hBitmap,int x,int y,UINT uState)103 BOOL wxDrawStateBitmap(HDC hDC, HBITMAP hBitmap, int x, int y, UINT uState)
104 {
105     // determine size of bitmap image
106     BITMAP bmp;
107     if ( !::GetObject(hBitmap, sizeof(BITMAP), &bmp) )
108         return FALSE;
109 
110     BOOL result;
111 
112     switch ( uState )
113     {
114         case wxDSB_NORMAL:
115         case wxDSB_SELECTED:
116             {
117                 // uses image list functions to draw
118                 //  - normal bitmap with support transparency
119                 //    (image list internally create mask etc.)
120                 //  - blend bitmap with the background colour
121                 //    (like default selected items)
122                 HIMAGELIST hIml = ::ImageList_Create(bmp.bmWidth, bmp.bmHeight,
123                                                      ILC_COLOR32 | ILC_MASK, 1, 1);
124                 ::ImageList_Add(hIml, hBitmap, NULL);
125                 UINT fStyle = uState == wxDSB_SELECTED ? ILD_SELECTED : ILD_NORMAL;
126                 result = ::ImageList_Draw(hIml, 0, hDC, x, y, fStyle);
127                 ::ImageList_Destroy(hIml);
128             }
129             break;
130 
131         case wxDSB_DISABLED:
132             result = ::DrawState(hDC, NULL, NULL, (LPARAM)hBitmap, 0, x, y,
133                                  bmp.bmWidth, bmp.bmHeight,
134                                  DST_BITMAP | DSS_DISABLED);
135             break;
136 
137         default:
138             wxFAIL_MSG( wxT("DrawStateBitmap: unknown wxDSBStates value") );
139             result = FALSE;
140     }
141 
142     return result;
143 }
144 
145 #endif // wxUSE_OWNER_DRAWN
146