#include "precompiled.h" /* * wxJavaScript - menubar.cpp * * Copyright (c) 2002-2007 Franky Braem and the wxJavaScript project * * Project Info: http://www.wxjavascript.net or http://wxjs.sourceforge.net * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * $Id: menubar.cpp 784 2007-06-25 18:34:22Z fbraem $ */ // menubar.cpp #ifndef WX_PRECOMP #include #endif #include "../../common/main.h" #include "menubar.h" #include "menu.h" using namespace wxjs; using namespace wxjs::gui; /*** * control/menubar * gui * * A menu bar is a series of menus accessible from the top of a frame. * See @wxFrame#menuBar property, @wxMenu and @wxMenuItem. * */ WXJS_INIT_CLASS(MenuBar, "wxMenuBar", 0) /*** * * * The number of menus * * * Returns all the menus belonging to the menubar. * * */ WXJS_BEGIN_PROPERTY_MAP(MenuBar) WXJS_READONLY_PROPERTY(P_MENUCOUNT, "menuCount") WXJS_READONLY_PROPERTY(P_MENUS, "menus") WXJS_END_PROPERTY_MAP() bool MenuBar::GetProperty(wxMenuBar *p, JSContext *cx, JSObject* WXUNUSED(obj), int id, jsval *vp) { switch(id) { case P_MENUCOUNT: { *vp = ToJS(cx, p->GetMenuCount()); break; } case P_MENUS: { jsint count = p->GetMenuCount(); JSObject *objMenus = JS_NewArrayObject(cx, count, NULL); *vp = OBJECT_TO_JSVAL(objMenus); for (jsint i = 0; i < count; i++ ) { JavaScriptClientData *data = dynamic_cast(p->GetMenu(i)); if ( data != NULL ) { jsval element = OBJECT_TO_JSVAL(data->GetObject()); JS_SetElement(cx, objMenus, i++, &element); } } break; } } return true; } /*** * * * (wxGTK only) * * */ WXJS_BEGIN_CONSTANT_MAP(MenuBar) // Style constants WXJS_CONSTANT(wxMB_, DOCKABLE) WXJS_END_CONSTANT_MAP() /*** * * * * * * Constructs a new wxMenuBar object. * * */ wxMenuBar* MenuBar::Construct(JSContext *cx, JSObject* obj, uintN argc, jsval *argv, bool WXUNUSED(constructing)) { int style = 0; if ( argc == 1 && ! FromJS(cx, argv[0], style) ) return NULL; wxMenuBar *p = new wxMenuBar(style); SetPrivate(cx, obj, p); return p; } WXJS_BEGIN_METHOD_MAP(MenuBar) WXJS_METHOD("append", append, 2) WXJS_METHOD("check", check, 2) WXJS_METHOD("enable", enable, 2) WXJS_METHOD("enableTop", enableTop, 2) WXJS_METHOD("getMenu", get_menu, 1) WXJS_METHOD("insert", insert, 3) WXJS_METHOD("findMenu", findMenu, 1) WXJS_METHOD("findMenuItem", findMenuItem, 2) WXJS_METHOD("getHelpString", getHelpString, 1) WXJS_METHOD("getLabel", getLabel, 1) WXJS_METHOD("getLabelTop", getLabelTop, 1) WXJS_METHOD("refresh", refresh, 0) WXJS_METHOD("remove", remove, 1) WXJS_METHOD("replace", replace, 3) WXJS_METHOD("setHelpString", setHelpString, 2) WXJS_METHOD("setLabel", setLabel, 2) WXJS_METHOD("setLabelTop", setLabelTop, 2) WXJS_END_METHOD_MAP() /*** * * * * * * * Adds the menu to the menubar * * */ JSBool MenuBar::append(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxMenu *menu = Menu::GetPrivate(cx, argv[0]); if ( menu == NULL ) return JS_FALSE; wxString name; FromJS(cx, argv[1], name); *rval = ToJS(cx, p->Append(menu, name)); return JS_TRUE; } /*** * * * * The menu item identifier * * * If true, checks the menu item, otherwise the item is unchecked * * * * Checks/Unchecks the menu with the given id. Only use this when the * menu bar has been associated with a @wxFrame; otherwise, * use the @wxMenu equivalent call. * * */ JSBool MenuBar::check(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int id; bool checked; if ( FromJS(cx, argv[0], id) && FromJS(cx, argv[1], checked) ) { p->Check(id, checked); return JS_TRUE; } return JS_FALSE; } /*** * * * * The menu item identifier * * * If true, enables the menu item, otherwise the item is disabled * * * * Enables/Disables the menu with the given id. * Only use this when the menu bar has been associated with a * @wxFrame; otherwise, use the @wxMenu equivalent call. * * */ JSBool MenuBar::enable(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int id; bool sw; if ( FromJS(cx, argv[0], id) && FromJS(cx, argv[1], sw) ) { p->Enable(id, sw); return JS_TRUE; } return JS_FALSE; } /*** * * * * The position of the menu, starting from zero * * * If true, enables the menu, otherwise the menu is disabled * * * * Enables or disables a whole menu. * Only use this when the menu bar has been associated with a @wxFrame. * * */ JSBool MenuBar::enableTop(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int id; bool sw; if ( FromJS(cx, argv[0], id) && FromJS(cx, argv[1], sw) ) { p->EnableTop(id, sw); return JS_TRUE; } return JS_FALSE; } /*** * * * * The name of the menu * * * * Returns the index of the menu with the given name. -1 * is returned when the menu is not found. * * */ JSBool MenuBar::findMenu(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxString name; FromJS(cx, argv[0], name); *rval = ToJS(cx, p->FindMenu(name)); return JS_TRUE; } /*** * * * * The name of the menu * * * The name of the menuitem. * * * * Finds the menu item id for a menu name/menu item string pair. * -1 is returned when nothing is found. * * */ JSBool MenuBar::findMenuItem(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxString menuName; wxString itemName; FromJS(cx, argv[0], menuName); FromJS(cx, argv[1], itemName); *rval = ToJS(cx, p->FindMenuItem(menuName, itemName)); return JS_TRUE; } /*** * * * * A menu item identifier. * * * * Returns the helpstring associated with the menu item or an empty * String when the menu item is not found. See @wxMenuItem#help property * and @wxMenu#getHelpString method. * * */ JSBool MenuBar::getHelpString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int id; if ( FromJS(cx, argv[0], id) ) { *rval = ToJS(cx, p->GetHelpString(id)); return JS_TRUE; } return JS_FALSE; } /*** * * * * A menu item identifier. * * * * Returns the label associated with the menu item or an empty * String when the menu item is not found. * Use only after the menubar has been associated with a @wxFrame. * * */ JSBool MenuBar::getLabel(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int id; if ( FromJS(cx, argv[0], id) ) { *rval = ToJS(cx, p->GetLabel(id)); return JS_TRUE; } return JS_FALSE; } /*** * * * * Position of the menu on the menu bar, starting from zero. * * * * Returns the menu label, or the empty string if the menu was not found. * Use only after the menubar has been associated with a @wxFrame. * See also @wxMenu#title and @wxMenuBar#setLabelTop * * */ JSBool MenuBar::getLabelTop(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int idx; if ( FromJS(cx, argv[0], idx) ) { *rval = ToJS(cx, p->GetLabelTop(idx)); return JS_TRUE; } return JS_FALSE; } /*** * * * * * * Returns the @wxMenu at the given index (zero-indexed). * * */ JSBool MenuBar::get_menu(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int idx; if ( FromJS(cx, argv[0], idx) ) { wxMenu *menu = (wxMenu*) p->GetMenu(idx); if ( menu != NULL ) { JavaScriptClientData *data = dynamic_cast(menu->GetClientObject()); if ( data != NULL ) { *rval = OBJECT_TO_JSVAL(data->GetObject()); } return JS_TRUE; } } return JS_FALSE; } /*** * * * * The position of the new menu in the menu bar * * * The menu to add. * * * The title of the menu. * * * * Inserts the menu at the given position into the menu bar. * Inserting menu at position 0 will insert it in the very beginning of it, * inserting at position @wxMenuBar#menuCount is the same as calling * @wxMenuBar#append. * * */ JSBool MenuBar::insert(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int pos; if ( ! FromJS(cx, argv[0], pos) ) return JS_FALSE; wxMenu *menu = Menu::GetPrivate(cx, argv[1]); if ( menu == NULL ) return JS_FALSE; wxString title; FromJS(cx, argv[2], title); p->Insert(pos, menu, title); return JS_TRUE; } /*** * * * * A menu item identifier. * * * * Returns true when the menu item is checked. * See @wxMenu#check method, @wxMenu#isChecked and @wxMenuItem#check property. * * */ JSBool MenuBar::isChecked(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int idx; if ( FromJS(cx, argv[0], idx) ) { *rval = ToJS(cx, p->IsChecked(idx)); return JS_TRUE; } return JS_FALSE; } /*** * * * * A menu item identifier. * * * * Returns true when the menu item is enabled. * @wxMenu#enable method, @wxMenuItem#enable property and @wxMenuBar#enable * * */ JSBool MenuBar::isEnabled(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int idx; if ( FromJS(cx, argv[0], idx) ) { *rval = ToJS(cx, p->IsEnabled(idx)); return JS_TRUE; } return JS_FALSE; } /*** * * * * Redraw the menu bar. * * */ JSBool MenuBar::refresh(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; p->Refresh(); return JS_TRUE; } /*** * * * * The index of the menu to remove (zero-indexed). * * * * Removes the menu from the menu bar and returns the @wxMenu object. * This function may be used together with @wxMenuBar#insert to change * the menubar dynamically. * * */ JSBool MenuBar::remove(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int idx; if ( FromJS(cx, argv[0], idx) ) { wxMenu *menu = p->Remove(idx); if ( menu != NULL ) { JavaScriptClientData *data = dynamic_cast(menu->GetClientObject()); if ( data != NULL ) *rval = OBJECT_TO_JSVAL(data->GetObject()); } return JS_TRUE; } return JS_FALSE; } /*** * * * * The index of the menu to replace (zero-indexed). * * * The new menu * * * The title of the menu * * * * Replaces the menu at the given position with the new one. * * */ JSBool MenuBar::replace(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int pos; if ( ! FromJS(cx, argv[0], pos) ) return JS_FALSE; wxMenu *menu = Menu::GetPrivate(cx, argv[1]); if ( menu == NULL ) return JS_FALSE; wxString title; FromJS(cx, argv[2], title); wxMenu *oldMenu = p->Replace(pos, menu, title); if ( oldMenu == NULL ) { *rval = JSVAL_VOID; } else { JavaScriptClientData *data = dynamic_cast(oldMenu->GetClientObject()); *rval = ( data == NULL ) ? JSVAL_VOID : OBJECT_TO_JSVAL(data->GetObject()); } return JS_TRUE; } /*** * * * * A menu item identifier. * * * The help text * * * * Sets the help associated with a menu item. * See @wxMenuItem#help property, @wxMenu#setHelpString method * * */ JSBool MenuBar::setHelpString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int id; if ( ! FromJS(cx, argv[0], id) ) return JS_FALSE; wxString str; FromJS(cx, argv[1], str); p->SetHelpString(id, str); return JS_TRUE; } /*** * * * * A menu item identifier. * * * A new label * * * * Sets the label of a menu item. * Only use this when the menubar is associated with a @wxFrame * @wxMenuItem#label property, @wxMenu#setLabel method * * */ JSBool MenuBar::setLabel(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int id; if ( ! FromJS(cx, argv[0], id) ) return JS_FALSE; wxString str; FromJS(cx, argv[1], str); p->SetLabel(id, str); return JS_TRUE; } /*** * * * * A menu index (zero-indexed) * * * A label for a menu * * * * Sets the label of a menu. * Only use this when the menubar is associated with a @wxFrame * See @wxMenu#title property * * */ JSBool MenuBar::setLabelTop(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxMenuBar *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; int idx; wxString str; if ( FromJS(cx, argv[0], idx) ) { FromJS(cx, argv[1], str); p->SetLabelTop(idx, str); return JS_TRUE; } return JS_FALSE; }