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
|
//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
//
// LDmicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LDmicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LDmicro. If not, see <http://www.gnu.org/licenses/>.
//------
//
// A ladder logic compiler for 8 bit micros: user draws a ladder diagram,
// with an appropriately constrained `schematic editor,' and then we can
// simulated it under Windows or generate PIC/AVR code that performs the
// requested operations. This files contains the program entry point, plus
// most of the UI logic relating to the main window.
// Jonathan Westhues, Oct 2004
//-----------------------------------------------------------------------------
#include "linuxUI.h"
#include <stdio.h>
#include <stdlib.h>
#include "ldmicro.h"
#include "freezeLD.h"
#include "mcutable.h"
#include <iomanip>
#include <iostream>
using namespace std;
HINSTANCE Instance;
HWID MainWindow;
HWID DrawWindow;
// parameters used to capture the mouse when implementing our totally non-
// general splitter control
//static HHOOK MouseHookHandle;
static int MouseY;
// For the open/save dialog boxes
#define LDMICRO_PATTERN "LDmicro Ladder Logic Programs (*.ld)\0*.ld\0" \
"All files\0*\0\0"
char CurrentSaveFile[MAX_PATH];
static BOOL ProgramChangedNotSaved = FALSE;
#define HEX_PATTERN "Intel Hex Files (*.hex)\0*.hex\0All files\0*\0\0"
#define C_PATTERN "C Source Files (*.c)\0*.c\0All Files\0*\0\0"
#define INTERPRETED_PATTERN \
"Interpretable Byte Code Files (*.int)\0*.int\0All Files\0*\0\0"
char CurrentCompileFile[MAX_PATH];
#define TXT_PATTERN "Text Files (*.txt)\0*.txt\0All files\0*\0\0"
// Everything relating to the PLC's program, I/O configuration, processor
// choice, and so on--basically everything that would be saved in the
// project file.
PlcProgram Prog;
/// Function to safely quit program gtk main loop
gboolean LD_WM_Close_call(GtkWidget *widget, GdkEvent *event, gpointer user_data);
//-----------------------------------------------------------------------------
// Get a filename with a common dialog box and then save the program to that
// file and then set our default filename to that.
//-----------------------------------------------------------------------------
static BOOL SaveAsDialog(void)
{
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.parentWindow = MainWindow;
ofn.lpstrFilter = LDMICRO_PATTERN;
ofn.lpstrDefExt = "ld";
ofn.lpstrFile = CurrentSaveFile;
ofn.nMaxFile = sizeof(CurrentSaveFile);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if(!GetSaveFileName(&ofn))
return FALSE;
if(!SaveProjectToFile(CurrentSaveFile)) {
Error(_("Couldn't write to '%s'."), CurrentSaveFile);
return FALSE;
} else {
ProgramChangedNotSaved = FALSE;
return TRUE;
}
}
//-----------------------------------------------------------------------------
// Get a filename with a common dialog box and then export the program as
// an ASCII art drawing.
//-----------------------------------------------------------------------------
static void ExportDialog(void)
{
char exportFile[MAX_PATH];
OPENFILENAME ofn;
exportFile[0] = '\0';
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.parentWindow = MainWindow;
ofn.lpstrFilter = TXT_PATTERN;
ofn.lpstrFile = exportFile;
ofn.lpstrDefExt = "txt";
ofn.lpstrTitle = _("Export As Text");
ofn.nMaxFile = sizeof(exportFile);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if(!GetSaveFileName(&ofn))
return;
ExportDrawingAsText(exportFile);
}
//-----------------------------------------------------------------------------
// If we already have a filename, save the program to that. Otherwise same
// as Save As. Returns TRUE if it worked, else returns FALSE.
//-----------------------------------------------------------------------------
static BOOL SaveProgram(void)
{
if(strlen(CurrentSaveFile)) {
if(!SaveProjectToFile(CurrentSaveFile)) {
Error(_("Couldn't write to '%s'."), CurrentSaveFile);
return FALSE;
} else {
ProgramChangedNotSaved = FALSE;
return TRUE;
}
} else {
return SaveAsDialog();
}
}
//-----------------------------------------------------------------------------
// Compile the program to a hex file for the target micro. Get the output
// file name if necessary, then call the micro-specific compile routines.
//-----------------------------------------------------------------------------
static void CompileProgram(BOOL compileAs)
{
if(compileAs || strlen(CurrentCompileFile)==0) {
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.parentWindow = MainWindow;
ofn.lpstrTitle = _("Compile To");
if(Prog.mcu && Prog.mcu->whichIsa == ISA_ANSIC) {
ofn.lpstrFilter = C_PATTERN;
ofn.lpstrDefExt = "c";
} else if(Prog.mcu && Prog.mcu->whichIsa == ISA_INTERPRETED) {
ofn.lpstrFilter = INTERPRETED_PATTERN;
ofn.lpstrDefExt = "int";
} else {
ofn.lpstrFilter = HEX_PATTERN;
ofn.lpstrDefExt = "hex";
}
ofn.lpstrFile = CurrentCompileFile;
ofn.nMaxFile = sizeof(CurrentCompileFile);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
if(!GetSaveFileName(&ofn))
return;
// hex output filename is stored in the .ld file
ProgramChangedNotSaved = TRUE;
}
if(!GenerateIntermediateCode()) return;
if(Prog.mcu == NULL) {
Error(_("Must choose a target microcontroller before compiling."));
return;
}
if(UartFunctionUsed() && Prog.mcu->uartNeeds.rxPin == 0) {
Error(_("UART function used but not supported for this micro."));
return;
}
if(PwmFunctionUsed() && Prog.mcu->pwmNeedsPin == 0) {
Error(_("PWM function used but not supported for this micro."));
return;
}
switch(Prog.mcu->whichIsa) {
case ISA_AVR: CompileAvr(CurrentCompileFile); break;
case ISA_PIC16: CompilePic16(CurrentCompileFile); break;
case ISA_ANSIC: CompileAnsiC(CurrentCompileFile); break;
case ISA_INTERPRETED: CompileInterpreted(CurrentCompileFile); break;
case ISA_ARDUINO: CompileArduino(CurrentCompileFile); break;
default: oops();
}
IntDumpListing("t.pl");
}
//-----------------------------------------------------------------------------
// If the program has been modified then give the user the option to save it
// or to cancel the operation they are performing. Return TRUE if they want
// to cancel.
//-----------------------------------------------------------------------------
BOOL CheckSaveUserCancels(void)
{
if(!ProgramChangedNotSaved) {
// no problem
return FALSE;
}
int r = MessageBox(MainWindow,
_("The program has changed since it was last saved.\r\n\r\n"
"Do you want to save the changes?"), "LDmicro",
MB_YESNOCANCEL | MB_ICONWARNING);
switch(r) {
case IDYES:
if(SaveProgram())
return FALSE;
else
return TRUE;
case IDNO:
return FALSE;
case IDCANCEL:
return TRUE;
default:
oops();
}
}
//-----------------------------------------------------------------------------
// Load a new program from a file. If it succeeds then set our default filename
// to that, else we end up with an empty file then.
//-----------------------------------------------------------------------------
static void OpenDialog(void)
{
OPENFILENAME ofn;
char tempSaveFile[MAX_PATH] = "";
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.parentWindow = MainWindow;
ofn.lpstrFilter = LDMICRO_PATTERN;
ofn.lpstrDefExt = "ld";
ofn.lpstrFile = tempSaveFile;
ofn.nMaxFile = sizeof(tempSaveFile);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
if(!GetOpenFileName(&ofn))
return;
if(!LoadProjectFromFile(tempSaveFile)) {
Error(_("Couldn't open '%s'."), tempSaveFile);
CurrentSaveFile[0] = '\0';
} else {
ProgramChangedNotSaved = FALSE;
strcpy(CurrentSaveFile, tempSaveFile);
UndoFlush();
}
GenerateIoListDontLoseSelection();
RefreshScrollbars();
UpdateMainWindowTitleBar();
}
//-----------------------------------------------------------------------------
// Housekeeping required when the program changes: mark the program as
// changed so that we ask if user wants to save before exiting, and update
// the I/O list.
//-----------------------------------------------------------------------------
void ProgramChanged(void)
{
ProgramChangedNotSaved = TRUE;
GenerateIoListDontLoseSelection();
RefreshScrollbars();
}
#define CHANGING_PROGRAM(x) { \
UndoRemember(); \
x; \
ProgramChanged();\
}
//-----------------------------------------------------------------------------
// Hook that we install when the user starts dragging the `splitter,' in case
// they drag it out of the narrow area of the drawn splitter bar. Resize
// the listview in response to mouse move, and unhook ourselves when they
// release the mouse button.
//-----------------------------------------------------------------------------
// static LRESULT CALLBACK MouseHook(int code, WPARAM wParam, LPARAM lParam)
// {
// switch(code) {
// case HC_ACTION: {
// MSLLHOOKSTRUCT *mhs = (MSLLHOOKSTRUCT *)lParam;
// switch(wParam) {
// case WM_MOUSEMOVE: {
// int dy = MouseY - mhs->pt.y;
// IoListHeight += dy;
// if(IoListHeight < 50) IoListHeight = 50;
// MouseY = mhs->pt.y;
// MainWindowResized();
// break;
// }
// case WM_LBUTTONUP:
// UnhookWindowsHookEx(MouseHookHandle);
// break;
// }
// break;
// }
// }
// return CallNextHookEx(MouseHookHandle, code, wParam, lParam);
// }
//-----------------------------------------------------------------------------
// Handle a selection from the menu bar of the main window.
//-----------------------------------------------------------------------------
static void ProcessMenu(int code)
{
if(code >= MNU_PROCESSOR_0 && code < MNU_PROCESSOR_0+NUM_SUPPORTED_MCUS) {
strcpy(CurrentCompileFile, "");
Prog.mcu = &SupportedMcus[code - MNU_PROCESSOR_0];
RefreshControlsToSettings();
return;
}
if(code == MNU_PROCESSOR_0+NUM_SUPPORTED_MCUS) {
Prog.mcu = NULL;
strcpy(CurrentCompileFile, "");
RefreshControlsToSettings();
return;
}
switch(code) {
case MNU_NEW:
if(CheckSaveUserCancels()) break;
NewProgram();
strcpy(CurrentSaveFile, "");
strcpy(CurrentCompileFile, "");
GenerateIoListDontLoseSelection();
RefreshScrollbars();
UpdateMainWindowTitleBar();
break;
case MNU_OPEN:
if(CheckSaveUserCancels()) break;
OpenDialog();
break;
case MNU_SAVE:
SaveProgram();
UpdateMainWindowTitleBar();
break;
case MNU_SAVE_AS:
SaveAsDialog();
UpdateMainWindowTitleBar();
break;
case MNU_EXPORT:
ExportDialog();
break;
case MNU_EXIT:
if(CheckSaveUserCancels()) break;
LD_WM_Close_call(NULL, NULL, NULL);
// PostQuitMessage(0);
break;
case MNU_INSERT_COMMENT:
CHANGING_PROGRAM(AddComment(_("--add comment here--")));
break;
case MNU_INSERT_CONTACTS:
CHANGING_PROGRAM(AddContact());
break;
case MNU_INSERT_COIL:
CHANGING_PROGRAM(AddCoil());
break;
case MNU_INSERT_TON:
CHANGING_PROGRAM(AddTimer(ELEM_TON));
break;
case MNU_INSERT_TOF:
CHANGING_PROGRAM(AddTimer(ELEM_TOF));
break;
case MNU_INSERT_RTO:
CHANGING_PROGRAM(AddTimer(ELEM_RTO));
break;
case MNU_INSERT_CTU:
CHANGING_PROGRAM(AddCounter(ELEM_CTU));
break;
case MNU_INSERT_CTD:
CHANGING_PROGRAM(AddCounter(ELEM_CTD));
break;
case MNU_INSERT_CTC:
CHANGING_PROGRAM(AddCounter(ELEM_CTC));
break;
case MNU_INSERT_RES:
CHANGING_PROGRAM(AddReset());
break;
case MNU_INSERT_OPEN:
CHANGING_PROGRAM(AddEmpty(ELEM_OPEN));
break;
case MNU_INSERT_SHORT:
CHANGING_PROGRAM(AddEmpty(ELEM_SHORT));
break;
case MNU_INSERT_MASTER_RLY:
CHANGING_PROGRAM(AddMasterRelay());
break;
case MNU_INSERT_SHIFT_REG:
CHANGING_PROGRAM(AddShiftRegister());
break;
case MNU_INSERT_LUT:
CHANGING_PROGRAM(AddLookUpTable());
break;
case MNU_INSERT_PWL:
CHANGING_PROGRAM(AddPiecewiseLinear());
break;
case MNU_INSERT_FMTD_STR:
CHANGING_PROGRAM(AddFormattedString());
break;
case MNU_INSERT_OSR:
CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_RISING));
break;
case MNU_INSERT_OSF:
CHANGING_PROGRAM(AddEmpty(ELEM_ONE_SHOT_FALLING));
break;
case MNU_INSERT_MOV:
CHANGING_PROGRAM(AddMove());
break;
case MNU_INSERT_SET_PWM:
CHANGING_PROGRAM(AddSetPwm());
break;
case MNU_INSERT_READ_ADC:
CHANGING_PROGRAM(AddReadAdc());
break;
case MNU_INSERT_UART_SEND:
CHANGING_PROGRAM(AddUart(ELEM_UART_SEND));
break;
case MNU_INSERT_UART_RECV:
CHANGING_PROGRAM(AddUart(ELEM_UART_RECV));
break;
case MNU_INSERT_PERSIST:
CHANGING_PROGRAM(AddPersist());
break;
{
int elem;
case MNU_INSERT_ADD: elem = ELEM_ADD; goto math;
case MNU_INSERT_SUB: elem = ELEM_SUB; goto math;
case MNU_INSERT_MUL: elem = ELEM_MUL; goto math;
case MNU_INSERT_DIV: elem = ELEM_DIV; goto math;
math:
CHANGING_PROGRAM(AddMath(elem));
break;
}
{
int elem;
case MNU_INSERT_EQU: elem = ELEM_EQU; goto cmp;
case MNU_INSERT_NEQ: elem = ELEM_NEQ; goto cmp;
case MNU_INSERT_GRT: elem = ELEM_GRT; goto cmp;
case MNU_INSERT_GEQ: elem = ELEM_GEQ; goto cmp;
case MNU_INSERT_LES: elem = ELEM_LES; goto cmp;
case MNU_INSERT_LEQ: elem = ELEM_LEQ; goto cmp;
cmp:
CHANGING_PROGRAM(AddCmp(elem));
break;
}
case MNU_MAKE_NORMAL:
CHANGING_PROGRAM(MakeNormalSelected());
break;
case MNU_NEGATE:
CHANGING_PROGRAM(NegateSelected());
break;
case MNU_MAKE_SET_ONLY:
CHANGING_PROGRAM(MakeSetOnlySelected());
break;
case MNU_MAKE_RESET_ONLY:
CHANGING_PROGRAM(MakeResetOnlySelected());
break;
case MNU_UNDO:
UndoUndo();
break;
case MNU_REDO:
UndoRedo();
break;
case MNU_INSERT_RUNG_BEFORE:
CHANGING_PROGRAM(InsertRung(FALSE));
break;
case MNU_INSERT_RUNG_AFTER:
CHANGING_PROGRAM(InsertRung(TRUE));
break;
case MNU_DELETE_RUNG:
CHANGING_PROGRAM(DeleteSelectedRung());
break;
case MNU_PUSH_RUNG_UP:
CHANGING_PROGRAM(PushRungUp());
break;
case MNU_PUSH_RUNG_DOWN:
CHANGING_PROGRAM(PushRungDown());
break;
case MNU_DELETE_ELEMENT:
CHANGING_PROGRAM(DeleteSelectedFromProgram());
break;
case MNU_MCU_SETTINGS:
CHANGING_PROGRAM(ShowConfDialog());
break;
case MNU_SIMULATION_MODE:
ToggleSimulationMode();
break;
case MNU_START_SIMULATION:
StartSimulation();
break;
case MNU_STOP_SIMULATION:
StopSimulation();
break;
case MNU_SINGLE_CYCLE:
SimulateOneCycle(TRUE);
break;
case MNU_COMPILE:
CompileProgram(FALSE);
break;
case MNU_COMPILE_AS:
CompileProgram(TRUE);
break;
case MNU_MANUAL:
ShowHelpDialog(FALSE);
break;
case MNU_ABOUT:
ShowHelpDialog(TRUE);
break;
}
gtk_widget_queue_draw(DrawWindow);
}
//-----------------------------------------------------------------------------
// WndProc functions for MainWindow.
//-----------------------------------------------------------------------------
gboolean LD_WM_KeyDown_call(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
/* Handles:
* WM_KEYDOWN
*/
UINT wParam = event->keyval;
if(wParam == VK_TAB) {
// SetFocus(IoList);
gtk_window_set_focus (GTK_WINDOW(MainWindow), view);
// BlinkCursor(0, 0, 0, 0);
}
if(InSimulationMode)
{
switch(wParam)
{
case VK_DOWN:
if(ScrollYOffset < ScrollYOffsetMax)
ScrollYOffset++;
RefreshScrollbars();
gtk_widget_queue_draw(DrawWindow);
break;
case VK_UP:
if(ScrollYOffset > 0)
ScrollYOffset--;
RefreshScrollbars();
gtk_widget_queue_draw(DrawWindow);
break;
case VK_LEFT:
ScrollXOffset -= FONT_WIDTH;
if(ScrollXOffset < 0)
ScrollXOffset = 0;
RefreshScrollbars();
gtk_widget_queue_draw(DrawWindow);
break;
case VK_RIGHT:
ScrollXOffset += FONT_WIDTH;
if(ScrollXOffset >= ScrollXOffsetMax)
ScrollXOffset = ScrollXOffsetMax;
RefreshScrollbars();
gtk_widget_queue_draw(DrawWindow);
break;
case VK_RETURN:
case VK_ESCAPE:
ToggleSimulationMode();
break;
}
}
switch(wParam)
{
case VK_UP:
if(event->state & GDK_SHIFT_MASK)
{
CHANGING_PROGRAM(PushRungUp());
}
else
{
MoveCursorKeyboard(wParam);
}
gtk_widget_queue_draw(DrawWindow);
break;
case VK_DOWN:
if(event->state & GDK_SHIFT_MASK)
{
CHANGING_PROGRAM(PushRungDown());
}
else
{
MoveCursorKeyboard(wParam);
}
gtk_widget_queue_draw(DrawWindow);
break;
case VK_RIGHT:
case VK_LEFT:
MoveCursorKeyboard(wParam);
gtk_widget_queue_draw(DrawWindow);
break;
case VK_RETURN:
CHANGING_PROGRAM(EditSelectedElement());
gtk_widget_queue_draw(DrawWindow);
break;
default:
break;
}
return FALSE;
}
gboolean LD_WM_Close_call(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Handles:
* WM_CLOSE
*/
if(CheckSaveUserCancels())
return TRUE;
FreezeWindowPos(MainWindow);
FreezeDWORD(IoListHeight);
gtk_main_quit();
}
gboolean LD_GTK_mouse_click_hook(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Handles:
* WM_LBUTTONDBLCLK, WM_LBUTTONDOWN
*/
RECT Rect;
GetWindowRect(ScrollWindow, &Rect);
int wy;
gtk_window_get_position(GTK_WINDOW(MainWindow), NULL, &wy);
// g_print("net: %i\n", wy + 30 + Rect.bottom);
/// Identify if mouse is clicked outside drawing area
if (wy + 30 + Rect.bottom < event->button.y_root)
return FALSE;
GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ScrollWindow));
switch(event->button.type)
{
case GDK_BUTTON_PRESS:
if (event->button.button == 1) /// left click
{
GLOBAL_mouse_last_clicked_x = event->button.x_root;
GLOBAL_mouse_last_clicked_y = event->button.y_root;
int x = event->button.x;
int y = event->button.y - 30 + gtk_adjustment_get_value(adjustment);
if(!InSimulationMode) MoveCursorMouseClick(x, y);
gtk_widget_queue_draw(DrawWindow);
}
break;
case GDK_2BUTTON_PRESS:
if (event->button.button == 1) /// left click
{
GLOBAL_mouse_last_clicked_x = event->button.x_root;
GLOBAL_mouse_last_clicked_y = event->button.y_root;
int x = event->button.x;
int y = event->button.y - 30 + gtk_adjustment_get_value(adjustment);
if(InSimulationMode) {
EditElementMouseDoubleclick(x, y);
} else {
CHANGING_PROGRAM(EditElementMouseDoubleclick(x, y));
}
gtk_widget_queue_draw(DrawWindow);
}
break;
}
return FALSE;
}
gboolean LD_GTK_mouse_scroll_hook(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Handles:
* WM_VSCROLL, WM_HSCROLL, WM_MOUSEWHEEL
*/
GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ScrollWindow));
switch(event->scroll.direction)
{
case GDK_SCROLL_UP:
if (gtk_adjustment_get_value(adjustment) == gtk_adjustment_get_lower(adjustment))
VscrollProc(SB_TOP);
else
VscrollProc(SB_LINEUP);
break;
case GDK_SCROLL_DOWN:
if (gtk_adjustment_get_value(adjustment) == gtk_adjustment_get_upper(adjustment) - gtk_widget_get_allocated_height (ScrollWindow))
VscrollProc(SB_BOTTOM);
else
VscrollProc(SB_LINEDOWN);
break;
case GDK_SCROLL_LEFT:
HscrollProc(SB_LINEUP);
break;
case GDK_SCROLL_RIGHT:
HscrollProc(SB_LINEDOWN);
break;
case GDK_SCROLL_SMOOTH:
double d_x, d_y;
gdk_event_get_scroll_deltas (event, &d_x, &d_y);
if(d_y > 0) {
VscrollProc(SB_LINEUP);
} else {
VscrollProc(SB_LINEDOWN);
}
break;
}
gtk_widget_queue_draw(DrawWindow);
return FALSE;
}
gboolean LD_WM_MouseMove_call(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Handles:
* WM_MOUSEMOVE
*/
// int x = LOWORD(lParam);
// int y = HIWORD(lParam);
// if((y > (IoListTop - 9)) && (y < (IoListTop + 3))) {
// SetCursor(LoadCursor(NULL, IDC_SIZENS));
// } else {
// SetCursor(LoadCursor(NULL, IDC_ARROW));
// }
// int dy = MouseY - mhs->pt.y;
// int dy = MouseY - mhs->pt.y;
// IoListHeight += dy;
// if(IoListHeight < 50) IoListHeight = 50;
// MouseY = mhs->pt.y;
// MainWindowResized();
return FALSE;
}
gboolean LD_WM_Paint_call(HWID widget, HCRDC cr, gpointer data)
{
/* Handles:
* WM_PAINT
*/
static BOOL Paint_call_first = TRUE;
if (Paint_call_first)
{
gtk_widget_override_background_color(GTK_WIDGET(widget),
GTK_STATE_FLAG_NORMAL, (HBRUSH)GetStockObject(BLACK_BRUSH));
gint width = gtk_widget_get_allocated_width (widget);
gint height = gtk_widget_get_allocated_height (widget);
gtk_widget_set_size_request(widget, width, height+1);
gdk_cairo_set_source_rgba (cr, (HBRUSH)GetStockObject(BLACK_BRUSH));
cairo_rectangle(cr, 0, 0, width, height);
cairo_stroke_preserve(cr);
cairo_fill (cr);
Paint_call_first = FALSE;
}
/// This draws the schematic.
MainWindowResized();
PaintWindow(cr);
return FALSE;
}
void LD_WM_Destroy_call(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Handles:
* WM_DESTROY
*/
g_print("destroy called\n");
CheckSaveUserCancels();
return;
FreezeWindowPos(MainWindow);
FreezeDWORD(IoListHeight);
gtk_main_quit();
gdk_threads_leave();
}
gboolean LD_WM_Size_call(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Handles:
* WM_SIZE
*/
MainWindowResized();
return FALSE;
}
gboolean LD_WM_Command_call(GtkMenuItem* men, gpointer gpcode)
{
int tempcode = GPOINTER_TO_INT(gpcode);
ProcessMenu (tempcode);
return FALSE;
}
void ProcessorCall(GtkCheckMenuItem* men, gpointer gpcode)
{
int tempcode = GPOINTER_TO_INT(gpcode);
if(gtk_check_menu_item_get_active(men))
{
ProcessMenu (tempcode);
}
}
gboolean LD_WM_SetFocus_call(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Handles:
* WM_SETFOCUS
*/
InvalidateRect(DrawWindow, NULL, FALSE);
return FALSE;
}
void LD_WM_Notify_Row_Activate_call(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{
/* Handles:
* WM_NOTIFY
*/
// g_print("Row activated!\n");
int *ip = gtk_tree_path_get_indices ( path );
NMHDR h;
h.code = LVN_ITEMACTIVATE;
h.item.iItem = ip[0];
h.hlistFrom = IoList;
IoListProc(&h);
}
void LD_WM_Notify_Cursor_Change_call(GtkTreeView *tree_view, gpointer user_data)
{
/* Handles:
* WM_NOTIFY
*/
ITLIST iter;
// BOOL empty = !gtk_tree_model_get_iter_first (IoList, &iter);
// g_print("empty = %i\n", (empty == TRUE) );
HLIST pTreeModel;
int *ip;
GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
if(gtk_tree_selection_get_selected (selection, &pTreeModel, &iter))
{
GtkTreePath *path = gtk_tree_model_get_path ( pTreeModel , &iter ) ;
ip = gtk_tree_path_get_indices ( path );
}
else
if(!gtk_tree_model_get_iter_first (IoList, &iter))
return;
NMHDR h;
h.code = LVN_GETDISPINFO;
h.item.iItem = (ip == NULL) ? 0 : ip[0];
h.hlistFrom = IoList;
h.hlistIter = &iter;
IoListProc(&h);
}
inline void MenuHandler ()
{
g_signal_connect(G_OBJECT(NewMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_NEW));
g_signal_connect(G_OBJECT(OpenMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_OPEN));
g_signal_connect(G_OBJECT(SaveMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SAVE));
g_signal_connect(G_OBJECT(SaveAsMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SAVE_AS));
g_signal_connect(G_OBJECT(ExportMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_EXPORT));
g_signal_connect(G_OBJECT(ExitMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_EXIT));
g_signal_connect(G_OBJECT(InsertCommentMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_COMMENT));
g_signal_connect(G_OBJECT(InsertContactsMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CONTACTS));
g_signal_connect(G_OBJECT(InsertCoilMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_COIL));
g_signal_connect(G_OBJECT(InsertTonMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_TON));
g_signal_connect(G_OBJECT(InsertTofMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_TOF));
g_signal_connect(G_OBJECT(InsertRtoMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RTO));
g_signal_connect(G_OBJECT(InsertCtuMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CTU));
g_signal_connect(G_OBJECT(InsertCtdMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CTD));
g_signal_connect(G_OBJECT(InsertCtcMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_CTC));
g_signal_connect(G_OBJECT(InsertResMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RES));
g_signal_connect(G_OBJECT(InsertOpenMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_OPEN));
g_signal_connect(G_OBJECT(InsertShortMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SHORT));
g_signal_connect(G_OBJECT(InsertMasterRlyMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_MASTER_RLY));
g_signal_connect(G_OBJECT(InsertShiftRegMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SHIFT_REG));
g_signal_connect(G_OBJECT(InsertLutMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_LUT));
g_signal_connect(G_OBJECT(InsertPwlMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_PWL));
g_signal_connect(G_OBJECT(InsertFmtdStrMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_FMTD_STR));
g_signal_connect(G_OBJECT(InsertOsrMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_OSR));
g_signal_connect(G_OBJECT(InsertOsfMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_OSF));
g_signal_connect(G_OBJECT(InsertMovMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_MOV));
g_signal_connect(G_OBJECT(InsertSetPwmMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SET_PWM));
g_signal_connect(G_OBJECT(InsertReadAdcMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_READ_ADC));
g_signal_connect(G_OBJECT(InsertUartSendMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_UART_SEND));
g_signal_connect(G_OBJECT(InsertUartRecvMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_UART_RECV));
g_signal_connect(G_OBJECT(InsertPersistMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_PERSIST));
g_signal_connect(G_OBJECT(InsertAddMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_ADD));
g_signal_connect(G_OBJECT(InsertSubMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_SUB));
g_signal_connect(G_OBJECT(InsertMulMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_MUL));
g_signal_connect(G_OBJECT(InsertDivMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_DIV));
g_signal_connect(G_OBJECT(InsertEquMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_EQU));
g_signal_connect(G_OBJECT(InsertNeqMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_NEQ));
g_signal_connect(G_OBJECT(InsertGrtMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_GRT));
g_signal_connect(G_OBJECT(InsertGeqMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_GEQ));
g_signal_connect(G_OBJECT(InsertLesMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_LES));
g_signal_connect(G_OBJECT(InsertLeqMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_LEQ));
g_signal_connect(G_OBJECT(MakeNormalMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MAKE_NORMAL));
g_signal_connect(G_OBJECT(NegateMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_NEGATE));
g_signal_connect(G_OBJECT(MakeSetOnlyMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MAKE_SET_ONLY));
g_signal_connect(G_OBJECT(MakeResetOnlyMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MAKE_RESET_ONLY));
g_signal_connect(G_OBJECT(UndoMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_UNDO));
g_signal_connect(G_OBJECT(RedoMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_REDO));
g_signal_connect(G_OBJECT(InsertRungBeforeMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RUNG_BEFORE));
g_signal_connect(G_OBJECT(InsertRungAfterMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_INSERT_RUNG_AFTER));
g_signal_connect(G_OBJECT(DeleteRungMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_DELETE_RUNG));
g_signal_connect(G_OBJECT(PushRungUpMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_PUSH_RUNG_UP));
g_signal_connect(G_OBJECT(PushRungDownMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_PUSH_RUNG_DOWN));
g_signal_connect(G_OBJECT(DeleteElementMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_DELETE_ELEMENT));
g_signal_connect(G_OBJECT(McuSettingsMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MCU_SETTINGS));
g_signal_connect(G_OBJECT(SimulationModeMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SIMULATION_MODE));
g_signal_connect(G_OBJECT(StartSimulationMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_START_SIMULATION));
g_signal_connect(G_OBJECT(StopSimulationMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_STOP_SIMULATION));
g_signal_connect(G_OBJECT(SingleCycleMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_SINGLE_CYCLE));
g_signal_connect(G_OBJECT(CompileMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_COMPILE));
g_signal_connect(G_OBJECT(CompileAsMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_COMPILE_AS));
g_signal_connect(G_OBJECT(ManualMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_MANUAL));
g_signal_connect(G_OBJECT(AboutMenu), "activate",
G_CALLBACK(LD_WM_Command_call), GINT_TO_POINTER(MNU_ABOUT));
// Connect microcontroller signals automatically
for(int i = 0; i < NUM_SUPPORTED_MCUS; i++)
{
g_signal_connect(G_OBJECT(ProcessorMenuItems[i]), "toggled",
G_CALLBACK(ProcessorCall), GINT_TO_POINTER((MNU_PROCESSOR_0 + i)));
}
}
//-----------------------------------------------------------------------------
// Entry point into the program.
//-----------------------------------------------------------------------------
int main(int argc, char** argv)
{
/// Check if we're running in non-interactive mode; in that case we should
/// load the file, compile, and exit.
if(argc >= 2 && !(argc < 4) ) {
RunningInBatchMode = TRUE;
char *err =
"Bad command line arguments: run 'ldmicro /c src.ld dest.hex'";
char *source = (char*)malloc(strlen(argv[2]) + strlen(argv[3]) + 2);
sprintf(source, "%s %s", argv[2], argv[3]);
while(isspace(*source)) {
source++;
}
if(*source == '\0')
{
Error(err);
free(source);
exit(-1);
}
char *dest = source;
while(!isspace(*dest) && *dest) {
dest++;
}
if(*dest == '\0')
{
Error(err);
free(source);
exit(-1);
}
*dest = '\0'; dest++;
while(isspace(*dest)) {
dest++;
}
if(*dest == '\0')
{
Error(err);
free(source);
exit(-1);
}
if(!LoadProjectFromFile(source)) {
Error("Couldn't open '%s', running non-interactively.\n", source);
free(source);
exit(-1);
}
strcpy(CurrentCompileFile, dest);
GenerateIoList(-1);
CompileProgram(FALSE);
exit(0);
}
//we need to initialize all these functions so that gtk knows
//to be thread-aware
if (!g_thread_supported ()){ g_thread_init(NULL); }
gdk_threads_init();
gdk_threads_enter();
gtk_init(&argc, &argv);
Instance = NULL;
MainHeap = HeapCreate(0, 1024*64, 0);
/// Make main window
MainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(MainWindow), "LDmicro");
gtk_window_set_default_size (GTK_WINDOW(MainWindow), 800, 600);
gtk_window_resize (GTK_WINDOW(MainWindow), 800, 600);
gtk_window_move(GTK_WINDOW(MainWindow), 10, 10);
gtk_widget_override_background_color(GTK_WIDGET(MainWindow),
GTK_STATE_FLAG_NORMAL, ((HBRUSH)GetStockObject(DKGRAY_BRUSH)));
gtk_window_set_default_icon(LoadImage(Instance, LDMICRO_ICON,
IMAGE_ICON, 32, 32, 0));
gtk_window_set_icon(GTK_WINDOW(MainWindow), LoadImage(Instance, LDMICRO_ICON,
IMAGE_ICON, 32, 32, 0));
/// Make main window - end
MakeMainWindowMenus();
MakeDialogBoxClass();
InitForDrawing();
ThawWindowPos(MainWindow);
IoListHeight = 100;
ThawDWORD(IoListHeight);
MakeMainWindowControls(); /// takes care of MakeMainWindowMenus()
MainWindowResized();
/// Keyboard and mouse hooks equivalent to MainWndProc
g_signal_connect (MainWindow, "delete_event", G_CALLBACK (LD_WM_Close_call), NULL);
g_signal_connect (MainWindow, "key_press_event", G_CALLBACK (LD_WM_KeyDown_call), NULL);
g_signal_connect (MainWindow, "button_press_event", G_CALLBACK (LD_GTK_mouse_click_hook), NULL);
g_signal_connect (MainWindow, "scroll_event", G_CALLBACK (LD_GTK_mouse_scroll_hook), NULL);
g_signal_connect (MainWindow, "motion_notify_event", G_CALLBACK (LD_WM_MouseMove_call), NULL);
g_signal_connect (DrawWindow, "draw", G_CALLBACK (LD_WM_Paint_call), NULL);
g_signal_connect (MainWindow, "destroy_event", G_CALLBACK (LD_WM_Destroy_call), NULL);
g_signal_connect (MainWindow, "configure_event", G_CALLBACK (LD_WM_Size_call), NULL);
g_signal_connect (MainWindow, "focus_in_event", G_CALLBACK (LD_WM_SetFocus_call), NULL);
g_signal_connect (view, "row_activated", G_CALLBACK (LD_WM_Notify_Row_Activate_call), NULL);
g_signal_connect (view, "cursor_changed", G_CALLBACK (LD_WM_Notify_Cursor_Change_call), NULL);
MenuHandler();
/// Keyboard and mouse hooks equivalent to MainWndProc - end
NewProgram();
strcpy(CurrentSaveFile, "");
/// We are running interactively, or we would already have exited. We
/// can therefore show the window now, and otherwise set up the GUI.
/// Displaying the window
gtk_widget_show_all(MainWindow);
/// Blink cursor
SetTimer(DrawWindow, TIMER_BLINK_CURSOR, 200, BlinkCursor);
// SetTimer(MainWindow, TIMER_BLINK_CURSOR, 800, BlinkCursor);
if(argc >= 2) {
char line[MAX_PATH];
if(*argv[1] == '"') {
strcpy(line, argv[1]+1);
} else {
strcpy(line, argv[1]);
}
if(strchr(line, '"')) *strchr(line, '"') = '\0';
realpath(line, CurrentSaveFile);
if(!LoadProjectFromFile(CurrentSaveFile)) {
NewProgram();
Error(_("Couldn't open '%s'."), CurrentSaveFile);
CurrentSaveFile[0] = '\0';
}
UndoFlush();
}
GenerateIoListDontLoseSelection();
RefreshScrollbars();
UpdateMainWindowTitleBar();
// MSG msg;
// DWORD ret;
// while(ret = GetMessage(&msg, NULL, 0, 0)) {
// if(msg.hwnd == IoList && msg.message == WM_KEYDOWN) {
// if(msg.wParam == VK_TAB) {
// SetFocus(MainWindow);
// continue;
// }
// }
// if(msg.message == WM_KEYDOWN && msg.wParam != VK_UP &&
// msg.wParam != VK_DOWN && msg.wParam != VK_RETURN && msg.wParam
// != VK_SHIFT)
// {
// if(msg.hwnd == IoList) {
// msg.hwnd = MainWindow;
// SetFocus(MainWindow);
// }
// }
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// }
gtk_main();
return EXIT_SUCCESS;
}
|