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