summaryrefslogtreecommitdiffstats
path: root/views/controls/button/native_button_gtk.cc
blob: 3f1089743a6d5a06af4153d6cc596b8549e205f7 (plain)
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
// 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/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).
  SetAssociatedFocusView(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());
}

void NativeButtonGtk::UpdateFont() {
  if (!native_view())
    return;

  NOTIMPLEMENTED();
  // 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())
    NOTIMPLEMENTED();
}

View* NativeButtonGtk::GetView() {
  return this;
}

void NativeButtonGtk::SetFocus() {
  // Focus the associated widget.
  Focus();
}

gfx::Size NativeButtonGtk::GetPreferredSize() {
  GtkRequisition size_request = { 0, 0 };
  gtk_widget_size_request(native_view(), &size_request);
  return gfx::Size(size_request.width, size_request.height);
}

void NativeButtonGtk::CreateNativeControl() {
  GtkWidget* widget = gtk_button_new();
  g_signal_connect(G_OBJECT(widget), "clicked",
                   G_CALLBACK(CallClicked), NULL);
  NativeControlCreated(widget);
}

void NativeButtonGtk::NativeControlCreated(GtkWidget* widget) {
  NativeControlGtk::NativeControlCreated(widget);

  UpdateFont();
  UpdateLabel();
  UpdateDefault();
}

// static
void NativeButtonGtk::CallClicked(GtkButton* widget) {
  View* view = GetViewForNative(GTK_WIDGET(widget));
  if (view)
    static_cast<NativeButtonGtk*>(view)->OnClicked();
}

void NativeButtonGtk::OnClicked() {
  native_button_->ButtonPressed();
}

NativeCheckboxGtk::NativeCheckboxGtk(Checkbox* checkbox)
    : NativeButtonGtk(checkbox) {
}

void NativeCheckboxGtk::CreateNativeControl() {
  GtkWidget* widget = gtk_check_button_new();
  NativeControlCreated(widget);
}

// 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);
}

}  // namespace views