forked from sanyaade-machine-learning/Transana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RichTextEditCtrl.py
1550 lines (1328 loc) · 71.5 KB
/
RichTextEditCtrl.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
# Copyright (C) 2003 - 2015 The Board of Regents of the University of Wisconsin System
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
""" This module implements the RichTextEditCtrl class, a Rich Text editor based on StyledTextCtrl. """
__author__ = 'Nathaniel Case, David Woods <dwoods@wcer.wisc.edu>, Jonathan Beavers <jonathan.beavers@gmail.com>'
DEBUG = False
if DEBUG:
print "RichTextEditCtrl DEBUG is ON."
DEBUG2 = False
if DEBUG2:
print "RichTextEditCtrl DEBUG2 is ON."
SHOWHIDDEN = False
if SHOWHIDDEN:
print "RichTextEditCtrl SHOWHIDDEN is ON."
import TransanaConstants # import Transana's Constants
import TransanaGlobal # import Transana's Globals
import Dialogs # import Transana's Dialogs for the ErrorDialog
import sys, os, string, re # import Python modules that are needed
import pickle # the pickle module enables us to fast-save
import wx # import wxPython
from wx import stc # import wxPython's StyledTextCtrl
# Add the "rtf" path to the Python Path so that the RTF Parser can be found.
RTFModulePath = "rtf"
if sys.path.count(RTFModulePath) == 0:
sys.path.append(RTFModulePath) # Add to path if not already there
import RTFParser # import our RTF Parser for reading RTF files
import RTFDoc, TextDoc # import RTFDoc and TextDoc for writing docs to RTF format
# NOTE: cStringIO is faster, but does not support Unicode strings!!! DKW
if 'unicode' in wx.PlatformInfo:
import StringIO # If we're using Unicode, we must use the Unicode-compatible StringIO
else:
import cStringIO # If we're using ansi, we can use the faster cStringIO
import traceback # Import Python's traceback module for error reporting in DEBUG mode
# This mask determines the number of style bits to use. The default is
# 31, which uses 5 bits (2^5) for 32 possible styles. We increase this
# to 128 possible styles since we don't use the indicators feature that
# is normally used for the other 3 bits. We have to use at least
# one indicator so we use 0x7f instead of 0xff. This is the maximum that
# the Styled Text Control allows at this time.
STYLE_MASK = 0x7f
# Although we can technically support 127 styles, because of the way styles are created and stored, which
# can result in significant changes in the number defined, we'd better set a lower limit for now.
NUM_STYLES_SUPPORTED = 100 # 127
class RichTextEditCtrl(stc.StyledTextCtrl):
""" This class is a Rich Text edit control implemented for Transana """
def __init__(self, parent, id=-1):
"""Initialize a StyledTextCtrl object."""
stc.StyledTextCtrl.__init__(self, parent, id)
# The text display on the Mac is simply TOO SMALL. This makes it a bit bigger by default.
if "__WXMAC__" in wx.PlatformInfo:
self.SetZoom(3)
# We need as many styles as possible, and will not use Indicators. This goes hand-in-hand with the STYLE_MASK defined above.
self.SetStyleBits(7)
# STYLE_HIDDEN is meant for things that are sometimes hidden and
# sometimes visible, depending on the ShowTimeCodes value.
# STYLE_HIDDEN_ALWAYS is intended for things that should never be
# visible to the user. (It's not actually used.)
# Any style assigned here needs to be added to __ResetBuffer() as well.
self.STYLE_HIDDEN = -1 # Define the Hidden Style for Time Codes and Time Code Data
self.STYLE_TIMECODE = -1 # Define the Time Code Style for Time Codes when not hidden
# self.STYLE_HIDDEN_ALWAYS = -1 NOT USED??
# self.HIDDEN_CHARS = [] NOT USED??
self.HIDDEN_REGEXPS = [] # Initialize a list for Regular Expressions for hidden data
self.__ResetBuffer() # Initialize the STC, including defining default font(s)
# Set the Default Style based on the Default Font information in the Configuration object
self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "size:%s,face:%s,fore:#000000,back:#ffffff" % (str(TransanaGlobal.configData.defaultFontSize), TransanaGlobal.configData.defaultFontFace))
# Set the Line Number style based on the Default Font Face
self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, "size:10,face:%s" % TransanaGlobal.configData.defaultFontFace)
# Set Word Wrap as configured
self.SetWrapMode(TransanaGlobal.configData.wordWrap) # (stc.STC_WRAP_WORD)
# Setting the LayoutCache to the whole document seems to reduce the typing lag problem on the PPC Mac
self.SetLayoutCache(stc.STC_CACHE_DOCUMENT)
# Additional suggestions from wxPython-mac user's mailing list:
# Limit the events that trigger the EVT_STC_MODIFIED handler
self.SetModEventMask(stc.STC_PERFORMED_UNDO | stc.STC_PERFORMED_REDO | stc.STC_MOD_DELETETEXT | stc.STC_MOD_INSERTTEXT)
# Turn off Anti-Aliasing
self.SetUseAntiAliasing(False)
# Set the Tab Width to the configured size
self.SetTabWidth(int(TransanaGlobal.configData.tabSize))
# Indicate that we would like Line Numbers in the STC Margin
self.SetMarginType(0, stc.STC_MARGIN_NUMBER)
# We need a slighly wider Line Number section on the Mac. This code determines an optimum width by platform
if "__WXMAC__" in wx.PlatformInfo:
self.lineNumberWidth = self.TextWidth(stc.STC_STYLE_LINENUMBER, '88888 ')
else:
self.lineNumberWidth = self.TextWidth(stc.STC_STYLE_LINENUMBER, '8888 ')
# Apply the Line Number section width determined above
self.SetMarginWidth(0, self.lineNumberWidth)
# Set the STC right and left Margins
self.SetMargins(6, 6)
# Set the STC Selection Colors to white text on a blue background
self.SetSelForeground(1, "black") # "white"
self.SetSelBackground(1, "cyan") # "blue"
# We need to capture key strokes so we can intercept the Paste command.
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
# Define the STC Events we need to implement or modify.
# Character Added event
stc.EVT_STC_CHARADDED(self, id, self.OnCharAdded)
# User Interface Update event
stc.EVT_STC_UPDATEUI(self, id, self.OnUpdateUI)
# STC Modified event
stc.EVT_STC_MODIFIED(self, id, self.OnModified)
# Initialzations:
# We need to know if a style has been changed, as this can indicate the need for a Save.
self.stylechange = 0
# If set to point to a wxProgressDialog, this dialog will be updated as a document is loaded.
# Initialize to None.
self.ProgressDlg = None
# There was a problem with the font being changed inappropriately during
# data loading. This flag helps prevent that.
self.loadingData = False
def OnKeyDown(self, event):
""" We need to capture the Paste call to intercept it and remove Time Codes from the
Clipboard Data """
# We only need to intercept Ctrl-V on Windows or Command-V on the Mac. First, see if the
# platform-appropriate modified key is currently pressed
if (('wxMac' not in wx.PlatformInfo) and event.ControlDown()) or \
(('wxMac' in wx.PlatformInfo) and event.CmdDown()):
# Look for "C" (Copy)
if event.GetKeyCode() == 67: # Ctrl-C
# If so, call our modified Copy() method
self.Copy()
# Look for "V" (Paste)
elif event.GetKeyCode() == 86: # Ctrl-V
# If so, call our modified Paste() method
self.Paste()
# Look for "X" (Cut)
elif event.GetKeyCode() == 88: # Ctrl-X
# If so, call our modified Cut() method
self.Cut()
# For anything other than Ctrl/Cmd- C, V, or X ...
else:
# ... we let processing drop to the STC
event.Skip()
# If the keystroke isn't modified platform-approptiately ...
else:
# ... we let processing drop to the STC
event.Skip()
def PutEditedSelectionInClipboard(self):
""" Put the TEXT for the current selection into the Clipboard """
tempTxt = self.GetSelectedText()
# Initialize an empty string for the modified data
newSt = ''
# Created a TextDataObject to hold the text data from the clipboard
tempDataObject = wx.TextDataObject()
# Track whether we're skipping characters or not. Start out NOT skipping
skipChars = False
# Now let's iterate through the characters in the text
for ch in tempTxt:
# Detect the time code character
if ch == TransanaConstants.TIMECODE_CHAR:
# If Time Code, start skipping characters
skipChars = True
# if we're skipping characters and we hit the ">" end of time code data symbol ...
elif (ch == '>') and skipChars:
# ... we can stop skipping characters.
skipChars = False
# If we're not skipping characters ...
elif not skipChars:
# ... add the character to the new string.
newSt += ch
# Save the new string in the Text Data Object
tempDataObject.SetText(newSt)
# Open the Clipboard
wx.TheClipboard.Open()
# Place the Text Data Object in the Clipboard
wx.TheClipboard.SetData(tempDataObject)
# Close the Clipboard
wx.TheClipboard.Close()
def Cut(self):
if 'wxMac' in wx.PlatformInfo:
self.PutEditedSelectionInClipboard()
self.ReplaceSelection('')
else:
stc.StyledTextCtrl.Cut(self)
def Copy(self):
if 'wxMac' in wx.PlatformInfo:
self.PutEditedSelectionInClipboard()
else:
stc.StyledTextCtrl.Copy(self)
def Paste(self):
""" We need to intercept the STC's Paste() method to deal with time codes. We need to strip
time codes out of the clipboard before pasting, as they don't paste properly. """
# Initialize an empty string for the modified data
newSt = ''
# Created a TextDataObject to hold the text data from the clipboard
tempDataObject = wx.TextDataObject()
# Open the Clipboard
wx.TheClipboard.Open()
# Read the text data from the clipboard
wx.TheClipboard.GetData(tempDataObject)
# Track whether we're skipping characters or not. Start out NOT skipping
skipChars = False
# Now let's iterate through the characters in the text
for ch in tempDataObject.GetText():
# Detect the time code character
if ch == TransanaConstants.TIMECODE_CHAR:
# If Time Code, start skipping characters
skipChars = True
# if we're skipping characters and we hit the ">" end of time code data symbol ...
elif (ch == '>') and skipChars:
# ... we can stop skipping characters.
skipChars = False
# If we're not skipping characters ...
elif not skipChars:
# ... add the character to the new string.
newSt += ch
# Save the new string in the Text Data Object
tempDataObject.SetText(newSt)
# Place the Text Data Object in the Clipboard
wx.TheClipboard.SetData(tempDataObject)
# if we're on Mac, we need to close the Clipboard to call Paste. We don't need this on
# Windows, but the Mac requires it.
if 'wxMac' in wx.PlatformInfo:
wx.TheClipboard.Close()
# Call the STC's Paste method to complete the paste.
stc.StyledTextCtrl.Paste(self)
# If the Clipboard is open ...
if wx.TheClipboard.IsOpened():
# Close the Clipboard
wx.TheClipboard.Close()
def __ResetBuffer(self):
""" Reset the wxSTC buffer -- that is, re-initialize the Rich Text Control's data """
if DEBUG:
print "RichTextEditCtrl: ResetBuffer()"
# Set the wxSTC Text to blank
self.SetText("")
# Empty out the Undo Buffer, removing old Undo data
self.EmptyUndoBuffer()
# Redefine the Default Style, just in case
self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "size:%s,face:%s,fore:#000000,back:#ffffff" % (str(TransanaGlobal.configData.defaultFontSize), TransanaGlobal.configData.defaultFontFace))
# Clear out all defined styles. Internally, the wxSTC resets all styles to the default style.
self.StyleClearAll()
# Reset the data structures that keep track of the defined styles.
# The style_specs list shows style information in a human-readable, yet parsable, way
self.style_specs = []
# The style_attrs list shows style information in a machine-readable way
self.style_attrs = []
# num_styles tracks the number of styles that have been defined
self.num_styles = 0
# Reset the Last Position indicator
self.last_pos = 0
# Define the initial style, Style 0, based on the user-specified defaults
self.__GetStyle("size:%s,face:%s,fore:#000000,back:#ffffff" % (str(TransanaGlobal.configData.defaultFontSize), TransanaGlobal.configData.defaultFontFace))
# Reset the global style currently being used to this default style
self.style = 0
# Redefine the Hidden and Timecode styles, which Transana always requires.
self.STYLE_HIDDEN = self.__GetStyle("hidden")
self.STYLE_TIMECODE = self.__GetStyle("timecode")
# The RTF Parser seems to insist on adding these two "default" styles. Let's add them here to reduce conflicts
# with the number of styles that exist in new Transcripts vs. saved Transcripts.
# NOTE: These could be removed if the Style Definition code is cleaned up.
self.__GetStyle("size:12,face:Times New Roman,fore:#000000,back:#ffffff")
self.__GetStyle("size:12,face:Courier New,fore:#000000,back:#ffffff")
# If we want to show the normally hidden time code data for debugging purposes, this does it!
if not SHOWHIDDEN:
self.StyleSetVisible(self.STYLE_HIDDEN, False)
# Define the particulars of the Timecode style, which is to use the default font in red.
self.StyleSetSpec(self.STYLE_TIMECODE, "size:%s,face:%s,fore:#FF0000,back:#ffffff" % (str(TransanaGlobal.configData.defaultFontSize), TransanaGlobal.configData.defaultFontFace))
def GetStyleAccessor(self, spec):
"""This method is simply a public wrapper around __GetStyle()"""
return self.__GetStyle(spec)
def __GetStyle(self, spec):
"""Get a style number from the given StyleSpec string and
return the Style index number. If such a style does not exist
then it will be created and a new number is returned. This is
limited in that you have to specify the spec string exactly
how it was originally specified or else it won't recognize it as
being the same (even if it is functionally the same)."""
# FIXME: We should do a split() on it and see if all the elements
# are compared rather than doing a string comparison. With this way
# it will waste some styles.
if DEBUG:
print "RichTextEditCtrl.__GetStyle(): spec = '%s', count = %d" % (spec, self.style_specs.count(spec))
# If the desired style isn't already defined ...
if self.style_specs.count(spec) == 0:
if DEBUG:
print "RichTextEditCtrl.__GetStyle(): Allocating new style (spec %s)" % spec
# NOTE: This is really messed up. Given the current (Aug. 11, 2005) infrastructure, specifying a new
# style can cause multiple styles to be created along the way. Saving and reloading a document
# can cause the number of styles to change, as some unused styles created above get dropped, but some
# style combinations that aren't used and weren't created above get created after a save. And
# saving a second time seems to have the effect of reducing the number of styles considerably.
# In one experiment, I had 31 defined styles. I added 5 styles, which took my total up to 45.
# Then I saved it and reloaded, and was faced with 65 styles. Then I saved again, and upon reload
# found that I had 49 styles defined.
# Because of this, trapping the number of styles won't really work until the style creation mechanism
# for the RichTextEditCtrl is rewritten.
# Check to see if we can support an additional style.
if (self.num_styles >= NUM_STYLES_SUPPORTED): # STYLE_MASK
if DEBUG:
print "No more styles left. Using Default"
# We need the ErrorDialog from the Dialogs module.
import Dialogs
# Build and display an error message
msg = _("Transana is only able to handle %d different font styles. You have exceeded that capacity.\nYour text will revert to a previously-used style.\nPlease reduce the number of unique styles used in this transcript.")
if 'unicode' in wx.PlatformInfo:
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
msg = unicode(msg, 'utf8')
dlg = Dialogs.ErrorDialog(self, msg % NUM_STYLES_SUPPORTED)
dlg.ShowModal()
dlg.Destroy()
# No more styles left, must use default
return stc.STC_STYLE_DEFAULT
# Okay, the stc.STC_STYLE_LINENUMBER style is getting over-written, which changes the style of the line numbers.
# We have to do a trick here to avoid that. HACK HACK HACK.
# Essentially, what this code does is insert the appropriate style when we get to this point in the
# style array. Apparently, the information is stored internally in the wxSTC until this 33rd style is created.
# This is necessary because we are using 7 style bits instead of 5, which is the wx.STC default.
while (self.num_styles == stc.STC_STYLE_LINENUMBER) or (self.num_styles == stc.STC_STYLE_DEFAULT):
if DEBUG:
print "Skipping %d because it equals %d (stc.STC_STYLE_LINENUMBER) or %s (stc.STC_STYLE_DEFAULT)" % (self.num_styles, stc.STC_STYLE_LINENUMBER, stc.STC_STYLE_DEFAULT)
attr = StyleSettings()
if (self.num_styles == stc.STC_STYLE_LINENUMBER):
# Define the StyleSpec appropriate for stc.STC_STYLE_LINENUMBER
self.StyleSetSpec(self.num_styles, "size:10,face:%s,fore:#000000,back:#ffffff" % TransanaGlobal.configData.defaultFontFace)
# Also place that style in style_specs
self.style_specs.append("size:10,face:%s,fore:#000000,back:#ffffff" % TransanaGlobal.configData.defaultFontFace)
attr.font_size = 10
else:
# Define the StyleSpec appropriate for stc.STC_STYLE_DEFAULT
self.StyleSetSpec(self.num_styles, 'size:%s,face:%s,fore:#000000,back:#ffffff' % (TransanaGlobal.configData.defaultFontSize, TransanaGlobal.configData.defaultFontFace))
# Also place that style in style_specs
self.style_specs.append('size:%s,face:%s,fore:#000000,back:#ffffff' % (TransanaGlobal.configData.defaultFontSize, TransanaGlobal.configData.defaultFontFace))
attr.font_size = TransanaGlobal.configData.defaultFontSize
# And also define the Attributes for the style ...
attr.font_face = TransanaGlobal.configData.defaultFontFace
attr.font_size = 10
attr.font_fg = 0x000000
# ... and store the style in the style_attrs list.
self.style_attrs.append(attr)
# Increment the style counter.
self.num_styles += 1
if DEBUG:
print "RichTextEditCtrl.__GetStyles(): StyleSetSpec(%s, %s)" % (self.num_styles, spec)
# Define the style for wx.STC
self.StyleSetSpec(self.num_styles, spec)
# Hidden text, when shown, is Red by default
if spec == "hidden":
self.StyleSetForeground(self.num_styles, wx.NamedColour("red"))
# Add the style to the style_specs list, which allows us to keep track of what styles have been
# defined internally by wx.STC.
self.style_specs.append(spec)
# We also need to gather the current attributes ...
attr = StyleSettings()
props = spec.split(",")
attr.bold = props.count("bold") > 0
attr.italic = props.count("italic") > 0
attr.underline = props.count("underline") > 0
for x in props:
if x[:5] == "face:":
attr.font_face = x[5:]
if x[:5] == "fore:":
attr.font_fg = int(x[6:],16)
if x[:5] == "back:":
attr.font_bg = int(x[6:],16)
if x[:5] == "size:":
attr.font_size = int(x[5:])
# ... and store them in the style_attrs list
self.style_attrs.append(attr)
# Increment the style counter
self.num_styles += 1
if DEBUG:
print "New style #%d = %s" % (self.num_styles - 1, spec)
# Return the style number, using -1 because we just incremented the counter!
return self.num_styles - 1
else:
# Return the number that matches the specified style
return self.style_specs.index(spec)
def CurrentSpec(self):
return self.__CurrentSpec()
def __CurrentSpec(self):
"""Return the current style spec string."""
# We could have events that update self.style each time
# the cursor is moved or text is selected rather than
# "polling" it like this, could perform better.
startpos = self.GetSelectionStart()
endpos = self.GetSelectionEnd()
if startpos == endpos:
# No selection
style = self.GetStyleAt(self.GetCurrentPos())
else:
style = self.GetStyleAt(startpos)
return self.style_specs[style]
def __ParseRTFStream(self, stream):
# Assume that this stage of parsing takes up 50% of progress dialog,
# so each element in the stream counts for 50/len(stream).
prog_level = 50.0
if len(stream) > 0:
inc_level = 50.0/len(stream)
else:
inc_level = 0
if self.ProgressDlg:
self.ProgressDlg.Update(prog_level, _("Processing RTF stream"))
for obj in stream:
if DEBUG and (obj.text != ''):
print
print "RichTextEditCtrl.__ParseRTFStream(): next object in stream is:", obj
print "RichTextEditCtrl.__ParseRTFStream(): type(obj.text):", type(obj.text)
if type(obj.text).__name__ == 'str':
for c in obj.text:
print c, ord(c)
# If we are opening a transcript from a non-unicode version of Transana, we need to convert
# the text to unicode.
if ('unicode' in wx.PlatformInfo) and isinstance(obj.text, str):
# There is a problem importing transcripts on single-user Win version. Specifically,
# the closed dot doesn't import correctly. This aims to fix that using character substitution.
if obj.text == chr(149):
obj.text = unicode('\xe2\x80\xa2', 'utf8')
else:
obj.text = unicode(obj.text, TransanaGlobal.encoding)
if self.ProgressDlg:
self.ProgressDlg.Update(prog_level)
if obj.attr == None:
if DEBUG:
print "RichTextCtrl.__ParseRTFStream(): '%s'" % obj.text.encode('utf8'),
for x in obj.text:
print ord(x),
if ord(x) < 127:
print x,
print
if (('wxMac' in wx.PlatformInfo) and (ord(obj.text[0]) == 183)) or \
(('wxMSW' in wx.PlatformInfo) and (ord(obj.text[0]) == 183)):
if DEBUG:
print "RichTextCtrl.__ParseRTFStream(): Closed Dot"
self.InsertInBreath()
elif (('wxMac' in wx.PlatformInfo) and (ord(obj.text[0]) == 176)) or \
(('wxMSW' in wx.PlatformInfo) and (ord(obj.text[0]) == 176)):
if DEBUG:
print "RichTextCtrl.__ParseRTFStream(): Open Dot"
self.InsertWhisper()
else:
# I've seen some sporadic problems converting RTF. Specifically, chunks of the transcript get loaded
# in the wrong order, or in the wrong place in the new document.
#
# This problem can be detected by comparing GetCurrentPos() to GetLength(), since new text should
# ALWAYS be added to the end of the document. If this problem is detected ...
if self.GetCurrentPos() != self.GetLength():
# ... correct it by resetting the CurrentPos to the last character in the document!
self.SetCurrentPos(self.GetLength())
startpos = self.GetCurrentPos()
self.AddText(obj.text)
# If we receive Unicode objects, we need to decode them so we can figure out their correct length, which
# wxSTC needs to know for the purpose of styling the right number of bytes.
if ('unicode' in wx.PlatformInfo) and (type(obj.text).__name__ == 'unicode'):
# Don't use the current encoding, but always use UTF8, as that's what Python (and hence the
# wx.STC) is set for. This fixes a bug with importing transcripts into Latin1 on single-user Win version.
txt = obj.text.encode('utf8') # (TransanaGlobal.encoding)
else:
txt = obj.text
if DEBUG:
print "RichTextEditCtrl.__ParseRTFStream(): Adding text '%s' with style %s, len =%d /%d" % (txt, self.style, len(obj.text), len(txt))
self.StartStyling(startpos, STYLE_MASK)
self.SetStyling(len(txt), self.style)
else:
# Everytime there's an attribute change, we change the
# 'current' attribute completely to the new one.
# Let's not change the attributes one at a time. That causes too many unused styles to be created.
# Since we have a limit on the number of styles we can support, let's not waste any.
# self.__SetAttr("bold", obj.attr.bold)
# self.__SetAttr("underline", obj.attr.underline)
# self.__SetAttr("italic", obj.attr.italic)
# self.__SetFontFace(obj.attr.font)
# self.__SetFontColor(obj.attr.fg, obj.attr.bg)
# self.__SetFontSize(obj.attr.fontsize)
# I'm not sure this is actually any better. I don't have any more time to explore this right now.
styleStr = "size:%d" % obj.attr.fontsize
styleStr += ",face:%s" % obj.attr.font
styleStr += ",fore:#%06x" % obj.attr.fg
styleStr += ",back:#%06x" % obj.attr.bg
if obj.attr.bold:
styleStr += ',bold'
if obj.attr.underline:
styleStr += ',underline'
if obj.attr.italic:
styleStr += ',italic'
if DEBUG:
print "RichTextEditCtrl.__ParseRTFStream(): styleStr = '%s'" % styleStr
self.style = self.__GetStyle(styleStr)
if DEBUG:
print "RichTextEditCtrl.__ParseRTFStream(): New style after attribute change: %d" % self.style
prog_level = prog_level + inc_level
# Do a second pass through the text to check for hidden expressions.
# This hides all of the Time Codes and the Time Code Data.
for hidden_re in self.HIDDEN_REGEXPS:
if DEBUG:
print "Checking for hidden expression"
i = 0
# Get list of expanded timecodes in text
hidden_seqs = hidden_re.findall(self.GetText())
if DEBUG:
print "Hidden seqs found = %s" % hidden_seqs
# Hide each one found
for seq in hidden_seqs:
if DEBUG:
print "type = %s" % type(seq)
seq_start = self.FindText(i, 999999, seq, 0)
if DEBUG:
print "Sequence starts at index %d" % seq_start
i = seq_start + 1
self.StartStyling(seq_start, STYLE_MASK)
if DEBUG:
print "Styling for len = %d" % len(seq)
if 'unicode' in wx.PlatformInfo:
self.SetStyling(len(seq) + 1, self.STYLE_HIDDEN)
else:
self.SetStyling(len(seq), self.STYLE_HIDDEN)
if self.ProgressDlg:
self.ProgressDlg.Update(100)
def InsertRisingIntonation(self):
""" Insert the Rising Intonation (Up Arrow) symbol """
# NOTE: Ugly HACK warning.
# The wxSTC on Mac isn't displaying Unicode characters correctly. Therefore, we
# need to send a character that the wxSTC can display, even if it's not actually
# the character we want stored in the database.
if 'unicode' in wx.PlatformInfo:
# This is the "Unicode" way, which works well on Windows and inserts a character that can be saved
# and loaded without difficulty. Unfortunately, the Mac cannot display this character.
# This has been abandoned for now, though might be resurrected if they fix the wxSTC.
ch = unicode('\xe2\x86\x91', 'utf8') # \u2191
len = 3
self.InsertUnicodeChar(ch, len)
# The new approach, the "Symbol" way, is to insert the appropriate character in the Symbol
# Font to display the up arrow character. This is a different character on Windows than it
# is on Mac. Therefore, there needs to be code in loading and saving files that translates
# this character so that files will work cross-platform.
# if 'wxMac' in wx.PlatformInfo:
# ch = unicode('\xe2\x89\xa0', 'utf8')
# len = 3
# else:
# ch = unicode('\xc2\xad', 'utf8')
# len = 2
# We insert the specified character in Symbol Font.
# self.InsertSymbol(ch, len)
else:
ch = '\xAD'
self.InsertSymbol(ch)
def InsertFallingIntonation(self):
""" Insert the Falling Intonation (Down Arrow) symbol """
# NOTE: Ugly HACK warning.
# The wxSTC on Mac isn't displaying Unicode characters correctly. Therefore, we
# need to send a character that the wxSTC can display, even if it's not actually
# the character we want stored in the database.
if 'unicode' in wx.PlatformInfo:
# This is the "Unicode" way, which works well on Windows and inserts a character that can be saved
# and loaded without difficulty. Unfortunately, the Mac cannot display this character.
# This has been abandoned for now, though might be resurrected if they fix the wxSTC.
ch = unicode('\xe2\x86\x93', 'utf8')
len = 3
self.InsertUnicodeChar(ch, len)
# The new approach, the "Symbol" way, is to insert the appropriate character in the Symbol
# Font to display the up arrow character. This is a different character on Windows than it
# is on Mac. Therefore, there needs to be code in loading and saving files that translates
# this character so that files will work cross-platform.
# if 'wxMac' in wx.PlatformInfo:
# ch = unicode('\xc3\x98', 'utf8')
# len = 2
# else:
# ch = unicode('\xc2\xaf', 'utf8')
# len = 2
# We insert the specified character in Symbol Font.
# self.InsertSymbol(ch, len)
else:
ch = '\xAF'
self.InsertSymbol(ch)
def InsertInBreath(self):
""" Insert the In Breath (Closed Dot) symbol """
if 'unicode' in wx.PlatformInfo:
ch = unicode('\xe2\x80\xa2', 'utf8')
len = 3
self.InsertUnicodeChar(ch, len)
else:
ch = '\xB7'
self.InsertSymbol(ch)
def InsertWhisper(self):
""" Insert the Whisper (Open Dot) symbol """
if 'unicode' in wx.PlatformInfo:
ch = unicode('\xc2\xb0', 'utf8')
len = 2
self.InsertUnicodeChar(ch, len)
else:
ch = chr(176)
self.InsertSymbol(ch)
def InsertSymbol(self, ch, length=1):
""" Insert a character in Symbol Font into the text """
# Find the cursor position
curpos = self.GetCurrentPos()
# Save the current font
f = self.get_font()
# Use symbol font
self.set_font("Symbol", TransanaGlobal.configData.defaultFontSize, 0x000000)
self.InsertStyledText(ch, length)
# restore previous font
self.set_font(f[0], f[1], f[2])
# Position the cursor after the inserted character. Unicode characters may have a length > 1, even though
# they show up as only a single character.
self.GotoPos(curpos + length)
def InsertUnicodeChar(self, ch, length=1):
""" Insert a Unicode character, which may not have a length of 1, into the text """
# Save the current font. If we are at the end of a document and we're not using the default font,
# the font gets changed to the default font by this routine if we don't control for it!
f = self.get_font()
# Find the cursor position
curpos = self.GetCurrentPos()
# Insert the specified character at the cursor position
self.InsertText(curpos, ch)
# Unicode characters are wider than 1 character. Move the cursor the appropriate number of characters.
self.GotoPos(curpos + length)
# restore previous font
self.set_font(f[0], f[1], f[2])
def InsertHiddenText(self, text):
"""Insert hidden text at current cursor position."""
# Determine the current cursor position
curpos = self.GetCurrentPos()
# Insert the text
self.InsertText(curpos, text)
# Determine how much to move the cursor. Start with the length of the text.
offset = len(text)
# Iterate through the characters looking for Unicode characters
for char in text:
if ('unicode' in wx.PlatformInfo) and (ord(char) > 128):
# I think we can get away with adding 1 here because the Time Code character, with a
# width of 2, should be the only Unicode character that gets hidden.
offset += 1
self.StartStyling(curpos, STYLE_MASK)
self.SetStyling(offset, self.STYLE_HIDDEN)
self.GotoPos(curpos+offset)
# Ensure that the OnPosChanged event isn't fooled into switching
# to this new style
self.last_pos = curpos + offset
def InsertStyledText(self, text, length=0):
"""Insert text with the current style."""
# Determine the length if needed
if length == 0:
if isinstance(text, unicode):
length = len(text.encode(TransanaGlobal.encoding)) # Maybe 'utf8' instead of TransanaGlobal.encoding??
else:
length = len(text)
# Determine the current cursor position
curpos = self.GetCurrentPos()
# Insert the text
self.InsertText(curpos, text)
# Determine how much to move the cursor. Start with the length of the text.
if length == 1:
offset = len(text)
# if we're using Unicode and we have a single character and it's a Unicode (double-width) character ...
# Iterate through the characters looking for Unicode characters
# NOTE: This will only work for 2-byte Unicode characters, not 3- or 4-byte characters.
# Those characters MUST pass in a "length" value.
for char in text:
if ('unicode' in wx.PlatformInfo) and (ord(char) > 128):
offset += 1
else:
offset = length
# Apply the desired style to the inserted text
self.StartStyling(curpos, STYLE_MASK)
self.SetStyling(offset, self.style)
# Move the cursor to after the inserted text
self.GotoPos(curpos+offset)
def ClearDoc(self):
"""Clear the document buffer."""
self.__ResetBuffer()
def LoadRTFData(self, data):
"""Load a RTF document into the editor with the document as a
buffer (string)."""
# Signal that we ARE loading data, so that the problem with font specification is avoided.
self.loadingData = True
RichTextEditCtrl.ClearDoc(self) # Don't want to call any parent method too
if DEBUG:
print "RichTextEditCtrl.LoadRTFData()", type(data)
try:
parse = RTFParser.RTFParser()
parse.init_progress_update(self.ProgressDlg.Update, 0, 50)
parse.buf = data
if DEBUG:
print "RichTextEditCtrl.LoadRTFData(): calling parse.read_stream()"
parse.read_stream()
except RTFParser.RTFParseError:
# If the length of the data passed is > 0, display the error message from the Parser
if DEBUG and len(data) > 0:
print sys.exc_info()[0], sys.exc_info()[1]
# If the length of the data is 0, we can safely ignore the parser error.
else:
pass
except:
print "Unhandled/ignored exception in RTF stream parsing"
print sys.exc_info()[0], sys.exc_info()[1]
traceback.print_exc()
else:
if DEBUG:
print "RichTextEditCtrl.LoadRTFData(): calling self.__ParseRTFStream()"
self.__ParseRTFStream(parse.stream)
if DEBUG:
print
print "self.style_specs:"
for i in range(len(self.style_specs)):
print "%3d %s" % (i, self.style_specs[i])
# Okay, we're done loading the data now.
self.loadingData = False
def InsertRTFText(self, text):
""" Add RTF text at the cursor, without clearing the whole document. Used to insert
Clip Transcripts into TextReports. """
# Start exception trapping
try:
# Create an instance of the RTF Parser
parse = RTFParser.RTFParser()
# Assign the RTF Text String to the Parser's buffer
parse.buf = text
# Process the RTF Stream
parse.read_stream()
# Handle exceptions if needed
except:
print "Unhandled/ignored exception in RTF parsing"
traceback.print_exc()
# If no exception occurs ...
else:
# ... Actually parse the RTF Stream and put the text in the RTFTextEditCtrl
self.__ParseRTFStream(parse.stream)
def LoadRTFFile(self, filename):
"""Load a RTF file into the editor."""
RichTextEditCtrl.ClearDoc(self) # Don't want to call any parent method too
if DEBUG:
print "RichTextEditCtrl.LoadRTFFile()", type(data)
try:
parse = RTFParser.RTFParser(filename)
parse.init_progress_update(self.ProgressDlg.Update, 0, 50)
parse.read_stream()
except:
print "Unhandled/ignored exception in RTF parsing"
traceback.print_exc()
else:
self.__ParseRTFStream(parse.stream)
def LoadDocument(self, filename):
"""Load a file into the editor."""
if filename.upper().endswith(".RTF"):
self.LoadRTFFile(filename)
def __ApplyStyle(self, style):
"""Apply the style to the current selection, if any."""
startpos = self.GetSelectionStart()
endpos = self.GetSelectionEnd()
if startpos != endpos:
self.StartStyling(startpos, STYLE_MASK)
self.SetStyling(endpos-startpos, self.style)
def GetBold(self):
"""Get bold state for current font or for the selected text."""
return self.style_attrs[self.style].bold
def GetItalic(self):
"""Get italic state for current font or for the selected text."""
return self.style_attrs[self.style].italic
def GetUnderline(self):
"""Get underline state for current font or for the selected text."""
return self.style_attrs[self.style].underline
def __SetAttrValue(self, attr, value):
"""Set the value of an attribute in the spec. Never remove it.
For example, use attr='face', and value='Times' to change the font
face to Times."""
props = self.style_specs[self.style].split(",")
newprops = []
hadface = 0
for x in props:
if x[:5] == attr + ":":
newprops.append("%s:%s" % (attr, value))
hadface = 1
else:
newprops.append(x)
if not hadface:
newprops.append("%s:%s" % (attr, value))
self.style = self.__GetStyle(string.join(newprops, ","))
def __SetFontFace(self, face):
self.__SetAttrValue("face", face)
return
def __SetFontColor(self, fg, bg):
self.__SetAttrValue("fore", "#%06x" % fg)
self.__SetAttrValue("back", "#%06x" % bg)
def __SetFontSize(self, size):
self.__SetAttrValue("size", "%d" % size)
def __SetAttr(self, attr, state=1):
"""Set a style attribute state for the current style."""
props = self.style_specs[self.style].split(",")
if DEBUG:
print "SetAttr: Existing props: %s" % str(props)
has_attr = props.count(attr) > 0
if state:
if not has_attr:
if DEBUG:
print "SetAttr: Have to add attr to style"
#print "SetAttr: CurrentSpec = %s" % self.__CurrentSpec()
# The problem with using CurrentSpec() is that it depends
# on the current selection or position. I think we want
# to just use the spec from the current style though, and
# let whoever calls this worry about setting it to the
# one that's at the current position or selection or whatever.
#self.style = self.__GetStyle(self.__CurrentSpec() + "," + attr)
self.style = self.__GetStyle(self.style_specs[self.style] + "," + attr)
else:
if has_attr:
if DEBUG:
print "SetAttr: Have to remove attr from style"
props.remove(attr)
self.style = self.__GetStyle(string.join(props, ","))
def SetBold(self, state=1):
"""Set bold state for current font or for the selected text."""
self.SetAttributeSkipTimeCodes("bold", state)
if DEBUG:
print "SetBold: now self.style = %d" % self.style
def SetItalic(self, state=1):
"""Set italic state for current font or for the selected text."""
self.SetAttributeSkipTimeCodes("italic", state)
if DEBUG:
print "SetItalic: now self.style = %d" % self.style
def SetUnderline(self, state=1):
"""Set underline state for current font or for the selected text."""
self.SetAttributeSkipTimeCodes("underline", state)
if DEBUG:
print "SetUnderline: now self.style = %d" % self.style
def SetAttributeSkipTimeCodes(self, attribute, state):
""" Set the supplied attribute for the current text selection, ignoring Time Codes and their hidden data """
# Let's try to remember the cursor position
self.SaveCursor()
# If we have a selection ...
if self.GetSelection()[0] != self.GetSelection()[1]:
# Set the Wait cursor
self.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
# Now we need to iterate through the selection and update the font information.
# It doesn't work to try to apply formatting to the whole block, as time codes get spoiled.
for selPos in range(self.GetSelection()[0], self.GetSelection()[1]):
# We don't want to update the formatting of Time Codes or of hidden Time Code Data.
if not (self.GetStyleAt(selPos) in [self.STYLE_TIMECODE, self.STYLE_HIDDEN]):
# Select the character we want to work on from the larger selection
self.SetSelection(selPos, selPos + 1)
# Set the style appropriately
self.__SetAttr(attribute, state)
self.__ApplyStyle(self.style)
# Set the cursor back to normal
self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
# If we do NOT have a selection ...
else:
# Set the style appropriately
self.__SetAttr(attribute, state)
self.__ApplyStyle(self.style)
# Let's try restoring the Cursor Position when all is said and done.
self.RestoreCursor()
# Signal that the transcript has changed, so that the Save prompt will be displayed if this format
# change is the only edit.
self.stylechange = 1
def SetFont(self, face, size, fg_color, bg_color):
"""Set the font."""
if DEBUG:
print "SetFont: face: %s, size:%d, color fg:#%06x, bg: #%06x" % (face, size, fg_color, bg_color)
if len(face) > 0:
self.__SetFontFace(face)
self.__SetFontColor(fg_color, bg_color)
self.__SetFontSize(size)
if DEBUG:
print "After SetATtr: Style=%d" % self.style