]> git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
added format parameter to File/DirName()
[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/colordlg.h"
43
44 #include "wx/htmllbox.h"
45
46 // you can also have a file containing HTML strings for testing, enable this if
47 // you want to use it
48 //#define USE_HTML_FILE
49 #ifdef USE_HTML_FILE
50 #include "wx/textfile.h"
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // resources
55 // ----------------------------------------------------------------------------
56
57 // the application icon (under Windows and OS/2 it is in resources)
58 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
59 #include "mondrian.xpm"
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // private classes
64 // ----------------------------------------------------------------------------
65
66 // to use wxHtmlListBox you must derive a new class from it as you must
67 // implement pure virtual OnGetItem()
68 class MyHtmlListBox : public wxHtmlListBox
69 {
70 public:
71 MyHtmlListBox(wxWindow *parent, bool multi = false);
72
73 void SetChangeSelFg(bool change) { m_change = change; }
74
75 protected:
76 virtual wxString OnGetItem(size_t n) const;
77
78 // change the appearance by overriding these functions
79 virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
80 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
81
82 bool m_change;
83
84 #ifdef USE_HTML_FILE
85 wxTextFile m_file;
86 #endif
87 };
88
89 class MyFrame : public wxFrame
90 {
91 public:
92 MyFrame();
93 virtual ~MyFrame();
94
95 // event handlers
96 void OnQuit(wxCommandEvent& event);
97 void OnAbout(wxCommandEvent& event);
98
99 void OnSetMargins(wxCommandEvent& event);
100 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
101 void OnToggleMulti(wxCommandEvent& event);
102 void OnSelectAll(wxCommandEvent& event);
103
104 void OnSetBgCol(wxCommandEvent& event);
105 void OnSetSelBgCol(wxCommandEvent& event);
106 void OnSetSelFgCol(wxCommandEvent& event);
107
108
109 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
110
111 void OnLboxSelect(wxCommandEvent& event);
112 void OnLboxDClick(wxCommandEvent& event)
113 {
114 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
115 }
116
117 private:
118 MyHtmlListBox *m_hlbox;
119
120 // any class wishing to process wxWindows events must use this macro
121 DECLARE_EVENT_TABLE()
122 };
123
124 class MyApp : public wxApp
125 {
126 public:
127 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE; }
128 };
129
130 // ----------------------------------------------------------------------------
131 // constants
132 // ----------------------------------------------------------------------------
133
134 // IDs for the controls and the menu commands
135 enum
136 {
137 // menu items
138 HtmlLbox_Quit = 1,
139
140 HtmlLbox_SetMargins,
141 HtmlLbox_DrawSeparator,
142 HtmlLbox_ToggleMulti,
143 HtmlLbox_SelectAll,
144
145 HtmlLbox_SetBgCol,
146 HtmlLbox_SetSelBgCol,
147 HtmlLbox_SetSelFgCol,
148
149 // it is important for the id corresponding to the "About" command to have
150 // this standard value as otherwise it won't be handled properly under Mac
151 // (where it is special and put into the "Apple" menu)
152 HtmlLbox_About = wxID_ABOUT
153 };
154
155 // ----------------------------------------------------------------------------
156 // event tables and other macros for wxWindows
157 // ----------------------------------------------------------------------------
158
159 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
160 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
161
162 EVT_MENU(HtmlLbox_SetMargins, MyFrame::OnSetMargins)
163 EVT_MENU(HtmlLbox_DrawSeparator, MyFrame::OnDrawSeparator)
164 EVT_MENU(HtmlLbox_ToggleMulti, MyFrame::OnToggleMulti)
165 EVT_MENU(HtmlLbox_SelectAll, MyFrame::OnSelectAll)
166
167 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
168
169 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
170 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
171 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
172
173 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
174
175
176 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
177 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
178 END_EVENT_TABLE()
179
180 IMPLEMENT_APP(MyApp)
181
182 // ============================================================================
183 // MyFrame
184 // ============================================================================
185
186 // ----------------------------------------------------------------------------
187 // MyFrame ctor/dtor
188 // ----------------------------------------------------------------------------
189
190 // frame constructor
191 MyFrame::MyFrame()
192 : wxFrame(NULL, -1, _T("HtmlLbox wxWindows Sample"),
193 wxDefaultPosition, wxSize(400, 500))
194 {
195 // set the frame icon
196 SetIcon(wxICON(mondrian));
197
198 #if wxUSE_MENUS
199 // create a menu bar
200 wxMenu *menuFile = new wxMenu;
201 menuFile->Append(HtmlLbox_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
202
203 // create our specific menu
204 wxMenu *menuHLbox = new wxMenu;
205 menuHLbox->Append(HtmlLbox_SetMargins,
206 _T("Set &margins...\tCtrl-G"),
207 _T("Change the margins around the items"));
208 menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator,
209 _T("&Draw separators\tCtrl-D"),
210 _T("Toggle drawing separators between cells"));
211 menuHLbox->AppendSeparator();
212 menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti,
213 _T("&Multiple selection\tCtrl-M"),
214 _T("Toggle multiple selection on/off"));
215 menuHLbox->AppendSeparator();
216 menuHLbox->Append(HtmlLbox_SelectAll, _T("Select &all items\tCtrl-A"));
217 menuHLbox->AppendSeparator();
218 menuHLbox->Append(HtmlLbox_SetBgCol, _T("Set &background...\tCtrl-B"));
219 menuHLbox->Append(HtmlLbox_SetSelBgCol,
220 _T("Set &selection background...\tCtrl-S"));
221 menuHLbox->AppendCheckItem(HtmlLbox_SetSelFgCol,
222 _T("Keep &foreground in selection\tCtrl-F"));
223
224 // the "About" item should be in the help menu
225 wxMenu *helpMenu = new wxMenu;
226 helpMenu->Append(HtmlLbox_About, _T("&About...\tF1"), _T("Show about dialog"));
227
228 // now append the freshly created menu to the menu bar...
229 wxMenuBar *menuBar = new wxMenuBar();
230 menuBar->Append(menuFile, _T("&File"));
231 menuBar->Append(menuHLbox, _T("&Listbox"));
232 menuBar->Append(helpMenu, _T("&Help"));
233
234 menuBar->Check(HtmlLbox_DrawSeparator, true);
235
236 // ... and attach this menu bar to the frame
237 SetMenuBar(menuBar);
238 #endif // wxUSE_MENUS
239
240 #if wxUSE_STATUSBAR
241 // create a status bar just for fun (by default with 1 pane only)
242 CreateStatusBar(2);
243 SetStatusText(_T("Welcome to wxWindows!"));
244 #endif // wxUSE_STATUSBAR
245
246 // create the child controls
247 m_hlbox = new MyHtmlListBox(this);
248 wxTextCtrl *text = new wxTextCtrl(this, -1, _T(""),
249 wxDefaultPosition, wxDefaultSize,
250 wxTE_MULTILINE);
251 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
252
253 // and lay them out
254 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
255 sizer->Add(m_hlbox, 1, wxGROW);
256 sizer->Add(text, 1, wxGROW);
257
258 SetSizer(sizer);
259 }
260
261 MyFrame::~MyFrame()
262 {
263 delete wxLog::SetActiveTarget(NULL);
264 }
265
266 // ----------------------------------------------------------------------------
267 // menu event handlers
268 // ----------------------------------------------------------------------------
269
270 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
271 {
272 // TRUE is to force the frame to close
273 Close(TRUE);
274 }
275
276 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
277 {
278 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
279 _T("\n")
280 _T("© 2003 Vadim Zeitlin"),
281 _T("About HtmlLbox"),
282 wxOK | wxICON_INFORMATION,
283 this);
284 }
285
286 void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
287 {
288 long margin = wxGetNumberFromUser
289 (
290 _T("Enter the margins to use for the listbox items."),
291 _T("Margin: "),
292 _T("HtmlLbox: Set the margins"),
293 0, 0, 20,
294 this
295 );
296
297 if ( margin != -1 )
298 {
299 m_hlbox->SetMargins(margin, margin);
300 m_hlbox->RefreshAll();
301 }
302 }
303
304 void MyFrame::OnToggleMulti(wxCommandEvent& event)
305 {
306 // we need to recreate the listbox
307 wxSizer *sizer = GetSizer();
308 sizer->Detach(m_hlbox);
309 delete m_hlbox;
310
311 m_hlbox = new MyHtmlListBox(this, event.IsChecked());
312 sizer->Prepend(m_hlbox, 1, wxGROW);
313
314 sizer->Layout();
315 }
316
317 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
318 {
319 m_hlbox->SelectAll();
320 }
321
322 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
323 {
324 event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
325 }
326
327 void MyFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
328 {
329 wxColour col = wxGetColourFromUser(this, m_hlbox->GetBackgroundColour());
330 if ( col.Ok() )
331 {
332 m_hlbox->SetBackgroundColour(col);
333 m_hlbox->Refresh();
334
335 SetStatusText(_T("Background colour changed."));
336 }
337 }
338
339 void MyFrame::OnSetSelBgCol(wxCommandEvent& WXUNUSED(event))
340 {
341 wxColour col = wxGetColourFromUser(this, m_hlbox->GetSelectionBackground());
342 if ( col.Ok() )
343 {
344 m_hlbox->SetSelectionBackground(col);
345 m_hlbox->Refresh();
346
347 SetStatusText(_T("Selection background colour changed."));
348 }
349 }
350
351 void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
352 {
353 m_hlbox->SetChangeSelFg(!event.IsChecked());
354 m_hlbox->Refresh();
355 }
356
357 // ----------------------------------------------------------------------------
358 // listbox event handlers
359 // ----------------------------------------------------------------------------
360
361 void MyFrame::OnLboxSelect(wxCommandEvent& event)
362 {
363 wxLogMessage(_T("Listbox selection is now %ld."), event.GetInt());
364
365 if ( m_hlbox->HasMultipleSelection() )
366 {
367 wxString s;
368
369 bool first = true;
370 unsigned long cookie;
371 for ( int item = m_hlbox->GetFirstSelected(cookie);
372 item != wxNOT_FOUND;
373 item = m_hlbox->GetNextSelected(cookie) )
374 {
375 if ( first )
376 first = false;
377 else
378 s << _T(", ");
379
380 s << item;
381 }
382
383 if ( !s.empty() )
384 wxLogMessage(_T("Selected items: %s"), s.c_str());
385 }
386
387 SetStatusText(wxString::Format(
388 _T("# items selected = %lu"),
389 (unsigned long)m_hlbox->GetSelectedCount()
390 ));
391 }
392
393 // ============================================================================
394 // MyHtmlListBox
395 // ============================================================================
396
397 MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
398 : wxHtmlListBox(parent, -1, wxDefaultPosition, wxDefaultSize,
399 multi ? wxLB_MULTIPLE : 0)
400 {
401 m_change = true;
402
403 SetMargins(5, 5);
404
405 #ifdef USE_HTML_FILE
406 if ( !m_file.Open("results") )
407 {
408 wxLogError("Failed to open results file");
409 }
410 else
411 {
412 SetItemCount(m_file.GetLineCount());
413 }
414 #else
415 SetItemCount(1000);
416 #endif
417
418 SetSelection(3);
419 }
420
421 void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const
422 {
423 if ( ((MyFrame *)GetParent())->
424 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator) )
425 {
426 dc.SetPen(*wxBLACK_DASHED_PEN);
427 dc.DrawLine(rect.x, rect.y, rect.GetRight(), rect.y);
428 dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight(), rect.GetBottom());
429 }
430 }
431
432 wxString MyHtmlListBox::OnGetItem(size_t n) const
433 {
434 #ifdef USE_HTML_FILE
435 wxString s;
436 if ( m_file.IsOpened() )
437 s = m_file[n];
438
439 return s;
440 #else
441 int level = n % 6 + 1;
442 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
443 _T("Item</font> <b>%lu</b>")
444 _T("</h%d>"),
445 level,
446 abs(n - 192) % 256,
447 abs(n - 256) % 256,
448 abs(n - 128) % 256,
449 (unsigned long)n, level);
450 #endif
451 }
452
453 wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
454 {
455 return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
456 }
457