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