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