]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/docview.cpp
use buffered DC again
[wxWidgets.git] / samples / docview / docview.cpp
CommitLineData
457814b5
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: docview.cpp
3// Purpose: Document/view demo
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows license
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
457814b5 12/*
be5a51fb 13* Purpose: Document/view architecture demo for wxWidgets class library
f6bcfd97
BP
14* Run with no arguments for multiple top-level windows, -single
15* for a single window.
16*/
457814b5
JS
17
18
19// For compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23#pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27#include "wx/wx.h"
28#endif
29
e4b19d9b 30#if !wxUSE_DOC_VIEW_ARCHITECTURE
ad813b00 31#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
457814b5
JS
32#endif
33
34#include "wx/docview.h"
35
36#include "docview.h"
37#include "doc.h"
38#include "view.h"
eb017efe
SC
39#ifdef __WXMAC__
40#include "wx/filename.h"
41#endif
457814b5 42
c67daf87 43MyFrame *frame = (MyFrame *) NULL;
457814b5
JS
44
45// In single window mode, don't have any child windows; use
46// main window.
f2aea0d1 47bool singleWindowMode = false;
457814b5
JS
48
49IMPLEMENT_APP(MyApp)
50
51MyApp::MyApp(void)
52{
c67daf87 53 m_docManager = (wxDocManager *) NULL;
457814b5
JS
54}
55
56bool MyApp::OnInit(void)
57{
45e6e6f8
VZ
58 if ( !wxApp::OnInit() )
59 return false;
60
f6bcfd97
BP
61 //// Find out if we're:
62 //// multiple window: multiple windows, each view in a separate frame
63 //// single window: one view (within the main frame) and one document at a time, as in Windows Write.
64 //// In single window mode, we only allow one document type
65 if (argc > 1)
457814b5 66 {
f6bcfd97
BP
67 if (wxStrcmp(argv[1], _T("-single")) == 0)
68 {
f2aea0d1 69 singleWindowMode = true;
f6bcfd97 70 }
457814b5 71 }
f6bcfd97
BP
72
73 //// Create a document manager
74 m_docManager = new wxDocManager;
3d0145a5 75
f6bcfd97 76 //// Create a template relating drawing documents to their views
39960310 77 (void) new wxDocTemplate(m_docManager, _T("Drawing"), _T("*.drw"), _T(""), _T("drw"), _T("Drawing Doc"), _T("Drawing View"),
f6bcfd97 78 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
eb017efe 79#ifdef __WXMAC__
162d656d 80 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("drw") , 'WXMB' , 'WXMA' ) ;
eb017efe 81#endif
f6bcfd97
BP
82
83 if (singleWindowMode)
84 {
85 // If we've only got one window, we only get to edit
86 // one document at a time. Therefore no text editing, just
87 // doodling.
88 m_docManager->SetMaxDocsOpen(1);
89 }
90 else
eb017efe 91 {
f6bcfd97 92 //// Create a template relating text documents to their views
2baf6272 93 (void) new wxDocTemplate(m_docManager, _T("Text"), _T("*.txt;*.text"), _T(""), _T("txt;text"), _T("Text Doc"), _T("Text View"),
f6bcfd97 94 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
eb017efe 95#ifdef __WXMAC__
162d656d 96 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("txt") , 'TEXT' , 'WXMA' ) ;
eb017efe
SC
97#endif
98 }
f6bcfd97
BP
99
100 //// Create the main frame window
f2aea0d1 101 frame = new MyFrame(m_docManager, (wxFrame *) NULL, wxID_ANY, _T("DocView Demo"), wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
f6bcfd97
BP
102
103 //// Give it an icon (this is ignored in MDI mode: uses resources)
2049ba38 104#ifdef __WXMSW__
39960310 105 frame->SetIcon(wxIcon(_T("doc_icn")));
457814b5 106#endif
f6bcfd97
BP
107
108 //// Make a menubar
109 wxMenu *file_menu = new wxMenu;
110 wxMenu *edit_menu = (wxMenu *) NULL;
111
39960310
MB
112 file_menu->Append(wxID_NEW, _T("&New..."));
113 file_menu->Append(wxID_OPEN, _T("&Open..."));
f6bcfd97
BP
114
115 if (singleWindowMode)
116 {
39960310
MB
117 file_menu->Append(wxID_CLOSE, _T("&Close"));
118 file_menu->Append(wxID_SAVE, _T("&Save"));
119 file_menu->Append(wxID_SAVEAS, _T("Save &As..."));
f6bcfd97 120 file_menu->AppendSeparator();
39960310
MB
121 file_menu->Append(wxID_PRINT, _T("&Print..."));
122 file_menu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
123 file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"));
f6bcfd97
BP
124
125 edit_menu = new wxMenu;
39960310
MB
126 edit_menu->Append(wxID_UNDO, _T("&Undo"));
127 edit_menu->Append(wxID_REDO, _T("&Redo"));
f6bcfd97 128 edit_menu->AppendSeparator();
39960310 129 edit_menu->Append(DOCVIEW_CUT, _T("&Cut last segment"));
f6bcfd97
BP
130
131 frame->editMenu = edit_menu;
132 }
133
457814b5 134 file_menu->AppendSeparator();
39960310 135 file_menu->Append(wxID_EXIT, _T("E&xit"));
f6bcfd97
BP
136
137 // A nice touch: a history of files visited. Use this menu.
138 m_docManager->FileHistoryUseMenu(file_menu);
139
140 wxMenu *help_menu = new wxMenu;
39960310 141 help_menu->Append(DOCVIEW_ABOUT, _T("&About"));
f6bcfd97
BP
142
143 wxMenuBar *menu_bar = new wxMenuBar;
144
39960310 145 menu_bar->Append(file_menu, _T("&File"));
f6bcfd97 146 if (edit_menu)
39960310
MB
147 menu_bar->Append(edit_menu, _T("&Edit"));
148 menu_bar->Append(help_menu, _T("&Help"));
f6bcfd97
BP
149
150 if (singleWindowMode)
151 frame->canvas = frame->CreateCanvas((wxView *) NULL, frame);
152
153 //// Associate the menu bar with the frame
154 frame->SetMenuBar(menu_bar);
155
156 frame->Centre(wxBOTH);
f2aea0d1 157 frame->Show(true);
f6bcfd97
BP
158
159 SetTopWindow(frame);
f2aea0d1 160 return true;
457814b5
JS
161}
162
163int MyApp::OnExit(void)
164{
165 delete m_docManager;
166 return 0;
167}
168
169/*
f6bcfd97
BP
170* Centralised code for creating a document frame.
171* Called from view.cpp, when a view is created, but not used at all
172* in 'single window' mode.
173*/
174
457814b5
JS
175wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
176{
f6bcfd97 177 //// Make a child frame
f2aea0d1 178 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, GetMainFrame(), wxID_ANY, _T("Child Frame"),
457814b5 179 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
f6bcfd97 180
2049ba38 181#ifdef __WXMSW__
39960310 182 subframe->SetIcon(wxString(isCanvas ? _T("chrt_icn") : _T("notepad_icn")));
457814b5 183#endif
f6bcfd97
BP
184
185 //// Make a menubar
186 wxMenu *file_menu = new wxMenu;
187
39960310
MB
188 file_menu->Append(wxID_NEW, _T("&New..."));
189 file_menu->Append(wxID_OPEN, _T("&Open..."));
190 file_menu->Append(wxID_CLOSE, _T("&Close"));
191 file_menu->Append(wxID_SAVE, _T("&Save"));
192 file_menu->Append(wxID_SAVEAS, _T("Save &As..."));
f6bcfd97
BP
193
194 if (isCanvas)
195 {
196 file_menu->AppendSeparator();
39960310
MB
197 file_menu->Append(wxID_PRINT, _T("&Print..."));
198 file_menu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
199 file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"));
f6bcfd97
BP
200 }
201
202 wxMenu *edit_menu = (wxMenu *) NULL;
203
204 if (isCanvas)
205 {
206 edit_menu = new wxMenu;
39960310
MB
207 edit_menu->Append(wxID_UNDO, _T("&Undo"));
208 edit_menu->Append(wxID_REDO, _T("&Redo"));
f6bcfd97 209 edit_menu->AppendSeparator();
39960310 210 edit_menu->Append(DOCVIEW_CUT, _T("&Cut last segment"));
f6bcfd97
BP
211
212 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
213 }
214
215 wxMenu *help_menu = new wxMenu;
39960310 216 help_menu->Append(DOCVIEW_ABOUT, _T("&About"));
f6bcfd97
BP
217
218 wxMenuBar *menu_bar = new wxMenuBar;
219
39960310 220 menu_bar->Append(file_menu, _T("&File"));
f6bcfd97 221 if (isCanvas)
39960310
MB
222 menu_bar->Append(edit_menu, _T("&Edit"));
223 menu_bar->Append(help_menu, _T("&Help"));
f6bcfd97
BP
224
225 //// Associate the menu bar with the frame
226 subframe->SetMenuBar(menu_bar);
227
228 subframe->Centre(wxBOTH);
229
230 return subframe;
457814b5
JS
231}
232
233/*
f6bcfd97
BP
234* This is the top-level window of the application.
235*/
236
457814b5
JS
237IMPLEMENT_CLASS(MyFrame, wxDocParentFrame)
238BEGIN_EVENT_TABLE(MyFrame, wxDocParentFrame)
239 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
240END_EVENT_TABLE()
241
2108f33a 242MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title,
f6bcfd97
BP
243 const wxPoint& pos, const wxSize& size, const long type):
244wxDocParentFrame(manager, frame, id, title, pos, size, type)
457814b5 245{
f6bcfd97
BP
246 // This pointer only needed if in single window mode
247 canvas = (MyCanvas *) NULL;
248 editMenu = (wxMenu *) NULL;
457814b5
JS
249}
250
e3e65dac 251void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
457814b5 252{
39960310 253 (void)wxMessageBox(_T("DocView Demo\nAuthor: Julian Smart\nUsage: docview.exe [-single]"), _T("About DocView"));
457814b5
JS
254}
255
256// Creates a canvas. Called either from view.cc when a new drawing
257// view is created, or in OnInit as a child of the main window,
258// if in 'single window' mode.
259MyCanvas *MyFrame::CreateCanvas(wxView *view, wxFrame *parent)
260{
f6bcfd97
BP
261 int width, height;
262 parent->GetClientSize(&width, &height);
263
264 // Non-retained canvas
265 MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), wxSize(width, height), 0);
266 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
267
268 // Give it scrollbars
269 canvas->SetScrollbars(20, 20, 50, 50);
270 canvas->SetBackgroundColour(*wxWHITE);
1d14c005 271 canvas->ClearBackground();
f6bcfd97
BP
272
273 return canvas;
457814b5
JS
274}
275
276MyFrame *GetMainFrame(void)
277{
f6bcfd97 278 return frame;
457814b5
JS
279}
280