Code::Blocks  SVN r11506
xtra_res.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 11019 $
6  * $Id: xtra_res.cpp 11019 2017-02-21 23:52:17Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/xtra_res.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include "xtra_res.h"
14  #include "scrollingdialog.h"
15  #include <wx/wx.h>
16 #endif
17 
18 #include <wx/xml/xml.h>
19 
21 // Name: xh_toolb.cpp
22 // Purpose: XRC resource for wxBoxSizer
23 // Author: Vaclav Slavik
24 // Created: 2000/08/11
25 // RCS-ID: $Id: xtra_res.cpp 11019 2017-02-21 23:52:17Z fuscated $
26 // Copyright: (c) 2000 Vaclav Slavik
27 // Licence: wxWindows licence
29 // Modified by Ricardo Garcia for Code::Blocks
30 // Comment: Things would've been much easier if field m_isInside had been
31 // protected instead of private! >:(
33 
35 : wxXmlResourceHandler(), m_isInside(FALSE), m_isAddon(false), m_toolbar(NULL)
36 {
37  XRC_ADD_STYLE(wxTB_FLAT);
38  XRC_ADD_STYLE(wxTB_DOCKABLE);
39  XRC_ADD_STYLE(wxTB_VERTICAL);
40  XRC_ADD_STYLE(wxTB_HORIZONTAL);
41  XRC_ADD_STYLE(wxTB_TEXT);
42  XRC_ADD_STYLE(wxTB_NOICONS);
43  XRC_ADD_STYLE(wxTB_NODIVIDER);
44  XRC_ADD_STYLE(wxTB_NOALIGN);
45 }
46 
48  const wxArtClient& defaultArtClient, wxSize size)
49 {
50  wxBitmap bitmap = GetBitmap(param, defaultArtClient, wxDefaultSize);
51  if (!bitmap.Ok()) // == wxNullBitmap
52  return bitmap;
53 
54  int bw = bitmap.GetWidth();
55  int bh = bitmap.GetHeight();
56  if (size == wxSize(bw, bh))
57  return bitmap;
58 
59  wxImage image = bitmap.ConvertToImage();
60 
61  int w = size.GetWidth();
62  int h = size.GetHeight();
63  int x = (w - bw) / 2;
64  int y = (h - bh) / 2;
65 
66  if (image.HasAlpha()) // Resize doesn't handle Alpha... :-(
67  {
68  const unsigned char *data = image.GetData();
69  const unsigned char *alpha = image.GetAlpha();
70  unsigned char *rgb = (unsigned char *) calloc(w * h, 3);
71  unsigned char *a = (unsigned char *) calloc(w * h, 1);
72 
73  // copy Data/Alpha from smaller bitmap to larger bitmap
74  for (int row = 0; row < bh; row++)
75  {
76  memcpy(rgb + ((row + y) * w + x) * 3, data + (row * bw) * 3, bw * 3);
77  memcpy(a + ((row + y) * w + x), alpha + (row * bw), bw);
78  }
79 
80  image = wxImage(w, h, rgb, a);
81  }
82  else
83  image.Resize(size, wxPoint(x,y));
84 
85  return wxBitmap(image);
86 }
87 
89 {
90  wxToolBar* toolbar=NULL;
91  if (m_class == _T("tool"))
92  {
93  wxCHECK_MSG(m_toolbar, NULL, _("Incorrect syntax of XRC resource: tool not within a toolbar!"));
94 
95  wxSize bitmapSize = m_toolbar->GetToolBitmapSize();
96 
97  if (GetPosition() != wxDefaultPosition)
98  {
99  m_toolbar->AddTool(GetID(),
100  #if wxCHECK_VERSION(3, 0, 0)
102  #endif
103  GetCenteredBitmap(_T("bitmap"), wxART_TOOLBAR, bitmapSize),
104  GetCenteredBitmap(_T("bitmap2"), wxART_TOOLBAR, bitmapSize),
105  #if !wxCHECK_VERSION(3, 0, 0)
106  GetBool(_T("toggle")),
107  GetPosition().x,
108  GetPosition().y,
109  NULL,
110  #else
112  #endif
113  GetText(_T("tooltip")),
114  GetText(_T("longhelp")));
115  if (GetBool(_T("disabled")))
116  {
117  m_toolbar->Realize();
118  m_toolbar->EnableTool(GetID(),false);
119  }
120  }
121  else
122  {
123  wxItemKind kind = wxITEM_NORMAL;
124  if (GetBool(_T("radio")))
125  kind = wxITEM_RADIO;
126  if (GetBool(_T("toggle")))
127  {
128  wxASSERT_MSG( kind == wxITEM_NORMAL,
129  _("can't have both toggleable and radion button at once") );
130  kind = wxITEM_CHECK;
131  }
132  m_toolbar->AddTool(GetID(),
133  GetText(_T("label")),
134  GetCenteredBitmap(_T("bitmap"), wxART_TOOLBAR, bitmapSize),
135  GetCenteredBitmap(_T("bitmap2"), wxART_TOOLBAR, bitmapSize),
136  kind,
137  GetText(_T("tooltip")),
138  GetText(_T("longhelp")));
139  if (GetBool(_T("disabled")))
140  {
141  m_toolbar->Realize();
142  m_toolbar->EnableTool(GetID(),false);
143  }
144  }
145  return m_toolbar; // must return non-NULL
146  }
147 
148  else if (m_class == _T("separator"))
149  {
150  wxCHECK_MSG(m_toolbar, NULL, _("Incorrect syntax of XRC resource: separator not within a toolbar!"));
152  return m_toolbar; // must return non-NULL
153  }
154 
155  else /*<object class="wxToolBar">*/
156  {
157  m_isAddon=(m_class == _T("wxToolBarAddOn"));
158  if(m_isAddon)
159  { // special case: Only add items to toolbar
160  toolbar=(wxToolBar*)m_instance;
161  // XRC_MAKE_INSTANCE(toolbar, wxToolBar);
162  }
163  else
164  {
165  int style = GetStyle(_T("style"), wxNO_BORDER | wxTB_HORIZONTAL);
166  #ifdef __WXMSW__
167  if (!(style & wxNO_BORDER)) style |= wxNO_BORDER;
168  #endif
169 
170  // XRC_MAKE_INSTANCE(toolbar, wxToolBar)
171  if (m_instance)
172  toolbar = wxStaticCast(m_instance, wxToolBar);
173  if (!toolbar)
174  toolbar = new wxToolBar;
175 
176  toolbar->Create(m_parentAsWindow,
177  GetID(),
178  GetPosition(),
179  GetSize(),
180  style,
181  GetName());
182  wxSize bmpsize = GetSize(_T("bitmapsize"));
183  if (!(bmpsize == wxDefaultSize))
184  toolbar->SetToolBitmapSize(bmpsize);
185  wxSize margins = GetSize(_T("margins"));
186  if (!(margins == wxDefaultSize))
187  toolbar->SetMargins(margins.x, margins.y);
188  long packing = GetLong(_T("packing"), -1);
189  if (packing != -1)
190  toolbar->SetToolPacking(packing);
191  long separation = GetLong(_T("separation"), -1);
192  if (separation != -1)
193  toolbar->SetToolSeparation(separation);
194  }
195 
196  wxXmlNode *children_node = GetParamNode(_T("object"));
197  if (!children_node)
198  children_node = GetParamNode(_T("object_ref"));
199 
200  if (children_node == NULL) return toolbar;
201 
202  m_isInside = TRUE;
203  m_toolbar = toolbar;
204 
205  wxXmlNode *n = children_node;
206 
207  while (n)
208  {
209  if ((n->GetType() == wxXML_ELEMENT_NODE) &&
210  (n->GetName() == _T("object") || n->GetName() == _T("object_ref")))
211  {
212  wxObject *created = CreateResFromNode(n, toolbar, NULL);
213  wxControl *control = wxDynamicCast(created, wxControl);
214  if (!IsOfClass(n, _T("tool")) &&
215  !IsOfClass(n, _T("separator")) &&
216  control != NULL &&
217  control != toolbar)
218  {
219  //Manager::Get()->GetLogManager()->DebugLog(F(_T("control=%p, parent=%p, toolbar=%p"), control, control->GetParent(), toolbar));
220  toolbar->AddControl(control);
221  }
222  }
223  n = n->GetNext();
224  }
225 
226  toolbar->Realize();
227 
228  m_isInside = FALSE;
229  m_toolbar = NULL;
230 
231  if(!m_isAddon)
232  {
233  if (m_parentAsWindow && !GetBool(_T("dontattachtoframe")))
234  {
235  wxFrame *parentFrame = wxDynamicCast(m_parent, wxFrame);
236  if (parentFrame)
237  parentFrame->SetToolBar(toolbar);
238  }
239  }
240  m_isAddon=false;
241  return toolbar;
242  }
243 }
244 
246 {
247 // NOTE (mandrav#1#): wxXmlResourceHandler::IsOfClass() doesn't work in unicode (2.6.2)
248 // Don't ask why. It does this and doesn't work for our custom handler:
249 // return node->GetPropVal(wxT("class"), wxEmptyString) == classname;
250 //
251 // This works though:
252 // return node->GetPropVal(wxT("class"), wxEmptyString).Matches(classname);
253 //
254 // Don't ask me why... >:-|
255 
256  #if wxCHECK_VERSION(3, 0, 0)
257  bool istbar = node->GetAttribute(wxT("class"), wxEmptyString).Matches(_T("wxToolBarAddOn"));
258  bool istool = node->GetAttribute(wxT("class"), wxEmptyString).Matches(_T("tool"));
259  bool issep = node->GetAttribute(wxT("class"), wxEmptyString).Matches(_T("separator"));
260  #else
261  bool istbar = node->GetPropVal(wxT("class"), wxEmptyString).Matches(_T("wxToolBarAddOn"));
262  bool istool = node->GetPropVal(wxT("class"), wxEmptyString).Matches(_T("tool"));
263  bool issep = node->GetPropVal(wxT("class"), wxEmptyString).Matches(_T("separator"));
264  #endif
265 
266  return ((!m_isInside && istbar) ||
267  (m_isInside && istool) ||
268  (m_isInside && issep));
269 }
270 
271 
272 
274 
276 {
277 }
278 
280 {
281  XRC_MAKE_INSTANCE(dlg, wxScrollingDialog);
282 
283  dlg->Create(m_parentAsWindow,
284  GetID(),
285  GetText(wxT("title")),
287  GetStyle(wxT("style"), wxDEFAULT_DIALOG_STYLE),
288  GetName());
289 
290  if (HasParam(wxT("size")))
291  dlg->SetClientSize(GetSize(wxT("size"), dlg));
292  if (HasParam(wxT("pos")))
293  dlg->Move(GetPosition());
294  if (HasParam(wxT("icon")))
295  dlg->SetIcon(GetIcon(wxT("icon"), wxART_FRAME_ICON));
296 
297  SetupWindow(dlg);
298 
299  CreateChildren(dlg);
300 
301  if (GetBool(wxT("centered"), false))
302  dlg->Centre();
303 
304  return dlg;
305 }
306 
308 {
309  return IsOfClass(node, wxT("wxScrollingDialog"));
310 }
virtual wxSize GetToolBitmapSize() const
virtual void SetToolPacking(int packing)
virtual wxToolBarToolBase * AddSeparator()
#define wxASSERT_MSG(condition, message)
#define wxDynamicCast(ptr, classname)
wxArtClient wxART_TOOLBAR
bool GetAttribute(const wxString &attrName, wxString *value) const
int GetWidth() const
int GetHeight() const
wxObject * DoCreateResource() override
Definition: xtra_res.cpp:279
bool CanHandle(wxXmlNode *node) override
Definition: xtra_res.cpp:245
virtual void EnableTool(int toolId, bool enable)
virtual void SetToolBitmapSize(const wxSize &size)
#define _T(string)
virtual wxImage ConvertToImage() const
unsigned char * GetAlpha() const
const wxString & GetName() const
#define wxT(string)
unsigned char * GetData() const
virtual void SetToolSeparation(int separation)
bool HasAlpha() const
wxXmlNode * GetNext() const
wxItemKind
wxXmlNodeType GetType() const
virtual int GetWidth() const
const wxSize wxDefaultSize
const wxPoint wxDefaultPosition
#define wxCHECK_VERSION(major, minor, release)
IMPLEMENT_DYNAMIC_CLASS(cbDummyEditor, wxPGEditor)
#define wxDEFAULT_DIALOG_STYLE
bool CanHandle(wxXmlNode *node) override
Definition: xtra_res.cpp:307
wxString wxEmptyString
virtual void SetMargins(int x, int y)
virtual wxToolBarToolBase * AddControl(wxControl *control, const wxString &label=wxEmptyString)
const wxString & _(const wxString &string)
wxBitmap GetCenteredBitmap(const wxString &param=wxT("bitmap"), const wxArtClient &defaultArtClient=wxART_OTHER, wxSize size=wxDefaultSize)
Definition: xtra_res.cpp:47
#define wxCHECK_MSG(condition, retValue, message)
#define wxStaticCast(ptr, classname)
wxArtClient wxART_FRAME_ICON
virtual wxToolBarToolBase * AddTool(wxToolBarToolBase *tool)
wxObject * DoCreateResource() override
Definition: xtra_res.cpp:88
#define wxNO_BORDER
virtual int GetHeight() const
wxImage & Resize(const wxSize &size, const wxPoint &pos, int red=-1, int green=-1, int blue=-1)
#define NULL
Definition: prefix.cpp:59
wxToolBar * m_toolbar
Definition: xtra_res.h:27
virtual bool Realize()