summaryrefslogtreecommitdiffstats
path: root/iw/mouse.py
blob: a145b5c0b15c9c1c180a1add67c93dad0a21223a (plain)
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
from gtk import *
from iw import *
from string import *
from re import *
import tree

class MouseWindow (InstallWindow):

    def reduce_leafs (self, a):
        if a == (): return a
        if len (a) > 1 and isinstance (a[1], type (())) and len (a[1]) == 1:
            return ("%s - %s" % (a[0], a[1][0]),) + self.reduce_leafs (a[2:])
        return (a[0],) + self.reduce_leafs (a[1:])

    def build_ctree (self, list, cur_parent = None, prev_node = None):
        if (list == ()): return
        
        if (len (list) > 1 and isinstance (list[1], type (()))): 
	    leaf = FALSE
        else:
	    leaf = TRUE
    
        if isinstance (list[0], type (())):
            self.build_ctree (list[0], prev_node, None)
            self.build_ctree (list[1:], cur_parent, None)
        else:
            index = find (list[0], " - ")
            if index != -1:
                list_item = list[0][0:index] + list[0][index+2:]
            else:
                list_item = list[0]
            node = self.ctree.insert_node (cur_parent, None, (list_item,), 2, is_leaf=leaf)
            self.ctree.node_set_row_data (node, list[0])
            self.build_ctree (list[1:], cur_parent, node)

    def selectMouse (self, ctreeNode, mouseNode):
        if len (ctreeNode) == 0 or len (mouseNode) == 0: return
        
        nodeLabel = self.ctree.get_node_info (ctreeNode[0])[0]
        if nodeLabel == mouseNode[0]:
            if len (mouseNode) == 1:
                self.ctree.select (ctreeNode[0])
                return
            else:
                self.ctree.expand (ctreeNode[0])
                self.selectMouse (ctreeNode[0].children, mouseNode[1:])
        else:
            self.selectMouse (ctreeNode[1:], mouseNode)


    def __init__ (self, ics):
	InstallWindow.__init__ (self, ics)

        ics.setTitle ("Mouse Configuration")
        ics.readHTML ("mouse")
        ics.setNextEnabled (TRUE)

    def getCurrentKey (self):
        if not len (self.ctree.selection): return
        name = ""
        node = self.ctree.selection[0]
        while node:
            name = self.ctree.node_get_row_data (node) + name
            node = node.parent
            if node:
                name = " - " + name

	if self.locList.selection:
	    self.serialDevice = self.locList.get_text (self.locList.selection[0], 0)
	# otherwise, just leave the old selection in place

        return name

    def getNext (self):
	if not self.__dict__.has_key("availableMice"): return
	cur = self.getCurrentKey()
	(gpm, xdev, device, emulate) = self.availableMice[cur]
        self.todo.mouse.set (cur, self.emulate3.get_active ())
	if (device == "ttyS"):
	    self.todo.mouse.setDevice(self.serialDevice)
	else:
	    self.todo.mouse.setDevice(device)
        return None
    
    def selectDeviceType(self, *args):
	self.ics.setNextEnabled (TRUE)

    def selectMouseType (self, widget, node, *args):
        if not node.is_leaf:
	    self.locList.unselect_all ()
	    self.locList.set_sensitive (FALSE)
            self.emulate3.set_sensitive (FALSE)
            self.ics.setNextEnabled (FALSE)
	    return

	cur = self.getCurrentKey()
	if (not self.availableMice.has_key(cur)):
            self.ics.setNextEnabled (FALSE)
	    return

        self.emulate3.set_sensitive (TRUE)
	(gpm, xdev, device, emulate) = self.availableMice[cur]
        self.emulate3.set_active (emulate)
	if device == "ttyS":
	    print "device is", self.serialDevice
	    if (self.serialDevice):
		self.locList.select_row(int(self.serialDevice[4]), 1)
		self.ics.setNextEnabled (TRUE)
	    else:
		self.locList.unselect_all()
		print "disabling next"
		self.ics.setNextEnabled (FALSE)

	    self.locList.set_sensitive (TRUE)
	else:
	    self.locList.unselect_all()
	    self.locList.set_sensitive(FALSE)
	    self.ics.setNextEnabled (TRUE)
	
    def getScreen (self):
	self.availableMice = self.todo.mouse.available()
        sorted_mice_keys = self.availableMice.keys()
        sorted_mice_keys.sort ()

        currentDev = self.todo.mouse.getDevice ()
	(currentMouse, emulate3) = self.todo.mouse.get ()

	deviceList = [ ("/dev/ttyS0 (COM1 under DOS)", "ttyS0" ),
    		       ("/dev/ttyS1 (COM2 under DOS)", "ttyS1" ),
		       ("/dev/ttyS2 (COM3 under DOS)", "ttyS2" ),
		       ("/dev/ttyS3 (COM4 under DOS)", "ttyS3" ) ]

        self.emulate3 = GtkCheckButton ("Emulate 3 Buttons")
        box = GtkVBox (FALSE)
        
        sw = GtkScrolledWindow ()
        sw.set_border_width (5)
        sw.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC)
        self.locList = GtkCList (2, ("Port", "Device"))
        self.locList.set_selection_mode (SELECTION_SINGLE)

	for (descrip, dev) in deviceList:
	    self.locList.append((dev, descrip))

        self.locList.columns_autosize ()
        self.locList.set_column_resizeable (0, FALSE)
        self.locList.column_title_passive (0)
        self.locList.column_title_passive (1)
        self.locList.set_border_width (5)

        sw = GtkScrolledWindow ()
        sw.set_border_width (5)
        sw.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC)
        self.ctree = GtkCTree (1)

        groups = ()
        for x in sorted_mice_keys:
            groups = tree.merge (groups, string.split (x, " - ", 1))
        groups = self.reduce_leafs (groups)

        self.build_ctree (groups)
        self.ctree.set_selection_mode (SELECTION_BROWSE)
        self.ctree.columns_autosize ()
        self.ctree.connect ("tree_select_row", self.selectMouseType)
        self.locList.connect ("select_row", self.selectDeviceType)
	self.locList.set_sensitive(FALSE)

        sw.add (self.ctree)

	if (currentDev and currentDev[0:3] == "ttyS"):
	    self.serialDevice = currentDev
	    self.locList.select_row(int(self.serialDevice[4]), 1)
	else:
	    self.locList.unselect_all();
	    self.serialDevice = None

	splitv = string.split (currentMouse, " - ", 1)
	nodes = self.ctree.base_nodes ()
        # do a simple search on the root nodes, since leaf reduction creates
        # a special case
        found = 0
	for x in nodes:
            if self.ctree.get_node_info (x)[0] == "%s %s" % tuple (splitv):
                found = 1
                self.ctree.select (x)
                break
        if not found:
            self.selectMouse (nodes, splitv)

        align = GtkAlignment ()
        align.add (self.emulate3)
        align.set_border_width (5)

        im = self.ics.readPixmap ("gnome-mouse.png")
        if im:
            im.render ()
            pix = im.make_pixmap ()
            a = GtkAlignment ()
            a.add (pix)
            a.set (0.0, 0.0, 0.0, 0.0)
            box.pack_start (a, FALSE)
            
        box.pack_start (sw)
        box.pack_start (self.locList, FALSE)
        box.pack_start (align, FALSE)

        return box