Help.icl 11 KB
Newer Older
Peter Achten's avatar
Peter Achten committed
1
2
3
4
5
6
7
8
9
10
implementation module Help

//	**************************************************************************************************
//
//	General utility for handling information about the application and present help.
//
//	This module has been written in Clean 1.3.2 and uses the Clean Standard Object I/O library 1.2
//	
//	**************************************************************************************************

Peter Achten's avatar
Peter Achten committed
11
12
import	StdArray, StdBool, StdFile, StdFunc, StdInt, StdList, StdTuple, StdMisc
import	StdId, StdProcess, StdPSt, StdPStClass, StdSystem, StdWindow
Peter Achten's avatar
Peter Achten committed
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

::	NoState		=	NoState
::	InfoDef		:== (Size,[InfoLine])
::	InfoLine	:== (InfoFontDef,Int,Int,String)
::	InfoFontDef	=	InfoFont Font	Centered
				|	NoFont			Centered
::	Centered	:== Bool
::	Fonts
	=	{	normal		:: Font
		,	large		:: Font
		,	bold		:: Font
		,	large_bold	:: Font
		}
::	Heights		:== (Int,Int)

InfoFontName1	:== SansSerifFontDef.fName
InfoFontName2	:== SerifFontDef.fName
NormalSize1		:== 9
NormalSize2		:== 12
LargeSize1		:== 12
LargeSize2		:== 14
Margin			:== 8
AboutBegin		:== "\\About"
AboutEnd		:== "\\EndAbout"
HelpBegin		:== "\\Help"
HelpEnd			:== "\\EndHelp"
About			:== False
Help			:== True


/*	showAbout opens a window:
	-	it has the title of the application name (String argument 1),
	-	it displays the about information of the application (found in the helpfile, name argument 2),
	-	it has an Ok button that closes this window, 
	-	it has a Help button that displays the help information (see showHelp).
*/
49
showAbout :: String String (PSt .l) -> PSt .l
Peter Achten's avatar
Peter Achten committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
showAbout appname helpfile pState
	# (okId, pState)		= accPIO openId pState
	# (fonts,pState)		= accPIO (accScreenPicture infoFonts) pState
	  {normal}				= fonts
	# ((size,text),pState)	= readInfo About fonts AboutBegin AboutEnd helpfile pState
	  about					= Dialog ("About "+++appname)
	  							(	CustomControl size (look normal text)
  															[ ControlPos		(Center,NoOffset)]
	  							:+:	ButtonControl "Ok"		[ ControlId			okId
  															, ControlFunction	(noLS closeActiveWindow)
  															, ControlPos		(Center,NoOffset)
  															]
	  							:+:	ButtonControl "Help"	[ ControlFunction	(noLS (showHelp helpfile o closeActiveWindow))]
	  							)
	  							[	WindowOk okId	]
	= snd (openDialog undef about pState)


/*	showHelp opens a SDI process that displays the help information found in the helpfile.
*/
70
showHelp :: String (PSt .l) -> PSt .l
Peter Achten's avatar
Peter Achten committed
71
72
73
74
75
76
77
78
79
80
81
82
showHelp helpfile pState
	# (fonts,pState)		= accPIO (accScreenPicture infoFonts) pState
	# ((size,text),pState)	= readInfo Help fonts HelpBegin HelpEnd helpfile pState
	  window				= Window "Help" 
	  							NilLS
	  							[	WindowViewSize		size
	  							,	WindowLook			True (look fonts.normal text)
	  							,	WindowHScroll		hscroll
	  							,	WindowVScroll		vscroll
	  							,	WindowClose			(noLS closeProcess)
	  							,	WindowViewDomain	{zero & corner2={x=size.w,y=size.h}}
	  							]
83
	= openProcesses (Process SDI NoState (snd o openWindow undef window) [ProcessClose closeProcess]) pState
Peter Achten's avatar
Peter Achten committed
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
where
	hscroll curViewFrame {sliderThumb} move
		= case move of
			SliderIncSmall -> sliderThumb+10
			SliderDecSmall -> sliderThumb-10
			SliderIncLarge -> sliderThumb+(rectangleSize curViewFrame).w*4/5
			SliderDecLarge -> sliderThumb-(rectangleSize curViewFrame).w*4/5
			SliderThumb x  -> x
				
	vscroll curViewFrame {sliderThumb} move
		= case move of
			SliderIncSmall -> sliderThumb+10
			SliderDecSmall -> sliderThumb-10
			SliderIncLarge -> sliderThumb+(rectangleSize curViewFrame).h*4/5
			SliderDecLarge -> sliderThumb-(rectangleSize curViewFrame).h*4/5
			SliderThumb x  -> x

look :: Font [InfoLine] SelectState UpdateState *Picture -> *Picture
look font lines _ {updArea} picture
	# picture	= setPenFont font picture
	# picture	= seq (map (\r -> drawInfo font (r.corner1.y-1) (r.corner2.y+40) lines o (unfill r)) updArea) picture
	= picture


//	Try to open a prefered set of fonts to display the help and about information:

infoFonts :: !*Picture -> (!Fonts,!*Picture)
infoFonts picture
	# (normal,    picture) = selectfont [(InfoFontName1,NormalSize1),(InfoFontName2,NormalSize2)] [] picture
	# (large,     picture) = selectfont [(InfoFontName1,LargeSize1 ),(InfoFontName2,LargeSize2 )] [] picture
	# (bold,      picture) = selectfont [(InfoFontName1,NormalSize1),(InfoFontName2,NormalSize2)] [BoldStyle] picture
	# (large_bold,picture) = selectfont [(InfoFontName1,LargeSize1 ),(InfoFontName2,LargeSize2 )] [BoldStyle] picture
	= ({normal=normal,large=large,bold=bold,large_bold=large_bold},picture)
where
	selectfont :: ![(String,Int)] ![FontStyle] !*Picture -> (!Font,!*Picture)
	selectfont [(fontname,size):preffonts] style picture
		# ((found,font),picture)	= openFont {fName=fontname,fStyles=style,fSize=size} picture
		| found						= (font,picture)
		| otherwise					= selectfont preffonts style picture
	selectfont _ style picture
		= openDefaultFont picture


//	Determine the line height and leading of a given font:

129
getFontHeightAndAscent :: Fonts (PSt .l) -> (((Int,Int),(Int,Int)),PSt .l)
Peter Achten's avatar
Peter Achten committed
130
131
132
133
134
135
136
137
138
139
140
141
142
getFontHeightAndAscent fonts pState
	# ((normal,large),pState)	= accPIO (accScreenPicture (getmetrics fonts)) pState
	= (((fontLineHeight normal,normal.fAscent), (fontLineHeight large,large.fAscent)),pState)
where
	getmetrics :: Fonts *Picture -> ((FontMetrics,FontMetrics),*Picture)
	getmetrics {normal,large} picture
		# (normal,picture)	= getFontMetrics normal picture
		# (large, picture)	= getFontMetrics large  picture
		= ((normal,large),picture)


//	Reading and pre-processing of the file containing the about- and help-info. */

143
readInfo :: Bool Fonts String String String (PSt .l) -> ((Size,[InfoLine]),PSt .l)
Peter Achten's avatar
Peter Achten committed
144
readInfo help fonts begin end filename pState
Peter Achten's avatar
Peter Achten committed
145
146
	# (metrics,    pState)	= getFontHeightAndAscent fonts pState
	# (succes,file,pState)	= fopen (applicationpath filename) FReadText pState
Peter Achten's avatar
Peter Achten committed
147
148
149
150
	| not succes && help
		= processInfoStrings fonts metrics [errpref+++"could not be found."] pState
	| not succes
		= processInfoStrings fonts metrics ["\\DThis is a Clean program."] pState
Peter Achten's avatar
Peter Achten committed
151
152
	# (found,info,file)		= readInfoFile begin end file
	# (_,pState)			= fclose file pState
Peter Achten's avatar
Peter Achten committed
153
154
155
156
157
158
159
160
161
	| not found && help
		= processInfoStrings fonts metrics [errpref+++"does not contain help information."] pState
	| not found
		= processInfoStrings fonts metrics ["\\DThis is a Clean program."] pState
	| otherwise
		= processInfoStrings fonts metrics info pState
where
	errpref						= "The help file \'"+++filename+++"\' " 
	
162
	processInfoStrings :: Fonts ((Int,Int),(Int,Int)) [String] (PSt .l) -> (InfoDef,PSt .l)
Peter Achten's avatar
Peter Achten committed
163
164
165
166
167
168
	processInfoStrings fonts ((normalHeight,normalAscent),(largeHeight,largeAscent)) lines pState
		# ((size,lines),pState)	= addFontToInfoLines fonts (normalHeight,largeHeight) 0 (Margin+largeAscent) lines pState
		  width					= Margin+size.w+Margin
		# (lines,pState)		= seqList (map (centerInfoLine fonts.normal width) lines) pState
		= (({w=width,h=size.h+Margin-largeAscent},lines),pState)
	where
169
		addFontToInfoLines :: Fonts Heights Int Int [String] (PSt .l) -> (InfoDef,PSt .l)
Peter Achten's avatar
Peter Achten committed
170
171
172
173
174
175
176
177
178
		addFontToInfoLines fonts heights maxx maxy [line:rest] pState
			# ((font,wid,hgt,line),pState)	= parseInfoLine fonts heights line pState
			# ((size,rest),        pState)	= addFontToInfoLines fonts heights (max maxx wid) (maxy+hgt) rest pState
			= ((size,[(font,Margin,maxy,line):rest]),pState)
		where
		/*	parseInfoLine determines the font that should be used to draw the line.
			If line == '\{L,b,B,c,C,d,D}'+++line1 then a special font is used, otherwise the default font is used.
			parseInfoLine also calculates the width and height of the line.
		*/
179
			parseInfoLine :: Fonts Heights String (PSt .l) -> ((InfoFontDef,Int,Int,String),PSt .l)
Peter Achten's avatar
Peter Achten committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
			parseInfoLine fonts=:{normal,large,bold,large_bold} heights=:(nhgt,lhgt) line pState
				# linelen	= size line
				| linelen<2 || line.[0]<>'\\'
					# (width,pState)	= accPIO (accScreenPicture (getFontStringWidth normal line)) pState
					= ((NoFont False,width,nhgt,line),pState)
				| otherwise
					# (infofont,font,height)= case (line.[1]) of
												'L' -> (InfoFont large      False, large,      lhgt)
												'b' -> (InfoFont bold       False, bold,       nhgt)
												'B' -> (InfoFont large_bold False, large_bold, lhgt)
												'c' -> (NoFont   True,             normal,     nhgt)
												'C' -> (InfoFont large      True , large,      lhgt)
												'd' -> (InfoFont bold       True , bold,       nhgt)
												'D' -> (InfoFont large_bold True , large_bold, lhgt)
												_   -> (NoFont   False,            normal,     nhgt)
					  line					= line%(2,linelen-1)
					# (width,pState)		= accPIO (accScreenPicture (getFontStringWidth font line)) pState
					= ((infofont,width,height,line),pState)
		addFontToInfoLines _ _ maxx maxy _ pState
			= (({w=maxx,h=maxy},[]),pState)
		
201
		centerInfoLine :: Font Int InfoLine (PSt .l) -> (InfoLine,PSt .l)
Peter Achten's avatar
Peter Achten committed
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
		centerInfoLine nft maxx info=:(inft=:NoFont centered,x,y,line) pState
			| not centered
				= (info,pState)
			| otherwise
				# (width,pState)	= accPIO (accScreenPicture (getFontStringWidth nft line)) pState
				= ((inft,(maxx-width)/2,y,line),pState)
		centerInfoLine nft maxx info=:(inft=:InfoFont font centered,x,y,line) pState
			| not centered
				= (info,pState)
			| otherwise
				# (width,pState)	= accPIO (accScreenPicture (getFontStringWidth font line)) pState
				= ((inft,(maxx-width)/2,y,line),pState)
	
	readInfoFile :: String String *File -> (Bool,[String],*File)
	readInfoFile begin end file
		# (begin_found,file)	= findInfoBegin begin file
		| not begin_found
			= (False,[],file)
		| otherwise
			# (lines,file)		= readInfoUntil end file
			= (True,lines,file)
	where
		findInfoBegin :: String *File -> (Bool,*File)
		findInfoBegin begin file
			# (endOfFile,file)		= fend file
			| endOfFile				= (False,file)
			# (line,file)			= freadline file
			| isPrefixOf begin line	= (True,file)
			| otherwise				= findInfoBegin begin file
		
		readInfoUntil :: String *File -> ([String],*File)
		readInfoUntil end file
			# (endOfFile,file)		= fend file
			| endOfFile
				= ([],file)
			# (line,file)			= freadline file
			| isPrefixOf end line
				= ([],file)
			| otherwise
				# (lines,file)		= readInfoUntil end file
				= ([stripNewline line:lines],file)
		where
			stripNewline :: String -> String
			stripNewline string
				| string==""			= string
				| string.[last]<>'\n'	= string
				| otherwise				= string%(0,last-1)
			where
				last					= size string-1
		
		isPrefixOf :: String String -> Bool
		isPrefixOf prefix string
			| prefixlen>size string		= False
			| otherwise					= prefix==string%(0,prefixlen-1) 
		where
			prefixlen					= size prefix


/*	The drawing of the about/help info. */

drawInfo :: Font Int Int [InfoLine] *Picture -> *Picture
drawInfo defaultfont top bot [(InfoFont font c,x,y,line):rest] pic
	| y>bot		= pic
	| y<top		= drawInfo defaultfont top bot rest pic
	| otherwise
		# pic	= setPenFont font pic
		# pic	= drawAt {x=x,y=y} line pic
		# pic	= setPenFont defaultfont pic
		= drawInfo defaultfont top bot rest pic
drawInfo defaultfont top bot [(NoFont c,x,y,line):rest] pic
	| y>bot		= pic
	| y<top		= drawInfo defaultfont top bot rest pic
	| otherwise	= drawInfo defaultfont top bot rest (drawAt {x=x,y=y} line pic)
drawInfo _ _ _ _ pic
	= pic