summaryrefslogtreecommitdiffstats
path: root/Mac/scripts/MkDistr_ui.py
blob: 6ca788c9339ce344a512567a7c4293cbc1afdb83 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#
# MkDistr - User Interface.
#
# Jack Jansen, CWI, August 1995
#
# XXXX To be done (requires mods of FrameWork and toolbox interfaces too):
# - Give dialogs titles (need dlg->win conversion)
# - Place dialogs better (???)
# - <return> as <ok>
# - big box around ok button
# - window-close crashes on reopen (why?)
# - Box around lists (???)
# - Change cursor while busy (need cursor support in Qd)
#
import Res
import Dlg
import Ctl
import List
import Win
import Qd
from FrameWork import *
import EasyDialogs
import macfs
import os
import sys

# Resource IDs
ID_MAIN = 514
MAIN_LIST=1
MAIN_MKDISTR=2
MAIN_CHECK=3
MAIN_INCLUDE=4
MAIN_EXCLUDE=5

ID_INCWINDOW=515
ID_EXCWINDOW=517
INCEXC_DELETE=2
INCEXC_CHANGE=3
INCEXC_ADD=4

ID_INCLUDE=512
ID_EXCLUDE=513
DLG_OK=1 # Include for include, exclude for exclude
DLG_CANCEL=2
DLG_SRCPATH=3
DLG_DSTPATH=4 # include dialog only
DLG_EXCLUDE=5 # Exclude, include dialog only

ID_DTYPE=516
DTYPE_EXIST=1
DTYPE_NEW=2
DTYPE_CANCEL=3

class EditDialogWindow(DialogWindow):
	"""Include/exclude editor (modeless dialog window)"""
	
	def open(self, id, (src, dst), callback, cancelrv):
		self.id = id
		self.callback = callback
		self.cancelrv = cancelrv
		DialogWindow.open(self, id)
		tp, h, rect = self.wid.GetDialogItem(DLG_SRCPATH)
		Dlg.SetDialogItemText(h, src)
		self.wid.SetDialogDefaultItem(DLG_OK)
		self.wid.SetDialogCancelItem(DLG_CANCEL)
		if id == ID_INCLUDE:
			tp, h, rect = self.wid.GetDialogItem(DLG_DSTPATH)
			if dst == None:
				dst = ''
			Dlg.SetDialogItemText(h, dst)
		self.wid.DrawDialog()
	
	def do_itemhit(self, item, event):
		if item in (DLG_OK, DLG_CANCEL, DLG_EXCLUDE):
			self.done(item)
		# else it is not interesting
		
	def done(self, item):
		tp, h, rect = self.wid.GetDialogItem(DLG_SRCPATH)
		src = Dlg.GetDialogItemText(h)
		if item == DLG_OK:
			if self.id == ID_INCLUDE:
				tp, h, rect = self.wid.GetDialogItem(DLG_DSTPATH)
				dst = Dlg.GetDialogItemText(h)
				rv = (src, dst)
			else:
				rv = (src, None)
		elif item == DLG_EXCLUDE:
			rv = (src, None)
		else:
			rv = self.cancelrv
		self.close()
		self.callback((item in (DLG_OK, DLG_EXCLUDE)), rv)
		
class ListWindow(DialogWindow):
	"""A dialog window containing a list as its main item"""
	
	def open(self, id, contents):
		self.id = id
		DialogWindow.open(self, id)
		Qd.SetPort(self.wid)
		tp, h, rect = self.wid.GetDialogItem(MAIN_LIST)
		self.listrect = rect
		rect2 = rect[0]+1, rect[1]+1, rect[2]-16, rect[3]-16	# Scroll bar space
		self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid,
				0, 1, 1, 1)
		self.setlist(contents)

	def setlist(self, contents):
		self.list.LDelRow(0, 0)
		self.list.LSetDrawingMode(0)
		if contents:
			self.list.LAddRow(len(contents), 0)
			for i in range(len(contents)):
				self.list.LSetCell(contents[i], (0, i))
		self.list.LSetDrawingMode(1)
		##self.list.LUpdate(self.wid.GetWindowPort().visRgn)
		Win.InvalRect(self.listrect)
		
	def additem(self, item):
		where = self.list.LAddRow(1, 0)
		self.list.LSetCell(item, (0, where))
		
	def delgetitem(self, item):
		data = self.list.LGetCell(1000, (0, item))
		self.list.LDelRow(1, item)
		return data
		
	def do_listhit(self, event):
		(what, message, when, where, modifiers) = event
		Qd.SetPort(self.wid)
		where = Qd.GlobalToLocal(where)
		if self.list.LClick(where, modifiers):
			self.do_dclick(self.delgetselection())
		
	def delgetselection(self):
		items = []
		point = (0,0)
		while 1:
			ok, point = self.list.LGetSelect(1, point)
			if not ok:
				break
			items.append(point[1])
			point = point[0], point[1]+1
		values = []
		items.reverse()
		for i in items:
			values.append(self.delgetitem(i))
		return values
		
	def do_rawupdate(self, window, event):
		Qd.SetPort(window)
		Qd.FrameRect(self.listrect)
		self.list.LUpdate(self.wid.GetWindowPort().visRgn)
		
	def do_close(self):
		self.close()
		
	def close(self):
		del self.list
		DialogWindow.close(self)
		
	def mycb_add(self, ok, item):
		if item:
			self.additem(item[0])
			self.cb_add(item)
		
class MainListWindow(ListWindow):
	"""The main window"""

	def open(self, id, cb_check, cb_run, cb_add):
		ListWindow.open(self, id, [])
		self.wid.SetDialogDefaultItem(ID_INCLUDE)
		self.cb_run = cb_run
		self.cb_check = cb_check
		self.cb_add = cb_add
		setwatchcursor()
		list = self.cb_check()
		self.setlist(list)
		setarrowcursor()

	def do_itemhit(self, item, event):
		if item == MAIN_LIST:
			self.do_listhit(event)
		if item == MAIN_MKDISTR:
## XXXX This somehow stopped working...
##			fss, ok = macfs.StandardPutFile('Destination folder:')
##			if not ok:
##				return
##			dest = fss.as_pathname()
			dest = os.path.join(os.getcwd(), 'Distribution')
			setwatchcursor()
			self.cb_run(dest)
			setarrowcursor()
		if item == MAIN_CHECK:
			setwatchcursor()
			list = self.cb_check()
			self.setlist(list)
			setarrowcursor()
		if item == MAIN_INCLUDE:
			self.do_dclick(self.delgetselection())
		if item == MAIN_EXCLUDE:
			for i in self.delgetselection():
				self.cb_add((i, None))
			
	def do_dclick(self, list):
		if not list:
			list = ['']
		for l in list:
			w = EditDialogWindow(self.parent)
			w.open(ID_INCLUDE, (l, None), self.mycb_add, None)

	def mycb_add(self, ok, item):
		if item:
			self.cb_add(item)

class IncListWindow(ListWindow):
	"""An include/exclude window"""
	def open(self, id, editid, contents, cb_add, cb_del, cb_get):
		ListWindow.open(self, id, contents)
		self.wid.SetDialogDefaultItem(INCEXC_CHANGE)
		self.editid = editid
		self.cb_add = cb_add
		self.cb_del = cb_del
		self.cb_get = cb_get

	def do_itemhit(self, item, event):
		if item == MAIN_LIST:
			self.do_listhit(event)
		if item == INCEXC_DELETE:
			old = self.delgetselection()
			for i in old:
				self.cb_del(i)
		if item == INCEXC_CHANGE:
			self.do_dclick(self.delgetselection())
		if item == INCEXC_ADD:
			w = EditDialogWindow(self.parent)
			w.open(self.editid, ('', None), self.mycb_add, None)
			
	def do_dclick(self, list):
		if not list:
			list = ['']
		for l in list:
			old = self.cb_get(l)
			self.cb_del(l)
			w = EditDialogWindow(self.parent)
			w.open(self.editid, old, self.mycb_add, old)

class MkDistrUI(Application):
	def __init__(self, main):
		self.main = main
		Application.__init__(self)
		self.mwin = MainListWindow(self)
		self.mwin.open(ID_MAIN, self.main.check, self.main.run, self.main.inc.add)
		self.iwin = None
		self.ewin = None	
		
	def makeusermenus(self):
		self.filemenu = m = Menu(self.menubar, "File")
		self.includeitem = MenuItem(m, "Show Include window", "", self.showinc)
		self.excludeitem = MenuItem(m, "Show Exclude window", "", self.showexc)
		self.saveitem = MenuItem(m, "Save databases", "S", self.save)
		self.quititem = MenuItem(m, "Quit", "Q", self.quit)
		
	def quit(self, *args):
		if self.main.is_modified():
			rv = EasyDialogs.AskYesNoCancel('Database modified. Save?', -1)
			if rv == -1:
				return
			if rv == 1:
				self.main.save()
		raise self
		
	def save(self, *args):
		self.main.save()
		
	def showinc(self, *args):
		if self.iwin:
			if self._windows.has_key(self.iwin):
				self.iwin.close()
			del self.iwin
		self.iwin = IncListWindow(self)
		self.iwin.open(ID_INCWINDOW, ID_INCLUDE, self.main.inc.getall(), self.main.inc.add,
			self.main.inc.delete, self.main.inc.get)
		
	def showexc(self, *args):
		if self.ewin:
			if self._windows.has_key(self.ewin):
				self.ewin.close()
			del self.ewin
		self.ewin = IncListWindow(self)
		self.ewin.open(ID_EXCWINDOW, ID_EXCLUDE, self.main.exc.getall(), self.main.exc.add,
			self.main.exc.delete, self.main.exc.get)

	def do_about(self, id, item, window, event):
		EasyDialogs.Message("Test the MkDistr user interface.")
		
def GetType():
	"""Ask user for distribution type"""
	while 1:
		d = Dlg.GetNewDialog(ID_DTYPE, -1)
		d.SetDialogDefaultItem(DTYPE_EXIST)
		d.SetDialogCancelItem(DTYPE_CANCEL)
		while 1:
			rv = ModalDialog(None)
			if rv in (DTYPE_EXIST, DTYPE_NEW, DTYPE_CANCEL):
				break
		del d
		if rv == DTYPE_CANCEL:
			sys.exit(0)
		if rv == DTYPE_EXIST:
##			macfs.SetFolder(':(MkDistr)')
			fss, ok = macfs.StandardGetFile('TEXT')
			if not ok:
				sys.exit(0)
			path = fss.as_pathname()
			basename = os.path.split(path)[-1]
			if basename[-8:] <> '.include':
				EasyDialogs.Message('That is not a distribution include file')
			else:
				return basename[:-8]
		else:
			name = EasyDialogs.AskString('Distribution name:')
			if name:
				return name
			sys.exit(0)
			
def InitUI():
	"""Initialize stuff needed by UI (a resource file)"""
	Res.OpenResFile('MkDistr.rsrc')

class _testerhelp:
	def __init__(self, which):
		self.which = which
		
	def get(self):
		return [self.which+'-one', self.which+'-two']
		
	def add(self, value):
		if value:
			print 'ADD', self.which, value
			
	def delete(self, value):
		print 'DEL', self.which, value
		
class _test:
	def __init__(self):
		import sys
		Res.OpenResFile('MkDistr.rsrc')
		self.inc = _testerhelp('include')
		self.exc = _testerhelp('exclude')
		self.ui = MkDistrUI(self)
		self.ui.mainloop()
		sys.exit(1)
		
	def check(self):
		print 'CHECK'
		return ['rv1', 'rv2']
		
	def run(self):
		print 'RUN'
		
if __name__ == '__main__':
	_test()