]> git.saurik.com Git - wxWidgets.git/blob - samples/docvwmdi/docview.cpp
fix crash which happened if you called SetAttr(NULL) followed by SetAttr(attr) (...
[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 #include "wx/stockitem.h"
40
41 static MyFrame* frame = NULL;
42
43 IMPLEMENT_APP(MyApp)
44
45 MyApp::MyApp(void)
46 {
47 m_docManager = NULL;
48 }
49
50 bool MyApp::OnInit(void)
51 {
52 if ( !wxApp::OnInit() )
53 return false;
54 //// Create a document manager
55 SetAppName(wxT("DocView Demo"));
56
57 //// Create a document manager
58 m_docManager = new wxDocManager;
59
60 //// Create a template relating drawing documents to their views
61 new wxDocTemplate(m_docManager, wxT("Drawing"), wxT("*.drw"), wxT(""), wxT("drw"), wxT("Drawing Doc"), wxT("Drawing View"),
62 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
63
64 //// Create a template relating text documents to their views
65 new wxDocTemplate(m_docManager, wxT("Text"), wxT("*.txt"), wxT(""), wxT("txt"), wxT("Text Doc"), wxT("Text View"),
66 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
67
68 //// Create the main frame window
69 frame = new MyFrame(m_docManager, NULL,
70 GetAppDisplayName(), wxPoint(0, 0), wxSize(500, 400),
71 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE);
72
73 //// Give it an icon (this is ignored in MDI mode: uses resources)
74 #ifdef __WXMSW__
75 frame->SetIcon(wxIcon(wxT("doc")));
76 #endif
77
78 //// Make a menubar
79 wxMenu *file_menu = new wxMenu;
80 wxMenu *edit_menu = NULL;
81
82 file_menu->Append(wxID_NEW);
83 file_menu->Append(wxID_OPEN);
84
85 file_menu->AppendSeparator();
86 file_menu->Append(wxID_EXIT);
87
88 // A nice touch: a history of files visited. Use this menu.
89 m_docManager->FileHistoryUseMenu(file_menu);
90
91 wxMenu *help_menu = new wxMenu;
92 help_menu->Append(DOCVIEW_ABOUT);
93
94 wxMenuBar *menu_bar = new wxMenuBar;
95
96 menu_bar->Append(file_menu, wxGetStockLabel(wxID_FILE));
97 if (edit_menu)
98 menu_bar->Append(edit_menu, wxGetStockLabel(wxID_EDIT));
99 menu_bar->Append(help_menu, wxGetStockLabel(wxID_HELP));
100
101 #ifdef __WXMAC__
102 wxMenuBar::MacSetCommonMenuBar(menu_bar);
103 #endif //def __WXMAC__
104 //// Associate the menu bar with the frame
105 frame->SetMenuBar(menu_bar);
106
107 frame->Centre(wxBOTH);
108 #ifndef __WXMAC__
109 frame->Show(true);
110 #endif //ndef __WXMAC__
111
112 SetTopWindow(frame);
113 return true;
114 }
115
116 int MyApp::OnExit(void)
117 {
118 wxDELETE(m_docManager)
119 return 0;
120 }
121
122 /*
123 * Centralised code for creating a document frame.
124 * Called from view.cpp, when a view is created.
125 */
126
127 wxMDIChildFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
128 {
129 //// Make a child frame
130 wxDocMDIChildFrame *subframe =
131 new wxDocMDIChildFrame(doc, view, GetMainFrame(), wxID_ANY, wxT("Child Frame"),
132 wxPoint(10, 10), wxSize(300, 300),
133 wxDEFAULT_FRAME_STYLE |
134 wxNO_FULL_REPAINT_ON_RESIZE);
135
136 #ifdef __WXMSW__
137 subframe->SetIcon(wxString(isCanvas ? wxT("chart") : wxT("notepad")));
138 #endif
139 #ifdef __X__
140 subframe->SetIcon(wxIcon(wxT("doc.xbm")));
141 #endif
142
143 //// Make a menubar
144 wxMenu *file_menu = new wxMenu;
145
146 file_menu->Append(wxID_NEW);
147 file_menu->Append(wxID_OPEN);
148 file_menu->Append(wxID_CLOSE);
149 file_menu->Append(wxID_SAVE);
150 file_menu->Append(wxID_SAVEAS);
151
152 if (isCanvas)
153 {
154 file_menu->AppendSeparator();
155 file_menu->Append(wxID_PRINT);
156 file_menu->Append(wxID_PRINT_SETUP, wxT("Print &Setup..."));
157 file_menu->Append(wxID_PREVIEW);
158 }
159
160 file_menu->AppendSeparator();
161 file_menu->Append(wxID_EXIT);
162
163 wxMenu *edit_menu = new wxMenu;
164 if (isCanvas)
165 {
166 edit_menu->Append(wxID_UNDO);
167 edit_menu->Append(wxID_REDO);
168 edit_menu->AppendSeparator();
169 edit_menu->Append(DOCVIEW_CUT, wxT("&Cut last segment"));
170
171 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
172 }
173 else
174 {
175 edit_menu->Append(wxID_COPY);
176 edit_menu->Append(wxID_PASTE);
177 edit_menu->Append(wxID_SELECTALL);
178 }
179 wxMenu *help_menu = new wxMenu;
180 help_menu->Append(DOCVIEW_ABOUT);
181
182 wxMenuBar *menu_bar = new wxMenuBar;
183
184 menu_bar->Append(file_menu, wxGetStockLabel(wxID_FILE));
185 menu_bar->Append(edit_menu, wxGetStockLabel(wxID_EDIT));
186 menu_bar->Append(help_menu, wxGetStockLabel(wxID_HELP));
187
188 //// Associate the menu bar with the frame
189 subframe->SetMenuBar(menu_bar);
190
191 return subframe;
192 }
193
194 /*
195 * This is the top-level window of the application.
196 */
197
198 IMPLEMENT_CLASS(MyFrame, wxDocMDIParentFrame)
199 BEGIN_EVENT_TABLE(MyFrame, wxDocMDIParentFrame)
200 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
201 END_EVENT_TABLE()
202
203 MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, const wxString& title,
204 const wxPoint& pos, const wxSize& size, long type):
205 wxDocMDIParentFrame(manager, frame, wxID_ANY, title, pos, size, type, wxT("myFrame"))
206 {
207 m_editMenu = NULL;
208 }
209
210 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
211 {
212 wxMessageBox(wxT("DocView Demo\nAuthor: Julian Smart\nUsage: docview.exe"), wxT("About DocView"));
213 /*
214 Better, but brings in adv lib
215 wxAboutDialogInfo info;
216 info.SetName(wxTheApp->GetAppDisplayName());
217 info.AddDeveloper(wxT("Julian Smart"));
218 wxAboutBox(info);
219 */
220 }
221
222 // Creates a canvas. Called from view.cpp when a new drawing
223 // view is created.
224 MyCanvas* MyFrame::CreateCanvas(DrawingView* view, wxMDIChildFrame* parent)
225 {
226 wxSize size = parent->GetClientSize();
227
228 // Non-retained canvas
229 MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), size, 0);
230 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
231
232 // Give it scrollbars
233 canvas->SetScrollbars(20, 20, 50, 50);
234
235 return canvas;
236 }
237
238 MyFrame* GetMainFrame()
239 {
240 return frame;
241 }
242