summaryrefslogtreecommitdiff
path: root/openjpeg/src/bin/wx/OPJViewer/source/OPJViewer.cpp
blob: bb1ea05da62ede7afceff31f29c9686c66fb5a5b (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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
/*
 * The copyright in this software is being made available under the 2-clauses 
 * BSD License, included below. This software may be subject to other third 
 * party and contributor rights, including patent rights, and no such rights
 * are granted under this license.
 *
 * Copyright (c) 2007, Digital Signal Processing Laboratory, Universita' degli studi di Perugia (UPG), Italy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/////////////////////////////////////////////////////////////////////////////
// Name:        sashtest.cpp
// Purpose:     Layout/sash sample
// Author:      Julian Smart
// Modified by:
// Created:     04/01/98
// RCS-ID:      $Id: sashtest.cpp,v 1.18 2005/08/23 15:54:35 ABX Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        treetest.cpp
// Purpose:     wxTreeCtrl sample
// Author:      Julian Smart
// Modified by:
// Created:     04/01/98
// RCS-ID:      $Id: treetest.cpp,v 1.110 2006/11/04 11:26:51 VZ Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        dialogs.cpp
// Purpose:     Common dialogs demo
// Author:      Julian Smart
// Modified by: ABX (2004) - adjustements for conditional building + new menu
// Created:     04/01/98
// RCS-ID:      $Id: dialogs.cpp,v 1.163 2006/11/04 10:57:24 VZ Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        thread.cpp
// Purpose:     wxWidgets thread sample
// Author:      Guilhem Lavaux, Vadim Zeitlin
// Modified by:
// Created:     06/16/98
// RCS-ID:      $Id: thread.cpp,v 1.26 2006/10/02 05:36:28 PC Exp $
// Copyright:   (c) 1998-2002 wxWidgets team
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Name:        samples/image/image.cpp
// Purpose:     sample showing operations with wxImage
// Author:      Robert Roebling
// Modified by:
// Created:     1998
// RCS-ID:      $Id: image.cpp,v 1.120 2006/12/06 17:13:11 VZ Exp $
// Copyright:   (c) 1998-2005 Robert Roebling
// License:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        samples/console/console.cpp
// Purpose:     A sample console (as opposed to GUI) program using wxWidgets
// Author:      Vadim Zeitlin
// Modified by:
// Created:     04.10.99
// RCS-ID:      $Id: console.cpp,v 1.206 2006/11/12 19:55:19 VZ Exp $
// Copyright:   (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        samples/notebook/notebook.cpp
// Purpose:     a sample demonstrating notebook usage
// Author:      Julian Smart
// Modified by: Dimitri Schoolwerth
// Created:     26/10/98
// RCS-ID:      $Id: notebook.cpp,v 1.49 2006/11/04 18:24:07 RR Exp $
// Copyright:   (c) 1998-2002 wxWidgets team
// License:     wxWindows license
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        dialogs.cpp
// Purpose:     Common dialogs demo
// Author:      Julian Smart
// Modified by: ABX (2004) - adjustements for conditional building + new menu
// Created:     04/01/98
// RCS-ID:      $Id: dialogs.cpp,v 1.163 2006/11/04 10:57:24 VZ Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        dnd.cpp
// Purpose:     Drag and drop sample
// Author:      Vadim Zeitlin
// Modified by:
// Created:     04/01/98
// RCS-ID:      $Id: dnd.cpp,v 1.107 2006/10/30 20:23:41 VZ Exp $
// Copyright:
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Name:        test.cpp
// Purpose:     wxHtml testing example
/////////////////////////////////////////////////////////////////////////////


#include "OPJViewer.h"

IMPLEMENT_APP(OPJViewerApp)

// For drawing lines in a canvas
long xpos = -1;
long ypos = -1;

int winNumber = 1;

// Initialise this in OnInit, not statically
bool OPJViewerApp::OnInit(void)
{
	int n;
#if wxUSE_UNICODE

    wxChar **wxArgv = new wxChar *[argc + 1];

    for (n = 0; n < argc; n++ ) {
        wxMB2WXbuf warg = wxConvertMB2WX((char *) argv[n]);
        wxArgv[n] = wxStrdup(warg);
    }

    wxArgv[n] = NULL;

#else // !wxUSE_UNICODE

    #define wxArgv argv

#endif // wxUSE_UNICODE/!wxUSE_UNICODE

#if wxUSE_CMDLINE_PARSER

    static const wxCmdLineEntryDesc cmdLineDesc[] =
    {
        { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show this help message"),
            wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },

        { wxCMD_LINE_PARAM,  NULL, NULL, _T("input file"),
            wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },

        { wxCMD_LINE_NONE }
    };

    wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);

    switch (parser.Parse()) {
    case -1:
        wxLogMessage(wxT("Help was given, terminating."));
        break;

    case 0:
        ShowCmdLine(parser);
        break;

    default:
        wxLogMessage(wxT("Syntax error detected."));
        break;
    }

#endif // wxUSE_CMDLINE_PARSER

    //wxInitAllImageHandlers();
#if wxUSE_LIBJPEG
  wxImage::AddHandler( new wxJPEGHandler );
#endif
#if wxUSE_LIBOPENJPEG
  wxImage::AddHandler( new wxJPEG2000Handler );
#endif
#if USE_MXF
  wxImage::AddHandler( new wxMXFHandler );
#endif // USE_MXF
#if OPJ_MANYFORMATS
  wxImage::AddHandler( new wxBMPHandler );
  wxImage::AddHandler( new wxPNGHandler );
  wxImage::AddHandler( new wxGIFHandler );
  wxImage::AddHandler( new wxPNMHandler );
  wxImage::AddHandler( new wxTIFFHandler );
#endif
    // we use a XPM image in our HTML page
    wxImage::AddHandler(new wxXPMHandler);

	// memory file system
    wxFileSystem::AddHandler(new wxMemoryFSHandler);

#ifdef OPJ_INICONFIG
	//load decoding engine parameters
	OPJconfig = new wxConfig(OPJ_APPLICATION, OPJ_APPLICATION_VENDOR);

	OPJconfig->Read(wxT("decode/enabledeco"), &m_enabledeco, (bool) true);
	OPJconfig->Read(wxT("decode/enableparse"), &m_enableparse, (bool) true);
	OPJconfig->Read(wxT("decode/resizemethod"), &m_resizemethod, (long) 0);
	OPJconfig->Read(wxT("decode/xxxreducefactor"), &m_reducefactor, (long) 0);
	OPJconfig->Read(wxT("decode/xxxqualitylayers"), &m_qualitylayers, (long) 0);
	OPJconfig->Read(wxT("decode/xxxcomponents"), &m_components, (long) 0);
	OPJconfig->Read(wxT("decode/xxxframenum"), &m_framenum, (long) 0);
#ifdef USE_JPWL
	OPJconfig->Read(wxT("decode/enablejpwl"), &m_enablejpwl, (bool) true);
	OPJconfig->Read(wxT("decode/expcomps"), &m_expcomps, (long) JPWL_EXPECTED_COMPONENTS);
	OPJconfig->Read(wxT("decode/maxtiles"), &m_maxtiles, (long) JPWL_MAXIMUM_TILES);
#endif // USE_JPWL

	OPJconfig->Write(wxT("teststring"), wxT("This is a test value"));
	OPJconfig->Write(wxT("testbool"), (bool) true);
	OPJconfig->Write(wxT("testlong"), (long) 245);

	OPJconfig->Read(wxT("showtoolbar"), &m_showtoolbar, (bool) true);
	OPJconfig->Read(wxT("showbrowser"), &m_showbrowser, (bool) true);
	OPJconfig->Read(wxT("showpeeker"), &m_showpeeker, (bool) true);
	OPJconfig->Read(wxT("browserwidth"), &m_browserwidth, (long) OPJ_BROWSER_WIDTH);
	OPJconfig->Read(wxT("peekerheight"), &m_peekerheight, (long) OPJ_PEEKER_HEIGHT);
	OPJconfig->Read(wxT("framewidth"), &m_framewidth, (long) OPJ_FRAME_WIDTH);
	OPJconfig->Read(wxT("frameheight"), &m_frameheight, (long) OPJ_FRAME_HEIGHT);

	// load encoding engine parameters
	OPJconfig->Read(wxT("encode/subsampling"), &m_subsampling, (wxString) wxT("1,1"));
	OPJconfig->Read(wxT("encode/origin"), &m_origin, (wxString) wxT("0,0"));
	OPJconfig->Read(wxT("encode/rates"), &m_rates, (wxString) wxT("20,10,5"));
	OPJconfig->Read(wxT("encode/quality"), &m_quality, (wxString) wxT("30,35,40"));
	OPJconfig->Read(wxT("encode/enablequality"), &m_enablequality, (bool) false);
	OPJconfig->Read(wxT("encode/multicomp"), &m_multicomp, (bool) false);	
	OPJconfig->Read(wxT("encode/irreversible"), &m_irreversible, (bool) false);	
	OPJconfig->Read(wxT("encode/resolutions"), &m_resolutions, (int) 6);	
	OPJconfig->Read(wxT("encode/progression"), &m_progression, (int) 0);	
	OPJconfig->Read(wxT("encode/cbsize"), &m_cbsize, (wxString) wxT("32,32"));
	OPJconfig->Read(wxT("encode/prsize"), &m_prsize, (wxString) wxT("[128,128],[128,128]"));
	OPJconfig->Read(wxT("encode/tsize"), &m_tsize, (wxString) wxT(""));
	OPJconfig->Read(wxT("encode/torigin"), &m_torigin, (wxString) wxT("0,0"));
	OPJconfig->Read(wxT("encode/enablesop"), &m_enablesop, (bool) false);	
	OPJconfig->Read(wxT("encode/enableeph"), &m_enableeph, (bool) false);	
	OPJconfig->Read(wxT("encode/enablebypass"), &m_enablebypass, (bool) false);	
	OPJconfig->Read(wxT("encode/enablereset"), &m_enablereset, (bool) false);	
	OPJconfig->Read(wxT("encode/enablerestart"), &m_enablerestart, (bool) false);	
	OPJconfig->Read(wxT("encode/enablevsc"), &m_enablevsc, (bool) false);	
	OPJconfig->Read(wxT("encode/enableerterm"), &m_enableerterm, (bool) false);	
	OPJconfig->Read(wxT("encode/enablesegmark"), &m_enablesegmark, (bool) false);	
	OPJconfig->Read(wxT("encode/enablecomm"), &m_enablecomm, (bool) true);	
	OPJconfig->Read(wxT("encode/enablepoc"), &m_enablepoc, (bool) false);	
	OPJconfig->Read(wxT("encode/comment"), &m_comment, (wxString) wxT(""));
	OPJconfig->Read(wxT("encode/poc"), &m_poc, (wxString) wxT("T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL"));
	OPJconfig->Read(wxT("encode/enableidx"), &m_enableidx, (bool) false);	
	OPJconfig->Read(wxT("encode/index"), &m_index, (wxString) wxT("index.txt"));
#ifdef USE_JPWL
	OPJconfig->Read(wxT("encode/enablejpwl"), &m_enablejpwle, (bool) true);
	for (n = 0; n < MYJPWL_MAX_NO_TILESPECS; n++) {
		OPJconfig->Read(wxT("encode/jpwl/hprotsel") + wxString::Format(wxT("%02d"), n), &m_hprotsel[n], 0);
		OPJconfig->Read(wxT("encode/jpwl/htileval") + wxString::Format(wxT("%02d"), n), &m_htileval[n], 0);
		OPJconfig->Read(wxT("encode/jpwl/pprotsel") + wxString::Format(wxT("%02d"), n), &m_pprotsel[n], 0);
		OPJconfig->Read(wxT("encode/jpwl/ptileval") + wxString::Format(wxT("%02d"), n), &m_ptileval[n], 0);
		OPJconfig->Read(wxT("encode/jpwl/ppackval") + wxString::Format(wxT("%02d"), n), &m_ppackval[n], 0);
		OPJconfig->Read(wxT("encode/jpwl/sensisel") + wxString::Format(wxT("%02d"), n), &m_sensisel[n], 0);
		OPJconfig->Read(wxT("encode/jpwl/stileval") + wxString::Format(wxT("%02d"), n), &m_stileval[n], 0);
	}
#endif // USE_JPWL

#else
	// set decoding engine parameters
	m_enabledeco = true;
	m_enableparse = true;
	m_resizemethod = 0;
	m_reducefactor = 0;
	m_qualitylayers = 0;
	m_components = 0;
	m_framenum = 0;
#ifdef USE_JPWL
	m_enablejpwl = true;
	m_expcomps = JPWL_EXPECTED_COMPONENTS;
	m_maxtiles = JPWL_MAXIMUM_TILES;
#endif // USE_JPWL
	m_showtoolbar = true;
	m_showbrowser = true;
	m_showpeeker = true;
	m_browserwidth = OPJ_BROWSER_WIDTH;
	m_peekerheight = OPJ_PEEKER_HEIGHT;
	m_framewidth = OPJ_FRAME_WIDTH;
	m_frameheight = OPJ_FRAME_HEIGHT;

	// set encoding engine parameters
	m_subsampling = wxT("1,1");
	m_origin = wxT("0,0");
	m_rates = wxT("20,10,5");
	m_quality = wxT("30,35,40");
	m_enablequality = false;
	m_multicomp = false;
	m_irreversible = false;
	m_resolutions = 6;
	m_progression = 0;
	m_cbsize= wxT("32,32");
	m_prsize= wxT("[128,128],[128,128]");
	m_tsize = wxT("");
	m_torigin = wxT("0,0");
	m_enablesop = false;
	m_enableeph = false;
	m_enablebypass = false;
	m_enablereset = false;
	m_enablerestart = false;
	m_enablevsc = false;
	m_enableerterm = false;
	m_enablesegmark = false;
	m_enableidx = false;
	m_index = wxT("index.txt");
	m_enablecomm = true;
	m_comment = wxT("");
	m_enablepoc = false;
	m_poc = wxT("T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL");
#ifdef USE_JPWL
	m_enablejpwle = true;
	for (n = 0; n < MYJPWL_MAX_NO_TILESPECS; n++) {
		m_hprotsel[n] = 0;
		m_htileval[n] = 0;
		m_pprotsel[n] = 0;
		m_ptileval[n] = 0;
		m_sensisel[n] = 0;
		m_stileval[n] = 0;
	}
#endif // USE_JPWL

#endif // OPJ_INICONFIG

	if (m_comment == wxT("")) {
#if defined __WXMSW__
		m_comment = wxT("Created by OPJViewer Win32 - OpenJPEG  version ");
#elif defined __WXGTK__
		m_comment = wxT("Created by OPJViewer Lin32 - OpenJPEG version ");
#else
		m_comment = wxT("Created by OPJViewer - OpenJPEG version ");
#endif

#ifdef USE_JPWL
		m_comment += wxString::Format(wxT("%s with JPWL"), (char *) opj_version());
#else
		m_comment += wxString::Format(wxT("%s"), (char *) opj_version());
#endif
	}

	// Create the main frame window
  OPJFrame *frame = new OPJFrame(NULL, wxID_ANY, OPJ_APPLICATION_TITLEBAR,
					  wxDefaultPosition, wxSize(wxGetApp().m_framewidth, wxGetApp().m_frameheight),
                      wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE |
                      wxHSCROLL | wxVSCROLL);

  // Give it an icon (this is ignored in MDI mode: uses resources)
#ifdef __WXMSW__
  frame->SetIcon(wxIcon(wxT("OPJViewer16")));
#endif

  frame->Show(true);

  SetTopWindow(frame);

	// if there are files on the command line, open them
	if (!(m_filelist.IsEmpty())) {
		//wxLogMessage(wxT("Habemus files!!!"));
		wxArrayString paths, filenames;
		for (unsigned int f = 0; f < wxGetApp().m_filelist.GetCount(); f++) {
			paths.Add(wxFileName(wxGetApp().m_filelist[f]).GetFullPath());
			filenames.Add(wxFileName(wxGetApp().m_filelist[f]).GetFullName());
		}
		//wxLogMessage(paths[0]);
		frame->OpenFiles(paths, filenames);
	}

  return true;
}

int OPJViewerApp::OnExit()
{
	int n;

#ifdef OPJ_INICONFIG
	OPJconfig->Write(wxT("decode/enabledeco"), m_enabledeco);
	OPJconfig->Write(wxT("decode/enableparse"), m_enableparse);
	OPJconfig->Write(wxT("decode/resizemethod"), m_resizemethod);
	OPJconfig->Write(wxT("decode/reducefactor"), m_reducefactor);
	OPJconfig->Write(wxT("decode/qualitylayers"), m_qualitylayers);
	OPJconfig->Write(wxT("decode/components"), m_components);
	OPJconfig->Write(wxT("decode/framenum"), m_framenum);
#ifdef USE_JPWL
	OPJconfig->Write(wxT("decode/enablejpwl"), m_enablejpwl);
	OPJconfig->Write(wxT("decode/expcomps"), m_expcomps);
	OPJconfig->Write(wxT("decode/maxtiles"), m_maxtiles);
#endif // USE_JPWL
	OPJconfig->Write(wxT("showtoolbar"), m_showtoolbar);
	OPJconfig->Write(wxT("showbrowser"), m_showbrowser);
	OPJconfig->Write(wxT("showpeeker"), m_showpeeker);
	OPJconfig->Write(wxT("browserwidth"), m_browserwidth);
	OPJconfig->Write(wxT("peekerheight"), m_peekerheight);
	OPJconfig->Write(wxT("framewidth"), m_framewidth);
	OPJconfig->Write(wxT("frameheight"), m_frameheight);

	OPJconfig->Write(wxT("encode/subsampling"), m_subsampling);
	OPJconfig->Write(wxT("encode/origin"), m_origin);
	OPJconfig->Write(wxT("encode/rates"), m_rates);
	OPJconfig->Write(wxT("encode/quality"), m_quality);
	OPJconfig->Write(wxT("encode/enablequality"), m_enablequality);
	OPJconfig->Write(wxT("encode/multicomp"), m_multicomp);
	OPJconfig->Write(wxT("encode/irreversible"), m_irreversible);
	OPJconfig->Write(wxT("encode/resolutions"), m_resolutions);
	OPJconfig->Write(wxT("encode/progression"), m_progression);
	OPJconfig->Write(wxT("encode/cbsize"), m_cbsize);
	OPJconfig->Write(wxT("encode/prsize"), m_prsize);
	OPJconfig->Write(wxT("encode/tiles"), m_tsize);
	OPJconfig->Write(wxT("encode/torigin"), m_torigin);
	OPJconfig->Write(wxT("encode/enablesop"), m_enablesop);
	OPJconfig->Write(wxT("encode/enableeph"), m_enableeph);
	OPJconfig->Write(wxT("encode/enablebypass"), m_enablebypass);
	OPJconfig->Write(wxT("encode/enablereset"), m_enablereset);
	OPJconfig->Write(wxT("encode/enablerestart"), m_enablerestart);
	OPJconfig->Write(wxT("encode/enablevsc"), m_enablevsc);
	OPJconfig->Write(wxT("encode/enableerterm"), m_enableerterm);
	OPJconfig->Write(wxT("encode/enablesegmark"), m_enablesegmark);
	OPJconfig->Write(wxT("encode/enableidx"), m_enableidx);
	OPJconfig->Write(wxT("encode/index"), m_index);
	OPJconfig->Write(wxT("encode/enablecomm"), m_enablecomm);
	OPJconfig->Write(wxT("encode/comment"), m_comment);
	OPJconfig->Write(wxT("encode/enablepoc"), m_enablepoc);
	OPJconfig->Write(wxT("encode/poc"), m_poc);
#ifdef USE_JPWL
	OPJconfig->Write(wxT("encode/enablejpwl"), m_enablejpwle);
	for (n = 0; n < MYJPWL_MAX_NO_TILESPECS; n++) {
		OPJconfig->Write(wxT("encode/jpwl/hprotsel") + wxString::Format(wxT("%02d"), n), m_hprotsel[n]);
		OPJconfig->Write(wxT("encode/jpwl/htileval") + wxString::Format(wxT("%02d"), n), m_htileval[n]);
		OPJconfig->Write(wxT("encode/jpwl/pprotsel") + wxString::Format(wxT("%02d"), n), m_pprotsel[n]);
		OPJconfig->Write(wxT("encode/jpwl/ptileval") + wxString::Format(wxT("%02d"), n), m_ptileval[n]);
		OPJconfig->Write(wxT("encode/jpwl/ppackval") + wxString::Format(wxT("%02d"), n), m_ppackval[n]);
		OPJconfig->Write(wxT("encode/jpwl/sensisel") + wxString::Format(wxT("%02d"), n), m_sensisel[n]);
		OPJconfig->Write(wxT("encode/jpwl/stileval") + wxString::Format(wxT("%02d"), n), m_stileval[n]);
	}
#endif // USE_JPWL

#endif // OPJ_INICONFIG

	return 1;
}

void OPJViewerApp::ShowCmdLine(const wxCmdLineParser& parser)
{
    wxString s = wxT("Command line parsed successfully:\nInput files: ");

    size_t count = parser.GetParamCount();
    for (size_t param = 0; param < count; param++) {
        s << parser.GetParam(param) << ';';
		m_filelist.Add(parser.GetParam(param));
    }

    //wxLogMessage(s);
}

// OPJFrame events

// Event class for sending text messages between worker and GUI threads
BEGIN_EVENT_TABLE(OPJFrame, wxMDIParentFrame)
    EVT_MENU(OPJFRAME_HELPABOUT, OPJFrame::OnAbout)
    EVT_MENU(OPJFRAME_FILEOPEN, OPJFrame::OnFileOpen)
    EVT_MENU(OPJFRAME_FILESAVEAS, OPJFrame::OnFileSaveAs)
    EVT_MENU(OPJFRAME_MEMORYOPEN, OPJFrame::OnMemoryOpen)
    EVT_SIZE(OPJFrame::OnSize)
    EVT_MENU(OPJFRAME_FILEEXIT, OPJFrame::OnQuit)
    EVT_MENU(OPJFRAME_FILECLOSE, OPJFrame::OnClose)
    EVT_MENU(OPJFRAME_VIEWZOOM, OPJFrame::OnZoom)
    EVT_MENU(OPJFRAME_VIEWFIT, OPJFrame::OnFit)
    EVT_MENU(OPJFRAME_VIEWRELOAD, OPJFrame::OnReload)
    EVT_MENU(OPJFRAME_VIEWPREVFRAME, OPJFrame::OnPrevFrame)
    EVT_MENU(OPJFRAME_VIEWHOMEFRAME, OPJFrame::OnHomeFrame)
    EVT_MENU(OPJFRAME_VIEWNEXTFRAME, OPJFrame::OnNextFrame)
    EVT_MENU(OPJFRAME_VIEWLESSLAYERS, OPJFrame::OnLessLayers)
    EVT_MENU(OPJFRAME_VIEWALLLAYERS, OPJFrame::OnAllLayers)
    EVT_MENU(OPJFRAME_VIEWMORELAYERS, OPJFrame::OnMoreLayers)
    EVT_MENU(OPJFRAME_VIEWLESSRES, OPJFrame::OnLessRes)
    EVT_MENU(OPJFRAME_VIEWFULLRES, OPJFrame::OnFullRes)
    EVT_MENU(OPJFRAME_VIEWMORERES, OPJFrame::OnMoreRes)
    EVT_MENU(OPJFRAME_VIEWPREVCOMP, OPJFrame::OnPrevComp)
    EVT_MENU(OPJFRAME_VIEWALLCOMPS, OPJFrame::OnAllComps)
    EVT_MENU(OPJFRAME_VIEWNEXTCOMP, OPJFrame::OnNextComp)
    EVT_MENU(OPJFRAME_FILETOGGLEB, OPJFrame::OnToggleBrowser)
    EVT_MENU(OPJFRAME_FILETOGGLEP, OPJFrame::OnTogglePeeker)
    EVT_MENU(OPJFRAME_FILETOGGLET, OPJFrame::OnToggleToolbar)
    EVT_MENU(OPJFRAME_SETSENCO, OPJFrame::OnSetsEnco)
    EVT_MENU(OPJFRAME_SETSDECO, OPJFrame::OnSetsDeco)
    EVT_SASH_DRAGGED_RANGE(OPJFRAME_BROWSEWIN, OPJFRAME_LOGWIN, OPJFrame::OnSashDrag)
    EVT_NOTEBOOK_PAGE_CHANGED(LEFT_NOTEBOOK_ID, OPJFrame::OnNotebook)
    EVT_MENU(OPJFRAME_THREADLOGMSG, OPJFrame::OnThreadLogmsg)
END_EVENT_TABLE()

// this is the frame constructor
OPJFrame::OPJFrame(wxWindow *parent, const wxWindowID id, const wxString& title,
				   const wxPoint& pos, const wxSize& size, const long style)
		: wxMDIParentFrame(parent, id, title, pos, size, style)
{
	// file menu and its items
	wxMenu *file_menu = new wxMenu;

	file_menu->Append(OPJFRAME_FILEOPEN, wxT("&Open\tCtrl+O"));
	file_menu->SetHelpString(OPJFRAME_FILEOPEN, wxT("Open one or more files"));

	file_menu->Append(OPJFRAME_MEMORYOPEN, wxT("&Memory\tCtrl+M"));
	file_menu->SetHelpString(OPJFRAME_MEMORYOPEN, wxT("Open a memory buffer"));

	file_menu->Append(OPJFRAME_FILECLOSE, wxT("&Close\tCtrl+C"));
	file_menu->SetHelpString(OPJFRAME_FILECLOSE, wxT("Close current image"));

	file_menu->AppendSeparator();

	file_menu->Append(OPJFRAME_FILESAVEAS, wxT("&Save as\tCtrl+S"));
	file_menu->SetHelpString(OPJFRAME_FILESAVEAS, wxT("Save the current image"));
	//file_menu->Enable(OPJFRAME_FILESAVEAS, false);

	file_menu->AppendSeparator();

	file_menu->Append(OPJFRAME_FILETOGGLEB, wxT("Toggle &browser\tCtrl+B"));
	file_menu->SetHelpString(OPJFRAME_FILETOGGLEB, wxT("Toggle the left browsing pane"));

	file_menu->Append(OPJFRAME_FILETOGGLEP, wxT("Toggle &peeker\tCtrl+P"));
	file_menu->SetHelpString(OPJFRAME_FILETOGGLEP, wxT("Toggle the bottom peeking pane"));

	file_menu->Append(OPJFRAME_FILETOGGLET, wxT("Toggle &toolbar\tCtrl+T"));
	file_menu->SetHelpString(OPJFRAME_FILETOGGLET, wxT("Toggle the toolbar"));

	file_menu->AppendSeparator();

	file_menu->Append(OPJFRAME_FILEEXIT, wxT("&Exit\tCtrl+Q"));
	file_menu->SetHelpString(OPJFRAME_FILEEXIT, wxT("Quit this program"));

	// view menu and its items
	wxMenu *view_menu = new wxMenu;

	view_menu->Append(OPJFRAME_VIEWZOOM, wxT("&Zoom\tCtrl+Z"));
	view_menu->SetHelpString(OPJFRAME_VIEWZOOM, wxT("Rescale the image"));

	view_menu->Append(OPJFRAME_VIEWFIT, wxT("Zoom to &fit\tCtrl+F"));
	view_menu->SetHelpString(OPJFRAME_VIEWFIT, wxT("Fit the image in canvas"));

	view_menu->Append(OPJFRAME_VIEWRELOAD, wxT("&Reload image\tCtrl+R"));
	view_menu->SetHelpString(OPJFRAME_VIEWRELOAD, wxT("Reload the current image"));

	view_menu->AppendSeparator();

	view_menu->Append(OPJFRAME_VIEWPREVFRAME, wxT("&Prev frame\tLeft"));
	view_menu->SetHelpString(OPJFRAME_VIEWPREVFRAME, wxT("View previous frame"));

	view_menu->Append(OPJFRAME_VIEWHOMEFRAME, wxT("&Start frame\tHome"));
	view_menu->SetHelpString(OPJFRAME_VIEWHOMEFRAME, wxT("View starting frame"));

	view_menu->Append(OPJFRAME_VIEWNEXTFRAME, wxT("&Next frame\tRight"));
	view_menu->SetHelpString(OPJFRAME_VIEWNEXTFRAME, wxT("View next frame"));

	view_menu->AppendSeparator();

	view_menu->Append(OPJFRAME_VIEWLESSLAYERS, wxT("&Less layers\t-"));
	view_menu->SetHelpString(OPJFRAME_VIEWLESSLAYERS, wxT("Remove a layer"));

	view_menu->Append(OPJFRAME_VIEWALLLAYERS, wxT("&All layers\t0"));
	view_menu->SetHelpString(OPJFRAME_VIEWALLLAYERS, wxT("Show all layers"));

	view_menu->Append(OPJFRAME_VIEWMORELAYERS, wxT("&More layers\t+"));
	view_menu->SetHelpString(OPJFRAME_VIEWMORELAYERS, wxT("Add a layer"));

	view_menu->AppendSeparator();

	view_menu->Append(OPJFRAME_VIEWLESSRES, wxT("&Less resolution\t<"));
	view_menu->SetHelpString(OPJFRAME_VIEWLESSRES, wxT("Reduce the resolution"));

	view_menu->Append(OPJFRAME_VIEWFULLRES, wxT("&Full resolution\tf"));
	view_menu->SetHelpString(OPJFRAME_VIEWFULLRES, wxT("Full resolution"));

	view_menu->Append(OPJFRAME_VIEWMORERES, wxT("&More resolution\t>"));
	view_menu->SetHelpString(OPJFRAME_VIEWMORERES, wxT("Increase the resolution"));

	view_menu->AppendSeparator();

	view_menu->Append(OPJFRAME_VIEWPREVCOMP, wxT("&Prev component\tDown"));
	view_menu->SetHelpString(OPJFRAME_VIEWPREVCOMP, wxT("View previous component"));

	view_menu->Append(OPJFRAME_VIEWALLCOMPS, wxT("&All components\ta"));
	view_menu->SetHelpString(OPJFRAME_VIEWALLCOMPS, wxT("View all components"));

	view_menu->Append(OPJFRAME_VIEWNEXTCOMP, wxT("&Next component\tUp"));
	view_menu->SetHelpString(OPJFRAME_VIEWNEXTCOMP, wxT("View next component"));


	// settings menu and its items
	wxMenu *sets_menu = new wxMenu;

	sets_menu->Append(OPJFRAME_SETSENCO, wxT("&Encoder\tCtrl+E"));
	sets_menu->SetHelpString(OPJFRAME_SETSENCO, wxT("Encoder settings"));

	sets_menu->Append(OPJFRAME_SETSDECO, wxT("&Decoder\tCtrl+D"));
	sets_menu->SetHelpString(OPJFRAME_SETSDECO, wxT("Decoder settings"));

	// help menu and its items
	wxMenu *help_menu = new wxMenu;

	help_menu->Append(OPJFRAME_HELPABOUT, wxT("&About\tF1"));
	help_menu->SetHelpString(OPJFRAME_HELPABOUT, wxT("Basic info on the program"));

	// the whole menubar
	wxMenuBar *menu_bar = new wxMenuBar;
	menu_bar->Append(file_menu, wxT("&File"));
	menu_bar->Append(view_menu, wxT("&View"));
	menu_bar->Append(sets_menu, wxT("&Settings"));
	menu_bar->Append(help_menu, wxT("&Help"));

	// Associate the menu bar with the frame
	SetMenuBar(menu_bar);

	// the status bar
	CreateStatusBar();

	// the toolbar
	tool_bar = new wxToolBar(this, OPJFRAME_TOOLBAR,
								wxDefaultPosition, wxDefaultSize,
								wxTB_HORIZONTAL | wxNO_BORDER);
	wxBitmap bmpOpen = wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpSaveAs = wxArtProvider::GetBitmap(wxART_FILE_SAVE_AS, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpZoom = wxArtProvider::GetBitmap(wxART_FIND, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpFit = wxArtProvider::GetBitmap(wxART_FIND_AND_REPLACE, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpReload = wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpDecosettings = wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpEncosettings = wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpPrevframe = wxArtProvider::GetBitmap(wxART_GO_BACK, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpHomeframe = wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpNextframe = wxArtProvider::GetBitmap(wxART_GO_FORWARD, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpLesslayers = bmpPrevframe;
	wxBitmap bmpAlllayers = wxArtProvider::GetBitmap(wxART_GO_TO_PARENT, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpMorelayers = bmpNextframe;
	wxBitmap bmpLessres = bmpPrevframe;
	wxBitmap bmpFullres = wxArtProvider::GetBitmap(wxART_GO_TO_PARENT, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpMoreres = bmpNextframe;
	wxBitmap bmpPrevcomp = bmpPrevframe;
	wxBitmap bmpAllcomps = wxArtProvider::GetBitmap(wxART_GO_TO_PARENT, wxART_TOOLBAR,
												wxDefaultSize);
	wxBitmap bmpNextcomp = bmpNextframe;

	tool_bar->AddTool(OPJFRAME_FILEOPEN, bmpOpen, wxT("Open"));
	tool_bar->AddTool(OPJFRAME_FILESAVEAS, bmpSaveAs, wxT("Save as "));
	//tool_bar->EnableTool(OPJFRAME_FILESAVEAS, false);
	tool_bar->AddSeparator();
	tool_bar->AddTool(OPJFRAME_VIEWZOOM, bmpZoom, wxT("Zoom"));
	tool_bar->AddTool(OPJFRAME_VIEWFIT, bmpFit, wxT("Zoom to fit"));
	tool_bar->AddTool(OPJFRAME_VIEWRELOAD, bmpReload, wxT("Reload"));
	tool_bar->AddSeparator();
	tool_bar->AddTool(OPJFRAME_SETSDECO, bmpDecosettings, wxT("Decoder settings"));
	tool_bar->AddTool(OPJFRAME_SETSENCO, bmpEncosettings, wxT("Encoder settings"));
	tool_bar->AddSeparator();
	tool_bar->AddTool(OPJFRAME_VIEWPREVFRAME, bmpPrevframe, wxT("Previous frame"));
	tool_bar->AddTool(OPJFRAME_VIEWHOMEFRAME, bmpHomeframe, wxT("Starting frame"));
	tool_bar->AddTool(OPJFRAME_VIEWNEXTFRAME, bmpNextframe, wxT("Next frame"));
	tool_bar->AddSeparator();
	tool_bar->AddTool(OPJFRAME_VIEWLESSLAYERS, bmpLesslayers, wxT("Remove a layer"));
	tool_bar->AddTool(OPJFRAME_VIEWALLLAYERS, bmpAlllayers, wxT("Show all layers"));
	tool_bar->AddTool(OPJFRAME_VIEWMORELAYERS, bmpMorelayers, wxT("Add a layer"));
	tool_bar->AddSeparator();
	tool_bar->AddTool(OPJFRAME_VIEWLESSRES, bmpLessres, wxT("Reduce the resolution"));
	tool_bar->AddTool(OPJFRAME_VIEWFULLRES, bmpFullres, wxT("Full resolution"));
	tool_bar->AddTool(OPJFRAME_VIEWMORERES, bmpMoreres, wxT("Increase the resolution"));
	tool_bar->AddSeparator();
	tool_bar->AddTool(OPJFRAME_VIEWPREVCOMP, bmpPrevcomp, wxT("Previous component"));
	tool_bar->AddTool(OPJFRAME_VIEWALLCOMPS, bmpAllcomps, wxT("All components"));
	tool_bar->AddTool(OPJFRAME_VIEWNEXTCOMP, bmpNextcomp, wxT("Next component"));
	tool_bar->Realize();
	
	// associate the toolbar with the frame
	SetToolBar(tool_bar);

	// show the toolbar?
	if (!wxGetApp().m_showtoolbar)
		tool_bar->Show(false);
	else
		tool_bar->Show(true);

	// the logging window
	loggingWindow = new wxSashLayoutWindow(this, OPJFRAME_LOGWIN,
											wxDefaultPosition, wxSize(400, wxGetApp().m_peekerheight),
											wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN
											);
	loggingWindow->SetDefaultSize(wxSize(1000, wxGetApp().m_peekerheight));
	loggingWindow->SetOrientation(wxLAYOUT_HORIZONTAL);
	loggingWindow->SetAlignment(wxLAYOUT_BOTTOM);
	//loggingWindow->SetBackgroundColour(wxColour(0, 0, 255));
	loggingWindow->SetSashVisible(wxSASH_TOP, true);

	// show the logging?
	if (!wxGetApp().m_showpeeker)
		loggingWindow->Show(false);
	else
		loggingWindow->Show(true);

	// create the bottom notebook
	m_bookCtrlbottom = new wxNotebook(loggingWindow, BOTTOM_NOTEBOOK_ID,
								wxDefaultPosition, wxDefaultSize,
								wxBK_LEFT);

	// create the text control of the logger
	m_textCtrl = new wxTextCtrl(m_bookCtrlbottom, wxID_ANY, wxT(""),
								wxDefaultPosition, wxDefaultSize,
								wxTE_MULTILINE | wxSUNKEN_BORDER | wxTE_READONLY
								);
	m_textCtrl->SetValue(_T("Logging window\n"));

	// add it to the notebook
	m_bookCtrlbottom->AddPage(m_textCtrl, wxT("Log"));

	// create the text control of the browser
	m_textCtrlbrowse = new wxTextCtrl(m_bookCtrlbottom, wxID_ANY, wxT(""),
								wxDefaultPosition, wxDefaultSize,
								wxTE_MULTILINE | wxSUNKEN_BORDER | wxTE_READONLY | wxTE_RICH
								);
	wxFont *browsefont = new wxFont(wxNORMAL_FONT->GetPointSize(),
		wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
    m_textCtrlbrowse->SetDefaultStyle(wxTextAttr(wxNullColour, wxNullColour, *browsefont));
	m_textCtrlbrowse->AppendText(wxT("Browsing window\n"));

	// add it the notebook
	m_bookCtrlbottom->AddPage(m_textCtrlbrowse, wxT("Peek"), false);

	// the browser window
	markerTreeWindow = new wxSashLayoutWindow(this, OPJFRAME_BROWSEWIN,
											  wxDefaultPosition, wxSize(wxGetApp().m_browserwidth, 30),
											  wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN
											  );
	markerTreeWindow->SetDefaultSize(wxSize(wxGetApp().m_browserwidth, 1000));
	markerTreeWindow->SetOrientation(wxLAYOUT_VERTICAL);
	markerTreeWindow->SetAlignment(wxLAYOUT_LEFT);
	//markerTreeWindow->SetBackgroundColour(wxColour(0, 255, 0));
	markerTreeWindow->SetSashVisible(wxSASH_RIGHT, true);
	markerTreeWindow->SetExtraBorderSize(0);

	// create the browser notebook
	m_bookCtrl = new wxNotebook(markerTreeWindow, LEFT_NOTEBOOK_ID,
								wxDefaultPosition, wxDefaultSize,
								wxBK_TOP);

	// show the browser?
	if (!wxGetApp().m_showbrowser)
		markerTreeWindow->Show(false);
	else
		markerTreeWindow->Show(true);

#ifdef __WXMOTIF__
	// For some reason, we get a memcpy crash in wxLogStream::DoLogStream
	// on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
	delete wxLog::SetActiveTarget(new wxLogStderr);
#else
	// set our text control as the log target
	wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
	delete wxLog::SetActiveTarget(logWindow);
#endif

	// associate drop targets with the controls
	SetDropTarget(new OPJDnDFile(this));

}

// this is the frame destructor
OPJFrame::~OPJFrame(void)
{
	// save size settings
	GetSize(&(wxGetApp().m_framewidth), &(wxGetApp().m_frameheight));

	// delete all possible things
	delete m_bookCtrl;
	m_bookCtrl = NULL;

	delete markerTreeWindow;
	markerTreeWindow = NULL;

	delete m_textCtrl;
	m_textCtrl = NULL;

	delete m_bookCtrlbottom;
	m_bookCtrlbottom = NULL;

	delete loggingWindow;
	loggingWindow = NULL;
}

void OPJFrame::OnNotebook(wxNotebookEvent& event)
{
	int sel = event.GetSelection();
	long childnum;

	m_bookCtrl->GetPageText(sel).ToLong(&childnum);

	if (m_childhash[childnum])
		m_childhash[childnum]->Activate();

	//wxLogMessage(wxT("Selection changed (now %d --> %d)"), childnum, m_childhash[childnum]->m_winnumber);

}


void OPJFrame::Resize(int number)
{
	wxSize size = GetClientSize();
}

void OPJFrame::OnSetsEnco(wxCommandEvent& event)
{
	int n;

    OPJEncoderDialog dialog(this, event.GetId());

    if (dialog.ShowModal() == wxID_OK) {

		// load settings
		wxGetApp().m_subsampling = dialog.m_subsamplingCtrl->GetValue();
		wxGetApp().m_origin = dialog.m_originCtrl->GetValue();
		wxGetApp().m_rates = dialog.m_rateCtrl->GetValue();
		wxGetApp().m_quality = dialog.m_qualityCtrl->GetValue();
		wxGetApp().m_enablequality = dialog.m_qualityRadio->GetValue();
		wxGetApp().m_multicomp = dialog.m_mctCheck->GetValue();
		wxGetApp().m_irreversible = dialog.m_irrevCheck->GetValue();
		wxGetApp().m_resolutions = dialog.m_resolutionsCtrl->GetValue();
		wxGetApp().m_cbsize = dialog.m_cbsizeCtrl->GetValue();
		wxGetApp().m_prsize = dialog.m_prsizeCtrl->GetValue();
		wxGetApp().m_tsize = dialog.m_tsizeCtrl->GetValue();
		wxGetApp().m_torigin = dialog.m_toriginCtrl->GetValue();
		wxGetApp().m_progression = dialog.progressionBox->GetSelection();
		wxGetApp().m_enablesop = dialog.m_sopCheck->GetValue();
		wxGetApp().m_enableeph = dialog.m_ephCheck->GetValue();
		wxGetApp().m_enablebypass = dialog.m_enablebypassCheck->GetValue();
		wxGetApp().m_enablereset = dialog.m_enableresetCheck->GetValue();
		wxGetApp().m_enablerestart = dialog.m_enablerestartCheck->GetValue();
		wxGetApp().m_enablevsc = dialog.m_enablevscCheck->GetValue();
		wxGetApp().m_enableerterm = dialog.m_enableertermCheck->GetValue();
		wxGetApp().m_enablesegmark = dialog.m_enablesegmarkCheck->GetValue();
		wxGetApp().m_enableidx = dialog.m_enableidxCheck->GetValue();
		wxGetApp().m_index = dialog.m_indexCtrl->GetValue();
		wxGetApp().m_enablecomm = dialog.m_enablecommCheck->GetValue();
		wxGetApp().m_comment = dialog.m_commentCtrl->GetValue();
		wxGetApp().m_enablepoc = dialog.m_enablepocCheck->GetValue();
		wxGetApp().m_poc = dialog.m_pocCtrl->GetValue();
#ifdef USE_JPWL
		wxGetApp().m_enablejpwle = dialog.m_enablejpwlCheck->GetValue();
		for (n = 0; n < MYJPWL_MAX_NO_TILESPECS; n++) {
			wxGetApp().m_hprotsel[n] = dialog.m_hprotChoice[n]->GetSelection();
			wxGetApp().m_htileval[n] = dialog.m_htileCtrl[n]->GetValue();
			wxGetApp().m_pprotsel[n] = dialog.m_pprotChoice[n]->GetSelection();
			wxGetApp().m_ptileval[n] = dialog.m_ptileCtrl[n]->GetValue();
			wxGetApp().m_ppackval[n] = dialog.m_ppackCtrl[n]->GetValue();
			wxGetApp().m_sensisel[n] = dialog.m_sensiChoice[n]->GetSelection();
			wxGetApp().m_stileval[n] = dialog.m_stileCtrl[n]->GetValue();
		}
#endif // USE_JPWL
	};
}

void OPJFrame::OnSetsDeco(wxCommandEvent& event)
{
    OPJDecoderDialog dialog(this, event.GetId());

    if (dialog.ShowModal() == wxID_OK) {

		// load settings
		wxGetApp().m_enabledeco = dialog.m_enabledecoCheck->GetValue();
		wxGetApp().m_enableparse = dialog.m_enableparseCheck->GetValue();
		wxGetApp().m_resizemethod = dialog.m_resizeBox->GetSelection() - 1;
		wxGetApp().m_reducefactor = dialog.m_reduceCtrl->GetValue();
		wxGetApp().m_qualitylayers = dialog.m_layerCtrl->GetValue();
		wxGetApp().m_components = dialog.m_numcompsCtrl->GetValue();
		wxGetApp().m_framenum = dialog.m_framenumCtrl->GetValue();
#ifdef USE_JPWL
		wxGetApp().m_enablejpwl = dialog.m_enablejpwlCheck->GetValue();
		wxGetApp().m_expcomps = dialog.m_expcompsCtrl->GetValue();
		wxGetApp().m_maxtiles = dialog.m_maxtilesCtrl->GetValue();
#endif // USE_JPWL

	};
}

void OPJFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	Close(true);
}

void OPJFrame::OnClose(wxCommandEvent& WXUNUSED(event))
{
	// current frame
	OPJChildFrame *currframe = (OPJChildFrame *) GetActiveChild();

	if (!currframe)
		return;

	wxCloseEvent e;
	currframe->OnClose(e);
}

void OPJFrame::OnFit(wxCommandEvent& event)
{
	OPJChildFrame *currchild;
	wxString eventstring = event.GetString();

	//wxLogMessage(wxT("OnFit:%d:%s"), event.GetInt(), eventstring);

	// current child
	if (event.GetInt() >= 1) {
		currchild = m_childhash[event.GetInt()];
	} else {
		currchild = (OPJChildFrame *) GetActiveChild();
	}

	// problems
	if (!currchild)
		return;

	// current canvas
	OPJCanvas *currcanvas = currchild->m_canvas;

	// find a fit-to-width zoom
	/*int zooml, wzooml, hzooml;
	wxSize clientsize = currcanvas->GetClientSize();
	wzooml = (int) ceil(100.0 * (double) (clientsize.GetWidth() - 2 * OPJ_CANVAS_BORDER) / (double) (currcanvas->m_image100.GetWidth()));
	hzooml = (int) ceil(100.0 * (double) (clientsize.GetHeight() - 2 * OPJ_CANVAS_BORDER) / (double) (currcanvas->m_image100.GetHeight()));
	zooml = wxMin(100, wxMin(wzooml, hzooml));*/

	// fit to width
	Rescale(-1, currchild);
}

void OPJFrame::OnZoom(wxCommandEvent& WXUNUSED(event))
{
	// current frame
	OPJChildFrame *currframe = (OPJChildFrame *) GetActiveChild();

	if (!currframe)
		return;

	// get the preferred zoom
	long zooml = wxGetNumberFromUser(wxT("Choose a scale between 5% and 300%"),
		wxT("Zoom (%)"),
		wxT("Image scale"),
		currframe->m_canvas->m_zooml, 5, 300, NULL, wxDefaultPosition);

	// rescale current frame image if necessary
	if (zooml >= 5) {
		Rescale(zooml, currframe);
		wxLogMessage(wxT("zoom to %d%%"), zooml);
	}
}

void OPJFrame::Rescale(int zooml, OPJChildFrame *currframe)
{
	wxImage new_image = currframe->m_canvas->m_image100.ConvertToImage();

	// resizing enabled?
	if (wxGetApp().m_resizemethod == -1) {

		zooml = 100;

	} else {

		if (zooml < 0) {
			// find a fit-to-width zoom
			int wzooml, hzooml;
			//wxSize clientsize = currframe->m_canvas->GetClientSize();
			wxSize clientsize = currframe->m_frame->GetActiveChild()->GetClientSize();
			wzooml = (int) floor(100.0 * (double) clientsize.GetWidth() / (double) (2 * OPJ_CANVAS_BORDER + currframe->m_canvas->m_image100.GetWidth()));
			hzooml = (int) floor(100.0 * (double) clientsize.GetHeight() / (double) (2 * OPJ_CANVAS_BORDER + currframe->m_canvas->m_image100.GetHeight()));
			zooml = wxMin(100, wxMin(wzooml, hzooml));
		}
	}

	if (zooml != 100)
		new_image.Rescale((int) ((double) zooml * (double) new_image.GetWidth() / 100.0),
			(int) ((double) zooml * (double) new_image.GetHeight() / 100.0),
			wxGetApp().m_resizemethod ? wxIMAGE_QUALITY_HIGH : wxIMAGE_QUALITY_NORMAL);
	currframe->m_canvas->m_image = wxBitmap(new_image);
	currframe->m_canvas->SetScrollbars(20,
										20,
										(int)(0.5 + (double) new_image.GetWidth() / 20.0),
										(int)(0.5 + (double) new_image.GetHeight() / 20.0)
										);

	currframe->m_canvas->Refresh();

	wxLogMessage(wxT("Rescale said %d%%"), zooml);

	// update zoom
	currframe->m_canvas->m_zooml = zooml;
}


void OPJFrame::OnReload(wxCommandEvent& event)
{
	OPJChildFrame *currframe = (OPJChildFrame *) GetActiveChild();

	if (currframe) {
		OPJDecoThread *dthread = currframe->m_canvas->CreateDecoThread();

		if (dthread->Run() != wxTHREAD_NO_ERROR)
			wxLogMessage(wxT("Can't start deco thread!"));
		else
			wxLogMessage(wxT("New deco thread started."));

		currframe->m_canvas->Refresh();

		// update zoom
		//currframe->m_canvas->m_zooml = zooml;
	}
}

void OPJFrame::OnPrevFrame(wxCommandEvent& event)
{
	if (--wxGetApp().m_framenum < 0)
		wxGetApp().m_framenum = 0;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnHomeFrame(wxCommandEvent& event)
{
	wxGetApp().m_framenum = 0;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnNextFrame(wxCommandEvent& event)
{
	++wxGetApp().m_framenum;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnLessLayers(wxCommandEvent& event)
{
	if (--wxGetApp().m_qualitylayers < 1)
		wxGetApp().m_qualitylayers = 1;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnAllLayers(wxCommandEvent& event)
{
	wxGetApp().m_qualitylayers = 0;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnMoreLayers(wxCommandEvent& event)
{
	++wxGetApp().m_qualitylayers;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnLessRes(wxCommandEvent& event)
{
	++wxGetApp().m_reducefactor;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnFullRes(wxCommandEvent& event)
{
	wxGetApp().m_reducefactor = 0;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnMoreRes(wxCommandEvent& event)
{
	if (--wxGetApp().m_reducefactor < 0)
		wxGetApp().m_reducefactor = 0;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnPrevComp(wxCommandEvent& event)
{
	if (--wxGetApp().m_components < 1)
		wxGetApp().m_components = 1;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnAllComps(wxCommandEvent& event)
{
	wxGetApp().m_components = 0;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnNextComp(wxCommandEvent& event)
{
	++wxGetApp().m_components;

	wxCommandEvent e;
	OnReload(e);
}

void OPJFrame::OnToggleBrowser(wxCommandEvent& WXUNUSED(event))
{
    if (markerTreeWindow->IsShown())
        markerTreeWindow->Show(false);
    else
        markerTreeWindow->Show(true);

    wxLayoutAlgorithm layout;
    layout.LayoutMDIFrame(this);

	wxGetApp().m_showbrowser = markerTreeWindow->IsShown();

    // Leaves bits of itself behind sometimes
    GetClientWindow()->Refresh();
}

void OPJFrame::OnTogglePeeker(wxCommandEvent& WXUNUSED(event))
{
    if (loggingWindow->IsShown())
        loggingWindow->Show(false);
    else
        loggingWindow->Show(true);

    wxLayoutAlgorithm layout;
    layout.LayoutMDIFrame(this);

	wxGetApp().m_showpeeker = loggingWindow->IsShown();

    // Leaves bits of itself behind sometimes
    GetClientWindow()->Refresh();
}

void OPJFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
{
    if (tool_bar->IsShown())
        tool_bar->Show(false);
    else
        tool_bar->Show(true);

    wxLayoutAlgorithm layout;
    layout.LayoutMDIFrame(this);

	wxGetApp().m_showtoolbar = tool_bar->IsShown();

    // Leaves bits of itself behind sometimes
    GetClientWindow()->Refresh();
}

void OPJFrame::OnSashDrag(wxSashEvent& event)
{
	int wid, hei;

    if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE)
        return;

    switch (event.GetId()) {
		case OPJFRAME_BROWSEWIN:
		{
			markerTreeWindow->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
			break;
		}
		case OPJFRAME_LOGWIN:
		{
			loggingWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
			break;
		}
    }

    wxLayoutAlgorithm layout;
    layout.LayoutMDIFrame(this);

    // Leaves bits of itself behind sometimes
    GetClientWindow()->Refresh();

	// update dimensions
	markerTreeWindow->GetSize(&wid, &hei);
	wxGetApp().m_browserwidth = wid;

	loggingWindow->GetSize(&wid, &hei);
	wxGetApp().m_peekerheight = hei;

}

void OPJFrame::OnThreadLogmsg(wxCommandEvent& event)
{
#if 1
    wxLogMessage(wxT("Frame got message from worker thread: %d"), event.GetInt());
    wxLogMessage(event.GetString());
#else
    int n = event.GetInt();
    if ( n == -1 )
    {
        m_dlgProgress->Destroy();
        m_dlgProgress = (wxProgressDialog *)NULL;

        // the dialog is aborted because the event came from another thread, so
        // we may need to wake up the main event loop for the dialog to be
        // really closed
        wxWakeUpIdle();
    }
    else
    {
        if ( !m_dlgProgress->Update(n) )
        {
            wxCriticalSectionLocker lock(m_critsectWork);

            m_cancelled = true;
        }
    }
#endif
}


// physically save the file
void OPJFrame::SaveFile(wxArrayString paths, wxArrayString filenames)
{
	size_t count = paths.GetCount();
	wxString msg, s;

	if (wxFile::Exists(paths[0].c_str())) {

		s.Printf(wxT("File %s already exists. Do you want to overwrite it?\n"), filenames[0].c_str());
		wxMessageDialog dialog3(this, s, _T("File exists"), wxYES_NO);
		if (dialog3.ShowModal() == wxID_NO)
			return;
	}

	/*s.Printf(_T("File %d: %s (%s)\n"), (int)0, paths[0].c_str(), filenames[0].c_str());
	msg += s;

	wxMessageDialog dialog2(this, msg, _T("Selected files"));
	dialog2.ShowModal();*/

	if (!GetActiveChild())
		return;

	((OPJChildFrame *) GetActiveChild())->m_canvas->m_savename = paths[0];

	OPJEncoThread *ethread = ((OPJChildFrame *) GetActiveChild())->m_canvas->CreateEncoThread();

    if (ethread->Run() != wxTHREAD_NO_ERROR)
        wxLogMessage(wxT("Can't start enco thread!"));
    else
		wxLogMessage(wxT("New enco thread started."));


}

// physically open the files
void OPJFrame::OpenFiles(wxArrayString paths, wxArrayString filenames)
{

	size_t count = paths.GetCount();
	for (size_t n = 0; n < count; n++) {

		wxString msg, s;
		s.Printf(_T("File %d: %s (%s)\n"), (int)n, paths[n].c_str(), filenames[n].c_str());

		msg += s;

		/*wxMessageDialog dialog2(this, msg, _T("Selected files"));
		dialog2.ShowModal();*/

		// Make another frame, containing a canvas
		OPJChildFrame *subframe = new OPJChildFrame(this,
													paths[n],
													winNumber,
													wxT("Canvas Frame"),
													wxDefaultPosition, wxSize(300, 300),
													wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE
													);
		m_childhash[winNumber] = subframe;

		// create own marker tree
		m_treehash[winNumber] = new OPJMarkerTree(m_bookCtrl, subframe, paths[n], wxT("Parsing..."), TreeTest_Ctrl,
												  wxDefaultPosition, wxDefaultSize,
												  wxTR_DEFAULT_STYLE | wxSUNKEN_BORDER
												  );

		m_bookCtrl->AddPage(m_treehash[winNumber], wxString::Format(wxT("%u"), winNumber), false);

		for (unsigned int p = 0; p < m_bookCtrl->GetPageCount(); p++) {
			if (m_bookCtrl->GetPageText(p) == wxString::Format(wxT("%u"), winNumber)) {
				m_bookCtrl->ChangeSelection(p);
				break;
			}
		}

		winNumber++;
	}
}

void OPJFrame::OnFileOpen(wxCommandEvent& WXUNUSED(event))
{
    wxString wildcards =
#ifdef __WXMOTIF__
	wxT("JPEG 2000 files (*.jp2,*.j2k,*.j2c,*.mj2)|*.*j*2*");
#else
#if wxUSE_LIBOPENJPEG
	wxT("JPEG 2000 files (*.jp2,*.j2k,*.j2c,*.mj2)|*.jp2;*.j2k;*.j2c;*.mj2")
#endif
#if USE_MXF
	wxT("|MXF JPEG 2000 video (*.mxf)|*.mxf")
#endif // USE_MXF
#if wxUSE_LIBJPEG
		wxT("|JPEG files (*.jpg)|*.jpg")
#endif
#if OPJ_MANYFORMATS
		wxT("|BMP files (*.bmp)|*.bmp")
		wxT("|PNG files (*.png)|*.png")
		wxT("|GIF files (*.gif)|*.gif")
		wxT("|PNM files (*.pnm)|*.pnm")
		wxT("|TIFF files (*.tif,*.tiff)|*.tif*")
#endif
		wxT("|All files|*");
#endif
    wxFileDialog dialog(this, _T("Open image file(s)"),
                        wxEmptyString, wxEmptyString, wildcards,
                        wxFD_OPEN|wxFD_MULTIPLE);

    if (dialog.ShowModal() == wxID_OK) {
        wxArrayString paths, filenames;

        dialog.GetPaths(paths);
        dialog.GetFilenames(filenames);

		OpenFiles(paths, filenames);
    }

}

void OPJFrame::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
{
    wxString wildcards =
#ifdef wxUSE_LIBOPENJPEG
#ifdef __WXMOTIF__
	wxT("JPEG 2000 codestream (*.j2k)|*.*j*2*");
#else
	wxT("JPEG 2000 codestream (*.j2k)|*.j2k")
	wxT("|JPEG 2000 file format (*.jp2)|*.jp2");
#endif
#else
	wxT("Houston we have a problem");
#endif

    wxFileDialog dialog(this, _T("Save image file"),
                        wxEmptyString, wxEmptyString, wildcards,
                        wxFD_SAVE);

    if (dialog.ShowModal() == wxID_OK) {
        wxArrayString paths, filenames;

        dialog.GetPaths(paths);
        dialog.GetFilenames(filenames);

		SaveFile(paths, filenames);
    }


}

void OPJFrame::OnMemoryOpen(wxCommandEvent& WXUNUSED(event))
{
	// do nothing
	return;
	
	wxTextEntryDialog dialog(this, wxT("Memory HEX address range: start_address-stop_address"),
							wxT("Decode a memory buffer"),
							wxT("0x-0x"),
							wxOK | wxCANCEL | wxCENTRE,
							wxDefaultPosition);

	if (dialog.ShowModal() == wxID_OK) {

	}

}

BEGIN_EVENT_TABLE(OPJCanvas, wxScrolledWindow)
    EVT_MOUSE_EVENTS(OPJCanvas::OnEvent)
    EVT_MENU(OPJCANVAS_THREADSIGNAL, OPJCanvas::OnThreadSignal)
END_EVENT_TABLE()

// Define a constructor for my canvas
OPJCanvas::OPJCanvas(wxFileName fname, wxWindow *parent, const wxPoint& pos, const wxSize& size)
        : wxScrolledWindow(parent, wxID_ANY, pos, size,
                           wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
{
    SetBackgroundColour(OPJ_CANVAS_COLOUR);

	m_fname = fname;
	m_childframe = (OPJChildFrame *) parent;
	// 100% zoom
	m_zooml = 100;


    OPJDecoThread *dthread = CreateDecoThread();

    if (dthread->Run() != wxTHREAD_NO_ERROR)
        wxLogMessage(wxT("Can't start deco thread!"));
    else
		wxLogMessage(wxT("New deco thread started."));

	// 100% zoom
	//m_zooml = 100;

}

OPJDecoThread *OPJCanvas::CreateDecoThread(void)
{
    OPJDecoThread *dthread = new OPJDecoThread(this);

    if (dthread->Create() != wxTHREAD_NO_ERROR)
		wxLogError(wxT("Can't create deco thread!"));

    wxCriticalSectionLocker enter(wxGetApp().m_deco_critsect);
    wxGetApp().m_deco_threads.Add(dthread);

    return dthread;
}

OPJEncoThread *OPJCanvas::CreateEncoThread(void)
{
    OPJEncoThread *ethread = new OPJEncoThread(this);

    if (ethread->Create() != wxTHREAD_NO_ERROR)
		wxLogError(wxT("Can't create enco thread!"));

    wxCriticalSectionLocker enter(wxGetApp().m_enco_critsect);
    wxGetApp().m_enco_threads.Add(ethread);

    return ethread;
}

#define activeoverlay 0
// Define the repainting behaviour
void OPJCanvas::OnDraw(wxDC& dc)
{
	if (m_image.Ok()) {
		dc.DrawBitmap(m_image, OPJ_CANVAS_BORDER, OPJ_CANVAS_BORDER);

		if (activeoverlay) {
			dc.SetPen(*wxRED_PEN);
			dc.SetBrush(*wxTRANSPARENT_BRUSH);
			//int tw, th;
			dc.DrawRectangle(OPJ_CANVAS_BORDER, OPJ_CANVAS_BORDER,
				(unsigned long int) (0.5 + (double) m_zooml * (double) m_childframe->m_twidth / 100.0),
				(unsigned long int) (0.5 + (double) m_zooml * (double) m_childframe->m_theight / 100.0));
		}

	} else {
		dc.SetFont(*wxSWISS_FONT);
		dc.SetPen(*wxBLACK_PEN);
#ifdef __WXGTK__
		dc.DrawText(_T("Decoding image, please wait... (press \"Zoom to Fit\" to show the image)"), 40, 50);
#else
		dc.DrawText(_T("Decoding image, please wait..."), 40, 50);
#endif
	}
}

// This implements a tiny doodling program! Drag the mouse using
// the left button.
void OPJCanvas::OnEvent(wxMouseEvent& event)
{
#if USE_PENCIL_ON_CANVAS
  wxClientDC dc(this);
  PrepareDC(dc);

  wxPoint pt(event.GetLogicalPosition(dc));

  if ((xpos > -1) && (ypos > -1) && event.Dragging()) {
    dc.SetPen(*wxRED_PEN);
    dc.DrawLine(xpos, ypos, pt.x, pt.y);
  }
  xpos = pt.x;
  ypos = pt.y;
#endif
}

void OPJFrame::OnSize(wxSizeEvent& WXUNUSED(event))
{
    wxLayoutAlgorithm layout;
    layout.LayoutMDIFrame(this);
}

void OPJCanvas::OnThreadSignal(wxCommandEvent& event)
{
#if 1
    wxLogMessage(wxT("Canvas got signal from deco thread: %d"), event.GetInt());
    wxLogMessage(event.GetString());
#else
    int n = event.GetInt();
    if ( n == -1 )
    {
        m_dlgProgress->Destroy();
        m_dlgProgress = (wxProgressDialog *)NULL;

        // the dialog is aborted because the event came from another thread, so
        // we may need to wake up the main event loop for the dialog to be
        // really closed
        wxWakeUpIdle();
    }
    else
    {
        if ( !m_dlgProgress->Update(n) )
        {
            wxCriticalSectionLocker lock(m_critsectWork);

            m_cancelled = true;
        }
    }
#endif
}


// Note that OPJFRAME_FILEOPEN and OPJFRAME_HELPABOUT commands get passed
// to the parent window for processing, so no need to
// duplicate event handlers here.

BEGIN_EVENT_TABLE(OPJChildFrame, wxMDIChildFrame)
  /*EVT_MENU(SASHTEST_CHILD_QUIT, OPJChildFrame::OnQuit)*/
  EVT_CLOSE(OPJChildFrame::OnClose)
  EVT_SET_FOCUS(OPJChildFrame::OnGotFocus)
  EVT_KILL_FOCUS(OPJChildFrame::OnLostFocus)
END_EVENT_TABLE()

OPJChildFrame::OPJChildFrame(OPJFrame *parent, wxFileName fname, int winnumber, const wxString& title, const wxPoint& pos, const wxSize& size,
const long style):
  wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style)
{
	m_frame = (OPJFrame  *) parent;
	m_canvas = NULL;
	//my_children.Append(this);
	m_fname = fname;
	m_winnumber = winnumber;
	SetTitle(wxString::Format(_T("%d: "), m_winnumber) + m_fname.GetFullName());

	  // Give it an icon (this is ignored in MDI mode: uses resources)
#ifdef __WXMSW__
	SetIcon(wxIcon(wxT("OPJChild16")));
#endif

	// Give it a status line
	/*CreateStatusBar();*/

	int width, height;
	GetClientSize(&width, &height);

	OPJCanvas *canvas = new OPJCanvas(fname, this, wxPoint(0, 0), wxSize(width, height));
#if USE_PENCIL_ON_CANVAS
	canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
#endif
	m_canvas = canvas;

	// Give it scrollbars
	canvas->SetScrollbars(20, 20, 5, 5);

	Show(true);
	Maximize(true);

	/*wxLogError(wxString::Format(wxT("Created tree %d (0x%x)"), m_winnumber, m_frame->m_treehash[m_winnumber]));*/

}

OPJChildFrame::~OPJChildFrame(void)
{
  //my_children.DeleteObject(this);
}


void OPJChildFrame::OnClose(wxCloseEvent& event)
{
	for (unsigned int p = 0; p < m_frame->m_bookCtrl->GetPageCount(); p++) {
		if (m_frame->m_bookCtrl->GetPageText(p) == wxString::Format(wxT("%u"), m_winnumber)) {
			m_frame->m_bookCtrl->DeletePage(p);
			break;
		}
	}
	Destroy();

	wxLogMessage(wxT("Closed: %d"), m_winnumber);
}

void OPJChildFrame::OnActivate(wxActivateEvent& event)
{
  /*if (event.GetActive() && m_canvas)
    m_canvas->SetFocus();*/
}

void OPJChildFrame::OnGotFocus(wxFocusEvent& event)
{
	// we need to check if the notebook is being destroyed or not
	if (!m_frame->m_bookCtrl)
		return;

	for (unsigned int p = 0; p < m_frame->m_bookCtrl->GetPageCount(); p++) {

		if (m_frame->m_bookCtrl->GetPageText(p) == wxString::Format(wxT("%u"), m_winnumber)) {
			m_frame->m_bookCtrl->ChangeSelection(p);
			break;
		}

	}

	//wxLogMessage(wxT("Got focus: %d (%x)"), m_winnumber, event.GetWindow());
}

void OPJChildFrame::OnLostFocus(wxFocusEvent& event)
{
	//wxLogMessage(wxT("Lost focus: %d (%x)"), m_winnumber, event.GetWindow());
}


////////////////////////////////
// drag and drop 
////////////////////////////////

bool OPJDnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames)
{
    /*size_t nFiles = filenames.GetCount();
    wxString str;
    str.Printf( _T("%d files dropped\n"), (int)nFiles);
    for ( size_t n = 0; n < nFiles; n++ ) {
        str << filenames[n] << wxT("\n");
    }
    wxLogMessage(str);*/
	m_pOwner->OpenFiles(filenames, filenames);

    return true;
}