View source with formatted comments or as raw
   1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
   2
   3    Author:        Jan Wielemaker and Anjo Anjewierden
   4    E-mail:        jan@swi.psy.uva.nl
   5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
   6    Copyright (c)  1985-2002, University of Amsterdam
   7    All rights reserved.
   8
   9    Redistribution and use in source and binary forms, with or without
  10    modification, are permitted provided that the following conditions
  11    are met:
  12
  13    1. Redistributions of source code must retain the above copyright
  14       notice, this list of conditions and the following disclaimer.
  15
  16    2. Redistributions in binary form must reproduce the above copyright
  17       notice, this list of conditions and the following disclaimer in
  18       the documentation and/or other materials provided with the
  19       distribution.
  20
  21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32    POSSIBILITY OF SUCH DAMAGE.
  33*/
  34
  35:- module(pce_util,
  36          [ get_object/3,               % +Receiver, +Selector, ..., -Result
  37            get_object/4,
  38            get_object/5,
  39            get_object/6,
  40            get_object/7,
  41            get_object/8,
  42
  43            send_list/2,                % +Receivers, +Methods
  44            send_list/3,                % +Receivers, +Methods, +Args
  45
  46            get_chain/3,                % +Receiver, +Selector, -List
  47            chain_list/2,               % ?Chain, ?List
  48
  49            default/3                   % +MethodArg, +Default, -Arg
  50          ]).
  51
  52
  53:- meta_predicate
  54    get_object(+, :, -),
  55    get_object(+, :, +, -),
  56    get_object(+, :, +, +, -),
  57    get_object(+, :, +, +, +, -),
  58    get_object(+, :, +, +, +, +, -),
  59    get_object(+, :, +, +, +, +, +, -),
  60
  61    send_list(:, +),
  62    send_list(:, +, +),
  63    send_list1(:, +),
  64    send_list1(:, +, +),
  65
  66    get_chain(+, :, -).
  67
  68
  69:- use_module(library(pce)).
  70
  71%%   get_object(+Object, +Selector, +Arg..., -Output) is semidet.
  72%
  73%   Succeeds once if Output is the value returned by invoking get method
  74%   called Selector on Object.  Output is an object description, except for the
  75%   special objects @nil, @default, @on and @off all of which are both
  76%   object descriptions and object names.
  77
  78get_object(Obj, Sel, Out) :-
  79    get(Obj, Sel, R),
  80    get_to_object(R, Out).
  81get_object(Obj, Sel, A1, Out) :-
  82    get(Obj, Sel, A1, R),
  83    get_to_object(R, Out).
  84get_object(Obj, Sel, A1, A2, Out) :-
  85    get(Obj, Sel, A1, A2, R),
  86    get_to_object(R, Out).
  87get_object(Obj, Sel, A1, A2, A3, Out) :-
  88    get(Obj, Sel, A1, A2, A3, R),
  89    get_to_object(R, Out).
  90get_object(Obj, Sel, A1, A2, A3, A4, Out) :-
  91    get(Obj, Sel, A1, A2, A3, A4, R),
  92    get_to_object(R, Out).
  93get_object(Obj, Sel, A1, A2, A3, A4, A5, Out) :-
  94    get(Obj, Sel, A1, A2, A3, A4, A5, R),
  95    get_to_object(R, Out).
  96
  97get_to_object(Ref, Object) :-
  98    (   atomic(Ref)
  99    ->  Object = Ref
 100    ;   object(Ref, Object)
 101    ).
 102
 103
 104%!  send_list(+ListOfObjs, +ListOfSels)
 105%
 106%   Send a messages to the carthesian product of ListOfObjs and
 107%   ListOfSels.
 108
 109send_list(X, _) :-
 110    var(X),
 111    throw(error(instantiation_error, _)).
 112send_list(_, X) :-
 113    var(X),
 114    throw(error(instantiation_error, _)).
 115pce_ifhostproperty(prolog(quintus), [],
 116(   send_list([], _) :- !)).
 117send_list(_, []) :- !.
 118pce_ifhostproperty(prolog(quintus), [],
 119(   send_list([Object|Objects], Selectors) :- !,
 120        send_list(Object, Selectors),
 121        send_list(Objects, Selectors))).
 122send_list(Object, [Selector|Selectors]) :-
 123    !,
 124    send_list(Object, Selector),
 125    send_list(Object, Selectors).
 126send_list(Object, Selector) :-
 127    send_list1(Object, Selector).
 128
 129send_list1(Module:Obj, Selector) :-
 130    atom(Module),
 131    !,
 132    send_list_module(Obj, Selector, Module).
 133send_list1(Object, Selector) :-
 134    send(Object, Selector).
 135
 136send_list_module([], _, _) :- !.
 137send_list_module(_, [], _) :- !.
 138send_list_module([Object|Objects], Selectors, Module) :-
 139    !,
 140    send_list_module(Object, Selectors, Module),
 141    send_list_module(Objects, Selectors, Module).
 142send_list_module(Object, [Selector|Selectors], Module) :-
 143    !,
 144    send_list_module(Object, Selector, Module),
 145    send_list_module(Object, Selectors, Module).
 146send_list_module(Object, Selector, Module) :-
 147    send(Object, Module:Selector).
 148
 149
 150%!   send_list(+ListOfObjs, +ListOfSels, +ListOfArgs)
 151%
 152%   Send a messages to the carthesian product of ListOfObjs and
 153%   ListOfSels.
 154
 155send_list(X, _, _) :-
 156    var(X),
 157    throw(error(instantiation_error, _)).
 158send_list(_, X, _) :-
 159    var(X),
 160    throw(error(instantiation_error, _)).
 161send_list(_, _, X) :-
 162    var(X),
 163    throw(error(instantiation_error, _)).
 164pce_ifhostproperty(prolog(quintus), [],
 165(   send_list([], _,  _) :- !)).
 166send_list(_, [], _) :- !.
 167send_list(_, _, []) :- !.
 168pce_ifhostproperty(prolog(quintus), [],
 169(   send_list([Object|Objects], Selectors, Arguments) :- !,
 170        send_list(Object, Selectors, Arguments),
 171        send_list(Objects, Selectors, Arguments))).
 172send_list(Objects, [Selector|Selectors], Arguments) :-
 173    !,
 174    send_list(Objects, Selector, Arguments),
 175    send_list(Objects, Selectors, Arguments).
 176send_list(Object, Selector, [Argument|Arguments]) :-
 177    !,
 178    send_list(Object, Selector, Argument),
 179    send_list(Object, Selector, Arguments).
 180send_list(Object, Selector, Argument) :-
 181    send_list1(Object, Selector, Argument).
 182
 183send_list1(Module:Obj, Selector, Arg) :-
 184    atom(Module),
 185    !,
 186    send_list_module(Obj, Selector, Arg, Module).
 187send_list1(Obj, Selector, Arg) :-
 188    send(Obj, Selector, Arg).
 189
 190send_list_module([], _, _, _) :- !.
 191send_list_module(_, [], _, _) :- !.
 192send_list_module(_, _, [], _) :- !.
 193send_list_module([Object|Objects], Selectors, Arguments, Module) :-
 194    !,
 195    send_list_module(Object, Selectors, Arguments, Module),
 196    send_list_module(Objects, Selectors, Arguments, Module).
 197send_list_module(Objects, [Selector|Selectors], Arguments, Module) :-
 198    !,
 199    send_list_module(Objects, Selector, Arguments, Module),
 200    send_list_module(Objects, Selectors, Arguments, Module).
 201send_list_module(Object, Selector, [Argument|Arguments], Module) :-
 202    !,
 203    send_list_module(Object, Selector, Argument, Module),
 204    send_list_module(Object, Selector, Arguments, Module).
 205send_list_module(Object, Selector, Argument, Module) :-
 206    send(Object, Module:Selector, Argument).
 207
 208
 209%%   get_chain(+Object, +Selector, -List:list) is semidet.
 210%
 211%   List is a Prolog list constructed from the PCE chain returned by <-Selector
 212%   on Object.  get_chain/3 returns a list of object names,
 213
 214get_chain(Object, Selector, List) :-
 215    get(Object, Selector, Chain),
 216    chain_list(Chain, List).
 217
 218
 219%!  chain_list(+Chain, -List) is det.
 220%!  chain_list(-Chain, +List) is det.
 221%
 222%   Convert between a Prolog list and an XPCE chain object.
 223
 224chain_list(Chain, List) :-
 225    nonvar(Chain),
 226    !,
 227    (   Chain == @nil
 228    ->  List = []
 229    ;   to_object(Chain, ChainObject),
 230        send(ChainObject, instance_of, chain),
 231        (   send(ChainObject, current_no, 1)
 232        ->  chain_to_list_(ChainObject, List)
 233        ;   List = []
 234        )
 235    ).
 236chain_list(Chain, List) :-
 237    new(Chain, chain),
 238    send_list(Chain, append, List).
 239
 240chain_to_list_(Chain, [El|Rest]) :-
 241    get(Chain, next, El),
 242    !,
 243    chain_to_list_(Chain, Rest).
 244chain_to_list_(Chain, []) :-
 245    \+ get(Chain, current, _).
 246
 247to_object(Ref, Ref) :-
 248    object(Ref),
 249    !.
 250to_object(Term, Obj) :-
 251    new(Obj, Term).
 252
 253
 254                /********************************
 255                *             DEFAULTS          *
 256                ********************************/
 257
 258%!  default(+Argument, +Default, -Value) is det.
 259%
 260%   Get the default value for an argument.  Default is either a
 261%   plain value or a term class_variable(+Object, +Name).
 262
 263default(@default, Default, Value) :-
 264    !,
 265    (   var(Default)
 266    ->  Value = Default
 267    ;   (   Default = class_variable(Obj, Name)
 268        ;   Default = resource(Obj, Name)
 269        )
 270    ->  (   get(Obj, class_variable_value, Name, Value)
 271        ->  true
 272        ;   pce_error(get_class_variable_failed(Name, Obj))
 273        )
 274    ;   Value = Default
 275    ).
 276default(Value, _Default, Value).