View source with raw comments or as raw
   1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
   2
   3    Author:        Jan Wielemaker and Anjo Anjewierden
   4    E-mail:        J.Wielemaker@cs.nu.nl
   5    WWW:           http://www.swi-prolog.nl/projects/xpce/
   6    Copyright (c)  1985-2011, University of Amsterdam
   7                              VU University Amsterdam
   8    All rights reserved.
   9
  10    Redistribution and use in source and binary forms, with or without
  11    modification, are permitted provided that the following conditions
  12    are met:
  13
  14    1. Redistributions of source code must retain the above copyright
  15       notice, this list of conditions and the following disclaimer.
  16
  17    2. Redistributions in binary form must reproduce the above copyright
  18       notice, this list of conditions and the following disclaimer in
  19       the documentation and/or other materials provided with the
  20       distribution.
  21
  22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33    POSSIBILITY OF SUCH DAMAGE.
  34*/
  35
  36:- module(pce_principal,
  37          [ new/2, free/1,
  38
  39            send/2, send/3, send/4, send/5, send/6, send/7,
  40            send/8,
  41
  42            get/3, get/4, get/5, get/6, get/7, get/8,
  43
  44            send_class/3,
  45            get_class/4,
  46
  47            object/1, object/2,
  48
  49            pce_class/6,
  50            pce_lazy_send_method/3,
  51            pce_lazy_get_method/3,
  52            pce_uses_template/2,
  53
  54            pce_method_implementation/2,
  55
  56            pce_open/3,                 % +Object, +Mode, -Stream
  57            in_pce_thread/1,            % :Goal
  58            set_pce_thread/0,
  59            pce_dispatch/0,
  60
  61            pce_postscript_stream/1,    % -Stream
  62
  63            op(200, fy,  @),
  64            op(250, yfx, ?),
  65            op(990, xfx, :=)
  66          ]).
  67
  68
  69:- meta_predicate
  70    send_class(+, +, :),
  71    send(+, :),
  72    send(+, :, +),
  73    send(+, :, +, +),
  74    send(+, :, +, +, +),
  75    send(+, :, +, +, +, +),
  76    send(+, :, +, +, +, +, +),
  77
  78    get_class(+, +, :, -),
  79    get(+, :, -),
  80    get(+, :, +, -),
  81    get(+, :, +, +, -),
  82    get(+, :, +, +, +, -),
  83    get(+, :, +, +, +, +, -),
  84    get(+, :, +, +, +, +, +, -),
  85
  86    new(?, :).
  87
  88:- op(100, fx, @).
  89:- op(150, yfx, ?).
  90:- op(990, xfx, :=).
  91
  92                /********************************
  93                *             HOME              *
  94                ********************************/
  95
  96%!  pce_home(-Home) is det.
  97%
  98%   True when Home is the home directory of XPCE.
  99
 100pce_home(PceHome) :-
 101    absolute_file_name(pce('.'), PceHome,
 102                       [ file_type(directory),
 103                         file_errors(fail)
 104                       ]),
 105    exists_directory(PceHome),
 106    !.
 107pce_home(PceHome) :-
 108    getenv('XPCEHOME', PceHome),
 109    exists_directory(PceHome),
 110    !.
 111pce_home(PceHome) :-
 112    (   current_prolog_flag(xpce_version, Version),
 113        atom_concat('/xpce-', Version, Suffix)
 114    ;   Suffix = '/xpce'
 115    ),
 116    absolute_file_name(swi(Suffix), PceHome,
 117                       [ file_type(directory),
 118                         file_errors(fail)
 119                       ]),
 120    exists_directory(PceHome),
 121    !.
 122pce_home(PceHome) :-
 123    current_prolog_flag(saved_program, true),
 124    !,
 125    (   current_prolog_flag(home, PceHome)
 126    ->  true
 127    ;   current_prolog_flag(executable, Exe)
 128    ->  file_directory_name(Exe, PceHome)
 129    ;   PceHome = '.'
 130    ).
 131pce_home(_) :-
 132    throw(error(pce_error(no_home), _)).
 133
 134
 135                /********************************
 136                *           LOAD C-PART         *
 137                ********************************/
 138
 139init_pce :-
 140    catch(load_foreign_library(foreign(pl2xpce)),
 141          error(Error, _Context),           % suppress stack trace
 142          (   print_message(error, error(Error, _)),
 143              fail
 144          )),
 145    pce_home(Home),
 146    pce_init(Home),
 147    !,
 148    create_prolog_flag(xpce, true, []),
 149    thread_self(Me),
 150    assert(pce:pce_thread(Me)).
 151init_pce :-
 152    print_message(error, error(pce_error(init_failed), _)),
 153    halt(1).
 154
 155:- initialization(init_pce, now).
 156
 157:- noprofile((send_implementation/3,
 158              get_implementation/4,
 159              send_class/3,
 160              get_class/4,
 161              new/2,
 162              send/2,
 163              get/3)).
 164
 165
 166                /********************************
 167                *          PROLOG LAYER         *
 168                ********************************/
 169
 170
 171%!  free(+Ref) is det.
 172%
 173%   Delete object if it exists.
 174
 175free(Ref) :-
 176    object(Ref),
 177    !,
 178    send(Ref, free).
 179free(_).
 180
 181
 182%!  send(+Object, +Selector, +Arg...) is semidet.
 183%
 184%   Succeeds if sending a message to Object with Selector and the
 185%   given Arguments succeeds. Normally, goal_expansion/2 expands all
 186%   these goals into send(Receiver, Method(Args...)).
 187
 188send(Receiver, M:Selector, A1) :-
 189    functor(Message, Selector, 1),
 190    arg(1, Message, A1),
 191    send(Receiver, M:Message).
 192
 193send(Receiver, M:Selector, A1, A2) :-
 194    functor(Message, Selector, 2),
 195    arg(1, Message, A1),
 196    arg(2, Message, A2),
 197    send(Receiver, M:Message).
 198
 199send(Receiver, M:Selector, A1, A2, A3) :-
 200    functor(Message, Selector, 3),
 201    arg(1, Message, A1),
 202    arg(2, Message, A2),
 203    arg(3, Message, A3),
 204    send(Receiver, M:Message).
 205
 206send(Receiver, M:Selector, A1, A2, A3, A4) :-
 207    functor(Message, Selector, 4),
 208    arg(1, Message, A1),
 209    arg(2, Message, A2),
 210    arg(3, Message, A3),
 211    arg(4, Message, A4),
 212    send(Receiver, M:Message).
 213
 214send(Receiver, M:Selector, A1, A2, A3, A4, A5) :-
 215    functor(Message, Selector, 5),
 216    arg(1, Message, A1),
 217    arg(2, Message, A2),
 218    arg(3, Message, A3),
 219    arg(4, Message, A4),
 220    arg(5, Message, A5),
 221    send(Receiver, M:Message).
 222
 223send(Receiver, M:Selector, A1, A2, A3, A4, A5, A6) :-
 224    functor(Message, Selector, 6),
 225    arg(1, Message, A1),
 226    arg(2, Message, A2),
 227    arg(3, Message, A3),
 228    arg(4, Message, A4),
 229    arg(5, Message, A5),
 230    arg(6, Message, A6),
 231    send(Receiver, M:Message).
 232
 233
 234%!  get(+Object, :Selector, +Arg..., ?Rval) is semidet.
 235%
 236%   See the comments with send/[3-12].
 237
 238get(Receiver, M:Selector, A1, Answer) :-
 239    functor(Message, Selector, 1),
 240    arg(1, Message, A1),
 241    get(Receiver, M:Message, Answer).
 242
 243get(Receiver, M:Selector, A1, A2, Answer) :-
 244    functor(Message, Selector, 2),
 245    arg(1, Message, A1),
 246    arg(2, Message, A2),
 247    get(Receiver, M:Message, Answer).
 248
 249get(Receiver, M:Selector, A1, A2, A3, Answer) :-
 250    functor(Message, Selector, 3),
 251    arg(1, Message, A1),
 252    arg(2, Message, A2),
 253    arg(3, Message, A3),
 254    get(Receiver, M:Message, Answer).
 255
 256get(Receiver, M:Selector, A1, A2, A3, A4, Answer) :-
 257    functor(Message, Selector, 4),
 258    arg(1, Message, A1),
 259    arg(2, Message, A2),
 260    arg(3, Message, A3),
 261    arg(4, Message, A4),
 262    get(Receiver, M:Message, Answer).
 263
 264get(Receiver, M:Selector, A1, A2, A3, A4, A5, Answer) :-
 265    functor(Message, Selector, 5),
 266    arg(1, Message, A1),
 267    arg(2, Message, A2),
 268    arg(3, Message, A3),
 269    arg(4, Message, A4),
 270    arg(5, Message, A5),
 271    get(Receiver, M:Message, Answer).
 272
 273
 274                 /*******************************
 275                 *           NEW SEND           *
 276                 *******************************/
 277
 278:- multifile
 279    send_implementation/3,
 280    get_implementation/4.
 281
 282%!  send_implementation(+Id, +Message, +Object)
 283%
 284%   Method-bodies are compiled into clauses for this predicate. Id
 285%   is a unique identifier for the implementation, Message is a
 286%   compound whose functor is the method name and whose arguments
 287%   are the arguments to the method-call. Object is the receiving
 288%   object.
 289
 290send_implementation(true, _Args, _Obj).
 291send_implementation(fail, _Args, _Obj) :- fail.
 292send_implementation(once(Id), Args, Obj) :-
 293    send_implementation(Id, Args, Obj),
 294    !.
 295send_implementation(spy(Id), Args, Obj) :-
 296    (   current_prolog_flag(debug, true)
 297    ->  trace,
 298        send_implementation(Id, Args, Obj)
 299    ;   send_implementation(Id, Args, Obj)
 300    ).
 301send_implementation(trace(Id), Args, Obj) :-
 302    pce_info(pce_trace(enter, send_implementation(Id, Args, Obj))),
 303    (   send_implementation(Id, Args, Obj)
 304    ->  pce_info(pce_trace(exit, send_implementation(Id, Args, Obj)))
 305    ;   pce_info(pce_trace(fail, send_implementation(Id, Args, Obj)))
 306    ).
 307
 308
 309%!  get_implementation(+Id, +Message, +Object, -Return)
 310%
 311%   As send_implementation/3, but for get-methods.
 312
 313get_implementation(true, _Args, _Obj, _Rval).
 314get_implementation(fail, _Args, _Obj, _Rval) :- fail.
 315get_implementation(once(Id), Args, Obj, Rval) :-
 316    get_implementation(Id, Args, Obj, Rval),
 317    !.
 318get_implementation(spy(Id), Args, Obj, Rval) :-
 319    (   current_prolog_flag(debug, true)
 320    ->  trace,
 321        get_implementation(Id, Args, Obj, Rval)
 322    ;   get_implementation(Id, Args, Obj, Rval)
 323    ).
 324get_implementation(trace(Id), Args, Obj, Rval) :-
 325    pce_info(pce_trace(enter, get_implementation(Id, Args, Obj, Rval))),
 326    (   get_implementation(Id, Args, Obj, Rval)
 327    ->  pce_info(pce_trace(exit, get_implementation(Id, Args, Obj, Rval)))
 328    ;   pce_info(pce_trace(fail, get_implementation(Id, Args, Obj, Rval))),
 329        fail
 330    ).
 331
 332%       SWI-Prolog: make this a normal user (debug-able) predicate.
 333
 334pce_ifhostproperty(prolog(swi), [
 335(:- unlock_predicate(send_implementation/3)),
 336(:- unlock_predicate(get_implementation/4)),
 337(:- '$set_predicate_attribute'(send_implementation(_,_,_),  hide_childs, false)),
 338(:- '$set_predicate_attribute'(get_implementation(_,_,_,_), hide_childs, false))
 339                   ]).
 340
 341
 342                 /*******************************
 343                 *          DECLARATIONS        *
 344                 *******************************/
 345
 346:- multifile
 347    pce_class/6,
 348    pce_lazy_send_method/3,
 349    pce_lazy_get_method/3,
 350    pce_uses_template/2.
 351
 352
 353                 /*******************************
 354                 *            @PROLOG           *
 355                 *******************************/
 356
 357:- initialization
 358   (object(@prolog) -> true ; send(@host, name_reference, prolog)).