]>
git.saurik.com Git - wxWidgets.git/blob - samples/docview/docview.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Document/view demo
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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.
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #if !wxUSE_DOC_VIEW_ARCHITECTURE
31 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
34 #include "wx/docview.h"
40 #include "wx/filename.h"
42 #include "wx/stockitem.h"
44 static MyFrame
* frame
= NULL
;
46 // In single window mode, don't have any child windows; use
48 bool singleWindowMode
= false;
57 bool MyApp::OnInit(void)
59 if ( !wxApp::OnInit() )
61 SetAppName(wxT("DocView Demo"));
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
70 if (wxStrcmp(argv
[1], wxT("-single")) == 0)
72 singleWindowMode
= true;
76 //// Create a document manager
77 m_docManager
= new wxDocManager
;
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
));
83 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("drw") , 'WXMB' , 'WXMA' ) ;
88 // If we've only got one window, we only get to edit
89 // one document at a time. Therefore no text editing, just
91 m_docManager
->SetMaxDocsOpen(1);
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
));
99 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("txt") , 'TEXT' , 'WXMA' ) ;
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
);
106 //// Give it an icon (this is ignored in MDI mode: uses resources)
108 frame
->SetIcon(wxIcon(wxT("doc_icn")));
112 wxMenu
*file_menu
= new wxMenu
;
113 wxMenu
*edit_menu
= NULL
;
115 file_menu
->Append(wxID_NEW
);
116 file_menu
->Append(wxID_OPEN
);
118 if (singleWindowMode
)
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
);
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"));
134 frame
->m_editMenu
= edit_menu
;
137 file_menu
->AppendSeparator();
138 file_menu
->Append(wxID_EXIT
);
140 // A nice touch: a history of files visited. Use this menu.
141 m_docManager
->FileHistoryUseMenu(file_menu
);
143 wxMenu
*help_menu
= new wxMenu
;
144 help_menu
->Append(DOCVIEW_ABOUT
);
146 wxMenuBar
*menu_bar
= new wxMenuBar
;
148 menu_bar
->Append(file_menu
, wxGetStockLabel(wxID_FILE
));
150 menu_bar
->Append(edit_menu
, wxGetStockLabel(wxID_EDIT
));
151 menu_bar
->Append(help_menu
, wxGetStockLabel(wxID_HELP
));
153 if (singleWindowMode
)
154 frame
->m_canvas
= frame
->CreateCanvas(NULL
, frame
);
156 //// Associate the menu bar with the frame
157 frame
->SetMenuBar(menu_bar
);
159 frame
->Centre(wxBOTH
);
166 int MyApp::OnExit(void)
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.
178 wxFrame
*MyApp::CreateChildFrame(wxDocument
*doc
, wxView
*view
, bool isCanvas
)
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
);
185 subframe
->SetIcon(wxString(isCanvas
? wxT("chrt_icn") : wxT("notepad_icn")));
189 wxMenu
*file_menu
= new wxMenu
;
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
);
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
);
205 wxMenu
*edit_menu
= new wxMenu
;
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"));
214 doc
->GetCommandProcessor()->SetEditMenu(edit_menu
);
218 edit_menu
->Append(wxID_COPY
);
219 edit_menu
->Append(wxID_PASTE
);
220 edit_menu
->Append(wxID_SELECTALL
);
223 wxMenu
*help_menu
= new wxMenu
;
224 help_menu
->Append(DOCVIEW_ABOUT
);
226 wxMenuBar
*menu_bar
= new wxMenuBar
;
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
));
232 //// Associate the menu bar with the frame
233 subframe
->SetMenuBar(menu_bar
);
235 subframe
->Centre(wxBOTH
);
241 * This is the top-level window of the application.
244 IMPLEMENT_CLASS(MyFrame
, wxDocParentFrame
)
245 BEGIN_EVENT_TABLE(MyFrame
, wxDocParentFrame
)
246 EVT_MENU(DOCVIEW_ABOUT
, MyFrame::OnAbout
)
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
)
253 // This pointer only needed if in single window mode
258 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
260 wxMessageBox(wxT("DocView Demo\nAuthor: Julian Smart\nUsage: docview.exe [-single]"), wxT("About DocView"));
262 Better, but brings in adv lib
263 wxAboutDialogInfo info;
264 info.SetName(wxTheApp->GetAppDisplayName());
265 info.AddDeveloper(wxT("Julian Smart"));
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
)
275 wxSize size
= parent
->GetClientSize();
277 // Non-retained canvas
278 MyCanvas
* canvas
= new MyCanvas(view
, parent
, wxPoint(0, 0), size
, 0);
279 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
281 // Give it scrollbars
282 canvas
->SetScrollbars(20, 20, 50, 50);
283 canvas
->SetBackgroundColour(*wxWHITE
);
284 canvas
->ClearBackground();
289 MyFrame
*GetMainFrame(void)