]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/docview.cpp
corrected getting FSRef associated with the POSIX path in wxString
[wxWidgets.git] / samples / docview / docview.cpp
CommitLineData
457814b5
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$
8// Copyright: (c) Julian Smart and Markus Holzem
2f6c54eb 9// Licence: wxWindows license
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13// #pragma implementation "docview.h"
14#endif
15
16/*
f6bcfd97
BP
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*/
457814b5
JS
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
e4b19d9b 34#if !wxUSE_DOC_VIEW_ARCHITECTURE
ad813b00 35#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
457814b5
JS
36#endif
37
38#include "wx/docview.h"
39
40#include "docview.h"
41#include "doc.h"
42#include "view.h"
43
c67daf87 44MyFrame *frame = (MyFrame *) NULL;
457814b5
JS
45
46// In single window mode, don't have any child windows; use
47// main window.
48bool singleWindowMode = FALSE;
49
50IMPLEMENT_APP(MyApp)
51
52MyApp::MyApp(void)
53{
c67daf87 54 m_docManager = (wxDocManager *) NULL;
457814b5
JS
55}
56
57bool MyApp::OnInit(void)
58{
f6bcfd97
BP
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)
457814b5 64 {
f6bcfd97
BP
65 if (wxStrcmp(argv[1], _T("-single")) == 0)
66 {
67 singleWindowMode = TRUE;
68 }
457814b5 69 }
f6bcfd97
BP
70
71 //// Create a document manager
72 m_docManager = new wxDocManager;
3d0145a5 73
f6bcfd97 74 //// Create a template relating drawing documents to their views
39960310 75 (void) new wxDocTemplate(m_docManager, _T("Drawing"), _T("*.drw"), _T(""), _T("drw"), _T("Drawing Doc"), _T("Drawing View"),
f6bcfd97
BP
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
39960310 87 (void) new wxDocTemplate(m_docManager, _T("Text"), _T("*.txt"), _T(""), _T("txt"), _T("Text Doc"), _T("Text View"),
f6bcfd97
BP
88 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
89
90 //// Create the main frame window
39960310 91 frame = new MyFrame(m_docManager, (wxFrame *) NULL, -1, _T("DocView Demo"), wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
f6bcfd97
BP
92
93 //// Give it an icon (this is ignored in MDI mode: uses resources)
2049ba38 94#ifdef __WXMSW__
39960310 95 frame->SetIcon(wxIcon(_T("doc_icn")));
457814b5 96#endif
f6bcfd97
BP
97
98 //// Make a menubar
99 wxMenu *file_menu = new wxMenu;
100 wxMenu *edit_menu = (wxMenu *) NULL;
101
39960310
MB
102 file_menu->Append(wxID_NEW, _T("&New..."));
103 file_menu->Append(wxID_OPEN, _T("&Open..."));
f6bcfd97
BP
104
105 if (singleWindowMode)
106 {
39960310
MB
107 file_menu->Append(wxID_CLOSE, _T("&Close"));
108 file_menu->Append(wxID_SAVE, _T("&Save"));
109 file_menu->Append(wxID_SAVEAS, _T("Save &As..."));
f6bcfd97 110 file_menu->AppendSeparator();
39960310
MB
111 file_menu->Append(wxID_PRINT, _T("&Print..."));
112 file_menu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
113 file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"));
f6bcfd97
BP
114
115 edit_menu = new wxMenu;
39960310
MB
116 edit_menu->Append(wxID_UNDO, _T("&Undo"));
117 edit_menu->Append(wxID_REDO, _T("&Redo"));
f6bcfd97 118 edit_menu->AppendSeparator();
39960310 119 edit_menu->Append(DOCVIEW_CUT, _T("&Cut last segment"));
f6bcfd97
BP
120
121 frame->editMenu = edit_menu;
122 }
123
457814b5 124 file_menu->AppendSeparator();
39960310 125 file_menu->Append(wxID_EXIT, _T("E&xit"));
f6bcfd97
BP
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;
39960310 131 help_menu->Append(DOCVIEW_ABOUT, _T("&About"));
f6bcfd97
BP
132
133 wxMenuBar *menu_bar = new wxMenuBar;
134
39960310 135 menu_bar->Append(file_menu, _T("&File"));
f6bcfd97 136 if (edit_menu)
39960310
MB
137 menu_bar->Append(edit_menu, _T("&Edit"));
138 menu_bar->Append(help_menu, _T("&Help"));
f6bcfd97
BP
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;
457814b5
JS
151}
152
153int MyApp::OnExit(void)
154{
155 delete m_docManager;
156 return 0;
157}
158
159/*
f6bcfd97
BP
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
457814b5
JS
165wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
166{
f6bcfd97 167 //// Make a child frame
39960310 168 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, GetMainFrame(), -1, _T("Child Frame"),
457814b5 169 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
f6bcfd97 170
2049ba38 171#ifdef __WXMSW__
39960310 172 subframe->SetIcon(wxString(isCanvas ? _T("chrt_icn") : _T("notepad_icn")));
457814b5 173#endif
f6bcfd97
BP
174
175 //// Make a menubar
176 wxMenu *file_menu = new wxMenu;
177
39960310
MB
178 file_menu->Append(wxID_NEW, _T("&New..."));
179 file_menu->Append(wxID_OPEN, _T("&Open..."));
180 file_menu->Append(wxID_CLOSE, _T("&Close"));
181 file_menu->Append(wxID_SAVE, _T("&Save"));
182 file_menu->Append(wxID_SAVEAS, _T("Save &As..."));
f6bcfd97
BP
183
184 if (isCanvas)
185 {
186 file_menu->AppendSeparator();
39960310
MB
187 file_menu->Append(wxID_PRINT, _T("&Print..."));
188 file_menu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
189 file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"));
f6bcfd97
BP
190 }
191
192 wxMenu *edit_menu = (wxMenu *) NULL;
193
194 if (isCanvas)
195 {
196 edit_menu = new wxMenu;
39960310
MB
197 edit_menu->Append(wxID_UNDO, _T("&Undo"));
198 edit_menu->Append(wxID_REDO, _T("&Redo"));
f6bcfd97 199 edit_menu->AppendSeparator();
39960310 200 edit_menu->Append(DOCVIEW_CUT, _T("&Cut last segment"));
f6bcfd97
BP
201
202 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
203 }
204
205 wxMenu *help_menu = new wxMenu;
39960310 206 help_menu->Append(DOCVIEW_ABOUT, _T("&About"));
f6bcfd97
BP
207
208 wxMenuBar *menu_bar = new wxMenuBar;
209
39960310 210 menu_bar->Append(file_menu, _T("&File"));
f6bcfd97 211 if (isCanvas)
39960310
MB
212 menu_bar->Append(edit_menu, _T("&Edit"));
213 menu_bar->Append(help_menu, _T("&Help"));
f6bcfd97
BP
214
215 //// Associate the menu bar with the frame
216 subframe->SetMenuBar(menu_bar);
217
218 subframe->Centre(wxBOTH);
219
220 return subframe;
457814b5
JS
221}
222
223/*
f6bcfd97
BP
224* This is the top-level window of the application.
225*/
226
457814b5
JS
227IMPLEMENT_CLASS(MyFrame, wxDocParentFrame)
228BEGIN_EVENT_TABLE(MyFrame, wxDocParentFrame)
229 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
230END_EVENT_TABLE()
231
2108f33a 232MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title,
f6bcfd97
BP
233 const wxPoint& pos, const wxSize& size, const long type):
234wxDocParentFrame(manager, frame, id, title, pos, size, type)
457814b5 235{
f6bcfd97
BP
236 // This pointer only needed if in single window mode
237 canvas = (MyCanvas *) NULL;
238 editMenu = (wxMenu *) NULL;
457814b5
JS
239}
240
e3e65dac 241void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
457814b5 242{
39960310 243 (void)wxMessageBox(_T("DocView Demo\nAuthor: Julian Smart\nUsage: docview.exe [-single]"), _T("About DocView"));
457814b5
JS
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.
249MyCanvas *MyFrame::CreateCanvas(wxView *view, wxFrame *parent)
250{
f6bcfd97
BP
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;
457814b5
JS
264}
265
266MyFrame *GetMainFrame(void)
267{
f6bcfd97 268 return frame;
457814b5
JS
269}
270