summaryrefslogtreecommitdiff
path: root/showlicense.mod/showlicense.bmx
blob: 09ea7c30eede3652edcd19be8d7641cc3122d40c (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
' Copyright (c) 2006 Ian Cowburn
' 
' Permission is hereby granted, free of charge, to any person obtaining a copy of
' this software and associated documentation files (the "Software"), to deal in
' the Software without restriction, including without limitation the rights to
' use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
' of the Software, and to permit persons to whom the Software is furnished to do
' so, subject to the following conditions:
' 
' The above copyright notice and this permission notice shall be included in all
' copies or substantial portions of the Software.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
' 
' $Id$
'
Rem
bbdoc: noddybox.showlicense
EndRem
Module noddybox.showlicense

ModuleInfo "Framework: License acceptance routines"
ModuleInfo "Copyright: Ian Cowburn -- released under the MIT License"
ModuleInfo "Author: Ian Cowburn"
ModuleInfo "Version: $Revision$"

Strict
Import brl.max2d
Import brl.stream
Import brl.basic
Import brl.linkedlist

Rem
bbdoc: Displays a license for the user to accept.
returns: True if the license is accepted, false otherwise.
about:  The license stored in text format at the URL @short_license is displayed with a title of @title.
If the URL @full_license is not null then the user is provided with an option to display that too.
The width of text in the license files must fit on the screen...
EndRem
Function ShowLicense:Int(title:String, short_license:String, full_license:String)
	Local accepted:Int=False
	Local license:TLicense=New TLicense
	
	If full_license
		license.Init(title,short_license,["ACCEPT","DECLINE","VIEW FULL LICENSE"])
	Else
		license.Init(title,short_license,["ACCEPT","DECLINE"])
	EndIf
	
	Select license.Show()
		Case 0
			accepted=True
		Case 1
		Case 2
			license=New TLicense
			license.Init(title,full_license,["ACCEPT","DECLINE"])
			If (license.Show()=0)
				accepted=True
			End If
	End Select
	
	Return accepted
End Function

Private

Type TLicense
	Field top:Int
	Field bottom:Int
	Field list:TList
	Field item:TLink
	Field title:String
	Field opts:String[]
	Field sel:Int
	Field col:Int
	Field coli:Int
	
	Method New()
	End Method
	
	Method Centre(s:String, y:Int)
		DrawText(s,(GraphicsWidth()-TextWidth(s))/2,y)
	End Method
	
	Method MaxHeight:Int()
		Return TextHeight("X")
	End Method
	
	Method Init(t:String, u:String, o:String[])
		Local str:TStream=ReadStream(u)
		
		Assert str,"Unable to open help file"
		
		list=CreateList()
		
		While Not str.Eof()
			list.AddLast(str.ReadLine())
		Wend
		
		str.Close()
		
		title=t
		item=list.FirstLink()
		
		top=MaxHeight()*2
		bottom=GraphicsHeight()-MaxHeight()*3
		opts=o
		sel=0
		col=255
		coli=-5
	End Method
	
	Method Draw()
		Local y:Int=top+1
		Local i:TLink=item
		
		While y<bottom And i
			Local s:String=String(i.Value())
			DrawText(s,0,y)
			y:+TextHeight(s)
			i=i.NextLink()
		Wend

		SetColor(0,0,0)
		DrawRect(0,0,GraphicsWidth(),top)
		DrawRect(0,bottom,GraphicsWidth(),GraphicsHeight()-bottom)

		SetColor(255,255,255)
		DrawLine(0,TOP,GraphicsWidth(),TOP)
		DrawLine(0,BOTTOM,GraphicsWidth(),BOTTOM)
		
		Centre(title,MaxHeight()/2)
		Centre("CURSORS VIEW AND CHOOSE.  RETURN SELECTS.",bottom+MaxHeight()/2)
		
		Local x:Int=0
		y=GraphicsHeight()-MaxHeight()
		
		For Local f:Int=0 Until opts.length
			If f=sel
				SetColor(col/2,col/2,128+col)
				DrawRect(x,y,TextWidth(opts[f]),MaxHeight())
				SetColor(255,255,255)
			EndIf
			DrawText(opts[f],x,y)
			x:+TextWidth(opts[f])+20
		Next
		
		col:+coli
		
		If col<128 Or col>255
			coli=-coli
			col:+coli
		EndIf
	End Method
	
	Method Current:String()
		Return String(item.Value())
	End Method
	
	Method Show:Int()
		Local done:Int=False
		
		FlushKeys()

		While Not done
			Cls

			Draw()
			
			If KeyDown(KEY_DOWN)
				If item.NextLink()
					item=item.NextLink()
				EndIf
			ElseIf KeyDown(KEY_UP)
				If item.PrevLink()
					item=item.PrevLink()
				EndIf
			ElseIf KeyHit(KEY_LEFT)
				sel=Max(0,sel-1)
			ElseIf KeyHit(KEY_RIGHT)
				sel=Min(opts.length-1,sel+1)
			ElseIf KeyHit(KEY_RETURN)
				done=True
			EndIf
			
			Flip
		Wend
		
		FlushKeys()
		
		Return sel
	End Method
End Type