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