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