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