]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
23 #include "wx/bitmap.h"
24 #include "wx/dcmemory.h"
31 #include "wx/ownerdrw.h"
32 #include "wx/menuitem.h"
35 // ============================================================================
36 // implementation of wxOwnerDrawn class
37 // ============================================================================
41 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
42 bool bCheckable
, bool bMenuItem
)
45 m_bCheckable
= bCheckable
;
46 m_bOwnerDrawn
= FALSE
;
48 m_nMarginWidth
= ms_nLastMarginWidth
;
51 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
53 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
58 // get size of the item
59 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
62 dc
.SetFont(GetFont());
65 wxChar
*szStripped
= new wxChar
[m_strName
.Len()];
66 wxStripMenuCodes((wxChar
*)m_strName
.c_str(), szStripped
);
67 wxString str
= szStripped
;
70 // # without this menu items look too tightly packed (at least under Windows)
71 str
+= wxT('W'); // 'W' is typically the widest letter
73 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
75 // JACS: items still look too tightly packed, so adding 2 pixels.
76 (*pheight
) = (*pheight
) + 2;
78 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
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)
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
92 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
94 ///////////////////////////////////////////////////////////////////////////////////////////////////
95 // Might want to check the native drawing apis for here and doo something like MSW does for WIN95
96 ///////////////////////////////////////////////////////////////////////////////////////////////////
98 // we do nothing on focus change
99 if ( act
== wxODFocusChanged
)
103 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
104 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
108 DWORD colBack
, colText
;
111 if ( st & wxODSelected ) {
112 colBack = GetSysColor(COLOR_HIGHLIGHT);
113 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
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);
121 // dc.SetTextForeground(wxColor(UnRGB(colText)));
122 // dc.SetTextBackground(wxColor(UnRGB(colBack)));
124 // select the font and draw the text
125 // ---------------------------------
127 // determine where to draw and leave space for a check-mark.
128 int x
= rc
.x
+ GetMarginWidth();
130 dc
.SetFont(GetFont());
131 dc
.DrawText(m_strName
, x
, rc
.y
);
135 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
136 if ( st
& wxODChecked
) {
137 // using native APIs for performance and simplicity
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 };
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);
154 // for uncheckable item we use only the 'checked' bitmap
155 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
157 wxMemoryDC
dcMem(&dc
);
158 dcMem
.SelectObject(bmp
);
161 int nBmpWidth
= bmp
.GetWidth(),
162 nBmpHeight
= bmp
.GetHeight();
164 // there should be enough place!
165 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
167 //MT: blit with mask enabled.
170 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
171 rc.y + (m_nHeight - nBmpHeight) /2,
172 nBmpWidth, nBmpHeight,
173 &dcMem, 0, 0, wxCOPY,true);
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);
187 #ifdef O_DRAW_NATIVE_API
188 ::SetTextColor(hdc, colOldText);
189 ::SetBkColor(hdc, colOldBack);
192 #endif //O_DRAW_NATIVE_API
197 #endif //wxUSE_OWNER_DRAWN