1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Owner-draw sample, for Windows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/ownerdrw.h"
28 #include "wx/menuitem.h"
29 #include "wx/msw/checklst.h"
31 // Define a new application type
32 class OwnerDrawnApp
: public wxApp
38 // Define a new frame type
39 class OwnerDrawnFrame
: public wxFrame
43 OwnerDrawnFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
47 void OnQuit (wxCommandEvent
& event
);
48 void OnMenuToggle (wxCommandEvent
& event
);
49 void OnAbout (wxCommandEvent
& event
);
50 void OnListboxSelect (wxCommandEvent
& event
);
51 void OnCheckboxToggle (wxCommandEvent
& event
);
52 void OnListboxDblClick (wxCommandEvent
& event
);
53 bool OnClose () { return TRUE
; }
60 wxCheckListBox
*m_pListBox
;
61 wxMenuItem
*pAboutItem
;
68 Menu_Test1
, Menu_Test2
, Menu_Test3
,
69 Menu_Bitmap
, Menu_Bitmap2
,
70 Menu_Submenu
, Menu_Sub1
, Menu_Sub2
, Menu_Sub3
,
71 Menu_Toggle
, Menu_About
,
73 Control_Listbox
, Control_Listbox2
,
76 BEGIN_EVENT_TABLE(OwnerDrawnFrame
, wxFrame
)
77 EVT_MENU(Menu_Toggle
, OwnerDrawnFrame::OnMenuToggle
)
78 EVT_MENU(Menu_About
, OwnerDrawnFrame::OnAbout
)
79 EVT_MENU(Menu_Quit
, OwnerDrawnFrame::OnQuit
)
80 EVT_LISTBOX(Control_Listbox
, OwnerDrawnFrame::OnListboxSelect
)
81 EVT_CHECKLISTBOX(Control_Listbox
, OwnerDrawnFrame::OnCheckboxToggle
)
82 EVT_COMMAND(Control_Listbox
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
,
83 OwnerDrawnFrame::OnListboxDblClick
)
86 IMPLEMENT_APP(OwnerDrawnApp
);
88 // init our app: create windows
89 bool OwnerDrawnApp::OnInit(void)
91 OwnerDrawnFrame
*pFrame
92 = new OwnerDrawnFrame(NULL
, _T("wxWindows Ownerdraw Sample"),
100 // create the menu bar for the main frame
101 void OwnerDrawnFrame::InitMenu()
104 wxMenu
*file_menu
= new wxMenu
,
105 *sub_menu
= new wxMenu
;
107 // vars used for menu construction
109 wxFont
fontLarge(18, wxROMAN
, wxNORMAL
, wxBOLD
, FALSE
),
110 fontUlined(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, TRUE
),
111 fontItalic(12, wxMODERN
, wxITALIC
, wxBOLD
, FALSE
),
112 // should be at least of the size of bitmaps
113 fontBmp(14, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
);
115 // sorry for my artistic skills...
116 wxBitmap
bmpBell(_T("bell")), bmpSound(_T("sound")), bmpNoSound(_T("nosound"));
117 wxBitmap
bmpInfo(_T("info")), bmpInfo_mono(_T("info_mono"));
120 pItem
= new wxMenuItem(sub_menu
, Menu_Sub1
, _T("Submenu &first"), _T("large"));
122 pItem
->SetFont(fontLarge
);
123 sub_menu
->Append(pItem
);
125 pItem
= new wxMenuItem(sub_menu
, Menu_Sub2
, _T("Submenu &second"), _T("italic"),
127 pItem
->SetFont(fontItalic
);
128 sub_menu
->Append(pItem
);
130 pItem
= new wxMenuItem(sub_menu
, Menu_Sub3
, _T("Submenu &third"), _T("underlined"),
132 pItem
->SetFont(fontUlined
);
133 sub_menu
->Append(pItem
);
136 pItem
= new wxMenuItem(file_menu
, Menu_Test1
, _T("&Uncheckable"), _T("red item"));
137 pItem
->SetFont(*wxITALIC_FONT
);
138 pItem
->SetTextColour(wxColor(255, 0, 0));
139 pItem
->SetMarginWidth(23);
140 file_menu
->Append(pItem
);
142 pItem
= new wxMenuItem(file_menu
, Menu_Test2
, _T("&Checkable"),
143 _T("checkable item"), wxITEM_CHECK
);
144 pItem
->SetFont(*wxSMALL_FONT
);
145 file_menu
->Append(pItem
);
146 file_menu
->Check(Menu_Test2
, TRUE
);
148 pItem
= new wxMenuItem(file_menu
, Menu_Test3
, _T("&Disabled"), _T("disabled item"));
149 pItem
->SetFont(*wxNORMAL_FONT
);
150 file_menu
->Append(pItem
);
151 file_menu
->Enable(Menu_Test3
, FALSE
);
153 file_menu
->AppendSeparator();
155 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap
, _T("&Bell"),
156 _T("check/uncheck me!"), wxITEM_CHECK
);
157 pItem
->SetFont(fontBmp
);
158 pItem
->SetBitmaps(bmpBell
);
159 file_menu
->Append(pItem
);
161 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap2
, _T("So&und"),
162 _T("icon changes!"), wxITEM_CHECK
);
163 pItem
->SetFont(fontBmp
);
164 pItem
->SetBitmaps(bmpSound
, bmpNoSound
);
165 file_menu
->Append(pItem
);
167 file_menu
->AppendSeparator();
169 pItem
= new wxMenuItem(file_menu
, Menu_Submenu
, _T("&Sub menu"), _T(""),
170 wxITEM_CHECK
, sub_menu
);
171 pItem
->SetFont(*wxSWISS_FONT
);
172 file_menu
->Append(pItem
);
174 file_menu
->AppendSeparator();
175 pItem
= new wxMenuItem(file_menu
, Menu_Toggle
, _T("&Disable/Enable\tCtrl+D"),
176 _T("enables/disables the About-Item"), wxITEM_NORMAL
);
177 pItem
->SetFont(*wxNORMAL_FONT
);
178 file_menu
->Append(pItem
);
180 // Of course Ctrl+RatherLongAccel will not work in this example:
181 pAboutItem
= new wxMenuItem(file_menu
, Menu_About
, _T("&About\tCtrl+RatherLongAccel"),
182 _T("display program information"), wxITEM_NORMAL
);
183 pAboutItem
->SetBitmap(bmpInfo
);
184 pAboutItem
->SetDisabledBitmap(bmpInfo_mono
);
185 pAboutItem
->SetOwnerDrawn(TRUE
);
186 file_menu
->Append(pAboutItem
);
188 file_menu
->AppendSeparator();
189 pItem
= new wxMenuItem(file_menu
, Menu_Quit
, _T("&Quit"), _T("Normal item"),
191 pItem
->SetFont(*wxNORMAL_FONT
);
192 file_menu
->Append(pItem
);
194 wxMenuBar
*menu_bar
= new wxMenuBar
;
196 menu_bar
->Append(file_menu
, _T("&File"));
197 SetMenuBar(menu_bar
);
200 // main frame constructor
201 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame
*frame
, wxChar
*title
,
202 int x
, int y
, int w
, int h
)
203 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
206 SetIcon(wxIcon(_T("mondrian")));
211 // create the status line
212 const int widths
[] = { -1, 60 };
214 SetStatusWidths(2, widths
);
215 SetStatusText(_T("no selection"), 0);
217 // make a panel with some controls
218 wxPanel
*pPanel
= new wxPanel(this, -1, wxPoint(0, 0),
219 wxSize(400, 200), wxTAB_TRAVERSAL
);
222 static const wxChar
* aszChoices
[] = { _T("Hello"), _T("world"), _T("and"),
223 _T("goodbye"), _T("cruel"), _T("world"),
224 _T("-------"), _T("owner-drawn"), _T("listbox") };
226 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
228 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
229 astrChoices
[ui
] = aszChoices
[ui
];
231 m_pListBox
= new wxCheckListBox
234 Control_Listbox
, // control id
235 wxPoint(10, 10), // listbox position
236 wxSize(200, 200), // listbox size
237 WXSIZEOF(aszChoices
), // number of strings
238 astrChoices
// array of strings
241 delete [] astrChoices
;
243 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 )
245 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
248 m_pListBox
->Check(2);
250 // normal (but owner-drawn) listbox
251 static const wxChar
* aszColors
[] = { _T("Red"), _T("Blue"), _T("Pink"),
252 _T("Green"), _T("Yellow"),
253 _T("Black"), _T("Violet") };
254 struct { unsigned int r
, g
, b
; } aColors
[] =
256 {255,0,0}, {0,0,255}, {255,128,192},
257 {0,255,0}, {255,255,128},
261 astrChoices
= new wxString
[WXSIZEOF(aszColors
)];
263 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
265 astrChoices
[ui
] = aszColors
[ui
];
268 wxListBox
*pListBox
= new wxListBox
271 Control_Listbox2
, // control id
272 wxPoint(220, 10), // listbox position
273 wxDefaultSize
, // listbox size
274 WXSIZEOF(aszColors
), // number of strings
275 astrChoices
, // array of strings
276 wxLB_OWNERDRAW
, // owner-drawn
277 wxDefaultValidator
, //
281 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
283 pListBox
->GetItem(ui
)->SetTextColour(wxColor(aColors
[ui
].r
,
286 // yellow on white is horrible...
289 pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(0, 0, 0));
294 delete[] astrChoices
;
299 OwnerDrawnFrame::~OwnerDrawnFrame()
303 void OwnerDrawnFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
308 void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent
& WXUNUSED(event
))
310 // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good
311 // example on how to enable and disable menuitems - this should be done with the help of
312 // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE !
313 pAboutItem
->Enable( pAboutItem
->IsEnabled() ? FALSE
: TRUE
);
316 void OwnerDrawnFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
318 wxMessageDialog
dialog(this,
319 _T("Demo of owner-drawn controls\n"),
320 _T("About wxOwnerDrawn"), wxYES_NO
| wxCANCEL
);
324 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent
& event
)
326 wxString strSelection
;
327 unsigned int nSel
= event
.GetSelection();
328 strSelection
.Printf(wxT("item %d selected (%schecked)"), nSel
,
329 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
330 SetStatusText(strSelection
);
333 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
335 wxString strSelection
;
336 strSelection
.Printf(wxT("item %d double clicked"),
337 m_pListBox
->GetSelection());
338 wxMessageDialog
dialog(this, strSelection
);
342 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent
& event
)
344 wxString strSelection
;
345 unsigned int nItem
= event
.GetInt();
346 strSelection
.Printf(wxT("item %d was %schecked"), nItem
,
347 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
348 SetStatusText(strSelection
);