1 /*
2 
3   Copyright (C) 2003 - 2013  Razvan Cojocaru <rzvncj@gmail.com>
4   Tabbed browsing support developed by Cedric Boudinet <bouced@gmx.fr>
5   (this file originally written by Cedric Boudinet)
6 
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11 
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16 
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20   MA 02110-1301, USA.
21 
22 */
23 
24 
25 #include <chmframe.h>
26 #include <chmhtmlwindow.h>
27 #include <chmhtmlnotebook.h>
28 
29 
CHMHtmlNotebook(wxWindow * parent,wxTreeCtrl * tc,const wxString & normalFont,const wxString & fixedFont,const int fontSize,CHMFrame * frame)30 CHMHtmlNotebook::CHMHtmlNotebook(wxWindow *parent, wxTreeCtrl *tc,
31 				 const wxString& normalFont,
32 				 const wxString& fixedFont,
33 				 const int fontSize, CHMFrame* frame)
34 	: wxAuiNotebook(parent, -1, wxDefaultPosition, wxDefaultSize,
35 			wxAUI_NB_DEFAULT_STYLE | wxAUI_NB_TAB_FIXED_WIDTH),
36 	  _tcl(tc), _frame(frame), _fonts_normal_face(normalFont),
37 	  _fonts_fixed_face(fixedFont)
38 {
39 	for(int i = -3; i <= 3; ++i)
40 		_fonts_sizes[i+3] = fontSize + i * 2;
41 
42 	wxAcceleratorEntry entries[2];
43 	entries[0].Set(wxACCEL_CTRL,   WXK_PAGEUP,     ID_PriorPage);
44 	entries[1].Set(wxACCEL_CTRL,   WXK_PAGEDOWN,   ID_NextPage);
45 
46 	wxAcceleratorTable accel(2, entries);
47 	this->SetAcceleratorTable(accel);
48 	SetTabCtrlHeight(0);
49 
50 	AddHtmlView(wxEmptyString, wxT("memory:about.html"));
51 }
52 
53 
CreateView()54 CHMHtmlWindow* CHMHtmlNotebook::CreateView()
55 {
56 	CHMHtmlWindow * htmlWin = new CHMHtmlWindow(this, _tcl, _frame);
57 	htmlWin->SetRelatedFrame(_frame, wxT("xCHM v. ") wxT(VERSION) wxT(": %s"));
58 	htmlWin->SetRelatedStatusBar(0);
59 	htmlWin->SetFonts(_fonts_normal_face, _fonts_fixed_face, _fonts_sizes);
60 
61 	AddPage(htmlWin, _("(Empty page)"));
62 	SetSelection(GetPageCount() - 1);
63 	return htmlWin;
64 }
65 
66 
AddHtmlView(const wxString & path,const wxString & link)67 void CHMHtmlNotebook::AddHtmlView(const wxString& path,
68 				  const wxString& link)
69 {
70 	CHMHtmlWindow* htmlWin = CreateView();
71 
72 	if(htmlWin) {
73 		htmlWin->GetParser()->GetFS()->ChangePathTo(path);
74 		htmlWin->LoadPage(link);
75 	}
76 }
77 
78 
LoadPageInCurrentView(const wxString & location)79 bool CHMHtmlNotebook::LoadPageInCurrentView(const wxString& location)
80 {
81 	return GetCurrentPage()->LoadPage(location);
82 }
83 
84 
85 // TODO: this is a misleading named function with side effects. It's a no-no.
GetCurrentPage()86 CHMHtmlWindow* CHMHtmlNotebook::GetCurrentPage()
87 {
88 	int selection = GetSelection();
89 
90 	if(selection == wxNOT_FOUND)
91 		return CreateView();
92 
93 	return dynamic_cast<CHMHtmlWindow *>(
94 		wxAuiNotebook::GetPage(selection));
95 }
96 
97 
OnGoToNextPage(wxCommandEvent &)98 void CHMHtmlNotebook::OnGoToNextPage(wxCommandEvent&)
99 {
100 	int selection = GetSelection();
101 
102 	if(selection >= static_cast<int>(GetPageCount() - 1))
103 		return;
104 
105 	SetSelection(selection + 1);
106 }
107 
108 
OnGoToPriorPage(wxCommandEvent &)109 void CHMHtmlNotebook::OnGoToPriorPage(wxCommandEvent&)
110 {
111 	int selection = GetSelection();
112 
113 	if(selection <= 0)
114 		return;
115 
116 	SetSelection(selection - 1);
117 }
118 
119 
OnCloseTab(wxCommandEvent & WXUNUSED (event))120 void CHMHtmlNotebook::OnCloseTab(wxCommandEvent& WXUNUSED(event))
121 {
122 	DeletePage(GetSelection());
123 
124 	if(GetPageCount() <= 1)
125 		SetTabCtrlHeight(0);
126 }
127 
128 
OnNewTab(wxCommandEvent & WXUNUSED (event))129 void CHMHtmlNotebook::OnNewTab(wxCommandEvent& WXUNUSED(event))
130 {
131 	AddHtmlView(wxEmptyString, wxEmptyString);
132 }
133 
134 
OnChildrenTitleChanged(const wxString & title)135 void CHMHtmlNotebook::OnChildrenTitleChanged(const wxString& title)
136 {
137 	// We assume the change occured in the currently displayed page
138 	// TODO: detect in which page the change occured.
139 	SetPageText(GetSelection(), title);
140 }
141 
142 
CloseAllPagesExceptFirst()143 void CHMHtmlNotebook::CloseAllPagesExceptFirst()
144 {
145 	SetSelection(0);
146 
147 	while(GetPageCount() > 1)
148 		DeletePage(1);
149 
150 	SetTabCtrlHeight(0);
151 }
152 
153 
SetChildrenFonts(const wxString & normal_face,const wxString & fixed_face,const int * sizes)154 void CHMHtmlNotebook::SetChildrenFonts(const wxString& normal_face,
155 				       const wxString& fixed_face,
156 				       const int *sizes)
157 {
158 	_fonts_normal_face = normal_face;
159 	_fonts_fixed_face = fixed_face;
160 
161 	for(int i = 0; i < 7; ++i)
162 		_fonts_sizes[i] = sizes[i];
163 
164 	size_t nPageCount = GetPageCount();
165 
166 	for(size_t nPage = 0; nPage < nPageCount; ++nPage) {
167 		CHMHtmlWindow* chw =
168 			dynamic_cast<CHMHtmlWindow *>(GetPage(nPage));
169 
170 		if(chw)
171 			chw->SetFonts(normal_face, fixed_face, sizes);
172 	}
173 }
174 
175 
AddPage(wxWindow * page,const wxString & title)176 bool CHMHtmlNotebook::AddPage(wxWindow* page, const wxString& title)
177 {
178 	if(!page)
179 		return false;
180 
181 	bool st = wxAuiNotebook::AddPage(page, title);
182 
183 	if(GetPageCount() == 2)
184 		SetTabCtrlHeight(-1);
185 
186 	return st;
187 }
188 
189 
OnPageChanged(wxAuiNotebookEvent &)190 void CHMHtmlNotebook::OnPageChanged(wxAuiNotebookEvent&)
191 {
192 	if(GetPageCount() == 1)
193 		SetTabCtrlHeight(0);
194 }
195 
196 
197 BEGIN_EVENT_TABLE(CHMHtmlNotebook, wxAuiNotebook)
198 	EVT_MENU(ID_PriorPage, CHMHtmlNotebook::OnGoToPriorPage)
199 	EVT_MENU(ID_NextPage, CHMHtmlNotebook::OnGoToNextPage)
200 	EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY, CHMHtmlNotebook::OnPageChanged)
201 END_EVENT_TABLE()
202 
203 
204 /*
205   Local Variables:
206   mode: c++
207   c-basic-offset: 8
208   tab-width: 8
209   c-indent-comments-syntactically-p: t
210   c-tab-always-indent: t
211   indent-tabs-mode: t
212   End:
213 */
214 
215 // vim:shiftwidth=8:autoindent:tabstop=8:noexpandtab:softtabstop=8
216 
217 
218