1 /*
2 Copyright (C) 2017-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: Dk4WxProgressDialog.wxc
12 */
13 
14 /**	@file Dk4WxProgressDialog.cpp The Dk4WxProgressDialog module.
15 */
16 
17 
18 #include "dk4conf.h"
19 #include <libdk4wx/Dk4WxProgressDialog.h>
20 
21 
22 
23 
24 
25 
26 
27 #if	wxCHECK_VERSION(3,0,0)
28 wxBEGIN_EVENT_TABLE(Dk4WxProgressDialog,wxDialog)
29 #else
30 BEGIN_EVENT_TABLE(Dk4WxProgressDialog,wxDialog)
31 #endif
32   EVT_IDLE(                     Dk4WxProgressDialog::OnIdle)
33   EVT_BUTTON(wxID_CANCEL,       Dk4WxProgressDialog::OnCancel)
34 #if	wxCHECK_VERSION(3,0,0)
35 wxEND_EVENT_TABLE()
36 #else
37 END_EVENT_TABLE()
38 #endif
39 
40 
41 static const wxChar *	dk4wx_progress_kw_wx[] = {
42 /* 0 */
43 wxT("Operation in progress"),
44 
45 /* 1 */
46 wxT("Cancel"),
47 
48 /* 2 */
49 wxT("Abort operation"),
50 
51 /* 3 */
52 wxT("Abortion scheduled, please wait."),
53 
54 NULL
55 
56 };
57 
58 
59 
60 /**	Integer preference names.
61 */
62 static const wxChar * const	names[] = {
63 /* 0 */
64 wxT("progress.min-width"),
65 
66 NULL
67 
68 };
69 
70 
Dk4WxProgressDialog(wxFrame * parent,Dk4WxApplicationHelper * pAppHelp)71 Dk4WxProgressDialog::Dk4WxProgressDialog(
72   wxFrame			*parent,
73   Dk4WxApplicationHelper	*pAppHelp
74 )
75 : wxDialog(
76   parent, wxID_ANY,
77   (
78     (NULL != pAppHelp)
79     ? (pAppHelp->GetBasicString(2))
80     : (dk4wx_progress_kw_wx[0])
81   ),
82   wxDefaultPosition,
83   wxDefaultSize,
84   wxDEFAULT_DIALOG_STYLE
85 )
86 {
87   const wxChar	*labelText = NULL;
88   int		 minWidth = 300;
89 
90   labelText = dk4wx_progress_kw_wx[0];
91   if (NULL != pAppHelp) {
92     labelText = pAppHelp->GetBasicString(2);
93     pAppHelp->GetMultiple(names, &minWidth, 1);
94     if (0 > minWidth) { minWidth = 300; }
95   }
96 
97   iOld = 0;
98   iNew = 0;
99   bActive = true;
100   bAborted = false;
101   bFinished = false;
102   bSuccess = false;
103   bHaveDialog = false;
104   sDialogTitle = wxEmptyString;
105   sDialogText = wxEmptyString;
106   iDialogStyle = wxID_OK;
107   if (NULL != pAppHelp) {
108     sText[0] = pAppHelp->GetBasicString(3);	// Button label
109     sText[1] = pAppHelp->GetBasicString(4);	// Button tooltip
110     sText[2] = pAppHelp->GetBasicString(5);	// Text hint
111   } else {
112     sText[0] = dk4wx_progress_kw_wx[1];
113     sText[1] = dk4wx_progress_kw_wx[2];
114     sText[2] = dk4wx_progress_kw_wx[3];
115   }
116   dkctGUILayoutOK = false;
117   sDialog = NULL;
118   verticalSizer = NULL;
119   sttInProgress = NULL;
120   gaugeProgress = NULL;
121   sttWait = NULL;
122   bCancel = NULL;
123   sDialog = new wxBoxSizer(
124     wxHORIZONTAL
125   );
126   if(!(sDialog)) {
127     goto dkctGUILayoutFinished;
128   }
129   sDialog->Add(10, 10, 0);
130   verticalSizer = new wxBoxSizer(
131     wxVERTICAL
132   );
133   if(!(verticalSizer)) {
134     goto dkctGUILayoutFinished;
135   }
136   verticalSizer->Add(10, 10, 0);
137   sttInProgress = new wxStaticText(
138     this,
139     wxID_ANY,
140     labelText,
141     wxDefaultPosition,
142     wxDefaultSize,
143     wxALIGN_CENTRE
144   );
145   if(!(sttInProgress)) {
146     goto dkctGUILayoutFinished;
147   }
148   verticalSizer->Add(
149     sttInProgress
150   );
151   verticalSizer->Add(10, 10, 0);
152   gaugeProgress = new wxGauge(
153     this,
154     wxID_ANY,
155     1000
156   );
157   if(!(gaugeProgress)) {
158     goto dkctGUILayoutFinished;
159   }
160   verticalSizer->Add(
161     gaugeProgress,
162     1,
163     wxALIGN_CENTER_HORIZONTAL|wxGROW
164   );
165   verticalSizer->Add(10, 10, 0);
166   sttWait = new wxStaticText(
167     this,
168     wxID_ANY,
169     wxT(""),
170     wxDefaultPosition,
171     wxDefaultSize,
172     wxALIGN_CENTRE
173   );
174   if(!(sttWait)) {
175     goto dkctGUILayoutFinished;
176   }
177   verticalSizer->Add(
178     sttWait,
179     1,
180     wxALIGN_CENTER_HORIZONTAL|wxGROW
181   );
182   verticalSizer->Add(10, 10, 0);
183   bCancel = new wxButton(
184     this,
185     wxID_CANCEL,
186     sText[0]
187   );
188   if(!(bCancel)) {
189     goto dkctGUILayoutFinished;
190   }
191   bCancel->SetToolTip(sText[1]);
192   verticalSizer->Add(
193     bCancel,
194     0,
195     wxALIGN_CENTER_HORIZONTAL
196   );
197   verticalSizer->Add(10, 10, 0);
198   sDialog->Add(
199     verticalSizer,
200     1,
201     wxGROW
202   );
203   sDialog->Add(10, 10, 0);
204   SetSizer(sDialog);
205   dkctGUILayoutOK = true;
206   dkctGUILayoutFinished:
207   if(dkctGUILayoutOK) {
208     sDialog->Fit(this);
209     sDialog->SetSizeHints(this);
210   }
211   if (dkctGUILayoutOK) {
212     wxSize gaugeSize = gaugeProgress->GetMinSize();
213     if (300 > gaugeSize.GetWidth()) {
214       gaugeSize.SetWidth(300);
215       gaugeProgress->SetMinSize(gaugeSize);
216     }
217     sDialog->Fit(this);
218     sDialog->SetSizeHints(this);
219   }
220 }
221 
222 
223 
224 
225 void
OnIdle(wxIdleEvent & event)226 Dk4WxProgressDialog::OnIdle(wxIdleEvent & event)
227 {
228   int	result;
229   {
230     wxCriticalSectionLocker	lock(csProtect);
231     if (bActive) {
232       if (bFinished) {
233         bActive = false;
234 	result = wxID_OK;
235 	if ((bAborted) || (!(bSuccess))) { result = wxID_CANCEL; }
236 	if (IsModal()) {
237 	  EndModal(result);
238 	} else {
239 	  SetReturnCode((bSuccess) && (!(bAborted)));
240 	  Show(false);
241 	}
242       } else {
243         if (iNew != iOld) {
244 	  iOld = iNew;
245 	  gaugeProgress->SetValue(iNew);
246 	  Refresh();
247 	  Update();
248 	}
249 	event.RequestMore();
250       }
251     }
252   }
253   event.Skip();
254 }
255 
256 
257 
258 void
OnCancel(wxCommandEvent & WXUNUSED (event))259 Dk4WxProgressDialog::OnCancel(wxCommandEvent & WXUNUSED(event))
260 {
261   bCancel->Enable(false);
262   bCancel->SetToolTip(sText[2]);
263   Refresh();
264   Update();
265   {
266     wxCriticalSectionLocker	lock(csProtect);
267     if (!(bFinished)) {
268       bAborted = true;
269     }
270   }
271 }
272 
273 
274 
275 void
SetGauge(int value)276 Dk4WxProgressDialog::SetGauge(int value)
277 {
278   wxCriticalSectionLocker	lock(csProtect);
279   iNew = value;
280 }
281 
282 
283 
284 void
SetDialogData(const wxString & title,const wxString & msg,int st)285 Dk4WxProgressDialog::SetDialogData(
286   const wxString &	title,
287   const wxString &	msg,
288   int	st
289 )
290 {
291   wxCriticalSectionLocker	lock(csProtect);
292   bHaveDialog = true;
293   sDialogTitle = title;
294   sDialogText = msg;
295   iDialogStyle = st;
296 }
297 
298 
299 
300 void
StartProcessing(void)301 Dk4WxProgressDialog::StartProcessing(void)
302 {
303   wxCriticalSectionLocker	lock(csProtect);
304   bActive = true;
305   sDialogTitle = wxEmptyString;
306   sDialogText = wxEmptyString;
307   iOld = 0;
308   iNew = 0;
309   bAborted = false;
310   bFinished = false;
311   bSuccess = true;
312   bActive = true;
313   bHaveDialog = false;
314 }
315 
316 
317 
318 void
EndProcessing(bool success)319 Dk4WxProgressDialog::EndProcessing(bool success)
320 {
321   wxCriticalSectionLocker	lock(csProtect);
322   bFinished = true;
323   if (!(bAborted)) {
324     bSuccess = success;
325   }
326 }
327 
328 
329 
330 bool
CanContinue(void)331 Dk4WxProgressDialog::CanContinue(void)
332 {
333   bool		 back	= true;
334   {
335     wxCriticalSectionLocker	lock(csProtect);
336     if ((bAborted) || (bFinished)) {
337       back = false;
338     }
339   }
340   return back;
341 }
342 
343 
344 
345 bool
IsFinished(void)346 Dk4WxProgressDialog::IsFinished(void)
347 {
348   bool		 back	= false;
349   {
350     wxCriticalSectionLocker	lock(csProtect);
351     back = bFinished;
352   }
353   return back;
354 }
355 
356 
357 
358 int
GetGauge(void)359 Dk4WxProgressDialog::GetGauge(void)
360 {
361   int		 back	= 0;
362   {
363     wxCriticalSectionLocker	lock(csProtect);
364     back = iNew;
365   }
366   return back;
367 }
368 
369 
370 
371 void
AbortProcessing(void)372 Dk4WxProgressDialog::AbortProcessing(void)
373 {
374   wxCriticalSectionLocker	lock(csProtect);
375   if (!(bFinished)) {
376     bAborted = true;
377   }
378 }
379 
380 
381 
382 bool
GetSuccess(void)383 Dk4WxProgressDialog::GetSuccess(void)
384 {
385   bool		back	= false;
386   {
387     wxCriticalSectionLocker	lock(csProtect);
388     if (bFinished) {
389       back = true;
390       if ((bAborted) || (!(bSuccess))) { back = false; }
391     }
392   }
393   return back;
394 }
395 
396 
397 
398 bool
GetDialogData(wxString & title,wxString & msg,int & style)399 Dk4WxProgressDialog::GetDialogData(
400   wxString &	title,
401   wxString &	msg,
402   int &		style
403 )
404 {
405   bool		 back	= false;
406   {
407     wxCriticalSectionLocker	lock(csProtect);
408     if (bHaveDialog) {
409       title = sDialogTitle;
410       msg = sDialogText;
411       style = iDialogStyle;
412       back = true;
413     }
414   }
415   return back;
416 }
417 
418