+void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
+{
+ long margin = wxGetNumberFromUser
+ (
+ _T("Enter the margins to use for the listbox items."),
+ _T("Margin: "),
+ _T("HtmlLbox: Set the margins"),
+ 0, 0, 20,
+ this
+ );
+
+ if ( margin != -1 )
+ {
+ m_hlbox->SetMargins(margin, margin);
+ m_hlbox->RefreshAll();
+ }
+}
+
+void MyFrame::OnToggleMulti(wxCommandEvent& event)
+{
+ // we need to recreate the listbox
+ wxSizer *sizer = GetSizer();
+ sizer->Detach(m_hlbox);
+ delete m_hlbox;
+
+ m_hlbox = new MyHtmlListBox(this, event.IsChecked());
+ sizer->Prepend(m_hlbox, 1, wxGROW);
+
+ sizer->Layout();
+}
+
+void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
+{
+ m_hlbox->SelectAll();
+}
+
+void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
+{
+ event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
+}
+
+void MyFrame::OnUpdateItem(wxCommandEvent& WXUNUSED(event))
+{
+ m_hlbox->UpdateFirstItem();
+}
+
+void MyFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
+{
+ wxColour col = wxGetColourFromUser(this, m_hlbox->GetBackgroundColour());
+ if ( col.Ok() )
+ {
+ m_hlbox->SetBackgroundColour(col);
+ m_hlbox->Refresh();
+
+#if wxUSE_STATUSBAR
+ SetStatusText(_T("Background colour changed."));
+#endif // wxUSE_STATUSBAR
+ }
+}
+
+void MyFrame::OnSetSelBgCol(wxCommandEvent& WXUNUSED(event))
+{
+ wxColour col = wxGetColourFromUser(this, m_hlbox->GetSelectionBackground());
+ if ( col.Ok() )
+ {
+ m_hlbox->SetSelectionBackground(col);
+ m_hlbox->Refresh();
+
+#if wxUSE_STATUSBAR
+ SetStatusText(_T("Selection background colour changed."));
+#endif // wxUSE_STATUSBAR
+ }
+}
+
+void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
+{
+ m_hlbox->SetChangeSelFg(!event.IsChecked());
+ m_hlbox->Refresh();
+}
+
+// ----------------------------------------------------------------------------
+// listbox event handlers
+// ----------------------------------------------------------------------------
+
+void MyFrame::OnLboxSelect(wxCommandEvent& event)
+{
+ wxLogMessage(_T("Listbox selection is now %ld."), event.GetInt());
+
+ if ( m_hlbox->HasMultipleSelection() )
+ {
+ wxString s;
+
+ bool first = true;
+ unsigned long cookie;
+ for ( int item = m_hlbox->GetFirstSelected(cookie);
+ item != wxNOT_FOUND;
+ item = m_hlbox->GetNextSelected(cookie) )
+ {
+ if ( first )
+ first = false;
+ else
+ s << _T(", ");
+
+ s << item;
+ }
+
+ if ( !s.empty() )
+ wxLogMessage(_T("Selected items: %s"), s.c_str());
+ }
+
+#if wxUSE_STATUSBAR
+ SetStatusText(wxString::Format(
+ _T("# items selected = %lu"),
+ (unsigned long)m_hlbox->GetSelectedCount()
+ ));
+#endif // wxUSE_STATUSBAR
+}
+
+// ============================================================================
+// MyHtmlListBox
+// ============================================================================
+
+MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
+ : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
+ multi ? wxLB_MULTIPLE : 0)
+{
+ m_change = true;
+ m_firstItemUpdated = false;
+
+
+ SetMargins(5, 5);
+
+#ifdef USE_HTML_FILE
+ if ( !m_file.Open(_T("results")) )
+ {
+ wxLogError(_T("Failed to open results file"));
+ }
+ else
+ {
+ SetItemCount(m_file.GetLineCount());
+ }
+#else
+ SetItemCount(1000);
+#endif
+
+ SetSelection(3);
+}
+
+void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const
+{
+ if ( ((MyFrame *)GetParent())->
+ GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator) )
+ {
+ dc.SetPen(*wxBLACK_DASHED_PEN);
+ dc.DrawLine(rect.x, rect.y, rect.GetRight(), rect.y);
+ dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight(), rect.GetBottom());
+ }
+}
+
+wxString MyHtmlListBox::OnGetItem(size_t n) const
+{
+ if ( !n && m_firstItemUpdated )
+ {
+ return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
+ }
+
+#ifdef USE_HTML_FILE
+ wxString s;
+ if ( m_file.IsOpened() )
+ s = m_file[n];
+
+ return s;
+#else
+ int level = n % 6 + 1;
+ return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
+ _T("Item</font> <b>%lu</b>")
+ _T("</h%d>"),
+ level,
+ abs((int)n - 192) % 256,
+ abs((int)n - 256) % 256,
+ abs((int)n - 128) % 256,
+ (unsigned long)n, level);
+#endif
+}
+
+wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
+{
+ return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
+}
+
+void MyHtmlListBox::UpdateFirstItem()
+{
+ m_firstItemUpdated = !m_firstItemUpdated;
+
+ RefreshLine(0);
+}
+