Adding a menu item

From Second Life Wiki
Revision as of 13:36, 1 November 2006 by Rob Linden (talk | contribs) (+{{OSWikiLearnBox|parent=Viewer architecture}})
Jump to navigation Jump to search

Menu items in the viewer are specified in XML files, located in the indra/newview/skins/xui/en-us/ directory.

There are separate XML files for various categories of menu. They are:

menu_viewer.xml - top of screen
menu_inventory.xml - right-click on inventory item
menu_pie_attachment.xml - right-click on your own attachment
menu_pie_avatar.xml - right-click on other avatar or their attachments
menu_pie_land.xml - right-click on land
menu_pie_self.xml - right-click on your avatar

The menu callbacks are set up in llviewermenu.cpp.

Let's add a menu item named "Foo" to the Tools menu.

  • In menu_viewer.xml find the "Tools" menu item. It is inside a <menu> tag.
  • Tear-off sub menus use the <tearoff_menu> tag.
  • Find the "Reset Scripts in Selection" menu item.
  • After it, add the following text:
<menu_item_call label="Foo" enabled="true" name="foo_menu">
  <on_click function="Tools.Foo" userdata="" />
</menu_item_call>
  • This sets up a menu item that will call a function. It can be referenced in C++ by the name "foo_menu". The user-visible label is "Foo". It defaults to enabled.
  • Open newview/llviewermenu.cpp
  • Create an event handler class for this menu item. By convention these are named after the menu name and item name.
class LLToolsFoo : public view_listener_t
{
    bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata)
    {
        llinfos << "foo!" << llendl;
        return true;
    }
};
  • This class will listen for events from a "view", the base class for all UI widgets. LLPointer is a reference counting smart pointer. LLSD is a structured data class similar to a Perl or Python variable - it can hold data of any type.
  • llinfos is the output stream for informational messages. It goes to the ctrl-shift-4 debug console, stdout, and the log file.
  • Now associate the "Tools.Foo" function with a C++ class. Open newview/llviewermenu.cpp.
  • In the function initialize_menu_actions() add the line:
(new LLToolsFoo())->registerListener(gMenuHolder, "Tools.Foo");
  • This makes your LLToolsFoo class listen to events from the gMenuHolder, a view which contains all the menu UI widgets.
  • It also associates this listener with the "Tools.Foo" event.

Compile and run!