*** empty log message ***
[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 !USE_DOC_VIEW_ARCHITECTURE
35 #error You must set USE_DOC_VIEW_ARCHITECTURE to 1 in wx_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 = 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 = NULL;
55 }
56
57 bool MyApp::OnInit(void)
58 {
59 //// Find out if we're:
60 //// SDI : multiple windows and documents but not MDI
61 //// MDI : multiple windows and documents with containing frame - MSW only)
62 /// single window : (one document at a time, only one frame, as in Windows Write)
63 if (argc > 1)
64 {
65 if (strcmp(argv[1], "-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, NULL, "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 __WINDOWS__
95 frame->SetIcon(wxIcon("doc_icn"));
96 #endif
97 #ifdef __X__
98 frame->SetIcon(wxIcon("aiai.xbm"));
99 #endif
100
101 //// Make a menubar
102 wxMenu *file_menu = new wxMenu;
103 wxMenu *edit_menu = NULL;
104
105 file_menu->Append(wxID_NEW, "&New...");
106 file_menu->Append(wxID_OPEN, "&Open...");
107
108 if (singleWindowMode)
109 {
110 file_menu->Append(wxID_CLOSE, "&Close");
111 file_menu->Append(wxID_SAVE, "&Save");
112 file_menu->Append(wxID_SAVEAS, "Save &As...");
113 file_menu->AppendSeparator();
114 file_menu->Append(wxID_PRINT, "&Print...");
115 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
116 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
117
118 edit_menu = new wxMenu;
119 edit_menu->Append(wxID_UNDO, "&Undo");
120 edit_menu->Append(wxID_REDO, "&Redo");
121 edit_menu->AppendSeparator();
122 edit_menu->Append(DOCVIEW_CUT, "&Cut last segment");
123
124 frame->editMenu = edit_menu;
125 }
126
127 file_menu->AppendSeparator();
128 file_menu->Append(wxID_EXIT, "E&xit");
129
130 // A nice touch: a history of files visited. Use this menu.
131 m_docManager->FileHistoryUseMenu(file_menu);
132
133 wxMenu *help_menu = new wxMenu;
134 help_menu->Append(DOCVIEW_ABOUT, "&About");
135
136 wxMenuBar *menu_bar = new wxMenuBar;
137
138 menu_bar->Append(file_menu, "&File");
139 if (edit_menu)
140 menu_bar->Append(edit_menu, "&Edit");
141 menu_bar->Append(help_menu, "&Help");
142
143 if (singleWindowMode)
144 frame->canvas = frame->CreateCanvas(NULL, frame);
145
146 //// Associate the menu bar with the frame
147 frame->SetMenuBar(menu_bar);
148
149 frame->Centre(wxBOTH);
150 frame->Show(TRUE);
151
152 SetTopWindow(frame);
153 return TRUE;
154 }
155
156 int MyApp::OnExit(void)
157 {
158 delete m_docManager;
159 return 0;
160 }
161
162 /*
163 * Centralised code for creating a document frame.
164 * Called from view.cpp, when a view is created, but not used at all
165 * in 'single window' mode.
166 */
167
168 wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
169 {
170 //// Make a child frame
171 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, GetMainFrame(), "Child Frame",
172 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
173
174 #ifdef __WINDOWS__
175 subframe->SetIcon(wxString(isCanvas ? "chrt_icn" : "notepad_icn"));
176 #endif
177 #ifdef __X__
178 subframe->SetIcon(wxIcon("aiai.xbm"));
179 #endif
180
181 //// Make a menubar
182 wxMenu *file_menu = new wxMenu;
183
184 file_menu->Append(wxID_NEW, "&New...");
185 file_menu->Append(wxID_OPEN, "&Open...");
186 file_menu->Append(wxID_CLOSE, "&Close");
187 file_menu->Append(wxID_SAVE, "&Save");
188 file_menu->Append(wxID_SAVEAS, "Save &As...");
189
190 if (isCanvas)
191 {
192 file_menu->AppendSeparator();
193 file_menu->Append(wxID_PRINT, "&Print...");
194 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
195 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
196 }
197
198 wxMenu *edit_menu = NULL;
199
200 if (isCanvas)
201 {
202 edit_menu = new wxMenu;
203 edit_menu->Append(wxID_UNDO, "&Undo");
204 edit_menu->Append(wxID_REDO, "&Redo");
205 edit_menu->AppendSeparator();
206 edit_menu->Append(DOCVIEW_CUT, "&Cut last segment");
207
208 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
209 }
210
211 wxMenu *help_menu = new wxMenu;
212 help_menu->Append(DOCVIEW_ABOUT, "&About");
213
214 wxMenuBar *menu_bar = new wxMenuBar;
215
216 menu_bar->Append(file_menu, "&File");
217 if (isCanvas)
218 menu_bar->Append(edit_menu, "&Edit");
219 menu_bar->Append(help_menu, "&Help");
220
221 //// Associate the menu bar with the frame
222 subframe->SetMenuBar(menu_bar);
223
224 subframe->Centre(wxBOTH);
225
226 return subframe;
227 }
228
229 /*
230 * This is the top-level window of the application.
231 */
232
233 IMPLEMENT_CLASS(MyFrame, wxDocParentFrame)
234 BEGIN_EVENT_TABLE(MyFrame, wxDocParentFrame)
235 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
236 END_EVENT_TABLE()
237
238 MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, const wxString& title,
239 const wxPoint& pos, const wxSize& size, const long type):
240 wxDocParentFrame(manager, frame, title, pos, size, type)
241 {
242 // This pointer only needed if in single window mode
243 canvas = NULL;
244 editMenu = NULL;
245 }
246
247 void MyFrame::OnAbout(wxCommandEvent& event)
248 {
249 (void)wxMessageBox("DocView Demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\nUsage: docview.exe [-single]", "About DocView");
250 }
251
252 // Creates a canvas. Called either from view.cc when a new drawing
253 // view is created, or in OnInit as a child of the main window,
254 // if in 'single window' mode.
255 MyCanvas *MyFrame::CreateCanvas(wxView *view, wxFrame *parent)
256 {
257 int width, height;
258 parent->GetClientSize(&width, &height);
259
260 // Non-retained canvas
261 MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), wxSize(width, height), 0);
262 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
263
264 // Give it scrollbars
265 canvas->SetScrollbars(20, 20, 50, 50);
266
267 return canvas;
268 }
269
270 MyFrame *GetMainFrame(void)
271 {
272 return frame;
273 }
274