TextViewer.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """TextViewer class.
  2. The TextViewer allows you to see how the selected color would affect various
  3. characteristics of a Tk text widget. This is an output viewer only.
  4. In the top part of the window is a standard text widget with some sample text
  5. in it. You are free to edit this text in any way you want (BAW: allow you to
  6. change font characteristics). If you want changes in other viewers to update
  7. text characteristics, turn on Track color changes.
  8. To select which characteristic tracks the change, select one of the radio
  9. buttons in the window below. Text foreground and background affect the text
  10. in the window above. The Selection is what you see when you click the middle
  11. button and drag it through some text. The Insertion is the insertion cursor
  12. in the text window (which only has a background).
  13. """
  14. from tkinter import *
  15. import ColorDB
  16. ADDTOVIEW = 'Text Window...'
  17. class TextViewer:
  18. def __init__(self, switchboard, master=None):
  19. self.__sb = switchboard
  20. optiondb = switchboard.optiondb()
  21. root = self.__root = Toplevel(master, class_='Pynche')
  22. root.protocol('WM_DELETE_WINDOW', self.withdraw)
  23. root.title('Pynche Text Window')
  24. root.iconname('Pynche Text Window')
  25. root.bind('<Alt-q>', self.__quit)
  26. root.bind('<Alt-Q>', self.__quit)
  27. root.bind('<Alt-w>', self.withdraw)
  28. root.bind('<Alt-W>', self.withdraw)
  29. #
  30. # create the text widget
  31. #
  32. self.__text = Text(root, relief=SUNKEN,
  33. background=optiondb.get('TEXTBG', 'black'),
  34. foreground=optiondb.get('TEXTFG', 'white'),
  35. width=35, height=15)
  36. sfg = optiondb.get('TEXT_SFG')
  37. if sfg:
  38. self.__text.configure(selectforeground=sfg)
  39. sbg = optiondb.get('TEXT_SBG')
  40. if sbg:
  41. self.__text.configure(selectbackground=sbg)
  42. ibg = optiondb.get('TEXT_IBG')
  43. if ibg:
  44. self.__text.configure(insertbackground=ibg)
  45. self.__text.pack()
  46. self.__text.insert(0.0, optiondb.get('TEXT', '''\
  47. Insert some stuff here and play
  48. with the buttons below to see
  49. how the colors interact in
  50. textual displays.
  51. See how the selection can also
  52. be affected by tickling the buttons
  53. and choosing a color.'''))
  54. insert = optiondb.get('TEXTINS')
  55. if insert:
  56. self.__text.mark_set(INSERT, insert)
  57. try:
  58. start, end = optiondb.get('TEXTSEL', (6.0, END))
  59. self.__text.tag_add(SEL, start, end)
  60. except ValueError:
  61. # selection wasn't set
  62. pass
  63. self.__text.focus_set()
  64. #
  65. # variables
  66. self.__trackp = BooleanVar()
  67. self.__trackp.set(optiondb.get('TRACKP', 0))
  68. self.__which = IntVar()
  69. self.__which.set(optiondb.get('WHICH', 0))
  70. #
  71. # track toggle
  72. self.__t = Checkbutton(root, text='Track color changes',
  73. variable=self.__trackp,
  74. relief=GROOVE,
  75. command=self.__toggletrack)
  76. self.__t.pack(fill=X, expand=YES)
  77. frame = self.__frame = Frame(root)
  78. frame.pack()
  79. #
  80. # labels
  81. self.__labels = []
  82. row = 2
  83. for text in ('Text:', 'Selection:', 'Insertion:'):
  84. l = Label(frame, text=text)
  85. l.grid(row=row, column=0, sticky=E)
  86. self.__labels.append(l)
  87. row += 1
  88. col = 1
  89. for text in ('Foreground', 'Background'):
  90. l = Label(frame, text=text)
  91. l.grid(row=1, column=col)
  92. self.__labels.append(l)
  93. col += 1
  94. #
  95. # radios
  96. self.__radios = []
  97. for col in (1, 2):
  98. for row in (2, 3, 4):
  99. # there is no insertforeground option
  100. if row==4 and col==1:
  101. continue
  102. r = Radiobutton(frame, variable=self.__which,
  103. value=(row-2)*2 + col-1,
  104. command=self.__set_color)
  105. r.grid(row=row, column=col)
  106. self.__radios.append(r)
  107. self.__toggletrack()
  108. def __quit(self, event=None):
  109. self.__root.quit()
  110. def withdraw(self, event=None):
  111. self.__root.withdraw()
  112. def deiconify(self, event=None):
  113. self.__root.deiconify()
  114. def __forceupdate(self, event=None):
  115. self.__sb.update_views_current()
  116. def __toggletrack(self, event=None):
  117. if self.__trackp.get():
  118. state = NORMAL
  119. fg = self.__radios[0]['foreground']
  120. else:
  121. state = DISABLED
  122. fg = self.__radios[0]['disabledforeground']
  123. for r in self.__radios:
  124. r.configure(state=state)
  125. for l in self.__labels:
  126. l.configure(foreground=fg)
  127. def __set_color(self, event=None):
  128. which = self.__which.get()
  129. text = self.__text
  130. if which == 0:
  131. color = text['foreground']
  132. elif which == 1:
  133. color = text['background']
  134. elif which == 2:
  135. color = text['selectforeground']
  136. elif which == 3:
  137. color = text['selectbackground']
  138. elif which == 5:
  139. color = text['insertbackground']
  140. try:
  141. red, green, blue = ColorDB.rrggbb_to_triplet(color)
  142. except ColorDB.BadColor:
  143. # must have been a color name
  144. red, green, blue = self.__sb.colordb().find_byname(color)
  145. self.__sb.update_views(red, green, blue)
  146. def update_yourself(self, red, green, blue):
  147. if self.__trackp.get():
  148. colorname = ColorDB.triplet_to_rrggbb((red, green, blue))
  149. which = self.__which.get()
  150. text = self.__text
  151. if which == 0:
  152. text.configure(foreground=colorname)
  153. elif which == 1:
  154. text.configure(background=colorname)
  155. elif which == 2:
  156. text.configure(selectforeground=colorname)
  157. elif which == 3:
  158. text.configure(selectbackground=colorname)
  159. elif which == 5:
  160. text.configure(insertbackground=colorname)
  161. def save_options(self, optiondb):
  162. optiondb['TRACKP'] = self.__trackp.get()
  163. optiondb['WHICH'] = self.__which.get()
  164. optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c')
  165. optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2]
  166. optiondb['TEXTINS'] = self.__text.index(INSERT)
  167. optiondb['TEXTFG'] = self.__text['foreground']
  168. optiondb['TEXTBG'] = self.__text['background']
  169. optiondb['TEXT_SFG'] = self.__text['selectforeground']
  170. optiondb['TEXT_SBG'] = self.__text['selectbackground']
  171. optiondb['TEXT_IBG'] = self.__text['insertbackground']