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 OnAbout (wxCommandEvent
& event
);
49 void OnListboxSelect (wxCommandEvent
& event
);
50 void OnCheckboxToggle (wxCommandEvent
& event
);
51 void OnListboxDblClick (wxCommandEvent
& event
);
52 bool OnClose () { return TRUE
; }
59 wxCheckListBox
*m_pListBox
;
66 Menu_Test1
, Menu_Test2
, Menu_Test3
,
67 Menu_Bitmap
, Menu_Bitmap2
,
68 Menu_Submenu
, Menu_Sub1
, Menu_Sub2
, Menu_Sub3
,
70 Control_Listbox
, Control_Listbox2
,
73 BEGIN_EVENT_TABLE(OwnerDrawnFrame
, wxFrame
)
74 EVT_MENU(Menu_Quit
, OwnerDrawnFrame::OnQuit
)
75 EVT_LISTBOX(Control_Listbox
, OwnerDrawnFrame::OnListboxSelect
)
76 EVT_CHECKLISTBOX(Control_Listbox
, OwnerDrawnFrame::OnCheckboxToggle
)
77 EVT_COMMAND(Control_Listbox
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
,
78 OwnerDrawnFrame::OnListboxDblClick
)
81 IMPLEMENT_APP(OwnerDrawnApp
);
83 // init our app: create windows
84 bool OwnerDrawnApp::OnInit(void)
86 OwnerDrawnFrame
*pFrame
87 = new OwnerDrawnFrame(NULL
, _T("wxWindows Ownerdraw Sample"),
95 // create the menu bar for the main frame
96 void OwnerDrawnFrame::InitMenu()
99 wxMenu
*file_menu
= new wxMenu
,
100 *sub_menu
= new wxMenu
;
102 // vars used for menu construction
104 wxFont
fontLarge(18, wxROMAN
, wxNORMAL
, wxBOLD
, FALSE
),
105 fontUlined(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, TRUE
),
106 fontItalic(12, wxMODERN
, wxITALIC
, wxBOLD
, FALSE
),
107 // should be at least of the size of bitmaps
108 fontBmp(14, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
);
110 // sorry for my artistic skills...
111 wxBitmap
bmpBell(_T("bell")), bmpSound(_T("sound")), bmpNoSound(_T("nosound"));
114 pItem
= new wxMenuItem(sub_menu
, Menu_Sub1
, _T("Submenu &first"), _T("large"));
116 pItem
->SetFont(fontLarge
);
117 sub_menu
->Append(pItem
);
119 pItem
= new wxMenuItem(sub_menu
, Menu_Sub2
, _T("Submenu &second"), _T("italic"),
121 pItem
->SetFont(fontItalic
);
122 sub_menu
->Append(pItem
);
124 pItem
= new wxMenuItem(sub_menu
, Menu_Sub3
, _T("Submenu &third"), _T("underlined"),
126 pItem
->SetFont(fontUlined
);
127 sub_menu
->Append(pItem
);
130 pItem
= new wxMenuItem(file_menu
, Menu_Test1
, _T("&Uncheckable"), _T("red item"));
131 pItem
->SetFont(*wxITALIC_FONT
);
132 pItem
->SetTextColour(wxColor(255, 0, 0));
133 pItem
->SetMarginWidth(23);
134 file_menu
->Append(pItem
);
136 pItem
= new wxMenuItem(file_menu
, Menu_Test2
, _T("&Checkable"),
137 _T("checkable item"), wxITEM_CHECK
);
138 pItem
->SetFont(*wxSMALL_FONT
);
139 file_menu
->Append(pItem
);
140 file_menu
->Check(Menu_Test2
, TRUE
);
142 pItem
= new wxMenuItem(file_menu
, Menu_Test3
, _T("&Disabled"), _T("disabled item"));
143 pItem
->SetFont(*wxNORMAL_FONT
);
144 file_menu
->Append(pItem
);
145 file_menu
->Enable(Menu_Test3
, FALSE
);
147 file_menu
->AppendSeparator();
149 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap
, _T("&Bell"),
150 _T("check/uncheck me!"), wxITEM_CHECK
);
151 pItem
->SetFont(fontBmp
);
152 pItem
->SetBitmaps(bmpBell
);
153 file_menu
->Append(pItem
);
155 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap2
, _T("So&und"),
156 _T("icon changes!"), wxITEM_CHECK
);
157 pItem
->SetFont(fontBmp
);
158 pItem
->SetBitmaps(bmpSound
, bmpNoSound
);
159 file_menu
->Append(pItem
);
161 file_menu
->AppendSeparator();
163 pItem
= new wxMenuItem(file_menu
, Menu_Submenu
, _T("&Sub menu"), _T(""),
164 wxITEM_CHECK
, sub_menu
);
165 pItem
->SetFont(*wxSWISS_FONT
);
166 file_menu
->Append(pItem
);
168 file_menu
->AppendSeparator();
169 pItem
= new wxMenuItem(file_menu
, Menu_Quit
, _T("&Quit"), _T("Normal item"),
171 pItem
->SetFont(*wxNORMAL_FONT
);
172 file_menu
->Append(pItem
);
174 wxMenuBar
*menu_bar
= new wxMenuBar
;
176 menu_bar
->Append(file_menu
, _T("&File"));
177 SetMenuBar(menu_bar
);
180 // main frame constructor
181 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame
*frame
, wxChar
*title
,
182 int x
, int y
, int w
, int h
)
183 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
186 SetIcon(wxIcon(_T("mondrian")));
191 // create the status line
192 const int widths
[] = { -1, 60 };
194 SetStatusWidths(2, widths
);
195 SetStatusText(_T("no selection"), 0);
197 // make a panel with some controls
198 wxPanel
*pPanel
= new wxPanel(this, -1, wxPoint(0, 0),
199 wxSize(400, 200), wxTAB_TRAVERSAL
);
202 static const wxChar
* aszChoices
[] = { _T("Hello"), _T("world"), _T("and"),
203 _T("goodbye"), _T("cruel"), _T("world"),
204 _T("-------"), _T("owner-drawn"), _T("listbox") };
206 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
208 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
209 astrChoices
[ui
] = aszChoices
[ui
];
211 m_pListBox
= new wxCheckListBox
214 Control_Listbox
, // control id
215 wxPoint(10, 10), // listbox position
216 wxSize(200, 200), // listbox size
217 WXSIZEOF(aszChoices
), // number of strings
218 astrChoices
// array of strings
221 delete [] astrChoices
;
223 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 )
225 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
228 m_pListBox
->Check(2);
230 // normal (but owner-drawn) listbox
231 static const wxChar
* aszColors
[] = { _T("Red"), _T("Blue"), _T("Pink"),
232 _T("Green"), _T("Yellow"),
233 _T("Black"), _T("Violet") };
234 struct { unsigned int r
, g
, b
; } aColors
[] =
236 {255,0,0}, {0,0,255}, {255,128,192},
237 {0,255,0}, {255,255,128},
241 astrChoices
= new wxString
[WXSIZEOF(aszColors
)];
243 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
245 astrChoices
[ui
] = aszColors
[ui
];
248 wxListBox
*pListBox
= new wxListBox
251 Control_Listbox2
, // control id
252 wxPoint(220, 10), // listbox position
253 wxDefaultSize
, // listbox size
254 WXSIZEOF(aszColors
), // number of strings
255 astrChoices
, // array of strings
256 wxLB_OWNERDRAW
, // owner-drawn
257 wxDefaultValidator
, //
261 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
263 pListBox
->GetItem(ui
)->SetTextColour(wxColor(aColors
[ui
].r
,
266 // yellow on white is horrible...
269 pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(0, 0, 0));
274 delete[] astrChoices
;
279 OwnerDrawnFrame::~OwnerDrawnFrame()
283 void OwnerDrawnFrame::OnQuit(wxCommandEvent
& event
)
288 void OwnerDrawnFrame::OnAbout(wxCommandEvent
& event
)
290 wxMessageDialog
dialog(this,
291 _T("Demo of owner-drawn controls\n"),
292 _T("About wxOwnerDrawn"), wxYES_NO
| wxCANCEL
);
296 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent
& event
)
298 wxString strSelection
;
299 unsigned int nSel
= event
.GetSelection();
300 strSelection
.Printf(wxT("item %d selected (%schecked)"), nSel
,
301 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
302 SetStatusText(strSelection
);
305 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent
& event
)
307 wxString strSelection
;
308 strSelection
.Printf(wxT("item %d double clicked"),
309 m_pListBox
->GetSelection());
310 wxMessageDialog
dialog(this, strSelection
);
314 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent
& event
)
316 wxString strSelection
;
317 unsigned int nItem
= event
.GetInt();
318 strSelection
.Printf(wxT("item %d was %schecked"), nItem
,
319 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
320 SetStatusText(strSelection
);