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" 
 132 //----------------------------------------------------------------------------- 
 134 static gint 
gtk_toolbar_enter_callback( GtkWidget 
*WXUNUSED(widget
),  
 135                                         GdkEventCrossing 
*WXUNUSED(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     tb
->OnMouseEnter( tool
->GetId() ); 
 150 //----------------------------------------------------------------------------- 
 151 // InsertChild callback for wxToolBar 
 152 //----------------------------------------------------------------------------- 
 154 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
), 
 155                                     wxWindow
* WXUNUSED(child
) ) 
 157     // we don't do anything here 
 160 // ---------------------------------------------------------------------------- 
 162 // ---------------------------------------------------------------------------- 
 164 void wxToolBarTool::Init() 
 167     m_pixmap 
= (GtkWidget 
*)NULL
; 
 170 wxToolBarToolBase 
*wxToolBar::CreateTool(int id
, 
 171                                          const wxBitmap
& bitmap1
, 
 172                                          const wxBitmap
& bitmap2
, 
 174                                          wxObject 
*clientData
, 
 175                                          const wxString
& shortHelpString
, 
 176                                          const wxString
& longHelpString
) 
 178     return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
, 
 179                              clientData
, shortHelpString
, longHelpString
); 
 182 wxToolBarToolBase 
*wxToolBar::CreateTool(wxControl 
*control
) 
 184     return new wxToolBarTool(this, control
); 
 187 //----------------------------------------------------------------------------- 
 188 // wxToolBar construction 
 189 //----------------------------------------------------------------------------- 
 191 void wxToolBar::Init() 
 194     m_bg 
= (GdkColor 
*)NULL
; 
 195     m_toolbar 
= (GtkToolbar 
*)NULL
; 
 196     m_blockNextEvent 
= FALSE
; 
 199 wxToolBar::~wxToolBar() 
 205 bool wxToolBar::Create( wxWindow 
*parent
, 
 210                         const wxString
& name 
) 
 213     m_insertCallback 
= (wxInsertChildFunction
)wxInsertChildInToolBar
; 
 215     if ( !PreCreation( parent
, pos
, size 
) || 
 216          !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name 
)) 
 218         wxFAIL_MSG( wxT("wxToolBar creation failed") ); 
 223     GtkOrientation orient 
= style 
& wxTB_VERTICAL 
? GTK_ORIENTATION_VERTICAL
 
 224                                                   : GTK_ORIENTATION_HORIZONTAL
; 
 225     m_toolbar 
= GTK_TOOLBAR( gtk_toolbar_new( orient
, GTK_TOOLBAR_ICONS 
) ); 
 227     SetToolSeparation(7); 
 229     if (style 
& wxTB_DOCKABLE
) 
 231         m_widget 
= gtk_handle_box_new(); 
 232         gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) ); 
 233         gtk_widget_show( GTK_WIDGET(m_toolbar
) ); 
 235         if (style 
& wxTB_FLAT
) 
 236             gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE 
); 
 240         m_widget 
= GTK_WIDGET(m_toolbar
); 
 243     gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE 
); 
 245     if (style 
& wxTB_FLAT
) 
 246         gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE 
); 
 254     fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) ); 
 255     m_fg
->pixel 
= fg
.GetPixel(); 
 261     wxColour 
bg(255,255,196); 
 262     bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) ); 
 263     m_bg
->pixel 
= bg
.GetPixel(); 
 265     gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips 
); 
 269                 gtk_widget_get_style(  
 270                     GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window 
) ); 
 272     g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
; 
 273     gdk_font_unref( g_style
->font 
); 
 274         g_style
->font 
= gdk_font_ref( GtkGetDefaultGuiFont() ); 
 275     gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style 
); 
 277     m_parent
->DoAddChild( this ); 
 286 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase 
*toolBase
) 
 288     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 290     // we have inserted a space before all the tools 
 291     if (m_xMargin 
> 1) pos
++; 
 293     if ( tool
->IsButton() ) 
 295         wxBitmap bitmap 
= tool
->GetBitmap1(); 
 297         wxCHECK_MSG( bitmap
.Ok(), FALSE
, 
 298                      wxT("invalid bitmap for wxToolBar icon") ); 
 300         wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
, 
 301                      wxT("wxToolBar doesn't support GdkBitmap") ); 
 303         wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
, 
 304                      wxT("wxToolBar::Add needs a wxBitmap") ); 
 306         GtkWidget 
*tool_pixmap 
= (GtkWidget 
*)NULL
; 
 308         GdkPixmap 
*pixmap 
= bitmap
.GetPixmap(); 
 310         GdkBitmap 
*mask 
= (GdkBitmap 
*)NULL
; 
 311         if ( bitmap
.GetMask() ) 
 312           mask 
= bitmap
.GetMask()->GetBitmap(); 
 314         tool_pixmap 
= gtk_pixmap_new( pixmap
, mask 
); 
 315 #if (GTK_MINOR_VERSION > 0) 
 316         gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE 
); 
 319         gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 ); 
 321         tool
->m_pixmap 
= tool_pixmap
; 
 324     switch ( tool
->GetStyle() ) 
 326         case wxTOOL_STYLE_BUTTON
: 
 327             tool
->m_item 
= gtk_toolbar_insert_element
 
 331                                 ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
 
 332                                 : GTK_TOOLBAR_CHILD_BUTTON
, 
 335                               tool
->GetShortHelp().mbc_str(), 
 336                               "", // tooltip_private_text (?) 
 338                               (GtkSignalFunc
)gtk_toolbar_callback
, 
 345                 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") ); 
 350             gtk_signal_connect( GTK_OBJECT(tool
->m_item
), 
 351                                 "enter_notify_event",  
 352                                 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
), 
 356         case wxTOOL_STYLE_SEPARATOR
: 
 357             gtk_toolbar_append_space( m_toolbar 
); 
 362         case wxTOOL_STYLE_CONTROL
: 
 363             gtk_toolbar_insert_widget( 
 365                                        tool
->GetControl()->m_widget
, 
 374     (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request 
) 
 376     m_width 
= req
.width 
+ m_xMargin
; 
 377     m_height 
= req
.height 
+ 2*m_yMargin
; 
 382 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase 
*toolBase
) 
 384     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 386     switch ( tool
->GetStyle() ) 
 388         case wxTOOL_STYLE_CONTROL
: 
 389             tool
->GetControl()->Destroy(); 
 392         case wxTOOL_STYLE_BUTTON
: 
 393             gtk_widget_destroy( tool
->m_item 
); 
 396         //case wxTOOL_STYLE_SEPARATOR: -- nothing to do 
 402 // ---------------------------------------------------------------------------- 
 403 // wxToolBar tools state 
 404 // ---------------------------------------------------------------------------- 
 406 void wxToolBar::DoEnableTool(wxToolBarToolBase 
*toolBase
, bool enable
) 
 408 #if (GTK_MINOR_VERSION > 0) 
 409     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 411     /* we don't disable the tools for GTK 1.0 as the bitmaps don't get 
 412        greyed anyway and this also disables tooltips */ 
 414         gtk_widget_set_sensitive( tool
->m_item
, enable 
); 
 418 void wxToolBar::DoToggleTool( wxToolBarToolBase 
*toolBase
, bool toggle 
)  
 420     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)toolBase
; 
 422     GtkWidget 
*item 
= tool
->m_item
; 
 423     if ( item 
&& GTK_IS_TOGGLE_BUTTON(item
) ) 
 425         wxBitmap bitmap 
= tool
->GetBitmap(); 
 428             GtkPixmap 
*pixmap 
= GTK_PIXMAP( tool
->m_pixmap 
); 
 430             GdkBitmap 
*mask 
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap() 
 433             gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask 
); 
 436         m_blockNextEvent 
= TRUE
;  // we cannot use gtk_signal_disconnect here 
 438         gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle 
); 
 442 void wxToolBar::DoSetToggle(wxToolBarToolBase 
* WXUNUSED(tool
), 
 443                             bool WXUNUSED(toggle
)) 
 445     // VZ: absolutely no idea about how to do it 
 446     wxFAIL_MSG( _T("not implemented") ); 
 449 // ---------------------------------------------------------------------------- 
 450 // wxToolBar geometry 
 451 // ---------------------------------------------------------------------------- 
 453 wxToolBarToolBase 
*wxToolBar::FindToolForPosition(wxCoord 
WXUNUSED(x
), 
 454                                                   wxCoord 
WXUNUSED(y
)) const 
 456     // VZ: GTK+ doesn't seem to have such thing 
 457     wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") ); 
 459     return (wxToolBarToolBase 
*)NULL
; 
 462 void wxToolBar::SetMargins( int x
, int y 
) 
 464     wxCHECK_RET( GetToolsCount() == 0, 
 465                  wxT("wxToolBar::SetMargins must be called before adding tools.") ); 
 467     if (x 
> 1) gtk_toolbar_append_space( m_toolbar 
);  // oh well 
 473 void wxToolBar::SetToolSeparation( int separation 
) 
 475     gtk_toolbar_set_space_size( m_toolbar
, separation 
); 
 476     m_toolSeparation 
= separation
; 
 479 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString 
) 
 481     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)FindById(id
); 
 485         (void)tool
->SetShortHelp(helpString
); 
 486         gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
, 
 487                              helpString
.mbc_str(), ""); 
 491 // ---------------------------------------------------------------------------- 
 492 // wxToolBar idle handling 
 493 // ---------------------------------------------------------------------------- 
 495 void wxToolBar::OnInternalIdle() 
 497     wxCursor cursor 
= m_cursor
; 
 498     if (g_globalCursor
.Ok()) cursor 
= g_globalCursor
; 
 502         /* I now set the cursor the anew in every OnInternalIdle call 
 503            as setting the cursor in a parent window also effects the 
 504            windows above so that checking for the current cursor is 
 507         if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
)) 
 509             /* if the toolbar is dockable, then m_widget stands for the 
 510                GtkHandleBox widget, which uses its own window so that we 
 511                can set the cursor for it. if the toolbar is not dockable, 
 512                m_widget comes from m_toolbar which uses its parent's 
 513                window ("windowless windows") and thus we cannot set the 
 515             gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() ); 
 518         wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 521             wxToolBarTool 
*tool 
= (wxToolBarTool 
*)node
->GetData(); 
 522             node 
= node
->GetNext(); 
 524             GtkWidget 
*item 
= tool
->m_item
; 
 527                 GdkWindow 
*window 
= item
->window
; 
 531                     gdk_window_set_cursor( window
, cursor
.GetCursor() ); 
 540 #endif // wxUSE_TOOLBAR_NATIVE