[SOLVED] Get the Window Type

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

Excizted
Not too shy to talk
Not too shy to talk
Posts: 21
Joined: Tue Feb 23, 2010 22:16

[SOLVED] Get the Window Type

Postby Excizted » Sat May 29, 2010 11:45

Hello,

How do I determine what window type my Window is, as in Editbox, MultilineEditbox, Button or something else?
With the CEGUI::Window::getType() I only get the looknfeel type which could be ANYTHING in the heart of the dear guy who wrote it (MyGUISheet/Editbox or even MyGUISheet/LeetBoxForJustAboutEverything).

Maybe I don't need it at all? Say I wan't to set readonly on my window.

Code: Select all

static_cast<CEGUI::Editbox*>(m_Window)->setReadOnly(true);

That works.. If the window is an editbox.

I need a function to cast it to the right Window type and then set read only, or abort if the Window type isn't a type who has setReadOnly anyway.

My original plan was something like this

Code: Select all

String type = m_Window->getType().c_str();
if(type == "Editbox")
    static_cast<CEGUI::Editbox*>(m_Window)->setReadOnly(mode);
else if(type == "MultiLineEditbox")
    static_cast<CEGUI::MultiLineEditbox*>(m_Window)->setReadOnly(mode);
else
    Log("Don't set read only on something that can't be...");



Thank you for your help :)
Last edited by Excizted on Sun May 30, 2010 00:38, edited 1 time in total.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Get the Window Type

Postby CrazyEddie » Sat May 29, 2010 14:13

There exists a few possibilities here, I'll post them as code snippets based on your own code, then you can pick one that you like :P

Use Window::testClassName to test whether a class is a particular class (or inherits that class).

Code: Select all

if (m_Window->testClassName( "Editbox" ))
    static_cast<CEGUI::Editbox*>(m_Window)->setReadOnly(mode);
else if (m_Window->testClassName( "MultiLineEditbox" ))
    static_cast<CEGUI::MultiLineEditbox*>(m_Window)->setReadOnly(mode);
else
    Log("Don't set read only on something that can't be...");


Use C++ dynamic_cast to determine whether class is a particular class (or inherits that class)

Code: Select all

CEGUI::Editbox* ebox = dynamic_cast<CEGUI::Editbox*>(m_Window);
if (ebox)
{
    ebox->setReadOnly(mode);
}
else
{
    CEGUI::MultiLineEditbox* mlbox = dynamic_cast<CEGUI::MultiLineEditbox*>(m_Window);
    if (mlbox)
    {
        mlbox->setReadOnly(mode);
    }
    else
    {
        Log("Don't set read only on something that can't be...");
    }
}


Use properties:

Code: Select all

if (m_Window->isPropertyPresent( "ReadOnly" ))
    m_Window->setProperty( "ReadOnly", CEGUI::PropertyHelper::boolToString(mode) );
else
    Log("Don't set read only on something that can't be...");


HTH

CE

Excizted
Not too shy to talk
Not too shy to talk
Posts: 21
Joined: Tue Feb 23, 2010 22:16

Re: Get the Window Type

Postby Excizted » Sat May 29, 2010 23:38

Just awesome!

I was being frightened that CEGUI might not support this, and I'd have to work it out myself.
But you're able to throw not less than 3 solutions in my face :)

I'm a bit ashamed, I should have known about dynamic_cast. I tried the exact code you wrote, with the static_cast call.

I'll be going with propertyPresent :)

Thank you very much!


Return to “Help”

Who is online

Users browsing this forum: No registered users and 14 guests