minimal.cpp 17.1 KB
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

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

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWindows headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wx/txtstrm.h"

#if !wxUSE_DOC_VIEW_ARCHITECTURE
#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
#endif
#include "wx/mdi.h"
#include "wx/docview.h"
#include "wx/docmdi.h"
#include "wx/cmdproc.h"

#include <list>
#include <iostream>
#include <fstream>
using namespace std;

// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------

// the application icon (under Windows and OS/2 it is in resources)
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
    #include "mondrian.xpm"
#endif

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
// Define a new application

class MyFrame;

class MyApp: public wxApp
{
  private:
	MyFrame *frame;
  public:
    MyApp(void);
    bool OnInit(void);
    int OnExit(void);
    MyFrame *GetMainFrame(void);
    wxMDIChildFrame *CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas);

  protected:
    wxDocManager* m_docManager;
};

DECLARE_APP(MyApp)

// Define a new frame
class MyCanvas;
class MyFrame: public wxDocMDIParentFrame
{
  DECLARE_CLASS(MyFrame)
 public:
  MyFrame(wxDocManager *manager, wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size,
    long type);

  void OnAbout(wxCommandEvent& event);
  MyCanvas *CreateCanvas(wxView *view, wxMDIChildFrame *parent);

DECLARE_EVENT_TABLE()
};

// Plots a line from one point to the other
class DoodleLine
{
 public:
  wxInt32 x1;
  wxInt32 y1;
  wxInt32 x2;
  wxInt32 y2;
};

// Contains a list of lines: represents a mouse-down doodle
class DoodleSegment
{
 public:
  list<DoodleLine> lines;
  bool empty() {return lines.begin()==lines.end();};
  void clear() {lines.clear();};
  void Draw(wxDC *dc);

#if wxUSE_STD_IOSTREAM
  wxSTD ostream& SaveObject(wxSTD ostream& stream);
  wxSTD istream& LoadObject(wxSTD istream& stream);
#else
  wxOutputStream& SaveObject(wxOutputStream& stream);
  wxInputStream& LoadObject(wxInputStream& stream);
#endif
};

class MyCanvas : public wxScrolledWindow
{
 private:
 DoodleSegment currentSegment;
 int xpos;
 int ypos;
 protected:
 public:
  wxView *view;
  MyCanvas(wxView *v, wxMDIChildFrame *frame, const wxPoint& pos, const wxSize& size, long style);
  void OnDraw(wxDC& dc);
  void OnMouse(wxMouseEvent& event);
  DECLARE_EVENT_TABLE()
};

class DrawingDocument: public wxDocument
{
  DECLARE_DYNAMIC_CLASS(DrawingDocument)
public:
#if wxUSE_STD_IOSTREAM
  wxSTD ostream& SaveObject(wxSTD ostream& text_stream);
  wxSTD istream& LoadObject(wxSTD istream& text_stream);
#else
  wxOutputStream& SaveObject(wxOutputStream& stream);
  wxInputStream& LoadObject(wxInputStream& stream);
#endif
 list<DoodleSegment> segments;
};


class DrawingView: public wxView
{
public:
    MyCanvas *canvas;
  
    DrawingView() { canvas = (MyCanvas *) NULL;}
    ~DrawingView() {}

    bool OnCreate(wxDocument *doc, long flags);
    void OnDraw(wxDC *dc);
    void OnUpdate(wxView *sender, wxObject *hint = (wxObject *) NULL);
    bool OnClose(bool deleteWindow = TRUE);

    void OnCut(wxCommandEvent& event);

private:
    DECLARE_DYNAMIC_CLASS(DrawingView)
    DECLARE_EVENT_TABLE()
};


class DrawingCommand: public wxCommand
{
 protected:
  DoodleSegment segment;
  DrawingDocument *doc;
  int cmd;
 public:
  DrawingCommand(const wxString& name, int cmd, DrawingDocument *ddoc, const DoodleSegment& seg);
  ~DrawingCommand(void);

  bool Do(void);
  bool Undo(void);
};


#define DOCVIEW_CUT   1
#define DOCVIEW_ABOUT   2
#define DOCVIEW_ADD   3

// ----------------------------------------------------------------------------
// implementations
// ----------------------------------------------------------------------------


IMPLEMENT_APP(MyApp)

MyApp::MyApp(void)
{
    m_docManager = (wxDocManager *) NULL;
}

MyFrame * MyApp::GetMainFrame(void)
{
    return frame;
}


bool MyApp::OnInit(void)
{
  //// Create a document manager
  m_docManager = new wxDocManager;

  //// Create a template relating drawing documents to their views
  (void) new wxDocTemplate((wxDocManager *) m_docManager, _T("Drawing"), _T("*.drw"), _T(""), _T("drw"), _T("Drawing Doc"), _T("Drawing View"),
          CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));

  //// Create the main frame window
  frame = new MyFrame((wxDocManager *) m_docManager, (wxFrame *) NULL,
                      _T("DocView Demo"), wxPoint(0, 0), wxSize(500, 400),
                      wxDEFAULT_FRAME_STYLE);

  //// Give it an icon (this is ignored in MDI mode: uses resources)
#ifdef __WXMSW__
  frame->SetIcon(wxIcon(_T("doc")));
#endif
#ifdef __X__
  frame->SetIcon(wxIcon(_T("doc.xbm")));
#endif

  //// Make a menubar
  wxMenu *file_menu = new wxMenu;

  file_menu->Append(wxID_NEW, _T("&New...\tCtrl-N"));
  file_menu->Append(wxID_OPEN, _T("&Open...\tCtrl-X"));

  file_menu->AppendSeparator();
  file_menu->Append(wxID_EXIT, _T("E&xit\tAlt-X"));

  // A nice touch: a history of files visited. Use this menu.
  m_docManager->FileHistoryUseMenu(file_menu);

  wxMenu *help_menu = new wxMenu;
  help_menu->Append(DOCVIEW_ABOUT, _T("&About\tF1"));

  wxMenuBar *menu_bar = new wxMenuBar;

  menu_bar->Append(file_menu, _T("&File"));
  menu_bar->Append(help_menu, _T("&Help"));

  //// Associate the menu bar with the frame
  frame->SetMenuBar(menu_bar);

  frame->Centre(wxBOTH);
  frame->Show(TRUE);

  SetTopWindow(frame);
  return TRUE;
}

int MyApp::OnExit(void)
{
    delete m_docManager;
    return 0;
}

/*
 * Centralised code for creating a document frame.
 * Called from view.cpp, when a view is created.
 */
 
wxMDIChildFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
{
  //// Make a child frame
  wxDocMDIChildFrame *subframe =
      new wxDocMDIChildFrame(doc, view, GetMainFrame(), -1, _T("Child Frame"),
							 wxDefaultPosition, wxDefaultSize,
                             wxDEFAULT_FRAME_STYLE |
							 wxMAXIMIZE);

#ifdef __WXMSW__
  subframe->SetIcon(wxString(isCanvas ? _T("chart") : _T("notepad")));
#endif
#ifdef __X__
  subframe->SetIcon(wxIcon(_T("doc.xbm")));
#endif

  //// Make a menubar
  wxMenu *file_menu = new wxMenu;

  file_menu->Append(wxID_NEW, _T("&New..."));
  file_menu->Append(wxID_OPEN, _T("&Open..."));
  file_menu->Append(wxID_CLOSE, _T("&Close"));
  file_menu->Append(wxID_SAVE, _T("&Save"));
  file_menu->Append(wxID_SAVEAS, _T("Save &As..."));

  if (isCanvas)
  {
    file_menu->AppendSeparator();
    file_menu->Append(wxID_PRINT, _T("&Print..."));
    file_menu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
    file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"));
  }

  file_menu->AppendSeparator();
  file_menu->Append(wxID_EXIT, _T("E&xit"));

  wxMenu *edit_menu = (wxMenu *) NULL;

  if (isCanvas)
  {
    edit_menu = new wxMenu;
    edit_menu->Append(wxID_UNDO, _T("&Undo"));
    edit_menu->Append(wxID_REDO, _T("&Redo"));
    edit_menu->AppendSeparator();
    edit_menu->Append(DOCVIEW_CUT, _T("&Cut last segment"));

    doc->GetCommandProcessor()->SetEditMenu(edit_menu);
  }

  wxMenu *help_menu = new wxMenu;
  help_menu->Append(DOCVIEW_ABOUT, _T("&About"));

  wxMenuBar *menu_bar = new wxMenuBar;

  menu_bar->Append(file_menu, _T("&File"));
  if (isCanvas)
    menu_bar->Append(edit_menu, _T("&Edit"));
  menu_bar->Append(help_menu, _T("&Help"));

  //// Associate the menu bar with the frame
  subframe->SetMenuBar(menu_bar);

  return subframe;
}

/*
 * This is the top-level window of the application.
 */
 
IMPLEMENT_CLASS(MyFrame, wxDocMDIParentFrame)
BEGIN_EVENT_TABLE(MyFrame, wxDocMDIParentFrame)
    EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
END_EVENT_TABLE()

MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, const wxString& title,
    const wxPoint& pos, const wxSize& size, long type):
  wxDocMDIParentFrame(manager, frame, -1, title, pos, size, type, _T("myFrame"))
{
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
{
    (void)wxMessageBox(_T("DocView Demo\nAuthor: Julian Smart\nUsage: docview.exe"), _T("About DocView"));
}

// Creates a canvas. Called from view.cpp when a new drawing
// view is created.
MyCanvas *MyFrame::CreateCanvas(wxView *view, wxMDIChildFrame *parent)
{
  int width, height;
  parent->GetClientSize(&width, &height);

  // Non-retained canvas
  MyCanvas *canvas = (MyCanvas*) new MyCanvas(view, parent, wxPoint(0, 0), wxSize(width, height), 0);
  canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));

  // Give it scrollbars
  canvas->SetScrollbars(20, 20, 50, 50);

  return canvas;
}



IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)

IMPLEMENT_DYNAMIC_CLASS(DrawingView, wxView)

BEGIN_EVENT_TABLE(DrawingView, wxView)
    EVT_MENU(DOCVIEW_CUT, DrawingView::OnCut)
END_EVENT_TABLE()


// What to do when a view is created. Creates actual
// windows for displaying the view.
bool DrawingView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
{
    wxMDIChildFrame *frame = wxGetApp().CreateChildFrame(doc, this, TRUE);
    frame->SetTitle(_T("DrawingView"));

    canvas = wxGetApp().GetMainFrame()->CreateCanvas(this, frame);
	SetFrame(frame);
#ifdef __X__
    // X seems to require a forced resize
    int x, y;
    frame->GetSize(&x, &y);
    frame->SetSize(-1, -1, x, y);
#endif
    frame->Show(TRUE);
    Activate(TRUE);

    return TRUE;
}

// Sneakily gets used for default print/preview
// as well as drawing on the screen.
void DrawingView::OnDraw(wxDC *dc)
{
  dc->SetPen(*wxRED_PEN);

  list<DoodleSegment>::iterator i,j;

  i = ((DrawingDocument *)GetDocument())->segments.begin();
  j = ((DrawingDocument *)GetDocument())->segments.end();
  for(;i!=j;++i)
    i->Draw(dc);
  dc->SetPen(wxNullPen);
}

void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
{
  if (canvas)
    canvas->Refresh();
}

// Clean up windows used for displaying the view.
bool DrawingView::OnClose(bool deleteWindow)
{
  if (!GetDocument()->Close())
    return FALSE;

  // Clear the canvas in  case we're in single-window mode,
  // and the canvas stays.
  canvas->view = (wxView *) NULL;
  canvas = (MyCanvas *) NULL;

  Activate(FALSE);
  
  if (deleteWindow)
    delete GetFrame();

  SetFrame((wxFrame*)NULL);  
  return TRUE;
}

void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
{
    DrawingDocument *doc = (DrawingDocument *)GetDocument();
    doc->GetCommandProcessor()->Submit(new DrawingCommand(_T("Cut Last Segment"), DOCVIEW_CUT, doc, DoodleSegment()));
}


BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
	EVT_MOUSE_EVENTS(MyCanvas::OnMouse)
END_EVENT_TABLE()

MyCanvas::MyCanvas(wxView *v, wxMDIChildFrame *frame, const wxPoint& pos, const wxSize& size, long style):
 wxScrolledWindow(frame, -1, pos, size, style)
{
  view = v;
  SetBackgroundColour(wxColour(_T("YELLOW")));
}


void MyCanvas::OnDraw(wxDC& dc)
{
  if (view)
    view->OnDraw(& dc);
}

void MyCanvas::OnMouse(wxMouseEvent& event)
{
  wxClientDC dc(this);
  PrepareDC(dc);

  wxPen redPen(_T("RED"), 1, wxSOLID);
  dc.SetPen(redPen);

  int x,y;
  event.GetPosition(&x,&y);
  wxPoint pt(event.GetLogicalPosition(dc));

  if (HasCapture() && event.LeftUp())
  {
	ReleaseMouse();
    if (!currentSegment.empty())
    {
    // We've got a valid segment on mouse left up, so store it.
		DrawingDocument *doc = (DrawingDocument *)view->GetDocument();
    	doc->GetCommandProcessor()->
		  Submit(new DrawingCommand(_T("Add Segment"), DOCVIEW_ADD, doc, currentSegment));
	  currentSegment.clear();
	}
  }

  if (HasCapture() && event.Dragging())
  {
    DoodleLine newLine;
    newLine.x1 = (long)xpos; 
    newLine.y1 = (long)ypos;
    newLine.x2 = pt.x; 
    newLine.y2 = pt.y;
    currentSegment.lines.push_back(newLine);
    dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
    xpos = pt.x;
    ypos = pt.y;
  }
  if(event.LeftDown())
  {
	  CaptureMouse();
      currentSegment.clear();
      xpos = pt.x;
	  ypos = pt.y;
  }
}

#if wxUSE_STD_IOSTREAM
wxSTD ostream& DrawingDocument::SaveObject(wxSTD ostream& stream)
{
  wxDocument::SaveObject(stream);
  
  wxInt32 n = segments.size();
  stream << n << _T('\n');
  

  list<DoodleSegment>::iterator i;
  for(i=segments.begin();i!=segments.end();i++)
  {
    i->SaveObject(stream);
    stream << _T('\n');
  }
  return stream;
}
#else
wxOutputStream& DrawingDocument::SaveObject(wxOutputStream& stream)
{
  wxDocument::SaveObject(stream);

  wxTextOutputStream text_stream( stream );

  wxInt32 n = segments.size();
  text_stream << n << _T('\n');

  list<DoodleSegment>::iterator i;
  for(i=segments.begin();i!=segments.end();i++)
  {
    i->SaveObject(stream);
    text_stream << _T('\n');
  }
  
  return stream;
}
#endif

#if wxUSE_STD_IOSTREAM
wxSTD istream& DrawingDocument::LoadObject(wxSTD istream& stream)
{
  wxDocument::LoadObject(stream);
  
  wxInt32 n = 0;
  stream >> n;

  for (int i = 0; i < n; i++)
  {
    DoodleSegment segment;
    segment.LoadObject(stream);
    segments.push_back(segment);
  }

  return stream;
}
#else
wxInputStream& DrawingDocument::LoadObject(wxInputStream& stream)
{
  wxDocument::LoadObject(stream);

  wxTextInputStream text_stream( stream );

  wxInt32 n = 0;
  text_stream >> n;

  for (int i = 0; i < n; i++)
  {
    DoodleSegment segment;
    segment.LoadObject(stream);
    segments.push_back(segment);
  }

  return stream;
}
#endif

#if wxUSE_STD_IOSTREAM
wxSTD istream& DoodleSegment::LoadObject(wxSTD istream& text_stream)
{
	list<DoodleLine> l;
	unsigned int size;
	text_stream >> size;
	for(unsigned i=0;i<size;i++)
	{
		DoodleLine d;
		text_stream >> d.x1>>d.y1>>d.x2>>d.y2;
		l.push_back(d);
	}
	lines.swap(l);
	return text_stream;
}
#else
wxInputStream& DoodleSegment::LoadObject(wxInputStream& stream)
{
    wxTextInputStream text_stream( stream );
	list<DoodleLine> l;
	wxInt32 size;
	text_stream >> size;
	for(unsigned i=0;i<size;i++)
	{
		DoodleLine d;
		text_stream >> d.x1>>d.y1>>d.x2>>d.y2;
		l.push_back(d);
	}
	lines.swap(l);
	return stream;
}
#endif

#if wxUSE_STD_IOSTREAM
wxSTD ostream& DoodleSegment::SaveObject(wxSTD ostream& text_stream)
{
	text_stream << lines.size() << endl;
    list<DoodleLine>::iterator i;  
    for(i=lines.begin();i!=lines.end();++i)
	{
		text_stream<<i->x1<<" "<<i->y1<<" "<<i->x2<<" "<<i->y2<<endl;
	}
	return text_stream;
};
#else
wxOutputStream& DoodleSegment::SaveObject(wxOutputStream& stream)
{
    wxTextOutputStream text_stream( stream );
	text_stream << lines.size() << endl;
    list<DoodleLine>::iterator i;  
    for(i=lines.begin();i!=lines.end();++i)
	{
		text_stream<<i->x1<<" "<<i->y1<<" "<<i->x2<<" "<<i->y2<<endl;
	}
	return stream;
};
#endif



DrawingCommand::DrawingCommand(const wxString& name, int command, DrawingDocument *ddoc, const DoodleSegment& seg):
  wxCommand(TRUE, name)
{
  doc = ddoc;
  segment = seg;
  cmd = command;
}

DrawingCommand::~DrawingCommand(void)
{
}

bool DrawingCommand::Do(void)
{
  switch (cmd)
  {
    case DOCVIEW_CUT:
    {
      // Cut the last segment
      if (doc->segments.size() > 0)
      {
        segment = doc->segments.back();
		doc->segments.pop_back();

        doc->Modify(TRUE);
        doc->UpdateAllViews();
      }
      break;
    }
    case DOCVIEW_ADD:
    {
      doc->segments.push_back(segment);
      doc->Modify(TRUE);
      doc->UpdateAllViews();
      break;
    }
  }
  return TRUE;
}

bool DrawingCommand::Undo(void)
{
  switch (cmd)
  {
    case DOCVIEW_CUT:
    {
      // Paste the segment
      doc->segments.push_back(segment);
      doc->Modify(TRUE);
      doc->UpdateAllViews();
      break;
    }
    case DOCVIEW_ADD:
    {
      // Cut the last segment
      if (doc->segments.size() > 0)
      {
        segment = doc->segments.back();
		doc->segments.pop_back();

        doc->Modify(TRUE);
        doc->UpdateAllViews();
      }
    }
  }
  return TRUE;
}

void DoodleSegment::Draw(wxDC *dc)
{

  list<DoodleLine>::iterator i;  
  for(i=lines.begin();i!=lines.end();++i)
    dc->DrawLine(i->x1, i->y1, i->x2, i->y2);
}