]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/tbargtk.cpp
Added (untested) support for sub-locales.
[wxWidgets.git] / src / gtk1 / tbargtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: tbargtk.cpp
3// Purpose: GTK toolbar
4// Author: Robert Roebling
32e9da8b 5// RCS-ID: $Id$
c801d85f 6// Copyright: (c) Robert Roebling
a3622daa 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "tbargtk.h"
12#endif
13
14#include "wx/toolbar.h"
15
83624f79
RR
16#include "glib.h"
17#include "gdk/gdk.h"
18#include "gtk/gtk.h"
19
314055fa
RR
20//-----------------------------------------------------------------------------
21// data
22//-----------------------------------------------------------------------------
23
24extern bool g_blockEventsOnDrag;
25
c801d85f
KB
26//-----------------------------------------------------------------------------
27// wxToolBarTool
28//-----------------------------------------------------------------------------
29
30IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool,wxObject)
a3622daa
VZ
31
32wxToolBarTool::wxToolBarTool( wxToolBar *owner, int theIndex,
03f38c58
VZ
33 const wxBitmap& bitmap1, const wxBitmap& bitmap2,
34 bool toggle,
35 wxObject *clientData,
36 const wxString& shortHelpString,
37 const wxString& longHelpString,
38 GtkWidget *item )
c801d85f 39{
1144d24d
RR
40 m_owner = owner;
41 m_index = theIndex;
42 m_bitmap1 = bitmap1;
43 m_bitmap2 = bitmap2;
44 m_isToggle = toggle;
45 m_enabled = TRUE;
46 m_toggleState = FALSE;
47 m_shortHelpString = shortHelpString;
48 m_longHelpString = longHelpString;
49 m_isMenuCommand = TRUE;
50 m_clientData = clientData;
51 m_deleteSecondBitmap = FALSE;
52 m_item = item;
fc008f25 53}
c801d85f 54
a3622daa 55wxToolBarTool::~wxToolBarTool()
c801d85f 56{
fc008f25 57}
c801d85f
KB
58
59//-----------------------------------------------------------------------------
2f2aa628 60// "clicked" (internal from gtk_toolbar)
c801d85f
KB
61//-----------------------------------------------------------------------------
62
63static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
64{
1144d24d
RR
65 if (g_blockEventsOnDrag) return;
66 if (!tool->m_enabled) return;
a3622daa 67
1144d24d 68 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
a3622daa 69
1144d24d 70 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
fc008f25 71}
c801d85f 72
2f2aa628
RR
73//-----------------------------------------------------------------------------
74// "enter_notify_event"
75//-----------------------------------------------------------------------------
76
314055fa
RR
77static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
78 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
79{
1144d24d 80 if (g_blockEventsOnDrag) return TRUE;
314055fa 81
1144d24d 82 tool->m_owner->OnMouseEnter( tool->m_index );
314055fa 83
1144d24d 84 return FALSE;
314055fa
RR
85}
86
2f2aa628
RR
87//-----------------------------------------------------------------------------
88// wxToolBar
c801d85f
KB
89//-----------------------------------------------------------------------------
90
716b7364 91IMPLEMENT_DYNAMIC_CLASS(wxToolBar,wxControl)
c801d85f 92
a3622daa 93wxToolBar::wxToolBar()
c801d85f 94{
fc008f25 95}
c801d85f 96
a3622daa 97wxToolBar::wxToolBar( wxWindow *parent, wxWindowID id,
c801d85f 98 const wxPoint& pos, const wxSize& size,
debe6624 99 long style, const wxString& name )
c801d85f 100{
1144d24d 101 Create( parent, id, pos, size, style, name );
fc008f25 102}
c801d85f 103
a3622daa 104wxToolBar::~wxToolBar()
c801d85f 105{
83624f79
RR
106 delete m_fg;
107 delete m_bg;
fc008f25 108}
c801d85f 109
a3622daa 110bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
c801d85f 111 const wxPoint& pos, const wxSize& size,
debe6624 112 long style, const wxString& name )
c801d85f 113{
1144d24d 114 m_needParent = TRUE;
a3622daa 115
1144d24d 116 PreCreation( parent, id, pos, size, style, name );
c801d85f 117
1144d24d 118 m_tools.DeleteContents( TRUE );
a3622daa 119
1144d24d
RR
120 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL,
121 GTK_TOOLBAR_ICONS ) );
a3622daa 122
1144d24d
RR
123 m_separation = 5;
124 gtk_toolbar_set_space_size( m_toolbar, m_separation );
125 m_hasToolAlready = FALSE;
126
127 m_widget = GTK_WIDGET(m_toolbar);
32e9da8b 128
1144d24d 129 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
83624f79
RR
130
131 m_fg = new GdkColor;
132 m_fg->red = 0;
133 m_fg->green = 0;
134 m_fg->blue = 0;
135 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_fg );
b46e8696 136
83624f79
RR
137 m_bg = new GdkColor;
138 m_bg->red = 65535;
139 m_bg->green = 65535;
140 m_bg->blue = 50000;
141 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_bg );
b46e8696 142
83624f79 143 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, m_bg, m_fg );
a3622daa 144
1144d24d
RR
145 m_xMargin = 0;
146 m_yMargin = 0;
147
148 m_parent->AddChild( this );
6ca41e57 149
1144d24d 150 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 151
1144d24d 152 PostCreation();
a3622daa 153
1144d24d 154 Show( TRUE );
a3622daa 155
1144d24d 156 return TRUE;
fc008f25 157}
c801d85f 158
716b7364 159bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
c801d85f 160{
1144d24d
RR
161 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
162 event.SetEventObject(this);
163 event.SetInt( toolIndex );
164 event.SetExtraLong((long) toggleDown);
c801d85f 165
1144d24d 166 GetEventHandler()->ProcessEvent(event);
c801d85f 167
1144d24d 168 return TRUE;
fc008f25 169}
c801d85f 170
716b7364 171void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
c801d85f 172{
1144d24d
RR
173 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
174 event.SetEventObject( this );
175 event.SetInt( toolIndex );
c801d85f 176
1144d24d 177 GetEventHandler()->ProcessEvent(event);
fc008f25 178}
c801d85f 179
716b7364 180void wxToolBar::OnMouseEnter( int toolIndex )
c801d85f 181{
1144d24d
RR
182 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
183 event.SetEventObject(this);
184 event.SetInt( toolIndex );
314055fa 185
1144d24d 186 GetEventHandler()->ProcessEvent(event);
fc008f25 187}
c801d85f 188
a3622daa 189wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
debe6624
JS
190 const wxBitmap& pushedBitmap, bool toggle,
191 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
c801d85f
KB
192 const wxString& helpString1, const wxString& helpString2 )
193{
1144d24d
RR
194 m_hasToolAlready = TRUE;
195
196 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
197 "invalid bitmap for wxToolBar icon" );
a3622daa 198
1144d24d
RR
199 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
200 "wxToolBar doesn't support GdkBitmap" );
03f38c58 201
1144d24d
RR
202 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
203 "wxToolBar::Add needs a wxBitmap" );
903f689b 204
1144d24d 205 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
903f689b 206
903f689b 207 GdkPixmap *pixmap = bitmap.GetPixmap();
a3622daa 208
68dda785
VZ
209 GdkBitmap *mask = (GdkBitmap *)NULL;
210 if ( bitmap.GetMask() )
211 mask = bitmap.GetMask()->GetBitmap();
903f689b
RR
212
213 tool_pixmap = gtk_pixmap_new( pixmap, mask );
903f689b 214
1144d24d 215 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
a3622daa 216
2b1c162e
RR
217 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
218 toggle, clientData,
219 helpString1, helpString2,
220 tool_pixmap );
221
1144d24d
RR
222 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
223 : GTK_TOOLBAR_CHILD_BUTTON;
03f38c58 224
1144d24d 225 GtkWidget *item = gtk_toolbar_append_element
68dda785
VZ
226 (
227 GTK_TOOLBAR(m_toolbar),
228 ctype,
229 (GtkWidget *)NULL,
230 (const char *)NULL,
231 helpString1,
232 "",
233 tool_pixmap,
234 (GtkSignalFunc)gtk_toolbar_callback,
235 (gpointer)tool
236 );
237
1144d24d 238 tool->m_item = item;
03f38c58 239
1144d24d
RR
240 gtk_signal_connect( GTK_OBJECT(tool->m_item),
241 "enter_notify_event",
242 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
243 (gpointer)tool );
314055fa 244
1144d24d 245 m_tools.Append( tool );
a3622daa 246
1144d24d 247 return tool;
fc008f25 248}
c801d85f 249
03f38c58 250void wxToolBar::AddSeparator()
c801d85f 251{
1144d24d 252 gtk_toolbar_append_space( m_toolbar );
fc008f25 253}
c801d85f 254
03f38c58 255void wxToolBar::ClearTools()
c801d85f 256{
1144d24d 257 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
fc008f25 258}
c801d85f 259
1144d24d 260bool wxToolBar::Realize()
46dc76ba 261{
1144d24d
RR
262 m_x = 0;
263 m_y = 0;
264 m_width = 100;
265 m_height = 0;
46dc76ba 266
1144d24d
RR
267 wxNode *node = m_tools.First();
268 while (node)
46dc76ba 269 {
1144d24d
RR
270 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
271 if (tool->m_bitmap1.Ok())
272 {
273 int tool_height = tool->m_bitmap1.GetHeight();
274 if (tool_height > m_height) m_height = tool_height;
275 }
46dc76ba 276
1144d24d
RR
277 node = node->Next();
278 }
46dc76ba 279
1144d24d
RR
280 m_height += 5 + 2*m_yMargin;
281
282 return TRUE;
fc008f25 283}
46dc76ba 284
716b7364 285void wxToolBar::EnableTool(int toolIndex, bool enable)
c801d85f 286{
1144d24d
RR
287 wxNode *node = m_tools.First();
288 while (node)
289 {
290 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
291 if (tool->m_index == toolIndex)
2b1c162e 292 {
1144d24d 293 tool->m_enabled = enable;
2b1c162e
RR
294
295 if (tool->m_item)
296 gtk_widget_set_sensitive( tool->m_item, enable );
297
1144d24d
RR
298 return;
299 }
300 node = node->Next();
cf4219e7 301 }
fc008f25 302
1144d24d 303 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 304}
c801d85f 305
fc008f25 306void wxToolBar::ToggleTool( int toolIndex, bool toggle )
c801d85f 307{
1144d24d
RR
308 wxNode *node = m_tools.First();
309 while (node)
310 {
311 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
312 if (tool->m_index == toolIndex)
313 {
314 tool->m_toggleState = toggle;
315 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
316 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
317 return;
318 }
319 node = node->Next();
fc008f25 320 }
fc008f25 321
1144d24d 322 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 323}
c801d85f 324
fc008f25 325wxObject *wxToolBar::GetToolClientData( int index ) const
c801d85f 326{
1144d24d
RR
327 wxNode *node = m_tools.First();
328 while (node)
329 {
330 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
331 if (tool->m_index == index) return tool->m_clientData;;
332 node = node->Next();
333 }
fc008f25 334
1144d24d 335 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 336
1144d24d 337 return (wxObject*)NULL;
fc008f25 338}
c801d85f 339
716b7364 340bool wxToolBar::GetToolState(int toolIndex) const
c801d85f 341{
1144d24d
RR
342 wxNode *node = m_tools.First();
343 while (node)
344 {
345 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
346 if (tool->m_index == toolIndex) return tool->m_toggleState;
347 node = node->Next();
348 }
fc008f25 349
1144d24d 350 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 351
1144d24d 352 return FALSE;
fc008f25 353}
c801d85f 354
716b7364 355bool wxToolBar::GetToolEnabled(int toolIndex) const
c801d85f 356{
1144d24d
RR
357 wxNode *node = m_tools.First();
358 while (node)
359 {
360 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
361 if (tool->m_index == toolIndex) return tool->m_enabled;
362 node = node->Next();
363 }
fc008f25 364
1144d24d 365 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 366
1144d24d 367 return FALSE;
fc008f25 368}
c801d85f 369
1144d24d 370void wxToolBar::SetMargins( int x, int y )
c801d85f 371{
1144d24d
RR
372 wxCHECK_RET( !m_hasToolAlready, "wxToolBar::SetMargins must be called before adding tool." );
373
374 if (x > 2) gtk_toolbar_append_space( m_toolbar ); // oh well
375
376 m_xMargin = x;
377 m_yMargin = y;
fc008f25 378}
c801d85f 379
cf4219e7 380void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
c801d85f 381{
1144d24d 382 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
fc008f25 383}
c801d85f 384
cf4219e7 385void wxToolBar::SetToolSeparation( int separation )
c801d85f 386{
1144d24d
RR
387 gtk_toolbar_set_space_size( m_toolbar, separation );
388 m_separation = separation;
389}
390
391int wxToolBar::GetToolPacking()
392{
393 return 0;
394}
395
396int wxToolBar::GetToolSeparation()
397{
398 return m_separation;
399}
400
401wxString wxToolBar::GetToolLongHelp(int toolIndex)
402{
403 wxNode *node = m_tools.First();
404 while (node)
405 {
406 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
407 if (tool->m_index == toolIndex)
408 {
409 return tool->m_longHelpString;
410 }
411 node = node->Next();
412 }
413
414 wxFAIL_MSG( "wrong toolbar index" );
415
416 return "";
417}
418
419wxString wxToolBar::GetToolShortHelp(int toolIndex)
420{
421 wxNode *node = m_tools.First();
422 while (node)
423 {
424 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
425 if (tool->m_index == toolIndex)
426 {
427 return tool->m_shortHelpString;
428 }
429 node = node->Next();
430 }
431
432 wxFAIL_MSG( "wrong toolbar index" );
433
434 return "";
fc008f25 435}
c801d85f 436
1144d24d
RR
437void wxToolBar::SetToolLongHelp(int toolIndex, const wxString& helpString)
438{
439 wxNode *node = m_tools.First();
440 while (node)
441 {
442 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
443 if (tool->m_index == toolIndex)
444 {
445 tool->m_longHelpString = helpString;
446 return;
447 }
448 node = node->Next();
449 }
450
451 wxFAIL_MSG( "wrong toolbar index" );
452
453 return;
454}
455
456void wxToolBar::SetToolShortHelp(int toolIndex, const wxString& helpString)
457{
458 wxNode *node = m_tools.First();
459 while (node)
460 {
461 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
462 if (tool->m_index == toolIndex)
463 {
464 tool->m_shortHelpString = helpString;
465 return;
466 }
467 node = node->Next();
468 }
469
470 wxFAIL_MSG( "wrong toolbar index" );
471
472 return;
473}
474
475