]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/button.cpp
bitmap and image updates
[wxWidgets.git] / src / gtk / button.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: button.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "button.h"
12#endif
13
14#include "wx/button.h"
15
20e05ffb
RR
16#include <gdk/gdk.h>
17#include <gtk/gtk.h>
83624f79 18
c801d85f
KB
19//-----------------------------------------------------------------------------
20// classes
21//-----------------------------------------------------------------------------
22
23class wxButton;
24
acfd422a
RR
25//-----------------------------------------------------------------------------
26// idle system
27//-----------------------------------------------------------------------------
28
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31
66bd6b93
RR
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
37
c801d85f 38//-----------------------------------------------------------------------------
e1e955e1 39// "clicked"
c801d85f
KB
40//-----------------------------------------------------------------------------
41
66bd6b93 42static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 43{
32ac755d
RR
44 if (g_isIdle)
45 wxapp_install_idle_handler();
acfd422a 46
a2053b27 47 if (!button->m_hasVMT) return;
acfd422a 48 if (g_blockEventsOnDrag) return;
32ac755d 49
acfd422a
RR
50 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
51 event.SetEventObject(button);
52 button->GetEventHandler()->ProcessEvent(event);
6de97a3b 53}
c801d85f
KB
54
55//-----------------------------------------------------------------------------
e1e955e1
RR
56// wxButton
57//-----------------------------------------------------------------------------
58
59IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 60
fd0eed64 61wxButton::wxButton()
c801d85f 62{
6de97a3b 63}
c801d85f 64
fd0eed64
RR
65wxButton::~wxButton()
66{
fd0eed64
RR
67}
68
c801d85f 69bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 70 const wxPoint &pos, const wxSize &size,
6de97a3b 71 long style, const wxValidator& validator, const wxString &name )
c801d85f 72{
b292e2f5
RR
73 m_needParent = TRUE;
74 m_acceptsFocus = TRUE;
32c77a71 75
4dcaf11a
RR
76 if (!PreCreation( parent, pos, size ) ||
77 !CreateBase( parent, id, pos, size, style, validator, name ))
78 {
223d09f6 79 wxFAIL_MSG( wxT("wxButton creation failed") );
4dcaf11a
RR
80 return FALSE;
81 }
c801d85f 82
354aa1e3
RR
83/*
84 wxString label2( label );
85 for (size_t i = 0; i < label2.Len(); i++)
86 {
87 if (label2.GetChar(i) == wxT('&'))
88 label2.SetChar(i,wxT('_'));
89 }
90
91 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
92 gtk_widget_show( accel_label );
93
94 m_widget = gtk_button_new();
95 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
96
97 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
de1c750f 98
354aa1e3
RR
99 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
100 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
101
102 wxControl::SetLabel( label );
103*/
104
105 m_widget = gtk_button_new_with_label("");
106
107 SetLabel( label );
108
de1c750f
RR
109#if (GTK_MINOR_VERSION > 0)
110 if (style & wxNO_BORDER)
111 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
112#endif
113
3417c2cd
RR
114 int x = 0; int y = 0;
115 wxFont new_font( parent->GetFont() );
116 GetTextExtent( m_label, &x, &y, (int*)NULL, (int*)NULL, &new_font );
117
4dcaf11a 118 wxSize newSize = size;
92afa2b1
RR
119 if (newSize.x == -1)
120 {
121 newSize.x = 12+x;
122 if (newSize.x < 80) newSize.x = 80;
123 }
124 if (newSize.y == -1)
125 {
126 newSize.y = 11+y;
127 if (newSize.x < 26) newSize.x = 26;
128 }
129
b292e2f5 130 SetSize( newSize.x, newSize.y );
32c77a71 131
b292e2f5
RR
132 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
133 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 134
f03fc89f 135 m_parent->DoAddChild( this );
6ca41e57 136
b292e2f5 137 PostCreation();
f96aa4d9 138
b292e2f5
RR
139 SetBackgroundColour( parent->GetBackgroundColour() );
140 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 141 SetFont( parent->GetFont() );
32c77a71 142
b292e2f5 143 Show( TRUE );
32c77a71 144
b292e2f5 145 return TRUE;
6de97a3b 146}
32c77a71 147
da048e3d 148void wxButton::SetDefault()
c801d85f 149{
3502e687
RR
150 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
151 gtk_widget_grab_default( m_widget );
152
153 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 154}
c801d85f 155
ebea0891
KB
156/* static */
157wxSize wxButton::GetDefaultSize()
8dbf4589
RR
158{
159 return wxSize(80,26);
160}
161
c801d85f
KB
162void wxButton::SetLabel( const wxString &label )
163{
223d09f6 164 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 165
b292e2f5 166 wxControl::SetLabel( label );
f96aa4d9 167
3bce7509 168 gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget)->child ), GetLabel().mbc_str() );
6de97a3b 169}
c801d85f 170
f03fc89f 171bool wxButton::Enable( bool enable )
a9c96bcc 172{
f03fc89f
VZ
173 if ( !wxControl::Enable( enable ) )
174 return FALSE;
f96aa4d9 175
b292e2f5 176 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
f03fc89f
VZ
177
178 return TRUE;
a9c96bcc
RR
179}
180
58614078 181void wxButton::ApplyWidgetStyle()
868a2826 182{
b292e2f5
RR
183 SetWidgetStyle();
184 gtk_widget_set_style( m_widget, m_widgetStyle );
185 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
a81258be 186}