📄 renderthemechromiummac.mm
字号:
/* * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. * Copyright (C) 2008, 2009 Google, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */// FIXME: we still need to figure out if passing a null view to the cell// drawing routines will work. I expect not, and if that's the case we'll have// to figure out something else. For now, at least leave the lines commented// in, but the procurement of the view if 0'd.#import "config.h"#import "RenderThemeChromiumMac.h"#import <Carbon/Carbon.h>#import <Cocoa/Cocoa.h>#import <math.h>#import "BitmapImage.h"#import "CSSStyleSelector.h"#import "CSSValueKeywords.h"#import "Document.h"#import "Element.h"#import "FoundationExtras.h"#import "FrameView.h"#import "GraphicsContext.h"#import "HTMLInputElement.h"#import "HTMLMediaElement.h"#import "HTMLNames.h"#import "Image.h"#import "LocalCurrentGraphicsContext.h"#import "MediaControlElements.h"#import "RenderSlider.h"#import "RenderView.h"#import "SharedBuffer.h"#import "WebCoreSystemInterface.h"#import <wtf/RetainPtr.h>#ifdef BUILDING_ON_TIGERtypedef int NSInteger;typedef unsigned NSUInteger;#endifusing std::min;// The methods in this file are specific to the Mac OS X platform.// FIXME: The platform-independent code in this class should be factored out and merged with RenderThemeSafari. @interface WebCoreRenderThemeNotificationObserver : NSObject{ WebCore::RenderTheme *_theme;}- (id)initWithTheme:(WebCore::RenderTheme *)theme;- (void)systemColorsDidChange:(NSNotification *)notification;@end@implementation WebCoreRenderThemeNotificationObserver- (id)initWithTheme:(WebCore::RenderTheme *)theme{ [super init]; _theme = theme; return self;}- (void)systemColorsDidChange:(NSNotification *)notification{ ASSERT([[notification name] isEqualToString:NSSystemColorsDidChangeNotification]); _theme->platformColorsDidChange();}@endnamespace WebCore {using namespace HTMLNames;enum { TopMargin, RightMargin, BottomMargin, LeftMargin};enum { TopPadding, RightPadding, BottomPadding, LeftPadding};// In our Mac port, we don't define PLATFORM(MAC) and thus don't pick up the// |operator NSRect()| on WebCore::IntRect and FloatRect. This substitues for// that missing conversion operator.NSRect IntRectToNSRect(const IntRect & rect){ return NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());}NSRect FloatRectToNSRect(const FloatRect & rect){ return NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());}IntRect NSRectToIntRect(const NSRect & rect){ return IntRect(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);}RenderTheme* theme(){ static RenderThemeChromiumMac* macTheme = new RenderThemeChromiumMac; return macTheme;}RenderThemeChromiumMac::RenderThemeChromiumMac() : m_isSliderThumbHorizontalPressed(false) , m_isSliderThumbVerticalPressed(false) , m_notificationObserver(AdoptNS, [[WebCoreRenderThemeNotificationObserver alloc] initWithTheme:this]){ [[NSNotificationCenter defaultCenter] addObserver:m_notificationObserver.get() selector:@selector(systemColorsDidChange:) name:NSSystemColorsDidChangeNotification object:nil];}RenderThemeChromiumMac::~RenderThemeChromiumMac(){ [[NSNotificationCenter defaultCenter] removeObserver:m_notificationObserver.get()];}Color RenderThemeChromiumMac::platformActiveSelectionBackgroundColor() const{ NSColor* color = [[NSColor selectedTextBackgroundColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace]; return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));}Color RenderThemeChromiumMac::platformInactiveSelectionBackgroundColor() const{ NSColor* color = [[NSColor secondarySelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace]; return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));}Color RenderThemeChromiumMac::activeListBoxSelectionBackgroundColor() const{ NSColor* color = [[NSColor alternateSelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace]; return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));}static FontWeight toFontWeight(NSInteger appKitFontWeight){ ASSERT(appKitFontWeight > 0 && appKitFontWeight < 15); if (appKitFontWeight > 14) appKitFontWeight = 14; else if (appKitFontWeight < 1) appKitFontWeight = 1; static FontWeight fontWeights[] = { FontWeight100, FontWeight100, FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600, FontWeight600, FontWeight700, FontWeight800, FontWeight800, FontWeight900, FontWeight900, FontWeight900 }; return fontWeights[appKitFontWeight - 1];}void RenderThemeChromiumMac::systemFont(int cssValueId, Document* document, FontDescription& fontDescription) const{ static FontDescription systemFont; static FontDescription smallSystemFont; static FontDescription menuFont; static FontDescription labelFont; static FontDescription miniControlFont; static FontDescription smallControlFont; static FontDescription controlFont; FontDescription* cachedDesc; NSFont* font = nil; switch (cssValueId) { case CSSValueSmallCaption: cachedDesc = &smallSystemFont; if (!smallSystemFont.isAbsoluteSize()) font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; break; case CSSValueMenu: cachedDesc = &menuFont; if (!menuFont.isAbsoluteSize()) font = [NSFont menuFontOfSize:[NSFont systemFontSize]]; break; case CSSValueStatusBar: cachedDesc = &labelFont; if (!labelFont.isAbsoluteSize()) font = [NSFont labelFontOfSize:[NSFont labelFontSize]]; break; case CSSValueWebkitMiniControl: cachedDesc = &miniControlFont; if (!miniControlFont.isAbsoluteSize()) font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]]; break; case CSSValueWebkitSmallControl: cachedDesc = &smallControlFont; if (!smallControlFont.isAbsoluteSize()) font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]; break; case CSSValueWebkitControl: cachedDesc = &controlFont; if (!controlFont.isAbsoluteSize()) font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]; break; default: cachedDesc = &systemFont; if (!systemFont.isAbsoluteSize()) font = [NSFont systemFontOfSize:[NSFont systemFontSize]]; } if (font) { NSFontManager *fontManager = [NSFontManager sharedFontManager]; cachedDesc->setIsAbsoluteSize(true); cachedDesc->setGenericFamily(FontDescription::NoFamily); cachedDesc->firstFamily().setFamily([font familyName]); cachedDesc->setSpecifiedSize([font pointSize]); cachedDesc->setWeight(toFontWeight([fontManager weightOfFont:font])); cachedDesc->setItalic([fontManager traitsOfFont:font] & NSItalicFontMask); } fontDescription = *cachedDesc;}static RGBA32 convertNSColorToColor(NSColor *color){ NSColor *colorInColorSpace = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; if (colorInColorSpace) { static const double scaleFactor = nextafter(256.0, 0.0); return makeRGB(static_cast<int>(scaleFactor * [colorInColorSpace redComponent]), static_cast<int>(scaleFactor * [colorInColorSpace greenComponent]), static_cast<int>(scaleFactor * [colorInColorSpace blueComponent])); } // This conversion above can fail if the NSColor in question is an NSPatternColor // (as many system colors are). These colors are actually a repeating pattern // not just a solid color. To work around this we simply draw a 1x1 image of // the color and use that pixel's color. It might be better to use an average of // the colors in the pattern instead. NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil pixelsWide:1 pixelsHigh:1 bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:4 bitsPerPixel:32]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep]]; NSEraseRect(NSMakeRect(0, 0, 1, 1)); [color drawSwatchInRect:NSMakeRect(0, 0, 1, 1)]; [NSGraphicsContext restoreGraphicsState]; NSUInteger pixel[4]; [offscreenRep getPixel:pixel atX:0 y:0]; [offscreenRep release]; return makeRGB(pixel[0], pixel[1], pixel[2]);}static RGBA32 menuBackgroundColor(){ NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil pixelsWide:1 pixelsHigh:1 bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:4 bitsPerPixel:32]; CGContextRef context = static_cast<CGContextRef>([[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep] graphicsPort]); CGRect rect = CGRectMake(0, 0, 1, 1); HIThemeMenuDrawInfo drawInfo; drawInfo.version = 0; drawInfo.menuType = kThemeMenuTypePopUp; HIThemeDrawMenuBackground(&rect, &drawInfo, context, kHIThemeOrientationInverted); NSUInteger pixel[4]; [offscreenRep getPixel:pixel atX:0 y:0]; [offscreenRep release]; return makeRGB(pixel[0], pixel[1], pixel[2]);}void RenderThemeChromiumMac::platformColorsDidChange(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -