]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/docview.cpp
added a note about the constraints being deprecated
[wxWidgets.git] / samples / docview / 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
18 * Run with no arguments for multiple top-level windows, -single
19 * for a single window.
20 */
21
22
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/wx.h"
32 #endif
33
34 #if !wxUSE_DOC_VIEW_ARCHITECTURE
35 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
36 #endif
37
38 #include "wx/docview.h"
39
40 #include "docview.h"
41 #include "doc.h"
42 #include "view.h"
43
44 MyFrame *frame = (MyFrame *) NULL;
45
46 // In single window mode, don't have any child windows; use
47 // main window.
48 bool singleWindowMode = FALSE;
49
50 IMPLEMENT_APP(MyApp)
51
52 MyApp::MyApp(void)
53 {
54 m_docManager = (wxDocManager *) NULL;
55 }
56
57 bool MyApp::OnInit(void)
58 {
59 //// Find out if we're:
60 //// multiple window: multiple windows, each view in a separate frame
61 //// single window: one view (within the main frame) and one document at a time, as in Windows Write.
62 //// In single window mode, we only allow one document type
63 if (argc > 1)
64 {
65 if (wxStrcmp(argv[1], _T("-single")) == 0)
66 {
67 singleWindowMode = TRUE;
68 }
69 }
70
71 //// Create a document manager
72 m_docManager = new wxDocManager;
73
74 //// Create a template relating drawing documents to their views
75 (void) new wxDocTemplate(m_docManager, "Drawing", "*.drw", "", "drw", "Drawing Doc", "Drawing View",
76 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
77
78 if (singleWindowMode)
79 {
80 // If we've only got one window, we only get to edit
81 // one document at a time. Therefore no text editing, just
82 // doodling.
83 m_docManager->SetMaxDocsOpen(1);
84 }
85 else
86 //// Create a template relating text documents to their views
87 (void) new wxDocTemplate(m_docManager, "Text", "*.txt", "", "txt", "Text Doc", "Text View",
88 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
89
90 //// Create the main frame window
91 frame = new MyFrame(m_docManager, (wxFrame *) NULL, -1, "DocView Demo", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
92
93 //// Give it an icon (this is ignored in MDI mode: uses resources)
94 #ifdef __WXMSW__
95 frame->SetIcon(wxIcon("doc_icn"));
96 #endif
97
98 //// Make a menubar
99 wxMenu *file_menu = new wxMenu;
100 wxMenu *edit_menu = (wxMenu *) NULL;
101
102 file_menu->Append(wxID_NEW, "&New...");
103 file_menu->Append(wxID_OPEN, "&Open...");
104
105 if (singleWindowMode)
106 {
107 file_menu->Append(wxID_CLOSE, "&Close");
108 file_menu->Append(wxID_SAVE, "&Save");
109 file_menu->Append(wxID_SAVEAS, "Save &As...");
110 file_menu->AppendSeparator();
111 file_menu->Append(wxID_PRINT, "&Print...");
112 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
113 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
114
115 edit_menu = new wxMenu;
116 edit_menu->Append(wxID_UNDO, "&Undo");
117 edit_menu->Append(wxID_REDO, "&Redo");
118 edit_menu->AppendSeparator();
119 edit_menu->Append(DOCVIEW_CUT, "&Cut last segment");
120
121 frame->editMenu = edit_menu;
122 }
123
124 file_menu->AppendSeparator();
125 file_menu->Append(wxID_EXIT, "E&xit");
126
127 // A nice touch: a history of files visited. Use this menu.
128 m_docManager->FileHistoryUseMenu(file_menu);
129
130 wxMenu *help_menu = new wxMenu;
131 help_menu->Append(DOCVIEW_ABOUT, "&About");
132
133 wxMenuBar *menu_bar = new wxMenuBar;
134
135 menu_bar->Append(file_menu, "&File");
136 if (edit_menu)
137 menu_bar->Append(edit_menu, "&Edit");
138 menu_bar->Append(help_menu, "&Help");
139
140 if (singleWindowMode)
141 frame->canvas = frame->CreateCanvas((wxView *) NULL, frame);
142
143 //// Associate the menu bar with the frame
144 frame->SetMenuBar(menu_bar);
145
146 frame->Centre(wxBOTH);
147 frame->Show(TRUE);
148
149 SetTopWindow(frame);
150 return TRUE;
151 }
152
153 int MyApp::OnExit(void)
154 {
155 delete m_docManager;
156 return 0;
157 }
158
159 /*
160 * Centralised code for creating a document frame.
161 * Called from view.cpp, when a view is created, but not used at all
162 * in 'single window' mode.
163 */
164
165 wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
166 {
167 //// Make a child frame
168 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, GetMainFrame(), -1, "Child Frame",
169 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
170
171 #ifdef __WXMSW__
172 subframe->SetIcon(wxString(isCanvas ? "chrt_icn" : "notepad_icn"));
173 #endif
174
175 //// Make a menubar
176 wxMenu *file_menu = new wxMenu;
177
178 file_menu->Append(wxID_NEW, "&New...");
179 file_menu->Append(wxID_OPEN, "&Open...");
180 file_menu->Append(wxID_CLOSE, "&Close");
181 file_menu->Append(wxID_SAVE, "&Save");
182 file_menu->Append(wxID_SAVEAS, "Save &As...");
183
184 if (isCanvas)
185 {
186 file_menu->AppendSeparator();
187 file_menu->Append(wxID_PRINT, "&Print...");
188 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
189 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
190 }
191
192 wxMenu *edit_menu = (wxMenu *) NULL;
193
194 if (isCanvas)
195 {
196 edit_menu = new wxMenu;
197 edit_menu->Append(wxID_UNDO, "&Undo");
198 edit_menu->Append(wxID_REDO, "&Redo");
199 edit_menu->AppendSeparator();
200 edit_menu->Append(DOCVIEW_CUT, "&Cut last segment");
201
202 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
203 }
204
205 wxMenu *help_menu = new wxMenu;
206 help_menu->Append(DOCVIEW_ABOUT, "&About");
207
208 wxMenuBar *menu_bar = new wxMenuBar;
209
210 menu_bar->Append(file_menu, "&File");
211 if (isCanvas)
212 menu_bar->Append(edit_menu, "&Edit");
213 menu_bar->Append(help_menu, "&Help");
214
215 //// Associate the menu bar with the frame
216 subframe->SetMenuBar(menu_bar);
217
218 subframe->Centre(wxBOTH);
219
220 return subframe;
221 }
222
223 /*
224 * This is the top-level window of the application.
225 */
226
227 IMPLEMENT_CLASS(MyFrame, wxDocParentFrame)
228 BEGIN_EVENT_TABLE(MyFrame, wxDocParentFrame)
229 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
230 END_EVENT_TABLE()
231
232 MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title,
233 const wxPoint& pos, const wxSize& size, const long type):
234 wxDocParentFrame(manager, frame, id, title, pos, size, type)
235 {
236 // This pointer only needed if in single window mode
237 canvas = (MyCanvas *) NULL;
238 editMenu = (wxMenu *) NULL;
239 }
240
241 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
242 {
243 (void)wxMessageBox("DocView Demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\nUsage: docview.exe [-single]", "About DocView");
244 }
245
246 // Creates a canvas. Called either from view.cc when a new drawing
247 // view is created, or in OnInit as a child of the main window,
248 // if in 'single window' mode.
249 MyCanvas *MyFrame::CreateCanvas(wxView *view, wxFrame *parent)
250 {
251 int width, height;
252 parent->GetClientSize(&width, &height);
253
254 // Non-retained canvas
255 MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), wxSize(width, height), 0);
256 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
257
258 // Give it scrollbars
259 canvas->SetScrollbars(20, 20, 50, 50);
260 canvas->SetBackgroundColour(*wxWHITE);
261 canvas->Clear();
262
263 return canvas;
264 }
265
266 MyFrame *GetMainFrame(void)
267 {
268 return frame;
269 }
270