summaryrefslogtreecommitdiffstats
path: root/Mac/Demo/example2/InterslipControl-2.py
blob: 90d57dd728bbf5e485cbe715510ffa1080b3402e (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
import FrameWork
import EasyDialogs
import Res
import Dlg
import sys
import interslip
#
# Definitions for our resources
ID_MAIN=512
ID_ABOUT=513

ITEM_CONNECT=1
ITEM_DISCONNECT=2
ITEM_STATUS=3
ITEM_MESSAGE=4

status2text = ["<idle>", "<wait-modem>", "<dialling>", "<logging in>", 
	"<connected>", "<disconnecting>"]

def main():
	try:
		interslip.open()
	except interslip.error, arg:
		EasyDialogs.Message("Cannot open interslip: "+arg[1])
		sys.exit(1)	
	try:
		dummy = Res.GetResource('DLOG', ID_MAIN)
	except Res.Error:
		try:
			Res.OpenResFile("InterslipControl-2.rsrc")
		except Res.error:
			EasyDialogs.Message("Cannot open InterslipControl-2.rsrc: "+arg[1])
			sys.exit(1)	
	InterslipControl()
	
class InterslipControl(FrameWork.Application):
	"Application class for InterslipControl"
	
	def __init__(self):
		# First init menus, etc.
		FrameWork.Application.__init__(self)
		# Next create our dialog
		self.main_dialog = MyDialog(self)
		# Now open the dialog
		self.main_dialog.open(ID_MAIN)
		# Finally, go into the event loop
		self.mainloop()
	
	def makeusermenus(self):
		self.filemenu = m = FrameWork.Menu(self.menubar, "File")
		self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
	
	def quit(self, *args):
		self._quit()
		
	def do_about(self, *args):
		f = Dlg.GetNewDialog(ID_ABOUT, -1)
		while 1:
			n = Dlg.ModalDialog(None)
			if n == 1:
				return
				
	def idle(self, event):
		"Idle routine - update status"
		self.main_dialog.updatestatus()
		
class MyDialog(FrameWork.DialogWindow):
	"Main dialog window for InterslipControl"
	def __init__(self, parent):
		FrameWork.DialogWindow.__init__(self, parent)
		self.last_status = None
		self.last_msgnum = None
	
	def do_itemhit(self, item, event):
		if item == ITEM_DISCONNECT:
			self.disconnect()
		elif item == ITEM_CONNECT:
			self.connect()

	def connect(self):
		try:
			interslip.connect()
		except interslip.error, arg:
			EasyDialogs.Message("Cannot connect: "+arg[1])

	def disconnect(self):
		try:
			interslip.disconnect()
		except interslip.error, arg:
			EasyDialogs.Message("Cannot disconnect: "+arg[1])
			
	def updatestatus(self):
		try:
			status, msgnum, msg = interslip.status()
		except interslip.error, arg:
			EasyDialogs.Message("Cannot get status: "+arg[1])
			sys.exit(1)
		if status == self.last_status and msgnum == self.last_msgnum:
			return
		self.last_status = status
		self.last_msgnum = msgnum
		if msgnum == 0:
			msg = ''
		
		try:
			txt = status2text[status]
		except IndexError:
			txt = "<unknown state %d>"%status

		tp, h, rect = self.wid.GetDialogItem(ITEM_STATUS)
		Dlg.SetDialogItemText(h, txt)
		
		tp, h, rect = self.wid.GetDialogItem(ITEM_MESSAGE)
		Dlg.SetDialogItemText(h, msg)

main()