WxWidgetsDocumentView

#ifndef _TUTORIALS_WXWIDGETS_WXDOCVIEW1APP_H_
#define _TUTORIALS_WXWIDGETS_WXDOCVIEW1APP_H_

#include <wx/app.h>
#include <wx/docview.h>

class WxDocView1App : public wxApp
{
public:
    WxDocView1App();

    virtual bool OnInit();
    virtual int OnExit();

private:
    wxDocManager* m_docManager;
};

DECLARE_APP(WxDocView1App)

#endif
#include "WxDocView1App.h"
#include "WxDocView1Frame.h"
#include "WxDocView1Document.h"
#include "WxDocView1View.h"

wxIMPLEMENT_APP(WxDocView1App);

WxDocView1App::WxDocView1App()
    : m_docManager(0)
{
}

bool WxDocView1App::OnInit()
{
    m_docManager = new wxDocManager();

    new wxDocTemplate(m_docManager, "WxDocView1", "*.wxdv1", "", "wxdv1", 
        "WxDocView1 Document", "WxDocView1 View", wxCLASSINFO(WxDocView1Document),
        wxCLASSINFO(WxDocView1View));

    m_docManager->SetMaxDocsOpen(1);
    
    WxDocView1Frame* frame = new WxDocView1Frame(m_docManager, "WxDocView1");
    frame->Show(true);
    return true;
}

int WxDocView1App::OnExit()
{
    delete m_docManager;
    return 0;
}
#ifndef _TUTORIALS_WXWIDGETS_WXDOCVIEW1FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXDOCVIEW1FRAME_H_

#include <wx/docview.h>

class WxDocView1Frame : public wxDocParentFrame
{
public:
    WxDocView1Frame(wxDocManager* manager, const wxString& title);

private:
    void OnPaint(wxPaintEvent& evt);

    wxDECLARE_EVENT_TABLE();
};

#endif
#include "WxDocView1Frame.h"
#include <wx/menu.h>
#include <wx/dcclient.h>

WxDocView1Frame::WxDocView1Frame(wxDocManager* manager,
                                 const wxString& title)
    : wxDocParentFrame(manager, NULL, wxID_ANY, title)
{
    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* menuFile = new wxMenu;
    menuFile->Append(wxID_NEW);
    menuFile->Append(wxID_CLOSE);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    menuBar->Append(menuFile, "&File");
    SetMenuBar(menuBar);
}

void WxDocView1Frame::OnPaint(wxPaintEvent& evt)
{
    wxPaintDC dc(this);

    wxView* view = GetDocumentManager()->GetCurrentView();
    if (view)
    {
        view->OnDraw(&dc);
    }
}

wxBEGIN_EVENT_TABLE(WxDocView1Frame, wxDocParentFrame)
    EVT_PAINT(WxDocView1Frame::OnPaint)
wxEND_EVENT_TABLE()
#ifndef _TUTORIALS_WXWIDGETS_WXDOCVIEW1DOCUMENT_H_
#define _TUTORIALS_WXWIDGETS_WXDOCVIEW1DOCUMENT_H_

#include <wx/docview.h>

class WxDocView1Document : public wxDocument
{
    DECLARE_DYNAMIC_CLASS(WxDocView1Document)
};

#endif
#include "WxDocView1Document.h"

IMPLEMENT_DYNAMIC_CLASS(WxDocView1Document, wxDocument)
#ifndef _TUTORIALS_WXWIDGETS_WXDOCVIEW1VIEW_H_
#define _TUTORIALS_WXWIDGETS_WXDOCVIEW1VIEW_H_

#include <wx/docview.h>

class WxDocView1View : public wxView
{
    DECLARE_DYNAMIC_CLASS(WxDocView1View)

public:
    virtual void OnActivateView(bool activate, wxView *activeView,
        wxView *deactiveView);
    virtual void OnDraw(wxDC* dc);
    virtual bool OnCreate(wxDocument* doc, long flags);
    virtual bool OnClose(bool deleteWindow);
};

#endif
#include "WxDocView1View.h"
#include "WxDocView1App.h"
#include "WxDocView1Frame.h"

IMPLEMENT_DYNAMIC_CLASS(WxDocView1View, wxView)

void WxDocView1View::OnActivateView(bool activate,
    wxView *activeView,
    wxView *deactiveView)
{
    WxDocView1Frame* frame = (WxDocView1Frame*)GetFrame();
    if (frame)
    {
        frame->Refresh();
    }
}

void WxDocView1View::OnDraw(wxDC* dc)
{
    wxDocument* doc = GetDocument();
    if (doc)
    {
        dc->SetTextForeground(*wxBLACK);
        std::string greeting = "Geetings from " + doc->GetTitle();
        dc->DrawText(greeting, 10, 10);
    }
}

bool WxDocView1View::OnCreate(wxDocument* doc, long flags)
{
    WxDocView1Frame* frame = (WxDocView1Frame*)wxTheApp->GetTopWindow();
    SetFrame(frame);
    Activate(true);
    return true;
}

bool WxDocView1View::OnClose(bool deleteWindow)
{
    WxDocView1Frame* frame = (WxDocView1Frame*)GetFrame();
    if (frame)
    {
        frame->SetTitle(wxTheApp->GetAppName());
    }
    Activate(false);
    return true;
}
TODO: second example TODO: 2 AuiNotebook and docview examples in CodePlex

Copyright(c) 2006-2017 Xavier Leclercq | Privacy policy

Home
Contact Us
Search