]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalCanvas.cxx
Interim windows build fix
[flightgear.git] / src / Scripting / NasalCanvas.cxx
index 4da6b5a23b00c34eb21cc51250b3131931268d18..4fa7652652561e4714607fc18e3ea90482922ee4 100644 (file)
@@ -39,6 +39,7 @@
 #include <simgear/canvas/layout/BoxLayout.hxx>
 #include <simgear/canvas/layout/NasalWidget.hxx>
 #include <simgear/canvas/events/CustomEvent.hxx>
+#include <simgear/canvas/events/KeyboardEvent.hxx>
 #include <simgear/canvas/events/MouseEvent.hxx>
 
 #include <simgear/nasal/cppbind/from_nasal.hxx>
@@ -58,6 +59,8 @@ naRef elementGetNode(Element& element, naContext c)
 
 typedef nasal::Ghost<sc::EventPtr> NasalEvent;
 typedef nasal::Ghost<sc::CustomEventPtr> NasalCustomEvent;
+typedef nasal::Ghost<sc::DeviceEventPtr> NasalDeviceEvent;
+typedef nasal::Ghost<sc::KeyboardEventPtr> NasalKeyboardEvent;
 typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
 
 struct CustomEventDetailWrapper;
@@ -72,6 +75,7 @@ typedef nasal::Ghost<sc::TextPtr> NasalText;
 
 typedef nasal::Ghost<sc::LayoutItemRef> NasalLayoutItem;
 typedef nasal::Ghost<sc::LayoutRef> NasalLayout;
+typedef nasal::Ghost<sc::BoxLayoutRef> NasalBoxLayout;
 
 typedef nasal::Ghost<sc::WindowPtr> NasalWindow;
 
@@ -138,11 +142,10 @@ static naRef f_createWindow(const nasal::CallContext& ctx)
 /**
  * Get ghost for existing Canvas.
  */
-static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
+static naRef f_getCanvas(const nasal::CallContext& ctx)
 {
-  nasal::CallContext ctx(c, argc, args);
   SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
-  CanvasMgr& canvas_mgr = requireCanvasMgr(c);
+  CanvasMgr& canvas_mgr = requireCanvasMgr(ctx.c);
 
   sc::CanvasPtr canvas;
   if( canvas_mgr.getPropertyRoot() == props.getParent() )
@@ -161,7 +164,7 @@ static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
       canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
   }
 
-  return nasal::to_nasal(c, canvas);
+  return ctx.to_nasal(canvas);
 }
 
 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
@@ -177,6 +180,25 @@ naRef f_getDesktop(naContext c, naRef me, int argc, naRef* args)
   return nasal::to_nasal(c, requireGUIMgr(c).getDesktop());
 }
 
+naRef f_setInputFocus(const nasal::CallContext& ctx)
+{
+  requireGUIMgr(ctx.c).setInputFocus(ctx.requireArg<sc::WindowPtr>(0));
+  return naNil();
+}
+
+naRef f_grabPointer(const nasal::CallContext& ctx)
+{
+  return ctx.to_nasal(
+    requireGUIMgr(ctx.c).grabPointer(ctx.requireArg<sc::WindowPtr>(0))
+  );
+}
+
+naRef f_ungrabPointer(const nasal::CallContext& ctx)
+{
+  requireGUIMgr(ctx.c).ungrabPointer(ctx.requireArg<sc::WindowPtr>(0));
+  return naNil();
+}
+
 static naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
 {
   return ctx.to_nasal( group.createChild( ctx.requireArg<std::string>(0),
@@ -272,18 +294,13 @@ static naRef f_propElementData( simgear::PropertyBasedElement& el,
   return naNil();
 }
 
-template<int Mask>
-naRef f_eventGetModifier(sc::MouseEvent& event, naContext)
-{
-  return naNum((event.getModifiers() & Mask) != 0);
-}
-
 static naRef f_createCustomEvent(const nasal::CallContext& ctx)
 {
   std::string const& type = ctx.requireArg<std::string>(0);
   if( type.empty() )
     return naNil();
 
+  bool bubbles = false;
   simgear::StringMap detail;
   if( ctx.isHash(1) )
   {
@@ -291,9 +308,12 @@ static naRef f_createCustomEvent(const nasal::CallContext& ctx)
     naRef na_detail = cfg.get("detail");
     if( naIsHash(na_detail) )
       detail = ctx.from_nasal<simgear::StringMap>(na_detail);
+    bubbles = cfg.get<bool>("bubbles");
   }
 
-  return ctx.to_nasal( sc::CustomEventPtr(new sc::CustomEvent(type, detail)) );
+  return ctx.to_nasal(
+    sc::CustomEventPtr(new sc::CustomEvent(type, bubbles, detail))
+  );
 }
 
 struct CustomEventDetailWrapper:
@@ -341,6 +361,38 @@ static naRef f_customEventGetDetail( sc::CustomEvent& event,
   );
 }
 
+static naRef f_boxLayoutAddItem( sc::BoxLayout& box,
+                                 const nasal::CallContext& ctx )
+{
+  box.addItem( ctx.requireArg<sc::LayoutItemRef>(0),
+               ctx.getArg<int>(1),
+               ctx.getArg<int>(2, sc::AlignFill) );
+  return naNil();
+}
+static naRef f_boxLayoutInsertItem( sc::BoxLayout& box,
+                                    const nasal::CallContext& ctx )
+{
+  box.insertItem( ctx.requireArg<int>(0),
+                  ctx.requireArg<sc::LayoutItemRef>(1),
+                  ctx.getArg<int>(2),
+                  ctx.getArg<int>(3, sc::AlignFill) );
+  return naNil();
+}
+
+static naRef f_boxLayoutAddStretch( sc::BoxLayout& box,
+                                    const nasal::CallContext& ctx )
+{
+  box.addStretch( ctx.getArg<int>(0) );
+  return naNil();
+}
+static naRef f_boxLayoutInsertStretch( sc::BoxLayout& box,
+                                       const nasal::CallContext& ctx )
+{
+  box.insertStretch( ctx.requireArg<int>(0),
+                     ctx.getArg<int>(1) );
+  return naNil();
+}
+
 template<class Type, class Base>
 static naRef f_newAsBase(const nasal::CallContext& ctx)
 {
@@ -362,7 +414,9 @@ naRef initNasalCanvas(naRef globals, naContext c)
     .member("type", &sc::Event::getTypeString)
     .member("target", &sc::Event::getTarget)
     .member("currentTarget", &sc::Event::getCurrentTarget)
-    .method("stopPropagation", &sc::Event::stopPropagation);
+    .member("defaultPrevented", &sc::Event::defaultPrevented)
+    .method("stopPropagation", &sc::Event::stopPropagation)
+    .method("preventDefault", &sc::Event::preventDefault);
 
   NasalCustomEvent::init("canvas.CustomEvent")
     .bases<NasalEvent>()
@@ -374,8 +428,24 @@ naRef initNasalCanvas(naRef globals, naContext c)
   canvas_module.createHash("CustomEvent")
                .set("new", &f_createCustomEvent);
 
-  NasalMouseEvent::init("canvas.MouseEvent")
+  NasalDeviceEvent::init("canvas.DeviceEvent")
     .bases<NasalEvent>()
+    .member("modifiers", &sc::DeviceEvent::getModifiers)
+    .member("ctrlKey", &sc::DeviceEvent::ctrlKey)
+    .member("shiftKey", &sc::DeviceEvent::shiftKey)
+    .member("altKey",  &sc::DeviceEvent::altKey)
+    .member("metaKey",  &sc::DeviceEvent::metaKey);
+
+  NasalKeyboardEvent::init("canvas.KeyboardEvent")
+    .bases<NasalDeviceEvent>()
+    .member("key", &sc::KeyboardEvent::key)
+    .member("location", &sc::KeyboardEvent::location)
+    .member("repeat", &sc::KeyboardEvent::repeat)
+    .member("charCode", &sc::KeyboardEvent::charCode)
+    .member("keyCode", &sc::KeyboardEvent::keyCode);
+
+  NasalMouseEvent::init("canvas.MouseEvent")
+    .bases<NasalDeviceEvent>()
     .member("screenX", &sc::MouseEvent::getScreenX)
     .member("screenY", &sc::MouseEvent::getScreenY)
     .member("clientX", &sc::MouseEvent::getClientX)
@@ -386,11 +456,6 @@ naRef initNasalCanvas(naRef globals, naContext c)
     .member("deltaY", &sc::MouseEvent::getDeltaY)
     .member("button", &sc::MouseEvent::getButton)
     .member("buttons", &sc::MouseEvent::getButtonMask)
-    .member("modifiers", &sc::MouseEvent::getModifiers)
-    .member("ctrlKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_CTRL>)
-    .member("shiftKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_SHIFT>)
-    .member("altKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_ALT>)
-    .member("metaKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_META>)
     .member("click_count", &sc::MouseEvent::getCurrentClickCount);
 
   //----------------------------------------------------------------------------
@@ -410,7 +475,10 @@ naRef initNasalCanvas(naRef globals, naContext c)
              static_cast<bool (sc::Canvas::*)( const std::string&,
                                                const sc::EventListener& )>
              (&sc::Canvas::addEventListener) )
-    .method("dispatchEvent", &sc::Canvas::dispatchEvent);
+    .method("dispatchEvent", &sc::Canvas::dispatchEvent)
+    .method("setLayout", &sc::Canvas::setLayout)
+    .method("setFocusElement", &sc::Canvas::setFocusElement)
+    .method("clearFocusElement", &sc::Canvas::clearFocusElement);
 
   canvas_module.set("_newCanvasGhost", f_createCanvas);
   canvas_module.set("_getCanvasGhost", f_getCanvas);
@@ -421,6 +489,7 @@ naRef initNasalCanvas(naRef globals, naContext c)
     .method("_getParent", &sc::Element::getParent)
     .method("_getCanvas", &sc::Element::getCanvas)
     .method("addEventListener", &sc::Element::addEventListener)
+    .method("setFocus", &sc::Element::setFocus)
     .method("dispatchEvent", &sc::Element::dispatchEvent)
     .method("getBoundingBox", &sc::Element::getBoundingBox)
     .method("getTightBoundingBox", &sc::Element::getTightBoundingBox);
@@ -432,35 +501,90 @@ naRef initNasalCanvas(naRef globals, naContext c)
     .method("_getElementById", &sc::Group::getElementById);
   NasalText::init("canvas.Text")
     .bases<NasalElement>()
-    .method("getNearestCursor", &sc::Text::getNearestCursor);
+    .method("heightForWidth", &sc::Text::heightForWidth)
+    .method("maxWidth", &sc::Text::maxWidth)
+    .method("lineCount", &sc::Text::lineCount)
+    .method("lineLength", &sc::Text::lineLength)
+    .method("getNearestCursor", &sc::Text::getNearestCursor)
+    .method("getCursorPos", &sc::Text::getCursorPos);
 
   //----------------------------------------------------------------------------
   // Layouting
 
+#define ALIGN_ENUM_MAPPING(key, val, comment) canvas_module.set(#key, sc::key);
+#  include <simgear/canvas/layout/AlignFlag_values.hxx>
+#undef ALIGN_ENUM_MAPPING
+
+  void (sc::LayoutItem::*f_layoutItemSetContentsMargins)(int, int, int, int)
+    = &sc::LayoutItem::setContentsMargins;
+
   NasalLayoutItem::init("canvas.LayoutItem")
     .method("getCanvas", &sc::LayoutItem::getCanvas)
     .method("setCanvas", &sc::LayoutItem::setCanvas)
-    .method("getParent", &sc::LayoutItem::getParent);
+    .method("getParent", &sc::LayoutItem::getParent)
+    .method("setParent", &sc::LayoutItem::setParent)
+    .method("setContentsMargins", f_layoutItemSetContentsMargins)
+    .method("setContentsMargin", &sc::LayoutItem::setContentsMargin)
+    .method("sizeHint", &sc::LayoutItem::sizeHint)
+    .method("minimumSize", &sc::LayoutItem::minimumSize)
+    .method("maximumSize", &sc::LayoutItem::maximumSize)
+    .method("hasHeightForWidth", &sc::LayoutItem::hasHeightForWidth)
+    .method("heightForWidth", &sc::LayoutItem::heightForWidth)
+    .method("minimumHeightForWidth", &sc::LayoutItem::minimumHeightForWidth)
+    .method("setAlignment", &sc::LayoutItem::setAlignment)
+    .method("alignment", &sc::LayoutItem::alignment)
+    .method("setVisible", &sc::LayoutItem::setVisible)
+    .method("isVisible", &sc::LayoutItem::isVisible)
+    .method("isExplicitlyHidden", &sc::LayoutItem::isExplicitlyHidden)
+    .method("show", &sc::LayoutItem::show)
+    .method("hide", &sc::LayoutItem::hide)
+    .method("setGeometry", &sc::LayoutItem::setGeometry)
+    .method("geometry", &sc::LayoutItem::geometry);
   sc::NasalWidget::setupGhost(canvas_module);
 
   NasalLayout::init("canvas.Layout")
     .bases<NasalLayoutItem>()
-    .method("addItem", &sc::Layout::addItem);
+    .method("addItem", &sc::Layout::addItem)
+    .method("setSpacing", &sc::Layout::setSpacing)
+    .method("spacing", &sc::Layout::spacing)
+    .method("count", &sc::Layout::count)
+    .method("itemAt", &sc::Layout::itemAt)
+    .method("takeAt", &sc::Layout::takeAt)
+    .method("removeItem", &sc::Layout::removeItem)
+    .method("clear", &sc::Layout::clear);
+
+  NasalBoxLayout::init("canvas.BoxLayout")
+    .bases<NasalLayout>()
+    .method("addItem", &f_boxLayoutAddItem)
+    .method("addSpacing", &sc::BoxLayout::addSpacing)
+    .method("addStretch", &f_boxLayoutAddStretch)
+    .method("insertItem", &f_boxLayoutInsertItem)
+    .method("insertSpacing", &sc::BoxLayout::insertSpacing)
+    .method("insertStretch", &f_boxLayoutInsertStretch)
+    .method("setStretch", &sc::BoxLayout::setStretch)
+    .method("setStretchFactor", &sc::BoxLayout::setStretchFactor)
+    .method("stretch", &sc::BoxLayout::stretch);
 
   canvas_module.createHash("HBoxLayout")
-               .set("new", &f_newAsBase<sc::HBoxLayout, sc::Layout>);
+               .set("new", &f_newAsBase<sc::HBoxLayout, sc::BoxLayout>);
+  canvas_module.createHash("VBoxLayout")
+               .set("new", &f_newAsBase<sc::VBoxLayout, sc::BoxLayout>);
 
   //----------------------------------------------------------------------------
   // Window
 
   NasalWindow::init("canvas.Window")
     .bases<NasalElement>()
+    .bases<NasalLayoutItem>()
     .member("_node_ghost", &elementGetNode<sc::Window>)
     .method("_getCanvasDecoration", &sc::Window::getCanvasDecoration)
     .method("setLayout", &sc::Window::setLayout);
 
   canvas_module.set("_newWindowGhost", f_createWindow);
   canvas_module.set("_getDesktopGhost", f_getDesktop);
+  canvas_module.set("setInputFocus", f_setInputFocus);
+  canvas_module.set("grabPointer", f_grabPointer);
+  canvas_module.set("ungrabPointer", f_ungrabPointer);
 
   return naNil();
 }