1# File: config.tcl
2
3# Purpose: icon configuration management
4
5#
6# Copyright (c) 1997-2001 Tim Baker
7#
8# This software may be copied and distributed for educational, research, and
9# not for profit purposes provided that this copyright and statement are
10# included in all such copies.
11#
12
13namespace eval NSConfig {
14
15	variable Priv
16
17# namespace eval NSConfig
18}
19
20# NSConfig::InitModule --
21#
22#	One-time-only-ever initialization.
23#
24# Arguments:
25#	arg1					about arg1
26#
27# Results:
28#	What happened.
29
30proc NSConfig::InitModule {} {
31
32	# Read tk/config, which contains a list of icon configurations
33	# and the current icon configuration.
34	ReadConfigFile
35
36	# Set the default set of files to pass to SourceOne. These
37	# can be overridden by scripts to use common configuration
38	# files. See ShareConfigFile() below.
39	SetPrefix [Value config,prefix]
40
41	return
42}
43
44# NSConfig::ReadConfigFile --
45#
46#	Reads the tk/config file, which holds a list of configuration
47#	prefixes, along with descriptive text for each prefix. Each
48#	prefix can be used to read and write certain icon configuration
49#	files.
50#
51# Arguments:
52#	arg1					about arg1
53#
54# Results:
55#	What happened.
56
57proc NSConfig::ReadConfigFile {} {
58
59	variable Priv
60
61	if {[catch {open [PathTk config config]} fileId]} {
62		set msg "The following error occurred while attempting to open "
63		append msg "the \"config\" file for reading:\n\n$fileId"
64		tk_messageBox -title Oops -message $msg
65		return
66	}
67
68	while {![eof $fileId]} {
69
70		# Read a line
71		set count [gets $fileId list]
72		if {$count == -1} break
73
74		# Save the text, so it can be written out later
75		lappend Priv(text) $list
76
77		if {$count == 0} continue
78
79		switch -- [lindex $list 0] {
80			Config: {
81				lappend Priv(config) [lindex $list 1] [lindex $list 2]
82			}
83		}
84	}
85
86	close $fileId
87
88	return
89}
90
91# NSConfig::Load --
92#
93#	Processes the set of files for the "current" configuration set.
94#
95# Arguments:
96#	arg1					about arg1
97#
98# Results:
99#	What happened.
100
101proc NSConfig::Load {} {
102
103	# Get the current configuration prefix
104	set prefix [Global config,prefix]
105
106	angband_load note $prefix
107
108	# Try "prefix.cfg"
109	SourceOne $prefix.cfg
110
111	# Try prefixNN.cfg
112	regsub {(16|24|32)} $prefix NN prefix
113	SourceOne $prefix.cfg
114	return
115}
116
117# NSConfig::InitIcons --
118#
119#	Description.
120#
121# Arguments:
122#	arg1					about arg1
123#
124# Results:
125#	What happened.
126
127proc NSConfig::InitIcons {iconSize} {
128
129	angband init_icons $iconSize [winfo depth .]
130
131	return
132}
133
134# NSConfig::SetPrefix --
135#
136#	Description.
137#
138# Arguments:
139#	arg1					about arg1
140#
141# Results:
142#	What happened.
143
144proc NSConfig::SetPrefix {prefix} {
145
146	Value config,prefix $prefix
147
148	Global config,prefix $prefix
149	Global config,assign $prefix-assign
150	Global config,town $prefix-town
151	Global config,postop $prefix-postop
152
153	return
154}
155
156# NSConfig::ShareConfigFile --
157#
158#	Description.
159#
160# Arguments:
161#	arg1					about arg1
162#
163# Results:
164#	What happened.
165
166proc NSConfig::ShareConfigFile {which file} {
167
168	switch -- $which {
169		assign -
170		town {
171			Global config,$which $file
172		}
173
174		default {
175			error "unknown config file \"$which\""
176		}
177	}
178
179	return
180}
181
182# NSConfig::SourceOne --
183#
184#	Looks for the given file in the tk/config directory. If it
185#	exists, it is sourced at the global level. This command is
186#	usually called from a icon configuration file, type ".cfg".
187#
188# Arguments:
189#	arg1					about arg1
190#
191# Results:
192#	What happened.
193
194proc NSConfig::SourceOne {fileName {required 0}} {
195
196	set fileName [file tail $fileName]
197	set path [PathTk config $fileName]
198	if {[file exists $path]} {
199		uplevel #0 source $path
200		return
201	}
202	if {$required} {
203		error "can't find file \"$fileName\""
204	}
205
206	return
207}
208
209# NSConfig::Source --
210#
211#	Looks for the given file in the tk/config directory. If it
212#	exists, it is sourced in the given namespace. This command is
213#	usually called from a icon configuration file, type ".cfg".
214#
215# Arguments:
216#	arg1					about arg1
217#
218# Results:
219#	What happened.
220
221proc NSConfig::Source {fileName namespace} {
222
223	set fileName [file tail $fileName]
224	set fileName [PathTk config $fileName]
225	if {[file exists $fileName]} {
226		${namespace}::Source $fileName
227	}
228
229	return
230}
231
232# NSConfig::FileLibData --
233#
234#	Takes the "tail" of the given file name, and appends it to the
235#	complete pathname of the image directory.
236#
237# Arguments:
238#	arg1					about arg1
239#
240# Results:
241#	What happened.
242
243proc NSConfig::FileLibData {file} {
244
245	set file [file tail $file]
246	return [PathTk image $file]
247}
248
249# NSConfig::FindImage --
250#
251#	Find an image file.
252#
253# Arguments:
254#	arg1					about arg1
255#
256# Results:
257#	What happened.
258
259proc NSConfig::FindImageFile {imageFile} {
260
261	set path [PathTk image $imageFile]
262	if {[file exists $path]} {
263		return $path
264	}
265	set path [PathTk image dg [file tail $imageFile]]
266	if {[file exists $path]} {
267		return $path
268	}
269	error "icon image file \"$imageFile\" was not found"
270
271	return
272}
273
274
275# NSConfig::NoMoreIcons --
276#
277#	This is a big silly hack called when all the icon types have
278#	been created. It is used just so I can update the progress bar
279#	during startup.
280#
281# Arguments:
282#	arg1					about arg1
283#
284# Results:
285#	What happened.
286
287proc NSConfig::NoMoreIcons {} {
288
289	global AngbandPriv
290
291	set canvas $AngbandPriv(load,win).canvas
292	$canvas itemconfigure message -text "Assigning icons..."
293	update
294
295	return
296}
297
298
299# Config::Assign --
300#
301#	A namespace with commands called when the tk/config/assign file
302#	is sourced.
303#
304
305namespace eval Config::Assign {
306
307	variable Priv
308
309#namespace eval Config::Assign
310}
311
312# Evaluate a script
313proc Config::Assign::Source {path} {
314
315	source $path
316
317	return
318}
319
320# Add an assign type
321proc Config::Assign::Assign {type} {
322
323	variable Priv
324
325	lappend Priv(assignType) $type
326
327	return
328}
329
330# Add an icon type
331proc Config::Assign::Type {type} {
332
333	variable Priv
334
335	lappend Priv(type) $type
336
337	return
338}
339
340# Start assigning to this group
341proc Config::Assign::Group {group} {
342
343	variable Priv
344
345	set Priv(group) $group
346
347	return
348}
349
350proc Config::Assign::Feat {light background} {
351
352	variable Priv
353
354	feature configure $Priv(member) -light $light -background $background
355
356	return
357}
358