gtklookandfeel.java

来自「java jdk 1.4的源码」· Java 代码 · 共 413 行 · 第 1/2 页

JAVA
413
字号
                // GTK rc file parsing:                // First, attempts to load the file specified in the                // swing.gtkthemefile system property.                // RC files come from one of the following locations:                // 1 - environment variable GTK2_RC_FILES, which is colon                //     separated list of rc files or                // 2 - SYSCONFDIR/gtk-2.0/gtkrc and ~/.gtkrc-2.0                //                 // Additionally the default Theme file is parsed last. The default                // theme name comes from the desktop property gnome.Net/ThemeName                //     Default theme is looked for in ~/.themes/THEME/gtk-2.0/gtkrc                //     and env variable GTK_DATA_PREFIX/THEME/gtkrc or                //     GTK_DATA_PREFIX/THEME/gtk-2.0/gtkrc                //     (or compiled GTK_DATA_PREFIX) GTK_DATA_PREFIX is                //     /usr/share/themes on debian,                //     /usr/sfw/share/themes on Solaris.                // Lastly key bindings are supposed to come from a different theme                // with the path built as above, using the desktop property                // named gnome.Gtk/KeyThemeName.                // Try system property override first:                String filename = System.getProperty("swing.gtkthemefile");                        if (filename == null || !parseThemeFile(filename, parser)) {	            // Try to load user's theme first	            String userHome = System.getProperty("user.home");	            if (userHome != null) {                        parseThemeFile(userHome + "/.gtkrc-2.0", parser);	            }	            // Now try to load "Default" theme	            String themeName = (String)Toolkit.getDefaultToolkit().                        getDesktopProperty("gnome.Net/ThemeName");	            if (themeName == null) {	        	themeName = "Default";	            }                    if (!parseThemeFile(userHome + "/.themes/" + themeName +                                "/gtk-2.0/gtkrc", parser)) {                        String themeDirName =                            System.getProperty("swing.gtkthemedir");                        if (themeDirName == null) {                            String[] dirs = new String[] {                                "/usr/share/themes", // Redhat/Debian/Solaris                                "/opt/gnome2/share/themes" // SUSE                            };                            // Find the first existing rc file in the list.                            for (int i = 0; i < dirs.length; i++) {                                if (new File(dirs[i] + "/" + themeName +                                        "/gtk-2.0/gtkrc").canRead()) {                                    themeDirName = dirs[i];                                    break;                                }                            }                        }                        if (themeDirName != null) {	        	    parseThemeFile(themeDirName + "/" + themeName +                                    "/gtk-2.0/gtkrc", parser);                        }                    }	        }                setStyleFactory(handleParsedData(parser));                parser.clearParser();		return null;	    }	});    }    private boolean parseThemeFile(String fileName, GTKParser parser) {        File file = new File(fileName);	if (file.canRead()) {            try {                parser.parseFile(file, fileName);            } catch (IOException ioe) {                System.err.println("error: (" + ioe.toString()                                   + ") while parsing file: \""                                   + fileName                                   + "\"");            }            return true;	}        return false; // file doesn't exist    }    /**     * This method is responsible for handling the data that was parsed.     * One of it's jobs is to fetch and deal with the GTK settings stored     * in the parser. It's other job is to create a style factory with an     * appropriate default style, load into it the styles from the parser,     * and return that factory.     */    private GTKStyleFactory handleParsedData(GTKParser parser) {        HashMap settings = parser.getGTKSettings();        /*         * The following is a list of the settings that GTK supports and their meanings.         * Currently, we only support a subset ("gtk-font-name" and "gtk-icon-sizes"):         *         *   "gtk-can-change-accels"     : Whether menu accelerators can be changed         *                                 by pressing a key over the menu item.         *   "gtk-color-palette"         : Palette to use in the color selector.         *   "gtk-cursor-blink"          : Whether the cursor should blink.         *   "gtk-cursor-blink-time"     : Length of the cursor blink cycle, in milleseconds.         *   "gtk-dnd-drag-threshold"    : Number of pixels the cursor can move before dragging.         *   "gtk-double-click-time"     : Maximum time allowed between two clicks for them         *                                 to be considered a double click (in milliseconds).         *   "gtk-entry-select-on-focus" : Whether to select the contents of an entry when it         *                                 is focused.         *   "gtk-font-name"             : Name of default font to use.         *   "gtk-icon-sizes"            : List of icon sizes (gtk-menu=16,16:gtk-button=20,20...         *   "gtk-key-theme-name"        : Name of key theme RC file to load.         *   "gtk-menu-bar-accel"        : Keybinding to activate the menu bar.         *   "gtk-menu-bar-popup-delay"  : Delay before the submenus of a menu bar appear.         *   "gtk-menu-popdown-delay"    : The time before hiding a submenu when the pointer is         *                                 moving towards the submenu.         *   "gtk-menu-popup-delay"      : Minimum time the pointer must stay over a menu item         *                                 before the submenu appear.         *   "gtk-split-cursor"          : Whether two cursors should be displayed for mixed         *                                 left-to-right and right-to-left text.         *   "gtk-theme-name"            : Name of theme RC file to load.         *   "gtk-toolbar-icon-size"     : Size of icons in default toolbars.         *   "gtk-toolbar-style"         : Whether default toolbars have text only, text and icons,         *                                 icons only, etc.         */        Object iconSizes = settings.get("gtk-icon-sizes");        if (iconSizes instanceof String) {            if (!configIconSizes((String)iconSizes)) {                System.err.println("Error parsing gtk-icon-sizes string: '" + iconSizes + "'");            }        }        // Desktop property appears to have preference over rc font.        Object fontName = Toolkit.getDefaultToolkit().getDesktopProperty(                                  "gnome.Gtk/FontName");        if (!(fontName instanceof String)) {            fontName = settings.get("gtk-font-name");            if (!(fontName instanceof String)) {                fontName = "sans 10";            }        }        Font defaultFont = PangoFonts.lookupFont((String)fontName);        GTKStyle defaultStyle = new GTKStyle(defaultFont);        GTKStyleFactory factory = new GTKStyleFactory(defaultStyle);        parser.loadStylesInto(factory);        fallbackFont = defaultFont;        return factory;    }    private boolean configIconSizes(String sizeString) {        String[] sizes = sizeString.split(":");        for (int i = 0; i < sizes.length; i++) {            String[] splits = sizes[i].split("=");            if (splits.length != 2) {                return false;            }                        String size = splits[0].trim().intern();            if (size.length() < 1) {                return false;            }            splits = splits[1].split(",");                        if (splits.length != 2) {                return false;            }                        String width = splits[0].trim();            String height = splits[1].trim();                        if (width.length() < 1 || height.length() < 1) {                return false;            }            int w = 0;            int h = 0;            try {                w = Integer.parseInt(width);                h = Integer.parseInt(height);            } catch (NumberFormatException nfe) {                return false;            }            if (w > 0 && h > 0) {                // Only allow resizing of existing icon sizes.                if (GTKStyle.getIconSize(size) != null) {                    GTKStyle.setIconSize(size, w, h);                }            } else {                System.err.println("Invalid size in gtk-icon-sizes: " + w + "," + h);            }        }                return true;    }}

⌨️ 快捷键说明

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