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
, char *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
= new OwnerDrawnFrame(NULL
, "wxWindows Ownerdraw Sample",
93 // create the menu bar for the main frame
94 void OwnerDrawnFrame::InitMenu()
97 wxMenu
*file_menu
= new wxMenu
,
98 *sub_menu
= new wxMenu
;
100 // vars used for menu construction
102 wxFont
fontLarge(18, wxROMAN
, wxNORMAL
, wxBOLD
, FALSE
),
103 fontUlined(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, TRUE
),
104 fontItalic(12, wxMODERN
, wxITALIC
, wxBOLD
, FALSE
),
105 // should be at least of the size of bitmaps
106 fontBmp(14, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
);
108 // sorry for my artistic skills...
109 wxBitmap
bmpBell("bell"), bmpSound("sound"), bmpNoSound("nosound");
112 pItem
= new wxMenuItem(sub_menu
, Menu_Sub1
, "Submenu &first", "large", TRUE
);
113 pItem
->SetFont(fontLarge
);
114 sub_menu
->Append(pItem
);
116 pItem
= new wxMenuItem(sub_menu
, Menu_Sub2
, "Submenu &second", "italic", TRUE
);
117 pItem
->SetFont(fontItalic
);
118 sub_menu
->Append(pItem
);
120 pItem
= new wxMenuItem(sub_menu
, Menu_Sub3
, "Submenu &third", "underlined", TRUE
);
121 pItem
->SetFont(fontUlined
);
122 sub_menu
->Append(pItem
);
125 pItem
= new wxMenuItem(file_menu
, Menu_Test1
, "&Uncheckable", "red item");
126 pItem
->SetFont(*wxITALIC_FONT
);
127 pItem
->SetTextColour(wxColor(255, 0, 0));
128 pItem
->SetMarginWidth(23);
129 file_menu
->Append(pItem
);
131 pItem
= new wxMenuItem(file_menu
, Menu_Test2
, "&Checkable", "checkable item", TRUE
);
132 pItem
->SetFont(*wxSMALL_FONT
);
133 file_menu
->Append(pItem
);
134 file_menu
->Check(Menu_Test2
, TRUE
);
136 pItem
= new wxMenuItem(file_menu
, Menu_Test3
, "&Disabled", "disabled item");
137 pItem
->SetFont(*wxNORMAL_FONT
);
138 file_menu
->Append(pItem
);
139 file_menu
->Enable(Menu_Test3
, FALSE
);
141 file_menu
->AppendSeparator();
143 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap
, "&Bell", "check/uncheck me!", TRUE
);
144 pItem
->SetFont(fontBmp
);
145 pItem
->SetBitmaps(bmpBell
);
146 file_menu
->Append(pItem
);
148 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap2
, "So&und", "icon changes!", TRUE
);
149 pItem
->SetFont(fontBmp
);
150 pItem
->SetBitmaps(bmpSound
, bmpNoSound
);
151 file_menu
->Append(pItem
);
153 file_menu
->AppendSeparator();
155 pItem
= new wxMenuItem(file_menu
, Menu_Submenu
, "&Sub menu", "", TRUE
, sub_menu
);
156 pItem
->SetFont(*wxSWISS_FONT
);
157 file_menu
->Append(pItem
);
159 file_menu
->AppendSeparator();
160 file_menu
->Append(Menu_Quit
, "&Quit", "Normal item");
162 wxMenuBar
*menu_bar
= new wxMenuBar
;
164 menu_bar
->Append(file_menu
, "&File");
165 SetMenuBar(menu_bar
);
168 // main frame constructor
169 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
)
170 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
173 SetIcon(wxIcon("mondrian"));
178 // create the status line
179 const int widths
[] = { -1, 60 };
181 SetStatusWidths(2, widths
);
182 SetStatusText("no selection", 0);
184 // make a panel with some controls
185 wxPanel
*pPanel
= new wxPanel(this, -1, wxPoint(0, 0),
186 wxSize(400, 200), wxTAB_TRAVERSAL
);
189 static const char* aszChoices
[] = { "Hello", "world", "and",
190 "goodbye", "cruel", "world",
191 "-------", "owner-drawn", "listbox" };
193 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
195 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
196 astrChoices
[ui
] = aszChoices
[ui
];
198 m_pListBox
= new wxCheckListBox
201 Control_Listbox
, // control id
202 wxPoint(10, 10), // listbox poistion
203 wxSize(200, 200), // listbox size
204 WXSIZEOF(aszChoices
), // number of strings
205 astrChoices
// array of strings
208 delete [] astrChoices
;
210 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
211 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
214 m_pListBox
->Check(2);
216 // normal (but owner-drawn) listbox
217 static const char* aszColors
[] = { "Red", "Blue", "Pink",
220 struct { unsigned int r
, g
, b
; } aColors
[] = { {255,0,0}, {0,0,255}, {255,128,192},
221 {0,255,0}, {255,255,128},
222 {0,0,0}, {128,0,255} };
223 astrChoices
= new wxString
[WXSIZEOF(aszColors
)];
224 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
225 astrChoices
[ui
] = aszColors
[ui
];
227 wxListBox
*pListBox
= new wxListBox
230 Control_Listbox2
, // control id
231 wxPoint(220, 10), // listbox poistion
232 wxDefaultSize
, // listbox size
233 WXSIZEOF(aszColors
), // number of strings
234 astrChoices
, // array of strings
235 wxLB_OWNERDRAW
, // owner-drawn
236 wxDefaultValidator
, //
240 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ ) {
241 pListBox
->GetItem(ui
)->SetTextColour(wxColor(aColors
[ui
].r
,
244 // yellow on white is horrible...
246 pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(0, 0, 0));
250 delete[] astrChoices
;
255 OwnerDrawnFrame::~OwnerDrawnFrame()
259 void OwnerDrawnFrame::OnQuit(wxCommandEvent
& event
)
264 void OwnerDrawnFrame::OnAbout(wxCommandEvent
& event
)
266 wxMessageDialog
dialog(this,
267 "Demo of owner-drawn controls\n",
268 "About wxOwnerDrawn", wxYES_NO
| wxCANCEL
);
272 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent
& event
)
274 wxString strSelection
;
275 unsigned int nSel
= event
.GetSelection();
276 strSelection
.sprintf(wxT("item %d selected (%schecked)"), nSel
,
277 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
278 SetStatusText(strSelection
);
281 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent
& event
)
283 wxString strSelection
;
284 strSelection
.sprintf(wxT("item %d double clicked"),
285 m_pListBox
->GetSelection());
286 wxMessageDialog
dialog(this, strSelection
);
290 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent
& event
)
292 wxString strSelection
;
293 unsigned int nItem
= event
.GetInt();
294 strSelection
.sprintf(wxT("item %d was %schecked"), nItem
,
295 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
296 SetStatusText(strSelection
);