]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/docview.cpp
initialize wxStringBuffer with the original contents of wxString it was created from...
[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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 * Purpose: Document/view architecture demo for wxWidgets class library
14 * Run with no arguments for multiple top-level windows, -single
15 * for a single window.
16 */
17
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #if !wxUSE_DOC_VIEW_ARCHITECTURE
31 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
32 #endif
33
34 #include "wx/docview.h"
35
36 #include "docview.h"
37 #include "doc.h"
38 #include "view.h"
39 #ifdef __WXMAC__
40 #include "wx/filename.h"
41 #endif
42 #include "wx/stockitem.h"
43
44 static 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 if ( !wxApp::OnInit() )
60 return false;
61 SetAppName(wxT("DocView Demo"));
62
63
64 //// Find out if we're:
65 //// multiple window: multiple windows, each view in a separate frame
66 //// single window: one view (within the main frame) and one document at a time, as in Windows Write.
67 //// In single window mode, we only allow one document type
68 if (argc > 1)
69 {
70 if (wxStrcmp(argv[1], wxT("-single")) == 0)
71 {
72 singleWindowMode = true;
73 }
74 }
75
76 //// Create a document manager
77 m_docManager = new wxDocManager;
78
79 //// Create a template relating drawing documents to their views
80 new wxDocTemplate(m_docManager, wxT("Drawing"), wxT("*.drw"), wxT(""), wxT("drw"), wxT("Drawing Doc"), wxT("Drawing View"),
81 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
82 #ifdef __WXMAC__
83 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("drw") , 'WXMB' , 'WXMA' ) ;
84 #endif
85
86 if (singleWindowMode)
87 {
88 // If we've only got one window, we only get to edit
89 // one document at a time. Therefore no text editing, just
90 // doodling.
91 m_docManager->SetMaxDocsOpen(1);
92 }
93 else
94 {
95 //// Create a template relating text documents to their views
96 new wxDocTemplate(m_docManager, wxT("Text"), wxT("*.txt;*.text"), wxT(""), wxT("txt;text"), wxT("Text Doc"), wxT("Text View"),
97 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
98 #ifdef __WXMAC__
99 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("txt") , 'TEXT' , 'WXMA' ) ;
100 #endif
101 }
102
103 //// Create the main frame window
104 frame = new MyFrame(m_docManager, NULL, wxID_ANY, GetAppDisplayName(), wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
105
106 //// Give it an icon (this is ignored in MDI mode: uses resources)
107 #ifdef __WXMSW__
108 frame->SetIcon(wxIcon(wxT("doc_icn")));
109 #endif
110
111 //// Make a menubar
112 wxMenu *file_menu = new wxMenu;
113 wxMenu *edit_menu = NULL;
114
115 file_menu->Append(wxID_NEW);
116 file_menu->Append(wxID_OPEN);
117
118 if (singleWindowMode)
119 {
120 file_menu->Append(wxID_CLOSE);
121 file_menu->Append(wxID_SAVE);
122 file_menu->Append(wxID_SAVEAS);
123 file_menu->AppendSeparator();
124 file_menu->Append(wxID_PRINT);
125 file_menu->Append(wxID_PRINT_SETUP, wxT("Print &Setup..."));
126 file_menu->Append(wxID_PREVIEW);
127
128 edit_menu = new wxMenu;
129 edit_menu->Append(wxID_UNDO);
130 edit_menu->Append(wxID_REDO);
131 edit_menu->AppendSeparator();
132 edit_menu->Append(DOCVIEW_CUT, wxT("&Cut last segment"));
133
134 frame->m_editMenu = edit_menu;
135 }
136
137 file_menu->AppendSeparator();
138 file_menu->Append(wxID_EXIT);
139
140 // A nice touch: a history of files visited. Use this menu.
141 m_docManager->FileHistoryUseMenu(file_menu);
142
143 wxMenu *help_menu = new wxMenu;
144 help_menu->Append(DOCVIEW_ABOUT);
145
146 wxMenuBar *menu_bar = new wxMenuBar;
147
148 menu_bar->Append(file_menu, wxGetStockLabel(wxID_FILE));
149 if (edit_menu)
150 menu_bar->Append(edit_menu, wxGetStockLabel(wxID_EDIT));
151 menu_bar->Append(help_menu, wxGetStockLabel(wxID_HELP));
152
153 if (singleWindowMode)
154 frame->m_canvas = frame->CreateCanvas(NULL, frame);
155
156 //// Associate the menu bar with the frame
157 frame->SetMenuBar(menu_bar);
158
159 frame->Centre(wxBOTH);
160 frame->Show(true);
161
162 SetTopWindow(frame);
163 return true;
164 }
165
166 int MyApp::OnExit(void)
167 {
168 delete m_docManager;
169 return 0;
170 }
171
172 /*
173 * Centralised code for creating a document frame.
174 * Called from view.cpp, when a view is created, but not used at all
175 * in 'single window' mode.
176 */
177
178 wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
179 {
180 //// Make a child frame
181 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, GetMainFrame(), wxID_ANY, wxT("Child Frame"),
182 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
183
184 #ifdef __WXMSW__
185 subframe->SetIcon(wxString(isCanvas ? wxT("chrt_icn") : wxT("notepad_icn")));
186 #endif
187
188 //// Make a menubar
189 wxMenu *file_menu = new wxMenu;
190
191 file_menu->Append(wxID_NEW);
192 file_menu->Append(wxID_OPEN);
193 file_menu->Append(wxID_CLOSE);
194 file_menu->Append(wxID_SAVE);
195 file_menu->Append(wxID_SAVEAS);
196
197 if (isCanvas)
198 {
199 file_menu->AppendSeparator();
200 file_menu->Append(wxID_PRINT);
201 file_menu->Append(wxID_PRINT_SETUP, wxT("Print &Setup..."));
202 file_menu->Append(wxID_PREVIEW);
203 }
204
205 wxMenu *edit_menu = new wxMenu;
206
207 if (isCanvas)
208 {
209 edit_menu->Append(wxID_UNDO);
210 edit_menu->Append(wxID_REDO);
211 edit_menu->AppendSeparator();
212 edit_menu->Append(DOCVIEW_CUT, wxT("&Cut last segment"));
213
214 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
215 }
216 else
217 {
218 edit_menu->Append(wxID_COPY);
219 edit_menu->Append(wxID_PASTE);
220 edit_menu->Append(wxID_SELECTALL);
221 }
222
223 wxMenu *help_menu = new wxMenu;
224 help_menu->Append(DOCVIEW_ABOUT);
225
226 wxMenuBar *menu_bar = new wxMenuBar;
227
228 menu_bar->Append(file_menu, wxGetStockLabel(wxID_FILE));
229 menu_bar->Append(edit_menu, wxGetStockLabel(wxID_EDIT));
230 menu_bar->Append(help_menu, wxGetStockLabel(wxID_HELP));
231
232 //// Associate the menu bar with the frame
233 subframe->SetMenuBar(menu_bar);
234
235 subframe->Centre(wxBOTH);
236
237 return subframe;
238 }
239
240 /*
241 * This is the top-level window of the application.
242 */
243
244 IMPLEMENT_CLASS(MyFrame, wxDocParentFrame)
245 BEGIN_EVENT_TABLE(MyFrame, wxDocParentFrame)
246 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
247 END_EVENT_TABLE()
248
249 MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title,
250 const wxPoint& pos, const wxSize& size, const long type):
251 wxDocParentFrame(manager, frame, id, title, pos, size, type)
252 {
253 // This pointer only needed if in single window mode
254 m_canvas = NULL;
255 m_editMenu = NULL;
256 }
257
258 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
259 {
260 wxMessageBox(wxT("DocView Demo\nAuthor: Julian Smart\nUsage: docview.exe [-single]"), wxT("About DocView"));
261 /*
262 Better, but brings in adv lib
263 wxAboutDialogInfo info;
264 info.SetName(wxTheApp->GetAppDisplayName());
265 info.AddDeveloper(wxT("Julian Smart"));
266 wxAboutBox(info);
267 */
268 }
269
270 // Creates a canvas. Called either from view.cc when a new drawing
271 // view is created, or in OnInit as a child of the main window,
272 // if in 'single window' mode.
273 MyCanvas *MyFrame::CreateCanvas(DrawingView* view, wxFrame *parent)
274 {
275 wxSize size = parent->GetClientSize();
276
277 // Non-retained canvas
278 MyCanvas* canvas = new MyCanvas(view, parent, wxPoint(0, 0), size, 0);
279 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
280
281 // Give it scrollbars
282 canvas->SetScrollbars(20, 20, 50, 50);
283 canvas->SetBackgroundColour(*wxWHITE);
284 canvas->ClearBackground();
285
286 return canvas;
287 }
288
289 MyFrame *GetMainFrame(void)
290 {
291 return frame;
292 }
293