File: win_menu.pl

package info (click to toggle)
swi-prolog 6.6.6-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 82,312 kB
  • sloc: ansic: 322,250; perl: 245,822; sh: 6,651; java: 5,254; makefile: 4,423; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (498 lines) | stat: -rw-r--r-- 13,550 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2013, University of Amsterdam
			      VU University 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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(win_menu,
	  [ init_win_menus/0
	  ]).
:- use_module(library(lists)).
:- use_module(library(apply)).
:- use_module(library(error)).
:- set_prolog_flag(generate_debug_info, false).
:- op(200, fy, @).
:- op(990, xfx, :=).

/** <module> Console window menu

This library sets up the menu of  *swipl-win.exe*. It is called from the
system initialisation file =plwin-win.rc=, predicate gui_setup_/0.
*/

:- if(current_prolog_flag(console_menu_version, qt)).
% The traditional swipl-win.exe predefines some menus.  The Qt version
% does not.  Here, we predefine the same menus to make the remainder
% compatiple.
menu('&File',
     [ 'E&xit' = pqConsole:quit_console
     ],
     [
     ]).
menu('&Edit',
     [ '&Copy'  = pqConsole:copy,
       '&Paste' = pqConsole:paste
     ],
     []).
menu('&Settings',
     [ '&Font ...' = pqConsole:select_font,
       '&Colors ...' = pqConsole:select_ANSI_term_colors
     ],
     []).
menu('&Run',
     [ '&Interrupt' = interrupt,
       '&New thread' = interactor
     ],
     []).

menu(File,
     [ '&Consult ...' = action(user:consult(+file(open,
						  'Load file into Prolog'))),
       '&Edit ...'    = action(user:edit(+file(open,
					       'Edit existing file'))),
       '&New ...'     = action(edit_new(+file(save,
					      'Create new Prolog source'))),
       --
     | MRU
     ], [before_item('E&xit')]) :-
	File = '&File',
	findall(Mru=true, mru_info(File, Mru, _, _, _), MRU, MRUTail),
	MRUTail = [ --,
		    '&Reload modified files' = user:make,
		    --,
		    '&Navigator ...' = prolog_ide(open_navigator),
		    --
		  ].

:- else.

menu('&File',
     [ '&Consult ...' = action(user:consult(+file(open,
						  'Load file into Prolog'))),
       '&Edit ...'    = action(user:edit(+file(open,
					       'Edit existing file'))),
       '&New ...'     = action(edit_new(+file(save,
					      'Create new Prolog source'))),
       --,
       '&Reload modified files' = user:make,
       --,
       '&Navigator ...' = prolog_ide(open_navigator),
       --
     ],
     [ before_item('&Exit')
     ]).
:- endif.

menu('&Settings',
     [ --,
       '&User init file ...'  = prolog_edit_preferences(prolog),
       '&GUI preferences ...' = prolog_edit_preferences(xpce)
     ],
     []).
menu('&Debug',
     [ %'&Trace'	     = trace,
       %'&Debug mode'	     = debug,
       %'&No debug mode'     = nodebug,
       '&Edit spy points ...' = user:prolog_ide(open_debug_status),
       '&Edit exceptions ...' = user:prolog_ide(open_exceptions(@on)),
       '&Threads monitor ...' = user:prolog_ide(thread_monitor),
       'Debug &messages ...'  = user:prolog_ide(debug_monitor),
       'Cross &referencer ...'= user:prolog_ide(xref),
       --,
       '&Graphical debugger' = user:guitracer
     ],
     [ before_menu(-)
     ]).
menu('&Help',
     [ '&About ...'				= about,
       '&Help ...'				= help,
       'Browse &PlDoc ...'			= doc_browser,
       --,
       'SWI-Prolog website ...'			= www_open(swipl),
       '  &Manual ...'				= www_open(swipl_man),
       '  &FAQ ...'				= www_open(swipl_faq),
       '  &Quick Start ...'			= www_open(swipl_quick),
       '  Mailing &List ...'			= www_open(swipl_mail),
       '  &Download ...'			= www_open(swipl_download),
       '  &Extension packs ...'			= www_open(swipl_pack),
       --,
       '&XPCE (GUI) Manual ...'			= manpce,
       --,
       'Submit &Bug report ...'			= www_open(swipl_bugs)
     ],
     [ before_menu(-)
     ]).


init_win_menus :-
	(   menu(Menu, Items, Options),
	    (	memberchk(before_item(Before), Options)
	    ->	true
	    ;	Before = (-)
	    ),
	    (	memberchk(before_menu(BM), Options)
	    ->	true
	    ;	BM = (-)
	    ),
	    win_insert_menu(Menu, BM),
	    (   '$member'(Item, Items),
                (   Item = (Label = Action)
		->  true
		;   Item == --
		->  Label = --
		),
		win_insert_menu_item(Menu, Label, Before, Action),
		fail
	    ;	true
	    ),
	    fail
	;   current_prolog_flag(associated_file, File),
	    add_to_mru(load, File)
	;   insert_associated_file
        ),
        refresh_mru.

associated_file(File) :-
	current_prolog_flag(associated_file, File), !.
associated_file(File) :-
	'$option'(script_file, OsFiles),
	OsFiles = [OsFile], !,
	prolog_to_os_filename(File, OsFile).

insert_associated_file :-
	associated_file(File), !,
	file_base_name(File, Base),
	atom_concat('Edit &', Base, Label),
	win_insert_menu_item('&File', Label, '&New ...', edit(file(File))).
insert_associated_file.


:- if(current_predicate(win_has_menu/0)).
:- initialization
   (   win_has_menu
   ->  init_win_menus
   ;   true
   ).
:- endif.

		 /*******************************
		 *	      ACTIONS		*
		 *******************************/

edit_new(File) :-
	call(edit(file(File))).		% avoid autoloading

www_open(Id) :-
	Spec =.. [Id, '.'],
	call(expand_url_path(Spec, URL)),
	print_message(informational, opening_url(URL)),
	call(www_open_url(URL)),	% avoid autoloading
	print_message(informational, opened_url(URL)).

html_open(Spec) :-
	absolute_file_name(Spec, [access(read)], Path),
	call(win_shell(open, Path)).

:- if(current_predicate(win_message_box/2)).

about :-
	message_to_string(about, AboutSWI),
	(   current_prolog_flag(console_menu_version, qt)
	->  message_to_string(about_qt, AboutQt),
	    format(atom(About), '<p>~w\n<p>~w', [AboutSWI, AboutQt])
	;   About = AboutSWI
	),
	atomic_list_concat(Lines, '\n', About),
	atomic_list_concat(Lines, '<br>', AboutHTML),
	win_message_box(
	    AboutHTML,
	    [ title('About swipl-win'),
	      image(':/swipl.png'),
	      min_width(700)
	    ]).

:- else.

about :-
	print_message(informational, about).

:- endif.

load(Path) :-
	(   \+ current_prolog_flag(associated_file, _)
	->  file_directory_name(Path, Dir),
	    working_directory(_, Dir),
	    set_prolog_flag(associated_file, Path)
	;   true
	),
	user:load_files(Path).


		 /*******************************
		 *	 HANDLE CALLBACK	*
		 *******************************/

action(Action) :-
	strip_module(Action, Module, Plain),
	Plain =.. [Name|Args],
	gather_args(Args, Values),
	Goal =.. [Name|Values],
	call(Module:Goal).

gather_args([], []).
gather_args([+H0|T0], [H|T]) :- !,
	gather_arg(H0, H),
	gather_args(T0, T).
gather_args([H|T0], [H|T]) :-
	gather_args(T0, T).

:- if(current_prolog_flag(console_menu_version, qt)).

gather_arg(file(open, Title), File) :- !,
	source_types_desc(Desc),
        pqConsole:getOpenFileName(Title, _, Desc, File),
        add_to_mru(edit, File).

gather_arg(file(save, Title), File) :-
	source_types_desc(Desc),
        pqConsole:getSaveFileName(Title, _, Desc, File),
        add_to_mru(edit, File).

source_types_desc(Desc) :-
	findall(Pattern, prolog_file_pattern(Pattern), Patterns),
	atomic_list_concat(Patterns, ' ', Atom),
	format(atom(Desc), 'Prolog Source (~w)', [Atom]).

:- else.

gather_arg(file(Mode, Title), File) :-
	findall(tuple('Prolog Source', Pattern),
		prolog_file_pattern(Pattern),
		Tuples),
	'$append'(Tuples, [tuple('All files', '*.*')], AllTuples),
	Filter =.. [chain|AllTuples],
	current_prolog_flag(hwnd, HWND),
	working_directory(CWD, CWD),
	call(get(@display, win_file_name,	% avoid autoloading
		 Mode, Filter, Title,
		 directory := CWD,
		 owner := HWND,
		 File)).

:- endif.

prolog_file_pattern(Pattern) :-
	user:prolog_file_type(Ext, prolog),
	atom_concat('*.', Ext, Pattern).


:- if(current_prolog_flag(windows, true)).

		 /*******************************
		 *	    APPLICATION		*
		 *******************************/

%%	init_win_app
%
%	If Prolog is started using --win_app, try to change directory
%	to <My Documents>\Prolog.

init_win_app :-
	current_prolog_flag(associated_file, _), !.
init_win_app :-
	'$option'(win_app, true), !,
	catch(my_prolog, E, print_message(warning, E)).
init_win_app.

my_prolog :-
	win_folder(personal, MyDocs),
	atom_concat(MyDocs, '/Prolog', PrologDir),
	(   ensure_dir(PrologDir)
	->  working_directory(_, PrologDir)
	;   working_directory(_, MyDocs)
	).


ensure_dir(Dir) :-
	exists_directory(Dir), !.
ensure_dir(Dir) :-
	catch(make_directory(Dir), E, (print_message(warning, E), fail)).


:- initialization
   init_win_app.

:- endif. /*windows*/


		 /*******************************
		 *	       MacOS		*
		 *******************************/

:- if(current_prolog_flag(console_menu_version, qt)).

:- multifile
	prolog:file_open_event/1.

:- create_prolog_flag(app_open_first, load, []).
:- create_prolog_flag(app_open,       edit, []).

%%	prolog:file_open_event(+Name)
%
%	Called when opening a file  from   the  MacOS finder. The action
%	depends on whether this is the first file or not, and defined by
%	one of these flags:
%
%	  - =app_open_first= defines the action for the first open event
%	  - =app_open= defines the action for subsequent open event
%
%	On the _first_ open event, the  working directory of the process
%	is changed to the directory holding the   file. Action is one of
%	the following:
%
%	  * load
%	  Load the file into Prolog
%	  * edit
%	  Open the file in the editor
%	  * new_instance
%	  Open the file in a new instance of Prolog and load it there.

prolog:file_open_event(Path) :-
	(   current_prolog_flag(associated_file, _)
	->  current_prolog_flag(app_open, Action)
	;   current_prolog_flag(app_open_first, Action),
	    file_directory_name(Path, Dir),
	    working_directory(_, Dir),
	    set_prolog_flag(associated_file, Path),
	    insert_associated_file
	),
	must_be(oneof([edit,load,new_instance]), Action),
	file_open_event(Action, Path).

file_open_event(edit, Path) :-
	edit(Path).
file_open_event(load, Path) :-
	add_to_mru(load, Path),
	user:load_files(Path).
:- if(current_prolog_flag(apple, true)).
file_open_event(new_instance, Path) :-
	current_app(Me),
	print_message(informational, new_instance(Path)),
	process_create(path(open), [ '-n', '-a', Me, Path ], []).
:- else.
file_open_event(new_instance, Path) :-
	current_prolog_flag(executable, Exe),
	process_create(Exe, [Path], [process(_Pid)]).
:- endif.


:- if(current_prolog_flag(apple, true)).
current_app(App) :-
	current_prolog_flag(executable, Exe),
	file_directory_name(Exe, MacOSDir),
	atom_concat(App, '/Contents/MacOS', MacOSDir).

%%	go_home_on_plain_app_start is det.
%
%	On Apple, we start in the users   home dir if the application is
%	started by opening the app directly.

go_home_on_plain_app_start :-
	current_prolog_flag(os_argv, [_Exe]),
	current_app(App),
	file_directory_name(App, Above),
	working_directory(PWD, PWD),
	same_file(PWD, Above),
	expand_file_name(~, [Home]), !,
	working_directory(_, Home).
go_home_on_plain_app_start.

:- initialization
	go_home_on_plain_app_start.

:- endif.
:- endif.

:- if(current_predicate(win_current_preference/3)).

mru_info('&File', 'Edit &Recent', 'MRU2',    path, edit).
mru_info('&File', 'Load &Recent', 'MRULoad', path, load).

add_to_mru(Action, File) :-
	mru_info(_Top, _Menu, PrefGroup, PrefKey, Action),
	(   win_current_preference(PrefGroup, PrefKey, CPs), nonvar(CPs)
	->  (   select(File, CPs, Rest)
	    ->  Updated = [File|Rest]
	    ;   length(CPs, Len),
		Len > 10
	    ->	append(CPs1, [_], CPs),
		Updated = [File|CPs1]
	    ;	Updated = [File|CPs]
	    )
	;   Updated = [File]
	),
	win_set_preference(PrefGroup, PrefKey, Updated),
	refresh_mru.

refresh_mru :-
	(   mru_info(FileMenu, Menu, PrefGroup, PrefKey, Action),
	    win_current_preference(PrefGroup, PrefKey, CPs),
	    maplist(action_path_menu(Action), CPs, Labels, Actions),
	    win_insert_menu_item(FileMenu, Menu/Labels, -, Actions),
	    fail
	;   true
	).

action_path_menu(ActionItem, Path, Label, win_menu:Action) :-
	file_base_name(Path, Label),
	Action =.. [ActionItem, Path].

:- else.

add_to_mru(_, _).
refresh_mru.

:- endif.


		 /*******************************
		 *	      MESSAGES		*
		 *******************************/

:- multifile
	prolog:message/3.

prolog:message(opening_url(Url)) -->
	[ 'Opening ~w ... '-[Url], flush ].
prolog:message(opened_url(_Url)) -->
	[ at_same_line, 'ok' ].
prolog:message(new_instance(Path)) -->
	[ 'Opening new Prolog instance for ~p'-[Path] ].
:- if(current_prolog_flag(console_menu_version, qt)).
prolog:message(about_qt) -->
	[ 'Qt-based console by Carlo Capelli' ].
:- endif.