]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tbargtk.cpp
* Fixed a bug in notebook.tex
[wxWidgets.git] / src / gtk / tbargtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: tbargtk.cpp
3// Purpose: GTK toolbar
4// Author: Robert Roebling
5// Modified by:
6// Created: 01/02/97
a3622daa 7// RCS-ID:
c801d85f 8// Copyright: (c) Robert Roebling
a3622daa 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "tbargtk.h"
14#endif
15
16#include "wx/toolbar.h"
1a5a8367 17#include <wx/intl.h>
c801d85f 18
314055fa
RR
19//-----------------------------------------------------------------------------
20// data
21//-----------------------------------------------------------------------------
22
23extern bool g_blockEventsOnDrag;
24
c801d85f
KB
25//-----------------------------------------------------------------------------
26// wxToolBarTool
27//-----------------------------------------------------------------------------
28
29IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool,wxObject)
a3622daa
VZ
30
31wxToolBarTool::wxToolBarTool( wxToolBar *owner, int theIndex,
debe6624
JS
32 const wxBitmap& bitmap1, const wxBitmap& bitmap2,
33 bool toggle, wxObject *clientData,
fc008f25
RR
34 const wxString& shortHelpString, const wxString& longHelpString,
35 GtkWidget *item )
c801d85f
KB
36{
37 m_owner = owner;
38 m_index = theIndex;
39 m_bitmap1 = bitmap1;
40 m_bitmap2 = bitmap2;
41 m_isToggle = toggle;
42 m_enabled = TRUE;
43 m_toggleState = FALSE;
44 m_shortHelpString = shortHelpString;
45 m_longHelpString = longHelpString;
46 m_isMenuCommand = TRUE;
47 m_clientData = clientData;
48 m_deleteSecondBitmap = FALSE;
fc008f25
RR
49 m_item = item;
50}
c801d85f 51
a3622daa 52wxToolBarTool::~wxToolBarTool()
c801d85f 53{
fc008f25 54}
c801d85f
KB
55
56//-----------------------------------------------------------------------------
2f2aa628 57// "clicked" (internal from gtk_toolbar)
c801d85f
KB
58//-----------------------------------------------------------------------------
59
60static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
61{
314055fa 62 if (g_blockEventsOnDrag) return;
c801d85f 63 if (!tool->m_enabled) return;
a3622daa 64
c801d85f 65 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
a3622daa 66
c801d85f 67 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
fc008f25 68}
c801d85f 69
2f2aa628
RR
70//-----------------------------------------------------------------------------
71// "enter_notify_event"
72//-----------------------------------------------------------------------------
73
314055fa
RR
74static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
75 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
76{
77 if (g_blockEventsOnDrag) return TRUE;
78
79 tool->m_owner->OnMouseEnter( tool->m_index );
80
83058c58 81 return FALSE;
314055fa
RR
82}
83
2f2aa628
RR
84//-----------------------------------------------------------------------------
85// wxToolBar
c801d85f
KB
86//-----------------------------------------------------------------------------
87
716b7364 88IMPLEMENT_DYNAMIC_CLASS(wxToolBar,wxControl)
c801d85f 89
a3622daa 90wxToolBar::wxToolBar()
c801d85f 91{
fc008f25 92}
c801d85f 93
a3622daa 94wxToolBar::wxToolBar( wxWindow *parent, wxWindowID id,
c801d85f 95 const wxPoint& pos, const wxSize& size,
debe6624 96 long style, const wxString& name )
c801d85f
KB
97{
98 Create( parent, id, pos, size, style, name );
fc008f25 99}
c801d85f 100
a3622daa 101wxToolBar::~wxToolBar()
c801d85f 102{
fc008f25 103}
c801d85f 104
a3622daa 105bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
c801d85f 106 const wxPoint& pos, const wxSize& size,
debe6624 107 long style, const wxString& name )
c801d85f
KB
108{
109 m_needParent = TRUE;
a3622daa 110
c801d85f
KB
111 PreCreation( parent, id, pos, size, style, name );
112
113 m_tools.DeleteContents( TRUE );
a3622daa 114
c801d85f 115 m_widget = gtk_handle_box_new();
a3622daa 116
c801d85f 117 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_ICONS ) );
a3622daa 118
c801d85f 119 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
a3622daa 120
c801d85f 121 gtk_widget_show( GTK_WIDGET(m_toolbar) );
a3622daa 122
c801d85f 123 PostCreation();
a3622daa 124
c801d85f 125 Show( TRUE );
a3622daa 126
c801d85f 127 return TRUE;
fc008f25 128}
c801d85f 129
716b7364 130bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
c801d85f 131{
cf4219e7 132 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
c801d85f 133 event.SetEventObject(this);
46dc76ba 134 event.SetInt( toolIndex );
c801d85f
KB
135 event.SetExtraLong((long) toggleDown);
136
137 GetEventHandler()->ProcessEvent(event);
138
139 return TRUE;
fc008f25 140}
c801d85f 141
716b7364 142void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
c801d85f 143{
cf4219e7 144 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
46dc76ba
RR
145 event.SetEventObject( this );
146 event.SetInt( toolIndex );
c801d85f
KB
147
148 GetEventHandler()->ProcessEvent(event);
fc008f25 149}
c801d85f 150
716b7364 151void wxToolBar::OnMouseEnter( int toolIndex )
c801d85f 152{
314055fa 153 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
c801d85f 154 event.SetEventObject(this);
46dc76ba 155 event.SetInt( toolIndex );
314055fa 156
c801d85f 157 GetEventHandler()->ProcessEvent(event);
fc008f25 158}
c801d85f 159
a3622daa 160wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
debe6624
JS
161 const wxBitmap& pushedBitmap, bool toggle,
162 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
c801d85f
KB
163 const wxString& helpString1, const wxString& helpString2 )
164{
c67daf87 165 if (!bitmap.Ok()) return (wxToolBarTool *) NULL;
a3622daa
VZ
166
167 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap, toggle,
fc008f25 168 clientData, helpString1, helpString2 );
a3622daa 169
c67daf87 170 GtkWidget *tool_pixmap = (GtkWidget *) NULL;
903f689b
RR
171
172 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL, "wxToolBar doesn't support GdkBitmap" )
173
174 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL, "wxToolBar::Add needs a wxBitmap" )
175
176 if (TRUE)
177 {
178 GdkPixmap *pixmap = bitmap.GetPixmap();
a3622daa 179
c67daf87 180 GdkBitmap *mask = (GdkBitmap *) NULL;
903f689b
RR
181 if (bitmap.GetMask()) mask = bitmap.GetMask()->GetBitmap();
182
183 tool_pixmap = gtk_pixmap_new( pixmap, mask );
184 }
185
c801d85f 186 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
a3622daa 187
c801d85f
KB
188 GtkToolbarChildType ctype = GTK_TOOLBAR_CHILD_BUTTON;
189 if (toggle) ctype = GTK_TOOLBAR_CHILD_TOGGLEBUTTON;
a3622daa 190
83058c58
RR
191 tool->m_item = gtk_toolbar_append_element(
192 m_toolbar, ctype, (GtkWidget *) NULL, (const char *) NULL, helpString1, "",
193 tool_pixmap, (GtkSignalFunc)gtk_toolbar_callback, (gpointer)tool );
a3622daa 194
314055fa
RR
195 gtk_signal_connect( GTK_OBJECT(tool->m_item), "enter_notify_event",
196 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback), (gpointer)tool );
197
a3622daa
VZ
198 m_tools.Append( tool );
199
c801d85f 200 return tool;
fc008f25 201}
c801d85f 202
716b7364 203void wxToolBar::AddSeparator(void)
c801d85f
KB
204{
205 gtk_toolbar_append_space( m_toolbar );
fc008f25 206}
c801d85f 207
716b7364 208void wxToolBar::ClearTools(void)
c801d85f 209{
fc008f25
RR
210 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
211}
c801d85f 212
e3e65dac 213void wxToolBar::Realize(void)
46dc76ba
RR
214{
215 m_x = 0;
216 m_y = 0;
217 m_width = 100;
218 m_height = 0;
219
220 wxNode *node = m_tools.First();
221 while (node)
222 {
223 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
224 if (tool->m_bitmap1.Ok())
225 {
226 int tool_height = tool->m_bitmap1.GetHeight();
227 if (tool_height > m_height) m_height = tool_height;
fc008f25 228 }
46dc76ba
RR
229
230 node = node->Next();
fc008f25 231 }
46dc76ba
RR
232
233 m_height += 10;
fc008f25 234}
46dc76ba 235
716b7364 236void wxToolBar::EnableTool(int toolIndex, bool enable)
c801d85f 237{
cf4219e7
RR
238 wxNode *node = m_tools.First();
239 while (node)
240 {
241 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
242 if (tool->m_index == toolIndex)
243 {
244 tool->m_enabled = enable;
245 return;
246 }
247 node = node->Next();
fc008f25
RR
248 }
249
250 wxFAIL_MSG( "wrong toolbar index" );
251}
c801d85f 252
fc008f25 253void wxToolBar::ToggleTool( int toolIndex, bool toggle )
c801d85f 254{
fc008f25
RR
255 wxNode *node = m_tools.First();
256 while (node)
257 {
258 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
259 if (tool->m_index == toolIndex)
260 {
261 tool->m_toggleState = toggle;
262 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
263 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
264 return;
265 }
266 node = node->Next();
267 }
268
269 wxFAIL_MSG( "wrong toolbar index" );
270}
c801d85f 271
fc008f25 272wxObject *wxToolBar::GetToolClientData( int index ) const
c801d85f 273{
cf4219e7
RR
274 wxNode *node = m_tools.First();
275 while (node)
276 {
277 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
278 if (tool->m_index == index) return tool->m_clientData;;
279 node = node->Next();
fc008f25
RR
280 }
281
282 wxFAIL_MSG( "wrong toolbar index" );
283
cf4219e7 284 return (wxObject*)NULL;
fc008f25 285}
c801d85f 286
716b7364 287bool wxToolBar::GetToolState(int toolIndex) const
c801d85f 288{
cf4219e7
RR
289 wxNode *node = m_tools.First();
290 while (node)
291 {
292 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
293 if (tool->m_index == toolIndex) return tool->m_toggleState;
294 node = node->Next();
fc008f25
RR
295 }
296
297 wxFAIL_MSG( "wrong toolbar index" );
298
cf4219e7 299 return FALSE;
fc008f25 300}
c801d85f 301
716b7364 302bool wxToolBar::GetToolEnabled(int toolIndex) const
c801d85f 303{
cf4219e7
RR
304 wxNode *node = m_tools.First();
305 while (node)
306 {
307 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
308 if (tool->m_index == toolIndex) return tool->m_enabled;
309 node = node->Next();
fc008f25
RR
310 }
311
312 wxFAIL_MSG( "wrong toolbar index" );
313
cf4219e7 314 return FALSE;
fc008f25 315}
c801d85f 316
cf4219e7 317void wxToolBar::SetMargins( int WXUNUSED(x), int WXUNUSED(y) )
c801d85f 318{
fc008f25 319}
c801d85f 320
cf4219e7 321void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
c801d85f 322{
fc008f25 323}
c801d85f 324
cf4219e7 325void wxToolBar::SetToolSeparation( int separation )
c801d85f 326{
cf4219e7 327 gtk_toolbar_set_space_size( m_toolbar, separation );
fc008f25 328}
c801d85f 329