]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tbargtk.cpp
1. wxTimer::Start() returns bool in wxGTK too (other minor corrections
[wxWidgets.git] / src / gtk1 / tbargtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tbargtk.cpp
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID:
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "tbargtk.h"
14 #endif
15
16 #include "wx/toolbar.h"
17
18 //-----------------------------------------------------------------------------
19 // data
20 //-----------------------------------------------------------------------------
21
22 extern bool g_blockEventsOnDrag;
23
24 //-----------------------------------------------------------------------------
25 // wxToolBarTool
26 //-----------------------------------------------------------------------------
27
28 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool,wxObject)
29
30 wxToolBarTool::wxToolBarTool( wxToolBar *owner, int theIndex,
31 const wxBitmap& bitmap1, const wxBitmap& bitmap2,
32 bool toggle,
33 wxObject *clientData,
34 const wxString& shortHelpString,
35 const wxString& longHelpString,
36 GtkWidget *item )
37 {
38 m_owner = owner;
39 m_index = theIndex;
40 m_bitmap1 = bitmap1;
41 m_bitmap2 = bitmap2;
42 m_isToggle = toggle;
43 m_enabled = TRUE;
44 m_toggleState = FALSE;
45 m_shortHelpString = shortHelpString;
46 m_longHelpString = longHelpString;
47 m_isMenuCommand = TRUE;
48 m_clientData = clientData;
49 m_deleteSecondBitmap = FALSE;
50 m_item = item;
51 }
52
53 wxToolBarTool::~wxToolBarTool()
54 {
55 }
56
57 //-----------------------------------------------------------------------------
58 // "clicked" (internal from gtk_toolbar)
59 //-----------------------------------------------------------------------------
60
61 static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
62 {
63 if (g_blockEventsOnDrag) return;
64 if (!tool->m_enabled) return;
65
66 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
67
68 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
69 }
70
71 //-----------------------------------------------------------------------------
72 // "enter_notify_event"
73 //-----------------------------------------------------------------------------
74
75 static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
76 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
77 {
78 if (g_blockEventsOnDrag) return TRUE;
79
80 tool->m_owner->OnMouseEnter( tool->m_index );
81
82 return FALSE;
83 }
84
85 //-----------------------------------------------------------------------------
86 // wxToolBar
87 //-----------------------------------------------------------------------------
88
89 IMPLEMENT_DYNAMIC_CLASS(wxToolBar,wxControl)
90
91 wxToolBar::wxToolBar()
92 {
93 }
94
95 wxToolBar::wxToolBar( wxWindow *parent, wxWindowID id,
96 const wxPoint& pos, const wxSize& size,
97 long style, const wxString& name )
98 {
99 Create( parent, id, pos, size, style, name );
100 }
101
102 wxToolBar::~wxToolBar()
103 {
104 }
105
106 bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
107 const wxPoint& pos, const wxSize& size,
108 long style, const wxString& name )
109 {
110 m_needParent = TRUE;
111
112 PreCreation( parent, id, pos, size, style, name );
113
114 m_tools.DeleteContents( TRUE );
115
116 m_widget = gtk_handle_box_new();
117
118 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_ICONS ) );
119 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
120
121 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
122
123 gtk_widget_show( GTK_WIDGET(m_toolbar) );
124
125 PostCreation();
126
127 Show( TRUE );
128
129 return TRUE;
130 }
131
132 bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
133 {
134 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
135 event.SetEventObject(this);
136 event.SetInt( toolIndex );
137 event.SetExtraLong((long) toggleDown);
138
139 GetEventHandler()->ProcessEvent(event);
140
141 return TRUE;
142 }
143
144 void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
145 {
146 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
147 event.SetEventObject( this );
148 event.SetInt( toolIndex );
149
150 GetEventHandler()->ProcessEvent(event);
151 }
152
153 void wxToolBar::OnMouseEnter( int toolIndex )
154 {
155 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
156 event.SetEventObject(this);
157 event.SetInt( toolIndex );
158
159 GetEventHandler()->ProcessEvent(event);
160 }
161
162 wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
163 const wxBitmap& pushedBitmap, bool toggle,
164 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
165 const wxString& helpString1, const wxString& helpString2 )
166 {
167 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
168 "invalid bitmap for wxToolBar icon" );
169
170 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
171 toggle, clientData,
172 helpString1, helpString2 );
173
174 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
175 "wxToolBar doesn't support GdkBitmap" );
176
177 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
178 "wxToolBar::Add needs a wxBitmap" );
179
180 GtkWidget *tool_pixmap = (GtkWidget *) NULL;
181
182 if (TRUE) // FIXME huh?
183 {
184 GdkPixmap *pixmap = bitmap.GetPixmap();
185
186 GdkBitmap *mask = (GdkBitmap *) NULL;
187 if (bitmap.GetMask()) mask = bitmap.GetMask()->GetBitmap();
188
189 tool_pixmap = gtk_pixmap_new( pixmap, mask );
190 }
191
192 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
193
194 #if 0
195 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
196 : GTK_TOOLBAR_CHILD_BUTTON;
197
198 tool->m_item = gtk_toolbar_append_element
199 (
200 GTK_TOOLBAR(m_toolbar),
201 ctype,
202 (GtkWidget *)NULL,
203 (const char *)NULL,
204 helpString1,
205 "",
206 tool_pixmap,
207 (GtkSignalFunc)gtk_toolbar_callback,
208 (gpointer)tool
209 );
210
211 gtk_signal_connect( GTK_OBJECT(tool->m_item),
212 "enter_notify_event",
213 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
214 (gpointer)tool );
215 #else
216 tool->m_item = gtk_toolbar_append_item
217 (
218 GTK_TOOLBAR(m_toolbar),
219 (const char *)NULL,
220 helpString1,
221 "",
222 tool_pixmap,
223 (GtkSignalFunc)gtk_toolbar_callback,
224 (gpointer)tool
225 );
226 #endif
227
228 m_tools.Append( tool );
229
230 return tool;
231 }
232
233 void wxToolBar::AddSeparator()
234 {
235 gtk_toolbar_append_space( m_toolbar );
236 }
237
238 void wxToolBar::ClearTools()
239 {
240 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
241 }
242
243 void wxToolBar::Realize()
244 {
245 m_x = 0;
246 m_y = 0;
247 m_width = 100;
248 m_height = 0;
249
250 wxNode *node = m_tools.First();
251 while (node)
252 {
253 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
254 if (tool->m_bitmap1.Ok())
255 {
256 int tool_height = tool->m_bitmap1.GetHeight();
257 if (tool_height > m_height) m_height = tool_height;
258 }
259
260 node = node->Next();
261 }
262
263 m_height += 10;
264 }
265
266 void wxToolBar::EnableTool(int toolIndex, bool enable)
267 {
268 wxNode *node = m_tools.First();
269 while (node)
270 {
271 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
272 if (tool->m_index == toolIndex)
273 {
274 tool->m_enabled = enable;
275 return;
276 }
277 node = node->Next();
278 }
279
280 wxFAIL_MSG( "wrong toolbar index" );
281 }
282
283 void wxToolBar::ToggleTool( int toolIndex, bool toggle )
284 {
285 wxNode *node = m_tools.First();
286 while (node)
287 {
288 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
289 if (tool->m_index == toolIndex)
290 {
291 tool->m_toggleState = toggle;
292 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
293 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
294 return;
295 }
296 node = node->Next();
297 }
298
299 wxFAIL_MSG( "wrong toolbar index" );
300 }
301
302 wxObject *wxToolBar::GetToolClientData( int index ) const
303 {
304 wxNode *node = m_tools.First();
305 while (node)
306 {
307 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
308 if (tool->m_index == index) return tool->m_clientData;;
309 node = node->Next();
310 }
311
312 wxFAIL_MSG( "wrong toolbar index" );
313
314 return (wxObject*)NULL;
315 }
316
317 bool wxToolBar::GetToolState(int toolIndex) const
318 {
319 wxNode *node = m_tools.First();
320 while (node)
321 {
322 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
323 if (tool->m_index == toolIndex) return tool->m_toggleState;
324 node = node->Next();
325 }
326
327 wxFAIL_MSG( "wrong toolbar index" );
328
329 return FALSE;
330 }
331
332 bool wxToolBar::GetToolEnabled(int toolIndex) const
333 {
334 wxNode *node = m_tools.First();
335 while (node)
336 {
337 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
338 if (tool->m_index == toolIndex) return tool->m_enabled;
339 node = node->Next();
340 }
341
342 wxFAIL_MSG( "wrong toolbar index" );
343
344 return FALSE;
345 }
346
347 void wxToolBar::SetMargins( int WXUNUSED(x), int WXUNUSED(y) )
348 {
349 wxFAIL_MSG( "wxToolBar::SetMargins not implemented" );
350 }
351
352 void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
353 {
354 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
355 }
356
357 void wxToolBar::SetToolSeparation( int separation )
358 {
359 gtk_toolbar_set_space_size( m_toolbar, separation );
360 }
361