]>
Commit | Line | Data |
---|---|---|
29ea4a29 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpext.cpp | |
3 | // Purpose: an external help controller for wxWindows | |
4 | // Author: Karsten Ballueder | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Karsten Ballueder | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | # pragma implementation "helpwxht.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | # pragma hdrstop | |
20 | #endif | |
21 | ||
4f84c635 VZ |
22 | #if wxUSE_HTML |
23 | ||
29ea4a29 | 24 | #ifndef WX_PRECOMP |
29ea4a29 KB |
25 | # include "wx/string.h" |
26 | # include "wx/utils.h" | |
27 | # include "wx/list.h" | |
28 | # include "wx/intl.h" | |
29 | # include "wx/layout.h" | |
db8db70a | 30 | # include "wx/combobox.h" |
29ea4a29 KB |
31 | #endif |
32 | ||
33 | #include "wx/helpbase.h" | |
34 | #include "wx/generic/helpwxht.h" | |
35 | #include "wx/html/htmlwin.h" | |
36 | ||
37 | #include <stdio.h> | |
38 | #include <ctype.h> | |
39 | #include <sys/stat.h> | |
40 | ||
41 | #ifndef __WINDOWS__ | |
42 | # include <unistd.h> | |
43 | #endif | |
44 | ||
45 | IMPLEMENT_CLASS(wxHelpControllerHtml, wxHTMLHelpControllerBase) | |
4f84c635 | 46 | |
29ea4a29 | 47 | /** |
8dd71e2b | 48 | This class implements help via wxHTML. |
29ea4a29 KB |
49 | It requires the name of a directory containing the documentation |
50 | and a file mapping numerical Section numbers to relative URLS. | |
51 | */ | |
52 | ||
8dd71e2b KB |
53 | class wxForceHtmlFilter : public wxHtmlFilter |
54 | { | |
55 | public: | |
420ec58a | 56 | virtual wxString ReadFile(const wxFSFile& file) const |
8dd71e2b KB |
57 | { |
58 | wxInputStream *s = file.GetStream(); | |
59 | char *src; | |
60 | wxString doc; | |
61 | ||
62 | if (s == NULL) return wxEmptyString; | |
4ba80ec7 KB |
63 | src = new char[s -> GetSize()+1]; |
64 | src[s -> GetSize()] = 0; | |
65 | s -> Read(src, s -> GetSize()); | |
8dd71e2b KB |
66 | doc = src; |
67 | delete [] src; | |
68 | return doc; | |
69 | } | |
70 | ||
420ec58a | 71 | virtual bool CanRead(const wxFSFile& file) const |
8dd71e2b KB |
72 | { |
73 | wxString filename = file.GetLocation(); | |
74 | if(filename.Length() >= 5 && | |
75 | ( | |
76 | filename.Right(4).MakeUpper() == ".HTM" || | |
77 | filename.Right(5).MakeUpper() == ".HTML")) | |
78 | return TRUE; | |
79 | else | |
80 | return FALSE; | |
81 | } | |
82 | }; | |
83 | ||
84 | #define FRAME_WIDTH 500 | |
4f84c635 | 85 | #define FRAME_HEIGHT 400 |
29ea4a29 KB |
86 | #define LAYOUT_X_MARGIN 2 |
87 | #define LAYOUT_Y_MARGIN 2 | |
88 | #define OFFSET 10 | |
8dd71e2b KB |
89 | #define BUTTON_WIDTH 70 |
90 | #define MAX_COMBO_ENTRIES 25 | |
4f84c635 | 91 | |
29ea4a29 KB |
92 | class wxHelpFrame : public wxFrame |
93 | { | |
94 | public: | |
95 | wxHelpFrame(wxWindow *parent, int id, const wxString &title, | |
96 | const wxPoint &pos, const wxSize &size, | |
97 | wxHelpControllerHtml *controller); | |
98 | ~wxHelpFrame(); | |
99 | void OnClose(wxCloseEvent &ev); | |
8dd71e2b | 100 | void OnButton(wxCommandEvent &ev); |
29ea4a29 KB |
101 | bool LoadPage(const wxString &url) { return m_htmlwin->LoadPage(url); } |
102 | private: | |
103 | wxHelpControllerHtml *m_controller; | |
104 | wxHtmlWindow *m_htmlwin; | |
8dd71e2b KB |
105 | wxHtmlFilter *m_filter; |
106 | wxComboBox *m_combo; | |
107 | long m_IdBack, m_IdFwd, m_IdContents, m_IdCombo, m_IdSearch; | |
29ea4a29 KB |
108 | DECLARE_EVENT_TABLE() |
109 | }; | |
110 | ||
111 | BEGIN_EVENT_TABLE(wxHelpFrame, wxFrame) | |
112 | EVT_CLOSE(wxHelpFrame::OnClose) | |
8dd71e2b | 113 | EVT_BUTTON(-1, wxHelpFrame::OnButton) |
29ea4a29 KB |
114 | END_EVENT_TABLE() |
115 | ||
8dd71e2b KB |
116 | |
117 | void | |
118 | wxHelpFrame::OnButton(wxCommandEvent &ev) | |
119 | { | |
120 | long id =ev.GetId(); | |
121 | ||
122 | if(id == m_IdBack) | |
123 | m_htmlwin->HistoryBack(); | |
124 | else if(id == m_IdFwd) | |
125 | m_htmlwin->HistoryForward(); | |
126 | else if(id == m_IdContents) | |
127 | m_controller->DisplayContents(); | |
128 | else if(id == m_IdSearch) | |
129 | { | |
130 | wxString str = m_combo->GetValue(); | |
131 | if(m_combo->FindString(str) == -1 && m_combo->Number() < MAX_COMBO_ENTRIES) | |
132 | m_combo->Append(str); | |
133 | m_controller->KeywordSearch(str); | |
134 | } | |
135 | } | |
136 | ||
29ea4a29 KB |
137 | wxHelpFrame::wxHelpFrame(wxWindow *parent, int id, |
138 | const wxString &title, | |
139 | const wxPoint &pos, const wxSize &size, | |
140 | wxHelpControllerHtml *controller) | |
141 | : wxFrame(parent, id, title, pos, size) | |
142 | { | |
143 | ||
144 | m_controller = controller; | |
4f84c635 | 145 | m_htmlwin = new wxHtmlWindow(this,-1,wxDefaultPosition,wxSize(FRAME_WIDTH, |
29ea4a29 | 146 | FRAME_HEIGHT)); |
4f84c635 | 147 | |
8dd71e2b KB |
148 | m_IdBack = wxWindow::NewControlId(); |
149 | m_IdFwd = wxWindow::NewControlId(); | |
150 | m_IdContents = wxWindow::NewControlId(); | |
151 | m_IdCombo = wxWindow::NewControlId(); | |
152 | m_IdSearch = wxWindow::NewControlId(); | |
153 | ||
154 | wxButton *btn_back = new wxButton(this, m_IdBack, _("Back")); | |
155 | wxButton *btn_fwd = new wxButton(this, m_IdFwd, _("Forward")); | |
156 | wxButton *btn_contents = new wxButton(this, m_IdContents, _("Contents")); | |
157 | m_combo = new wxComboBox(this, m_IdCombo); | |
158 | wxButton *btn_search = new wxButton(this, m_IdSearch, _("Search")); | |
159 | ||
160 | m_filter = new wxForceHtmlFilter; | |
161 | ||
29ea4a29 KB |
162 | wxLayoutConstraints *c; |
163 | ||
164 | c = new wxLayoutConstraints; | |
165 | c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN); | |
8dd71e2b | 166 | c->width.Absolute(BUTTON_WIDTH); |
29ea4a29 | 167 | c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN); |
8dd71e2b KB |
168 | c->height.AsIs(); |
169 | btn_back->SetConstraints(c); | |
170 | ||
171 | c = new wxLayoutConstraints; | |
172 | c->left.SameAs(btn_back, wxRight, 2*LAYOUT_X_MARGIN); | |
173 | c->width.Absolute(BUTTON_WIDTH); | |
174 | c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN); | |
175 | c->height.AsIs(); | |
176 | btn_fwd->SetConstraints(c); | |
177 | ||
178 | c = new wxLayoutConstraints; | |
179 | c->left.SameAs(btn_fwd, wxRight, 2*LAYOUT_X_MARGIN); | |
180 | c->width.Absolute(BUTTON_WIDTH); | |
181 | c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN); | |
182 | c->height.AsIs(); | |
183 | btn_contents->SetConstraints(c); | |
184 | ||
185 | c = new wxLayoutConstraints; | |
186 | c->left.SameAs(btn_contents, wxRight, 2*LAYOUT_X_MARGIN); | |
187 | c->width.Absolute(3*BUTTON_WIDTH); | |
188 | c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN); | |
189 | c->height.AsIs(); | |
190 | m_combo->SetConstraints(c); | |
191 | ||
192 | c = new wxLayoutConstraints; | |
193 | c->left.SameAs(m_combo, wxRight, 2*LAYOUT_X_MARGIN); | |
194 | c->width.Absolute(BUTTON_WIDTH); | |
195 | c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN); | |
196 | c->height.AsIs(); | |
197 | btn_search->SetConstraints(c); | |
198 | ||
199 | ||
200 | c = new wxLayoutConstraints; | |
201 | c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN); | |
202 | c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN); | |
203 | c->top.SameAs(btn_back, wxBottom, 2*LAYOUT_Y_MARGIN); | |
29ea4a29 KB |
204 | c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN); |
205 | m_htmlwin->SetConstraints(c); | |
206 | SetAutoLayout(TRUE); | |
8dd71e2b KB |
207 | CreateStatusBar(); |
208 | ||
209 | m_htmlwin->SetRelatedFrame(this, title); | |
210 | m_htmlwin->SetRelatedStatusBar(0); | |
211 | m_htmlwin->AddFilter(m_filter); | |
212 | ||
29ea4a29 KB |
213 | Show(TRUE); |
214 | } | |
215 | ||
216 | wxHelpFrame::~wxHelpFrame() | |
217 | { | |
218 | } | |
219 | ||
220 | void | |
221 | wxHelpFrame::OnClose(wxCloseEvent &ev) | |
222 | { | |
223 | wxASSERT(m_controller); | |
224 | m_controller->m_Frame = NULL; | |
225 | bool newFrame; | |
226 | int x,y; | |
227 | GetPosition(&x,&y); | |
228 | ||
229 | m_controller->GetFrameParameters(NULL, NULL, &newFrame); | |
230 | m_controller->SetFrameParameters(GetTitle(), GetSize(), | |
231 | wxPoint(x,y), | |
232 | newFrame); | |
233 | Destroy(); | |
234 | } | |
235 | ||
236 | wxHelpControllerHtml::wxHelpControllerHtml(void) | |
237 | { | |
238 | m_Frame = NULL; | |
239 | m_offset = 0; | |
4f84c635 | 240 | |
8dd71e2b | 241 | SetFrameParameters(_("Help: %s"), |
29ea4a29 KB |
242 | wxSize(FRAME_WIDTH, FRAME_HEIGHT), |
243 | wxDefaultPosition); | |
244 | } | |
245 | ||
246 | wxHelpControllerHtml::~wxHelpControllerHtml(void) | |
247 | { | |
248 | if(m_Frame && ! m_NewFrameEachTime) | |
249 | m_Frame->Close(); | |
250 | } | |
251 | ||
252 | ||
253 | #ifdef __WXMSW__ | |
254 | # define SEP '\\' | |
255 | #else | |
256 | # define SEP '/' | |
257 | #endif | |
258 | ||
259 | bool | |
260 | wxHelpControllerHtml::DisplayHelp(wxString const &relativeURL) | |
261 | { | |
262 | wxBusyCursor b; // display a busy cursor | |
263 | ||
264 | wxString url; | |
265 | url << m_MapFile << SEP<< relativeURL; | |
266 | if(! m_Frame || m_NewFrameEachTime) | |
267 | { | |
268 | m_Frame = new wxHelpFrame(NULL, -1, m_FrameTitle, | |
269 | m_FramePosition+wxPoint(m_offset,m_offset), | |
270 | m_FrameSize, | |
271 | this); | |
272 | if(m_NewFrameEachTime) | |
273 | { | |
274 | m_offset += OFFSET; | |
275 | if(m_offset > 200) | |
276 | m_offset = 0; | |
277 | } | |
4f84c635 | 278 | |
29ea4a29 | 279 | } |
8dd71e2b | 280 | m_Frame->Raise(); |
29ea4a29 KB |
281 | return m_Frame->LoadPage(url); |
282 | } | |
283 | ||
284 | ||
285 | void | |
286 | wxHelpControllerHtml::SetFrameParameters(const wxString &title, | |
287 | const wxSize &size, | |
288 | const wxPoint &pos, | |
289 | bool newFrame) | |
290 | { | |
291 | m_FrameTitle = title; | |
292 | m_FrameSize = size; | |
293 | m_FramePosition = pos; | |
294 | m_NewFrameEachTime = newFrame; | |
295 | } | |
296 | ||
4ba80ec7 | 297 | wxFrame * |
259d1674 VZ |
298 | wxHelpControllerHtml::GetFrameParameters(wxSize *size, |
299 | wxPoint *pos, | |
300 | bool *newframe) | |
29ea4a29 KB |
301 | { |
302 | if(size) *size = m_FrameSize; | |
303 | if(pos) *pos = m_FramePosition; | |
304 | if(newframe) *newframe = m_NewFrameEachTime; | |
4ba80ec7 | 305 | return m_Frame; |
29ea4a29 | 306 | } |
4f84c635 VZ |
307 | |
308 | #endif // wxUSE_HTML |