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/statbmp.h"
38 #include "wx/dialog.h"
41 #include "wx/layout.h"
42 #include "wx/settings.h"
43 #include "wx/textctrl.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();
84 // the dialog we show in wxShowTip()
85 class WXDLLEXPORT wxTipDialog
: public wxDialog
88 wxTipDialog(wxWindow
*parent
,
89 wxTipProvider
*tipProvider
,
92 // the tip dialog has "Show tips on startup" checkbox - return TRUE if it
93 // was checked (or wasn't unchecked)
94 bool ShowTipsOnStartup() const { return m_checkbox
->GetValue(); }
96 // sets the (next) tip text
97 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
99 // "Next" button handler
100 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
103 wxTipProvider
*m_tipProvider
;
106 wxCheckBox
*m_checkbox
;
108 DECLARE_EVENT_TABLE()
111 // ============================================================================
113 // ============================================================================
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
121 : wxTipProvider(currentTip
), m_textfile(filename
)
126 wxString
wxFileTipProvider::GetTip()
128 size_t count
= m_textfile
.GetLineCount();
130 return _("Tips not available, sorry!");
132 // notice that it may be greater, actually, if we remembered it from the
133 // last time and the number of tips changed
134 if ( m_currentTip
== count
)
140 return m_textfile
.GetLine(m_currentTip
++);
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
148 EVT_BUTTON(wxID_NEXT_TIP
, OnNextTip
)
151 wxTipDialog::wxTipDialog(wxWindow
*parent
,
152 wxTipProvider
*tipProvider
,
154 : wxDialog(parent
, -1, _("Tip of the Day"),
155 wxDefaultPosition
, wxDefaultSize
,
156 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
158 m_tipProvider
= tipProvider
;
160 wxSize sizeBtn
= GetStandardButtonSize();
161 wxLayoutConstraints
*c
;
163 // create the controls in the right order, then set the constraints
164 wxButton
*btnClose
= new wxButton(this, wxID_CANCEL
, _("&Close"));
165 m_checkbox
= new wxCheckBox(this, -1, _("&Show tips at startup"));
166 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next"));
168 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _("Did you know..."),
169 wxDefaultPosition
, wxDefaultSize
,
170 wxTE_READONLY
| wxNO_BORDER
);
171 text
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxBOLD
));
172 text
->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
174 m_text
= new wxTextCtrl(this, -1, _T(""),
175 wxDefaultPosition
, wxDefaultSize
,
176 wxTE_MULTILINE
| wxTE_READONLY
| wxSUNKEN_BORDER
);
177 m_text
->SetFont(wxFont(14, wxROMAN
, wxNORMAL
, wxNORMAL
));
180 wxIcon
icon("wxICON_TIP");
182 #include "wx/generic/tip.xpm"
183 wxIcon
icon(tipIcon
);
186 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, -1, icon
);
188 const int iconSize
= icon
.GetWidth();
190 c
= new wxLayoutConstraints
;
191 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
192 c
->left
.RightOf(bmp
, 2*LAYOUT_X_MARGIN
);
193 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
194 c
->height
.Absolute(2*text
->GetSize().GetHeight());
195 text
->SetConstraints(c
);
197 c
= new wxLayoutConstraints
;
198 c
->centreY
.SameAs(text
, wxCentreY
);
199 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
200 c
->width
.Absolute(iconSize
);
201 c
->height
.Absolute(iconSize
);
202 bmp
->SetConstraints(c
);
204 c
= new wxLayoutConstraints
;
205 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_X_MARGIN
);
206 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
207 c
->width
.Absolute(sizeBtn
.GetWidth());
208 c
->height
.Absolute(sizeBtn
.GetHeight());
209 btnClose
->SetConstraints(c
);
211 c
= new wxLayoutConstraints
;
212 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_X_MARGIN
);
213 c
->right
.LeftOf(btnClose
, 2*LAYOUT_X_MARGIN
);
214 c
->width
.Absolute(sizeBtn
.GetWidth());
215 c
->height
.Absolute(sizeBtn
.GetHeight());
216 btnNext
->SetConstraints(c
);
218 c
= new wxLayoutConstraints
;
219 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_X_MARGIN
);
220 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
223 m_checkbox
->SetConstraints(c
);
224 m_checkbox
->SetValue(showAtStartup
);
226 c
= new wxLayoutConstraints
;
228 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
229 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
230 c
->bottom
.Above(btnClose
, -2*LAYOUT_Y_MARGIN
);
231 m_text
->SetConstraints(c
);
235 Centre(wxBOTH
| wxCENTER_FRAME
);
237 wxSize
size(5*sizeBtn
.GetWidth(), 10*sizeBtn
.GetHeight());
239 SetSizeHints(size
.x
, size
.y
);
244 // ----------------------------------------------------------------------------
245 // our public interface
246 // ----------------------------------------------------------------------------
248 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
251 return new wxFileTipProvider(filename
, currentTip
);
254 bool wxShowTip(wxWindow
*parent
,
255 wxTipProvider
*tipProvider
,
258 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
261 return dlg
.ShowTipsOnStartup();
264 #endif // wxUSE_STARTUP_TIPS