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