Refactor owner-drawing code.
[wxWidgets.git] / src / os2 / ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: David Webster
5 // Modified by: Marcin Malich
6 // Created: 10/12/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_OWNER_DRAWN
16
17 #include "wx/ownerdrw.h"
18 #include "wx/os2/private.h"
19 #include "wx/os2/dcclient.h"
20
21 // ----------------------------------------------------------------------------
22 // constants for base class
23 // ----------------------------------------------------------------------------
24
25 int wxOwnerDrawnBase::ms_defaultMargin = 3;
26
27 // ============================================================================
28 // implementation of wxOwnerDrawn class
29 // ============================================================================
30
31 // draw the item
32 bool wxOwnerDrawn::OnDrawItem( wxDC& rDC,
33 const wxRect& rRect,
34 wxODAction eAction,
35 wxODStatus eStatus )
36 {
37
38 //
39 // Select the font and draw the text
40 // ---------------------------------
41 //
42
43 CHARBUNDLE vCbnd;
44 wxPMDCImpl *impl = (wxPMDCImpl*) rDC.GetImpl();
45 HPS hPS= impl->GetHPS();
46 wxFont vFont;
47 wxColour vColBack;
48 wxColour vColText;
49 COLORREF vRef;
50 RECTL vRect = {rRect.x + 4, rRect.y + 1, rRect.x + (rRect.width - 2), rRect.y + rRect.height};
51
52 memset(&vCbnd, 0, sizeof(CHARBUNDLE));
53
54 GetFontToUse(vFont);
55 GetColourToUse(eStatus, vColText, vColBack);
56
57 rDC.SetFont(vFont);
58 rDC.SetTextBackground(vColBack);
59 rDC.SetTextForeground(vColText);
60 rDC.SetBackgroundMode(wxTRANSPARENT);
61
62 vCbnd.lColor = vColText.GetPixel();
63 vCbnd.lBackColor = vColBack.GetPixel();
64 ::GpiSetAttrs( hPS
65 ,PRIM_CHAR
66 ,CBB_BACK_COLOR | CBB_COLOR
67 ,0
68 ,&vCbnd
69 );
70 ::GpiSetBackMix( hPS
71 ,BM_LEAVEALONE
72 );
73
74 //
75 // Paint the background
76 //
77 ::WinFillRect(hPS, &vRect, vColBack.GetPixel());
78
79 //
80 // Determine where to draw and leave space for a check-mark.
81 //
82 int nX = rRect.x + GetMarginWidth();
83
84 //
85 // Unfortunately, unlike Win32, PM has no owner drawn specific text
86 // drawing methods like ::DrawState that can cleanly handle accel
87 // mnemonics and deal, automatically, with various states, so we have
88 // to handle them ourselves. Notice Win32 can't handle \t in ownerdrawn
89 // strings either. We cannot handle mnemonics either. We display
90 // them, though, in the hope we can figure them out some day.
91 //
92
93 //
94 // Display main text
95 //
96 wxString sFullString = GetItemLabel(); // need to save the original text
97 int nIndex;
98 size_t nWidth;
99 size_t nCharWidth;
100 size_t nHeight;
101 bool bFoundMnemonic = false;
102
103 //
104 // Deal with the mnemonic character
105 //
106 nIndex = sFullString.Find(wxT("~"));
107 if (nIndex != -1)
108 {
109 wxString sTmp = sFullString;
110
111 bFoundMnemonic = true;
112 sTmp.Remove(nIndex);
113 rDC.GetTextExtent( sTmp
114 ,(wxCoord *)&nWidth
115 ,(wxCoord *)&nHeight
116 );
117 sTmp = sFullString[(size_t)(nIndex + 1)];
118 rDC.GetTextExtent( sTmp
119 ,(wxCoord *)&nCharWidth
120 ,(wxCoord *)&nHeight
121 );
122 sFullString.Replace(sTgt.c_str(), wxEmptyString, true);
123 }
124
125 //
126 // Draw the main item text sans the accel text
127 //
128 POINTL vPntStart = {nX, rRect.y + 4};
129 ::GpiCharStringAt( impl->GetHPS()
130 ,&vPntStart
131 ,sFullString.length()
132 ,sFullString.char_str()
133 );
134 if (bFoundMnemonic)
135 {
136 //
137 // Underline the mnemonic -- still won't work, but at least it "looks" right
138 //
139 wxPen vPen;
140 POINTL vPntEnd = {nX + nWidth + nCharWidth - 3, rRect.y + 2}; //CharWidth is bit wide
141
142 vPntStart.x = nX + nWidth - 1;
143 vPntStart.y = rRect.y + 2; // Make it look pretty!
144 vPen = wxPen(vColText, 1, wxSOLID); // Assuming we are always black
145 rDC.SetPen(vPen);
146 ::GpiMove(hPS, &vPntStart);
147 ::GpiLine(hPS, &vPntEnd);
148 }
149
150 return true;
151 } // end of wxOwnerDrawn::OnDrawItem
152
153 #endif //wxUSE_OWNER_DRAWN