fixed strange focus order in wxTipDialog and set initial focus to the checkbox instea...
[wxWidgets.git] / src / generic / tipdlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/tipdlg.cpp
3 // Purpose: implementation of wxTipDialog
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 28.06.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STARTUP_TIPS
28
29 #ifndef WX_PRECOMP
30 #include "wx/button.h"
31 #include "wx/checkbox.h"
32 #include "wx/statbox.h"
33 #include "wx/dialog.h"
34 #include "wx/icon.h"
35 #include "wx/intl.h"
36 #include "wx/textctrl.h"
37 #include "wx/statbmp.h"
38 #include "wx/stattext.h"
39 #include "wx/sizer.h"
40 #include "wx/settings.h"
41 #endif // WX_PRECOMP
42
43 #include "wx/statline.h"
44 #include "wx/artprov.h"
45
46 #include "wx/tipdlg.h"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 static const int wxID_NEXT_TIP = 32000; // whatever
53
54 // ---------------------------------------------------------------------------
55 // macros
56 // ---------------------------------------------------------------------------
57
58 /* Macro for avoiding #ifdefs when value have to be different depending on size of
59 device we display on - take it from something like wxDesktopPolicy in the future
60 */
61
62 #if defined(__SMARTPHONE__)
63 #define wxLARGESMALL(large,small) small
64 #else
65 #define wxLARGESMALL(large,small) large
66 #endif
67
68 // ----------------------------------------------------------------------------
69 // private classes
70 // ----------------------------------------------------------------------------
71
72 // an implementation which takes the tips from the text file - each line
73 // represents a tip
74 class WXDLLIMPEXP_ADV wxFileTipProvider : public wxTipProvider
75 {
76 public:
77 wxFileTipProvider(const wxString& filename, size_t currentTip);
78
79 virtual wxString GetTip();
80
81 private:
82 wxTextFile m_textfile;
83
84 DECLARE_NO_COPY_CLASS(wxFileTipProvider)
85 };
86
87 #ifdef __WIN32__
88 // TODO an implementation which takes the tips from the given registry key
89 class WXDLLIMPEXP_ADV wxRegTipProvider : public wxTipProvider
90 {
91 public:
92 wxRegTipProvider(const wxString& keyname);
93
94 virtual wxString GetTip();
95 };
96
97 // Empty implementation for now to keep the linker happy
98 wxString wxRegTipProvider::GetTip()
99 {
100 return wxEmptyString;
101 }
102
103 #endif // __WIN32__
104
105 // the dialog we show in wxShowTip()
106 class WXDLLIMPEXP_ADV wxTipDialog : public wxDialog
107 {
108 public:
109 wxTipDialog(wxWindow *parent,
110 wxTipProvider *tipProvider,
111 bool showAtStartup);
112
113 // the tip dialog has "Show tips on startup" checkbox - return true if it
114 // was checked (or wasn't unchecked)
115 bool ShowTipsOnStartup() const { return m_checkbox->GetValue(); }
116
117 // sets the (next) tip text
118 void SetTipText() { m_text->SetValue(m_tipProvider->GetTip()); }
119
120 // "Next" button handler
121 void OnNextTip(wxCommandEvent& WXUNUSED(event)) { SetTipText(); }
122
123 private:
124 wxTipProvider *m_tipProvider;
125
126 wxTextCtrl *m_text;
127 wxCheckBox *m_checkbox;
128
129 DECLARE_EVENT_TABLE()
130 DECLARE_NO_COPY_CLASS(wxTipDialog)
131 };
132
133 // ============================================================================
134 // implementation
135 // ============================================================================
136
137 // ----------------------------------------------------------------------------
138 // wxFileTipProvider
139 // ----------------------------------------------------------------------------
140
141 wxFileTipProvider::wxFileTipProvider(const wxString& filename,
142 size_t currentTip)
143 : wxTipProvider(currentTip), m_textfile(filename)
144 {
145 m_textfile.Open();
146 }
147
148 wxString wxFileTipProvider::GetTip()
149 {
150 size_t count = m_textfile.GetLineCount();
151 if ( !count )
152 {
153 return _("Tips not available, sorry!");
154 }
155
156 wxString tip;
157
158 // Comments start with a # symbol.
159 // Loop reading lines until get the first one that isn't a comment.
160 // The max number of loop executions is the number of lines in the
161 // textfile so that can't go into an eternal loop in the [oddball]
162 // case of a comment-only tips file, or the developer has vetoed
163 // them all via PreprecessTip().
164 for ( size_t i=0; i < count; i++ )
165 {
166 // The current tip may be at the last line of the textfile, (or
167 // past it, if the number of lines in the textfile changed, such
168 // as changing to a different textfile, with less tips). So check
169 // to see at last line of text file, (or past it)...
170 if ( m_currentTip >= count )
171 {
172 // .. and if so, wrap back to line 0.
173 m_currentTip = 0;
174 }
175
176 // Read the tip, and increment the current tip counter.
177 tip = m_textfile.GetLine(m_currentTip++);
178
179 // Allow a derived class's overrided virtual to modify the tip
180 // now if so desired.
181 tip = PreprocessTip(tip);
182
183 // Break if tip isn't a comment, and isn't an empty string
184 // (or only stray space characters).
185 if ( !tip.StartsWith(wxT("#")) && (tip.Trim() != wxEmptyString) )
186 {
187 break;
188 }
189 }
190
191 // If tip starts with '_(', then it is a gettext string of format
192 // _("My \"global\" tip text") so first strip off the leading '_("'...
193 if ( tip.StartsWith(wxT("_(\"" ), &tip))
194 {
195 //...and strip off the trailing '")'...
196 tip = tip.BeforeLast(wxT('\"'));
197 // ...and replace escaped quotes
198 tip.Replace(wxT("\\\""), wxT("\""));
199
200 // and translate it as requested
201 tip = wxGetTranslation(tip);
202 }
203
204 return tip;
205 }
206
207 // ----------------------------------------------------------------------------
208 // wxTipDialog
209 // ----------------------------------------------------------------------------
210
211 BEGIN_EVENT_TABLE(wxTipDialog, wxDialog)
212 EVT_BUTTON(wxID_NEXT_TIP, wxTipDialog::OnNextTip)
213 END_EVENT_TABLE()
214
215 wxTipDialog::wxTipDialog(wxWindow *parent,
216 wxTipProvider *tipProvider,
217 bool showAtStartup)
218 : wxDialog(GetParentForModalDialog(parent), wxID_ANY, _("Tip of the Day"),
219 wxDefaultPosition, wxDefaultSize,
220 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
221 )
222 {
223 m_tipProvider = tipProvider;
224 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
225
226 // 1) create all controls in tab order
227
228 wxStaticText *text = new wxStaticText(this, wxID_ANY, _("Did you know..."));
229
230 if (!isPda)
231 {
232 wxFont font = text->GetFont();
233 font.SetPointSize(int(1.6 * font.GetPointSize()));
234 font.SetWeight(wxFONTWEIGHT_BOLD);
235 text->SetFont(font);
236 }
237
238 m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
239 wxDefaultPosition, wxSize(200, 160),
240 wxTE_MULTILINE |
241 wxTE_READONLY |
242 wxTE_NO_VSCROLL |
243 wxTE_RICH2 | // a hack to get rid of vert scrollbar
244 wxDEFAULT_CONTROL_BORDER
245 );
246 #if defined(__WXMSW__)
247 m_text->SetFont(wxFont(12, wxSWISS, wxNORMAL, wxNORMAL));
248 #endif
249
250 //#if defined(__WXPM__)
251 //
252 // The only way to get icons into an OS/2 static bitmap control
253 //
254 // wxBitmap vBitmap;
255
256 // vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
257 // // and for StatBmp's under OS/2 it MUST be a valid resource ID.
258 //
259 // wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
260 //
261 //#else
262
263 wxIcon icon = wxArtProvider::GetIcon(wxART_TIP, wxART_CMN_DIALOG);
264 wxStaticBitmap *bmp = new wxStaticBitmap(this, wxID_ANY, icon);
265
266 //#endif
267
268 m_checkbox = new wxCheckBox(this, wxID_ANY, _("&Show tips at startup"));
269 m_checkbox->SetValue(showAtStartup);
270 m_checkbox->SetFocus();
271
272 // smart phones does not support or do not waste space for wxButtons
273 #ifndef __SMARTPHONE__
274 wxButton *btnNext = new wxButton(this, wxID_NEXT_TIP, _("&Next Tip"));
275 #endif
276
277 // smart phones does not support or do not waste space for wxButtons
278 #ifndef __SMARTPHONE__
279 wxButton *btnClose = new wxButton(this, wxID_CLOSE);
280 SetAffirmativeId(wxID_CLOSE);
281 #endif
282
283
284 // 2) put them in boxes
285
286 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
287
288 wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
289 icon_text->Add( bmp, 0, wxCENTER );
290 icon_text->Add( text, 1, wxCENTER | wxLEFT, wxLARGESMALL(20,0) );
291 topsizer->Add( icon_text, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
292
293 topsizer->Add( m_text, 1, wxEXPAND | wxLEFT|wxRIGHT, wxLARGESMALL(10,0) );
294
295 wxBoxSizer *bottom = new wxBoxSizer( wxHORIZONTAL );
296 if (isPda)
297 topsizer->Add( m_checkbox, 0, wxCENTER|wxTOP );
298 else
299 bottom->Add( m_checkbox, 0, wxCENTER );
300
301 // smart phones does not support or do not waste space for wxButtons
302 #ifdef __SMARTPHONE__
303 SetRightMenu(wxID_NEXT_TIP, _("Next"));
304 SetLeftMenu(wxID_CLOSE);
305 #else
306 if (!isPda)
307 bottom->Add( 10,10,1 );
308 bottom->Add( btnNext, 0, wxCENTER | wxLEFT, wxLARGESMALL(10,0) );
309 bottom->Add( btnClose, 0, wxCENTER | wxLEFT, wxLARGESMALL(10,0) );
310 #endif
311
312 if (isPda)
313 topsizer->Add( bottom, 0, wxCENTER | wxALL, 5 );
314 else
315 topsizer->Add( bottom, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
316
317 SetTipText();
318
319 SetSizer( topsizer );
320
321 topsizer->SetSizeHints( this );
322 topsizer->Fit( this );
323
324 Centre(wxBOTH | wxCENTER_FRAME);
325 }
326
327 // ----------------------------------------------------------------------------
328 // our public interface
329 // ----------------------------------------------------------------------------
330
331 wxTipProvider *wxCreateFileTipProvider(const wxString& filename,
332 size_t currentTip)
333 {
334 return new wxFileTipProvider(filename, currentTip);
335 }
336
337 bool wxShowTip(wxWindow *parent,
338 wxTipProvider *tipProvider,
339 bool showAtStartup)
340 {
341 wxTipDialog dlg(parent, tipProvider, showAtStartup);
342 dlg.ShowModal();
343
344 return dlg.ShowTipsOnStartup();
345 }
346
347 #endif // wxUSE_STARTUP_TIPS