summaryrefslogtreecommitdiff
path: root/support/dktools/WinprintApp.cpt
blob: e2784892bb0a99510c1350e693ef8970df088e32 (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
%%	options

# __CHANGE__ 001:	Correct copyright owner if necessary.
copyright owner	=	Dirk Krause

# __CHANGE__ 003:	Correct copyright year(s) if necessary.
copyright year	=	2013-xxxx

# __CHANGE__ 002:	Correct license type if necessary.
#			Use "bsd", "gpl", "lgpl", or "commercial".
license		=	bsd



%%	header


/**	Application class, derived from wxApp, one instance per program.
*/
class WinprintApp : public wxApp
{
  protected:

    /**	Helper object.
    */
    DkWxAppHelper		*pHelper;

    /**	Controller fr online help.
    */
    DkWxHelpController		*helpController;

    /**	Print configuration.
    */
    dk3_print_conf_t		*pc;

    /**	Printer names.
    */
    DK3_PCWXCHAR		*pNames;

    /**	Number of elements in pNames.
    */
    size_t			 nNames;

    /**	Index of the default printer in pNames.
    */
    int				 nDefPrinter;

  protected:

    /**	Show error message if there is not sufficient memory available.
    */
    void
    ShowMemoryErrorMessage(void);

  public:

    /**	Application initialization.
    	@return	true on success, false on error.
    */
    virtual
    bool
    OnInit();

    /**	Application shutdown.
    	@return	0 on success, any other value indicates an error.
    */
    virtual
    int
    OnExit();

  protected:

    /**	Set printer names.
    */
    void setPrinterNames(void);

    /**	Release printer names.
    */
    void releasePrinterNames(void);
};



/**	Declaration as the wxApp object.
*/
DECLARE_APP(WinprintApp)



%%	module

#include "winprint.h"


$!trace-include



/**	Localized wxChar texts.
*/
static wxChar const *	winprint_texts[] = {
$!string-table	macro=wxT
#
#   0:	Menu "File"
#
File
#
#   1:	Menu item "File/Exit"
#
Exit
#
#   2:	Tool tip text for menu item "File/Exit"
#
Exit the application
#
#   3:	Menu "Help"
#
Help
#
#   4:	Menu item "Help/About"
#
About
#
#   5:	Tooltip text for menu item "Help/About"
#
Show version information
#
#   6:	Menu item "Help/Contents"
#
Contents
#
#   7:	Tooltip text for menu item "Help/Contents"
#
Open table of contents
#
#   8:	Initial status text
#
Ready
#
#   9:	Copyright notice.
#
Copyright (c) 
#
#  10:	Dialog box title "About ..."
#
About 
#
#  11:	This program uses the following libraries
#
This program uses the following libraries:
#
#  12:	See
#
See:
#
#  13:	__CHANGE__ 015:	Text for dummy label, can be reused if lDummy removed.
#
Initializing print queue names.
#
#   14
#
Success, exiting.
#
#   15
#
Print
#
#   16
#
Schedule a file to printer.
#
#   17
#
Transferring print data.
#
#   18
#
Choose file to print
#
#   19
#
Aborted by user!
#
#   20
#
Data transfer
#
#   21
#
Cancel
#
#   22
#
Cancel data transfer
#
#   23
#
Data transfer cancelled, wait please...
#
#   24
#
ERROR: Bad printer name!
#
#   25
#
ERROR: No default printer found!
#
#   26
#
ERROR: Failed to obtain print configuration!
#
#   27
#
ERROR: Failed to create background thread!
#
#   28
#
ERROR: Failed to create progress dialog!
#
#   29
#
Error while scheduling print data
#
#   30
#
Printer selection
#
#   31
#
Choose printer:
#
#   32
#
OK
#
#   33
#
Use selected printer.
#
#   34
#
Cancel
#
#   35
#
Abort operation
#
#   36
#
Choose the destination printer
#
#   37
#
Transfer completed.
#
#   38
#
Error while writing print data to job file!
#
#   39
#
Failed to open input file!
#
#   40
#
Failed to schedule job to print queue!
#
#   41
#
Failed to create print job data file!
#
#   42
#
Failed to add print job to queue!
#
#   43
#
Failed to open print queue!
#
#   44
#
File name conversion failed!
#
#   45
#
Printer name conversion failed!
$!end
};



/**	Non-localized wxChar texts.
*/
static wxChar const *	winprint_nl_wx[] = {
$!string-table	macro=wxT
#
#   0:	Program name.		__CHANGE__ 004:	Correct program name.
#
winprint
#
#   1:	Program version.	__CHANGE__ 005:	Correct program version.
#
1.0.0
#
#   2:	Copyright owner name.	__CHANGE__ 001:	Correct copyright owner name.
#
Dirk Krause
#
#   3:	Software vendor name.	__CHANGE__ 007: Correct software vendor name
#						if necessary. Must be one
#						string without spaces.
#
Dirk Krause
#
#   4:	Resource name of Windows icon
#
aaaaa
#
#   5:	Windows chm help file name
#
winprint.chm
#
#   6:	non-Windows htb help file name
#
winprint.htb
#
#   7:	Space
#
 
#
#   8:	Newline
#
\n
#
#   9 10 11 12 13 14 15:	List of libraries used.
#
DK tools, wxWidgets, libpng, libjpeg, libtiff, zlib.
http://dktools.sourceforge.net
http://www.wxwidgets.org
http://www.libpng.org/pub/png/libpng.html
http://www.ijg.org
http://www.remotesensing.org/libtiff
http://www.zlib.net
#
# 16 17
#
Error
Not enough memory (RAM)!
$!end
};



/**	Non-localized dkChar texts.
*/
static dkChar const *	winprint_nl_dk[] = {
$!string-table	macro=dkT
#
#   0:	Program group name.	__CHANGE__ 006:	Correct program group name.
#
dktools
#
#   1:	String table name.
#
winprint.str
$!end
};



/**	Implementation of the wxApp functionality.
*/
IMPLEMENT_APP(WinprintApp)


bool
WinprintApp::OnInit()
{
  WinprintFrame		*frame		= NULL;	/* Main window to show. */
  wxChar const * const	*localizedTexts	= NULL;	/* Localized texts. */
  wxPNGHandler		*phPng		= NULL;
  wxXPMHandler		*phXpm		= NULL;
  wxICOHandler		*phIco		= NULL;
  wxZipFSHandler	*phZipFs	= NULL;
  bool			 back		= false;

  $!trace-init winprint.deb

  /* Initialize members. */
  nDefPrinter = 0;
  pNames = NULL;
  pHelper = NULL;
  helpController = NULL;
  pc = NULL;

  /*	__CHANGE__ 009:	Initialize further members here.
  */

  /* Set up helper object. */
  pHelper = new DkWxAppHelper(
    argv[0],
    winprint_nl_wx[3],
    winprint_nl_dk[0]
  );
  if(!(pHelper)) { ShowMemoryErrorMessage(); goto finished; }
  if(!(pHelper->checkSetup())) { goto finished; }

  /* Add image and file system handlers for online help. */
  phPng = new wxPNGHandler();
  if (NULL != phPng) {
    wxImage::AddHandler(phPng);
  } else {
    ShowMemoryErrorMessage();
    goto finished;
  }
  phXpm = new wxXPMHandler();
  if (NULL != phXpm) {
    wxImage::AddHandler(phXpm);
  } else {
    ShowMemoryErrorMessage();
    goto finished;
  }
  phIco = new wxICOHandler();
  if (NULL != phIco) {
    wxImage::AddHandler(phIco);
  } else {
    ShowMemoryErrorMessage();
    goto finished;
  }
  phZipFs = new wxZipFSHandler();
  if (NULL != phZipFs) {
    wxFileSystem::AddHandler(phZipFs);
  } else {
    ShowMemoryErrorMessage();
    goto finished;
  }

  localizedTexts = pHelper->getStringTable(winprint_texts, winprint_nl_dk[1]);
  if(!(localizedTexts)) { localizedTexts = winprint_texts; }

  /* Set up online help controller. */
  helpController = new DkWxHelpController(
    pHelper,
    winprint_nl_wx[5],
    winprint_nl_wx[6]
  );
  if(!(helpController)) { ShowMemoryErrorMessage(); goto finished; }

  pc = dk3print_conf_open(pHelper->getApp(), 0);
  if(!(pc)) { ShowMemoryErrorMessage(); goto finished; }

  setPrinterNames();
  if(!(pNames)) { ShowMemoryErrorMessage(); goto finished; }

  /* Create and show frame. */
  frame = new WinprintFrame(
    WinprintFrame_MainWindow,
    pHelper,
    helpController,
    argc,
    argv,
    localizedTexts,
    winprint_nl_wx,
    winprint_nl_dk,
    pc,
    pNames,
    nNames,
    nDefPrinter
  );
  if(!(frame)) { ShowMemoryErrorMessage(); goto finished; }
  frame->Show();
  frame->restorePosition();
  back = true;

  /* Release resources if initialization failed. */
  finished:
  if(!(back)) {
    if(pNames) {
      releasePrinterNames();
    }
    if(helpController) {
      delete(helpController);
      helpController = NULL;
    }
    if(pHelper) {
      delete(pHelper);
      pHelper = NULL;
    }
    if(pc) {
      dk3print_conf_close(pc);
      pc = NULL;
    }
  }
  return back;
}



int
WinprintApp::OnExit()
{
  int			 back = 0;

  /*	__CHANGE__ 009:	Release resources allocated by further members.
  */

  /* Release resources. */
  if(pNames) {
    releasePrinterNames();
  }
  if(pc) {
    dk3print_conf_close(pc);
    pc = NULL;
  }
  if(helpController) {
    delete(helpController);
    helpController = NULL;
  }
  if(pHelper) {
    delete(pHelper);
    pHelper = NULL;
  }

  $!trace-end

  /*	__CHANGE__ 010:	Set back to exit status code.
  */
  return back;
}



void
WinprintApp::setPrinterNames(void)
{
  wxChar		 buf[512];	/* Conversion buffer. */
  void			*vptr;		/* Printer as returned from iterator. */
  dk3_printer_t		*pr;		/* Current printer. */
  DK3_PCWXCHAR		*wxptr;		/* Printer name in wxChar. */
  size_t	 	 n = 0;		/* Number of printers. */
  size_t		 i;		/* Index of current printer. */
  int			 wxe;		/* wxChar encoding. */
  int			 dke;		/* dkChar encoding. */
  
  if(pc) {
    dk3sto_it_reset(pc->iPrinters);
    while(NULL != (vptr = dk3sto_it_next(pc->iPrinters))) {
      pr = (dk3_printer_t *)vptr;
      if(DK3_PRINTER_TYPE_WINDOWS == pr->t_p) {
        if(pr->name) {
	  n++;
	}
      }
    }
    if(n) {
      pNames = dk3_new_app(DK3_PCWXCHAR,n,pHelper->getApp());
      if(pNames) {
        wxptr = pNames;
	for(i = 0; i < n; i++) {
	  *(wxptr++) = NULL;
	}
	i = 0;
	wxptr = pNames;
	dk3sto_it_reset(pc->iPrinters);
	while(NULL != (vptr = dk3sto_it_next(pc->iPrinters))) {
	  pr = (dk3_printer_t *)vptr;
	  if(DK3_PRINTER_TYPE_WINDOWS == pr->t_p) {
	    if(pr->name) {
	      wxe = pHelper->getWxEncoding();
	      dke = pHelper->getDkEncoding();
	      if(dk3wxs_from_dkstr(buf,DK3_SIZEOF(buf,wxChar),wxe,pr->name,dke))
	      {
	        *wxptr = dk3wxs_dup(buf);
		if(*wxptr) {
		  if(pc->defPrinter) {
		    if((pc->defPrinter)->name) {
		      if(dk3str_cmp(pr->name, (pc->defPrinter)->name) == 0) {
		        nDefPrinter = (int)i;
		      }
		    }
		  }
		  wxptr++;
		  i++;
		}
	      }
	    }
	  }
	}
	nNames = i;
      }
    }
  }
}


void
WinprintApp::releasePrinterNames(void)
{
  DK3_PCWXCHAR		*wxptr;	/* Current printer name to release. */
  size_t		 i;	/* Index of current printer name. */
  $? "+ WinprintApp::releasePrinterNames"
  if(pNames) {
    wxptr = pNames;
    for(i = 0; i < nNames; i++) {
      if(*wxptr) {
        $? ". Printer name \"%ls\"", *wxptr
        dk3_release(*wxptr);
	wxptr++;
      }
    }
    dk3_delete(pNames);
    pNames = NULL;
  } $? "- WinprintApp::releasePrinterNames"
}



void
WinprintApp::ShowMemoryErrorMessage(void)
{
  const wxChar	*s_title	= NULL;
  const wxChar	*s_text		= NULL;

  if (NULL != pHelper) {
    s_title = pHelper->getBasicString(3);
    s_text  = pHelper->getBasicString(9);
  }
  if (NULL == s_title) {
    s_title = winprint_nl_wx[16];
  }
  if (NULL == s_text) {
    s_text  = winprint_nl_wx[17];
  }
  wxMessageBox(s_text, s_title, (wxOK | wxCENTRE | wxICON_ERROR));
}