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