📄 canvas.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: canvas.cpp
// Purpose: Shape canvas class
// Author: Julian Smart
// Modified by:
// Created: 12/07/98
// RCS-ID: $Id: canvas.cpp,v 1.18 2005/10/06 18:17:21 ABX 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
#if wxUSE_PROLOGIO
#include "wx/deprecated/wxexpr.h"
#endif
#ifdef new
#undef new
#endif
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include "wx/ogl/ogl.h"
#define CONTROL_POINT_SIZE 6
// Control point types
// Rectangle and most other shapes
#define CONTROL_POINT_VERTICAL 1
#define CONTROL_POINT_HORIZONTAL 2
#define CONTROL_POINT_DIAGONAL 3
// Line
#define CONTROL_POINT_ENDPOINT_TO 4
#define CONTROL_POINT_ENDPOINT_FROM 5
#define CONTROL_POINT_LINE 6
IMPLEMENT_DYNAMIC_CLASS(wxShapeCanvas, wxScrolledWindow)
BEGIN_EVENT_TABLE(wxShapeCanvas, wxScrolledWindow)
EVT_PAINT(wxShapeCanvas::OnPaint)
EVT_MOUSE_EVENTS(wxShapeCanvas::OnMouseEvent)
END_EVENT_TABLE()
const wxChar* wxShapeCanvasNameStr = wxT("shapeCanvas");
// Object canvas
wxShapeCanvas::wxShapeCanvas(wxWindow *parent, wxWindowID id,
const wxPoint& pos,
const wxSize& size,
long style,
const wxString& name):
wxScrolledWindow(parent, id, pos, size, style, name)
{
m_shapeDiagram = NULL;
m_dragState = NoDragging;
m_draggedShape = NULL;
m_oldDragX = 0;
m_oldDragY = 0;
m_firstDragX = 0;
m_firstDragY = 0;
m_checkTolerance = true;
}
wxShapeCanvas::~wxShapeCanvas()
{
}
void wxShapeCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
{
wxPaintDC dc(this);
PrepareDC(dc);
dc.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
dc.Clear();
if (GetDiagram())
GetDiagram()->Redraw(dc);
}
void wxShapeCanvas::OnMouseEvent(wxMouseEvent& event)
{
wxClientDC dc(this);
PrepareDC(dc);
wxPoint logPos(event.GetLogicalPosition(dc));
double x, y;
x = (double) logPos.x;
y = (double) logPos.y;
int keys = 0;
if (event.ShiftDown())
keys = keys | KEY_SHIFT;
if (event.ControlDown())
keys = keys | KEY_CTRL;
bool dragging = event.Dragging();
// Check if we're within the tolerance for mouse movements.
// If we're very close to the position we started dragging
// from, this may not be an intentional drag at all.
if (dragging)
{
int dx = abs(dc.LogicalToDeviceX((long) (x - m_firstDragX)));
int dy = abs(dc.LogicalToDeviceY((long) (y - m_firstDragY)));
if (m_checkTolerance && (dx <= GetDiagram()->GetMouseTolerance()) && (dy <= GetDiagram()->GetMouseTolerance()))
{
return;
}
else
// If we've ignored the tolerance once, then ALWAYS ignore
// tolerance in this drag, even if we come back within
// the tolerance range.
m_checkTolerance = false;
}
// Dragging - note that the effect of dragging is left entirely up
// to the object, so no movement is done unless explicitly done by
// object.
if (dragging && m_draggedShape && m_dragState == StartDraggingLeft)
{
m_dragState = ContinueDraggingLeft;
// If the object isn't m_draggable, transfer message to canvas
if (m_draggedShape->Draggable())
m_draggedShape->GetEventHandler()->OnBeginDragLeft((double)x, (double)y, keys, m_draggedAttachment);
else
{
m_draggedShape = NULL;
OnBeginDragLeft((double)x, (double)y, keys);
}
m_oldDragX = x; m_oldDragY = y;
}
else if (dragging && m_draggedShape && m_dragState == ContinueDraggingLeft)
{
// Continue dragging
m_draggedShape->GetEventHandler()->OnDragLeft(false, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
m_draggedShape->GetEventHandler()->OnDragLeft(true, (double)x, (double)y, keys, m_draggedAttachment);
m_oldDragX = x; m_oldDragY = y;
}
else if (event.LeftUp() && m_draggedShape && m_dragState == ContinueDraggingLeft)
{
m_dragState = NoDragging;
m_checkTolerance = true;
m_draggedShape->GetEventHandler()->OnDragLeft(false, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
m_draggedShape->GetEventHandler()->OnEndDragLeft((double)x, (double)y, keys, m_draggedAttachment);
m_draggedShape = NULL;
}
else if (dragging && m_draggedShape && m_dragState == StartDraggingRight)
{
m_dragState = ContinueDraggingRight;
if (m_draggedShape->Draggable())
m_draggedShape->GetEventHandler()->OnBeginDragRight((double)x, (double)y, keys, m_draggedAttachment);
else
{
m_draggedShape = NULL;
OnBeginDragRight((double)x, (double)y, keys);
}
m_oldDragX = x; m_oldDragY = y;
}
else if (dragging && m_draggedShape && m_dragState == ContinueDraggingRight)
{
// Continue dragging
m_draggedShape->GetEventHandler()->OnDragRight(false, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
m_draggedShape->GetEventHandler()->OnDragRight(true, (double)x, (double)y, keys, m_draggedAttachment);
m_oldDragX = x; m_oldDragY = y;
}
else if (event.RightUp() && m_draggedShape && m_dragState == ContinueDraggingRight)
{
m_dragState = NoDragging;
m_checkTolerance = true;
m_draggedShape->GetEventHandler()->OnDragRight(false, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
m_draggedShape->GetEventHandler()->OnEndDragRight((double)x, (double)y, keys, m_draggedAttachment);
m_draggedShape = NULL;
}
// All following events sent to canvas, not object
else if (dragging && !m_draggedShape && m_dragState == StartDraggingLeft)
{
m_dragState = ContinueDraggingLeft;
OnBeginDragLeft((double)x, (double)y, keys);
m_oldDragX = x; m_oldDragY = y;
}
else if (dragging && !m_draggedShape && m_dragState == ContinueDraggingLeft)
{
// Continue dragging
OnDragLeft(false, m_oldDragX, m_oldDragY, keys);
OnDragLeft(true, (double)x, (double)y, keys);
m_oldDragX = x; m_oldDragY = y;
}
else if (event.LeftUp() && !m_draggedShape && m_dragState == ContinueDraggingLeft)
{
m_dragState = NoDragging;
m_checkTolerance = true;
OnDragLeft(false, m_oldDragX, m_oldDragY, keys);
OnEndDragLeft((double)x, (double)y, keys);
m_draggedShape = NULL;
}
else if (dragging && !m_draggedShape && m_dragState == StartDraggingRight)
{
m_dragState = ContinueDraggingRight;
OnBeginDragRight((double)x, (double)y, keys);
m_oldDragX = x; m_oldDragY = y;
}
else if (dragging && !m_draggedShape && m_dragState == ContinueDraggingRight)
{
// Continue dragging
OnDragRight(false, m_oldDragX, m_oldDragY, keys);
OnDragRight(true, (double)x, (double)y, keys);
m_oldDragX = x; m_oldDragY = y;
}
else if (event.RightUp() && !m_draggedShape && m_dragState == ContinueDraggingRight)
{
m_dragState = NoDragging;
m_checkTolerance = true;
OnDragRight(false, m_oldDragX, m_oldDragY, keys);
OnEndDragRight((double)x, (double)y, keys);
m_draggedShape = NULL;
}
// Non-dragging events
else if (event.IsButton())
{
m_checkTolerance = true;
// Find the nearest object
int attachment = 0;
wxShape *nearest_object = FindShape(x, y, &attachment);
if (nearest_object) // Object event
{
if (event.LeftDown())
{
m_draggedShape = nearest_object;
m_draggedAttachment = attachment;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -