clipboard.icl 4.3 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
implementation module clipboard

import StdFunc,StdMisc
import StdClipboard,StdMenuElement,StdTimer,StdWindow
import EdWindow, EdText, EdClient, EdVisualCursor
import IDE
import ioutil

/*
o Don't allow Clipboard window title to set 'need save' on clipboard changes...
o Remember clipboard pos and size...
*/

//--

class Clipper env where
	getClipInfo :: !*env -> *(!ClipInfo,!*env)
	setClipInfo :: !ClipInfo !*env -> *env

Diederik van Arkel's avatar
Diederik van Arkel committed
20
instance Clipper (PSt *l) | Clipper l
Diederik van Arkel's avatar
Diederik van Arkel committed
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
where
	getClipInfo ps		= accPLoc getClipInfo ps
	setClipInfo ci ps	= appPLoc (setClipInfo ci) ps

//--

:: ClipInfo =
	{ clip_clipId	:: !Id			// Id of clipboard window
	, clip_itemId	:: !Id			// Id of show/hide clipboard entry
	, clip_undoId	:: !Id			// Id of undo/redo menu entry
	, clip_timeId	:: !Id			// Id of timer
	, clip_state	:: !ClipState`	// Shown or Hidden
	, menuIds		:: ![Id]
	}

:: ClipState` = Shown | Hidden

instance == ClipState`
where
	(==) Shown Shown = True
	(==) Hidden Hidden = True
	(==) _ _ = False

//--

isClipboardWindow :: !Id !*env -> (Bool,*env) | Clipper env
isClipboardWindow wId ps
	# ({clip_clipId},ps) = getClipInfo ps
	= (clip_clipId == wId,ps)

Diederik van Arkel's avatar
Diederik van Arkel committed
51
cw_maybe_close :: !Id !*(PSt *l) -> (Bool,*PSt *l) | Clipper, Editor l
Diederik van Arkel's avatar
Diederik van Arkel committed
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
cw_maybe_close win ps
	# (ci,ps) = getClipInfo ps
	| ci.clip_clipId <> win
		= (False,ps)
	// change menu...
	# ps				= appPIO (setMenuElementTitles [(ci.clip_itemId,"Show Clipboard")]) ps
	// close timer...
	# ps				= appPIO (closeTimer ci.clip_timeId) ps
	// close window...
	# ps				= closeEditWindow ci.clip_clipId ps
	# ci				= {ci & clip_state = Hidden}
	# ps				= setClipInfo ci ps
	# (act,ps) = accPIO getActiveWindow ps
	| isNothing act = (True,ps)
	# act = fromJust act
	| win <> act = (True,ps)
	# ps				= deactivate ps
	= (True,ps)
	
initClipInfo :: Id [Id] *env -> *(ClipInfo,*env) | Ids env
initClipInfo undoId menuIds env
	# (clipId,env)		= openId env
	# (itemId,env)		= openId env
	# (timeId,env)		= openId env
	= ({clip_clipId=clipId,clip_itemId=itemId,clip_undoId=undoId,clip_timeId=timeId,clip_state=Hidden,menuIds=menuIds},env)


deactivate ps
	# (ci,ps)			= getClipInfo ps
	# ps				= appPIO ( enableMenuElements ci.menuIds ) ps
	= ps

Diederik van Arkel's avatar
Diederik van Arkel committed
84
clipMenuItems :: !Id !Id !ClipInfo -> .MenuItem .c *(PSt *l) | Clipper , Editor l
Diederik van Arkel's avatar
Diederik van Arkel committed
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
clipMenuItems mn_sav mn_rev ci
	= MenuItem "Show Clipboard" [MenuFunction (noLS showClip), MenuId ci.clip_itemId]
where
	showClip ps
		# (ci,ps)			= getClipInfo ps
		| ci.clip_state == Hidden
			# ((_, fnt), ps)	= accScreenPicture (openFont NonProportionalFontDef) ps // depends on ide and project prefs?
			# (clip,ps)			= getClipboard ps
			# newText			= StringToText (getString clip)
			// start timer...
			# (err,ps)			= openTimer undef (tdef ci.clip_timeId ci.clip_clipId) ps
			| err <> NoError = ps
			// change menu...
			# ps				= appPIO (setMenuElementTitles [(ci.clip_itemId,"Hide Clipboard")]) ps
			# tbs				= (4,False,False,False,False)
			# ps				= openEditWindow ci.clip_undoId "Clipboard" "" newText fnt tbs DefaultSyntaxColours ci.clip_clipId
									[WindowActivate		(noLS activate)
									,WindowDeactivate	(noLS deactivate)
									,WindowClose		(noLS showClip)
									] ps
			# ci				= {ci & clip_state = Shown}
			# ps				= setClipInfo ci ps
			# (_,ps)			= message ci.clip_clipId (vHideCursor) ps
			= ps
		| otherwise
			// change menu...
			# ps				= appPIO (setMenuElementTitles [(ci.clip_itemId,"Show Clipboard")]) ps
			// close timer...
			# ps				= appPIO (closeTimer ci.clip_timeId) ps
			// close window...
			# ps				= closeEditWindow ci.clip_clipId ps
			# ci				= {ci & clip_state = Hidden}
			# ps				= setClipInfo ci ps
			# ps				= deactivate ps
			= ps
	where
		activate ps
			# ps = appPIO
					( disableMenuElements [mn_sav,mn_rev:ci.menuIds]
					o setMenuElementTitles [(mn_sav,"Save"),(mn_rev,"Revert")]
					) ps
			= ps
		tdef tId cId = Timer 1000 (NilLS) [TimerId tId, TimerFunction (noLS1 (const (show cId)))]
		show cId ps
			# (new,ps)	= clipboardHasChanged ps
			| new
				# (clip,ps)	= getClipboard ps
				# newtext	= StringToText (getString clip)
				# (_,ps)	= message cId (msgSetText newtext) ps
				# (_,ps)	= message cId (msgSetNeedSave False) ps
				# (_,ps)	= message cId (vHideCursor) ps
				= ps
			| otherwise
				= ps
	
		getString [clip:clips]
			# clip = fromClipboard clip
			| isNothing clip
				= getString clips
			| otherwise
				= fromJust clip
		getString []
			= ""