Mozilla: fix compilation on OSX, where we don't use libX11
[vlc.git] / projects / mozilla / vlcshell.cpp
index 66a73e5..de0011a 100644 (file)
@@ -94,7 +94,8 @@ NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
             return NPERR_NO_ERROR;
 
         case NPPVpluginDescriptionString:
-            snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION, VLC_Version() );
+            snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
+                      VLC_Version() );
             *((char **)value) = psz_desc;
             return NPERR_NO_ERROR;
 
@@ -220,7 +221,8 @@ int16 NPP_HandleEvent( NPP instance, void * event )
                             libvlc_playlist_get_media_instance(p_vlc, NULL);
                         if( p_md )
                         {
-                            hasVout = libvlc_media_instance_has_vout(p_md, NULL);
+                            hasVout = libvlc_media_instance_has_vout(p_md,
+                                                                     NULL);
                             if( hasVout )
                             {
                                 libvlc_rectangle_t area;
@@ -228,7 +230,8 @@ int16 NPP_HandleEvent( NPP instance, void * event )
                                 area.top = 0;
                                 area.right = npwindow.width;
                                 area.bottom = npwindow.height;
-                                libvlc_video_redraw_rectangle(p_md, &area, NULL);
+                                libvlc_video_redraw_rectangle(p_md, &area,
+                                                              NULL);
                             }
                             libvlc_media_instance_release(p_md);
                         }
@@ -242,7 +245,8 @@ int16 NPP_HandleEvent( NPP instance, void * event )
                     ForeColor(blackColor);
                     PenMode( patCopy );
 
-                    /* seems that firefox forgets to set the following on occasion (reload) */
+                    /* seems that firefox forgets to set the following
+                     * on occasion (reload) */
                     SetOrigin(((NP_Port *)npwindow.window)->portx,
                               ((NP_Port *)npwindow.window)->porty);
 
@@ -360,6 +364,11 @@ NPError NPP_Destroy( NPP instance, NPSavedData** save )
 
 NPError NPP_SetWindow( NPP instance, NPWindow* window )
 {
+#ifdef XP_UNIX && !defined(__APPLE__)
+    Window control;
+    unsigned int i_control_height = 0, i_control_width = 0;
+#endif
+
     if( ! instance )
     {
         return NPERR_INVALID_INSTANCE_ERROR;
@@ -367,12 +376,14 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
 
     /* NPP_SetWindow may be called before NPP_New (Opera) */
     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
-    if( ! p_plugin )
+    if( NULL == p_plugin )
     {
         /* we should probably show a splash screen here */
         return NPERR_NO_ERROR;
     }
-
+#ifdef XP_UNIX && !defined(__APPLE__)
+    control = p_plugin->getControlWindow();
+#endif
     libvlc_instance_t *p_vlc = p_plugin->getVLC();
 
     /*
@@ -401,8 +412,9 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
         libvlc_rectangle_t view, clip;
 
         /*
-        ** browser sets port origin to top-left location of plugin relative to GrafPort
-        ** window origin is set relative to document, which of little use for drawing
+        ** browser sets port origin to top-left location of plugin
+        ** relative to GrafPort window origin is set relative to document,
+        ** which of little use for drawing
         */
         view.top     = ((NP_Port*) (window->window))->porty;
         view.left    = ((NP_Port*) (window->window))->portx;
@@ -478,30 +490,47 @@ 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;
+    }
+
     if( window && window->window )
     {
         Window  parent   = (Window) window->window;
         if( !curwin.window || (parent != (Window)curwin.window) )
         {
-            Display *p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
+            Display *p_display = ( (NPSetWindowCallbackStruct *)
+                                   window->ws_info )->display;
 
             XResizeWindow( p_display, parent, window->width, window->height );
 
             int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));
 
+            /* create windows */
             Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
-                           window->width, window->height - p_plugin->i_control_height, 0,
-                           i_blackColor, i_blackColor );
-            Window controls = XCreateSimpleWindow( p_display, parent, 0,
-                            window->height - p_plugin->i_control_height-1, window->width,
-                            p_plugin->i_control_height-1, 0, i_blackColor, i_blackColor );
+                           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 );
+            }
 
             XMapWindow( p_display, parent );
             XMapWindow( p_display, video );
-            XMapWindow( p_display, controls );
+            if( controls ) { XMapWindow( p_display, controls ); }
 
             XFlush(p_display);
 
+            /* bind events */
             Widget w = XtWindowToWidget( p_display, parent );
 
             XtAddEventHandler( w, ExposureMask, FALSE,
@@ -529,7 +558,7 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
             /* remember window */
             p_plugin->setWindow( *window );
             p_plugin->setVideoWindow( video );
-            p_plugin->setControlWindow( controls );
+            if( controls ) { p_plugin->setControlWindow( controls ); }
 
             Redraw( w, (XtPointer)p_plugin, NULL );
         }
@@ -628,7 +657,8 @@ void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
         return;
     }
 
-    if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL ) != -1 )
+    if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL )
+        != -1 )
     {
         if( p_plugin->b_autoplay )
         {
@@ -750,7 +780,8 @@ static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpa
         }
         default:
             /* delegate to default handler */
-            return CallWindowProc(p_plugin->getWindowProc(), p_hwnd, i_msg, wpar, lpar );
+            return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
+                                   i_msg, wpar, lpar );
     }
 }
 #endif /* XP_WIN */
@@ -762,32 +793,42 @@ static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpa
 static void Redraw( Widget w, XtPointer closure, XEvent *event )
 {
     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
+    Window control = p_plugin->getControlWindow();
     const NPWindow& window = p_plugin->getWindow();
     GC gc;
     XGCValues gcv;
+    unsigned int i_control_height, i_control_width;
+
+    if( p_plugin->b_toolbar )
+    {
+        p_plugin->showToolbar();
+        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
+    }
+    else
+        i_control_height = i_control_width = 0;
 
     Window video = p_plugin->getVideoWindow();
     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
 
-    p_plugin->showToolbar();
-
     gcv.foreground = BlackPixel( p_display, 0 );
     gc = XCreateGC( p_display, video, GCForeground, &gcv );
 
     XFillRectangle( p_display, video, gc,
-                    0, 0, window.width, window.height - p_plugin->i_control_height );
+                    0, 0, window.width, window.height - i_control_height);
 
     gcv.foreground = WhitePixel( p_display, 0 );
     XChangeGC( p_display, gc, GCForeground, &gcv );
 
     XDrawString( p_display, video, gc,
-                 window.width / 2 - 40, (window.height - p_plugin->i_control_height) / 2,
+                 window.width / 2 - 40, (window.height - i_control_height) / 2,
                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
     XFreeGC( p_display, gc );
 
-    p_plugin->redrawToolbar();
-
-    p_plugin->hideToolbar();
+    if( p_plugin->b_toolbar )
+    {
+        p_plugin->redrawToolbar();
+        p_plugin->hideToolbar();
+    }
 }
 
 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
@@ -800,7 +841,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 )
+    if( p_plugin && p_plugin->b_toolbar )
     {
         libvlc_exception_t ex;
         libvlc_exception_init( &ex );
@@ -811,17 +852,21 @@ static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
         /* jump in the movie */
         if( i_yPos <= (i_height-30) )
         {
-            vlc_int64_t f_length;
-            libvlc_exception_init( &ex );
-            f_length = libvlc_media_instance_get_length( p_md, &ex ) / 100;
-            libvlc_exception_clear( &ex );
+            /* if a movie is loaded */
+            if( p_md )
+            {
+                vlc_int64_t f_length;
+                libvlc_exception_init( &ex );
+                f_length = libvlc_media_instance_get_length( p_md, &ex ) / 100;
+                libvlc_exception_clear( &ex );
 
-            f_length = (float)f_length *
-                    ( ((float)i_xPos-4 ) / ( ((float)i_width-8)/100) );
+                f_length = (float)f_length *
+                           ( ((float)i_xPos-4 ) / ( ((float)i_width-8)/100) );
 
-            libvlc_exception_init( &ex );
-            libvlc_media_instance_set_time( p_md, f_length, &ex );
-            libvlc_exception_clear( &ex );
+                libvlc_exception_init( &ex );
+                libvlc_media_instance_set_time( p_md, f_length, &ex );
+                libvlc_exception_clear( &ex );
+            }
         }
 
         /* play/pause toggle */
@@ -856,7 +901,7 @@ static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
             i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
             libvlc_exception_clear( &ex );
 
-            if( i_playing == 1 )
+            if( (i_playing == 1) && p_md )
             {
                 libvlc_exception_init( &ex );
                 libvlc_set_fullscreen( p_md, 1, &ex );
@@ -871,7 +916,8 @@ static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
             libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
             libvlc_exception_clear( &ex );
         }
-        libvlc_media_instance_release( p_md );
+
+        if( p_md ) libvlc_media_instance_release( p_md );
     }
     Redraw( w, closure, event );
 }
@@ -879,6 +925,7 @@ static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
 static void Resize ( Widget w, XtPointer closure, XEvent *event )
 {
     VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
+    Window control = p_plugin->getControlWindow();
     const NPWindow& window = p_plugin->getWindow();
     Window  drawable   = p_plugin->getVideoWindow();
     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
@@ -887,6 +934,16 @@ static void Resize ( Widget w, XtPointer closure, XEvent *event )
     Window root_return, parent_return, * children_return;
     Window base_window;
     unsigned int i_nchildren;
+    unsigned int i_control_height, i_control_width;
+
+    if( p_plugin->b_toolbar )
+    {
+        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
+    }
+    else
+    {
+        i_control_height = i_control_width = 0;
+    }
 
 #ifdef X11_RESIZE_DEBUG
     XWindowAttributes attr;
@@ -900,13 +957,14 @@ static void Resize ( Widget w, XtPointer closure, XEvent *event )
     }
 #endif /* X11_RESIZE_DEBUG */
 
-    if( ! p_plugin->setSize(window.width, (window.height - p_plugin->i_control_height)) )
+    if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
     {
         /* size already set */
         return;
     }
 
-    i_ret = XResizeWindow( p_display, drawable, window.width, (window.height - p_plugin->i_control_height) );
+    i_ret = XResizeWindow( p_display, drawable,
+                           window.width, (window.height - i_control_height) );
 
 #ifdef X11_RESIZE_DEBUG
     fprintf( stderr,
@@ -938,7 +996,7 @@ static void Resize ( Widget w, XtPointer closure, XEvent *event )
 #endif /* X11_RESIZE_DEBUG */
 
         i_ret = XResizeWindow( p_display, base_window,
-                window.width, ( window.height - p_plugin->i_control_height ) );
+                window.width, ( window.height - i_control_height ) );
 
 #ifdef X11_RESIZE_DEBUG
         fprintf( stderr,