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