1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import "CSystemColors.h"
  27 
  28 #import "java_awt_SystemColor.h"
  29 #import "sun_lwawt_macosx_LWCToolkit.h"
  30 
  31 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  32 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  33 
  34 #import "ThreadUtilities.h"
  35 
  36 NSColor **sColors = nil;
  37 NSColor **appleColors = nil;
  38 
  39 @implementation CSystemColors
  40 
  41 + (void)initialize {
  42     JNIEnv *env = [ThreadUtilities getJNIEnv];
  43 JNF_COCOA_ENTER(env);
  44     [CSystemColors reloadColors];
  45     [[NSNotificationCenter defaultCenter] addObserver:[CSystemColors class] selector:@selector(systemColorsDidChange:) name:NSSystemColorsDidChangeNotification object:nil];
  46 JNF_COCOA_EXIT(env);
  47 }
  48 
  49 static JNF_CLASS_CACHE(jc_LWCToolkit, "sun/lwawt/macosx/LWCToolkit");
  50 static JNF_STATIC_MEMBER_CACHE(jm_systemColorsChanged, jc_LWCToolkit, "systemColorsChanged", "()V");
  51 
  52 + (void)systemColorsDidChange:(NSNotification *)notification {
  53     AWT_ASSERT_APPKIT_THREAD;
  54 
  55     [CSystemColors reloadColors];
  56 
  57     // Call LWCToolkit with the news. LWCToolkit makes certain to do its duties
  58     // from a new thread.
  59     JNIEnv* env = [ThreadUtilities getJNIEnv];
  60     JNFCallStaticVoidMethod(env, jm_systemColorsChanged); // AWT_THREADING Safe (event)
  61 }
  62 
  63 
  64 + (void)reloadColors {
  65     // NOTE: <rdar://problem/3447825> was filed to make this code even lazier. Each
  66     //  color below could be set lazily when it was first accessed. This way the
  67     //  arrays would be sparse, and filled in as SystemColors were used.
  68     int i;
  69     if (sColors == nil) {
  70         sColors = (NSColor**)malloc(sizeof(NSColor*) * java_awt_SystemColor_NUM_COLORS);
  71     } else {
  72         for (i = 0; i < java_awt_SystemColor_NUM_COLORS; i++) {
  73             if (sColors[i] != NULL) [sColors[i] release];
  74         }
  75     }
  76 
  77     sColors[java_awt_SystemColor_DESKTOP] =                    [NSColor greenColor];
  78     sColors[java_awt_SystemColor_ACTIVE_CAPTION] =            [NSColor whiteColor];
  79     sColors[java_awt_SystemColor_ACTIVE_CAPTION_TEXT] =        [NSColor blackColor];
  80     sColors[java_awt_SystemColor_ACTIVE_CAPTION_BORDER] =    [NSColor whiteColor];
  81     sColors[java_awt_SystemColor_INACTIVE_CAPTION] =        [NSColor grayColor];
  82     sColors[java_awt_SystemColor_INACTIVE_CAPTION_TEXT] =    [NSColor grayColor];
  83     sColors[java_awt_SystemColor_INACTIVE_CAPTION_BORDER] =    [NSColor grayColor];
  84     const CGFloat color = (CGFloat)0xEE/(CGFloat)0xFF;
  85     sColors[java_awt_SystemColor_WINDOW] = [NSColor colorWithCalibratedRed:color green:color blue:color alpha:1.0f];
  86     sColors[java_awt_SystemColor_WINDOW_BORDER] =            [NSColor windowFrameColor];
  87     sColors[java_awt_SystemColor_WINDOW_TEXT] =                [NSColor windowFrameTextColor];
  88     sColors[java_awt_SystemColor_MENU] =                    [NSColor controlBackgroundColor];
  89     sColors[java_awt_SystemColor_MENU_TEXT] =                [NSColor controlTextColor];
  90     sColors[java_awt_SystemColor_TEXT] =                    [NSColor textBackgroundColor];
  91     sColors[java_awt_SystemColor_TEXT_TEXT] =                [NSColor textColor];
  92     sColors[java_awt_SystemColor_TEXT_HIGHLIGHT] =            [NSColor selectedTextBackgroundColor];
  93     sColors[java_awt_SystemColor_TEXT_HIGHLIGHT_TEXT] =        [NSColor selectedTextColor];
  94     sColors[java_awt_SystemColor_TEXT_INACTIVE_TEXT] =        [NSColor disabledControlTextColor];
  95     sColors[java_awt_SystemColor_CONTROL] =                    [NSColor controlColor];
  96     sColors[java_awt_SystemColor_CONTROL_TEXT] =            [NSColor controlTextColor];
  97     sColors[java_awt_SystemColor_CONTROL_HIGHLIGHT] =        [NSColor alternateSelectedControlColor];
  98     sColors[java_awt_SystemColor_CONTROL_LT_HIGHLIGHT] =    [NSColor alternateSelectedControlTextColor];
  99     sColors[java_awt_SystemColor_CONTROL_SHADOW] =            [NSColor controlShadowColor];
 100     sColors[java_awt_SystemColor_CONTROL_DK_SHADOW] =        [NSColor controlDarkShadowColor];
 101     sColors[java_awt_SystemColor_SCROLLBAR] =                [NSColor scrollBarColor];
 102     sColors[java_awt_SystemColor_INFO] =                    [NSColor textBackgroundColor];
 103     sColors[java_awt_SystemColor_INFO_TEXT] =                [NSColor textColor];
 104 
 105     for (i = 0; i < java_awt_SystemColor_NUM_COLORS; i++) {
 106         [sColors[i] retain];
 107     }
 108 
 109     if (appleColors == nil) {
 110         appleColors = (NSColor**)malloc(sizeof(NSColor*) * sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS);
 111     } else {
 112         for (i = 0; i < sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS; i++) {
 113             if (appleColors[i] != NULL) [appleColors[i] release];
 114         }
 115     }
 116 
 117     appleColors[sun_lwawt_macosx_LWCToolkit_KEYBOARD_FOCUS_COLOR] =                    [NSColor keyboardFocusIndicatorColor];
 118     appleColors[sun_lwawt_macosx_LWCToolkit_INACTIVE_SELECTION_BACKGROUND_COLOR] =    [NSColor secondarySelectedControlColor];
 119     appleColors[sun_lwawt_macosx_LWCToolkit_INACTIVE_SELECTION_FOREGROUND_COLOR] =    [NSColor controlDarkShadowColor];
 120 
 121     for (i = 0; i < sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS; i++) {
 122         [appleColors[i] retain];
 123     }
 124 }
 125 
 126 + (NSColor*)getColor:(NSUInteger)colorIndex useAppleColor:(BOOL)useAppleColor {
 127     NSColor* result = nil;
 128 
 129     if (colorIndex < (useAppleColor) ? sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS : java_awt_SystemColor_NUM_COLORS) {
 130         result = (useAppleColor ? appleColors : sColors)[colorIndex];
 131     }
 132     else {
 133         NSLog(@"%s: %s %sColor: %ld not found, returning black.", __FILE__, __FUNCTION__, (useAppleColor) ? "Apple" : "System", colorIndex);
 134         result = [NSColor blackColor];
 135     }
 136 
 137     return result;
 138 }
 139 @end