For Programmers: Free Programming Magazines  


Home > Archive > Prolog > October 2004 > SWI-prolog, XPCE help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SWI-prolog, XPCE help
Henrik ?stman

2004-10-15, 8:56 pm

Hi!

Could anyone give me a quick example of how to use XPCE to build a
GUI-application??
For example make a new window that contains two rectangles in diffrent
colors, i guess you could call them "labels" in Visual Basic, but
basic rectangles works great as an demostration.

Many Thanks!!

// Henrik
Jan Wielemaker

2004-10-16, 8:55 am

In article <909d9f43.0410151522.e2f69dd@posting.google.com>,
Henrik ?stman wrote:
> Hi!
>
> Could anyone give me a quick example of how to use XPCE to build a
> GUI-application??
> For example make a new window that contains two rectangles in diffrent
> colors, i guess you could call them "labels" in Visual Basic, but
> basic rectangles works great as an demostration.


Find the XPCE Userguide. As for this problem:

test :-
new(P, picture),
send(P, display, new(B1, box(100,100)), point(10,10)),
send(P, display, new(B2, box(100,100)), point(110,110)),
send(B1, colour, green),
send(B2, fill_pattern, colour(blue)),
send(P, open).

Cheers --- Jan
Henrik ?stman

2004-10-17, 8:56 pm

Thanks Jan, that ment alot to me!
I'll try it out.

// Henrik

Jan Wielemaker <jan@ct.xs4all.nl> wrote in message news:<slrncn1m75.dlh.jan@ct.xs4all.nl>...
> In article <909d9f43.0410151522.e2f69dd@posting.google.com>,
> Henrik ?stman wrote:
>
> Find the XPCE Userguide. As for this problem:
>
> test :-
> new(P, picture),
> send(P, display, new(B1, box(100,100)), point(10,10)),
> send(P, display, new(B2, box(100,100)), point(110,110)),
> send(B1, colour, green),
> send(B2, fill_pattern, colour(blue)),
> send(P, open).
>
> Cheers --- Jan

Henrik ?stman

2004-10-18, 8:56 pm

I used and modified your code for my needs and found the XPCE
userguide.
But i can't figure out the last part of my puzzle. How do i call
methods(predicates)? Or can this problem be solved with only one
predicate?? Here is the program so far:

:- use_module(library(pce)).
jv_kontroll:-
new(P, dialog('JV-Kontroll')),
send(P, display, new(Box1, box(75,64)), point(110,48)),
send(P, display, new(Box2, box(75,64)), point(110,120)),
send(Box1, fill_pattern, colour(red)),
send(Box2, fill_pattern, colour(grey)),
send(P, display, button(switch, message(P, change)),
point(110,220)),
send(P, open, point(0, 0)).

change:-
send(D, message(Box1, fill_pattern, colour(grey))),
send(D, message(Box2, fill_pattern, colour(white))).

Yes, it's very incompleate..
It should work like this, a windows opens that contains two
rectangles.
The first one is red, the second one is grey. There is a button that
shifts colors on the rectangles when pressed. Pressing the button one
time changes the color of the first rectangle to grey and the second
one to white. Pressing the button again changes the colors
back(Box1=red, Box2=grey).
I was thinking that every time the button was pressed it called the
funktion "change", that changes the colors. But i don't have enought
knowleage to solve this, can anybody give me some hints??

Thanx!

// Henrik

trycoon2@hotmail.com (Henrik ?stman) wrote in message news:<909d9f43.0410171352.38eb427a@posting.google.com>...[color=darkred]
> Thanks Jan, that ment alot to me!
> I'll try it out.
>
> // Henrik
>
> Jan Wielemaker <jan@ct.xs4all.nl> wrote in message news:<slrncn1m75.dlh.jan@ct.xs4all.nl>...
Jan Wielemaker

2004-10-19, 3:58 pm

In article <909d9f43.0410181634.67a5731c@posting.google.com>,
Henrik ?stman wrote:
> I used and modified your code for my needs and found the XPCE
> userguide.
> But i can't figure out the last part of my puzzle. How do i call
> methods(predicates)? Or can this problem be solved with only one
> predicate?? Here is the program so far:
>
>:- use_module(library(pce)).
> jv_kontroll:-
> new(P, dialog('JV-Kontroll')),
> send(P, display, new(Box1, box(75,64)), point(110,48)),
> send(P, display, new(Box2, box(75,64)), point(110,120)),
> send(Box1, fill_pattern, colour(red)),
> send(Box2, fill_pattern, colour(grey)),
> send(P, display, button(switch, message(P, change)),
> point(110,220)),
> send(P, open, point(0, 0)).
>
> change:-
> send(D, message(Box1, fill_pattern, colour(grey))),
> send(D, message(Box2, fill_pattern, colour(white))).


There are two ways. One is to define a class and the other is
to send messages to @prolog, which are mapped to calls.

I'll give you the class based version as you'll notice that the other
approach is simpler, but quickly results in a big mess. Also notice
the name and member usage to keep track of the identity of the boxes.
They could also have been passed as extra arguments in the message
associated to the button.

:- use_module(library(pce)).


jv_kontroll :-
send(new(jv_kontroll), open).

:- pce_begin_class(jv_kontroll, dialog).

initialise(P) :->
send_super(P, initialise, 'JV-Kontroll'),
send(P, display, new(Box1, box(75,64)), point(110,48)),
send(P, display, new(Box2, box(75,64)), point(110,120)),
send(Box1, fill_pattern, colour(red)),
send(Box2, fill_pattern, colour(grey)),
send(Box1, name, box1),
send(Box2, name, box2),
send(P, display, button(switch, message(P, change)),
point(110,220)).

change(P) :->
"Called from the button"::
get(P, member, box1, Box1),
get(P, member, box2, Box2),
send(Box1, fill_pattern, colour(grey)),
send(Box2, fill_pattern, colour(white)).

:- pce_end_class.


> one to white. Pressing the button again changes the colors
> back(Box1=red, Box2=grey).


You'll have to remember some state. I'll leave this as a
challenge :-)

Cheers --- Jan
Henrik ?stman

2004-10-20, 8:56 am

Jan Wielemaker <jan@ct.xs4all.nl> wrote in message news:<slrncnabim.jkn.jan@ct.xs4all.nl>...
> In article <909d9f43.0410181634.67a5731c@posting.google.com>,
> Henrik ?stman wrote:
>
> There are two ways. One is to define a class and the other is
> to send messages to @prolog, which are mapped to calls.
>
> I'll give you the class based version as you'll notice that the other
> approach is simpler, but quickly results in a big mess. Also notice
> the name and member usage to keep track of the identity of the boxes.
> They could also have been passed as extra arguments in the message
> associated to the button.
>
> :- use_module(library(pce)).
>
>
> jv_kontroll :-
> send(new(jv_kontroll), open).
>
> :- pce_begin_class(jv_kontroll, dialog).
>
> initialise(P) :->
> send_super(P, initialise, 'JV-Kontroll'),
> send(P, display, new(Box1, box(75,64)), point(110,48)),
> send(P, display, new(Box2, box(75,64)), point(110,120)),
> send(Box1, fill_pattern, colour(red)),
> send(Box2, fill_pattern, colour(grey)),
> send(Box1, name, box1),
> send(Box2, name, box2),
> send(P, display, button(switch, message(P, change)),
> point(110,220)).
>
> change(P) :->
> "Called from the button"::
> get(P, member, box1, Box1),
> get(P, member, box2, Box2),
> send(Box1, fill_pattern, colour(grey)),
> send(Box2, fill_pattern, colour(white)).
>
> :- pce_end_class.
>
>
>
> You'll have to remember some state. I'll leave this as a
> challenge :-)
>
> Cheers --- Jan


Hi, and THANKS!
By the time i got your last message i had come to the same conclusion,
either you send a message to @prolog or create a class based version.
I tried the class based version first, but could'nt realy understand
how to setup the constructor and how to call a method(like the
switch-method) even thought there is an example in XPCE Userguide.
Then i tried the "@prolog-version" but it worked even worse. =)
I'm having a hard time to understand the laungauge, i'm a beginner
programmer and a prolog newbie. So i'm very greatfull that you take
time of to help me getting started! I have still not finished the
program, i know what i would like to do but the prolog/XPCE syntax is
confusing me. Where is my "Begin" and "End;"?? =))

So now i'm stuck with a If-case, since i guess that the normal prolog
atom-based
":-" does'nt work on my objects i found the "If"-command(class?).

My current program looks like this(almost the same as you left me
with):

:- use_module(library(pce)).

jv_kontroll :-
send(new(jv_kontroll), open, point(0,0)).

:- pce_begin_class(jv_kontroll, dialog).
initialise(P) :->
send_super(P, initialise, 'JV-kontroll'),
send(P, display, new(Box1, box(75, 64)), point(110,
48)),
send(P, display, new(Box2, box(75, 64)), point(110,
120)),
send(Box1, colour(lightgrey)),
send(Box1, fill_pattern, colour(grey)),
%send(Box1, radius, 10),
send(Box2, colour(red)),
send(Box2, fill_pattern, colour(red)),
%send(Box2, radius, 10),
send(Box1, name, box1),
send(Box2, name, box2),
send(P, display, button(switch), point(110,220)).

switch(P) :->
"Kallas när man trycker på knappen"::
get(P, member, box1, Box1),
get(P, member, box2, Box2),

% pseudo-code, for how i would like it to work..
%if(get(P, Box1, colour(white)))
% {
% send(Box1, fill_pattern, @nil),
% send(Box2, fill_pattern, colour(red).
% }
%else
% {
% send(Box1, fill_pattern, colour(white)),
% send(Box2, fill_pattern, @nil).
% }
% if the colour "@nil" does'nt work, use colour(grey)

if(get(Box1, colour, 'white')), % If box if
"white".
send(Box1, fill_pattern, @nil), %Then, make
that box invissible.
send(Box1, fill_pattern,
colour(white)).%Else, make that box "white".
:- pce_end_class.

What's wrong with the IF-statement? And how do i process more than
only one line of code in the "Then" and "Else"-section? Do i have to
call yet two other methods just to execute two lines of code?(does'nt
seem right).

Many Thanks, again.

// Henrik
Jan Wielemaker

2004-10-20, 4:06 pm

In article <909d9f43.0410200402.79d80bd5@posting.google.com>,
Henrik ?stman wrote:
> My current program looks like this(almost the same as you left me
> with):
>
>:- use_module(library(pce)).
>
> jv_kontroll :-
> send(new(jv_kontroll), open, point(0,0)).
>
> :- pce_begin_class(jv_kontroll, dialog).
> initialise(P) :->
> send_super(P, initialise, 'JV-kontroll'),
> send(P, display, new(Box1, box(75, 64)), point(110,
> 48)),
> send(P, display, new(Box2, box(75, 64)), point(110,
> 120)),
> send(Box1, colour(lightgrey)),
> send(Box1, fill_pattern, colour(grey)),
> %send(Box1, radius, 10),
> send(Box2, colour(red)),
> send(Box2, fill_pattern, colour(red)),
> %send(Box2, radius, 10),
> send(Box1, name, box1),
> send(Box2, name, box2),
> send(P, display, button(switch), point(110,220)).
>
> switch(P) :->
> "Kallas när man trycker på knappen"::
> get(P, member, box1, Box1),
> get(P, member, box2, Box2),
>
> % pseudo-code, for how i would like it to work..
> %if(get(P, Box1, colour(white)))
> % {
> % send(Box1, fill_pattern, @nil),
> % send(Box2, fill_pattern, colour(red).
> % }
> %else
> % {
> % send(Box1, fill_pattern, colour(white)),
> % send(Box2, fill_pattern, @nil).
> % }
> % if the colour "@nil" does'nt work, use colour(grey)
>
> if(get(Box1, colour, 'white')), % If box if
> "white".
> send(Box1, fill_pattern, @nil), %Then, make
> that box invissible.
> send(Box1, fill_pattern,
> colour(white)).%Else, make that box "white".
> :- pce_end_class.
>
> What's wrong with the IF-statement? And how do i process more than
> only one line of code in the "Then" and "Else"-section? Do i have to
> call yet two other methods just to execute two lines of code?(does'nt
> seem right).


Various things are wrong with it. First of all the use of if(...). I
do not know from where you get it, but I suspect you've looked at the
XPCE manual decribing the class 'if'. The body of a method however is
executed in _Prolog_ and therefore you must use Prolog code to express
it. Prolog if-then-else is expressed as (If -> Then ; Else) and you
find the details in the Prolog manual or (better) a Prolog textbook.

So, you get (for example)

( get(Box, colour, colour(green))
-> send(Box, colour, colour(red))
; send(Box, colour, colour(green))
)

Now you must be a bit careful with this as colour(green) actually creates
a colour _object_ and there are some potential problems with identity
and equality around the corner. For this particular example however,
it works. In general however you'd have to deal with this. Here is
an alternative:

( get(Box, colour, Old),
get(Old, name, green)
-> send(Box, colour, red)
; send(Box, colour, green)
).

Actually the two action (send(..)) lines are the same as above and
either form can be used in both examples. If 'green' is sent to a
method that only accepts objects of type colour the system asks
class colour to convert 'green' into an object described by it.

Of course you can also query the RBG value, etc. In many applications
it is better to keep the state represented seperately and in some more
meaningful way than colours.

Finally, IF gets back to XPCE's notion of executable code that can
be passed and stored in objects. So, you can also write the code
below, which doesn't touch Prolog at all if you click the button.

send(P, display, new(B, box(100,100))),
send(B, colour, green),
...,
send(P, display,
button(switch,
if(B?colour?name == green,
message(B, colour, red),
message(B, colour, green)))),
...

--- Jan



Henrik ?stman

2004-10-21, 3:58 am

Jan Wielemaker <jan@ct.xs4all.nl> wrote in message news:<slrncncp3u.ov2.jan@ct.xs4all.nl>...
> In article <909d9f43.0410200402.79d80bd5@posting.google.com>,
> Henrik ?stman wrote:
>
> Various things are wrong with it. First of all the use of if(...). I
> do not know from where you get it, but I suspect you've looked at the
> XPCE manual decribing the class 'if'. The body of a method however is
> executed in _Prolog_ and therefore you must use Prolog code to express
> it. Prolog if-then-else is expressed as (If -> Then ; Else) and you
> find the details in the Prolog manual or (better) a Prolog textbook.
>
> So, you get (for example)
>
> ( get(Box, colour, colour(green))
> -> send(Box, colour, colour(red))
> ; send(Box, colour, colour(green))
> )
>
> Now you must be a bit careful with this as colour(green) actually creates
> a colour _object_ and there are some potential problems with identity
> and equality around the corner. For this particular example however,
> it works. In general however you'd have to deal with this. Here is
> an alternative:
>
> ( get(Box, colour, Old),
> get(Old, name, green)
> -> send(Box, colour, red)
> ; send(Box, colour, green)
> ).
>
> Actually the two action (send(..)) lines are the same as above and
> either form can be used in both examples. If 'green' is sent to a
> method that only accepts objects of type colour the system asks
> class colour to convert 'green' into an object described by it.
>
> Of course you can also query the RBG value, etc. In many applications
> it is better to keep the state represented seperately and in some more
> meaningful way than colours.
>
> Finally, IF gets back to XPCE's notion of executable code that can
> be passed and stored in objects. So, you can also write the code
> below, which doesn't touch Prolog at all if you click the button.
>
> send(P, display, new(B, box(100,100))),
> send(B, colour, green),
> ...,
> send(P, display,
> button(switch,
> if(B?colour?name == green,
> message(B, colour, red),
> message(B, colour, green)))),
> ...
>
> --- Jan


Thanks again Jan!

Yes, you are right, i found the IF-class in the XPCE-manual and
thought i could use it for the program. I think i have to study some
more Prolog because i seam to lack some fundamental knowlage.. =)
Have you ever considered writing a book about Prolog or XPCE?? I have
learned more by having this diskution with you than reading all the
e-books that i have!
I used your second if-example since it looked more prologish and
"right" than the last one. Thanks!

// Henrik
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com