Any way to set font for Window and its children? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
cd_hodges
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Feb 03, 2005 4:46 pm

Any way to set font for Window and its children?

Post by cd_hodges » Tue Aug 09, 2005 10:58 am

I was wondering if there is a way to sent a font for say a panel and have it propogate down to its children?

I'm working with a cross-platform app (Windows,Mac) and I need to be able to set basically a global font but i'd settle for the ability to set a font for a wxPanel and have its children change to that font.

thanks,

Chris Hodges

vdell
Moderator
Moderator
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Post by vdell » Tue Aug 09, 2005 11:04 am

Did you took a look at the docs? wxWindow::SetFont
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere

eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew
Contact:

Post by eco » Tue Aug 09, 2005 11:18 am

Something like this will probably do the trick:

Code: Select all

std::stack<wxWindow*> window_stack;
window_stack.push(parent_window);
while(!window_stack.empty())
{
    wxWindow* current = window_stack.top();
    window_stack.pop();
    current->SetFont(font);
    wxWindowList list = parent_window.GetChildren();
    for(WindowList::Node *node = list.GetFirst(); node; node = node->GetNext())
        window_stack.push(node->GetData());
}
I can't remember if wxWindowList is the correct name for what GetChildren returns but thats the essential logic.

eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew
Contact:

Post by eco » Tue Aug 09, 2005 11:22 am

Oh, forgot to mention that is only if you want to do it after the parent window and children have been created. If you SetFont() (like vdell said) before you create the children, they will inherit the font. Someone correct me if I'm wrong on this (I've always assumed this is what the docs where trying to say but the whole property inheritance is a bit confusing).

cd_hodges
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Feb 03, 2005 4:46 pm

Post by cd_hodges » Tue Aug 09, 2005 11:26 am

Yes I did look at that but it does not seem to work at all under wxMAC. here's an example of what I tried.

wPanel *mainPanel = new wxPanel(this);
wxFont ff = mainPanel->GetFont();
ff->SetPointSize(24); // something way too large -- for testing
mainPanel->SetFont(ff);

wxPanel *subPanel = new wxPanel(mainPanel,-1);
wxTextCtrl *textCtrl = new wxTextCtrl(subPanel,-1 ........


The textCtrl is not changed to use the new larger font so then I tried inserting this after the creates of each.....

subPanel->InheritAttributes();
textCtrl->InheritAttributes();

And nothing.... still doesn't take. Now I've only tested this code on the Mac - maybe its a platform thing.

Chris Hodges

eco wrote:Oh, forgot to mention that is only if you want to do it after the parent window and children have been created. If you SetFont() (like vdell said) before you create the children, they will inherit the font. Someone correct me if I'm wrong on this (I've always assumed this is what the docs where trying to say but the whole property inheritance is a bit confusing).

cd_hodges
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Feb 03, 2005 4:46 pm

Post by cd_hodges » Tue Aug 09, 2005 2:06 pm

So at this point I'm having to explicitly set the font for EVERY single control in my application depending on which platform I'm on.

As you can imagine this gets pretty sloppy and inefficient.

Chris Hodges

vdell
Moderator
Moderator
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Post by vdell » Tue Aug 09, 2005 2:24 pm

cd_hodges wrote:Yes I did look at that but it does not seem to work at all under wxMAC. here's an example of what I tried.

wPanel *mainPanel = new wxPanel(this);
wxFont ff = mainPanel->GetFont();
ff->SetPointSize(24); // something way too large -- for testing
mainPanel->SetFont(ff);

wxPanel *subPanel = new wxPanel(mainPanel,-1);
wxTextCtrl *textCtrl = new wxTextCtrl(subPanel,-1 ........


The textCtrl is not changed to use the new larger font so then I tried inserting this after the creates of each.....

subPanel->InheritAttributes();
textCtrl->InheritAttributes();

And nothing.... still doesn't take. Now I've only tested this code on the Mac - maybe its a platform thing.

Chris Hodges

eco wrote:Oh, forgot to mention that is only if you want to do it after the parent window and children have been created. If you SetFont() (like vdell said) before you create the children, they will inherit the font. Someone correct me if I'm wrong on this (I've always assumed this is what the docs where trying to say but the whole property inheritance is a bit confusing).
Just an idea, but have you tried to create all controls first and then call SetFont from the parent?
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere

cd_hodges
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Feb 03, 2005 4:46 pm

Post by cd_hodges » Tue Aug 09, 2005 6:50 pm

Yeah I tried setting the font to the parent AFTER creating the controls but no dice. Sigh..... Looks like I'm stuck on setting the font on each individual control on the Mac platform. Sucks.


Chris Hodges

Post Reply