⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 renderthememac.mm

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 MM
📖 第 1 页 / 共 5 页
字号:
/* * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. * * 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. */#import "config.h"#import "RenderThemeMac.h"#import "BitmapImage.h"#import "CSSStyleSelector.h"#import "CSSValueKeywords.h"#import "Document.h"#import "Element.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 "UserAgentStyleSheets.h"#import <Carbon/Carbon.h>#import <Cocoa/Cocoa.h>#import <wtf/RetainPtr.h>#import <wtf/StdLibExtras.h>#import <math.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 *)unusedNotification{    ASSERT_UNUSED(unusedNotification, [[unusedNotification name] isEqualToString:NSSystemColorsDidChangeNotification]);    _theme->platformColorsDidChange();}@endnamespace WebCore {using namespace HTMLNames;enum {    topMargin,    rightMargin,    bottomMargin,    leftMargin};enum {    topPadding,    rightPadding,    bottomPadding,    leftPadding};RenderTheme* theme(){    static RenderThemeMac* macTheme = new RenderThemeMac;    return macTheme;}RenderThemeMac::RenderThemeMac()    : 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];}RenderThemeMac::~RenderThemeMac(){    [[NSNotificationCenter defaultCenter] removeObserver:m_notificationObserver.get()];}Color RenderThemeMac::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 RenderThemeMac::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 RenderThemeMac::platformActiveListBoxSelectionBackgroundColor() 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]));}Color RenderThemeMac::platformActiveListBoxSelectionForegroundColor() const{    return Color::white;}Color RenderThemeMac::platformInactiveListBoxSelectionForegroundColor() const{    return Color::black;}Color RenderThemeMac::platformInactiveListBoxSelectionBackgroundColor() const{    return platformInactiveSelectionBackgroundColor();}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 RenderThemeMac::systemFont(int cssValueId, FontDescription& fontDescription) const{    DEFINE_STATIC_LOCAL(FontDescription, systemFont, ());    DEFINE_STATIC_LOCAL(FontDescription, smallSystemFont, ());    DEFINE_STATIC_LOCAL(FontDescription, menuFont, ());    DEFINE_STATIC_LOCAL(FontDescription, labelFont, ());    DEFINE_STATIC_LOCAL(FontDescription, miniControlFont, ());    DEFINE_STATIC_LOCAL(FontDescription, smallControlFont, ());    DEFINE_STATIC_LOCAL(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:NSDeviceRGBColorSpace];    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:NSDeviceRGBColorSpace                                                                            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:NSDeviceRGBColorSpace                                                                            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 RenderThemeMac::platformColorsDidChange(){    m_systemColorCache.clear();    RenderTheme::platformColorsDidChange();}Color RenderThemeMac::systemColor(int cssValueId) const{    if (m_systemColorCache.contains(cssValueId))        return m_systemColorCache.get(cssValueId);        Color color;    switch (cssValueId) {        case CSSValueActiveborder:            color = convertNSColorToColor([NSColor keyboardFocusIndicatorColor]);            break;        case CSSValueActivecaption:            color = convertNSColorToColor([NSColor windowFrameTextColor]);            break;        case CSSValueAppworkspace:            color = convertNSColorToColor([NSColor headerColor]);            break;        case CSSValueBackground:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -