]> git.saurik.com Git - wxWidgets.git/blob - samples/docvwmdi/docview.cpp
* fixed a wrong line.
[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 and Markus Holzem
9 // Licence: wxWindows license
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
32 #if !USE_DOC_VIEW_ARCHITECTURE
33 #error You must set USE_DOC_VIEW_ARCHITECTURE to 1 in wx_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, "Drawing", "*.drw", "", "drw", "Drawing Doc", "Drawing View",
56 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
57
58 //// Create a template relating text documents to their views
59 (void) new wxDocTemplate(m_docManager, "Text", "*.txt", "", "txt", "Text Doc", "Text View",
60 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
61
62 //// Create the main frame window
63 frame = new MyFrame((wxDocManager *) m_docManager, (wxFrame *) NULL, (const wxString) "DocView Demo", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
64
65 //// Give it an icon (this is ignored in MDI mode: uses resources)
66 #ifdef __WXMSW__
67 frame->SetIcon(wxIcon("doc"));
68 #endif
69 #ifdef __X__
70 frame->SetIcon(wxIcon("doc.xbm"));
71 #endif
72
73 //// Make a menubar
74 wxMenu *file_menu = new wxMenu;
75 wxMenu *edit_menu = (wxMenu *) NULL;
76
77 file_menu->Append(wxID_NEW, "&New...");
78 file_menu->Append(wxID_OPEN, "&Open...");
79
80 file_menu->AppendSeparator();
81 file_menu->Append(wxID_EXIT, "E&xit");
82
83 // A nice touch: a history of files visited. Use this menu.
84 m_docManager->FileHistoryUseMenu(file_menu);
85
86 wxMenu *help_menu = new wxMenu;
87 help_menu->Append(DOCVIEW_ABOUT, "&About");
88
89 wxMenuBar *menu_bar = new wxMenuBar;
90
91 menu_bar->Append(file_menu, "&File");
92 if (edit_menu)
93 menu_bar->Append(edit_menu, "&Edit");
94 menu_bar->Append(help_menu, "&Help");
95
96 //// Associate the menu bar with the frame
97 frame->SetMenuBar(menu_bar);
98
99 frame->Centre(wxBOTH);
100 frame->Show(TRUE);
101
102 SetTopWindow(frame);
103 return TRUE;
104 }
105
106 int MyApp::OnExit(void)
107 {
108 delete m_docManager;
109 return 0;
110 }
111
112 /*
113 * Centralised code for creating a document frame.
114 * Called from view.cpp, when a view is created.
115 */
116
117 wxMDIChildFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
118 {
119 //// Make a child frame
120 wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame(doc, view, GetMainFrame(), -1, "Child Frame",
121 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
122
123 #ifdef __WXMSW__
124 subframe->SetIcon(wxString(isCanvas ? "chart" : "notepad"));
125 #endif
126 #ifdef __X__
127 subframe->SetIcon(wxIcon("doc.xbm"));
128 #endif
129
130 //// Make a menubar
131 wxMenu *file_menu = new wxMenu;
132
133 file_menu->Append(wxID_NEW, "&New...");
134 file_menu->Append(wxID_OPEN, "&Open...");
135 file_menu->Append(wxID_CLOSE, "&Close");
136 file_menu->Append(wxID_SAVE, "&Save");
137 file_menu->Append(wxID_SAVEAS, "Save &As...");
138
139 if (isCanvas)
140 {
141 file_menu->AppendSeparator();
142 file_menu->Append(wxID_PRINT, "&Print...");
143 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
144 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
145 }
146
147 file_menu->AppendSeparator();
148 file_menu->Append(wxID_EXIT, "E&xit");
149
150 wxMenu *edit_menu = (wxMenu *) NULL;
151
152 if (isCanvas)
153 {
154 edit_menu = new wxMenu;
155 edit_menu->Append(wxID_UNDO, "&Undo");
156 edit_menu->Append(wxID_REDO, "&Redo");
157 edit_menu->AppendSeparator();
158 edit_menu->Append(DOCVIEW_CUT, "&Cut last segment");
159
160 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
161 }
162
163 wxMenu *help_menu = new wxMenu;
164 help_menu->Append(DOCVIEW_ABOUT, "&About");
165
166 wxMenuBar *menu_bar = new wxMenuBar;
167
168 menu_bar->Append(file_menu, "&File");
169 if (isCanvas)
170 menu_bar->Append(edit_menu, "&Edit");
171 menu_bar->Append(help_menu, "&Help");
172
173 //// Associate the menu bar with the frame
174 subframe->SetMenuBar(menu_bar);
175
176 return subframe;
177 }
178
179 /*
180 * This is the top-level window of the application.
181 */
182
183 IMPLEMENT_CLASS(MyFrame, wxDocMDIParentFrame)
184 BEGIN_EVENT_TABLE(MyFrame, wxDocMDIParentFrame)
185 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
186 END_EVENT_TABLE()
187
188 MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, const wxString& title,
189 const wxPoint& pos, const wxSize& size, long type):
190 wxDocMDIParentFrame(manager, frame, -1, title, pos, size, type, "myFrame")
191 {
192 editMenu = (wxMenu *) NULL;
193 }
194
195 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
196 {
197 (void)wxMessageBox("DocView Demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\nUsage: docview.exe", "About DocView");
198 }
199
200 // Creates a canvas. Called from view.cpp when a new drawing
201 // view is created.
202 MyCanvas *MyFrame::CreateCanvas(wxView *view, wxFrame *parent)
203 {
204 int width, height;
205 parent->GetClientSize(&width, &height);
206
207 // Non-retained canvas
208 MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), wxSize(width, height), 0);
209 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
210
211 // Give it scrollbars
212 canvas->SetScrollbars(20, 20, 50, 50);
213
214 return canvas;
215 }
216
217 MyFrame *GetMainFrame(void)
218 {
219 return frame;
220 }
221