]>
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
);
119 void OnGetItemRect(wxCommandEvent
& event
);
121 void OnSetBgCol(wxCommandEvent
& event
);
122 void OnSetSelBgCol(wxCommandEvent
& event
);
123 void OnSetSelFgCol(wxCommandEvent
& event
);
125 void OnClear(wxCommandEvent
& event
);
127 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
129 void OnLboxSelect(wxCommandEvent
& event
);
130 void OnLboxDClick(wxCommandEvent
& event
)
132 wxLogMessage(_T("Listbox item %d double clicked."), event
.GetInt());
135 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
136 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
137 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
139 wxSimpleHtmlListBox
*GetSimpleBox()
140 { return wxDynamicCast(m_hlbox
, wxSimpleHtmlListBox
); }
141 MyHtmlListBox
*GetMyBox()
142 { return wxDynamicCast(m_hlbox
, MyHtmlListBox
); }
147 wxHtmlListBox
*m_hlbox
;
149 // any class wishing to process wxWidgets events must use this macro
150 DECLARE_EVENT_TABLE()
153 class MyApp
: public wxApp
156 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 // IDs for the controls and the menu commands
167 HtmlLbox_CustomBox
= 1,
172 HtmlLbox_DrawSeparator
,
173 HtmlLbox_ToggleMulti
,
176 HtmlLbox_GetItemRect
,
179 HtmlLbox_SetSelBgCol
,
180 HtmlLbox_SetSelFgCol
,
184 // it is important for the id corresponding to the "About" command to have
185 // this standard value as otherwise it won't be handled properly under Mac
186 // (where it is special and put into the "Apple" menu)
187 HtmlLbox_About
= wxID_ABOUT
190 // ----------------------------------------------------------------------------
191 // event tables and other macros for wxWidgets
192 // ----------------------------------------------------------------------------
194 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
195 EVT_MENU(HtmlLbox_CustomBox
, MyFrame::OnSimpleOrCustomBox
)
196 EVT_MENU(HtmlLbox_SimpleBox
, MyFrame::OnSimpleOrCustomBox
)
197 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
199 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
200 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
201 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
202 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
203 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
204 EVT_MENU(HtmlLbox_GetItemRect
, MyFrame::OnGetItemRect
)
206 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
208 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
209 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
210 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
212 EVT_MENU(HtmlLbox_Clear
, MyFrame::OnClear
)
214 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
217 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
218 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
221 // the HTML listbox's events
222 EVT_HTML_LINK_CLICKED(wxID_ANY
, MyFrame::OnHtmlLinkClicked
)
223 EVT_HTML_CELL_HOVER(wxID_ANY
, MyFrame::OnHtmlCellHover
)
224 EVT_HTML_CELL_CLICKED(wxID_ANY
, MyFrame::OnHtmlCellClicked
)
230 // ============================================================================
232 // ============================================================================
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
240 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
241 wxDefaultPosition
, wxSize(500, 500))
243 // set the frame icon
244 SetIcon(wxIcon(sample_xpm
));
248 wxMenu
*menuFile
= new wxMenu
;
249 menuFile
->AppendRadioItem(HtmlLbox_CustomBox
, _T("Use custom box"),
250 _T("Use a wxHtmlListBox virtual class control"));
251 menuFile
->AppendRadioItem(HtmlLbox_SimpleBox
, _T("Use simple box"),
252 _T("Use a wxSimpleHtmlListBox control"));
253 menuFile
->AppendSeparator();
254 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
256 // create our specific menu
257 wxMenu
*menuHLbox
= new wxMenu
;
258 menuHLbox
->Append(HtmlLbox_SetMargins
,
259 _T("Set &margins...\tCtrl-G"),
260 _T("Change the margins around the items"));
261 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
262 _T("&Draw separators\tCtrl-D"),
263 _T("Toggle drawing separators between cells"));
264 menuHLbox
->AppendSeparator();
265 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
266 _T("&Multiple selection\tCtrl-M"),
267 _T("Toggle multiple selection on/off"));
268 menuHLbox
->AppendSeparator();
269 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
270 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
271 menuHLbox
->Append(HtmlLbox_GetItemRect
, _T("Show &rectangle of item #10\tCtrl-R"));
272 menuHLbox
->AppendSeparator();
273 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
274 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
275 _T("Set &selection background...\tCtrl-S"));
276 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
277 _T("Keep &foreground in selection\tCtrl-F"));
279 menuHLbox
->AppendSeparator();
280 menuHLbox
->Append(HtmlLbox_Clear
, _T("&Clear\tCtrl-L"));
282 // the "About" item should be in the help menu
283 wxMenu
*helpMenu
= new wxMenu
;
284 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
286 // now append the freshly created menu to the menu bar...
287 wxMenuBar
*menuBar
= new wxMenuBar();
288 menuBar
->Append(menuFile
, _T("&File"));
289 menuBar
->Append(menuHLbox
, _T("&Listbox"));
290 menuBar
->Append(helpMenu
, _T("&Help"));
292 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
294 // ... and attach this menu bar to the frame
296 #endif // wxUSE_MENUS
299 // create a status bar just for fun (by default with 1 pane only)
301 SetStatusText(_T("Welcome to wxWidgets!"));
302 #endif // wxUSE_STATUSBAR
304 // create the child controls
306 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
307 wxDefaultPosition
, wxDefaultSize
,
309 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
312 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
313 sizer
->Add(m_hlbox
, 2, wxGROW
);
314 sizer
->Add(text
, 3, wxGROW
);
321 delete wxLog::SetActiveTarget(NULL
);
324 void MyFrame::CreateBox()
326 bool multi
= GetMenuBar()->IsChecked(HtmlLbox_ToggleMulti
);
328 if ( GetMenuBar()->IsChecked(HtmlLbox_CustomBox
) )
330 m_hlbox
= new MyHtmlListBox(this, multi
);
332 else // simple listbox
334 m_hlbox
= new wxSimpleHtmlListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
335 0, NULL
, multi
? wxLB_MULTIPLE
: 0);
337 // unlike wxHtmlListBox which is abstract, wxSimpleHtmlListBox is a
338 // concrete control and doesn't support virtual mode, this we need
339 // to add all of its items from the beginning
341 for (size_t n
= 0; n
< 1000; n
++ )
343 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
344 (unsigned char)(abs((int)n
- 256) % 256),
345 (unsigned char)(abs((int)n
- 128) % 256));
346 int level
= n
% 6 + 1;
348 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
349 _T("Item</font> <b>%lu</b>")
352 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
353 (unsigned long)n
, level
);
357 GetSimpleBox()->Append(arr
);
362 // ----------------------------------------------------------------------------
363 // menu event handlers
364 // ----------------------------------------------------------------------------
366 void MyFrame::OnSimpleOrCustomBox(wxCommandEvent
& WXUNUSED(event
))
368 wxWindow
*old
= m_hlbox
;
370 // we need to recreate the listbox
372 GetSizer()->Replace(old
, m_hlbox
);
375 GetSizer()->Layout();
379 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
381 // true is to force the frame to close
385 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
387 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
389 _T("(c) 2003 Vadim Zeitlin"),
390 _T("About HtmlLbox"),
391 wxOK
| wxICON_INFORMATION
,
395 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
397 long margin
= wxGetNumberFromUser
399 _T("Enter the margins to use for the listbox items."),
401 _T("HtmlLbox: Set the margins"),
408 m_hlbox
->SetMargins(margin
, margin
);
409 m_hlbox
->RefreshAll();
413 void MyFrame::OnToggleMulti(wxCommandEvent
& WXUNUSED(event
))
415 wxWindow
*old
= m_hlbox
;
417 // we need to recreate the listbox
419 GetSizer()->Replace(old
, m_hlbox
);
422 GetSizer()->Layout();
425 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
427 m_hlbox
->SelectAll();
430 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
432 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
435 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
438 GetMyBox()->UpdateFirstItem();
441 void MyFrame::OnGetItemRect(wxCommandEvent
& WXUNUSED(event
))
443 static const int ITEM
= 10;
444 const wxRect r
= m_hlbox
->GetItemRect(ITEM
);
445 wxLogMessage("Rect of item %d: (%d, %d)-(%d, %d)",
446 ITEM
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
449 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
451 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
454 m_hlbox
->SetBackgroundColour(col
);
458 SetStatusText(_T("Background colour changed."));
459 #endif // wxUSE_STATUSBAR
463 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
465 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
468 m_hlbox
->SetSelectionBackground(col
);
472 SetStatusText(_T("Selection background colour changed."));
473 #endif // wxUSE_STATUSBAR
477 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
481 GetMyBox()->SetChangeSelFg(!event
.IsChecked());
482 GetMyBox()->Refresh();
486 void MyFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
491 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
493 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
497 GetMyBox()->m_linkClicked
= true;
498 GetMyBox()->RefreshRow(1);
502 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
504 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
505 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
508 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
510 wxLogMessage(wxT("Click over cell %p at %d;%d"),
511 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
513 // if we don't skip the event, OnHtmlLinkClicked won't be called!
517 // ----------------------------------------------------------------------------
518 // listbox event handlers
519 // ----------------------------------------------------------------------------
521 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
523 wxLogMessage(_T("Listbox selection is now %d."), event
.GetInt());
525 if ( m_hlbox
->HasMultipleSelection() )
530 unsigned long cookie
;
531 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
533 item
= m_hlbox
->GetNextSelected(cookie
) )
544 wxLogMessage(_T("Selected items: %s"), s
.c_str());
548 SetStatusText(wxString::Format(
549 _T("# items selected = %lu"),
550 (unsigned long)m_hlbox
->GetSelectedCount()
552 #endif // wxUSE_STATUSBAR
555 // ============================================================================
557 // ============================================================================
559 IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox
, wxHtmlListBox
)
561 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
562 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
563 multi
? wxLB_MULTIPLE
: 0)
566 m_firstItemUpdated
= false;
567 m_linkClicked
= false;
573 if ( !m_file
.Open(_T("results")) )
575 wxLogError(_T("Failed to open results file"));
579 SetItemCount(m_file
.GetLineCount());
588 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
590 if ( ((MyFrame
*)GetParent())->
591 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
593 dc
.SetPen(*wxBLACK_DASHED_PEN
);
594 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
595 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
599 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
601 if ( !n
&& m_firstItemUpdated
)
603 return _T("<h1><b>Just updated</b></h1>");
608 if ( m_file
.IsOpened() )
613 int level
= n
% 6 + 1;
615 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
616 (unsigned char)(abs((int)n
- 256) % 256),
617 (unsigned char)(abs((int)n
- 128) % 256));
619 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
620 _T("Item</font> <b>%lu</b>")
623 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
624 (unsigned long)n
, level
);
627 if ( !m_linkClicked
)
628 label
+= _T("<a href='1'>Click here...</a>");
630 label
+= _T("<font color='#9999ff'>Clicked here...</font>");
637 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
639 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
642 void MyHtmlListBox::UpdateFirstItem()
644 m_firstItemUpdated
= !m_firstItemUpdated
;