[PATCH] Add "toolbar:bool" RW property for the JS API, fix toolbar visibility

Anthony Loiseau anthony.loiseau at gmail.com
Mon Mar 31 17:03:31 CEST 2008


The mozilla plugin has already a "toolbar"(bool) parameter.
For coherency between ActiveX and mozilla plugin, the "toolbar"
parameter has been added to ActiveX, which has created also a
"toolbar" property in the exported API (JavaScript for web pages).
This commit add it to mozilla plugin too, to maintain coherency.

This commit also fix toolbar visibility when toolbar="true" parameter
is used in embed object.
---
 projects/mozilla/control/npolibvlc.cpp |   51 ++++++++++++-
 projects/mozilla/control/npolibvlc.h   |    1 +
 projects/mozilla/vlcplugin.cpp         |  133 ++++++++++++++++++++++++--------
 projects/mozilla/vlcplugin.h           |   16 ++++-
 projects/mozilla/vlcshell.cpp          |   50 ++++++-------
 5 files changed, 187 insertions(+), 64 deletions(-)

diff --git a/projects/mozilla/control/npolibvlc.cpp b/projects/mozilla/control/npolibvlc.cpp
index 3084d3e..75dc38e 100644
--- a/projects/mozilla/control/npolibvlc.cpp
+++ b/projects/mozilla/control/npolibvlc.cpp
@@ -56,14 +56,15 @@ LibvlcRootNPObject::~LibvlcRootNPObject()
     }
 }
 
-const NPUTF8 * const LibvlcRootNPObject::propertyNames[] = 
+const NPUTF8 * const LibvlcRootNPObject::propertyNames[] =
 {
     "audio",
     "input",
     "log",
     "playlist",
     "video",
-    "VersionInfo",
+    "toolbar",     /* bool, RW, get/set toobar visibility   */
+    "VersionInfo", /* string, RO, like "0.9.0-git Grishenko" */
 };
 
 const int LibvlcRootNPObject::propertyCount = sizeof(LibvlcRootNPObject::propertyNames)/sizeof(NPUTF8 *);
@@ -75,6 +76,7 @@ enum LibvlcRootNPObjectPropertyIds
     ID_root_log,
     ID_root_playlist,
     ID_root_video,
+    ID_root_toolbar,
     ID_root_VersionInfo,
 };
 
@@ -83,6 +85,8 @@ RuntimeNPObject::InvokeResult LibvlcRootNPObject::getProperty(int index, NPVaria
     /* is plugin still running */
     if( _instance->pdata )
     {
+        VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
+
         switch( index )
         {
             case ID_root_audio:
@@ -115,6 +119,11 @@ RuntimeNPObject::InvokeResult LibvlcRootNPObject::getProperty(int index, NPVaria
                     videoObj = NPN_CreateObject(_instance,RuntimeNPClass<LibvlcVideoNPObject>::getClass());
                 OBJECT_TO_NPVARIANT(NPN_RetainObject(videoObj), result);
                 return INVOKERESULT_NO_ERROR;
+            case ID_root_toolbar:
+                bool b_show_toolbar;
+                b_show_toolbar = p_plugin->getToolbar();
+                BOOLEAN_TO_NPVARIANT(b_show_toolbar, result);
+                return INVOKERESULT_NO_ERROR;
             case ID_root_VersionInfo:
             {
                 int len = strlen(libvlc_get_version());
@@ -137,6 +146,43 @@ RuntimeNPObject::InvokeResult LibvlcRootNPObject::getProperty(int index, NPVaria
     return INVOKERESULT_GENERIC_ERROR;
 }
 
+
+RuntimeNPObject::InvokeResult LibvlcRootNPObject::setProperty(int index, const NPVariant &value)
+{
+    /* is plugin still running */
+    if( _instance->pdata )
+    {
+        VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
+
+        switch( index )
+        {
+            case ID_root_toolbar:
+                if( NPVARIANT_IS_BOOLEAN(value) )
+                {
+#if XP_UNIX
+                    bool b_show;
+                    b_show = NPVARIANT_TO_BOOLEAN(value);
+
+                    if ( b_show )
+                    {
+                        p_plugin->showToolbar();
+                    }
+                    else
+                    {
+                        p_plugin->hideToolbar();
+                    }
+#endif /* XP_UNIX */
+                    return INVOKERESULT_NO_ERROR;
+                }
+                return INVOKERESULT_INVALID_VALUE;
+
+            default:
+                ;
+        }
+    }
+    return INVOKERESULT_GENERIC_ERROR;
+}
+
 const NPUTF8 * const LibvlcRootNPObject::methodNames[] =
 {
     "versionInfo",
@@ -2162,4 +2208,3 @@ RuntimeNPObject::InvokeResult LibvlcVideoNPObject::invoke(int index, const NPVar
     }
     return INVOKERESULT_GENERIC_ERROR;
 }
-
diff --git a/projects/mozilla/control/npolibvlc.h b/projects/mozilla/control/npolibvlc.h
index c471462..b181ed4 100644
--- a/projects/mozilla/control/npolibvlc.h
+++ b/projects/mozilla/control/npolibvlc.h
@@ -46,6 +46,7 @@ protected:
     static const NPUTF8 * const propertyNames[];
 
     InvokeResult getProperty(int index, NPVariant &result);
+    InvokeResult setProperty(int index, const NPVariant &value);
 
     static const int methodCount;
     static const NPUTF8 * const methodNames[];
diff --git a/projects/mozilla/vlcplugin.cpp b/projects/mozilla/vlcplugin.cpp
index c4b1f50..bf80890 100644
--- a/projects/mozilla/vlcplugin.cpp
+++ b/projects/mozilla/vlcplugin.cpp
@@ -59,6 +59,14 @@ VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
     ,i_tb_width(0)
     ,i_tb_height(0)
     ,i_last_position(0)
+    ,p_btnPlay(NULL)
+    ,p_btnPause(NULL)
+    ,p_btnStop(NULL)
+    ,p_btnMute(NULL)
+    ,p_btnUnmute(NULL)
+    ,p_btnFullscreen(NULL)
+    ,p_btnTime(NULL)
+    ,p_timeline(NULL)
 #endif
 {
     memset(&npwindow, 0, sizeof(NPWindow));
@@ -420,75 +428,107 @@ void VlcPlugin::showToolbar()
 {
     const NPWindow& window = getWindow();
     Window control = getControlWindow();
+    Window video = getVideoWindow();
     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
     unsigned int i_height = 0, i_width = 0;
 
     /* load icons */
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
-                        &p_btnPlay, NULL, NULL);
+    if( ! p_btnPlay )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
+                            &p_btnPlay, NULL, NULL);
     if( p_btnPlay )
     {
         i_height = __MAX( i_height, p_btnPlay->height );
         i_width  = __MAX( i_width,  p_btnPlay->width );
     }
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
-                        &p_btnPause, NULL, NULL);
+
+    if( ! p_btnPause )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
+                            &p_btnPause, NULL, NULL);
     if( p_btnPause )
     {
         i_height = __MAX( i_height, p_btnPause->height );
         i_width  = __MAX( i_width,  p_btnPause->width );
     }
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
-                        &p_btnStop, NULL, NULL );
+
+    if( ! p_btnStop )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
+                            &p_btnStop, NULL, NULL );
     if( p_btnStop )
     {
         i_height = __MAX( i_height, p_btnStop->height );
         i_width  = __MAX( i_width,  p_btnStop->width );
     }
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
-                        &p_timeline, NULL, NULL);
+
+    if( ! p_timeline )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
+                            &p_timeline, NULL, NULL);
     if( p_timeline )
     {
         i_height = __MAX( i_height, p_timeline->height );
         i_width  = __MAX( i_width,  p_timeline->width );
     }
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
-                        &p_btnTime, NULL, NULL);
+
+    if( ! p_btnTime )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
+                            &p_btnTime, NULL, NULL);
     if( p_btnTime )
     {
         i_height = __MAX( i_height, p_btnTime->height );
         i_width  = __MAX( i_width,  p_btnTime->width );
     }
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
-                        &p_btnFullscreen, NULL, NULL);
+
+    if( ! p_btnFullscreen  )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
+                            &p_btnFullscreen, NULL, NULL);
     if( p_btnFullscreen )
     {
         i_height = __MAX( i_height, p_btnFullscreen->height );
         i_width  = __MAX( i_width,  p_btnFullscreen->width );
     }
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
-                        &p_btnMute, NULL, NULL);
+
+    if( ! p_btnMute )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
+                            &p_btnMute, NULL, NULL);
     if( p_btnMute )
     {
         i_height = __MAX( i_height, p_btnMute->height );
         i_width  = __MAX( i_width,  p_btnMute->width );
     }
-    XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
-                        &p_btnUnmute, NULL, NULL);
+
+    if( ! p_btnUnmute )
+        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
+                            &p_btnUnmute, NULL, NULL);
     if( p_btnUnmute )
     {
         i_height = __MAX( i_height, p_btnUnmute->height );
         i_width  = __MAX( i_width,  p_btnUnmute->width );
     }
+
     setToolbarSize( i_width, i_height );
 
     if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
         !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
-        fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
+        fprintf( stderr,
+                 "Error: some button images not found in %s\n", DATA_PATH );
+
+
+    /* reset panels position and size */
+    /* XXX  use i_width */
+    XResizeWindow( p_display, video, window.width, window.height - i_height);
+    XMoveWindow( p_display, control, 0, window.height - i_height );
+    XResizeWindow( p_display, control, window.width, i_height -1);
+
+    setToolbar( 1 ); /* says toolbar is now shown */
+    redrawToolbar();
 }
 
 void VlcPlugin::hideToolbar()
 {
+    const NPWindow& window = getWindow();
+    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
+    Window control = getControlWindow();
+    Window video = getVideoWindow();
     i_tb_width = i_tb_height = 0;
 
     if( p_btnPlay )  XDestroyImage( p_btnPlay );
@@ -508,6 +548,16 @@ void VlcPlugin::hideToolbar()
     p_btnFullscreen = NULL;
     p_btnMute = NULL;
     p_btnUnmute = NULL;
+
+
+    /* reset panels position and size */
+    /* XXX  use i_width */
+    XResizeWindow( p_display, video, window.width, window.height );
+    XMoveWindow( p_display, control, 0, window.height-1 );
+    XResizeWindow( p_display, control, window.width, 1 );
+
+    setToolbar( 0 ); /* says toolbar is now hidden */
+    redrawToolbar();
 }
 
 void VlcPlugin::redrawToolbar()
@@ -520,12 +570,21 @@ void VlcPlugin::redrawToolbar()
     unsigned int dst_x, dst_y;
     GC gc;
     XGCValues gcv;
+    unsigned int i_tb_width, i_tb_height;
 #define BTN_SPACE ((unsigned int)4)
 
+    /* This method does nothing if toolbar is hidden. */
+    if ( ! getToolbar() )
+    {
+        return;
+    }
+
     const NPWindow& window = getWindow();
     Window control = getControlWindow();
     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
 
+    getToolbarSize( &i_tb_width, &i_tb_height );
+
     /* get media instance */
     libvlc_exception_init( &ex );
     p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
@@ -560,67 +619,75 @@ void VlcPlugin::redrawToolbar()
 
 
     /* position icons */
-    dst_x = 4; dst_y = 4;
+    dst_x = BTN_SPACE;
+    dst_y = i_tb_height >> 1; /* baseline = vertical middle */
 
     /* "play/pause" button image */
     if( p_btnPause && (i_playing == 1) )
     {
-        XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x, dst_y,
+        XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
+                   dst_y - (p_btnPause->height >> 1),
                    p_btnPause->width, p_btnPause->height );
+
+        dst_x += BTN_SPACE + p_btnPause->width;
     }
     else if( p_btnPlay )
     {
-        XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x, dst_y,
+        XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
+                   dst_y - (p_btnPlay->height >> 1),
                    p_btnPlay->width, p_btnPlay->height );
+
+        dst_x += BTN_SPACE + p_btnPlay->width;
     }
 
-    dst_x += BTN_SPACE + ( p_btnPlay ? p_btnPlay->width : 0 );
-    dst_y = 4;
 
     /* "stop" button image */
     if( p_btnStop )
-        XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x, dst_y,
+        XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
+                   dst_y - (p_btnStop->height >> 1),
                    p_btnStop->width, p_btnStop->height );
 
     dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
-    dst_y = 4;
 
     if( p_btnFullscreen )
-        XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x, dst_y,
+        XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
+                   dst_y - (p_btnFullscreen->height >> 1),
                    p_btnFullscreen->width, p_btnFullscreen->height );
 
     dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
-    dst_y = 4;
 
     /* "mute/unmute" button image */
     if( p_btnUnmute && b_mute )
     {
-        XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x, dst_y,
+        XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
+                   dst_y - (p_btnUnmute->height >> 1),
                    p_btnUnmute->width, p_btnUnmute->height );
 
         dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
-        dst_y = 4;
     }
     else if( p_btnMute )
     {
-        XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x, dst_y,
+        XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
+                   dst_y - (p_btnMute->height >> 1),
                    p_btnMute->width, p_btnMute->height );
 
         dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
-        dst_y = 4;
     }
 
     /* timeline */
     if( p_timeline )
-        XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x, dst_y,
+        XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
+                    dst_y - (p_timeline->height >> 1),
                    (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
 
     if( f_position > 0 )
-        i_last_position = (((float)window.width-8.0)/100.0)*f_position;
+        i_last_position = (int)( f_position *
+                        ( ((float)(window.width-(dst_x+BTN_SPACE))) / 100.0 ));
 
     if( p_btnTime )
         XPutImage( p_display, control, gc, p_btnTime,
-                   0, 0, (dst_x+i_last_position), dst_y,
+                   0, 0, (dst_x+i_last_position),
+                   dst_y - (p_btnTime->height >> 1),
                    p_btnTime->width, p_btnTime->height );
 
     XFreeGC( p_display, gc );
diff --git a/projects/mozilla/vlcplugin.h b/projects/mozilla/vlcplugin.h
index 4886aec..a2cb03b 100644
--- a/projects/mozilla/vlcplugin.h
+++ b/projects/mozilla/vlcplugin.h
@@ -117,12 +117,15 @@ public:
                             { i_tb_width = width; i_tb_height = height; return 1; };
 #endif
 
+    /** Get flag which indicate wether toolbar is shown */
+    inline int          getToolbar()
+                            { return b_toolbar; };
+
     uint16    i_npmode; /* either NP_EMBED or NP_FULL */
 
     /* plugin properties */
     int      b_stream;
     int      b_autoplay;
-    int      b_toolbar;
     char *   psz_target;
 
 private:
@@ -131,6 +134,9 @@ private:
     libvlc_log_t        *libvlc_log;
     NPClass             *p_scriptClass;
 
+    /* plugin properties */
+    int      b_toolbar;
+
     /* browser reference */
     NPP     p_browser;
     char*   psz_baseURL;
@@ -155,6 +161,14 @@ private:
     XImage *p_btnUnmute;
 
     int i_last_position;
+
+    /**
+     * Set flag which indicate wether toolbar is shown or not.
+     *
+     * It should be called by showToolbar() and hideToolbar()
+     */
+    inline void         setToolbar(int b_show_toolbar)
+                            { b_toolbar = b_show_toolbar; };
 #endif
 };
 
diff --git a/projects/mozilla/vlcshell.cpp b/projects/mozilla/vlcshell.cpp
index ebde8d9..75b624f 100644
--- a/projects/mozilla/vlcshell.cpp
+++ b/projects/mozilla/vlcshell.cpp
@@ -491,18 +491,15 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
 #endif /* XP_WIN */
 
 #ifdef XP_UNIX
-    if( p_plugin->b_toolbar )
-    {
-        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
-    }
-    else
-    {
-        i_control_height = i_control_width = 0;
-    }
+    /* default to hidden toolbar, shown at the end of this method if asked */
+    /* developers note : getToolbarSize need to wait the end of this method */
+    i_control_height = 0;
+    i_control_width = window->width;
+
 
     if( window && window->window )
     {
-        Window  parent   = (Window) window->window;
+        Window  parent = (Window) window->window;
         if( !curwin.window || (parent != (Window)curwin.window) )
         {
             Display *p_display = ( (NPSetWindowCallbackStruct *)
@@ -517,13 +514,12 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
                            window->width, window->height - i_control_height,
                            0, i_blackColor, i_blackColor );
             Window controls = (Window) NULL;
-            if( p_plugin->b_toolbar )
-            {
-                controls = XCreateSimpleWindow( p_display, parent,
-                                0, window->height - i_control_height-1,
-                                window->width, i_control_height-1,
-                                0, i_blackColor, i_blackColor );
-            }
+
+            controls = XCreateSimpleWindow( p_display, parent,
+                            0, window->height - i_control_height-1,
+                            window->width, i_control_height-1,
+                            0, i_blackColor, i_blackColor );
+
 
             XMapWindow( p_display, parent );
             XMapWindow( p_display, video );
@@ -562,6 +558,12 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
             if( controls ) { p_plugin->setControlWindow( controls ); }
 
             Redraw( w, (XtPointer)p_plugin, NULL );
+
+            /* now display toolbar if asked through parameters */
+            if( p_plugin->getToolbar() )
+            {
+                p_plugin->showToolbar();
+            }
         }
     }
     else if ( curwin.window )
@@ -802,11 +804,8 @@ static void Redraw( Widget w, XtPointer closure, XEvent *event )
     XGCValues gcv;
     unsigned int i_control_height, i_control_width;
 
-    if( p_plugin->b_toolbar )
-    {
-        p_plugin->showToolbar();
+    if( p_plugin->getToolbar() )
         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
-    }
     else
         i_control_height = i_control_width = 0;
 
@@ -827,11 +826,7 @@ static void Redraw( Widget w, XtPointer closure, XEvent *event )
                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
     XFreeGC( p_display, gc );
 
-    if( p_plugin->b_toolbar )
-    {
-        p_plugin->redrawToolbar();
-        p_plugin->hideToolbar();
-    }
+    p_plugin->redrawToolbar();
 }
 
 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
@@ -844,7 +839,7 @@ static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
     int i_xPos = event->xbutton.x;
     int i_yPos = event->xbutton.y;
 
-    if( p_plugin && p_plugin->b_toolbar )
+    if( p_plugin && p_plugin->getToolbar() )
     {
         libvlc_exception_t ex;
         libvlc_exception_init( &ex );
@@ -852,6 +847,7 @@ static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
                 libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
         libvlc_exception_clear( &ex );
 
+
         /* jump in the movie */
         if( i_yPos <= (i_height-30) )
         {
@@ -939,7 +935,7 @@ static void Resize ( Widget w, XtPointer closure, XEvent *event )
     unsigned int i_nchildren;
     unsigned int i_control_height, i_control_width;
 
-    if( p_plugin->b_toolbar )
+    if( p_plugin->getToolbar() )
     {
         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
     }
-- 
1.5.4.2


--=-gN8rmrPxLMYcIphhtM/6
Content-Disposition: attachment; filename=0003-fix-moz-toolbar-play-pause.patch
Content-Type: application/mbox; name=0003-fix-moz-toolbar-play-pause.patch
Content-Transfer-Encoding: 7bit



More information about the vlc-devel mailing list