typewin.icl 15.5 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
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
implementation module typewin

import StdClass, StdInt, StdList, StdFunc,StdMisc
import StdPSt, StdWindow, StdControlReceiver, StdMenuElement
import EdLook, EdMessage, EdVisualText, EdWindow
import EdMouse, EdKeyboard

from StrictList import slFromList
from UtilStrictLists import StrictListToList,List,IsEmptyList,:!

import IDE,IdeState

//--

isWindow :: !Id *(PSt .l) -> (Bool,*(PSt .l))
isWindow wId ps
	# (s,ps)	= accPIO getWindowsStack ps
	= (isMember wId s, ps)

isTypeWindow :: !Id !.TypeWinInfo -> Bool
isTypeWindow xId {TypeWinInfo | wId}
	= xId==wId

//--

:: TypeWinInfo =
	{ wId	:: !Id		// id of the types window
	, eId	:: !EditId	// receiver id
	, uId	:: !Id		// id of undo menu item
	, ids	:: ![Id]	// ids to disable when typewin is active
	, tfnt	:: !Font	// window font
	, tpos	:: !Vector2	// window pos
	, tsiz	:: !Size	// window size
	, sync	:: !SyntaxColours
	}

TW_GetInf :: !TypeWinInfo -> TypPrefs
TW_GetInf {tfnt,tpos,tsiz,sync} =
	{ typewinfont	= getFontDef tfnt
	, typewinpos	= tpos
	, typewinsiz	= tsiz
	, typewinsync	= sync
	}

TW_SetFnt :: !Font !TypeWinInfo -> TypeWinInfo
TW_SetFnt fnt twi = {twi & tfnt = fnt}

TW_GetSync :: !TypeWinInfo -> SyntaxColours
TW_GetSync {sync} = sync

TW_SetSync :: !SyntaxColours !TypeWinInfo -> TypeWinInfo
TW_SetSync sync twi = {twi & sync = sync}

TW_GetWind :: !TypeWinInfo -> Id
TW_GetWind {TypeWinInfo | wId} = wId

TW_SetUpd :: ![Id] !TypeWinInfo -> TypeWinInfo
TW_SetUpd ids twi = {twi & ids = ids}

class Typer env
where
	getTypeWinInfo :: !*env -> *(!TypeWinInfo, !*env)
	setTypeWinInfo :: !TypeWinInfo !*env -> *env

getTypeWinInf :: *(PSt *b) -> *(TypeWinInfo,*PSt *b) | Typer b
getTypeWinInf ps = accPLoc getTypeWinInfo ps

setTypeWinInf :: .TypeWinInfo *(PSt *b) -> *PSt *b | Typer b
setTypeWinInf twi ps = appPLoc (setTypeWinInfo twi) ps


//--- Types window

iniTypeWinInfo :: !Id !Id ![Id] !TypPrefs !*a -> *(.TypeWinInfo,*a) | Ids, accScreenPicture a
iniTypeWinInfo mId uId ids tprefs env
	# ((ok,twFont),env)	= accScreenPicture (openFont tprefs.typewinfont) env
	| not ok = abort "Unable to open font for Types window"
	# (wId,env) = openId env
	# (eId,env) = openEditId env
	= (	{wId	= wId
		,eId	= eId
		,uId	= uId
		,ids	= ids
		,tfnt	= twFont
		,tpos	= tprefs.typewinpos
		,tsiz	= tprefs.typewinsiz
		,sync	= tprefs.typewinsync
		},env)

//--

92 93 94
updateTypeWindow :: !Bool !String [WindowAttribute *(EditState,*(PSt *b))] ![String] !*(PSt *b) -> *PSt *b | Typer b
updateTypeWindow interact name atts message ps	// fun to update type info in type window, text :: !Text
	| not interact || isEmpty message
Diederik van Arkel's avatar
Diederik van Arkel committed
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
		= ps
	# message								= [quoteString name : message]
	# (twi=:{TypeWinInfo | wId,eId},ps)		= getTypeWinInf ps
	# (iswin,ps)							= isWindow wId ps
	# textfrag								= slFromList message
	| iswin
		// alleen update
		#! (_,ps)							= type_message eId (vAppendLines textfrag) ps
		= ps
	// open & update
	# text									= emptyText
	# ps									= openTypeWindow twi text atts ps
	#! (_,ps)								= type_message eId (vAppendLines textfrag) ps
	= ps
where
	quoteString s = "\""+++s+++"\""

maybe_type_win_message :: !Id .(EditMonad *(PSt *b) .c) !*(PSt *b) -> (Maybe .c,*(PSt *b)) | Typer b
maybe_type_win_message id message ps
	# (twi=:{TypeWinInfo | wId,eId},ps)	=  getTypeWinInf ps
	| id == wId = type_message eId message ps
	= (Nothing,ps)

maybe_type_win_message2 :: .(EditMonad *(PSt *b) .c) !*(PSt *b) -> (Maybe .c,*(PSt *b)) | Typer b
maybe_type_win_message2 message ps
	# (twi=:{eId},ps)	=  getTypeWinInf ps
	# (has,ps) = hasEditState eId ps
	| has
		= type_message eId message ps
	= (Nothing,ps)

type_win_message :: !.(EditMonad *(PSt *b) .c) !*(PSt *b) -> *(Maybe .c,*(PSt *b)) | Typer b
type_win_message message ps
	# (twi=:{eId},ps)	=  getTypeWinInf ps
	= type_message eId message ps

type_message :: !EditId !.(EditMonad *(PSt *b) .c) !*(PSt *b) -> *(Maybe .c,*(PSt *b))
type_message editId monad pState
133
	# (x,pState) = appEditState editId monad pState
Diederik van Arkel's avatar
Diederik van Arkel committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147
	= (Just x,pState)

//---

openTypeWindow twi text atts ps
	# editId						= twi.eId
	# windowId						= twi.TypeWinInfo.wId
	# pathName						= "" 					// dummy pathName
	# title							= "Types"
	# tabs							= (4,True,False,False,True)
	# (editState, ps)				= initEditState windowId twi.uId pathName twi.tfnt tabs twi.sync ps
	# (_, (editState, ps))			= setText text (editState, ps)
	# (fontInfo, (editState, ps))	= getFontInfo (editState, ps) 
	# (viewDomain, (editState, ps))	= computeViewDomain (editState, ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
148
	# editLook						= editWindowLook editState
Diederik van Arkel's avatar
Diederik van Arkel committed
149 150 151 152 153 154
	# windowAttrs 
	  = [ WindowOuterSize  	twi.tsiz
		, WindowHMargin	  	0 0
		, WindowVMargin	  	0 0
		, WindowId		  	windowId
		, WindowViewDomain	viewDomain
155
		, WindowLook		True editLook
Diederik van Arkel's avatar
Diederik van Arkel committed
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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		, WindowHScroll		(hScrollFun	   fontInfo)
		, WindowVScroll		(vScrollFun	   fontInfo)
		, WindowPos			(Fix, OffsetVector twi.tpos)
		, WindowClose		(noLS (tw_close o tw_deactivate))	// be more careful if editable...
		, WindowActivate	(noLS (tw_activate))
		, WindowDeactivate 	(noLS (tw_deactivate))
		: atts
		]
	# receiver	  					= openEditReceiver editId
	# window						= Window title receiver windowAttrs
	# (_, ps)	 					= openWindow editState window ps
	= ps

//--

tw_maybe_close :: !Id !*(PSt *b) -> *(Bool,*(PSt *b)) | Typer b
tw_maybe_close win ps
	# (twi,ps)	= getTypeWinInf ps
	| isTypeWindow win twi
		# ps	= tw_deactivate ps
		# ps	= tw_close ps
		= (True,ps)
	= (False,ps)

tw_safe_close :: !*(PSt *b) -> *PSt *b | Typer b
tw_safe_close ps
	# (win,ps)	= accPIO getActiveWindow ps
	| isNothing win
		= tw_close ps
	# win = fromJust win
	# (twi,ps)	= getTypeWinInf ps
	| isTypeWindow win twi
		# ps	= tw_deactivate ps
		= tw_close ps
	= tw_close ps

tw_close :: !*(PSt *b) -> *PSt *b | Typer b
tw_close ps
	# (twi=:{TypeWinInfo | wId},ps)	= getTypeWinInf ps
	# (mpos,ps) = accPIO (getWindowPos wId) ps
	| isNothing mpos
		= ps
	# tpos		= fromJust mpos
	# (tsiz,ps) = accPIO (getWindowOuterSize wId) ps
	# twi		= {twi & tpos = tpos, tsiz = tsiz}
	# ps		= setTypeWinInf twi ps
	# ps		= closeWindow wId ps
	= ps

tw_activate ps
	# (twi,ps=:{io})	= getTypeWinInf ps
	# io = disableMenuElements twi.ids io
	= {ps & io = io}
tw_deactivate ps
	# (twi,ps=:{io})	= getTypeWinInf ps
	# io = enableMenuElements twi.ids io
	= {ps & io = io}

/**********************
 * SCROLLBAR HANDLING *
 **********************/
 
hScrollFun fontInfo
	:== altScrollFunction Horizontal fontInfo.metrics.fMaxWidth
vScrollFun fontInfo
	:== alignScrollFunction Vertical fontInfo.FontInfo.lineHeight

//-- type win options...
import morecontrols, colorpickcontrol, ioutil, colourclip
import StdClipboard

lisFixedWidth :: .FontName *Picture -> (Bool,*Picture)
lisFixedWidth fontname env
  # ((ok,font),   env) = openFont {fName=fontname,fSize=12,fStyles=[]} env
  | not ok = (ok,env)
  # (wide,	 env) = getFontCharWidth font 'M' env
	(narrow, env) = getFontCharWidth font 'i' env
  = (wide == narrow, env)

lfilter :: [.Bool] [.a] -> [.a];
lfilter [True:r] [a:x]	= [a:lfilter r x]
lfilter [_:r] [_:x] = lfilter r x
lfilter _ _				= []

::TWC_LS =
	{ cur	:: TWC_CR	// current colour
	, txt	:: Colour
	, cmt	:: Colour
	, mod	:: Colour
	, bck	:: Colour
	, fn	:: String	// font name
	, fs	:: Int		// font size
	}

::TWC_CR
	= TXT
	| CMT
	| MOD
	| BCK

instance == TWC_CR
where
	(==) TXT TXT = True
	(==) CMT CMT = True
	(==) MOD MOD = True
	(==) BCK BCK = True
	(==) _ _ = False

typewinColours :: !*(PSt *a) -> *(PSt *a) | Typer a
typewinColours ps
	# (wloc,pane,watt,ps)= typewinPane ps
	# wdef			= Dialog "Types Window..." pane watt
	# (_,ps)		= openModalDialog wloc wdef ps
	= ps
				    
typewinPane ps
	# (twi,ps)		= accPLoc getTypeWinInfo ps
	# (fontNames, ps)
					= accPIO (accScreenPicture getFontNames) ps
//	# fixed			= map lisFixedWidth fontNames	// allow all fonts for types window
//	# fontNames		= lfilter fixed fontNames
	# fontSizes		= [7, 8, 9, 10, 12, 14, 18, 24 ]
	# {typewinfont={fName=inifn,fSize=inifs},typewinsync=sync}
					= TW_GetInf twi
	# win			= TW_GetWind twi
	# (rgbid,ps)	= openRGBId  ps
 	# (wId,ps)		= openId ps
 	# (okId,ps)		= openId ps
 	# (cancelId,ps)	= openId ps
 	# (cb1id,ps)	= openId ps
 	# (cb2id,ps)	= openId ps
 	# (cb3id,ps)	= openId ps
 	# (cb4id,ps)	= openId ps
 	# (lsid,ps)		= openRId ps
	# wloc			= {fn=inifn,fs=inifs,cur = TXT,txt=sync.textColour,cmt=sync.commentColour,mod=sync.stringColour,bck=sync.backgroundColour}
	# wdef			= 	(LayoutControl 
						(	FontNameSizeControl inifn inifs fontNames fontSizes fontfun sizefun [ left ]
						:+:	RGBColourPickControl` rgbid sync.textColour cb1id (Just (Left, zero))
						) []

						:+: LayoutControl
						(	TextControl "Text:" [ left , ControlWidth (ContentWidth "Background: ")]
						:+: ColourBoxControl` (toRGBColour sync.textColour) cb1id (mfilter,mfun rgbid lsid cb1id cb2id cb3id cb4id TXT) Nothing//(Just (Left, zero))
						:+: TextControl "Modules:" [ left , ControlWidth (ContentWidth "Background: ")]
						:+: ColourBoxControl` (toRGBColour sync.stringColour) cb3id (mfilter,mfun rgbid lsid cb1id cb2id cb3id cb4id MOD) Nothing//(Just (Left, zero))
						:+: TextControl "Comments:" [ left, ControlWidth (ContentWidth "Background: ")]
						:+: ColourBoxControl` (toRGBColour sync.commentColour) cb2id (mfilter,mfun rgbid lsid cb1id cb2id cb3id cb4id CMT) Nothing//(Just (Left, zero))
						:+: TextControl "Background:" [ left, ControlWidth (ContentWidth "Background: ")]
						:+: ColourBoxControl` (toRGBColour sync.backgroundColour) cb4id (mfilter,mfun rgbid lsid cb1id cb2id cb3id cb4id BCK) Nothing//(Just (Left, zero))
						) []
						
					   	:+:	ButtonControl "Ok"
				  			[ ControlId okId
				  			, ControlFunction (okFun win rgbid lsid wId) 
				  			, ControlPos (Right, zero)
				  			, ControlWidth (ContentWidth "Cancel")
				  			]
				    	:+:	ButtonControl "Cancel"
				  			[ ControlPos (LeftOfPrev, zero) 
				  			, ControlFunction (cancelFun win wloc wId)
				  			, ControlId cancelId
				  			] 
						:+: ButtonControl "Apply"
							[ ControlPos (LeftOfPrev,zero)
							, ControlFunction (applyFun win rgbid lsid)
							]
				  		:+: ButtonControl "Paste"
				  			[ ControlPos (LeftOfPrev,zero)
				  			, ControlFunction (pasteFun rgbid cb1id cb2id cb3id cb4id)
				  			]
				  		:+: ButtonControl "Copy"
				  			[ ControlPos (LeftOfPrev,zero)
				  			, ControlFunction (copyFun rgbid)
				  			]

				  		:+: Receiver lsid lsfun []
						)
	# watt		=		[ WindowPen		[PenBack LightGrey]
						, WindowClose 	(cancelFun win wloc wId)
						, WindowId		wId
						, WindowOk		okId
						, WindowCancel	cancelId
						, WindowInit	(setBoxCol cb1id cb2id cb3id cb4id)
						]
	= (wloc,wdef,watt,ps)
where
	fontfun name (ls,ps)
		# ls		= {TWC_LS | ls & fn = name}
		= (ls,ps)
	sizefun size (ls,ps)
		# ls		= {TWC_LS | ls & fs = size}
		= (ls,ps)
	lsfun f (ls,ps)
		= f (ls,ps)
    left = ControlPos (Left, zero)
	mfilter (MouseDown _ _ _) = True
	mfilter _ = False
	mfun rgbid lsid cb1id cb2id cb3id cb4id act _ (ls=:{cur,txt,cmt,mod,bck},ps)
		| act == cur = (ls,ps)
		# ps = getColourBoxColour rgbid cont ps
		= (ls,ps)
	where
		cont col ps
			| isNothing col = ps
			# col = fromJust col
			# ps	= appPIO (case act of
								TXT	-> SetColourBox` cb1id (case cur of
									TXT	-> toRGBColour col
									_	-> toRGBColour txt
									)
								_	-> SetColourBox cb1id (case cur of
									TXT	-> toRGBColour col
									_	-> toRGBColour txt
									)
							) ps
			# ps	= appPIO (case act of
								CMT	-> SetColourBox` cb2id (case cur of
									CMT	-> toRGBColour col
									_	-> toRGBColour cmt
									)
								_	-> SetColourBox cb2id (case cur of
									CMT	-> toRGBColour col
									_	-> toRGBColour cmt
									)
							) ps
			# ps	= appPIO (case act of
								MOD	-> SetColourBox` cb3id (case cur of
									MOD	-> toRGBColour col
									_	-> toRGBColour mod
									)
								_	-> SetColourBox cb3id (case cur of
									MOD	-> toRGBColour col
									_	-> toRGBColour mod
									)
							) ps
			# ps	= appPIO (case act of
								BCK	-> SetColourBox` cb4id (case cur of
									BCK	-> toRGBColour col
									_	-> toRGBColour bck
									)
								_	-> SetColourBox cb4id (case cur of
									BCK	-> toRGBColour col
									_	-> toRGBColour bck
									)
							) ps
			# cId = case act of
						TXT -> cb1id
						CMT -> cb2id
						MOD -> cb3id
						BCK -> cb4id
			# col` = case act of
						TXT -> txt
						CMT -> cmt
						MOD -> mod
						BCK -> bck
			# ps	= setColourBoxId rgbid cId ps
			# ps	= setColourBoxColour` rgbid col` ps
			# (_,ps) = asyncSend lsid (cont2 col) ps
			= ps
		cont2 col (ls=:{cur},ps)
			# ls	= case cur of
						TXT -> {ls & txt = col, cur = act}
						CMT -> {ls & cmt = col, cur = act}
						MOD -> {ls & mod = col, cur = act}
						BCK -> {ls & bck = col, cur = act}
			= (ls,ps)
	toStringC :: !Colour -> String
	toStringC c = toString c
	copyFun rgbid (ls,ps)
		# ps = getColourBoxColour rgbid cont ps
		= (ls,ps)
	where
		cont col ps
			| isNothing col
				= ps
			# col = fromJust col
			# ps	= setClipboard [toClipboard (toString col)] ps
			= ps
	pasteFun rgbId cb1id cb2id cb3id cb4id (ls=:{cur},ps)
		// get clipboard
		// and put in active colour control
		# (its,ps)	= getClipboard ps
		| isEmpty its
			= (ls,ps)
		# its		= map fromClipboard its
		# its		= filter isJust its
		| isEmpty its
			= (ls,ps)
		# it		= fromJust (hd its)
		| it <> toStringC (fromString it)
			= (ls,ps)
		# col		= fromString it
		# ls		= case cur of
						TXT -> {TWC_LS | ls & txt = col}
						CMT -> {TWC_LS | ls & cmt = col}
						MOD -> {TWC_LS | ls & mod = col}
						BCK -> {TWC_LS | ls & bck = col}
		# (ls,ps)	= setBoxCol cb1id cb2id cb3id cb4id (ls,ps)
		# ps		= setColourBoxColour` rgbId col ps
		= (ls,ps)
	applyFun win rgbid lsid (ls=:{cur},ps)
		# ps = getColourBoxColour rgbid cont ps
		= (ls,ps)
	where
		cont col ps
			| isNothing col
				= ps
			# col = fromJust col
			# (_,ps) = asyncSend lsid (cont2 col) ps
			= ps
		cont2 col (ls,ps)
			# ls = case cur of
						TXT	-> {TWC_LS | ls & txt = col}
						CMT	-> {TWC_LS | ls & cmt = col}
						MOD	-> {TWC_LS | ls & mod = col}
						BCK	-> {TWC_LS | ls & bck = col}
			= updateTW win (ls,ps)
	okFun win rgbid lsid dialogId (ls=:{cur},ps)
		# ps = getColourBoxColour rgbid cont ps
		= (ls,ps)
	where
		cont col ps
			| isNothing col
				= ps
			# col = fromJust col
			# (_,ps) = asyncSend lsid (cont2 col) ps
			= ps
		cont2 col (ls,ps)
			# ls = case cur of
						TXT	-> {TWC_LS | ls & txt = col}
						CMT	-> {TWC_LS | ls & cmt = col}
						MOD	-> {TWC_LS | ls & mod = col}
						BCK	-> {TWC_LS | ls & bck = col}
			= updateTW win (ls,closeWindow dialogId ps)
	cancelFun win wini dialogId (ls,ps)
		= updateTW win (wini,closeWindow dialogId ps)
492
	setBoxCol cb1id cb2id cb3id cb4id (ls=:{cur,txt,cmt,mod,bck},ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
493
		# (cId,col) = case cur of
494 495 496 497
						TXT	-> (cb1id,txt)
						CMT	-> (cb2id,cmt)
						MOD	-> (cb3id,mod)
						BCK	-> (cb4id,bck)
Diederik van Arkel's avatar
Diederik van Arkel committed
498 499
		# ps = appPIO (SetColourBox` cId (toRGBColour col)) ps
		= (ls,ps)
500
	updateTW win (ls=:{fn,fs,txt,cmt,mod,bck},ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
501 502
		# (twi,ps)		= accPLoc getTypeWinInfo ps
		# sync			= TW_GetSync twi
503 504
		# sync			= sc_update sync
		# (font,ps)		= safeOpen {fName = fn, fSize = fs, fStyles = []} ps
Diederik van Arkel's avatar
Diederik van Arkel committed
505 506 507
		# twi			= TW_SetSync sync twi
		# twi			= TW_SetFnt font twi
		# ps			= appPLoc (setTypeWinInfo twi) ps
508
		# (_,ps)		= maybe_type_win_message2 (appFontInfo fi_update) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
509 510 511 512
		# (_,ps)		= maybe_type_win_message2 (setFont font) ps
		# ps			= appPIO (updateWindow win Nothing) ps
		= (ls,ps)
	where	
513
		fi_update fi =
Diederik van Arkel's avatar
Diederik van Arkel committed
514
			{ fi
515
			& syntaxColours = sc_update fi.syntaxColours
Diederik van Arkel's avatar
Diederik van Arkel committed
516 517
			}
		
518
		sc_update sc =
Diederik van Arkel's avatar
Diederik van Arkel committed
519 520 521 522 523 524 525 526 527 528 529 530 531
			{ sc
			& textColour		= txt
			, commentColour		= cmt
			, stringColour		= mod
			, backgroundColour	= bck
			}

	safeOpen fdef ps
		# ((ok,font),ps)	= accScreenPicture (openFont fdef) ps
		| not ok
			= accScreenPicture openDefaultFont ps
		= (font,ps)