]>
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 !USE_DOC_VIEW_ARCHITECTURE
35 #error You must set USE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
38 #include "wx/docview.h"
44 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 //// 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
, 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"));
98 frame
->SetIcon(wxIcon("aiai.xbm"));
102 wxMenu
*file_menu
= new wxMenu
;
103 wxMenu
*edit_menu
= NULL
;
105 file_menu
->Append(wxID_NEW
, "&New...");
106 file_menu
->Append(wxID_OPEN
, "&Open...");
108 if (singleWindowMode
)
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");
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");
124 frame
->editMenu
= edit_menu
;
127 file_menu
->AppendSeparator();
128 file_menu
->Append(wxID_EXIT
, "E&xit");
130 // A nice touch: a history of files visited. Use this menu.
131 m_docManager
->FileHistoryUseMenu(file_menu
);
133 wxMenu
*help_menu
= new wxMenu
;
134 help_menu
->Append(DOCVIEW_ABOUT
, "&About");
136 wxMenuBar
*menu_bar
= new wxMenuBar
;
138 menu_bar
->Append(file_menu
, "&File");
140 menu_bar
->Append(edit_menu
, "&Edit");
141 menu_bar
->Append(help_menu
, "&Help");
143 if (singleWindowMode
)
144 frame
->canvas
= frame
->CreateCanvas(NULL
, frame
);
146 //// Associate the menu bar with the frame
147 frame
->SetMenuBar(menu_bar
);
149 frame
->Centre(wxBOTH
);
156 int MyApp::OnExit(void)
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.
168 wxFrame
*MyApp::CreateChildFrame(wxDocument
*doc
, wxView
*view
, bool isCanvas
)
170 //// Make a child frame
171 wxDocChildFrame
*subframe
= new wxDocChildFrame(doc
, view
, GetMainFrame(), -1, "Child Frame",
172 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE
);
175 subframe
->SetIcon(wxString(isCanvas
? "chrt_icn" : "notepad_icn"));
178 subframe
->SetIcon(wxIcon("aiai.xbm"));
182 wxMenu
*file_menu
= new wxMenu
;
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...");
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");
198 wxMenu
*edit_menu
= NULL
;
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");
208 doc
->GetCommandProcessor()->SetEditMenu(edit_menu
);
211 wxMenu
*help_menu
= new wxMenu
;
212 help_menu
->Append(DOCVIEW_ABOUT
, "&About");
214 wxMenuBar
*menu_bar
= new wxMenuBar
;
216 menu_bar
->Append(file_menu
, "&File");
218 menu_bar
->Append(edit_menu
, "&Edit");
219 menu_bar
->Append(help_menu
, "&Help");
221 //// Associate the menu bar with the frame
222 subframe
->SetMenuBar(menu_bar
);
224 subframe
->Centre(wxBOTH
);
230 * This is the top-level window of the application.
233 IMPLEMENT_CLASS(MyFrame
, wxDocParentFrame
)
234 BEGIN_EVENT_TABLE(MyFrame
, wxDocParentFrame
)
235 EVT_MENU(DOCVIEW_ABOUT
, MyFrame::OnAbout
)
238 MyFrame::MyFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
239 const wxPoint
& pos
, const wxSize
& size
, const long type
):
240 wxDocParentFrame(manager
, frame
, id
, title
, pos
, size
, type
)
242 // This pointer only needed if in single window mode
247 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
249 (void)wxMessageBox("DocView Demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\nUsage: docview.exe [-single]", "About DocView");
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
)
258 parent
->GetClientSize(&width
, &height
);
260 // Non-retained canvas
261 MyCanvas
*canvas
= new MyCanvas(view
, parent
, wxPoint(0, 0), wxSize(width
, height
), 0);
262 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
264 // Give it scrollbars
265 canvas
->SetScrollbars(20, 20, 50, 50);
270 MyFrame
*GetMainFrame(void)