| 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 #import "test_shell_webview.h" | |
| 6 | |
| 7 #import <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 13 #include "ui/gfx/rect.h" | |
| 14 #include "webkit/tools/test_shell/test_shell.h" | |
| 15 #include "webkit/tools/test_shell/webwidget_host.h" | |
| 16 | |
| 17 @implementation TestShellWebView | |
| 18 | |
| 19 @synthesize shell = shell_; | |
| 20 | |
| 21 - (id)initWithFrame:(NSRect)frame { | |
| 22 self = [super initWithFrame:frame]; | |
| 23 if (self) { | |
| 24 trackingArea_ = | |
| 25 [[NSTrackingArea alloc] initWithRect:frame | |
| 26 options:NSTrackingMouseMoved | | |
| 27 NSTrackingActiveInActiveApp | | |
| 28 NSTrackingInVisibleRect | |
| 29 owner:self | |
| 30 userInfo:nil]; | |
| 31 [self addTrackingArea:trackingArea_]; | |
| 32 } | |
| 33 return self; | |
| 34 } | |
| 35 | |
| 36 - (void) dealloc { | |
| 37 [self removeTrackingArea:trackingArea_]; | |
| 38 [trackingArea_ release]; | |
| 39 | |
| 40 [super dealloc]; | |
| 41 } | |
| 42 | |
| 43 - (void)drawRect:(NSRect)rect { | |
| 44 CGContextRef context = | |
| 45 reinterpret_cast<CGContextRef>([[NSGraphicsContext currentContext] | |
| 46 graphicsPort]); | |
| 47 | |
| 48 // start by filling the rect with magenta, so that we can see what's drawn | |
| 49 CGContextSetRGBFillColor (context, 1, 0, 1, 1); | |
| 50 CGContextFillRect(context, NSRectToCGRect(rect)); | |
| 51 | |
| 52 if (shell_ && shell_->webView()) { | |
| 53 gfx::Rect client_rect(NSRectToCGRect(rect)); | |
| 54 // flip from cocoa coordinates | |
| 55 client_rect.set_y([self frame].size.height - | |
| 56 client_rect.height() - client_rect.y()); | |
| 57 | |
| 58 shell_->webViewHost()->UpdatePaintRect(client_rect); | |
| 59 shell_->webViewHost()->Paint(); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 - (IBAction)goBack:(id)sender { | |
| 64 if (shell_) | |
| 65 shell_->GoBackOrForward(-1); | |
| 66 } | |
| 67 | |
| 68 - (IBAction)goForward:(id)sender { | |
| 69 if (shell_) | |
| 70 shell_->GoBackOrForward(1); | |
| 71 } | |
| 72 | |
| 73 - (IBAction)reload:(id)sender { | |
| 74 if (shell_) | |
| 75 shell_->Reload(); | |
| 76 } | |
| 77 | |
| 78 - (IBAction)stopLoading:(id)sender { | |
| 79 if (shell_ && shell_->webView()) | |
| 80 shell_->webView()->mainFrame()->stopLoading(); | |
| 81 } | |
| 82 | |
| 83 - (IBAction)takeURLStringValueFrom:(NSTextField *)sender { | |
| 84 NSString *url = [sender stringValue]; | |
| 85 | |
| 86 // if it doesn't already have a prefix, add http. If we can't parse it, | |
| 87 // just don't bother rather than making things worse. | |
| 88 NSURL* tempUrl = [NSURL URLWithString:url]; | |
| 89 if (tempUrl && ![tempUrl scheme]) | |
| 90 url = [@"http://" stringByAppendingString:url]; | |
| 91 shell_->LoadURL(GURL(std::string([url UTF8String]))); | |
| 92 } | |
| 93 | |
| 94 - (void)mouseDown:(NSEvent *)theEvent { | |
| 95 if (shell_ && shell_->webView()) | |
| 96 shell_->webViewHost()->MouseEvent(theEvent); | |
| 97 } | |
| 98 | |
| 99 - (void)rightMouseDown:(NSEvent *)theEvent { | |
| 100 if (shell_ && shell_->webView()) | |
| 101 shell_->webViewHost()->MouseEvent(theEvent); | |
| 102 } | |
| 103 | |
| 104 - (void)otherMouseDown:(NSEvent *)theEvent { | |
| 105 if (shell_ && shell_->webView()) | |
| 106 shell_->webViewHost()->MouseEvent(theEvent); | |
| 107 } | |
| 108 | |
| 109 - (void)mouseUp:(NSEvent *)theEvent { | |
| 110 if (shell_ && shell_->webView()) | |
| 111 shell_->webViewHost()->MouseEvent(theEvent); | |
| 112 } | |
| 113 | |
| 114 - (void)rightMouseUp:(NSEvent *)theEvent { | |
| 115 if (shell_ && shell_->webView()) | |
| 116 shell_->webViewHost()->MouseEvent(theEvent); | |
| 117 } | |
| 118 | |
| 119 - (void)otherMouseUp:(NSEvent *)theEvent { | |
| 120 if (shell_ && shell_->webView()) | |
| 121 shell_->webViewHost()->MouseEvent(theEvent); | |
| 122 } | |
| 123 | |
| 124 - (void)mouseMoved:(NSEvent *)theEvent { | |
| 125 if (shell_ && shell_->webView()) | |
| 126 shell_->webViewHost()->MouseEvent(theEvent); | |
| 127 } | |
| 128 | |
| 129 - (void)mouseDragged:(NSEvent *)theEvent { | |
| 130 if (shell_ && shell_->webView()) | |
| 131 shell_->webViewHost()->MouseEvent(theEvent); | |
| 132 } | |
| 133 | |
| 134 - (void)scrollWheel:(NSEvent *)theEvent { | |
| 135 if (shell_ && shell_->webView()) | |
| 136 shell_->webViewHost()->WheelEvent(theEvent); | |
| 137 } | |
| 138 | |
| 139 - (void)rightMouseDragged:(NSEvent *)theEvent { | |
| 140 if (shell_ && shell_->webView()) | |
| 141 shell_->webViewHost()->MouseEvent(theEvent); | |
| 142 } | |
| 143 | |
| 144 - (void)otherMouseDragged:(NSEvent *)theEvent { | |
| 145 if (shell_ && shell_->webView()) | |
| 146 shell_->webViewHost()->MouseEvent(theEvent); | |
| 147 } | |
| 148 | |
| 149 - (void)mouseEntered:(NSEvent *)theEvent { | |
| 150 if (shell_ && shell_->webView()) | |
| 151 shell_->webViewHost()->MouseEvent(theEvent); | |
| 152 } | |
| 153 | |
| 154 - (void)mouseExited:(NSEvent *)theEvent { | |
| 155 if (shell_ && shell_->webView()) | |
| 156 shell_->webViewHost()->MouseEvent(theEvent); | |
| 157 } | |
| 158 | |
| 159 - (void)keyDown:(NSEvent *)theEvent { | |
| 160 if (shell_ && shell_->webView()) | |
| 161 shell_->webViewHost()->KeyEvent(theEvent); | |
| 162 } | |
| 163 | |
| 164 - (void)keyUp:(NSEvent *)theEvent { | |
| 165 if (shell_ && shell_->webView()) | |
| 166 shell_->webViewHost()->KeyEvent(theEvent); | |
| 167 } | |
| 168 | |
| 169 - (BOOL)isOpaque { | |
| 170 return YES; | |
| 171 } | |
| 172 | |
| 173 - (BOOL)canBecomeKeyView { | |
| 174 return shell_ && shell_->webView(); | |
| 175 } | |
| 176 | |
| 177 - (BOOL)acceptsFirstResponder { | |
| 178 return shell_ && shell_->webView(); | |
| 179 } | |
| 180 | |
| 181 - (BOOL)becomeFirstResponder { | |
| 182 if (shell_ && shell_->webView()) { | |
| 183 shell_->webViewHost()->SetFocus(YES); | |
| 184 return YES; | |
| 185 } | |
| 186 | |
| 187 return NO; | |
| 188 } | |
| 189 | |
| 190 - (BOOL)resignFirstResponder { | |
| 191 if (shell_ && shell_->webView()) { | |
| 192 shell_->webViewHost()->SetFocus(NO); | |
| 193 return YES; | |
| 194 } | |
| 195 | |
| 196 return NO; | |
| 197 } | |
| 198 | |
| 199 - (void)setIsActive:(BOOL)active { | |
| 200 if (shell_ && shell_->webView()) | |
| 201 shell_->webViewHost()->SetIsActive(active ? true : false); | |
| 202 } | |
| 203 | |
| 204 - (void)setFrame:(NSRect)frameRect { | |
| 205 [super setFrame:frameRect]; | |
| 206 if (shell_ && shell_->webView()) | |
| 207 shell_->webViewHost()->Resize(gfx::Rect(NSRectToCGRect(frameRect))); | |
| 208 [self setNeedsDisplay:YES]; | |
| 209 } | |
| 210 | |
| 211 @end | |
| OLD | NEW |