3 This class is used to coordinate updates among all Viewers. Every Viewer must
4 conform to the following interface:
6 - it must include a method called update_yourself() which takes three
7 arguments; the red, green, and blue values of the selected color.
9 - When a Viewer selects a color and wishes to update all other Views, it
10 should call update_views() on the Switchboard object. Note that the
11 Viewer typically does *not* update itself before calling update_views(),
12 since this would cause it to get updated twice.
14 Optionally, Viewers can also implement:
16 - save_options() which takes an optiondb (a dictionary). Store into this
17 dictionary any values the Viewer wants to save in the persistent
18 ~/.pynche file. This dictionary is saved using marshal. The namespace
19 for the keys is ad-hoc; make sure you don't clobber some other Viewer's
22 - withdraw() which takes no arguments. This is called when Pynche is
23 unmapped. All Viewers should implement this.
25 - colordb_changed() which takes a single argument, an instance of
26 ColorDB. This is called whenever the color name database is changed and
27 gives a chance for the Viewers to do something on those events. See
28 ListViewer for details.
30 External Viewers are found dynamically. Viewer modules should have names such
31 as FooViewer.py. If such a named module has a module global variable called
32 ADDTOVIEW and this variable is true, the Viewer will be added dynamically to
33 the `View' menu. ADDTOVIEW contains a string which is used as the menu item
34 to display the Viewer (one kludge: if the string contains a `%', this is used
35 to indicate that the next character will get an underline in the menu,
36 otherwise the first character is underlined).
38 FooViewer.py should contain a class called FooViewer, and its constructor
39 should take two arguments, an instance of Switchboard, and optionally a Tk
45 from types
import DictType
51 def __init__(self
, initfile
):
52 self
.__initfile
= initfile
60 # read the initialization file
66 self
.__optiondb
= marshal
.load(fp
)
67 if not isinstance(self
.__optiondb
, DictType
):
68 print >> sys
.stderr
, \
69 'Problem reading options from file:', initfile
71 except (IOError, EOFError, ValueError):
77 def add_view(self
, view
):
78 self
.__views
.append(view
)
80 def update_views(self
, red
, green
, blue
):
84 for v
in self
.__views
:
85 v
.update_yourself(red
, green
, blue
)
87 def update_views_current(self
):
88 self
.update_views(self
.__red
, self
.__green
, self
.__blue
)
90 def current_rgb(self
):
91 return self
.__red
, self
.__green
, self
.__blue
96 def set_colordb(self
, colordb
):
97 self
.__colordb
= colordb
98 for v
in self
.__views
:
99 if hasattr(v
, 'colordb_changed'):
100 v
.colordb_changed(colordb
)
101 self
.update_views_current()
104 return self
.__optiondb
106 def save_views(self
):
107 # save the current color
108 self
.__optiondb
['RED'] = self
.__red
109 self
.__optiondb
['GREEN'] = self
.__green
110 self
.__optiondb
['BLUE'] = self
.__blue
111 for v
in self
.__views
:
112 if hasattr(v
, 'save_options'):
113 v
.save_options(self
.__optiondb
)
114 # save the name of the file used for the color database. we'll try to
116 self
.__optiondb
['DBFILE'] = self
.__colordb
.filename()
120 fp
= open(self
.__initfile
, 'w')
122 print >> sys
.stderr
, 'Cannot write options to file:', \
125 marshal
.dump(self
.__optiondb
, fp
)
130 def withdraw_views(self
):
131 for v
in self
.__views
:
132 if hasattr(v
, 'withdraw'):
135 def canceled(self
, flag
=1):
136 self
.__canceled
= flag
138 def canceled_p(self
):
139 return self
.__canceled