1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     GTK toolbar 
   4 // Author:      Robert Roebling 
   5 // Modified:    13.12.99 by VZ to derive from wxToolBarBase 
   7 // Copyright:   (c) Robert Roebling 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  11 // ============================================================================ 
  13 // ============================================================================ 
  15 // ---------------------------------------------------------------------------- 
  17 // ---------------------------------------------------------------------------- 
  20     #pragma implementation "tbargtk.h" 
  23 #include "wx/toolbar.h" 
  25 #if wxUSE_TOOLBAR_NATIVE 
  33 extern GdkFont 
*GtkGetDefaultGuiFont(); 
  35 // ---------------------------------------------------------------------------- 
  37 // ---------------------------------------------------------------------------- 
  40 extern void wxapp_install_idle_handler(); 
  44 extern bool       g_blockEventsOnDrag
; 
  45 extern wxCursor   g_globalCursor
; 
  47 // ---------------------------------------------------------------------------- 
  49 // ---------------------------------------------------------------------------- 
  51 class wxToolBarTool 
: public wxToolBarToolBase
 
  54     wxToolBarTool(wxToolBar 
*tbar
, 
  56                   const wxBitmap
& bitmap1
, 
  57                   const wxBitmap
& bitmap2
, 
  60                   const wxString
& shortHelpString
, 
  61                   const wxString
& longHelpString
) 
  62         : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
, 
  63                             clientData
, shortHelpString
, longHelpString
) 
  68     wxToolBarTool(wxToolBar 
*tbar
, wxControl 
*control
) 
  69         : wxToolBarToolBase(tbar
, control
) 
  81 // ---------------------------------------------------------------------------- 
  83 // ---------------------------------------------------------------------------- 
  85 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
) 
  87 // ============================================================================ 
  89 // ============================================================================ 
  91 //----------------------------------------------------------------------------- 
  92 // "clicked" (internal from gtk_toolbar) 
  93 //----------------------------------------------------------------------------- 
  95 static void gtk_toolbar_callback( GtkWidget 
*WXUNUSED(widget
), 
  99         wxapp_install_idle_handler(); 
 101     wxToolBar 
*tbar 
= (wxToolBar 
*)tool
->GetToolBar(); 
 102     if ( tbar
->m_blockNextEvent 
) 
 104         tbar
->m_blockNextEvent 
= FALSE
; 
 108     if (g_blockEventsOnDrag
) return; 
 109     if (!tool
->IsEnabled()) return; 
 111     if (tool
->CanBeToggled()) 
 115         wxBitmap bitmap 
= tool
->GetBitmap(); 
 118             GtkPixmap 
*pixmap 
= GTK_PIXMAP( tool
->m_pixmap 
); 
 120             GdkBitmap 
*mask 
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap() 
 123             gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask 
); 
 127     tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ); 
 130 //----------------------------------------------------------------------------- 
 131 // "enter_notify_event" / "leave_notify_event" 
 132 //----------------------------------------------------------------------------- 
 134 static gint 
gtk_toolbar_tool_callback( GtkWidget 
*WXUNUSED(widget
),  
 135                                        GdkEventCrossing 
*gdk_event
, 
 136                                        wxToolBarTool 
*tool 
) 
 138     if (g_isIdle
) wxapp_install_idle_handler(); 
 140     if (g_blockEventsOnDrag
) return TRUE
; 
 142     wxToolBar 
*tb 
= (wxToolBar 
*)tool
->GetToolBar(); 
 145     if( gdk_event
->type 
== GDK_ENTER_NOTIFY 
) 
 146         tb
->OnMouseEnter( tool
->GetId() ); 
 148         tb
->OnMouseEnter( -1 ); 
 153 //----------------------------------------------------------------------------- 
 154 // InsertChild callback for wxToolBar 
 155 //----------------------------------------------------------------------------- 
 157 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
), 
 158                                     wxWindow
* WXUNUSED(child
) ) 
 160     // we don't do anything here 
 163 // ---------------------------------------------------------------------------- 
 165 // ---------------------------------------------------------------------------- 
 167 void wxToolBarTool::Init() 
 170     m_pixmap 
= (GtkWidget 
*)NULL
; 
 173 wxToolBarToolBase 
*wxToolBar::CreateTool(int id
, 
 174                                          const wxBitmap
& bitmap1
, 
 175                                          const wxBitmap
& bitmap2
, 
 177                                          wxObject 
*clientData
, 
 178                                          const wxString
& shortHelpString
, 
 179                                          const wxString
& longHelpString
) 
 181     return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
, 
 182                              clientData
, shortHelpString
, longHelpString
); 
 185 wxToolBarToolBase 
*wxToolBar::CreateTool(wxControl 
*control
) 
 187     return new wxToolBarTool(this, control
); 
 190 //----------------------------------------------------------------------------- 
 191 // wxToolBar construction 
 192 //----------------------------------------------------------------------------- 
 194 void wxToolBar::Init() 
 197     m_bg 
= (GdkColor 
*)NULL
; 
 198     m_toolbar 
= (GtkToolbar 
*)NULL
; 
 199     m_blockNextEvent 
= FALSE
; 
 202 wxToolBar::~wxToolBar() 
 208 bool wxToolBar::Create( wxWindow 
*parent
, 
 213                         const wxString
& name 
) 
 216     m_insertCallback 
= (wxInsertChildFunction
)wxInsertChildInToolBar
; 
 218     if ( !PreCreation( parent
, pos
, size 
) || 
 219          !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name 
)) 
 221         wxFAIL_MSG( wxT("wxToolBar creation failed") ); 
 226     GtkOrientation orient 
= style 
& wxTB_VERTICAL 
? GTK_ORIENTATION_VERTICAL
 
 227                                                   : GTK_ORIENTATION_HORIZONTAL
; 
 228     m_toolbar 
= GTK_TOOLBAR( gtk_toolbar_new( orient
, GTK_TOOLBAR_ICONS 
) ); 
 230     SetToolSeparation(7); 
 232     if (style 
& wxTB_DOCKABLE
) 
 234         m_widget 
= gtk_handle_box_new(); 
 235         gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) ); 
 236         gtk_widget_show( GTK_WIDGET(m_toolbar
) ); 
 238         if (style 
& wxTB_FLAT
) 
 239             gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE 
); 
 243         m_widget 
= GTK_WIDGET(m_toolbar
); 
 246     gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE 
); 
 248     if (style 
& wxTB_FLAT
) 
 249         gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE 
); 
 257     fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) ); 
 258     m_fg
->pixel 
= fg
.GetPixel(); 
 264     wxColour 
bg(255,255,196); 
 265     bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) ); 
 266     m_bg
->pixel 
= bg
.GetPixel(); 
 268     gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips 
); 
 272                 gtk_widget_get_style(  
 273                     GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window 
) ); 
 275     g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
; 
 276     gdk_font_unref( g_style
->font 
); 
 277         g_style
->font 
= gdk_font_ref( GtkGetDefaultGuiFont() ); 
 278     gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style 
); 
 280     m_parent
->DoAddChild( this ); 
 289 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase 
*toolBase
) 
 291     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 293     // we have inserted a space before all the tools 
 294     if (m_xMargin 
> 1) pos
++; 
 296     if ( tool
->IsButton() ) 
 298         wxBitmap bitmap 
= tool
->GetBitmap1(); 
 300         wxCHECK_MSG( bitmap
.Ok(), FALSE
, 
 301                      wxT("invalid bitmap for wxToolBar icon") ); 
 303         wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
, 
 304                      wxT("wxToolBar doesn't support GdkBitmap") ); 
 306         wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
, 
 307                      wxT("wxToolBar::Add needs a wxBitmap") ); 
 309         GtkWidget 
*tool_pixmap 
= (GtkWidget 
*)NULL
; 
 311         GdkPixmap 
*pixmap 
= bitmap
.GetPixmap(); 
 313         GdkBitmap 
*mask 
= (GdkBitmap 
*)NULL
; 
 314         if ( bitmap
.GetMask() ) 
 315           mask 
= bitmap
.GetMask()->GetBitmap(); 
 317         tool_pixmap 
= gtk_pixmap_new( pixmap
, mask 
); 
 318 #if (GTK_MINOR_VERSION > 0) 
 319         gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE 
); 
 322         gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 ); 
 324         tool
->m_pixmap 
= tool_pixmap
; 
 327     switch ( tool
->GetStyle() ) 
 329         case wxTOOL_STYLE_BUTTON
: 
 330             tool
->m_item 
= gtk_toolbar_insert_element
 
 334                                 ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
 
 335                                 : GTK_TOOLBAR_CHILD_BUTTON
, 
 338                               tool
->GetShortHelp().mbc_str(), 
 339                               "", // tooltip_private_text (?) 
 341                               (GtkSignalFunc
)gtk_toolbar_callback
, 
 348                 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") ); 
 353             gtk_signal_connect( GTK_OBJECT(tool
->m_item
), 
 354                                 "enter_notify_event",  
 355                                 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
), 
 357             gtk_signal_connect( GTK_OBJECT(tool
->m_item
), 
 358                                 "leave_notify_event",  
 359                                 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
), 
 363         case wxTOOL_STYLE_SEPARATOR
: 
 364             gtk_toolbar_insert_space( m_toolbar
, pos 
); 
 369         case wxTOOL_STYLE_CONTROL
: 
 370             gtk_toolbar_insert_widget( 
 372                                        tool
->GetControl()->m_widget
, 
 381     (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request 
) 
 383     m_width 
= req
.width 
+ m_xMargin
; 
 384     m_height 
= req
.height 
+ 2*m_yMargin
; 
 389 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase 
*toolBase
) 
 391     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 393     switch ( tool
->GetStyle() ) 
 395         case wxTOOL_STYLE_CONTROL
: 
 396             tool
->GetControl()->Destroy(); 
 399         case wxTOOL_STYLE_BUTTON
: 
 400             gtk_widget_destroy( tool
->m_item 
); 
 403         //case wxTOOL_STYLE_SEPARATOR: -- nothing to do 
 409 // ---------------------------------------------------------------------------- 
 410 // wxToolBar tools state 
 411 // ---------------------------------------------------------------------------- 
 413 void wxToolBar::DoEnableTool(wxToolBarToolBase 
*toolBase
, bool enable
) 
 415 #if (GTK_MINOR_VERSION > 0) 
 416     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 418     /* we don't disable the tools for GTK 1.0 as the bitmaps don't get 
 419        greyed anyway and this also disables tooltips */ 
 421         gtk_widget_set_sensitive( tool
->m_item
, enable 
); 
 425 void wxToolBar::DoToggleTool( wxToolBarToolBase 
*toolBase
, bool toggle 
)  
 427     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 429     GtkWidget 
*item 
= tool
->m_item
; 
 430     if ( item 
&& GTK_IS_TOGGLE_BUTTON(item
) ) 
 432         wxBitmap bitmap 
= tool
->GetBitmap(); 
 435             GtkPixmap 
*pixmap 
= GTK_PIXMAP( tool
->m_pixmap 
); 
 437             GdkBitmap 
*mask 
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap() 
 440             gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask 
); 
 443         m_blockNextEvent 
= TRUE
;  // we cannot use gtk_signal_disconnect here 
 445         gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle 
); 
 449 void wxToolBar::DoSetToggle(wxToolBarToolBase 
* WXUNUSED(tool
), 
 450                             bool WXUNUSED(toggle
)) 
 452     // VZ: absolutely no idea about how to do it 
 453     wxFAIL_MSG( _T("not implemented") ); 
 456 // ---------------------------------------------------------------------------- 
 457 // wxToolBar geometry 
 458 // ---------------------------------------------------------------------------- 
 460 wxToolBarToolBase 
*wxToolBar::FindToolForPosition(wxCoord 
WXUNUSED(x
), 
 461                                                   wxCoord 
WXUNUSED(y
)) const 
 463     // VZ: GTK+ doesn't seem to have such thing 
 464     wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") ); 
 466     return (wxToolBarToolBase 
*)NULL
; 
 469 void wxToolBar::SetMargins( int x
, int y 
) 
 471     wxCHECK_RET( GetToolsCount() == 0, 
 472                  wxT("wxToolBar::SetMargins must be called before adding tools.") ); 
 474     if (x 
> 1) gtk_toolbar_append_space( m_toolbar 
);  // oh well 
 480 void wxToolBar::SetToolSeparation( int separation 
) 
 482     gtk_toolbar_set_space_size( m_toolbar
, separation 
); 
 483     m_toolSeparation 
= separation
; 
 486 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString 
) 
 488     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)FindById(id
); 
 492         (void)tool
->SetShortHelp(helpString
); 
 493         gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
, 
 494                              helpString
.mbc_str(), ""); 
 498 // ---------------------------------------------------------------------------- 
 499 // wxToolBar idle handling 
 500 // ---------------------------------------------------------------------------- 
 502 void wxToolBar::OnInternalIdle() 
 504     wxCursor cursor 
= m_cursor
; 
 505     if (g_globalCursor
.Ok()) cursor 
= g_globalCursor
; 
 509         /* I now set the cursor the anew in every OnInternalIdle call 
 510            as setting the cursor in a parent window also effects the 
 511            windows above so that checking for the current cursor is 
 514         if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
)) 
 516             /* if the toolbar is dockable, then m_widget stands for the 
 517                GtkHandleBox widget, which uses its own window so that we 
 518                can set the cursor for it. if the toolbar is not dockable, 
 519                m_widget comes from m_toolbar which uses its parent's 
 520                window ("windowless windows") and thus we cannot set the 
 522             gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() ); 
 525         wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 528             wxToolBarTool 
*tool 
= (wxToolBarTool 
*)node
->GetData(); 
 529             node 
= node
->GetNext(); 
 531             GtkWidget 
*item 
= tool
->m_item
; 
 534                 GdkWindow 
*window 
= item
->window
; 
 538                     gdk_window_set_cursor( window
, cursor
.GetCursor() ); 
 547 #endif // wxUSE_TOOLBAR_NATIVE