User talk:Jiez

From MozillaWiki
Jump to navigation Jump to search

Using embeded API in wxWidget application

Setup system environment

Please refer to https://wiki.mozilla.org/Embedding/NewApi/Win32

Extended wxWidgets using embeded API

Create an new Class wxMozView inheriting wxWindows

Header file wxMozView.h


 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The code follows the example of embeded API QT project.
 * Contact: Jiez <MagicDesign.CN@Gmail.Com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
#ifndef _EXTERN_WXWIDGET_EMBEDED_MOZILLA_WXMOZVIE_H_
#define _EXTERN_WXWIDGET_EMBEDED_MOZILLA_WXMOZVIE_H_

#include "embed.h"
#include <wx/control.h>
#include <wx/string.h>

class wxMozViewListener;
class nsIInterfaceRequestor;
/////////////////////////////////////////////////////
///Class wxMozView
/////////////////////////////////////////////////////
class wxMozView :
      public wxControl
{
public:
    explicit wxMozView(wxWindow* parent = NULL,
                       wxWindowID id = wxID_ANY, 
                       const wxPoint& pos = wxDefaultPosition, 
                       const wxSize& size = wxDefaultSize,  
                       long style = 0);
    virtual ~wxMozView();

    void LoadUri(const wxString& uri);
    void GetInterfaceRequestor(nsIInterfaceRequestor** aRequestor);
    wxString EvaluateJavaScript(const wxString& script);

    void GetSize(int *width, int *height);

protected:
    virtual void OnSize(wxSizeEvent& e);

    virtual wxMozView* OpenWindow(unsigned int flags);


private:
    //here I am using the QT way to create the private members for the class wxMozView.
    //It is really a good way to hide private info of a class. :)
    //I love QT!
    class Private;
    Private * const m_private;

    friend class wxMozViewListener;

};

/////////////////////////////////////////////////////
///Class wxMozViewListener
/////////////////////////////////////////////////////
class wxMozViewListener :
      public MozViewListener
{
public:
    wxMozViewListener(wxMozView* awxMozView) : pwxMozView(awxMozView) {}
    virtual ~wxMozViewListener() {}

    void StatusChanged(const char* newStatus, PRUint32 statusType);
    void LocationChanged(const char* newLocation);
    MozView* OpenWindow(PRUint32 flags);
    void SizeTo(PRUint32 width, PRUint32 height);

private:
    wxMozView* pwxMozView;

};

#endif // _EXTERN_WXWIDGET_EMBEDED_MOZILLA_WXMOZVIE_H_

File wxMozView.cpp


#include "wxMozView.h"

///////////////////////////////////////////
//Class Private
class wxMozView::Private
{
public:
    Private(wxMozView* awxMozView) :
        listener(awxMozView)
    {
        mozView.SetListener(&listener);
    }

    MozView mozView;
    wxMozViewListener listener;
    wxSize preferredSize;
};

////////////////////////////////////////////
//Implement class wxMozViewListener
void wxMozViewListener::StatusChanged(const char* newStatus, PRUint32 statusType)
{
    //add your own code to deal with the status changed events
    pwxMozView->Refresh();
}

void wxMozViewListener::LocationChanged(const char* newLocation)
{
    //add your own code to deal with the location changed events
    pwxMozView->Refresh();
}

MozView* wxMozViewListener::OpenWindow(PRUint32 flags)
{
    wxMozView* newwxMozView = pwxMozView->OpenWindow(flags);
    if(!newwxMozView)
        return 0;
    return &(newwxMozView->m_private->mozView);
}

void wxMozViewListener::SizeTo(PRUint32 width, PRUint32 height)
{
    pwxMozView->m_private->preferredSize = wxSize(width, height);
    pwxMozView->Update();
    pwxMozView->GetParent()->Fit();
}
///////////////////////////////////////////////////
//Implement class wxMozView
wxMozView::wxMozView(wxWindow* parent, 
                     wxWindowID id, 
                     const wxPoint& pos, 
                     const wxSize& size, 
                     long style) :
    wxControl(parent, id, pos, size, style),
    m_private(new Private(this))
{
    wxControl::wxWindow::Show(false);
    m_private->mozView.CreateBrowser(
                      (void*)parent->GetHandle(), 
                      parent->GetRect().GetLeft(), 
                      parent->GetRect().GetTop(), 
                      parent->GetRect().GetRight() - parent->GetRect().GetLeft(), 
                      parent->GetRect().GetBottom() - parent->GetRect().GetTop(), 
                      1);
}

wxMozView::~wxMozView()
{
    delete m_private;
}

void wxMozView::OnSize(wxSizeEvent& e)
{
    int width = 0, height = 0;
    GetSize(&width, &height);
    m_private->mozView.SetPositionAndSize(0, 0, width, height);
}

void wxMozView::LoadUri(const wxString &uri)
{
    m_private->mozView.LoadURI(uri.char_str(wxMBConvUTF8()));
}

void wxMozView::GetInterfaceRequestor(nsIInterfaceRequestor** aRequestor)
{
    *aRequestor = 0;
    m_private->mozView.GetInterfaceRequestor(aRequestor);
}

wxString wxMozView::EvaluateJavaScript(const wxString& script)
{
    return wxString::FromUTF8(m_private->mozView.EvaluateJavaScript(script.char_str(wxMBConvUTF8())));
}

wxMozView* wxMozView::OpenWindow(unsigned int flags)
{
    return 0;
}

void wxMozView::GetSize(int *width, int *height)
{
    *width = m_private->preferredSize.GetWidth();
    *height = m_private->preferredSize.GetHeight();
}

//
// (C) 2009 Copyrights
// All rights opened under MPL/GPL