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"
30 #include "wx/msw/checklst.h"
32 // Define a new application type
33 class OwnerDrawnApp
: public wxApp
39 // Define a new frame type
40 class OwnerDrawnFrame
: public wxFrame
44 OwnerDrawnFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
48 void OnQuit (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
;
67 Menu_Test1
, Menu_Test2
, Menu_Test3
,
68 Menu_Bitmap
, Menu_Bitmap2
,
69 Menu_Submenu
, Menu_Sub1
, Menu_Sub2
, Menu_Sub3
,
71 Control_Listbox
, Control_Listbox2
,
74 BEGIN_EVENT_TABLE(OwnerDrawnFrame
, wxFrame
)
75 EVT_MENU(Menu_Quit
, OwnerDrawnFrame::OnQuit
)
76 EVT_LISTBOX(Control_Listbox
, OwnerDrawnFrame::OnListboxSelect
)
77 EVT_CHECKLISTBOX(Control_Listbox
, OwnerDrawnFrame::OnCheckboxToggle
)
78 EVT_COMMAND(Control_Listbox
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
,
79 OwnerDrawnFrame::OnListboxDblClick
)
82 IMPLEMENT_APP(OwnerDrawnApp
);
84 // init our app: create windows
85 bool OwnerDrawnApp::OnInit(void)
87 wxInitAllImageHandlers();
88 OwnerDrawnFrame
*pFrame
89 = new OwnerDrawnFrame(NULL
, _T("wxWindows Ownerdraw Sample"),
97 // create the menu bar for the main frame
98 void OwnerDrawnFrame::InitMenu()
101 wxMenu
*file_menu
= new wxMenu
,
102 *sub_menu
= new wxMenu
;
104 // vars used for menu construction
106 wxFont
fontLarge(18, wxROMAN
, wxNORMAL
, wxBOLD
, FALSE
),
107 fontUlined(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, TRUE
),
108 fontItalic(12, wxMODERN
, wxITALIC
, wxBOLD
, FALSE
),
109 // should be at least of the size of bitmaps
110 fontBmp(14, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
);
112 // sorry for my artistic skills...
113 wxBitmap
bmpBell(_T("bell"));
114 wxBitmap
bmpSound(_T("sound.png"), wxBITMAP_TYPE_PNG
);
115 wxBitmap
bmpNoSound(_T("nosound.png"), wxBITMAP_TYPE_PNG
);
118 pItem
= new wxMenuItem(sub_menu
, Menu_Sub1
, _T("Submenu &first"), _T("large"));
120 pItem
->SetFont(fontLarge
);
121 sub_menu
->Append(pItem
);
123 pItem
= new wxMenuItem(sub_menu
, Menu_Sub2
, _T("Submenu &second"), _T("italic"),
125 pItem
->SetFont(fontItalic
);
126 sub_menu
->Append(pItem
);
128 pItem
= new wxMenuItem(sub_menu
, Menu_Sub3
, _T("Submenu &third"), _T("underlined"),
130 pItem
->SetFont(fontUlined
);
131 sub_menu
->Append(pItem
);
134 pItem
= new wxMenuItem(file_menu
, Menu_Test1
, _T("&Uncheckable"), _T("red item"));
135 pItem
->SetFont(*wxITALIC_FONT
);
136 pItem
->SetTextColour(wxColor(255, 0, 0));
137 pItem
->SetMarginWidth(23);
138 file_menu
->Append(pItem
);
140 pItem
= new wxMenuItem(file_menu
, Menu_Test2
, _T("&Checkable"),
141 _T("checkable item"), wxITEM_CHECK
);
142 pItem
->SetFont(*wxSMALL_FONT
);
143 file_menu
->Append(pItem
);
144 file_menu
->Check(Menu_Test2
, TRUE
);
146 pItem
= new wxMenuItem(file_menu
, Menu_Test3
, _T("&Disabled"), _T("disabled item"));
147 pItem
->SetFont(*wxNORMAL_FONT
);
148 file_menu
->Append(pItem
);
149 file_menu
->Enable(Menu_Test3
, FALSE
);
151 file_menu
->AppendSeparator();
153 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap
, _T("&Bell"),
154 _T("check/uncheck me!"), wxITEM_CHECK
);
155 pItem
->SetFont(fontBmp
);
156 pItem
->SetBitmaps(bmpBell
);
157 file_menu
->Append(pItem
);
159 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap2
, _T("So&und"),
160 _T("icon changes!"), wxITEM_CHECK
);
161 pItem
->SetFont(fontBmp
);
162 pItem
->SetBitmaps(bmpSound
, bmpNoSound
);
163 file_menu
->Append(pItem
);
165 file_menu
->AppendSeparator();
167 pItem
= new wxMenuItem(file_menu
, Menu_Submenu
, _T("&Sub menu"), _T(""),
168 wxITEM_CHECK
, sub_menu
);
169 pItem
->SetFont(*wxSWISS_FONT
);
170 file_menu
->Append(pItem
);
172 file_menu
->AppendSeparator();
173 pItem
= new wxMenuItem(file_menu
, Menu_Quit
, _T("&Quit"), _T("Normal item"),
175 pItem
->SetFont(*wxNORMAL_FONT
);
176 file_menu
->Append(pItem
);
178 wxMenuBar
*menu_bar
= new wxMenuBar
;
180 menu_bar
->Append(file_menu
, _T("&File"));
181 SetMenuBar(menu_bar
);
184 // main frame constructor
185 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame
*frame
, wxChar
*title
,
186 int x
, int y
, int w
, int h
)
187 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
190 SetIcon(wxIcon(_T("mondrian")));
195 // create the status line
196 const int widths
[] = { -1, 60 };
198 SetStatusWidths(2, widths
);
199 SetStatusText(_T("no selection"), 0);
201 // make a panel with some controls
202 wxPanel
*pPanel
= new wxPanel(this, -1, wxPoint(0, 0),
203 wxSize(400, 200), wxTAB_TRAVERSAL
);
206 static const wxChar
* aszChoices
[] = { _T("Hello"), _T("world"), _T("and"),
207 _T("goodbye"), _T("cruel"), _T("world"),
208 _T("-------"), _T("owner-drawn"), _T("listbox") };
210 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
212 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
213 astrChoices
[ui
] = aszChoices
[ui
];
215 m_pListBox
= new wxCheckListBox
218 Control_Listbox
, // control id
219 wxPoint(10, 10), // listbox position
220 wxSize(200, 200), // listbox size
221 WXSIZEOF(aszChoices
), // number of strings
222 astrChoices
// array of strings
225 delete [] astrChoices
;
227 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 )
229 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
232 m_pListBox
->Check(2);
234 // normal (but owner-drawn) listbox
235 static const wxChar
* aszColors
[] = { _T("Red"), _T("Blue"), _T("Pink"),
236 _T("Green"), _T("Yellow"),
237 _T("Black"), _T("Violet") };
238 struct { unsigned int r
, g
, b
; } aColors
[] =
240 {255,0,0}, {0,0,255}, {255,128,192},
241 {0,255,0}, {255,255,128},
245 astrChoices
= new wxString
[WXSIZEOF(aszColors
)];
247 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
249 astrChoices
[ui
] = aszColors
[ui
];
252 wxListBox
*pListBox
= new wxListBox
255 Control_Listbox2
, // control id
256 wxPoint(220, 10), // listbox position
257 wxDefaultSize
, // listbox size
258 WXSIZEOF(aszColors
), // number of strings
259 astrChoices
, // array of strings
260 wxLB_OWNERDRAW
, // owner-drawn
261 wxDefaultValidator
, //
265 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
267 pListBox
->GetItem(ui
)->SetTextColour(wxColor(aColors
[ui
].r
,
270 // yellow on white is horrible...
273 pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(0, 0, 0));
278 delete[] astrChoices
;
283 OwnerDrawnFrame::~OwnerDrawnFrame()
287 void OwnerDrawnFrame::OnQuit(wxCommandEvent
& event
)
292 void OwnerDrawnFrame::OnAbout(wxCommandEvent
& event
)
294 wxMessageDialog
dialog(this,
295 _T("Demo of owner-drawn controls\n"),
296 _T("About wxOwnerDrawn"), wxYES_NO
| wxCANCEL
);
300 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent
& event
)
302 wxString strSelection
;
303 unsigned int nSel
= event
.GetSelection();
304 strSelection
.Printf(wxT("item %d selected (%schecked)"), nSel
,
305 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
306 SetStatusText(strSelection
);
309 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent
& event
)
311 wxString strSelection
;
312 strSelection
.Printf(wxT("item %d double clicked"),
313 m_pListBox
->GetSelection());
314 wxMessageDialog
dialog(this, strSelection
);
318 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent
& event
)
320 wxString strSelection
;
321 unsigned int nItem
= event
.GetInt();
322 strSelection
.Printf(wxT("item %d was %schecked"), nItem
,
323 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
324 SetStatusText(strSelection
);