]>
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
);
125 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
127 void OnLboxSelect(wxCommandEvent
& event
);
128 void OnLboxDClick(wxCommandEvent
& event
)
130 wxLogMessage(_T("Listbox item %d double clicked."), event
.GetInt());
133 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
134 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
135 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
137 wxSimpleHtmlListBox
*GetSimpleBox()
138 { return wxDynamicCast(m_hlbox
, wxSimpleHtmlListBox
); }
139 MyHtmlListBox
*GetMyBox()
140 { return wxDynamicCast(m_hlbox
, MyHtmlListBox
); }
145 wxHtmlListBox
*m_hlbox
;
147 // any class wishing to process wxWidgets events must use this macro
148 DECLARE_EVENT_TABLE()
151 class MyApp
: public wxApp
154 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // IDs for the controls and the menu commands
165 HtmlLbox_CustomBox
= 1,
170 HtmlLbox_DrawSeparator
,
171 HtmlLbox_ToggleMulti
,
176 HtmlLbox_SetSelBgCol
,
177 HtmlLbox_SetSelFgCol
,
179 // it is important for the id corresponding to the "About" command to have
180 // this standard value as otherwise it won't be handled properly under Mac
181 // (where it is special and put into the "Apple" menu)
182 HtmlLbox_About
= wxID_ABOUT
185 // ----------------------------------------------------------------------------
186 // event tables and other macros for wxWidgets
187 // ----------------------------------------------------------------------------
189 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
190 EVT_MENU(HtmlLbox_CustomBox
, MyFrame::OnSimpleOrCustomBox
)
191 EVT_MENU(HtmlLbox_SimpleBox
, MyFrame::OnSimpleOrCustomBox
)
192 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
194 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
195 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
196 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
197 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
198 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
200 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
202 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
203 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
204 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
206 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
209 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
210 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
213 // the HTML listbox's events
214 EVT_HTML_LINK_CLICKED(wxID_ANY
, MyFrame::OnHtmlLinkClicked
)
215 EVT_HTML_CELL_HOVER(wxID_ANY
, MyFrame::OnHtmlCellHover
)
216 EVT_HTML_CELL_CLICKED(wxID_ANY
, MyFrame::OnHtmlCellClicked
)
222 // ============================================================================
224 // ============================================================================
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
232 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
233 wxDefaultPosition
, wxSize(500, 500))
235 // set the frame icon
236 SetIcon(wxIcon(sample_xpm
));
240 wxMenu
*menuFile
= new wxMenu
;
241 menuFile
->AppendRadioItem(HtmlLbox_CustomBox
, _T("Use custom box"),
242 _T("Use a wxHtmlListBox virtual class control"));
243 menuFile
->AppendRadioItem(HtmlLbox_SimpleBox
, _T("Use simple box"),
244 _T("Use a wxSimpleHtmlListBox control"));
245 menuFile
->AppendSeparator();
246 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
248 // create our specific menu
249 wxMenu
*menuHLbox
= new wxMenu
;
250 menuHLbox
->Append(HtmlLbox_SetMargins
,
251 _T("Set &margins...\tCtrl-G"),
252 _T("Change the margins around the items"));
253 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
254 _T("&Draw separators\tCtrl-D"),
255 _T("Toggle drawing separators between cells"));
256 menuHLbox
->AppendSeparator();
257 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
258 _T("&Multiple selection\tCtrl-M"),
259 _T("Toggle multiple selection on/off"));
260 menuHLbox
->AppendSeparator();
261 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
262 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
263 menuHLbox
->AppendSeparator();
264 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
265 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
266 _T("Set &selection background...\tCtrl-S"));
267 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
268 _T("Keep &foreground in selection\tCtrl-F"));
270 // the "About" item should be in the help menu
271 wxMenu
*helpMenu
= new wxMenu
;
272 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
274 // now append the freshly created menu to the menu bar...
275 wxMenuBar
*menuBar
= new wxMenuBar();
276 menuBar
->Append(menuFile
, _T("&File"));
277 menuBar
->Append(menuHLbox
, _T("&Listbox"));
278 menuBar
->Append(helpMenu
, _T("&Help"));
280 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
282 // ... and attach this menu bar to the frame
284 #endif // wxUSE_MENUS
287 // create a status bar just for fun (by default with 1 pane only)
289 SetStatusText(_T("Welcome to wxWidgets!"));
290 #endif // wxUSE_STATUSBAR
292 // create the child controls
294 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
295 wxDefaultPosition
, wxDefaultSize
,
297 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
300 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
301 sizer
->Add(m_hlbox
, 2, wxGROW
);
302 sizer
->Add(text
, 3, wxGROW
);
309 delete wxLog::SetActiveTarget(NULL
);
312 void MyFrame::CreateBox()
314 bool multi
= GetMenuBar()->IsChecked(HtmlLbox_ToggleMulti
);
316 if ( GetMenuBar()->IsChecked(HtmlLbox_CustomBox
) )
318 m_hlbox
= new MyHtmlListBox(this, multi
);
320 else // simple listbox
322 m_hlbox
= new wxSimpleHtmlListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
323 0, NULL
, multi
? wxLB_MULTIPLE
: 0);
325 // unlike wxHtmlListBox which is abstract, wxSimpleHtmlListBox is a
326 // concrete control and doesn't support virtual mode, this we need
327 // to add all of its items from the beginning
329 for (size_t n
= 0; n
< 1000; n
++ )
331 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
332 (unsigned char)(abs((int)n
- 256) % 256),
333 (unsigned char)(abs((int)n
- 128) % 256));
334 int level
= n
% 6 + 1;
336 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
337 _T("Item</font> <b>%lu</b>")
340 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
341 (unsigned long)n
, level
);
345 GetSimpleBox()->Append(arr
);
350 // ----------------------------------------------------------------------------
351 // menu event handlers
352 // ----------------------------------------------------------------------------
354 void MyFrame::OnSimpleOrCustomBox(wxCommandEvent
& WXUNUSED(event
))
356 wxWindow
*old
= m_hlbox
;
358 // we need to recreate the listbox
360 GetSizer()->Replace(old
, m_hlbox
);
363 GetSizer()->Layout();
367 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
369 // true is to force the frame to close
373 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
375 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
377 _T("(c) 2003 Vadim Zeitlin"),
378 _T("About HtmlLbox"),
379 wxOK
| wxICON_INFORMATION
,
383 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
385 long margin
= wxGetNumberFromUser
387 _T("Enter the margins to use for the listbox items."),
389 _T("HtmlLbox: Set the margins"),
396 m_hlbox
->SetMargins(margin
, margin
);
397 m_hlbox
->RefreshAll();
401 void MyFrame::OnToggleMulti(wxCommandEvent
& WXUNUSED(event
))
403 wxWindow
*old
= m_hlbox
;
405 // we need to recreate the listbox
407 GetSizer()->Replace(old
, m_hlbox
);
410 GetSizer()->Layout();
413 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
415 m_hlbox
->SelectAll();
418 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
420 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
423 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
426 GetMyBox()->UpdateFirstItem();
429 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
431 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
434 m_hlbox
->SetBackgroundColour(col
);
438 SetStatusText(_T("Background colour changed."));
439 #endif // wxUSE_STATUSBAR
443 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
445 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
448 m_hlbox
->SetSelectionBackground(col
);
452 SetStatusText(_T("Selection background colour changed."));
453 #endif // wxUSE_STATUSBAR
457 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
461 GetMyBox()->SetChangeSelFg(!event
.IsChecked());
462 GetMyBox()->Refresh();
466 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
468 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
472 GetMyBox()->m_linkClicked
= true;
473 GetMyBox()->RefreshRow(1);
477 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
479 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
480 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
483 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
485 wxLogMessage(wxT("Click over cell %p at %d;%d"),
486 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
488 // if we don't skip the event, OnHtmlLinkClicked won't be called!
492 // ----------------------------------------------------------------------------
493 // listbox event handlers
494 // ----------------------------------------------------------------------------
496 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
498 wxLogMessage(_T("Listbox selection is now %d."), event
.GetInt());
500 if ( m_hlbox
->HasMultipleSelection() )
505 unsigned long cookie
;
506 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
508 item
= m_hlbox
->GetNextSelected(cookie
) )
519 wxLogMessage(_T("Selected items: %s"), s
.c_str());
523 SetStatusText(wxString::Format(
524 _T("# items selected = %lu"),
525 (unsigned long)m_hlbox
->GetSelectedCount()
527 #endif // wxUSE_STATUSBAR
530 // ============================================================================
532 // ============================================================================
534 IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox
, wxHtmlListBox
)
536 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
537 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
538 multi
? wxLB_MULTIPLE
: 0)
541 m_firstItemUpdated
= false;
542 m_linkClicked
= false;
548 if ( !m_file
.Open(_T("results")) )
550 wxLogError(_T("Failed to open results file"));
554 SetItemCount(m_file
.GetLineCount());
563 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
565 if ( ((MyFrame
*)GetParent())->
566 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
568 dc
.SetPen(*wxBLACK_DASHED_PEN
);
569 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
570 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
574 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
576 if ( !n
&& m_firstItemUpdated
)
578 return _T("<h1><b>Just updated</b></h1>");
583 if ( m_file
.IsOpened() )
588 int level
= n
% 6 + 1;
590 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
591 (unsigned char)(abs((int)n
- 256) % 256),
592 (unsigned char)(abs((int)n
- 128) % 256));
594 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
595 _T("Item</font> <b>%lu</b>")
598 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
599 (unsigned long)n
, level
);
602 if ( !m_linkClicked
)
603 label
+= _T("<a href='1'>Click here...</a>");
605 label
+= _T("<font color='#9999ff'>Clicked here...</font>");
612 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
614 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
617 void MyHtmlListBox::UpdateFirstItem()
619 m_firstItemUpdated
= !m_firstItemUpdated
;