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