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