]> git.saurik.com Git - wxWidgets.git/blob - src/generic/notifmsgg.cpp
wxSizer::Fit() now sets client size
[wxWidgets.git] / src / generic / notifmsgg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/notifmsgg.cpp
3 // Purpose: generic implementation of wxGenericNotificationMessage
4 // Author: Vadim Zeitlin
5 // Created: 2007-11-24
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef wxUSE_LIBHILDON
27 #define wxUSE_LIBHILDON 0
28 #endif
29
30 #if wxUSE_NOTIFICATION_MESSAGE && !wxUSE_LIBHILDON
31
32 #ifndef WX_PRECOMP
33 #include "wx/dialog.h"
34 #include "wx/timer.h"
35 #include "wx/sizer.h"
36 #include "wx/statbmp.h"
37 #endif //WX_PRECOMP
38
39 #include "wx/artprov.h"
40
41 // even if the platform has the native implementation, we still normally want
42 // to use the generic one (unless it's totally unsuitable for the target UI as
43 // is the case of Hildon) because it may provide more features, so include
44 // wx/generic/notifmsg.h to get wxGenericNotificationMessage declaration even
45 // if wx/notifmsg.h only declares wxNotificationMessage itself (if it already
46 // uses the generic version, the second inclusion will do no harm)
47 #include "wx/notifmsg.h"
48 #include "wx/generic/notifmsg.h"
49
50 // ----------------------------------------------------------------------------
51 // wxNotificationMessageDialog
52 // ----------------------------------------------------------------------------
53
54 class wxNotificationMessageDialog : public wxDialog
55 {
56 public:
57 wxNotificationMessageDialog(wxWindow *parent,
58 const wxString& text,
59 int timeout,
60 int flags);
61
62 void Set(wxWindow *parent,
63 const wxString& text,
64 int timeout,
65 int flags);
66
67 bool IsAutomatic() const { return m_timer.IsRunning(); }
68 void SetDeleteOnHide() { m_deleteOnHide = true; }
69
70 private:
71 void OnClose(wxCloseEvent& event);
72 void OnTimer(wxTimerEvent& event);
73
74 // if true, delete the dialog when it should disappear, otherwise just hide
75 // it (initially false)
76 bool m_deleteOnHide;
77
78 // timer which will hide this dialog when it expires, if it's not running
79 // it means we were created without timeout
80 wxTimer m_timer;
81
82
83 DECLARE_EVENT_TABLE()
84 DECLARE_NO_COPY_CLASS(wxNotificationMessageDialog)
85 };
86
87 // ============================================================================
88 // wxNotificationMessageDialog implementation
89 // ============================================================================
90
91 BEGIN_EVENT_TABLE(wxNotificationMessageDialog, wxDialog)
92 EVT_CLOSE(wxNotificationMessageDialog::OnClose)
93
94 EVT_TIMER(wxID_ANY, wxNotificationMessageDialog::OnTimer)
95 END_EVENT_TABLE()
96
97 wxNotificationMessageDialog::wxNotificationMessageDialog(wxWindow *parent,
98 const wxString& text,
99 int timeout,
100 int flags)
101 : wxDialog(parent, wxID_ANY, _("Notice"),
102 wxDefaultPosition, wxDefaultSize,
103 0 /* no caption, no border styles */),
104 m_timer(this)
105 {
106 m_deleteOnHide = false;
107
108 Set(parent, text, timeout, flags);
109 }
110
111 void
112 wxNotificationMessageDialog::Set(wxWindow * WXUNUSED(parent),
113 const wxString& text,
114 int timeout,
115 int flags)
116 {
117 wxSizer * const sizerTop = new wxBoxSizer(wxHORIZONTAL);
118 if ( flags & wxICON_MASK )
119 {
120 sizerTop->Add(new wxStaticBitmap
121 (
122 this,
123 wxID_ANY,
124 wxArtProvider::GetMessageBoxIcon(flags)
125 ),
126 wxSizerFlags().Centre().Border());
127 }
128
129 sizerTop->Add(CreateTextSizer(text), wxSizerFlags(1).Border());
130 SetSizerAndFit(sizerTop);
131
132 if ( timeout != wxGenericNotificationMessage::Timeout_Never )
133 {
134 // wxTimer uses ms, timeout is in seconds
135 m_timer.Start(timeout*1000, true /* one shot only */);
136 }
137 else if ( m_timer.IsRunning() )
138 {
139 m_timer.Stop();
140 }
141 }
142
143 void wxNotificationMessageDialog::OnClose(wxCloseEvent& event)
144 {
145 if ( m_deleteOnHide )
146 {
147 // we don't need to keep this dialog alive any more
148 Destroy();
149 }
150 else // don't really close, just hide, as we can be shown again later
151 {
152 event.Veto();
153
154 Hide();
155 }
156 }
157
158 void wxNotificationMessageDialog::OnTimer(wxTimerEvent& WXUNUSED(event))
159 {
160 if ( m_deleteOnHide )
161 Destroy();
162 else
163 Hide();
164 }
165
166 // ============================================================================
167 // wxGenericNotificationMessage implementation
168 // ============================================================================
169
170 int wxGenericNotificationMessage::ms_timeout = 10;
171
172 /* static */ void wxGenericNotificationMessage::SetDefaultTimeout(int timeout)
173 {
174 wxASSERT_MSG( timeout > 0,
175 "negative or zero default timeout doesn't make sense" );
176
177 ms_timeout = timeout;
178 }
179
180 void wxGenericNotificationMessage::Init()
181 {
182 m_dialog = NULL;
183 }
184
185 wxGenericNotificationMessage::~wxGenericNotificationMessage()
186 {
187 if ( m_dialog->IsAutomatic() )
188 {
189 // we want to allow the user to create an automatically hidden
190 // notification just by creating a local wxGenericNotificationMessage object
191 // and so we shouldn't hide the notification when this object goes out
192 // of scope
193 m_dialog->SetDeleteOnHide();
194 }
195 else // manual dialog, hide it immediately
196 {
197 // OTOH for permanently shown dialogs only the code can hide them and
198 // if the object is deleted, we must do it now as it won't be
199 // accessible programmatically any more
200 delete m_dialog;
201 }
202 }
203
204 bool wxGenericNotificationMessage::Show(int timeout)
205 {
206 if ( timeout == Timeout_Auto )
207 {
208 timeout = GetDefaultTimeout();
209 }
210
211 if ( !m_dialog )
212 {
213 m_dialog = new wxNotificationMessageDialog
214 (
215 GetParent(),
216 GetFullMessage(),
217 timeout,
218 GetFlags()
219 );
220 }
221 else // update the existing dialog
222 {
223 m_dialog->Set(GetParent(), GetFullMessage(), timeout, GetFlags());
224 }
225
226 m_dialog->Show();
227
228 return true;
229 }
230
231 bool wxGenericNotificationMessage::Close()
232 {
233 if ( !m_dialog )
234 return false;
235
236 m_dialog->Hide();
237
238 return true;
239 }
240
241 #endif // wxUSE_NOTIFICATION_MESSAGE && !wxUSE_LIBHILDON