]>
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;
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
;
94 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
97 class MyFrame
: public wxFrame
104 void OnQuit(wxCommandEvent
& event
);
105 void OnAbout(wxCommandEvent
& event
);
107 void OnSetMargins(wxCommandEvent
& event
);
108 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
109 void OnToggleMulti(wxCommandEvent
& event
);
110 void OnSelectAll(wxCommandEvent
& event
);
111 void OnUpdateItem(wxCommandEvent
& event
);
113 void OnSetBgCol(wxCommandEvent
& event
);
114 void OnSetSelBgCol(wxCommandEvent
& event
);
115 void OnSetSelFgCol(wxCommandEvent
& event
);
118 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
120 void OnLboxSelect(wxCommandEvent
& event
);
121 void OnLboxDClick(wxCommandEvent
& event
)
123 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
127 MyHtmlListBox
*m_hlbox
;
129 // any class wishing to process wxWidgets events must use this macro
130 DECLARE_EVENT_TABLE()
133 class MyApp
: public wxApp
136 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 // IDs for the controls and the menu commands
150 HtmlLbox_DrawSeparator
,
151 HtmlLbox_ToggleMulti
,
156 HtmlLbox_SetSelBgCol
,
157 HtmlLbox_SetSelFgCol
,
159 // it is important for the id corresponding to the "About" command to have
160 // this standard value as otherwise it won't be handled properly under Mac
161 // (where it is special and put into the "Apple" menu)
162 HtmlLbox_About
= wxID_ABOUT
165 // ----------------------------------------------------------------------------
166 // event tables and other macros for wxWidgets
167 // ----------------------------------------------------------------------------
169 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
170 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
172 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
173 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
174 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
175 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
176 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
178 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
180 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
181 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
182 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
184 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
187 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
188 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
193 // ============================================================================
195 // ============================================================================
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
203 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
204 wxDefaultPosition
, wxSize(400, 500))
206 // set the frame icon
207 SetIcon(wxIcon(sample_xpm
));
211 wxMenu
*menuFile
= new wxMenu
;
212 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
214 // create our specific menu
215 wxMenu
*menuHLbox
= new wxMenu
;
216 menuHLbox
->Append(HtmlLbox_SetMargins
,
217 _T("Set &margins...\tCtrl-G"),
218 _T("Change the margins around the items"));
219 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
220 _T("&Draw separators\tCtrl-D"),
221 _T("Toggle drawing separators between cells"));
222 menuHLbox
->AppendSeparator();
223 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
224 _T("&Multiple selection\tCtrl-M"),
225 _T("Toggle multiple selection on/off"));
226 menuHLbox
->AppendSeparator();
227 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
228 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
229 menuHLbox
->AppendSeparator();
230 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
231 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
232 _T("Set &selection background...\tCtrl-S"));
233 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
234 _T("Keep &foreground in selection\tCtrl-F"));
236 // the "About" item should be in the help menu
237 wxMenu
*helpMenu
= new wxMenu
;
238 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
240 // now append the freshly created menu to the menu bar...
241 wxMenuBar
*menuBar
= new wxMenuBar();
242 menuBar
->Append(menuFile
, _T("&File"));
243 menuBar
->Append(menuHLbox
, _T("&Listbox"));
244 menuBar
->Append(helpMenu
, _T("&Help"));
246 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
248 // ... and attach this menu bar to the frame
250 #endif // wxUSE_MENUS
253 // create a status bar just for fun (by default with 1 pane only)
255 SetStatusText(_T("Welcome to wxWidgets!"));
256 #endif // wxUSE_STATUSBAR
258 // create the child controls
259 m_hlbox
= new MyHtmlListBox(this);
260 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
261 wxDefaultPosition
, wxDefaultSize
,
263 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
266 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
267 sizer
->Add(m_hlbox
, 1, wxGROW
);
268 sizer
->Add(text
, 1, wxGROW
);
275 delete wxLog::SetActiveTarget(NULL
);
278 // ----------------------------------------------------------------------------
279 // menu event handlers
280 // ----------------------------------------------------------------------------
282 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
284 // true is to force the frame to close
288 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
290 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
292 _T("(c) 2003 Vadim Zeitlin"),
293 _T("About HtmlLbox"),
294 wxOK
| wxICON_INFORMATION
,
298 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
300 long margin
= wxGetNumberFromUser
302 _T("Enter the margins to use for the listbox items."),
304 _T("HtmlLbox: Set the margins"),
311 m_hlbox
->SetMargins(margin
, margin
);
312 m_hlbox
->RefreshAll();
316 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
318 // we need to recreate the listbox
319 wxSizer
*sizer
= GetSizer();
320 sizer
->Detach(m_hlbox
);
323 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
324 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
329 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
331 m_hlbox
->SelectAll();
334 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
336 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
339 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
341 m_hlbox
->UpdateFirstItem();
344 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
346 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
349 m_hlbox
->SetBackgroundColour(col
);
353 SetStatusText(_T("Background colour changed."));
354 #endif // wxUSE_STATUSBAR
358 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
360 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
363 m_hlbox
->SetSelectionBackground(col
);
367 SetStatusText(_T("Selection background colour changed."));
368 #endif // wxUSE_STATUSBAR
372 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
374 m_hlbox
->SetChangeSelFg(!event
.IsChecked());
378 // ----------------------------------------------------------------------------
379 // listbox event handlers
380 // ----------------------------------------------------------------------------
382 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
384 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
386 if ( m_hlbox
->HasMultipleSelection() )
391 unsigned long cookie
;
392 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
394 item
= m_hlbox
->GetNextSelected(cookie
) )
405 wxLogMessage(_T("Selected items: %s"), s
.c_str());
409 SetStatusText(wxString::Format(
410 _T("# items selected = %lu"),
411 (unsigned long)m_hlbox
->GetSelectedCount()
413 #endif // wxUSE_STATUSBAR
416 // ============================================================================
418 // ============================================================================
420 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
421 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
422 multi
? wxLB_MULTIPLE
: 0)
425 m_firstItemUpdated
= false;
431 if ( !m_file
.Open(_T("results")) )
433 wxLogError(_T("Failed to open results file"));
437 SetItemCount(m_file
.GetLineCount());
446 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
448 if ( ((MyFrame
*)GetParent())->
449 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
451 dc
.SetPen(*wxBLACK_DASHED_PEN
);
452 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
453 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
457 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
459 if ( !n
&& m_firstItemUpdated
)
461 return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
466 if ( m_file
.IsOpened() )
471 int level
= n
% 6 + 1;
472 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
473 _T("Item</font> <b>%lu</b>")
476 abs((int)n
- 192) % 256,
477 abs((int)n
- 256) % 256,
478 abs((int)n
- 128) % 256,
479 (unsigned long)n
, level
);
483 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
485 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
488 void MyHtmlListBox::UpdateFirstItem()
490 m_firstItemUpdated
= !m_firstItemUpdated
;