]>
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
66 MyHtmlListBox(wxWindow
*parent
, bool multi
= false);
68 void SetChangeSelFg(bool change
) { m_change
= change
; }
69 void UpdateFirstItem();
72 // override this method to return data to be shown in the listbox (this is
74 virtual wxString
OnGetItem(size_t n
) const;
76 // change the appearance by overriding these functions (this is optional)
77 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
78 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
80 // override this method to handle mouse clicks
81 virtual void OnLinkClicked(size_t n
, const wxHtmlLinkInfo
& link
);
83 // flag telling us whether we should use fg colour even for the selected
87 // flag which we toggle to update the first items text in OnGetItem()
88 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
99 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
102 class MyFrame
: public wxFrame
109 void OnQuit(wxCommandEvent
& event
);
110 void OnAbout(wxCommandEvent
& event
);
112 void OnSetMargins(wxCommandEvent
& event
);
113 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
114 void OnToggleMulti(wxCommandEvent
& event
);
115 void OnSelectAll(wxCommandEvent
& event
);
116 void OnUpdateItem(wxCommandEvent
& event
);
118 void OnSetBgCol(wxCommandEvent
& event
);
119 void OnSetSelBgCol(wxCommandEvent
& event
);
120 void OnSetSelFgCol(wxCommandEvent
& event
);
123 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
125 void OnLboxSelect(wxCommandEvent
& event
);
126 void OnLboxDClick(wxCommandEvent
& event
)
128 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
132 MyHtmlListBox
*m_hlbox
;
134 // any class wishing to process wxWidgets events must use this macro
135 DECLARE_EVENT_TABLE()
138 class MyApp
: public wxApp
141 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 // IDs for the controls and the menu commands
155 HtmlLbox_DrawSeparator
,
156 HtmlLbox_ToggleMulti
,
161 HtmlLbox_SetSelBgCol
,
162 HtmlLbox_SetSelFgCol
,
164 // it is important for the id corresponding to the "About" command to have
165 // this standard value as otherwise it won't be handled properly under Mac
166 // (where it is special and put into the "Apple" menu)
167 HtmlLbox_About
= wxID_ABOUT
170 // ----------------------------------------------------------------------------
171 // event tables and other macros for wxWidgets
172 // ----------------------------------------------------------------------------
174 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
175 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
177 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
178 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
179 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
180 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
181 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
183 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
185 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
186 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
187 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
189 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
192 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
193 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
198 // ============================================================================
200 // ============================================================================
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
208 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
209 wxDefaultPosition
, wxSize(400, 500))
211 // set the frame icon
212 SetIcon(wxIcon(sample_xpm
));
216 wxMenu
*menuFile
= new wxMenu
;
217 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
219 // create our specific menu
220 wxMenu
*menuHLbox
= new wxMenu
;
221 menuHLbox
->Append(HtmlLbox_SetMargins
,
222 _T("Set &margins...\tCtrl-G"),
223 _T("Change the margins around the items"));
224 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
225 _T("&Draw separators\tCtrl-D"),
226 _T("Toggle drawing separators between cells"));
227 menuHLbox
->AppendSeparator();
228 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
229 _T("&Multiple selection\tCtrl-M"),
230 _T("Toggle multiple selection on/off"));
231 menuHLbox
->AppendSeparator();
232 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
233 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
234 menuHLbox
->AppendSeparator();
235 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
236 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
237 _T("Set &selection background...\tCtrl-S"));
238 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
239 _T("Keep &foreground in selection\tCtrl-F"));
241 // the "About" item should be in the help menu
242 wxMenu
*helpMenu
= new wxMenu
;
243 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
245 // now append the freshly created menu to the menu bar...
246 wxMenuBar
*menuBar
= new wxMenuBar();
247 menuBar
->Append(menuFile
, _T("&File"));
248 menuBar
->Append(menuHLbox
, _T("&Listbox"));
249 menuBar
->Append(helpMenu
, _T("&Help"));
251 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
253 // ... and attach this menu bar to the frame
255 #endif // wxUSE_MENUS
258 // create a status bar just for fun (by default with 1 pane only)
260 SetStatusText(_T("Welcome to wxWidgets!"));
261 #endif // wxUSE_STATUSBAR
263 // create the child controls
264 m_hlbox
= new MyHtmlListBox(this);
265 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
266 wxDefaultPosition
, wxDefaultSize
,
268 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
271 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
272 sizer
->Add(m_hlbox
, 1, wxGROW
);
273 sizer
->Add(text
, 1, wxGROW
);
280 delete wxLog::SetActiveTarget(NULL
);
283 // ----------------------------------------------------------------------------
284 // menu event handlers
285 // ----------------------------------------------------------------------------
287 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
289 // true is to force the frame to close
293 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
295 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
297 _T("(c) 2003 Vadim Zeitlin"),
298 _T("About HtmlLbox"),
299 wxOK
| wxICON_INFORMATION
,
303 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
305 long margin
= wxGetNumberFromUser
307 _T("Enter the margins to use for the listbox items."),
309 _T("HtmlLbox: Set the margins"),
316 m_hlbox
->SetMargins(margin
, margin
);
317 m_hlbox
->RefreshAll();
321 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
323 // we need to recreate the listbox
324 wxSizer
*sizer
= GetSizer();
325 sizer
->Detach(m_hlbox
);
328 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
329 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
334 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
336 m_hlbox
->SelectAll();
339 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
341 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
344 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
346 m_hlbox
->UpdateFirstItem();
349 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
351 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
354 m_hlbox
->SetBackgroundColour(col
);
358 SetStatusText(_T("Background colour changed."));
359 #endif // wxUSE_STATUSBAR
363 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
365 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
368 m_hlbox
->SetSelectionBackground(col
);
372 SetStatusText(_T("Selection background colour changed."));
373 #endif // wxUSE_STATUSBAR
377 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
379 m_hlbox
->SetChangeSelFg(!event
.IsChecked());
383 // ----------------------------------------------------------------------------
384 // listbox event handlers
385 // ----------------------------------------------------------------------------
387 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
389 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
391 if ( m_hlbox
->HasMultipleSelection() )
396 unsigned long cookie
;
397 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
399 item
= m_hlbox
->GetNextSelected(cookie
) )
410 wxLogMessage(_T("Selected items: %s"), s
.c_str());
414 SetStatusText(wxString::Format(
415 _T("# items selected = %lu"),
416 (unsigned long)m_hlbox
->GetSelectedCount()
418 #endif // wxUSE_STATUSBAR
421 // ============================================================================
423 // ============================================================================
425 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
426 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
427 multi
? wxLB_MULTIPLE
: 0)
430 m_firstItemUpdated
= false;
431 m_linkClicked
= false;
437 if ( !m_file
.Open(_T("results")) )
439 wxLogError(_T("Failed to open results file"));
443 SetItemCount(m_file
.GetLineCount());
452 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
454 if ( ((MyFrame
*)GetParent())->
455 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
457 dc
.SetPen(*wxBLACK_DASHED_PEN
);
458 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
459 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
463 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
465 if ( !n
&& m_firstItemUpdated
)
467 return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
472 if ( m_file
.IsOpened() )
477 int level
= n
% 6 + 1;
479 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
480 (unsigned char)(abs((int)n
- 256) % 256),
481 (unsigned char)(abs((int)n
- 128) % 256));
483 wxString label
= wxString::Format(_T("<h%d><font color=%s>")
484 _T("Item</font> <b>%lu</b>")
487 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
488 (unsigned long)n
, level
);
491 if ( !m_linkClicked
)
492 label
+= _T("<a href='1'>Click here...</a>");
494 label
+= _T("<font color='#9999ff'>Clicked here...</font>");
501 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
503 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
506 void MyHtmlListBox::UpdateFirstItem()
508 m_firstItemUpdated
= !m_firstItemUpdated
;
513 void MyHtmlListBox::OnLinkClicked(size_t WXUNUSED(n
),
514 const wxHtmlLinkInfo
& WXUNUSED(link
))
516 m_linkClicked
= true;