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