1[subsection {Class API}]
2
3The package exports the API described here.
4
5[list_begin definitions]
6
7	[call [cmd ::diagram] [arg objectName] [arg canvas] [opt [arg script]]]
8
9		The command creates a new instance of a diagram
10		controller and returns the fully qualified name of the
11		object command as its result.
12
13		The new instance is connected to the specified
14		[arg canvas] object, which is used as the diagrams
15		graphics engine. This is usually an instance of Tk's
16		canvas, however any object which is API compatible to
17		Tk's canvas can be used here.
18
19	[para]
20
21		The API of this object command is described in the
22		following section, [sectref {Object API}]. It may be
23		used to invoke various operations on the object.
24
25	[para]
26
27		If the [arg script] argument is specified then method
28		[method draw] will be invoked on it.
29
30[list_end]
31
32[subsection {Object API}]
33
34	    Instances of the diagram class support the following
35	    methods:
36
37[list_begin definitions]
38
39	[call [arg diagramObject] [method {new direction}] [arg name] [opt "[arg key] [arg value]..."]]
40
41		This method defines a new named direction and its
42		attributes. The latter is given through the
43		[arg key]/[arg value] pairs coming after the [arg name].
44
45	[para]
46
47		Users are mostly free to specify arbitrary attributes
48		with whatever meaning they desire. The exception are
49		the names [emph angle] and [emph opposite]. They are
50		special to the diagram package and have a fixed meaning.
51
52	[list_begin definitions]
53	[def angle]
54
55		This attribute specifies the angle of the direction in
56		degrees, where 0 points east (to the right) and 90 points
57		north (up).
58
59	[def opposite]
60
61		This attribute specifies the name of the direction
62		which should be considered as complementary to the
63		named one.
64
65	[list_end]
66
67
68	[call [arg diagramObject] [method {new element}] [arg name] [arg attributes] [arg cmdprefix]]
69
70		This method defines a new graphics element for the
71		drawing language. I.e. [arg name] will become a new
72		command in the language, and the specified command
73		prefix ([arg cmdprefix]) will be called to perform
74		the actual drawing.
75
76	[para]
77		[arg attributes] specifies the set of attributes for which
78		data has to be available. I.e. the system will run the
79		...-callbacks for these attributes.
80
81		See the method [method {new attribute}] for more information
82		on attribute definitions.
83
84	[para]
85
86		The command prefix is expected to conform to the
87		following signature:
88
89	[list_begin definitions]
90	[def "[cmd cmdprefix] [arg canvas] [arg attributes]"]
91
92		Where [arg canvas] is the handle of the canvas widget
93		to draw to, and [arg attributes] is a dictionary
94		holding the attributes for the element, be they
95		user-specified, or defaults.
96
97	[para]
98		The results of the command has to be a list containing
99		at least two and at most four items. These are, in order:
100
101		[list_begin enumerated]
102		[enum]	The list of canvas items the drawn element consists of.
103		[enum]	The dictionary of named locations in the element, its
104			[term corners].
105		[enum]	An optional mode, either "relative" or "absolute".
106			When not returned "relative" is assumed. In the
107			case of a relative element position the attributes
108			"with" and "at" are used to determine the final
109			position of the new element.
110		[enum]	An optional name of a direction. If not the
111			empty string this is handed to the automatic
112			layouter as the new direction to follow.
113		[list_end]
114	[list_end]
115
116
117	[call [arg diagramObject] [method {new alias}] [arg name] [arg cmdprefix]]
118
119		This method defines a new command for the drawing
120		language. I.e. [arg name] will become a new command in
121		the language, and the specified command prefix
122		([arg cmdprefix]) will be called on use of this new
123		command. Any arguments given to the command are
124		simply passed to the prefix. There is no fixed siganture.
125
126	[para]
127		Note that the prefix is run in the context of the
128		drawing language, allowing the direct use of any
129		existing commands.
130
131
132	[call [arg diagramObject] [method {new command}] [arg name] [arg arguments] [arg body]]
133
134		This is like [method {new alias}] except that the new
135		command is defined as a procedure in the language's
136		context, with regular argument list and body.
137
138	[call [arg diagramObject] [method {new attribute}] [arg name] [opt "[arg key] [arg value]..."]]
139
140		This method defines a new named attribute which can be
141		used by graphical elements. The handling of the
142		attribute by the processor is declared through the
143		[arg key]/[arg value] pairs coming after the [arg name].
144
145	[para]
146		The accepted keys and their meanings are:
147
148		[list_begin definitions]
149
150		[def [const key]]
151
152			The value of this key is the name of the key
153			under which the attribute's value shall be
154			stored in the attribute dictionary given to
155			the drawing command after attribute processing
156			is complete.
157
158		[para]
159
160			This key is optional. If it is not specified it
161			defaults to the name of the attribute.
162
163		[def [const get]]
164
165			The value of this key is a command prefix
166			which will be invoked to retrieve the
167			attribute's argument(s) from the command
168			line.
169
170		[para]
171
172			This key is optional. If it is not specified a
173			default is used which takes the single word
174			after the attribute name as the attribute's
175			value.
176
177		[para]
178			The signature of the command prefix is
179
180			[list_begin definitions]
181			[def "[cmd cmdprefix] [arg wordqueue]"]
182
183				Where [arg wordqueue] is the handle of
184				a queue object conforming to the API
185				of the queues provided by package
186				[package struct::queue]. This queue
187				contains the not-yet-processed part of
188				the attribute definitions, with one
189				entry per word, with the first entry
190				the word [emph after] name of the
191				attribute. In other words, the
192				attribute's name has already been
193				removed from the queue.
194
195			[para]
196				The result of the command is the value
197				of the attribute, which may have been
198				taken from the queue, or not.
199
200			[list_end]
201
202
203		[def [const transform]]
204
205			The value of this key is a command prefix
206			which will be invoked to transform the
207			retrieved value (See [const get]) into their
208			final form.
209
210		[para]
211
212			This key is optional. If it is not specified
213			no transformation is done.
214
215			The signature of the command prefix is
216
217			[list_begin definitions]
218			[def "[cmd cmdprefix] [arg value]"]
219
220				Where [arg value] is the value to transform.
221
222			[para]
223
224				The result of the command is the final
225				value of the attribute.
226
227			[list_end]
228
229		[def [const type]]
230
231			The value of this key is a command prefix
232			which will be invoked to validate the
233			attribute's argument(s).
234
235		[para]
236
237			This key is optional. If it is not specified
238			no validation is done.
239
240		[para]
241
242			The signature of the command prefix is that of
243			snit validation types. See the documentation
244			of the [package snit] package.
245
246		[def [const merge]]
247
248			The value of this key is a command prefix
249			which will be invoked to insert the
250			transformed and validated attribute value into
251			the dictionary of collected attributes.
252
253		[para]
254
255			This key is optional. If it is not specified
256			a default merge is chosen, based on the data
257			for key [const aggregate], see below.
258
259			The signature of the command prefix is
260
261			[list_begin definitions]
262			[def "[cmd cmdprefix] [arg value] [arg dict]"]
263
264				Where [arg value] is the value to
265				insert, and [arg dict] the dictionary
266				of attributes and values collected so
267				far.
268
269			[para]
270
271				The result of the command is the new
272				dictionary of attributes.
273
274			[list_end]
275
276		[def [const aggregate]]
277
278			The value of this key is a boolean flag. It
279			has an effect if and only if the key [const \
280			merge] was not specified. This key is
281			optional. If it is not specified it defaults
282			to [const False].
283
284		[para]
285
286			If the key is effective, the value of [const \
287			False] means that the attribute's value is
288			[emph set] into the dictionary using the value
289			of key [arg key] (see above) as index,
290			[emph overwriting] any previously specified value.
291
292		[para]
293			If the key is effective, the value of [const \
294			True] means that the attribute's value is
295			[emph added] to the dictionary using the value
296			of key [arg key] (see above) as index,
297			[emph extending] any previously specified value.
298
299			This means that the final value of the
300			attribute as seen after processing will be a
301			list of the collected values.
302
303		[def [const default]]
304
305			The value of this key is a command prefix
306			which will be invoked after collection of
307			attributes has been completed and this
308			attribute is in the list of required
309			attributes for the drawing element (See
310			argument [arg attributes] of method
311			[method {new element}]).
312
313		[para]
314
315			Note that the connection is made through the
316			value of key [arg key], not through the
317			attribute name per se.
318
319		[para]
320
321			Further note that this command prefix is
322			invoked even if a user specified attribute
323			value is present. This allows the command
324			to go beyond simply setting defaults, it
325			can calculate and store derived values as
326			well.
327
328		[para]
329
330			This key is optional. If an element requires
331			this attribute, but [arg default] is not
332			specified then nothing will be done.
333
334		[para]
335			The signature of the command prefix is
336
337			[list_begin definitions]
338			[def "[cmd cmdprefix] [method init]"]
339
340				This method is run when the attribute
341				is defined, its responsibility is to
342				initialize anything in the language
343				namespace for the attribute and
344				default processing.
345			[para]
346				The result of this method is ignored.
347
348			[def "[cmd cmdprefix] [method fill] [arg varname]"]
349
350				This method is run to put defaults, or
351				derived values into the attribute dictionary
352				named by [arg varname]. This variable will
353				be found in the calling context.
354			[para]
355				The result of this method is ignored.
356
357			[def "[cmd cmdprefix] [method set] [arg name] [arg value]"]
358
359				This method is run to push current a
360				attribute value into the language
361				namespace, to make it the new default.
362
363			[para]
364				The result of this method is ignored.
365
366			[list_end]
367
368		[def [const linked]]
369
370			This key is effective if and only if key
371			[const default] is not specified. In that
372			case is supplies a default handling for
373			[const default], linking the attribute to a
374			variable in the language context.
375
376		[para]
377			The value for this key is a 2-element list
378			containing the name of the variable to link
379			to, and its initial value, in this order.
380
381		[list_end]
382
383	[call [arg diagramObject] [method {unknown attribute}] [arg cmdprefix]]
384
385		This method registers the command prefix with the
386		subsystem processing the attributes for element
387		commands, telling it to call it when it encounters an
388		attribute it is unable to handle on its on.
389
390	[para]
391
392		It is allowed to register more than callback, these
393		will be called in order of registration (i.e. first to
394		last), until one of the callbacks accepts the current
395		input.
396
397		The command prefix is expected to conform to the
398		following signature:
399
400		[list_begin definitions]
401		[def "[cmd cmdprefix] [arg wordqueue]"]
402
403			Where [arg wordqueue] is the handle of a queue
404			object conforming to the API of the queues
405			provided by package [package struct::queue].
406			This queue contains the not-yet-processed part
407			of the attribute definitions, with one entry
408			per word, with the first entry the name of the
409			attribute which could not be processed.
410
411		[para]
412
413			The results of the command has to be a boolean
414			value where [const True] signals that this
415			callback has accepted the attribute, processed
416			it, and the new state of the [arg wordqueue]
417			is where the general processing shall continue.
418
419		[para]
420
421			Given the signature the command has basically
422			two ways of handling (rewriting) the attributes
423			it recognizes:
424
425			[list_begin enumerated]
426			[enum]	Replace the attribute (and arguments)
427				with a different attribute and arguments.
428			[enum]	Push additional words in front to get
429				the general processing unstuck.
430			[list_end]
431		[list_end]
432
433
434	[call [arg diagramObject] [method draw] [arg script]]
435
436		This method runs the given [arg script] in the
437		context of the drawing language definitions.
438		See section [sectref {Language Reference}] for
439		details on the available commands.
440
441	[para]
442		[emph Note] that [arg script] is [emph trusted].
443		It is executed in the current interpreter with
444		access to its full abilities.
445
446		For the execution of untrusted diagram scripts this
447		interpreter should be a safe one.
448[list_end]
449
450[comment {
451[subsection {Element/Attribute Processing}]
452
453[image examples/figure-00-eap]
454
455}]
456