1 /*
   2  * Copyright 2005-2008 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 #include <dlfcn.h>
  26 #include <setjmp.h>
  27 #include <X11/Xlib.h>
  28 #include <limits.h>
  29 #include <stdio.h>
  30 #include <string.h>
  31 #include "gtk2_interface.h"
  32 #include "java_awt_Transparency.h"
  33 
  34 #define GTK2_LIB "libgtk-x11-2.0.so.0"
  35 #define GTHREAD_LIB "libgthread-2.0.so.0"
  36 
  37 #define G_TYPE_INVALID                  G_TYPE_MAKE_FUNDAMENTAL (0)
  38 #define G_TYPE_NONE                     G_TYPE_MAKE_FUNDAMENTAL (1)
  39 #define G_TYPE_INTERFACE                G_TYPE_MAKE_FUNDAMENTAL (2)
  40 #define G_TYPE_CHAR                     G_TYPE_MAKE_FUNDAMENTAL (3)
  41 #define G_TYPE_UCHAR                    G_TYPE_MAKE_FUNDAMENTAL (4)
  42 #define G_TYPE_BOOLEAN                  G_TYPE_MAKE_FUNDAMENTAL (5)
  43 #define G_TYPE_INT                      G_TYPE_MAKE_FUNDAMENTAL (6)
  44 #define G_TYPE_UINT                     G_TYPE_MAKE_FUNDAMENTAL (7)
  45 #define G_TYPE_LONG                     G_TYPE_MAKE_FUNDAMENTAL (8)
  46 #define G_TYPE_ULONG                    G_TYPE_MAKE_FUNDAMENTAL (9)
  47 #define G_TYPE_INT64                    G_TYPE_MAKE_FUNDAMENTAL (10)
  48 #define G_TYPE_UINT64                   G_TYPE_MAKE_FUNDAMENTAL (11)
  49 #define G_TYPE_ENUM                     G_TYPE_MAKE_FUNDAMENTAL (12)
  50 #define G_TYPE_FLAGS                    G_TYPE_MAKE_FUNDAMENTAL (13)
  51 #define G_TYPE_FLOAT                    G_TYPE_MAKE_FUNDAMENTAL (14)
  52 #define G_TYPE_DOUBLE                   G_TYPE_MAKE_FUNDAMENTAL (15)
  53 #define G_TYPE_STRING                   G_TYPE_MAKE_FUNDAMENTAL (16)
  54 #define G_TYPE_POINTER                  G_TYPE_MAKE_FUNDAMENTAL (17)
  55 #define G_TYPE_BOXED                    G_TYPE_MAKE_FUNDAMENTAL (18)
  56 #define G_TYPE_PARAM                    G_TYPE_MAKE_FUNDAMENTAL (19)
  57 #define G_TYPE_OBJECT                   G_TYPE_MAKE_FUNDAMENTAL (20)
  58 
  59 #define GTK_TYPE_BORDER                 ((*fp_gtk_border_get_type)())
  60 
  61 #define G_TYPE_FUNDAMENTAL_SHIFT        (2)
  62 #define G_TYPE_MAKE_FUNDAMENTAL(x)      ((GType) ((x) << G_TYPE_FUNDAMENTAL_SHIFT))
  63 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
  64 
  65 #define CONV_BUFFER_SIZE 128
  66 
  67 #define NO_SYMBOL_EXCEPTION 1
  68 
  69 /* SynthConstants */
  70 const gint ENABLED    = 1 << 0;
  71 const gint MOUSE_OVER = 1 << 1;
  72 const gint PRESSED    = 1 << 2;
  73 const gint DISABLED   = 1 << 3;
  74 const gint FOCUSED    = 1 << 8;
  75 const gint SELECTED   = 1 << 9;
  76 const gint DEFAULT    = 1 << 10;
  77 
  78 static void *gtk2_libhandle = NULL;
  79 static void *gthread_libhandle = NULL;
  80 static jmp_buf j;
  81 
  82 /* Widgets */
  83 static GtkWidget *gtk2_widget = NULL;
  84 static GtkWidget *gtk2_window = NULL;
  85 static GtkFixed  *gtk2_fixed  = NULL;
  86 
  87 /* Paint system */
  88 static GdkPixmap *gtk2_white_pixmap = NULL;
  89 static GdkPixmap *gtk2_black_pixmap = NULL;
  90 static GdkPixbuf *gtk2_white_pixbuf = NULL;
  91 static GdkPixbuf *gtk2_black_pixbuf = NULL;
  92 static int gtk2_pixbuf_width = 0;
  93 static int gtk2_pixbuf_height = 0;
  94 
  95 /* Static buffer for conversion from java.lang.String to UTF-8 */
  96 static char convertionBuffer[CONV_BUFFER_SIZE];
  97 
  98 static gboolean new_combo = TRUE;
  99 const char ENV_PREFIX[] = "GTK_MODULES=";
 100 
 101 /*******************/
 102 enum GtkWidgetType
 103 {
 104     _GTK_ARROW_TYPE,
 105     _GTK_BUTTON_TYPE,
 106     _GTK_CHECK_BUTTON_TYPE,
 107     _GTK_CHECK_MENU_ITEM_TYPE,
 108     _GTK_COLOR_SELECTION_DIALOG_TYPE,
 109     _GTK_COMBO_BOX_TYPE,
 110     _GTK_COMBO_BOX_ARROW_BUTTON_TYPE,
 111     _GTK_COMBO_BOX_TEXT_FIELD_TYPE,
 112     _GTK_CONTAINER_TYPE,
 113     _GTK_ENTRY_TYPE,
 114     _GTK_FRAME_TYPE,
 115     _GTK_HANDLE_BOX_TYPE,
 116     _GTK_HPANED_TYPE,
 117     _GTK_HPROGRESS_BAR_TYPE,
 118     _GTK_HSCALE_TYPE,
 119     _GTK_HSCROLLBAR_TYPE,
 120     _GTK_HSEPARATOR_TYPE,
 121     _GTK_IMAGE_TYPE,
 122     _GTK_MENU_TYPE,
 123     _GTK_MENU_BAR_TYPE,
 124     _GTK_MENU_ITEM_TYPE,
 125     _GTK_NOTEBOOK_TYPE,
 126     _GTK_LABEL_TYPE,
 127     _GTK_RADIO_BUTTON_TYPE,
 128     _GTK_RADIO_MENU_ITEM_TYPE,
 129     _GTK_SCROLLED_WINDOW_TYPE,
 130     _GTK_SEPARATOR_MENU_ITEM_TYPE,
 131     _GTK_SEPARATOR_TOOL_ITEM_TYPE,
 132     _GTK_SPIN_BUTTON_TYPE,
 133     _GTK_TEXT_VIEW_TYPE,
 134     _GTK_TOGGLE_BUTTON_TYPE,
 135     _GTK_TOOLBAR_TYPE,
 136     _GTK_TOOLTIP_TYPE,
 137     _GTK_TREE_VIEW_TYPE,
 138     _GTK_VIEWPORT_TYPE,
 139     _GTK_VPANED_TYPE,
 140     _GTK_VPROGRESS_BAR_TYPE,
 141     _GTK_VSCALE_TYPE,
 142     _GTK_VSCROLLBAR_TYPE,
 143     _GTK_VSEPARATOR_TYPE,
 144     _GTK_WINDOW_TYPE,
 145     _GTK_DIALOG_TYPE,
 146     _GTK_WIDGET_TYPE_SIZE
 147 };
 148 
 149 
 150 static GtkWidget *gtk2_widgets[_GTK_WIDGET_TYPE_SIZE];
 151 
 152 /*************************
 153  * Glib function pointers
 154  *************************/
 155 
 156 static gboolean (*fp_g_main_context_iteration)(GMainContext *context,
 157                                              gboolean may_block);
 158 
 159 static GValue*      (*fp_g_value_init)(GValue *value, GType g_type);
 160 static gboolean     (*fp_g_type_is_a)(GType type, GType is_a_type);
 161 static gboolean     (*fp_g_value_get_boolean)(const GValue *value);
 162 static gchar        (*fp_g_value_get_char)(const GValue *value);
 163 static guchar       (*fp_g_value_get_uchar)(const GValue *value);
 164 static gint         (*fp_g_value_get_int)(const GValue *value);
 165 static guint        (*fp_g_value_get_uint)(const GValue *value);
 166 static glong        (*fp_g_value_get_long)(const GValue *value);
 167 static gulong       (*fp_g_value_get_ulong)(const GValue *value);
 168 static gint64       (*fp_g_value_get_int64)(const GValue *value);
 169 static guint64      (*fp_g_value_get_uint64)(const GValue *value);
 170 static gfloat       (*fp_g_value_get_float)(const GValue *value);
 171 static gdouble      (*fp_g_value_get_double)(const GValue *value);
 172 static const gchar* (*fp_g_value_get_string)(const GValue *value);
 173 static gint         (*fp_g_value_get_enum)(const GValue *value);
 174 static guint        (*fp_g_value_get_flags)(const GValue *value);
 175 static GParamSpec*  (*fp_g_value_get_param)(const GValue *value);
 176 static gpointer*    (*fp_g_value_get_boxed)(const GValue *value);
 177 static gpointer*    (*fp_g_value_get_pointer)(const GValue *value);
 178 static GObject*     (*fp_g_value_get_object)(const GValue *value);
 179 static GParamSpec*  (*fp_g_param_spec_int)(const gchar *name,
 180         const gchar *nick, const gchar *blurb,
 181         gint minimum, gint maximum, gint default_value,
 182         GParamFlags flags);
 183 static void         (*fp_g_object_get)(gpointer object,
 184                                        const gchar* fpn, ...);
 185 static void         (*fp_g_object_set)(gpointer object,
 186                                        const gchar *first_property_name,
 187                                        ...);
 188 /************************
 189  * GDK function pointers
 190  ************************/
 191 static GdkPixmap *(*fp_gdk_pixmap_new)(GdkDrawable *drawable,
 192         gint width, gint height, gint depth);
 193 static GdkGC *(*fp_gdk_gc_new)(GdkDrawable*);
 194 static void (*fp_gdk_rgb_gc_set_foreground)(GdkGC*, guint32);
 195 static void (*fp_gdk_draw_rectangle)(GdkDrawable*, GdkGC*, gboolean,
 196         gint, gint, gint, gint);
 197 static GdkPixbuf *(*fp_gdk_pixbuf_new)(GdkColorspace colorspace,
 198         gboolean has_alpha, int bits_per_sample, int width, int height);
 199 static GdkPixbuf *(*fp_gdk_pixbuf_get_from_drawable)(GdkPixbuf *dest,
 200         GdkDrawable *src, GdkColormap *cmap, int src_x, int src_y,
 201         int dest_x, int dest_y, int width, int height);
 202 static void (*fp_gdk_drawable_get_size)(GdkDrawable *drawable,
 203         gint* width, gint* height);
 204 
 205 /************************
 206  * Gtk function pointers
 207  ************************/
 208 static gboolean (*fp_gtk_init_check)(int* argc, char** argv);
 209 
 210 /* Painting */
 211 static void (*fp_gtk_paint_hline)(GtkStyle* style, GdkWindow* window,
 212         GtkStateType state_type, GdkRectangle* area, GtkWidget* widget,
 213         const gchar* detail, gint x1, gint x2, gint y);
 214 static void (*fp_gtk_paint_vline)(GtkStyle* style, GdkWindow* window,
 215         GtkStateType state_type, GdkRectangle* area, GtkWidget* widget,
 216         const gchar* detail, gint y1, gint y2, gint x);
 217 static void (*fp_gtk_paint_shadow)(GtkStyle* style, GdkWindow* window,
 218         GtkStateType state_type, GtkShadowType shadow_type,
 219         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 220         gint x, gint y, gint width, gint height);
 221 static void (*fp_gtk_paint_arrow)(GtkStyle* style, GdkWindow* window,
 222         GtkStateType state_type, GtkShadowType shadow_type,
 223         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 224         GtkArrowType arrow_type, gboolean fill, gint x, gint y,
 225         gint width, gint height);
 226 static void (*fp_gtk_paint_diamond)(GtkStyle* style, GdkWindow* window,
 227         GtkStateType state_type, GtkShadowType shadow_type,
 228         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 229         gint x, gint y, gint width, gint height);
 230 static void (*fp_gtk_paint_box)(GtkStyle* style, GdkWindow* window,
 231         GtkStateType state_type, GtkShadowType shadow_type,
 232         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 233         gint x, gint y, gint width, gint height);
 234 static void (*fp_gtk_paint_flat_box)(GtkStyle* style, GdkWindow* window,
 235         GtkStateType state_type, GtkShadowType shadow_type,
 236         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 237         gint x, gint y, gint width, gint height);
 238 static void (*fp_gtk_paint_check)(GtkStyle* style, GdkWindow* window,
 239         GtkStateType state_type, GtkShadowType shadow_type,
 240         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 241         gint x, gint y, gint width, gint height);
 242 static void (*fp_gtk_paint_option)(GtkStyle* style, GdkWindow* window,
 243         GtkStateType state_type, GtkShadowType shadow_type,
 244         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 245         gint x, gint y, gint width, gint height);
 246 static void (*fp_gtk_paint_box_gap)(GtkStyle* style, GdkWindow* window,
 247         GtkStateType state_type, GtkShadowType shadow_type,
 248         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 249         gint x, gint y, gint width, gint height,
 250         GtkPositionType gap_side, gint gap_x, gint gap_width);
 251 static void (*fp_gtk_paint_extension)(GtkStyle* style, GdkWindow* window,
 252         GtkStateType state_type, GtkShadowType shadow_type,
 253         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 254         gint x, gint y, gint width, gint height, GtkPositionType gap_side);
 255 static void (*fp_gtk_paint_focus)(GtkStyle* style, GdkWindow* window,
 256         GtkStateType state_type, GdkRectangle* area, GtkWidget* widget,
 257         const gchar* detail, gint x, gint y, gint width, gint height);
 258 static void (*fp_gtk_paint_slider)(GtkStyle* style, GdkWindow* window,
 259         GtkStateType state_type, GtkShadowType shadow_type,
 260         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 261         gint x, gint y, gint width, gint height, GtkOrientation orientation);
 262 static void (*fp_gtk_paint_handle)(GtkStyle* style, GdkWindow* window,
 263         GtkStateType state_type, GtkShadowType shadow_type,
 264         GdkRectangle* area, GtkWidget* widget, const gchar* detail,
 265         gint x, gint y, gint width, gint height, GtkOrientation orientation);
 266 static void (*fp_gtk_paint_expander)(GtkStyle* style, GdkWindow* window,
 267         GtkStateType state_type, GdkRectangle* area, GtkWidget* widget,
 268         const gchar* detail, gint x, gint y, GtkExpanderStyle expander_style);
 269 static void (*fp_gtk_style_apply_default_background)(GtkStyle* style,
 270         GdkWindow* window, gboolean set_bg, GtkStateType state_type,
 271         GdkRectangle* area, gint x, gint y, gint width, gint height);
 272 
 273 /* Widget creation */
 274 static GtkWidget* (*fp_gtk_arrow_new)(GtkArrowType arrow_type,
 275                                       GtkShadowType shadow_type);
 276 static GtkWidget* (*fp_gtk_button_new)();
 277 static GtkWidget* (*fp_gtk_check_button_new)();
 278 static GtkWidget* (*fp_gtk_check_menu_item_new)();
 279 static GtkWidget* (*fp_gtk_color_selection_dialog_new)(const gchar* title);
 280 static GtkWidget* (*fp_gtk_combo_box_new)();
 281 static GtkWidget* (*fp_gtk_combo_box_entry_new)();
 282 static GtkWidget* (*fp_gtk_entry_new)();
 283 static GtkWidget* (*fp_gtk_fixed_new)();
 284 static GtkWidget* (*fp_gtk_handle_box_new)();
 285 static GtkWidget* (*fp_gtk_hpaned_new)();
 286 static GtkWidget* (*fp_gtk_vpaned_new)();
 287 static GtkWidget* (*fp_gtk_hscale_new)(GtkAdjustment* adjustment);
 288 static GtkWidget* (*fp_gtk_vscale_new)(GtkAdjustment* adjustment);
 289 static GtkWidget* (*fp_gtk_hscrollbar_new)(GtkAdjustment* adjustment);
 290 static GtkWidget* (*fp_gtk_vscrollbar_new)(GtkAdjustment* adjustment);
 291 static GtkWidget* (*fp_gtk_hseparator_new)();
 292 static GtkWidget* (*fp_gtk_vseparator_new)();
 293 static GtkWidget* (*fp_gtk_image_new)();
 294 static GtkWidget* (*fp_gtk_label_new)(const gchar* str);
 295 static GtkWidget* (*fp_gtk_menu_new)();
 296 static GtkWidget* (*fp_gtk_menu_bar_new)();
 297 static GtkWidget* (*fp_gtk_menu_item_new)();
 298 static GtkWidget* (*fp_gtk_notebook_new)();
 299 static GtkWidget* (*fp_gtk_progress_bar_new)();
 300 static GtkWidget* (*fp_gtk_progress_bar_set_orientation)(
 301         GtkProgressBar *pbar,
 302         GtkProgressBarOrientation orientation);
 303 static GtkWidget* (*fp_gtk_radio_button_new)(GSList *group);
 304 static GtkWidget* (*fp_gtk_radio_menu_item_new)(GSList *group);
 305 static GtkWidget* (*fp_gtk_scrolled_window_new)(GtkAdjustment *hadjustment,
 306         GtkAdjustment *vadjustment);
 307 static GtkWidget* (*fp_gtk_separator_menu_item_new)();
 308 static GtkWidget* (*fp_gtk_separator_tool_item_new)();
 309 static GtkWidget* (*fp_gtk_text_view_new)();
 310 static GtkWidget* (*fp_gtk_toggle_button_new)();
 311 static GtkWidget* (*fp_gtk_toolbar_new)();
 312 static GtkWidget* (*fp_gtk_tree_view_new)();
 313 static GtkWidget* (*fp_gtk_viewport_new)(GtkAdjustment *hadjustment,
 314         GtkAdjustment *vadjustment);
 315 static GtkWidget* (*fp_gtk_window_new)(GtkWindowType type);
 316 static GtkWidget* (*fp_gtk_dialog_new)();
 317 static GtkWidget* (*fp_gtk_spin_button_new)(GtkAdjustment *adjustment,
 318         gdouble climb_rate, guint digits);
 319 static GtkWidget* (*fp_gtk_frame_new)(const gchar *label);
 320 
 321 /* Other widget operations */
 322 static GtkObject* (*fp_gtk_adjustment_new)(gdouble value,
 323         gdouble lower, gdouble upper, gdouble step_increment,
 324         gdouble page_increment, gdouble page_size);
 325 static void (*fp_gtk_container_add)(GtkContainer *window, GtkWidget *widget);
 326 static void (*fp_gtk_menu_shell_append)(GtkMenuShell *menu_shell,
 327         GtkWidget *child);
 328 static void (*fp_gtk_menu_item_set_submenu)(GtkMenuItem *menu_item,
 329         GtkWidget *submenu);
 330 static void (*fp_gtk_widget_realize)(GtkWidget *widget);
 331 static GdkPixbuf* (*fp_gtk_widget_render_icon)(GtkWidget *widget,
 332         const gchar *stock_id, GtkIconSize size, const gchar *detail);
 333 static void (*fp_gtk_widget_set_name)(GtkWidget *widget, const gchar *name);
 334 static void (*fp_gtk_widget_set_parent)(GtkWidget *widget, GtkWidget *parent);
 335 static void (*fp_gtk_widget_set_direction)(GtkWidget *widget,
 336         GtkTextDirection direction);
 337 static void (*fp_gtk_widget_style_get)(GtkWidget *widget,
 338         const gchar *first_property_name, ...);
 339 static void (*fp_gtk_widget_class_install_style_property)(
 340         GtkWidgetClass* class, GParamSpec *pspec);
 341 static GParamSpec* (*fp_gtk_widget_class_find_style_property)(
 342         GtkWidgetClass* class, const gchar* property_name);
 343 static void (*fp_gtk_widget_style_get_property)(GtkWidget* widget,
 344         const gchar* property_name, GValue* value);
 345 static char* (*fp_pango_font_description_to_string)(
 346         const PangoFontDescription* fd);
 347 static GtkSettings* (*fp_gtk_settings_get_default)();
 348 static GtkSettings* (*fp_gtk_widget_get_settings)(GtkWidget *widget);
 349 static GType        (*fp_gtk_border_get_type)();
 350 static void (*fp_gtk_arrow_set)(GtkWidget* arrow,
 351                                 GtkArrowType arrow_type,
 352                                 GtkShadowType shadow_type);
 353 static void (*fp_gtk_widget_size_request)(GtkWidget *widget,
 354                                           GtkRequisition *requisition);
 355 static GtkAdjustment* (*fp_gtk_range_get_adjustment)(GtkRange* range);
 356 
 357 /* Method bodies */
 358 const char *getStrFor(JNIEnv *env, jstring val)
 359 {
 360     int length = (*env)->GetStringLength(env, val);
 361     if (length > CONV_BUFFER_SIZE-1)
 362     {
 363         length = CONV_BUFFER_SIZE-1;
 364 #ifdef INTERNAL_BUILD
 365         fprintf(stderr, "Note: Detail is too long: %d chars\n", length);
 366 #endif /* INTERNAL_BUILD */
 367     }
 368 
 369     (*env)->GetStringUTFRegion(env, val, 0, length, convertionBuffer);
 370     return convertionBuffer;
 371 }
 372 
 373 /* This is a workaround for the bug:
 374  * http://sourceware.org/bugzilla/show_bug.cgi?id=1814
 375  * (dlsym/dlopen clears dlerror state)
 376  * This bug is specific to Linux, but there is no harm in
 377  * applying this workaround on Solaris as well.
 378  */
 379 static void* dl_symbol(const char* name)
 380 {
 381     void* result = dlsym(gtk2_libhandle, name);
 382     if (!result)
 383         longjmp(j, NO_SYMBOL_EXCEPTION);
 384 
 385     return result;
 386 }
 387 
 388 static void* dl_symbol_gthread(const char* name)
 389 {
 390     void* result = dlsym(gthread_libhandle, name);
 391     if (!result)
 392         longjmp(j, NO_SYMBOL_EXCEPTION);
 393 
 394     return result;
 395 }
 396 
 397 gboolean gtk2_check_version()
 398 {
 399     if (gtk2_libhandle != NULL) {
 400         /* We've already successfully opened the GTK libs, so return true. */
 401         return TRUE;
 402     } else {
 403         void *lib = NULL;
 404         gboolean result = FALSE;
 405 
 406         lib = dlopen(GTK2_LIB, RTLD_LAZY | RTLD_LOCAL);
 407         if (lib == NULL) {
 408             return FALSE;
 409         }
 410 
 411         fp_gtk_check_version = dlsym(lib, "gtk_check_version");
 412         /* Check for GTK 2.2+ */
 413         if (!fp_gtk_check_version(2, 2, 0)) {
 414             result = TRUE;
 415         }
 416 
 417         dlclose(lib);
 418 
 419         return result;
 420     }
 421 }
 422 
 423 /**
 424  * Functions for sun_awt_X11_GtkFileDialogPeer.c
 425  */
 426 void gtk2_file_chooser_load()
 427 {
 428     fp_gtk_file_chooser_get_filename = dl_symbol(
 429             "gtk_file_chooser_get_filename");
 430     fp_gtk_file_chooser_dialog_new = dl_symbol("gtk_file_chooser_dialog_new");
 431     fp_gtk_file_chooser_set_current_folder = dl_symbol(
 432             "gtk_file_chooser_set_current_folder");
 433     fp_gtk_file_chooser_set_filename = dl_symbol(
 434             "gtk_file_chooser_set_filename");
 435     fp_gtk_file_filter_add_custom = dl_symbol("gtk_file_filter_add_custom");
 436     fp_gtk_file_chooser_set_filter = dl_symbol("gtk_file_chooser_set_filter");
 437     fp_gtk_file_chooser_get_type = dl_symbol("gtk_file_chooser_get_type");
 438     fp_gtk_file_filter_new = dl_symbol("gtk_file_filter_new");
 439     fp_gtk_file_chooser_set_do_overwrite_confirmation = dl_symbol(
 440             "gtk_file_chooser_set_do_overwrite_confirmation");
 441     fp_gtk_file_chooser_set_select_multiple = dl_symbol(
 442             "gtk_file_chooser_set_select_multiple");
 443     fp_gtk_file_chooser_get_current_folder = dl_symbol(
 444             "gtk_file_chooser_get_current_folder");
 445     fp_gtk_file_chooser_get_filenames = dl_symbol(
 446             "gtk_file_chooser_get_filenames");
 447     fp_gtk_g_slist_length = dl_symbol("g_slist_length");
 448 }
 449 
 450 gboolean gtk2_load()
 451 {
 452     gboolean result;
 453     int i;
 454     int (*handler)();
 455     int (*io_handler)();
 456     char *gtk_modules_env;
 457 
 458     gtk2_libhandle = dlopen(GTK2_LIB, RTLD_LAZY | RTLD_LOCAL);
 459     gthread_libhandle = dlopen(GTHREAD_LIB, RTLD_LAZY | RTLD_LOCAL);
 460 
 461     if (gtk2_libhandle == NULL || gthread_libhandle == NULL)
 462         return FALSE;
 463 
 464     if (setjmp(j) == 0)
 465     {
 466         fp_gtk_check_version = dl_symbol("gtk_check_version");
 467         /* Check for GTK 2.2+ */
 468         if (fp_gtk_check_version(2, 2, 0)) {
 469             longjmp(j, NO_SYMBOL_EXCEPTION);
 470         }
 471 
 472         /* GLib */
 473         fp_g_free = dl_symbol("g_free");
 474         fp_g_object_unref = dl_symbol("g_object_unref");
 475 
 476         fp_g_main_context_iteration =
 477             dl_symbol("g_main_context_iteration");
 478 
 479         fp_g_value_init = dl_symbol("g_value_init");
 480         fp_g_type_is_a = dl_symbol("g_type_is_a");
 481 
 482         fp_g_value_get_boolean = dl_symbol("g_value_get_boolean");
 483         fp_g_value_get_char = dl_symbol("g_value_get_char");
 484         fp_g_value_get_uchar = dl_symbol("g_value_get_uchar");
 485         fp_g_value_get_int = dl_symbol("g_value_get_int");
 486         fp_g_value_get_uint = dl_symbol("g_value_get_uint");
 487         fp_g_value_get_long = dl_symbol("g_value_get_long");
 488         fp_g_value_get_ulong = dl_symbol("g_value_get_ulong");
 489         fp_g_value_get_int64 = dl_symbol("g_value_get_int64");
 490         fp_g_value_get_uint64 = dl_symbol("g_value_get_uint64");
 491         fp_g_value_get_float = dl_symbol("g_value_get_float");
 492         fp_g_value_get_double = dl_symbol("g_value_get_double");
 493         fp_g_value_get_string = dl_symbol("g_value_get_string");
 494         fp_g_value_get_enum = dl_symbol("g_value_get_enum");
 495         fp_g_value_get_flags = dl_symbol("g_value_get_flags");
 496         fp_g_value_get_param = dl_symbol("g_value_get_param");
 497         fp_g_value_get_boxed = dl_symbol("g_value_get_boxed");
 498         fp_g_value_get_pointer = dl_symbol("g_value_get_pointer");
 499         fp_g_value_get_object = dl_symbol("g_value_get_object");
 500         fp_g_param_spec_int = dl_symbol("g_param_spec_int");
 501         fp_g_object_get = dl_symbol("g_object_get");
 502         fp_g_object_set = dl_symbol("g_object_set");
 503 
 504         /* GDK */
 505         fp_gdk_pixmap_new = dl_symbol("gdk_pixmap_new");
 506         fp_gdk_pixbuf_get_from_drawable =
 507             dl_symbol("gdk_pixbuf_get_from_drawable");
 508         fp_gdk_gc_new = dl_symbol("gdk_gc_new");
 509         fp_gdk_rgb_gc_set_foreground =
 510             dl_symbol("gdk_rgb_gc_set_foreground");
 511         fp_gdk_draw_rectangle = dl_symbol("gdk_draw_rectangle");
 512         fp_gdk_drawable_get_size = dl_symbol("gdk_drawable_get_size");
 513 
 514         /* Pixbuf */
 515         fp_gdk_pixbuf_new = dl_symbol("gdk_pixbuf_new");
 516         fp_gdk_pixbuf_new_from_file =
 517                 dl_symbol("gdk_pixbuf_new_from_file");
 518         fp_gdk_pixbuf_get_width = dl_symbol("gdk_pixbuf_get_width");
 519         fp_gdk_pixbuf_get_height = dl_symbol("gdk_pixbuf_get_height");
 520         fp_gdk_pixbuf_get_pixels = dl_symbol("gdk_pixbuf_get_pixels");
 521         fp_gdk_pixbuf_get_rowstride =
 522                 dl_symbol("gdk_pixbuf_get_rowstride");
 523         fp_gdk_pixbuf_get_has_alpha =
 524                 dl_symbol("gdk_pixbuf_get_has_alpha");
 525         fp_gdk_pixbuf_get_bits_per_sample =
 526                 dl_symbol("gdk_pixbuf_get_bits_per_sample");
 527         fp_gdk_pixbuf_get_n_channels =
 528                 dl_symbol("gdk_pixbuf_get_n_channels");
 529 
 530         /* GTK painting */
 531         fp_gtk_init_check = dl_symbol("gtk_init_check");
 532         fp_gtk_paint_hline = dl_symbol("gtk_paint_hline");
 533         fp_gtk_paint_vline = dl_symbol("gtk_paint_vline");
 534         fp_gtk_paint_shadow = dl_symbol("gtk_paint_shadow");
 535         fp_gtk_paint_arrow = dl_symbol("gtk_paint_arrow");
 536         fp_gtk_paint_diamond = dl_symbol("gtk_paint_diamond");
 537         fp_gtk_paint_box = dl_symbol("gtk_paint_box");
 538         fp_gtk_paint_flat_box = dl_symbol("gtk_paint_flat_box");
 539         fp_gtk_paint_check = dl_symbol("gtk_paint_check");
 540         fp_gtk_paint_option = dl_symbol("gtk_paint_option");
 541         fp_gtk_paint_box_gap = dl_symbol("gtk_paint_box_gap");
 542         fp_gtk_paint_extension = dl_symbol("gtk_paint_extension");
 543         fp_gtk_paint_focus = dl_symbol("gtk_paint_focus");
 544         fp_gtk_paint_slider = dl_symbol("gtk_paint_slider");
 545         fp_gtk_paint_handle = dl_symbol("gtk_paint_handle");
 546         fp_gtk_paint_expander = dl_symbol("gtk_paint_expander");
 547         fp_gtk_style_apply_default_background =
 548                 dl_symbol("gtk_style_apply_default_background");
 549 
 550         /* GTK widgets */
 551         fp_gtk_arrow_new = dl_symbol("gtk_arrow_new");
 552         fp_gtk_button_new = dl_symbol("gtk_button_new");
 553         fp_gtk_spin_button_new = dl_symbol("gtk_spin_button_new");
 554         fp_gtk_check_button_new = dl_symbol("gtk_check_button_new");
 555         fp_gtk_check_menu_item_new =
 556                 dl_symbol("gtk_check_menu_item_new");
 557         fp_gtk_color_selection_dialog_new =
 558                 dl_symbol("gtk_color_selection_dialog_new");
 559         fp_gtk_entry_new = dl_symbol("gtk_entry_new");
 560         fp_gtk_fixed_new = dl_symbol("gtk_fixed_new");
 561         fp_gtk_handle_box_new = dl_symbol("gtk_handle_box_new");
 562         fp_gtk_image_new = dl_symbol("gtk_image_new");
 563         fp_gtk_hpaned_new = dl_symbol("gtk_hpaned_new");
 564         fp_gtk_vpaned_new = dl_symbol("gtk_vpaned_new");
 565         fp_gtk_hscale_new = dl_symbol("gtk_hscale_new");
 566         fp_gtk_vscale_new = dl_symbol("gtk_vscale_new");
 567         fp_gtk_hscrollbar_new = dl_symbol("gtk_hscrollbar_new");
 568         fp_gtk_vscrollbar_new = dl_symbol("gtk_vscrollbar_new");
 569         fp_gtk_hseparator_new = dl_symbol("gtk_hseparator_new");
 570         fp_gtk_vseparator_new = dl_symbol("gtk_vseparator_new");
 571         fp_gtk_label_new = dl_symbol("gtk_label_new");
 572         fp_gtk_menu_new = dl_symbol("gtk_menu_new");
 573         fp_gtk_menu_bar_new = dl_symbol("gtk_menu_bar_new");
 574         fp_gtk_menu_item_new = dl_symbol("gtk_menu_item_new");
 575         fp_gtk_menu_item_set_submenu =
 576                 dl_symbol("gtk_menu_item_set_submenu");
 577         fp_gtk_notebook_new = dl_symbol("gtk_notebook_new");
 578         fp_gtk_progress_bar_new =
 579             dl_symbol("gtk_progress_bar_new");
 580         fp_gtk_progress_bar_set_orientation =
 581             dl_symbol("gtk_progress_bar_set_orientation");
 582         fp_gtk_radio_button_new =
 583             dl_symbol("gtk_radio_button_new");
 584         fp_gtk_radio_menu_item_new =
 585             dl_symbol("gtk_radio_menu_item_new");
 586         fp_gtk_scrolled_window_new =
 587             dl_symbol("gtk_scrolled_window_new");
 588         fp_gtk_separator_menu_item_new =
 589             dl_symbol("gtk_separator_menu_item_new");
 590         fp_gtk_text_view_new = dl_symbol("gtk_text_view_new");
 591         fp_gtk_toggle_button_new =
 592             dl_symbol("gtk_toggle_button_new");
 593         fp_gtk_toolbar_new = dl_symbol("gtk_toolbar_new");
 594         fp_gtk_tree_view_new = dl_symbol("gtk_tree_view_new");
 595         fp_gtk_viewport_new = dl_symbol("gtk_viewport_new");
 596         fp_gtk_window_new = dl_symbol("gtk_window_new");
 597         fp_gtk_dialog_new = dl_symbol("gtk_dialog_new");
 598         fp_gtk_frame_new = dl_symbol("gtk_frame_new");
 599 
 600         fp_gtk_adjustment_new = dl_symbol("gtk_adjustment_new");
 601         fp_gtk_container_add = dl_symbol("gtk_container_add");
 602         fp_gtk_menu_shell_append =
 603             dl_symbol("gtk_menu_shell_append");
 604         fp_gtk_widget_realize = dl_symbol("gtk_widget_realize");
 605         fp_gtk_widget_destroy = dl_symbol("gtk_widget_destroy");
 606         fp_gtk_widget_render_icon =
 607             dl_symbol("gtk_widget_render_icon");
 608         fp_gtk_widget_set_name =
 609             dl_symbol("gtk_widget_set_name");
 610         fp_gtk_widget_set_parent =
 611             dl_symbol("gtk_widget_set_parent");
 612         fp_gtk_widget_set_direction =
 613             dl_symbol("gtk_widget_set_direction");
 614         fp_gtk_widget_style_get =
 615             dl_symbol("gtk_widget_style_get");
 616         fp_gtk_widget_class_install_style_property =
 617             dl_symbol("gtk_widget_class_install_style_property");
 618         fp_gtk_widget_class_find_style_property =
 619             dl_symbol("gtk_widget_class_find_style_property");
 620         fp_gtk_widget_style_get_property =
 621             dl_symbol("gtk_widget_style_get_property");
 622         fp_pango_font_description_to_string =
 623             dl_symbol("pango_font_description_to_string");
 624         fp_gtk_settings_get_default =
 625             dl_symbol("gtk_settings_get_default");
 626         fp_gtk_widget_get_settings =
 627             dl_symbol("gtk_widget_get_settings");
 628         fp_gtk_border_get_type =  dl_symbol("gtk_border_get_type");
 629         fp_gtk_arrow_set = dl_symbol("gtk_arrow_set");
 630         fp_gtk_widget_size_request =
 631             dl_symbol("gtk_widget_size_request");
 632         fp_gtk_range_get_adjustment =
 633             dl_symbol("gtk_range_get_adjustment");
 634 
 635         fp_gtk_widget_hide = dl_symbol("gtk_widget_hide");
 636         fp_gtk_main_quit = dl_symbol("gtk_main_quit");
 637         fp_g_signal_connect_data = dl_symbol("g_signal_connect_data");
 638         fp_gtk_widget_show = dl_symbol("gtk_widget_show");
 639         fp_gtk_main = dl_symbol("gtk_main");
 640 
 641         /**
 642          * GLib thread system
 643          */
 644         fp_g_thread_get_initialized = dl_symbol_gthread(
 645                 "g_thread_get_initialized");
 646         fp_g_thread_init = dl_symbol_gthread("g_thread_init");
 647         fp_gdk_threads_init = dl_symbol("gdk_threads_init");
 648         fp_gdk_threads_enter = dl_symbol("gdk_threads_enter");
 649         fp_gdk_threads_leave = dl_symbol("gdk_threads_leave");
 650 
 651         /**
 652          * Functions for sun_awt_X11_GtkFileDialogPeer.c
 653          */
 654         if (fp_gtk_check_version(2, 4, 0) == NULL) {
 655             // The current GtkFileChooser is available from GTK+ 2.4
 656             gtk2_file_chooser_load();
 657         }
 658 
 659         /* Some functions may be missing in pre-2.4 GTK.
 660            We handle them specially here.
 661          */
 662         fp_gtk_combo_box_new = dlsym(gtk2_libhandle, "gtk_combo_box_new");
 663         if (fp_gtk_combo_box_new == NULL) {
 664             fp_gtk_combo_box_new = dl_symbol("gtk_combo_new");
 665         }
 666 
 667         fp_gtk_combo_box_entry_new =
 668             dlsym(gtk2_libhandle, "gtk_combo_box_entry_new");
 669         if (fp_gtk_combo_box_entry_new == NULL) {
 670             fp_gtk_combo_box_entry_new = dl_symbol("gtk_combo_new");
 671             new_combo = FALSE;
 672         }
 673 
 674         fp_gtk_separator_tool_item_new =
 675             dlsym(gtk2_libhandle, "gtk_separator_tool_item_new");
 676         if (fp_gtk_separator_tool_item_new == NULL) {
 677             fp_gtk_separator_tool_item_new =
 678                 dl_symbol("gtk_vseparator_new");
 679         }
 680     }
 681     /* Now we have only one kind of exceptions: NO_SYMBOL_EXCEPTION
 682      * Otherwise we can check the return value of setjmp method.
 683      */
 684     else
 685     {
 686         dlclose(gtk2_libhandle);
 687         gtk2_libhandle = NULL;
 688 
 689         dlclose(gthread_libhandle);
 690         gthread_libhandle = NULL;
 691         
 692         return FALSE;
 693     }
 694 
 695     /*
 696      * Strip the AT-SPI GTK_MODULEs if present
 697      */
 698     gtk_modules_env = getenv ("GTK_MODULES");
 699 
 700     if (gtk_modules_env && strstr (gtk_modules_env, "atk-bridge") ||
 701         gtk_modules_env && strstr (gtk_modules_env, "gail"))
 702     {
 703         /* the new env will be smaller than the old one */
 704         gchar *s, *new_env = malloc (sizeof(ENV_PREFIX)+strlen (gtk_modules_env));
 705 
 706         if (new_env != NULL )
 707         {
 708             /* careful, strtok modifies its args */
 709             gchar *tmp_env = strdup (gtk_modules_env);
 710             strcpy(new_env, ENV_PREFIX);
 711 
 712             /* strip out 'atk-bridge' and 'gail' */
 713             size_t PREFIX_LENGTH = strlen(ENV_PREFIX);
 714             while (s = strtok(tmp_env, ":"))
 715             {
 716                 if ((!strstr (s, "atk-bridge")) && (!strstr (s, "gail")))
 717                 {
 718                     if (strlen (new_env) > PREFIX_LENGTH) {
 719                         new_env = strcat (new_env, ":");
 720                     }
 721                     new_env = strcat(new_env, s);
 722                 }
 723                 if (tmp_env)
 724                 {
 725                     free (tmp_env);
 726                     tmp_env = NULL; /* next call to strtok arg1==NULL */
 727                 }
 728             }
 729             putenv (new_env);
 730             free (new_env);
 731         }
 732     }
 733 
 734     /*
 735      * GTK should be initialized with gtk_init_check() before use.
 736      *
 737      * gtk_init_check installs its own error handlers. It is critical that
 738      * we preserve error handler set from AWT. Otherwise we'll crash on
 739      * BadMatch errors which we would normally ignore. The IO error handler
 740      * is preserved here, too, just for consistency.
 741     */
 742     handler = XSetErrorHandler(NULL);
 743     io_handler = XSetIOErrorHandler(NULL);
 744 
 745     if (fp_gtk_check_version(2, 2, 0) == NULL) {
 746         // Init the thread system to use GLib in a thread-safe mode
 747         if (!fp_g_thread_get_initialized()) {
 748             fp_g_thread_init(NULL);
 749 
 750             //According the GTK documentation, gdk_threads_init() should be
 751             //called before gtk_init() or gtk_init_check()
 752             fp_gdk_threads_init();
 753         }
 754     }
 755     result = (*fp_gtk_init_check)(NULL, NULL);
 756 
 757     XSetErrorHandler(handler);
 758     XSetIOErrorHandler(io_handler);
 759 
 760     /* Initialize widget array. */
 761     for (i = 0; i < _GTK_WIDGET_TYPE_SIZE; i++)
 762     {
 763         gtk2_widgets[i] = NULL;
 764     }
 765 
 766     return result;
 767 }
 768 
 769 int gtk2_unload()
 770 {
 771     int i;
 772     char *gtk2_error;
 773 
 774     if (!gtk2_libhandle)
 775         return TRUE;
 776 
 777     /* Release painting objects */
 778     if (gtk2_white_pixmap != NULL) {
 779         (*fp_g_object_unref)(gtk2_white_pixmap);
 780         (*fp_g_object_unref)(gtk2_black_pixmap);
 781         (*fp_g_object_unref)(gtk2_white_pixbuf);
 782         (*fp_g_object_unref)(gtk2_black_pixbuf);
 783         gtk2_white_pixmap = gtk2_black_pixmap =
 784             gtk2_white_pixbuf = gtk2_black_pixbuf = NULL;
 785     }
 786     gtk2_pixbuf_width = 0;
 787     gtk2_pixbuf_height = 0;
 788 
 789     if (gtk2_window != NULL) {
 790         /* Destroying toplevel widget will destroy all contained widgets */
 791         (*fp_gtk_widget_destroy)(gtk2_window);
 792 
 793         /* Unset some static data so they get reinitialized on next load */
 794         gtk2_window = NULL;
 795     }
 796 
 797     dlerror();
 798     dlclose(gtk2_libhandle);
 799     dlclose(gthread_libhandle);
 800     if ((gtk2_error = dlerror()) != NULL)
 801     {
 802         return FALSE;
 803     }
 804     return TRUE;
 805 }
 806 
 807 /* Dispatch all pending events from the GTK event loop.
 808  * This is needed to catch theme change and update widgets' style.
 809  */
 810 void flush_gtk_event_loop()
 811 {
 812     while( (*fp_g_main_context_iteration)(NULL, FALSE));
 813 }
 814 
 815 /*
 816  * Initialize components of containment hierarchy. This creates a GtkFixed
 817  * inside a GtkWindow. All widgets get realized.
 818  */
 819 static void init_containers()
 820 {
 821     if (gtk2_window == NULL)
 822     {
 823         gtk2_window = (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
 824         gtk2_fixed = (GtkFixed *)(*fp_gtk_fixed_new)();
 825         (*fp_gtk_container_add)((GtkContainer*)gtk2_window,
 826                                 (GtkWidget *)gtk2_fixed);
 827         (*fp_gtk_widget_realize)(gtk2_window);
 828         (*fp_gtk_widget_realize)((GtkWidget *)gtk2_fixed);
 829     }
 830 }
 831 
 832 /*
 833  * Ensure everything is ready for drawing an element of the specified width
 834  * and height.
 835  *
 836  * We should somehow handle translucent images. GTK can draw to X Drawables
 837  * only, which don't support alpha. When we retrieve the image back from
 838  * the server, translucency information is lost. There're several ways to
 839  * work around this:
 840  * 1) Subclass GdkPixmap and cache translucent objects on client side. This
 841  * requires us to implement parts of X server drawing logic on client side.
 842  * Many X requests can potentially be "translucent"; e.g. XDrawLine with
 843  * fill=tile and a translucent tile is a "translucent" operation, whereas
 844  * XDrawLine with fill=solid is an "opaque" one. Moreover themes can (and some
 845  * do) intermix transparent and opaque operations which makes caching even
 846  * more problematic.
 847  * 2) Use Xorg 32bit ARGB visual when available. GDK has no native support
 848  * for it (as of version 2.6). Also even in JDS 3 Xorg does not support
 849  * these visuals by default, which makes optimizing for them pointless.
 850  * We can consider doing this at a later point when ARGB visuals become more
 851  * popular.
 852  * 3') GTK has plans to use Cairo as its graphical backend (presumably in
 853  * 2.8), and Cairo supports alpha. With it we could also get rid of the
 854  * unnecessary round trip to server and do all the drawing on client side.
 855  * 4) For now we draw to two different pixmaps and restore alpha channel by
 856  * comparing results. This can be optimized by using subclassed pixmap and
 857  * doing the second drawing only if necessary.
 858 */
 859 void gtk2_init_painting(gint width, gint height)
 860 {
 861     GdkGC *gc;
 862 
 863     init_containers();
 864 
 865     if (gtk2_pixbuf_width < width || gtk2_pixbuf_height < height)
 866     {
 867         if (gtk2_white_pixmap != NULL) {
 868             /* free old stuff */
 869             (*fp_g_object_unref)(gtk2_white_pixmap);
 870             (*fp_g_object_unref)(gtk2_black_pixmap);
 871             (*fp_g_object_unref)(gtk2_white_pixbuf);
 872             (*fp_g_object_unref)(gtk2_black_pixbuf);
 873         }
 874 
 875         gtk2_white_pixmap = (*fp_gdk_pixmap_new)
 876                                 (gtk2_window->window, width, height, -1);
 877         gtk2_black_pixmap = (*fp_gdk_pixmap_new)
 878                                 (gtk2_window->window, width, height, -1);
 879         gtk2_white_pixbuf = (*fp_gdk_pixbuf_new)(GDK_COLORSPACE_RGB, TRUE, 8,
 880                                                  width, height);
 881         gtk2_black_pixbuf = (*fp_gdk_pixbuf_new)(GDK_COLORSPACE_RGB, TRUE, 8,
 882                                                  width, height);
 883         gtk2_pixbuf_width = width;
 884         gtk2_pixbuf_height = height;
 885     }
 886 
 887     /* clear the pixmaps */
 888     gc = (*fp_gdk_gc_new)(gtk2_white_pixmap);
 889     (*fp_gdk_rgb_gc_set_foreground)(gc, 0xffffff);
 890     (*fp_gdk_draw_rectangle)(gtk2_white_pixmap, gc, TRUE, 0, 0, width, height);
 891     (*fp_g_object_unref)(gc);
 892 
 893     gc = (*fp_gdk_gc_new)(gtk2_black_pixmap);
 894     (*fp_gdk_rgb_gc_set_foreground)(gc, 0x000000);
 895     (*fp_gdk_draw_rectangle)(gtk2_black_pixmap, gc, TRUE, 0, 0, width, height);
 896     (*fp_g_object_unref)(gc);
 897 }
 898 
 899 /*
 900  * Restore image from white and black pixmaps and copy it into destination
 901  * buffer. This method compares two pixbufs taken from white and black
 902  * pixmaps and decodes color and alpha components. Pixbufs are RGB without
 903  * alpha, destination buffer is ABGR.
 904  *
 905  * The return value is the transparency type of the resulting image, either
 906  * one of java_awt_Transparency_OPAQUE, java_awt_Transparency_BITMASK, and
 907  * java_awt_Transparency_TRANSLUCENT.
 908  */
 909 gint gtk2_copy_image(gint *dst, gint width, gint height)
 910 {
 911     gint i, j, r, g, b;
 912     guchar *white, *black;
 913     gint stride, padding;
 914     gboolean is_opaque = TRUE;
 915     gboolean is_bitmask = TRUE;
 916 
 917     (*fp_gdk_pixbuf_get_from_drawable)(gtk2_white_pixbuf, gtk2_white_pixmap,
 918             NULL, 0, 0, 0, 0, width, height);
 919     (*fp_gdk_pixbuf_get_from_drawable)(gtk2_black_pixbuf, gtk2_black_pixmap,
 920             NULL, 0, 0, 0, 0, width, height);
 921 
 922     white = (*fp_gdk_pixbuf_get_pixels)(gtk2_white_pixbuf);
 923     black = (*fp_gdk_pixbuf_get_pixels)(gtk2_black_pixbuf);
 924     stride = (*fp_gdk_pixbuf_get_rowstride)(gtk2_black_pixbuf);
 925     padding = stride - width * 4;
 926 
 927     for (i = 0; i < height; i++) {
 928         for (j = 0; j < width; j++) {
 929             int r1 = *white++;
 930             int r2 = *black++;
 931             int alpha = 0xff + r2 - r1;
 932 
 933             switch (alpha) {
 934                 case 0:       /* transparent pixel */
 935                     r = g = b = 0;
 936                     black += 3;
 937                     white += 3;
 938                     is_opaque = FALSE;
 939                     break;
 940 
 941                 case 0xff:    /* opaque pixel */
 942                     r = r2;
 943                     g = *black++;
 944                     b = *black++;
 945                     black++;
 946                     white += 3;
 947                     break;
 948 
 949                 default:      /* translucent pixel */
 950                     r = 0xff * r2 / alpha;
 951                     g = 0xff * *black++ / alpha;
 952                     b = 0xff * *black++ / alpha;
 953                     black++;
 954                     white += 3;
 955                     is_opaque = FALSE;
 956                     is_bitmask = FALSE;
 957                     break;
 958             }
 959 
 960             *dst++ = (alpha << 24 | r << 16 | g << 8 | b);
 961         }
 962 
 963         white += padding;
 964         black += padding;
 965     }
 966     return is_opaque ? java_awt_Transparency_OPAQUE :
 967                        (is_bitmask ? java_awt_Transparency_BITMASK :
 968                                      java_awt_Transparency_TRANSLUCENT);
 969 }
 970 
 971 static void
 972 gtk2_set_direction(GtkWidget *widget, GtkTextDirection dir)
 973 {
 974     /*
 975      * Some engines (inexplicably) look at the direction of the widget's
 976      * parent, so we need to set the direction of both the widget and its
 977      * parent.
 978      */
 979     (*fp_gtk_widget_set_direction)(widget, dir);
 980     if (widget->parent != NULL) {
 981         (*fp_gtk_widget_set_direction)(widget->parent, dir);
 982     }
 983 }
 984 
 985 /*
 986  * Initializes the widget to correct state for some engines.
 987  * This is a pure empirical method.
 988  */
 989 static void init_toggle_widget(WidgetType widget_type, gint synth_state)
 990 {
 991     gboolean is_active = ((synth_state & SELECTED) != 0);
 992 
 993     if (widget_type == RADIO_BUTTON ||
 994         widget_type == CHECK_BOX ||
 995         widget_type == TOGGLE_BUTTON) {
 996         ((GtkToggleButton*)gtk2_widget)->active = is_active;
 997     }
 998 
 999     if ((synth_state & FOCUSED) != 0) {
1000         ((GtkObject*)gtk2_widget)->flags |= GTK_HAS_FOCUS;
1001     } else {
1002         ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_FOCUS;
1003     }
1004 
1005     if ((synth_state & MOUSE_OVER) != 0 && (synth_state & PRESSED) == 0 ||
1006            (synth_state & FOCUSED) != 0 && (synth_state & PRESSED) != 0) {
1007         gtk2_widget->state = GTK_STATE_PRELIGHT;
1008     } else if ((synth_state & DISABLED) != 0) {
1009         gtk2_widget->state = GTK_STATE_INSENSITIVE;
1010     } else {
1011         gtk2_widget->state = is_active ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1012     }
1013 }
1014 
1015 /* GTK state_type filter */
1016 static GtkStateType get_gtk_state_type(WidgetType widget_type, gint synth_state)
1017 {
1018     GtkStateType result = GTK_STATE_NORMAL;
1019 
1020     if ((synth_state & DISABLED) != 0) {
1021         result = GTK_STATE_INSENSITIVE;
1022     } else if ((synth_state & PRESSED) != 0) {
1023         result = GTK_STATE_ACTIVE;
1024     } else if ((synth_state & MOUSE_OVER) != 0) {
1025         result = GTK_STATE_PRELIGHT;
1026     }
1027     return result;
1028 }
1029 
1030 /* GTK shadow_type filter */
1031 static GtkShadowType get_gtk_shadow_type(WidgetType widget_type, gint synth_state)
1032 {
1033     GtkShadowType result = GTK_SHADOW_OUT;
1034 
1035     if ((synth_state & SELECTED) != 0) {
1036         result = GTK_SHADOW_IN;
1037     }
1038     return result;
1039 }
1040 
1041 
1042 static GtkWidget* gtk2_get_arrow(GtkArrowType arrow_type, GtkShadowType shadow_type)
1043 {
1044     GtkWidget *arrow = NULL;
1045     if (NULL == gtk2_widgets[_GTK_ARROW_TYPE])
1046     {
1047         gtk2_widgets[_GTK_ARROW_TYPE] = (*fp_gtk_arrow_new)(arrow_type, shadow_type);
1048         (*fp_gtk_container_add)((GtkContainer *)gtk2_fixed, gtk2_widgets[_GTK_ARROW_TYPE]);
1049         (*fp_gtk_widget_realize)(gtk2_widgets[_GTK_ARROW_TYPE]);
1050     }
1051     arrow = gtk2_widgets[_GTK_ARROW_TYPE];
1052 
1053     (*fp_gtk_arrow_set)(arrow, arrow_type, shadow_type);
1054     return arrow;
1055 }
1056 
1057 static GtkAdjustment* create_adjustment()
1058 {
1059     return (GtkAdjustment *)
1060             (*fp_gtk_adjustment_new)(50.0, 0.0, 100.0, 10.0, 20.0, 20.0);
1061 }
1062 
1063 /**
1064  * Returns a pointer to the cached native widget for the specified widget
1065  * type.
1066  */
1067 static GtkWidget *gtk2_get_widget(WidgetType widget_type)
1068 {
1069     gboolean init_result = FALSE;
1070     GtkWidget *result = NULL;
1071     switch (widget_type)
1072     {
1073         case BUTTON:
1074         case TABLE_HEADER:
1075             if (init_result = (NULL == gtk2_widgets[_GTK_BUTTON_TYPE]))
1076             {
1077                 gtk2_widgets[_GTK_BUTTON_TYPE] = (*fp_gtk_button_new)();
1078             }
1079             result = gtk2_widgets[_GTK_BUTTON_TYPE];
1080             break;
1081         case CHECK_BOX:
1082             if (init_result = (NULL == gtk2_widgets[_GTK_CHECK_BUTTON_TYPE]))
1083             {
1084                 gtk2_widgets[_GTK_CHECK_BUTTON_TYPE] =
1085                     (*fp_gtk_check_button_new)();
1086             }
1087             result = gtk2_widgets[_GTK_CHECK_BUTTON_TYPE];
1088             break;
1089         case CHECK_BOX_MENU_ITEM:
1090             if (init_result = (NULL == gtk2_widgets[_GTK_CHECK_MENU_ITEM_TYPE]))
1091             {
1092                 gtk2_widgets[_GTK_CHECK_MENU_ITEM_TYPE] =
1093                     (*fp_gtk_check_menu_item_new)();
1094             }
1095             result = gtk2_widgets[_GTK_CHECK_MENU_ITEM_TYPE];
1096             break;
1097         /************************************************************
1098          *    Creation a dedicated color chooser is dangerous because
1099          * it deadlocks the EDT
1100          ************************************************************/
1101 /*        case COLOR_CHOOSER:
1102             if (init_result =
1103                     (NULL == gtk2_widgets[_GTK_COLOR_SELECTION_DIALOG_TYPE]))
1104             {
1105                 gtk2_widgets[_GTK_COLOR_SELECTION_DIALOG_TYPE] =
1106                     (*fp_gtk_color_selection_dialog_new)(NULL);
1107             }
1108             result = gtk2_widgets[_GTK_COLOR_SELECTION_DIALOG_TYPE];
1109             break;*/
1110         case COMBO_BOX:
1111             if (init_result = (NULL == gtk2_widgets[_GTK_COMBO_BOX_TYPE]))
1112             {
1113                 gtk2_widgets[_GTK_COMBO_BOX_TYPE] =
1114                     (*fp_gtk_combo_box_new)();
1115             }
1116             result = gtk2_widgets[_GTK_COMBO_BOX_TYPE];
1117             break;
1118         case COMBO_BOX_ARROW_BUTTON:
1119             if (init_result =
1120                     (NULL == gtk2_widgets[_GTK_COMBO_BOX_ARROW_BUTTON_TYPE]))
1121             {
1122                 gtk2_widgets[_GTK_COMBO_BOX_ARROW_BUTTON_TYPE] =
1123                      (*fp_gtk_toggle_button_new)();
1124             }
1125             result = gtk2_widgets[_GTK_COMBO_BOX_ARROW_BUTTON_TYPE];
1126             break;
1127         case COMBO_BOX_TEXT_FIELD:
1128             if (init_result =
1129                     (NULL == gtk2_widgets[_GTK_COMBO_BOX_TEXT_FIELD_TYPE]))
1130             {
1131                 result = gtk2_widgets[_GTK_COMBO_BOX_TEXT_FIELD_TYPE] =
1132                      (*fp_gtk_entry_new)();
1133 
1134                 GtkSettings* settings = fp_gtk_widget_get_settings(result);
1135                 fp_g_object_set(settings, "gtk-cursor-blink", FALSE, NULL);
1136             }
1137             result = gtk2_widgets[_GTK_COMBO_BOX_TEXT_FIELD_TYPE];
1138             break;
1139         case DESKTOP_ICON:
1140         case INTERNAL_FRAME_TITLE_PANE:
1141         case LABEL:
1142             if (init_result = (NULL == gtk2_widgets[_GTK_LABEL_TYPE]))
1143             {
1144                 gtk2_widgets[_GTK_LABEL_TYPE] =
1145                     (*fp_gtk_label_new)(NULL);
1146             }
1147             result = gtk2_widgets[_GTK_LABEL_TYPE];
1148             break;
1149         case DESKTOP_PANE:
1150         case PANEL:
1151         case ROOT_PANE:
1152             if (init_result = (NULL == gtk2_widgets[_GTK_CONTAINER_TYPE]))
1153             {
1154                 /* There is no constructor for a container type.  I've
1155                  * choosen GtkFixed container since it has a default
1156                  * constructor.
1157                  */
1158                 gtk2_widgets[_GTK_CONTAINER_TYPE] =
1159                     (*fp_gtk_fixed_new)();
1160             }
1161             result = gtk2_widgets[_GTK_CONTAINER_TYPE];
1162             break;
1163         case EDITOR_PANE:
1164         case TEXT_AREA:
1165         case TEXT_PANE:
1166             if (init_result = (NULL == gtk2_widgets[_GTK_TEXT_VIEW_TYPE]))
1167             {
1168                 gtk2_widgets[_GTK_TEXT_VIEW_TYPE] =
1169                     (*fp_gtk_text_view_new)();
1170             }
1171             result = gtk2_widgets[_GTK_TEXT_VIEW_TYPE];
1172             break;
1173         case FORMATTED_TEXT_FIELD:
1174         case PASSWORD_FIELD:
1175         case TEXT_FIELD:
1176             if (init_result = (NULL == gtk2_widgets[_GTK_ENTRY_TYPE]))
1177             {
1178                 gtk2_widgets[_GTK_ENTRY_TYPE] =
1179                     (*fp_gtk_entry_new)();
1180 
1181                 GtkSettings* settings =
1182                     fp_gtk_widget_get_settings(gtk2_widgets[_GTK_ENTRY_TYPE]);
1183                 fp_g_object_set(settings, "gtk-cursor-blink", FALSE, NULL);
1184             }
1185             result = gtk2_widgets[_GTK_ENTRY_TYPE];
1186             break;
1187         case HANDLE_BOX:
1188             if (init_result = (NULL == gtk2_widgets[_GTK_HANDLE_BOX_TYPE]))
1189             {
1190                 gtk2_widgets[_GTK_HANDLE_BOX_TYPE] =
1191                     (*fp_gtk_handle_box_new)();
1192             }
1193             result = gtk2_widgets[_GTK_HANDLE_BOX_TYPE];
1194             break;
1195         case HSCROLL_BAR:
1196         case HSCROLL_BAR_BUTTON_LEFT:
1197         case HSCROLL_BAR_BUTTON_RIGHT:
1198         case HSCROLL_BAR_TRACK:
1199         case HSCROLL_BAR_THUMB:
1200             if (init_result = (NULL == gtk2_widgets[_GTK_HSCROLLBAR_TYPE]))
1201             {
1202                 gtk2_widgets[_GTK_HSCROLLBAR_TYPE] =
1203                     (*fp_gtk_hscrollbar_new)(create_adjustment());
1204             }
1205             result = gtk2_widgets[_GTK_HSCROLLBAR_TYPE];
1206             break;
1207         case HSEPARATOR:
1208             if (init_result = (NULL == gtk2_widgets[_GTK_HSEPARATOR_TYPE]))
1209             {
1210                 gtk2_widgets[_GTK_HSEPARATOR_TYPE] =
1211                     (*fp_gtk_hseparator_new)();
1212             }
1213             result = gtk2_widgets[_GTK_HSEPARATOR_TYPE];
1214             break;
1215         case HSLIDER:
1216         case HSLIDER_THUMB:
1217         case HSLIDER_TRACK:
1218             if (init_result = (NULL == gtk2_widgets[_GTK_HSCALE_TYPE]))
1219             {
1220                 gtk2_widgets[_GTK_HSCALE_TYPE] =
1221                     (*fp_gtk_hscale_new)(NULL);
1222             }
1223             result = gtk2_widgets[_GTK_HSCALE_TYPE];
1224             break;
1225         case HSPLIT_PANE_DIVIDER:
1226         case SPLIT_PANE:
1227             if (init_result = (NULL == gtk2_widgets[_GTK_HPANED_TYPE]))
1228             {
1229                 gtk2_widgets[_GTK_HPANED_TYPE] = (*fp_gtk_hpaned_new)();
1230             }
1231             result = gtk2_widgets[_GTK_HPANED_TYPE];
1232             break;
1233         case IMAGE:
1234             if (init_result = (NULL == gtk2_widgets[_GTK_IMAGE_TYPE]))
1235             {
1236                 gtk2_widgets[_GTK_IMAGE_TYPE] = (*fp_gtk_image_new)();
1237             }
1238             result = gtk2_widgets[_GTK_IMAGE_TYPE];
1239             break;
1240         case INTERNAL_FRAME:
1241             if (init_result = (NULL == gtk2_widgets[_GTK_WINDOW_TYPE]))
1242             {
1243                 gtk2_widgets[_GTK_WINDOW_TYPE] =
1244                     (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
1245             }
1246             result = gtk2_widgets[_GTK_WINDOW_TYPE];
1247             break;
1248         case TOOL_TIP:
1249             if (init_result = (NULL == gtk2_widgets[_GTK_TOOLTIP_TYPE]))
1250             {
1251                 result = (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
1252                 (*fp_gtk_widget_set_name)(result, "gtk-tooltips");
1253                 gtk2_widgets[_GTK_TOOLTIP_TYPE] = result;
1254             }
1255             result = gtk2_widgets[_GTK_TOOLTIP_TYPE];
1256             break;
1257         case LIST:
1258         case TABLE:
1259         case TREE:
1260         case TREE_CELL:
1261             if (init_result = (NULL == gtk2_widgets[_GTK_TREE_VIEW_TYPE]))
1262             {
1263                 gtk2_widgets[_GTK_TREE_VIEW_TYPE] =
1264                     (*fp_gtk_tree_view_new)();
1265             }
1266             result = gtk2_widgets[_GTK_TREE_VIEW_TYPE];
1267             break;
1268         case TITLED_BORDER:
1269             if (init_result = (NULL == gtk2_widgets[_GTK_FRAME_TYPE]))
1270             {
1271                 gtk2_widgets[_GTK_FRAME_TYPE] = fp_gtk_frame_new(NULL);
1272             }
1273             result = gtk2_widgets[_GTK_FRAME_TYPE];
1274             break;
1275         case POPUP_MENU:
1276             if (init_result = (NULL == gtk2_widgets[_GTK_MENU_TYPE]))
1277             {
1278                 gtk2_widgets[_GTK_MENU_TYPE] =
1279                     (*fp_gtk_menu_new)();
1280             }
1281             result = gtk2_widgets[_GTK_MENU_TYPE];
1282             break;
1283         case MENU:
1284         case MENU_ITEM:
1285         case MENU_ITEM_ACCELERATOR:
1286             if (init_result = (NULL == gtk2_widgets[_GTK_MENU_ITEM_TYPE]))
1287             {
1288                 gtk2_widgets[_GTK_MENU_ITEM_TYPE] =
1289                     (*fp_gtk_menu_item_new)();
1290             }
1291             result = gtk2_widgets[_GTK_MENU_ITEM_TYPE];
1292             break;
1293         case MENU_BAR:
1294             if (init_result = (NULL == gtk2_widgets[_GTK_MENU_BAR_TYPE]))
1295             {
1296                 gtk2_widgets[_GTK_MENU_BAR_TYPE] =
1297                     (*fp_gtk_menu_bar_new)();
1298             }
1299             result = gtk2_widgets[_GTK_MENU_BAR_TYPE];
1300             break;
1301         case COLOR_CHOOSER:
1302         case OPTION_PANE:
1303             if (init_result = (NULL == gtk2_widgets[_GTK_DIALOG_TYPE]))
1304             {
1305                 gtk2_widgets[_GTK_DIALOG_TYPE] =
1306                     (*fp_gtk_dialog_new)();
1307             }
1308             result = gtk2_widgets[_GTK_DIALOG_TYPE];
1309             break;
1310         case POPUP_MENU_SEPARATOR:
1311             if (init_result =
1312                     (NULL == gtk2_widgets[_GTK_SEPARATOR_MENU_ITEM_TYPE]))
1313             {
1314                 gtk2_widgets[_GTK_SEPARATOR_MENU_ITEM_TYPE] =
1315                     (*fp_gtk_separator_menu_item_new)();
1316             }
1317             result = gtk2_widgets[_GTK_SEPARATOR_MENU_ITEM_TYPE];
1318             break;
1319         case HPROGRESS_BAR:
1320             if (init_result = (NULL == gtk2_widgets[_GTK_HPROGRESS_BAR_TYPE]))
1321             {
1322                 gtk2_widgets[_GTK_HPROGRESS_BAR_TYPE] =
1323                     (*fp_gtk_progress_bar_new)();
1324             }
1325             result = gtk2_widgets[_GTK_HPROGRESS_BAR_TYPE];
1326             break;
1327         case VPROGRESS_BAR:
1328             if (init_result = (NULL == gtk2_widgets[_GTK_VPROGRESS_BAR_TYPE]))
1329             {
1330                 gtk2_widgets[_GTK_VPROGRESS_BAR_TYPE] =
1331                     (*fp_gtk_progress_bar_new)();
1332                 /*
1333                  * Vertical JProgressBars always go bottom-to-top,
1334                  * regardless of the ComponentOrientation.
1335                  */
1336                 (*fp_gtk_progress_bar_set_orientation)(
1337                     (GtkProgressBar *)gtk2_widgets[_GTK_VPROGRESS_BAR_TYPE],
1338                     GTK_PROGRESS_BOTTOM_TO_TOP);
1339             }
1340             result = gtk2_widgets[_GTK_VPROGRESS_BAR_TYPE];
1341             break;
1342         case RADIO_BUTTON:
1343             if (init_result = (NULL == gtk2_widgets[_GTK_RADIO_BUTTON_TYPE]))
1344             {
1345                 gtk2_widgets[_GTK_RADIO_BUTTON_TYPE] =
1346                     (*fp_gtk_radio_button_new)(NULL);
1347             }
1348             result = gtk2_widgets[_GTK_RADIO_BUTTON_TYPE];
1349             break;
1350         case RADIO_BUTTON_MENU_ITEM:
1351             if (init_result =
1352                     (NULL == gtk2_widgets[_GTK_RADIO_MENU_ITEM_TYPE]))
1353             {
1354                 gtk2_widgets[_GTK_RADIO_MENU_ITEM_TYPE] =
1355                     (*fp_gtk_radio_menu_item_new)(NULL);
1356             }
1357             result = gtk2_widgets[_GTK_RADIO_MENU_ITEM_TYPE];
1358             break;
1359         case SCROLL_PANE:
1360             if (init_result =
1361                     (NULL == gtk2_widgets[_GTK_SCROLLED_WINDOW_TYPE]))
1362             {
1363                 gtk2_widgets[_GTK_SCROLLED_WINDOW_TYPE] =
1364                     (*fp_gtk_scrolled_window_new)(NULL, NULL);
1365             }
1366             result = gtk2_widgets[_GTK_SCROLLED_WINDOW_TYPE];
1367             break;
1368         case SPINNER:
1369         case SPINNER_ARROW_BUTTON:
1370         case SPINNER_TEXT_FIELD:
1371             if (init_result = (NULL == gtk2_widgets[_GTK_SPIN_BUTTON_TYPE]))
1372             {
1373                 result = gtk2_widgets[_GTK_SPIN_BUTTON_TYPE] =
1374                     (*fp_gtk_spin_button_new)(NULL, 0, 0);
1375 
1376                 GtkSettings* settings = fp_gtk_widget_get_settings(result);
1377                 fp_g_object_set(settings, "gtk-cursor-blink", FALSE, NULL);
1378             }
1379             result = gtk2_widgets[_GTK_SPIN_BUTTON_TYPE];
1380             break;
1381         case TABBED_PANE:
1382         case TABBED_PANE_TAB_AREA:
1383         case TABBED_PANE_CONTENT:
1384         case TABBED_PANE_TAB:
1385             if (init_result = (NULL == gtk2_widgets[_GTK_NOTEBOOK_TYPE]))
1386             {
1387                 gtk2_widgets[_GTK_NOTEBOOK_TYPE] =
1388                     (*fp_gtk_notebook_new)(NULL);
1389             }
1390             result = gtk2_widgets[_GTK_NOTEBOOK_TYPE];
1391             break;
1392         case TOGGLE_BUTTON:
1393             if (init_result = (NULL == gtk2_widgets[_GTK_TOGGLE_BUTTON_TYPE]))
1394             {
1395                 gtk2_widgets[_GTK_TOGGLE_BUTTON_TYPE] =
1396                     (*fp_gtk_toggle_button_new)(NULL);
1397             }
1398             result = gtk2_widgets[_GTK_TOGGLE_BUTTON_TYPE];
1399             break;
1400         case TOOL_BAR:
1401         case TOOL_BAR_DRAG_WINDOW:
1402             if (init_result = (NULL == gtk2_widgets[_GTK_TOOLBAR_TYPE]))
1403             {
1404                 gtk2_widgets[_GTK_TOOLBAR_TYPE] =
1405                     (*fp_gtk_toolbar_new)(NULL);
1406             }
1407             result = gtk2_widgets[_GTK_TOOLBAR_TYPE];
1408             break;
1409         case TOOL_BAR_SEPARATOR:
1410             if (init_result =
1411                     (NULL == gtk2_widgets[_GTK_SEPARATOR_TOOL_ITEM_TYPE]))
1412             {
1413                 gtk2_widgets[_GTK_SEPARATOR_TOOL_ITEM_TYPE] =
1414                     (*fp_gtk_separator_tool_item_new)();
1415             }
1416             result = gtk2_widgets[_GTK_SEPARATOR_TOOL_ITEM_TYPE];
1417             break;
1418         case VIEWPORT:
1419             if (init_result = (NULL == gtk2_widgets[_GTK_VIEWPORT_TYPE]))
1420             {
1421                 GtkAdjustment *adjustment = create_adjustment();
1422                 gtk2_widgets[_GTK_VIEWPORT_TYPE] =
1423                     (*fp_gtk_viewport_new)(adjustment, adjustment);
1424             }
1425             result = gtk2_widgets[_GTK_VIEWPORT_TYPE];
1426             break;
1427         case VSCROLL_BAR:
1428         case VSCROLL_BAR_BUTTON_UP:
1429         case VSCROLL_BAR_BUTTON_DOWN:
1430         case VSCROLL_BAR_TRACK:
1431         case VSCROLL_BAR_THUMB:
1432             if (init_result = (NULL == gtk2_widgets[_GTK_VSCROLLBAR_TYPE]))
1433             {
1434                 gtk2_widgets[_GTK_VSCROLLBAR_TYPE] =
1435                     (*fp_gtk_vscrollbar_new)(create_adjustment());
1436             }
1437             result = gtk2_widgets[_GTK_VSCROLLBAR_TYPE];
1438             break;
1439         case VSEPARATOR:
1440             if (init_result = (NULL == gtk2_widgets[_GTK_VSEPARATOR_TYPE]))
1441             {
1442                 gtk2_widgets[_GTK_VSEPARATOR_TYPE] =
1443                     (*fp_gtk_vseparator_new)();
1444             }
1445             result = gtk2_widgets[_GTK_VSEPARATOR_TYPE];
1446             break;
1447         case VSLIDER:
1448         case VSLIDER_THUMB:
1449         case VSLIDER_TRACK:
1450             if (init_result = (NULL == gtk2_widgets[_GTK_VSCALE_TYPE]))
1451             {
1452                 gtk2_widgets[_GTK_VSCALE_TYPE] =
1453                     (*fp_gtk_vscale_new)(NULL);
1454             }
1455             result = gtk2_widgets[_GTK_VSCALE_TYPE];
1456             /*
1457              * Vertical JSliders start at the bottom, while vertical
1458              * GtkVScale widgets start at the top (by default), so to fix
1459              * this we set the "inverted" flag to get the Swing behavior.
1460              */
1461             ((GtkRange*)result)->inverted = 1;
1462             break;
1463         case VSPLIT_PANE_DIVIDER:
1464             if (init_result = (NULL == gtk2_widgets[_GTK_VPANED_TYPE]))
1465             {
1466                 gtk2_widgets[_GTK_VPANED_TYPE] = (*fp_gtk_vpaned_new)();
1467             }
1468             result = gtk2_widgets[_GTK_VPANED_TYPE];
1469             break;
1470         default:
1471             result = NULL;
1472             break;
1473     }
1474 
1475     if (result != NULL && init_result)
1476     {
1477         if (widget_type == RADIO_BUTTON_MENU_ITEM ||
1478                 widget_type == CHECK_BOX_MENU_ITEM ||
1479                 widget_type == MENU_ITEM ||
1480                 widget_type == MENU ||
1481                 widget_type == POPUP_MENU_SEPARATOR)
1482         {
1483             GtkWidget *menu = gtk2_get_widget(POPUP_MENU);
1484             (*fp_gtk_menu_shell_append)((GtkMenuShell *)menu, result);
1485         }
1486         else if (widget_type == POPUP_MENU)
1487         {
1488             GtkWidget *menu_bar = gtk2_get_widget(MENU_BAR);
1489             GtkWidget *root_menu = (*fp_gtk_menu_item_new)();
1490             (*fp_gtk_menu_item_set_submenu)((GtkMenuItem*)root_menu, result);
1491             (*fp_gtk_menu_shell_append)((GtkMenuShell *)menu_bar, root_menu);
1492         }
1493         else if (widget_type == COMBO_BOX_ARROW_BUTTON ||
1494                  widget_type == COMBO_BOX_TEXT_FIELD)
1495         {
1496             /*
1497             * We add a regular GtkButton/GtkEntry to a GtkComboBoxEntry
1498             * in order to trick engines into thinking it's a real combobox
1499             * arrow button/text field.
1500             */
1501             GtkWidget *combo = (*fp_gtk_combo_box_entry_new)();
1502 
1503             if (new_combo && widget_type == COMBO_BOX_ARROW_BUTTON) {
1504                 (*fp_gtk_widget_set_parent)(result, combo);
1505                 ((GtkBin*)combo)->child = result;
1506             } else {
1507                 (*fp_gtk_container_add)((GtkContainer *)combo, result);
1508             }
1509             (*fp_gtk_container_add)((GtkContainer *)gtk2_fixed, combo);
1510         }
1511         else if (widget_type != TOOL_TIP &&
1512                  widget_type != INTERNAL_FRAME &&
1513                  widget_type != OPTION_PANE)
1514         {
1515             (*fp_gtk_container_add)((GtkContainer *)gtk2_fixed, result);
1516         }
1517         (*fp_gtk_widget_realize)(result);
1518     }
1519     return result;
1520 }
1521 
1522 void gtk2_paint_arrow(WidgetType widget_type, GtkStateType state_type,
1523         GtkShadowType shadow_type, const gchar *detail,
1524         gint x, gint y, gint width, gint height,
1525         GtkArrowType arrow_type, gboolean fill)
1526 {
1527     static int w, h;
1528     static GtkRequisition size;
1529 
1530     if (widget_type == COMBO_BOX_ARROW_BUTTON || widget_type == TABLE)
1531         gtk2_widget = gtk2_get_arrow(arrow_type, shadow_type);
1532     else
1533         gtk2_widget = gtk2_get_widget(widget_type);
1534 
1535     switch (widget_type)
1536     {
1537         case SPINNER_ARROW_BUTTON:
1538             x = 1;
1539             y = ((arrow_type == GTK_ARROW_UP) ? 2 : 0);
1540             height -= 2;
1541             width -= 3;
1542 
1543             w = width / 2;
1544             w -= w % 2 - 1;
1545             h = (w + 1) / 2;
1546             break;
1547 
1548         case HSCROLL_BAR_BUTTON_LEFT:
1549         case HSCROLL_BAR_BUTTON_RIGHT:
1550         case VSCROLL_BAR_BUTTON_UP:
1551         case VSCROLL_BAR_BUTTON_DOWN:
1552             w = width / 2;
1553             h = height / 2;
1554             break;
1555 
1556         case COMBO_BOX_ARROW_BUTTON:
1557         case TABLE:
1558             x = 1;
1559             (*fp_gtk_widget_size_request)(gtk2_widget, &size);
1560             w = size.width - ((GtkMisc*)gtk2_widget)->xpad * 2;
1561             h = size.height - ((GtkMisc*)gtk2_widget)->ypad * 2;
1562             w = h = MIN(MIN(w, h), MIN(width,height)) * 0.7;
1563             break;
1564 
1565         default:
1566             w = width;
1567             h = height;
1568             break;
1569     }
1570     x += (width - w) / 2;
1571     y += (height - h) / 2;
1572 
1573     (*fp_gtk_paint_arrow)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1574             shadow_type, NULL, gtk2_widget, detail, arrow_type, fill,
1575             x, y, w, h);
1576     (*fp_gtk_paint_arrow)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1577             shadow_type, NULL, gtk2_widget, detail, arrow_type, fill,
1578             x, y, w, h);
1579 }
1580 
1581 void gtk2_paint_box(WidgetType widget_type, GtkStateType state_type,
1582                     GtkShadowType shadow_type, const gchar *detail,
1583                     gint x, gint y, gint width, gint height,
1584                     gint synth_state, GtkTextDirection dir)
1585 {
1586     gtk2_widget = gtk2_get_widget(widget_type);
1587 
1588     /*
1589      * The clearlooks engine sometimes looks at the widget's state field
1590      * instead of just the state_type variable that we pass in, so to account
1591      * for those cases we set the widget's state field accordingly.  The
1592      * flags field is similarly important for things like focus/default state.
1593      */
1594     gtk2_widget->state = state_type;
1595 
1596     if (widget_type == HSLIDER_TRACK) {
1597         /*
1598          * For horizontal JSliders with right-to-left orientation, we need
1599          * to set the "inverted" flag to match the native GTK behavior where
1600          * the foreground highlight is on the right side of the slider thumb.
1601          * This is needed especially for the ubuntulooks engine, which looks
1602          * exclusively at the "inverted" flag to determine on which side of
1603          * the thumb to paint the highlight...
1604          */
1605         ((GtkRange*)gtk2_widget)->inverted = (dir == GTK_TEXT_DIR_RTL);
1606 
1607         /*
1608          * Note however that other engines like clearlooks will look at both
1609          * the "inverted" field and the text direction to determine how
1610          * the foreground highlight is painted:
1611          *     !inverted && ltr --> paint highlight on left side
1612          *     !inverted && rtl --> paint highlight on right side
1613          *      inverted && ltr --> paint highlight on right side
1614          *      inverted && rtl --> paint highlight on left side
1615          * So the only way to reliably get the desired results for horizontal
1616          * JSlider (i.e., highlight on left side for LTR ComponentOrientation
1617          * and highlight on right side for RTL ComponentOrientation) is to
1618          * always override text direction as LTR, and then set the "inverted"
1619          * flag accordingly (as we have done above).
1620          */
1621         dir = GTK_TEXT_DIR_LTR;
1622     }
1623 
1624     /*
1625      * Some engines (e.g. clearlooks) will paint the shadow of certain
1626      * widgets (e.g. COMBO_BOX_ARROW_BUTTON) differently depending on the
1627      * the text direction.
1628      */
1629     gtk2_set_direction(gtk2_widget, dir);
1630 
1631     switch (widget_type) {
1632     case BUTTON:
1633         if (synth_state & DEFAULT) {
1634             ((GtkObject*)gtk2_widget)->flags |= GTK_HAS_DEFAULT;
1635         } else {
1636             ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_DEFAULT;
1637         }
1638         break;
1639     case TOGGLE_BUTTON:
1640         init_toggle_widget(widget_type, synth_state);
1641         break;
1642     case HSCROLL_BAR_BUTTON_LEFT:
1643         /*
1644          * The clearlooks engine will draw a "left" button when:
1645          *   x == w->allocation.x
1646          *
1647          * The ubuntulooks engine will draw a "left" button when:
1648          *   [x,y,width,height]
1649          *     intersects
1650          *   [w->alloc.x,w->alloc.y,width,height]
1651          *
1652          * The values that are set below should ensure that a "left"
1653          * button is rendered for both of these (and other) engines.
1654          */
1655         gtk2_widget->allocation.x = x;
1656         gtk2_widget->allocation.y = y;
1657         gtk2_widget->allocation.width = width;
1658         gtk2_widget->allocation.height = height;
1659         break;
1660     case HSCROLL_BAR_BUTTON_RIGHT:
1661         /*
1662          * The clearlooks engine will draw a "right" button when:
1663          *   x + width == w->allocation.x + w->allocation.width
1664          *
1665          * The ubuntulooks engine will draw a "right" button when:
1666          *   [x,y,width,height]
1667          *     does not intersect
1668          *   [w->alloc.x,w->alloc.y,width,height]
1669          *     but does intersect
1670          *   [w->alloc.x+width,w->alloc.y,width,height]
1671          *
1672          * The values that are set below should ensure that a "right"
1673          * button is rendered for both of these (and other) engines.
1674          */
1675         gtk2_widget->allocation.x = x+width;
1676         gtk2_widget->allocation.y = 0;
1677         gtk2_widget->allocation.width = 0;
1678         gtk2_widget->allocation.height = height;
1679         break;
1680     case VSCROLL_BAR_BUTTON_UP:
1681         /*
1682          * The clearlooks engine will draw an "up" button when:
1683          *   y == w->allocation.y
1684          *
1685          * The ubuntulooks engine will draw an "up" button when:
1686          *   [x,y,width,height]
1687          *     intersects
1688          *   [w->alloc.x,w->alloc.y,width,height]
1689          *
1690          * The values that are set below should ensure that an "up"
1691          * button is rendered for both of these (and other) engines.
1692          */
1693         gtk2_widget->allocation.x = x;
1694         gtk2_widget->allocation.y = y;
1695         gtk2_widget->allocation.width = width;
1696         gtk2_widget->allocation.height = height;
1697         break;
1698     case VSCROLL_BAR_BUTTON_DOWN:
1699         /*
1700          * The clearlooks engine will draw a "down" button when:
1701          *   y + height == w->allocation.y + w->allocation.height
1702          *
1703          * The ubuntulooks engine will draw a "down" button when:
1704          *   [x,y,width,height]
1705          *     does not intersect
1706          *   [w->alloc.x,w->alloc.y,width,height]
1707          *     but does intersect
1708          *   [w->alloc.x,w->alloc.y+height,width,height]
1709          *
1710          * The values that are set below should ensure that a "down"
1711          * button is rendered for both of these (and other) engines.
1712          */
1713         gtk2_widget->allocation.x = x;
1714         gtk2_widget->allocation.y = y+height;
1715         gtk2_widget->allocation.width = width;
1716         gtk2_widget->allocation.height = 0;
1717         break;
1718     default:
1719         break;
1720     }
1721 
1722     (*fp_gtk_paint_box)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1723             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
1724     (*fp_gtk_paint_box)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1725             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
1726 
1727     /*
1728      * Reset the text direction to the default value so that we don't
1729      * accidentally affect other operations and widgets.
1730      */
1731     gtk2_set_direction(gtk2_widget, GTK_TEXT_DIR_LTR);
1732 }
1733 
1734 void gtk2_paint_box_gap(WidgetType widget_type, GtkStateType state_type,
1735         GtkShadowType shadow_type, const gchar *detail,
1736         gint x, gint y, gint width, gint height,
1737         GtkPositionType gap_side, gint gap_x, gint gap_width)
1738 {
1739     /* Clearlooks needs a real clip area to paint the gap properly */
1740     GdkRectangle area = { x, y, width, height };
1741 
1742     gtk2_widget = gtk2_get_widget(widget_type);
1743     (*fp_gtk_paint_box_gap)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1744             shadow_type, &area, gtk2_widget, detail,
1745             x, y, width, height, gap_side, gap_x, gap_width);
1746     (*fp_gtk_paint_box_gap)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1747             shadow_type, &area, gtk2_widget, detail,
1748             x, y, width, height, gap_side, gap_x, gap_width);
1749 }
1750 
1751 void gtk2_paint_check(WidgetType widget_type, gint synth_state,
1752         const gchar *detail, gint x, gint y, gint width, gint height)
1753 {
1754     GtkStateType state_type = get_gtk_state_type(widget_type, synth_state);
1755     GtkShadowType shadow_type = get_gtk_shadow_type(widget_type, synth_state);
1756 
1757     gtk2_widget = gtk2_get_widget(widget_type);
1758     init_toggle_widget(widget_type, synth_state);
1759 
1760     (*fp_gtk_paint_check)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1761             shadow_type, NULL, gtk2_widget, detail,
1762             x, y, width, height);
1763     (*fp_gtk_paint_check)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1764             shadow_type, NULL, gtk2_widget, detail,
1765             x, y, width, height);
1766 }
1767 
1768 void gtk2_paint_diamond(WidgetType widget_type, GtkStateType state_type,
1769         GtkShadowType shadow_type, const gchar *detail,
1770         gint x, gint y, gint width, gint height)
1771 {
1772     gtk2_widget = gtk2_get_widget(widget_type);
1773     (*fp_gtk_paint_diamond)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1774             shadow_type, NULL, gtk2_widget, detail,
1775             x, y, width, height);
1776     (*fp_gtk_paint_diamond)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1777             shadow_type, NULL, gtk2_widget, detail,
1778             x, y, width, height);
1779 }
1780 
1781 void gtk2_paint_expander(WidgetType widget_type, GtkStateType state_type,
1782         const gchar *detail, gint x, gint y, gint width, gint height,
1783         GtkExpanderStyle expander_style)
1784 {
1785     gtk2_widget = gtk2_get_widget(widget_type);
1786     (*fp_gtk_paint_expander)(gtk2_widget->style, gtk2_white_pixmap,
1787             state_type, NULL, gtk2_widget, detail,
1788             x + width / 2, y + height / 2, expander_style);
1789     (*fp_gtk_paint_expander)(gtk2_widget->style, gtk2_black_pixmap,
1790             state_type, NULL, gtk2_widget, detail,
1791             x + width / 2, y + height / 2, expander_style);
1792 }
1793 
1794 void gtk2_paint_extension(WidgetType widget_type, GtkStateType state_type,
1795         GtkShadowType shadow_type, const gchar *detail,
1796         gint x, gint y, gint width, gint height, GtkPositionType gap_side)
1797 {
1798     gtk2_widget = gtk2_get_widget(widget_type);
1799     (*fp_gtk_paint_extension)(gtk2_widget->style, gtk2_white_pixmap,
1800             state_type, shadow_type, NULL, gtk2_widget, detail,
1801             x, y, width, height, gap_side);
1802     (*fp_gtk_paint_extension)(gtk2_widget->style, gtk2_black_pixmap,
1803             state_type, shadow_type, NULL, gtk2_widget, detail,
1804             x, y, width, height, gap_side);
1805 }
1806 
1807 void gtk2_paint_flat_box(WidgetType widget_type, GtkStateType state_type,
1808         GtkShadowType shadow_type, const gchar *detail,
1809         gint x, gint y, gint width, gint height, gboolean has_focus)
1810 {
1811     gtk2_widget = gtk2_get_widget(widget_type);
1812 
1813     if (has_focus)
1814         ((GtkObject*)gtk2_widget)->flags |= GTK_HAS_FOCUS;
1815     else
1816         ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_FOCUS;
1817 
1818     (*fp_gtk_paint_flat_box)(gtk2_widget->style, gtk2_white_pixmap,
1819             state_type, shadow_type, NULL, gtk2_widget, detail,
1820             x, y, width, height);
1821     (*fp_gtk_paint_flat_box)(gtk2_widget->style, gtk2_black_pixmap,
1822             state_type, shadow_type, NULL, gtk2_widget, detail,
1823             x, y, width, height);
1824 }
1825 
1826 void gtk2_paint_focus(WidgetType widget_type, GtkStateType state_type,
1827         const char *detail, gint x, gint y, gint width, gint height)
1828 {
1829     gtk2_widget = gtk2_get_widget(widget_type);
1830     (*fp_gtk_paint_focus)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1831             NULL, gtk2_widget, detail, x, y, width, height);
1832     (*fp_gtk_paint_focus)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1833             NULL, gtk2_widget, detail, x, y, width, height);
1834 }
1835 
1836 void gtk2_paint_handle(WidgetType widget_type, GtkStateType state_type,
1837         GtkShadowType shadow_type, const gchar *detail,
1838         gint x, gint y, gint width, gint height, GtkOrientation orientation)
1839 {
1840     gtk2_widget = gtk2_get_widget(widget_type);
1841     (*fp_gtk_paint_handle)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1842             shadow_type, NULL, gtk2_widget, detail,
1843             x, y, width, height, orientation);
1844     (*fp_gtk_paint_handle)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1845             shadow_type, NULL, gtk2_widget, detail,
1846             x, y, width, height, orientation);
1847 }
1848 
1849 void gtk2_paint_hline(WidgetType widget_type, GtkStateType state_type,
1850         const gchar *detail, gint x, gint y, gint width, gint height)
1851 {
1852     gtk2_widget = gtk2_get_widget(widget_type);
1853     (*fp_gtk_paint_hline)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1854             NULL, gtk2_widget, detail, x, x + width, y);
1855     (*fp_gtk_paint_hline)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1856             NULL, gtk2_widget, detail, x, x + width, y);
1857 }
1858 
1859 void gtk2_paint_option(WidgetType widget_type, gint synth_state,
1860         const gchar *detail, gint x, gint y, gint width, gint height)
1861 {
1862     GtkStateType state_type = get_gtk_state_type(widget_type, synth_state);
1863     GtkShadowType shadow_type = get_gtk_shadow_type(widget_type, synth_state);
1864 
1865     gtk2_widget = gtk2_get_widget(widget_type);
1866     init_toggle_widget(widget_type, synth_state);
1867 
1868     (*fp_gtk_paint_option)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1869             shadow_type, NULL, gtk2_widget, detail,
1870             x, y, width, height);
1871     (*fp_gtk_paint_option)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1872             shadow_type, NULL, gtk2_widget, detail,
1873             x, y, width, height);
1874 }
1875 
1876 void gtk2_paint_shadow(WidgetType widget_type, GtkStateType state_type,
1877                        GtkShadowType shadow_type, const gchar *detail,
1878                        gint x, gint y, gint width, gint height,
1879                        gint synth_state, GtkTextDirection dir)
1880 {
1881     gtk2_widget = gtk2_get_widget(widget_type);
1882 
1883     /*
1884      * The clearlooks engine sometimes looks at the widget's state field
1885      * instead of just the state_type variable that we pass in, so to account
1886      * for those cases we set the widget's state field accordingly.  The
1887      * flags field is similarly important for things like focus state.
1888      */
1889     gtk2_widget->state = state_type;
1890 
1891     /*
1892      * Some engines (e.g. clearlooks) will paint the shadow of certain
1893      * widgets (e.g. COMBO_BOX_TEXT_FIELD) differently depending on the
1894      * the text direction.
1895      */
1896     gtk2_set_direction(gtk2_widget, dir);
1897 
1898     switch (widget_type) {
1899     case COMBO_BOX_TEXT_FIELD:
1900     case FORMATTED_TEXT_FIELD:
1901     case PASSWORD_FIELD:
1902     case SPINNER_TEXT_FIELD:
1903     case TEXT_FIELD:
1904         if (synth_state & FOCUSED) {
1905             ((GtkObject*)gtk2_widget)->flags |= GTK_HAS_FOCUS;
1906         } else {
1907             ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_FOCUS;
1908         }
1909         break;
1910     default:
1911         break;
1912     }
1913 
1914     (*fp_gtk_paint_shadow)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1915             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
1916     (*fp_gtk_paint_shadow)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1917             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
1918 
1919     /*
1920      * Reset the text direction to the default value so that we don't
1921      * accidentally affect other operations and widgets.
1922      */
1923     gtk2_set_direction(gtk2_widget, GTK_TEXT_DIR_LTR);
1924 }
1925 
1926 void gtk2_paint_slider(WidgetType widget_type, GtkStateType state_type,
1927         GtkShadowType shadow_type, const gchar *detail,
1928         gint x, gint y, gint width, gint height, GtkOrientation orientation)
1929 {
1930     gtk2_widget = gtk2_get_widget(widget_type);
1931     (*fp_gtk_paint_slider)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1932             shadow_type, NULL, gtk2_widget, detail,
1933             x, y, width, height, orientation);
1934     (*fp_gtk_paint_slider)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1935             shadow_type, NULL, gtk2_widget, detail,
1936             x, y, width, height, orientation);
1937 }
1938 
1939 void gtk2_paint_vline(WidgetType widget_type, GtkStateType state_type,
1940         const gchar *detail, gint x, gint y, gint width, gint height)
1941 {
1942     gtk2_widget = gtk2_get_widget(widget_type);
1943     (*fp_gtk_paint_vline)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1944             NULL, gtk2_widget, detail, y, y + height, x);
1945     (*fp_gtk_paint_vline)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1946             NULL, gtk2_widget, detail, y, y + height, x);
1947 }
1948 
1949 void gtk_paint_background(WidgetType widget_type, GtkStateType state_type,
1950         gint x, gint y, gint width, gint height)
1951 {
1952     gtk2_widget = gtk2_get_widget(widget_type);
1953     (*fp_gtk_style_apply_default_background)(gtk2_widget->style,
1954             gtk2_white_pixmap, TRUE, state_type, NULL, x, y, width, height);
1955     (*fp_gtk_style_apply_default_background)(gtk2_widget->style,
1956             gtk2_black_pixmap, TRUE, state_type, NULL, x, y, width, height);
1957 }
1958 
1959 GdkPixbuf *gtk2_get_stock_icon(gint widget_type, const gchar *stock_id,
1960         GtkIconSize size, GtkTextDirection direction, const char *detail)
1961 {
1962     init_containers();
1963     gtk2_widget = gtk2_get_widget((widget_type < 0) ? IMAGE : widget_type);
1964     gtk2_widget->state = GTK_STATE_NORMAL;
1965     (*fp_gtk_widget_set_direction)(gtk2_widget, direction);
1966     return (*fp_gtk_widget_render_icon)(gtk2_widget, stock_id, size, detail);
1967 }
1968 
1969 /*************************************************/
1970 gint gtk2_get_xthickness(JNIEnv *env, WidgetType widget_type)
1971 {
1972     init_containers();
1973 
1974     gtk2_widget = gtk2_get_widget(widget_type);
1975     GtkStyle* style = gtk2_widget->style;
1976     return style->xthickness;
1977 }
1978 
1979 gint gtk2_get_ythickness(JNIEnv *env, WidgetType widget_type)
1980 {
1981     init_containers();
1982 
1983     gtk2_widget = gtk2_get_widget(widget_type);
1984     GtkStyle* style = gtk2_widget->style;
1985     return style->ythickness;
1986 }
1987 
1988 /*************************************************/
1989 guint8 recode_color(guint16 channel)
1990 {
1991     return (guint8)(channel>>8);
1992 }
1993 
1994 gint gtk2_get_color_for_state(JNIEnv *env, WidgetType widget_type,
1995                               GtkStateType state_type, ColorType color_type)
1996 {
1997     gint result = 0;
1998     GdkColor *color = NULL;
1999 
2000     init_containers();
2001 
2002     gtk2_widget = gtk2_get_widget(widget_type);
2003     GtkStyle* style = gtk2_widget->style;
2004 
2005     switch (color_type)
2006     {
2007         case FOREGROUND:
2008             color = &(style->fg[state_type]);
2009             break;
2010         case BACKGROUND:
2011             color = &(style->bg[state_type]);
2012             break;
2013         case TEXT_FOREGROUND:
2014             color = &(style->text[state_type]);
2015             break;
2016         case TEXT_BACKGROUND:
2017             color = &(style->base[state_type]);
2018             break;
2019         case LIGHT:
2020             color = &(style->light[state_type]);
2021             break;
2022         case DARK:
2023             color = &(style->dark[state_type]);
2024             break;
2025         case MID:
2026             color = &(style->mid[state_type]);
2027             break;
2028         case FOCUS:
2029         case BLACK:
2030             color = &(style->black);
2031             break;
2032         case WHITE:
2033             color = &(style->white);
2034             break;
2035     }
2036 
2037     if (color)
2038         result = recode_color(color->red)   << 16 |
2039                  recode_color(color->green) << 8  |
2040                  recode_color(color->blue);
2041 
2042     return result;
2043 }
2044 
2045 /*************************************************/
2046 jobject create_Boolean(JNIEnv *env, jboolean boolean_value);
2047 jobject create_Integer(JNIEnv *env, jint int_value);
2048 jobject create_Long(JNIEnv *env, jlong long_value);
2049 jobject create_Float(JNIEnv *env, jfloat float_value);
2050 jobject create_Double(JNIEnv *env, jdouble double_value);
2051 jobject create_Character(JNIEnv *env, jchar char_value);
2052 jobject create_Insets(JNIEnv *env, GtkBorder *border);
2053 
2054 jobject gtk2_get_class_value(JNIEnv *env, WidgetType widget_type, jstring jkey)
2055 {
2056     init_containers();
2057 
2058     const char* key = getStrFor(env, jkey);
2059     gtk2_widget = gtk2_get_widget(widget_type);
2060 
2061     GValue value;
2062     value.g_type = 0;
2063 
2064     GParamSpec* param = (*fp_gtk_widget_class_find_style_property)(
2065                                     ((GTypeInstance*)gtk2_widget)->g_class, key);
2066     if( param )
2067     {
2068         (*fp_g_value_init)( &value, param->value_type );
2069         (*fp_gtk_widget_style_get_property)(gtk2_widget, key, &value);
2070 
2071         if( (*fp_g_type_is_a)( param->value_type, G_TYPE_BOOLEAN ))
2072         {
2073             gboolean val = (*fp_g_value_get_boolean)(&value);
2074             return create_Boolean(env, (jboolean)val);
2075         }
2076         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_CHAR ))
2077         {
2078             gchar val = (*fp_g_value_get_char)(&value);
2079             return create_Character(env, (jchar)val);
2080         }
2081         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_UCHAR ))
2082         {
2083             guchar val = (*fp_g_value_get_uchar)(&value);
2084             return create_Character(env, (jchar)val);
2085         }
2086         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_INT ))
2087         {
2088             gint val = (*fp_g_value_get_int)(&value);
2089             return create_Integer(env, (jint)val);
2090         }
2091         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_UINT ))
2092         {
2093             guint val = (*fp_g_value_get_uint)(&value);
2094             return create_Integer(env, (jint)val);
2095         }
2096         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_LONG ))
2097         {
2098             glong val = (*fp_g_value_get_long)(&value);
2099             return create_Long(env, (jlong)val);
2100         }
2101         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_ULONG ))
2102         {
2103             gulong val = (*fp_g_value_get_ulong)(&value);
2104             return create_Long(env, (jlong)val);
2105         }
2106         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_INT64 ))
2107         {
2108             gint64 val = (*fp_g_value_get_int64)(&value);
2109             return create_Long(env, (jlong)val);
2110         }
2111         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_UINT64 ))
2112         {
2113             guint64 val = (*fp_g_value_get_uint64)(&value);
2114             return create_Long(env, (jlong)val);
2115         }
2116         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_FLOAT ))
2117         {
2118             gfloat val = (*fp_g_value_get_float)(&value);
2119             return create_Float(env, (jfloat)val);
2120         }
2121         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_DOUBLE ))
2122         {
2123             gdouble val = (*fp_g_value_get_double)(&value);
2124             return create_Double(env, (jdouble)val);
2125         }
2126         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_ENUM ))
2127         {
2128             gint val = (*fp_g_value_get_enum)(&value);
2129             return create_Integer(env, (jint)val);
2130         }
2131         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_FLAGS ))
2132         {
2133             guint val = (*fp_g_value_get_flags)(&value);
2134             return create_Integer(env, (jint)val);
2135         }
2136         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_STRING ))
2137         {
2138             const gchar* val = (*fp_g_value_get_string)(&value);
2139 
2140             /* We suppose that all values come in C locale and
2141              * utf-8 representation of a string is the same as
2142              * the string itself. If this isn't so we should
2143              * use g_convert.
2144              */
2145             return (*env)->NewStringUTF(env, val);
2146         }
2147         else if( (*fp_g_type_is_a)( param->value_type, GTK_TYPE_BORDER ))
2148         {
2149             GtkBorder *border = (GtkBorder*)(*fp_g_value_get_boxed)(&value);
2150             return border ? create_Insets(env, border) : NULL;
2151         }
2152 
2153         /*      TODO: Other types are not supported yet.*/
2154 /*        else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_PARAM ))
2155         {
2156             GParamSpec* val = (*fp_g_value_get_param)(&value);
2157             printf( "Param: %p\n", val );
2158         }
2159         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_BOXED ))
2160         {
2161             gpointer* val = (*fp_g_value_get_boxed)(&value);
2162             printf( "Boxed: %p\n", val );
2163         }
2164         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_POINTER ))
2165         {
2166             gpointer* val = (*fp_g_value_get_pointer)(&value);
2167             printf( "Pointer: %p\n", val );
2168         }
2169         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_OBJECT ))
2170         {
2171             GObject* val = (GObject*)(*fp_g_value_get_object)(&value);
2172             printf( "Object: %p\n", val );
2173         }*/
2174     }
2175 
2176     return NULL;
2177 }
2178 
2179 void gtk2_set_range_value(WidgetType widget_type, jdouble value,
2180                           jdouble min, jdouble max, jdouble visible)
2181 {
2182     GtkAdjustment *adj;
2183 
2184     gtk2_widget = gtk2_get_widget(widget_type);
2185 
2186     adj = (*fp_gtk_range_get_adjustment)((GtkRange *)gtk2_widget);
2187     adj->value = (gdouble)value;
2188     adj->lower = (gdouble)min;
2189     adj->upper = (gdouble)max;
2190     adj->page_size = (gdouble)visible;
2191 }
2192 
2193 /*************************************************/
2194 jobject create_Object(JNIEnv *env, jmethodID *cid,
2195                              const char* class_name,
2196                              const char* signature,
2197                              jvalue* value)
2198 {
2199     jclass  class;
2200     jobject result;
2201 
2202     class = (*env)->FindClass(env, class_name);
2203     if( class == NULL )
2204         return NULL; /* can't find/load the class, exception thrown */
2205 
2206     if( *cid == NULL)
2207     {
2208         *cid = (*env)->GetMethodID(env, class, "<init>", signature);
2209         if( *cid == NULL )
2210         {
2211             (*env)->DeleteLocalRef(env, class);
2212             return NULL; /* can't find/get the method, exception thrown */
2213         }
2214     }
2215 
2216     result = (*env)->NewObjectA(env, class, *cid, value);
2217 
2218     (*env)->DeleteLocalRef(env, class);
2219     return result;
2220 }
2221 
2222 jobject create_Boolean(JNIEnv *env, jboolean boolean_value)
2223 {
2224     static jmethodID cid = NULL;
2225     jvalue value;
2226 
2227     value.z = boolean_value;
2228 
2229     return create_Object(env, &cid, "java/lang/Boolean", "(Z)V", &value);
2230 }
2231 
2232 jobject create_Integer(JNIEnv *env, jint int_value)
2233 {
2234     static jmethodID cid = NULL;
2235     jvalue value;
2236 
2237     value.i = int_value;
2238 
2239     return create_Object(env, &cid, "java/lang/Integer", "(I)V", &value);
2240 }
2241 
2242 jobject create_Long(JNIEnv *env, jlong long_value)
2243 {
2244     static jmethodID cid = NULL;
2245     jvalue value;
2246 
2247     value.j = long_value;
2248 
2249     return create_Object(env, &cid, "java/lang/Long", "(J)V", &value);
2250 }
2251 
2252 jobject create_Float(JNIEnv *env, jfloat float_value)
2253 {
2254     static jmethodID cid = NULL;
2255     jvalue value;
2256 
2257     value.f = float_value;
2258 
2259     return create_Object(env, &cid, "java/lang/Float", "(F)V", &value);
2260 }
2261 
2262 jobject create_Double(JNIEnv *env, jdouble double_value)
2263 {
2264     static jmethodID cid = NULL;
2265     jvalue value;
2266 
2267     value.d = double_value;
2268 
2269     return create_Object(env, &cid, "java/lang/Double", "(D)V", &value);
2270 }
2271 
2272 jobject create_Character(JNIEnv *env, jchar char_value)
2273 {
2274     static jmethodID cid = NULL;
2275     jvalue value;
2276 
2277     value.c = char_value;
2278 
2279     return create_Object(env, &cid, "java/lang/Character", "(C)V", &value);
2280 }
2281 
2282 
2283 jobject create_Insets(JNIEnv *env, GtkBorder *border)
2284 {
2285     static jmethodID cid = NULL;
2286     jvalue values[4];
2287 
2288     values[0].i = border->top;
2289     values[1].i = border->left;
2290     values[2].i = border->bottom;
2291     values[3].i = border->right;
2292 
2293     return create_Object(env, &cid, "java/awt/Insets", "(IIII)V", values);
2294 }
2295 
2296 /*********************************************/
2297 jstring gtk2_get_pango_font_name(JNIEnv *env, WidgetType widget_type)
2298 {
2299     init_containers();
2300 
2301     gtk2_widget = gtk2_get_widget(widget_type);
2302     jstring  result = NULL;
2303     GtkStyle* style = gtk2_widget->style;
2304 
2305     if (style && style->font_desc)
2306     {
2307         gchar* val = (*fp_pango_font_description_to_string)(style->font_desc);
2308         result = (*env)->NewStringUTF(env, val);
2309         (*fp_g_free)( val );
2310     }
2311 
2312     return result;
2313 }
2314 
2315 /***********************************************/
2316 jobject get_string_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2317 {
2318     jobject result = NULL;
2319     gchar*  strval = NULL;
2320 
2321     (*fp_g_object_get)(settings, key, &strval, NULL);
2322     result = (*env)->NewStringUTF(env, strval);
2323     (*fp_g_free)(strval);
2324 
2325     return result;
2326 }
2327 /*
2328 jobject get_integer_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2329 {
2330     gint    intval = NULL;
2331 
2332     (*fp_g_object_get)(settings, key, &intval, NULL);
2333     return create_Integer(env, intval);
2334 }*/
2335 
2336 jobject gtk2_get_setting(JNIEnv *env, Setting property)
2337 {
2338     GtkSettings* settings = (*fp_gtk_settings_get_default)();
2339 
2340     switch (property)
2341     {
2342         case GTK_FONT_NAME:
2343             return get_string_property(env, settings, "gtk-font-name");
2344         case GTK_ICON_SIZES:
2345             return get_string_property(env, settings, "gtk-icon-sizes");
2346     }
2347 
2348     return NULL;
2349 }