1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxTipDialog
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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
= -100; // whatever
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // an implementation which takes the tips from the text file - each line
64 class WXDLLEXPORT wxFileTipProvider
: public wxTipProvider
67 wxFileTipProvider(const wxString
& filename
, size_t currentTip
);
69 virtual wxString
GetTip();
72 wxTextFile m_textfile
;
76 // TODO an implementation which takes the tips from the given registry key
77 class WXDLLEXPORT wxRegTipProvider
: public wxTipProvider
80 wxRegTipProvider(const wxString
& keyname
);
82 virtual wxString
GetTip();
85 // Empty implementation for now to keep the linker happy
86 wxString
wxRegTipProvider::GetTip()
93 // the dialog we show in wxShowTip()
94 class WXDLLEXPORT wxTipDialog
: public wxDialog
97 wxTipDialog(wxWindow
*parent
,
98 wxTipProvider
*tipProvider
,
101 // the tip dialog has "Show tips on startup" checkbox - return TRUE if it
102 // was checked (or wasn't unchecked)
103 bool ShowTipsOnStartup() const { return m_checkbox
->GetValue(); }
105 // sets the (next) tip text
106 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
108 // "Next" button handler
109 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
112 wxTipProvider
*m_tipProvider
;
115 wxCheckBox
*m_checkbox
;
117 DECLARE_EVENT_TABLE()
120 // ============================================================================
122 // ============================================================================
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
130 : wxTipProvider(currentTip
), m_textfile(filename
)
135 wxString
wxFileTipProvider::GetTip()
137 size_t count
= m_textfile
.GetLineCount();
140 return _("Tips not available, sorry!");
145 // Comments start with a # symbol.
146 // Loop reading lines until get the first one that isn't a comment.
147 // The max number of loop executions is the number of lines in the
148 // textfile so that can't go into an eternal loop in the [oddball]
149 // case of a comment-only tips file, or the developer has vetoed
150 // them all via PreprecessTip().
151 for ( size_t i
=0; i
< count
; i
++ )
153 // The current tip may be at the last line of the textfile, (or
154 // past it, if the number of lines in the textfile changed, such
155 // as changing to a different textfile, with less tips). So check
156 // to see at last line of text file, (or past it)...
157 if ( m_currentTip
>= count
)
159 // .. and if so, wrap back to line 0.
163 // Read the tip, and increment the current tip counter.
164 tip
= m_textfile
.GetLine(m_currentTip
++);
166 // Allow a derived class's overrided virtual to modify the tip
167 // now if so desired.
168 tip
= PreprocessTip(tip
);
170 // Break if tip isn't a comment, and isn't an empty string
171 // (or only stray space characters).
172 if ( !tip
.StartsWith(wxT("#")) && (tip
.Trim() != wxEmptyString
) )
178 // If tip starts with '_(', then it is a gettext string of format
179 // _("My \"global\" tip text") so first strip off the leading '_("'...
180 if ( tip
.StartsWith(wxT("_(\"" ), &tip
))
182 //...and strip off the trailing '")'...
183 tip
= tip
.BeforeLast(wxT('\"'));
184 // ...and replace escaped quotes
185 tip
.Replace(wxT("\\\""), wxT("\""));
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
196 EVT_BUTTON(wxID_NEXT_TIP
, wxTipDialog::OnNextTip
)
199 wxTipDialog::wxTipDialog(wxWindow
*parent
,
200 wxTipProvider
*tipProvider
,
202 : wxDialog(parent
, -1, _("Tip of the Day"),
203 wxDefaultPosition
, wxDefaultSize
,
204 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
206 m_tipProvider
= tipProvider
;
208 // 1) create all controls in tab order
210 wxButton
*btnClose
= new wxButton(this, wxID_CANCEL
, _("&Close"));
212 m_checkbox
= new wxCheckBox(this, -1, _("&Show tips at startup"));
213 m_checkbox
->SetValue(showAtStartup
);
215 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next Tip"));
217 wxStaticText
*text
= new wxStaticText(this, -1, _("Did you know..."), wxDefaultPosition
, wxSize(-1,30) );
218 #if defined(__WXMSW__) || defined(__WXPM__)
219 text
->SetFont(wxFont(16, wxSWISS
, wxNORMAL
, wxBOLD
));
221 text
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxBOLD
));
224 // text->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
226 m_text
= new wxTextCtrl(this, -1, wxT(""),
227 wxDefaultPosition
, wxSize(200, 160),
231 wxTE_RICH
| // a hack to get rid of vert scrollbar
233 #if defined(__WXMSW__)
234 m_text
->SetFont(wxFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
));
236 m_text
->SetFont(wxFont(14, wxSWISS
, wxNORMAL
, wxNORMAL
));
239 //#if defined(__WXPM__)
241 // The only way to get icons into an OS/2 static bitmap control
245 // vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
246 // // and for StatBmp's under OS/2 it MUST be a valid resource ID.
248 // wxStaticBitmap* bmp = new wxStaticBitmap(this, -1, vBitmap);
252 wxIcon icon
= wxArtProvider::GetIcon(wxART_TIP
, wxART_CMN_DIALOG
);
253 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, -1, icon
);
257 // 2) put them in boxes
259 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
261 wxBoxSizer
*icon_text
= new wxBoxSizer( wxHORIZONTAL
);
262 icon_text
->Add( bmp
, 0, wxCENTER
);
263 icon_text
->Add( text
, 1, wxCENTER
| wxLEFT
, 20 );
264 topsizer
->Add( icon_text
, 0, wxEXPAND
| wxALL
, 10 );
266 topsizer
->Add( m_text
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
268 wxBoxSizer
*bottom
= new wxBoxSizer( wxHORIZONTAL
);
269 bottom
->Add( m_checkbox
, 0, wxCENTER
);
270 bottom
->Add( 10,10,1 );
271 bottom
->Add( btnNext
, 0, wxCENTER
| wxLEFT
, 10 );
272 bottom
->Add( btnClose
, 0, wxCENTER
| wxLEFT
, 10 );
273 topsizer
->Add( bottom
, 0, wxEXPAND
| wxALL
, 10 );
278 SetSizer( topsizer
);
280 topsizer
->SetSizeHints( this );
281 topsizer
->Fit( this );
283 Centre(wxBOTH
| wxCENTER_FRAME
);
287 // ----------------------------------------------------------------------------
288 // our public interface
289 // ----------------------------------------------------------------------------
291 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
294 return new wxFileTipProvider(filename
, currentTip
);
297 bool wxShowTip(wxWindow
*parent
,
298 wxTipProvider
*tipProvider
,
301 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
304 return dlg
.ShowTipsOnStartup();
307 #endif // wxUSE_STARTUP_TIPS