-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtmpNote.py
executable file
Β·1253 lines (1058 loc) Β· 45.9 KB
/
tmpNote.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.lib.agw.flatnotebook as fnb
import wx.stc as stc
import keyword
import os
import datetime
import webbrowser
from tmpNoteIcon import gettmpNoteIconIcon
__author__ = 'jmg1138'
__email__ = 'jmg1138@tmpnote.com'
__copyright__ = 'Copyright tmpNote.com'
__license__ = 'MIT'
__version__ = '0.0.7'
class FlatNotebook(fnb.FlatNotebook):
"""Create a FlatNotebook."""
def __init__(self, parent):
"""Define the initialization behavior of the FlatNotebook."""
fnb.FlatNotebook.__init__(
self,
parent=parent,
id=wx.ID_ANY,
agwStyle=(
fnb.FNB_NO_TAB_FOCUS | fnb.FNB_X_ON_TAB |
fnb.FNB_NAV_BUTTONS_WHEN_NEEDED | fnb.FNB_HIDE_ON_SINGLE_TAB |
fnb.FNB_RIBBON_TABS
)
)
self.SetTabAreaColour((100, 100, 100))
self.SetActiveTabTextColour((0, 0, 0))
self.SetNonActiveTabTextColour((0, 0, 0))
self.right_click_menu()
self.custom_page()
def right_click_menu(self):
"""Create a right-click menu for each FlatNotebook page tab."""
menu = wx.Menu()
self.SetRightClickMenu(menu)
menu.Append(wx.ID_CLOSE, 'Close', 'Close')
self.Bind(wx.EVT_MENU, self.close, id=wx.ID_CLOSE)
def close(self, event):
"""Close the selected FlatNotebook page tab."""
# Try deleting the currently selected notebook page.
# This will trigger the EVT_FLATBOOK_PAGE_CLOSING event.
# That event is bound to self.closing_file_event.
self.DeletePage(self.GetSelection())
def custom_page(self):
"""A page to display when all FlatNotebook page tabs are closed."""
panel = wx.Panel(self)
font = wx.Font(9, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
panel.SetFont(font)
panel.SetBackgroundColour((34, 34, 34))
panel.SetForegroundColour((255, 255, 255))
# wx.StaticText(panel, -1, '\n\n\nSomething here later?')
self.SetCustomPage(panel)
class TxtCtrl(stc.StyledTextCtrl):
"""Create a StyledTextCtrl."""
def __init__(self, parent, text, readonly):
"""Define the initialization behavior of the StyledTextCtrl."""
stc.StyledTextCtrl.__init__(self, parent, wx.ID_ANY)
self.SetText(text)
self.SetReadOnly(readonly) # bool
self.Bind(stc.EVT_STC_MARGINCLICK, self.margin_click)
# The following methods of margin_click, fold_all, and expand, are
# copied from the demo. I haven't done anything special here.
def margin_click(self, event):
"""Take proper action when a margin is clicked by the operator."""
# Action for the folding symbols margin.
if event.GetMargin() == 2:
if event.GetShift() and event.GetControl():
self.fold_all()
else:
lineClicked = self.LineFromPosition(event.GetPosition())
if (
self.GetFoldLevel(lineClicked) and
stc.STC_FOLDLEVELHEADERFLAG
):
if event.GetShift():
self.SetFoldExpanded(lineClicked, True)
self.expand(lineClicked, True, True, 1)
elif event.GetControl():
if self.GetFoldExpanded(lineClicked):
self.SetFoldExpanded(lineClicked, False)
self.expand(lineClicked, False, True, 0)
else:
self.SetFoldExpanded(lineClicked, True)
self.expand(lineClicked, True, True, 100)
else:
self.ToggleFold(lineClicked)
else:
event.Skip()
def fold_all(self):
"""Fold or unfold all folding symbols."""
lineCount = self.GetLineCount()
expanding = True
# find out if folding or unfolding
for lineNum in range(lineCount):
if self.GetFoldLevel(lineNum) &\
stc.STC_FOLDLEVELHEADERFLAG:
expanding = not self.GetFoldExpanded(lineNum)
break
lineNum = 0
while lineNum < lineCount:
level = self.GetFoldLevel(lineNum)
if (
level & stc.STC_FOLDLEVELHEADERFLAG and
(level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE
):
if expanding:
self.SetFoldExpanded(lineNum, True)
lineNum = self.expand(lineNum, True)
lineNum = lineNum - 1
else:
lastChild = self.GetLastChild(lineNum, -1)
self.SetFoldExpanded(lineNum, False)
if lastChild > lineNum:
self.HideLines(lineNum + 1, lastChild)
lineNum = lineNum + 1
def expand(self, line, doexpand, force=False, visLevels=0, level=-1):
"""Unfold folding symbols."""
lastChild = self.GetLastChild(line, level)
line = line + 1
while line <= lastChild:
if force:
if visLevels > 0:
self.ShowLines(line, line)
else:
self.HideLines(line, line)
else:
if doexpand:
self.ShowLines(line, line)
if level == -1:
level = self.GetFoldLevel(line)
if level & stc.STC_FOLDLEVELHEADERFLAG:
if force:
if visLevels > 1:
self.SetFoldExpanded(line, True)
else:
self.SetFoldExpanded(line, False)
line = self.expand(line, doexpand, force, visLevels - 1)
else:
if doexpand and self.GetFoldExpanded(line):
line = self.expand(line, True, force, visLevels - 1)
else:
line = self.expand(line, False, force, visLevels - 1)
else:
line = line + 1
return line
class TmpNote(wx.Frame):
"""Use wx.Frame to create the graphical user interface."""
def __init__(self, parent):
"""Define the initialization behavior of the wx.Frame."""
super(TmpNote, self).__init__(
parent=parent,
id=wx.ID_ANY,
title=' tmpNote ',
size=(600, 400),
style=wx.DEFAULT_FRAME_STYLE
)
user_interface = self.ui()
self.Show()
def ui(self):
"""Assemble the pieces of the Graphical User Interface."""
self.Bind(wx.EVT_CLOSE, self.destroyer_event)
self.Bind(fnb.EVT_FLATNOTEBOOK_PAGE_CHANGED, self.page_changed_event)
self.Bind(fnb.EVT_FLATNOTEBOOK_PAGE_CLOSING, self.closing_file_event)
self.SetIcon(gettmpNoteIconIcon())
self.menu_bar()
self.status_bar()
panel = wx.Panel(self)
panel.SetBackgroundColour((34, 34, 34))
# List to contain the FlatNotebook pages as they are created.
# The objects in this list will be StyledTextCtrl objects.
self.pages = []
self.notebook = FlatNotebook(panel)
first_page = self.new_file()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.ALL | wx.EXPAND, 0)
panel.SetSizerAndFit(sizer)
panel.Layout()
def menu_bar(self):
"""Create a main menu bar."""
menubar = wx.MenuBar()
self.SetMenuBar(menubar)
# Stock IDs: http://wiki.wxpython.org/SpecialIDs
# For menu items that do not use Stock IDs / Special IDs, use the
# following ranges instead:
# +-------------+-------------+-----------+
# | Menu name | Range Start | Range End |
# +-------------+-------------+-----------+
# | File | 100 | 199 |
# | Edit | 200 | 299 |
# | Find | 300 | 399 |
# | View | 400 | 499 |
# | Preferences | 500 | 599 |
# | Help | 900 | 999 |
# +-------------+-------------+-----------+
# How to reserve these ID ranges so that something else doesn't use
# one of these IDs before the menu does?
filemenu = wx.Menu()
menubar.Append(filemenu, '&File')
filemenu.Append(wx.ID_NEW, '&New File', 'Begin a new file.')
self.Bind(wx.EVT_MENU, self.new_file_event, id=wx.ID_NEW)
filemenu.Append(wx.ID_OPEN, '&Open File\tCtrl+O',
'Open an existing file.')
self.Bind(wx.EVT_MENU, self.open_file_event, id=wx.ID_OPEN)
filemenu.Append(wx.ID_SAVE, '&Save\tCtrl+S',
'Save using the current file name.')
self.Bind(wx.EVT_MENU, self.save_file_event, id=wx.ID_SAVE)
filemenu.Append(wx.ID_SAVEAS, 'Save &As',
'Save using a different file name.')
self.Bind(wx.EVT_MENU, self.save_file_event, id=wx.ID_SAVEAS)
filemenu.Append(wx.ID_CLOSE, '&Close File', 'Close the current file.')
self.Bind(wx.EVT_MENU, self.close_file_event, id=wx.ID_CLOSE)
filemenu.Append(wx.ID_CLOSE_ALL, 'Close All Files',
'Close all open files.')
self.Bind(wx.EVT_MENU, self.close_all_event, id=wx.ID_CLOSE_ALL)
# Print was removed for now until I can find a way to print with cross
# platform compatibility.
# filemenu.AppendSeparator()
# filemenu.Append(wx.ID_PRINT, '&Print', 'Print the current file.')
# self.Bind(wx.EVT_MENU, None, id=wx.ID_PRINT)
filemenu.AppendSeparator()
filemenu.Append(wx.ID_EXIT, 'E&xit', 'Exit the tmpNote application.')
self.Bind(wx.EVT_MENU, self.destroyer_event, id=wx.ID_EXIT)
editmenu = wx.Menu()
menubar.Append(editmenu, '&Edit')
editmenu.Append(wx.ID_UNDO, 'Undo', 'Undo an action.')
self.Bind(wx.EVT_MENU, self.undo_redo_event, id=wx.ID_UNDO)
editmenu.Append(wx.ID_REDO, 'Redo', 'Redo an action.')
self.Bind(wx.EVT_MENU, self.undo_redo_event, id=wx.ID_REDO)
editmenu.AppendSeparator()
editmenu.Append(wx.ID_CUT, 'Cut\tCtrl+X', 'Cut selection from file.')
self.Bind(wx.EVT_MENU, self.cut_copy_paste_del_sel_event, id=wx.ID_CUT)
editmenu.Append(
wx.ID_COPY, '&Copy\tCtrl+C', 'Copy selection from file.'
)
self.Bind(
wx.EVT_MENU, self.cut_copy_paste_del_sel_event, id=wx.ID_COPY
)
editmenu.Append(
wx.ID_PASTE, '&Paste\tCtrl+V', 'Paste clipboard into file.'
)
self.Bind(
wx.EVT_MENU, self.cut_copy_paste_del_sel_event, id=wx.ID_PASTE
)
editmenu.Append(wx.ID_DELETE, 'Delete', 'Delete the selected text.')
self.Bind(
wx.EVT_MENU, self.cut_copy_paste_del_sel_event, id=wx.ID_DELETE
)
editmenu.AppendSeparator()
editmenu.Append(wx.ID_SELECTALL, 'Select All', 'Select all text.')
self.Bind(
wx.EVT_MENU, self.cut_copy_paste_del_sel_event, id=wx.ID_SELECTALL
)
# findmenu = wx.Menu()
# menubar.Append(findmenu, 'F&ind')
# findmenu.Append(wx.ID_FIND, '&Find\tCtrl+f', 'Find a string.')
# self.Bind(wx.EVT_MENU, None, id=wx.ID_FIND)
# findmenu.Append(
# 301, 'Find &Next\tCtrl+g', 'Find the next occurance of a string.'
# )
# self.Bind(wx.EVT_MENU, None, id=301)
# findmenu.Append(
# wx.ID_REPLACE, '&Replace\tCtrl+h', 'Replace a string.'
# )
# self.Bind(wx.EVT_MENU, None, id=wx.ID_REPLACE)
self.viewmenu = wx.Menu()
menubar.Append(self.viewmenu, '&View')
self.word_wrap_option = self.viewmenu.Append(
401,
'&Word Wrap',
'Wrap lines at the text area width.',
kind=wx.ITEM_CHECK
)
self.viewmenu.Check(401, True)
self.Bind(wx.EVT_MENU, self.word_wrap_toggle_event, id=401)
self.viewmenu.AppendSeparator()
self.status_bar_option = self.viewmenu.Append(
402,
'&Status Bar',
'Display the status bar at the bottom of the window.',
kind=wx.ITEM_CHECK
)
self.viewmenu.Check(402, True)
self.Bind(wx.EVT_MENU, self.status_bar_toggle_event, id=402)
self.notebook_visible_option = self.viewmenu.Append(
406, 'Notebook', 'Display the notebook.', kind=wx.ITEM_CHECK)
self.viewmenu.Check(406, True)
self.Bind(wx.EVT_MENU, self.notebook_visible_toggle_event)
self.viewmenu.AppendSeparator()
self.line_numbers_option = self.viewmenu.Append(
403,
'&Line Numbers',
'Display line numbers in the left margin.',
kind=wx.ITEM_CHECK
)
self.viewmenu.Check(403, True)
self.Bind(wx.EVT_MENU, self.line_numbers_toggle_event, id=403)
self.folding_symbols_option = self.viewmenu.Append(
404,
'Folding Symbols',
'Display folding symbols in the left margin.',
kind=wx.ITEM_CHECK
)
self.viewmenu.Check(404, False)
self.Bind(wx.EVT_MENU, self.folding_symbols_toggle_event, id=404)
# self.nonfolding_symbols_option = self.viewmenu.Append(
# 405,
# 'Non-Folding Symbols',
# 'Display non-folding symbols in the left margin.',
# kind=wx.ITEM_CHECK
# )
# self.viewmenu.Check(405, True)
# self.Bind(wx.EVT_MENU, None, id=405)
self.viewmenu.AppendSeparator()
self.python_lexer_option = self.viewmenu.Append(
407,
'Python syntax',
'Syntax highlighting using the Python lexer.',
kind=wx.ITEM_CHECK
)
self.viewmenu.Check(407, False)
self.Bind(wx.EVT_MENU, self.syntax_python_event, id=407)
# prefmenu = wx.Menu()
# menubar.Append(prefmenu, 'Prefere&nces')
helpmenu = wx.Menu()
menubar.Append(helpmenu, '&Help')
# helpmenu.Append(
# wx.ID_HELP,
# 'Helpful &Documentation',
# 'View the helpful documentation.'
# )
# self.Bind(wx.EVT_MENU, None, id=wx.ID_HELP)
# helpmenu.AppendSeparator()
helpmenu.Append(wx.ID_ABOUT, '&About tmpNote', 'Learn about tmpNote')
self.Bind(wx.EVT_MENU, self.about_event, id=wx.ID_ABOUT)
self.about_already = False
helpmenu.Append(901, 'tmpNote Website', 'Visit the tmpNote website.')
self.Bind(wx.EVT_MENU, self.visit_website_event, id=901)
def set_styles_default(self):
"""Apply default styles to the current notebook page."""
page = self.notebook.GetCurrentPage()
# Set all style bytes to 0, remove all folding information.
page.ClearDocumentStyle()
# After this we can set base styles.
page.SetUseTabs(False)
page.SetTabWidth(4)
page.SetViewWhiteSpace(False)
page.SetViewEOL(False)
# page.SetEOLMode(stc.STC_EOL_CRLF)
# Using generic wx.Font for cross platform compatibility.
font = wx.Font(9, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
page.StyleSetFont(stc.STC_STYLE_DEFAULT, font)
page.StyleSetForeground(stc.STC_STYLE_DEFAULT, (255, 255, 255))
page.StyleSetBackground(stc.STC_STYLE_DEFAULT, (34, 34, 34))
page.SetSelForeground(True, (255, 255, 255))
page.SetSelBackground(True, (68, 68, 68))
page.SetCaretForeground((0, 255, 0))
# Reboot the styles after having just set the base styles.
page.StyleClearAll()
# After this we can set non-base styles.
page.StyleSetForeground(wx.stc.STC_STYLE_LINENUMBER, (151, 151, 151))
page.StyleSetBackground(wx.stc.STC_STYLE_LINENUMBER, (51, 51, 51))
# Use the NULL lexer for default styles.
page.SetLexer(stc.STC_LEX_NULL)
page.SetKeyWords(0, " ".join(keyword.kwlist))
# For folding symbols: http://www.yellowbrain.com/stc/folding.html
# Folding symbols margin settings.
# Color 1 of checker pattern
page.SetFoldMarginColour(True, (41, 41, 41))
# Color 2 of checker pattern
page.SetFoldMarginHiColour(True, (51, 51, 51))
page.SetMarginSensitive(2, False)
def set_styles_python(self):
"""Apply Python styles to the current notebook page."""
page = self.notebook.GetCurrentPage()
# Set all style bytes to 0, remove all folding information.
page.ClearDocumentStyle()
# After this we can set base styles.
page.SetUseTabs(False)
page.SetTabWidth(4)
page.SetViewWhiteSpace(False)
page.SetViewEOL(False)
# page.SetEOLMode(stc.STC_EOL_CRLF)
# Using generic wx.Font for cross platform compatibility.
font = wx.Font(9, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
page.StyleSetFont(stc.STC_STYLE_DEFAULT, font)
page.StyleSetForeground(stc.STC_STYLE_DEFAULT, (255, 255, 255))
page.StyleSetBackground(stc.STC_STYLE_DEFAULT, (34, 34, 34))
page.SetSelForeground(True, (255, 255, 255))
page.SetSelBackground(True, (68, 68, 68))
page.SetCaretForeground((0, 255, 0))
# Reboot the styles after having just set the base styles.
page.StyleClearAll()
# After this we can set non-base styles.
page.StyleSetForeground(wx.stc.STC_STYLE_LINENUMBER, (151, 151, 151))
page.StyleSetBackground(wx.stc.STC_STYLE_LINENUMBER, (51, 51, 51))
# Use the PYTHON lexer for Python styles.
page.SetLexer(stc.STC_LEX_PYTHON)
page.SetKeyWords(0, " ".join(keyword.kwlist))
page.StyleSetSpec(stc.STC_P_COMMENTLINE, 'fore:#777777')
page.StyleSetSpec(stc.STC_P_NUMBER, 'fore:#11ff11')
page.StyleSetSpec(stc.STC_P_STRING, 'fore:#ff77ff')
page.StyleSetSpec(stc.STC_P_CHARACTER, 'fore:#f777f7')
page.StyleSetSpec(stc.STC_P_WORD, 'fore:#77ff77')
page.StyleSetSpec(stc.STC_P_TRIPLE, 'fore:#ff7777')
page.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, 'fore:#777777')
page.StyleSetSpec(stc.STC_P_CLASSNAME, 'fore:#ffffff')
page.StyleSetSpec(stc.STC_P_DEFNAME, 'fore:#7777ff')
page.StyleSetSpec(stc.STC_P_OPERATOR, '')
page.StyleSetSpec(stc.STC_P_IDENTIFIER, '')
page.StyleSetSpec(stc.STC_P_COMMENTBLOCK, 'fore:#777777')
# For folding symbols: http://www.yellowbrain.com/stc/folding.html
# Define markers and colors for folding symbols.
c1 = (51, 51, 51) # Color 1
c2 = (151, 151, 151) # Color 2
# These seven logical symbols make up the mask stc.STC_MASK_FOLDERS
# which we use a bit later.
page.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,
stc.STC_MARK_BOXMINUS, c1, c2)
page.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_BOXPLUS, c1, c2)
page.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,
stc.STC_MARK_VLINE, c1, c2)
page.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,
stc.STC_MARK_LCORNERCURVE, c1, c2)
page.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,
stc.STC_MARK_BOXPLUSCONNECTED, c1, c2)
page.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID,
stc.STC_MARK_BOXMINUSCONNECTED, c1, c2)
page.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL,
stc.STC_MARK_TCORNERCURVE, c1, c2)
# Folding symbols margin settings.
page.SetFoldFlags(16) # 16 = Draw a solid line below folded markers
# Color 1 of checker pattern
page.SetFoldMarginColour(True, (41, 41, 41))
# Color 2 of checker pattern
page.SetFoldMarginHiColour(True, (51, 51, 51))
page.SetProperty("fold", "1")
page.SetMarginType(2, stc.STC_MARGIN_SYMBOL)
# Use the seven logical symbols defined a bit earlier.
page.SetMarginMask(2, stc.STC_MASK_FOLDERS)
page.SetMarginSensitive(2, True)
def status_bar(self):
"""Create a status bar."""
self.statusbar = self.CreateStatusBar()
# Two sections of the status bar.
# 0 | Updates and status messages.
# 1 | Current open file name.
self.statusbar.SetFieldsCount(2)
# Ratio: 2 parts first section, 1 part second section, for size.
self.statusbar.SetStatusWidths([-2, -1])
self.statusbar.SetStatusText('Welcome to tmpNote.', 0)
self.statusbar.SetStatusText('No open file.', 1)
self.statusbar.Show()
def show_notebook_if_not_shown(self):
"""Show the notebook if it is currently hidden."""
if self.notebook.IsShown() is False:
self.notebook_visible_toggle_action()
def status_bar_toggle_event(self, event):
"""Event asking to toggle the status bar visibility."""
if event.GetId() == 402:
self.status_bar_toggle_action()
else:
event.Skip()
def status_bar_toggle_action(self):
"""Toggle the status bar visibility."""
self.statusbar.Show(not self.statusbar.IsShown())
self.SendSizeEvent()
def syntax_python_event(self, event):
"""Event asking to toggle syntax highlighting for Python."""
if event.GetId() == 407:
self.syntax_python_action()
else:
event.Skip()
def syntax_python_action(self):
"""Toggle syntax highlighting with the Python lexer."""
self.show_notebook_if_not_shown()
checkbox_orig_value = not self.viewmenu.IsChecked(407)
page = self.notebook.GetCurrentPage()
page_count = self.notebook.GetPageCount()
if page_count > 0:
if self.viewmenu.IsChecked(407):
self.set_styles_python()
page.python_syntax = True
else:
self.set_styles_default()
page.python_syntax = False
else:
message = 'You selected to toggle Python syntax highlighting. ' + \
'There is no file open to toggle syntax highlighting.'
caption = 'There is no file open to toggle syntax highlighting.'
self.notify_ok(self, message, caption)
self.viewmenu.Check(407, checkbox_orig_value)
def notebook_visible_toggle_event(self, event):
"""Event asking to toggle the FlatNotebook visibility."""
if event.GetId() == 406:
self.notebook_visible_toggle_action()
else:
event.Skip()
def notebook_visible_toggle_action(self):
"""Toggle the FlatNotebook visibility."""
self.notebook.Show(not self.notebook.IsShown())
self.viewmenu.Check(406, self.notebook.IsShown())
self.SendSizeEvent()
def line_numbers_toggle_event(self, event):
"""Event asking to toggle the line numbers margin visibility."""
if event.GetId() == 403:
self.line_numbers_toggle_action()
else:
event.Skip()
def line_numbers_toggle_action(self):
"""Toggle the line numbers margin visibility."""
self.show_notebook_if_not_shown()
checkbox_orig_value = not self.viewmenu.IsChecked(403)
page_count = self.notebook.GetPageCount()
if page_count > 0:
page = self.notebook.GetCurrentPage()
if page.line_numbers is True:
page.SetMarginWidth(0, 0)
page.line_numbers = False
else:
page.SetMarginWidth(0, 30)
page.line_numbers = True
else:
message = 'You selected to toggle line number visibility.' +\
' There is no file open to toggle line number visibility.'
caption = 'There is no file open to toggle line number visibility.'
self.notify_ok(self, message, caption)
self.viewmenu.Check(403, checkbox_orig_value)
def folding_symbols_toggle_event(self, event):
"""Event asking to toggle the folding symbols margin visibility."""
if event.GetId() == 404:
self.folding_symbols_toggle_action()
else:
event.Skip()
def folding_symbols_toggle_action(self):
"""Toggle the folding symbols margin visibility."""
self.show_notebook_if_not_shown()
checkbox_orig_value = not self.viewmenu.IsChecked(404)
page_count = self.notebook.GetPageCount()
if page_count > 0:
page = self.notebook.GetCurrentPage()
if page.folding_symbols is True:
page.SetMarginWidth(2, 0)
page.folding_symbols = False
else:
page.SetMarginWidth(2, 30)
page.folding_symbols = True
else:
message = 'You selected to toggle folding symbols visibility. ' +\
'There is no file open to toggle folding symbols visibility.'
caption = 'There is no file open to toggle folding symbols ' +\
'visibility.'
self.notify_ok(self, message, caption)
self.viewmenu.Check(404, checkbox_orig_value)
def word_wrap_toggle_event(self, event):
"""Event asking to toggle the word wrap option."""
if event.GetId() == 401:
self.word_wrap_toggle_action()
else:
event.Skip()
def word_wrap_toggle_action(self):
"""Toggle the word wrap option."""
self.show_notebook_if_not_shown()
checkbox_orig_value = not self.viewmenu.IsChecked(401)
page_count = self.notebook.GetPageCount()
if page_count > 0:
page = self.notebook.GetCurrentPage()
page.SetWrapMode(not page.GetWrapMode())
page.word_wrap = page.GetWrapMode()
else:
message = 'You selected to toggle word wrap. There is no file ' +\
'open to toggle word wrap. Please open a files before' +\
'selecting to toggle word wrap.'
caption = 'There is no file open to toggle word wrap.'
self.notify_ok(self, message, caption)
self.viewmenu.Check(401, checkbox_orig_value)
def page_changed_event(self, event):
"""Event to gracefully change pages."""
page = self.notebook.GetCurrentPage()
self.statusbar.SetStatusText(
'You switched to {0}'.format(page.filename), 0)
self.statusbar.SetStatusText(page.filename, 1)
self.viewmenu.Check(401, page.word_wrap)
self.viewmenu.Check(403, page.line_numbers)
self.viewmenu.Check(404, page.folding_symbols)
self.viewmenu.Check(407, page.python_syntax)
def new_file_event(self, event):
"""Event requesting to create a new file."""
if event.GetId() == wx.ID_NEW:
self.new_file()
else:
event.Skip()
def new_file(self):
"""Create a new TextCtrl page and add it to the FlatNotebook."""
self.show_notebook_if_not_shown()
page = TxtCtrl(self, text='', readonly=False)
self.pages.append(page)
page.SetUndoCollection(True)
page.SetBufferedDraw(True)
page.SetWrapMode(stc.STC_WRAP_WORD)
page.python_syntax = False
page.folding_symbols = False
page.line_numbers = False
page.word_wrap = True
page.path = ''
page.filename = 'Untitled'
page.datetime = str(datetime.datetime.now())
# http://www.scintilla.org/ScintillaDoc.html#Margins
page.SetMarginLeft(6) # Text area left margin.
page.SetMarginWidth(0, 0) # Line numbers margin.
page.SetMarginWidth(1, 0) # Non-folding symbols margin.
page.SetMarginWidth(2, 0) # Folding symbols margin.
self.notebook.AddPage(
page=page,
text='Untitled',
select=True
)
self.set_styles_default()
page.SetFocus()
def open_file_event(self, event):
"""Event requesting to open a file."""
if event.GetId() == wx.ID_OPEN:
self.open_file()
else:
event.Skip()
def open_file(self):
"""Open the contents of a file into a new FlatNotebook page."""
self.show_notebook_if_not_shown()
dlg = wx.FileDialog(
parent=self,
message='Select a file to open.',
defaultDir=os.getcwd(),
defaultFile='tmpNote.txtmp',
wildcard='All files (*.*)|*.*|tmpNote files (*.txtmp)|' +
'*.txtmp|Text files (*.txt)|*.txt',
style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_MULTIPLE
)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
paths = dlg.GetPaths()
filenames = dlg.GetFilenames()
for index, filename in enumerate(filenames):
path = paths[index]
try:
f = open(path, 'r')
text = f.read()
f.close()
# Lose the default untitled page if that is the only
# page and there are no unsaved modifications to it,
# and then reset the pages list.
if (
(len(self.pages) == 1) and
(self.pages[0].filename == 'Untitled') and
(self.pages[0].GetModify() is False)
):
self.notebook.DeletePage(0)
self.pages = []
page = TxtCtrl(self, text=text, readonly=False)
self.pages.append(page)
page.SetUndoCollection(True)
page.SetBufferedDraw(True)
page.SetWrapMode(stc.STC_WRAP_WORD)
page.python_syntax = False
page.folding_symbols = False
page.line_numbers = False
page.word_wrap = True
page.path = path
page.filename = filename
page.datetime = str(datetime.datetime.now())
# http://www.scintilla.org/ScintillaDoc.html#Margins
page.SetMarginLeft(6) # Text area left margin.
page.SetMarginWidth(0, 0) # Line numbers margin.
page.SetMarginWidth(1, 0) # Non-folding symbols margin.
page.SetMarginWidth(2, 0) # Folding symbols margin.
self.notebook.AddPage(
page=page,
text=page.filename,
select=True
)
self.set_styles_default()
page.SetFocus()
page.SetSavePoint()
self.statusbar.SetStatusText(
'You opened {0}'.format(filename), 0)
self.statusbar.SetStatusText(filename, 1)
except (IOError, UnicodeDecodeError) as error:
error_dlg = wx.MessageDialog(
parent=self,
message='Error trying to open {0}.\n\n{1}'.format(
filename, error),
caption='Error',
style=wx.ICON_EXCLAMATION
)
error_dlg.ShowModal()
error_dlg.Destroy()
except:
error_dlg = wx.MessageDialog(
parent=self,
message='Error trying to open {0}.\n\n'.format(
filename),
caption='Error',
style=wx.ICON_EXCLAMATION
)
error_dlg.ShowModal()
error_dlg.Destroy()
else:
self.statusbar.SetStatusText('The file was not opened.', 0)
def save_file_event(self, event):
"""Event requesting to save a file, or save as."""
page_count = self.notebook.GetPageCount()
if (event.GetId() == wx.ID_SAVE or wx.ID_SAVEAS) and page_count == 0:
self.statusbar.SetStatusText('There is no file open to save.', 0)
message = 'You selected to save a file or to save as.' + \
' There is no file open to save.'
caption = 'There is no file open to save.'
self.notify_ok(self, message, caption)
elif event.GetId() == wx.ID_SAVE and page_count > 0:
self.save_file()
elif event.GetId() == wx.ID_SAVEAS and page_count > 0:
self.save_file_as()
else:
event.Skip()
def save_file(self):
"""Save the selected page text to file."""
self.show_notebook_if_not_shown()
page = self.notebook.GetCurrentPage()
if page.path == '':
# Page hasn't been saved before, use save as instead.
self.save_file_as()
else:
try:
text = page.GetText()
f = open(page.path, 'w')
f.write(text)
f.close()
page.SetSavePoint()
self.statusbar.SetStatusText(
'You saved {0}'.format(page.filename), 0)
self.statusbar.SetStatusText(page.filename, 1)
except IOError:
error_dlg = wx.MessageDialog(
parent=self,
message='Error trying to save {0}.\n\n{1}'.format(
page.filename, error),
caption='Error',
style=wx.ICON_EXCLAMATION
)
error_dlg.ShowModal()
error_dlg.Destroy()
self.statusbar.SetStatusText(
'There was an error saving the file.',
0
)
except Exception:
error_dlg = wx.MessageDialog(
parent=self,
message='Error trying to save {0}.\n\n'.format(
page.filename),
caption='Error',
style=wx.ICON_EXCLAMATION
)
error_dlg.ShowModal()
error_dlg.Destroy()
self.statusbar.SetStatusText(
'There was an error saving the file.',
0
)
def save_file_as(self):
"""Save the selected page text to file, using Save As."""
self.show_notebook_if_not_shown()
page = self.notebook.GetCurrentPage()
if page.path == '':
default_file = ''
else:
default_file = page.filename
dlg = wx.FileDialog(
parent=self,
message='Select a file to save.',
defaultDir=os.getcwd(),
defaultFile=default_file,
wildcard='All files (*.*)|*.*|tmpNote files (*.txtmp)|' +
'*.txtmp|Text files (*.txt)|*.txt',
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
)
result = dlg.ShowModal()
path = dlg.GetPath()
filename = dlg.GetFilename()
dlg.Destroy()
if result == wx.ID_OK:
try:
text = page.GetText()
f = open(path, 'w')
f.write(text)
f.close()
page.SetSavePoint()
page.path = path
page.filename = filename
self.statusbar.SetStatusText(
'You saved {0}.'.format(filename), 0)
self.statusbar.SetStatusText(filename, 1)
self.notebook.SetPageText(
page=self.notebook.GetSelection(),
text=filename
)
except IOError:
error_dlg = wx.MessageDialog(
parent=self,
message='Error trying to save {0}.\n\n{1},'.format(
filename, error),
caption='Error',
style=wx.ICON_EXCLAMATION
)
error_dlg.ShowModal()
error_dlg.Destroy()
self.statusbar.SetStatusText(
'There was an error saving the file.', 0)
except Exception:
error_dlg = wx.MessageDialog(
parent=self,
message='Error trying to save {0}.\n\n'.format(filename),
caption='Error',
style=wx.ICON_EXCLAMATION
)
error_dlg.ShowModal()
error_dlg.Destroy()
self.statusbar.SetStatusText(
'There was an error saving the file.', 0)
else:
self.statusbar.SetStatusText('The file was not saved.', 0)
def close_file_event(self, event):
"""Event requesting to close a file."""
page_count = self.notebook.GetPageCount()
if event.GetId() == wx.ID_CLOSE and page_count == 0:
self.statusbar.SetStatusText('There is no file open to close.', 0)
message = 'You selected to close a file. There is no file' +\
'open to close.'
caption = 'There is no file open to close.'
self.notify_ok(self, message, caption)
elif event.GetId() == wx.ID_CLOSE and page_count > 0:
# Try deleting the currently selected notebook page.
# This will trigger the EVT_FLATBOOK_PAGE_CLOSING event.
# That event is bound to self.closing_file_event.
self.notebook.DeletePage(self.notebook.GetSelection())
else:
event.Skip()
def closing_file_event(self, event):
"""This event is triggered when any FlatNotebook page is deleted.