| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "webkit/tools/test_shell/webwidget_host.h" | 7 #include "webkit/tools/test_shell/webwidget_host.h" |
| 8 | 8 |
| 9 #include "base/gfx/platform_canvas.h" | 9 #include "base/gfx/platform_canvas.h" |
| 10 #include "base/gfx/rect.h" | 10 #include "base/gfx/rect.h" |
| 11 #include "base/gfx/size.h" | 11 #include "base/gfx/size.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "webkit/api/public/mac/WebInputEventFactory.h" | 13 #include "webkit/api/public/mac/WebInputEventFactory.h" |
| 14 #include "webkit/api/public/mac/WebScreenInfoFactory.h" | 14 #include "webkit/api/public/mac/WebScreenInfoFactory.h" |
| 15 #include "webkit/api/public/WebInputEvent.h" | 15 #include "webkit/api/public/WebInputEvent.h" |
| 16 #include "webkit/api/public/WebPopupMenu.h" |
| 16 #include "webkit/api/public/WebScreenInfo.h" | 17 #include "webkit/api/public/WebScreenInfo.h" |
| 17 #include "webkit/api/public/WebSize.h" | 18 #include "webkit/api/public/WebSize.h" |
| 18 #include "webkit/glue/webwidget.h" | |
| 19 #include "webkit/tools/test_shell/test_shell.h" | 19 #include "webkit/tools/test_shell/test_shell.h" |
| 20 | 20 |
| 21 using WebKit::WebInputEvent; | 21 using WebKit::WebInputEvent; |
| 22 using WebKit::WebInputEventFactory; | 22 using WebKit::WebInputEventFactory; |
| 23 using WebKit::WebKeyboardEvent; | 23 using WebKit::WebKeyboardEvent; |
| 24 using WebKit::WebMouseEvent; | 24 using WebKit::WebMouseEvent; |
| 25 using WebKit::WebMouseWheelEvent; | 25 using WebKit::WebMouseWheelEvent; |
| 26 using WebKit::WebPopupMenu; |
| 26 using WebKit::WebScreenInfo; | 27 using WebKit::WebScreenInfo; |
| 27 using WebKit::WebScreenInfoFactory; | 28 using WebKit::WebScreenInfoFactory; |
| 28 using WebKit::WebSize; | 29 using WebKit::WebSize; |
| 30 using WebKit::WebWidgetClient; |
| 29 | 31 |
| 30 /*static*/ | 32 /*static*/ |
| 31 WebWidgetHost* WebWidgetHost::Create(NSView* parent_view, | 33 WebWidgetHost* WebWidgetHost::Create(NSView* parent_view, |
| 32 WebWidgetDelegate* delegate) { | 34 WebWidgetClient* client) { |
| 33 WebWidgetHost* host = new WebWidgetHost(); | 35 WebWidgetHost* host = new WebWidgetHost(); |
| 34 | 36 |
| 35 NSRect content_rect = [parent_view frame]; | 37 NSRect content_rect = [parent_view frame]; |
| 36 content_rect.origin.y += 64; | 38 content_rect.origin.y += 64; |
| 37 content_rect.size.height -= 64; | 39 content_rect.size.height -= 64; |
| 38 host->view_ = [[NSView alloc] initWithFrame:content_rect]; | 40 host->view_ = [[NSView alloc] initWithFrame:content_rect]; |
| 39 [parent_view addSubview:host->view_]; | 41 [parent_view addSubview:host->view_]; |
| 40 | 42 |
| 41 // win_util::SetWindowUserData(host->hwnd_, host); | 43 // win_util::SetWindowUserData(host->hwnd_, host); |
| 42 | 44 |
| 43 host->webwidget_ = WebWidget::Create(delegate); | 45 host->webwidget_ = WebPopupMenu::create(client); |
| 44 host->webwidget_->Resize(WebSize(content_rect.size.width, | 46 host->webwidget_->resize(WebSize(content_rect.size.width, |
| 45 content_rect.size.height)); | 47 content_rect.size.height)); |
| 46 return host; | 48 return host; |
| 47 } | 49 } |
| 48 | 50 |
| 49 /*static*/ | 51 /*static*/ |
| 50 void WebWidgetHost::HandleEvent(NSView* view, NSEvent* event) { | 52 void WebWidgetHost::HandleEvent(NSView* view, NSEvent* event) { |
| 51 /* TODO(port): rig up a way to get to the host */ | 53 /* TODO(port): rig up a way to get to the host */ |
| 52 WebWidgetHost* host = NULL; | 54 WebWidgetHost* host = NULL; |
| 53 if (host) { | 55 if (host) { |
| 54 switch ([event type]) { | 56 switch ([event type]) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 track_mouse_leave_(false) | 144 track_mouse_leave_(false) |
| 143 { | 145 { |
| 144 set_painting(false); | 146 set_painting(false); |
| 145 } | 147 } |
| 146 | 148 |
| 147 WebWidgetHost::~WebWidgetHost() { | 149 WebWidgetHost::~WebWidgetHost() { |
| 148 // win_util::SetWindowUserData(hwnd_, 0); | 150 // win_util::SetWindowUserData(hwnd_, 0); |
| 149 | 151 |
| 150 TrackMouseLeave(false); | 152 TrackMouseLeave(false); |
| 151 | 153 |
| 152 webwidget_->Close(); | 154 webwidget_->close(); |
| 153 } | 155 } |
| 154 | 156 |
| 155 void WebWidgetHost::UpdatePaintRect(const gfx::Rect& rect) { | 157 void WebWidgetHost::UpdatePaintRect(const gfx::Rect& rect) { |
| 156 paint_rect_ = paint_rect_.Union(rect); | 158 paint_rect_ = paint_rect_.Union(rect); |
| 157 } | 159 } |
| 158 | 160 |
| 159 void WebWidgetHost::Paint() { | 161 void WebWidgetHost::Paint() { |
| 160 NSRect r = [view_ frame]; | 162 NSRect r = [view_ frame]; |
| 161 gfx::Rect client_rect(NSRectToCGRect(r)); | 163 gfx::Rect client_rect(NSRectToCGRect(r)); |
| 162 NSGraphicsContext* view_context = [NSGraphicsContext currentContext]; | 164 NSGraphicsContext* view_context = [NSGraphicsContext currentContext]; |
| 163 CGContextRef context = static_cast<CGContextRef>([view_context graphicsPort]); | 165 CGContextRef context = static_cast<CGContextRef>([view_context graphicsPort]); |
| 164 | 166 |
| 165 // Allocate a canvas if necessary | 167 // Allocate a canvas if necessary |
| 166 if (!canvas_.get()) { | 168 if (!canvas_.get()) { |
| 167 ResetScrollRect(); | 169 ResetScrollRect(); |
| 168 paint_rect_ = client_rect; | 170 paint_rect_ = client_rect; |
| 169 canvas_.reset(new skia::PlatformCanvas( | 171 canvas_.reset(new skia::PlatformCanvas( |
| 170 paint_rect_.width(), paint_rect_.height(), true)); | 172 paint_rect_.width(), paint_rect_.height(), true)); |
| 171 } | 173 } |
| 172 | 174 |
| 173 // make sure webkit draws into our bitmap, not the window | 175 // make sure webkit draws into our bitmap, not the window |
| 174 CGContextRef bitmap_context = | 176 CGContextRef bitmap_context = |
| 175 canvas_->getTopPlatformDevice().GetBitmapContext(); | 177 canvas_->getTopPlatformDevice().GetBitmapContext(); |
| 176 [NSGraphicsContext setCurrentContext: | 178 [NSGraphicsContext setCurrentContext: |
| 177 [NSGraphicsContext graphicsContextWithGraphicsPort:bitmap_context | 179 [NSGraphicsContext graphicsContextWithGraphicsPort:bitmap_context |
| 178 flipped:NO]]; | 180 flipped:NO]]; |
| 179 | 181 |
| 180 // This may result in more invalidation | 182 // This may result in more invalidation |
| 181 webwidget_->Layout(); | 183 webwidget_->layout(); |
| 182 | 184 |
| 183 // Scroll the canvas if necessary | 185 // Scroll the canvas if necessary |
| 184 scroll_rect_ = client_rect.Intersect(scroll_rect_); | 186 scroll_rect_ = client_rect.Intersect(scroll_rect_); |
| 185 if (!scroll_rect_.IsEmpty()) { | 187 if (!scroll_rect_.IsEmpty()) { |
| 186 // add to invalidate rect, since there's no equivalent of ScrollDC. | 188 // add to invalidate rect, since there's no equivalent of ScrollDC. |
| 187 paint_rect_ = paint_rect_.Union(scroll_rect_); | 189 paint_rect_ = paint_rect_.Union(scroll_rect_); |
| 188 } | 190 } |
| 189 ResetScrollRect(); | 191 ResetScrollRect(); |
| 190 | 192 |
| 191 // Paint the canvas if necessary. Allow painting to generate extra rects the | 193 // Paint the canvas if necessary. Allow painting to generate extra rects the |
| (...skipping 28 matching lines...) Expand all Loading... |
| 220 } | 222 } |
| 221 } | 223 } |
| 222 | 224 |
| 223 WebScreenInfo WebWidgetHost::GetScreenInfo() { | 225 WebScreenInfo WebWidgetHost::GetScreenInfo() { |
| 224 return WebScreenInfoFactory::screenInfo(view_); | 226 return WebScreenInfoFactory::screenInfo(view_); |
| 225 } | 227 } |
| 226 | 228 |
| 227 void WebWidgetHost::Resize(const gfx::Rect& rect) { | 229 void WebWidgetHost::Resize(const gfx::Rect& rect) { |
| 228 // Force an entire re-paint. TODO(darin): Maybe reuse this memory buffer. | 230 // Force an entire re-paint. TODO(darin): Maybe reuse this memory buffer. |
| 229 DiscardBackingStore(); | 231 DiscardBackingStore(); |
| 230 webwidget_->Resize(WebSize(rect.width(), rect.height())); | 232 webwidget_->resize(WebSize(rect.width(), rect.height())); |
| 231 } | 233 } |
| 232 | 234 |
| 233 void WebWidgetHost::MouseEvent(NSEvent *event) { | 235 void WebWidgetHost::MouseEvent(NSEvent *event) { |
| 234 const WebMouseEvent& web_event = WebInputEventFactory::mouseEvent( | 236 const WebMouseEvent& web_event = WebInputEventFactory::mouseEvent( |
| 235 event, view_); | 237 event, view_); |
| 236 switch (web_event.type) { | 238 switch (web_event.type) { |
| 237 case WebInputEvent::MouseMove: | 239 case WebInputEvent::MouseMove: |
| 238 TrackMouseLeave(true); | 240 TrackMouseLeave(true); |
| 239 break; | 241 break; |
| 240 case WebInputEvent::MouseLeave: | 242 case WebInputEvent::MouseLeave: |
| 241 TrackMouseLeave(false); | 243 TrackMouseLeave(false); |
| 242 break; | 244 break; |
| 243 default: | 245 default: |
| 244 break; | 246 break; |
| 245 } | 247 } |
| 246 webwidget_->HandleInputEvent(&web_event); | 248 webwidget_->handleInputEvent(web_event); |
| 247 } | 249 } |
| 248 | 250 |
| 249 void WebWidgetHost::WheelEvent(NSEvent *event) { | 251 void WebWidgetHost::WheelEvent(NSEvent *event) { |
| 250 const WebMouseWheelEvent& web_event = WebInputEventFactory::mouseWheelEvent( | 252 webwidget_->handleInputEvent( |
| 251 event, view_); | 253 WebInputEventFactory::mouseWheelEvent(event, view_)); |
| 252 webwidget_->HandleInputEvent(&web_event); | |
| 253 } | 254 } |
| 254 | 255 |
| 255 void WebWidgetHost::KeyEvent(NSEvent *event) { | 256 void WebWidgetHost::KeyEvent(NSEvent *event) { |
| 256 const WebKeyboardEvent& web_event = WebInputEventFactory::keyboardEvent( | 257 webwidget_->handleInputEvent(WebInputEventFactory::keyboardEvent(event)); |
| 257 event); | |
| 258 webwidget_->HandleInputEvent(&web_event); | |
| 259 } | 258 } |
| 260 | 259 |
| 261 void WebWidgetHost::SetFocus(bool enable) { | 260 void WebWidgetHost::SetFocus(bool enable) { |
| 262 // Ignore focus calls in layout test mode so that tests don't mess with each | 261 // Ignore focus calls in layout test mode so that tests don't mess with each |
| 263 // other's focus when running in parallel. | 262 // other's focus when running in parallel. |
| 264 if (!TestShell::layout_test_mode()) | 263 if (!TestShell::layout_test_mode()) |
| 265 webwidget_->SetFocus(enable); | 264 webwidget_->setFocus(enable); |
| 266 } | 265 } |
| 267 | 266 |
| 268 void WebWidgetHost::TrackMouseLeave(bool track) { | 267 void WebWidgetHost::TrackMouseLeave(bool track) { |
| 269 } | 268 } |
| 270 | 269 |
| 271 void WebWidgetHost::ResetScrollRect() { | 270 void WebWidgetHost::ResetScrollRect() { |
| 272 scroll_rect_ = gfx::Rect(); | 271 scroll_rect_ = gfx::Rect(); |
| 273 scroll_dx_ = 0; | 272 scroll_dx_ = 0; |
| 274 scroll_dy_ = 0; | 273 scroll_dy_ = 0; |
| 275 } | 274 } |
| 276 | 275 |
| 277 void WebWidgetHost::PaintRect(const gfx::Rect& rect) { | 276 void WebWidgetHost::PaintRect(const gfx::Rect& rect) { |
| 278 #ifndef NDEBUG | 277 #ifndef NDEBUG |
| 279 DCHECK(!painting_); | 278 DCHECK(!painting_); |
| 280 #endif | 279 #endif |
| 281 DCHECK(canvas_.get()); | 280 DCHECK(canvas_.get()); |
| 282 | 281 |
| 283 set_painting(true); | 282 set_painting(true); |
| 284 webwidget_->Paint(canvas_.get(), rect); | 283 webwidget_->paint(canvas_.get(), rect); |
| 285 set_painting(false); | 284 set_painting(false); |
| 286 } | 285 } |
| OLD | NEW |