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