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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_STARTUP_TIPS
30 #include "wx/button.h"
31 #include "wx/checkbox.h"
32 #include "wx/statbox.h"
33 #include "wx/dialog.h"
36 #include "wx/textctrl.h"
37 #include "wx/statbmp.h"
38 #include "wx/stattext.h"
42 #include "wx/statline.h"
43 #include "wx/artprov.h"
44 #include "wx/settings.h"
46 #include "wx/tipdlg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 static const int wxID_NEXT_TIP
= 32000; // whatever
54 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
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
62 #if defined(__SMARTPHONE__)
63 #define wxLARGESMALL(large,small) small
65 #define wxLARGESMALL(large,small) large
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // an implementation which takes the tips from the text file - each line
74 class WXDLLIMPEXP_ADV wxFileTipProvider
: public wxTipProvider
77 wxFileTipProvider(const wxString
& filename
, size_t currentTip
);
79 virtual wxString
GetTip();
82 wxTextFile m_textfile
;
84 DECLARE_NO_COPY_CLASS(wxFileTipProvider
)
88 // TODO an implementation which takes the tips from the given registry key
89 class WXDLLIMPEXP_ADV wxRegTipProvider
: public wxTipProvider
92 wxRegTipProvider(const wxString
& keyname
);
94 virtual wxString
GetTip();
97 // Empty implementation for now to keep the linker happy
98 wxString
wxRegTipProvider::GetTip()
100 return wxEmptyString
;
105 // the dialog we show in wxShowTip()
106 class WXDLLIMPEXP_ADV wxTipDialog
: public wxDialog
109 wxTipDialog(wxWindow
*parent
,
110 wxTipProvider
*tipProvider
,
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(); }
117 // sets the (next) tip text
118 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
120 // "Next" button handler
121 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
124 wxTipProvider
*m_tipProvider
;
127 wxCheckBox
*m_checkbox
;
129 DECLARE_EVENT_TABLE()
130 DECLARE_NO_COPY_CLASS(wxTipDialog
)
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
143 : wxTipProvider(currentTip
), m_textfile(filename
)
148 wxString
wxFileTipProvider::GetTip()
150 size_t count
= m_textfile
.GetLineCount();
153 return _("Tips not available, sorry!");
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
++ )
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
)
172 // .. and if so, wrap back to line 0.
176 // Read the tip, and increment the current tip counter.
177 tip
= m_textfile
.GetLine(m_currentTip
++);
179 // Allow a derived class's overrided virtual to modify the tip
180 // now if so desired.
181 tip
= PreprocessTip(tip
);
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
) )
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
))
195 //...and strip off the trailing '")'...
196 tip
= tip
.BeforeLast(wxT('\"'));
197 // ...and replace escaped quotes
198 tip
.Replace(wxT("\\\""), wxT("\""));
200 // and translate it as requested
201 tip
= wxGetTranslation(tip
);
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
212 EVT_BUTTON(wxID_NEXT_TIP
, wxTipDialog::OnNextTip
)
213 EVT_BUTTON(wxID_CLOSE
, wxTipDialog::OnCancel
)
216 wxTipDialog::wxTipDialog(wxWindow
*parent
,
217 wxTipProvider
*tipProvider
,
219 : wxDialog(parent
, wxID_ANY
, _("Tip of the Day"),
220 wxDefaultPosition
, wxDefaultSize
,
221 wxDEFAULT_DIALOG_STYLE
222 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
227 m_tipProvider
= tipProvider
;
228 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
230 // 1) create all controls in tab order
232 // smart phones does not support or do not waste space for wxButtons
233 #ifndef __SMARTPHONE__
234 wxButton
*btnClose
= new wxButton(this, wxID_CLOSE
);
237 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _("&Show tips at startup"));
238 m_checkbox
->SetValue(showAtStartup
);
240 // smart phones does not support or do not waste space for wxButtons
241 #ifndef __SMARTPHONE__
242 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next Tip"));
245 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
, _("Did you know..."));
249 wxFont font
= text
->GetFont();
250 font
.SetPointSize(int(1.6 * font
.GetPointSize()));
251 font
.SetWeight(wxFONTWEIGHT_BOLD
);
255 m_text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
256 wxDefaultPosition
, wxSize(200, 160),
260 wxTE_RICH2
| // a hack to get rid of vert scrollbar
261 wxDEFAULT_CONTROL_BORDER
263 #if defined(__WXMSW__)
264 m_text
->SetFont(wxFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
));
267 //#if defined(__WXPM__)
269 // The only way to get icons into an OS/2 static bitmap control
273 // vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
274 // // and for StatBmp's under OS/2 it MUST be a valid resource ID.
276 // wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
280 wxIcon icon
= wxArtProvider::GetIcon(wxART_TIP
, wxART_CMN_DIALOG
);
281 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, wxID_ANY
, icon
);
285 // 2) put them in boxes
287 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
289 wxBoxSizer
*icon_text
= new wxBoxSizer( wxHORIZONTAL
);
290 icon_text
->Add( bmp
, 0, wxCENTER
);
291 icon_text
->Add( text
, 1, wxCENTER
| wxLEFT
, wxLARGESMALL(20,0) );
292 topsizer
->Add( icon_text
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
294 topsizer
->Add( m_text
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, wxLARGESMALL(10,0) );
296 wxBoxSizer
*bottom
= new wxBoxSizer( wxHORIZONTAL
);
298 topsizer
->Add( m_checkbox
, 0, wxCENTER
|wxTOP
);
300 bottom
->Add( m_checkbox
, 0, wxCENTER
);
302 // smart phones does not support or do not waste space for wxButtons
303 #ifdef __SMARTPHONE__
304 SetRightMenu(wxID_NEXT_TIP
, _("Next"));
305 SetLeftMenu(wxID_CLOSE
);
308 bottom
->Add( 10,10,1 );
309 bottom
->Add( btnNext
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
310 bottom
->Add( btnClose
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
314 topsizer
->Add( bottom
, 0, wxCENTER
| wxALL
, 5 );
316 topsizer
->Add( bottom
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
320 SetSizer( topsizer
);
322 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
323 topsizer
->SetSizeHints( this );
324 topsizer
->Fit( this );
326 Centre(wxBOTH
| wxCENTER_FRAME
);
330 // ----------------------------------------------------------------------------
331 // our public interface
332 // ----------------------------------------------------------------------------
334 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
337 return new wxFileTipProvider(filename
, currentTip
);
340 bool wxShowTip(wxWindow
*parent
,
341 wxTipProvider
*tipProvider
,
344 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
347 return dlg
.ShowTipsOnStartup();
350 #endif // wxUSE_STARTUP_TIPS