]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
Got generic wxListCtrl, wxTreeCtrl working under Windows, wxNotebook almost;
[wxWidgets.git] / src / msw / ownerdrw.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/ownerdrw.cpp
3// Purpose: implementation of wxOwnerDrawn class
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 13.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
a3b46648
UU
12#ifdef __GNUG__
13#pragma implementation
14#endif
2bda0e17
KB
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
a3b46648 20#pragma hdrstop
2bda0e17
KB
21#endif
22
23#ifndef WX_PRECOMP
24 #include "wx/menu.h"
25#endif
26
27#include "wx/ownerdrw.h"
28#include "wx/menuitem.h"
29
30#include <windows.h>
31
32// ============================================================================
33// implementation of wxOwnerDrawn class
34// ============================================================================
35
36// ctor
37// ----
3aadbb82 38wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
2bda0e17
KB
39 bool bCheckable, bool bMenuItem)
40 : m_strName(str)
41{
42 m_bCheckable = bCheckable;
43 m_bOwnerDrawn = FALSE;
44 m_nHeight = 0;
45 m_nMarginWidth = ms_nLastMarginWidth;
46}
47
2049ba38 48#if defined(__WXMSW__) && defined(__WIN32__)
c86f1403 49 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = GetSystemMetrics(SM_CXMENUCHECK);
2bda0e17 50#else // # what is the reasonable default?
c86f1403 51 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15;
2bda0e17
KB
52#endif
53
c86f1403 54size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth;
2bda0e17
KB
55
56// drawing
57// -------
58
59// get size of the item
c86f1403 60bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17
KB
61{
62 wxMemoryDC dc;
63 dc.SetFont(GetFont());
64
65 // ## ugly...
66 char *szStripped = new char[m_strName.Len()];
67 wxStripMenuCodes((char *)m_strName.c_str(), szStripped);
68 wxString str = szStripped;
69 delete [] szStripped;
70
71 // # without this menu items look too tightly packed (at least under Windows)
72 str += 'W'; // 'W' is typically the widest letter
73
74 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
75 m_nHeight = *pheight; // remember height for use in OnDrawItem
76
77 return TRUE;
78}
79
80// searching for this macro you'll find all the code where I'm using the native
81// Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
82// port this code to X. (VZ)
83
84#ifdef __WIN32__
85#define O_DRAW_NATIVE_API // comments below explain why I use it
86#endif
87
88// draw the item
89bool wxOwnerDrawn::OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus st)
90{
91 // we do nothing on focus change
92 if ( act == wxODFocusChanged )
93 return TRUE;
94
95 // wxColor <-> RGB
96 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
97 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
98
99 // set the colors
100 // --------------
101 DWORD colBack, colText;
102 if ( st & wxODSelected ) {
103 colBack = GetSysColor(COLOR_HIGHLIGHT);
104 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
105 }
106 else {
107 // fall back to default colors if none explicitly specified
108 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
109 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
110 }
111
112 #ifdef O_DRAW_NATIVE_API
113 #define hdc (HDC)dc.GetHDC()
114 COLORREF colOldText = ::SetTextColor(hdc, colText),
115 colOldBack = ::SetBkColor(hdc, colBack);
116 #else
117 dc.SetTextForeground(wxColor(UnRGB(colText)));
118 dc.SetTextBackground(wxColor(UnRGB(colBack)));
119 #endif
120
121 // select the font and draw the text
122 // ---------------------------------
123
124 // determine where to draw and leave space for a check-mark.
125 int x = rc.x + GetMarginWidth();
126
127 // using native API because it reckognizes '&'
128 #ifdef O_DRAW_NATIVE_API
129 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
130 HBRUSH hbr = CreateSolidBrush(colBack),
c4e7c2aa 131 hPrevBrush = (HBRUSH) SelectObject(hdc, hbr);
2bda0e17
KB
132
133 RECT rectAll = { rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom() };
134 FillRect(hdc, &rectAll, hbr);
135
136 // use default font if no font set
137 HFONT hfont;
138 if ( m_font.Ok() ) {
139 m_font.RealizeResource();
140 hfont = (HFONT)m_font.GetResourceHandle();
141 }
142 else {
143 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
144 }
145
c4e7c2aa 146 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
2bda0e17
KB
147 DrawState(hdc, NULL, NULL,
148 (LPARAM)(const char *)m_strName, m_strName.Length(),
149 x, rc.y, rc.GetWidth(), rc.GetHeight(),
150 DST_PREFIXTEXT | ( st & wxODDisabled ? DSS_DISABLED : 0) );
151
152 (void)SelectObject(hdc, hPrevBrush);
153 (void)SelectObject(hdc, hPrevFont);
154 (void)SetBkMode(hdc, nPrevMode);
155 #else
156 dc.SetFont(GetFont());
157 dc.DrawText(m_strName, x, rc.y);
158 #endif //O_DRAW_NATIVE_API
159
160 // draw the bitmap
161 // ---------------
162 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
163 if ( st & wxODChecked ) {
164 // using native APIs for performance and simplicity
5260b1c5 165#ifdef O_DRAW_NATIVE_API
2bda0e17
KB
166 // what goes on: DrawFrameControl creates a b/w mask,
167 // then we copy it to screen to have right colors
168
169 // first create a monochrome bitmap in a memory DC
170 HDC hdcMem = CreateCompatibleDC(hdc);
171 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
172 SelectObject(hdcMem, hbmpCheck);
173
174 // then draw a check mark into it
175 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
176 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
177
178 // finally copy it to screen DC and clean up
179 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
180 hdcMem, 0, 0, SRCCOPY);
181 DeleteDC(hdcMem);
5260b1c5 182#else
2bda0e17 183 // #### to do: perhaps using Marlett font (create equiv. font under X)
5260b1c5
JS
184// wxFAIL("not implemented");
185#endif //O_DRAW_NATIVE_API
2bda0e17
KB
186 }
187 }
188 else {
189 // for uncheckable item we use only the 'checked' bitmap
190 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
191 if ( bmp.Ok() ) {
192 wxMemoryDC dcMem(&dc);
193 dcMem.SelectObject(bmp);
194
195 // center bitmap
196 int nBmpWidth = bmp.GetWidth(),
197 nBmpHeight = bmp.GetHeight();
198
199 // there should be enough place!
200 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
201
202 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
203 rc.y + (m_nHeight - nBmpHeight) /2,
204 nBmpWidth, nBmpHeight,
205 &dcMem, 0, 0, wxCOPY);
206
207 if ( st & wxODSelected ) {
208 #ifdef O_DRAW_NATIVE_API
209 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
210 rc.GetLeft() + GetMarginWidth(),
211 rc.GetTop() + m_nHeight };
212 SetBkColor(hdc, colBack);
213 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
214 #else
215 // ## to write portable DrawEdge
216 #endif //O_DRAW_NATIVE_API
217 }
218 }
219 }
220
221 #ifdef O_DRAW_NATIVE_API
222 ::SetTextColor(hdc, colOldText);
223 ::SetBkColor(hdc, colOldBack);
224
225 #undef hdc
226 #endif //O_DRAW_NATIVE_API
227
228 return TRUE;
229}
230