blob: 9f2c64c2706fcae4b2dec671f560b5243bbd7b42 [file] [log] [blame]
Barry Warsaw4638c5b1998-10-02 16:20:14 +00001"""Switchboard class.
2
3This class is used to coordinate updates among all Viewers. Every Viewer must
4conform to the following interface:
5
6 - it must include a method called update_yourself() which takes three
7 arguments; the red, green, and blue values of the selected color.
8
9 - When a Viewer selects a color and wishes to update all other Views, it
10 should call update_views() on the Switchboard object. Not that the
11 Viewer typically does *not* update itself before calling update_views(),
12 since this would cause it to get updated twice.
13"""
14
Barry Warsaw4ab5ae21998-10-22 18:48:01 +000015import sys
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000016from types import DictType
17import marshal
18
Barry Warsaw987fb921998-09-28 15:59:21 +000019class Switchboard:
Barry Warsaw0604d721999-04-26 23:17:16 +000020 def __init__(self, initfile):
Barry Warsawca07ba01998-10-22 03:25:59 +000021 self.__initfile = initfile
Barry Warsaw0604d721999-04-26 23:17:16 +000022 self.__colordb = None
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000023 self.__optiondb = {}
24 self.__views = []
Barry Warsawa7ba45b1998-10-01 16:46:43 +000025 self.__red = 0
26 self.__green = 0
27 self.__blue = 0
Barry Warsawca07ba01998-10-22 03:25:59 +000028 self.__canceled = 0
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000029 # read the initialization file
30 fp = None
31 if initfile:
32 try:
33 try:
34 fp = open(initfile)
35 self.__optiondb = marshal.load(fp)
36 if type(self.__optiondb) <> DictType:
Barry Warsaw4ab5ae21998-10-22 18:48:01 +000037 sys.stderr.write(
38 'Problem reading options from file: %s\n' %
39 initfile)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000040 self.__optiondb = {}
41 except (IOError, EOFError):
42 pass
43 finally:
44 if fp:
45 fp.close()
Barry Warsaw987fb921998-09-28 15:59:21 +000046
47 def add_view(self, view):
48 self.__views.append(view)
49
Barry Warsaw1ac18cd1998-09-28 23:41:12 +000050 def update_views(self, red, green, blue):
Barry Warsawa7ba45b1998-10-01 16:46:43 +000051 self.__red = red
52 self.__green = green
53 self.__blue = blue
Barry Warsaw987fb921998-09-28 15:59:21 +000054 for v in self.__views:
Barry Warsaw1ac18cd1998-09-28 23:41:12 +000055 v.update_yourself(red, green, blue)
Barry Warsawfda3ace1998-09-29 20:04:19 +000056
Barry Warsawa7ba45b1998-10-01 16:46:43 +000057 def update_views_current(self):
58 self.update_views(self.__red, self.__green, self.__blue)
59
Barry Warsawcd098671998-10-05 21:14:12 +000060 def current_rgb(self):
61 return self.__red, self.__green, self.__blue
62
Barry Warsawfda3ace1998-09-29 20:04:19 +000063 def colordb(self):
64 return self.__colordb
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000065
Barry Warsaw0604d721999-04-26 23:17:16 +000066 def set_colordb(self, colordb):
67 self.__colordb = colordb
68
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000069 def optiondb(self):
70 return self.__optiondb
71
Barry Warsawca07ba01998-10-22 03:25:59 +000072 def save_views(self):
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000073 # save the current color
74 self.__optiondb['RED'] = self.__red
75 self.__optiondb['GREEN'] = self.__green
76 self.__optiondb['BLUE'] = self.__blue
77 for v in self.__views:
Barry Warsawca07ba01998-10-22 03:25:59 +000078 if hasattr(v, 'save_options'):
79 v.save_options(self.__optiondb)
Barry Warsaw0604d721999-04-26 23:17:16 +000080 # save the name of the file used for the color database. we'll try to
81 # load this first.
82 self.__optiondb['DBFILE'] = self.__colordb.filename()
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000083 fp = None
84 try:
85 try:
Barry Warsawca07ba01998-10-22 03:25:59 +000086 fp = open(self.__initfile, 'w')
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000087 except IOError:
Barry Warsaw4ab5ae21998-10-22 18:48:01 +000088 sys.stderr.write('Cannot write options to file: %s\n' %
89 self.__initfile)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000090 else:
91 marshal.dump(self.__optiondb, fp)
92 finally:
93 if fp:
94 fp.close()
Barry Warsawca07ba01998-10-22 03:25:59 +000095
96 def withdraw_views(self):
97 for v in self.__views:
98 if hasattr(v, 'withdraw'):
99 v.withdraw()
100
Barry Warsaw4ab5ae21998-10-22 18:48:01 +0000101 def canceled(self, flag=1):
102 self.__canceled = flag
Barry Warsawca07ba01998-10-22 03:25:59 +0000103
104 def canceled_p(self):
105 return self.__canceled