]>
git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HtmlLbox wxWidgets sample
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
31 #include "wx/textdlg.h"
35 #include "wx/msgdlg.h"
36 #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
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 // 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
;
89 // flag which we toggle when the user clicks on the link in 2nd item
90 // to change 2nd item's text
97 wxDECLARE_NO_COPY_CLASS(MyHtmlListBox
);
98 DECLARE_DYNAMIC_CLASS(MyHtmlListBox
)
102 class MyFrame
: public wxFrame
109 void OnSimpleOrCustomBox(wxCommandEvent
& event
);
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
);
118 void OnGetItemRect(wxCommandEvent
& event
);
120 void OnSetBgCol(wxCommandEvent
& event
);
121 void OnSetSelBgCol(wxCommandEvent
& event
);
122 void OnSetSelFgCol(wxCommandEvent
& event
);
124 void OnClear(wxCommandEvent
& event
);
126 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
128 void OnLboxSelect(wxCommandEvent
& event
);
129 void OnLboxDClick(wxCommandEvent
& event
)
131 wxLogMessage(wxT("Listbox item %d double clicked."), event
.GetInt());
134 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
135 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
136 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
138 wxSimpleHtmlListBox
*GetSimpleBox()
139 { return wxDynamicCast(m_hlbox
, wxSimpleHtmlListBox
); }
140 MyHtmlListBox
*GetMyBox()
141 { return wxDynamicCast(m_hlbox
, MyHtmlListBox
); }
146 wxHtmlListBox
*m_hlbox
;
148 // any class wishing to process wxWidgets events must use this macro
149 DECLARE_EVENT_TABLE()
152 class MyApp
: public wxApp
155 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // IDs for the controls and the menu commands
166 HtmlLbox_CustomBox
= 1,
171 HtmlLbox_DrawSeparator
,
172 HtmlLbox_ToggleMulti
,
175 HtmlLbox_GetItemRect
,
178 HtmlLbox_SetSelBgCol
,
179 HtmlLbox_SetSelFgCol
,
183 // it is important for the id corresponding to the "About" command to have
184 // this standard value as otherwise it won't be handled properly under Mac
185 // (where it is special and put into the "Apple" menu)
186 HtmlLbox_About
= wxID_ABOUT
189 // ----------------------------------------------------------------------------
190 // event tables and other macros for wxWidgets
191 // ----------------------------------------------------------------------------
193 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
194 EVT_MENU(HtmlLbox_CustomBox
, MyFrame::OnSimpleOrCustomBox
)
195 EVT_MENU(HtmlLbox_SimpleBox
, MyFrame::OnSimpleOrCustomBox
)
196 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
198 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
199 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
200 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
201 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
202 EVT_MENU(HtmlLbox_UpdateItem
, MyFrame::OnUpdateItem
)
203 EVT_MENU(HtmlLbox_GetItemRect
, MyFrame::OnGetItemRect
)
205 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
207 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
208 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
209 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
211 EVT_MENU(HtmlLbox_Clear
, MyFrame::OnClear
)
213 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
216 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
217 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
220 // the HTML listbox's events
221 EVT_HTML_LINK_CLICKED(wxID_ANY
, MyFrame::OnHtmlLinkClicked
)
222 EVT_HTML_CELL_HOVER(wxID_ANY
, MyFrame::OnHtmlCellHover
)
223 EVT_HTML_CELL_CLICKED(wxID_ANY
, MyFrame::OnHtmlCellClicked
)
229 // ============================================================================
231 // ============================================================================
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
239 : wxFrame(NULL
, wxID_ANY
, wxT("HtmlLbox wxWidgets Sample"),
240 wxDefaultPosition
, wxSize(500, 500))
242 // set the frame icon
243 SetIcon(wxICON(sample
));
247 wxMenu
*menuFile
= new wxMenu
;
248 menuFile
->AppendRadioItem(HtmlLbox_CustomBox
, wxT("Use custom box"),
249 wxT("Use a wxHtmlListBox virtual class control"));
250 menuFile
->AppendRadioItem(HtmlLbox_SimpleBox
, wxT("Use simple box"),
251 wxT("Use a wxSimpleHtmlListBox control"));
252 menuFile
->AppendSeparator();
253 menuFile
->Append(HtmlLbox_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
255 // create our specific menu
256 wxMenu
*menuHLbox
= new wxMenu
;
257 menuHLbox
->Append(HtmlLbox_SetMargins
,
258 wxT("Set &margins...\tCtrl-G"),
259 wxT("Change the margins around the items"));
260 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
261 wxT("&Draw separators\tCtrl-D"),
262 wxT("Toggle drawing separators between cells"));
263 menuHLbox
->AppendSeparator();
264 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
265 wxT("&Multiple selection\tCtrl-M"),
266 wxT("Toggle multiple selection on/off"));
267 menuHLbox
->AppendSeparator();
268 menuHLbox
->Append(HtmlLbox_SelectAll
, wxT("Select &all items\tCtrl-A"));
269 menuHLbox
->Append(HtmlLbox_UpdateItem
, wxT("Update &first item\tCtrl-U"));
270 menuHLbox
->Append(HtmlLbox_GetItemRect
, wxT("Show &rectangle of item #10\tCtrl-R"));
271 menuHLbox
->AppendSeparator();
272 menuHLbox
->Append(HtmlLbox_SetBgCol
, wxT("Set &background...\tCtrl-B"));
273 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
274 wxT("Set &selection background...\tCtrl-S"));
275 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
276 wxT("Keep &foreground in selection\tCtrl-F"));
278 menuHLbox
->AppendSeparator();
279 menuHLbox
->Append(HtmlLbox_Clear
, wxT("&Clear\tCtrl-L"));
281 // the "About" item should be in the help menu
282 wxMenu
*helpMenu
= new wxMenu
;
283 helpMenu
->Append(HtmlLbox_About
, wxT("&About\tF1"), wxT("Show about dialog"));
285 // now append the freshly created menu to the menu bar...
286 wxMenuBar
*menuBar
= new wxMenuBar();
287 menuBar
->Append(menuFile
, wxT("&File"));
288 menuBar
->Append(menuHLbox
, wxT("&Listbox"));
289 menuBar
->Append(helpMenu
, wxT("&Help"));
291 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
293 // ... and attach this menu bar to the frame
295 #endif // wxUSE_MENUS
298 // create a status bar just for fun (by default with 1 pane only)
300 SetStatusText(wxT("Welcome to wxWidgets!"));
301 #endif // wxUSE_STATUSBAR
303 // create the child controls
305 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxT(""),
306 wxDefaultPosition
, wxDefaultSize
,
308 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
311 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
312 sizer
->Add(m_hlbox
, 2, wxGROW
);
313 sizer
->Add(text
, 3, wxGROW
);
320 delete wxLog::SetActiveTarget(NULL
);
323 void MyFrame::CreateBox()
325 bool multi
= GetMenuBar()->IsChecked(HtmlLbox_ToggleMulti
);
327 if ( GetMenuBar()->IsChecked(HtmlLbox_CustomBox
) )
329 m_hlbox
= new MyHtmlListBox(this, multi
);
331 else // simple listbox
333 m_hlbox
= new wxSimpleHtmlListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
334 0, NULL
, multi
? wxLB_MULTIPLE
: 0);
336 // unlike wxHtmlListBox which is abstract, wxSimpleHtmlListBox is a
337 // concrete control and doesn't support virtual mode, this we need
338 // to add all of its items from the beginning
340 for (size_t n
= 0; n
< 1000; n
++ )
342 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
343 (unsigned char)(abs((int)n
- 256) % 256),
344 (unsigned char)(abs((int)n
- 128) % 256));
345 int level
= n
% 6 + 1;
347 wxString label
= wxString::Format(wxT("<h%d><font color=%s>")
348 wxT("Item</font> <b>%lu</b>")
351 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
352 (unsigned long)n
, level
);
356 GetSimpleBox()->Append(arr
);
361 // ----------------------------------------------------------------------------
362 // menu event handlers
363 // ----------------------------------------------------------------------------
365 void MyFrame::OnSimpleOrCustomBox(wxCommandEvent
& WXUNUSED(event
))
367 wxWindow
*old
= m_hlbox
;
369 // we need to recreate the listbox
371 GetSizer()->Replace(old
, m_hlbox
);
374 GetSizer()->Layout();
378 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
380 // true is to force the frame to close
384 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
386 wxMessageBox(wxT("This sample shows wxHtmlListBox class.\n")
388 wxT("(c) 2003 Vadim Zeitlin"),
389 wxT("About HtmlLbox"),
390 wxOK
| wxICON_INFORMATION
,
394 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
396 long margin
= wxGetNumberFromUser
398 wxT("Enter the margins to use for the listbox items."),
400 wxT("HtmlLbox: Set the margins"),
407 m_hlbox
->SetMargins(margin
, margin
);
408 m_hlbox
->RefreshAll();
412 void MyFrame::OnToggleMulti(wxCommandEvent
& WXUNUSED(event
))
414 wxWindow
*old
= m_hlbox
;
416 // we need to recreate the listbox
418 GetSizer()->Replace(old
, m_hlbox
);
421 GetSizer()->Layout();
424 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
426 m_hlbox
->SelectAll();
429 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
431 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
434 void MyFrame::OnUpdateItem(wxCommandEvent
& WXUNUSED(event
))
437 GetMyBox()->UpdateFirstItem();
440 void MyFrame::OnGetItemRect(wxCommandEvent
& WXUNUSED(event
))
442 static const int ITEM
= 10;
443 const wxRect r
= m_hlbox
->GetItemRect(ITEM
);
444 wxLogMessage("Rect of item %d: (%d, %d)-(%d, %d)",
445 ITEM
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
448 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
450 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
453 m_hlbox
->SetBackgroundColour(col
);
457 SetStatusText(wxT("Background colour changed."));
458 #endif // wxUSE_STATUSBAR
462 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
464 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
467 m_hlbox
->SetSelectionBackground(col
);
471 SetStatusText(wxT("Selection background colour changed."));
472 #endif // wxUSE_STATUSBAR
476 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
480 GetMyBox()->SetChangeSelFg(!event
.IsChecked());
481 GetMyBox()->Refresh();
485 void MyFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
490 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
492 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
496 GetMyBox()->m_linkClicked
= true;
497 GetMyBox()->RefreshRow(1);
501 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
503 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
504 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
507 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
509 wxLogMessage(wxT("Click over cell %p at %d;%d"),
510 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
512 // if we don't skip the event, OnHtmlLinkClicked won't be called!
516 // ----------------------------------------------------------------------------
517 // listbox event handlers
518 // ----------------------------------------------------------------------------
520 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
522 wxLogMessage(wxT("Listbox selection is now %d."), event
.GetInt());
524 if ( m_hlbox
->HasMultipleSelection() )
529 unsigned long cookie
;
530 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
532 item
= m_hlbox
->GetNextSelected(cookie
) )
544 wxLogMessage(wxT("Selected items: %s"), s
.c_str());
549 SetStatusText(wxString::Format(
550 wxT("# items selected = %lu"),
551 (unsigned long)m_hlbox
->GetSelectedCount()
553 #endif // wxUSE_STATUSBAR
556 // ============================================================================
558 // ============================================================================
560 IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox
, wxHtmlListBox
)
562 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
563 : wxHtmlListBox(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
564 multi
? wxLB_MULTIPLE
: 0)
567 m_firstItemUpdated
= false;
568 m_linkClicked
= false;
574 if ( !m_file
.Open(wxT("results")) )
576 wxLogError(wxT("Failed to open results file"));
580 SetItemCount(m_file
.GetLineCount());
589 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
591 if ( ((MyFrame
*)GetParent())->
592 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
594 dc
.SetPen(*wxBLACK_DASHED_PEN
);
595 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
596 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
600 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
602 if ( !n
&& m_firstItemUpdated
)
604 return wxT("<h1><b>Just updated</b></h1>");
609 if ( m_file
.IsOpened() )
614 int level
= n
% 6 + 1;
616 wxColour
clr((unsigned char)(abs((int)n
- 192) % 256),
617 (unsigned char)(abs((int)n
- 256) % 256),
618 (unsigned char)(abs((int)n
- 128) % 256));
620 wxString label
= wxString::Format(wxT("<h%d><font color=%s>")
621 wxT("Item</font> <b>%lu</b>")
624 clr
.GetAsString(wxC2S_HTML_SYNTAX
).c_str(),
625 (unsigned long)n
, level
);
628 if ( !m_linkClicked
)
629 label
+= wxT("<a href='1'>Click here...</a>");
631 label
+= wxT("<font color='#9999ff'>Clicked here...</font>");
638 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
640 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;
643 void MyHtmlListBox::UpdateFirstItem()
645 m_firstItemUpdated
= !m_firstItemUpdated
;