+void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
+{
+ long margin = wxGetNumberFromUser
+ (
+ wxT("Enter the margins to use for the listbox items."),
+ wxT("Margin: "),
+ wxT("HtmlLbox: Set the margins"),
+ 0, 0, 20,
+ this
+ );
+
+ if ( margin != -1 )
+ {
+ m_hlbox->SetMargins(margin, margin);
+ m_hlbox->RefreshAll();
+ }
+}
+
+void MyFrame::OnToggleMulti(wxCommandEvent& WXUNUSED(event))
+{
+ wxWindow *old = m_hlbox;
+
+ // we need to recreate the listbox
+ CreateBox();
+ GetSizer()->Replace(old, m_hlbox);
+ delete old;
+
+ GetSizer()->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))
+{
+ if (GetMyBox())
+ GetMyBox()->UpdateFirstItem();
+}
+
+void MyFrame::OnGetItemRect(wxCommandEvent& WXUNUSED(event))
+{
+ static const int ITEM = 10;
+ const wxRect r = m_hlbox->GetItemRect(ITEM);
+ wxLogMessage("Rect of item %d: (%d, %d)-(%d, %d)",
+ ITEM, r.x, r.y, r.x + r.width, r.y + r.height);
+}
+
+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(wxT("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(wxT("Selection background colour changed."));
+#endif // wxUSE_STATUSBAR
+ }
+}
+
+void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
+{
+ if (GetMyBox())
+ {
+ GetMyBox()->SetChangeSelFg(!event.IsChecked());
+ GetMyBox()->Refresh();
+ }
+}
+
+void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
+{
+ m_hlbox->Clear();
+}
+
+void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent &event)
+{
+ wxLogMessage(wxT("The url '%s' has been clicked!"), event.GetLinkInfo().GetHref().c_str());
+
+ if (GetMyBox())
+ {
+ GetMyBox()->m_linkClicked = true;
+ GetMyBox()->RefreshRow(1);
+ }
+}
+
+void MyFrame::OnHtmlCellHover(wxHtmlCellEvent &event)
+{
+ wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
+ event.GetCell(), event.GetPoint().x, event.GetPoint().y);
+}
+
+void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent &event)
+{
+ wxLogMessage(wxT("Click over cell %p at %d;%d"),
+ event.GetCell(), event.GetPoint().x, event.GetPoint().y);
+
+ // if we don't skip the event, OnHtmlLinkClicked won't be called!
+ event.Skip();
+}
+
+// ----------------------------------------------------------------------------
+// listbox event handlers
+// ----------------------------------------------------------------------------
+
+void MyFrame::OnLboxSelect(wxCommandEvent& event)
+{
+ wxLogMessage(wxT("Listbox selection is now %d."), 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 << wxT(", ");
+
+ s << item;
+ }
+
+ if ( !s.empty() )
+ {
+ wxLogMessage(wxT("Selected items: %s"), s.c_str());
+ }
+ }
+
+#if wxUSE_STATUSBAR
+ SetStatusText(wxString::Format(
+ wxT("# items selected = %lu"),
+ (unsigned long)m_hlbox->GetSelectedCount()
+ ));
+#endif // wxUSE_STATUSBAR
+}
+
+// ============================================================================
+// MyHtmlListBox
+// ============================================================================
+
+IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox, wxHtmlListBox)
+
+MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
+ : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
+ multi ? wxLB_MULTIPLE : 0)
+{
+ m_change = true;
+ m_firstItemUpdated = false;
+ m_linkClicked = false;
+
+
+ SetMargins(5, 5);
+
+#ifdef USE_HTML_FILE
+ if ( !m_file.Open(wxT("results")) )
+ {
+ wxLogError(wxT("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 wxT("<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;
+
+ wxColour clr((unsigned char)(abs((int)n - 192) % 256),
+ (unsigned char)(abs((int)n - 256) % 256),
+ (unsigned char)(abs((int)n - 128) % 256));
+
+ wxString label = wxString::Format(wxT("<h%d><font color=%s>")
+ wxT("Item</font> <b>%lu</b>")
+ wxT("</h%d>"),
+ level,
+ clr.GetAsString(wxC2S_HTML_SYNTAX).c_str(),
+ (unsigned long)n, level);
+ if ( n == 1 )
+ {
+ if ( !m_linkClicked )
+ label += wxT("<a href='1'>Click here...</a>");
+ else
+ label += wxT("<font color='#9999ff'>Clicked here...</font>");
+ }
+
+ return label;
+#endif
+}
+
+wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
+{
+ return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
+}
+
+void MyHtmlListBox::UpdateFirstItem()
+{
+ m_firstItemUpdated = !m_firstItemUpdated;
+
+ RefreshRow(0);
+}