1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxTipDialog
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "tipdlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_STARTUP_TIPS
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/statbox.h"
37 #include "wx/dialog.h"
40 #include "wx/textctrl.h"
41 #include "wx/statbmp.h"
42 #include "wx/stattext.h"
46 #include "wx/statline.h"
47 #include "wx/artprov.h"
48 #include "wx/settings.h"
50 #include "wx/tipdlg.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 static const int wxID_NEXT_TIP
= 32000; // whatever
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
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
66 #if defined(__SMARTPHONE__)
67 #define wxLARGESMALL(large,small) small
69 #define wxLARGESMALL(large,small) large
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // an implementation which takes the tips from the text file - each line
78 class WXDLLIMPEXP_ADV wxFileTipProvider
: public wxTipProvider
81 wxFileTipProvider(const wxString
& filename
, size_t currentTip
);
83 virtual wxString
GetTip();
86 wxTextFile m_textfile
;
88 DECLARE_NO_COPY_CLASS(wxFileTipProvider
)
92 // TODO an implementation which takes the tips from the given registry key
93 class WXDLLIMPEXP_ADV wxRegTipProvider
: public wxTipProvider
96 wxRegTipProvider(const wxString
& keyname
);
98 virtual wxString
GetTip();
101 // Empty implementation for now to keep the linker happy
102 wxString
wxRegTipProvider::GetTip()
104 return wxEmptyString
;
109 // the dialog we show in wxShowTip()
110 class WXDLLIMPEXP_ADV wxTipDialog
: public wxDialog
113 wxTipDialog(wxWindow
*parent
,
114 wxTipProvider
*tipProvider
,
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(); }
121 // sets the (next) tip text
122 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
124 // "Next" button handler
125 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
128 wxTipProvider
*m_tipProvider
;
131 wxCheckBox
*m_checkbox
;
133 DECLARE_EVENT_TABLE()
134 DECLARE_NO_COPY_CLASS(wxTipDialog
)
137 // ============================================================================
139 // ============================================================================
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
147 : wxTipProvider(currentTip
), m_textfile(filename
)
152 wxString
wxFileTipProvider::GetTip()
154 size_t count
= m_textfile
.GetLineCount();
157 return _("Tips not available, sorry!");
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
++ )
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
)
176 // .. and if so, wrap back to line 0.
180 // Read the tip, and increment the current tip counter.
181 tip
= m_textfile
.GetLine(m_currentTip
++);
183 // Allow a derived class's overrided virtual to modify the tip
184 // now if so desired.
185 tip
= PreprocessTip(tip
);
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
) )
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
))
199 //...and strip off the trailing '")'...
200 tip
= tip
.BeforeLast(wxT('\"'));
201 // ...and replace escaped quotes
202 tip
.Replace(wxT("\\\""), wxT("\""));
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
213 EVT_BUTTON(wxID_NEXT_TIP
, wxTipDialog::OnNextTip
)
214 EVT_BUTTON(wxID_CLOSE
, wxTipDialog::OnCancel
)
217 wxTipDialog::wxTipDialog(wxWindow
*parent
,
218 wxTipProvider
*tipProvider
,
220 : wxDialog(parent
, wxID_ANY
, _("Tip of the Day"),
221 wxDefaultPosition
, wxDefaultSize
,
222 wxDEFAULT_DIALOG_STYLE
223 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
228 m_tipProvider
= tipProvider
;
229 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
231 // 1) create all controls in tab order
233 // smart phones does not support or do not waste space for wxButtons
234 #ifndef __SMARTPHONE__
235 wxButton
*btnClose
= new wxButton(this, wxID_CLOSE
);
238 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _("&Show tips at startup"));
239 m_checkbox
->SetValue(showAtStartup
);
241 // smart phones does not support or do not waste space for wxButtons
242 #ifndef __SMARTPHONE__
243 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next Tip"));
246 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
, _("Did you know..."));
250 wxFont font
= text
->GetFont();
251 font
.SetPointSize(int(1.6 * font
.GetPointSize()));
252 font
.SetWeight(wxFONTWEIGHT_BOLD
);
256 m_text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
257 wxDefaultPosition
, wxSize(200, 160),
261 wxTE_RICH
| // a hack to get rid of vert scrollbar
262 wxDEFAULT_CONTROL_BORDER
264 #if defined(__WXMSW__)
265 m_text
->SetFont(wxFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
));
268 //#if defined(__WXPM__)
270 // The only way to get icons into an OS/2 static bitmap control
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.
277 // wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
281 wxIcon icon
= wxArtProvider::GetIcon(wxART_TIP
, wxART_CMN_DIALOG
);
282 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, wxID_ANY
, icon
);
286 // 2) put them in boxes
288 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
290 wxBoxSizer
*icon_text
= new wxBoxSizer( wxHORIZONTAL
);
291 icon_text
->Add( bmp
, 0, wxCENTER
);
292 icon_text
->Add( text
, 1, wxCENTER
| wxLEFT
, wxLARGESMALL(20,0) );
293 topsizer
->Add( icon_text
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
295 topsizer
->Add( m_text
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, wxLARGESMALL(10,0) );
297 wxBoxSizer
*bottom
= new wxBoxSizer( wxHORIZONTAL
);
299 topsizer
->Add( m_checkbox
, 0, wxCENTER
|wxTOP
);
301 bottom
->Add( m_checkbox
, 0, wxCENTER
);
303 // smart phones does not support or do not waste space for wxButtons
304 #ifdef __SMARTPHONE__
305 SetRightMenu(wxID_NEXT_TIP
, _("Next"));
306 SetLeftMenu(wxID_CLOSE
);
309 bottom
->Add( 10,10,1 );
310 bottom
->Add( btnNext
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
311 bottom
->Add( btnClose
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
315 topsizer
->Add( bottom
, 0, wxCENTER
| wxALL
, 5 );
317 topsizer
->Add( bottom
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
321 SetSizer( topsizer
);
323 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
324 topsizer
->SetSizeHints( this );
325 topsizer
->Fit( this );
327 Centre(wxBOTH
| wxCENTER_FRAME
);
331 // ----------------------------------------------------------------------------
332 // our public interface
333 // ----------------------------------------------------------------------------
335 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
338 return new wxFileTipProvider(filename
, currentTip
);
341 bool wxShowTip(wxWindow
*parent
,
342 wxTipProvider
*tipProvider
,
345 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
348 return dlg
.ShowTipsOnStartup();
351 #endif // wxUSE_STARTUP_TIPS