]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: button.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "button.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/button.h" | |
15 | ||
16 | #include "gdk/gdk.h" | |
17 | #include "gtk/gtk.h" | |
18 | ||
19 | //----------------------------------------------------------------------------- | |
20 | // classes | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | class wxButton; | |
24 | ||
25 | //----------------------------------------------------------------------------- | |
26 | // idle system | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | extern void wxapp_install_idle_handler(); | |
30 | extern bool g_isIdle; | |
31 | ||
32 | //----------------------------------------------------------------------------- | |
33 | // data | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | extern bool g_blockEventsOnDrag; | |
37 | ||
38 | //----------------------------------------------------------------------------- | |
39 | // "clicked" | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) | |
43 | { | |
44 | if (g_isIdle) | |
45 | wxapp_install_idle_handler(); | |
46 | ||
47 | if (!button->m_hasVMT) return; | |
48 | if (g_blockEventsOnDrag) return; | |
49 | ||
50 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); | |
51 | event.SetEventObject(button); | |
52 | button->GetEventHandler()->ProcessEvent(event); | |
53 | } | |
54 | ||
55 | //----------------------------------------------------------------------------- | |
56 | // wxButton | |
57 | //----------------------------------------------------------------------------- | |
58 | ||
59 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) | |
60 | ||
61 | wxButton::wxButton() | |
62 | { | |
63 | } | |
64 | ||
65 | wxButton::~wxButton() | |
66 | { | |
67 | if (m_clientData) delete m_clientData; | |
68 | } | |
69 | ||
70 | bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, | |
71 | const wxPoint &pos, const wxSize &size, | |
72 | long style, const wxValidator& validator, const wxString &name ) | |
73 | { | |
74 | m_clientData = (wxClientData*) NULL; | |
75 | m_needParent = TRUE; | |
76 | m_acceptsFocus = TRUE; | |
77 | ||
78 | wxSize newSize = size; | |
79 | ||
80 | PreCreation( parent, id, pos, newSize, style, name ); | |
81 | ||
82 | #if wxUSE_VALIDATORS | |
83 | SetValidator( validator ); | |
84 | #endif // wxUSE_VALIDATORS | |
85 | ||
86 | m_widget = gtk_button_new_with_label( "" ); | |
87 | ||
88 | #if (GTK_MINOR_VERSION > 0) | |
89 | if (style & wxNO_BORDER) | |
90 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
91 | #endif | |
92 | ||
93 | SetLabel(label); | |
94 | ||
95 | if (newSize.x == -1) newSize.x = 15+gdk_string_measure( m_widget->style->font, label.mbc_str() ); | |
96 | if (newSize.y == -1) newSize.y = 26; | |
97 | SetSize( newSize.x, newSize.y ); | |
98 | ||
99 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", | |
100 | GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this ); | |
101 | ||
102 | m_parent->DoAddChild( this ); | |
103 | ||
104 | PostCreation(); | |
105 | ||
106 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
107 | SetForegroundColour( parent->GetForegroundColour() ); | |
108 | SetFont( parent->GetFont() ); | |
109 | ||
110 | Show( TRUE ); | |
111 | ||
112 | return TRUE; | |
113 | } | |
114 | ||
115 | void wxButton::SetDefault(void) | |
116 | { | |
117 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
118 | gtk_widget_grab_default( m_widget ); | |
119 | ||
120 | SetSize( m_x, m_y, m_width, m_height ); | |
121 | } | |
122 | ||
123 | /* static */ | |
124 | wxSize wxButton::GetDefaultSize() | |
125 | { | |
126 | return wxSize(80,26); | |
127 | } | |
128 | ||
129 | void wxButton::SetLabel( const wxString &label ) | |
130 | { | |
131 | wxCHECK_RET( m_widget != NULL, _T("invalid button") ); | |
132 | ||
133 | wxControl::SetLabel( label ); | |
134 | ||
135 | gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget)->child ), GetLabel().mbc_str() ); | |
136 | } | |
137 | ||
138 | bool wxButton::Enable( bool enable ) | |
139 | { | |
140 | if ( !wxControl::Enable( enable ) ) | |
141 | return FALSE; | |
142 | ||
143 | gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable ); | |
144 | ||
145 | return TRUE; | |
146 | } | |
147 | ||
148 | void wxButton::ApplyWidgetStyle() | |
149 | { | |
150 | SetWidgetStyle(); | |
151 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
152 | gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle ); | |
153 | } |