]>
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 // override this method to handle mouse clicks
82 virtual void OnLinkClicked(size_t n
, const wxHtmlLinkInfo
& link
);
84 // flag telling us whether we should use fg colour even for the selected
88 // flag which we toggle to update the first items text in OnGetItem()
89 bool m_firstItemUpdated
;
91 // flag which we toggle when the user clicks on the link in 2nd item
92 // to change 2nd item's text
100 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
101 DECLARE_DYNAMIC_CLASS(MyHtmlListBox
)
105 class MyFrame
: public wxFrame
112 void OnSimpleOrCustomBox(wxCommandEvent
& event
);
113 void OnQuit(wxCommandEvent
& event
);
114 void OnAbout(wxCommandEvent
& event
);
116 void OnSetMargins(wxCommandEvent
& event
);
117 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
118 void OnToggleMulti(wxCommandEvent
& event
);
119 void OnSelectAll(wxCommandEvent
& event
);
120 void OnUpdateItem(wxCommandEvent
& event
);
122 void OnSetBgCol(wxCommandEvent
& event
);
123 void OnSetSelBgCol(wxCommandEvent
& event
);
124 void OnSetSelFgCol(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 wxSimpleHtmlListBox
*GetSimpleBox()
136 { return wxDynamicCast(m_hlbox
, wxSimpleHtmlListBox
); }
137 MyHtmlListBox
*GetMyBox()
138 { return wxDynamicCast(m_hlbox
, MyHtmlListBox
); }
143 wxHtmlListBox
*m_hlbox
;
145 // any class wishing to process wxWidgets events must use this macro
146 DECLARE_EVENT_TABLE()
149 class MyApp
: public wxApp
152 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 // IDs for the controls and the menu commands
163 HtmlLbox_CustomBox
= 1,
168 HtmlLbox_DrawSeparator
,
169 HtmlLbox_ToggleMulti
,
174 HtmlLbox_SetSelBgCol
,
175 HtmlLbox_SetSelFgCol
,
177 // it is important for the id corresponding to the "About" command to have
178 // this standard value as otherwise it won't be handled properly under Mac
179 // (where it is special and put into the "Apple" menu)
180 HtmlLbox_About
= wxID_ABOUT
183 // ----------------------------------------------------------------------------
184 // event tables and other macros for wxWidgets
185 // ----------------------------------------------------------------------------
187 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
188 EVT_MENU(HtmlLbox_CustomBox
, MyFrame::OnSimpleOrCustomBox
)
189 EVT_MENU(HtmlLbox_SimpleBox
, MyFrame::OnSimpleOrCustomBox
)
190 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
192 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
193 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
194 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
195 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
196 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
198 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
200 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
201 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
202 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
204 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
207 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
208 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
213 // ============================================================================
215 // ============================================================================
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
223 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
224 wxDefaultPosition
, wxSize(500, 500))
226 // set the frame icon
227 SetIcon(wxIcon(sample_xpm
));
231 wxMenu
*menuFile
= new wxMenu
;
232 menuFile
->AppendRadioItem(HtmlLbox_CustomBox
, _T("Use custom box"),
233 _T("Use a wxHtmlListBox virtual class control"));
234 menuFile
->AppendRadioItem(HtmlLbox_SimpleBox
, _T("Use simple box"),
235 _T("Use a wxSimpleHtmlListBox control"));
236 menuFile
->AppendSeparator();
237 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
239 // create our specific menu
240 wxMenu
*menuHLbox
= new wxMenu
;
241 menuHLbox
->Append(HtmlLbox_SetMargins
,
242 _T("Set &margins...\tCtrl-G"),
243 _T("Change the margins around the items"));
244 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
245 _T("&Draw separators\tCtrl-D"),
246 _T("Toggle drawing separators between cells"));
247 menuHLbox
->AppendSeparator();
248 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
249 _T("&Multiple selection\tCtrl-M"),
250 _T("Toggle multiple selection on/off"));
251 menuHLbox
->AppendSeparator();
252 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
253 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
254 menuHLbox
->AppendSeparator();
255 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
256 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
257 _T("Set &selection background...\tCtrl-S"));
258 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
259 _T("Keep &foreground in selection\tCtrl-F"));
261 // the "About" item should be in the help menu
262 wxMenu
*helpMenu
= new wxMenu
;
263 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
265 // now append the freshly created menu to the menu bar...
266 wxMenuBar
*menuBar
= new wxMenuBar();
267 menuBar
->Append(menuFile
, _T("&File"));
268 menuBar
->Append(menuHLbox
, _T("&Listbox"));
269 menuBar
->Append(helpMenu
, _T("&Help"));
271 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
273 // ... and attach this menu bar to the frame
275 #endif // wxUSE_MENUS
278 // create a status bar just for fun (by default with 1 pane only)
280 SetStatusText(_T("Welcome to wxWidgets!"));
281 #endif // wxUSE_STATUSBAR
283 // create the child controls
285 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
286 wxDefaultPosition
, wxDefaultSize
,
288 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
291 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
292 sizer
->Add(m_hlbox
, 2, wxGROW
);
293 sizer
->Add(text
, 3, wxGROW
);
300 delete wxLog::SetActiveTarget(NULL
);
303 void MyFrame::CreateBox()
305 bool multi
= GetMenuBar()->IsChecked(HtmlLbox_ToggleMulti
);
307 if ( GetMenuBar()->IsChecked(HtmlLbox_CustomBox
) )
309 m_hlbox
= new MyHtmlListBox(this, multi
);
311 else // simple listbox
313 m_hlbox
= new wxSimpleHtmlListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
314 0, NULL
, multi
? wxLB_MULTIPLE
: 0);
316 // unlike wxHtmlListBox which is abstract, wxSimpleHtmlListBox is a
317 // concrete control and doesn't support virtual mode, this we need
318 // to add all of its items from the beginning
320 for (size_t n
= 0; n
< 1000; n
++ )
322 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
323 (unsigned char)(abs((int)n
- 256) % 256),
324 (unsigned char)(abs((int)n
- 128) % 256));
325 int level
= n
% 6 + 1;
327 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
328 _T("Item</font> <b>%lu</b>")
331 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
332 (unsigned long)n
, level
);
336 GetSimpleBox()->Append(arr
);
341 // ----------------------------------------------------------------------------
342 // menu event handlers
343 // ----------------------------------------------------------------------------
345 void MyFrame::OnSimpleOrCustomBox(wxCommandEvent
& WXUNUSED(event
))
347 wxWindow
*old
= m_hlbox
;
349 // we need to recreate the listbox
351 GetSizer()->Replace(old
, m_hlbox
);
354 GetSizer()->Layout();
358 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
360 // true is to force the frame to close
364 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
366 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
368 _T("(c) 2003 Vadim Zeitlin"),
369 _T("About HtmlLbox"),
370 wxOK
| wxICON_INFORMATION
,
374 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
376 long margin
= wxGetNumberFromUser
378 _T("Enter the margins to use for the listbox items."),
380 _T("HtmlLbox: Set the margins"),
387 m_hlbox
->SetMargins(margin
, margin
);
388 m_hlbox
->RefreshAll();
392 void MyFrame::OnToggleMulti(wxCommandEvent
& WXUNUSED(event
))
394 wxWindow
*old
= m_hlbox
;
396 // we need to recreate the listbox
398 GetSizer()->Replace(old
, m_hlbox
);
401 GetSizer()->Layout();
404 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
406 m_hlbox
->SelectAll();
409 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
411 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
414 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
417 GetMyBox()->UpdateFirstItem();
420 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
422 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
425 m_hlbox
->SetBackgroundColour(col
);
429 SetStatusText(_T("Background colour changed."));
430 #endif // wxUSE_STATUSBAR
434 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
436 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
439 m_hlbox
->SetSelectionBackground(col
);
443 SetStatusText(_T("Selection background colour changed."));
444 #endif // wxUSE_STATUSBAR
448 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
452 GetMyBox()->SetChangeSelFg(!event
.IsChecked());
453 GetMyBox()->Refresh();
457 // ----------------------------------------------------------------------------
458 // listbox event handlers
459 // ----------------------------------------------------------------------------
461 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
463 wxLogMessage(_T("Listbox selection is now %d."), event
.GetInt());
465 if ( m_hlbox
->HasMultipleSelection() )
470 unsigned long cookie
;
471 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
473 item
= m_hlbox
->GetNextSelected(cookie
) )
484 wxLogMessage(_T("Selected items: %s"), s
.c_str());
488 SetStatusText(wxString::Format(
489 _T("# items selected = %lu"),
490 (unsigned long)m_hlbox
->GetSelectedCount()
492 #endif // wxUSE_STATUSBAR
495 // ============================================================================
497 // ============================================================================
499 IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox
, wxHtmlListBox
)
501 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
502 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
503 multi
? wxLB_MULTIPLE
: 0)
506 m_firstItemUpdated
= false;
507 m_linkClicked
= false;
513 if ( !m_file
.Open(_T("results")) )
515 wxLogError(_T("Failed to open results file"));
519 SetItemCount(m_file
.GetLineCount());
528 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
530 if ( ((MyFrame
*)GetParent())->
531 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
533 dc
.SetPen(*wxBLACK_DASHED_PEN
);
534 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
535 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
539 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
541 if ( !n
&& m_firstItemUpdated
)
543 return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
548 if ( m_file
.IsOpened() )
553 int level
= n
% 6 + 1;
555 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
556 (unsigned char)(abs((int)n
- 256) % 256),
557 (unsigned char)(abs((int)n
- 128) % 256));
559 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
560 _T("Item</font> <b>%lu</b>")
563 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
564 (unsigned long)n
, level
);
567 if ( !m_linkClicked
)
568 label
+= _T("<a href='1'>Click here...</a>");
570 label
+= _T("<font color='#9999ff'>Clicked here...</font>");
577 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
579 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
582 void MyHtmlListBox::UpdateFirstItem()
584 m_firstItemUpdated
= !m_firstItemUpdated
;
589 void MyHtmlListBox::OnLinkClicked(size_t WXUNUSED(n
),
590 const wxHtmlLinkInfo
& WXUNUSED(link
))
592 m_linkClicked
= true;