rev 54094 : 8257853: Remove dependencies on JNF's JNI utility functions in AWT and 2D code
rev 54096 : 8259651: [macOS] Replace JNF_COCOA_ENTER/EXIT macros
rev 54098 : 8260616: Removing remaining JNF dependencies in the java.desktop module
8259729: Missed JNFInstanceOf -> IsInstanceOf conversion

   1 /*
   2  * Copyright (c) 2011, 2020, 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 <JavaRuntimeSupport/JavaRuntimeSupport.h>
  32 
  33 #import "ThreadUtilities.h"
  34 #import "JNIUtilities.h"
  35 
  36 NSColor **sColors = nil;
  37 NSColor **appleColors = nil;
  38 
  39 @implementation CSystemColors
  40 
  41 + (void)initialize {
  42     JNIEnv *env = [ThreadUtilities getJNIEnv];
  43 JNI_COCOA_ENTER(env);
  44     [CSystemColors reloadColors];
  45     [[NSNotificationCenter defaultCenter] addObserver:[CSystemColors class] selector:@selector(systemColorsDidChange:) name:NSSystemColorsDidChangeNotification object:nil];
  46 JNI_COCOA_EXIT(env);
  47 }
  48 


  49 
  50 + (void)systemColorsDidChange:(NSNotification *)notification {
  51     AWT_ASSERT_APPKIT_THREAD;
  52 
  53     [CSystemColors reloadColors];
  54 
  55     // Call LWCToolkit with the news. LWCToolkit makes certain to do its duties
  56     // from a new thread.
  57     JNIEnv* env = [ThreadUtilities getJNIEnv];
  58     DECLARE_CLASS(jc_LWCToolkit, "sun/lwawt/macosx/LWCToolkit");
  59     DECLARE_STATIC_METHOD(jm_systemColorsChanged, jc_LWCToolkit, "systemColorsChanged", "()V");
  60     (*env)->CallStaticVoidMethod(env, jc_LWCToolkit, jm_systemColorsChanged); // AWT_THREADING Safe (event)
  61     CHECK_EXCEPTION();
  62 
  63 }
  64 
  65 
  66 + (void)reloadColors {
  67     // NOTE: <rdar://problem/3447825> was filed to make this code even lazier. Each
  68     //  color below could be set lazily when it was first accessed. This way the
  69     //  arrays would be sparse, and filled in as SystemColors were used.
  70     int i;
  71     if (sColors == nil) {
  72         sColors = (NSColor**)malloc(sizeof(NSColor*) * java_awt_SystemColor_NUM_COLORS);
  73     } else {
  74         for (i = 0; i < java_awt_SystemColor_NUM_COLORS; i++) {
  75             if (sColors[i] != NULL) [sColors[i] release];
  76         }
  77     }
  78 
  79     sColors[java_awt_SystemColor_DESKTOP] =                    [NSColor greenColor];
  80     sColors[java_awt_SystemColor_ACTIVE_CAPTION] =            [NSColor whiteColor];
  81     sColors[java_awt_SystemColor_ACTIVE_CAPTION_TEXT] =        [NSColor blackColor];
  82     sColors[java_awt_SystemColor_ACTIVE_CAPTION_BORDER] =    [NSColor whiteColor];
  83     sColors[java_awt_SystemColor_INACTIVE_CAPTION] =        [NSColor grayColor];
  84     sColors[java_awt_SystemColor_INACTIVE_CAPTION_TEXT] =    [NSColor grayColor];
  85     sColors[java_awt_SystemColor_INACTIVE_CAPTION_BORDER] =    [NSColor grayColor];
  86     const CGFloat color = (CGFloat)0xEE/(CGFloat)0xFF;
  87     sColors[java_awt_SystemColor_WINDOW] = [NSColor colorWithCalibratedRed:color green:color blue:color alpha:1.0f];
  88     sColors[java_awt_SystemColor_WINDOW_BORDER] =            [NSColor windowFrameColor];
  89     sColors[java_awt_SystemColor_WINDOW_TEXT] =                [NSColor windowFrameTextColor];
  90     sColors[java_awt_SystemColor_MENU] =                    [NSColor controlBackgroundColor];
  91     sColors[java_awt_SystemColor_MENU_TEXT] =                [NSColor controlTextColor];
  92     sColors[java_awt_SystemColor_TEXT] =                    [NSColor textBackgroundColor];
  93     sColors[java_awt_SystemColor_TEXT_TEXT] =                [NSColor textColor];
  94     sColors[java_awt_SystemColor_TEXT_HIGHLIGHT] =            [NSColor selectedTextBackgroundColor];
  95     sColors[java_awt_SystemColor_TEXT_HIGHLIGHT_TEXT] =        [NSColor selectedTextColor];
  96     sColors[java_awt_SystemColor_TEXT_INACTIVE_TEXT] =        [NSColor disabledControlTextColor];
  97     sColors[java_awt_SystemColor_CONTROL] =                    [NSColor controlColor];
  98     sColors[java_awt_SystemColor_CONTROL_TEXT] =            [NSColor controlTextColor];
  99     sColors[java_awt_SystemColor_CONTROL_HIGHLIGHT] =        [NSColor alternateSelectedControlColor];
 100     sColors[java_awt_SystemColor_CONTROL_LT_HIGHLIGHT] =    [NSColor alternateSelectedControlTextColor];
 101     sColors[java_awt_SystemColor_CONTROL_SHADOW] =            [NSColor controlShadowColor];
 102     sColors[java_awt_SystemColor_CONTROL_DK_SHADOW] =        [NSColor controlDarkShadowColor];
 103     sColors[java_awt_SystemColor_SCROLLBAR] =                [NSColor scrollBarColor];
 104     sColors[java_awt_SystemColor_INFO] =                    [NSColor textBackgroundColor];
 105     sColors[java_awt_SystemColor_INFO_TEXT] =                [NSColor textColor];
 106 
 107     for (i = 0; i < java_awt_SystemColor_NUM_COLORS; i++) {
 108         [sColors[i] retain];
 109     }
 110 
 111     if (appleColors == nil) {
 112         appleColors = (NSColor**)malloc(sizeof(NSColor*) * sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS);
 113     } else {
 114         for (i = 0; i < sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS; i++) {
 115             if (appleColors[i] != NULL) [appleColors[i] release];
 116         }
 117     }
 118 
 119     appleColors[sun_lwawt_macosx_LWCToolkit_KEYBOARD_FOCUS_COLOR] =                    [NSColor keyboardFocusIndicatorColor];
 120     appleColors[sun_lwawt_macosx_LWCToolkit_INACTIVE_SELECTION_BACKGROUND_COLOR] =    [NSColor secondarySelectedControlColor];
 121     appleColors[sun_lwawt_macosx_LWCToolkit_INACTIVE_SELECTION_FOREGROUND_COLOR] =    [NSColor controlDarkShadowColor];
 122 
 123     for (i = 0; i < sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS; i++) {
 124         [appleColors[i] retain];
 125     }
 126 }
 127 
 128 + (NSColor*)getColor:(NSUInteger)colorIndex useAppleColor:(BOOL)useAppleColor {
 129     NSColor* result = nil;
 130 
 131     if (colorIndex < ((useAppleColor) ? sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS : java_awt_SystemColor_NUM_COLORS)) {
 132         result = (useAppleColor ? appleColors : sColors)[colorIndex];
 133     }
 134     else {
 135         NSLog(@"%s: %s %sColor: %ld not found, returning black.", __FILE__, __FUNCTION__, (useAppleColor) ? "Apple" : "System", colorIndex);
 136         result = [NSColor blackColor];
 137     }
 138 
 139     return result;
 140 }
 141 @end
--- EOF ---