1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "toolbar.h"
17 #include "wx/motif/toolbar.h"
20 #include <Xm/PushBG.h>
22 #include <Xm/ToggleB.h>
23 #include <Xm/ToggleBG.h>
26 #include "wx/motif/private.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
31 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
35 wxToolBar::wxToolBar():
36 m_widgets(wxKEY_INTEGER
)
45 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
46 long style
, const wxString
& name
)
54 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
55 m_foregroundColour
= parent
->GetForegroundColour();
56 m_windowStyle
= style
;
60 if (parent
) parent
->AddChild(this);
62 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
64 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
65 xmFormWidgetClass
, parentWidget
,
66 XmNtraversalOn
, False
,
67 XmNhorizontalSpacing
, 0,
68 XmNverticalSpacing
, 0,
71 m_mainWidget
= (WXWidget
) toolbar
;
73 SetCanAddEventHandler(TRUE
);
74 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
76 SetFont(* parent
->GetFont());
77 ChangeBackgroundColour();
82 wxToolBar::~wxToolBar()
87 bool wxToolBar::CreateTools()
89 if (m_tools
.Number() == 0)
93 Widget prevButton
= (Widget
) 0;
94 wxNode
* node
= m_tools
.First();
97 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
98 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok())
100 Widget button
= (Widget
) 0;
102 if (tool
->m_isToggle
)
104 button
= XtVaCreateManagedWidget("toggleButton",
105 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
106 XmNleftAttachment
, (prevButton
== (Widget
) 0) ? XmATTACH_FORM
: XmATTACH_WIDGET
,
107 XmNleftWidget
, (prevButton
== (Widget
) 0) ? NULL
: prevButton
,
109 XmNtopAttachment
, XmATTACH_FORM
,
110 // XmNpushButtonEnabled, True,
111 XmNmultiClick
, XmMULTICLICK_KEEP
,
112 XmNlabelType
, XmPIXMAP
,
117 button
= XtVaCreateManagedWidget("button",
118 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
119 XmNleftAttachment
, (prevButton
== (Widget
) 0) ? XmATTACH_FORM
: XmATTACH_WIDGET
,
120 XmNleftWidget
, (prevButton
== (Widget
) 0) ? NULL
: prevButton
,
122 XmNtopAttachment
, XmATTACH_FORM
,
123 XmNpushButtonEnabled
, True
,
124 XmNmultiClick
, XmMULTICLICK_KEEP
,
125 XmNlabelType
, XmPIXMAP
,
129 // For each button, if there is a mask, we must create
130 // a new wxBitmap that has the correct background colour
131 // for the button. Otherwise the background will just be
132 // e.g. black if a transparent XPM has been loaded.
133 if (tool
->m_bitmap1
.GetMask())
135 wxBitmap
newBitmap(tool
->m_bitmap1
.GetWidth(),
136 tool
->m_bitmap1
.GetHeight(),
137 tool
->m_bitmap1
.GetDepth());
139 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
144 col
.SetPixel(backgroundPixel
);
148 srcDC
.SelectObject(tool
->m_bitmap1
);
149 destDC
.SelectObject(newBitmap
);
151 wxBrush
brush(col
, wxSOLID
);
152 destDC
.SetOptimization(FALSE
);
153 destDC
.SetBackground(brush
);
155 destDC
.Blit(0, 0, tool
->m_bitmap1
.GetWidth(), tool
->m_bitmap1
.GetHeight(), & srcDC
, 0, 0, wxCOPY
, TRUE
);
157 tool
->m_bitmap1
= newBitmap
;
159 if (tool
->m_bitmap2
.Ok() && tool
->m_bitmap2
.GetMask())
161 wxBitmap
newBitmap(tool
->m_bitmap2
.GetWidth(),
162 tool
->m_bitmap2
.GetHeight(),
163 tool
->m_bitmap2
.GetDepth());
165 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
170 col
.SetPixel(backgroundPixel
);
174 srcDC
.SelectObject(tool
->m_bitmap2
);
175 destDC
.SelectObject(newBitmap
);
177 wxBrush
brush(col
, wxSOLID
);
178 destDC
.SetOptimization(FALSE
);
179 destDC
.SetBackground(brush
);
181 destDC
.Blit(0, 0, tool
->m_bitmap2
.GetWidth(), tool
->m_bitmap2
.GetHeight(), & srcDC
, 0, 0, wxCOPY
, TRUE
);
183 tool
->m_bitmap2
= newBitmap
;
185 Pixmap pixmap
= (Pixmap
) tool
->m_bitmap1
.GetPixmap();
186 Pixmap insensPixmap
= (Pixmap
) tool
->m_bitmap1
.GetInsensPixmap();
188 if (tool
->m_isToggle
)
190 Pixmap pixmap2
= (Pixmap
) 0;
191 Pixmap insensPixmap2
= (Pixmap
) 0;
193 // If there's a bitmap for the toggled state, use it,
194 // otherwise generate one.
195 if (tool
->m_bitmap2
.Ok())
197 pixmap2
= (Pixmap
) tool
->m_bitmap2
.GetPixmap();
198 insensPixmap2
= (Pixmap
) tool
->m_bitmap2
.GetInsensPixmap();
202 pixmap2
= (Pixmap
) tool
->m_bitmap1
.GetArmPixmap(button
);
203 // This has to be both toggled and insensitive, but
204 // wxBitmap doesn't yet have a member to store & destroy
205 // it, so make it the same as pixmap2. Actually it's not
207 insensPixmap2
= pixmap2
;
209 XtVaSetValues (button
,
210 XmNlabelPixmap
, pixmap
,
211 XmNselectPixmap
, pixmap
,
212 XmNlabelInsensitivePixmap
, insensPixmap
,
213 XmNselectInsensitivePixmap
, insensPixmap
,
214 XmNarmPixmap
, pixmap2
,
215 XmNlabelType
, XmPIXMAP
,
221 XtVaSetValues(button
,
222 XmNlabelPixmap
, pixmap
,
223 XmNlabelInsensitivePixmap
, insensPixmap
,
227 m_widgets
.Append(tool
->m_index
, (wxObject
*) button
);
238 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
240 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
244 wxSize
wxToolBar::GetMaxSize() const
250 // The button size is bigger than the bitmap size
251 wxSize
wxToolBar::GetToolSize() const
254 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
257 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
259 wxNode
*node
= m_tools
.Find((long)toolIndex
);
262 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
263 tool
->m_enabled
= enable
;
264 // TODO enable button
268 void wxToolBar::ToggleTool(int toolIndex
, bool toggle
)
270 wxNode
*node
= m_tools
.Find((long)toolIndex
);
273 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
274 if (tool
->m_isToggle
)
276 tool
->m_toggleState
= toggle
;
277 // TODO: set toggle state
282 void wxToolBar::ClearTools()
284 wxNode
* node
= m_widgets
.First();
287 Widget button
= (Widget
) node
->Data();
288 XtDestroyWidget(button
);
293 wxToolBarBase::ClearTools();
296 // If pushedBitmap is NULL, a reversed version of bitmap is
297 // created and used as the pushed/toggled image.
298 // If toggle is TRUE, the button toggles between the two states.
300 wxToolBarTool
*wxToolBar::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
301 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
303 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, (wxBitmap
*)NULL
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
304 tool
->m_clientData
= clientData
;
309 tool
->m_x
= m_xMargin
;
314 tool
->m_y
= m_yMargin
;
316 tool
->SetSize(GetDefaultButtonWidth(), GetDefaultButtonHeight());
318 m_tools
.Append((long)index
, tool
);