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_image,
  36          [ pce_image_directory/1
  37          ]).
  38:- use_module(library(pce)).
  39:- require([ absolute_file_name/3
  40           , atomic_list_concat/2
  41           , is_absolute_file_name/1
  42           ]).
  43
  44/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  45Prepend the given  directory  to  the   image  search-path.  Useful  for
  46applications defining a private image directory.
  47
  48Typically one would put all images required   by an application in *.xpm
  49files in a directory called bitmaps. The   load  file of the application
  50makes a call
  51
  52        :- pce_image_directory(bitmaps).
  53
  54after which the images from the  directory   may  be accessed using -for
  55example-:
  56
  57        send(Box, fill_pattern, image('my_image.bm')).
  58
  59See also the ImageViewer demo  tool  to   get  an  overview of available
  60images in a directory.
  61
  62Images and program resources
  63----------------------------
  64
  65pce_image_directory/1   prepends   the   given     directory    to   the
  66file_search_path/2 declarations using the alias   `image'. Especially if
  67the application should (eventually)  be  turned   into  a  runtime,  the
  68following skeleton for using images as program resources is adviced:
  69
  70resource(cute,  image, image('cute.xpm')).
  71
  72        ...
  73        new(I, image(resource(cute)),
  74        ...
  75- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  76
  77pce_image_directory(Dir) :-
  78    (   atom(Dir)
  79    ->  (   \+ is_absolute_file_name(Dir),
  80            prolog_load_context(directory, Cwd)
  81        ->  atomic_list_concat([Cwd, /, Dir], DirPath)
  82        ;   DirPath = Dir
  83        ),
  84        asserta(user:file_search_path(image, DirPath))
  85    ;   asserta(user:file_search_path(image, Dir)),
  86        absolute_file_name(Dir, DirPath,
  87                           [ file_type(directory),
  88                             access(read)
  89                           ])
  90    ),
  91    (   compiling,
  92        get(@display, open, @off)   % don't force the display
  93                                    % when compiling
  94    ->  true
  95    ;   get(class(image), class_variable, path, PathVar),
  96        get(PathVar, value, Path),
  97        send(PathVar, value, string('%s:%s', DirPath, Path))
  98    ).