wxArt2D
docmdiref.cpp
Go to the documentation of this file.
1 /*! \file docview/src/docmdiref.cpp
2  \brief Frame classes for MDI document/view applications
3  \author Julian Smart
4  \author Klaas Holwerda
5  \date Created 01/02/97
6 
7  Copyright: (c) Julian Smart
8 
9  Licence: wxWidgets licence
10 
11  RCS-ID: $Id: docmdiref.cpp,v 1.44 2009/09/29 20:06:47 titato Exp $
12 */
13 
14 #include "docviewprec.h"
15 
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19 
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif
23 
24 #if wxUSE_MDI_ARCHITECTURE && wxUSE_DOC_VIEW_ARCHITECTURE
25 
26 #ifndef WX_PRECOMP
27 #include "wx/string.h"
28 #include "wx/utils.h"
29 #include "wx/app.h"
30 #include "wx/dc.h"
31 #include "wx/dialog.h"
32 #include "wx/menu.h"
33 #include "wx/list.h"
34 #include "wx/filedlg.h"
35 #include "wx/intl.h"
36 #include "wx/file.h"
37 #endif
38 
39 #include "wx/docview/doccom.h"
40 #include "wx/docview/docmdiref.h"
41 
42 // ----------------------------------------------------------------------------
43 // template instantiations
44 // ----------------------------------------------------------------------------
45 
46 //#include "wx/general/id.inl"
47 
48 //----------------------------------------------------------------------------
49 // template instanitiations
50 //----------------------------------------------------------------------------
51 
52 /*
53  * Docview MDI parent frame
54  */
55 
56 IMPLEMENT_CLASS( a2dDocumentMDIParentFrame, wxMDIParentFrame )
57 
58 BEGIN_EVENT_TABLE( a2dDocumentMDIParentFrame, wxMDIParentFrame )
59  EVT_MENU( wxID_EXIT, a2dDocumentMDIParentFrame::OnExit )
60  EVT_MENU_RANGE( wxID_FILE1, wxID_FILE9, a2dDocumentMDIParentFrame::OnMRUFile )
61  EVT_CLOSE( a2dDocumentMDIParentFrame::OnCloseWindow )
62 END_EVENT_TABLE()
63 
64 a2dDocumentMDIParentFrame::a2dDocumentMDIParentFrame( wxFrame* frame, wxWindowID id, const wxString& title,
65  const wxPoint& pos, const wxSize& size, long style, const wxString& name ):
66  wxMDIParentFrame( frame, id, title, pos, size, style, name )
67 {
68 }
69 
70 a2dDocumentMDIParentFrame::a2dDocumentMDIParentFrame()
71 {
72 }
73 
75  wxWindow* frame,
76  wxWindowID id,
77  const wxString& title,
78  const wxPoint& pos,
79  const wxSize& size,
80  long style,
81  const wxString& name )
82 {
83  bool res = wxMDIParentFrame::Create( frame, id, title, pos, size, style, name );
84  return res;
85 }
86 
87 
88 void a2dDocumentMDIParentFrame::OnExit( wxCommandEvent& WXUNUSED( event ) )
89 {
90  Close();
91 }
92 
93 void a2dDocumentMDIParentFrame::OnMRUFile( wxCommandEvent& event )
94 {
95  int n = event.GetId() - wxID_FILE1; // the index in MRU list
97  if ( fileHistItem )
98  {
99  // verify that the file exists before doing anything else
100  if ( wxFile::Exists( fileHistItem->m_filename.GetFullPath() ) )
101  {
102  // try to open it
103  a2dError returncode = a2dDocviewGlobals->GetDocviewCommandProcessor()->CreateDocuments( fileHistItem->m_filename.GetFullPath(), a2dREFDOC_SILENT, fileHistItem->m_docTemplate );
104  if ( returncode == a2dError_NoDocTemplateRef )
105  {
106  a2dDocviewGlobals->ReportError( a2dError_CouldNotCreateDocument, _( "No templates to create document, removed from history, a2dDocumentCommandProcessor::OnMRUFile" ) );
108  }
109  }
110  else
111  {
112  a2dDocviewGlobals->ReportErrorF( a2dError_FileHistory, _( "The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list." ),
113  fileHistItem->m_filename.GetFullPath().c_str() );
114 
115  // remove the bogus filename from the MRU list and notify the user
116  // about it
118  }
119  }
120 }
121 
123 {
124  if ( !wxEvtHandler::ProcessEvent( event ) )
125  {
126  if ( a2dDocviewGlobals->GetDocviewCommandProcessor() ) //&& event.IsKindOf(CLASSINFO(wxCommandEvent)) )
128  return false;
129  }
130  return true;
131 }
132 
133 void a2dDocumentMDIParentFrame::OnCloseWindow( wxCloseEvent& event )
134 {
135  if ( a2dDocviewGlobals->GetDocviewCommandProcessor()->Clear( !event.CanVeto() ) )
136  {
137  this->Destroy();
138  }
139  else
140  event.Veto();
141 }
142 
143 void a2dDocumentMDIParentFrame::OnCmdMenuId( wxCommandEvent& event )
144 {
145  event.Skip();
146 }
147 
148 void a2dDocumentMDIParentFrame::AddCmdMenu( wxMenu* parentMenu, wxMenuItem* item )
149 {
150  Connect( item->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIParentFrame::OnCmdMenuId ) );
151  parentMenu->Append( item );
152 }
153 
154 void a2dDocumentMDIParentFrame::AddCmdMenu( wxMenu* parentMenu, const a2dMenuIdItem& cmdId )
155 {
156  Connect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIParentFrame::OnCmdMenuId ) );
157  parentMenu->Append( cmdId.GetId(), cmdId.GetLabel(), cmdId.GetHelp(), cmdId.IsCheckable() );
158 }
159 
160 void a2dDocumentMDIParentFrame::RemoveCmdMenu( wxMenu* parentMenu, const a2dMenuIdItem& cmdId )
161 {
162  Disconnect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIParentFrame::OnCmdMenuId ) );
163  parentMenu->Delete( cmdId.GetId() );
164 }
165 
167 {
168  if ( ! GetToolBar() )
169  return;
170  Connect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIParentFrame::OnCmdMenuId ) );
171  wxString error = _T( "No Bitmap for a2dToolCmd found for:" ) + cmdId.GetIdName();
172  wxASSERT_MSG( cmdId.GetBitmap().Ok(), error );
173  GetToolBar()->AddTool( cmdId.GetId(), cmdId.GetLabel(), cmdId.GetBitmap(), cmdId.GetHelp(), cmdId.GetKind() );
174 }
175 
176 /********************************************************************
177 * a2dDocumentMDIChildFrame
178 *********************************************************************/
179 
180 IMPLEMENT_CLASS( a2dDocumentMDIChildFrame, wxMDIChildFrame )
181 
182 BEGIN_EVENT_TABLE( a2dDocumentMDIChildFrame, wxMDIChildFrame )
183  EVT_ACTIVATE( a2dDocumentMDIChildFrame::OnActivate )
184  EVT_CLOSE( a2dDocumentMDIChildFrame::OnCloseWindow )
185  EVT_PAINT( a2dDocumentMDIChildFrame::OnPaint )
186  EVT_ACTIVATE_VIEW_SENT_FROM_CHILD( a2dDocumentMDIChildFrame::OnActivateViewSentFromChild )
187  EVT_CLOSE_VIEW( a2dDocumentMDIChildFrame::OnCloseView )
188 END_EVENT_TABLE()
189 
190 
191 a2dDocumentMDIChildFrame::a2dDocumentMDIChildFrame()
192 {
193  m_destroyOnCloseView = true;
194  m_view = NULL;
195 }
196 
197 
198 a2dDocumentMDIChildFrame::a2dDocumentMDIChildFrame( wxMDIParentFrame* frame, a2dView* view, wxWindowID id,
199  const wxString& title, const wxPoint& pos, const wxSize& size, long style, const wxString& name ):
200  wxMDIChildFrame( frame, id, title, pos, size, style, name )
201 {
202  m_destroyOnCloseView = true;
203  m_view = view;
204  if ( m_view )
205  {
206  m_view->SetDisplayWindow( this );
207  }
208 }
209 
211  wxMDIParentFrame* frame,
212  a2dView* view,
213  wxWindowID id,
214  const wxString& title,
215  const wxPoint& pos,
216  const wxSize& size,
217  long style,
218  const wxString& name )
219 {
220  bool res = wxMDIChildFrame::Create( frame, id, title, pos, size, style, name );
221  m_view = view;
222  if ( m_view )
223  {
224  m_view->SetDisplayWindow( this );
225  }
226  return res;
227 }
228 
230 {
231  m_view = ( a2dView* ) NULL;
232 }
233 
234 void a2dDocumentMDIChildFrame::OnCmdMenuId( wxCommandEvent& event )
235 {
236  event.Skip();
237 }
238 
239 void a2dDocumentMDIChildFrame::AddCmdMenu( wxMenu* parentMenu, wxMenuItem* item )
240 {
241  Connect( item->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIChildFrame::OnCmdMenuId ) );
242  parentMenu->Append( item );
243 }
244 
245 void a2dDocumentMDIChildFrame::AddCmdMenu( wxMenu* parentMenu, const a2dMenuIdItem& cmdId )
246 {
247  Connect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIChildFrame::OnCmdMenuId ) );
248  parentMenu->Append( cmdId.GetId(), cmdId.GetLabel(), cmdId.GetHelp(), cmdId.IsCheckable() );
249 }
250 
251 void a2dDocumentMDIChildFrame::RemoveCmdMenu( wxMenu* parentMenu, const a2dMenuIdItem& cmdId )
252 {
253  Disconnect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIChildFrame::OnCmdMenuId ) );
254  parentMenu->Delete( cmdId.GetId() );
255 }
256 
258 {
259  if ( ! GetToolBar() )
260  return;
261  Connect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentMDIChildFrame::OnCmdMenuId ) );
262  wxString error = _T( "No Bitmap for a2dToolCmd found for:" ) + cmdId.GetIdName();
263  wxASSERT_MSG( cmdId.GetBitmap( false ).Ok(), error );
264  GetToolBar()->AddTool( cmdId.GetId(), cmdId.GetLabel(), cmdId.GetBitmap( false ), cmdId.GetHelp(), cmdId.GetKind() );
265 }
266 
268 {
269  if ( m_view )
270  {
271  m_view->SetDisplayWindow( NULL );
272  }
273 
274  m_view = view;
275 
276  if ( m_view )
277  {
278  //Attach the canvas as the window for the view to display its stuff
279  m_view->SetDisplayWindow( this );
280  }
281 }
282 
283 // Extend event processing to search the view's event table
285 {
286  static wxEvent* ActiveEvent = NULL;
287 
288  // Break recursion loops
289  if ( ActiveEvent == &event )
290  return false;
291 
292  ActiveEvent = &event;
293 
294  bool ret = false;
295  if ( event.GetEventType() == wxEVT_CLOSE_WINDOW )
296  {
297  //window close events should lead to view close events.
298  //
299  //When a child frame is being closed, only the child view will be closed and the frame flaged
300  //for Destroy ( see OnCloseView )
301 
302  wxCloseEvent& closeevent = ( wxCloseEvent& ) event;
303 
304  //give the Frame the right to veto in for instance a OnCloseWindow which is called by wxEVT_CLOSE
305  if ( wxEvtHandler::ProcessEvent( event ) && !closeevent.GetVeto() )
306  {
307  closeevent.SetCanVeto( false );
308  if ( m_view )
309  m_view->ProcessEvent( closeevent );
310  m_view = NULL;
311  }
312  else
313  closeevent.Veto();
314  ret = true;
315  }
316  else if ( event.GetEventType() == wxEVT_ACTIVATE )
317  {
318  // the wxEVT_ACTIVATE event should lead to a wxEVT_ACTIVATE_VIEW event here.
319  // This last event is always sent to a2dDocviewGlobals->GetDocviewCommandProcessor()() from
320  // within a2dView::Activate()
321 
322  if ( m_view && !m_view->IsClosed() )
323  m_view->ProcessEvent( event );
324 
325  ret = wxEvtHandler::ProcessEvent( event );
326  }
327  else
328  {
329  // EVENTS needed for view directly, should be handled above.
330  // events down here go only to the parent window or commandprocessor
331 
332  //In case the event is a commandevent, it will automatically be redirected to the parent window by
333  // wxEvtHandler::ProcessEvent
334  //There is no need to handle that case in here.
335  if ( !wxEvtHandler::ProcessEvent( event ) )
336  {
337  // Try the document manager, then do default processing
339  {
341  }
342  else
343  ret = false;
344  }
345  else
346  ret = true;
347  }
348  ActiveEvent = NULL;
349  return ret;
350 }
351 
352 void a2dDocumentMDIChildFrame::OnActivate( wxActivateEvent& event )
353 {
354  wxMDIChildFrame::OnActivate( event );
355 
356  if ( event.GetActive() && m_view )
357  m_view->Activate( event.GetActive() );
358 }
359 
361 {
362  if ( viewevent.GetActive() )
363  {
364  m_view = ( a2dView* ) viewevent.GetEventObject();
365  }
366 }
367 
368 // Clean up frames/windows used for displaying the view.
369 // They or not really deleted here only flagged for deletion using Destroy(),
370 // this means that they will really be deleted in idle time by wxWindows.
372 {
373  wxASSERT_MSG( m_view || event.GetEventObject(), wxT( "not view of frame closed" ) );
374 
375  //there is no need to handle the parent frame differently
376 
377  //frames can be destroyed savely ( happening in idle time )
378  //This default, asumes a multi frame application, where each view has one frame
379  if ( m_destroyOnCloseView )
380  Destroy(); //very likely done somewhere else, but makes sure it happens.
381 
382  if ( m_view )
383  m_view->SetDisplayWindow( NULL );
384  m_view = NULL;
385 
386  Refresh();
387 }
388 
389 void a2dDocumentMDIChildFrame::OnCloseWindow( wxCloseEvent& event )
390 {
391  if ( !m_view )
392  Destroy();
393 
394  // Close view but don't delete the frame while doing so!
395  // ...since it will be deleted by wxWindows
396 
397  if ( event.CanVeto() )
398  {
399  if ( !m_view || m_view->Close( !event.CanVeto() ) )
400  Destroy();
401  else
402  event.Veto( true );
403 
404  }
405  else
406  Destroy();
407 }
408 
409 bool a2dDocumentMDIChildFrame::Destroy()
410 {
411  m_view = ( a2dView* )NULL;
412  return wxMDIChildFrame::Destroy();
413 }
414 
415 void a2dDocumentMDIChildFrame::OnPaint( wxPaintEvent& WXUNUSED( event ) )
416 {
417  wxPaintDC dc( this );
418  PrepareDC( dc );
419 
420  if ( m_view && m_view->GetDisplayWindow() == this )
421  OnDraw( dc );
422 }
423 
424 // Define the repainting behaviour
426 {
427  if ( m_view && m_view->GetDisplayWindow() == this )
428  m_view->OnDraw( & dc );
429 }
430 
431 #endif
432 // wxUSE_DOC_VIEW_ARCHITECTURE
433 
void OnPaint(wxPaintEvent &event)
default handler for paint events, it calls OnDraw().
Definition: docmdiref.cpp:415
The a2dDocumentMDIChildFrame class provides a default frame for displaying documents.
Definition: docmdiref.h:150
const a2dError a2dError_NoDocTemplateRef
Frame classes for MDI document/view applications.
bool IsCheckable() const
get if menu is checkable
Definition: comevt.h:1612
void AddCmdToToolbar(const a2dMenuIdItem &cmdId)
add items to toolbar using a predefined command ID.
Definition: docmdiref.cpp:257
void AddCmdToToolbar(const a2dMenuIdItem &cmdId)
add items to toolbar using a predefined command ID.
Definition: docmdiref.cpp:166
virtual void SetDisplayWindow(wxWindow *display)
Set the display window.
wxWindow * GetDisplayWindow()
Get the display window.
Definition: docviewref.h:1875
store a menu Id generated by XRCID( menuIdString ) plus a menustring and helpstring ...
Definition: comevt.h:1563
bool Create(wxWindow *frame, wxWindowID id=-1, const wxString &title=wxT("frame"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE, const wxString &name=wxT("frame"))
Constructor for two-step creation (2nd step).
Definition: docmdiref.cpp:74
void OnCmdMenuId(wxCommandEvent &event)
Definition: docmdiref.cpp:143
void OnMRUFile(wxCommandEvent &event)
Creates a new view for a file from file-history (a new MDI child will be created) ...
Definition: docmdiref.cpp:93
void RemoveCmdMenu(wxMenu *parentMenu, const a2dMenuIdItem &cmdId)
remove a command menu from the parent menu.
Definition: docmdiref.cpp:251
virtual void OnDraw(wxDC *dc)=0
needs to be implemented to print a view, also so used for simple redrawing of views.
a2dView event, to report events in the a2dView class
Definition: docviewref.h:424
Docview framework its controlling class.
bool GetActive()
when ::wxEVT_ACTIVATE_VIEW get activation value of the event
Definition: docviewref.h:521
void RemoveCmdMenu(wxMenu *parentMenu, const a2dMenuIdItem &cmdId)
remove a command menu from the parent menu.
Definition: docmdiref.cpp:160
const a2dError a2dError_FileHistory
bool IsClosed()
A closed flag is set when a view is closed.
Definition: docviewref.h:1829
const a2dError a2dError_CouldNotCreateDocument
virtual a2dError CreateDocuments(const wxString &path, a2dDocumentFlagMask documentflags=a2dREFDOC_NON, a2dDocumentTemplate *wantedDocTemplate=NULL, int dialogflags=wxFD_OPEN, a2dTemplateFlagMask docTemplateFlags=a2dTemplateFlag::VISIBLE)
Creates new documents in a manner determined by the flags parameter, which can be: ...
Definition: doccom.cpp:1197
Holds a view on a a2dDocument.
Definition: docviewref.h:1804
void OnActivateViewSentFromChild(a2dViewEvent &viewevent)
sets m_view to the current active view, generate by a2dView::Activate()
Definition: docmdiref.cpp:360
virtual bool ProcessEvent(wxEvent &event)
Extend event processing to search the view's event table.
Definition: docmdiref.cpp:284
virtual a2dFileHistoryItem * GetHistoryFileItem(size_t i) const
return the a2dFileHistoryItem of the i'th file in the history
Definition: doccom.cpp:1663
bool Clear(bool force=true)
Clear remaining documents and templates.
Definition: doccom.cpp:469
virtual void ReportError(const a2dError &error, const wxString &errorstr=wxEmptyString)
concatenate to the the error report the given error.
Definition: comevt.cpp:1221
virtual bool ProcessEvent(wxEvent &event)
Definition: doccom.cpp:2309
virtual void OnDraw(wxDC &dc)
Override this function to draw the graphic (or just process EVT_PAINT)
Definition: docmdiref.cpp:425
#define EVT_CLOSE_VIEW(func)
event sent to document and view when a view needs to be closed ( can veto)
Definition: docviewref.h:993
int GetId() const
get id
Definition: comevt.h:1583
#define EVT_ACTIVATE_VIEW_SENT_FROM_CHILD(func)
event sent from view and its display up the parent chain.
Definition: docviewref.h:1001
virtual void RemoveFileFromHistory(size_t i)
Remove a file from history.
Definition: doccom.cpp:1644
a2dDocumentMDIChildFrame()
Constructor for two-step creation. Call a2dDocumentMDIChildFrame::Create later.
Definition: docmdiref.cpp:191
void Activate(bool activate)
is called via ProcessEvent() when the view becomes active
void AddCmdMenu(wxMenu *parentMenu, const a2dMenuIdItem &cmdId)
add a command menu to the parent menu, and connect it to the eventhandler of the frame ...
Definition: docmdiref.cpp:245
void OnCloseView(a2dCloseViewEvent &event)
default for closing a view in this frame
Definition: docmdiref.cpp:371
void OnCloseWindow(wxCloseEvent &event)
Closes the this window, if not vetoed.
Definition: docmdiref.cpp:133
void AddCmdMenu(wxMenu *parentMenu, const a2dMenuIdItem &cmdId)
add a command menu to the parent menu, and connect it to the eventhandler of the frame ...
Definition: docmdiref.cpp:154
~a2dDocumentMDIChildFrame()
Destructor.
Definition: docmdiref.cpp:229
wxItemKind GetKind() const
what kind of menu item we are
Definition: comevt.h:1605
void OnExit(wxCommandEvent &event)
Does a call to Close()
Definition: docmdiref.cpp:88
a2dDocviewGlobal * a2dDocviewGlobals
a global pointer to get to global instance of important classes.
Definition: doccom.cpp:2348
wxString GetIdName() const
name of the menu coupled to unique id
Definition: comevt.h:1653
virtual bool Close(bool force=false)
Close a view and remove the view from its document.
wxString GetLabel() const
get label text
Definition: comevt.h:1597
virtual bool ProcessEvent(wxEvent &event)
Extend event processing to search the view's event table.
virtual void ReportErrorF(const a2dError &error, const wxChar *Format,...)
concatenate to the the error report the given error.
Definition: comevt.cpp:1312
bool m_destroyOnCloseView
when closing the view the frame will be destroyed.
Definition: docmdiref.h:302
see a2dDocumentEvent
Definition: docviewref.h:356
a2dDocumentCommandProcessor * GetDocviewCommandProcessor() const
Gets a2dDocumentCommandProcessor pointer.
Definition: doccom.h:1034
Use wxDocMDIParentFrame instead of wxDocMDIParentFrame.
Definition: docmdiref.h:35
virtual bool ProcessEvent(wxEvent &event)
Extend event processing to search the document manager's event table.
Definition: docmdiref.cpp:122
holds one error report.
Definition: gen.h:623
a2dSmrtPtr< a2dView > m_view
Definition: docmdiref.h:299
void OnCmdMenuId(wxCommandEvent &event)
Definition: docmdiref.cpp:234
bool Create(wxMDIParentFrame *frame, a2dView *view=NULL, wxWindowID id=-1, const wxString &title=wxT("a2dDocumentMDIChildFrame"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE, const wxString &name=wxT("a2dDocumentMDIChildFrame"))
Constructor for two-step creation (2nd step).
Definition: docmdiref.cpp:210
void SetView(a2dView *view)
set a pointer to a a2dView to sent view events to.
Definition: docmdiref.cpp:267
holds one file for placing in history list
Definition: doccom.h:1067
docmdiref.cpp Source File -- Sun Oct 12 2014 17:04:15 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation