Debian Codegraph @

wxcrafter/src/menu_bar.cpp

raw
1 #include "menu_bar.h"
2 #include "allocator_mgr.h"
3 #include <event_notifier.h>
4 #include <wx/bitmap.h>
5 #include <wx/dcbuffer.h>
6 #include <wx/dcmemory.h>
7 #include <wx/msgdlg.h>
8 #include <wx/settings.h>
9
10 #define MARGIN_X 5
11 #define MARGIN_Y 5
12
13 const wxEventType wxEVT_PREVIEW_BAR_SELECTED = wxNewEventType();
14
15 BEGIN_EVENT_TABLE(MenuBar, wxPanel)
16 EVT_PAINT(MenuBar::OnPaint)
17 EVT_ERASE_BACKGROUND(MenuBar::OnEraseBG)
18 EVT_LEFT_DOWN(MenuBar::OnLeftDown)
19 EVT_RIGHT_DOWN(MenuBar::OnLeftDown)
20 END_EVENT_TABLE()
21
22 MenuBar::MenuBar(wxWindow* parent, wxMenuBar* mb)
23 : wxPanel(parent)
24 , m_mb(mb)
25 , m_controlHeight(30)
26 {
27 for(size_t i = 0; i < m_mb->GetMenuCount(); i++) {
28 MenuInfo mi;
29 mi.label = m_mb->GetMenuLabelText(i);
30 mi.menu = m_mb->GetMenu(i);
31 m_menus.push_back(mi);
32 }
33
34 // Remove the menus
35 size_t nCount = m_mb->GetMenuCount();
36 for(size_t i = 0; i < nCount; ++i) {
37 m_mb->Remove(0);
38 }
39 m_mbname = wxT("MENU_BAR_ID");
40
41 #ifndef __WXGTK__
42 // The parent (hidden frame) will destroy it for us)
43 m_mb->Destroy();
44 #endif
45
46 int width;
47 // Initialize a temporary memory dc and calculate the window height
48 wxBitmap bmp(30, 30);
49 wxMemoryDC memDc(bmp);
50
51 wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
52 memDc.GetTextExtent(wxT("Tp"), &width, &m_controlHeight, NULL, NULL, &font);
53
54 m_controlHeight += 2 * MARGIN_Y;
55 SetSizeHints(-1, m_controlHeight);
56 }
57
58 MenuBar::~MenuBar()
59 {
60 for(size_t i = 0; i < m_menus.size(); i++) {
61 wxDELETE(m_menus.at(i).menu);
62 }
63 m_menus.clear();
64 }
65
66 void MenuBar::OnEraseBG(wxEraseEvent& e) { wxUnusedVar(e); }
67
68 void MenuBar::OnLeftDown(wxMouseEvent& e)
69 {
70 size_t where = wxString::npos;
71 wxPoint pt = e.GetPosition();
72 for(size_t i = 0; i < m_menus.size(); i++) {
73 if(m_menus.at(i).rect.Contains(pt)) {
74 where = i;
75 break;
76 }
77 }
78
79 if(where == wxString::npos) {
80 wxCommandEvent evt(wxEVT_PREVIEW_BAR_SELECTED);
81 evt.SetString(wxT(""));
82 EventNotifier::Get()->AddPendingEvent(evt);
83 return;
84 }
85
86 wxString name;
87 if(m_menus.at(where).menu) {
88 wxMenu* menu = m_menus.at(where).menu;
89
90 wxCommandEvent evt(wxEVT_PREVIEW_BAR_SELECTED);
91 wxString s;
92 s << m_mbname << wxT(":") << m_menus.at(where).label;
93 evt.SetString(s);
94 evt.SetInt(ID_WXMENUBAR);
95 EventNotifier::Get()->AddPendingEvent(evt);
96 PopupMenu(menu, m_menus.at(where).rect.GetBottomLeft());
97 }
98 }
99
100 void MenuBar::OnPaint(wxPaintEvent& e)
101 {
102 int width, height;
103 wxBufferedPaintDC dc(this);
104 wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
105
106 wxColour bgColor = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR);
107
108 // Color the background
109 dc.SetPen(bgColor);
110 dc.SetBrush(bgColor);
111 dc.DrawRectangle(GetClientRect());
112
113 int posx = 0;
114 int posy = MARGIN_Y;
115
116 size_t count = m_menus.size();
117 for(size_t i = 0; i < count; i++) {
118 wxString menuTitle = m_menus.at(i).label;
119 dc.GetTextExtent(menuTitle, &width, &height, NULL, NULL, &font);
120
121 wxRect labelRect(posx, 0, MARGIN_X + width + MARGIN_X, m_controlHeight);
122 dc.SetFont(font);
123 dc.DrawText(menuTitle, labelRect.GetX() + MARGIN_X, posy);
124 posx += labelRect.GetWidth();
125 m_menus.at(i).rect = labelRect;
126 }
127 }