]>
git.saurik.com Git - wxWidgets.git/blob - samples/docview/doc.cpp
7ce1d828e6419b166a25733b41492cbc431f437c
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     Implements document functionality 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart and Markus Holzem 
   9 // Licence:     wxWindows license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 // #pragma implementation 
  16 // For compilers that support precompilation, includes "wx/wx.h". 
  17 #include "wx/wxprec.h" 
  26 #include "wx/txtstrm.h" 
  28 #include "wx/filename.h" 
  31 #if !wxUSE_DOC_VIEW_ARCHITECTURE 
  32 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h! 
  37 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
) 
  39 DrawingDocument::DrawingDocument(void) 
  43 DrawingDocument::~DrawingDocument(void) 
  45     doodleSegments
.DeleteContents(TRUE
); 
  48 #if wxUSE_STD_IOSTREAM 
  49 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
) 
  51     wxDocument::SaveObject(stream
); 
  53     wxInt32 n 
= doodleSegments
.Number(); 
  56     wxNode 
*node 
= doodleSegments
.First(); 
  59         DoodleSegment 
*segment 
= (DoodleSegment 
*)node
->Data(); 
  60         segment
->SaveObject(stream
); 
  69 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
) 
  71     wxDocument::SaveObject(stream
); 
  73     wxTextOutputStream 
text_stream( stream 
); 
  75     wxInt32 n 
= doodleSegments
.Number(); 
  76     text_stream 
<< n 
<< '\n'; 
  78     wxNode 
*node 
= doodleSegments
.First(); 
  81         DoodleSegment 
*segment 
= (DoodleSegment 
*)node
->Data(); 
  82         segment
->SaveObject(stream
); 
  92 #if wxUSE_STD_IOSTREAM 
  93 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
) 
  95     wxDocument::LoadObject(stream
); 
 100     for (int i 
= 0; i 
< n
; i
++) 
 102         DoodleSegment 
*segment 
= new DoodleSegment
; 
 103         segment
->LoadObject(stream
); 
 104         doodleSegments
.Append(segment
); 
 110 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
) 
 112     wxDocument::LoadObject(stream
); 
 114     wxTextInputStream 
text_stream( stream 
); 
 119     for (int i 
= 0; i 
< n
; i
++) 
 121         DoodleSegment 
*segment 
= new DoodleSegment
; 
 122         segment
->LoadObject(stream
); 
 123         doodleSegments
.Append(segment
); 
 130 DoodleSegment::DoodleSegment(void) 
 134 DoodleSegment::DoodleSegment(DoodleSegment
& seg
) 
 136     wxNode 
*node 
= seg
.lines
.First(); 
 139         DoodleLine 
*line 
= (DoodleLine 
*)node
->Data(); 
 140         DoodleLine 
*newLine 
= new DoodleLine
; 
 141         newLine
->x1 
= line
->x1
; 
 142         newLine
->y1 
= line
->y1
; 
 143         newLine
->x2 
= line
->x2
; 
 144         newLine
->y2 
= line
->y2
; 
 146         lines
.Append(newLine
); 
 152 DoodleSegment::~DoodleSegment(void) 
 154     lines
.DeleteContents(TRUE
); 
 157 #if wxUSE_STD_IOSTREAM 
 158 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
) 
 160     wxInt32 n 
= lines
.Number(); 
 163     wxNode 
*node 
= lines
.First(); 
 166         DoodleLine 
*line 
= (DoodleLine 
*)node
->Data(); 
 167         stream 
<< line
->x1 
<< " " <<  
 177 wxOutputStream 
&DoodleSegment::SaveObject(wxOutputStream
& stream
) 
 179     wxTextOutputStream 
text_stream( stream 
); 
 181     wxInt32 n 
= lines
.Number(); 
 182     text_stream 
<< n 
<< _T('\n'); 
 184     wxNode 
*node 
= lines
.First(); 
 187         DoodleLine 
*line 
= (DoodleLine 
*)node
->Data(); 
 188         text_stream 
<< line
->x1 
<< _T(" ") <<  
 189             line
->y1 
<< _T(" ") <<  
 190             line
->x2 
<< _T(" ") <<  
 191             line
->y2 
<< _T("\n"); 
 199 #if wxUSE_STD_IOSTREAM 
 200 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
) 
 205     for (int i 
= 0; i 
< n
; i
++) 
 207         DoodleLine 
*line 
= new DoodleLine
; 
 208         stream 
>> line
->x1 
>>  
 218 wxInputStream 
&DoodleSegment::LoadObject(wxInputStream
& stream
) 
 220     wxTextInputStream 
text_stream( stream 
); 
 225     for (int i 
= 0; i 
< n
; i
++) 
 227         DoodleLine 
*line 
= new DoodleLine
; 
 228         text_stream 
>> line
->x1 
>>  
 239 void DoodleSegment::Draw(wxDC 
*dc
) 
 241     wxNode 
*node 
= lines
.First(); 
 244         DoodleLine 
*line 
= (DoodleLine 
*)node
->Data(); 
 245         dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
); 
 251 * Implementation of drawing command 
 254 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument 
*ddoc
, DoodleSegment 
*seg
): 
 255 wxCommand(TRUE
, name
) 
 262 DrawingCommand::~DrawingCommand(void) 
 268 bool DrawingCommand::Do(void) 
 274             // Cut the last segment 
 275             if (doc
->GetDoodleSegments().Number() > 0) 
 277                 wxNode 
*node 
= doc
->GetDoodleSegments().Last(); 
 281                 segment 
= (DoodleSegment 
*)node
->Data(); 
 285                 doc
->UpdateAllViews(); 
 291             doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
)); 
 293             doc
->UpdateAllViews(); 
 300 bool DrawingCommand::Undo(void) 
 309                 doc
->GetDoodleSegments().Append(segment
); 
 311                 doc
->UpdateAllViews(); 
 312                 segment 
= (DoodleSegment 
*) NULL
; 
 315             doc
->UpdateAllViews(); 
 320             // Cut the last segment 
 321             if (doc
->GetDoodleSegments().Number() > 0) 
 323                 wxNode 
*node 
= doc
->GetDoodleSegments().Last(); 
 324                 DoodleSegment 
*seg 
= (DoodleSegment 
*)node
->Data(); 
 329                 doc
->UpdateAllViews(); 
 336 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
) 
 338 // Since text windows have their own method for saving to/loading from files, 
 339 // we override OnSave/OpenDocument instead of Save/LoadObject 
 340 bool TextEditDocument::OnSaveDocument(const wxString
& filename
) 
 342     TextEditView 
*view 
= (TextEditView 
*)GetFirstView(); 
 344     if (!view
->textsw
->SaveFile(filename
)) 
 348     wxFileName 
fn(filename
) ; 
 349     fn
.MacSetDefaultTypeAndCreator() ; 
 354 bool TextEditDocument::OnOpenDocument(const wxString
& filename
) 
 356     TextEditView 
*view 
= (TextEditView 
*)GetFirstView(); 
 357     if (!view
->textsw
->LoadFile(filename
)) 
 360     SetFilename(filename
, TRUE
); 
 366 bool TextEditDocument::IsModified(void) const 
 368     TextEditView 
*view 
= (TextEditView 
*)GetFirstView(); 
 371         return (wxDocument::IsModified() || view
->textsw
->IsModified()); 
 374         return wxDocument::IsModified(); 
 377 void TextEditDocument::Modify(bool mod
) 
 379     TextEditView 
*view 
= (TextEditView 
*)GetFirstView(); 
 381     wxDocument::Modify(mod
); 
 383     if (!mod 
&& view 
&& view
->textsw
) 
 384         view
->textsw
->DiscardEdits();