1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/tipdlg.cpp
3 // Purpose: implementation of wxTipDialog
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_STARTUP_TIPS
29 #include "wx/button.h"
30 #include "wx/checkbox.h"
31 #include "wx/statbox.h"
32 #include "wx/dialog.h"
35 #include "wx/textctrl.h"
36 #include "wx/statbmp.h"
37 #include "wx/stattext.h"
39 #include "wx/settings.h"
42 #include "wx/statline.h"
43 #include "wx/artprov.h"
45 #include "wx/tipdlg.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 static const int wxID_NEXT_TIP
= 32000; // whatever
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 /* Macro for avoiding #ifdefs when value have to be different depending on size of
58 device we display on - take it from something like wxDesktopPolicy in the future
61 #if defined(__SMARTPHONE__)
62 #define wxLARGESMALL(large,small) small
64 #define wxLARGESMALL(large,small) large
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // 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 wxDECLARE_NO_COPY_CLASS(wxFileTipProvider
);
86 #endif // wxUSE_TEXTFILE
89 // TODO an implementation which takes the tips from the given registry key
90 class WXDLLIMPEXP_ADV wxRegTipProvider
: public wxTipProvider
93 wxRegTipProvider(const wxString
& keyname
);
95 virtual wxString
GetTip();
98 // Empty implementation for now to keep the linker happy
99 wxString
wxRegTipProvider::GetTip()
101 return wxEmptyString
;
106 // the dialog we show in wxShowTip()
107 class WXDLLIMPEXP_ADV wxTipDialog
: public wxDialog
110 wxTipDialog(wxWindow
*parent
,
111 wxTipProvider
*tipProvider
,
114 // the tip dialog has "Show tips on startup" checkbox - return true if it
115 // was checked (or wasn't unchecked)
116 bool ShowTipsOnStartup() const { return m_checkbox
->GetValue(); }
118 // sets the (next) tip text
119 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
121 // "Next" button handler
122 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
125 wxTipProvider
*m_tipProvider
;
128 wxCheckBox
*m_checkbox
;
130 DECLARE_EVENT_TABLE()
131 wxDECLARE_NO_COPY_CLASS(wxTipDialog
);
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
144 : wxTipProvider(currentTip
), m_textfile(filename
)
149 wxString
wxFileTipProvider::GetTip()
151 size_t count
= m_textfile
.GetLineCount();
154 return _("Tips not available, sorry!");
159 // Comments start with a # symbol.
160 // Loop reading lines until get the first one that isn't a comment.
161 // The max number of loop executions is the number of lines in the
162 // textfile so that can't go into an eternal loop in the [oddball]
163 // case of a comment-only tips file, or the developer has vetoed
164 // them all via PreprecessTip().
165 for ( size_t i
=0; i
< count
; i
++ )
167 // The current tip may be at the last line of the textfile, (or
168 // past it, if the number of lines in the textfile changed, such
169 // as changing to a different textfile, with less tips). So check
170 // to see at last line of text file, (or past it)...
171 if ( m_currentTip
>= count
)
173 // .. and if so, wrap back to line 0.
177 // Read the tip, and increment the current tip counter.
178 tip
= m_textfile
.GetLine(m_currentTip
++);
180 // Allow a derived class's overrided virtual to modify the tip
181 // now if so desired.
182 tip
= PreprocessTip(tip
);
184 // Break if tip isn't a comment, and isn't an empty string
185 // (or only stray space characters).
186 if ( !tip
.StartsWith(wxT("#")) && (tip
.Trim() != wxEmptyString
) )
192 // If tip starts with '_(', then it is a gettext string of format
193 // _("My \"global\" tip text") so first strip off the leading '_("'...
194 if ( tip
.StartsWith(wxT("_(\"" ), &tip
))
196 //...and strip off the trailing '")'...
197 tip
= tip
.BeforeLast(wxT('\"'));
198 // ...and replace escaped quotes
199 tip
.Replace(wxT("\\\""), wxT("\""));
201 // and translate it as requested
202 tip
= wxGetTranslation(tip
);
207 #endif // wxUSE_TEXTFILE
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
214 EVT_BUTTON(wxID_NEXT_TIP
, wxTipDialog::OnNextTip
)
217 wxTipDialog::wxTipDialog(wxWindow
*parent
,
218 wxTipProvider
*tipProvider
,
220 : wxDialog(GetParentForModalDialog(parent
, 0), wxID_ANY
, _("Tip of the Day"),
221 wxDefaultPosition
, wxDefaultSize
,
222 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
225 m_tipProvider
= tipProvider
;
226 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
228 // 1) create all controls in tab order
230 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
, _("Did you know..."));
234 wxFont font
= text
->GetFont();
235 font
.SetPointSize(int(1.6 * font
.GetPointSize()));
236 font
.SetWeight(wxFONTWEIGHT_BOLD
);
240 m_text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
241 wxDefaultPosition
, wxSize(200, 160),
245 wxTE_RICH2
| // a hack to get rid of vert scrollbar
246 wxDEFAULT_CONTROL_BORDER
248 #if defined(__WXMSW__)
249 m_text
->SetFont(wxFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
));
252 //#if defined(__WXPM__)
254 // The only way to get icons into an OS/2 static bitmap control
258 // vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
259 // // and for StatBmp's under OS/2 it MUST be a valid resource ID.
261 // wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
265 wxIcon icon
= wxArtProvider::GetIcon(wxART_TIP
, wxART_CMN_DIALOG
);
266 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, wxID_ANY
, icon
);
270 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _("&Show tips at startup"));
271 m_checkbox
->SetValue(showAtStartup
);
272 m_checkbox
->SetFocus();
274 // smart phones does not support or do not waste space for wxButtons
275 #ifndef __SMARTPHONE__
276 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next Tip"));
279 // smart phones does not support or do not waste space for wxButtons
280 #ifndef __SMARTPHONE__
281 wxButton
*btnClose
= new wxButton(this, wxID_CLOSE
);
282 SetAffirmativeId(wxID_CLOSE
);
286 // 2) put them in boxes
288 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
290 wxBoxSizer
*icon_text
= new wxBoxSizer( wxHORIZONTAL
);
291 icon_text
->Add( bmp
, 0, wxCENTER
);
292 icon_text
->Add( text
, 1, wxCENTER
| wxLEFT
, wxLARGESMALL(20,0) );
293 topsizer
->Add( icon_text
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
295 topsizer
->Add( m_text
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, wxLARGESMALL(10,0) );
297 wxBoxSizer
*bottom
= new wxBoxSizer( wxHORIZONTAL
);
299 topsizer
->Add( m_checkbox
, 0, wxCENTER
|wxTOP
);
301 bottom
->Add( m_checkbox
, 0, wxCENTER
);
303 // smart phones does not support or do not waste space for wxButtons
304 #ifdef __SMARTPHONE__
305 SetRightMenu(wxID_NEXT_TIP
, _("Next"));
306 SetLeftMenu(wxID_CLOSE
);
309 bottom
->Add( 10,10,1 );
310 bottom
->Add( btnNext
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
311 bottom
->Add( btnClose
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
315 topsizer
->Add( bottom
, 0, wxCENTER
| wxALL
, 5 );
317 topsizer
->Add( bottom
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
321 SetSizer( topsizer
);
323 topsizer
->SetSizeHints( this );
324 topsizer
->Fit( this );
326 Centre(wxBOTH
| wxCENTER_FRAME
);
329 // ----------------------------------------------------------------------------
330 // our public interface
331 // ----------------------------------------------------------------------------
334 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
337 return new wxFileTipProvider(filename
, currentTip
);
339 #endif // wxUSE_TEXTFILE
341 bool wxShowTip(wxWindow
*parent
,
342 wxTipProvider
*tipProvider
,
345 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
348 return dlg
.ShowTipsOnStartup();
351 #endif // wxUSE_STARTUP_TIPS