]>
Commit | Line | Data |
---|---|---|
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$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
2f6c54eb | 9 | // Licence: wxWindows license |
2108f33a JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2108f33a | 12 | /* |
be5a51fb | 13 | * Purpose: Document/view architecture demo for wxWidgets class library - MDI |
2108f33a JS |
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 | ||
e4b19d9b | 28 | #if !wxUSE_DOC_VIEW_ARCHITECTURE |
ad813b00 | 29 | #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h! |
2108f33a JS |
30 | #endif |
31 | ||
feea7c52 VZ |
32 | #if !wxUSE_MDI_ARCHITECTURE |
33 | #error You must set wxUSE_MDI_ARCHITECTURE to 1 in setup.h! | |
34 | #endif | |
35 | ||
2108f33a JS |
36 | #include "docview.h" |
37 | #include "doc.h" | |
38 | #include "view.h" | |
6bdf5153 | 39 | #include "wx/stockitem.h" |
2108f33a | 40 | |
6bdf5153 | 41 | static MyFrame* frame = NULL; |
2108f33a JS |
42 | |
43 | IMPLEMENT_APP(MyApp) | |
44 | ||
45 | MyApp::MyApp(void) | |
46 | { | |
6bdf5153 | 47 | m_docManager = NULL; |
2108f33a JS |
48 | } |
49 | ||
50 | bool MyApp::OnInit(void) | |
51 | { | |
6bdf5153 VZ |
52 | if ( !wxApp::OnInit() ) |
53 | return false; | |
54 | //// Create a document manager | |
55 | SetAppName(wxT("DocView Demo")); | |
2108f33a | 56 | |
6bdf5153 VZ |
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"), | |
2108f33a JS |
62 | CLASSINFO(DrawingDocument), CLASSINFO(DrawingView)); |
63 | ||
6bdf5153 VZ |
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"), | |
2108f33a JS |
66 | CLASSINFO(TextEditDocument), CLASSINFO(TextEditView)); |
67 | ||
6bdf5153 VZ |
68 | //// Create the main frame window |
69 | frame = new MyFrame(m_docManager, NULL, | |
70 | GetAppDisplayName(), wxPoint(0, 0), wxSize(500, 400), | |
1dbb34ec | 71 | wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE); |
2108f33a | 72 | |
6bdf5153 | 73 | //// Give it an icon (this is ignored in MDI mode: uses resources) |
2108f33a | 74 | #ifdef __WXMSW__ |
6bdf5153 | 75 | frame->SetIcon(wxIcon(wxT("doc"))); |
2108f33a | 76 | #endif |
2108f33a | 77 | |
6bdf5153 VZ |
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); | |
2108f33a | 84 | |
6bdf5153 VZ |
85 | file_menu->AppendSeparator(); |
86 | file_menu->Append(wxID_EXIT); | |
2108f33a | 87 | |
6bdf5153 VZ |
88 | // A nice touch: a history of files visited. Use this menu. |
89 | m_docManager->FileHistoryUseMenu(file_menu); | |
2108f33a | 90 | |
6bdf5153 VZ |
91 | wxMenu *help_menu = new wxMenu; |
92 | help_menu->Append(DOCVIEW_ABOUT); | |
2108f33a | 93 | |
6bdf5153 | 94 | wxMenuBar *menu_bar = new wxMenuBar; |
2108f33a | 95 | |
6bdf5153 VZ |
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)); | |
2108f33a | 100 | |
8eabf559 | 101 | #ifdef __WXMAC__ |
6bdf5153 | 102 | wxMenuBar::MacSetCommonMenuBar(menu_bar); |
8eabf559 | 103 | #endif //def __WXMAC__ |
6bdf5153 VZ |
104 | //// Associate the menu bar with the frame |
105 | frame->SetMenuBar(menu_bar); | |
2108f33a | 106 | |
6bdf5153 | 107 | frame->Centre(wxBOTH); |
8eabf559 | 108 | #ifndef __WXMAC__ |
6bdf5153 | 109 | frame->Show(true); |
8eabf559 | 110 | #endif //ndef __WXMAC__ |
2108f33a | 111 | |
6bdf5153 VZ |
112 | SetTopWindow(frame); |
113 | return true; | |
2108f33a JS |
114 | } |
115 | ||
116 | int MyApp::OnExit(void) | |
117 | { | |
6bdf5153 | 118 | wxDELETE(m_docManager) |
2108f33a JS |
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 | */ | |
6bdf5153 | 126 | |
9746a2ba | 127 | wxMDIChildFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas) |
2108f33a JS |
128 | { |
129 | //// Make a child frame | |
6bdf5153 VZ |
130 | wxDocMDIChildFrame *subframe = |
131 | new wxDocMDIChildFrame(doc, view, GetMainFrame(), wxID_ANY, wxT("Child Frame"), | |
1dbb34ec VZ |
132 | wxPoint(10, 10), wxSize(300, 300), |
133 | wxDEFAULT_FRAME_STYLE | | |
134 | wxNO_FULL_REPAINT_ON_RESIZE); | |
2108f33a JS |
135 | |
136 | #ifdef __WXMSW__ | |
6bdf5153 | 137 | subframe->SetIcon(wxString(isCanvas ? wxT("chart") : wxT("notepad"))); |
2108f33a JS |
138 | #endif |
139 | #ifdef __X__ | |
6bdf5153 | 140 | subframe->SetIcon(wxIcon(wxT("doc.xbm"))); |
2108f33a JS |
141 | #endif |
142 | ||
6bdf5153 VZ |
143 | //// Make a menubar |
144 | wxMenu *file_menu = new wxMenu; | |
2108f33a | 145 | |
6bdf5153 VZ |
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); | |
2108f33a | 151 | |
6bdf5153 VZ |
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 | } | |
2108f33a | 159 | |
6bdf5153 VZ |
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; | |
2108f33a JS |
192 | } |
193 | ||
194 | /* | |
195 | * This is the top-level window of the application. | |
196 | */ | |
6bdf5153 | 197 | |
2108f33a JS |
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): | |
6bdf5153 | 205 | wxDocMDIParentFrame(manager, frame, wxID_ANY, title, pos, size, type, wxT("myFrame")) |
2108f33a | 206 | { |
6bdf5153 | 207 | m_editMenu = NULL; |
2108f33a JS |
208 | } |
209 | ||
210 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
211 | { | |
6bdf5153 VZ |
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 | */ | |
2108f33a JS |
220 | } |
221 | ||
222 | // Creates a canvas. Called from view.cpp when a new drawing | |
223 | // view is created. | |
6bdf5153 | 224 | MyCanvas* MyFrame::CreateCanvas(DrawingView* view, wxMDIChildFrame* parent) |
2108f33a | 225 | { |
6bdf5153 | 226 | wxSize size = parent->GetClientSize(); |
2108f33a | 227 | |
6bdf5153 VZ |
228 | // Non-retained canvas |
229 | MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), size, 0); | |
230 | canvas->SetCursor(wxCursor(wxCURSOR_PENCIL)); | |
2108f33a | 231 | |
6bdf5153 VZ |
232 | // Give it scrollbars |
233 | canvas->SetScrollbars(20, 20, 50, 50); | |
2108f33a | 234 | |
6bdf5153 | 235 | return canvas; |
2108f33a JS |
236 | } |
237 | ||
6bdf5153 | 238 | MyFrame* GetMainFrame() |
2108f33a | 239 | { |
6bdf5153 | 240 | return frame; |
2108f33a JS |
241 | } |
242 |