/*  -*-Prolog-*-

    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 library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser 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
*/

:- module(link_xpce, []).
:- style_check(+dollar).		% lock as system module

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This file is loaded  on  startup.  Use   it  for  local  (not  personal)
configuration purposes.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- initialization op(200, fy,  user:(@)).
:- initialization op(250, yfx, user:(?)).
:- initialization op(990, xfx, user:(:=)).

:- multifile
	user:file_search_path/2.

:- dynamic
	pcehomestore_/1.
:- volatile
	pcehomestore_/1.

pcehome_(Home) :-
	pcehomestore_(Home), !.
pcehome_(Home) :-
	(   getenv('XPCEHOME', RawHome)
	;   current_prolog_flag(home, PlHome),
	    (   current_prolog_flag(xpce_version, Version),
		atom_concat('/xpce-', Version, Suffix)
	    ;   Suffix = '/xpce'
	    ),
	    atom_concat(PlHome, Suffix, RawHome)
	),
	exists_directory(RawHome), !,
	absolute_file_name(RawHome, Home),
	asserta(pcehomestore_(Home)).

user:file_search_path(pce, PceHome) :-
	pcehome_(PceHome).
user:file_search_path(library, pce('prolog/lib')).
user:file_search_path(foreign, pce(ArchLib)) :-
	current_prolog_flag(arch, Arch),
	atom_concat('lib/', Arch, ArchLib).
user:file_search_path(foreign, pce(lib)).

% We added a directory to the autoload directories: force reloading the
% index
:- reload_library_index.

gui_setup_ :-
	current_prolog_flag(gui, true), !.
gui_setup_ :-
	(   getenv('DISPLAY', _)
	;   current_prolog_flag(windows, true)
	), !,
	create_prolog_flag(gui, true, []),
	menu_setup_,
	editor_setup,
	use_module(user:library(swi_hooks)).	% help, etc.

menu_setup_ :-					% plwin.exe menus
	current_prolog_flag(console_menu, true),
	use_module(user:library(win_menu)).
menu_setup_.

editor_setup :-
	current_prolog_flag(editor, default), !,
	set_prolog_flag(editor, pce_emacs).
editor_setup.

pce_setup_ :-
	current_prolog_flag(xpce, true), !.
pce_setup_ :-
	current_prolog_flag(argv, [Argv0|Args]),
	\+ memberchk('--nopce', Args),	% explicitely no XPCE
	pcehome_(PceHome),
	exists_directory(PceHome),
	gui_setup_,
	(   memberchk('--pce', Args)
	;   prolog_to_os_filename(Path, Argv0),
	    file_base_name(Path, Base),
	    '$apropos_match'(pce, Base)
	), !,
	load_files(user:library(pce), [silent(true)]),
	flag('$banner_goal', _, pce_welcome).
pce_setup_.

:- initialization pce_setup_.
