]>
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"
42 #include "wx/colordlg.h"
43 #include "wx/numdlg.h"
45 #include "wx/htmllbox.h"
47 // you can also have a file containing HTML strings for testing, enable this if
49 //#define USE_HTML_FILE
51 #include "wx/textfile.h"
54 #include "../sample.xpm"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // to use wxHtmlListBox you must derive a new class from it as you must
61 // implement pure virtual OnGetItem()
62 class MyHtmlListBox
: public wxHtmlListBox
65 MyHtmlListBox(wxWindow
*parent
, bool multi
= false);
67 void SetChangeSelFg(bool change
) { m_change
= change
; }
68 void UpdateFirstItem();
71 // override this method to return data to be shown in the listbox (this is
73 virtual wxString
OnGetItem(size_t n
) const;
75 // change the appearance by overriding these functions (this is optional)
76 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
77 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
80 // flag telling us whether we should use fg colour even for the selected
84 // flag which we toggle to update the first items text in OnGetItem()
85 bool m_firstItemUpdated
;
93 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
96 class MyFrame
: public wxFrame
103 void OnQuit(wxCommandEvent
& event
);
104 void OnAbout(wxCommandEvent
& event
);
106 void OnSetMargins(wxCommandEvent
& event
);
107 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
108 void OnToggleMulti(wxCommandEvent
& event
);
109 void OnSelectAll(wxCommandEvent
& event
);
110 void OnUpdateItem(wxCommandEvent
& event
);
112 void OnSetBgCol(wxCommandEvent
& event
);
113 void OnSetSelBgCol(wxCommandEvent
& event
);
114 void OnSetSelFgCol(wxCommandEvent
& event
);
117 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
119 void OnLboxSelect(wxCommandEvent
& event
);
120 void OnLboxDClick(wxCommandEvent
& event
)
122 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
126 MyHtmlListBox
*m_hlbox
;
128 // any class wishing to process wxWidgets events must use this macro
129 DECLARE_EVENT_TABLE()
132 class MyApp
: public wxApp
135 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 // IDs for the controls and the menu commands
149 HtmlLbox_DrawSeparator
,
150 HtmlLbox_ToggleMulti
,
155 HtmlLbox_SetSelBgCol
,
156 HtmlLbox_SetSelFgCol
,
158 // it is important for the id corresponding to the "About" command to have
159 // this standard value as otherwise it won't be handled properly under Mac
160 // (where it is special and put into the "Apple" menu)
161 HtmlLbox_About
= wxID_ABOUT
164 // ----------------------------------------------------------------------------
165 // event tables and other macros for wxWidgets
166 // ----------------------------------------------------------------------------
168 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
169 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
171 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
172 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
173 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
174 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
175 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
177 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
179 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
180 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
181 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
183 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
186 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
187 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
192 // ============================================================================
194 // ============================================================================
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
202 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
203 wxDefaultPosition
, wxSize(400, 500))
205 // set the frame icon
206 SetIcon(wxIcon(sample_xpm
));
210 wxMenu
*menuFile
= new wxMenu
;
211 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
213 // create our specific menu
214 wxMenu
*menuHLbox
= new wxMenu
;
215 menuHLbox
->Append(HtmlLbox_SetMargins
,
216 _T("Set &margins...\tCtrl-G"),
217 _T("Change the margins around the items"));
218 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
219 _T("&Draw separators\tCtrl-D"),
220 _T("Toggle drawing separators between cells"));
221 menuHLbox
->AppendSeparator();
222 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
223 _T("&Multiple selection\tCtrl-M"),
224 _T("Toggle multiple selection on/off"));
225 menuHLbox
->AppendSeparator();
226 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
227 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
228 menuHLbox
->AppendSeparator();
229 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
230 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
231 _T("Set &selection background...\tCtrl-S"));
232 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
233 _T("Keep &foreground in selection\tCtrl-F"));
235 // the "About" item should be in the help menu
236 wxMenu
*helpMenu
= new wxMenu
;
237 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
239 // now append the freshly created menu to the menu bar...
240 wxMenuBar
*menuBar
= new wxMenuBar();
241 menuBar
->Append(menuFile
, _T("&File"));
242 menuBar
->Append(menuHLbox
, _T("&Listbox"));
243 menuBar
->Append(helpMenu
, _T("&Help"));
245 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
247 // ... and attach this menu bar to the frame
249 #endif // wxUSE_MENUS
252 // create a status bar just for fun (by default with 1 pane only)
254 SetStatusText(_T("Welcome to wxWidgets!"));
255 #endif // wxUSE_STATUSBAR
257 // create the child controls
258 m_hlbox
= new MyHtmlListBox(this);
259 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
260 wxDefaultPosition
, wxDefaultSize
,
262 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
265 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
266 sizer
->Add(m_hlbox
, 1, wxGROW
);
267 sizer
->Add(text
, 1, wxGROW
);
274 delete wxLog::SetActiveTarget(NULL
);
277 // ----------------------------------------------------------------------------
278 // menu event handlers
279 // ----------------------------------------------------------------------------
281 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
283 // true is to force the frame to close
287 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
289 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
291 _T("(c) 2003 Vadim Zeitlin"),
292 _T("About HtmlLbox"),
293 wxOK
| wxICON_INFORMATION
,
297 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
299 long margin
= wxGetNumberFromUser
301 _T("Enter the margins to use for the listbox items."),
303 _T("HtmlLbox: Set the margins"),
310 m_hlbox
->SetMargins(margin
, margin
);
311 m_hlbox
->RefreshAll();
315 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
317 // we need to recreate the listbox
318 wxSizer
*sizer
= GetSizer();
319 sizer
->Detach(m_hlbox
);
322 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
323 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
328 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
330 m_hlbox
->SelectAll();
333 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
335 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
338 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
340 m_hlbox
->UpdateFirstItem();
343 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
345 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
348 m_hlbox
->SetBackgroundColour(col
);
352 SetStatusText(_T("Background colour changed."));
353 #endif // wxUSE_STATUSBAR
357 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
359 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
362 m_hlbox
->SetSelectionBackground(col
);
366 SetStatusText(_T("Selection background colour changed."));
367 #endif // wxUSE_STATUSBAR
371 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
373 m_hlbox
->SetChangeSelFg(!event
.IsChecked());
377 // ----------------------------------------------------------------------------
378 // listbox event handlers
379 // ----------------------------------------------------------------------------
381 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
383 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
385 if ( m_hlbox
->HasMultipleSelection() )
390 unsigned long cookie
;
391 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
393 item
= m_hlbox
->GetNextSelected(cookie
) )
404 wxLogMessage(_T("Selected items: %s"), s
.c_str());
408 SetStatusText(wxString::Format(
409 _T("# items selected = %lu"),
410 (unsigned long)m_hlbox
->GetSelectedCount()
412 #endif // wxUSE_STATUSBAR
415 // ============================================================================
417 // ============================================================================
419 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
420 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
421 multi
? wxLB_MULTIPLE
: 0)
424 m_firstItemUpdated
= false;
430 if ( !m_file
.Open(_T("results")) )
432 wxLogError(_T("Failed to open results file"));
436 SetItemCount(m_file
.GetLineCount());
445 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
447 if ( ((MyFrame
*)GetParent())->
448 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
450 dc
.SetPen(*wxBLACK_DASHED_PEN
);
451 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
452 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
456 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
458 if ( !n
&& m_firstItemUpdated
)
460 return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
465 if ( m_file
.IsOpened() )
470 int level
= n
% 6 + 1;
471 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
472 _T("Item</font> <b>%lu</b>")
475 abs((int)n
- 192) % 256,
476 abs((int)n
- 256) % 256,
477 abs((int)n
- 128) % 256,
478 (unsigned long)n
, level
);
482 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
484 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
487 void MyHtmlListBox::UpdateFirstItem()
489 m_firstItemUpdated
= !m_firstItemUpdated
;