docview.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/docview/docview.cpp
  3. // Purpose: Document/view demo
  4. // Author: Julian Smart
  5. // Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
  6. // Created: 04/01/98
  7. // Copyright: (c) 1998 Julian Smart
  8. // (c) 2008 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. /*
  12. This sample show document/view support in wxWidgets.
  13. It can be run in several ways:
  14. * With "--mdi" command line option to use multiple MDI child frames
  15. for the multiple documents (this is the default).
  16. * With "--sdi" command line option to use multiple top level windows
  17. for the multiple documents
  18. * With "--single" command line option to support opening a single
  19. document only
  20. Notice that doing it like this somewhat complicates the code, you could
  21. make things much simpler in your own programs by using either
  22. wxDocParentFrame or wxDocMDIParentFrame unconditionally (and never using
  23. the single mode) instead of supporting all of them as this sample does.
  24. */
  25. // ----------------------------------------------------------------------------
  26. // headers
  27. // ----------------------------------------------------------------------------
  28. // For compilers that support precompilation, includes "wx/wx.h".
  29. #include "wx/wxprec.h"
  30. #ifdef __BORLANDC__
  31. #pragma hdrstop
  32. #endif
  33. #ifndef WX_PRECOMP
  34. #include "wx/wx.h"
  35. #include "wx/stockitem.h"
  36. #endif
  37. #if !wxUSE_DOC_VIEW_ARCHITECTURE
  38. #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
  39. #endif
  40. #include "wx/docview.h"
  41. #include "wx/docmdi.h"
  42. #include "docview.h"
  43. #include "doc.h"
  44. #include "view.h"
  45. #include "wx/cmdline.h"
  46. #include "wx/config.h"
  47. #ifdef __WXMAC__
  48. #include "wx/filename.h"
  49. #endif
  50. #ifndef wxHAS_IMAGES_IN_RESOURCES
  51. #include "doc.xpm"
  52. #include "chart.xpm"
  53. #include "notepad.xpm"
  54. #endif
  55. // ----------------------------------------------------------------------------
  56. // MyApp implementation
  57. // ----------------------------------------------------------------------------
  58. IMPLEMENT_APP(MyApp)
  59. wxBEGIN_EVENT_TABLE(MyApp, wxApp)
  60. EVT_MENU(wxID_ABOUT, MyApp::OnAbout)
  61. wxEND_EVENT_TABLE()
  62. MyApp::MyApp()
  63. {
  64. #if wxUSE_MDI_ARCHITECTURE
  65. m_mode = Mode_MDI;
  66. #else
  67. m_mode = Mode_SDI;
  68. #endif
  69. m_canvas = NULL;
  70. m_menuEdit = NULL;
  71. }
  72. // constants for the command line options names
  73. namespace CmdLineOption
  74. {
  75. const char * const MDI = "mdi";
  76. const char * const SDI = "sdi";
  77. const char * const SINGLE = "single";
  78. } // namespace CmdLineOption
  79. void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
  80. {
  81. wxApp::OnInitCmdLine(parser);
  82. parser.AddSwitch("", CmdLineOption::MDI,
  83. "run in MDI mode: multiple documents, single window");
  84. parser.AddSwitch("", CmdLineOption::SDI,
  85. "run in SDI mode: multiple documents, multiple windows");
  86. parser.AddSwitch("", CmdLineOption::SINGLE,
  87. "run in single document mode");
  88. }
  89. bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
  90. {
  91. int numModeOptions = 0;
  92. #if wxUSE_MDI_ARCHITECTURE
  93. if ( parser.Found(CmdLineOption::MDI) )
  94. {
  95. m_mode = Mode_MDI;
  96. numModeOptions++;
  97. }
  98. #endif // wxUSE_MDI_ARCHITECTURE
  99. if ( parser.Found(CmdLineOption::SDI) )
  100. {
  101. m_mode = Mode_SDI;
  102. numModeOptions++;
  103. }
  104. if ( parser.Found(CmdLineOption::SINGLE) )
  105. {
  106. m_mode = Mode_Single;
  107. numModeOptions++;
  108. }
  109. if ( numModeOptions > 1 )
  110. {
  111. wxLogError("Only a single option choosing the mode can be given.");
  112. return false;
  113. }
  114. return wxApp::OnCmdLineParsed(parser);
  115. }
  116. bool MyApp::OnInit()
  117. {
  118. if ( !wxApp::OnInit() )
  119. return false;
  120. ::wxInitAllImageHandlers();
  121. // Fill in the application information fields before creating wxConfig.
  122. SetVendorName("wxWidgets");
  123. SetAppName("wx_docview_sample");
  124. SetAppDisplayName("wxWidgets DocView Sample");
  125. //// Create a document manager
  126. wxDocManager *docManager = new wxDocManager;
  127. //// Create a template relating drawing documents to their views
  128. new wxDocTemplate(docManager, "Drawing", "*.drw", "", "drw",
  129. "Drawing Doc", "Drawing View",
  130. CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
  131. #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
  132. wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA');
  133. #endif
  134. if ( m_mode == Mode_Single )
  135. {
  136. // If we've only got one window, we only get to edit one document at a
  137. // time. Therefore no text editing, just doodling.
  138. docManager->SetMaxDocsOpen(1);
  139. }
  140. else // multiple documents mode: allow documents of different types
  141. {
  142. // Create a template relating text documents to their views
  143. new wxDocTemplate(docManager, "Text", "*.txt;*.text", "", "txt;text",
  144. "Text Doc", "Text View",
  145. CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
  146. #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
  147. wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA');
  148. #endif
  149. // Create a template relating image documents to their views
  150. new wxDocTemplate(docManager, "Image", "*.png;*.jpg", "", "png;jpg",
  151. "Image Doc", "Image View",
  152. CLASSINFO(ImageDocument), CLASSINFO(ImageView));
  153. }
  154. // create the main frame window
  155. wxFrame *frame;
  156. #if wxUSE_MDI_ARCHITECTURE
  157. if ( m_mode == Mode_MDI )
  158. {
  159. frame = new wxDocMDIParentFrame(docManager, NULL, wxID_ANY,
  160. GetAppDisplayName(),
  161. wxDefaultPosition,
  162. wxSize(500, 400));
  163. }
  164. else
  165. #endif // wxUSE_MDI_ARCHITECTURE
  166. {
  167. frame = new wxDocParentFrame(docManager, NULL, wxID_ANY,
  168. GetAppDisplayName(),
  169. wxDefaultPosition,
  170. wxSize(500, 400));
  171. }
  172. // and its menu bar
  173. wxMenu *menuFile = new wxMenu;
  174. menuFile->Append(wxID_NEW);
  175. menuFile->Append(wxID_OPEN);
  176. if ( m_mode == Mode_Single )
  177. AppendDocumentFileCommands(menuFile, true);
  178. menuFile->AppendSeparator();
  179. menuFile->Append(wxID_EXIT);
  180. // A nice touch: a history of files visited. Use this menu.
  181. docManager->FileHistoryUseMenu(menuFile);
  182. #if wxUSE_CONFIG
  183. docManager->FileHistoryLoad(*wxConfig::Get());
  184. #endif // wxUSE_CONFIG
  185. if ( m_mode == Mode_Single )
  186. {
  187. m_canvas = new MyCanvas(NULL, frame);
  188. m_menuEdit = CreateDrawingEditMenu();
  189. docManager->CreateNewDocument();
  190. }
  191. CreateMenuBarForFrame(frame, menuFile, m_menuEdit);
  192. frame->SetIcon(wxICON(doc));
  193. frame->Centre();
  194. frame->Show();
  195. return true;
  196. }
  197. int MyApp::OnExit()
  198. {
  199. wxDocManager * const manager = wxDocManager::GetDocumentManager();
  200. #if wxUSE_CONFIG
  201. manager->FileHistorySave(*wxConfig::Get());
  202. #endif // wxUSE_CONFIG
  203. delete manager;
  204. return wxApp::OnExit();
  205. }
  206. void MyApp::AppendDocumentFileCommands(wxMenu *menu, bool supportsPrinting)
  207. {
  208. menu->Append(wxID_CLOSE);
  209. menu->Append(wxID_SAVE);
  210. menu->Append(wxID_SAVEAS);
  211. menu->Append(wxID_REVERT, _("Re&vert..."));
  212. if ( supportsPrinting )
  213. {
  214. menu->AppendSeparator();
  215. menu->Append(wxID_PRINT);
  216. menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
  217. menu->Append(wxID_PREVIEW);
  218. }
  219. }
  220. wxMenu *MyApp::CreateDrawingEditMenu()
  221. {
  222. wxMenu * const menu = new wxMenu;
  223. menu->Append(wxID_UNDO);
  224. menu->Append(wxID_REDO);
  225. menu->AppendSeparator();
  226. menu->Append(wxID_CUT, "&Cut last segment");
  227. return menu;
  228. }
  229. void MyApp::CreateMenuBarForFrame(wxFrame *frame, wxMenu *file, wxMenu *edit)
  230. {
  231. wxMenuBar *menubar = new wxMenuBar;
  232. menubar->Append(file, wxGetStockLabel(wxID_FILE));
  233. if ( edit )
  234. menubar->Append(edit, wxGetStockLabel(wxID_EDIT));
  235. wxMenu *help= new wxMenu;
  236. help->Append(wxID_ABOUT);
  237. menubar->Append(help, wxGetStockLabel(wxID_HELP));
  238. frame->SetMenuBar(menubar);
  239. }
  240. wxFrame *MyApp::CreateChildFrame(wxView *view, bool isCanvas)
  241. {
  242. // create a child frame of appropriate class for the current mode
  243. wxFrame *subframe;
  244. wxDocument *doc = view->GetDocument();
  245. #if wxUSE_MDI_ARCHITECTURE
  246. if ( GetMode() == Mode_MDI )
  247. {
  248. subframe = new wxDocMDIChildFrame
  249. (
  250. doc,
  251. view,
  252. wxStaticCast(GetTopWindow(), wxDocMDIParentFrame),
  253. wxID_ANY,
  254. "Child Frame",
  255. wxDefaultPosition,
  256. wxSize(300, 300)
  257. );
  258. }
  259. else
  260. #endif // wxUSE_MDI_ARCHITECTURE
  261. {
  262. subframe = new wxDocChildFrame
  263. (
  264. doc,
  265. view,
  266. wxStaticCast(GetTopWindow(), wxDocParentFrame),
  267. wxID_ANY,
  268. "Child Frame",
  269. wxDefaultPosition,
  270. wxSize(300, 300)
  271. );
  272. subframe->Centre();
  273. }
  274. wxMenu *menuFile = new wxMenu;
  275. menuFile->Append(wxID_NEW);
  276. menuFile->Append(wxID_OPEN);
  277. AppendDocumentFileCommands(menuFile, isCanvas);
  278. menuFile->AppendSeparator();
  279. menuFile->Append(wxID_EXIT);
  280. wxMenu *menuEdit;
  281. if ( isCanvas )
  282. {
  283. menuEdit = CreateDrawingEditMenu();
  284. doc->GetCommandProcessor()->SetEditMenu(menuEdit);
  285. doc->GetCommandProcessor()->Initialize();
  286. }
  287. else // text frame
  288. {
  289. menuEdit = new wxMenu;
  290. menuEdit->Append(wxID_COPY);
  291. menuEdit->Append(wxID_PASTE);
  292. menuEdit->Append(wxID_SELECTALL);
  293. }
  294. CreateMenuBarForFrame(subframe, menuFile, menuEdit);
  295. subframe->SetIcon(isCanvas ? wxICON(chrt) : wxICON(notepad));
  296. return subframe;
  297. }
  298. void MyApp::OnAbout(wxCommandEvent& WXUNUSED(event))
  299. {
  300. wxString modeName;
  301. switch ( m_mode )
  302. {
  303. #if wxUSE_MDI_ARCHITECTURE
  304. case Mode_MDI:
  305. modeName = "MDI";
  306. break;
  307. #endif // wxUSE_MDI_ARCHITECTURE
  308. case Mode_SDI:
  309. modeName = "SDI";
  310. break;
  311. case Mode_Single:
  312. modeName = "single document";
  313. break;
  314. default:
  315. wxFAIL_MSG( "unknown mode ");
  316. }
  317. #ifdef __VISUALC6__
  318. const int docsCount =
  319. wxDocManager::GetDocumentManager()->GetDocuments().GetCount();
  320. #else
  321. const int docsCount =
  322. wxDocManager::GetDocumentManager()->GetDocumentsVector().size();
  323. #endif
  324. wxLogMessage
  325. (
  326. "This is the wxWidgets Document/View Sample\n"
  327. "running in %s mode.\n"
  328. "%d open documents.\n"
  329. "\n"
  330. "Authors: Julian Smart, Vadim Zeitlin\n"
  331. "\n"
  332. "Usage: docview [--{mdi,sdi,single}]",
  333. modeName,
  334. docsCount
  335. );
  336. }