]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | /////////////////////////////////////////////////////////////////////////////// |
e4db172a | 2 | // Name: src/msw/menuitem.cpp |
2bda0e17 KB |
3 | // Purpose: wxMenuItem implementation |
4 | // Author: Vadim Zeitlin | |
c2dcfdef | 5 | // Modified by: |
2bda0e17 KB |
6 | // Created: 11.11.97 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
c2dcfdef VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
c2dcfdef | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_MENUS |
28 | ||
3b3dc801 | 29 | #include "wx/menuitem.h" |
ee0a94cf | 30 | #include "wx/stockitem.h" |
e4db172a | 31 | |
2bda0e17 | 32 | #ifndef WX_PRECOMP |
9d043a92 VZ |
33 | #include "wx/app.h" |
34 | #include "wx/dcmemory.h" | |
c2dcfdef VZ |
35 | #include "wx/font.h" |
36 | #include "wx/bitmap.h" | |
37 | #include "wx/settings.h" | |
4e938f5b | 38 | #include "wx/window.h" |
0c589ad0 | 39 | #include "wx/accel.h" |
0c589ad0 | 40 | #include "wx/string.h" |
e4db172a | 41 | #include "wx/log.h" |
3b3dc801 | 42 | #include "wx/menu.h" |
2bda0e17 KB |
43 | #endif |
44 | ||
717a57c2 VZ |
45 | #if wxUSE_ACCEL |
46 | #include "wx/accel.h" | |
47 | #endif // wxUSE_ACCEL | |
48 | ||
42e69d6b | 49 | #include "wx/msw/private.h" |
98fbab9e | 50 | #include "wx/msw/dc.h" |
ce3ed50d | 51 | |
4676948b JS |
52 | #ifdef __WXWINCE__ |
53 | // Implemented in menu.cpp | |
54 | UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ; | |
55 | #endif | |
56 | ||
aa4919ed VZ |
57 | #if wxUSE_UXTHEME |
58 | #include "wx/msw/uxtheme.h" | |
59 | #endif | |
60 | ||
c2dcfdef | 61 | // --------------------------------------------------------------------------- |
974e8d94 | 62 | // macro |
c2dcfdef VZ |
63 | // --------------------------------------------------------------------------- |
64 | ||
974e8d94 | 65 | // hide the ugly cast |
c2dcfdef VZ |
66 | #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu()) |
67 | ||
2bda0e17 KB |
68 | // ============================================================================ |
69 | // implementation | |
70 | // ============================================================================ | |
71 | ||
98fbab9e VZ |
72 | #if wxUSE_OWNER_DRAWN |
73 | ||
74 | #include "wx/fontutil.h" | |
75 | #include "wx/msw/private/metrics.h" | |
76 | ||
77 | #ifndef SPI_GETKEYBOARDCUES | |
78 | #define SPI_GETKEYBOARDCUES 0x100A | |
79 | #endif | |
80 | ||
81 | #ifndef DSS_HIDEPREFIX | |
82 | #define DSS_HIDEPREFIX 0x0200 | |
83 | #endif | |
84 | ||
aa4919ed VZ |
85 | #if wxUSE_UXTHEME |
86 | ||
87 | enum MENUPARTS | |
88 | { | |
89 | MENU_MENUITEM_TMSCHEMA = 1, | |
90 | MENU_SEPARATOR_TMSCHEMA = 6, | |
91 | MENU_POPUPBACKGROUND = 9, | |
92 | MENU_POPUPBORDERS = 10, | |
93 | MENU_POPUPCHECK = 11, | |
94 | MENU_POPUPCHECKBACKGROUND = 12, | |
95 | MENU_POPUPGUTTER = 13, | |
96 | MENU_POPUPITEM = 14, | |
97 | MENU_POPUPSEPARATOR = 15, | |
98 | MENU_POPUPSUBMENU = 16, | |
99 | }; | |
100 | ||
101 | ||
102 | enum POPUPITEMSTATES | |
103 | { | |
104 | MPI_NORMAL = 1, | |
105 | MPI_HOT = 2, | |
106 | MPI_DISABLED = 3, | |
107 | MPI_DISABLEDHOT = 4, | |
108 | }; | |
109 | ||
110 | enum POPUPCHECKBACKGROUNDSTATES | |
111 | { | |
112 | MCB_DISABLED = 1, | |
113 | MCB_NORMAL = 2, | |
114 | MCB_BITMAP = 3, | |
115 | }; | |
116 | ||
117 | enum POPUPCHECKSTATES | |
118 | { | |
119 | MC_CHECKMARKNORMAL = 1, | |
120 | MC_CHECKMARKDISABLED = 2, | |
121 | MC_BULLETNORMAL = 3, | |
122 | MC_BULLETDISABLED = 4, | |
123 | }; | |
124 | ||
125 | const int TMT_MENUFONT = 803; | |
126 | const int TMT_BORDERSIZE = 2403; | |
127 | const int TMT_CONTENTMARGINS = 3602; | |
128 | const int TMT_SIZINGMARGINS = 3601; | |
129 | ||
130 | #endif // wxUSE_UXTHEME | |
131 | ||
98fbab9e VZ |
132 | #endif // wxUSE_OWNER_DRAWN |
133 | ||
2bda0e17 KB |
134 | // ---------------------------------------------------------------------------- |
135 | // dynamic classes implementation | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
e70b4f10 SC |
138 | #if wxUSE_EXTENDED_RTTI |
139 | ||
140 | bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & ) | |
141 | { | |
142 | const wxMenuItem * mitem = dynamic_cast<const wxMenuItem*>(object) ; | |
3b3dc801 | 143 | if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().empty() ) |
e70b4f10 SC |
144 | { |
145 | // we don't stream out the first two items for menus with a title, they will be reconstructed | |
146 | if ( mitem->GetMenu()->FindItemByPosition(0) == mitem || mitem->GetMenu()->FindItemByPosition(1) == mitem ) | |
147 | return false ; | |
148 | } | |
149 | return true ; | |
150 | } | |
151 | ||
3ff066a4 | 152 | wxBEGIN_ENUM( wxItemKind ) |
598ddd96 WS |
153 | wxENUM_MEMBER( wxITEM_SEPARATOR ) |
154 | wxENUM_MEMBER( wxITEM_NORMAL ) | |
155 | wxENUM_MEMBER( wxITEM_CHECK ) | |
156 | wxENUM_MEMBER( wxITEM_RADIO ) | |
3ff066a4 | 157 | wxEND_ENUM( wxItemKind ) |
e70b4f10 SC |
158 | |
159 | IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem, wxObject,"wx/menuitem.h",wxMenuItemStreamingCallback) | |
160 | ||
3ff066a4 | 161 | wxBEGIN_PROPERTIES_TABLE(wxMenuItem) |
598ddd96 WS |
162 | wxPROPERTY( Parent,wxMenu*, SetMenu, GetMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
163 | wxPROPERTY( Id,int, SetId, GetId, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
3ff066a4 SC |
164 | wxPROPERTY( Text, wxString , SetText, GetText, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
165 | wxPROPERTY( Help, wxString , SetHelp, GetHelp, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
af498247 | 166 | wxREADONLY_PROPERTY( Kind, wxItemKind , GetKind , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
598ddd96 WS |
167 | wxPROPERTY( SubMenu,wxMenu*, SetSubMenu, GetSubMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
168 | wxPROPERTY( Enabled , bool , Enable , IsEnabled , wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
169 | wxPROPERTY( Checked , bool , Check , IsChecked , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
170 | wxPROPERTY( Checkable , bool , SetCheckable , IsCheckable , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
3ff066a4 SC |
171 | wxEND_PROPERTIES_TABLE() |
172 | ||
173 | wxBEGIN_HANDLERS_TABLE(wxMenuItem) | |
174 | wxEND_HANDLERS_TABLE() | |
175 | ||
176 | wxDIRECT_CONSTRUCTOR_6( wxMenuItem , wxMenu* , Parent , int , Id , wxString , Text , wxString , Help , wxItemKind , Kind , wxMenu* , SubMenu ) | |
e70b4f10 | 177 | #else |
621b3e21 | 178 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) |
e70b4f10 | 179 | #endif |
2bda0e17 KB |
180 | |
181 | // ---------------------------------------------------------------------------- | |
182 | // wxMenuItem | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
98fbab9e VZ |
185 | #if wxUSE_OWNER_DRAWN |
186 | ||
aa4919ed VZ |
187 | namespace |
188 | { | |
189 | ||
190 | // helper class to keep information about metrics and other stuff | |
191 | // needed for measuring and drawing menu item | |
192 | class MenuDrawData | |
193 | { | |
194 | public: | |
195 | ||
196 | struct Margins | |
197 | { | |
198 | int left; | |
199 | int right; | |
200 | int top; | |
201 | int bottom; | |
202 | ||
203 | Margins() | |
204 | : left(0), | |
205 | right(0), | |
206 | top(0), | |
207 | bottom(0) | |
208 | {} | |
209 | }; | |
210 | ||
211 | Margins ItemMargin; // popup item margins | |
212 | ||
213 | Margins CheckMargin; // popup check margins | |
214 | Margins CheckBgMargin; // popup check background margins | |
215 | ||
9c32ed26 VZ |
216 | Margins ArrowMargin; // popup submenu arrow margins |
217 | ||
aa4919ed VZ |
218 | Margins SeparatorMargin; // popup separator margins |
219 | ||
220 | SIZE CheckSize; // popup check size metric | |
9c32ed26 | 221 | SIZE ArrowSize; // popup submenu arrow size metric |
aa4919ed VZ |
222 | SIZE SeparatorSize; // popup separator size metric |
223 | ||
aa4919ed VZ |
224 | int TextBorder; // popup border space between |
225 | // item text and gutter | |
226 | ||
9c32ed26 VZ |
227 | int AccelBorder; // popup border space between |
228 | // item text and accelerator | |
229 | ||
230 | int ArrowBorder; // popup border space between | |
231 | // item accelerator and submenu arrow | |
232 | ||
233 | int Offset; // system added space at the end of the menu, | |
234 | // add this offset for remove the extra space | |
235 | ||
aa4919ed VZ |
236 | wxFont Font; // default menu font |
237 | ||
238 | bool AlwaysShowCues; // must keyboard cues always be shown? | |
239 | ||
240 | bool Theme; // is data initialized for FullTheme? | |
241 | ||
242 | static const MenuDrawData* Get() | |
243 | { | |
60a7194e VZ |
244 | // notice that s_menuData can't be created as a global variable because |
245 | // it needs a window to initialize and no windows exist at the time of | |
246 | // globals initialization yet | |
247 | if ( !ms_instance ) | |
248 | { | |
249 | static MenuDrawData s_menuData; | |
250 | ms_instance = &s_menuData; | |
251 | } | |
252 | ||
aa4919ed VZ |
253 | #if wxUSE_UXTHEME |
254 | bool theme = MenuLayout() == FullTheme; | |
255 | if ( ms_instance->Theme != theme ) | |
256 | ms_instance->Init(); | |
257 | #endif // wxUSE_UXTHEME | |
258 | return ms_instance; | |
259 | } | |
260 | ||
261 | MenuDrawData() | |
262 | { | |
aa4919ed VZ |
263 | Init(); |
264 | } | |
265 | ||
266 | ||
267 | // get the theme engine or NULL if themes | |
268 | // are not available or not supported on menu | |
269 | static wxUxThemeEngine *GetUxThemeEngine() | |
270 | { | |
271 | #if wxUSE_UXTHEME | |
272 | if ( MenuLayout() == FullTheme ) | |
273 | return wxUxThemeEngine::GetIfActive(); | |
274 | #endif // wxUSE_UXTHEME | |
275 | return NULL; | |
276 | } | |
277 | ||
278 | ||
279 | enum MenuLayoutType | |
280 | { | |
281 | FullTheme, // full menu themes (Vista or new) | |
282 | PseudoTheme, // pseudo menu themes (on XP) | |
283 | Classic | |
284 | }; | |
285 | ||
286 | static MenuLayoutType MenuLayout() | |
287 | { | |
288 | MenuLayoutType menu = Classic; | |
289 | #if wxUSE_UXTHEME | |
290 | if ( wxUxThemeEngine::GetIfActive() != NULL ) | |
291 | { | |
292 | static wxWinVersion ver = wxGetWinVersion(); | |
293 | if ( ver >= wxWinVersion_Vista ) | |
294 | menu = FullTheme; | |
295 | else if ( ver == wxWinVersion_XP ) | |
296 | menu = PseudoTheme; | |
297 | } | |
298 | #endif // wxUSE_UXTHEME | |
299 | return menu; | |
300 | } | |
301 | ||
302 | private: | |
303 | void Init(); | |
304 | ||
305 | static MenuDrawData* ms_instance; | |
306 | }; | |
307 | ||
308 | MenuDrawData* MenuDrawData::ms_instance = NULL; | |
309 | ||
aa4919ed VZ |
310 | void MenuDrawData::Init() |
311 | { | |
312 | #if wxUSE_UXTHEME | |
313 | wxUxThemeEngine* theme = GetUxThemeEngine(); | |
314 | if ( theme ) | |
315 | { | |
316 | wxWindow* window = static_cast<wxApp*>(wxApp::GetInstance())->GetTopWindow(); | |
317 | wxUxThemeHandle hTheme(window, L"MENU"); | |
318 | ||
319 | theme->GetThemeMargins(hTheme, NULL, MENU_POPUPITEM, 0, | |
320 | TMT_CONTENTMARGINS, NULL, | |
321 | reinterpret_cast<MARGINS*>(&ItemMargin)); | |
322 | ||
323 | theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECK, 0, | |
324 | TMT_CONTENTMARGINS, NULL, | |
325 | reinterpret_cast<MARGINS*>(&CheckMargin)); | |
326 | theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECKBACKGROUND, 0, | |
327 | TMT_CONTENTMARGINS, NULL, | |
328 | reinterpret_cast<MARGINS*>(&CheckBgMargin)); | |
329 | ||
9c32ed26 VZ |
330 | theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSUBMENU, 0, |
331 | TMT_CONTENTMARGINS, NULL, | |
332 | reinterpret_cast<MARGINS*>(&ArrowMargin)); | |
333 | ||
aa4919ed VZ |
334 | theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSEPARATOR, 0, |
335 | TMT_SIZINGMARGINS, NULL, | |
336 | reinterpret_cast<MARGINS*>(&SeparatorMargin)); | |
337 | ||
338 | theme->GetThemePartSize(hTheme, NULL, MENU_POPUPCHECK, 0, | |
339 | NULL, TS_TRUE, &CheckSize); | |
340 | ||
9c32ed26 VZ |
341 | theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSUBMENU, 0, |
342 | NULL, TS_TRUE, &ArrowSize); | |
343 | ||
aa4919ed VZ |
344 | theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSEPARATOR, 0, |
345 | NULL, TS_TRUE, &SeparatorSize); | |
346 | ||
aa4919ed VZ |
347 | theme->GetThemeInt(hTheme, MENU_POPUPBACKGROUND, 0, TMT_BORDERSIZE, &TextBorder); |
348 | ||
9c32ed26 VZ |
349 | AccelBorder = 34; |
350 | ArrowBorder = 0; | |
351 | ||
352 | Offset = -14; | |
353 | ||
aa4919ed VZ |
354 | wxNativeFontInfo fontInfo; |
355 | theme->GetThemeSysFont(hTheme, TMT_MENUFONT, &fontInfo.lf); | |
356 | Font = wxFont(fontInfo); | |
357 | ||
358 | Theme = true; | |
359 | ||
360 | // native menu doesn't uses the vertical margins | |
361 | ItemMargin.top = ItemMargin.bottom = 0; | |
362 | ||
363 | // native menu uses small top margin for separator | |
364 | if ( SeparatorMargin.top >= 2 ) | |
365 | SeparatorMargin.top -= 2; | |
366 | } | |
367 | else | |
368 | #endif // wxUSE_UXTHEME | |
369 | { | |
370 | const NONCLIENTMETRICS& metrics = wxMSWImpl::GetNonClientMetrics(); | |
371 | ||
372 | ItemMargin = Margins(); | |
373 | ||
374 | CheckMargin.left = | |
375 | CheckMargin.right = ::GetSystemMetrics(SM_CXEDGE); | |
376 | CheckMargin.top = | |
377 | CheckMargin.bottom = ::GetSystemMetrics(SM_CYEDGE); | |
378 | ||
379 | CheckBgMargin = Margins(); | |
380 | ||
381 | CheckSize.cx = ::GetSystemMetrics(SM_CXMENUCHECK); | |
382 | CheckSize.cy = ::GetSystemMetrics(SM_CYMENUCHECK); | |
383 | ||
9c32ed26 VZ |
384 | ArrowMargin = Margins(); |
385 | ||
386 | ArrowSize = CheckSize; | |
387 | ||
aa4919ed VZ |
388 | // separator height with margins |
389 | int sepFullSize = metrics.iMenuHeight / 2; | |
390 | ||
391 | SeparatorMargin.left = | |
392 | SeparatorMargin.right = 1; | |
393 | SeparatorMargin.top = | |
394 | SeparatorMargin.bottom = sepFullSize / 2 - 1; | |
395 | ||
396 | SeparatorSize.cx = 1; | |
397 | SeparatorSize.cy = sepFullSize - SeparatorMargin.top - SeparatorMargin.bottom; | |
398 | ||
399 | TextBorder = 0; | |
400 | AccelBorder = 8; | |
9c32ed26 VZ |
401 | ArrowBorder = 6; |
402 | ||
403 | Offset = -12; | |
aa4919ed VZ |
404 | |
405 | Font = wxFont(wxNativeFontInfo(metrics.lfMenuFont)); | |
406 | ||
407 | Theme = false; | |
408 | } | |
409 | ||
410 | int value; | |
411 | if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &value, 0) == 0 ) | |
412 | { | |
413 | // if it's not supported, we must be on an old Windows version | |
414 | // which always shows them | |
415 | value = 1; | |
416 | } | |
417 | ||
418 | AlwaysShowCues = value == 1; | |
419 | ||
420 | } | |
421 | ||
422 | } // anonymous namespace | |
98fbab9e VZ |
423 | |
424 | #endif // wxUSE_OWNER_DRAWN | |
425 | ||
426 | ||
2bda0e17 KB |
427 | // ctor & dtor |
428 | // ----------- | |
429 | ||
974e8d94 VZ |
430 | wxMenuItem::wxMenuItem(wxMenu *pParentMenu, |
431 | int id, | |
432 | const wxString& text, | |
433 | const wxString& strHelp, | |
d65c269b | 434 | wxItemKind kind, |
90002c49 | 435 | wxMenu *pSubMenu) |
d65c269b | 436 | : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu) |
2bda0e17 | 437 | { |
2368dcda VZ |
438 | Init(); |
439 | } | |
2bda0e17 | 440 | |
efebabb7 | 441 | #if WXWIN_COMPATIBILITY_2_8 |
2368dcda VZ |
442 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
443 | int id, | |
444 | const wxString& text, | |
445 | const wxString& help, | |
446 | bool isCheckable, | |
447 | wxMenu *subMenu) | |
448 | : wxMenuItemBase(parentMenu, id, text, help, | |
449 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) | |
2368dcda VZ |
450 | { |
451 | Init(); | |
452 | } | |
efebabb7 | 453 | #endif |
2368dcda VZ |
454 | |
455 | void wxMenuItem::Init() | |
456 | { | |
be15b995 | 457 | m_radioGroup.start = -1; |
598ddd96 | 458 | m_isRadioGroupStart = false; |
0472ece7 | 459 | |
47d67540 | 460 | #if wxUSE_OWNER_DRAWN |
c2dcfdef | 461 | |
51d2fa37 VZ |
462 | // when the color is not valid, wxOwnerDraw takes the default ones. |
463 | // If we set the colors here and they are changed by the user during | |
464 | // the execution, then the colors are not updated until the application | |
465 | // is restarted and our menus look bad | |
466 | SetTextColour(wxNullColour); | |
467 | SetBackgroundColour(wxNullColour); | |
6d5b2a57 | 468 | |
271fa250 | 469 | // setting default colors switched ownerdraw on: switch it off again |
98fbab9e | 470 | SetOwnerDrawn(false); |
2bda0e17 | 471 | |
271fa250 | 472 | // switch ownerdraw back on if using a non default margin |
fa7134b0 | 473 | if ( !IsSeparator() ) |
271fa250 JS |
474 | SetMarginWidth(GetMarginWidth()); |
475 | ||
974e8d94 | 476 | #endif // wxUSE_OWNER_DRAWN |
2bda0e17 KB |
477 | } |
478 | ||
c2dcfdef | 479 | wxMenuItem::~wxMenuItem() |
2bda0e17 KB |
480 | { |
481 | } | |
482 | ||
483 | // misc | |
484 | // ---- | |
485 | ||
c2dcfdef | 486 | // return the id for calling Win32 API functions |
dca0f651 | 487 | WXWPARAM wxMenuItem::GetMSWId() const |
c2dcfdef | 488 | { |
660e7fda VZ |
489 | // we must use ids in unsigned short range with Windows functions, if we |
490 | // pass ids > USHRT_MAX to them they get very confused (e.g. start | |
491 | // generating WM_COMMAND messages with negative high word of wParam), so | |
492 | // use the cast to ensure the id is in range | |
dca0f651 | 493 | return m_subMenu ? wxPtrToUInt(m_subMenu->GetHMenu()) |
5c33522f | 494 | : static_cast<unsigned short>(GetId()); |
c2dcfdef VZ |
495 | } |
496 | ||
3dfac970 VZ |
497 | // get item state |
498 | // -------------- | |
499 | ||
a8cfd0cb | 500 | bool wxMenuItem::IsChecked() const |
3dfac970 | 501 | { |
654c223b VZ |
502 | // fix that RTTI is always getting the correct state (separators cannot be |
503 | // checked, but the Windows call below returns true | |
fa7134b0 | 504 | if ( IsSeparator() ) |
654c223b | 505 | return false; |
e70b4f10 | 506 | |
654c223b VZ |
507 | // the item might not be attached to a menu yet |
508 | // | |
509 | // TODO: shouldn't we just always call the base class version? It seems | |
510 | // like it ought to always be in sync | |
511 | if ( !m_parentMenu ) | |
512 | return wxMenuItemBase::IsChecked(); | |
513 | ||
514 | HMENU hmenu = GetHMenuOf(m_parentMenu); | |
515 | int flag = ::GetMenuState(hmenu, GetMSWId(), MF_BYCOMMAND); | |
3dfac970 | 516 | |
4aee367e | 517 | return (flag & MF_CHECKED) != 0; |
3dfac970 VZ |
518 | } |
519 | ||
be15b995 VZ |
520 | // radio group stuff |
521 | // ----------------- | |
522 | ||
523 | void wxMenuItem::SetAsRadioGroupStart() | |
524 | { | |
598ddd96 | 525 | m_isRadioGroupStart = true; |
be15b995 VZ |
526 | } |
527 | ||
528 | void wxMenuItem::SetRadioGroupStart(int start) | |
529 | { | |
530 | wxASSERT_MSG( !m_isRadioGroupStart, | |
9a83f860 | 531 | wxT("should only be called for the next radio items") ); |
be15b995 VZ |
532 | |
533 | m_radioGroup.start = start; | |
534 | } | |
535 | ||
536 | void wxMenuItem::SetRadioGroupEnd(int end) | |
537 | { | |
538 | wxASSERT_MSG( m_isRadioGroupStart, | |
9a83f860 | 539 | wxT("should only be called for the first radio item") ); |
be15b995 VZ |
540 | |
541 | m_radioGroup.end = end; | |
542 | } | |
543 | ||
2bda0e17 KB |
544 | // change item state |
545 | // ----------------- | |
546 | ||
717a57c2 | 547 | void wxMenuItem::Enable(bool enable) |
2bda0e17 | 548 | { |
717a57c2 VZ |
549 | if ( m_isEnabled == enable ) |
550 | return; | |
551 | ||
654c223b VZ |
552 | if ( m_parentMenu ) |
553 | { | |
554 | long rc = EnableMenuItem(GetHMenuOf(m_parentMenu), | |
555 | GetMSWId(), | |
556 | MF_BYCOMMAND | | |
557 | (enable ? MF_ENABLED : MF_GRAYED)); | |
c2dcfdef | 558 | |
654c223b VZ |
559 | if ( rc == -1 ) |
560 | { | |
561 | wxLogLastError(wxT("EnableMenuItem")); | |
562 | } | |
c2dcfdef | 563 | } |
717a57c2 VZ |
564 | |
565 | wxMenuItemBase::Enable(enable); | |
2bda0e17 KB |
566 | } |
567 | ||
717a57c2 | 568 | void wxMenuItem::Check(bool check) |
2bda0e17 | 569 | { |
d65c269b | 570 | wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") ); |
c2dcfdef | 571 | |
717a57c2 VZ |
572 | if ( m_isChecked == check ) |
573 | return; | |
c2dcfdef | 574 | |
654c223b | 575 | if ( m_parentMenu ) |
0472ece7 | 576 | { |
654c223b VZ |
577 | int flags = check ? MF_CHECKED : MF_UNCHECKED; |
578 | HMENU hmenu = GetHMenuOf(m_parentMenu); | |
0472ece7 | 579 | |
654c223b | 580 | if ( GetKind() == wxITEM_RADIO ) |
be15b995 | 581 | { |
654c223b VZ |
582 | // it doesn't make sense to uncheck a radio item -- what would this |
583 | // do? | |
584 | if ( !check ) | |
585 | return; | |
586 | ||
587 | // get the index of this item in the menu | |
588 | const wxMenuItemList& items = m_parentMenu->GetMenuItems(); | |
589 | int pos = items.IndexOf(this); | |
590 | wxCHECK_RET( pos != wxNOT_FOUND, | |
9a83f860 | 591 | wxT("menuitem not found in the menu items list?") ); |
654c223b VZ |
592 | |
593 | // get the radio group range | |
594 | int start, | |
595 | end; | |
596 | ||
597 | if ( m_isRadioGroupStart ) | |
598 | { | |
599 | // we already have all information we need | |
600 | start = pos; | |
601 | end = m_radioGroup.end; | |
602 | } | |
603 | else // next radio group item | |
604 | { | |
605 | // get the radio group end from the start item | |
606 | start = m_radioGroup.start; | |
607 | end = items.Item(start)->GetData()->m_radioGroup.end; | |
608 | } | |
be15b995 | 609 | |
0472ece7 | 610 | #ifdef __WIN32__ |
654c223b VZ |
611 | // calling CheckMenuRadioItem() with such parameters hangs my system |
612 | // (NT4 SP6) and I suspect this could happen to the others as well, | |
613 | // so don't do it! | |
614 | wxCHECK_RET( start != -1 && end != -1, | |
9a83f860 | 615 | wxT("invalid ::CheckMenuRadioItem() parameter(s)") ); |
654c223b VZ |
616 | |
617 | if ( !::CheckMenuRadioItem(hmenu, | |
618 | start, // the first radio group item | |
619 | end, // the last one | |
620 | pos, // the one to check | |
621 | MF_BYPOSITION) ) | |
622 | { | |
9a83f860 | 623 | wxLogLastError(wxT("CheckMenuRadioItem")); |
654c223b | 624 | } |
0472ece7 VZ |
625 | #endif // __WIN32__ |
626 | ||
654c223b VZ |
627 | // also uncheck all the other items in this radio group |
628 | wxMenuItemList::compatibility_iterator node = items.Item(start); | |
629 | for ( int n = start; n <= end && node; n++ ) | |
0472ece7 | 630 | { |
654c223b VZ |
631 | if ( n != pos ) |
632 | { | |
633 | node->GetData()->m_isChecked = false; | |
634 | } | |
0472ece7 | 635 | |
654c223b VZ |
636 | node = node->GetNext(); |
637 | } | |
0472ece7 | 638 | } |
654c223b | 639 | else // check item |
0472ece7 | 640 | { |
654c223b VZ |
641 | if ( ::CheckMenuItem(hmenu, |
642 | GetMSWId(), | |
643 | MF_BYCOMMAND | flags) == (DWORD)-1 ) | |
644 | { | |
9a83f860 | 645 | wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?")); |
654c223b | 646 | } |
0472ece7 | 647 | } |
c2dcfdef | 648 | } |
717a57c2 VZ |
649 | |
650 | wxMenuItemBase::Check(check); | |
c2dcfdef VZ |
651 | } |
652 | ||
52af3158 | 653 | void wxMenuItem::SetItemLabel(const wxString& txt) |
c2dcfdef | 654 | { |
ee0a94cf RR |
655 | wxString text = txt; |
656 | ||
c2dcfdef | 657 | // don't do anything if label didn't change |
ee0a94cf | 658 | if ( m_text == txt ) |
c2dcfdef VZ |
659 | return; |
660 | ||
345319d6 | 661 | // wxMenuItemBase will do stock ID checks |
52af3158 | 662 | wxMenuItemBase::SetItemLabel(text); |
345319d6 | 663 | |
5c6aad47 VZ |
664 | // the item can be not attached to any menu yet and SetItemLabel() is still |
665 | // valid to call in this case and should do nothing else | |
3350ab0c VZ |
666 | if ( !m_parentMenu ) |
667 | return; | |
668 | ||
654c223b VZ |
669 | #if wxUSE_ACCEL |
670 | m_parentMenu->UpdateAccel(this); | |
671 | #endif // wxUSE_ACCEL | |
672 | ||
5c6aad47 VZ |
673 | const UINT id = GetMSWId(); |
674 | HMENU hMenu = GetHMenuOf(m_parentMenu); | |
675 | if ( !hMenu || ::GetMenuState(hMenu, id, MF_BYCOMMAND) == (UINT)-1 ) | |
676 | return; | |
677 | ||
3d45718d VZ |
678 | #if wxUSE_OWNER_DRAWN |
679 | if ( IsOwnerDrawn() ) | |
c2dcfdef | 680 | { |
3d45718d VZ |
681 | // we don't need to do anything for owner drawn items, they will redraw |
682 | // themselves using the new text the next time they're displayed | |
683 | return; | |
c2dcfdef | 684 | } |
3d45718d | 685 | #endif // owner drawn |
c2dcfdef | 686 | |
3d45718d | 687 | // update the text of the native menu item |
3d45718d | 688 | WinStruct<MENUITEMINFO> info; |
c2dcfdef | 689 | |
3d45718d VZ |
690 | // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't |
691 | // work as it resets the menu bitmap, so we need to first get the old item | |
692 | // state and then modify it | |
693 | const bool isLaterThanWin95 = wxGetWinVersion() > wxWinVersion_95; | |
d4290fa5 VZ |
694 | info.fMask = MIIM_STATE | |
695 | MIIM_ID | | |
696 | MIIM_SUBMENU | | |
697 | MIIM_CHECKMARKS | | |
698 | MIIM_DATA; | |
3d45718d VZ |
699 | if ( isLaterThanWin95 ) |
700 | info.fMask |= MIIM_BITMAP | MIIM_FTYPE; | |
701 | else | |
d4290fa5 | 702 | info.fMask |= MIIM_TYPE; |
3d45718d VZ |
703 | if ( !::GetMenuItemInfo(hMenu, id, FALSE, &info) ) |
704 | { | |
705 | wxLogLastError(wxT("GetMenuItemInfo")); | |
706 | return; | |
707 | } | |
708 | ||
709 | if ( isLaterThanWin95 ) | |
710 | info.fMask |= MIIM_STRING; | |
711 | //else: MIIM_TYPE already specified | |
712 | info.dwTypeData = (LPTSTR)m_text.wx_str(); | |
713 | info.cch = m_text.length(); | |
714 | if ( !::SetMenuItemInfo(hMenu, id, FALSE, &info) ) | |
715 | { | |
716 | wxLogLastError(wxT("SetMenuItemInfo")); | |
c2dcfdef VZ |
717 | } |
718 | } | |
2bda0e17 | 719 | |
98fbab9e VZ |
720 | #if wxUSE_OWNER_DRAWN |
721 | ||
9c32ed26 VZ |
722 | int wxMenuItem::MeasureAccelWidth() const |
723 | { | |
724 | wxString accel = GetItemLabel().AfterFirst(wxT('\t')); | |
725 | ||
726 | wxMemoryDC dc; | |
727 | wxFont font; | |
728 | GetFontToUse(font); | |
729 | dc.SetFont(font); | |
730 | ||
731 | wxCoord w; | |
732 | dc.GetTextExtent(accel, &w, NULL); | |
733 | ||
734 | return w; | |
735 | } | |
736 | ||
98fbab9e | 737 | wxString wxMenuItem::GetName() const |
974e8d94 | 738 | { |
98fbab9e | 739 | return GetItemLabelText(); |
974e8d94 VZ |
740 | } |
741 | ||
98fbab9e VZ |
742 | bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height) |
743 | { | |
aa4919ed VZ |
744 | const MenuDrawData* data = MenuDrawData::Get(); |
745 | ||
98fbab9e VZ |
746 | if ( IsOwnerDrawn() ) |
747 | { | |
aa4919ed VZ |
748 | *width = data->ItemMargin.left + data->ItemMargin.right; |
749 | *height = data->ItemMargin.top + data->ItemMargin.bottom; | |
98fbab9e | 750 | |
aa4919ed | 751 | if ( IsSeparator() ) |
98fbab9e | 752 | { |
aa4919ed VZ |
753 | *width += data->SeparatorSize.cx |
754 | + data->SeparatorMargin.left + data->SeparatorMargin.right; | |
755 | *height += data->SeparatorSize.cy | |
756 | + data->SeparatorMargin.top + data->SeparatorMargin.bottom; | |
757 | return true; | |
98fbab9e VZ |
758 | } |
759 | ||
9c32ed26 | 760 | wxString str = GetName(); |
aa4919ed | 761 | |
98fbab9e VZ |
762 | wxMemoryDC dc; |
763 | wxFont font; | |
764 | GetFontToUse(font); | |
765 | dc.SetFont(font); | |
766 | ||
767 | wxCoord w, h; | |
768 | dc.GetTextExtent(str, &w, &h); | |
aa4919ed | 769 | |
9c32ed26 | 770 | *width = data->TextBorder + w + data->AccelBorder; |
98fbab9e | 771 | *height = h; |
aa4919ed | 772 | |
9c32ed26 VZ |
773 | w = m_parentMenu->GetMaxAccelWidth(); |
774 | if ( w > 0 ) | |
775 | *width += w + data->ArrowBorder; | |
776 | ||
777 | *width += data->Offset; | |
778 | *width += data->ArrowMargin.left + data->ArrowSize.cx + data->ArrowMargin.right; | |
98fbab9e VZ |
779 | } |
780 | else // don't draw the text, just the bitmap (if any) | |
781 | { | |
782 | *width = 0; | |
783 | *height = 0; | |
784 | } | |
785 | ||
aa4919ed VZ |
786 | // bitmap |
787 | ||
788 | if ( IsOwnerDrawn() ) | |
98fbab9e | 789 | { |
03cc2991 | 790 | // width of menu icon with margins in ownerdrawn menu |
aa4919ed VZ |
791 | // if any bitmap is not set, the width of space reserved for icon |
792 | // image is equal to the width of std check mark, | |
793 | // if bitmap is set, then the width is set to the width of the widest | |
794 | // bitmap in menu (GetMarginWidth()) unless std check mark is wider, | |
795 | // then it's is set to std mark's width | |
796 | int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx) | |
797 | + data->CheckMargin.left + data->CheckMargin.right; | |
798 | ||
799 | *width += imgWidth + data->CheckBgMargin.left + data->CheckBgMargin.right; | |
800 | } | |
801 | ||
802 | if ( m_bmpChecked.IsOk() || m_bmpChecked.IsOk() ) | |
803 | { | |
804 | // get size of bitmap always return valid value (0 for invalid bitmap), | |
805 | // so we don't needed check if bitmap is valid ;) | |
806 | size_t heightBmp = wxMax(m_bmpChecked.GetHeight(), m_bmpUnchecked.GetHeight()); | |
807 | size_t widthtBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth()); | |
98fbab9e | 808 | |
98fbab9e VZ |
809 | if ( IsOwnerDrawn() ) |
810 | { | |
aa4919ed | 811 | heightBmp += data->CheckMargin.top + data->CheckMargin.bottom; |
98fbab9e | 812 | } |
aa4919ed | 813 | else |
98fbab9e | 814 | { |
aa4919ed VZ |
815 | // we must allocate enough space for the bitmap |
816 | *width += widthtBmp; | |
98fbab9e | 817 | } |
98fbab9e | 818 | |
aa4919ed VZ |
819 | // Is BMP height larger than text height? |
820 | if ( *height < heightBmp ) | |
821 | *height = heightBmp; | |
98fbab9e VZ |
822 | } |
823 | ||
824 | // make sure that this item is at least as tall as the system menu height | |
aa4919ed VZ |
825 | const size_t menuHeight = data->CheckMargin.top + data->CheckMargin.bottom |
826 | + data->CheckSize.cy; | |
827 | if (*height < menuHeight) | |
828 | *height = menuHeight; | |
98fbab9e VZ |
829 | |
830 | return true; | |
831 | } | |
832 | ||
833 | bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, | |
834 | wxODAction WXUNUSED(act), wxODStatus stat) | |
835 | { | |
aa4919ed | 836 | const MenuDrawData* data = MenuDrawData::Get(); |
98fbab9e | 837 | |
aa4919ed VZ |
838 | wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl(); |
839 | HDC hdc = GetHdcOf(*impl); | |
98fbab9e | 840 | |
aa4919ed VZ |
841 | RECT rect; |
842 | wxCopyRectToRECT(rc, rect); | |
98fbab9e | 843 | |
03cc2991 | 844 | int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx); |
98fbab9e VZ |
845 | |
846 | if ( IsOwnerDrawn() ) | |
847 | { | |
aa4919ed VZ |
848 | // font and colors to use |
849 | wxFont font; | |
850 | GetFontToUse(font); | |
851 | ||
852 | wxColour colText1, colBack1; | |
853 | GetColourToUse(stat, colText1, colBack1); | |
854 | ||
855 | DWORD colText = wxColourToPalRGB(colText1); | |
856 | DWORD colBack = wxColourToPalRGB(colBack1); | |
857 | ||
858 | // calculate metrics of item parts | |
859 | RECT rcSelection; | |
860 | RECT rcSeparator; | |
861 | RECT rcGutter; | |
862 | RECT rcText; | |
863 | ||
864 | SetRect(&rcSelection, | |
865 | rect.left + data->ItemMargin.left, | |
866 | rect.top + data->ItemMargin.top, | |
867 | rect.right - data->ItemMargin.right, | |
868 | rect.bottom - data->ItemMargin.bottom); | |
869 | ||
870 | SetRect(&rcSeparator, | |
871 | rcSelection.left + data->SeparatorMargin.left, | |
872 | rcSelection.top + data->SeparatorMargin.top, | |
873 | rcSelection.right - data->SeparatorMargin.right, | |
874 | rcSelection.bottom - data->SeparatorMargin.bottom); | |
875 | ||
876 | CopyRect(&rcGutter, &rcSelection); | |
877 | rcGutter.right = data->ItemMargin.left | |
878 | + data->CheckBgMargin.left | |
03cc2991 | 879 | + data->CheckMargin.left |
aa4919ed | 880 | + imgWidth |
03cc2991 | 881 | + data->CheckMargin.right |
aa4919ed VZ |
882 | + data->CheckBgMargin.right; |
883 | ||
884 | CopyRect(&rcText, &rcSelection); | |
885 | rcText.left = rcGutter.right + data->TextBorder; | |
886 | ||
03cc2991 VZ |
887 | // we draw the text label vertically centered, but this results in it |
888 | // being 1px too low compared to native menus for some reason, fix it | |
889 | if ( data->MenuLayout() != MenuDrawData::FullTheme ) | |
890 | rcText.top--; | |
891 | ||
aa4919ed VZ |
892 | #if wxUSE_UXTHEME |
893 | wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine(); | |
894 | if ( theme ) | |
98fbab9e | 895 | { |
aa4919ed VZ |
896 | POPUPITEMSTATES state; |
897 | if ( stat & wxODDisabled ) | |
898 | { | |
899 | state = (stat & wxODSelected) ? MPI_DISABLEDHOT | |
900 | : MPI_DISABLED; | |
901 | } | |
902 | else if ( stat & wxODSelected ) | |
903 | { | |
904 | state = MPI_HOT; | |
905 | } | |
906 | else | |
907 | { | |
908 | state = MPI_NORMAL; | |
909 | } | |
98fbab9e | 910 | |
aa4919ed | 911 | wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU"); |
98fbab9e | 912 | |
aa4919ed VZ |
913 | if ( theme->IsThemeBackgroundPartiallyTransparent(hTheme, |
914 | MENU_POPUPITEM, state) ) | |
915 | { | |
916 | theme->DrawThemeBackground(hTheme, hdc, | |
917 | MENU_POPUPBACKGROUND, | |
918 | 0, &rect, NULL); | |
919 | } | |
98fbab9e | 920 | |
aa4919ed VZ |
921 | theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPGUTTER, |
922 | 0, &rcGutter, NULL); | |
98fbab9e | 923 | |
aa4919ed VZ |
924 | if ( IsSeparator() ) |
925 | { | |
926 | rcSeparator.left = rcGutter.right; | |
927 | theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPSEPARATOR, | |
928 | 0, &rcSeparator, NULL); | |
929 | return true; | |
930 | } | |
98fbab9e | 931 | |
aa4919ed VZ |
932 | theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPITEM, |
933 | state, &rcSelection, NULL); | |
98fbab9e | 934 | |
aa4919ed VZ |
935 | } |
936 | else | |
937 | #endif // wxUSE_UXTHEME | |
938 | { | |
939 | if ( IsSeparator() ) | |
940 | { | |
941 | DrawEdge(hdc, &rcSeparator, EDGE_ETCHED, BF_TOP); | |
942 | return true; | |
943 | } | |
98fbab9e | 944 | |
aa4919ed VZ |
945 | AutoHBRUSH hbr(colBack); |
946 | SelectInHDC selBrush(hdc, hbr); | |
947 | ::FillRect(hdc, &rcSelection, hbr); | |
948 | } | |
98fbab9e | 949 | |
98fbab9e | 950 | |
aa4919ed VZ |
951 | // draw text label |
952 | // using native API because it recognizes '&' | |
98fbab9e | 953 | |
aa4919ed VZ |
954 | COLORREF colOldText = ::SetTextColor(hdc, colText); |
955 | COLORREF colOldBack = ::SetBkColor(hdc, colBack); | |
98fbab9e | 956 | |
aa4919ed | 957 | int prevMode = SetBkMode(hdc, TRANSPARENT); |
98fbab9e | 958 | |
98fbab9e VZ |
959 | SelectInHDC selFont(hdc, GetHfontOf(font)); |
960 | ||
98fbab9e | 961 | |
aa4919ed VZ |
962 | // item text name without menemonic for calculating size |
963 | wxString text = GetName(); | |
964 | ||
965 | SIZE textSize; | |
966 | ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize); | |
98fbab9e | 967 | |
aa4919ed VZ |
968 | // item text name with menemonic |
969 | text = GetItemLabel().BeforeFirst('\t'); | |
98fbab9e VZ |
970 | |
971 | int flags = DST_PREFIXTEXT; | |
aa4919ed VZ |
972 | // themes menu is using specified color for disabled labels |
973 | if ( data->MenuLayout() == MenuDrawData::Classic && | |
974 | (stat & wxODDisabled) && !(stat & wxODSelected) ) | |
98fbab9e VZ |
975 | flags |= DSS_DISABLED; |
976 | ||
aa4919ed | 977 | if ( (stat & wxODHidePrefix) && !data->AlwaysShowCues ) |
98fbab9e VZ |
978 | flags |= DSS_HIDEPREFIX; |
979 | ||
aa4919ed VZ |
980 | int x = rcText.left; |
981 | int y = rcText.top + (rcText.bottom - rcText.top - textSize.cy) / 2; | |
98fbab9e VZ |
982 | |
983 | ::DrawState(hdc, NULL, NULL, (LPARAM)text.wx_str(), | |
aa4919ed | 984 | text.length(), x, y, 0, 0, flags); |
98fbab9e VZ |
985 | |
986 | // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO | |
987 | // as the last parameter in DrawState() (at least with Windows98). So we have | |
988 | // to take care of right alignment ourselves. | |
989 | wxString accel = GetItemLabel().AfterFirst(wxT('\t')); | |
990 | if ( !accel.empty() ) | |
991 | { | |
aa4919ed VZ |
992 | SIZE accelSize; |
993 | ::GetTextExtentPoint32(hdc, accel.c_str(), accel.length(), &accelSize); | |
98fbab9e VZ |
994 | |
995 | int flags = DST_TEXT; | |
aa4919ed VZ |
996 | // themes menu is using specified color for disabled labels |
997 | if ( data->MenuLayout() == MenuDrawData::Classic && | |
998 | (stat & wxODDisabled) && !(stat & wxODSelected) ) | |
98fbab9e VZ |
999 | flags |= DSS_DISABLED; |
1000 | ||
9c32ed26 VZ |
1001 | int x = rcText.right - data->ArrowMargin.left |
1002 | - data->ArrowSize.cx | |
1003 | - data->ArrowMargin.right | |
1004 | - data->ArrowBorder; | |
1005 | ||
1006 | // right align accel on FullTheme menu, left otherwise | |
1007 | if ( data->MenuLayout() == MenuDrawData::FullTheme) | |
1008 | x -= accelSize.cx; | |
1009 | else | |
1010 | x -= m_parentMenu->GetMaxAccelWidth(); | |
98fbab9e | 1011 | |
aa4919ed VZ |
1012 | int y = rcText.top + (rcText.bottom - rcText.top - accelSize.cy) / 2; |
1013 | ||
98fbab9e VZ |
1014 | ::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(), |
1015 | accel.length(), x, y, 0, 0, flags); | |
1016 | } | |
1017 | ||
1018 | ::SetBkMode(hdc, prevMode); | |
aa4919ed VZ |
1019 | ::SetBkColor(hdc, colOldBack); |
1020 | ::SetTextColor(hdc, colOldText); | |
98fbab9e VZ |
1021 | } |
1022 | ||
1023 | ||
1024 | // draw the bitmap | |
aa4919ed VZ |
1025 | |
1026 | RECT rcImg; | |
1027 | SetRect(&rcImg, | |
03cc2991 VZ |
1028 | rect.left + data->ItemMargin.left |
1029 | + data->CheckBgMargin.left | |
1030 | + data->CheckMargin.left, | |
1031 | rect.top + data->ItemMargin.top | |
1032 | + data->CheckBgMargin.top | |
1033 | + data->CheckMargin.top, | |
1034 | rect.left + data->ItemMargin.left | |
1035 | + data->CheckBgMargin.left | |
1036 | + data->CheckMargin.left | |
1037 | + imgWidth, | |
1038 | rect.bottom - data->ItemMargin.bottom | |
1039 | - data->CheckBgMargin.bottom | |
1040 | - data->CheckMargin.bottom); | |
aa4919ed | 1041 | |
98fbab9e VZ |
1042 | if ( IsCheckable() && !m_bmpChecked.Ok() ) |
1043 | { | |
1044 | if ( stat & wxODChecked ) | |
1045 | { | |
b8190883 | 1046 | DrawStdCheckMark((WXHDC)hdc, &rcImg, stat); |
98fbab9e VZ |
1047 | } |
1048 | } | |
1049 | else | |
1050 | { | |
1051 | wxBitmap bmp; | |
1052 | ||
1053 | if ( stat & wxODDisabled ) | |
1054 | { | |
1055 | bmp = GetDisabledBitmap(); | |
1056 | } | |
1057 | ||
1058 | if ( !bmp.Ok() ) | |
1059 | { | |
1060 | // for not checkable bitmaps we should always use unchecked one | |
1061 | // because their checked bitmap is not set | |
1062 | bmp = GetBitmap(!IsCheckable() || (stat & wxODChecked)); | |
1063 | ||
1064 | #if wxUSE_IMAGE | |
1065 | if ( bmp.Ok() && stat & wxODDisabled ) | |
1066 | { | |
1067 | // we need to grey out the bitmap as we don't have any specific | |
1068 | // disabled bitmap | |
1069 | wxImage imgGrey = bmp.ConvertToImage().ConvertToGreyscale(); | |
1070 | if ( imgGrey.Ok() ) | |
1071 | bmp = wxBitmap(imgGrey); | |
1072 | } | |
1073 | #endif // wxUSE_IMAGE | |
1074 | } | |
1075 | ||
1076 | if ( bmp.Ok() ) | |
1077 | { | |
1078 | wxMemoryDC dcMem(&dc); | |
1079 | dcMem.SelectObjectAsSource(bmp); | |
1080 | ||
1081 | // center bitmap | |
aa4919ed | 1082 | int nBmpWidth = bmp.GetWidth(), |
98fbab9e VZ |
1083 | nBmpHeight = bmp.GetHeight(); |
1084 | ||
1085 | // there should be enough space! | |
aa4919ed | 1086 | wxASSERT( nBmpWidth <= imgWidth && nBmpHeight <= (rcImg.bottom - rcImg.top) ); |
98fbab9e | 1087 | |
aa4919ed VZ |
1088 | int x = rcImg.left + (imgWidth - nBmpWidth) / 2; |
1089 | int y = rcImg.top + (rcImg.bottom - rcImg.top - nBmpHeight) / 2; | |
1090 | dc.Blit(x, y, nBmpWidth, nBmpHeight, &dcMem, 0, 0, wxCOPY, true); | |
98fbab9e VZ |
1091 | } |
1092 | } | |
1093 | ||
98fbab9e VZ |
1094 | return true; |
1095 | ||
1096 | } | |
1097 | ||
ee009313 VZ |
1098 | namespace |
1099 | { | |
1100 | ||
1101 | // helper function for draw coloured check mark | |
1102 | void DrawColorCheckMark(HDC hdc, int x, int y, int cx, int cy, HDC hdcCheckMask, int idxColor) | |
1103 | { | |
1104 | const COLORREF colBlack = RGB(0, 0, 0); | |
1105 | const COLORREF colWhite = RGB(255, 255, 255); | |
1106 | ||
1107 | COLORREF colOldText = ::SetTextColor(hdc, colBlack); | |
1108 | COLORREF colOldBack = ::SetBkColor(hdc, colWhite); | |
1109 | int prevMode = SetBkMode(hdc, TRANSPARENT); | |
1110 | ||
1111 | // memory DC for color bitmap | |
1112 | MemoryHDC hdcMem(hdc); | |
1113 | CompatibleBitmap hbmpMem(hdc, cx, cy); | |
1114 | SelectInHDC selMem(hdcMem, hbmpMem); | |
1115 | ||
1116 | RECT rect = { 0, 0, cx, cy }; | |
1117 | ::FillRect(hdcMem, &rect, ::GetSysColorBrush(idxColor)); | |
1118 | ||
1119 | const COLORREF colCheck = ::GetSysColor(idxColor); | |
1120 | if ( colCheck == colWhite ) | |
1121 | { | |
1122 | ::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, MERGEPAINT); | |
1123 | ::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCAND); | |
1124 | } | |
1125 | else | |
1126 | { | |
1127 | if ( colCheck != colBlack ) | |
1128 | { | |
1129 | const DWORD ROP_DSna = 0x00220326; // dest = (NOT src) AND dest | |
1130 | ::BitBlt(hdcMem, 0, 0, cx, cy, hdcCheckMask, 0, 0, ROP_DSna); | |
1131 | } | |
1132 | ||
1133 | ::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, SRCAND); | |
1134 | ::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCPAINT); | |
1135 | } | |
1136 | ||
1137 | ::SetBkMode(hdc, prevMode); | |
1138 | ::SetBkColor(hdc, colOldBack); | |
1139 | ::SetTextColor(hdc, colOldText); | |
1140 | } | |
1141 | ||
1142 | } // anonymous namespace | |
1143 | ||
b8190883 | 1144 | void wxMenuItem::DrawStdCheckMark(WXHDC hdc_, const RECT* rc, wxODStatus stat) |
ee009313 | 1145 | { |
b8190883 VZ |
1146 | HDC hdc = (HDC)hdc_; |
1147 | ||
ee009313 VZ |
1148 | #if wxUSE_UXTHEME |
1149 | wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine(); | |
1150 | if ( theme ) | |
1151 | { | |
1152 | wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU"); | |
1153 | ||
03cc2991 VZ |
1154 | const MenuDrawData* data = MenuDrawData::Get(); |
1155 | ||
1156 | // rect for background must be without check margins | |
1157 | RECT rcBg; | |
1158 | SetRect(&rcBg, | |
1159 | rc->left - data->CheckMargin.left, | |
1160 | rc->top - data->CheckMargin.top, | |
1161 | rc->right + data->CheckMargin.right, | |
1162 | rc->bottom + data->CheckMargin.bottom); | |
1163 | ||
ee009313 VZ |
1164 | POPUPCHECKBACKGROUNDSTATES stateCheckBg = (stat & wxODDisabled) |
1165 | ? MCB_DISABLED | |
1166 | : MCB_NORMAL; | |
1167 | ||
1168 | theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECKBACKGROUND, | |
03cc2991 | 1169 | stateCheckBg, &rcBg, NULL); |
ee009313 VZ |
1170 | |
1171 | POPUPCHECKSTATES stateCheck; | |
1172 | if ( GetKind() == wxITEM_CHECK ) | |
1173 | { | |
1174 | stateCheck = (stat & wxODDisabled) ? MC_CHECKMARKDISABLED | |
1175 | : MC_CHECKMARKNORMAL; | |
1176 | } | |
1177 | else | |
1178 | { | |
1179 | stateCheck = (stat & wxODDisabled) ? MC_BULLETDISABLED | |
1180 | : MC_BULLETNORMAL; | |
1181 | } | |
1182 | ||
1183 | theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECK, | |
1184 | stateCheck, rc, NULL); | |
1185 | } | |
1186 | else | |
1187 | #endif // wxUSE_UXTHEME | |
1188 | { | |
1189 | int cx = rc->right - rc->left; | |
1190 | int cy = rc->bottom - rc->top; | |
1191 | ||
1192 | // first create mask of check mark | |
1193 | MemoryHDC hdcMask(hdc); | |
1194 | MonoBitmap hbmpMask(cx, cy); | |
1195 | SelectInHDC selMask(hdcMask,hbmpMask); | |
1196 | ||
1197 | // then draw a check mark into it | |
1198 | UINT stateCheck = (GetKind() == wxITEM_CHECK) ? DFCS_MENUCHECK | |
1199 | : DFCS_MENUBULLET; | |
1200 | RECT rect = { 0, 0, cx, cy }; | |
1201 | ::DrawFrameControl(hdcMask, &rect, DFC_MENU, stateCheck); | |
1202 | ||
1203 | // first draw shadow if disabled | |
1204 | if ( (stat & wxODDisabled) && !(stat & wxODSelected) ) | |
1205 | { | |
1206 | DrawColorCheckMark(hdc, rc->left + 1, rc->top + 1, | |
1207 | cx, cy, hdcMask, COLOR_3DHILIGHT); | |
1208 | } | |
1209 | ||
1210 | // then draw a check mark | |
1211 | int color = COLOR_MENUTEXT; | |
1212 | if ( stat & wxODDisabled ) | |
1213 | color = COLOR_BTNSHADOW; | |
1214 | else if ( stat & wxODSelected ) | |
1215 | color = COLOR_HIGHLIGHTTEXT; | |
1216 | ||
1217 | DrawColorCheckMark(hdc, rc->left, rc->top, cx, cy, hdcMask, color); | |
1218 | } | |
1219 | } | |
1220 | ||
98fbab9e VZ |
1221 | void wxMenuItem::GetFontToUse(wxFont& font) const |
1222 | { | |
1223 | font = GetFont(); | |
1224 | if ( !font.IsOk() ) | |
aa4919ed | 1225 | font = MenuDrawData::Get()->Font; |
98fbab9e VZ |
1226 | } |
1227 | ||
aa4919ed VZ |
1228 | void wxMenuItem::GetColourToUse(wxODStatus stat, wxColour& colText, wxColour& colBack) const |
1229 | { | |
1230 | #if wxUSE_UXTHEME | |
1231 | wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine(); | |
1232 | if ( theme ) | |
1233 | { | |
1234 | wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU"); | |
1235 | ||
1236 | if ( stat & wxODDisabled) | |
1237 | { | |
1238 | wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_GRAYTEXT)); | |
1239 | } | |
1240 | else | |
1241 | { | |
1242 | colText = GetTextColour(); | |
1243 | if ( !colText.IsOk() ) | |
1244 | wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_MENUTEXT)); | |
1245 | } | |
1246 | ||
1247 | if ( stat & wxODSelected ) | |
1248 | { | |
1249 | wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_HIGHLIGHT)); | |
1250 | } | |
1251 | else | |
1252 | { | |
1253 | colBack = GetBackgroundColour(); | |
1254 | if ( !colBack.IsOk() ) | |
1255 | wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_MENU)); | |
1256 | } | |
1257 | } | |
1258 | else | |
1259 | #endif // wxUSE_UXTHEME | |
1260 | { | |
1261 | wxOwnerDrawn::GetColourToUse(stat, colText, colBack); | |
1262 | } | |
1263 | } | |
98fbab9e VZ |
1264 | #endif // wxUSE_OWNER_DRAWN |
1265 | ||
974e8d94 VZ |
1266 | // ---------------------------------------------------------------------------- |
1267 | // wxMenuItemBase | |
1268 | // ---------------------------------------------------------------------------- | |
1269 | ||
1270 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
1271 | int id, | |
1272 | const wxString& name, | |
1273 | const wxString& help, | |
d65c269b | 1274 | wxItemKind kind, |
974e8d94 VZ |
1275 | wxMenu *subMenu) |
1276 | { | |
d65c269b | 1277 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); |
974e8d94 | 1278 | } |
1e6feb95 VZ |
1279 | |
1280 | #endif // wxUSE_MENUS |