Recent

Author Topic: Dark Theme - forms inherit from parents ?  (Read 3580 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Dark Theme - forms inherit from parents ?
« on: March 15, 2019, 09:00:35 am »
I understand that new forms (and controls etc) should inherit (eg) background color from its parent. But I cannot make it work ...

Suppose I havea  form, I set its background color in OnCreate to, say clBlack and then, later, create a new form with its background color set to clDefault, however it comes up in the usual gray.

The "background" here is I want to make my app capable of the very fashionable Dark Theme but really don't want to have code deciding to do that in every forms OnCreate or OnShow functions if I can possibly avoid it.

Thoughts ?
« Last Edit: March 16, 2019, 02:51:39 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Re: forms inherit from parents ?
« Reply #1 on: March 15, 2019, 09:28:31 am »
You can also try "File", "New...", "Inherited project component"

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: forms inherit from parents ?
« Reply #2 on: March 15, 2019, 10:11:28 pm »
Thanks Wadman, no thats not quite what I was looking for. I already have all my forms ....

I did find some delphi docs that firstly made me realise that there is a difference between a Forms Owner and its Parent. And secondly was doing things like -
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.     Frm : Tform2;
  4. begin
  5.     Frm := TForm2.Create(Application);
  6.     Form2.Parent := self;
  7.     Frm.show;
  8. end;  

But that gives me a SegV. I have established that without the Frm.Parent:=self line, a new form does not have a parent at all and that seems normal (?)  So, my plan of having lower level forms inherit seems doomed ?

"to lose one parent is unfortunate, to lose both smacks of carelessness", Oscar Wild

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: forms inherit from parents ?
« Reply #3 on: March 15, 2019, 11:22:16 pm »
Maybe it's a typo while copying to the forum but, shouldn't that code be:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.     Frm : Tform2;
  4. begin
  5.     Frm := TForm2.Create(Application);
  6.     {Form2.} frm.Parent := self;
  7.     Frm.show;
  8. end;

Also: I don't remember where I read it but, shouldn't normal forms be created with Application.CreateForm? Something to do with streaming the form resource, IIRC

And isn't using a local var for the form a little dangerous unless you use ShowModal and destroy the form before exiting the proc?

And finally ... I'm not sure making one form the parent of another has anything to do with how the second form behaves (if it does at all!). The only way I know of inheriting changed properties, etc. is ... well, creating a child class which modifies those properties.

I understand that new forms (and controls etc) should inherit (eg) background color from its parent.

No, not really. It quite depends on how the control is constructed, the widgetset, the OS theme, etc. There's no immediate relation unless the control has a ParentColor property that really works.

Quote from: dbannon
Suppose I havea  form, I set its background color in OnCreate to, say clBlack and then, later, create a new form with its background color set to clDefault, however it comes up in the usual gray.

All clDefault does is tell the form to use the underlying widgetset default color for forms.
« Last Edit: March 15, 2019, 11:30:32 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: forms inherit from parents ?
« Reply #4 on: March 16, 2019, 12:26:42 am »
Thanks Lucamar

Quote
{Form2.} frm.Parent := self;

No, I don't think so. Frm is a local var, its declared in this scope. What I am saying is create it and set its parent.   (yes, I agree having the frm local to the function is dodgy but its just demo in this case. As owner is Application, it [will, should] free it .....

EDIT : Sorry, I misread your correction, you are quite correct, it should be frm.parent:=  A copy and past error, was trying a number of different models. The frm.Parent:= model only crashes if the parent is set before show. If its set after show (which sounds like a bad idea anyway) it silently closes the second form.

Quote
but, shouldn't normal forms be created with Application.CreateForm?

Hmm, thats an interesting question. I considered both reasonable, will look into that....
Quote
I'm not sure making one form the parent of another has anything to do with how the second form behaves (if it does at all!).

Description of the Color property of a form (default setting being clDefault) -
Quote
The form's background color.
The background color of the control.
The default Color is the same as the parent window Color. If the color is clDefault then the result will need to be passed through GetDefaultColor to resolve clDefault. Convenience routines which obtains the color resolving clDefault and ParentColor are also provided as TControl.GetColorResolvingParent and TControl.GetRGBColorResolvingParent

Now, does that mean its the same as Parent's because its set the same way or because it looks to see what the parent is and sets itself accordingly ?

Davo
« Last Edit: March 16, 2019, 12:40:16 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: forms inherit from parents ?
« Reply #5 on: March 16, 2019, 01:27:10 am »
Description of the Color property of a form (default setting being clDefault) -
Quote
The form's background color.
The background color of the control.
The default Color is the same as the parent window Color. If the color is clDefault then the result will need to be passed through GetDefaultColor to resolve clDefault. Convenience routines which obtains the color resolving clDefault and ParentColor are also provided as TControl.GetColorResolvingParent and TControl.GetRGBColorResolvingParent
Now, does that mean its the same as Parent's because its set the same way or because it looks to see what the parent is and sets itself accordingly ?

Davo

GetDefaultColor does what I said above, try to find what the default color should be depending on the widgetset, OS theme, etc. If it can't be found that way, one of (clWindow, clWindowText) is used, depending on whether it's a background or foreground color. You can find it in: [LazarusFolder]/lcl/include/control.inc

The final color also depends on whether ParentColor is set to True and that's what GetColorResolvingParent does: go up the chain of Parents until it finds the color.

So in a certain sense you're right: setting Form2.ParentColor = True should make that form take on the color of whatever control you set as its Parent. The problem really is whether forms can be parents/childs of other forms ... and I'm not very sure they can.

It's a little late now but I'll try to investigate a little more tomorrow in a few hours :)
« Last Edit: March 16, 2019, 01:29:18 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: forms inherit from parents ?
« Reply #6 on: March 16, 2019, 02:04:35 am »
Ha, Lucamar, its still morning here !

What I have found is that forms cannot inherit from their parents because they don't have parents !  Maybe exceptions (was it MDI forms ? too long ago....).  Anyway, in with LCL, it seems HasParent always (?) returns false. And setting a parent manually closes the form.

On the other hand, I have had some success with controls on a a form inheriting from the form itself. Controls like TEdit, TMemo and TLabel all have a ParentColor property that if set to True, does what I want. The docs say its True by default but it appears its actually false by default.

Buttons do not have ParentColor and cannot, apparently inherit ! 

Interesting !  It seems on Linux at least, some aspects do set colours according to theme. As I always have a light theme, I did not realise. More research is indicated.......

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: forms inherit from parents ?
« Reply #7 on: March 16, 2019, 02:51:01 am »
OK, I have a much better understanding of what is going on here. And its complicated. Different components respond to an OS Dark Theme in different ways. Thus-

TForm - (edit) remembers the background colour of theme active when the app it started. Does not change when the OS theme is changed.

Buttons - always follow the theme and look good. But bitmaps need transparent backgrounds ?

Line around (radio button) group box, unselected buttons  - stays black even when background is dark gray, so, in a dark theme, impossible to see.

KMemo - (not interesting to most readers) needs to be manually changed, not difficult.

And, this is tested on Linux only so far.

Now, I personally don't like dark themes, they look ugly even when well done. But there cannot be any doubt that some users walk on the dark side.

I'm going to change the title of this thread to emphasis its dark theme content.

Davo



« Last Edit: March 16, 2019, 03:31:48 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

TraumTaenzerDieter

  • New Member
  • *
  • Posts: 28
Re: Dark Theme - forms inherit from parents ?
« Reply #8 on: March 16, 2019, 08:14:54 am »
Maybe you should have a look at "VFI" (Visual Form Inheritance).
That's the way I use to inherit all the forms of a project from
just one "baseform". (see attached example)
« Last Edit: March 16, 2019, 08:25:37 am by TraumTaenzerDieter »

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Dark Theme - forms inherit from parents ?
« Reply #9 on: March 16, 2019, 09:48:22 am »
Thanks TraumTaenzerDiet, I will have a look as you suggest.

But, truth be know, this whole thread is down to me doing things in the wrong order. The things I could not (manually) change the colours on were, if I looked a little closer, being changed automatically for me.

There are a handful of things that don't quite work as they should but if you set a dark theme, a Lazarus programme does make a pretty good effort to act accordingly.

The exception being -
* Forms retain the colouring that they determined  was appropriate when the app started. Some controls change as soon as the theme is changed.
* My application uses KControls extensively, it needs manual changing.
* Some controls, while changing with theme changes, don't do it terribly well.

This, still experimental, is useful to determine is we are currently 'doing' a dark theme so we can do the manual adjusting for those things that need it.

Code: Pascal  [Select][+][-]
  1. var
  2.   Col : string;
  3. begin
  4.     // if char 3, 5 and 7 are all 'A' or above, we are not in a DarkTheme
  5.     Col := hexstr(qword(GetRGBColorResolvingParent()), 8);
  6.     if (Col[3] < 'A') and (Col[5] < 'A') and (Col[7] < 'A') then begin
  7.         debugln('Its definitely a Dark Theme');
  8.         ......
  9.  

(I still think dark themes are ugly ! )
Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Dark Theme - forms inherit from parents ?
« Reply #10 on: March 16, 2019, 08:13:45 pm »
Not really usefull for what you want  but here is my little experiment with setting a form's Parent, ParentColor, etc. It works, somehow, but ... well, see for yoursef :o

Quote from: dbannon
(I still think dark themes are ugly ! )
Davo

Not only ugly but unhealthful. When testing with one I always end up with my head aching %)
« Last Edit: March 16, 2019, 08:16:15 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Dark Theme - forms inherit from parents ?
« Reply #11 on: March 17, 2019, 11:16:01 am »
Yep, looks like you have nailed it Lucamar, when we set Form2.Parent := Self, the TForm2 becomes a component _on_ Form1.   In my case, I could not see it because its Top and Left were way beyond Form1's area. So, you set its bounds, and there it is. Well done, at least we now know a bit more about how that works.

And, yep, does not really address what I was trying to do. But as it turns out, I dont need it addressed, Lazarus does much of it form if I change my theme. Sigh ....

Thanks, interesting exercise.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018