no message
[wxWidgets.git] / src / os2 / ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/12/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/window.h"
17 #include "wx/msw/private.h"
18 #include "wx/font.h"
19 #include "wx/bitmap.h"
20 #include "wx/dcmemory.h"
21 #include "wx/menu.h"
22 #include "wx/utils.h"
23 #endif
24
25 #include "wx/ownerdrw.h"
26 #include "wx/menuitem.h"
27
28
29 // ============================================================================
30 // implementation of wxOwnerDrawn class
31 // ============================================================================
32
33 // ctor
34 // ----
35 wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
36 bool bCheckable, bool bMenuItem)
37 : m_strName(str)
38 {
39 m_bCheckable = bCheckable;
40 m_bOwnerDrawn = FALSE;
41 m_nHeight = 0;
42 m_nMarginWidth = ms_nLastMarginWidth;
43 }
44
45 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15;
46
47 size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth;
48
49 // drawing
50 // -------
51
52 // get size of the item
53 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
54 {
55 wxMemoryDC dc;
56 dc.SetFont(GetFont());
57
58 // ## ugly...
59 wxChar *szStripped = new wxChar[m_strName.Len()];
60 wxStripMenuCodes((wxChar *)m_strName.c_str(), szStripped);
61 wxString str = szStripped;
62 delete [] szStripped;
63
64 // # without this menu items look too tightly packed (at least under Windows)
65 str += wxT('W'); // 'W' is typically the widest letter
66
67 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
68
69 // JACS: items still look too tightly packed, so adding 2 pixels.
70 (*pheight) = (*pheight) + 2;
71
72 m_nHeight = *pheight; // remember height for use in OnDrawItem
73
74 return TRUE;
75 }
76
77 // searching for this macro you'll find all the code where I'm using the native
78 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
79 // port this code to X. (VZ)
80
81 // JACS: TODO. Why does a disabled but highlighted item still
82 // get drawn embossed? How can we tell DrawState that we don't want the
83 // embossing?
84
85 // draw the item
86 bool wxOwnerDrawn::OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus st)
87 {
88 ///////////////////////////////////////////////////////////////////////////////////////////////////
89 // Might want to check the native drawing apis for here and doo something like MSW does for WIN95
90 ///////////////////////////////////////////////////////////////////////////////////////////////////
91
92 // we do nothing on focus change
93 if ( act == wxODFocusChanged )
94 return TRUE;
95
96 // wxColor <-> RGB
97 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
98 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
99
100 // set the colors
101 // --------------
102 DWORD colBack, colText;
103 if ( st & wxODSelected ) {
104 colBack = GetSysColor(COLOR_HIGHLIGHT);
105 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
106 }
107 else {
108 // fall back to default colors if none explicitly specified
109 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
110 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
111 }
112
113 dc.SetTextForeground(wxColor(UnRGB(colText)));
114 dc.SetTextBackground(wxColor(UnRGB(colBack)));
115
116 // select the font and draw the text
117 // ---------------------------------
118
119 // determine where to draw and leave space for a check-mark.
120 int x = rc.x + GetMarginWidth();
121
122 dc.SetFont(GetFont());
123 dc.DrawText(m_strName, x, rc.y);
124
125 // draw the bitmap
126 // ---------------
127 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
128 if ( st & wxODChecked ) {
129 // using native APIs for performance and simplicity
130 // TODO:
131 /*
132 HDC hdcMem = CreateCompatibleDC(hdc);
133 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
134 SelectObject(hdcMem, hbmpCheck);
135 // then draw a check mark into it
136 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
137
138 // finally copy it to screen DC and clean up
139 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
140 hdcMem, 0, 0, SRCCOPY);
141 DeleteDC(hdcMem);
142 */
143 #else
144 // #### to do: perhaps using Marlett font (create equiv. font under X)
145 // wxFAIL("not implemented");
146 #endif //O_DRAW_NATIVE_API
147 }
148 }
149 else {
150 // for uncheckable item we use only the 'checked' bitmap
151 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
152 if ( bmp.Ok() ) {
153 wxMemoryDC dcMem(&dc);
154 dcMem.SelectObject(bmp);
155
156 // center bitmap
157 int nBmpWidth = bmp.GetWidth(),
158 nBmpHeight = bmp.GetHeight();
159
160 // there should be enough place!
161 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
162
163 //MT: blit with mask enabled.
164 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
165 rc.y + (m_nHeight - nBmpHeight) /2,
166 nBmpWidth, nBmpHeight,
167 &dcMem, 0, 0, wxCOPY,true);
168
169 if ( st & wxODSelected ) {
170 // TODO:
171 /*
172 #ifdef O_DRAW_NATIVE_API
173 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
174 rc.GetLeft() + GetMarginWidth(),
175 rc.GetTop() + m_nHeight };
176 SetBkColor(hdc, colBack);
177 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
178 */
179 }
180 }
181 }
182 /*
183 #ifdef O_DRAW_NATIVE_API
184 ::SetTextColor(hdc, colOldText);
185 ::SetBkColor(hdc, colOldBack);
186
187 #undef hdc
188 #endif //O_DRAW_NATIVE_API
189 */
190 return TRUE;
191 }
192