| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/cursors/webcursor.h" | 5 #include "content/common/cursors/webcursor.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "third_party/WebKit/public/platform/WebImage.h" | 10 #include "third_party/WebKit/public/platform/WebImage.h" |
| 11 | 11 |
| 12 using blink::WebCursorInfo; | 12 using blink::WebCursorInfo; |
| 13 | 13 |
| 14 static const int kMaxCursorDimension = 1024; | 14 static const int kMaxCursorDimension = 1024; |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 WebCursor::WebCursor() | 18 WebCursor::WebCursor() |
| 19 : type_(WebCursorInfo::TypePointer), | 19 : type_(WebCursorInfo::TypePointer), |
| 20 custom_scale_(1) { | 20 custom_scale_(1) { |
| 21 #if defined(OS_WIN) | |
| 22 external_cursor_ = NULL; | |
| 23 #endif | |
| 24 #if defined(USE_AURA) | |
| 25 device_scale_factor_ = 1.0f; | |
| 26 #endif | |
| 27 InitPlatformData(); | 21 InitPlatformData(); |
| 28 } | 22 } |
| 29 | 23 |
| 30 WebCursor::~WebCursor() { | 24 WebCursor::~WebCursor() { |
| 31 Clear(); | 25 Clear(); |
| 32 } | 26 } |
| 33 | 27 |
| 34 WebCursor::WebCursor(const WebCursor& other) { | 28 WebCursor::WebCursor(const WebCursor& other) { |
| 35 InitPlatformData(); | 29 InitPlatformData(); |
| 36 Copy(other); | 30 Copy(other); |
| 37 } | 31 } |
| 38 | 32 |
| 39 const WebCursor& WebCursor::operator=(const WebCursor& other) { | 33 const WebCursor& WebCursor::operator=(const WebCursor& other) { |
| 40 if (this == &other) | 34 if (this == &other) |
| 41 return *this; | 35 return *this; |
| 42 | 36 |
| 43 Clear(); | 37 Clear(); |
| 44 Copy(other); | 38 Copy(other); |
| 45 return *this; | 39 return *this; |
| 46 } | 40 } |
| 47 | 41 |
| 48 void WebCursor::InitFromCursorInfo(const CursorInfo& cursor_info) { | 42 void WebCursor::InitFromCursorInfo(const CursorInfo& cursor_info) { |
| 49 Clear(); | 43 Clear(); |
| 50 | 44 |
| 51 #if defined(OS_WIN) | |
| 52 if (cursor_info.external_handle) { | |
| 53 InitFromExternalCursor(cursor_info.external_handle); | |
| 54 return; | |
| 55 } | |
| 56 #endif | |
| 57 | |
| 58 type_ = cursor_info.type; | 45 type_ = cursor_info.type; |
| 59 hotspot_ = cursor_info.hotspot; | 46 hotspot_ = cursor_info.hotspot; |
| 60 if (IsCustom()) | 47 if (IsCustom()) |
| 61 SetCustomData(cursor_info.custom_image); | 48 SetCustomData(cursor_info.custom_image); |
| 62 custom_scale_ = cursor_info.image_scale_factor; | 49 custom_scale_ = cursor_info.image_scale_factor; |
| 63 CHECK(custom_scale_ > 0); | 50 CHECK(custom_scale_ > 0); |
| 64 ClampHotspot(); | 51 ClampHotspot(); |
| 65 } | 52 } |
| 66 | 53 |
| 67 void WebCursor::GetCursorInfo(CursorInfo* cursor_info) const { | 54 void WebCursor::GetCursorInfo(CursorInfo* cursor_info) const { |
| 68 cursor_info->type = static_cast<WebCursorInfo::Type>(type_); | 55 cursor_info->type = static_cast<WebCursorInfo::Type>(type_); |
| 69 cursor_info->hotspot = hotspot_; | 56 cursor_info->hotspot = hotspot_; |
| 70 ImageFromCustomData(&cursor_info->custom_image); | 57 ImageFromCustomData(&cursor_info->custom_image); |
| 71 cursor_info->image_scale_factor = custom_scale_; | 58 cursor_info->image_scale_factor = custom_scale_; |
| 72 | |
| 73 #if defined(OS_WIN) | |
| 74 cursor_info->external_handle = external_cursor_; | |
| 75 #endif | |
| 76 } | 59 } |
| 77 | 60 |
| 78 bool WebCursor::Deserialize(base::PickleIterator* iter) { | 61 bool WebCursor::Deserialize(base::PickleIterator* iter) { |
| 79 int type, hotspot_x, hotspot_y, size_x, size_y, data_len; | 62 int type, hotspot_x, hotspot_y, size_x, size_y, data_len; |
| 80 float scale; | 63 float scale; |
| 81 const char* data; | 64 const char* data; |
| 82 | 65 |
| 83 // Leave |this| unmodified unless we are going to return success. | 66 // Leave |this| unmodified unless we are going to return success. |
| 84 if (!iter->ReadInt(&type) || | 67 if (!iter->ReadInt(&type) || |
| 85 !iter->ReadInt(&hotspot_x) || | 68 !iter->ReadInt(&hotspot_x) || |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 142 |
| 160 if (!IsPlatformDataEqual(other)) | 143 if (!IsPlatformDataEqual(other)) |
| 161 return false; | 144 return false; |
| 162 | 145 |
| 163 return hotspot_ == other.hotspot_ && | 146 return hotspot_ == other.hotspot_ && |
| 164 custom_size_ == other.custom_size_ && | 147 custom_size_ == other.custom_size_ && |
| 165 custom_scale_ == other.custom_scale_ && | 148 custom_scale_ == other.custom_scale_ && |
| 166 custom_data_ == other.custom_data_; | 149 custom_data_ == other.custom_data_; |
| 167 } | 150 } |
| 168 | 151 |
| 169 #if defined(OS_WIN) | |
| 170 | |
| 171 static WebCursorInfo::Type ToCursorType(HCURSOR cursor) { | |
| 172 static struct { | |
| 173 HCURSOR cursor; | |
| 174 WebCursorInfo::Type type; | |
| 175 } kStandardCursors[] = { | |
| 176 { LoadCursor(NULL, IDC_ARROW), WebCursorInfo::TypePointer }, | |
| 177 { LoadCursor(NULL, IDC_CROSS), WebCursorInfo::TypeCross }, | |
| 178 { LoadCursor(NULL, IDC_HAND), WebCursorInfo::TypeHand }, | |
| 179 { LoadCursor(NULL, IDC_IBEAM), WebCursorInfo::TypeIBeam }, | |
| 180 { LoadCursor(NULL, IDC_WAIT), WebCursorInfo::TypeWait }, | |
| 181 { LoadCursor(NULL, IDC_HELP), WebCursorInfo::TypeHelp }, | |
| 182 { LoadCursor(NULL, IDC_SIZENESW), WebCursorInfo::TypeNorthEastResize }, | |
| 183 { LoadCursor(NULL, IDC_SIZENWSE), WebCursorInfo::TypeNorthWestResize }, | |
| 184 { LoadCursor(NULL, IDC_SIZENS), WebCursorInfo::TypeNorthSouthResize }, | |
| 185 { LoadCursor(NULL, IDC_SIZEWE), WebCursorInfo::TypeEastWestResize }, | |
| 186 { LoadCursor(NULL, IDC_SIZEALL), WebCursorInfo::TypeMove }, | |
| 187 { LoadCursor(NULL, IDC_APPSTARTING), WebCursorInfo::TypeProgress }, | |
| 188 { LoadCursor(NULL, IDC_NO), WebCursorInfo::TypeNotAllowed }, | |
| 189 }; | |
| 190 for (const auto& kStandardCursor : kStandardCursors) { | |
| 191 if (cursor == kStandardCursor.cursor) | |
| 192 return kStandardCursor.type; | |
| 193 } | |
| 194 return WebCursorInfo::TypeCustom; | |
| 195 } | |
| 196 | |
| 197 void WebCursor::InitFromExternalCursor(HCURSOR cursor) { | |
| 198 WebCursorInfo::Type cursor_type = ToCursorType(cursor); | |
| 199 | |
| 200 InitFromCursorInfo(CursorInfo(cursor_type)); | |
| 201 | |
| 202 if (cursor_type == WebCursorInfo::TypeCustom) | |
| 203 external_cursor_ = cursor; | |
| 204 } | |
| 205 | |
| 206 #endif // defined(OS_WIN) | |
| 207 | |
| 208 void WebCursor::Clear() { | 152 void WebCursor::Clear() { |
| 209 type_ = WebCursorInfo::TypePointer; | 153 type_ = WebCursorInfo::TypePointer; |
| 210 hotspot_.set_x(0); | 154 hotspot_.set_x(0); |
| 211 hotspot_.set_y(0); | 155 hotspot_.set_y(0); |
| 212 custom_size_.set_width(0); | 156 custom_size_.set_width(0); |
| 213 custom_size_.set_height(0); | 157 custom_size_.set_height(0); |
| 214 custom_scale_ = 1; | 158 custom_scale_ = 1; |
| 215 custom_data_.clear(); | 159 custom_data_.clear(); |
| 216 CleanupPlatformData(); | 160 CleanupPlatformData(); |
| 217 } | 161 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 return; | 207 return; |
| 264 | 208 |
| 265 // Clamp the hotspot to the custom image's dimensions. | 209 // Clamp the hotspot to the custom image's dimensions. |
| 266 hotspot_.set_x(std::max(0, | 210 hotspot_.set_x(std::max(0, |
| 267 std::min(custom_size_.width() - 1, hotspot_.x()))); | 211 std::min(custom_size_.width() - 1, hotspot_.x()))); |
| 268 hotspot_.set_y(std::max(0, | 212 hotspot_.set_y(std::max(0, |
| 269 std::min(custom_size_.height() - 1, hotspot_.y()))); | 213 std::min(custom_size_.height() - 1, hotspot_.y()))); |
| 270 } | 214 } |
| 271 | 215 |
| 272 } // namespace content | 216 } // namespace content |
| OLD | NEW |