1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbargtk.h"
16 #include "wx/toolbar.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
,wxObject
)
31 wxToolBarTool::wxToolBarTool( wxToolBar
*owner
, int theIndex
,
32 const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
,
33 bool toggle
, wxObject
*clientData
,
34 const wxString
& shortHelpString
, const wxString
& longHelpString
,
43 m_toggleState
= FALSE
;
44 m_shortHelpString
= shortHelpString
;
45 m_longHelpString
= longHelpString
;
46 m_isMenuCommand
= TRUE
;
47 m_clientData
= clientData
;
48 m_deleteSecondBitmap
= FALSE
;
52 wxToolBarTool::~wxToolBarTool()
56 //-----------------------------------------------------------------------------
57 // "clicked" (internal from gtk_toolbar)
58 //-----------------------------------------------------------------------------
60 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
62 if (g_blockEventsOnDrag
) return;
63 if (!tool
->m_enabled
) return;
65 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
67 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
70 //-----------------------------------------------------------------------------
71 // "enter_notify_event"
72 //-----------------------------------------------------------------------------
74 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
75 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
77 if (g_blockEventsOnDrag
) return TRUE
;
79 tool
->m_owner
->OnMouseEnter( tool
->m_index
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
90 wxToolBar::wxToolBar()
94 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 long style
, const wxString
& name
)
98 Create( parent
, id
, pos
, size
, style
, name
);
101 wxToolBar::~wxToolBar()
105 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
106 const wxPoint
& pos
, const wxSize
& size
,
107 long style
, const wxString
& name
)
111 PreCreation( parent
, id
, pos
, size
, style
, name
);
113 m_tools
.DeleteContents( TRUE
);
115 m_widget
= gtk_handle_box_new();
117 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
, GTK_TOOLBAR_ICONS
) );
119 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
121 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
130 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
132 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
133 event
.SetEventObject(this);
134 event
.SetInt( toolIndex
);
135 event
.SetExtraLong((long) toggleDown
);
137 GetEventHandler()->ProcessEvent(event
);
142 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
144 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
145 event
.SetEventObject( this );
146 event
.SetInt( toolIndex
);
148 GetEventHandler()->ProcessEvent(event
);
151 void wxToolBar::OnMouseEnter( int toolIndex
)
153 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
154 event
.SetEventObject(this);
155 event
.SetInt( toolIndex
);
157 GetEventHandler()->ProcessEvent(event
);
160 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
161 const wxBitmap
& pushedBitmap
, bool toggle
,
162 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
163 const wxString
& helpString1
, const wxString
& helpString2
)
165 if (!bitmap
.Ok()) return (wxToolBarTool
*) NULL
;
167 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
, toggle
,
168 clientData
, helpString1
, helpString2
);
170 GtkWidget
*tool_pixmap
= (GtkWidget
*) NULL
;
172 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
, "wxToolBar doesn't support GdkBitmap" )
174 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
, "wxToolBar::Add needs a wxBitmap" )
178 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
180 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
181 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
183 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
186 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
188 GtkToolbarChildType ctype
= GTK_TOOLBAR_CHILD_BUTTON
;
189 if (toggle
) ctype
= GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
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
);
195 gtk_signal_connect( GTK_OBJECT(tool
->m_item
), "enter_notify_event",
196 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
), (gpointer
)tool
);
198 m_tools
.Append( tool
);
203 void wxToolBar::AddSeparator(void)
205 gtk_toolbar_append_space( m_toolbar
);
208 void wxToolBar::ClearTools(void)
210 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
213 void wxToolBar::Realize(void)
220 wxNode
*node
= m_tools
.First();
223 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
224 if (tool
->m_bitmap1
.Ok())
226 int tool_height
= tool
->m_bitmap1
.GetHeight();
227 if (tool_height
> m_height
) m_height
= tool_height
;
236 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
238 wxNode
*node
= m_tools
.First();
241 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
242 if (tool
->m_index
== toolIndex
)
244 tool
->m_enabled
= enable
;
250 wxFAIL_MSG( "wrong toolbar index" );
253 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
255 wxNode
*node
= m_tools
.First();
258 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
259 if (tool
->m_index
== toolIndex
)
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
);
269 wxFAIL_MSG( "wrong toolbar index" );
272 wxObject
*wxToolBar::GetToolClientData( int index
) const
274 wxNode
*node
= m_tools
.First();
277 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
278 if (tool
->m_index
== index
) return tool
->m_clientData
;;
282 wxFAIL_MSG( "wrong toolbar index" );
284 return (wxObject
*)NULL
;
287 bool wxToolBar::GetToolState(int toolIndex
) const
289 wxNode
*node
= m_tools
.First();
292 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
293 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
297 wxFAIL_MSG( "wrong toolbar index" );
302 bool wxToolBar::GetToolEnabled(int toolIndex
) const
304 wxNode
*node
= m_tools
.First();
307 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
308 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
312 wxFAIL_MSG( "wrong toolbar index" );
317 void wxToolBar::SetMargins( int WXUNUSED(x
), int WXUNUSED(y
) )
321 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
325 void wxToolBar::SetToolSeparation( int separation
)
327 gtk_toolbar_set_space_size( m_toolbar
, separation
);