]>
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 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // the application icon (under Windows and OS/2 it is in resources)
59 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
60 #include "mondrian.xpm"
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // to use wxHtmlListBox you must derive a new class from it as you must
68 // implement pure virtual OnGetItem()
69 class MyHtmlListBox
: public wxHtmlListBox
72 MyHtmlListBox(wxWindow
*parent
, bool multi
= false);
74 void SetChangeSelFg(bool change
) { m_change
= change
; }
75 void UpdateFirstItem();
78 // override this method to return data to be shown in the listbox (this is
80 virtual wxString
OnGetItem(size_t n
) const;
82 // change the appearance by overriding these functions (this is optional)
83 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
84 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
87 // flag telling us whether we should use fg colour even for the selected
91 // flag which we toggle to update the first items text in OnGetItem()
92 bool m_firstItemUpdated
;
100 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
103 class MyFrame
: public wxFrame
110 void OnQuit(wxCommandEvent
& event
);
111 void OnAbout(wxCommandEvent
& event
);
113 void OnSetMargins(wxCommandEvent
& event
);
114 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
115 void OnToggleMulti(wxCommandEvent
& event
);
116 void OnSelectAll(wxCommandEvent
& event
);
117 void OnUpdateItem(wxCommandEvent
& event
);
119 void OnSetBgCol(wxCommandEvent
& event
);
120 void OnSetSelBgCol(wxCommandEvent
& event
);
121 void OnSetSelFgCol(wxCommandEvent
& event
);
124 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
126 void OnLboxSelect(wxCommandEvent
& event
);
127 void OnLboxDClick(wxCommandEvent
& event
)
129 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
133 MyHtmlListBox
*m_hlbox
;
135 // any class wishing to process wxWidgets events must use this macro
136 DECLARE_EVENT_TABLE()
139 class MyApp
: public wxApp
142 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 // IDs for the controls and the menu commands
156 HtmlLbox_DrawSeparator
,
157 HtmlLbox_ToggleMulti
,
162 HtmlLbox_SetSelBgCol
,
163 HtmlLbox_SetSelFgCol
,
165 // it is important for the id corresponding to the "About" command to have
166 // this standard value as otherwise it won't be handled properly under Mac
167 // (where it is special and put into the "Apple" menu)
168 HtmlLbox_About
= wxID_ABOUT
171 // ----------------------------------------------------------------------------
172 // event tables and other macros for wxWidgets
173 // ----------------------------------------------------------------------------
175 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
176 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
178 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
179 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
180 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
181 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
182 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
184 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
186 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
187 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
188 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
190 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
193 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
194 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
199 // ============================================================================
201 // ============================================================================
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
209 : wxFrame(NULL
, wxID_ANY
, _T("HtmlLbox wxWidgets Sample"),
210 wxDefaultPosition
, wxSize(400, 500))
212 // set the frame icon
213 SetIcon(wxICON(mondrian
));
217 wxMenu
*menuFile
= new wxMenu
;
218 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
220 // create our specific menu
221 wxMenu
*menuHLbox
= new wxMenu
;
222 menuHLbox
->Append(HtmlLbox_SetMargins
,
223 _T("Set &margins...\tCtrl-G"),
224 _T("Change the margins around the items"));
225 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
226 _T("&Draw separators\tCtrl-D"),
227 _T("Toggle drawing separators between cells"));
228 menuHLbox
->AppendSeparator();
229 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
230 _T("&Multiple selection\tCtrl-M"),
231 _T("Toggle multiple selection on/off"));
232 menuHLbox
->AppendSeparator();
233 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
234 menuHLbox
->Append(HtmlLbox_UpdateItem
, _T("Update &first item\tCtrl-U"));
235 menuHLbox
->AppendSeparator();
236 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
237 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
238 _T("Set &selection background...\tCtrl-S"));
239 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
240 _T("Keep &foreground in selection\tCtrl-F"));
242 // the "About" item should be in the help menu
243 wxMenu
*helpMenu
= new wxMenu
;
244 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
246 // now append the freshly created menu to the menu bar...
247 wxMenuBar
*menuBar
= new wxMenuBar();
248 menuBar
->Append(menuFile
, _T("&File"));
249 menuBar
->Append(menuHLbox
, _T("&Listbox"));
250 menuBar
->Append(helpMenu
, _T("&Help"));
252 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
254 // ... and attach this menu bar to the frame
256 #endif // wxUSE_MENUS
259 // create a status bar just for fun (by default with 1 pane only)
261 SetStatusText(_T("Welcome to wxWidgets!"));
262 #endif // wxUSE_STATUSBAR
264 // create the child controls
265 m_hlbox
= new MyHtmlListBox(this);
266 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
267 wxDefaultPosition
, wxDefaultSize
,
269 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
272 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
273 sizer
->Add(m_hlbox
, 1, wxGROW
);
274 sizer
->Add(text
, 1, wxGROW
);
281 delete wxLog::SetActiveTarget(NULL
);
284 // ----------------------------------------------------------------------------
285 // menu event handlers
286 // ----------------------------------------------------------------------------
288 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
290 // true is to force the frame to close
294 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
296 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
298 _T("(c) 2003 Vadim Zeitlin"),
299 _T("About HtmlLbox"),
300 wxOK
| wxICON_INFORMATION
,
304 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
306 long margin
= wxGetNumberFromUser
308 _T("Enter the margins to use for the listbox items."),
310 _T("HtmlLbox: Set the margins"),
317 m_hlbox
->SetMargins(margin
, margin
);
318 m_hlbox
->RefreshAll();
322 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
324 // we need to recreate the listbox
325 wxSizer
*sizer
= GetSizer();
326 sizer
->Detach(m_hlbox
);
329 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
330 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
335 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
337 m_hlbox
->SelectAll();
340 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
342 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
345 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
347 m_hlbox
->UpdateFirstItem();
350 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
352 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
355 m_hlbox
->SetBackgroundColour(col
);
359 SetStatusText(_T("Background colour changed."));
360 #endif // wxUSE_STATUSBAR
364 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
366 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
369 m_hlbox
->SetSelectionBackground(col
);
373 SetStatusText(_T("Selection background colour changed."));
374 #endif // wxUSE_STATUSBAR
378 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
380 m_hlbox
->SetChangeSelFg(!event
.IsChecked());
384 // ----------------------------------------------------------------------------
385 // listbox event handlers
386 // ----------------------------------------------------------------------------
388 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
390 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
392 if ( m_hlbox
->HasMultipleSelection() )
397 unsigned long cookie
;
398 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
400 item
= m_hlbox
->GetNextSelected(cookie
) )
411 wxLogMessage(_T("Selected items: %s"), s
.c_str());
415 SetStatusText(wxString::Format(
416 _T("# items selected = %lu"),
417 (unsigned long)m_hlbox
->GetSelectedCount()
419 #endif // wxUSE_STATUSBAR
422 // ============================================================================
424 // ============================================================================
426 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
427 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
428 multi
? wxLB_MULTIPLE
: 0)
431 m_firstItemUpdated
= 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;
478 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
479 _T("Item</font> <b>%lu</b>")
482 abs((int)n
- 192) % 256,
483 abs((int)n
- 256) % 256,
484 abs((int)n
- 128) % 256,
485 (unsigned long)n
, level
);
489 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
491 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
494 void MyHtmlListBox::UpdateFirstItem()
496 m_firstItemUpdated
= !m_firstItemUpdated
;