]>
git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HtmlLbox wxWidgets sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
32 #include "wx/textdlg.h"
36 #include "wx/msgdlg.h"
37 #include "wx/textctrl.h"
43 #include "wx/colordlg.h"
44 #include "wx/numdlg.h"
46 #include "wx/htmllbox.h"
48 // you can also have a file containing HTML strings for testing, enable this if
50 //#define USE_HTML_FILE
52 #include "wx/textfile.h"
55 #include "../sample.xpm"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // to use wxHtmlListBox you must derive a new class from it as you must
62 // implement pure virtual OnGetItem()
63 class MyHtmlListBox
: public wxHtmlListBox
67 MyHtmlListBox(wxWindow
*parent
, bool multi
= false);
69 void SetChangeSelFg(bool change
) { m_change
= change
; }
70 void UpdateFirstItem();
73 // override this method to return data to be shown in the listbox (this is
75 virtual wxString
OnGetItem(size_t n
) const;
77 // change the appearance by overriding these functions (this is optional)
78 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
79 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
81 // flag telling us whether we should use fg colour even for the selected
85 // flag which we toggle to update the first items text in OnGetItem()
86 bool m_firstItemUpdated
;
90 // flag which we toggle when the user clicks on the link in 2nd item
91 // to change 2nd item's text
98 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
99 DECLARE_DYNAMIC_CLASS(MyHtmlListBox
)
103 class MyFrame
: public wxFrame
110 void OnSimpleOrCustomBox(wxCommandEvent
& event
);
111 void OnQuit(wxCommandEvent
& event
);
112 void OnAbout(wxCommandEvent
& event
);
114 void OnSetMargins(wxCommandEvent
& event
);
115 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
116 void OnToggleMulti(wxCommandEvent
& event
);
117 void OnSelectAll(wxCommandEvent
& event
);
118 void OnUpdateItem(wxCommandEvent
& event
);
120 void OnSetBgCol(wxCommandEvent
& event
);
121 void OnSetSelBgCol(wxCommandEvent
& event
);
122 void OnSetSelFgCol(wxCommandEvent
& event
);
124 void OnClear(wxCommandEvent
& event
);
126 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
128 void OnLboxSelect(wxCommandEvent
& event
);
129 void OnLboxDClick(wxCommandEvent
& event
)
131 wxLogMessage(_T("Listbox item %d double clicked."), event
.GetInt());
134 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
135 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
136 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
138 wxSimpleHtmlListBox
*GetSimpleBox()
139 { return wxDynamicCast(m_hlbox
, wxSimpleHtmlListBox
); }
140 MyHtmlListBox
*GetMyBox()
141 { return wxDynamicCast(m_hlbox
, MyHtmlListBox
); }
146 wxHtmlListBox
*m_hlbox
;
148 // any class wishing to process wxWidgets events must use this macro
149 DECLARE_EVENT_TABLE()
152 class MyApp
: public wxApp
155 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // IDs for the controls and the menu commands
166 HtmlLbox_CustomBox
= 1,
171 HtmlLbox_DrawSeparator
,
172 HtmlLbox_ToggleMulti
,
177 HtmlLbox_SetSelBgCol
,
178 HtmlLbox_SetSelFgCol
,
182 // it is important for the id corresponding to the "About" command to have
183 // this standard value as otherwise it won't be handled properly under Mac
184 // (where it is special and put into the "Apple" menu)
185 HtmlLbox_About
= wxID_ABOUT
188 // ----------------------------------------------------------------------------
189 // event tables and other macros for wxWidgets
190 // ----------------------------------------------------------------------------
192 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
193 EVT_MENU(HtmlLbox_CustomBox
, MyFrame::OnSimpleOrCustomBox
)
194 EVT_MENU(HtmlLbox_SimpleBox
, MyFrame::OnSimpleOrCustomBox
)
195 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
197 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
198 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
199 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
200 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
201 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
203 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
205 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
206 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
207 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
209 EVT_MENU(HtmlLbox_Clear
, MyFrame::OnClear
)
211 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
214 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
215 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
218 // the HTML listbox's events
219 EVT_HTML_LINK_CLICKED(wxID_ANY
, MyFrame::OnHtmlLinkClicked
)
220 EVT_HTML_CELL_HOVER(wxID_ANY
, MyFrame::OnHtmlCellHover
)
221 EVT_HTML_CELL_CLICKED(wxID_ANY
, MyFrame::OnHtmlCellClicked
)
227 // ============================================================================
229 // ============================================================================
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
237 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
238 wxDefaultPosition
, wxSize(500, 500))
240 // set the frame icon
241 SetIcon(wxIcon(sample_xpm
));
245 wxMenu
*menuFile
= new wxMenu
;
246 menuFile
->AppendRadioItem(HtmlLbox_CustomBox
, _T("Use custom box"),
247 _T("Use a wxHtmlListBox virtual class control"));
248 menuFile
->AppendRadioItem(HtmlLbox_SimpleBox
, _T("Use simple box"),
249 _T("Use a wxSimpleHtmlListBox control"));
250 menuFile
->AppendSeparator();
251 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
253 // create our specific menu
254 wxMenu
*menuHLbox
= new wxMenu
;
255 menuHLbox
->Append(HtmlLbox_SetMargins
,
256 _T("Set &margins...\tCtrl-G"),
257 _T("Change the margins around the items"));
258 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
259 _T("&Draw separators\tCtrl-D"),
260 _T("Toggle drawing separators between cells"));
261 menuHLbox
->AppendSeparator();
262 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
263 _T("&Multiple selection\tCtrl-M"),
264 _T("Toggle multiple selection on/off"));
265 menuHLbox
->AppendSeparator();
266 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
267 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
268 menuHLbox
->AppendSeparator();
269 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
270 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
271 _T("Set &selection background...\tCtrl-S"));
272 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
273 _T("Keep &foreground in selection\tCtrl-F"));
275 menuHLbox
->AppendSeparator();
276 menuHLbox
->Append(HtmlLbox_Clear
, _T("&Clear\tCtrl-L"));
278 // the "About" item should be in the help menu
279 wxMenu
*helpMenu
= new wxMenu
;
280 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
282 // now append the freshly created menu to the menu bar...
283 wxMenuBar
*menuBar
= new wxMenuBar();
284 menuBar
->Append(menuFile
, _T("&File"));
285 menuBar
->Append(menuHLbox
, _T("&Listbox"));
286 menuBar
->Append(helpMenu
, _T("&Help"));
288 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
290 // ... and attach this menu bar to the frame
292 #endif // wxUSE_MENUS
295 // create a status bar just for fun (by default with 1 pane only)
297 SetStatusText(_T("Welcome to wxWidgets!"));
298 #endif // wxUSE_STATUSBAR
300 // create the child controls
302 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
303 wxDefaultPosition
, wxDefaultSize
,
305 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
308 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
309 sizer
->Add(m_hlbox
, 2, wxGROW
);
310 sizer
->Add(text
, 3, wxGROW
);
317 delete wxLog::SetActiveTarget(NULL
);
320 void MyFrame::CreateBox()
322 bool multi
= GetMenuBar()->IsChecked(HtmlLbox_ToggleMulti
);
324 if ( GetMenuBar()->IsChecked(HtmlLbox_CustomBox
) )
326 m_hlbox
= new MyHtmlListBox(this, multi
);
328 else // simple listbox
330 m_hlbox
= new wxSimpleHtmlListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
331 0, NULL
, multi
? wxLB_MULTIPLE
: 0);
333 // unlike wxHtmlListBox which is abstract, wxSimpleHtmlListBox is a
334 // concrete control and doesn't support virtual mode, this we need
335 // to add all of its items from the beginning
337 for (size_t n
= 0; n
< 1000; n
++ )
339 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
340 (unsigned char)(abs((int)n
- 256) % 256),
341 (unsigned char)(abs((int)n
- 128) % 256));
342 int level
= n
% 6 + 1;
344 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
345 _T("Item</font> <b>%lu</b>")
348 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
349 (unsigned long)n
, level
);
353 GetSimpleBox()->Append(arr
);
358 // ----------------------------------------------------------------------------
359 // menu event handlers
360 // ----------------------------------------------------------------------------
362 void MyFrame::OnSimpleOrCustomBox(wxCommandEvent
& WXUNUSED(event
))
364 wxWindow
*old
= m_hlbox
;
366 // we need to recreate the listbox
368 GetSizer()->Replace(old
, m_hlbox
);
371 GetSizer()->Layout();
375 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
377 // true is to force the frame to close
381 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
383 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
385 _T("(c) 2003 Vadim Zeitlin"),
386 _T("About HtmlLbox"),
387 wxOK
| wxICON_INFORMATION
,
391 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
393 long margin
= wxGetNumberFromUser
395 _T("Enter the margins to use for the listbox items."),
397 _T("HtmlLbox: Set the margins"),
404 m_hlbox
->SetMargins(margin
, margin
);
405 m_hlbox
->RefreshAll();
409 void MyFrame::OnToggleMulti(wxCommandEvent
& WXUNUSED(event
))
411 wxWindow
*old
= m_hlbox
;
413 // we need to recreate the listbox
415 GetSizer()->Replace(old
, m_hlbox
);
418 GetSizer()->Layout();
421 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
423 m_hlbox
->SelectAll();
426 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
428 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
431 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
434 GetMyBox()->UpdateFirstItem();
437 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
439 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
442 m_hlbox
->SetBackgroundColour(col
);
446 SetStatusText(_T("Background colour changed."));
447 #endif // wxUSE_STATUSBAR
451 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
453 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
456 m_hlbox
->SetSelectionBackground(col
);
460 SetStatusText(_T("Selection background colour changed."));
461 #endif // wxUSE_STATUSBAR
465 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
469 GetMyBox()->SetChangeSelFg(!event
.IsChecked());
470 GetMyBox()->Refresh();
474 void MyFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
479 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
481 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
485 GetMyBox()->m_linkClicked
= true;
486 GetMyBox()->RefreshRow(1);
490 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
492 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
493 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
496 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
498 wxLogMessage(wxT("Click over cell %p at %d;%d"),
499 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
501 // if we don't skip the event, OnHtmlLinkClicked won't be called!
505 // ----------------------------------------------------------------------------
506 // listbox event handlers
507 // ----------------------------------------------------------------------------
509 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
511 wxLogMessage(_T("Listbox selection is now %d."), event
.GetInt());
513 if ( m_hlbox
->HasMultipleSelection() )
518 unsigned long cookie
;
519 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
521 item
= m_hlbox
->GetNextSelected(cookie
) )
532 wxLogMessage(_T("Selected items: %s"), s
.c_str());
536 SetStatusText(wxString::Format(
537 _T("# items selected = %lu"),
538 (unsigned long)m_hlbox
->GetSelectedCount()
540 #endif // wxUSE_STATUSBAR
543 // ============================================================================
545 // ============================================================================
547 IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox
, wxHtmlListBox
)
549 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
550 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
551 multi
? wxLB_MULTIPLE
: 0)
554 m_firstItemUpdated
= false;
555 m_linkClicked
= false;
561 if ( !m_file
.Open(_T("results")) )
563 wxLogError(_T("Failed to open results file"));
567 SetItemCount(m_file
.GetLineCount());
576 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
578 if ( ((MyFrame
*)GetParent())->
579 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
581 dc
.SetPen(*wxBLACK_DASHED_PEN
);
582 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
583 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
587 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
589 if ( !n
&& m_firstItemUpdated
)
591 return _T("<h1><b>Just updated</b></h1>");
596 if ( m_file
.IsOpened() )
601 int level
= n
% 6 + 1;
603 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
604 (unsigned char)(abs((int)n
- 256) % 256),
605 (unsigned char)(abs((int)n
- 128) % 256));
607 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
608 _T("Item</font> <b>%lu</b>")
611 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
612 (unsigned long)n
, level
);
615 if ( !m_linkClicked
)
616 label
+= _T("<a href='1'>Click here...</a>");
618 label
+= _T("<font color='#9999ff'>Clicked here...</font>");
625 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
627 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
630 void MyHtmlListBox::UpdateFirstItem()
632 m_firstItemUpdated
= !m_firstItemUpdated
;