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/layout.h"
41 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/statbmp.h"
46 #include "wx/statline.h"
48 #include "wx/tipdlg.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int wxID_NEXT_TIP
= -100; // whatever
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // an implementation which takes the tips from the text file - each line
62 class WXDLLEXPORT wxFileTipProvider
: public wxTipProvider
65 wxFileTipProvider(const wxString
& filename
, size_t currentTip
);
67 virtual wxString
GetTip();
70 wxTextFile m_textfile
;
74 // TODO an implementation which takes the tips from the given registry key
75 class WXDLLEXPORT wxRegTipProvider
: public wxTipProvider
78 wxRegTipProvider(const wxString
& keyname
);
80 virtual wxString
GetTip();
83 // Empty implementation for now to keep the linker happy
84 wxString
wxRegTipProvider::GetTip()
91 // the dialog we show in wxShowTip()
92 class WXDLLEXPORT wxTipDialog
: public wxDialog
95 wxTipDialog(wxWindow
*parent
,
96 wxTipProvider
*tipProvider
,
99 // the tip dialog has "Show tips on startup" checkbox - return TRUE if it
100 // was checked (or wasn't unchecked)
101 bool ShowTipsOnStartup() const { return m_checkbox
->GetValue(); }
103 // sets the (next) tip text
104 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
106 // "Next" button handler
107 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
110 wxTipProvider
*m_tipProvider
;
113 wxCheckBox
*m_checkbox
;
115 DECLARE_EVENT_TABLE()
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
128 : wxTipProvider(currentTip
), m_textfile(filename
)
133 wxString
wxFileTipProvider::GetTip()
135 size_t count
= m_textfile
.GetLineCount();
137 return _("Tips not available, sorry!");
139 // notice that it may be greater, actually, if we remembered it from the
140 // last time and the number of tips changed
141 if ( m_currentTip
== count
)
147 return m_textfile
.GetLine(m_currentTip
++);
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
155 EVT_BUTTON(wxID_NEXT_TIP
, OnNextTip
)
158 wxTipDialog::wxTipDialog(wxWindow
*parent
,
159 wxTipProvider
*tipProvider
,
161 : wxDialog(parent
, -1, _("Tip of the Day"),
162 wxDefaultPosition
, wxDefaultSize
,
163 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
165 m_tipProvider
= tipProvider
;
167 wxSize sizeBtn
= GetStandardButtonSize();
168 wxLayoutConstraints
*c
;
170 // create the controls in the right order, then set the constraints
171 wxButton
*btnClose
= new wxButton(this, wxID_CANCEL
, _("&Close"));
172 m_checkbox
= new wxCheckBox(this, -1, _("&Show tips at startup"));
173 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next"));
175 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _("Did you know..."),
176 wxDefaultPosition
, wxDefaultSize
,
177 wxTE_READONLY
| wxNO_BORDER
);
178 text
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxBOLD
));
179 text
->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
181 m_text
= new wxTextCtrl(this, -1, _T(""),
182 wxDefaultPosition
, wxDefaultSize
,
183 wxTE_MULTILINE
| wxTE_READONLY
| wxSUNKEN_BORDER
);
184 m_text
->SetFont(wxFont(14, wxROMAN
, wxNORMAL
, wxNORMAL
));
187 wxIcon
icon("wxICON_TIP");
189 #include "wx/generic/tip.xpm"
190 wxIcon
icon(tipIcon
);
193 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, -1, icon
);
195 const int iconSize
= icon
.GetWidth();
197 c
= new wxLayoutConstraints
;
198 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
199 c
->left
.RightOf(bmp
, 2*LAYOUT_X_MARGIN
);
200 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
201 c
->height
.Absolute(2*text
->GetSize().GetHeight());
202 text
->SetConstraints(c
);
204 c
= new wxLayoutConstraints
;
205 c
->centreY
.SameAs(text
, wxCentreY
);
206 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
207 c
->width
.Absolute(iconSize
);
208 c
->height
.Absolute(iconSize
);
209 bmp
->SetConstraints(c
);
211 c
= new wxLayoutConstraints
;
212 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_X_MARGIN
);
213 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
214 c
->width
.Absolute(sizeBtn
.GetWidth());
215 c
->height
.Absolute(sizeBtn
.GetHeight());
216 btnClose
->SetConstraints(c
);
218 c
= new wxLayoutConstraints
;
219 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_X_MARGIN
);
220 c
->right
.LeftOf(btnClose
, 2*LAYOUT_X_MARGIN
);
221 c
->width
.Absolute(sizeBtn
.GetWidth());
222 c
->height
.Absolute(sizeBtn
.GetHeight());
223 btnNext
->SetConstraints(c
);
225 c
= new wxLayoutConstraints
;
226 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_X_MARGIN
);
227 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
230 m_checkbox
->SetConstraints(c
);
231 m_checkbox
->SetValue(showAtStartup
);
233 c
= new wxLayoutConstraints
;
235 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
236 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
237 c
->bottom
.Above(btnClose
, -2*LAYOUT_Y_MARGIN
);
238 m_text
->SetConstraints(c
);
242 Centre(wxBOTH
| wxCENTER_FRAME
);
244 wxSize
size(5*sizeBtn
.GetWidth(), 10*sizeBtn
.GetHeight());
246 SetSizeHints(size
.x
, size
.y
);
251 // ----------------------------------------------------------------------------
252 // our public interface
253 // ----------------------------------------------------------------------------
255 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
258 return new wxFileTipProvider(filename
, currentTip
);
261 bool wxShowTip(wxWindow
*parent
,
262 wxTipProvider
*tipProvider
,
265 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
268 return dlg
.ShowTipsOnStartup();
271 #endif // wxUSE_STARTUP_TIPS