summaryrefslogtreecommitdiffstats
path: root/views/controls/separator.cc
blob: f7ec9ff4275fa8b6673197a76940208d5ca269ec (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
// Copyright (c) 2011 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 "views/controls/separator.h"

#include "base/logging.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "views/widget/widget.h"

#if defined(OS_LINUX)
#include "views/controls/native_control_gtk.h"
#elif defined(OS_WIN)
#include "ui/base/win/hwnd_util.h"
#include "views/controls/native_control_win.h"
#endif

namespace views {

#if defined(OS_WIN)
class NativeSeparatorWin : public NativeControlWin {
 public:
  explicit NativeSeparatorWin(Separator* separator) : separator_(separator) {}
  virtual ~NativeSeparatorWin() {}

  // Overridden from NativeControlWin:
  virtual void CreateNativeControl() {
    HWND control_hwnd = CreateWindowEx(GetAdditionalExStyle(), L"STATIC", L"",
                                       WS_CHILD | SS_ETCHEDHORZ | SS_SUNKEN,
                                       0, 0, width(), height(),
                                       separator_->GetWidget()->GetNativeView(),
                                       NULL, NULL, NULL);
    ui::CheckWindowCreated(control_hwnd);
    NativeControlCreated(control_hwnd);
  }

 private:
  Separator* separator_;

  DISALLOW_COPY_AND_ASSIGN(NativeSeparatorWin);
};
#elif defined(OS_LINUX)
class NativeSeparatorGtk : public NativeControlGtk {
 public:
  explicit NativeSeparatorGtk(Separator* separator) : separator_(separator) {}
  virtual ~NativeSeparatorGtk() {}

  // Overridden from NativeSeparatorGtk:
  virtual void CreateNativeControl() {
    // TODO(port): create a separator widget and pass to NativeControlCreated.
  }

 private:
  Separator* separator_;

  DISALLOW_COPY_AND_ASSIGN(NativeSeparatorGtk);
};
#endif

// static
const char Separator::kViewClassName[] = "views/Separator";

const int kSeparatorSize = 2;

Separator::Separator() : native_wrapper_(NULL) {
  SetFocusable(false);
}

Separator::~Separator() {
}

////////////////////////////////////////////////////////////////////////////////
// Separator, View overrides:

gfx::Size Separator::GetPreferredSize() {
  return gfx::Size(width(), kSeparatorSize);
}

void Separator::Layout() {
  if (native_wrapper_) {
    int y = static_cast<int>((height() - kSeparatorSize) / 2);
    native_wrapper_->SetBounds(0, y, width(), kSeparatorSize);
    native_wrapper_->Layout();
  }
}

void Separator::ViewHierarchyChanged(bool is_add, View* parent,
                                     View* child) {
  if (is_add && !native_wrapper_ && GetWidget()) {
    CreateNativeWrapper();
    AddChildView(native_wrapper_);
  }
}

std::string Separator::GetClassName() const {
  return kViewClassName;
}

void Separator::GetAccessibleState(ui::AccessibleViewState* state) {
  state->role = ui::AccessibilityTypes::ROLE_SEPARATOR;
}

////////////////////////////////////////////////////////////////////////////////
// Separator, private:

void Separator::CreateNativeWrapper() {
#if defined(OS_WIN)
  native_wrapper_ = new NativeSeparatorWin(this);
#elif defined(OS_LINUX)
  native_wrapper_ = new NativeSeparatorGtk(this);
#endif
}

}  // namespace views