# Changing background colors and mutability of XPCE dialogs

**URL:** <https://swi-prolog.discourse.group/t/changing-background-colors-and-mutability-of-xpce-dialogs/5406>\
**Category:** Help!\
**Tags:** how-to, xpce\
**Created:** [May 19, 2022, 5:13am UTC](https://swi-prolog.discourse.group/t/changing-background-colors-and-mutability-of-xpce-dialogs/5406 "2022-05-19T05:13:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![acmiyaguchi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/acmiyaguchi/32/3842_2.png) [@acmiyaguchi](https://swi-prolog.discourse.group/u/acmiyaguchi)\
**Post date:** [May 19, 2022, 5:13am UTC](https://swi-prolog.discourse.group/t/changing-background-colors-and-mutability-of-xpce-dialogs/5406/1 "2022-05-19T05:13:03Z")

</div>

I’ve been trying to scratch a recent itch involving prolog and gui applications, so I’ve been trying my hand at writing some basic applications with XPCE. While `manpce` and the [user guide](https://www.swi-prolog.org/download/xpce/doc/userguide/userguide.pdf) have been useful for building the most basic of applications, I am struggling to manipulate simple elements.

I’m building a toy flight booker app ala [7GUIs](https://eugenkiss.github.io/7guis/tasks). I’m currently struggling on changing the background color of an XPCE `text_item`. If possible, I’d also like to make a text element immutable (or view only) like one might make an immutable textbox in HTML.

My code so far is simple:

```prolog
% main.pl
% usage: swipl-win.exe main.pl
:- use_module(library(pce)).
:- initialization main.

main :-
    new(D, dialog('Flight Booker')),
    menu(Menu),
    date_box(StartBox, '2022-01-01'),
    date_box(ReturnBox, '2022-02-01'),
    send(D, append, Menu),
    send(D, append, StartBox),
    send(D, append, ReturnBox),
    send(Menu, message, message(@prolog, menu_selected, Menu)),
    get(D, confirm, _).

menu_selected(Menu) :-
    get(Menu, selection, Item),
    % print Item
    write(Item), nl.

menu(Menu):-
    new(Menu, menu(flighttype, cycle)),
    send(Menu, show_label, @off),
    send_list(Menu, append, ['one-way flight', 'return flight']).

date_box(Box, InitDate) :-
    new(Box, text_item(date, InitDate)),
    send(Box, type, string),
    send(Box, length, 12),
    send(Box, show_label, @off).

```

![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/5/54b2046d1e0db688fc9a5312af740d7d40f015a2.png)

I can change the color of the text in the `text_item` box by sending a message the `color` properties in `date_box`, but I can’t change the color of the background (which I would like to turn red). In general, it has been difficult to determine which properties I can notify by reading the manual. The closest I’ve gotten is to look through Appendix H and glimpsing some hints from the code samples there. I have used the manpce dialog editor to build elements and view the generated source in (the built-in) emacs, but unfortunately it doesn’t look like there is anything related to the background.

I’m hoping there’s a way to modify visual properties from one of the dialog’s super classes:

![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/f/f9f372f2798bca6d4fdcaf5f93cb31603ee63671.png)

It would be great too if there were ways to prevent input from temporarily being accepted to one of the dialogs. I’m just not sure how I could achieve this goal.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 19, 2022, 7:10am UTC](https://swi-prolog.discourse.group/t/changing-background-colors-and-mutability-of-xpce-dialogs/5406/2 "2022-05-19T07:10:27Z")

</div>

To make it non-editable you can use `send(TextItem, editable, @off).`. A quick look through the source indicates that the background is than painted using an internal `elevation` instance that has no externally visible reference. See `init_entry_resources()` in `x_menu.c` You’d have to go through the same setup as used for the elevation of buttons that do provide user access. Means changing the C code …

Xpce is old ☹

---

<div class="post-metadata">

**Author:** ![acmiyaguchi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/acmiyaguchi/32/3842_2.png) [@acmiyaguchi](https://swi-prolog.discourse.group/u/acmiyaguchi)\
**Post date:** [May 20, 2022, 6:07am UTC](https://swi-prolog.discourse.group/t/changing-background-colors-and-mutability-of-xpce-dialogs/5406/3 "2022-05-20T06:07:36Z")

</div>

Nice to see the `editable` is easy to handle. I started digging into the code and found that `h/graphics.h` is super useful for figuring out what messages are available to send to different resources. I noticed that `background` is actually available to dialog items like buttons or text items:

> <https://github.com/SWI-Prolog/packages-xpce/blob/75f797b68d09dca0dfc5a1c3405298e69f25db75/src/h/graphics.h#L107>

I tried setting `background` on buttons and text items with no avail – it seems to retain the same gray background regardless of the value:

```prolog
:- use_module(library(pce)).
:- initialization main.
main :-
    new(D, dialog(test)),
    new(B, button(test)),
    send(B, background, colour(green)),
    send(D, append, B),
    get(D, confirm, _).

```

I noticed that dialog groups have a public elevation, so I tried sending a new elevation with a background color over:

```prolog
main :-
    new(D, dialog(test)),
    new(G, dialog_group(test)),
    send(G, elevation, elevation(background:=colour(green))),
    send(D, append, G),
    get(D, confirm, _).

```

But this also looks like a dead-end. I’m guessing that the background isn’t hooked into the colour initialization in xmenu/msmenu implementations. It’s not clear to me how the the colors are actually being initialized here:

> <https://github.com/SWI-Prolog/packages-xpce/blob/895aafaa9fbe562d7586f4a4185042ffd2a9cdbf/src/msw/msmenu.c#L294-L303>

Am I reading this correctly? Is that the native scrollbar color? It sure does seem a tad strange.

Would it be feasible it is to grab values from the available properties/attributes/messages and get them into the dialog drawing routines, with a little bit of massaging here?

---

<div class="post-metadata">

**Author:** ![acmiyaguchi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/acmiyaguchi/32/3842_2.png) [@acmiyaguchi](https://swi-prolog.discourse.group/u/acmiyaguchi)\
**Post date:** [May 20, 2022, 6:29am UTC](https://swi-prolog.discourse.group/t/changing-background-colors-and-mutability-of-xpce-dialogs/5406/4 "2022-05-20T06:29:51Z")

</div>

> [@jan](#):
>
> You’d have to go through the same setup as used for the elevation of buttons that do provide user access. Means changing the C code …

Aha, I see where I’d have to make change, probably somewhere around here.

> <https://github.com/SWI-Prolog/packages-xpce/blob/04d27192393d6c83ba4327657cbba8b1c54030ae/src/men/textitem.c#L99>

Well, that and all of the elements where it makes sense to have the background color change. Looks like this has context to class variables and does the actual drawing of elements, calling into the platform specific code where necessary.

I might try my hand at compiling XPCE eventually, but I think I’ll go ahead and set the text color instead of the background color for my exercise and call it a day 🙂
