2000-03-03 06:25:10 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2006-02-10 06:59:52 -05:00
|
|
|
// Name: contrib/samples/ogl/ogledit/view.cpp
|
2000-03-03 06:25:10 -05:00
|
|
|
// Purpose: Implements view functionality in OGLEdit
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Modified by:
|
|
|
|
// Created: 12/07/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Julian Smart
|
2004-06-09 12:42:34 -04:00
|
|
|
// Licence: wxWindows licence
|
2000-03-03 06:25:10 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
2001-10-30 08:33:34 -05:00
|
|
|
#include "wx/wxprec.h"
|
2000-03-03 06:25:10 -05:00
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
2006-02-10 06:59:52 -05:00
|
|
|
#include "wx/wx.h"
|
2000-03-03 06:25:10 -05:00
|
|
|
#endif
|
|
|
|
|
2006-02-10 06:59:52 -05:00
|
|
|
#include "wx/colordlg.h"
|
2000-03-03 06:25:10 -05:00
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "palette.h"
|
|
|
|
|
|
|
|
IMPLEMENT_DYNAMIC_CLASS(DiagramView, wxView)
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(DiagramView, wxView)
|
2005-03-31 14:18:25 -05:00
|
|
|
EVT_MENU(wxID_CUT, DiagramView::OnCut)
|
2000-03-03 06:25:10 -05:00
|
|
|
EVT_MENU(OGLEDIT_CHANGE_BACKGROUND_COLOUR, DiagramView::OnChangeBackgroundColour)
|
|
|
|
EVT_MENU(OGLEDIT_EDIT_LABEL, DiagramView::OnEditLabel)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// What to do when a view is created. Creates actual
|
|
|
|
// windows for displaying the view.
|
2003-08-21 05:05:56 -04:00
|
|
|
bool DiagramView::OnCreate(wxDocument *doc, long WXUNUSED(flags))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
frame = GetMainFrame();
|
|
|
|
canvas = GetMainFrame()->canvas;
|
|
|
|
canvas->view = this;
|
|
|
|
|
|
|
|
SetFrame(frame);
|
2004-06-09 12:42:34 -04:00
|
|
|
Activate(true);
|
2000-03-03 06:25:10 -05:00
|
|
|
|
|
|
|
// Initialize the edit menu Undo and Redo items
|
|
|
|
doc->GetCommandProcessor()->SetEditMenu(((MyFrame *)frame)->editMenu);
|
|
|
|
doc->GetCommandProcessor()->Initialize();
|
|
|
|
|
|
|
|
wxShapeCanvas *shapeCanvas = (wxShapeCanvas *)canvas;
|
|
|
|
DiagramDocument *diagramDoc = (DiagramDocument *)doc;
|
|
|
|
shapeCanvas->SetDiagram(diagramDoc->GetDiagram());
|
|
|
|
diagramDoc->GetDiagram()->SetCanvas(shapeCanvas);
|
|
|
|
|
2004-06-09 12:42:34 -04:00
|
|
|
return true;
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
2004-06-09 12:42:34 -04:00
|
|
|
#define CENTER false // Place the drawing to the center of the page
|
2000-03-03 06:25:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
// Sneakily gets used for default print/preview
|
2003-08-14 13:29:17 -04:00
|
|
|
// as well as drawing on the screen.
|
|
|
|
void DiagramView::OnDraw(wxDC *dc)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* You might use THIS code if you were scaling
|
|
|
|
* graphics of known size to fit on the page.
|
|
|
|
*/
|
|
|
|
int w, h;
|
|
|
|
|
|
|
|
// We need to adjust for the graphic size, a formula will be added
|
|
|
|
float maxX = 900;
|
|
|
|
float maxY = 700;
|
|
|
|
// A better way of find the maxium values would be to search through
|
|
|
|
// the linked list
|
|
|
|
|
|
|
|
// Let's have at least 10 device units margin
|
|
|
|
float marginX = 10;
|
|
|
|
float marginY = 10;
|
|
|
|
|
|
|
|
// Add the margin to the graphic size
|
|
|
|
maxX += (2 * marginX);
|
|
|
|
maxY += (2 * marginY);
|
|
|
|
|
|
|
|
// Get the size of the DC in pixels
|
|
|
|
dc->GetSize (&w, &h);
|
|
|
|
|
|
|
|
// Calculate a suitable scaling factor
|
|
|
|
float scaleX = (float) (w / maxX);
|
|
|
|
float scaleY = (float) (h / maxY);
|
|
|
|
|
|
|
|
// Use x or y scaling factor, whichever fits on the DC
|
|
|
|
float actualScale = wxMin (scaleX, scaleY);
|
|
|
|
|
|
|
|
float posX, posY;
|
|
|
|
// Calculate the position on the DC for centring the graphic
|
2003-08-21 05:05:56 -04:00
|
|
|
#if 0
|
|
|
|
// center the drawing
|
2003-08-14 13:29:17 -04:00
|
|
|
posX = (float) ((w - (200 * actualScale)) / 2.0);
|
|
|
|
posY = (float) ((h - (200 * actualScale)) / 2.0);
|
2003-08-21 05:05:56 -04:00
|
|
|
#else
|
|
|
|
// Use defined presets
|
2003-08-14 13:29:17 -04:00
|
|
|
posX = 10;
|
|
|
|
posY = 35;
|
2003-08-21 05:05:56 -04:00
|
|
|
#endif
|
2003-08-14 13:29:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
// Set the scale and origin
|
|
|
|
dc->SetUserScale (actualScale, actualScale);
|
|
|
|
dc->SetDeviceOrigin ((long) posX, (long) posY);
|
|
|
|
|
|
|
|
// This part was added to preform the print preview and printing functions
|
|
|
|
|
2000-03-03 06:25:10 -05:00
|
|
|
wxDiagram *diagram_p=((DiagramDocument*)GetDocument())->GetDiagram(); // Get the current diagram
|
2003-08-14 13:29:17 -04:00
|
|
|
if (diagram_p->GetShapeList())
|
|
|
|
{
|
2003-08-21 05:05:56 -04:00
|
|
|
/* wxCursor *old_cursor = NULL; */
|
2004-07-22 15:07:59 -04:00
|
|
|
wxObjectList::compatibility_iterator current = diagram_p->GetShapeList()->GetFirst();
|
2000-03-03 06:25:10 -05:00
|
|
|
|
2003-08-14 13:29:17 -04:00
|
|
|
while (current) // Loop through the entire list of shapes
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
2003-11-09 16:40:34 -05:00
|
|
|
wxShape *object = (wxShape *)current->GetData();
|
2000-03-03 06:25:10 -05:00
|
|
|
if (!object->GetParent())
|
|
|
|
{
|
|
|
|
object->Draw(* dc); // Draw the shape onto our printing dc
|
|
|
|
}
|
2003-11-09 16:40:34 -05:00
|
|
|
current = current->GetNext(); // Procede to the next shape in the list
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void DiagramView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
if (canvas)
|
|
|
|
canvas->Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up windows used for displaying the view.
|
2003-08-21 05:05:56 -04:00
|
|
|
bool DiagramView::OnClose(bool WXUNUSED(deleteWindow))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
if (!GetDocument()->Close())
|
2004-06-09 12:42:34 -04:00
|
|
|
return false;
|
2000-03-03 06:25:10 -05:00
|
|
|
|
|
|
|
DiagramDocument *diagramDoc = (DiagramDocument *)GetDocument();
|
|
|
|
diagramDoc->GetDiagram()->SetCanvas(NULL);
|
|
|
|
|
2003-08-14 13:29:17 -04:00
|
|
|
canvas->ClearBackground();
|
2000-03-03 06:25:10 -05:00
|
|
|
canvas->SetDiagram(NULL);
|
|
|
|
canvas->view = NULL;
|
|
|
|
canvas = NULL;
|
|
|
|
|
|
|
|
wxString s = wxTheApp->GetAppName();
|
|
|
|
if (frame)
|
|
|
|
frame->SetTitle(s);
|
|
|
|
|
|
|
|
SetFrame(NULL);
|
|
|
|
|
2004-06-09 12:42:34 -04:00
|
|
|
Activate(false);
|
2003-08-14 13:29:17 -04:00
|
|
|
|
2004-06-09 12:42:34 -04:00
|
|
|
return true;
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
wxShape *DiagramView::FindSelectedShape(void)
|
|
|
|
{
|
|
|
|
DiagramDocument *doc = (DiagramDocument *)GetDocument();
|
2004-07-22 15:07:59 -04:00
|
|
|
wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
|
2000-03-03 06:25:10 -05:00
|
|
|
while (node)
|
|
|
|
{
|
2003-11-09 16:40:34 -05:00
|
|
|
wxShape *eachShape = (wxShape *)node->GetData();
|
2000-03-03 06:25:10 -05:00
|
|
|
if ((eachShape->GetParent() == NULL) && eachShape->Selected())
|
|
|
|
{
|
2004-07-23 14:04:12 -04:00
|
|
|
return eachShape;
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
2003-11-09 16:40:34 -05:00
|
|
|
else node = node->GetNext();
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
2004-07-23 14:04:12 -04:00
|
|
|
return NULL;
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void DiagramView::OnCut(wxCommandEvent& WXUNUSED(event))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
DiagramDocument *doc = (DiagramDocument *)GetDocument();
|
|
|
|
|
|
|
|
wxShape *theShape = FindSelectedShape();
|
|
|
|
if (theShape)
|
2005-03-31 14:18:25 -05:00
|
|
|
doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), wxID_CUT, doc, NULL, 0.0, 0.0, true, theShape));
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void DiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
DiagramDocument *doc = (DiagramDocument *)GetDocument();
|
|
|
|
|
|
|
|
wxShape *theShape = FindSelectedShape();
|
|
|
|
if (theShape)
|
|
|
|
{
|
|
|
|
wxColourData data;
|
2004-06-09 12:42:34 -04:00
|
|
|
data.SetChooseFull(true);
|
2000-03-03 06:25:10 -05:00
|
|
|
data.SetColour(theShape->GetBrush()->GetColour());
|
|
|
|
|
|
|
|
wxColourDialog *dialog = new wxColourDialog(frame, &data);
|
|
|
|
wxBrush *theBrush = NULL;
|
|
|
|
if (dialog->ShowModal() == wxID_OK)
|
|
|
|
{
|
|
|
|
wxColourData retData = dialog->GetColourData();
|
|
|
|
wxColour col = retData.GetColour();
|
|
|
|
theBrush = wxTheBrushList->FindOrCreateBrush(col, wxSOLID);
|
|
|
|
}
|
|
|
|
dialog->Close();
|
|
|
|
|
|
|
|
if (theBrush)
|
2003-08-21 05:05:56 -04:00
|
|
|
doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc,
|
2000-03-03 06:25:10 -05:00
|
|
|
theBrush, theShape));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void DiagramView::OnEditLabel(wxCommandEvent& WXUNUSED(event))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
wxShape *theShape = FindSelectedShape();
|
|
|
|
if (theShape)
|
|
|
|
{
|
2003-08-21 05:05:56 -04:00
|
|
|
wxString newLabel = wxGetTextFromUser(_T("Enter new label"), _T("Shape Label"), ((MyEvtHandler *)theShape->GetEventHandler())->label);
|
|
|
|
GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(_T("Edit label"), OGLEDIT_EDIT_LABEL, (DiagramDocument*) GetDocument(), newLabel, theShape));
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Window implementations
|
|
|
|
*/
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyCanvas, wxShapeCanvas)
|
|
|
|
EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
|
|
|
|
EVT_PAINT(MyCanvas::OnPaint)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// Define a constructor for my canvas
|
|
|
|
MyCanvas::MyCanvas(wxView *v, wxWindow *parent, wxWindowID id, const wxPoint& pos,
|
|
|
|
const wxSize& size, long style):
|
|
|
|
wxShapeCanvas(parent, id, pos, size, style)
|
|
|
|
{
|
|
|
|
SetBackgroundColour(*wxWHITE);
|
|
|
|
view = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
MyCanvas::~MyCanvas(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
EditorToolPalette *palette = wxGetApp().frame->palette;
|
|
|
|
wxClassInfo *info = NULL;
|
|
|
|
switch (palette->currentlySelected)
|
|
|
|
{
|
|
|
|
case PALETTE_TOOL1:
|
|
|
|
{
|
|
|
|
info = CLASSINFO(wxRectangleShape);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PALETTE_TOOL2:
|
|
|
|
{
|
|
|
|
info = CLASSINFO(wxRoundedRectangleShape);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PALETTE_TOOL3:
|
|
|
|
{
|
|
|
|
info = CLASSINFO(wxEllipseShape);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PALETTE_TOOL4:
|
|
|
|
{
|
|
|
|
info = CLASSINFO(wxDiamondShape);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (info)
|
|
|
|
{
|
2001-04-26 06:17:58 -04:00
|
|
|
view->GetDocument()->GetCommandProcessor()->Submit(
|
2003-08-21 05:05:56 -04:00
|
|
|
new DiagramCommand( info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info,
|
2001-04-26 06:17:58 -04:00
|
|
|
x, y));
|
2000-03-03 06:25:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnBeginDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnEndDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:05:56 -04:00
|
|
|
void MyCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
|
2000-03-03 06:25:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyCanvas::OnMouseEvent(wxMouseEvent& event)
|
|
|
|
{
|
|
|
|
wxShapeCanvas::OnMouseEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyCanvas::OnPaint(wxPaintEvent& event)
|
|
|
|
{
|
|
|
|
// if (GetDiagram())
|
|
|
|
wxShapeCanvas::OnPaint(event);
|
|
|
|
}
|