summaryrefslogtreecommitdiff
path: root/support/dktools/DkWxCommunicator.cpt
blob: d5efa833827559cf865c7cf282244b4c280ac36f (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
%%	options

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



%%	header

#ifndef	DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef WX_WXPREC_H_INCLUDED
#include <wx/wxprec.h>
#define	WX_WXPREC_H_INCLUDED 1
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#ifndef WX_WX_H_INCLUDED
#include <wx/wx.h>
#define	WX_WX_H_INCLUDED 1
#endif
#endif

/**	Synchronized communication between a worker thread and a
	progress dialog (ie DkWxProgressDialog).
*/
class DkWxCommunicator
{
  protected:

    /**	Grant access to only one thread at a time to other members.
    */
    wxCriticalSection	csProtect;

    /**	Flag: Thread is still running.
    */
    int			iRunning;

    /**	Autostart state.
    */
    int			iAutostart;

    /**	Flag: Worker thread can continue.
    */
    int			iCanContinue;

    /**	String containing all the messages produced by worker thread.
    */
    wxString		*sMessages;

    /**	Current file name processed.
    */
    wxChar		*sFilename;

    /**	Current gauge value (0-1000).
    */
    int			iGauge;

    /**	Flag: Update required.
    */
    int			iUpdated;

    /**	Encoding for wxChar.
    */
    int			wxe;

    /**	Encoding for dkChar.
    */
    int			dke;

    /**	Most significant log level.
    */
    int			iLogLevelMax;

    /**	Change file name, set iUpdated if new file name differs from
    	previous one.
    	@param	newFileName	New file name.
	@return	1 on success (new file name saved), 0 on error.
    */
    int
    internalSetFilename(wxChar const *newFileName);

    /**	Change gauge value, set iUpdated if new value differs
    	from previous one.
	@param	newValue	New gauge value.
    */
    void
    internalSetGauge(int newValue);

  public:

    /**	Constructor.
    	@param	we	Encoding used for wxChar strings.
	@param	de	Encoding used for dkChar strings.
    */
    DkWxCommunicator(int we, int de);

    /**	Destructor.
    */
    ~DkWxCommunicator();
    
    /**	Check whether the object has set up correctly and is ready to use.
    	@return	1 if setup is ok, 0 for error.
    */
    int
    checkSetup() const;

    /**	Update GUI elements.
    	@param	wDialog		Progress dialog.
	@param	tFilename	Static text to show file name.
	@param	gProgress	Progress bar.
	@return	Or-combination of DK_WX_COMMUNICATOR_STATUS_RUNNING
	and DK_WX_COMMUNICATOR_STATUS_UPDATE.
	If the thread is no longer running, you can close the progress
	dialog. If there was an update to file name or progress bar
	value you need to call Refresh() and Update().
    */
    int
    getUpdates(
      wxWindow		*wDialog,
      wxStaticText	*tFilename,
      wxGauge		*gProgress
    );

    /**	Set new file name and gauge value, may be used by a worker thread.
    	@param	fn	New file name.
	@param	nv	New gauge value.
	@return	1 on success, 0 on error.
    */
    int
    setUpdates(wxChar const *fn, int nv);

    /**	Update gauge value only, leave file name untouched.
    	@param	nv	New gauge value.
    */
    void
    updateGauge(int nv);

    /**	Set text to text control.
    	@param	tc	Text control to use for log messages from
			worker thread.
    */
    void
    getText(
      wxTextCtrl	*tc
    );

    /**	Prepare to run a worker thread.
    */
    void
    prepareRun();

    /**	Set can-continue flag.
    	@param	nv	New flag value (1=can continue, 0=must stop).
			Set to 0 if a "Cancel" button was pressed in the
			progress dialog.
    */
    void
    setCanContinue(int nv);

    /**	Get can-continue flag, used by the worker thread to react
    	on "Cancel" button.
    	@return	1 on success (can continue), 0 on error (cancel button pressed).
    */
    int
    getCanContinue();

    /**	Set iRunning.
    	A worker thread uses setRunning(0) to inidicate it is finished.
	@param	nv	New iRunning value.
    */
    void
    setRunning(int nv);

    /**	Check whether the thread is still running.
    	@return	1 for thread still running, 0 for thread finished.
    */
    int
    getRunning();

    /**	Enable/disable autostart job.
    	@param	nv	New autostart flag.
    */
    void
    autostartEnable(bool nv = true);

    /**	Check whether we can run an autostart job.
    	On success we change the internal autostart variable to indicate
	that an autostart job is currently running.
    	@return	1 to run autostart job, 0 otherwise.
    */
    int
    autostartCanRun();

    /**	Mark autostart job as finished. This is typically invoked
    	at the end of the autostart worker thread.
    */
    void
    autostartSetFinished();

    /**	Check whether the autostart job is completed so we can close the
    	window.
	@return	1 to close the window, 0 otherwise.
    */
    int
    autostartIsFinished();

    /**	Add text to log messages string.
    	@param	t	Text to add.
    */
    void
    addWxText(wxChar const *t);

    /**	Add text to log messages string.
    	@param	t	Text to add.
    */
    void
    addDkText(dkChar const * t);

    /**	Add text to log messages string.
    	@param	t	Text to add.
    */
    void
    addCharText(char const *t);

    /**	Add newline to log messages string.
    */
    void
    nl();

    /**	Set most significant log level.
    	@param	nv	Log level occured.
    */
    void
    setLogLevel(int nv);

    /**	Retrieve most significant log level.
    	@return	Log level.
    */
    int
    getLogLevel();

    /**	Retrieve autostart information.
    	@return	Current iAutostart value.
    */
    int
    autostartGet();

    /**	Get current messages text.
    	@return	Current text stored.
    */
    wxString getText();
};



/** Status: Thread is still running.
*/
#define	DK_WX_COMMUNICATOR_STATUS_RUNNING	1

/** Status: Thread updated data since last request.
*/
#define	DK_WX_COMMUNICATOR_STATUS_UPDATE	2

%%	module


#include "dk3wxs.h"
#include <dk3mem.h>
#include "DkWxCommunicator.h"


$!trace-include



/**	Empty text to use if no file name is specified.
*/
static wxChar const dkwx_communicator_empty_text[] = { wxT("") } ;

/**	Newline.
*/
static wxChar const dkwx_communicator_newline[] = { wxT("\n") } ;


DkWxCommunicator::DkWxCommunicator(int we, int de)
{
  $? "+ DkWxCommunicator::DkWxCommunicator"
  wxe = we;
  dke = de;
  iRunning = 0;
  iAutostart = DK3_WX_AUTOSTART_NORMAL;	$? ". normal start %d", iAutostart
  sMessages = NULL;
  sFilename = NULL;
  iGauge = 0;
  iUpdated = 0;
  iCanContinue = 1;
  iLogLevelMax = DK3_LL_IGNORE;
  sMessages = new wxString(dkwx_communicator_empty_text);
  $? "- DkWxCommunicator::DkWxCommunicator"
}



DkWxCommunicator::~DkWxCommunicator()
{
  {
    wxCriticalSectionLocker	lock(csProtect);
  }
  if(sMessages) {
    delete sMessages;
  }
  if(sFilename) {
    dk3_release(sFilename);
  }
  iRunning = 0;
  iGauge = 0;
  iUpdated = 0;
  iCanContinue = 0;
}



int
DkWxCommunicator::checkSetup() const
{
  int back = 0;
  if(sMessages) {
    back = 1;
  }
  return back;
}



int
DkWxCommunicator::getUpdates(
      wxWindow		*wDialog,
      wxStaticText	*tFilename,
      wxGauge		*gProgress
)
{
  int		back = 0;
  wxCriticalSectionLocker lock(csProtect);
  if(iUpdated) {
    back |= DK_WX_COMMUNICATOR_STATUS_UPDATE;
    if(tFilename) {
      if(sFilename) {
	tFilename->SetLabel(sFilename);
      } else {
	tFilename->SetLabel(dkwx_communicator_empty_text);
      }
    }
    if(gProgress) {
        gProgress->SetValue(iGauge);
    }
    iUpdated = 0;
  }
  if(iRunning) {
    back |= DK_WX_COMMUNICATOR_STATUS_RUNNING;
  }
  return back;
}



void
DkWxCommunicator::internalSetGauge(int newValue)
{
  int		nv;
  nv = newValue;
  if(nv < 0) nv = 0;
  if(nv > 1000) nv = 1000;
  if(iRunning) {
    if(nv == 1000) nv = 999;
  }
  if(nv != iGauge) {
    iGauge = nv;
    iUpdated = 1;
  }
}



int
DkWxCommunicator::internalSetFilename(wxChar const *newFileName)
{
  int		back = 0;
  if(newFileName) {
    if(sFilename) {
      if(dk3wxs_cmp(newFileName, sFilename)) {
        dk3_release(sFilename);
	sFilename = dk3wxs_dup(newFileName);
	if(sFilename) {
	  back = 1;
	}
	iUpdated = 1;
      } else {
        back = 1;
      }
    } else {
      sFilename = dk3wxs_dup(newFileName);
      if(sFilename) {
        iUpdated = 1;
	back = 1;
      }
    }
  } else {
    if(sFilename) {
      dk3_release(sFilename);
      sFilename = NULL;
      iUpdated = 1;
    }
    back = 1;
  }
  return back;
}



int
DkWxCommunicator::setUpdates(wxChar const *fn, int nv)
{
  int back = 0;
  wxCriticalSectionLocker lock(csProtect);
  if(internalSetFilename(fn)) { back = 1; }
  internalSetGauge(nv);
  return back;
}



void
DkWxCommunicator::updateGauge(int nv)
{
  wxCriticalSectionLocker lock(csProtect);
  internalSetGauge(nv);
}



void
DkWxCommunicator::prepareRun()
{
  wxCriticalSectionLocker	lock(csProtect);
  internalSetFilename(NULL);
  internalSetGauge(0);
  iRunning = 1;
  iCanContinue = 1;
  sMessages->Empty();
  iUpdated = 1;
  iLogLevelMax = DK3_LL_IGNORE;
}



void
DkWxCommunicator::setCanContinue(int nv)
{
  wxCriticalSectionLocker	lock(csProtect);
  iCanContinue = nv;
}



int
DkWxCommunicator::getCanContinue()
{
  int		back = 0;
  wxCriticalSectionLocker	lock(csProtect);
  back = iCanContinue;
  return back;
}



void
DkWxCommunicator::setRunning(int nv)
{
  $? "+ DkWxCommunicator::setRunning %d", nv
  wxCriticalSectionLocker	lock(csProtect);
  /*
  	Set new value.
  */
  iRunning = nv;
  /*
  	If we are no longer running we have finished
	the autostart job.
  */
  if(nv == 0) {		$? ". no longer running"
    if(iAutostart == DK3_WX_AUTOSTART_RUNNING) {	$? ". was autostart"
      iAutostart = DK3_WX_AUTOSTART_FINISHED;		$? ". autostart finished %d", iAutostart
    }
  } $? "- DkWxCommunicator::setRunning"
}



int
DkWxCommunicator::getRunning()
{
  int back = 1;
  {
    wxCriticalSectionLocker	lock(csProtect);
    back = iRunning;
  }
  return back;
}



void
DkWxCommunicator::getText( wxTextCtrl	*tc)
{
  wxCriticalSectionLocker	lock(csProtect);
  if(tc) {
    if(sMessages) {
      tc->ChangeValue(*sMessages);
    }
  }
}



void
DkWxCommunicator::addWxText(wxChar const *t)
{
  $? "+ addWxText \"%ls\"", t
  if(sMessages) {	$? ". sMessages ok"
    if(t) {		$? ". t ok"
      wxCriticalSectionLocker	lock(csProtect);
      $? ". before Append"
      sMessages->Append(t);
      $? ". after Append"
    }
  } $? "- addWxText"
}



void
DkWxCommunicator::addDkText(dkChar const *t)
{
  wxChar	bu[2 * DK3_MAX_PATH];

  if(sMessages) {
    if(t) {
      if(dk3wxs_from_dkstr(bu, DK3_SIZEOF(bu,wxChar), wxe, t, dke)) {
        wxCriticalSectionLocker	lock(csProtect);
	sMessages->Append(bu);
      }
    }
  }
}



void
DkWxCommunicator::addCharText(char const *t)
{
  wxChar	bu[2 * DK3_MAX_PATH];

  if(sMessages) {
    if(t) {
      if(dk3wxs_from_plain(bu, DK3_SIZEOF(bu,wxChar), t)) {
        wxCriticalSectionLocker lock(csProtect);
	sMessages->Append(bu);
      }
    }
  }
}


void
DkWxCommunicator::nl()
{
  if(sMessages) {
    wxCriticalSectionLocker	lock(csProtect);
    sMessages->Append(dkwx_communicator_newline);
  }
}



int
DkWxCommunicator::autostartCanRun()
{
  int 		back	= 0;
  $? "+ DkWxCommunicator::autostartCanRun"
  {
    wxCriticalSectionLocker	lock(csProtect);
    if(iAutostart == DK3_WX_AUTOSTART_START) {	$? ". autostart wanted"
      iAutostart = DK3_WX_AUTOSTART_RUNNING;	$? ". autostart running %d", iAutostart
      back = 1;
    }
  } $? "- DkWxCommunicator::autostartCanRun %d", back
  return back;
}



void
DkWxCommunicator::autostartSetFinished()
{
  $? "+ DkWxCommunicator::autostartSetFinished"
  wxCriticalSectionLocker	lock(csProtect);
  if(iAutostart == DK3_WX_AUTOSTART_RUNNING) {
    iAutostart = DK3_WX_AUTOSTART_FINISHED;	$? ". autostart finished %d", iAutostart
  } $? "- DkWxCommunicator::autostartSetFinished"
}



int
DkWxCommunicator::autostartIsFinished()
{
  int		back	= 0;
  $? "+ DkWxCommunicator::autostartIsFinished"
  {
    wxCriticalSectionLocker	lock(csProtect);
    if(iAutostart == DK3_WX_AUTOSTART_FINISHED) {
      iAutostart = DK3_WX_AUTOSTART_WAIT_FOR_EXIT; $? ". autostart waiting for exit %d", iAutostart
      back = 1;
    }
  } $? "- DkWxCommunicator::autostartIsFinished %d", back
  return back;
}



int
DkWxCommunicator::autostartGet()
{
  int back;
  $? "+ DkWxCommunicator::autostartGet"
  {
    wxCriticalSectionLocker	lock(csProtect);
    back = iAutostart;
  } $? "- DkWxCommunicator::autostartGet %d", back
  return back;
}



void
DkWxCommunicator::autostartEnable(bool nv)
{
  $? "+ DkWxCommunicator::autostartEnable %d", ((nv) ? 1 : 0)
  wxCriticalSectionLocker	lock(csProtect);
  if(nv) {
    iAutostart = DK3_WX_AUTOSTART_START;	$? ". autostart enabled %d", iAutostart
  } else {
    iAutostart = DK3_WX_AUTOSTART_NORMAL;	$? ". autostart disabled %d", iAutostart
  } $? "- DkWxCommunicator::autostartEnable"
}


void
DkWxCommunicator::setLogLevel(int nv)
{
  wxCriticalSectionLocker	lock(csProtect);
  if(nv < iLogLevelMax) {
    iLogLevelMax = nv;
  }
}



int
DkWxCommunicator::getLogLevel()
{
  int		back	= DK3_LL_IGNORE;
  {
    wxCriticalSectionLocker	lock(csProtect);
    back = iLogLevelMax;
  }
  return back;
}



wxString
DkWxCommunicator::getText()
{
  wxString back(wxT(""));
  if(sMessages) {
    back = *sMessages;
  }
  return back;
}