1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
#include <gtk/gtk.h>
#include "views/controls/button/native_button_gtk.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "views/controls/button/checkbox.h"
#include "views/controls/button/native_button.h"
#include "views/controls/button/radio_button.h"
#include "views/controls/native/native_view_host_gtk.h"
#include "views/widget/widget.h"
namespace views {
NativeButtonGtk::NativeButtonGtk(NativeButton* native_button)
: NativeControlGtk(),
native_button_(native_button) {
// Associates the actual GtkWidget with the native_button so the native_button
// is the one considered as having the focus (not the wrapper) when the
// GtkWidget is focused directly (with a click for example).
set_focus_view(native_button);
}
NativeButtonGtk::~NativeButtonGtk() {
}
void NativeButtonGtk::UpdateLabel() {
if (!native_view())
return;
gtk_button_set_label(GTK_BUTTON(native_view()),
WideToUTF8(native_button_->label()).c_str());
preferred_size_ = gfx::Size();
}
void NativeButtonGtk::UpdateFont() {
if (!native_view())
return;
NOTIMPLEMENTED();
preferred_size_ = gfx::Size();
// SendMessage(GetHWND(), WM_SETFONT,
// reinterpret_cast<WPARAM>(native_button_->font().hfont()),
// FALSE);
}
void NativeButtonGtk::UpdateEnabled() {
if (!native_view())
return;
SetEnabled(native_button_->IsEnabled());
}
void NativeButtonGtk::UpdateDefault() {
if (!native_view())
return;
if (IsCheckbox())
UpdateChecked();
else
NOTIMPLEMENTED();
}
View* NativeButtonGtk::GetView() {
return this;
}
void NativeButtonGtk::SetFocus() {
// Focus the associated widget.
Focus();
}
bool NativeButtonGtk::UsesNativeLabel() const {
return true;
}
gfx::NativeView NativeButtonGtk::GetTestingHandle() const {
return native_view();
}
gfx::Size NativeButtonGtk::GetPreferredSize() {
if (!native_view())
return gfx::Size();
if (preferred_size_.IsEmpty()) {
GtkRequisition size_request = { 0, 0 };
gtk_widget_size_request(native_view(), &size_request);
preferred_size_.SetSize(size_request.width,
std::max(size_request.height, 29));
}
return preferred_size_;
}
void NativeButtonGtk::CreateNativeControl() {
GtkWidget* widget = gtk_button_new();
g_signal_connect(G_OBJECT(widget), "clicked",
G_CALLBACK(CallClicked), this);
NativeControlCreated(widget);
}
void NativeButtonGtk::NativeControlCreated(GtkWidget* widget) {
NativeControlGtk::NativeControlCreated(widget);
UpdateFont();
UpdateLabel();
UpdateDefault();
}
// static
void NativeButtonGtk::CallClicked(GtkButton* widget, NativeButtonGtk* button) {
button->OnClicked();
}
void NativeButtonGtk::OnClicked() {
native_button_->ButtonPressed();
}
////////////////////////////////////////////////////////////////////////////////
// NativeCheckboxGtk
NativeCheckboxGtk::NativeCheckboxGtk(Checkbox* checkbox)
: NativeButtonGtk(checkbox),
checkbox_(checkbox),
deliver_click_event_(true) {
}
void NativeCheckboxGtk::CreateNativeControl() {
GtkWidget* widget = gtk_check_button_new();
g_signal_connect(G_OBJECT(widget), "clicked",
G_CALLBACK(CallClicked), this);
NativeControlCreated(widget);
}
void NativeCheckboxGtk::OnClicked() {
// ignore event if the event is generated by
// gtk_toggle_button_set_active below.
if (deliver_click_event_) {
checkbox_->SetChecked(!checkbox_->checked());
NativeButtonGtk::OnClicked();
}
}
void NativeCheckboxGtk::UpdateChecked() {
if (!native_view())
return;
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(native_view()))
!= checkbox_->checked()) {
// gtk_toggle_button_set_active emites "clicked" signal, which
// invokes OnClicked method above. deliver_click_event_ flag is used
// to prevent such signal to invoke OnClicked callback.
deliver_click_event_ = false;
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(native_view()),
checkbox_->checked());
deliver_click_event_ = true;
}
}
// static
void NativeCheckboxGtk::CallClicked(GtkButton* widget,
NativeCheckboxGtk* button) {
button->OnClicked();
}
////////////////////////////////////////////////////////////////////////////////
// NativeButtonWrapper
// static
int NativeButtonWrapper::GetFixedWidth() {
// TODO(brettw) implement this properly.
return 10;
}
// static
NativeButtonWrapper* NativeButtonWrapper::CreateNativeButtonWrapper(
NativeButton* native_button) {
return new NativeButtonGtk(native_button);
}
// static
NativeButtonWrapper* NativeButtonWrapper::CreateCheckboxWrapper(
Checkbox* checkbox) {
return new NativeCheckboxGtk(checkbox);
}
// static
NativeButtonWrapper* NativeButtonWrapper::CreateRadioButtonWrapper(
RadioButton* radio_button) {
NOTIMPLEMENTED();
return NULL;
}
} // namespace views
|