summaryrefslogtreecommitdiffstats
path: root/Mac/Unsupported/twit/mac_widgets.py
blob: d52748bc23ca22ccdea6a1bfc656c79ac16f48d2 (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
from FrameWork import *
import Win
import Qd
import Controls
import Ctl
import TE
import List
import os
import string
import macfs

SCROLLBAR=16
MARGIN=2
ICONSIZE=16
TEXTWIDTH=4096 # More-or-less random value

TEXTFONT=4
TEXTSIZE=9

# Resource numbers
PIC_CURRENT=500
PIC_BREAK=501

picture_cache={}

class MT_TextWidget:
	def __init__(self, wid, r):
		self.wid = wid
		self.rect = r
		left, top, right, bottom = r
		self.terect = left+MARGIN+ICONSIZE, top+MARGIN, \
				right-(MARGIN+SCROLLBAR), bottom-(MARGIN+SCROLLBAR)
		dr = self.terect[0], self.terect[1], TEXTWIDTH, self.terect[3]
		Qd.SetPort(wid)
		Qd.TextFont(TEXTFONT)
		Qd.TextSize(TEXTSIZE)
		self.ted = TE.TENew(dr, self.terect)
		self.ted.TEAutoView(1)
		self.activate(1)
		
		rect = right-SCROLLBAR, top, right, bottom-SCROLLBAR+1
		self.bary = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0)
		rect = left, bottom-SCROLLBAR, right-SCROLLBAR+1, bottom
		self.barx = Ctl.NewControl(self.wid, rect, "", 1, 0, 0, 0, 16, 0)
		
		self.have_data = 0
		self.line_index = []
		
	def close(self):
		del self.barx
		del self.bary
		del self.ted
		
	def scrollbars(self):
		pass
		
	def setcontent(self, file):
		self.line_index = []
		if file == None:
			data = ''
			self.have_data = 0
		else:
			try:
				fp = open(file, 'rb') # NOTE the binary
				data = fp.read()
				self.have_data = 1
			except IOError, arg:
				data = 'Cannot open file:\r'+`arg`
				self.have_data = 0
		if len(data) > 32767:
			self.have_data = 0
			data = 'File too big'
		self.ted.TESetText(data)
		if self.have_data:
			cur = 0
			while 1:
				self.line_index.append(cur)
				try:
					cur = string.index(data, '\r', cur+1)
				except ValueError:
					break
			self.line_index.append(len(data))
		self.wid.InvalWindowRect(self.rect)
		self.ted.TESetSelect(0,0)
		self.ted.TECalText()
		self.ted.TESelView()
		self.setscrollbars()
		
	def setscrollbars(self):
		docleft, doctop, docright, docbot = self.ted.destRect
		winleft, wintop, winright, winbot = self.ted.viewRect
		docbot = self.ted.nLines*self.ted.lineHeight + doctop
		self.setbar(self.barx, docleft, docright, winleft, winright)
		self.setbar(self.bary, doctop, docbot, wintop, winbot)
		
	def setbar(self, bar, minmin, maxmax, curmin, curmax):
		if maxmax-minmin > 32767 or (curmin <= minmin and curmax >= maxmax):
			bar.SetControlMinimum(0)
			bar.SetControlMaximum(0)
			bar.SetControlValue(0)
			return
		bar.SetControlMinimum(minmin)
		bar.SetControlMaximum(maxmax-(curmax-curmin))
		bar.SetControlValue(curmin)

	def update(self, rgn):
		Qd.EraseRect(self.terect)
		Qd.FrameRect(self.rect)
		self.ted.TEUpdate(self.terect)
		
	def activate(self, onoff):
		if onoff:
			self.ted.TEActivate()
		else:
			self.ted.TEDeactivate()

	def select(self, line):
		if line == None or line <= 0 or not self.have_data:
			self.ted.TESetSelect(0,0)
		else:
			line = line - 1
			if line > len(self.line_index)-1: line = len(self.line_index)-1
			if line == 1:
				self.ted.TESetSelect(0, self.line_index[1])
			else:
				self.ted.TESetSelect(self.line_index[line]+1, self.line_index[line+1])
		self.setscrollbars()
		
	def click(self, where, modifiers):
		# First check scrollbars
		ctltype, control = Ctl.FindControl(where, self.wid)
		if ctltype and control:
			partcode = control.TrackControl(where)
			if partcode:
				self.controlhit(control, partcode)
			return None, 0
		off = self.ted.TEGetOffset(where)
		inborder = where[0] < self.terect[0]
		l, t, r, b = self.terect
		if l <= where[0] <= r and t <= where[1] <= b or inborder:
			return self.offsettoline(off), inborder
		return None, 0	# In the grow box or something.
		
	def offsettoline(self, offset):
		for i in range(len(self.line_index)):
			if offset < self.line_index[i]:
				return i   # Not i-1: 1-based line numbers in files
		return None

	def controlhit(self, control, partcode):
		if partcode <> Controls.inThumb:
			if control == self.barx:
				if partcode == Controls.inUpButton:
					delta = -10
				if partcode == Controls.inDownButton:
					delta = 10
				if partcode == Controls.inPageUp:
					delta = 10-(self.terect[2]-self.terect[0])
				if partcode == Controls.inPageDown:
					delta = (self.terect[2]-self.terect[0])-10
				old = control.GetControlValue()
				control.SetControlValue(old+delta)
			if control == self.bary:
				if partcode == Controls.inUpButton:
					delta = -self.ted.lineHeight
				if partcode == Controls.inDownButton:
					delta = self.ted.lineHeight
				if partcode == Controls.inPageUp:
					delta = self.ted.lineHeight-(self.terect[3]-self.terect[1])
				if partcode == Controls.inPageDown:
					delta = (self.terect[3]-self.terect[1])-self.ted.lineHeight
				old = control.GetControlValue()
				control.SetControlValue(old+delta)
		newx = self.barx.GetControlValue()
		newy = self.bary.GetControlValue()
		oldx = self.ted.viewRect[0]
		oldy = self.ted.viewRect[1]
		self.ted.TEPinScroll(oldx-newx, oldy-newy)
		self.setscrollbars() # XXXX Bibbert, maar hoe anders?
			
class MT_IconTextWidget(MT_TextWidget):
	def __init__(self, wid, r):
		MT_TextWidget.__init__(self, wid, r)
		self.breakpointlist = []
		self.curline = None
		self.iconrect = (self.rect[0]+1, self.rect[1]+1, 
				self.terect[0]-1, self.rect[3]-SCROLLBAR)
		self.curlinerange = (self.terect[1]+self.ted.lineHeight,
				self.terect[3]-2*self.ted.lineHeight)
		self.piccurrent = PIC_CURRENT
		
	def setbreaks(self, list):
		self.breakpointlist = list[:]
		Qd.SetPort(self.wid)
		self.wid.InvalWindowRect(self.iconrect)
		
	def setcurline(self, line, pic=PIC_CURRENT):
		self.curline = line
		self.piccurrent = pic
		Qd.SetPort(self.wid)
		self.showline(line)

	def showline(self, line):
		if line <= 0: line = 1
		if line >= len(self.line_index): line = len(self.line_index)-1
		if line < 0: return
		off = self.line_index[line]
		x, y = self.ted.TEGetPoint(off)
		if self.curlinerange[0] <= y <= self.curlinerange[1]:
			return # It is in view
		middle = (self.curlinerange[0]+self.curlinerange[1])/2
		self.ted.TEPinScroll(0, middle-y) # Of andersom?
		self.setscrollbars()
		
	def setscrollbars(self):
		MT_TextWidget.setscrollbars(self)
		self.wid.InvalWindowRect(self.iconrect)
				
	def update(self, rgn):
		MT_TextWidget.update(self, rgn)
		self.drawallicons()
		
	def drawallicons(self):
		Qd.EraseRect(self.iconrect)
		Qd.MoveTo(self.iconrect[2], self.iconrect[1])
		Qd.LineTo(self.iconrect[2], self.iconrect[3])
		topoffset = self.ted.TEGetOffset((self.terect[0], self.terect[1]))
		botoffset = self.ted.TEGetOffset((self.terect[0], self.terect[3]))
		topline = self.offsettoline(topoffset)
		botline = self.offsettoline(botoffset)
		if topline == None: topline = 1 # ???
		if botline == None: botline = len(self.line_index)
		for i in self.breakpointlist:
			if topline <= i <= botline:
				self.draw1icon(i, PIC_BREAK)
		if self.curline <> None and topline <= self.curline <= botline:
			self.draw1icon(self.curline, self.piccurrent)
			
	def draw1icon(self, line, which):
		offset = self.line_index[line]
		botx, boty = self.ted.TEGetPoint(offset)
		rect = self.rect[0]+2, boty-self.ted.lineHeight, \
			self.rect[0]+ICONSIZE-2, boty
		if not picture_cache.has_key(which):
			picture_cache[which] = Qd.GetPicture(which)
		self.drawicon(rect, picture_cache[which])
		
	def drawicon(self, rect, which):
		Qd.DrawPicture(which, rect)

class MT_IndexList:
	def __init__(self, wid, rect, width):
		# wid is the window (dialog) where our list is going to be in
		# rect is it's item rectangle (as in dialog item)
		self.rect = rect
		rect2 = rect[0]+1, rect[1]+1, rect[2]-16, rect[3]-1
		self.list = List.LNew(rect2, (0, 0, width, 0), (0,0), 0, wid,
					0, 1, 0, 1)
		self.wid = wid
		self.width = width
	
	def setcontent(self, *content):
		self.list.LDelRow(0, 1)
		self.list.LSetDrawingMode(0)
		self.list.LAddRow(len(content[0]), 0)
		for x in range(len(content)):
			column = content[x]
			for y in range(len(column)):
				self.list.LSetCell(column[y], (x, y))
		self.list.LSetDrawingMode(1)
		self.wid.InvalWindowRect(self.rect)

	def deselectall(self):
		while 1:
			ok, pt = self.list.LGetSelect(1, (0,0))
			if not ok: return
			self.list.LSetSelect(0, pt)
			
	def select(self, num):
		self.deselectall()
		if num < 0:
			return
		for i in range(self.width):
			self.list.LSetSelect(1, (i, num))
			
	def click(self, where, modifiers):
		is_double = self.list.LClick(where, modifiers)
		ok, (x, y) = self.list.LGetSelect(1, (0, 0))
		if ok:
			return y, is_double
		else:
			return None, is_double
			
	# draw a frame around the list, List Manager doesn't do that
	def drawframe(self):
		Qd.SetPort(self.wid)
		Qd.FrameRect(self.rect)
		
	def update(self, rgn):
		self.drawframe()
		self.list.LUpdate(rgn)
		
	def activate(self, onoff):
		self.list.LActivate(onoff)
		
class MT_AnyList(MT_IndexList):

	def click(self, where, modifiers):
		is_double = self.list.LClick(where, modifiers)
		ok, (x, y) = self.list.LGetSelect(1, (0, 0))
		if ok:
			self.select(y)
			field0 = self.list.LGetCell(1000,(0,y))
		else:
			field0 = None
		return field0, is_double