]> git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
added commands to toggle multiple selection and to select all items
[wxWidgets.git] / samples / htlbox / htlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmllbox.cpp
3 // Purpose: HtmlLbox wxWindows sample
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.05.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/frame.h"
31 #include "wx/log.h"
32 #include "wx/textdlg.h"
33 #include "wx/sizer.h"
34
35 #include "wx/menu.h"
36 #include "wx/msgdlg.h"
37 #include "wx/textctrl.h"
38
39 #include "wx/dc.h"
40 #endif
41
42 #include "wx/htmllbox.h"
43
44 // you can also have a file containing HTML strings for testing, enable this if
45 // you want to use it
46 //#define USE_HTML_FILE
47 #ifdef USE_HTML_FILE
48 #include "wx/textfile.h"
49 #endif
50
51 // ----------------------------------------------------------------------------
52 // resources
53 // ----------------------------------------------------------------------------
54
55 // the application icon (under Windows and OS/2 it is in resources)
56 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
57 #include "mondrian.xpm"
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // to use wxHtmlListBox you must derive a new class from it as you must
65 // implement pure virtual OnGetItem()
66 class MyHtmlListBox : public wxHtmlListBox
67 {
68 public:
69 MyHtmlListBox(wxWindow *parent, bool multi = false)
70 : wxHtmlListBox(parent, -1, wxDefaultPosition, wxDefaultSize,
71 multi ? wxLB_MULTIPLE : 0)
72 {
73 SetMargins(5, 5);
74
75 #ifdef USE_HTML_FILE
76 if ( !m_file.Open("results") )
77 {
78 wxLogError("Failed to open results file");
79 }
80 else
81 {
82 SetItemCount(m_file.GetLineCount());
83 }
84 #else
85 SetItemCount(10);
86 #endif
87
88 if ( HasMultipleSelection() )
89 Select(3);
90 else
91 SetSelection(3);
92 }
93
94 protected:
95 virtual wxString OnGetItem(size_t n) const
96 {
97 #ifdef USE_HTML_FILE
98 wxString s;
99 if ( m_file.IsOpened() )
100 s = m_file[n];
101
102 return s;
103 #else
104 int level = n % 6 + 1;
105 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
106 _T("Item</font> <b>%lu</b>")
107 _T("</h%d>"),
108 level,
109 abs(n - 192) % 256,
110 abs(n - 256) % 256,
111 abs(n - 128) % 256,
112 (unsigned long)n, level);
113 #endif
114 }
115
116 virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
117
118 wxTextFile m_file;
119 };
120
121 class MyFrame : public wxFrame
122 {
123 public:
124 MyFrame();
125 virtual ~MyFrame();
126
127 // event handlers
128 void OnQuit(wxCommandEvent& event);
129 void OnAbout(wxCommandEvent& event);
130
131 void OnSetMargins(wxCommandEvent& event);
132 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
133 void OnToggleMulti(wxCommandEvent& event);
134 void OnSelectAll(wxCommandEvent& event);
135
136 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
137
138 void OnLboxSelect(wxCommandEvent& event);
139 void OnLboxDClick(wxCommandEvent& event)
140 {
141 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
142 }
143
144 private:
145 MyHtmlListBox *m_hlbox;
146
147 // any class wishing to process wxWindows events must use this macro
148 DECLARE_EVENT_TABLE()
149 };
150
151 class MyApp : public wxApp
152 {
153 public:
154 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE; }
155 };
156
157 // ----------------------------------------------------------------------------
158 // constants
159 // ----------------------------------------------------------------------------
160
161 // IDs for the controls and the menu commands
162 enum
163 {
164 // menu items
165 HtmlLbox_Quit = 1,
166
167 HtmlLbox_SetMargins,
168 HtmlLbox_DrawSeparator,
169 HtmlLbox_ToggleMulti,
170 HtmlLbox_SelectAll,
171
172 // it is important for the id corresponding to the "About" command to have
173 // this standard value as otherwise it won't be handled properly under Mac
174 // (where it is special and put into the "Apple" menu)
175 HtmlLbox_About = wxID_ABOUT
176 };
177
178 // ----------------------------------------------------------------------------
179 // event tables and other macros for wxWindows
180 // ----------------------------------------------------------------------------
181
182 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
183 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
184
185 EVT_MENU(HtmlLbox_SetMargins, MyFrame::OnSetMargins)
186 EVT_MENU(HtmlLbox_DrawSeparator, MyFrame::OnDrawSeparator)
187 EVT_MENU(HtmlLbox_ToggleMulti, MyFrame::OnToggleMulti)
188 EVT_MENU(HtmlLbox_SelectAll, MyFrame::OnSelectAll)
189
190 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
191
192
193 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
194
195
196 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
197 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
198 END_EVENT_TABLE()
199
200 IMPLEMENT_APP(MyApp)
201
202 // ============================================================================
203 // MyFrame
204 // ============================================================================
205
206 // ----------------------------------------------------------------------------
207 // MyFrame ctor/dtor
208 // ----------------------------------------------------------------------------
209
210 // frame constructor
211 MyFrame::MyFrame()
212 : wxFrame(NULL, -1, _T("HtmlLbox wxWindows Sample"),
213 wxDefaultPosition, wxSize(400, 500))
214 {
215 // set the frame icon
216 SetIcon(wxICON(mondrian));
217
218 #if wxUSE_MENUS
219 // create a menu bar
220 wxMenu *menuFile = new wxMenu;
221 menuFile->Append(HtmlLbox_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
222
223 // create our specific menu
224 wxMenu *menuHLbox = new wxMenu;
225 menuHLbox->Append(HtmlLbox_SetMargins,
226 _T("Set &margins...\tCtrl-G"),
227 _T("Change the margins around the items"));
228 menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator,
229 _T("Draw &separators\tCtrl-S"),
230 _T("Toggle drawing separators between cells"));
231 menuHLbox->AppendSeparator();
232 menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti,
233 _T("&Multiple selection\tCtrl-M"),
234 _T("Toggle multiple selection on/off"));
235 menuHLbox->AppendSeparator();
236 menuHLbox->Append(HtmlLbox_SelectAll, _T("Select &all items\tCtrl-A"));
237
238 // the "About" item should be in the help menu
239 wxMenu *helpMenu = new wxMenu;
240 helpMenu->Append(HtmlLbox_About, _T("&About...\tF1"), _T("Show about dialog"));
241
242 // now append the freshly created menu to the menu bar...
243 wxMenuBar *menuBar = new wxMenuBar();
244 menuBar->Append(menuFile, _T("&File"));
245 menuBar->Append(menuHLbox, _T("&Listbox"));
246 menuBar->Append(helpMenu, _T("&Help"));
247
248 menuBar->Check(HtmlLbox_DrawSeparator, true);
249
250 // ... and attach this menu bar to the frame
251 SetMenuBar(menuBar);
252 #endif // wxUSE_MENUS
253
254 #if wxUSE_STATUSBAR
255 // create a status bar just for fun (by default with 1 pane only)
256 CreateStatusBar(2);
257 SetStatusText(_T("Welcome to wxWindows!"));
258 #endif // wxUSE_STATUSBAR
259
260 // create the child controls
261 m_hlbox = new MyHtmlListBox(this);
262 wxTextCtrl *text = new wxTextCtrl(this, -1, _T(""),
263 wxDefaultPosition, wxDefaultSize,
264 wxTE_MULTILINE);
265 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
266
267 // and lay them out
268 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
269 sizer->Add(m_hlbox, 1, wxGROW);
270 sizer->Add(text, 1, wxGROW);
271
272 SetSizer(sizer);
273 }
274
275 MyFrame::~MyFrame()
276 {
277 delete wxLog::SetActiveTarget(NULL);
278 }
279
280 // ----------------------------------------------------------------------------
281 // menu event handlers
282 // ----------------------------------------------------------------------------
283
284 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
285 {
286 // TRUE is to force the frame to close
287 Close(TRUE);
288 }
289
290 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
291 {
292 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
293 _T("\n")
294 _T("© 2003 Vadim Zeitlin"),
295 _T("About HtmlLbox"),
296 wxOK | wxICON_INFORMATION,
297 this);
298 }
299
300 void MyFrame::OnSetMargins(wxCommandEvent&)
301 {
302 long margin = wxGetNumberFromUser
303 (
304 _T("Enter the margins to use for the listbox items."),
305 _T("Margin: "),
306 _T("HtmlLbox: Set the margins"),
307 0, 0, 20,
308 this
309 );
310
311 if ( margin != -1 )
312 {
313 m_hlbox->SetMargins(margin, margin);
314 m_hlbox->RefreshAll();
315 }
316 }
317
318 void MyFrame::OnToggleMulti(wxCommandEvent& event)
319 {
320 // we need to recreate the listbox
321 wxSizer *sizer = GetSizer();
322 sizer->Detach(m_hlbox);
323 delete m_hlbox;
324
325 m_hlbox = new MyHtmlListBox(this, event.IsChecked());
326 sizer->Prepend(m_hlbox, 1, wxGROW);
327
328 sizer->Layout();
329 }
330
331 void MyFrame::OnSelectAll(wxCommandEvent& event)
332 {
333 m_hlbox->SelectRange(0, m_hlbox->GetItemCount() - 1);
334 }
335
336 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
337 {
338 event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
339 }
340
341 // ----------------------------------------------------------------------------
342 // listbox event handlers
343 // ----------------------------------------------------------------------------
344
345 void MyFrame::OnLboxSelect(wxCommandEvent& event)
346 {
347 wxLogMessage(_T("Listbox selection is now %ld."), event.GetInt());
348
349 if ( m_hlbox->HasMultipleSelection() )
350 {
351 wxString s;
352
353 bool first = true;
354 unsigned long cookie;
355 for ( int item = m_hlbox->GetFirstSelected(cookie);
356 item != wxNOT_FOUND;
357 item = m_hlbox->GetNextSelected(cookie) )
358 {
359 if ( first )
360 first = false;
361 else
362 s << _T(", ");
363
364 s << item;
365 }
366
367 if ( !s.empty() )
368 wxLogMessage(_T("Selected items: %s"), s.c_str());
369 }
370
371 SetStatusText(wxString::Format(
372 _T("# items selected = %lu"),
373 (unsigned long)m_hlbox->GetSelectedCount()
374 ));
375 }
376
377 // ============================================================================
378 // MyHtmlListBox
379 // ============================================================================
380
381 void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const
382 {
383 if ( ((MyFrame *)GetParent())->
384 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator) )
385 {
386 dc.SetPen(*wxBLACK_DASHED_PEN);
387 dc.DrawLine(rect.x, rect.y, rect.GetRight(), rect.y);
388 dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight(), rect.GetBottom());
389 }
390 }
391