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