]>
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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation "docview.h"
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.
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "wx/wxprec.h"
34 #if !wxUSE_DOC_VIEW_ARCHITECTURE
35 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
38 #include "wx/docview.h"
44 MyFrame
*frame
= (MyFrame
*) NULL
;
46 // In single window mode, don't have any child windows; use
48 bool singleWindowMode
= FALSE
;
54 m_docManager
= (wxDocManager
*) NULL
;
57 bool MyApp::OnInit(void)
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)
65 if (strcmp(argv
[1], "-single") == 0)
67 singleWindowMode
= TRUE
;
71 //// Create a document manager
72 m_docManager
= new wxDocManager
;
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
));
80 // If we've only got one window, we only get to edit
81 // one document at a time. Therefore no text editing, just
83 m_docManager
->SetMaxDocsOpen(1);
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
));
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
);
93 //// Give it an icon (this is ignored in MDI mode: uses resources)
95 frame
->SetIcon(wxIcon("doc_icn"));
99 wxMenu
*file_menu
= new wxMenu
;
100 wxMenu
*edit_menu
= (wxMenu
*) NULL
;
102 file_menu
->Append(wxID_NEW
, "&New...");
103 file_menu
->Append(wxID_OPEN
, "&Open...");
105 if (singleWindowMode
)
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");
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");
121 frame
->editMenu
= edit_menu
;
124 file_menu
->AppendSeparator();
125 file_menu
->Append(wxID_EXIT
, "E&xit");
127 // A nice touch: a history of files visited. Use this menu.
128 m_docManager
->FileHistoryUseMenu(file_menu
);
130 wxMenu
*help_menu
= new wxMenu
;
131 help_menu
->Append(DOCVIEW_ABOUT
, "&About");
133 wxMenuBar
*menu_bar
= new wxMenuBar
;
135 menu_bar
->Append(file_menu
, "&File");
137 menu_bar
->Append(edit_menu
, "&Edit");
138 menu_bar
->Append(help_menu
, "&Help");
140 if (singleWindowMode
)
141 frame
->canvas
= frame
->CreateCanvas((wxView
*) NULL
, frame
);
143 //// Associate the menu bar with the frame
144 frame
->SetMenuBar(menu_bar
);
146 frame
->Centre(wxBOTH
);
153 int MyApp::OnExit(void)
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.
165 wxFrame
*MyApp::CreateChildFrame(wxDocument
*doc
, wxView
*view
, bool isCanvas
)
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
);
172 subframe
->SetIcon(wxString(isCanvas
? "chrt_icn" : "notepad_icn"));
176 wxMenu
*file_menu
= new wxMenu
;
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...");
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");
192 wxMenu
*edit_menu
= (wxMenu
*) NULL
;
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");
202 doc
->GetCommandProcessor()->SetEditMenu(edit_menu
);
205 wxMenu
*help_menu
= new wxMenu
;
206 help_menu
->Append(DOCVIEW_ABOUT
, "&About");
208 wxMenuBar
*menu_bar
= new wxMenuBar
;
210 menu_bar
->Append(file_menu
, "&File");
212 menu_bar
->Append(edit_menu
, "&Edit");
213 menu_bar
->Append(help_menu
, "&Help");
215 //// Associate the menu bar with the frame
216 subframe
->SetMenuBar(menu_bar
);
218 subframe
->Centre(wxBOTH
);
224 * This is the top-level window of the application.
227 IMPLEMENT_CLASS(MyFrame
, wxDocParentFrame
)
228 BEGIN_EVENT_TABLE(MyFrame
, wxDocParentFrame
)
229 EVT_MENU(DOCVIEW_ABOUT
, MyFrame::OnAbout
)
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
)
236 // This pointer only needed if in single window mode
237 canvas
= (MyCanvas
*) NULL
;
238 editMenu
= (wxMenu
*) NULL
;
241 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
243 (void)wxMessageBox("DocView Demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\nUsage: docview.exe [-single]", "About DocView");
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
)
252 parent
->GetClientSize(&width
, &height
);
254 // Non-retained canvas
255 MyCanvas
*canvas
= new MyCanvas(view
, parent
, wxPoint(0, 0), wxSize(width
, height
), 0);
256 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
258 // Give it scrollbars
259 canvas
->SetScrollbars(20, 20, 50, 50);
260 canvas
->SetBackgroundColour(*wxWHITE
);
266 MyFrame
*GetMainFrame(void)