Précédent   Forum des professionnels en informatique > Autres langages > Autres langages > Basic > PureBasic
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 05/04/2011, 07h27   #1
Candidat au titre de Membre du Club
 
Inscription : mars 2006
Messages : 40
Détails du profil
Informations personnelles :
Âge : 32

Informations forums :
Inscription : mars 2006
Messages : 40
Points : 12
Points : 12
Envoyer un message via MSN à powerpsy
Par défaut Demande d'aide pour un Notepad

Bonjour !

Hier soir j'ai relancé un peu le purebasic et j'ai commencé à écrire un petit éditeur de texte. J'ai vu pas mal de choses intéressantes dans les exemples de PB et je me demande je dois utiliser certaines fonctions type AllocateMemory, Set-GetClipboard, ReadData... car mon code est vraiment TRES lent si j'ouvre un gros fichier ou si ce n'est pas un fichier texte.

Une autre question concerne le choix de la façon de faire: j'ai ouvert une fenêtre et j'ai placé un gadget type EditorGadget. Est-ce le bon choix ?

Autre chose encore: pour l'ouverture du fichier je prends chaîne après chaîne, est ce la bonne façon de procéder ?

Merci d'avance pour toute lumière additionnelle sur le sujet !

Sam

Code :
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
; List of functions
#New = 1
#Open = 2
#Save = 3
#SaveAs = 4
#Quit = 5
#Cut = 6
#Copy = 7
#Paste = 8
#Find = 9
 
; List of gadgets
#StatusBar = 0
#Editor = 1
 
Procedure ResizeEditWindow()
  ResizeGadget(1, #PB_Ignore, ToolBarHeight(0), WindowWidth(0), WindowHeight(0)-MenuHeight()-ToolBarHeight(0)-StatusBarHeight(0))
EndProcedure
 
Procedure P_NewFile()
  ClearGadgetItems(#Editor)
 
EndProcedure
 
Procedure P_OpenFile()
  file$ = OpenFileRequester("Select a file to open","","Text (.txt)|*.txt|All files (*.*)|*.*",0)
  If file$
    open_file = OpenFile(0,file$)
    While Eof(0) = 0
      read_file$ = ReadString(0)
      AddGadgetItem (#Editor, -1, read_file$)
    Wend
    CloseFile(0)
    StatusBarText(#StatusBar, 0, file$)
  EndIf
EndProcedure
 
If OpenWindow(0, 0, 0, 600, 400, "SimplifyEdit", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_BorderLess)
  If CreateImageMenu(0, WindowID(0))
    MenuTitle("File")
    MenuItem(#New, "&New"  + Chr(9)    + "Ctrl+n")
    MenuItem(#Open, "&Open" + Chr(9)    + "Ctrl+o")
    MenuItem(#Save, "&Save" + Chr(9)    + "Ctrl+s")
    MenuItem(#SaveAs, "&Save as")
    MenuBar()
    MenuItem(#Quit, "&Quit" + Chr(9)    + "Ctrl+q")
 
    MenuTitle("Edit")
    MenuItem(#Cut, "&Cut"   + Chr(9) + "Ctrl+x")
    MenuItem(#Copy, "&Copy"  + Chr(9) + "Ctrl+c")
    MenuItem(#Paste, "&Paste" + Chr(9) + "Ctrl+v") 
  EndIf
 
  If CreateToolBar(0, WindowID(0))
    ToolBarStandardButton(#New, #PB_ToolBarIcon_New)
    ToolBarStandardButton(#Open, #PB_ToolBarIcon_Open)
    ToolBarStandardButton(#Save, #PB_ToolBarIcon_Save)
 
    ToolBarSeparator()
 
    ToolBarStandardButton(#Cut, #PB_ToolBarIcon_Cut)
    ToolBarStandardButton(#Copy, #PB_ToolBarIcon_Copy)
    ToolBarStandardButton(#Paste, #PB_ToolBarIcon_Paste)
 
    ToolBarSeparator()
 
    ToolBarStandardButton(#Find, #PB_ToolBarIcon_Find)
    DisableToolBarButton(0, #Find, 1)
 
  EndIf  
 
  If CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
  EndIf
  StatusBarText(#StatusBar, 0, "Editing new file")
 
  EditorGadget(#Editor, 0, ToolBarHeight(0), WindowWidth(0), WindowHeight(0)-MenuHeight()-ToolBarHeight(0)-StatusBarHeight(0))
 
  ResizeEditWindow()
 
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_Menu
        Select EventMenu()
          Case #New
            P_NewFile()
          Case #Open
            P_NewFile()
            P_OpenFile()
          Case #Save
            MessageRequester("PureBasic", "Save", 0)
          Case #Quit
            Quit = 1
        EndSelect
      Case #PB_Event_SizeWindow
        ResizeEditWindow()
      Case #PB_Event_CloseWindow
        Quit = 1
    EndSelect
  Until Quit > 0
  CloseWindow(0)
EndIf
; IDE Options = PureBasic 4.50 (Windows - x64)
; CursorPosition = 31
; FirstLine = 21
; Folding = +
; EnableXP
powerpsy est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/04/2011, 10h05   #2
Invité régulier
 
Deimon
Inscription : octobre 2010
Messages : 45
Détails du profil
Informations personnelles :
Nom : Deimon

Informations forums :
Inscription : octobre 2010
Messages : 45
Points : 9
Points : 9
Yo!
Citation:
utiliser certaines fonctions type AllocateMemory, Set-GetClipboard, ReadData
Effectivement, tu peux essayer d'integrer à ton code certaines fonctions de la bibliothèque memory pour essayer d'augmenter sa vitesse d'execution (mais perso je sais pas trop )
Citation:
EditorGadget. Est-ce le bon choix ?
EditorGadget permet de saisir une grande quantité de texte donc ce choix me parait plutot bon
Citation:
je prends chaîne après chaîne
C'est une technique simple mais efficace, qui est une bonne facon de procéder en effet.
En résumé, tes choix sont plutot bon (à voir pour les fonctions mémoire).
Dzoumaka est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/04/2011, 18h27   #3
Responsable Purebasic
 
Avatar de comtois
 
Inscription : avril 2003
Messages : 810
Détails du profil
Informations forums :
Inscription : avril 2003
Messages : 810
Points : 1 856
Points : 1 856
voila un très bel exemple, tu vas pouvoir étudier ça !

Code :
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
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
;utopiomania text editor template 20091203
 
;-initialise
;-
Enumeration
  #WIN1
  #MENU
  #POP_UP
  #STAT1
  #EDIT1
  #DATE1
  #FILE_OPEN
  #FILE_INSERT
  #FILE_NEW
  #FILE_RECENT1
  #FILE_RECENT2
  #FILE_RECENT3
  #FILE_RECENT4
  #FILE_RECENT5
  #FILE_RECENT6
  #FILE_RECENT7
  #FILE_RECENT8
  #FILE_SAVE
  #FILE_SAVEAS
  #FILE_SAVETO
  #FILE_SAVEEXIT
  #FILE_PRINT
  #FILE_HOME
  #FILE_EXPLORE
  #FILE_EXIT
  #EDIT_UNDO
  #EDIT_CUT
  #EDIT_COPY
  #EDIT_PASTE
  #EDIT_ERASE
  #EDIT_FINDREPL
  #EDIT_DATE
  #EDIT_TIME
  #TOOLS_SEARCH
  #TOOLS_CHARMAP
  #TOOLS_DATEPICKER
  #TOOLS_CALCULATOR
  #TOOLS_ADDRESS
  #TOOLS_COMMAND
  #SETUP_TOPMOST
  #SETUP_WORDWRAP
  #SETUP_SMALLER
  #SETUP_NORMAL
  #SETUP_LARGER
  #SETUP_CUSTOM
  #SETUP_FONT
  #SETUP_COLOR
  #SETUP_DESKTOP
  #SETUP_STARTMENU
  #HELP_HTM
  #HELP_PDF
  #HELP_ABOUT
  #ESCAPE   
EndEnumeration
 
Declare openMainWindow()
Declare fileHome()
Declare fileOpen()
Declare saveRecentFiles(path.s)
Declare fileInsert()
Declare fileSave()
Declare fileSaveAs()
Declare fileSaveTo()
Declare fileNew()
Declare fileExit()
Declare fileRecent1()
Declare fileRecent2()
Declare fileRecent3()
Declare fileRecent4()
Declare fileRecent5()
Declare fileRecent6()
Declare fileRecent7()
Declare fileRecent8()
Declare filePrint()
Declare fileExplore()
 
Declare editUndo()
Declare editCut()
Declare editCopy()
Declare editPaste()
Declare editInsertDate()
Declare editInsertTime()
Declare editCalculator()
Declare editFindReplace(id)
 
Declare toolsSearch()
Declare toolsCharmap()
Declare.s toolsDatePicker(parent)
Declare toolsCalendar()
Declare toolsCaculator()
Declare toolsAddress()
Declare toolsCommand()
 
Declare setupTopmost()
Declare setupWordwrap()
Declare setupSmaller()
Declare setupNormal()
Declare setupLarger()
Declare setupColor()
Declare setupDesktop()
Declare setupStartmenu()
 
Declare helpHtm()
Declare helpPdf()
Declare helpAbout()
 
Declare.s dateStr()
Declare.s timeStr()
Declare.s toolsDatePicker(parent)
Declare setupFont()
Declare about(parent, html.s)
Declare reLoad(id, name.s, rtf)
Declare reSave(id, name.s, rtf)
Declare reLoadSaveCallback(lenF, *pbBuff, cb, *pcb.LONG)
Declare insertFile(id, path.s)
Declare saveModifiedFile()
Declare isModified()
Declare resetModified()
Declare getLinenumber(id)
Declare getSelTextlength()
Declare.s getprogramName(exeName.s)
Declare loadPreferences(inFolder.s, name.s)
Declare savePreferences(inFolder.s, name.s)
Declare setInnerSize(wdt, hgt)
Declare createShortcut(file, res.s, url.s, icon.s, index)
Declare.s getSpecialFolder(id)
Declare processCmdLine()
Declare processDotCommands()
 
;linenumber callback and procedure
Declare editCallback(hWnd, uMsg, wParam, lParam)
Declare drawLinenumbers()
Declare getLinenumber(id)
 
Global appName.s = GetFilePart(ProgramFilename())
;strip out the extension and version info
appName = getProgramName(appName)
Global appFolder.s = GetPathPart(ProgramFilename())
Global preferences.s
Global maximized, windowW, windowH
Global untitled.s = "untitled.txt"
Global fileName.s = untitled
Global topmost, wordwrap
;separate default folders
Global fileOpenFolder.s, fileInsertFolder.s
Global fileSavetoFolder.s
Global fontName.s, fontSize
Global fontColor, backColor
;recent files menu items
Global recent1.s, recent2.s, recent3.s, recent4.s
Global recent5.s, recent6.s, recent7.s, recent8.s
;used by reLoad/reSave/reLoadSaveCallback()
Global lenF, fileH
;for linenumber callback and functions
Global editProc, leftM = 56
;use linenumbers
Global lineNumbers = 1
;used by tools/internet
Global internetUrl.s = "http://www.google.com"
;used by getSpecialFolder(),..user\start menu\programs
#CSIDL_PROGRAMS = $2
;used by getSpecialFolder(),..user\desktop
#CSIDL_DESKTOPDIRECTORY = $10
 
;-run
loadPreferences("", appName)
openMainWindow()
processCmdLine()
 
;-eventhandler
Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_Menu
      Select EventMenu()
        Case #FILE_OPEN
          fileOpen()
          SetActiveGadget(#EDIT1)
        Case #FILE_INSERT
          fileInsert()
          SetActiveGadget(#EDIT1)
        Case #FILE_NEW
          fileNew()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT1
          fileRecent1()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT2
          fileRecent2()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT3
          fileRecent3()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT4
          fileRecent4()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT5
          fileRecent5()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT6
          fileRecent6()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT7
          fileRecent7()
          SetActiveGadget(#EDIT1)
        Case #FILE_RECENT8
          fileRecent8()
          SetActiveGadget(#EDIT1)
        Case #FILE_SAVE
          fileSave()
          SetActiveGadget(#EDIT1)
        Case #FILE_SAVEAS
          fileSaveAs()
          SetActiveGadget(#EDIT1)
        Case #FILE_SAVETO
          fileSaveTo()
          SetActiveGadget(#EDIT1)
        Case #FILE_SAVEEXIT
          fileSave()
          Break
        Case #FILE_PRINT
          filePrint()
          SetActiveGadget(#EDIT1)
        Case #FILE_HOME
          fileHome()
        Case #FILE_EXPLORE
          fileExplore()
        Case #FILE_EXIT
          If saveModifiedFile() <> #IDCANCEL
            Break
          EndIf
 
        Case #EDIT_UNDO
          editUndo()
        Case #EDIT_CUT
          editCut()
        Case #EDIT_COPY
          editCopy()
        Case #EDIT_PASTE
          editPaste()
        Case #EDIT_TIME
          editInsertTime()
        Case #EDIT_DATE
          editInsertDate()
        Case #EDIT_FINDREPL
          editFindReplace(#EDIT1)
 
        Case #TOOLS_SEARCH
          toolsSearch()
        Case #TOOLS_CHARMAP
          toolsCharmap()
        Case #TOOLS_DATEPICKER
          toolsCalendar()
        Case #TOOLS_CALCULATOR
          editCalculator()
        Case #TOOLS_ADDRESS
          toolsAddress()
        Case #TOOLS_COMMAND
          toolsCommand()
 
        Case #SETUP_TOPMOST
          setupTopmost()
        Case #SETUP_WORDWRAP
          setupWordwrap()
        Case #SETUP_WORDWRAP
          setupWordwrap()
        Case #SETUP_FONT
          setupFont()
          SetActiveGadget(#EDIT1)
        Case #SETUP_SMALLER
          setupSmaller()
        Case #SETUP_NORMAL
          setupNormal()           
        Case #SETUP_LARGER
          setupLarger()
        Case #SETUP_COLOR
          setupColor()
          SetActiveGadget(#EDIT1)
        Case #SETUP_DESKTOP
          ;create a shortcut to this program on the desktop
          setupDesktop()
          SetActiveGadget(#EDIT1)
        Case #SETUP_STARTMENU
          ;create a shortcut to this program in the startmenu
          setupStartmenu()
          SetActiveGadget(#EDIT1)
 
        Case #HELP_HTM
          RunProgram(appname + ".htm1.html")
          SetActiveGadget(#EDIT1)
        Case #HELP_PDF
          RunProgram(appName + ".pdf1.pdf")
          SetActiveGadget(#EDIT1)
        Case #HELP_ABOUT
          helpAbout()
          SetActiveGadget(#EDIT1)
      EndSelect
 
    Case #WM_RBUTTONDOWN
      Select EventWindow()
        Case #WIN1
          DisplayPopupMenu(#POP_UP, WindowID(#WIN1))
      EndSelect
 
    Case #PB_Event_SizeWindow
      If GetWindowState(#WIN1) = #PB_Window_Maximize
        maximized = 1
      Else
        maximized = 0
        windowW = WindowWidth(#WIN1)
        windowH = WindowHeight(#WIN1)
      EndIf
      ResizeGadget(#EDIT1, 0, 0, WindowWidth(#WIN1), WindowHeight(#WIN1) - MenuHeight() - StatusBarHeight(#STAT1))
      SetActiveGadget(#EDIT1)
 
    Case #PB_Event_CloseWindow
      If saveModifiedFile() <> #IDCANCEL
        Break
      EndIf
  EndSelect
ForEver
 
;-end
CloseWindow(#WIN1)
savePreferences("", appName)
End
 
;-
Procedure openMainWindow()
  flags = #PB_Window_SystemMenu |  #PB_Window_SizeGadget
  flags | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget
  text.s = appName + " - " + fileOpenFolder + fileName
  If OpenWindow(#WIN1, 0, 0, windowW, windowH, text, flags)
    If maximized
      SetWindowState(#WIN1, #PB_Window_Maximize)
    Else
      ;center at top
      l = (GetSystemMetrics_(#SM_CXSCREEN) - WindowWidth(#WIN1)) / 2
      ResizeWindow(#WIN1, l, 0, #PB_Ignore, #PB_Ignore)
    EndIf
 
    hMnu = CreateMenu(#MENU, WindowID(#WIN1))
    If hMnu
      MenuTitle("&File")
      MenuItem(#FILE_OPEN, "&Open..." + Chr(9) + "Ctrl + O")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_O, #FILE_OPEN)     
 
      MenuItem(#FILE_NEW, "&New" + Chr(9) + "Ctrl + N")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_N, #FILE_NEW)     
 
      OpenSubMenu("Recent Files...")
      MenuItem(#FILE_RECENT1, recent1)
      MenuItem(#FILE_RECENT2, recent2)
      MenuItem(#FILE_RECENT3, recent3)
      MenuItem(#FILE_RECENT4, recent4)
      MenuItem(#FILE_RECENT5, recent5)
      MenuItem(#FILE_RECENT6, recent6)
      MenuItem(#FILE_RECENT7, recent7)
      MenuItem(#FILE_RECENT8, recent8)
      CloseSubMenu()
 
      MenuItem(#FILE_INSERT, "Insert...")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_I, #FILE_INSERT)     
      MenuBar()
      MenuItem(#FILE_EXPLORE, "Explore Folder")
      MenuItem(#FILE_HOME, "User Home...")
      MenuBar()
 
      MenuItem(#FILE_SAVE, "&Save" + Chr(9) + "Ctrl + S")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_S, #FILE_SAVE)     
 
      MenuItem(#FILE_SAVEAS, "Save As...")     
      MenuItem(#FILE_SAVETO, "Save To...")     
      MenuBar()
      MenuItem(#FILE_SAVEEXIT, "Save and Exit")     
      MenuBar()
 
      MenuItem(#FILE_PRINT, "&Print" + Chr(9) + "Ctrl + P")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_P, #FILE_PRINT)     
      MenuBar()
 
      MenuItem(#FILE_EXIT, "Exit")
 
      ;edit menuitem
      MenuTitle("&Edit")
      MenuItem(#EDIT_CUT, "Cut (All)" + Chr(9) + "Ctrl + X")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_X, #EDIT_CUT)     
 
      MenuItem(#EDIT_COPY, "&Copy (All)" + Chr(9) + "Ctrl + C")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_C, #EDIT_COPY)     
 
      MenuItem(#EDIT_PASTE, "Paste" + Chr(9) + "Ctrl + V")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_V, #EDIT_PASTE)     
 
      MenuItem(#EDIT_UNDO, "Undo" + Chr(9) + "Ctrl + Z")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_Z, #EDIT_UNDO)     
      MenuBar()
 
      MenuItem(#EDIT_TIME, "Insert &Time" + Chr(9) + "Ctrl + T")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_T, #EDIT_TIME)     
      MenuItem(#EDIT_DATE, "Insert &Date" + Chr(9) + "Ctrl + D")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_D, #EDIT_DATE)     
      MenuBar()
 
      MenuItem(#EDIT_FINDREPL, "&Find/Replace..." + Chr(9) + "Ctrl + F")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_F, #EDIT_FINDREPL)     
 
      ;tools menuitem
      MenuTitle("&Tools")
      MenuItem(#TOOLS_SEARCH, "Google" + Chr(9) + "Ctrl + G")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_G, #TOOLS_SEARCH)     
      MenuBar()
      MenuItem(#TOOLS_DATEPICKER, "Date Picker")
      MenuItem(#TOOLS_CHARMAP, "Character Map")
      MenuItem(#TOOLS_CALCULATOR, "Calculator")
      MenuItem(#TOOLS_COMMAND, "Command Line")
      MenuItem(#TOOLS_ADDRESS, "Address Book")
 
      ;setup menuitem
      MenuTitle("&Setup")
      MenuItem(#SETUP_TOPMOST, "Always On Top")
      SetMenuItemState(#MENU, #SETUP_TOPMOST, topmost)
 
      MenuItem(#SETUP_WORDWRAP, "Enable Wordwrap")
      SetMenuItemState(#MENU, #SETUP_WORDWRAP, wordwrap)
      MenuBar()
 
      MenuItem(#SETUP_SMALLER, "Smaller Size")
      MenuItem(#SETUP_NORMAL, "Normal Size")
      MenuItem(#SETUP_LARGER, "Larger Size")
      MenuBar()
 
      MenuItem(#SETUP_FONT, "Editor Font/Font Color...")
      MenuItem(#SETUP_COLOR, "Editor Background Color...")
      MenuBar()
 
      MenuItem(#SETUP_DESKTOP, "Create Desktop Shortcut to this program")
      MenuItem(#SETUP_STARTMENU, "Create Startmenu Shortcut to this program")
 
      ;help menuitem
      MenuTitle("&Help")
      If FileSize(appFolder + appName + ".htm1.html") <> -1
        ;a html help file exists, add a shortcut to it
        MenuItem(#HELP_HTM, "&Help" + Chr(9) + "Ctrl + H")
        AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_H, #HELP_HTM)     
      EndIf
      If FileSize(appFolder + appName + ".pdf1.pdf") <> -1
        ;a manual file exists, add a shortcut to it
        MenuItem(#HELP_PDF, "Manual")
      EndIf
      MenuItem(#HELP_ABOUT, "About '" + appName + "'")
    Else
      ;error
      ProcedureReturn 0
    EndIf
 
    ;popup menu
    hMnu = CreatePopupMenu(#POP_UP)
    If hMnu
      ;edit main menu items
      MenuItem(#FILE_OPEN, "&Open..." + Chr(9) + "Ctrl + O")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_O, #FILE_OPEN)     
 
      MenuItem(#FILE_NEW, "&New" + Chr(9) + "Ctrl + N")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_N, #FILE_NEW)     
 
      OpenSubMenu("Recent Files...")
      MenuItem(#FILE_RECENT1, recent1)
      MenuItem(#FILE_RECENT2, recent2)
      MenuItem(#FILE_RECENT3, recent3)
      MenuItem(#FILE_RECENT4, recent4)
      MenuItem(#FILE_RECENT5, recent5)
      MenuItem(#FILE_RECENT6, recent6)
      MenuItem(#FILE_RECENT7, recent7)
      MenuItem(#FILE_RECENT8, recent8)
      CloseSubMenu()
      MenuBar()
      MenuItem(#FILE_EXPLORE, "Explore Folder")
      MenuBar()
 
      MenuItem(#FILE_SAVE, "&Save" + Chr(9) + "Ctrl + S")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_S, #FILE_SAVE)     
      MenuItem(#FILE_SAVEAS, "Save As...")     
      MenuItem(#FILE_SAVETO, "Save To...")     
      MenuBar()
      MenuItem(#FILE_SAVEEXIT, "Save and Exit")     
      MenuBar()
 
      MenuItem(#EDIT_CUT, "Cut (All)" + Chr(9) + "Ctrl + X")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_X, #EDIT_CUT)     
 
      MenuItem(#EDIT_COPY, "&Copy (All)" + Chr(9) + "Ctrl + C")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_C, #EDIT_COPY)     
 
      MenuItem(#EDIT_PASTE, "Paste" + Chr(9) + "Ctrl + V")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_V, #EDIT_PASTE)     
 
      MenuItem(#EDIT_UNDO, "Undo" + Chr(9) + "Ctrl + Z")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_Z, #EDIT_UNDO)     
      MenuBar()     
 
      MenuItem(#EDIT_FINDREPL, "&Find/Replace..." + Chr(9) + "Ctrl + F")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_F, #EDIT_FINDREPL)     
      MenuBar()
 
      MenuItem(#FILE_PRINT, "&Print" + Chr(9) + "Ctrl + P")
      AddKeyboardShortcut(#WIN1, #PB_Shortcut_Control | #PB_Shortcut_P, #FILE_PRINT)     
      MenuBar()
      MenuItem(#FILE_EXIT, "Exit")
    Else
      ;error
      ProcedureReturn 0
    EndIf
 
    ;statusbar
    If CreateStatusBar(#STAT1, WindowID(#WIN1))
      AddStatusBarField(400)
      StatusBarText(#STAT1, 0, "  u n d e r c o n s t r u c t i o n")
    EndIf
 
    ;gadgets   
    wdt = WindowWidth(#WIN1)
    hgt = WindowHeight(#WIN1)
    EditorGadget(#EDIT1, 0, 0, wdt, hgt)
 
    ;set to plaintext
    SendMessage_(GadgetID(#EDIT1), #EM_SETTEXTMODE, #TM_PLAINTEXT, 0)
    ;    sendMessage_(GadgetID(#EDIT1), #EM_SETTEXTMODE, 1, 0)
    ;    sendMessage_(GadgetID(#EDIT1), #EM_SETTEXTMODE, #TM_RICHTEXT, 0)
 
    ;set up the editorgadget to send caret notifications to get line number:
    evMask = SendMessage_(GadgetID(#EDIT1), #EM_GETEVENTMASK, 0, 0)
    SendMessage_(GadgetID(#EDIT1), #EM_SETEVENTMASK, 0, evMask | #ENM_KEYEVENTS | #ENM_MOUSEEVENTS)
 
    ;remove the editorgadget border
    style = GetWindowLong_(GadgetID(#EDIT1), #GWL_EXSTYLE)
    style = style & (~#WS_EX_CLIENTEDGE)
    SetWindowLong_(GadgetID(#EDIT1), #GWL_EXSTYLE, style)
    SetWindowPos_(GadgetID(#EDIT1), 0, 0, 0, 0, 0, #SWP_SHOWWINDOW|#SWP_NOSIZE|#SWP_NOMOVE|#SWP_FRAMECHANGED)
 
    ;editorgadget font
    hFont = LoadFont(0, fontName, fontSize)
    SetGadgetFont(#EDIT1, hFont)
 
    ;set text size limit (bytes) and colors
    SendMessage_(GadgetID(#EDIT1), #EM_EXLIMITTEXT, 0, 1024 * 1024 * 1024)
    SetGadgetColor(#EDIT1, #PB_Gadget_FrontColor, fontColor)
    SetGadgetColor(#EDIT1, #PB_Gadget_BackColor, backColor)
 
    ;linenumbers
    If lineNumbers
      ;set up margins to allow for linenumbers
      GetClientRect_(GadgetID(#EDIT1), rc.RECT)
      rc\left = leftM
      SendMessage_(GadgetID(#EDIT1), #EM_SETRECT, 0, rc)
      ;subclass the editorgadget to handle linenumbers
      editProc = SetWindowLong_(GadgetID(#EDIT1), #GWL_WNDPROC, @editCallback())
    EndIf
 
    ;preferences
    If topmost
      StickyWindow(#WIN1, 1)
    EndIf
    If wordwrap
      SendMessage_(GadgetID(#EDIT1), #EM_SETTARGETDEVICE, 0, 0)
    Else
      SendMessage_(GadgetID(#EDIT1), #EM_SETTARGETDEVICE, 0, $FFFFFF)
    EndIf
  EndIf
 
  resetModified()
  ProcedureReturn
EndProcedure
 
;-
Procedure fileNew()
  If saveModifiedFile() <> #IDCANCEL
    ClearGadgetItems(#EDIT1)
    fileName = untitled
    SetWindowTitle(#WIN1, appName + " - " + fileName)
  EndIf
EndProcedure
 
Procedure fileHome()
  ext.s = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
  path.s = OpenFileRequester("", GetHomeDirectory(), ext, 0)
  If path
    If saveModifiedFile() <> #IDCANCEL
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileOpen()
  If saveModifiedFile() <> #IDCANCEL
    ext.s = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    path.s = OpenFileRequester("Open File", fileOpenFolder, ext, 0)
    If path
      reLoad(#EDIT1, path, 0)
      ;process dot commands:
      processDotCommands()
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      saveRecentFiles(fileOpenFolder + fileName)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileInsert()
  ext.s = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
  path.s = OpenFileRequester("Insert into open file", fileInsertFolder, ext, 0)
  If path
    insertFile(#EDIT1, path)
    fileInsertFolder = GetPathPart(path)
  EndIf
EndProcedure
 
Procedure fileSave()
  If fileName = untitled
    If fileSaveAs() = #False
      ProcedureReturn #False
    EndIf
  Else
    reSave(#EDIT1, fileOpenFolder + fileName, 0)
  EndIf
  ProcedureReturn #True
EndProcedure
 
Procedure fileSaveAs()
  ext.s = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
  path.s = SaveFileRequester("Save File As", fileOpenFolder + fileName, ext, 0)
  If path
    If GetExtensionPart(path) = ""
      path + ".txt"
    EndIf
    reSave(#EDIT1, path, 0)
    fileName = GetFilePart(path)
    fileOpenFolder = GetPathPart(path)
    SetWindowTitle(#WIN1, appName + " - " + path)
  Else
    ProcedureReturn #False
  EndIf
  ProcedureReturn #True 
EndProcedure
 
Procedure fileSaveTo()
  ext.s = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
  path.s = SaveFileRequester("Save to the end of file", fileSavetoFolder, ext, 0)
  If path = fileSavetoFolder + fileName
    txt.s = "Can not save to the end of an open file"
    MessageRequester("Warning", txt, #MB_ICONWARNING)
  EndIf
  If path
    If OpenFile(0, path)
      ;goto end of the file
      FileSeek(0, Lof(0))
      ;write a newline
      WriteString(0, "")
      For line = 0 To CountGadgetItems(#EDIT1)
        WriteStringN(0, GetGadgetItemText(#EDIT1, line, 0))
      Next line
      CloseFile(0)
      fileSavetoFolder = GetPathPart(path)
    EndIf
  Else
    ProcedureReturn #False
  EndIf
  ProcedureReturn #True 
EndProcedure
 
Procedure fileRecent1()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent1
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileRecent2()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent2
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileRecent3()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent3
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileRecent4()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent4
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileRecent5()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent5
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileRecent6()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent6
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileRecent7()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent7
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure fileRecent8()
  If recent1 <> "[No file]"
    If saveModifiedFile() <> #IDCANCEL
      path.s = recent8
      reLoad(#EDIT1, path, 0)
      fileName = GetFilePart(path)
      fileOpenFolder = GetPathPart(path)
      SetWindowTitle(#WIN1, appName + " - " + path)
    EndIf
  EndIf
EndProcedure
 
Procedure filePrint()
  If fileSave()
    ShellExecute_(0, "print", fileOpenFolder + fileName, 0, 0, #SW_SHOWNORMAL)
    ;runProgram("notepad.exe", "/p " + fileOpenFolder + fileName, "")     
  EndIf
EndProcedure
 
Procedure fileExplore()
  ShellExecute_(0, "explore", fileOpenFolder, 0, 0, #SW_SHOWNORMAL)
EndProcedure
;-
 
Procedure editUndo()
  SendMessage_(GadgetID(#EDIT1), #WM_UNDO, 0, 0)
EndProcedure
 
Procedure editCut()
  If getSelTextLength() = 0
    ;no text selected, select all
    SendMessage_(GadgetID(#EDIT1), #EM_SETSEL, 0, -1)
  EndIf
  SendMessage_(GadgetID(#EDIT1), #WM_CUT, 0, 0)
EndProcedure
 
Procedure editCopy()
  If getSelTextLength() = 0
    ;no text selected, select all
    SendMessage_(GadgetID(#EDIT1), #EM_SETSEL, 0, -1)
  EndIf
  SendMessage_(GadgetID(#EDIT1), #WM_COPY, 0, 0)
EndProcedure
 
Procedure editPaste()
  ;sendMessage_(gadgetId(#EDIT1), #WM_PASTE, 0, 0)
  SendMessage_(GadgetID(#EDIT1), #EM_PASTESPECIAL, #CF_TEXT, 0)
EndProcedure
 
Procedure editInsertDate()
  date.s = dateStr()
  SetActiveGadget(#EDIT1)
  ;replace selected text, or insert at the caret position
  SendMessage_(GadgetID(#EDIT1), #EM_REPLACESEL, 1, @date)
EndProcedure
 
Procedure editCalculator()
  RunProgram("calc.exe")
EndProcedure
 
Procedure editInsertTime()
  time.s = timeStr()
  SetActiveGadget(#EDIT1)
  ;replace selected text, or insert at the caret position
  SendMessage_(GadgetID(#EDIT1), #EM_REPLACESEL, 1, @time)
EndProcedure
 
Procedure editFindReplace(id)
  flags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
  win = OpenWindow(#PB_Any, 0, 0, 500, 180, "Find/Replace", flags)
  If win
    StickyWindow(win, #True)
    TextGadget(#PB_Any, 10, 20, 100, 22, "Find What:")
    str1 = StringGadget(#PB_Any, 100, 15, 300, 25, "")
    btn1 = ButtonGadget(#PB_Any, 410, 15, 80, 26, "Find Next", #PB_Button_Default)
    DisableGadget(btn1, #True)
 
    TextGadget(#PB_Any, 10, 60, 100, 22, "Replace With:")
    str2 = StringGadget(#PB_Any, 100, 55, 300, 25, "")
    btn2 = ButtonGadget(#PB_Any, 410, 55, 80, 26, "Replace")
    DisableGadget(btn2, #True)
 
    btn3 = ButtonGadget(#PB_Any, 410, 95, 80, 26, "Replace all")
    DisableGadget(btn3, #True)
    btn4 = ButtonGadget(#PB_Any, 410, 135, 80, 26, "Cancel")
 
    chk1 = CheckBoxGadget(#PB_Any, 10, 100, 120, 26, "Match Case")
    chk2 = CheckBoxGadget(#PB_Any, 10, 140, 120, 26, "Whole Words")
    chk3 = CheckBoxGadget(#PB_Any, 135, 140, 160, 26, "From Top")
 
    ;get any selected text in the richedit
    SendMessage_(GadgetID(id), #EM_EXGETSEL, 0, @text.FINDTEXT\chrg)
    If (text\chrg\cpMin <> text\chrg\cpMax)
      ;selected text range found, check it's length
      len = text\chrg\cpMax - text\chrg\cpMin
      If len < 128
        char.c
        *txt = AllocateMemory((len + 1) * SizeOf(char))
        ;get the selected text
        SendMessage_(GadgetID(id), #EM_GETSELTEXT, 0, *txt)
        SetGadgetText(str1, PeekS(*txt))
        FreeMemory(*txt)
        ;focus the replace input field
        SetActiveGadget(str2)
        ;enable the buttons
        DisableGadget(btn1, #False)
        DisableGadget(btn2, #False)
        DisableGadget(btn3, #False)
      EndIf
    Else
      ;focus the findstring input field
      SetActiveGadget(str1)
    EndIf
 
    ;no text found yet:
    pos = -1
    ;default search flag
    flg | #FR_DOWN
 
    Repeat
      event = WaitWindowEvent()
      If EventWindow() = win
        Select event
          Case #PB_Event_Gadget
            Select EventGadget()
              Case str1
                ;find string
                GadgetToolTip(str1, GetGadgetText(str1))
                If Len(GetGadgetText(str1))
                  DisableGadget(btn1, #False)
                  DisableGadget(btn2, #False)
                  DisableGadget(btn3, #False)
                Else
                  DisableGadget(btn1, #True)
                  DisableGadget(btn2, #True)
                  DisableGadget(btn3, #True)
                EndIf
 
              Case str2
                ;replace string
                GadgetToolTip(str2, GetGadgetText(str2))
 
              Case btn1
                ;find button
                find.s = GetGadgetText(str1)
                text.FINDTEXT\lpstrText = @find
                ;get current position or selected range in the text
                SendMessage_(GadgetID(id), #EM_EXGETSEL, 0, @text\chrg)
                If (text\chrg\cpMin <> text\chrg\cpMax)
                  ;selected text range found, search from the end of it
                  text\chrg\cpMin = text\chrg\cpMax
                EndIf
                If GetGadgetState(chk3)
                  ;reset, search from the top
                  SetGadgetState(chk3, 0)
                  text\chrg\cpMin = 0
                EndIf
                ;search to the end of the text:
                text\chrg\cpMax = -1
                ;set the search flags
                flg = 0
                flg | #FR_DOWN
                If GetGadgetState(chk1)
                  flg | #FR_MATCHCASE
                EndIf
                If GetGadgetState(chk2)
                  flg | #FR_WHOLEWORD
                EndIf
                pos = SendMessage_(GadgetID(id), #EM_FINDTEXT, flg, @text)
                If pos <> -1
                  ;found, select the text range
                  SendMessage_(GadgetID(id), #EM_SETSEL, pos, pos + Len(find))
                Else
                  msg.s = "Cannot find " + find + Chr(13) + Chr(10)
                  msg + "Search again from the top ?"
                  flags = #MB_ICONQUESTION | #MB_YESNOCANCEL
                  Select MessageRequester("Find/Replace", msg, flags)
                    Case #IDYES
                      ;first position in the text
                      SendMessage_(GadgetID(id), #EM_SETSEL, 0, 0)
                      text\chrg\cpMin = 0
                      ;search to the end of the text:
                      text\chrg\cpMax = -1
                      pos = SendMessage_(GadgetID(id), #EM_FINDTEXT, flg, @text)
                      If pos <> -1
                        ;found, select the text range
                        SendMessage_(GadgetID(id), #EM_SETSEL, pos, pos + Len(find))
                      Else
                        msg.s = "Cannot find " + find
                        flags = #MB_ICONINFORMATION | #MB_OKCANCEL
                        Select MessageRequester("Find/Replace", msg, flags)
                          Case #IDCANCEL
                            Break
                        EndSelect
                      EndIf
                      SetActiveGadget(id)
                    Case #IDCANCEL
                      Break
                  EndSelect
                EndIf
 
              Case btn2
                ;replace button
                ;get selected range in the text
                SendMessage_(GadgetID(id), #EM_EXGETSEL, 0, @text\chrg)
                If (text\chrg\cpMin <> text\chrg\cpMax)
                  ;found selected text range
                  repl.s = GetGadgetText(str2)
                  If text\chrg\cpMax - text\chrg\cpMin = Len(GetGadgetText(str1))
                    ;same length as the find string, replace it
                    SendMessage_(GadgetID(id), #EM_REPLACESEL, 1, @repl)
                    ;advance the current position
                    text\chrg\cpMin + Len(repl)
                  EndIf
                Else
                  ;no selection made yet, search from the current position
                  text\chrg\cpMin = text\chrg\cpMax
                EndIf
                If GetGadgetState(chk3)
                  ;reset, search from the top
                  SetGadgetState(chk3, 0)
                  text\chrg\cpMin = 0
                EndIf
                ;search to the end of the text:
                text\chrg\cpMax = -1
                find.s = GetGadgetText(str1)
                text\lpstrText = @find
                ;set the search flags
                flg = 0
                flg | #FR_DOWN
                If GetGadgetState(chk1)
                  flg | #FR_MATCHCASE
                EndIf
                If GetGadgetState(chk2)
                  flg | #FR_WHOLEWORD
                EndIf
                pos = SendMessage_(GadgetID(id), #EM_FINDTEXT, flg, @text)
                If pos <> -1
                  ;found, select the text range
                  SendMessage_(GadgetID(id), #EM_SETSEL, pos, pos + Len(find))
                Else
                  msg.s = "Cannot find " + find + Chr(13) + Chr(10)
                  msg + "Search again from the top ?"
                  flags = #MB_ICONQUESTION | #MB_YESNOCANCEL
                  Select MessageRequester("Find/Replace", msg, flags)
                    Case #IDYES
                      ;first position in the text
                      SendMessage_(GadgetID(id), #EM_SETSEL, 0, 0)
                      text\chrg\cpMin = 0
                      ;search to the end of the text:
                      text\chrg\cpMax = -1
                      pos = SendMessage_(GadgetID(id), #EM_FINDTEXT, flg, @text)
                      If pos <> -1
                        ;found, select the text range
                        SendMessage_(GadgetID(id), #EM_SETSEL, pos, pos + Len(find))
                      Else
                        msg.s = "Cannot find " + find
                        flags = #MB_ICONINFORMATION | #MB_OKCANCEL
                        Select MessageRequester("Find/Replace", msg, flags)
                          Case #IDCANCEL
                            Break
                        EndSelect
                      EndIf
                    Case #IDCANCEL
                      Break
                  EndSelect
                EndIf
 
              Case btn3
                ;replace all button
                Repeat
                  ;get selected range in the text
                  SendMessage_(GadgetID(id), #EM_EXGETSEL, 0, @text\chrg)
                  If (text\chrg\cpMin <> text\chrg\cpMax)
                    ;found selected text range
                    repl.s = GetGadgetText(str2)
                    If text\chrg\cpMax - text\chrg\cpMin = Len(GetGadgetText(str1))
                      ;same length as the find string, replace it
                      SendMessage_(GadgetID(id), #EM_REPLACESEL, 1, @repl)
                      ;advance the current position
                      text\chrg\cpMin + Len(repl)
                    EndIf
                  Else
                    ;no selection made yet, search from the current position
                    text\chrg\cpMin = text\chrg\cpMax
                  EndIf
                  If GetGadgetState(chk3)
                    ;reset, search from the top
                    SetGadgetState(chk3, 0)
                    text\chrg\cpMin = 0
                  EndIf
                  ;search to the end of the text:
                  text\chrg\cpMax = -1
                  find.s = GetGadgetText(str1)
                  text\lpstrText = @find
                  ;set the search flags
                  flg = 0
                  flg | #FR_DOWN
                  If GetGadgetState(chk1)
                    flg | #FR_MATCHCASE
                  EndIf
                  If GetGadgetState(chk2)
                    flg | #FR_WHOLEWORD
                  EndIf
                  pos = SendMessage_(GadgetID(id), #EM_FINDTEXT, flg, @text)
                  If pos <> -1
                    ;found, select the text range
                    SendMessage_(GadgetID(id), #EM_SETSEL, pos, pos + Len(find))
                  EndIf
                Until pos = -1
 
                msg.s = "Cannot find " + find + Chr(13) + Chr(10)
                msg + "Search again from the top ?"
                flags = #MB_ICONQUESTION | #MB_YESNOCANCEL
                Select MessageRequester("Find/Replace", msg, flags)
                  Case #IDYES
                    ;first position in the text
                    SendMessage_(GadgetID(id), #EM_SETSEL, 0, 0)
                    text\chrg\cpMin = 0
                    ;search to the end of the text:
                    text\chrg\cpMax = -1
                    pos = SendMessage_(GadgetID(id), #EM_FINDTEXT, flg, @text)
                    If pos <> -1
                      ;found, select the text range
                      SendMessage_(GadgetID(id), #EM_SETSEL, pos, pos + Len(find))
                    Else
                      msg.s = "Cannot find " + find
                      flags = #MB_ICONINFORMATION | #MB_OKCANCEL
                      Select MessageRequester("Find/Replace", msg, flags)
                        Case #IDCANCEL
                          Break
                      EndSelect
                    EndIf
                  Case #IDCANCEL
                    Break
                EndSelect
 
              Case btn4
                ;cancel button
                Break
            EndSelect
          Case #PB_Event_CloseWindow
            Break
        EndSelect
      EndIf
    ForEver
    CloseWindow(win)
  EndIf
EndProcedure
 
;-
Procedure toolsSearch()
  ;reset topmost if opening another program
  SetMenuItemState(#MENU, #SETUP_TOPMOST, 0)
  StickyWindow(#WIN1, 0)
  ;get any selected text in the richedit
  SendMessage_(GadgetID(#EDIT1), #EM_EXGETSEL, 0, @text.FINDTEXT\chrg)
  If (text\chrg\cpMin <> text\chrg\cpMax)
    ;selected text range found, check it's length
    len = text\chrg\cpMax - text\chrg\cpMin
    If len < 128
      char.c
      *txt = AllocateMemory((len + 1) * SizeOf(char))
      ;get the selected text
      SendMessage_(GadgetID(#EDIT1), #EM_GETSELTEXT, 0, *txt)
      ;open the site and search for the selected text
      RunProgram("http://www.google.com/search?q=" + PeekS(*txt) + "&btnG")
      FreeMemory(*txt)
    EndIf
  Else
    ;no selected text, open the site
    RunProgram("http://www.google.com")
  EndIf
EndProcedure
 
Procedure toolsCharmap()
  RunProgram("charmap.exe")
EndProcedure
 
Procedure toolsCalculator()
  RunProgram("calc.exe")
EndProcedure
 
Procedure toolsCalendar()
  date.s = toolsDatePicker(#WIN1)
  SetActiveGadget(#EDIT1)
  If Len(date)
    ;replace selected text, or insert at the caret position
    SendMessage_(GadgetID(#EDIT1), #EM_REPLACESEL, 1, @date)
  EndIf
EndProcedure
 
Procedure toolsAddress()
  RunProgram("wab.exe")
EndProcedure
 
Procedure toolsCommand()
  RunProgram("cmd.exe", "/T:F0", fileOpenFolder)
EndProcedure
 
Procedure.s toolsDatePicker(parent)
  flags = #PB_Window_ScreenCentered  | #PB_Window_SystemMenu
  win = OpenWindow(#PB_Any, 0, 0, 300, 220, "Date Picker", flags)
  If win
    EnableWindow_(WindowID(parent), #False)
    StickyWindow(win, #True)
    ResizeWindow(win, #PB_Ignore, WindowY(win) - 50, #PB_Ignore, #PB_Ignore) 
 
    DateGadget(#DATE1, 0, 0, 300, 24)
    btn1 = ButtonGadget(#PB_Any, 120, 185, 80, 24, "Insert")
    btn2 = ButtonGadget(#PB_Any, 210, 185, 80, 24, "Cancel" )
 
    Repeat
      event = WaitWindowEvent()
      Select event
        Case #PB_Event_Gadget
          Select EventGadget()
            Case btn1
              date.s = GetGadgetText(#DATE1)
              exit = #True
            Case btn2
              date.s = ""
              exit = #True
          EndSelect
        Case #PB_Event_CloseWindow
          exit = #True
      EndSelect
    Until exit
 
    CloseWindow(win)
  EndIf
  EnableWindow_(WindowID(parent), #True)
  SetActiveWindow(parent)
  ProcedureReturn date
EndProcedure
 
;-
Procedure setupFont()
  If FontRequester(fontName, fontSize, #PB_FontRequester_Effects)
    fontName = SelectedFontName()
    fontSize = SelectedFontSize()
    fontColor = SelectedFontColor()
    SetGadgetColor(#EDIT1, #PB_Gadget_FrontColor, fontColor)
    LoadFont(0, fontName, fontSize, #PB_Font_HighQuality)
    SetGadgetFont(#EDIT1, FontID(0))
    SetGadgetFont(#EDIT1, FontID(0))
  EndIf
EndProcedure
 
Procedure setupTopmost()
  If GetMenuItemState(#MENU, #SETUP_TOPMOST)
    SetMenuItemState(#MENU, #SETUP_TOPMOST, 0)
    topmost = 0
    StickyWindow(#WIN1, 0)
  Else
    SetMenuItemState(#MENU, #SETUP_TOPMOST, 1)
    topmost = 1
    StickyWindow(#WIN1, 1)
  EndIf
EndProcedure
 
Procedure setupWordwrap()
  If GetMenuItemState(#MENU, #SETUP_WORDWRAP)
    SetMenuItemState(#MENU, #SETUP_WORDWRAP, 0)
    wordwrap = 0
    ;disable wordwrap
    SendMessage_(GadgetID(#EDIT1), #EM_SETTARGETDEVICE, 0, $FFFFFF)
  Else
    SetMenuItemState(#MENU, #SETUP_WORDWRAP, 1)
    wordwrap = 1
    ;enable wordwrap
    SendMessage_(GadgetID(#EDIT1), #EM_SETTARGETDEVICE, 0, 0)
  EndIf
EndProcedure
 
Procedure setupSmaller()
  setInnerSize(GetSystemMetrics_(#SM_CXSCREEN) * 0.40, 0)
  setInnerSize(0, GetSystemMetrics_(#SM_CYSCREEN) * 0.40)
EndProcedure
 
Procedure setupNormal()
  setInnerSize(GetSystemMetrics_(#SM_CXSCREEN) * 0.6, 0)
  setInnerSize(0, GetSystemMetrics_(#SM_CYSCREEN) * 0.6)
EndProcedure
 
Procedure setupLarger()
  setInnerSize(GetSystemMetrics_(#SM_CXSCREEN) * 0.8, 0)
  setInnerSize(0, GetSystemMetrics_(#SM_CYSCREEN) * 0.8)
EndProcedure
 
Procedure setupColor()
  color = ColorRequester(RGB(216, 234, 255))
  If color <> -1 
    backColor = color
    SetGadgetColor(#EDIT1, #PB_Gadget_BackColor, backColor)
  EndIf
EndProcedure
 
Procedure setupDesktop()
  res.s = ProgramFilename()
  url.s = getSpecialFolder(#CSIDL_DESKTOPDIRECTORY) + appName
  If createShortcut(#True, res, url, res, 0)
    MessageRequester(appName, "Shortcut created", #MB_ICONINFORMATION)
  EndIf
EndProcedure
 
Procedure setupStartmenu()
  res.s = ProgramFilename()
  url.s = getSpecialFolder(#CSIDL_PROGRAMS) + appName
  If createShortcut(#True, res, url, res, 0)
    MessageRequester(appName, "Shortcut created", #MB_ICONINFORMATION)
  EndIf
EndProcedure
 
;-
Procedure helpHtm()
EndProcedure
 
Procedure helpPdf()
EndProcedure
 
 
Procedure helpAbout()
  ;about html
  html.s = "<html><body"
  html + " bgcolor = 'white' text = 'gray' link = 'blue' vlink = 'blue' alink = 'blue'"
  html + " onContextMenu = 'javascript:return false;' scroll = 'no'>"
  html + "<center>"
  html + "<p><a href = 'http://www.google.com'>" + appName + " program home</a>"
  html + "<p>programming"
  html + "<p><a href = 'http://www.google.com'>u n d e r c o n st r u c t i o n</a>"
  html + "<p>programming"
  html + "<p><a href = 'http://www.google.com'>u n d e r c o n st r u c t i o n</a>"
  html + "</body></html>"
 
  about(0, html)
EndProcedure
 
Procedure about(parent, html.s)
  flags = #PB_Window_ScreenCentered  | #PB_Window_SystemMenu
  win = OpenWindow(#PB_Any, 0, 0, 412, 445,  "About " + appName, flags)
  If win
    EnableWindow_(WindowID(parent), #False)
    ResizeWindow(win, #PB_Ignore, WindowY(win) - 50, #PB_Ignore, #PB_Ignore)
    StickyWindow(win, #True)
 
    ;about html
    web1 = WebGadget(#PB_Any, 0, 0, 412, 389, "")
    SetGadgetItemText(web1, #PB_Web_HtmlCode, html)
 
    ;divider line
    Frame3DGadget(#PB_Any, 0, 390, 412, 2, "", #PB_Frame3D_Single)
 
    ;ok button
    btn1 = ButtonGadget(#PB_Any, 156, 405, 100, 26, "Ok", #PB_Button_Default)
    Repeat
      event = WaitWindowEvent()
      Select event
        Case #PB_Event_Gadget
          Select EventGadget()
            Case btn1
              exit = #True
          EndSelect
        Case #PB_Event_CloseWindow
          exit = #True
      EndSelect
    Until exit
 
    CloseWindow(win)
  EndIf
  EnableWindow_(WindowID(parent), #True)
  SetActiveWindow(parent)
EndProcedure
 
Procedure.s dateStr()
  date.s = Space(12)
  GetDateFormat_(#LOCALE_USER_DEFAULT, 0, 0, 0, date, 12)
  ProcedureReturn date
EndProcedure
 
Procedure.s timeStr()
  date.s = Space(12)
  GetTimeFormat_(#LOCALE_USER_DEFAULT, 0, 0, 0, date, 12)
  ProcedureReturn date
EndProcedure
 
Procedure reLoad(id, name.s, rtf)
  ;richedit streamin
  lenF = FileSize(name)
  If lenF
    fileH = ReadFile(#PB_Any, name)
    If lenF And fileH
      stream.EDITSTREAM
      stream\dwCookie = lenF
      stream\pfnCallback = @reLoadSaveCallback()
      If rtf
        ;rich text format
        SendMessage_(GadgetID(id), #EM_STREAMIN, #SF_RTF, @stream)
      Else
        ;text format
        SendMessage_(GadgetID(id), #EM_STREAMIN, #SF_TEXT, @stream)
      EndIf
      CloseFile(fileH)
      resetModified()
      ;go to top of document
      SendMessage_(GadgetID(id), #EM_SETSEL, 0, 0)
      ProcedureReturn 1
    EndIf
  EndIf
  ProcedureReturn 0
EndProcedure
 
Procedure reSave(id, name.s, rtf)
  ;richedit streamout
  fileH = CreateFile(#PB_Any, name)
  If fileH
    stream.EDITSTREAM
    stream\dwCookie = 0
    stream\pfnCallback = @reLoadSaveCallback()
    If rtf
      ;rich text format
      SendMessage_(GadgetID(id), #EM_STREAMOUT, #SF_RTF, @stream)
    Else
      ;text format
      SendMessage_(GadgetID(id), #EM_STREAMOUT, #SF_TEXT, @stream)
    EndIf
    CloseFile(fileH)
    resetModified()
    ProcedureReturn 1
  EndIf
  ProcedureReturn 0
EndProcedure
 
Procedure reLoadSaveCallback(lenF, *pbBuff, cb, *pcb.LONG)
  ;stream io callback
  If lenF = 0
    ;save
    WriteData(fileH, *pbBuff, cb)
    *pcb\l = cb
  Else
    ;load
    If lenF > cb
      *pcb\l = ReadData(fileH, *pbBuff, cb)
    Else
      *pcb\l = ReadData(fileH, *pbBuff, lenF)
    EndIf
  EndIf
  ProcedureReturn 0
EndProcedure
 
Procedure editCallback(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_PAINT
      CallWindowProc_(editProc, hWnd, uMsg, wParam, lParam)
      drawLineNumbers()
      ProcedureReturn 0
  EndSelect
  ProcedureReturn CallWindowProc_(editProc, hWnd, uMsg, wParam, lParam)
EndProcedure
 
Procedure drawLinenumbers()
  id = GadgetID(#EDIT1)
  hdc = GetDCEx_(id, 0, #DCX_CACHE)
 
  hFont = FontID(GetGadgetFont(#EDIT1))
  prevObj = SelectObject_(hDC, hFont)
 
  GetTextMetrics_(hDC, @tm.TEXTMETRIC)
  fontH = tm\tmHeight
 
  GetClientRect_(id, rc.RECT)
  rc\right = leftM - 8
  FillRect_(hDC, rc, GetStockObject_(#WHITE_BRUSH))
  SetBkMode_(hDC, #TRANSPARENT)
  SetTextColor_(hDC, $C0C0C0)
 
  ;get the first line
  first = SendMessage_(id, #EM_GETFIRSTVISIBLELINE, 0, 0)
 
  ;get the last character/line 
  pt.POINT\x = rc\left
  pt\y = rc\bottom
  lastChar = SendMessage_(id, #EM_CHARFROMPOS, 0, pt)
  last = SendMessage_(id, #EM_LINEFROMCHAR, lastChar, 0)
  ; get the first character
  pt\y = rc\top
  firstChar = SendMessage_(id, #EM_CHARFROMPOS, 0, pt)
 
  ;get the client coordinates fo the first character
  SendMessage_(id, #EM_POSFROMCHAR, @tChar.POINT, firstChar)
  ;adjust linenumber RECT
  rc\top = tChar\y
 
  For line = first To last
    DrawText_(hDc, Str(line + 1), -1, rc, #DT_RIGHT)
    rc\top + fontH
  Next
  SelectObject_(hDC, prevObj)
  ReleaseDC_(id, hDC)
EndProcedure
 
Procedure saveRecentFiles(path.s)
  recent8 = GetMenuItemText(#MENU, #FILE_RECENT7)
  SetMenuItemText(#MENU, #FILE_RECENT8, recent8)
 
  recent7 = GetMenuItemText(#MENU, #FILE_RECENT6)
  SetMenuItemText(#MENU, #FILE_RECENT7, recent7)
 
  recent6 = GetMenuItemText(#MENU, #FILE_RECENT5)
  SetMenuItemText(#MENU, #FILE_RECENT6, recent6)
 
  recent5 = GetMenuItemText(#MENU, #FILE_RECENT4)
  SetMenuItemText(#MENU, #FILE_RECENT5, recent5)
 
  recent4 = GetMenuItemText(#MENU, #FILE_RECENT3)
  SetMenuItemText(#MENU, #FILE_RECENT4, recent4)
 
  recent3 = GetMenuItemText(#MENU, #FILE_RECENT2)
  SetMenuItemText(#MENU, #FILE_RECENT3, recent3)
 
  recent2 = GetMenuItemText(#MENU, #FILE_RECENT1)
  SetMenuItemText(#MENU, #FILE_RECENT2, recent2)
 
  recent1 = path
  SetMenuItemText(#MENU, #FILE_RECENT1, recent1)
EndProcedure
 
Procedure insertFile(id, path.s)
  If ReadFile(0, path)
    line = getLinenumber(id)
    While Eof(0) = 0
      AddGadgetItem(id, line, ReadString(0))
      line + 1
    Wend
    CloseFile(0)
  Else
    ProcedureReturn #False
  EndIf
  ProcedureReturn #True
EndProcedure
 
Procedure saveModifiedFile()
  If isModified()
    Select MessageRequester("Save modified file?", "Save " + fileName + "?", #MB_YESNOCANCEL | #MB_ICONQUESTION)
      Case #IDYES
        If fileSave() = #False
          ProcedureReturn #IDCANCEL
        EndIf
      Case #IDCANCEL
        ProcedureReturn #IDCANCEL
    EndSelect
  EndIf
  ProcedureReturn #True
EndProcedure
 
Procedure isModified()
  ;query the editorgadget for modified text since last reset
  result = SendMessage_(GadgetID(#EDIT1), #EM_GETMODIFY, 0, 0)
  ProcedureReturn result
EndProcedure
 
Procedure resetModified()
  ;reset editorgadget modify flag
  SendMessage_(GadgetID(#EDIT1), #EM_SETMODIFY, #False, 0)
EndProcedure
 
Procedure getLinenumber(id)
  ;gets the linenumber in an editorgadget from the caret position
  SetActiveGadget(id)
  GetCaretPos_(@pos.point)
  char = SendMessage_(GadgetID(id), #EM_CHARFROMPOS, 0, @pos)
  line =  SendMessage_(GadgetID(id), #EM_LINEFROMCHAR, char, 0)
  ProcedureReturn line
EndProcedure
 
Procedure getSelTextlength()
  SendMessage_(GadgetID(#EDIT1), #EM_EXGETSEL, 0, @text.FINDTEXT\chrg)
  ProcedureReturn text\chrg\cpMax - text\chrg\cpMin
EndProcedure
 
Procedure.s getProgramName(exeName.s)
  ;extracts an application name from an exe name on the form 'c..n...exe'
  ;where c.. are letters and n.. version numbers
  exeName = Left(exeName, FindString(exeName, ".", 1) - 1)
  For i = 1 To Len(exeName)
    C.s = Mid(exeName, i, 1)
    If c < "0" Or c > "9"
      name.s + c
    EndIf
  Next i
  ProcedureReturn RTrim(name)
EndProcedure
 
Procedure setInnerSize(wdt, hgt)
  ;sets editorgadget size to w, h.
  winW = WindowWidth(#WIN1)
  winH = WindowHeight(#WIN1)
  If GetWindowState(#WIN1) = #PB_Window_Maximize
    SetWindowState(#WIN1, #PB_Window_Normal)
    ;or the size gadget disappears!?
  EndIf
  If wdt
    ResizeWindow(#win1, #PB_Ignore, #PB_Ignore, wdt, #PB_Ignore)
  EndIf
  If hgt
    ResizeWindow(#WIN1, #PB_Ignore, #PB_Ignore, #PB_Ignore, hgt)
  EndIf
  ;center at top
  l = (GetSystemMetrics_(#SM_CXSCREEN) - WindowWidth(#WIN1)) / 2
  ResizeWindow(#WIN1, l, 0, #PB_Ignore, #PB_Ignore)
EndProcedure
 
Procedure createShortcut(file, res.s, url.s, icon.s, index)
  ;creates internet shortcuts. res: linked to, url: shortcut path
  ;icon: the path of the icon library file, .ico, .dll or .exe
  ;index: the icon index within the icon library file
  dq.s = Chr(34)
  If file
    ;links to a file
    res = "file://" + res
  Else
    ;links to a web resource
    res = "http://" + res
  EndIf
  If LCase(Right(url, 4)) <> ".url"
    url = url + ".url"
  EndIf
  ;try to create folders from the top down
  For i = 1 To Len(url)
    If Mid(url, i, 1) = "\"
      ;create folder:
      CreateDirectory(Left(url, i))   
    EndIf
  Next
  If CreateFile(0, url)
    WriteStringN(0, "[InternetShortcut]")
    WriteStringN(0, "URL = " + dq + res + dq)
    If file
      If Len(icon)
        WriteStringN(0, "IconFile = " + dq + icon + dq)
        WriteStringN(0, "IconIndex = " + dq + Str(index) + dq)
      EndIf
    EndIf
    CloseFile(0)
    ProcedureReturn 1
  EndIf
  ProcedureReturn 0
EndProcedure
 
Procedure.s getSpecialFolder(id)
  Protected path.s, *ItemId.ITEMIDLIST
  *itemId = 0
  If SHGetSpecialFolderLocation_(0, id, @*ItemId) = #NOERROR
    path = Space(#MAX_PATH)
    If SHGetPathFromIDList_(*itemId, @path)
      If Right(path, 1) <> "\"
        path + "\"
      EndIf
      ProcedureReturn path
    EndIf
  EndIf
  ProcedureReturn ""
EndProcedure
 
Procedure processCmdLine()
  ;process command line parameters
  If CountProgramParameters()
    ;open a doubleclicked file
    path.s = ProgramParameter()
    MessageRequester("", path)
    reLoad(#EDIT1, path, 0)
    ;    load(#EDIT1, path)             
    ;process dot commands:
    processDotCommands()
    fileName = GetFilePart(path)
    fileOpenFolder = GetPathPart(path)
    saveRecentFiles(fileOpenFolder + fileName)
    SetWindowTitle(#WIN1, appName + " - " + path)
  EndIf
EndProcedure
 
Procedure processDotCommands()
  ;process dot commands:
  cmd.s = UCase(GetGadgetItemText(#EDIT1, 0, 0))
  Select cmd
    Case ".LOG"
      ;append a date/time entry
      dt.s = #CR$ + dateStr() + ", " + timeStr() + #CR$
      SendMessage_(GadgetID(#EDIT1), #EM_SETSEL, -1, -1)
      SendMessage_(GadgetID(#EDIT1), #EM_REPLACESEL, 1, @dt)
  EndSelect
EndProcedure
 
Procedure loadPreferences(inFolder.s, name.s)
  #CSIDL_APPDATA = $1A
  ;   XP:     C:\Documents and Settings\username\Application Data*   
  ;   Vista:  C:\Users\username\AppData\Roaming
  If Len(inFolder)
    inFolder + "\"
  EndIf
  path.s = GetPathPart(ProgramFilename())
  type = GetDriveType_(Left(path, 2))
  If type = 2
    ;removable
    path = path + inFolder + name
  Else
    path = getSpecialFolder(#CSIDL_APPDATA) + inFolder + name
  EndIf
  OpenPreferences(path)
  PreferenceGroup("metrics")
  maximized = ReadPreferenceLong("maximized", 0)
  w = GetSystemMetrics_(#SM_CXSCREEN) - 100
  h = GetSystemMetrics_(#SM_CYSCREEN) - 100
  windowW = ReadPreferenceLong("windowW", w)
  windowH = ReadPreferenceLong("windowH", h)
  PreferenceGroup("files")
  fileOpenFolder = ReadPreferenceString("fileOpenFolder", GetPathPart(GetHomeDirectory()))
  fileInsertFolder = ReadPreferenceString("fileInsertFolder", GetPathPart(GetHomeDirectory()))
  fileSavetoFolder = ReadPreferenceString("fileSavetoFolder", GetPathPart(GetHomeDirectory()))
  PreferenceGroup("setup")
  fontName = ReadPreferenceString("fontName", "tahoma")
  fontSize = ReadPreferenceLong("fontSize", 11)
  fontColor = ReadPreferenceLong("fontColor", #Black)
  backColor = ReadPreferenceLong("backColor", RGB(216, 234, 255))
  topmost = ReadPreferenceLong("topmost", 0)
  wordwrap = ReadPreferenceLong("wordwrap", 0)   
  PreferenceGroup("recent files")
  recent1 = ReadPreferenceString("recent1", "[No file]")
  recent2 = ReadPreferenceString("recent2", "[No file]")
  recent3 = ReadPreferenceString("recent3", "[No file]")
  recent4 = ReadPreferenceString("recent4", "[No file]")
  recent5 = ReadPreferenceString("recent5", "[No file]")
  recent6 = ReadPreferenceString("recent6", "[No file]")
  recent7 = ReadPreferenceString("recent7", "[No file]")
  recent8 = ReadPreferenceString("recent8", "[No File]")
  ClosePreferences()
EndProcedure
 
Procedure savePreferences(inFolder.s, name.s)
  #CSIDL_APPDATA = $1A
  ;   XP:     C:\Documents and Settings\username\Application Data*   
  ;   Vista:  C:\Users\username\AppData\Roaming
  If Len(inFolder)
    inFolder + "\"
  EndIf
  path.s = GetPathPart(ProgramFilename())
  type = GetDriveType_(Left(path, 2))
  If type = 2
    ;removable
    path = path + inFolder
  Else
    path = getSpecialFolder(#CSIDL_APPDATA) + inFolder
  EndIf
  CreateDirectory(path)
  If CreatePreferences(path + name)
    PreferenceGroup("metrics")
    WritePreferenceLong("maximized", maximized)
    WritePreferenceLong("windowW", windowW)
    WritePreferenceLong("windowH", windowH)
    PreferenceGroup("files")
    WritePreferenceString("fileOpenFolder", fileOpenFolder)
    WritePreferenceString("fileInsertFolder", fileInsertFolder)
    WritePreferenceString("fileSavetoFolder", fileSavetoFolder)
    PreferenceGroup("setup")
    WritePreferenceString("fontName", fontName)
    WritePreferenceLong("fontSize", fontSize)
    WritePreferenceLong("fontColor", fontColor)
    WritePreferenceLong("backColor", backColor)
    WritePreferenceLong("topmost", topmost)
    WritePreferenceLong("wordwrap", wordwrap)
    PreferenceGroup("recent files")
    WritePreferenceString("recent1", recent1)
    WritePreferenceString("recent2", recent2)
    WritePreferenceString("recent3", recent3)
    WritePreferenceString("recent4", recent4)
    WritePreferenceString("recent5", recent5)
    WritePreferenceString("recent6", recent6)
    WritePreferenceString("recent7", recent7)
    WritePreferenceString("recent8", recent8)
    ClosePreferences()
  EndIf
EndProcedure
__________________
Vous souhaitez participer à la rubrique PureBasic (tutoriels, FAQ, sources) ? Contactez-moi par MP.
comtois est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 05h24.


 
 
 
 
Partenaires

Hébergement Web