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