Discussions related to Visual Prolog
choibakk
Active Member
Posts: 35
Joined: 5 Sep 2019 19:30

Custom Control Issue with workaround -- Build 1000

Unread post by choibakk »

Hello Thomas,

The Problem: Add a "textControl" to a custom control, and place the custom control on a main form (formWindow). set the "ForegroundColor" property of the textControl (I use red in my example below). When the custom control's "generatedInitialize()" predicate is called at startup the application will crash. I believe happens due to the order in which things are initialized.

Here is the example code snippet of the custom control's "generatedInitialize" (last line):

Code: Select all

predicates     generatedInitialize : (). clauses     generatedInitialize() :-         setText("gameclock"),         setSize(280, 116),         setPaintResponder(onPaint),         gameClockWhite := lcdclock::new(This),         gameClockWhite:setRect(vpiDomains::rct(16, 26, 140, 54)),         gameClockBlack := lcdclock::new(This),         gameClockBlack:setRect(vpiDomains::rct(140, 26, 264, 54)),         whiteColorLabel := textControl::new(This),         whiteColorLabel:setText("WHITE"),         whiteColorLabel:setPosition(16, 6),         whiteColorLabel:setFont(vpi::fontCreateByName("Consolas", 18)),         whiteColorLabel:setSize(124, 20),         whiteColorLabel:setAlignment(vpiDomains::alignCenter),         whiteColorLabel:setForegroundColor(255), % <----------- Setting foreground color to red.
When I step through the code, I believe it is failing because the custom control is not able to properly retrieve the parent window handle do to timing (I'll let you decide).

If I manually change the "setForgroundColor" call in "generatedInitialize " (yes, I know you are not supposed to do this, I'm only showing you for example) to the following the problem is averted:

Code: Select all

predicates     generatedInitialize : (). clauses     generatedInitialize() :-         setText("gameclock"),         setSize(280, 116),         setPaintResponder(onPaint),         gameClockWhite := lcdclock::new(This),         gameClockWhite:setRect(vpiDomains::rct(16, 26, 140, 54)),         gameClockBlack := lcdclock::new(This),         gameClockBlack:setRect(vpiDomains::rct(140, 26, 264, 54)),         whiteColorLabel := textControl::new(This),         whiteColorLabel:setText("WHITE"),         whiteColorLabel:setPosition(16, 6),         whiteColorLabel:setFont(vpi::fontCreateByName("Consolas", 18)),         whiteColorLabel:setSize(124, 20),         whiteColorLabel:setAlignment(vpiDomains::alignCenter),         whenCreated({  :- whiteColorLabel:setForegroundColor(255) }),  %  <--- for example only (never modify code in the "generatedInitialize" function for those of your reading this!)         %whiteColorLabel:setForegroundColor(255),
So, of course removing the call in "generatedInitialize" (set by the VPC GUI property editor), and manually setting it in my own code after "generatedInitialize" is called, is my workaround.

I consider this and the secondary observation below to be extremely minor things with regards to my own development. I only report these kinds of issues (like the ZIP file issue) for your tracking system, and help provide you with information, if they turn out to be real issues.

Secondary Observation:

When adding a "textControl" to a main "formWindow" I cannot edit certain properties in the VPC properties editor (like "Font" and "ForegroundColor" because they are not displayed), that I can edit when placed on a "custom control". I mention this because, manually adding the "setForgroundColor(255)" in the "generatedInitialize" function of the "formWindow" does not have the same problem in my application.

Thanks,
choibakk
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: Custom Control Issue with workaround -- Build 1000

Unread post by Thomas Linder Puls »

What is VPC? Do you mean VIP? (rhetorical questions :-)).

Thank you for the feedback. The problem arises because the customControl sets the Container after calling generatedInitialize.

Code: Select all

clauses     new(Parent) :-         new(),  % call to generatedInitialize         setContainer(Parent).
It will work, if you remove the default constructor (including the declaration) and change the one with Parent like this:

Code: Select all

clauses     new(Parent) :-         setContainer(Parent),         generatedInitialize().
I will consider whether this is a proper solution to the problem (I am a bit in doubt why we have a Parent-free constructor at all).

With regards to be able to set the foreground color on the main form, I think it may change if the project is compiled or not. The thing is that the IDE figures out which things that can be set based on compilation of the relevant things. Advantage: the IDE is not hard-coded to know it; disadvantage: given compilation state the IDE may "get it wrong".
Regards Thomas Linder Puls
PDC
Post Reply