summaryrefslogtreecommitdiff
path: root/support/dktools/DkWxProgressDialog.wxc
blob: 942d0d56d3a7e5cd99bd6b2be385ebd6e987558b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2011-xxxx
license		=	bsd


%%	wx-gui

type		=	dialog
contents	=	sDialog
# status bar	=	1	dkwx_pd_def_texts[0]
# title		=	((title) ? title : dkwx_pd_def_texts[1])

[wxBoxSizer sDialog]
direction	=	horizontal
contents	=	$space(10)
contents	=	verticalSizer
contents	=	$space(10)

[wxBoxSizer verticalSizer]
direction	=	vertical
grow		=	yes
proportion	=	1
contents	=	$space(10)
contents	=	sttFilename	left
contents	=	$space(10)
contents	=	gaugeProgress	centered-x
contents	=	$space(10)
contents	=	sttWait		left
contents	=	$space(10)
contents	=	bCancel		centered-x
contents	=	$space(10)

[wxStaticText sttFilename]
text		=	((firstFileName) ? firstFileName : dkwx_pd_def_texts[0])
text style	=	centered
grow		=	yes
proportion	=	1

[wxGauge gaugeProgress]
range		=	1000
value		=	0
grow		=	yes

[wxButton bCancel]
text		=	((buttonText) ? buttonText : dkwx_pd_def_texts[2])
tip		=	((buttonTip) ? buttonTip : dkwx_pd_def_texts[3])
id		=	wxID_CANCEL

[wxStaticText sttWait]
text		=	((waitText) ? waitText : dkwx_pd_def_texts[4])
text style	=	left no-auto-resize



%%	header start

#include <DkWxCommunicator.h>



%%	class start

/**	Progress dialog showing currently process file, progress bar
	and a button to abort operation.
*/
class DkWxProgressDialog : public wxDialog
{
  private:
    /**	Event table.
    */
    DECLARE_EVENT_TABLE()

  protected:

    /**	Communicator object delivering the file name and progress bar
    	value.
    */
    DkWxCommunicator	*pComm;

    /**	String: Cancel operation scheduled. Please wait.
    */
    wxChar const	*sWaitPlease;

    /**	Parent frame.
    */
    DkWxFrame		*pParent;

    /**	Log text field to receive the messages.
    */
    wxTextCtrl		*pLogTextField;

%%	class end
  public:

  /**	Constructor.
  	@param	parent		Parent window.
	@param	comm		Communicator object.
	@param	tc		Text control to show messages.
	@param	title		Title text.
	@param	firstFileName	First file name to show.
	@param	buttonText	Text for "Cancel" button.
	@param	buttonTip	Tooltip text for button.
	@param	waitText	Button to show while waiting for thread exit.
  */
  DkWxProgressDialog(
    DkWxFrame		*parent,
    DkWxCommunicator	*comm,
    wxTextCtrl		*tc,
    wxChar const	*title,
    wxChar const	*firstFileName,
    wxChar const	*buttonText,
    wxChar const	*buttonTip,
    wxChar const	*waitText
  );

  /**	Handler for idle events.
  	We request the current file name and progress bar from
	the communicator object and update the information shown
	in the dialog.
  */
  void
  OnIdle(wxIdleEvent & event);

  /**	Handler for cancel button.
  */
  void
  OnCancel(wxCommandEvent & WXUNUSED(event));

  /**	Choose a modal position centered on the parent.
  */
  void		 chooseModalPosition();

};



%%	header end

%%	module start

#include "dkwxtrace.h"

$!trace-include

BEGIN_EVENT_TABLE(DkWxProgressDialog,wxDialog)
  EVT_IDLE(DkWxProgressDialog::OnIdle)
  EVT_BUTTON(wxID_CANCEL,	DkWxProgressDialog::OnCancel)
END_EVENT_TABLE()



/**	Default texts to use if the constructor has NULL pointer arguments.
*/
wxChar const * const dkwx_pd_def_texts[] = {
$!string-table	macro=wxT
#
#  0: Empty string
#

#
#  1: Default title.
#
Progress
#
#  2: Button text.
#
Cancel
#
#  3: Button tool tip.
#
Push button to interrupt processing.
#
#  4: Notification, user should wait.
#
Cancel command scheduled... Wait, please!
$!end
};



%%	constructor start
DkWxProgressDialog::DkWxProgressDialog(
  DkWxFrame		*parent,
  DkWxCommunicator	*comm,
  wxTextCtrl		*tc,
  wxChar const		*title,
  wxChar const		*firstFileName,
  wxChar const		*buttonText,
  wxChar const		*buttonTip,
  wxChar const		*waitText
) : wxDialog(
  parent,
  wxID_ANY,
  ((title) ? title : dkwx_pd_def_texts[1]),
  wxDefaultPosition,
  wxDefaultSize,
  wxDEFAULT_DIALOG_STYLE
)
{
  $? "+ DkWxProgressDialog::DkWxProgressDialog"
  pParent = parent;
  pLogTextField = tc;
  pComm = comm;
  sWaitPlease = ((waitText) ? waitText : dkwx_pd_def_texts[4]);
%%	constructor end
  if(dkctGUILayoutOK) {
    if(sttWait) {
      sttWait->SetLabel(dkwx_pd_def_texts[0]);
    }
  }
  $? "- DkWxProgressDialog::DkWxProgressDialog"
}



%%	module end


void
DkWxProgressDialog::OnIdle(wxIdleEvent & event)
{
  int		canContinue = 1;
  int		res;
  $? "+ DkWxProgressDialog::OnIdle"
  if(pComm) {		$? ". pComm ok"
    res = pComm->getUpdates(this, sttFilename, gaugeProgress);
    $? ". res = %d", res
    if(res & DK_WX_COMMUNICATOR_STATUS_UPDATE) {	$? ". must update"
      Refresh();
      Update();
    } else {						$? ". no update"
    }
    if(res & DK_WX_COMMUNICATOR_STATUS_RUNNING) {	$? ". can continue"
      canContinue = 1;
    } else {						$? ". finished, end indicated"
      canContinue = 0;
    }
  } else {						$? "! no pComm"
    canContinue = 0;
  }
  if(canContinue) {					$? ". can continue"
    event.RequestMore();
  } else {						$? ". finished"
    canContinue = 1;
    if(pComm) {
      canContinue = pComm->getCanContinue();
      if(pLogTextField) {				$? ". transfer text"
        pComm->getText(pLogTextField);
	if(pParent) {					$? ". refresh win"
	  pParent->Refresh();
	} else {					$? ". refresh text"
	  pLogTextField->Refresh();
	}
      }
    }
    if(IsModal()) {					$? ". end modal"
      EndModal((canContinue) ? wxID_OK : wxID_CANCEL);
    } else {						$? ". show false"
      SetReturnCode((canContinue) ? wxID_OK : wxID_CANCEL);
      Show(false);
    }
  }
  event.Skip(); $? "- DkWxProgressDialog::OnIdle"
}



void
DkWxProgressDialog::OnCancel(wxCommandEvent & event)
{
  $? "+ DkWxProgressDialog::OnCancel"
  if(pComm) {
    pComm->setCanContinue(0);
  }
  if(sttWait) {
    sttWait->SetLabel(sWaitPlease);
  }
  if(bCancel) {
    bCancel->Enable(false);
    bCancel->SetToolTip(sWaitPlease);
  }
  Refresh();
  Update();
  $? "- DkWxProgressDialog::OnCancel"
}



void
DkWxProgressDialog::chooseModalPosition()
{
  int	px	=	0;	/* Parent x position. */
  int	py	=	0;	/* Parent y position. */
  int	pw	=	0;	/* Parent width. */
  int	ph	=	0;	/* Parent height. */
  int	x	=	0;	/* X position. */
  int	y	=	0;	/* Y position. */
  int	w	=	0;	/* Width. */
  int	h	=	0;	/* Height. */
  $? "+ DkWxProgressDialog::chooseModalPosition"
  /* Sceen size */
  wxSize	scsz	=	wxGetDisplaySize();
  pParent->GetPosition(&px, &py);
  pParent->GetSize(&pw, &ph);
  GetSize(&w, &h);
  x = px + (pw - w) / 2;
  y = py + (ph - h) / 2;
  if((x + w) > scsz.x) { x = scsz.x - w; }
  if((y + h) > scsz.y) { y = scsz.y - h; }
  if(x < 0) { x = 0; }
  if(y < 0) { y = 0; }
  SetSize(x, y, w, h);
  $? "- DkWxProgressDialog::chooseModalPosition"
}