]> git.saurik.com Git - wxWidgets.git/blame - src/generic/tipdlg.cpp
check for NULL pointer in OnMeasureItem() as well as in OnDrawItem()
[wxWidgets.git] / src / generic / tipdlg.cpp
CommitLineData
c50f1fb9
VZ
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
65571936 9// Licence: wxWindows licence
c50f1fb9
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
dfe1eee3 21 #pragma implementation "tipdlg.h"
c50f1fb9
VZ
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"
c50f1fb9 37 #include "wx/dialog.h"
dfe1eee3
VZ
38 #include "wx/icon.h"
39 #include "wx/intl.h"
dfe1eee3 40 #include "wx/textctrl.h"
3bb0b01c 41 #include "wx/statbmp.h"
9c884972 42 #include "wx/stattext.h"
92afa2b1 43 #include "wx/sizer.h"
c50f1fb9
VZ
44#endif // WX_PRECOMP
45
46#include "wx/statline.h"
91f43f15 47#include "wx/artprov.h"
94f53923 48#include "wx/settings.h"
c50f1fb9
VZ
49
50#include "wx/tipdlg.h"
51
52// ----------------------------------------------------------------------------
53// constants
54// ----------------------------------------------------------------------------
55
d8d18184 56static const int wxID_NEXT_TIP = 32000; // whatever
c50f1fb9 57
8b3d02a1
WS
58// ---------------------------------------------------------------------------
59// macros
60// ---------------------------------------------------------------------------
61
62/* Macro for avoiding #ifdefs when value have to be different depending on size of
9a357011 63 device we display on - take it from something like wxDesktopPolicy in the future
8b3d02a1
WS
64 */
65
66#if defined(__SMARTPHONE__)
67 #define wxLARGESMALL(large,small) small
68#else
69 #define wxLARGESMALL(large,small) large
70#endif
71
c50f1fb9
VZ
72// ----------------------------------------------------------------------------
73// private classes
74// ----------------------------------------------------------------------------
75
76// an implementation which takes the tips from the text file - each line
77// represents a tip
12f190b0 78class WXDLLIMPEXP_ADV wxFileTipProvider : public wxTipProvider
c50f1fb9
VZ
79{
80public:
81 wxFileTipProvider(const wxString& filename, size_t currentTip);
82
83 virtual wxString GetTip();
84
85private:
86 wxTextFile m_textfile;
fc7a2a60
VZ
87
88 DECLARE_NO_COPY_CLASS(wxFileTipProvider)
c50f1fb9
VZ
89};
90
91#ifdef __WIN32__
92// TODO an implementation which takes the tips from the given registry key
12f190b0 93class WXDLLIMPEXP_ADV wxRegTipProvider : public wxTipProvider
c50f1fb9
VZ
94{
95public:
96 wxRegTipProvider(const wxString& keyname);
97
98 virtual wxString GetTip();
99};
06d7fdef
RD
100
101// Empty implementation for now to keep the linker happy
102wxString wxRegTipProvider::GetTip()
103{
abceee76 104 return wxEmptyString;
06d7fdef
RD
105}
106
c50f1fb9
VZ
107#endif // __WIN32__
108
109// the dialog we show in wxShowTip()
12f190b0 110class WXDLLIMPEXP_ADV wxTipDialog : public wxDialog
c50f1fb9
VZ
111{
112public:
113 wxTipDialog(wxWindow *parent,
114 wxTipProvider *tipProvider,
115 bool showAtStartup);
116
dabbc6a5 117 // the tip dialog has "Show tips on startup" checkbox - return true if it
c50f1fb9
VZ
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
127private:
128 wxTipProvider *m_tipProvider;
129
130 wxTextCtrl *m_text;
131 wxCheckBox *m_checkbox;
132
133 DECLARE_EVENT_TABLE()
22f3361e 134 DECLARE_NO_COPY_CLASS(wxTipDialog)
c50f1fb9
VZ
135};
136
137// ============================================================================
138// implementation
139// ============================================================================
140
141// ----------------------------------------------------------------------------
142// wxFileTipProvider
143// ----------------------------------------------------------------------------
144
145wxFileTipProvider::wxFileTipProvider(const wxString& filename,
146 size_t currentTip)
147 : wxTipProvider(currentTip), m_textfile(filename)
148{
149 m_textfile.Open();
150}
151
152wxString wxFileTipProvider::GetTip()
153{
154 size_t count = m_textfile.GetLineCount();
155 if ( !count )
c50f1fb9 156 {
70373b5a 157 return _("Tips not available, sorry!");
c50f1fb9 158 }
dabbc6a5 159
70373b5a
JS
160 wxString tip;
161
162 // Comments start with a # symbol.
163 // Loop reading lines until get the first one that isn't a comment.
dabbc6a5
DS
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
70373b5a
JS
167 // them all via PreprecessTip().
168 for ( size_t i=0; i < count; i++ )
dabbc6a5
DS
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
70373b5a
JS
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;
dabbc6a5
DS
178 }
179
70373b5a
JS
180 // Read the tip, and increment the current tip counter.
181 tip = m_textfile.GetLine(m_currentTip++);
dabbc6a5
DS
182
183 // Allow a derived class's overrided virtual to modify the tip
70373b5a 184 // now if so desired.
dabbc6a5
DS
185 tip = PreprocessTip(tip);
186
70373b5a
JS
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 }
dabbc6a5 194
70373b5a
JS
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('\"'));
dabbc6a5
DS
201 // ...and replace escaped quotes
202 tip.Replace(wxT("\\\""), wxT("\""));
203 }
204
205 return tip;
c50f1fb9
VZ
206}
207
208// ----------------------------------------------------------------------------
209// wxTipDialog
210// ----------------------------------------------------------------------------
211
212BEGIN_EVENT_TABLE(wxTipDialog, wxDialog)
65fd5cb0 213 EVT_BUTTON(wxID_NEXT_TIP, wxTipDialog::OnNextTip)
eb3e6de3 214 EVT_BUTTON(wxID_CLOSE, wxTipDialog::OnCancel)
c50f1fb9
VZ
215END_EVENT_TABLE()
216
217wxTipDialog::wxTipDialog(wxWindow *parent,
218 wxTipProvider *tipProvider,
219 bool showAtStartup)
dabbc6a5 220 : wxDialog(parent, wxID_ANY, _("Tip of the Day"),
c50f1fb9 221 wxDefaultPosition, wxDefaultSize,
aa66250b
JS
222 wxDEFAULT_DIALOG_STYLE
223#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
224 | wxRESIZE_BORDER
225#endif
226 )
c50f1fb9
VZ
227{
228 m_tipProvider = tipProvider;
94f53923 229 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
c50f1fb9 230
92afa2b1 231 // 1) create all controls in tab order
abceee76 232
9a357011 233 // smart phones does not support or do not waste space for wxButtons
8b3d02a1 234#ifndef __SMARTPHONE__
eb3e6de3 235 wxButton *btnClose = new wxButton(this, wxID_CLOSE);
8b3d02a1 236#endif
abceee76 237
dabbc6a5 238 m_checkbox = new wxCheckBox(this, wxID_ANY, _("&Show tips at startup"));
92afa2b1 239 m_checkbox->SetValue(showAtStartup);
abceee76 240
9a357011 241 // smart phones does not support or do not waste space for wxButtons
8b3d02a1 242#ifndef __SMARTPHONE__
e16ebee6 243 wxButton *btnNext = new wxButton(this, wxID_NEXT_TIP, _("&Next Tip"));
8b3d02a1 244#endif
c50f1fb9 245
4e4688be
VS
246 wxStaticText *text = new wxStaticText(this, wxID_ANY, _("Did you know..."));
247
94f53923
JS
248 if (!isPda)
249 {
250 wxFont font = text->GetFont();
251 font.SetPointSize(int(1.6 * font.GetPointSize()));
252 font.SetWeight(wxFONTWEIGHT_BOLD);
253 text->SetFont(font);
254 }
c50f1fb9 255
dabbc6a5 256 m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
92afa2b1 257 wxDefaultPosition, wxSize(200, 160),
abceee76
VZ
258 wxTE_MULTILINE |
259 wxTE_READONLY |
58ec2255 260 wxTE_NO_VSCROLL |
e111012a 261 wxTE_RICH2 | // a hack to get rid of vert scrollbar
aa66250b
JS
262 wxDEFAULT_CONTROL_BORDER
263 );
e16ebee6 264#if defined(__WXMSW__)
58ec2255 265 m_text->SetFont(wxFont(12, wxSWISS, wxNORMAL, wxNORMAL));
e16ebee6 266#endif
c50f1fb9 267
e225b106 268//#if defined(__WXPM__)
1c4fbbf1
DW
269 //
270 // The only way to get icons into an OS/2 static bitmap control
271 //
e225b106 272// wxBitmap vBitmap;
1c4fbbf1 273
e225b106
DW
274// vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
275// // and for StatBmp's under OS/2 it MUST be a valid resource ID.
276//
dabbc6a5 277// wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
e225b106
DW
278//
279//#else
1c4fbbf1 280
91f43f15 281 wxIcon icon = wxArtProvider::GetIcon(wxART_TIP, wxART_CMN_DIALOG);
dabbc6a5 282 wxStaticBitmap *bmp = new wxStaticBitmap(this, wxID_ANY, icon);
c50f1fb9 283
e225b106 284//#endif
1c4fbbf1 285
92afa2b1 286 // 2) put them in boxes
c50f1fb9 287
92afa2b1 288 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
abceee76 289
92afa2b1
RR
290 wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
291 icon_text->Add( bmp, 0, wxCENTER );
8b3d02a1
WS
292 icon_text->Add( text, 1, wxCENTER | wxLEFT, wxLARGESMALL(20,0) );
293 topsizer->Add( icon_text, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
abceee76 294
8b3d02a1 295 topsizer->Add( m_text, 1, wxEXPAND | wxLEFT|wxRIGHT, wxLARGESMALL(10,0) );
92afa2b1
RR
296
297 wxBoxSizer *bottom = new wxBoxSizer( wxHORIZONTAL );
94f53923
JS
298 if (isPda)
299 topsizer->Add( m_checkbox, 0, wxCENTER|wxTOP );
300 else
301 bottom->Add( m_checkbox, 0, wxCENTER );
8b3d02a1 302
9a357011
WS
303 // smart phones does not support or do not waste space for wxButtons
304#ifdef __SMARTPHONE__
305 SetRightMenu(wxID_NEXT_TIP, _("Next"));
eb3e6de3 306 SetLeftMenu(wxID_CLOSE);
9a357011 307#else
94f53923
JS
308 if (!isPda)
309 bottom->Add( 10,10,1 );
8b3d02a1
WS
310 bottom->Add( btnNext, 0, wxCENTER | wxLEFT, wxLARGESMALL(10,0) );
311 bottom->Add( btnClose, 0, wxCENTER | wxLEFT, wxLARGESMALL(10,0) );
312#endif
313
94f53923
JS
314 if (isPda)
315 topsizer->Add( bottom, 0, wxCENTER | wxALL, 5 );
316 else
317 topsizer->Add( bottom, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
c50f1fb9
VZ
318
319 SetTipText();
abceee76 320
92afa2b1 321 SetSizer( topsizer );
abceee76 322
aa66250b 323#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
92afa2b1
RR
324 topsizer->SetSizeHints( this );
325 topsizer->Fit( this );
c50f1fb9
VZ
326
327 Centre(wxBOTH | wxCENTER_FRAME);
aa66250b 328#endif
c50f1fb9
VZ
329}
330
331// ----------------------------------------------------------------------------
332// our public interface
333// ----------------------------------------------------------------------------
334
335wxTipProvider *wxCreateFileTipProvider(const wxString& filename,
336 size_t currentTip)
337{
338 return new wxFileTipProvider(filename, currentTip);
339}
340
341bool wxShowTip(wxWindow *parent,
342 wxTipProvider *tipProvider,
343 bool showAtStartup)
344{
345 wxTipDialog dlg(parent, tipProvider, showAtStartup);
346 dlg.ShowModal();
347
348 return dlg.ShowTipsOnStartup();
349}
350
351#endif // wxUSE_STARTUP_TIPS
352