]>
git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HtmlLbox wxWindows sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.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"
44 #include "wx/htmllbox.h"
46 // you can also have a file containing HTML strings for testing, enable this if
48 //#define USE_HTML_FILE
50 #include "wx/textfile.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // the application icon (under Windows and OS/2 it is in resources)
58 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
59 #include "mondrian.xpm"
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // to use wxHtmlListBox you must derive a new class from it as you must
67 // implement pure virtual OnGetItem()
68 class MyHtmlListBox
: public wxHtmlListBox
71 MyHtmlListBox(wxWindow
*parent
, bool multi
= false);
73 void SetChangeSelFg(bool change
) { m_change
= change
; }
76 virtual wxString
OnGetItem(size_t n
) const;
78 // change the appearance by overriding these functions
79 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
80 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
88 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
91 class MyFrame
: public wxFrame
98 void OnQuit(wxCommandEvent
& event
);
99 void OnAbout(wxCommandEvent
& event
);
101 void OnSetMargins(wxCommandEvent
& event
);
102 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
103 void OnToggleMulti(wxCommandEvent
& event
);
104 void OnSelectAll(wxCommandEvent
& event
);
106 void OnSetBgCol(wxCommandEvent
& event
);
107 void OnSetSelBgCol(wxCommandEvent
& event
);
108 void OnSetSelFgCol(wxCommandEvent
& event
);
111 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
113 void OnLboxSelect(wxCommandEvent
& event
);
114 void OnLboxDClick(wxCommandEvent
& event
)
116 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
120 MyHtmlListBox
*m_hlbox
;
122 // any class wishing to process wxWindows events must use this macro
123 DECLARE_EVENT_TABLE()
126 class MyApp
: public wxApp
129 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE
; }
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 // IDs for the controls and the menu commands
143 HtmlLbox_DrawSeparator
,
144 HtmlLbox_ToggleMulti
,
148 HtmlLbox_SetSelBgCol
,
149 HtmlLbox_SetSelFgCol
,
151 // it is important for the id corresponding to the "About" command to have
152 // this standard value as otherwise it won't be handled properly under Mac
153 // (where it is special and put into the "Apple" menu)
154 HtmlLbox_About
= wxID_ABOUT
157 // ----------------------------------------------------------------------------
158 // event tables and other macros for wxWindows
159 // ----------------------------------------------------------------------------
161 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
162 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
164 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
165 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
166 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
167 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
169 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
171 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
172 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
173 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
175 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
178 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
179 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
184 // ============================================================================
186 // ============================================================================
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
194 : wxFrame(NULL
, -1, _T("HtmlLbox wxWindows Sample"),
195 wxDefaultPosition
, wxSize(400, 500))
197 // set the frame icon
198 SetIcon(wxICON(mondrian
));
202 wxMenu
*menuFile
= new wxMenu
;
203 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
205 // create our specific menu
206 wxMenu
*menuHLbox
= new wxMenu
;
207 menuHLbox
->Append(HtmlLbox_SetMargins
,
208 _T("Set &margins...\tCtrl-G"),
209 _T("Change the margins around the items"));
210 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
211 _T("&Draw separators\tCtrl-D"),
212 _T("Toggle drawing separators between cells"));
213 menuHLbox
->AppendSeparator();
214 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
215 _T("&Multiple selection\tCtrl-M"),
216 _T("Toggle multiple selection on/off"));
217 menuHLbox
->AppendSeparator();
218 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
219 menuHLbox
->AppendSeparator();
220 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
221 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
222 _T("Set &selection background...\tCtrl-S"));
223 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
224 _T("Keep &foreground in selection\tCtrl-F"));
226 // the "About" item should be in the help menu
227 wxMenu
*helpMenu
= new wxMenu
;
228 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
230 // now append the freshly created menu to the menu bar...
231 wxMenuBar
*menuBar
= new wxMenuBar();
232 menuBar
->Append(menuFile
, _T("&File"));
233 menuBar
->Append(menuHLbox
, _T("&Listbox"));
234 menuBar
->Append(helpMenu
, _T("&Help"));
236 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
238 // ... and attach this menu bar to the frame
240 #endif // wxUSE_MENUS
243 // create a status bar just for fun (by default with 1 pane only)
245 SetStatusText(_T("Welcome to wxWindows!"));
246 #endif // wxUSE_STATUSBAR
248 // create the child controls
249 m_hlbox
= new MyHtmlListBox(this);
250 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
251 wxDefaultPosition
, wxDefaultSize
,
253 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
256 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
257 sizer
->Add(m_hlbox
, 1, wxGROW
);
258 sizer
->Add(text
, 1, wxGROW
);
265 delete wxLog::SetActiveTarget(NULL
);
268 // ----------------------------------------------------------------------------
269 // menu event handlers
270 // ----------------------------------------------------------------------------
272 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
274 // TRUE is to force the frame to close
278 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
280 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
282 _T("© 2003 Vadim Zeitlin"),
283 _T("About HtmlLbox"),
284 wxOK
| wxICON_INFORMATION
,
288 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
290 long margin
= wxGetNumberFromUser
292 _T("Enter the margins to use for the listbox items."),
294 _T("HtmlLbox: Set the margins"),
301 m_hlbox
->SetMargins(margin
, margin
);
302 m_hlbox
->RefreshAll();
306 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
308 // we need to recreate the listbox
309 wxSizer
*sizer
= GetSizer();
310 sizer
->Detach(m_hlbox
);
313 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
314 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
319 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
321 m_hlbox
->SelectAll();
324 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
326 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
329 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
331 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
334 m_hlbox
->SetBackgroundColour(col
);
337 SetStatusText(_T("Background colour changed."));
341 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
343 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
346 m_hlbox
->SetSelectionBackground(col
);
349 SetStatusText(_T("Selection background colour changed."));
353 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
355 m_hlbox
->SetChangeSelFg(!event
.IsChecked());
359 // ----------------------------------------------------------------------------
360 // listbox event handlers
361 // ----------------------------------------------------------------------------
363 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
365 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
367 if ( m_hlbox
->HasMultipleSelection() )
372 unsigned long cookie
;
373 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
375 item
= m_hlbox
->GetNextSelected(cookie
) )
386 wxLogMessage(_T("Selected items: %s"), s
.c_str());
389 SetStatusText(wxString::Format(
390 _T("# items selected = %lu"),
391 (unsigned long)m_hlbox
->GetSelectedCount()
395 // ============================================================================
397 // ============================================================================
399 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
400 : wxHtmlListBox(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
401 multi
? wxLB_MULTIPLE
: 0)
408 if ( !m_file
.Open(_T("results")) )
410 wxLogError(_T("Failed to open results file"));
414 SetItemCount(m_file
.GetLineCount());
423 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
425 if ( ((MyFrame
*)GetParent())->
426 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
428 dc
.SetPen(*wxBLACK_DASHED_PEN
);
429 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
430 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
434 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
438 if ( m_file
.IsOpened() )
443 int level
= n
% 6 + 1;
444 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
445 _T("Item</font> <b>%lu</b>")
451 (unsigned long)n
, level
);
455 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
457 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;