1998-05-22 15:57:05 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2008-10-03 11:16:01 -04:00
|
|
|
// Name: samples/docview/doc.cpp
|
1998-05-22 15:57:05 -04:00
|
|
|
// Purpose: Implements document functionality
|
|
|
|
// Author: Julian Smart
|
2008-10-03 11:16:01 -04:00
|
|
|
// Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
|
1998-05-22 15:57:05 -04:00
|
|
|
// Created: 04/01/98
|
|
|
|
// RCS-ID: $Id$
|
2008-10-03 11:16:01 -04:00
|
|
|
// Copyright: (c) 1998 Julian Smart
|
|
|
|
// (c) 2008 Vadim Zeitlin
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
1998-05-22 15:57:05 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1998-05-22 15:57:05 -04:00
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
2008-10-03 11:16:01 -04:00
|
|
|
#pragma hdrstop
|
1998-05-22 15:57:05 -04:00
|
|
|
#endif
|
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
#if !wxUSE_DOC_VIEW_ARCHITECTURE
|
|
|
|
#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
|
1998-05-22 15:57:05 -04:00
|
|
|
#endif
|
2008-10-03 11:16:01 -04:00
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
2003-01-10 12:57:05 -05:00
|
|
|
#endif
|
1998-05-22 15:57:05 -04:00
|
|
|
|
2006-11-03 19:02:02 -05:00
|
|
|
#if wxUSE_STD_IOSTREAM
|
|
|
|
#include "wx/ioswrap.h"
|
|
|
|
#else
|
|
|
|
#include "wx/txtstrm.h"
|
|
|
|
#endif
|
2009-09-24 12:04:06 -04:00
|
|
|
#include "wx/wfstream.h"
|
2006-11-03 19:02:02 -05:00
|
|
|
|
1998-05-22 15:57:05 -04:00
|
|
|
#include "doc.h"
|
|
|
|
#include "view.h"
|
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// DrawingDocument implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-22 15:57:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
|
|
|
|
|
2008-10-15 07:22:53 -04:00
|
|
|
DocumentOstream& DrawingDocument::SaveObject(DocumentOstream& ostream)
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
2008-10-15 07:22:53 -04:00
|
|
|
#if wxUSE_STD_IOSTREAM
|
|
|
|
DocumentOstream& stream = ostream;
|
|
|
|
#else
|
|
|
|
wxTextOutputStream stream(ostream);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
wxDocument::SaveObject(ostream);
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
const wxInt32 count = m_doodleSegments.size();
|
|
|
|
stream << count << '\n';
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
for ( int n = 0; n < count; n++ )
|
2000-07-15 15:51:35 -04:00
|
|
|
{
|
2008-10-15 07:22:53 -04:00
|
|
|
m_doodleSegments[n].SaveObject(ostream);
|
2000-07-15 15:51:35 -04:00
|
|
|
stream << '\n';
|
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-15 07:22:53 -04:00
|
|
|
return ostream;
|
1998-05-22 15:57:05 -04:00
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-15 07:22:53 -04:00
|
|
|
DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& istream)
|
2008-10-03 11:16:01 -04:00
|
|
|
{
|
2008-10-15 07:22:53 -04:00
|
|
|
#if wxUSE_STD_IOSTREAM
|
|
|
|
DocumentIstream& stream = istream;
|
|
|
|
#else
|
|
|
|
wxTextInputStream stream(istream);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
wxDocument::LoadObject(istream);
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
wxInt32 count = 0;
|
|
|
|
stream >> count;
|
2011-07-22 12:16:00 -04:00
|
|
|
if ( count < 0 )
|
|
|
|
{
|
|
|
|
wxLogWarning("Drawing document corrupted: invalid segments count.");
|
|
|
|
#if wxUSE_STD_IOSTREAM
|
|
|
|
istream.clear(std::ios::badbit);
|
|
|
|
#else
|
|
|
|
istream.Reset(wxSTREAM_READ_ERROR);
|
|
|
|
#endif
|
|
|
|
return istream;
|
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
for ( int n = 0; n < count; n++ )
|
2000-07-15 15:51:35 -04:00
|
|
|
{
|
2008-10-03 11:16:01 -04:00
|
|
|
DoodleSegment segment;
|
2008-10-15 07:22:53 -04:00
|
|
|
segment.LoadObject(istream);
|
2008-10-03 11:16:01 -04:00
|
|
|
m_doodleSegments.push_back(segment);
|
2000-07-15 15:51:35 -04:00
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-15 07:22:53 -04:00
|
|
|
return istream;
|
1999-07-11 15:29:59 -04:00
|
|
|
}
|
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
void DrawingDocument::DoUpdate()
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
2008-10-03 11:16:01 -04:00
|
|
|
Modify(true);
|
|
|
|
UpdateAllViews();
|
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
|
|
|
|
{
|
|
|
|
m_doodleSegments.push_back(segment);
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
DoUpdate();
|
1998-05-22 15:57:05 -04:00
|
|
|
}
|
2008-10-03 11:16:01 -04:00
|
|
|
|
|
|
|
bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
|
1999-07-11 15:29:59 -04:00
|
|
|
{
|
2008-10-03 11:16:01 -04:00
|
|
|
if ( m_doodleSegments.empty() )
|
|
|
|
return false;
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
if ( segment )
|
|
|
|
*segment = m_doodleSegments.back();
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
m_doodleSegments.pop_back();
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
DoUpdate();
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
return true;
|
1999-07-11 15:29:59 -04:00
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// DoodleSegment implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-22 15:57:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
1999-07-11 15:29:59 -04:00
|
|
|
#if wxUSE_STD_IOSTREAM
|
2008-10-03 11:16:01 -04:00
|
|
|
DocumentOstream& stream = ostream;
|
1999-07-11 15:29:59 -04:00
|
|
|
#else
|
2008-10-03 11:16:01 -04:00
|
|
|
wxTextOutputStream stream(ostream);
|
|
|
|
#endif
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
const wxInt32 count = m_lines.size();
|
|
|
|
stream << count << '\n';
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
for ( int n = 0; n < count; n++ )
|
2000-07-15 15:51:35 -04:00
|
|
|
{
|
2008-10-03 11:16:01 -04:00
|
|
|
const DoodleLine& line = m_lines[n];
|
|
|
|
stream
|
|
|
|
<< line.x1 << ' '
|
|
|
|
<< line.y1 << ' '
|
|
|
|
<< line.x2 << ' '
|
|
|
|
<< line.y2 << '\n';
|
2000-07-15 15:51:35 -04:00
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-15 07:22:53 -04:00
|
|
|
return ostream;
|
1999-07-11 15:29:59 -04:00
|
|
|
}
|
1998-05-22 15:57:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
2008-10-03 11:16:01 -04:00
|
|
|
#if wxUSE_STD_IOSTREAM
|
|
|
|
DocumentIstream& stream = istream;
|
1999-07-11 15:29:59 -04:00
|
|
|
#else
|
2008-10-03 11:16:01 -04:00
|
|
|
wxTextInputStream stream(istream);
|
|
|
|
#endif
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
wxInt32 count = 0;
|
|
|
|
stream >> count;
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
for ( int n = 0; n < count; n++ )
|
2000-07-15 15:51:35 -04:00
|
|
|
{
|
2008-10-03 11:16:01 -04:00
|
|
|
DoodleLine line;
|
|
|
|
stream
|
|
|
|
>> line.x1
|
|
|
|
>> line.y1
|
|
|
|
>> line.x2
|
|
|
|
>> line.y2;
|
|
|
|
m_lines.push_back(line);
|
2000-07-15 15:51:35 -04:00
|
|
|
}
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2008-10-15 07:22:53 -04:00
|
|
|
return istream;
|
1999-07-11 15:29:59 -04:00
|
|
|
}
|
1998-05-22 15:57:05 -04:00
|
|
|
|
2008-10-03 11:16:01 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-01-09 11:01:34 -05:00
|
|
|
// wxTextDocument: wxDocument and wxTextCtrl married
|
2008-10-03 11:16:01 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-22 15:57:05 -04:00
|
|
|
|
2009-01-09 11:01:34 -05:00
|
|
|
IMPLEMENT_CLASS(wxTextDocument, wxDocument)
|
1998-05-22 15:57:05 -04:00
|
|
|
|
2009-05-22 18:52:31 -04:00
|
|
|
bool wxTextDocument::OnCreate(const wxString& path, long flags)
|
|
|
|
{
|
|
|
|
if ( !wxDocument::OnCreate(path, flags) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// subscribe to changes in the text control to update the document state
|
|
|
|
// when it's modified
|
|
|
|
GetTextCtrl()->Connect
|
|
|
|
(
|
|
|
|
wxEVT_COMMAND_TEXT_UPDATED,
|
|
|
|
wxCommandEventHandler(wxTextDocument::OnTextChange),
|
|
|
|
NULL,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-06-13 15:42:28 -04:00
|
|
|
|
1998-05-22 15:57:05 -04:00
|
|
|
// Since text windows have their own method for saving to/loading from files,
|
2008-08-21 04:58:32 -04:00
|
|
|
// we override DoSave/OpenDocument instead of Save/LoadObject
|
2009-01-09 11:01:34 -05:00
|
|
|
bool wxTextDocument::DoSaveDocument(const wxString& filename)
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
2009-01-09 11:01:34 -05:00
|
|
|
return GetTextCtrl()->SaveFile(filename);
|
1998-05-22 15:57:05 -04:00
|
|
|
}
|
|
|
|
|
2009-01-09 11:01:34 -05:00
|
|
|
bool wxTextDocument::DoOpenDocument(const wxString& filename)
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
2009-06-13 15:42:28 -04:00
|
|
|
if ( !GetTextCtrl()->LoadFile(filename) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// we're not modified by the user yet
|
|
|
|
Modify(false);
|
|
|
|
|
|
|
|
return true;
|
1998-05-22 15:57:05 -04:00
|
|
|
}
|
|
|
|
|
2009-01-09 11:01:34 -05:00
|
|
|
bool wxTextDocument::IsModified() const
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
2009-01-09 11:01:34 -05:00
|
|
|
wxTextCtrl* wnd = GetTextCtrl();
|
|
|
|
return wxDocument::IsModified() || (wnd && wnd->IsModified());
|
1998-05-22 15:57:05 -04:00
|
|
|
}
|
|
|
|
|
2009-01-09 11:01:34 -05:00
|
|
|
void wxTextDocument::Modify(bool modified)
|
1998-05-22 15:57:05 -04:00
|
|
|
{
|
2008-10-03 11:16:01 -04:00
|
|
|
wxDocument::Modify(modified);
|
2004-10-06 16:32:05 -04:00
|
|
|
|
2009-01-09 11:01:34 -05:00
|
|
|
wxTextCtrl* wnd = GetTextCtrl();
|
|
|
|
if (wnd && !modified)
|
|
|
|
{
|
|
|
|
wnd->DiscardEdits();
|
|
|
|
}
|
1998-05-22 15:57:05 -04:00
|
|
|
}
|
2008-04-06 09:57:23 -04:00
|
|
|
|
2009-05-22 18:52:31 -04:00
|
|
|
void wxTextDocument::OnTextChange(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
Modify(true);
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
2009-01-09 11:01:34 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// TextEditDocument implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
|
|
|
|
|
|
|
|
wxTextCtrl* TextEditDocument::GetTextCtrl() const
|
2008-04-06 09:57:23 -04:00
|
|
|
{
|
2009-01-09 11:01:34 -05:00
|
|
|
wxView* view = GetFirstView();
|
|
|
|
return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
|
2008-04-06 09:57:23 -04:00
|
|
|
}
|
2009-09-24 12:04:06 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2011-06-26 20:09:37 -04:00
|
|
|
// ImageDocument and ImageDetailsDocument implementation
|
2009-09-24 12:04:06 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-05-13 10:37:06 -04:00
|
|
|
IMPLEMENT_DYNAMIC_CLASS(ImageDocument, wxDocument)
|
2009-09-24 12:04:06 -04:00
|
|
|
|
2010-05-13 10:37:06 -04:00
|
|
|
bool ImageDocument::DoOpenDocument(const wxString& file)
|
2009-09-24 12:04:06 -04:00
|
|
|
{
|
2010-05-13 10:37:06 -04:00
|
|
|
return m_image.LoadFile(file);
|
2009-09-24 12:04:06 -04:00
|
|
|
}
|
|
|
|
|
2011-06-26 20:09:37 -04:00
|
|
|
bool ImageDocument::OnOpenDocument(const wxString& filename)
|
|
|
|
{
|
|
|
|
if ( !wxDocument::OnOpenDocument(filename) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// we don't have a wxDocTemplate for the image details document as it's
|
|
|
|
// never created by wxWidgets automatically, instead just do it manually
|
|
|
|
ImageDetailsDocument * const docDetails = new ImageDetailsDocument(this);
|
|
|
|
docDetails->SetFilename(filename);
|
|
|
|
|
|
|
|
new ImageDetailsView(docDetails);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageDetailsDocument::ImageDetailsDocument(ImageDocument *parent)
|
|
|
|
: wxDocument(parent)
|
|
|
|
{
|
|
|
|
const wxImage image = parent->GetImage();
|
|
|
|
|
|
|
|
m_size.x = image.GetWidth();
|
|
|
|
m_size.y = image.GetHeight();
|
|
|
|
m_numColours = image.CountColours();
|
|
|
|
m_type = image.GetType();
|
|
|
|
m_hasAlpha = image.HasAlpha();
|
|
|
|
}
|