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