Page 1 of 1

A doubt abou popmenu

Posted: Mon Sep 05, 2005 03:39
by thejinchao
My application always crash when i click any menuitem in a popmenu. The key code is below:

Code: Select all

void MenuItem::closeAllMenuItemPopups()
{
   // are we attached to a PopupMenu?
   PopupMenu* pop = (PopupMenu*)getParent();
   if (getParent()->testClassName("PopupMenu") && pop->isItemInList(this))
   {
      // is this parent popup attached to a menu item?
      MenuItem* item = (MenuItem*)pop->getParent();
      if (item!=NULL && item->testClassName("MenuItem"))
      {
         // close popup
         item->closePopupMenu();
         // recurse
         item->closeAllMenuItemPopups();
      }
      // otherwise we just hide ourselves
      else
      {
         //----------------------------------
         //ATTENTION HERE!!!!
         //If "item" is NOT a MenuItem class, this line will crash the application
         //----------------------------------
         item->closePopupMenu();
      }
   }
}

Re: A doubt abou popmenu

Posted: Mon Sep 05, 2005 08:23
by CrazyEddie
I'm sure you'll appreciate that with the amount of code there is in the library, combined with the large probability that the issue is in the client code, saying "it crashes" is of little use to us when trying to help you ;)

You need to debug this and at least post the exception that you're getting and a debug stack trace of some description.

Thanks.

Re: A doubt abou popmenu

Posted: Mon Sep 05, 2005 08:50
by Blakharaz
You are using this C cast

Code: Select all

MenuItem* item = (MenuItem*)pop->getParent();

So every parent of pop is cast to a MenuItem pointer. You can't call a method on this pointer if it is NULL (I bet this is the case, since the else branch includes the case of item being NULL) or not a MenuItem.
The cast always works, since this is a C cast (use dynamic_cast when CEGUI is compiled with RTTI to be sure, or at least static_cast, these are easier to locate in the code), but you must be sure to have a pointer of the right type, or else your next method call will fail.

Re: A doubt abou popmenu

Posted: Tue Sep 06, 2005 01:37
by thejinchao
Sorry for my halfhearted :oops:
I found the actual reason is my wrong method of create.
I wrote menuitem in layout file as "sub window"

Code: Select all

<Window Type="MyLook/PopupMenu" Name="ContexMenu_OtherPlayer">
   ...
   <Window Type="MyLook/MenuItem" Name="OtherPlayer_Observe">
      <Property Name="Text" Value="{ID=OBSERVE}" />
      ...
   </Window>
   ...
</Window>


I found menuitem can't be added as a "subitem" in popupmenu.
The function MenuItem::setPopupMenu wasn't be executed at all. So, the exception will appear when i clicked a menuitem.
So..., I am little confused about it. Is there a method to create a whole menu(include menuitem) in a layout file?

Re: A doubt abou popmenu

Posted: Tue Sep 06, 2005 12:29
by Blakharaz
Just use PopupMenuItem instead of just MenuItem, that should do.

To provide a complete menu bar in a XML file use something like

Code: Select all

<Window Type="Look/Menubar" Name="MainMenu">
       <Window Type="Look/MenubarItem" Name="MainMenu/Item1">
              <Property Name="Text" Value="Item1"/>

              <Window Type="Look/PopupMenu" Name="MainMenu/Item1/Menu">
                    <Window Type="RastullahLook/PopupMenuItem" Name="MainMenu/Item1/Item1">
                          <Property Name="Text" Value="Item1"/>
                    </Window>

                    <Window Type="Look/PopupMenuItem" Name="MainMenu/Item1/Item2">
                          <Property Name="Text" Value="Item2"/>
                    </Window>
              </Window>
        </Window>
    </Window>
</GUILayout>

Re: A doubt abou popmenu

Posted: Tue Sep 06, 2005 13:35
by thejinchao
Thank you,I see it :)