wxArt2D
xh_a2dmenu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xh_a2dmenu.cpp
3 // Purpose: XRC resource for a2dmenus and a2dmenubars
4 // Author: Vaclav Slavik, Klaas Holwerda, Leo Kadisoff
5 // Created: 2006/05/19
6 // RCS-ID: $Id: xh_a2dmenu.cpp,v 1.7 2009/09/30 19:17:00 titato Exp $
7 // Copyright: (c) 2006 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // For compilers that support precompilation, includes "wx.h".
12 #include "docviewprec.h"
13 #include <wx/xml/xml.h>
14 
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18 
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #include "wx/frame.h"
22 #include "wx/menu.h"
23 #endif
24 
25 #if wxUSE_XRC
26 
27 #include "wx/xrc/xh_menu.h"
28 
29 #include "wx/general/comevt.h"
30 #include "wx/docview/docviewref.h"
31 #include "wx/docview/docmdiref.h"
32 #include "wx/docview/xh_a2dmenu.h"
33 
34 IMPLEMENT_DYNAMIC_CLASS( a2dMenuXmlHandler, wxMenuXmlHandler )
35 
37  wxMenuXmlHandler()
38 {
39 }
40 
41 wxObject* a2dMenuXmlHandler::DoCreateResource()
42 {
43  if ( m_class == wxT( "a2dMenu" ) || m_class == wxT( "wxMenu" ) )
44  {
45  wxMenu* menu = new wxMenu( GetStyle() );
46  wxString title = GetText( wxT( "label" ) );
47  wxString help = GetText( wxT( "help" ) );
48 
49  bool oldins = m_insideA2DMenu;
50  m_insideA2DMenu = true;
51  CreateChildren( menu, true/*only this handler*/ );
52  m_insideA2DMenu = oldins;
53 
54  wxMenuBar* p_bar = wxDynamicCast( m_parent, wxMenuBar );
55  if ( p_bar )
56  p_bar->Append( menu, title );
57  else
58  {
59  wxMenu* p_menu = wxDynamicCast( m_parent, wxMenu );
60  if ( p_menu )
61  {
62  p_menu->Append( GetID(), title, menu, help );
63  if ( HasParam( wxT( "enabled" ) ) )
64  p_menu->Enable( GetID(), GetBool( wxT( "enabled" ) ) );
65  }
66  }
67 
68  return menu;
69  }
70 
71 
72 
73 
74 
75  else if ( m_class == wxT( "a2dMenuIdItem" ) )
76  {
77  wxMenu* p_menu = wxDynamicCast( m_parent, wxMenu );
78 
79  int id = GetID();
80  wxString label = GetText( wxT( "label" ) );
81  wxString accel = GetText( wxT( "accel" ), false );
82  wxString help = GetText( wxT( "help" ) );
83 
84  wxString a2dCmdIdName = GetName();// = GetText(wxT("a2dname"));
85  const a2dMenuIdItem& a2dmenu = a2dMenuIdItem::GetItemByName( a2dCmdIdName );
86  if( &a2dmenu == &a2dMenuIdItem::sm_noCmdMenuId )
87  {
88  wxString error = _T( "a2dMenuIdItem with id name: " ) + a2dCmdIdName + _T( " not know" );
89  wxMessageBox( error, _( "XRC problem" ), wxICON_INFORMATION | wxOK );
90  return NULL;
91  }
92  if ( label.IsEmpty() )
93  label = a2dmenu.GetLabel();
94  if ( help.IsEmpty() )
95  help = a2dmenu.GetHelp();
96 
97  wxItemKind kind = a2dmenu.GetKind();
98 
99  wxString fullLabel = label;
100  if ( !accel.IsEmpty() )
101  fullLabel << wxT( "\t" ) << accel;
102 
103  id = a2dmenu.GetId();
104  wxMenuItem* mitem = new wxMenuItem( p_menu, id, fullLabel, help, kind );
105 
106 #if (!defined(__WXMSW__) && !defined(__WXPM__)) || wxUSE_OWNER_DRAWN
107  if ( HasParam( wxT( "bitmap" ) ) )
108  mitem->SetBitmap( GetBitmap( wxT( "bitmap" ), wxART_MENU ) );
109 #endif
110  if ( a2dMenuBarXmlHandler::m_parentFrame )
111  {
112  a2dDocumentFrame* parentFrame = wxDynamicCast( a2dMenuBarXmlHandler::m_parentFrame, a2dDocumentFrame );
113  if ( parentFrame )
114  parentFrame->AddCmdMenu( p_menu, mitem );
115  a2dDocumentMDIChildFrame* mdiChildFrame = wxDynamicCast( a2dMenuBarXmlHandler::m_parentFrame, a2dDocumentMDIChildFrame );
116  if ( mdiChildFrame )
117  mdiChildFrame->AddCmdMenu( p_menu, mitem );
118  a2dDocumentMDIParentFrame* mdiParentFrame = wxDynamicCast( a2dMenuBarXmlHandler::m_parentFrame, a2dDocumentMDIParentFrame );
119  if ( mdiParentFrame )
120  mdiParentFrame->AddCmdMenu( p_menu, mitem );
121  }
122 
123  mitem->Enable( GetBool( wxT( "enabled" ), true ) );
124  if ( kind == wxITEM_CHECK )
125  mitem->Check( GetBool( wxT( "checked" ) ) );
126 
127  return NULL;
128  }
129  else
130  return wxMenuXmlHandler::DoCreateResource();
131 }
132 
133 bool a2dMenuXmlHandler::CanHandle( wxXmlNode* node )
134 {
135  return IsOfClass( node, wxT( "a2dMenu" ) ) || IsOfClass( node, wxT( "wxMenu" ) ) ||
136  ( m_insideA2DMenu &&
137  ( IsOfClass( node, wxT( "a2dMenuIdItem" ) ) ||
138  IsOfClass( node, wxT( "wxMenuItem" ) ) ||
139  IsOfClass( node, wxT( "break" ) ) ||
140  IsOfClass( node, wxT( "separator" ) ) )
141  );
142 }
143 
144 
145 /********************************************************************
146 * a2dMenuBarXmlHandler
147 *********************************************************************/
148 
149 IMPLEMENT_DYNAMIC_CLASS( a2dMenuBarXmlHandler, wxMenuBarXmlHandler )
150 
151 wxFrame* a2dMenuBarXmlHandler::m_parentFrame = NULL;
152 
153 a2dMenuBarXmlHandler::a2dMenuBarXmlHandler() : wxMenuBarXmlHandler()
154 {
155 }
156 
157 wxObject* a2dMenuBarXmlHandler::DoCreateResource()
158 {
159  wxFrame* curFrame = m_parentFrame;
160  wxMenuBar* menubar = NULL;
161  if( m_parentAsWindow )
162  {
163  wxFrame* parentFrame = wxDynamicCast( m_parent, wxFrame );
164  if( parentFrame )
165  {
166  m_parentFrame = parentFrame;
167  menubar = m_parentFrame->GetMenuBar();
168  CreateChildren( menubar );
169  }
170  }
171 
172  if ( !menubar )
173  {
174  menubar = new wxMenuBar( GetStyle() );
175  CreateChildren( menubar );
176  if ( m_parentAsWindow && m_parentFrame )
177  {
178  m_parentFrame->SetMenuBar( menubar );
179  }
180  }
181 
182  m_parentFrame = curFrame;
183 
184  return menubar;
185 }
186 
187 bool a2dMenuBarXmlHandler::CanHandle( wxXmlNode* node )
188 {
189  return IsOfClass( node, wxT( "a2dMenuBar" ) ) || IsOfClass( node, wxT( "wxMenuBar" ) );
190 }
191 
192 #endif // wxUSE_XRC
193 
194 /********************************************************************
195 * a2dToolBarXmlHandler
196 *********************************************************************/
197 
198 #if wxUSE_XRC && wxUSE_TOOLBAR
199 
200 #include "wx/xrc/xh_toolb.h"
201 
202 #ifndef WX_PRECOMP
203 #include "wx/frame.h"
204 #include "wx/toolbar.h"
205 #endif
206 
207 IMPLEMENT_DYNAMIC_CLASS( a2dToolBarXmlHandler, wxToolBarXmlHandler )
208 
210  : wxToolBarXmlHandler(), m_isInside( false ), m_toolbar( NULL )
211 {
212 }
213 
214 wxObject* a2dToolBarXmlHandler::DoCreateResource()
215 {
216  if ( m_class == wxT( "a2dToolCmd" ) )
217  {
218  wxCHECK_MSG( m_toolbar, NULL, wxT( "Incorrect syntax of XRC resource: tool not within a toolbar!" ) );
219 
220  int id = GetID();
221  wxString a2dCmdIdName = GetName();
222  const a2dMenuIdItem& a2dmenu = a2dMenuIdItem::GetItemByName( a2dCmdIdName );
223  if( &a2dmenu == &a2dMenuIdItem::sm_noCmdMenuId )
224  {
225  wxString error = _T( "a2dToolCmd with id name: " ) + a2dCmdIdName + _T( " not know" );
226  wxMessageBox( error, _( "XRC problem" ), wxICON_INFORMATION | wxOK );
227  return NULL;
228  }
229 
230  id = a2dmenu.GetId();
231  wxString label = GetText( wxT( "label" ) );
232  wxString help = GetText( wxT( "tooltip" ) );
233  wxBitmap selBitmap = GetBitmap( wxT( "bitmap" ), wxART_TOOLBAR );
234  if ( ! selBitmap.Ok() )
235  selBitmap = a2dmenu.GetBitmap( false );
236 
237  wxString error = _T( "No Bitmap for a2dToolCmd found for:" ) + a2dCmdIdName;
238  wxASSERT_MSG( selBitmap.Ok(), error );
239 
240  if ( label.IsEmpty() )
241  label = a2dmenu.GetLabel();
242  if ( help.IsEmpty() )
243  help = a2dmenu.GetHelp();
244 
245 
246  if ( GetPosition() != wxDefaultPosition )
247  {
248  m_toolbar->InsertTool( GetPosition().x, id, label,
249  selBitmap,
250  GetBitmap( wxT( "bitmap2" ), wxART_TOOLBAR ),
251  GetBool( wxT( "toggle" ) ) ? wxITEM_CHECK : wxITEM_NORMAL,
252  help,
253  GetText( wxT( "longhelp" ) ) );
254  }
255  else
256  {
257  wxItemKind kind = wxITEM_NORMAL;
258  if ( GetBool( wxT( "radio" ) ) )
259  kind = wxITEM_RADIO;
260  if ( GetBool( wxT( "toggle" ) ) )
261  {
262  wxASSERT_MSG( kind == wxITEM_NORMAL,
263  _T( "can't have both toggleable and radion button at once" ) );
264  kind = wxITEM_CHECK;
265  }
266  m_toolbar->AddTool( id,
267  label,
268  selBitmap,
269  GetBitmap( wxT( "bitmap2" ), wxART_TOOLBAR ),
270  kind,
271  help,
272  GetText( wxT( "longhelp" ) ) );
273 
274  if ( GetBool( wxT( "disabled" ) ) )
275  m_toolbar->EnableTool( GetID(), false );
276  }
277 
278 
279  if ( a2dToolBarXmlHandler::m_parentAsWindow )
280  {
281  a2dDocumentFrame* parentFrame = wxDynamicCast( a2dMenuBarXmlHandler::m_parentFrame, a2dDocumentFrame );
282  if ( parentFrame )
283  parentFrame->ConnectCmdId( a2dmenu );
284  /*
285  a2dDocumentMDIChildFrame *mdiChildFrame = wxDynamicCast(a2dMenuBarXmlHandler::m_parentFrame, a2dDocumentMDIChildFrame);
286  if (mdiChildFrame)
287  mdiChildFrame->ConnectCmdId( a2dmenu );
288  a2dDocumentMDIParentFrame *mdiParentFrame = wxDynamicCast(a2dMenuBarXmlHandler::m_parentFrame, a2dDocumentMDIParentFrame);
289  if (mdiParentFrame)
290  mdiParentFrame->ConnectCmdId( a2dmenu );
291  */
292  }
293 
294  return m_toolbar; // must return non-NULL
295  }
296  else if ( m_class == wxT( "a2dToolBar" ) )
297  {
298  int style = GetStyle( wxT( "style" ), wxNO_BORDER | wxTB_HORIZONTAL );
299 #ifdef __WXMSW__
300  if ( !( style & wxNO_BORDER ) ) style |= wxNO_BORDER;
301 #endif
302 
303  XRC_MAKE_INSTANCE( toolbar, wxToolBar )
304 
305  toolbar->Create( m_parentAsWindow,
306  GetID(),
307  GetPosition(),
308  GetSize(),
309  style,
310  GetName() );
311  SetupWindow( toolbar );
312 
313  wxSize bmpsize = GetSize( wxT( "bitmapsize" ) );
314  if ( !( bmpsize == wxDefaultSize ) )
315  toolbar->SetToolBitmapSize( bmpsize );
316  wxSize margins = GetSize( wxT( "margins" ) );
317  if ( !( margins == wxDefaultSize ) )
318  toolbar->SetMargins( margins.x, margins.y );
319  long packing = GetLong( wxT( "packing" ), -1 );
320  if ( packing != -1 )
321  toolbar->SetToolPacking( packing );
322  long separation = GetLong( wxT( "separation" ), -1 );
323  if ( separation != -1 )
324  toolbar->SetToolSeparation( separation );
325 
326  wxXmlNode* children_node = GetParamNode( wxT( "object" ) );
327  if ( !children_node )
328  children_node = GetParamNode( wxT( "object_ref" ) );
329 
330  if ( children_node == NULL ) return toolbar;
331 
332  m_isInside = true;
333  m_toolbar = toolbar;
334 
335  wxXmlNode* n = children_node;
336 
337  while ( n )
338  {
339  if ( ( n->GetType() == wxXML_ELEMENT_NODE ) &&
340  ( n->GetName() == wxT( "object" ) || n->GetName() == wxT( "object_ref" ) ) )
341  {
342  wxObject* created = CreateResFromNode( n, toolbar, NULL );
343  wxControl* control = wxDynamicCast( created, wxControl );
344  if ( !IsOfClass( n, wxT( "tool" ) ) &&
345  !IsOfClass( n, wxT( "a2dToolCmd" ) ) &&
346  !IsOfClass( n, wxT( "separator" ) ) &&
347  control != NULL )
348  toolbar->AddControl( control );
349  }
350  n = n->GetNext();
351  }
352 
353  m_isInside = false;
354  m_toolbar = NULL;
355 
356  toolbar->Realize();
357 
358  if ( m_parentAsWindow && !GetBool( wxT( "dontattachtoframe" ) ) )
359  {
360  wxFrame* parentFrame = wxDynamicCast( m_parent, wxFrame );
361  if ( parentFrame )
362  parentFrame->SetToolBar( toolbar );
363  }
364 
365  return toolbar;
366  }
367  else
368  return a2dToolBarXmlHandler::DoCreateResource();
369 
370 }
371 
372 bool a2dToolBarXmlHandler::CanHandle( wxXmlNode* node )
373 {
374  return ( ( !m_isInside && IsOfClass( node, wxT( "a2dToolBar" ) ) ) ||
375  ( m_isInside && IsOfClass( node, wxT( "a2dToolCmd" ) ) ) ||
376  wxToolBarXmlHandler::CanHandle( node ) );
377 }
378 
379 #endif // wxUSE_XRC && wxUSE_TOOLBAR
The a2dDocumentMDIChildFrame class provides a default frame for displaying documents.
Definition: docmdiref.h:150
#define wxDynamicCast(obj, className)
Define wxDynamicCast so that it will give a compiler error for unrelated types.
Definition: gen.h:75
Frame classes for MDI document/view applications.
static const a2dMenuIdItem sm_noCmdMenuId
constant to defined non valid id.
Definition: comevt.h:1570
store a menu Id generated by XRCID( menuIdString ) plus a menustring and helpstring ...
Definition: comevt.h:1563
void AddCmdMenu(wxMenu *parentMenu, const a2dMenuIdItem &cmdId)
add a menu to the parent menu, and connect it to the eventhandler of the frame
Docview classes for document view, window and frame.
The a2dDocumentFrame class provides a default frame for displaying documents.
Definition: docviewref.h:3357
int GetId() const
get id
Definition: comevt.h:1583
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 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
wxItemKind GetKind() const
what kind of menu item we are
Definition: comevt.h:1605
static const a2dMenuIdItem & GetItemByName(const wxString &menuIdName)
search Id given the name of the command
Definition: comevt.cpp:1616
wxString GetLabel() const
get label text
Definition: comevt.h:1597
command processor and intializing and event handling classes specific for wxDocview.
Use wxDocMDIParentFrame instead of wxDocMDIParentFrame.
Definition: docmdiref.h:35
xh_a2dmenu.cpp Source File -- Sun Oct 12 2014 17:04:26 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation