]>
git.saurik.com Git - wxWidgets.git/blob - src/html/helpfrm.cpp
d99173552de0c4646469ac9dc1bcd02d0c53574b
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlHelpFrame
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
31 #include "wx/html/helpfrm.h"
32 #include "wx/notebook.h"
33 #include "wx/imaglist.h"
34 #include "wx/treectrl.h"
35 #include "wx/tokenzr.h"
36 #include "wx/wfstream.h"
37 #include "wx/html/htmlwin.h"
38 #include "wx/busyinfo.h"
39 #include "wx/progdlg.h"
44 #include "bitmaps/panel.xpm"
45 #include "bitmaps/back.xpm"
46 #include "bitmaps/forward.xpm"
47 #include "bitmaps/book.xpm"
48 #include "bitmaps/folder.xpm"
49 #include "bitmaps/page.xpm"
52 #include "wx/stream.h"
54 // number of times that the contents/index creation progress dialog
56 #define PROGRESS_STEP 40
58 //--------------------------------------------------------------------------
59 // wxHtmlHelpTreeItemData
60 //--------------------------------------------------------------------------
62 class wxHtmlHelpTreeItemData
: public wxTreeItemData
68 wxHtmlHelpTreeItemData(wxHtmlContentsItem
*it
) : wxTreeItemData()
70 m_Page
= it
-> m_Book
-> GetBasePath() + it
-> m_Page
;
72 const wxString
& GetPage() { return m_Page
; }
75 //---------------------------------------------------------------------------
77 //---------------------------------------------------------------------------
79 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpFrame
, wxFrame
)
81 wxHtmlHelpFrame::wxHtmlHelpFrame(wxWindow
* parent
, wxWindowID id
, const wxString
& title
,
82 int style
, wxHtmlHelpData
* data
)
85 Create(parent
, id
, title
, style
);
88 void wxHtmlHelpFrame::Init(wxHtmlHelpData
* data
)
92 m_DataCreated
= FALSE
;
94 m_Data
= new wxHtmlHelpData();
98 m_ContentsImageList
= new wxImageList(12, 12);
99 m_ContentsImageList
-> Add(wxICON(book
));
100 m_ContentsImageList
-> Add(wxICON(folder
));
101 m_ContentsImageList
-> Add(wxICON(page
));
103 m_ContentsBox
= NULL
;
106 m_SearchButton
= NULL
;
108 m_SearchChoice
= NULL
;
113 m_ConfigRoot
= wxEmptyString
;
115 m_Cfg
.x
= m_Cfg
.y
= 0;
119 m_Cfg
.navig_on
= TRUE
;
122 // Create: builds the GUI components.
123 // with the style flag it's possible to toggle the toolbar, contents, index and search
125 // m_HtmlWin will *always* be created, but it's important to realize that
126 // m_ContentsBox, m_IndexBox, m_SearchList, m_SearchButton, m_SearchText and
127 // m_SearchButton may be NULL.
128 // moreover, if no contents, index or searchpage is needed, m_Splitter and
129 // m_NavigPan will be NULL too (with m_HtmlWin directly connected to the frame)
131 bool wxHtmlHelpFrame::Create(wxWindow
* parent
, wxWindowID id
, const wxString
& title
,
134 // Do the config in two steps. We read the HtmlWindow customization after we
135 // create the window.
137 ReadCustomization(m_Config
, m_ConfigRoot
);
139 wxFrame::Create(parent
, id
, "", wxPoint(m_Cfg
.x
, m_Cfg
.y
), wxSize(m_Cfg
.w
, m_Cfg
.h
));
141 int notebook_page
= 0;
146 if (style
& wxHF_TOOLBAR
) {
147 wxToolBar
*toolBar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
| /*wxTB_FLAT | */
149 toolBar
-> SetMargins(2, 2);
150 wxBitmap
* toolBarBitmaps
[3];
153 toolBarBitmaps
[0] = new wxBitmap("panel");
154 toolBarBitmaps
[1] = new wxBitmap("back");
155 toolBarBitmaps
[2] = new wxBitmap("forward");
158 toolBarBitmaps
[0] = new wxBitmap(panel_xpm
);
159 toolBarBitmaps
[1] = new wxBitmap(back_xpm
);
160 toolBarBitmaps
[2] = new wxBitmap(forward_xpm
);
166 toolBar
-> AddTool(wxID_HTML_PANEL
, *(toolBarBitmaps
[0]), wxNullBitmap
,
167 FALSE
, currentX
, -1, (wxObject
*) NULL
,
168 _("Show/hide navigation panel"));
169 currentX
+= width
+ 5;
170 toolBar
-> AddSeparator();
171 toolBar
-> AddTool(wxID_HTML_BACK
, *(toolBarBitmaps
[1]), wxNullBitmap
,
172 FALSE
, currentX
, -1, (wxObject
*) NULL
,
173 _("Go back to the previous HTML page"));
174 currentX
+= width
+ 5;
175 toolBar
-> AddTool(wxID_HTML_FORWARD
, *(toolBarBitmaps
[2]), wxNullBitmap
,
176 FALSE
, currentX
, -1, (wxObject
*) NULL
,
177 _("Go forward to the next HTML page"));
178 currentX
+= width
+ 5;
180 toolBar
-> Realize();
182 // Can delete the bitmaps since they're reference counted
183 for (int i
= 0; i
< 3; i
++)
184 delete toolBarBitmaps
[i
];
187 if (style
& (wxHF_CONTENTS
| wxHF_INDEX
| wxHF_SEARCH
)) {
188 // traditional help controller; splitter window with html page on the
189 // right and a notebook containing various pages on the left
190 m_Splitter
= new wxSplitterWindow(this);
192 m_HtmlWin
= new wxHtmlWindow(m_Splitter
);
193 m_NavigPan
= new wxNotebook(m_Splitter
, wxID_HTML_NOTEBOOK
,
194 wxDefaultPosition
, wxDefaultSize
);
195 } else { // only html window, no notebook with index,contents etc
196 m_HtmlWin
= new wxHtmlWindow(this);
199 m_HtmlWin
-> SetRelatedFrame(this, m_TitleFormat
);
200 m_HtmlWin
-> SetRelatedStatusBar(0);
202 m_HtmlWin
-> ReadCustomization(m_Config
, m_ConfigRoot
);
204 // contents tree panel?
205 if (style
& wxHF_CONTENTS
) {
206 m_ContentsBox
= new wxTreeCtrl(m_NavigPan
, wxID_HTML_TREECTRL
,
207 wxDefaultPosition
, wxDefaultSize
,
208 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
209 m_ContentsBox
-> SetImageList(m_ContentsImageList
);
210 m_NavigPan
-> AddPage(m_ContentsBox
, _("Contents"));
211 m_ContentsPage
= notebook_page
++;
214 // index listbox panel?
215 if (style
& wxHF_INDEX
) {
216 wxWindow
*dummy
= new wxPanel(m_NavigPan
, wxID_HTML_INDEXPAGE
);
217 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
218 b1
-> top
.SameAs (dummy
, wxTop
, 0);
219 b1
-> left
.SameAs (dummy
, wxLeft
, 0);
220 b1
-> width
.PercentOf (dummy
, wxWidth
, 100);
221 b1
-> bottom
.SameAs (dummy
, wxBottom
, 0);
222 m_IndexBox
= new wxListBox(dummy
, wxID_HTML_INDEXLIST
, wxDefaultPosition
,
223 wxDefaultSize
, 0, NULL
, wxLB_SINGLE
| wxLB_ALWAYS_SB
);
224 m_IndexBox
-> SetConstraints(b1
);
225 dummy
-> SetAutoLayout(TRUE
);
226 m_NavigPan
-> AddPage(dummy
, _("Index"));
227 m_IndexPage
= notebook_page
++;
230 // search list panel?
231 if (style
& wxHF_SEARCH
) {
232 wxWindow
*dummy
= new wxPanel(m_NavigPan
, wxID_HTML_SEARCHPAGE
);
234 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
235 m_SearchText
= new wxTextCtrl(dummy
, wxID_HTML_SEARCHTEXT
);
236 b1
-> top
.SameAs (dummy
, wxTop
, 10);
237 b1
-> left
.SameAs (dummy
, wxLeft
, 10);
238 b1
-> right
.SameAs (dummy
, wxRight
, 10);
240 m_SearchText
-> SetConstraints(b1
);
242 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
243 m_SearchButton
= new wxButton(dummy
, wxID_HTML_SEARCHBUTTON
, _("Search"));
244 b2
-> top
.Below (m_SearchText
, 10);
245 b2
-> left
.SameAs (dummy
, wxLeft
, 10);
248 m_SearchButton
-> SetConstraints(b2
);
250 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
251 m_SearchList
= new wxListBox(dummy
, wxID_HTML_SEARCHLIST
, wxDefaultPosition
, wxDefaultSize
, 0);
252 b3
-> top
.Below (m_SearchButton
, 10);
253 b3
-> left
.SameAs (dummy
, wxLeft
, 0);
254 b3
-> right
.SameAs (dummy
, wxRight
, 0);
255 b3
-> bottom
.SameAs (dummy
, wxBottom
, 0);
256 m_SearchList
-> SetConstraints(b3
);
258 wxLayoutConstraints
*b4
= new wxLayoutConstraints
;
259 m_SearchChoice
= new wxChoice(dummy
, wxID_HTML_SEARCHCHOICE
, wxDefaultPosition
,
261 b4
-> top
.Below (m_SearchText
, 10);
262 b4
-> left
.SameAs (m_SearchButton
, wxRight
, 10);
263 b4
-> right
.SameAs (dummy
, wxRight
, 10);
265 m_SearchChoice
-> SetConstraints(b4
);
267 dummy
-> SetAutoLayout(TRUE
);
269 m_NavigPan
-> AddPage(dummy
, _("Search"));
270 m_SearchPage
= notebook_page
++;
276 if (m_NavigPan
&& m_Splitter
) {
277 m_NavigPan
-> Show(TRUE
);
278 m_Splitter
-> SetMinimumPaneSize(20);
280 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
282 m_HtmlWin
-> Show(TRUE
);
286 wxHtmlHelpFrame::~wxHtmlHelpFrame()
288 delete m_ContentsImageList
;
293 bool wxHtmlHelpFrame::Display(const wxString
& x
)
295 wxString url
= m_Data
->FindPageByName(x
);
296 if (! url
.IsEmpty()) {
297 m_HtmlWin
->LoadPage(url
);
303 bool wxHtmlHelpFrame::Display(const int id
)
305 wxString url
= m_Data
->FindPageById(id
);
306 if (! url
.IsEmpty()) {
307 m_HtmlWin
->LoadPage(url
);
315 bool wxHtmlHelpFrame::DisplayContents()
319 if (!m_Splitter
-> IsSplit()) {
320 m_NavigPan
-> Show(TRUE
);
321 m_HtmlWin
-> Show(TRUE
);
322 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
324 m_NavigPan
-> SetSelection(0);
330 bool wxHtmlHelpFrame::DisplayIndex()
334 if (!m_Splitter
-> IsSplit()) {
335 m_NavigPan
-> Show(TRUE
);
336 m_HtmlWin
-> Show(TRUE
);
337 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
339 m_NavigPan
-> SetSelection(1);
343 bool wxHtmlHelpFrame::KeywordSearch(const wxString
& keyword
)
345 if (! (m_SearchList
&& m_SearchButton
&& m_SearchText
&& m_SearchChoice
))
350 wxString book
= wxEmptyString
;
352 if (!m_Splitter
-> IsSplit()) {
353 m_NavigPan
-> Show(TRUE
);
354 m_HtmlWin
-> Show(TRUE
);
355 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
357 m_NavigPan
-> SetSelection(m_SearchPage
);
358 m_SearchList
-> Clear();
359 m_SearchText
-> SetValue(keyword
);
360 m_SearchButton
-> Enable(FALSE
);
362 if (m_SearchChoice
->GetSelection() != 0)
363 book
= m_SearchChoice
->GetStringSelection();
365 wxHtmlSearchStatus
status(m_Data
, keyword
, book
);
367 wxProgressDialog
progress(_("Searching..."), _("No matching page found yet"),
368 status
.GetMaxIndex(), this,
369 wxPD_APP_MODAL
| wxPD_CAN_ABORT
| wxPD_AUTO_HIDE
);
371 while (status
.IsActive()) {
372 if (progress
.Update(status
.GetCurIndex()) == FALSE
)
374 if (status
.Search()) {
375 foundstr
.Printf(_("Found %i matches"), ++foundcnt
);
376 progress
.Update(status
.GetCurIndex(), foundstr
);
377 m_SearchList
-> Append(status
.GetName(), status
.GetContentsItem());
382 m_SearchButton
-> Enable(TRUE
);
383 m_SearchText
-> SetSelection(0, keyword
.Length());
384 m_SearchText
-> SetFocus();
386 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(0);
387 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
389 return (foundcnt
> 0);
394 void wxHtmlHelpFrame::CreateContents(bool show_progress
)
399 wxProgressDialog
*progress
;
402 m_ContentsBox
->Clear();
404 int cnt
= m_Data
->GetContentsCnt();
405 int div
= (cnt
/ PROGRESS_STEP
) + 1;
408 wxHtmlContentsItem
*it
= m_Data
->GetContents();
411 progress
= new wxProgressDialog(_("Building contents tree..."), wxEmptyString
,
412 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
415 wxTreeItemId roots
[MAX_ROOTS
];
416 bool imaged
[MAX_ROOTS
];
418 m_ContentsBox
-> DeleteAllItems();
419 roots
[0] = m_ContentsBox
-> AddRoot(_("(Help)"));
422 for (i
= 0; i
< cnt
; i
++, it
++) {
423 if (show_progress
&& ((i
% div
) == 0)) {
424 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
425 if (! progress
->Update(i
, proginfo
))
429 roots
[it
-> m_Level
+ 1] = m_ContentsBox
-> AppendItem(
430 roots
[it
-> m_Level
], it
-> m_Name
, IMG_Page
, -1,
431 new wxHtmlHelpTreeItemData(it
));
433 if (it
-> m_Level
== 0) {
434 m_ContentsBox
-> SetItemBold(roots
[1], TRUE
);
435 m_ContentsBox
-> SetItemImage(roots
[1], IMG_Book
);
436 m_ContentsBox
-> SetItemSelectedImage(roots
[1], IMG_Book
);
438 } else imaged
[it
-> m_Level
+ 1] = FALSE
;
440 if (!imaged
[it
-> m_Level
]) {
441 m_ContentsBox
-> SetItemImage(roots
[it
-> m_Level
], IMG_Folder
);
442 m_ContentsBox
-> SetItemSelectedImage(roots
[it
-> m_Level
], IMG_Folder
);
443 imaged
[it
-> m_Level
] = TRUE
;
448 m_ContentsBox
-> Expand(roots
[0]);
452 void wxHtmlHelpFrame::CreateIndex(bool show_progress
)
457 wxProgressDialog
*progress
;
462 int cnt
= m_Data
->GetIndexCnt();
463 int div
= (cnt
/ PROGRESS_STEP
) + 1;
465 wxHtmlContentsItem
* index
= m_Data
->GetIndex();
468 progress
= new wxProgressDialog(_("Building index list..."), wxEmptyString
,
469 cnt
, this, wxPD_APP_MODAL
| wxPD_CAN_ABORT
|
471 for (int i
= 0; i
< cnt
; i
++) {
472 if (show_progress
&& ((i
% div
) == 0)) {
473 proginfo
.Printf(_("Added %d/%d items"), i
, cnt
);
474 if (! progress
->Update(i
, proginfo
))
478 m_IndexBox
-> Append(index
[i
].m_Name
, (char*)(index
+ i
));
485 void wxHtmlHelpFrame::CreateSearch()
487 if (! (m_SearchList
&& m_SearchChoice
))
489 m_SearchList
-> Clear();
490 m_SearchChoice
-> Clear();
491 m_SearchChoice
-> Append(_("all books"));
492 const wxHtmlBookRecArray
& bookrec
= m_Data
->GetBookRecArray();
493 int i
, cnt
= bookrec
.GetCount();
494 for (i
= 0; i
< cnt
; i
++)
495 m_SearchChoice
->Append(bookrec
[i
].GetTitle());
496 m_SearchChoice
->SetSelection(0);
500 void wxHtmlHelpFrame::RefreshLists(bool show_progress
)
502 CreateContents(show_progress
);
503 CreateIndex(show_progress
);
507 void wxHtmlHelpFrame::ReadCustomization(wxConfigBase
*cfg
, const wxString
& path
)
512 if (path
!= wxEmptyString
) {
513 oldpath
= cfg
-> GetPath();
514 cfg
-> SetPath(path
);
517 m_Cfg
.navig_on
= cfg
-> Read("hcNavigPanel", m_Cfg
.navig_on
) != 0;
518 m_Cfg
.sashpos
= cfg
-> Read("hcSashPos", m_Cfg
.sashpos
);
519 m_Cfg
.x
= cfg
-> Read("hcX", m_Cfg
.x
);
520 m_Cfg
.y
= cfg
-> Read("hcY", m_Cfg
.y
);
521 m_Cfg
.w
= cfg
-> Read("hcW", m_Cfg
.w
);
522 m_Cfg
.h
= cfg
-> Read("hcH", m_Cfg
.h
);
525 m_HtmlWin
->ReadCustomization(cfg
, path
);
527 if (path
!= wxEmptyString
)
528 cfg
-> SetPath(oldpath
);
531 void wxHtmlHelpFrame::WriteCustomization(wxConfigBase
*cfg
, const wxString
& path
)
536 if (path
!= wxEmptyString
) {
537 oldpath
= cfg
-> GetPath();
538 cfg
-> SetPath(path
);
541 cfg
-> Write("hcNavigPanel", m_Cfg
.navig_on
);
542 cfg
-> Write("hcSashPos", (long)m_Cfg
.sashpos
);
543 cfg
-> Write("hcX", (long)m_Cfg
.x
);
544 cfg
-> Write("hcY", (long)m_Cfg
.y
);
545 cfg
-> Write("hcW", (long)m_Cfg
.w
);
546 cfg
-> Write("hcH", (long)m_Cfg
.h
);
549 m_HtmlWin
->WriteCustomization(cfg
, path
);
551 if (path
!= wxEmptyString
)
552 cfg
-> SetPath(oldpath
);
561 void wxHtmlHelpFrame::OnToolbar(wxCommandEvent
& event
)
563 switch (event
.GetId()) {
564 case wxID_HTML_BACK
:
565 m_HtmlWin
-> HistoryBack();
567 case wxID_HTML_FORWARD
:
568 m_HtmlWin
-> HistoryForward();
570 case wxID_HTML_PANEL
:
571 if (! (m_Splitter
&& m_NavigPan
))
573 if (m_Splitter
-> IsSplit()) {
574 m_Cfg
.sashpos
= m_Splitter
-> GetSashPosition();
575 m_Splitter
-> Unsplit(m_NavigPan
);
577 m_NavigPan
-> Show(TRUE
);
578 m_HtmlWin
-> Show(TRUE
);
579 m_Splitter
-> SplitVertically(m_NavigPan
, m_HtmlWin
, m_Cfg
.sashpos
);
587 void wxHtmlHelpFrame::OnContentsSel(wxTreeEvent
& event
)
589 wxHtmlHelpTreeItemData
*pg
;
591 pg
= (wxHtmlHelpTreeItemData
*) m_ContentsBox
-> GetItemData(event
.GetItem());
592 if (pg
) m_HtmlWin
-> LoadPage(pg
-> GetPage());
597 void wxHtmlHelpFrame::OnIndexSel(wxCommandEvent
& WXUNUSED(event
))
599 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_IndexBox
-> GetClientData(m_IndexBox
-> GetSelection());
600 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
605 void wxHtmlHelpFrame::OnSearchSel(wxCommandEvent
& WXUNUSED(event
))
607 wxHtmlContentsItem
*it
= (wxHtmlContentsItem
*) m_SearchList
-> GetClientData(m_SearchList
-> GetSelection());
608 if (it
) m_HtmlWin
-> LoadPage(it
-> m_Book
-> GetBasePath() + it
-> m_Page
);
611 void wxHtmlHelpFrame::OnSearch(wxCommandEvent
& WXUNUSED(event
))
613 wxString sr
= m_SearchText
-> GetLineText(0);
615 if (sr
!= wxEmptyString
) KeywordSearch(sr
);
618 void wxHtmlHelpFrame::OnCloseWindow(wxCloseEvent
& evt
)
621 WriteCustomization(m_Config
, m_ConfigRoot
);
625 BEGIN_EVENT_TABLE(wxHtmlHelpFrame
, wxFrame
)
626 EVT_TOOL_RANGE(wxID_HTML_PANEL
, wxID_HTML_FORWARD
, wxHtmlHelpFrame::OnToolbar
)
627 EVT_TREE_SEL_CHANGED(wxID_HTML_TREECTRL
, wxHtmlHelpFrame::OnContentsSel
)
628 EVT_LISTBOX(wxID_HTML_INDEXLIST
, wxHtmlHelpFrame::OnIndexSel
)
629 EVT_LISTBOX(wxID_HTML_SEARCHLIST
, wxHtmlHelpFrame::OnSearchSel
)
630 EVT_BUTTON(wxID_HTML_SEARCHBUTTON
, wxHtmlHelpFrame::OnSearch
)
631 EVT_TEXT_ENTER(wxID_HTML_SEARCHTEXT
, wxHtmlHelpFrame::OnSearch
)
632 EVT_CLOSE(wxHtmlHelpFrame::OnCloseWindow
)