File: portray_object.pl

package info (click to toggle)
swi-prolog-packages 5.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 50,688 kB
  • ctags: 25,904
  • sloc: ansic: 195,096; perl: 91,425; cpp: 7,660; sh: 3,046; makefile: 2,750; yacc: 843; awk: 14; sed: 12
file content (195 lines) | stat: -rw-r--r-- 6,465 bytes parent folder | download | duplicates (2)
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
/*  $Id: portray_object.pl,v 1.7 2002/02/01 15:04:49 jan Exp $

    Part of XPCE --- The SWI-Prolog GUI toolkit

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    Copyright (C): 1985-2002, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(pce_portray_object,
	[ portray_object/1
	, portray_object/2
	]).


:- use_module(library(pce)).
:- require([ maplist/3
	   , memberchk/2
	   ]).

%   Note: you may wish to incorporate portray_object/2 with the
%   standard portray mechanism of your Prolog.  In that case:
%
%	portray(Object) :-
%		object(Object), !,
%		portray_object(Object).


%   Sometimes the use of object references can be a new nuisance, in particular
%   while writing and debugging PCE programs.  Suppose you have done:
%
%	new(@s, spatial(xref=x+w, yref=x+h/2, xref=x, yref=y+h))
%
%   then
%
%	object(@s, S)
%	S = spatial(@1234, @1235, @1236, @1237, @default, @default)
%
%   is not of much use.  portray_object/2 makes life easier:
%
%	portray_object(@s, S)
%	S = spatial(xref=x+w, yref=x+h/2, xref=x, yref=y+h)
%
%   More or less expanding the arguments until they become readable.
%   portray_object/3 uses rules which specify how each object will be
%   portrayed.  You can make private extensions to these rules if you like.


%   +portray_class(Description, Term)
%
%   Term is a template which may contain object references
%   which need to be portrayed recursively (indicated with the "p/" prefix):
%
%	portray_class(constraint(A, B, C), _, constraint(A, B, p/C)).
%
%   Which should not touch the first two arguments (A and B), but
%   portrays C recursively.

vararg_class(Class) :-
	get(@pce, convert, Class, class, TheClass),
	get(TheClass, term_names, @nil).

portray_class(+(A, B), +(p/A, p/B)).
portray_class(-(A, B), -(p/A, p/B)).
portray_class(*(A, B), *(p/A, p/B)).
portray_class(/(A, B), /(p/A, p/B)).
portray_class(=(A, B), =(p/A, p/B)).
portray_class(==(A, B), ==(p/A, p/B)).
portray_class(\==(A, B), \==(p/A, p/B)).
portray_class(if(A,B,C), if(p/A, p/B, p/C)).
portray_class(while(A,B), while(p/A, p/B)).
portray_class(when(A,B,C), when(p/A, p/B, p/C)).
portray_class(attribute(A, B), attribute(A, p/B)).
portray_class(constraint(A, B, C), constraint(A, B, p/C)).
portray_class(handler(A, B, C), handler(A, p/B, p/C)).
portray_class(identity(A, A), identity(A)).
portray_class(identity(A, B), identity(A, B)).
portray_class(line(A, B, C, D), line(A, B, C, D)).
portray_class(link(A, A, _), link(A)).
portray_class(link(A, B, C), link(A, B, p/C)).
portray_class(number(A), A).
portray_class(node(A), node(p/A)).
portray_class(text(A,B,C), text(p/A, B, C)).
portray_class(button(A,B), button(A, p/B)).
portray_class(real(A), A).
portray_class(type(Name, _, _, _), Name).
portray_class(spatial(A, B, C, D, @default, @default), spatial(p/A, p/B, p/C, p/D)).
portray_class(spatial(A, B, C, D, @nil, @nil), spatial(p/A, p/B, p/C, p/D)).
portray_class(spatial(A, B, C, D, E, F), spatial(p/A, p/B, p/C, p/D, p/E, p/F)).
portray_class(string(A), A).
portray_class(click_gesture(A, B, C, D, E, F),
	      click_gesture(A, p/B, C, p/D, p/E, p/F)).
portray_class(handle(A,B,C,D), handle(p/A, p/B, C, D)).
portray_class(quote_function(X), quote_function(p/X)).
portray_class(Term, NewTerm) :-
	functor(Term, Functor, _), 
	vararg_class(Functor), !,
	Term =.. [Functor|Arguments], 
	maplist(tag_p, Arguments, NewArguments), 
	NewTerm =.. [Functor|NewArguments].
portray_class(A, A).

tag_p(X, p/X).

%	global_object(+Ref)
%	Declare commonly known objects

global_object(@nil).
global_object(@default).
global_object(@arg1).
global_object(@arg2).
global_object(@arg3).
global_object(@arg4).
global_object(@arg5).
global_object(@arg6).
global_object(@arg7).
global_object(@arg8).
global_object(@arg9).
global_object(@arg10).
global_object(@receiver).
global_object(@event).
global_object(@pce).
global_object(@prolog).
global_object(@display).
global_object(@classes).
global_object(@cursor_names).
global_object(@event_tree).
global_object(@white_image).
global_object(@grey12_image).
global_object(@grey25_image).
global_object(@grey50_image).
global_object(@grey75_image).
global_object(@black_image).
global_object(@on).
global_object(@off).

%   portray_object(+@Object)
%
%   Prints the result of portray_object/2 on the display.

portray_object(Object) :-
	portray_object(Object, Term), 
	print(Term), nl, !.


%   portray_object(+@Object, -Term)
%
%   Expands the object description of Object in a human readable form
%   and returs this in Term.  portray_object/2 uses the rules found under
%   portray_class/2.

portray_object(Obj, Term) :-
	portray_object(Obj, Term, []).

portray_object(@Object, @Object, _) :-
	global_object(@Object), !.
portray_object(Obj, '<recursive>'(Obj), Done) :-
	memberchk(Obj, Done), !.
portray_object(@Object, Term, Done) :-
	object(@Object, Description), 
	portray_class(Description, Result), 
	portray_description(Result, Term, [@Object|Done]), !.
portray_object(Term, Term, _).

portray_description(Result, Term, Done) :-
	Result =.. Arguments, 
	maplist(portray_argument(Done), Arguments, List), !,
	Term =.. List.
portray_description(Term, Term, _).

portray_argument(Done, p/Object, Term) :- !,
	portray_object(Object, Term, Done).
portray_argument(_, Term, Term).