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/settings.h"
41 #include "wx/textctrl.h"
42 #include "wx/statbmp.h"
43 #include "wx/stattext.h"
47 #include "wx/statline.h"
48 #include "wx/artprov.h"
50 #include "wx/tipdlg.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 static const int wxID_NEXT_TIP
= 32000; // whatever
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // an implementation which takes the tips from the text file - each line
64 class WXDLLIMPEXP_ADV wxFileTipProvider
: public wxTipProvider
67 wxFileTipProvider(const wxString
& filename
, size_t currentTip
);
69 virtual wxString
GetTip();
72 wxTextFile m_textfile
;
74 DECLARE_NO_COPY_CLASS(wxFileTipProvider
)
78 // TODO an implementation which takes the tips from the given registry key
79 class WXDLLIMPEXP_ADV wxRegTipProvider
: public wxTipProvider
82 wxRegTipProvider(const wxString
& keyname
);
84 virtual wxString
GetTip();
87 // Empty implementation for now to keep the linker happy
88 wxString
wxRegTipProvider::GetTip()
95 // the dialog we show in wxShowTip()
96 class WXDLLIMPEXP_ADV wxTipDialog
: public wxDialog
99 wxTipDialog(wxWindow
*parent
,
100 wxTipProvider
*tipProvider
,
103 // the tip dialog has "Show tips on startup" checkbox - return true if it
104 // was checked (or wasn't unchecked)
105 bool ShowTipsOnStartup() const { return m_checkbox
->GetValue(); }
107 // sets the (next) tip text
108 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
110 // "Next" button handler
111 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
114 wxTipProvider
*m_tipProvider
;
117 wxCheckBox
*m_checkbox
;
119 DECLARE_EVENT_TABLE()
120 DECLARE_NO_COPY_CLASS(wxTipDialog
)
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
133 : wxTipProvider(currentTip
), m_textfile(filename
)
138 wxString
wxFileTipProvider::GetTip()
140 size_t count
= m_textfile
.GetLineCount();
143 return _("Tips not available, sorry!");
148 // Comments start with a # symbol.
149 // Loop reading lines until get the first one that isn't a comment.
150 // The max number of loop executions is the number of lines in the
151 // textfile so that can't go into an eternal loop in the [oddball]
152 // case of a comment-only tips file, or the developer has vetoed
153 // them all via PreprecessTip().
154 for ( size_t i
=0; i
< count
; i
++ )
156 // The current tip may be at the last line of the textfile, (or
157 // past it, if the number of lines in the textfile changed, such
158 // as changing to a different textfile, with less tips). So check
159 // to see at last line of text file, (or past it)...
160 if ( m_currentTip
>= count
)
162 // .. and if so, wrap back to line 0.
166 // Read the tip, and increment the current tip counter.
167 tip
= m_textfile
.GetLine(m_currentTip
++);
169 // Allow a derived class's overrided virtual to modify the tip
170 // now if so desired.
171 tip
= PreprocessTip(tip
);
173 // Break if tip isn't a comment, and isn't an empty string
174 // (or only stray space characters).
175 if ( !tip
.StartsWith(wxT("#")) && (tip
.Trim() != wxEmptyString
) )
181 // If tip starts with '_(', then it is a gettext string of format
182 // _("My \"global\" tip text") so first strip off the leading '_("'...
183 if ( tip
.StartsWith(wxT("_(\"" ), &tip
))
185 //...and strip off the trailing '")'...
186 tip
= tip
.BeforeLast(wxT('\"'));
187 // ...and replace escaped quotes
188 tip
.Replace(wxT("\\\""), wxT("\""));
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
199 EVT_BUTTON(wxID_NEXT_TIP
, wxTipDialog::OnNextTip
)
202 wxTipDialog::wxTipDialog(wxWindow
*parent
,
203 wxTipProvider
*tipProvider
,
205 : wxDialog(parent
, wxID_ANY
, _("Tip of the Day"),
206 wxDefaultPosition
, wxDefaultSize
,
207 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
209 m_tipProvider
= tipProvider
;
211 // 1) create all controls in tab order
213 wxButton
*btnClose
= new wxButton(this, wxID_CANCEL
, _("&Close"));
215 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _("&Show tips at startup"));
216 m_checkbox
->SetValue(showAtStartup
);
218 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next Tip"));
220 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
, _("Did you know..."), wxDefaultPosition
, wxSize(-1,30) );
221 #if defined(__WXMSW__) || defined(__WXPM__)
222 text
->SetFont(wxFont(16, wxSWISS
, wxNORMAL
, wxBOLD
));
224 text
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxBOLD
));
227 // text->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
229 m_text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
230 wxDefaultPosition
, wxSize(200, 160),
234 wxTE_RICH
| // a hack to get rid of vert scrollbar
236 #if defined(__WXMSW__)
237 m_text
->SetFont(wxFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
));
239 m_text
->SetFont(wxFont(14, wxSWISS
, wxNORMAL
, wxNORMAL
));
242 //#if defined(__WXPM__)
244 // The only way to get icons into an OS/2 static bitmap control
248 // vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
249 // // and for StatBmp's under OS/2 it MUST be a valid resource ID.
251 // wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
255 wxIcon icon
= wxArtProvider::GetIcon(wxART_TIP
, wxART_CMN_DIALOG
);
256 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, wxID_ANY
, icon
);
260 // 2) put them in boxes
262 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
264 wxBoxSizer
*icon_text
= new wxBoxSizer( wxHORIZONTAL
);
265 icon_text
->Add( bmp
, 0, wxCENTER
);
266 icon_text
->Add( text
, 1, wxCENTER
| wxLEFT
, 20 );
267 topsizer
->Add( icon_text
, 0, wxEXPAND
| wxALL
, 10 );
269 topsizer
->Add( m_text
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
271 wxBoxSizer
*bottom
= new wxBoxSizer( wxHORIZONTAL
);
272 bottom
->Add( m_checkbox
, 0, wxCENTER
);
273 bottom
->Add( 10,10,1 );
274 bottom
->Add( btnNext
, 0, wxCENTER
| wxLEFT
, 10 );
275 bottom
->Add( btnClose
, 0, wxCENTER
| wxLEFT
, 10 );
276 topsizer
->Add( bottom
, 0, wxEXPAND
| wxALL
, 10 );
280 SetSizer( topsizer
);
282 topsizer
->SetSizeHints( this );
283 topsizer
->Fit( this );
285 Centre(wxBOTH
| wxCENTER_FRAME
);
289 // ----------------------------------------------------------------------------
290 // our public interface
291 // ----------------------------------------------------------------------------
293 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
296 return new wxFileTipProvider(filename
, currentTip
);
299 bool wxShowTip(wxWindow
*parent
,
300 wxTipProvider
*tipProvider
,
303 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
306 return dlg
.ShowTipsOnStartup();
309 #endif // wxUSE_STARTUP_TIPS