doc.cpp

来自「Wxpython Implemented on Windows CE, Sou」· C++ 代码 · 共 623 行 · 第 1/2 页

CPP
623
字号
/////////////////////////////////////////////////////////////////////////////
// Name:        contrib/samples/ogl/ogledit/doc.cpp
// Purpose:     Implements document functionality in OGLEdit
// Author:      Julian Smart
// Modified by:
// Created:     12/07/98
// RCS-ID:      $Id: doc.cpp,v 1.18 2006/04/18 22:26:23 PC Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "wx/ioswrap.h"

#if !wxUSE_DOC_VIEW_ARCHITECTURE
#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
#endif

#include "ogledit.h"
#include "doc.h"
#include "view.h"

IMPLEMENT_DYNAMIC_CLASS(DiagramDocument, wxDocument)

DiagramDocument::DiagramDocument(void)
{
}

DiagramDocument::~DiagramDocument(void)
{
}

bool DiagramDocument::OnCloseDocument(void)
{
    diagram.DeleteAllShapes();
    return true;
}

#if wxUSE_STD_IOSTREAM
wxSTD ostream& DiagramDocument::SaveObject(wxSTD ostream& stream)
{
#if wxUSE_PROLOGIO

    wxDocument::SaveObject(stream);

    char buf[400];
    (void) wxGetTempFileName("diag", buf);

    diagram.SaveFile(buf);
    wxTransferFileToStream(buf, stream);

    wxRemoveFile(buf);

#endif

    return stream;
}

wxSTD istream& DiagramDocument::LoadObject(wxSTD istream& stream)
{
#if wxUSE_PROLOGIO

    wxDocument::LoadObject(stream);

    char buf[400];
    (void) wxGetTempFileName("diag", buf);

    wxTransferStreamToFile(stream, buf);

    diagram.DeleteAllShapes();
    diagram.LoadFile(buf);
    wxRemoveFile(buf);

#endif

    return stream;
}
#else

wxOutputStream& DiagramDocument::SaveObject(wxOutputStream& stream)
{
#if wxUSE_PROLOGIO

    wxDocument::SaveObject(stream);
    wxChar buf[400];
    (void) wxGetTempFileName(_T("diag"), buf);

    diagram.SaveFile(buf);

    wxTransferFileToStream(buf, stream);

    wxRemoveFile(buf);

#endif

    return stream;
}

wxInputStream& DiagramDocument::LoadObject(wxInputStream& stream)
{
#if wxUSE_PROLOGIO
    wxDocument::LoadObject(stream);

    wxChar buf[400];
    (void) wxGetTempFileName(_T("diag"), buf);

    wxTransferStreamToFile(stream, buf);

    diagram.DeleteAllShapes();
    diagram.LoadFile(buf);
    wxRemoveFile(buf);
#endif

    return stream;
}

#endif

/*
 * Implementation of drawing command
 */

DiagramCommand::DiagramCommand(const wxString& name, int command, DiagramDocument *ddoc, wxClassInfo *info, double xx, double yy,
                               bool sel, wxShape *theShape, wxShape *fs, wxShape *ts)
               :wxCommand(true, name)
{
    doc = ddoc;
    cmd = command;
    shape = theShape;
    fromShape = fs;
    toShape = ts;
    shapeInfo = info;
    shapeBrush = NULL;
    shapePen = NULL;
    x = xx;
    y = yy;
    selected = sel;
    deleteShape = false;
}

DiagramCommand::DiagramCommand(const wxString& name, int command, DiagramDocument *ddoc, wxBrush *backgroundColour, wxShape *theShape)
               :wxCommand(true, name)
{
    doc = ddoc;
    cmd = command;
    shape = theShape;
    fromShape = NULL;
    toShape = NULL;
    shapeInfo = NULL;
    x = 0.0;
    y = 0.0;
    selected = false;
    deleteShape = false;
    shapeBrush = backgroundColour;
    shapePen = NULL;
}

DiagramCommand::DiagramCommand(const wxString& name, int command, DiagramDocument *ddoc, const wxString& lab, wxShape *theShape)
               :wxCommand(true, name)
{
    doc = ddoc;
    cmd = command;
    shape = theShape;
    fromShape = NULL;
    toShape = NULL;
    shapeInfo = NULL;
    x = 0.0;
    y = 0.0;
    selected = false;
    deleteShape = false;
    shapeBrush = NULL;
    shapePen = NULL;
    shapeLabel = lab;
}

DiagramCommand::~DiagramCommand(void)
{
  if (shape && deleteShape)
  {
    shape->SetCanvas(NULL);
    delete shape;
  }
}

bool DiagramCommand::Do(void)
{
  switch (cmd)
  {
    case wxID_CUT:
    {
      if (shape)
      {
        deleteShape = true;

        shape->Select(false);

        // Generate commands to explicitly remove each connected line.
        RemoveLines(shape);

        doc->GetDiagram()->RemoveShape(shape);
        if (shape->IsKindOf(CLASSINFO(wxLineShape)))
        {
          wxLineShape *lineShape = (wxLineShape *)shape;
          fromShape = lineShape->GetFrom();
          toShape = lineShape->GetTo();
        }
        shape->Unlink();

        doc->Modify(true);
        doc->UpdateAllViews();
      }

      break;
    }
    case OGLEDIT_ADD_SHAPE:
    {
      wxShape *theShape;
      if (shape)
        theShape = shape; // Saved from undoing the shape
      else
      {
        theShape = (wxShape *)shapeInfo->CreateObject();
        theShape->AssignNewIds();
        theShape->SetEventHandler(new MyEvtHandler(theShape, theShape, wxEmptyString));
        theShape->SetCentreResize(false);
        theShape->SetPen(wxBLACK_PEN);
        theShape->SetBrush(wxCYAN_BRUSH);

        theShape->SetSize(60, 60);
      }
      doc->GetDiagram()->AddShape(theShape);
      theShape->Show(true);

      wxClientDC dc(theShape->GetCanvas());
      theShape->GetCanvas()->PrepareDC(dc);

      theShape->Move(dc, x, y);

      shape = theShape;
      deleteShape = false;

      doc->Modify(true);
      doc->UpdateAllViews();
      break;
    }
    case OGLEDIT_ADD_LINE:
    {
      wxShape *theShape;
      if (shape)
        theShape = shape; // Saved from undoing the line
      else
      {
        theShape = (wxShape *)shapeInfo->CreateObject();
        theShape->AssignNewIds();
        theShape->SetEventHandler(new MyEvtHandler(theShape, theShape, wxEmptyString));
        theShape->SetPen(wxBLACK_PEN);
        theShape->SetBrush(wxRED_BRUSH);

        wxLineShape *lineShape = (wxLineShape *)theShape;

        // Yes, you can have more than 2 control points, in which case
        // it becomes a multi-segment line.
        lineShape->MakeLineControlPoints(2);
        lineShape->AddArrow(ARROW_ARROW, ARROW_POSITION_END, 10.0, 0.0, _T("Normal arrowhead"));
      }

      doc->GetDiagram()->AddShape(theShape);

      fromShape->AddLine((wxLineShape *)theShape, toShape);

      theShape->Show(true);

      wxClientDC dc(theShape->GetCanvas());
      theShape->GetCanvas()->PrepareDC(dc);

      // It won't get drawn properly unless you move both
      // connected images
      fromShape->Move(dc, fromShape->GetX(), fromShape->GetY());
      toShape->Move(dc, toShape->GetX(), toShape->GetY());

      shape = theShape;
      deleteShape = false;

      doc->Modify(true);
      doc->UpdateAllViews();
      break;
    }
    case OGLEDIT_CHANGE_BACKGROUND_COLOUR:
    {
      if (shape)
      {
        wxClientDC dc(shape->GetCanvas());
        shape->GetCanvas()->PrepareDC(dc);

        const wxBrush *oldBrush = shape->GetBrush();
        shape->SetBrush(shapeBrush);
        shapeBrush = oldBrush;
        shape->Draw(dc);

        doc->Modify(true);
        doc->UpdateAllViews();
      }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?