1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// hdDrawingView.cpp - Main canvas where all figures are drawn
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
// wxWindows headers
#include <wx/wx.h>
#include <wx/dcbuffer.h>
// App headers
#include "hotdraw/main/hdDrawingView.h"
#include "hotdraw/utilities/hdArrayCollection.h"
#include "hotdraw/main/hdDrawingEditor.h"
#include "hotdraw/utilities/hdGeometry.h"
#include "hotdraw/utilities/hdMouseEvent.h"
#include "hotdraw/tools/hdCanvasMenuTool.h"
#include "hotdraw/figures/defaultAttributes/hdFontAttribute.h"
#include "hotdraw/tools/hdSelectionTool.h"
// Images
#include "images/check.pngc"
#include "images/ddcancel.pngc"
BEGIN_EVENT_TABLE(hdDrawingView, wxScrolledWindow)
EVT_PAINT( hdDrawingView::onPaint)
EVT_MOTION( hdDrawingView::onMotion)
EVT_RIGHT_DOWN( hdDrawingView::onMouseDown)
EVT_RIGHT_UP( hdDrawingView::onMouseUp)
EVT_LEFT_DOWN( hdDrawingView::onMouseDown)
EVT_LEFT_DCLICK( hdDrawingView::onMouseDown)
EVT_LEFT_UP( hdDrawingView::onMouseUp)
EVT_ERASE_BACKGROUND( hdDrawingView::onEraseBackGround) //This erase flicker
EVT_TEXT(CTL_TEXTTOOLID, hdDrawingView::simpleTextToolChangeHandler)
EVT_BUTTON(CTL_OKBUTTONID, hdDrawingView::OnOkTxtButton)
EVT_BUTTON(CTL_CANCELBUTTONID, hdDrawingView::OnCancelTxtButton)
EVT_KEY_DOWN( hdDrawingView::onKeyDown)
EVT_KEY_UP( hdDrawingView::onKeyUp)
END_EVENT_TABLE()
hdDrawingView::hdDrawingView(int diagram, wxWindow *ddParent, hdDrawingEditor *editor, wxSize size, hdDrawing *initialDrawing)// gqbController *controller, gqbModel *model)
: wxScrolledWindow(ddParent, wxID_ANY, wxPoint(0, 0), size,
wxHSCROLL | wxVSCROLL | wxBORDER | wxRETAINED)
{
diagramIndex = diagram;
drawing = initialDrawing;
drawingEditor = editor;
canvasSize = size;
#if wxCHECK_VERSION(2, 9, 0)
FitInside();
#else
SetVirtualSizeHints(canvasSize);
#endif
// Hack to avoid selection rectangle drawing bug
drawSelRect = false;
// Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event
simpleTextToolEdit = new wxTextCtrl(this, CTL_TEXTTOOLID, wxT(""), wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
simpleTextToolEdit->Hide();
simpleTextFigure = NULL;
menuFigure = NULL;
okTxtButton = new wxBitmapButton(this, CTL_OKBUTTONID, *check_png_bmp, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
okTxtButton->Hide();
cancelTxtButton = new wxBitmapButton(this, 1981, wxBitmap(*ddcancel_png_img), wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
cancelTxtButton->Hide();
canvasMenu = NULL;
_tool = NULL;
_tool = new hdSelectionTool(this);
}
hdDrawingView::~hdDrawingView()
{
if(simpleTextToolEdit)
delete simpleTextToolEdit;
if(okTxtButton)
delete okTxtButton;
if(cancelTxtButton)
delete cancelTxtButton;
if(_tool)
delete _tool;
}
void hdDrawingView::onPaint(wxPaintEvent &event)
{
// Prepare Context for Buffered Draw
wxPaintDC dcc(this);
wxBufferedDC dc(&dcc, canvasSize);
dc.Clear();
hdIFigure *toDraw = NULL;
hdIteratorBase *iterator = drawing->figuresEnumerator();
while(iterator->HasNext())
{
toDraw = (hdIFigure *)iterator->Next();
if(toDraw->isSelected(diagramIndex))
toDraw->drawSelected(dc, this);
else
toDraw->draw(dc, this);
}
delete iterator;
hdIHandle *tmpHandle = NULL;
hdIteratorBase *selectionIterator = drawing->selectionFigures();//selection->createIterator();
while(selectionIterator->HasNext())
{
toDraw = (hdIFigure *)selectionIterator->Next();
hdIteratorBase *handlesIterator = toDraw->handlesEnumerator()->createIterator();
while(handlesIterator->HasNext())
{
tmpHandle = (hdIHandle *)handlesIterator->Next();
tmpHandle->draw(dc, this);
}
delete handlesIterator;
}
delete selectionIterator;
//Hack to avoid selection rectangle drawing bug
if (drawSelRect)
{
wxPen *pen = wxThePenList->FindOrCreatePen(*wxRED, 1, wxDOT);
dc.SetPen(*pen);
wxBrush *brush = wxTheBrushList->FindOrCreateBrush(*wxRED, wxTRANSPARENT);
dc.SetBackground(*brush);
dc.SetBackgroundMode(wxTRANSPARENT);
//Adjust points before drawing
wxPoint selAjustedPoints[5];
CalcScrolledPosition(selPoints[0].x, selPoints[0].y, &selAjustedPoints[0].x, &selAjustedPoints[0].y);
CalcScrolledPosition(selPoints[1].x, selPoints[1].y, &selAjustedPoints[1].x, &selAjustedPoints[1].y);
CalcScrolledPosition(selPoints[2].x, selPoints[2].y, &selAjustedPoints[2].x, &selAjustedPoints[2].y);
CalcScrolledPosition(selPoints[3].x, selPoints[3].y, &selAjustedPoints[3].x, &selAjustedPoints[3].y);
CalcScrolledPosition(selPoints[4].x, selPoints[4].y, &selAjustedPoints[4].x, &selAjustedPoints[4].y);
//Draw
dc.DrawLines(5, selAjustedPoints, 0, 0);
drawSelRect = false;
}
}
//Hack to avoid selection rectangle drawing bug
void hdDrawingView::disableSelRectDraw()
{
drawSelRect = false;
}
//Hack to avoid selection rectangle drawing bug
void hdDrawingView::setSelRect(hdRect &selectionRect)
{
//Create rectangle lines to avoid non transparent brush for filling bug in wxwidgets
selPoints[0].x = selectionRect.x;
selPoints[0].y = selectionRect.y;
selPoints[1].x = selectionRect.x + selectionRect.width;
selPoints[1].y = selectionRect.y;
selPoints[2].x = selectionRect.x + selectionRect.width;
selPoints[2].y = selectionRect.y + selectionRect.height;
selPoints[3].x = selectionRect.x;
selPoints[3].y = selectionRect.y + selectionRect.height;
selPoints[4].x = selectionRect.x;
selPoints[4].y = selectionRect.y;
drawSelRect = true;
}
// Overwrite and disable onEraseBackground Event to avoid Flicker
void hdDrawingView::onEraseBackGround(wxEraseEvent &event)
{
}
hdMultiPosRect hdDrawingView::getVisibleArea()
{
int x, y, w, h;
GetViewStart(&x, &y);
GetClientSize(&w, &h);
hdMultiPosRect visibleArea(x, y, w, h);
return visibleArea;
}
hdMultiPosRect hdDrawingView::getVirtualSize()
{
int w, h;
GetVirtualSize(&w, &h);
hdMultiPosRect virtualSize(0, 0, w, h);
return virtualSize;
}
void hdDrawingView::ScrollToMakeVisible(hdPoint p)
{
//implement this function
}
void hdDrawingView::ScrollToMakeVisible (hdRect r)
{
//implement this function
}
hdIHandle *hdDrawingView::findHandle(int posIdx, double x, double y)
{
hdIFigure *tmpFigure = NULL;
hdIHandle *tmpHandle = NULL, *out = NULL;
//Look for handles at each figure in SelectionEnumerator
hdIteratorBase *selectionIterator = drawing->selectionFigures(); //selection->createIterator();
while(selectionIterator->HasNext())
{
tmpFigure = (hdIFigure *)selectionIterator->Next();
hdIteratorBase *handlesIterator = tmpFigure->handlesEnumerator()->createIterator();
while(handlesIterator->HasNext())
{
tmpHandle = (hdIHandle *)handlesIterator->Next();
if(tmpHandle->containsPoint(posIdx, x, y))
{
out = tmpHandle;
break;
}
}
delete handlesIterator;
}
delete selectionIterator;
return out;
}
hdDrawing *hdDrawingView::getDrawing()
{
return drawing;
}
void hdDrawingView::onMotion(wxMouseEvent &event)
{
// simple hack to don't update so frequently the canvas and mouse events
// Should be changed for a better one using time intervals not a simple counter
static int simpleOptimization = 0;
simpleOptimization++;
if(simpleOptimization > 0)
{
simpleOptimization = 0;
hdMouseEvent ddEvent = hdMouseEvent(event, this);
if(event.Dragging())
{
_tool->mouseDrag(ddEvent);
this->Refresh(); //only a dragging event on montion will change model
}
else
{
_tool->mouseMove(ddEvent);
}
}
}
void hdDrawingView::onMouseDown(wxMouseEvent &event)
{
this->AcceptsFocus();
this->SetFocus();
startDrag = event.GetPosition();
hdMouseEvent ddEvent = hdMouseEvent(event, this);
_tool->mouseDown(ddEvent);
this->Refresh();
}
void hdDrawingView::onMouseUp(wxMouseEvent &event)
{
this->AcceptsFocus();
this->SetFocus();
hdMouseEvent ddEvent = hdMouseEvent(event, this);
_tool->mouseUp(ddEvent);
this->Refresh();
}
void hdDrawingView::onKeyDown(wxKeyEvent &event)
{
hdKeyEvent ddEvent = hdKeyEvent(event, this);
_tool->keyDown(ddEvent);
this->Refresh();
}
void hdDrawingView::onKeyUp(wxKeyEvent &event)
{
hdKeyEvent ddEvent = hdKeyEvent(event, this);
_tool->keyUp(ddEvent);
this->Refresh();
}
//Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event
void hdDrawingView::setSimpleTextToolFigure(hdSimpleTextFigure *figure, bool onlySetFigure)
{
simpleTextFigure = figure;
menuFigure = NULL;
if(simpleTextFigure && !onlySetFigure)
{
oldText = simpleTextFigure->getText();
simpleTextToolEdit->SetValue(simpleTextFigure->getText());
simpleTextToolEdit->SelectAll();
}
}
//Hack to allow use (events) of wxmenu inside a tool Generic Way
void hdDrawingView::setMenuToolFigure(hdAbstractMenuFigure *figure)
{
menuFigure = figure;
simpleTextFigure = NULL;
}
//Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event
void hdDrawingView::OnOkTxtButton(wxCommandEvent &event)
{
_tool->deactivate(this);
simpleTextToolEdit->Hide();
okTxtButton->Hide();
cancelTxtButton->Hide();
setSimpleTextToolFigure(NULL);
}
//Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event
void hdDrawingView::OnCancelTxtButton(wxCommandEvent &event)
{
simpleTextToolEdit->SetValue(oldText);
_tool->deactivate(this);
simpleTextToolEdit->Hide();
okTxtButton->Hide();
cancelTxtButton->Hide();
setSimpleTextToolFigure(NULL);
}
//Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event (when text is set at edit it generate this event not sure ????)
void hdDrawingView::simpleTextToolChangeHandler(wxCommandEvent &event)
{
if(!simpleTextToolEdit->IsModified() && simpleTextFigure)
{
simpleTextFigure->setText(simpleTextToolEdit->GetValue());
//getFontMetrics
int width, height;
wxWindowDC dc(this);
dc.SetFont(*hdFontAttribute::defaultFont);
if(simpleTextFigure->getText(true).length() > 5)
dc.GetTextExtent(simpleTextFigure->getText(true), &width, &height);
else
dc.GetTextExtent(wxT("EMPTY"), &width, &height);
//recalculateDisplayBox
hdGeometry g;
simpleTextFigure->displayBox().width = g.max(width, 10) + simpleTextFigure->getPadding();
simpleTextFigure->displayBox().height = g.max(height, 10) + simpleTextFigure->getPadding();
//calculateSizeEntry
hdPoint p = simpleTextFigure->displayBox().GetPosition(this->diagramIndex);
CalcScrolledPosition(p.x, p.y, &p.x, &p.y);
simpleTextToolEdit->SetPosition(p);
simpleTextToolEdit->SetSize(simpleTextFigure->displayBox().GetSize());
okTxtButton->SetPosition(wxPoint(p.x + simpleTextToolEdit->GetSize().GetWidth() + 4, p.y));
cancelTxtButton->SetPosition(wxPoint(okTxtButton->GetPosition().x + okTxtButton->GetSize().GetWidth() + 4, p.y));
}
else if(!simpleTextFigure)
{
wxMessageDialog *error = new wxMessageDialog(NULL, wxT("Error locating hdSimpleTextTool figure"), wxT("Error!"), wxOK | wxICON_ERROR);
error->ShowModal();
delete error;
}
event.Skip();
}
//Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event
wxTextCtrl *hdDrawingView::getSimpleTextToolEdit()
{
return simpleTextToolEdit;
}
//Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event
wxBitmapButton *hdDrawingView::getOkTxt()
{
return okTxtButton;
}
//Hack to avoid event problem with simpleTextTool wxTextCrtl at EVT_TEXT event
wxBitmapButton *hdDrawingView::getCancelTxt()
{
return cancelTxtButton;
}
//Hack to allow use (events) of wxmenu inside a tool like simpletexttool
void hdDrawingView::OnGenericPopupClick(wxCommandEvent &event)
{
if(canvasMenu)
canvasMenu->OnGenericPopupClick(event, this);
else if(menuFigure)
menuFigure->OnGenericPopupClick(event, this);
else if(simpleTextFigure)
simpleTextFigure->OnGenericPopupClick(event, this);
simpleTextFigure = NULL;
menuFigure = NULL;
canvasMenu = NULL;
event.Skip();
}
//Hack to allow use (events) of wxmenu inside a tool like simpletexttool
void hdDrawingView::connectPopUpMenu(wxMenu &mnu)
{
// Connect the main menu
mnu.Connect(wxEVT_COMMAND_MENU_SELECTED,
(wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) &hdDrawingView::OnGenericPopupClick,
NULL,
this);
// Connect all submenus
wxMenuItem *item;
wxMenuItemList list = mnu.GetMenuItems();
for (unsigned int index = 0; index < list.GetCount(); index++)
{
wxMenuItemList::compatibility_iterator node = list.Item(index);
item = (wxMenuItem *) node->GetData();
if (item->IsSubMenu())
{
wxMenu *submenu = item->GetSubMenu();
submenu->Connect(wxEVT_COMMAND_MENU_SELECTED,
(wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) &hdDrawingView::OnGenericPopupClick,
NULL,
this);
}
}
}
//Hack to allow use (events) of wxmenu inside a tool without a figure, Generic Way
void hdDrawingView::setCanvasMenuTool(hdCanvasMenuTool *menuTool)
{
canvasMenu = menuTool;
}
hdDrawingEditor *hdDrawingView::editor()
{
return drawingEditor;
}
bool hdDrawingView::AcceptsFocus() const
{
return true;
}
void hdDrawingView::setTool(hdITool *tool)
{
if(_tool)
delete _tool;
_tool = tool;
}
void hdDrawingView::createViewMenu(wxMenu &mnu)
{
wxMenuItem *item;
item = mnu.AppendCheckItem(1000, _("Sample Item"));
item->Check(true);
}
void hdDrawingView::OnGenericViewPopupClick(wxCommandEvent &event)
{
switch(event.GetId())
{
case 1000:
wxMessageBox(_("Sample menu item"), _("Sample"), wxOK, this);
}
}
void hdDrawingView::notifyChanged()
{
drawingEditor->notifyChanged();
}
|