Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: plugin/mac/overlay_window_mac.mm

Issue 2129015: Refactored full-screen code under FullscreenWindowMac base class and... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/o3d/
Patch Set: '' Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « plugin/mac/overlay_window_mac.h ('k') | plugin/mac/plugin_mac.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright 2010, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #import "overlay_window_mac.h"
33
34 #import "plugin/mac/graphics_utils_mac.h"
35
36 #ifdef O3D_PLUGIN_ENABLE_FULLSCREEN_MSG
37
38 #define kTransitionTime 1.0
39
40 namespace o3d {
41
42 // A little wrapper for ATSUSetAttributes to make calling it with one attribute
43 // less annoying.
44 static void MySetAttribute(ATSUStyle style,
45 ATSUAttributeTag tag,
46 ByteCount size,
47 ATSUAttributeValuePtr value) {
48
49 ATSUAttributeTag tags[2] = {tag, 0};
50 ByteCount sizes[2] = {size, 0};
51 ATSUAttributeValuePtr values[2] = {value, 0};
52
53 ATSUSetAttributes(style, 1, tags, sizes, values);
54 }
55
56 // A little wrapper for ATSUSetLayoutControls to make calling it with one
57 // attribute less annoying.
58 static void MySetLayoutControl(ATSUTextLayout layout,
59 ATSUAttributeTag tag,
60 ByteCount size,
61 ATSUAttributeValuePtr value) {
62
63 ATSUAttributeTag tags[2] = {tag, 0};
64 ByteCount sizes[2] = {size, 0};
65 ATSUAttributeValuePtr values[2] = {value, 0};
66
67 ATSUSetLayoutControls(layout, 1, tags, sizes, values);
68 }
69
70 // Returns the unicode 16 chars that we need to display as the fullscreen
71 // message. Should be disposed with free() after use.
72 static UniChar * GetFullscreenDisplayText(int *returned_length) {
73 // TODO this will need to be a localized string.
74 NSString* ns_display_text = @"Press ESC to exit fullscreen";
75 int count = [ns_display_text length];
76 UniChar* display_text_16 = (UniChar*) calloc(count, sizeof(UniChar));
77
78 [ns_display_text getCharacters:display_text_16];
79 *returned_length = count;
80 return display_text_16;
81 }
82
83
84 static void DrawToOverlayWindow(WindowRef overlayWindow) {
85 CGContextRef overlayContext = NULL;
86 CGFloat kWhiteOpaque[] = {1.0, 1.0, 1.0, 1.0};
87 CGFloat kBlackNotOpaque[] = {0.0, 0.0, 0.0, 0.5};
88 Rect bounds = {0, 0, 0, 0};
89 const char* kOverlayWindowFontName = "Arial";
90 const int kPointSize = 22;
91 const float kShadowRadius = 5.0;
92 const float kRoundRectRadius = 9.0;
93 const float kTextLeftMargin = 15.0;
94 const float kTextBottomMargin = 22.0;
95
96 QDBeginCGContext(GetWindowPort(overlayWindow), &overlayContext);
97 GetWindowBounds(overlayWindow, kWindowContentRgn, &bounds);
98
99 // Make the global rect local.
100 bounds.right -= bounds.left;
101 bounds.left = 0;
102 bounds.bottom -= bounds.top;
103 bounds.top = 0;
104
105 CGRect cgTotalRect = Rect2CGRect(bounds);
106 CGContextSetShouldSmoothFonts(overlayContext, true);
107 CGContextClearRect(overlayContext, cgTotalRect);
108
109 CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
110 CGColorRef shadow = CGColorCreate(myColorSpace, kBlackNotOpaque);
111 CGColorRef roundRectBackColor = CGColorCreate(myColorSpace, kBlackNotOpaque);
112 CGSize shadowOffset = {0.0,0.0};
113
114 CGContextSetFillColor(overlayContext, kWhiteOpaque);
115 CGContextSetStrokeColor(overlayContext, kWhiteOpaque);
116
117 // Draw the round rect background.
118 CGContextSaveGState(overlayContext);
119 CGContextSetFillColorWithColor(overlayContext, roundRectBackColor);
120 CGRect cg_rounded_area =
121 CGRectMake(// Offset from left and bottom to give shadow its space.
122 kShadowRadius, kShadowRadius,
123 // Increase width and height so rounded corners
124 // will be clipped out, except at bottom left.
125 (bounds.right - bounds.left) + 30,
126 (bounds.bottom - bounds.top) + 30);
127 // Save state before applying shadow.
128 CGContextSetShadowWithColor(overlayContext, shadowOffset,
129 kShadowRadius, shadow);
130 PaintRoundedCGRect(overlayContext, cg_rounded_area, kRoundRectRadius, true);
131 // Restore graphics state to remove shadow.
132 CGContextRestoreGState(overlayContext);
133
134 // Draw the text.
135 int text_length = 0;
136 UniChar* display_text = GetFullscreenDisplayText(&text_length);
137
138 if ((text_length > 0) && (display_text != NULL)) {
139 ATSUStyle style;
140 ATSUTextLayout layout;
141 ATSUFontID font;
142 Fixed pointSize = Long2Fix(kPointSize);
143 Boolean is_bold = true;
144
145 ATSUCreateStyle(&style);
146 ATSUFindFontFromName(kOverlayWindowFontName, strlen(kOverlayWindowFontName),
147 kFontFullName, kFontNoPlatformCode, kFontNoScriptCode,
148 kFontNoLanguageCode, &font);
149
150 MySetAttribute(style, kATSUFontTag, sizeof(font), &font);
151 MySetAttribute(style, kATSUSizeTag, sizeof(pointSize), &pointSize);
152 MySetAttribute(style, kATSUQDBoldfaceTag, sizeof(Boolean), &is_bold);
153
154
155 ATSUCreateTextLayout(&layout);
156 ATSUSetTextPointerLocation(layout, display_text,
157 kATSUFromTextBeginning, kATSUToTextEnd,
158 text_length);
159 ATSUSetRunStyle(layout, style, kATSUFromTextBeginning, kATSUToTextEnd);
160
161 MySetLayoutControl(layout, kATSUCGContextTag,
162 sizeof(CGContextRef), &overlayContext);
163
164 // Need to enable this for languages like Japanese to draw as something
165 // other than a series of squares.
166 ATSUSetTransientFontMatching(layout, true);
167
168
169 CGContextSetFillColor(overlayContext, kWhiteOpaque);
170 ATSUDrawText(layout, kATSUFromTextBeginning, kATSUToTextEnd,
171 X2Fix(kShadowRadius + kTextLeftMargin),
172 X2Fix(kShadowRadius + kTextBottomMargin));
173 ATSUDisposeStyle(style);
174 ATSUDisposeTextLayout(layout);
175 free(display_text);
176 }
177
178 CGColorRelease(roundRectBackColor);
179 CGColorRelease(shadow);
180 CGColorSpaceRelease (myColorSpace);
181
182 QDEndCGContext(GetWindowPort(overlayWindow), &overlayContext);
183 }
184
185 static OSStatus HandleOverlayWindow(EventHandlerCallRef inHandlerCallRef,
186 EventRef inEvent,
187 void *inUserData) {
188 OSType event_class = GetEventClass(inEvent);
189 OSType event_kind = GetEventKind(inEvent);
190
191 if (event_class == kEventClassWindow &&
192 event_kind == kEventWindowPaint) {
193 WindowRef theWindow = NULL;
194 GetEventParameter(inEvent, kEventParamDirectObject,
195 typeWindowRef, NULL,
196 sizeof(theWindow), NULL,
197 &theWindow);
198 if (theWindow) {
199 CallNextEventHandler(inHandlerCallRef, inEvent);
200 DrawToOverlayWindow(theWindow);
201 }
202 }
203
204 return noErr;
205 }
206
207
208
209 static Rect GetOverlayWindowRect(bool visible) {
210 #define kOverlayHeight 60
211 #define kOverlayWidth 340
212 Rect screen_bounds = CGRect2Rect(CGDisplayBounds(CGMainDisplayID()));
213 Rect hidden_window_bounds = {screen_bounds.top - kOverlayHeight,
214 screen_bounds.right - kOverlayWidth,
215 screen_bounds.top,
216 screen_bounds.right};
217 Rect visible_window_bounds = {screen_bounds.top,
218 screen_bounds.right - kOverlayWidth,
219 screen_bounds.top + kOverlayHeight,
220 screen_bounds.right};
221
222 return (visible) ? visible_window_bounds : hidden_window_bounds;
223 }
224
225
226 static WindowRef CreateOverlayWindow(void) {
227 Rect window_bounds = GetOverlayWindowRect(false);
228 WindowClass wClass = kOverlayWindowClass;
229 WindowRef window = NULL;
230 OSStatus err = noErr;
231 WindowAttributes overlayAttributes = kWindowNoShadowAttribute |
232 kWindowIgnoreClicksAttribute |
233 kWindowNoActivatesAttribute |
234 kWindowStandardHandlerAttribute;
235 EventTypeSpec eventTypes[] = {
236 {kEventClassWindow, kEventWindowPaint},
237 {kEventClassWindow, kEventWindowShown}
238 };
239
240 err = CreateNewWindow(wClass,
241 overlayAttributes,
242 &window_bounds,
243 &window);
244 if (err)
245 return NULL;
246
247 SetWindowLevel(window, CGShieldingWindowLevel() + 1);
248 InstallEventHandler(GetWindowEventTarget(window), HandleOverlayWindow,
249 sizeof(eventTypes)/sizeof(eventTypes[0]), eventTypes,
250 NULL, NULL);
251 return window;
252 }
253
254 OverlayWindowMac::OverlayWindowMac()
255 : overlay_window_(NULL),
256 time_to_hide_overlay_(0.0) {
257 overlay_window_ = CreateOverlayWindow();
258 ShowWindow(overlay_window_);
259 o3d::SlideWindowToRect(overlay_window_,
260 Rect2CGRect(GetOverlayWindowRect(true)),
261 kTransitionTime);
262
263 // Hide the overlay text 4 seconds from now.
264 time_to_hide_overlay_ = [NSDate timeIntervalSinceReferenceDate] + 4.0;
265 }
266
267 OverlayWindowMac::~OverlayWindowMac() {
268 HideWindow(overlay_window_);
269 ReleaseWindowGroup(GetWindowGroup(overlay_window_));
270 DisposeWindow(overlay_window_);
271 }
272
273 void OverlayWindowMac::IdleCallback() {
274 if ((overlay_window_ != NULL) &&
275 (time_to_hide_overlay_ != 0.0) &&
276 (time_to_hide_overlay_ < [NSDate timeIntervalSinceReferenceDate])) {
277 time_to_hide_overlay_ = 0.0;
278 SlideWindowToRect(overlay_window_,
279 Rect2CGRect(GetOverlayWindowRect(false)),
280 kTransitionTime);
281 }
282 }
283
284 } // namespace o3d
285
286 #endif // O3D_PLUGIN_ENABLE_FULLSCREEN_MSG
OLDNEW
« no previous file with comments | « plugin/mac/overlay_window_mac.h ('k') | plugin/mac/plugin_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698