]> git.saurik.com Git - wxWidgets.git/blame - samples/docvwmdi/docview.cpp
Make scrollbars appear.
[wxWidgets.git] / samples / docvwmdi / docview.cpp
CommitLineData
2108f33a
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$
8// Copyright: (c) Julian Smart and Markus Holzem
2f6c54eb 9// Licence: wxWindows license
2108f33a
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13// #pragma implementation "docview.h"
14#endif
15
16/*
17 * Purpose: Document/view architecture demo for wxWindows class library - MDI
18 */
19
20
21// For compilers that support precompilation, includes "wx/wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25#pragma hdrstop
26#endif
27
28#ifndef WX_PRECOMP
29#include "wx/wx.h"
30#endif
31
e4b19d9b 32#if !wxUSE_DOC_VIEW_ARCHITECTURE
ad813b00 33#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
2108f33a
JS
34#endif
35
feea7c52
VZ
36#if !wxUSE_MDI_ARCHITECTURE
37#error You must set wxUSE_MDI_ARCHITECTURE to 1 in setup.h!
38#endif
39
2108f33a
JS
40#include "docview.h"
41#include "doc.h"
42#include "view.h"
43
c67daf87 44MyFrame *frame = (MyFrame *) NULL;
2108f33a
JS
45
46IMPLEMENT_APP(MyApp)
47
48MyApp::MyApp(void)
49{
c67daf87 50 m_docManager = (wxDocManager *) NULL;
2108f33a
JS
51}
52
53bool MyApp::OnInit(void)
54{
55 //// Create a document manager
56 m_docManager = new wxDocManager;
57
58 //// Create a template relating drawing documents to their views
c67daf87 59 (void) new wxDocTemplate((wxDocManager *) m_docManager, "Drawing", "*.drw", "", "drw", "Drawing Doc", "Drawing View",
2108f33a
JS
60 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
61
62 //// Create a template relating text documents to their views
63 (void) new wxDocTemplate(m_docManager, "Text", "*.txt", "", "txt", "Text Doc", "Text View",
64 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
65
66 //// Create the main frame window
1dbb34ec
VZ
67 frame = new MyFrame((wxDocManager *) m_docManager, (wxFrame *) NULL,
68 "DocView Demo", wxPoint(0, 0), wxSize(500, 400),
69 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE);
2108f33a
JS
70
71 //// Give it an icon (this is ignored in MDI mode: uses resources)
72#ifdef __WXMSW__
73 frame->SetIcon(wxIcon("doc"));
74#endif
75#ifdef __X__
76 frame->SetIcon(wxIcon("doc.xbm"));
77#endif
78
79 //// Make a menubar
80 wxMenu *file_menu = new wxMenu;
c67daf87 81 wxMenu *edit_menu = (wxMenu *) NULL;
2108f33a 82
f6bcfd97
BP
83 file_menu->Append(wxID_NEW, "&New...\tCtrl-N");
84 file_menu->Append(wxID_OPEN, "&Open...\tCtrl-X");
2108f33a
JS
85
86 file_menu->AppendSeparator();
f6bcfd97 87 file_menu->Append(wxID_EXIT, "E&xit\tAlt-X");
2108f33a
JS
88
89 // A nice touch: a history of files visited. Use this menu.
90 m_docManager->FileHistoryUseMenu(file_menu);
91
92 wxMenu *help_menu = new wxMenu;
f6bcfd97 93 help_menu->Append(DOCVIEW_ABOUT, "&About\tF1");
2108f33a
JS
94
95 wxMenuBar *menu_bar = new wxMenuBar;
96
97 menu_bar->Append(file_menu, "&File");
98 if (edit_menu)
99 menu_bar->Append(edit_menu, "&Edit");
100 menu_bar->Append(help_menu, "&Help");
101
102 //// Associate the menu bar with the frame
103 frame->SetMenuBar(menu_bar);
104
105 frame->Centre(wxBOTH);
106 frame->Show(TRUE);
107
108 SetTopWindow(frame);
109 return TRUE;
110}
111
112int MyApp::OnExit(void)
113{
114 delete m_docManager;
115 return 0;
116}
117
118/*
119 * Centralised code for creating a document frame.
120 * Called from view.cpp, when a view is created.
121 */
122
9746a2ba 123wxMDIChildFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
2108f33a
JS
124{
125 //// Make a child frame
1dbb34ec
VZ
126 wxDocMDIChildFrame *subframe =
127 new wxDocMDIChildFrame(doc, view, GetMainFrame(), -1, "Child Frame",
128 wxPoint(10, 10), wxSize(300, 300),
129 wxDEFAULT_FRAME_STYLE |
130 wxNO_FULL_REPAINT_ON_RESIZE);
2108f33a
JS
131
132#ifdef __WXMSW__
133 subframe->SetIcon(wxString(isCanvas ? "chart" : "notepad"));
134#endif
135#ifdef __X__
136 subframe->SetIcon(wxIcon("doc.xbm"));
137#endif
138
139 //// Make a menubar
140 wxMenu *file_menu = new wxMenu;
141
142 file_menu->Append(wxID_NEW, "&New...");
143 file_menu->Append(wxID_OPEN, "&Open...");
144 file_menu->Append(wxID_CLOSE, "&Close");
145 file_menu->Append(wxID_SAVE, "&Save");
146 file_menu->Append(wxID_SAVEAS, "Save &As...");
147
148 if (isCanvas)
149 {
150 file_menu->AppendSeparator();
151 file_menu->Append(wxID_PRINT, "&Print...");
152 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
153 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
154 }
155
156 file_menu->AppendSeparator();
157 file_menu->Append(wxID_EXIT, "E&xit");
158
c67daf87 159 wxMenu *edit_menu = (wxMenu *) NULL;
2108f33a
JS
160
161 if (isCanvas)
162 {
163 edit_menu = new wxMenu;
164 edit_menu->Append(wxID_UNDO, "&Undo");
165 edit_menu->Append(wxID_REDO, "&Redo");
166 edit_menu->AppendSeparator();
167 edit_menu->Append(DOCVIEW_CUT, "&Cut last segment");
168
169 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
170 }
171
172 wxMenu *help_menu = new wxMenu;
173 help_menu->Append(DOCVIEW_ABOUT, "&About");
174
175 wxMenuBar *menu_bar = new wxMenuBar;
176
177 menu_bar->Append(file_menu, "&File");
178 if (isCanvas)
179 menu_bar->Append(edit_menu, "&Edit");
180 menu_bar->Append(help_menu, "&Help");
181
182 //// Associate the menu bar with the frame
183 subframe->SetMenuBar(menu_bar);
184
185 return subframe;
186}
187
188/*
189 * This is the top-level window of the application.
190 */
191
192IMPLEMENT_CLASS(MyFrame, wxDocMDIParentFrame)
193BEGIN_EVENT_TABLE(MyFrame, wxDocMDIParentFrame)
194 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
195END_EVENT_TABLE()
196
197MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, const wxString& title,
198 const wxPoint& pos, const wxSize& size, long type):
199 wxDocMDIParentFrame(manager, frame, -1, title, pos, size, type, "myFrame")
200{
c67daf87 201 editMenu = (wxMenu *) NULL;
2108f33a
JS
202}
203
204void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
205{
206 (void)wxMessageBox("DocView Demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\nUsage: docview.exe", "About DocView");
207}
208
209// Creates a canvas. Called from view.cpp when a new drawing
210// view is created.
211MyCanvas *MyFrame::CreateCanvas(wxView *view, wxFrame *parent)
212{
213 int width, height;
214 parent->GetClientSize(&width, &height);
215
216 // Non-retained canvas
217 MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), wxSize(width, height), 0);
218 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
219
220 // Give it scrollbars
221 canvas->SetScrollbars(20, 20, 50, 50);
222
223 return canvas;
224}
225
226MyFrame *GetMainFrame(void)
227{
228 return frame;
229}
230