EdVisualText.icl 11.1 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1
2
implementation module EdVisualText

3
4
// visual operations on texts

Diederik van Arkel's avatar
Diederik van Arkel committed
5
6
import StdClass, StdFunc, StdArray, StdInt, StdMisc, StdList
import StdIOCommon, StdPicture, StdPSt, StdWindow
7
import EdText, EdTab, StrictList, EdMonad, EdSelection
Diederik van Arkel's avatar
Diederik van Arkel committed
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

//-- util

tabStringWidth :: !Int ![String] !FontInfo -> Int
tabStringWidth startX strings {tabSize, charWidth}
	# widths = map width strings
	= (virtualDraw widths startX) - startX
where
	width s = (size s) * charWidth

	virtualDraw []		_ = 0
	virtualDraw [w]		x = x + w
	virtualDraw [w:ws]	x = virtualDraw ws x`
	where
		x`	= tabWidth * (inc ((x + w) / tabWidth))
	
	tabSize`	= max 1 tabSize			// ensure sensible minimum
	tabWidth	= tabSize` * charWidth

seqmap :: (.a -> .(.b -> .b)) ![.a] !.b -> .b;
seqmap f [] e = e
seqmap f [h:t] e
	#! e = f h e
	= seqmap f t e

// compute the y-coordinate of the base line of a certain text line

computeBaseLine :: !LineNr !FontInfo -> Int
computeBaseLine lineNr { metrics, lineHeight }
  = lineNr * lineHeight + metrics.fAscent + metrics.fLeading

// computeViewDomain computes the rectangle that is needed
// to completely show the current visual text

computeViewDomain :: EditMonad .env ViewDomain 
computeViewDomain 
  = getText			>>>= \text ->
	getFontInfo		>>>= \fontInfo ->
	getLineNumbers	>>>= \lineNumbers ->
	result 
	   { corner1 = { x = if lineNumbers (fontInfo.metrics.fMaxWidth*(-5)) (-4)
				   , y = 0
				   }
	   , corner2 = { x = MAX_LINE_WIDTH
				   , y = fontInfo.lineHeight * textLength text + fontInfo.metrics.fLeading + fontInfo.metrics.fDescent
				   }
	   }
where
	MAX_LINE_WIDTH = 2000

//\\//\\//\\//\\//
Diederik van Arkel's avatar
Diederik van Arkel committed
59
getViewFrame :: EditMonad (PSt .l) ViewFrame
Diederik van Arkel's avatar
Diederik van Arkel committed
60
61
62
63
64
65
66
67
68
69
getViewFrame
  = getWindowId		>>>= \windowId ->
	accEnv (accPIO (getWindowViewFrame windowId))
  
//-- EXPORTED

vUpdateText :: !FontInfo !Text !ViewFrame ![Rectangle] -> (*Picture -> *Picture)
vUpdateText fontInfo text frame rectangles
	= seqmap (vUpdateRectangle text fontInfo) rectangles

Diederik van Arkel's avatar
Diederik van Arkel committed
70
vInsertText :: Position TextFragment -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
71
72
73
74
75
76
77
78
79
vInsertText position textFragment =
	getText										>>>= \text ->
	let (fin,newText) = insertText position textFragment text
	in
	setText newText								>>>
	IF (isNothing fin)
	THEN
		vResetViewDomain
	ELSE
80
81
82
83
84
//		(vTextUpdate position (fromJust fin - position.row + 1))
		let
			(beg,end)	= fromJust fin
		in
		(vTextUpdate {col=0,row=beg} (end - beg + 1))
Diederik van Arkel's avatar
Diederik van Arkel committed
85

Diederik van Arkel's avatar
Diederik van Arkel committed
86
vAppendLines :: TextFragment -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
vAppendLines textFragment =
	getText										>>>= \text ->
	let
		newText = appendLines textFragment text
	in
	setText newText								>>>
	vResetViewDomain
where
	vResetViewDomain (editState,pState)
		#! (windowId,(editState,pState))	= getWindowId (editState,pState)
		#! (maybeOldViewDomain,pState)		= accPIO (getWindowViewDomain windowId) pState
		| isNothing maybeOldViewDomain
			= abort "vResetViewDomain (EdVisualText.icl): unknown window id"
		#! oldViewDomain					= fromJust maybeOldViewDomain
101
//		#! (oldViewFrame,pState)			= accPIO (getWindowViewFrame windowId) pState
Diederik van Arkel's avatar
Diederik van Arkel committed
102
103
104
105
106
107
108
		#! (viewDomain,(editState,pState))	= computeViewDomain (editState,pState)
		| viewDomain == oldViewDomain
			#! pState						= appPIO (updateWindow windowId Nothing) pState
			= (undef,(editState,pState))
		#! pState = appPIO (setWindowViewDomain windowId viewDomain) pState
		= (undef,(editState,pState))
	
Diederik van Arkel's avatar
Diederik van Arkel committed
109
vAppendText :: TextFragment -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
vAppendText textFragment =
	getText										>>>= \text ->
	let
		newText = appendText textFragment text
	in
	setText newText								>>>
	vResetViewDomain
where
	vResetViewDomain (editState,pState)
		#! (windowId,(editState,pState))	= getWindowId (editState,pState)
		#! (maybeOldViewDomain,pState)		= accPIO (getWindowViewDomain windowId) pState
		| isNothing maybeOldViewDomain
			= abort "vResetViewDomain (EdVisualText.icl): unknown window id"
		#! oldViewDomain					= fromJust maybeOldViewDomain
124
//		#! (oldViewFrame,pState)			= accPIO (getWindowViewFrame windowId) pState
Diederik van Arkel's avatar
Diederik van Arkel committed
125
126
127
128
129
130
131
132
		#! (viewDomain,(editState,pState))	= computeViewDomain (editState,pState)
		| viewDomain == oldViewDomain
			#! pState						= appPIO (updateWindow windowId Nothing) pState
			= (undef,(editState,pState))
		#! pState = appPIO (setWindowViewDomain windowId viewDomain) pState
		= (undef,(editState,pState))
	

Diederik van Arkel's avatar
Diederik van Arkel committed
133
vRemoveText :: !Selection -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
134
135
136
137
138
139
140
141
142
143
144
vRemoveText selection=:{ start=start=:{ col=col1,row=row1 }
					   , end={ col=col2,row=row2 }
					   }
  = getText										>>>= \text ->
	let (fin,newText) = removeText selection text
	in
	setText newText								>>>
	IF (isNothing fin)
	THEN
		vResetViewDomain
	ELSE
145
146
147
//		(vTextUpdate start (fromJust fin - row1 + 1))
		let (beg,end) = fromJust fin in
		(vTextUpdate {col=0,row=beg} (end - beg + 1))
Diederik van Arkel's avatar
Diederik van Arkel committed
148
149
150
151
152
153

//--

// updating a rectangle is done by first erasing it and then redrawing
// the lines contained in that rectangle

154
155
156
157
// FIXME: this code assumes that corner1 of the rectangle is the upper left
// and corner2 the lower right corner, which is not garantueed by the ObjectIO
// (although in practice it is)

Diederik van Arkel's avatar
Diederik van Arkel committed
158
159
160
161
162
163
164
165
166
167
168
169
//vUpdateRectangle :: Text FontInfo Rectangle *Picture -> *Picture//-> EditMonad *Picture nothing
vUpdateRectangle text fontInfo=:{lineHeight,thefont,syntaxColours} rectangle=:{ corner1 = {x=x1,y=y1}, corner2 = {x=x2,y=y2} } //pict =
	// compute which lines were affected and retrieve them
	= vUR
where
	vUR pict
		# lineNr1	= validateLineNr (y1 / lineHeight) text 
		# lineNr2	= validateLineNr (y2 / lineHeight) text
		# rectangle` = {corner1 = {x = max (-1) x1, y = y1}, corner2 = {x = max (-1) x2, y = y2}}
		# region`	= toRegion rectangle`
		# pict		= appClipPicture region` (seq
						[ setPenFont thefont
170
						, drawBackground syntaxColours fontInfo rectangle`
Diederik van Arkel's avatar
Diederik van Arkel committed
171
172
173
174
						, drawLines lineNr1 lineNr2 fontInfo text
						]) pict
		= pict

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	// drawBackground colours the background
	drawBackground colours {charWidth, marginWidth} r=:{ corner1 = {x=x1,y=y1}, corner2 = {x=x2,y=y2} }
		| marginWidth <= 0
			= seq	[ setPenBack colours.backgroundColour
					, unfill r
					]
		// otherwise
			= seq	[ setPenBack colours.backgroundColour
					, unfill {r & corner2.x = margin}
					, setPenBack colours.marginColour
					, unfill {r & corner1.x = margin}
					]
		where
			margin
				=	marginWidth * charWidth
			
Diederik van Arkel's avatar
Diederik van Arkel committed
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
	// drawLines draws the lines in the range indicated by
	// the first two arguments, e.g. drawLines 0 3 draws the
	// text lines 0, 1, 2, and 3.
	
	//drawLines :: LineNr LineNr FontInfo Text *Picture -> *Picture 
	drawLines f l i=:{showSyntax} t p
		| showSyntax = drawLinesC f l i t p
		= drawLinesP f l i t p
	
	// draw lines with syntax colouring
	drawLinesC firstLine lastLine fontInfo=:{ lineHeight, metrics} text picture
		= drawTextLines firstLine firstY text picture
	where
		firstY		= computeBaseLine firstLine fontInfo
	
//		drawTextLines :: Int Int Text *Picture -> *Picture
		drawTextLines f y text picture
		  | f > lastLine
		  	= picture
		  # (line,text) = getLineC f text
		  # picture = tabDrawStringC {x=0,y=y} line fontInfo picture
		  = drawTextLines (inc f) (y + lineHeight) text picture
	
	// draw lines plain
	drawLinesP firstLine lastLine fontInfo=:{ lineHeight, metrics} text picture
		= drawTextLines firstY lines picture
	where
		firstY		= computeBaseLine firstLine fontInfo
		(lines, _)	= getLines firstLine lastLine text
	
		drawTextLines :: Int (StrictList String) *Picture -> *Picture
		drawTextLines _ SNil picture
		  = picture
		drawTextLines y (SCons string strings) picture
		  # picture = tabDrawString { x=0,y=y } string fontInfo picture
		  # picture = drawTextLines (y + lineHeight) strings picture
		  = picture

Diederik van Arkel's avatar
Diederik van Arkel committed
229
vTextUpdate :: !Position Int -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
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
vTextUpdate position=:{row} numLines =
	getText 					>>>= \text ->
	getViewFrame				>>>= \frame ->
	getFontInfo					>>>= \fontInfo ->
	let point = positionToPoint position text fontInfo in
	
	// one line affected or more ?
	IF (numLines <> 0)
	THEN
	  ( 
		// compute the rectangle that covers the rest of the line
		let updateRect
				  = { corner1={ x=frame.corner1.x, y=point.y }
					, corner2={ x=frame.corner2.x, y= min (frame.corner2.y) (point.y+fontInfo.lineHeight * numLines)}
					}
		in
		vDraw (vUpdateRectangle text fontInfo updateRect) 
	  )
	ELSE
	  (
		let updateRect
			  = { corner1={ x=frame.corner1.x, y=point.y }
				, corner2={ x=frame.corner2.x, y=frame.corner2.y }
				}
		in
		vDraw (vUpdateRectangle text fontInfo updateRect)
	  )

// vResetViewDomain changes the view domain, so that the current visual
// text fits in the window.

261
vResetViewDomain :: EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
vResetViewDomain  = monad
where
	monad (editState,pState)
		#! (windowId,(editState,pState))	= getWindowId (editState,pState)
		#! (maybeOldViewDomain,pState)		= accPIO (getWindowViewDomain windowId) pState
		| isNothing maybeOldViewDomain
			= abort "vResetViewDomain (EdVisualText.icl): unknown window id"
		#! oldViewDomain					= fromJust maybeOldViewDomain
		#! (viewDomain,(editState,pState))	= computeViewDomain (editState,pState)
		| viewDomain == oldViewDomain
			#! pState						= appPIO (updateWindow windowId Nothing) pState
			= (undef,(editState,pState))
		#! (x,(editState,pState))			= my_set_view_domain windowId viewDomain (editState,pState)
		= (x,(editState,pState))

	my_set_view_domain windowId viewDomain (editState,pState)
		#! pState = appPIO (setWindowViewDomain windowId viewDomain) pState
		#! pState = appPIO (updateWindow windowId Nothing) pState	// quick fix for changed setViewDomain interpretation...
		= (undef,(editState,pState))
	
Diederik van Arkel's avatar
Diederik van Arkel committed
282
vDraw :: (*Picture -> *Picture) -> EditMonad (PSt .l) a
Diederik van Arkel's avatar
Diederik van Arkel committed
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
vDraw drawFun =
	getWindowId		>>>= \windowId ->
	appEnv (appPIO (appWindowPicture windowId drawFun))

//--

positionToPoint :: !Position !Text !FontInfo -> Point2
positionToPoint {col, row} text fontInfo
	# (textLine, _)	= getLine row text
	# left			= textLine % (0, col-1) 
	# y				= fontInfo.lineHeight * row
	# x				= tabStringWidth 0 (splitAtTabs left) fontInfo
	= {x=x, y=y}

// pointToPosition 
// First, it determines the character in the middle of the line and
// then x-coordinate of the middle of that character. It then
// goes into recursion on the correct "half" until the segment
// has become empty.
	
pointToPosition :: !Point2 !Text !FontInfo -> Position
pointToPosition { x, y } text fontInfo
	# row = y / fontInfo.lineHeight 
	# lastLineNr = textLength text - 1
	| (row > lastLineNr)
	  	# (lastLine, _) = getLine lastLineNr text
	  	= {col=size lastLine, row=lastLineNr}
	| (row < 0)
	  = {col=0, row=0}
	# col = findColumn x row text fontInfo
	= {col=col, row=row}
where
	findColumn :: Int LineNr Text FontInfo -> Int
	findColumn x row text fontInfo
		| x < 0
			= 0
		# (textLine, _)	= getLine row text
		# len			= size textLine
		# splitLine		= splitAtTabs textLine
		# width			= tabStringWidth 0 splitLine fontInfo
		| x > width
			= len
			= binarySearch x 0 len splitLine
	where
		binarySearch :: Int Int Int [String] -> Int
		binarySearch x left right splitLine
			| left==right
				= left
			# middle = left + (right - left) / 2
			# width1 = tabStringWidth 0 (tabTake middle splitLine) fontInfo
			# width2 = tabStringWidth 0 (tabTake (middle+1) splitLine) fontInfo
			# middleX = (width1 + width2) / 2
			| x < middleX
				= binarySearch x left       middle splitLine
				= binarySearch x (middle+1) right  splitLine