OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/widget/widget.h" |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "ui/views/view.h" |
| 10 #include "ui/views/widget/native_widget.h" |
| 11 #include "ui/views/widget/root_view.h" |
| 12 |
| 13 namespace ui { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // TODO(beng): move to platform file |
| 18 int GetHorizontalDragThreshold() { |
| 19 static int threshold = -1; |
| 20 if (threshold == -1) |
| 21 threshold = GetSystemMetrics(SM_CXDRAG) / 2; |
| 22 return threshold; |
| 23 } |
| 24 |
| 25 // TODO(beng): move to platform file |
| 26 int GetVerticalDragThreshold() { |
| 27 static int threshold = -1; |
| 28 if (threshold == -1) |
| 29 threshold = GetSystemMetrics(SM_CYDRAG) / 2; |
| 30 return threshold; |
| 31 } |
| 32 |
| 33 bool ExceededDragThreshold(int delta_x, int delta_y) { |
| 34 return (abs(delta_x) > GetHorizontalDragThreshold() || |
| 35 abs(delta_y) > GetVerticalDragThreshold()); |
| 36 } |
| 37 |
| 38 } |
| 39 |
| 40 //////////////////////////////////////////////////////////////////////////////// |
| 41 // Widget, public: |
| 42 |
| 43 Widget::Widget(View* contents_view) |
| 44 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 45 native_widget_(NativeWidget::CreateNativeWidget(this))), |
| 46 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 47 root_view_(new internal::RootView(this, contents_view))), |
| 48 is_mouse_button_pressed_(false), |
| 49 last_mouse_event_was_move_(false), |
| 50 ALLOW_THIS_IN_INITIALIZER_LIST(close_widget_factory_(this)) { |
| 51 } |
| 52 |
| 53 Widget::~Widget() { |
| 54 } |
| 55 |
| 56 void Widget::InitWithNativeViewParent(gfx::NativeView parent, |
| 57 const gfx::Rect& bounds) { |
| 58 native_widget_->InitWithNativeViewParent(parent, bounds); |
| 59 } |
| 60 |
| 61 void Widget::InitWithWidgetParent(Widget* parent, const gfx::Rect& bounds) { |
| 62 native_widget_->InitWithWidgetParent(parent, bounds); |
| 63 } |
| 64 |
| 65 void Widget::InitWithViewParent(View* parent, const gfx::Rect& bounds) { |
| 66 native_widget_->InitWithViewParent(parent, bounds); |
| 67 } |
| 68 |
| 69 gfx::Rect Widget::GetWindowScreenBounds() const { |
| 70 return native_widget_->GetWindowScreenBounds(); |
| 71 } |
| 72 |
| 73 gfx::Rect Widget::GetClientAreaScreenBounds() const { |
| 74 return native_widget_->GetClientAreaScreenBounds(); |
| 75 } |
| 76 |
| 77 void Widget::SetBounds(const gfx::Rect& bounds) { |
| 78 native_widget_->SetBounds(bounds); |
| 79 } |
| 80 |
| 81 void Widget::SetShape(const gfx::Path& shape) { |
| 82 native_widget_->SetShape(shape); |
| 83 } |
| 84 |
| 85 void Widget::Show() { |
| 86 native_widget_->Show(); |
| 87 } |
| 88 |
| 89 void Widget::Hide() { |
| 90 native_widget_->Hide(); |
| 91 } |
| 92 |
| 93 void Widget::Close() { |
| 94 native_widget_->Hide(); |
| 95 |
| 96 if (close_widget_factory_.empty()) { |
| 97 MessageLoop::current()->PostTask(FROM_HERE, |
| 98 close_widget_factory_.NewRunnableMethod(&Widget::CloseNow)); |
| 99 } |
| 100 } |
| 101 |
| 102 void Widget::MoveAbove(Widget* other) { |
| 103 native_widget_->MoveAbove(other->native_widget()); |
| 104 } |
| 105 |
| 106 void Widget::InvalidateRect(const gfx::Rect& invalid_rect) { |
| 107 native_widget_->InvalidateRect(invalid_rect); |
| 108 } |
| 109 |
| 110 ThemeProvider* Widget::GetThemeProvider() const { |
| 111 return NULL; |
| 112 } |
| 113 |
| 114 //////////////////////////////////////////////////////////////////////////////// |
| 115 // Widget, NativeWidgetListener implementation: |
| 116 |
| 117 void Widget::OnClose() { |
| 118 Close(); |
| 119 } |
| 120 |
| 121 void Widget::OnDestroy() { |
| 122 if (delete_on_destroy_) |
| 123 delete this; |
| 124 } |
| 125 |
| 126 void Widget::OnDisplayChanged() { |
| 127 // TODO(beng): |
| 128 } |
| 129 |
| 130 bool Widget::OnKeyEvent(const KeyEvent& event) { |
| 131 // find root view. |
| 132 |
| 133 //return root_view_->OnKeyEvent(event); |
| 134 return true; |
| 135 } |
| 136 |
| 137 void Widget::OnMouseCaptureLost() { |
| 138 if (native_widget_->HasMouseCapture()) { |
| 139 if (is_mouse_button_pressed_) |
| 140 root_view_->OnMouseCaptureLost(); |
| 141 is_mouse_button_pressed_ = false; |
| 142 } |
| 143 } |
| 144 |
| 145 bool Widget::OnMouseEvent(const MouseEvent& event) { |
| 146 last_mouse_event_was_move_ = false; |
| 147 switch (event.type()) { |
| 148 case Event::ET_MOUSE_PRESSED: |
| 149 if (root_view_->OnMousePressed(event)) { |
| 150 is_mouse_button_pressed_ = true; |
| 151 if (!native_widget_->HasMouseCapture()) |
| 152 native_widget_->SetMouseCapture(); |
| 153 return true; |
| 154 } |
| 155 return false; |
| 156 case Event::ET_MOUSE_RELEASED: |
| 157 // TODO(beng): NativeWidgetGtk should not call this function if drag data |
| 158 // exists, see comment in this function in WidgetGtk. |
| 159 // Release the capture first, that way we don't get confused if |
| 160 // OnMouseReleased blocks. |
| 161 if (native_widget_->HasMouseCapture() && |
| 162 native_widget_->ShouldReleaseCaptureOnMouseReleased()) { |
| 163 native_widget_->ReleaseMouseCapture(); |
| 164 } |
| 165 is_mouse_button_pressed_ = false; |
| 166 root_view_->OnMouseReleased(event); |
| 167 return true; |
| 168 case Event::ET_MOUSE_MOVED: |
| 169 if (native_widget_->HasMouseCapture() && is_mouse_button_pressed_) { |
| 170 last_mouse_event_was_move_ = false; |
| 171 root_view_->OnMouseDragged(event); |
| 172 } else { |
| 173 gfx::Point screen_loc(event.location()); |
| 174 View::ConvertPointToScreen(root_view_.get(), &screen_loc); |
| 175 if (last_mouse_event_was_move_ && |
| 176 last_mouse_event_position_ == screen_loc) { |
| 177 // Don't generate a mouse event for the same location as the last. |
| 178 return true; |
| 179 } |
| 180 last_mouse_event_position_ = screen_loc; |
| 181 last_mouse_event_was_move_ = true; |
| 182 root_view_->OnMouseMoved(event); |
| 183 } |
| 184 break; |
| 185 case Event::ET_MOUSE_EXITED: |
| 186 root_view_->OnMouseExited(event); |
| 187 return true; |
| 188 } |
| 189 return true; |
| 190 } |
| 191 |
| 192 bool Widget::OnMouseWheelEvent(const MouseWheelEvent& event) { |
| 193 return root_view_->OnMouseWheel(event); |
| 194 } |
| 195 |
| 196 void Widget::OnNativeWidgetCreated() { |
| 197 } |
| 198 |
| 199 void Widget::OnPaint(gfx::Canvas* canvas) { |
| 200 root_view_->Paint(canvas); |
| 201 } |
| 202 |
| 203 void Widget::OnSizeChanged(const gfx::Size& size) { |
| 204 root_view_->SetSize(size); |
| 205 } |
| 206 |
| 207 void Widget::OnWorkAreaChanged() { |
| 208 |
| 209 } |
| 210 |
| 211 //////////////////////////////////////////////////////////////////////////////// |
| 212 // Widget, private: |
| 213 |
| 214 void Widget::CloseNow() { |
| 215 native_widget_->Close(); |
| 216 } |
| 217 |
| 218 } // namespace ui |
| 219 |
OLD | NEW |