< prev index next >

src/solaris/native/sun/awt/gtk2_interface.c

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 #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 #include "jvm_md.h"
  34 #include "sizecalc.h"
  35 #include <jni_util.h>
  36 #include "awt.h"
  37 
  38 #define GTK2_LIB_VERSIONED VERSIONED_JNI_LIB_NAME("gtk-x11-2.0", "0")
  39 #define GTK2_LIB JNI_LIB_NAME("gtk-x11-2.0")
  40 #define GTHREAD_LIB_VERSIONED VERSIONED_JNI_LIB_NAME("gthread-2.0", "0")
  41 #define GTHREAD_LIB JNI_LIB_NAME("gthread-2.0")
  42 
  43 #define G_TYPE_INVALID                  G_TYPE_MAKE_FUNDAMENTAL (0)
  44 #define G_TYPE_NONE                     G_TYPE_MAKE_FUNDAMENTAL (1)
  45 #define G_TYPE_INTERFACE                G_TYPE_MAKE_FUNDAMENTAL (2)
  46 #define G_TYPE_CHAR                     G_TYPE_MAKE_FUNDAMENTAL (3)
  47 #define G_TYPE_UCHAR                    G_TYPE_MAKE_FUNDAMENTAL (4)
  48 #define G_TYPE_BOOLEAN                  G_TYPE_MAKE_FUNDAMENTAL (5)
  49 #define G_TYPE_INT                      G_TYPE_MAKE_FUNDAMENTAL (6)
  50 #define G_TYPE_UINT                     G_TYPE_MAKE_FUNDAMENTAL (7)
  51 #define G_TYPE_LONG                     G_TYPE_MAKE_FUNDAMENTAL (8)
  52 #define G_TYPE_ULONG                    G_TYPE_MAKE_FUNDAMENTAL (9)
  53 #define G_TYPE_INT64                    G_TYPE_MAKE_FUNDAMENTAL (10)
  54 #define G_TYPE_UINT64                   G_TYPE_MAKE_FUNDAMENTAL (11)
  55 #define G_TYPE_ENUM                     G_TYPE_MAKE_FUNDAMENTAL (12)
  56 #define G_TYPE_FLAGS                    G_TYPE_MAKE_FUNDAMENTAL (13)
  57 #define G_TYPE_FLOAT                    G_TYPE_MAKE_FUNDAMENTAL (14)
  58 #define G_TYPE_DOUBLE                   G_TYPE_MAKE_FUNDAMENTAL (15)
  59 #define G_TYPE_STRING                   G_TYPE_MAKE_FUNDAMENTAL (16)
  60 #define G_TYPE_POINTER                  G_TYPE_MAKE_FUNDAMENTAL (17)
  61 #define G_TYPE_BOXED                    G_TYPE_MAKE_FUNDAMENTAL (18)
  62 #define G_TYPE_PARAM                    G_TYPE_MAKE_FUNDAMENTAL (19)
  63 #define G_TYPE_OBJECT                   G_TYPE_MAKE_FUNDAMENTAL (20)
  64 
  65 #define GTK_TYPE_BORDER                 ((*fp_gtk_border_get_type)())
  66 
  67 #define G_TYPE_FUNDAMENTAL_SHIFT        (2)
  68 #define G_TYPE_MAKE_FUNDAMENTAL(x)      ((GType) ((x) << G_TYPE_FUNDAMENTAL_SHIFT))
  69 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
  70 
  71 #define CONV_BUFFER_SIZE 128
  72 
  73 #define NO_SYMBOL_EXCEPTION 1
  74 
  75 /* SynthConstants */
  76 const gint ENABLED    = 1 << 0;
  77 const gint MOUSE_OVER = 1 << 1;
  78 const gint PRESSED    = 1 << 2;
  79 const gint DISABLED   = 1 << 3;
  80 const gint FOCUSED    = 1 << 8;
  81 const gint SELECTED   = 1 << 9;
  82 const gint DEFAULT    = 1 << 10;
  83 
  84 static void *gtk2_libhandle = NULL;
  85 static void *gthread_libhandle = NULL;
  86 
  87 static jmp_buf j;
  88 
  89 /* Widgets */
  90 static GtkWidget *gtk2_widget = NULL;
  91 static GtkWidget *gtk2_window = NULL;
  92 static GtkFixed  *gtk2_fixed  = NULL;
  93 
  94 /* Paint system */
  95 static GdkPixmap *gtk2_white_pixmap = NULL;
  96 static GdkPixmap *gtk2_black_pixmap = NULL;
  97 static GdkPixbuf *gtk2_white_pixbuf = NULL;
  98 static GdkPixbuf *gtk2_black_pixbuf = NULL;
  99 static int gtk2_pixbuf_width = 0;
 100 static int gtk2_pixbuf_height = 0;
 101 
 102 /* Static buffer for conversion from java.lang.String to UTF-8 */
 103 static char convertionBuffer[CONV_BUFFER_SIZE];
 104 
 105 static gboolean new_combo = TRUE;
 106 const char ENV_PREFIX[] = "GTK_MODULES=";
 107 
 108 /*******************/
 109 enum GtkWidgetType
 110 {
 111     _GTK_ARROW_TYPE,
 112     _GTK_BUTTON_TYPE,
 113     _GTK_CHECK_BUTTON_TYPE,
 114     _GTK_CHECK_MENU_ITEM_TYPE,
 115     _GTK_COLOR_SELECTION_DIALOG_TYPE,
 116     _GTK_COMBO_BOX_TYPE,
 117     _GTK_COMBO_BOX_ARROW_BUTTON_TYPE,
 118     _GTK_COMBO_BOX_TEXT_FIELD_TYPE,
 119     _GTK_CONTAINER_TYPE,
 120     _GTK_ENTRY_TYPE,
 121     _GTK_FRAME_TYPE,
 122     _GTK_HANDLE_BOX_TYPE,
 123     _GTK_HPANED_TYPE,
 124     _GTK_HPROGRESS_BAR_TYPE,
 125     _GTK_HSCALE_TYPE,
 126     _GTK_HSCROLLBAR_TYPE,
 127     _GTK_HSEPARATOR_TYPE,
 128     _GTK_IMAGE_TYPE,
 129     _GTK_MENU_TYPE,
 130     _GTK_MENU_BAR_TYPE,
 131     _GTK_MENU_ITEM_TYPE,
 132     _GTK_NOTEBOOK_TYPE,
 133     _GTK_LABEL_TYPE,
 134     _GTK_RADIO_BUTTON_TYPE,
 135     _GTK_RADIO_MENU_ITEM_TYPE,
 136     _GTK_SCROLLED_WINDOW_TYPE,
 137     _GTK_SEPARATOR_MENU_ITEM_TYPE,
 138     _GTK_SEPARATOR_TOOL_ITEM_TYPE,
 139     _GTK_SPIN_BUTTON_TYPE,
 140     _GTK_TEXT_VIEW_TYPE,
 141     _GTK_TOGGLE_BUTTON_TYPE,
 142     _GTK_TOOLBAR_TYPE,
 143     _GTK_TOOLTIP_TYPE,
 144     _GTK_TREE_VIEW_TYPE,
 145     _GTK_VIEWPORT_TYPE,
 146     _GTK_VPANED_TYPE,
 147     _GTK_VPROGRESS_BAR_TYPE,
 148     _GTK_VSCALE_TYPE,
 149     _GTK_VSCROLLBAR_TYPE,
 150     _GTK_VSEPARATOR_TYPE,
 151     _GTK_WINDOW_TYPE,
 152     _GTK_DIALOG_TYPE,
 153     _GTK_WIDGET_TYPE_SIZE
 154 };
 155 
 156 
 157 static GtkWidget *gtk2_widgets[_GTK_WIDGET_TYPE_SIZE];
 158 
 159 /*************************
 160  * Glib function pointers
 161  *************************/
 162 
 163 static gboolean (*fp_g_main_context_iteration)(GMainContext *context,
 164                                              gboolean may_block);
 165 
 166 static GValue*      (*fp_g_value_init)(GValue *value, GType g_type);
 167 static gboolean     (*fp_g_type_is_a)(GType type, GType is_a_type);
 168 static gboolean     (*fp_g_value_get_boolean)(const GValue *value);
 169 static gchar        (*fp_g_value_get_char)(const GValue *value);
 170 static guchar       (*fp_g_value_get_uchar)(const GValue *value);
 171 static gint         (*fp_g_value_get_int)(const GValue *value);
 172 static guint        (*fp_g_value_get_uint)(const GValue *value);
 173 static glong        (*fp_g_value_get_long)(const GValue *value);
 174 static gulong       (*fp_g_value_get_ulong)(const GValue *value);
 175 static gint64       (*fp_g_value_get_int64)(const GValue *value);
 176 static guint64      (*fp_g_value_get_uint64)(const GValue *value);


 345         const gchar *first_property_name, ...);
 346 static void (*fp_gtk_widget_class_install_style_property)(
 347         GtkWidgetClass* class, GParamSpec *pspec);
 348 static GParamSpec* (*fp_gtk_widget_class_find_style_property)(
 349         GtkWidgetClass* class, const gchar* property_name);
 350 static void (*fp_gtk_widget_style_get_property)(GtkWidget* widget,
 351         const gchar* property_name, GValue* value);
 352 static char* (*fp_pango_font_description_to_string)(
 353         const PangoFontDescription* fd);
 354 static GtkSettings* (*fp_gtk_settings_get_default)();
 355 static GtkSettings* (*fp_gtk_widget_get_settings)(GtkWidget *widget);
 356 static GType        (*fp_gtk_border_get_type)();
 357 static void (*fp_gtk_arrow_set)(GtkWidget* arrow,
 358                                 GtkArrowType arrow_type,
 359                                 GtkShadowType shadow_type);
 360 static void (*fp_gtk_widget_size_request)(GtkWidget *widget,
 361                                           GtkRequisition *requisition);
 362 static GtkAdjustment* (*fp_gtk_range_get_adjustment)(GtkRange* range);
 363 
 364 /* Method bodies */
 365 const char *getStrFor(JNIEnv *env, jstring val)
 366 {
 367     int length = (*env)->GetStringLength(env, val);
 368     if (length > CONV_BUFFER_SIZE-1)
 369     {
 370         length = CONV_BUFFER_SIZE-1;
 371 #ifdef INTERNAL_BUILD
 372         fprintf(stderr, "Note: Detail is too long: %d chars\n", length);
 373 #endif /* INTERNAL_BUILD */
 374     }
 375 
 376     (*env)->GetStringUTFRegion(env, val, 0, length, convertionBuffer);
 377     return convertionBuffer;
 378 }
 379 
 380 static void throw_exception(JNIEnv *env, const char* name, const char* message)
 381 {
 382     jclass class = (*env)->FindClass(env, name);
 383 
 384     if (class != NULL)
 385         (*env)->ThrowNew(env, class, message);
 386 
 387     (*env)->DeleteLocalRef(env, class);
 388 }
 389 
 390 /* This is a workaround for the bug:
 391  * http://sourceware.org/bugzilla/show_bug.cgi?id=1814
 392  * (dlsym/dlopen clears dlerror state)
 393  * This bug is specific to Linux, but there is no harm in
 394  * applying this workaround on Solaris as well.
 395  */
 396 static void* dl_symbol(const char* name)
 397 {
 398     void* result = dlsym(gtk2_libhandle, name);
 399     if (!result)
 400         longjmp(j, NO_SYMBOL_EXCEPTION);
 401 
 402     return result;
 403 }
 404 
 405 static void* dl_symbol_gthread(const char* name)
 406 {
 407     void* result = dlsym(gthread_libhandle, name);
 408     if (!result)
 409         longjmp(j, NO_SYMBOL_EXCEPTION);
 410 
 411     return result;
 412 }
 413 
 414 gboolean gtk2_check_version()
 415 {
 416     if (gtk2_libhandle != NULL) {
 417         /* We've already successfully opened the GTK libs, so return true. */
 418         return TRUE;
 419     } else {
 420         void *lib = NULL;
 421         gboolean result = FALSE;














 422 
 423         lib = dlopen(GTK2_LIB_VERSIONED, RTLD_LAZY | RTLD_LOCAL);
 424         if (lib == NULL) {
 425             lib = dlopen(GTK2_LIB, RTLD_LAZY | RTLD_LOCAL);
 426             if (lib == NULL) {
 427                 return FALSE;
 428             }
 429         }
 430 
 431         fp_gtk_check_version = dlsym(lib, "gtk_check_version");
 432         /* Check for GTK 2.2+ */
 433         if (!fp_gtk_check_version(2, 2, 0)) {
 434             result = TRUE;
 435         }
 436 
 437         // 8048289: workaround for https://bugzilla.gnome.org/show_bug.cgi?id=733065
 438         // dlclose(lib);
 439 
 440         return result;
 441     }
 442 }
 443 
 444 #define ADD_SUPPORTED_ACTION(actionStr) \
 445 do { \
 446     jfieldID fld_action = (*env)->GetStaticFieldID(env, cls_action, actionStr, "Ljava/awt/Desktop$Action;"); \
 447     if (!(*env)->ExceptionCheck(env)) { \
 448         jobject action = (*env)->GetStaticObjectField(env, cls_action, fld_action); \
 449         (*env)->CallBooleanMethod(env, supportedActions, mid_arrayListAdd, action); \
 450     } else { \
 451         (*env)->ExceptionClear(env); \
 452     } \
 453 } while(0);
 454 
 455 
 456 void update_supported_actions(JNIEnv *env) {
 457     GVfs * (*fp_g_vfs_get_default) (void);
 458     const gchar * const * (*fp_g_vfs_get_supported_uri_schemes) (GVfs * vfs);
 459     const gchar * const * schemes = NULL;
 460 
 461     jclass cls_action = (*env)->FindClass(env, "java/awt/Desktop$Action");
 462     CHECK_NULL(cls_action);
 463     jclass cls_xDesktopPeer = (*env)->FindClass(env, "sun/awt/X11/XDesktopPeer");
 464     CHECK_NULL(cls_xDesktopPeer);
 465     jfieldID fld_supportedActions = (*env)->GetStaticFieldID(env, cls_xDesktopPeer, "supportedActions", "Ljava/util/List;");
 466     CHECK_NULL(fld_supportedActions);
 467     jobject supportedActions = (*env)->GetStaticObjectField(env, cls_xDesktopPeer, fld_supportedActions);
 468 
 469     jclass cls_arrayList = (*env)->FindClass(env, "java/util/ArrayList");
 470     CHECK_NULL(cls_arrayList);
 471     jmethodID mid_arrayListAdd = (*env)->GetMethodID(env, cls_arrayList, "add", "(Ljava/lang/Object;)Z");
 472     CHECK_NULL(mid_arrayListAdd);
 473     jmethodID mid_arrayListClear = (*env)->GetMethodID(env, cls_arrayList, "clear", "()V");
 474     CHECK_NULL(mid_arrayListClear);
 475 
 476     (*env)->CallVoidMethod(env, supportedActions, mid_arrayListClear);


 499             int i = 0;
 500             while (schemes[i]) {
 501                 if (strcmp(schemes[i], "http") == 0) {
 502                     ADD_SUPPORTED_ACTION("BROWSE");
 503                     ADD_SUPPORTED_ACTION("MAIL");
 504                     break;
 505                 }
 506                 i++;
 507             }
 508         }
 509     } else {
 510 #ifdef INTERNAL_BUILD
 511         fprintf(stderr, "Cannot load g_vfs_get_supported_uri_schemes\n");
 512 #endif /* INTERNAL_BUILD */
 513     }
 514 
 515 }
 516 /**
 517  * Functions for awt_Desktop.c
 518  */
 519 gboolean gtk2_show_uri_load(JNIEnv *env) {
 520      gboolean success = FALSE;
 521      dlerror();
 522      const char *gtk_version = fp_gtk_check_version(2, 14, 0);
 523      if (gtk_version != NULL) {
 524          // The gtk_show_uri is available from GTK+ 2.14
 525 #ifdef INTERNAL_BUILD
 526          fprintf (stderr, "The version of GTK is %s. "
 527              "The gtk_show_uri function is supported "
 528              "since GTK+ 2.14.\n", gtk_version);
 529 #endif /* INTERNAL_BUILD */
 530      } else {
 531          // Loading symbols only if the GTK version is 2.14 and higher
 532          fp_gtk_show_uri = dl_symbol("gtk_show_uri");
 533          const char *dlsym_error = dlerror();
 534          if (dlsym_error) {
 535 #ifdef INTERNAL_BUILD
 536              fprintf (stderr, "Cannot load symbol: %s \n", dlsym_error);
 537 #endif /* INTERNAL_BUILD */
 538          } else if (fp_gtk_show_uri == NULL) {
 539 #ifdef INTERNAL_BUILD
 540              fprintf(stderr, "dlsym(gtk_show_uri) returned NULL\n");
 541 #endif /* INTERNAL_BUILD */
 542         } else {

 543             update_supported_actions(env);
 544             success = TRUE;
 545         }
 546      }
 547      return success;
 548 }
 549 
 550 /**
 551  * Functions for sun_awt_X11_GtkFileDialogPeer.c
 552  */
 553 void gtk2_file_chooser_load()
 554 {
 555     fp_gtk_file_chooser_get_filename = dl_symbol(
 556             "gtk_file_chooser_get_filename");
 557     fp_gtk_file_chooser_dialog_new = dl_symbol("gtk_file_chooser_dialog_new");
 558     fp_gtk_file_chooser_set_current_folder = dl_symbol(
 559             "gtk_file_chooser_set_current_folder");
 560     fp_gtk_file_chooser_set_filename = dl_symbol(
 561             "gtk_file_chooser_set_filename");
 562     fp_gtk_file_chooser_set_current_name = dl_symbol(
 563             "gtk_file_chooser_set_current_name");
 564     fp_gtk_file_filter_add_custom = dl_symbol("gtk_file_filter_add_custom");
 565     fp_gtk_file_chooser_set_filter = dl_symbol("gtk_file_chooser_set_filter");
 566     fp_gtk_file_chooser_get_type = dl_symbol("gtk_file_chooser_get_type");
 567     fp_gtk_file_filter_new = dl_symbol("gtk_file_filter_new");
 568     if (fp_gtk_check_version(2, 8, 0) == NULL) {
 569         fp_gtk_file_chooser_set_do_overwrite_confirmation = dl_symbol(
 570                 "gtk_file_chooser_set_do_overwrite_confirmation");
 571     }
 572     fp_gtk_file_chooser_set_select_multiple = dl_symbol(
 573             "gtk_file_chooser_set_select_multiple");
 574     fp_gtk_file_chooser_get_current_folder = dl_symbol(
 575             "gtk_file_chooser_get_current_folder");
 576     fp_gtk_file_chooser_get_filenames = dl_symbol(
 577             "gtk_file_chooser_get_filenames");
 578     fp_gtk_g_slist_length = dl_symbol("g_slist_length");
 579 }
 580 
 581 gboolean gtk2_load(JNIEnv *env)
 582 {
 583     gboolean result;
 584     int i;
 585     int (*handler)();
 586     int (*io_handler)();
 587     char *gtk_modules_env;
 588 
 589     gtk2_libhandle = dlopen(GTK2_LIB_VERSIONED, RTLD_LAZY | RTLD_LOCAL);
 590     if (gtk2_libhandle == NULL) {
 591         gtk2_libhandle = dlopen(GTK2_LIB, RTLD_LAZY | RTLD_LOCAL);
 592         if (gtk2_libhandle == NULL)
 593             return FALSE;
 594     }
 595 
 596     gthread_libhandle = dlopen(GTHREAD_LIB_VERSIONED, RTLD_LAZY | RTLD_LOCAL);
 597     if (gthread_libhandle == NULL) {
 598         gthread_libhandle = dlopen(GTHREAD_LIB, RTLD_LAZY | RTLD_LOCAL);
 599         if (gthread_libhandle == NULL)
 600             return FALSE;
 601     }
 602 
 603     if (setjmp(j) == 0)
 604     {
 605         fp_gtk_check_version = dl_symbol("gtk_check_version");
 606         /* Check for GTK 2.2+ */
 607         if (fp_gtk_check_version(2, 2, 0)) {
 608             longjmp(j, NO_SYMBOL_EXCEPTION);
 609         }
 610 
 611         /* GLib */
 612         fp_glib_check_version = dlsym(gtk2_libhandle, "glib_check_version");


 874                     /* no free() on success, putenv() doesn't copy string */
 875                     free(new_env);
 876                 }
 877             }
 878             free(tmp_env);
 879         }
 880     }
 881     /*
 882      * GTK should be initialized with gtk_init_check() before use.
 883      *
 884      * gtk_init_check installs its own error handlers. It is critical that
 885      * we preserve error handler set from AWT. Otherwise we'll crash on
 886      * BadMatch errors which we would normally ignore. The IO error handler
 887      * is preserved here, too, just for consistency.
 888     */
 889     AWT_LOCK();
 890     handler = XSetErrorHandler(NULL);
 891     io_handler = XSetIOErrorHandler(NULL);
 892 
 893     if (fp_gtk_check_version(2, 2, 0) == NULL) {
 894         jclass clazz = (*env)->FindClass(env, "sun/misc/GThreadHelper");
 895         jmethodID mid_getAndSetInitializationNeededFlag =
 896                 (*env)->GetStaticMethodID(env, clazz, "getAndSetInitializationNeededFlag", "()Z");
 897         jmethodID mid_lock = (*env)->GetStaticMethodID(env, clazz, "lock", "()V");
 898         jmethodID mid_unlock = (*env)->GetStaticMethodID(env, clazz, "unlock", "()V");
 899 
 900         // Init the thread system to use GLib in a thread-safe mode
 901         (*env)->CallStaticVoidMethod(env, clazz, mid_lock);
 902 
 903         // Calling g_thread_init() multiple times leads to crash on GLib < 2.24
 904         // We can use g_thread_get_initialized () but it is available only for
 905         // GLib >= 2.20. We rely on GThreadHelper for GLib < 2.20.
 906         gboolean is_g_thread_get_initialized = FALSE;
 907         if (GLIB_CHECK_VERSION(2, 20, 0)) {
 908             is_g_thread_get_initialized = fp_g_thread_get_initialized();
 909         }
 910 
 911         if (!(*env)->CallStaticBooleanMethod(env, clazz, mid_getAndSetInitializationNeededFlag)) {
 912             if (!is_g_thread_get_initialized) {
 913                 fp_g_thread_init(NULL);
 914             }
 915 
 916             //According the GTK documentation, gdk_threads_init() should be
 917             //called before gtk_init() or gtk_init_check()
 918             fp_gdk_threads_init();
 919         }
 920         (*env)->CallStaticVoidMethod(env, clazz, mid_unlock);
 921     }
 922     result = (*fp_gtk_init_check)(NULL, NULL);
 923 
 924     XSetErrorHandler(handler);
 925     XSetIOErrorHandler(io_handler);
 926     AWT_UNLOCK();
 927 
 928     /* Initialize widget array. */
 929     for (i = 0; i < _GTK_WIDGET_TYPE_SIZE; i++)
 930     {
 931         gtk2_widgets[i] = NULL;
 932     }
 933 
 934     return result;






 935 }
 936 
 937 int gtk2_unload()
 938 {
 939     int i;
 940     char *gtk2_error;
 941 
 942     if (!gtk2_libhandle)
 943         return TRUE;
 944 
 945     /* Release painting objects */
 946     if (gtk2_white_pixmap != NULL) {
 947         (*fp_g_object_unref)(gtk2_white_pixmap);
 948         (*fp_g_object_unref)(gtk2_black_pixmap);
 949         (*fp_g_object_unref)(gtk2_white_pixbuf);
 950         (*fp_g_object_unref)(gtk2_black_pixbuf);
 951         gtk2_white_pixmap = gtk2_black_pixmap =
 952             gtk2_white_pixbuf = gtk2_black_pixbuf = NULL;
 953     }
 954     gtk2_pixbuf_width = 0;


 958         /* Destroying toplevel widget will destroy all contained widgets */
 959         (*fp_gtk_widget_destroy)(gtk2_window);
 960 
 961         /* Unset some static data so they get reinitialized on next load */
 962         gtk2_window = NULL;
 963     }
 964 
 965     dlerror();
 966     dlclose(gtk2_libhandle);
 967     dlclose(gthread_libhandle);
 968     if ((gtk2_error = dlerror()) != NULL)
 969     {
 970         return FALSE;
 971     }
 972     return TRUE;
 973 }
 974 
 975 /* Dispatch all pending events from the GTK event loop.
 976  * This is needed to catch theme change and update widgets' style.
 977  */
 978 void flush_gtk_event_loop()
 979 {
 980     while( (*fp_g_main_context_iteration)(NULL, FALSE));
 981 }
 982 
 983 /*
 984  * Initialize components of containment hierarchy. This creates a GtkFixed
 985  * inside a GtkWindow. All widgets get realized.
 986  */
 987 static void init_containers()
 988 {
 989     if (gtk2_window == NULL)
 990     {
 991         gtk2_window = (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
 992         gtk2_fixed = (GtkFixed *)(*fp_gtk_fixed_new)();
 993         (*fp_gtk_container_add)((GtkContainer*)gtk2_window,
 994                                 (GtkWidget *)gtk2_fixed);
 995         (*fp_gtk_widget_realize)(gtk2_window);
 996         (*fp_gtk_widget_realize)((GtkWidget *)gtk2_fixed);
 997     }
 998 }


1007  * work around this:
1008  * 1) Subclass GdkPixmap and cache translucent objects on client side. This
1009  * requires us to implement parts of X server drawing logic on client side.
1010  * Many X requests can potentially be "translucent"; e.g. XDrawLine with
1011  * fill=tile and a translucent tile is a "translucent" operation, whereas
1012  * XDrawLine with fill=solid is an "opaque" one. Moreover themes can (and some
1013  * do) intermix transparent and opaque operations which makes caching even
1014  * more problematic.
1015  * 2) Use Xorg 32bit ARGB visual when available. GDK has no native support
1016  * for it (as of version 2.6). Also even in JDS 3 Xorg does not support
1017  * these visuals by default, which makes optimizing for them pointless.
1018  * We can consider doing this at a later point when ARGB visuals become more
1019  * popular.
1020  * 3') GTK has plans to use Cairo as its graphical backend (presumably in
1021  * 2.8), and Cairo supports alpha. With it we could also get rid of the
1022  * unnecessary round trip to server and do all the drawing on client side.
1023  * 4) For now we draw to two different pixmaps and restore alpha channel by
1024  * comparing results. This can be optimized by using subclassed pixmap and
1025  * doing the second drawing only if necessary.
1026 */
1027 void gtk2_init_painting(JNIEnv *env, gint width, gint height)
1028 {
1029     GdkGC *gc;
1030     GdkPixbuf *white, *black;
1031 
1032     init_containers();
1033 
1034     if (gtk2_pixbuf_width < width || gtk2_pixbuf_height < height)
1035     {
1036         white = (*fp_gdk_pixbuf_new)(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
1037         black = (*fp_gdk_pixbuf_new)(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
1038 
1039         if (white == NULL || black == NULL)
1040         {
1041             snprintf(convertionBuffer, CONV_BUFFER_SIZE, "Couldn't create pixbuf of size %dx%d", width, height);
1042             throw_exception(env, "java/lang/RuntimeException", convertionBuffer);
1043             fp_gdk_threads_leave();
1044             return;
1045         }
1046 
1047         if (gtk2_white_pixmap != NULL) {


1067     (*fp_gdk_rgb_gc_set_foreground)(gc, 0xffffff);
1068     (*fp_gdk_draw_rectangle)(gtk2_white_pixmap, gc, TRUE, 0, 0, width, height);
1069     (*fp_g_object_unref)(gc);
1070 
1071     gc = (*fp_gdk_gc_new)(gtk2_black_pixmap);
1072     (*fp_gdk_rgb_gc_set_foreground)(gc, 0x000000);
1073     (*fp_gdk_draw_rectangle)(gtk2_black_pixmap, gc, TRUE, 0, 0, width, height);
1074     (*fp_g_object_unref)(gc);
1075 }
1076 
1077 /*
1078  * Restore image from white and black pixmaps and copy it into destination
1079  * buffer. This method compares two pixbufs taken from white and black
1080  * pixmaps and decodes color and alpha components. Pixbufs are RGB without
1081  * alpha, destination buffer is ABGR.
1082  *
1083  * The return value is the transparency type of the resulting image, either
1084  * one of java_awt_Transparency_OPAQUE, java_awt_Transparency_BITMASK, and
1085  * java_awt_Transparency_TRANSLUCENT.
1086  */
1087 gint gtk2_copy_image(gint *dst, gint width, gint height)
1088 {
1089     gint i, j, r, g, b;
1090     guchar *white, *black;
1091     gint stride, padding;
1092     gboolean is_opaque = TRUE;
1093     gboolean is_bitmask = TRUE;
1094 
1095     (*fp_gdk_pixbuf_get_from_drawable)(gtk2_white_pixbuf, gtk2_white_pixmap,
1096             NULL, 0, 0, 0, 0, width, height);
1097     (*fp_gdk_pixbuf_get_from_drawable)(gtk2_black_pixbuf, gtk2_black_pixmap,
1098             NULL, 0, 0, 0, 0, width, height);
1099 
1100     white = (*fp_gdk_pixbuf_get_pixels)(gtk2_white_pixbuf);
1101     black = (*fp_gdk_pixbuf_get_pixels)(gtk2_black_pixbuf);
1102     stride = (*fp_gdk_pixbuf_get_rowstride)(gtk2_black_pixbuf);
1103     padding = stride - width * 4;
1104 
1105     for (i = 0; i < height; i++) {
1106         for (j = 0; j < width; j++) {
1107             int r1 = *white++;


1729             h = size.height - ((GtkMisc*)gtk2_widget)->ypad * 2;
1730             w = h = MIN(MIN(w, h), MIN(width,height)) * 0.7;
1731             break;
1732 
1733         default:
1734             w = width;
1735             h = height;
1736             break;
1737     }
1738     x += (width - w) / 2;
1739     y += (height - h) / 2;
1740 
1741     (*fp_gtk_paint_arrow)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1742             shadow_type, NULL, gtk2_widget, detail, arrow_type, fill,
1743             x, y, w, h);
1744     (*fp_gtk_paint_arrow)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1745             shadow_type, NULL, gtk2_widget, detail, arrow_type, fill,
1746             x, y, w, h);
1747 }
1748 
1749 void gtk2_paint_box(WidgetType widget_type, GtkStateType state_type,
1750                     GtkShadowType shadow_type, const gchar *detail,
1751                     gint x, gint y, gint width, gint height,
1752                     gint synth_state, GtkTextDirection dir)
1753 {
1754     gtk2_widget = gtk2_get_widget(widget_type);
1755 
1756     /*
1757      * The clearlooks engine sometimes looks at the widget's state field
1758      * instead of just the state_type variable that we pass in, so to account
1759      * for those cases we set the widget's state field accordingly.  The
1760      * flags field is similarly important for things like focus/default state.
1761      */
1762     gtk2_widget->state = state_type;
1763 
1764     if (widget_type == HSLIDER_TRACK) {
1765         /*
1766          * For horizontal JSliders with right-to-left orientation, we need
1767          * to set the "inverted" flag to match the native GTK behavior where
1768          * the foreground highlight is on the right side of the slider thumb.
1769          * This is needed especially for the ubuntulooks engine, which looks


1899     gtk2_set_direction(gtk2_widget, GTK_TEXT_DIR_LTR);
1900 }
1901 
1902 void gtk2_paint_box_gap(WidgetType widget_type, GtkStateType state_type,
1903         GtkShadowType shadow_type, const gchar *detail,
1904         gint x, gint y, gint width, gint height,
1905         GtkPositionType gap_side, gint gap_x, gint gap_width)
1906 {
1907     /* Clearlooks needs a real clip area to paint the gap properly */
1908     GdkRectangle area = { x, y, width, height };
1909 
1910     gtk2_widget = gtk2_get_widget(widget_type);
1911     (*fp_gtk_paint_box_gap)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1912             shadow_type, &area, gtk2_widget, detail,
1913             x, y, width, height, gap_side, gap_x, gap_width);
1914     (*fp_gtk_paint_box_gap)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1915             shadow_type, &area, gtk2_widget, detail,
1916             x, y, width, height, gap_side, gap_x, gap_width);
1917 }
1918 
1919 void gtk2_paint_check(WidgetType widget_type, gint synth_state,
1920         const gchar *detail, gint x, gint y, gint width, gint height)
1921 {
1922     GtkStateType state_type = get_gtk_state_type(widget_type, synth_state);
1923     GtkShadowType shadow_type = get_gtk_shadow_type(widget_type, synth_state);
1924 
1925     gtk2_widget = gtk2_get_widget(widget_type);
1926     init_toggle_widget(widget_type, synth_state);
1927 
1928     (*fp_gtk_paint_check)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1929             shadow_type, NULL, gtk2_widget, detail,
1930             x, y, width, height);
1931     (*fp_gtk_paint_check)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1932             shadow_type, NULL, gtk2_widget, detail,
1933             x, y, width, height);
1934 }
1935 
1936 void gtk2_paint_diamond(WidgetType widget_type, GtkStateType state_type,
1937         GtkShadowType shadow_type, const gchar *detail,
1938         gint x, gint y, gint width, gint height)
1939 {
1940     gtk2_widget = gtk2_get_widget(widget_type);
1941     (*fp_gtk_paint_diamond)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1942             shadow_type, NULL, gtk2_widget, detail,
1943             x, y, width, height);
1944     (*fp_gtk_paint_diamond)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1945             shadow_type, NULL, gtk2_widget, detail,
1946             x, y, width, height);
1947 }
1948 
1949 void gtk2_paint_expander(WidgetType widget_type, GtkStateType state_type,
1950         const gchar *detail, gint x, gint y, gint width, gint height,
1951         GtkExpanderStyle expander_style)
1952 {
1953     gtk2_widget = gtk2_get_widget(widget_type);
1954     (*fp_gtk_paint_expander)(gtk2_widget->style, gtk2_white_pixmap,
1955             state_type, NULL, gtk2_widget, detail,
1956             x + width / 2, y + height / 2, expander_style);
1957     (*fp_gtk_paint_expander)(gtk2_widget->style, gtk2_black_pixmap,
1958             state_type, NULL, gtk2_widget, detail,
1959             x + width / 2, y + height / 2, expander_style);
1960 }
1961 
1962 void gtk2_paint_extension(WidgetType widget_type, GtkStateType state_type,
1963         GtkShadowType shadow_type, const gchar *detail,
1964         gint x, gint y, gint width, gint height, GtkPositionType gap_side)
1965 {
1966     gtk2_widget = gtk2_get_widget(widget_type);
1967     (*fp_gtk_paint_extension)(gtk2_widget->style, gtk2_white_pixmap,
1968             state_type, shadow_type, NULL, gtk2_widget, detail,
1969             x, y, width, height, gap_side);
1970     (*fp_gtk_paint_extension)(gtk2_widget->style, gtk2_black_pixmap,
1971             state_type, shadow_type, NULL, gtk2_widget, detail,
1972             x, y, width, height, gap_side);
1973 }
1974 
1975 void gtk2_paint_flat_box(WidgetType widget_type, GtkStateType state_type,
1976         GtkShadowType shadow_type, const gchar *detail,
1977         gint x, gint y, gint width, gint height, gboolean has_focus)
1978 {
1979     gtk2_widget = gtk2_get_widget(widget_type);
1980 
1981     if (has_focus)
1982         ((GtkObject*)gtk2_widget)->flags |= GTK_HAS_FOCUS;
1983     else
1984         ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_FOCUS;
1985 
1986     (*fp_gtk_paint_flat_box)(gtk2_widget->style, gtk2_white_pixmap,
1987             state_type, shadow_type, NULL, gtk2_widget, detail,
1988             x, y, width, height);
1989     (*fp_gtk_paint_flat_box)(gtk2_widget->style, gtk2_black_pixmap,
1990             state_type, shadow_type, NULL, gtk2_widget, detail,
1991             x, y, width, height);
1992 }
1993 
1994 void gtk2_paint_focus(WidgetType widget_type, GtkStateType state_type,
1995         const char *detail, gint x, gint y, gint width, gint height)
1996 {
1997     gtk2_widget = gtk2_get_widget(widget_type);
1998     (*fp_gtk_paint_focus)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1999             NULL, gtk2_widget, detail, x, y, width, height);
2000     (*fp_gtk_paint_focus)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2001             NULL, gtk2_widget, detail, x, y, width, height);
2002 }
2003 
2004 void gtk2_paint_handle(WidgetType widget_type, GtkStateType state_type,
2005         GtkShadowType shadow_type, const gchar *detail,
2006         gint x, gint y, gint width, gint height, GtkOrientation orientation)
2007 {
2008     gtk2_widget = gtk2_get_widget(widget_type);
2009     (*fp_gtk_paint_handle)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2010             shadow_type, NULL, gtk2_widget, detail,
2011             x, y, width, height, orientation);
2012     (*fp_gtk_paint_handle)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2013             shadow_type, NULL, gtk2_widget, detail,
2014             x, y, width, height, orientation);
2015 }
2016 
2017 void gtk2_paint_hline(WidgetType widget_type, GtkStateType state_type,
2018         const gchar *detail, gint x, gint y, gint width, gint height)
2019 {
2020     gtk2_widget = gtk2_get_widget(widget_type);
2021     (*fp_gtk_paint_hline)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2022             NULL, gtk2_widget, detail, x, x + width, y);
2023     (*fp_gtk_paint_hline)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2024             NULL, gtk2_widget, detail, x, x + width, y);
2025 }
2026 
2027 void gtk2_paint_option(WidgetType widget_type, gint synth_state,
2028         const gchar *detail, gint x, gint y, gint width, gint height)
2029 {
2030     GtkStateType state_type = get_gtk_state_type(widget_type, synth_state);
2031     GtkShadowType shadow_type = get_gtk_shadow_type(widget_type, synth_state);
2032 
2033     gtk2_widget = gtk2_get_widget(widget_type);
2034     init_toggle_widget(widget_type, synth_state);
2035 
2036     (*fp_gtk_paint_option)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2037             shadow_type, NULL, gtk2_widget, detail,
2038             x, y, width, height);
2039     (*fp_gtk_paint_option)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2040             shadow_type, NULL, gtk2_widget, detail,
2041             x, y, width, height);
2042 }
2043 
2044 void gtk2_paint_shadow(WidgetType widget_type, GtkStateType state_type,
2045                        GtkShadowType shadow_type, const gchar *detail,
2046                        gint x, gint y, gint width, gint height,
2047                        gint synth_state, GtkTextDirection dir)
2048 {
2049     gtk2_widget = gtk2_get_widget(widget_type);
2050 
2051     /*
2052      * The clearlooks engine sometimes looks at the widget's state field
2053      * instead of just the state_type variable that we pass in, so to account
2054      * for those cases we set the widget's state field accordingly.  The
2055      * flags field is similarly important for things like focus state.
2056      */
2057     gtk2_widget->state = state_type;
2058 
2059     /*
2060      * Some engines (e.g. clearlooks) will paint the shadow of certain
2061      * widgets (e.g. COMBO_BOX_TEXT_FIELD) differently depending on the
2062      * the text direction.
2063      */
2064     gtk2_set_direction(gtk2_widget, dir);


2074         } else {
2075             ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_FOCUS;
2076         }
2077         break;
2078     default:
2079         break;
2080     }
2081 
2082     (*fp_gtk_paint_shadow)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2083             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
2084     (*fp_gtk_paint_shadow)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2085             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
2086 
2087     /*
2088      * Reset the text direction to the default value so that we don't
2089      * accidentally affect other operations and widgets.
2090      */
2091     gtk2_set_direction(gtk2_widget, GTK_TEXT_DIR_LTR);
2092 }
2093 
2094 void gtk2_paint_slider(WidgetType widget_type, GtkStateType state_type,
2095         GtkShadowType shadow_type, const gchar *detail,
2096         gint x, gint y, gint width, gint height, GtkOrientation orientation)

2097 {
2098     gtk2_widget = gtk2_get_widget(widget_type);
2099     (*fp_gtk_paint_slider)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2100             shadow_type, NULL, gtk2_widget, detail,
2101             x, y, width, height, orientation);
2102     (*fp_gtk_paint_slider)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2103             shadow_type, NULL, gtk2_widget, detail,
2104             x, y, width, height, orientation);
2105 }
2106 
2107 void gtk2_paint_vline(WidgetType widget_type, GtkStateType state_type,
2108         const gchar *detail, gint x, gint y, gint width, gint height)
2109 {
2110     gtk2_widget = gtk2_get_widget(widget_type);
2111     (*fp_gtk_paint_vline)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2112             NULL, gtk2_widget, detail, y, y + height, x);
2113     (*fp_gtk_paint_vline)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2114             NULL, gtk2_widget, detail, y, y + height, x);
2115 }
2116 
2117 void gtk_paint_background(WidgetType widget_type, GtkStateType state_type,
2118         gint x, gint y, gint width, gint height)
2119 {
2120     gtk2_widget = gtk2_get_widget(widget_type);
2121     (*fp_gtk_style_apply_default_background)(gtk2_widget->style,
2122             gtk2_white_pixmap, TRUE, state_type, NULL, x, y, width, height);
2123     (*fp_gtk_style_apply_default_background)(gtk2_widget->style,
2124             gtk2_black_pixmap, TRUE, state_type, NULL, x, y, width, height);
2125 }
2126 
2127 GdkPixbuf *gtk2_get_stock_icon(gint widget_type, const gchar *stock_id,
2128         GtkIconSize size, GtkTextDirection direction, const char *detail)
2129 {
2130     init_containers();
2131     gtk2_widget = gtk2_get_widget((widget_type < 0) ? IMAGE : widget_type);
2132     gtk2_widget->state = GTK_STATE_NORMAL;
2133     (*fp_gtk_widget_set_direction)(gtk2_widget, direction);
2134     return (*fp_gtk_widget_render_icon)(gtk2_widget, stock_id, size, detail);
2135 }
2136 












































2137 /*************************************************/
2138 gint gtk2_get_xthickness(JNIEnv *env, WidgetType widget_type)
2139 {
2140     init_containers();
2141 
2142     gtk2_widget = gtk2_get_widget(widget_type);
2143     GtkStyle* style = gtk2_widget->style;
2144     return style->xthickness;
2145 }
2146 
2147 gint gtk2_get_ythickness(JNIEnv *env, WidgetType widget_type)
2148 {
2149     init_containers();
2150 
2151     gtk2_widget = gtk2_get_widget(widget_type);
2152     GtkStyle* style = gtk2_widget->style;
2153     return style->ythickness;
2154 }
2155 
2156 /*************************************************/
2157 guint8 recode_color(guint16 channel)
2158 {
2159     return (guint8)(channel>>8);
2160 }
2161 
2162 gint gtk2_get_color_for_state(JNIEnv *env, WidgetType widget_type,
2163                               GtkStateType state_type, ColorType color_type)
2164 {
2165     gint result = 0;
2166     GdkColor *color = NULL;
2167 
2168     init_containers();
2169 
2170     gtk2_widget = gtk2_get_widget(widget_type);
2171     GtkStyle* style = gtk2_widget->style;
2172 
2173     switch (color_type)
2174     {
2175         case FOREGROUND:
2176             color = &(style->fg[state_type]);
2177             break;
2178         case BACKGROUND:
2179             color = &(style->bg[state_type]);
2180             break;
2181         case TEXT_FOREGROUND:
2182             color = &(style->text[state_type]);


2194             color = &(style->mid[state_type]);
2195             break;
2196         case FOCUS:
2197         case BLACK:
2198             color = &(style->black);
2199             break;
2200         case WHITE:
2201             color = &(style->white);
2202             break;
2203     }
2204 
2205     if (color)
2206         result = recode_color(color->red)   << 16 |
2207                  recode_color(color->green) << 8  |
2208                  recode_color(color->blue);
2209 
2210     return result;
2211 }
2212 
2213 /*************************************************/
2214 jobject create_Boolean(JNIEnv *env, jboolean boolean_value);
2215 jobject create_Integer(JNIEnv *env, jint int_value);
2216 jobject create_Long(JNIEnv *env, jlong long_value);
2217 jobject create_Float(JNIEnv *env, jfloat float_value);
2218 jobject create_Double(JNIEnv *env, jdouble double_value);
2219 jobject create_Character(JNIEnv *env, jchar char_value);
2220 jobject create_Insets(JNIEnv *env, GtkBorder *border);
2221 
2222 jobject gtk2_get_class_value(JNIEnv *env, WidgetType widget_type, jstring jkey)

2223 {
2224     init_containers();
2225 
2226     const char* key = getStrFor(env, jkey);
2227     gtk2_widget = gtk2_get_widget(widget_type);
2228 
2229     GValue value;
2230     value.g_type = 0;
2231 
2232     GParamSpec* param = (*fp_gtk_widget_class_find_style_property)(
2233                                     ((GTypeInstance*)gtk2_widget)->g_class, key);
2234     if( param )
2235     {
2236         (*fp_g_value_init)( &value, param->value_type );
2237         (*fp_gtk_widget_style_get_property)(gtk2_widget, key, &value);
2238 
2239         if( (*fp_g_type_is_a)( param->value_type, G_TYPE_BOOLEAN ))
2240         {
2241             gboolean val = (*fp_g_value_get_boolean)(&value);
2242             return create_Boolean(env, (jboolean)val);
2243         }
2244         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_CHAR ))
2245         {
2246             gchar val = (*fp_g_value_get_char)(&value);


2327         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_BOXED ))
2328         {
2329             gpointer* val = (*fp_g_value_get_boxed)(&value);
2330             printf( "Boxed: %p\n", val );
2331         }
2332         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_POINTER ))
2333         {
2334             gpointer* val = (*fp_g_value_get_pointer)(&value);
2335             printf( "Pointer: %p\n", val );
2336         }
2337         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_OBJECT ))
2338         {
2339             GObject* val = (GObject*)(*fp_g_value_get_object)(&value);
2340             printf( "Object: %p\n", val );
2341         }*/
2342     }
2343 
2344     return NULL;
2345 }
2346 
2347 void gtk2_set_range_value(WidgetType widget_type, jdouble value,
2348                           jdouble min, jdouble max, jdouble visible)
2349 {
2350     GtkAdjustment *adj;
2351 
2352     gtk2_widget = gtk2_get_widget(widget_type);
2353 
2354     adj = (*fp_gtk_range_get_adjustment)((GtkRange *)gtk2_widget);
2355     adj->value = (gdouble)value;
2356     adj->lower = (gdouble)min;
2357     adj->upper = (gdouble)max;
2358     adj->page_size = (gdouble)visible;
2359 }
2360 
2361 /*************************************************/
2362 jobject create_Object(JNIEnv *env, jmethodID *cid,
2363                              const char* class_name,
2364                              const char* signature,
2365                              jvalue* value)
2366 {
2367     jclass  class;
2368     jobject result;
2369 
2370     class = (*env)->FindClass(env, class_name);
2371     if( class == NULL )
2372         return NULL; /* can't find/load the class, exception thrown */
2373 
2374     if( *cid == NULL)
2375     {
2376         *cid = (*env)->GetMethodID(env, class, "<init>", signature);
2377         if( *cid == NULL )
2378         {
2379             (*env)->DeleteLocalRef(env, class);
2380             return NULL; /* can't find/get the method, exception thrown */
2381         }
2382     }


2445     value.c = char_value;
2446 
2447     return create_Object(env, &cid, "java/lang/Character", "(C)V", &value);
2448 }
2449 
2450 
2451 jobject create_Insets(JNIEnv *env, GtkBorder *border)
2452 {
2453     static jmethodID cid = NULL;
2454     jvalue values[4];
2455 
2456     values[0].i = border->top;
2457     values[1].i = border->left;
2458     values[2].i = border->bottom;
2459     values[3].i = border->right;
2460 
2461     return create_Object(env, &cid, "java/awt/Insets", "(IIII)V", values);
2462 }
2463 
2464 /*********************************************/
2465 jstring gtk2_get_pango_font_name(JNIEnv *env, WidgetType widget_type)
2466 {
2467     init_containers();
2468 
2469     gtk2_widget = gtk2_get_widget(widget_type);
2470     jstring  result = NULL;
2471     GtkStyle* style = gtk2_widget->style;
2472 
2473     if (style && style->font_desc)
2474     {
2475         gchar* val = (*fp_pango_font_description_to_string)(style->font_desc);
2476         result = (*env)->NewStringUTF(env, val);
2477         (*fp_g_free)( val );
2478     }
2479 
2480     return result;
2481 }
2482 
2483 /***********************************************/
2484 jobject get_string_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2485 {
2486     jobject result = NULL;
2487     gchar*  strval = NULL;
2488 
2489     (*fp_g_object_get)(settings, key, &strval, NULL);
2490     result = (*env)->NewStringUTF(env, strval);
2491     (*fp_g_free)(strval);
2492 
2493     return result;
2494 }
2495 
2496 jobject get_integer_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2497 {
2498     gint intval = NULL;
2499     (*fp_g_object_get)(settings, key, &intval, NULL);
2500     return create_Integer(env, intval);
2501 }
2502 
2503 jobject get_boolean_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2504 {
2505     gint intval = NULL;
2506     (*fp_g_object_get)(settings, key, &intval, NULL);
2507     return create_Boolean(env, intval);
2508 }
2509 
2510 jobject gtk2_get_setting(JNIEnv *env, Setting property)
2511 {
2512     GtkSettings* settings = (*fp_gtk_settings_get_default)();
2513 
2514     switch (property)
2515     {
2516         case GTK_FONT_NAME:
2517             return get_string_property(env, settings, "gtk-font-name");
2518         case GTK_ICON_SIZES:
2519             return get_string_property(env, settings, "gtk-icon-sizes");
2520         case GTK_CURSOR_BLINK:
2521             return get_boolean_property(env, settings, "gtk-cursor-blink");
2522         case GTK_CURSOR_BLINK_TIME:
2523             return get_integer_property(env, settings, "gtk-cursor-blink-time");
2524     }
2525 
2526     return NULL;




















































































2527 }


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 #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 #include "jvm_md.h"
  34 #include "sizecalc.h"
  35 #include <jni_util.h>
  36 #include "awt.h"
  37 



























  38 #define GTK_TYPE_BORDER                 ((*fp_gtk_border_get_type)())
  39 
  40 #define G_TYPE_FUNDAMENTAL_SHIFT        (2)
  41 #define G_TYPE_MAKE_FUNDAMENTAL(x)      ((GType) ((x) << G_TYPE_FUNDAMENTAL_SHIFT))

  42 
  43 #define CONV_BUFFER_SIZE 128
  44 
  45 #define NO_SYMBOL_EXCEPTION 1
  46 









  47 static void *gtk2_libhandle = NULL;
  48 static void *gthread_libhandle = NULL;
  49 
  50 static jmp_buf j;
  51 
  52 /* Widgets */
  53 static GtkWidget *gtk2_widget = NULL;
  54 static GtkWidget *gtk2_window = NULL;
  55 static GtkFixed  *gtk2_fixed  = NULL;
  56 
  57 /* Paint system */
  58 static GdkPixmap *gtk2_white_pixmap = NULL;
  59 static GdkPixmap *gtk2_black_pixmap = NULL;
  60 static GdkPixbuf *gtk2_white_pixbuf = NULL;
  61 static GdkPixbuf *gtk2_black_pixbuf = NULL;
  62 static int gtk2_pixbuf_width = 0;
  63 static int gtk2_pixbuf_height = 0;
  64 
  65 /* Static buffer for conversion from java.lang.String to UTF-8 */
  66 static char convertionBuffer[CONV_BUFFER_SIZE];
  67 
  68 static gboolean new_combo = TRUE;
  69 const char ENV_PREFIX[] = "GTK_MODULES=";
  70 

















































  71 static GtkWidget *gtk2_widgets[_GTK_WIDGET_TYPE_SIZE];
  72 
  73 /*************************
  74  * Glib function pointers
  75  *************************/
  76 
  77 static gboolean (*fp_g_main_context_iteration)(GMainContext *context,
  78                                              gboolean may_block);
  79 
  80 static GValue*      (*fp_g_value_init)(GValue *value, GType g_type);
  81 static gboolean     (*fp_g_type_is_a)(GType type, GType is_a_type);
  82 static gboolean     (*fp_g_value_get_boolean)(const GValue *value);
  83 static gchar        (*fp_g_value_get_char)(const GValue *value);
  84 static guchar       (*fp_g_value_get_uchar)(const GValue *value);
  85 static gint         (*fp_g_value_get_int)(const GValue *value);
  86 static guint        (*fp_g_value_get_uint)(const GValue *value);
  87 static glong        (*fp_g_value_get_long)(const GValue *value);
  88 static gulong       (*fp_g_value_get_ulong)(const GValue *value);
  89 static gint64       (*fp_g_value_get_int64)(const GValue *value);
  90 static guint64      (*fp_g_value_get_uint64)(const GValue *value);


 259         const gchar *first_property_name, ...);
 260 static void (*fp_gtk_widget_class_install_style_property)(
 261         GtkWidgetClass* class, GParamSpec *pspec);
 262 static GParamSpec* (*fp_gtk_widget_class_find_style_property)(
 263         GtkWidgetClass* class, const gchar* property_name);
 264 static void (*fp_gtk_widget_style_get_property)(GtkWidget* widget,
 265         const gchar* property_name, GValue* value);
 266 static char* (*fp_pango_font_description_to_string)(
 267         const PangoFontDescription* fd);
 268 static GtkSettings* (*fp_gtk_settings_get_default)();
 269 static GtkSettings* (*fp_gtk_widget_get_settings)(GtkWidget *widget);
 270 static GType        (*fp_gtk_border_get_type)();
 271 static void (*fp_gtk_arrow_set)(GtkWidget* arrow,
 272                                 GtkArrowType arrow_type,
 273                                 GtkShadowType shadow_type);
 274 static void (*fp_gtk_widget_size_request)(GtkWidget *widget,
 275                                           GtkRequisition *requisition);
 276 static GtkAdjustment* (*fp_gtk_range_get_adjustment)(GtkRange* range);
 277 
 278 /* Method bodies */














 279 
 280 static void throw_exception(JNIEnv *env, const char* name, const char* message)
 281 {
 282     jclass class = (*env)->FindClass(env, name);
 283 
 284     if (class != NULL)
 285         (*env)->ThrowNew(env, class, message);
 286 
 287     (*env)->DeleteLocalRef(env, class);
 288 }
 289 
 290 /* This is a workaround for the bug:
 291  * http://sourceware.org/bugzilla/show_bug.cgi?id=1814
 292  * (dlsym/dlopen clears dlerror state)
 293  * This bug is specific to Linux, but there is no harm in
 294  * applying this workaround on Solaris as well.
 295  */
 296 static void* dl_symbol(const char* name)
 297 {
 298     void* result = dlsym(gtk2_libhandle, name);
 299     if (!result)
 300         longjmp(j, NO_SYMBOL_EXCEPTION);
 301 
 302     return result;
 303 }
 304 
 305 static void* dl_symbol_gthread(const char* name)
 306 {
 307     void* result = dlsym(gthread_libhandle, name);
 308     if (!result)
 309         longjmp(j, NO_SYMBOL_EXCEPTION);
 310 
 311     return result;
 312 }
 313 
 314 gboolean gtk2_check(const char* lib_name, gboolean load)
 315 {
 316     if (gtk2_libhandle != NULL) {
 317         /* We've already successfully opened the GTK libs, so return true. */
 318         return TRUE;
 319     } else {
 320         void *lib = NULL;
 321         #ifdef RTLD_NOLOAD
 322         /* Just check if gtk libs are already in the process space */
 323         lib = dlopen(lib_name, RTLD_LAZY | RTLD_NOLOAD);
 324         if (!load || lib != NULL) {
 325             return lib != NULL;
 326         }
 327 #else
 328 #ifdef _AIX
 329         /* On AIX we could implement this with the help of loadquery(L_GETINFO, ..)  */
 330         /* (see reload_table() in hotspot/src/os/aix/vm/loadlib_aix.cpp) but it is   */
 331         /* probably not worth it because most AIX servers don't have GTK libs anyway */
 332 #endif
 333 #endif
 334 
 335         lib = dlopen(lib_name, RTLD_LAZY | RTLD_LOCAL);
 336 



 337         if (lib == NULL) {
 338             return FALSE;
 339         }

 340 
 341         fp_gtk_check_version = dlsym(lib, "gtk_check_version");
 342         /* Check for GTK 2.2+ */
 343         if (!fp_gtk_check_version(2, 2, 0)) {
 344             return  TRUE;
 345         }
 346 
 347         // 8048289: workaround for https://bugzilla.gnome.org/show_bug.cgi?id=733065
 348         // dlclose(lib);
 349 
 350         return FALSE;
 351     }
 352 }
 353 
 354 #define ADD_SUPPORTED_ACTION(actionStr) \
 355 do { \
 356     jfieldID fld_action = (*env)->GetStaticFieldID(env, cls_action, actionStr, "Ljava/awt/Desktop$Action;"); \
 357     if (!(*env)->ExceptionCheck(env)) { \
 358         jobject action = (*env)->GetStaticObjectField(env, cls_action, fld_action); \
 359         (*env)->CallBooleanMethod(env, supportedActions, mid_arrayListAdd, action); \
 360     } else { \
 361         (*env)->ExceptionClear(env); \
 362     } \
 363 } while(0);
 364 
 365 
 366 static void update_supported_actions(JNIEnv *env) {
 367     GVfs * (*fp_g_vfs_get_default) (void);
 368     const gchar * const * (*fp_g_vfs_get_supported_uri_schemes) (GVfs * vfs);
 369     const gchar * const * schemes = NULL;
 370 
 371     jclass cls_action = (*env)->FindClass(env, "java/awt/Desktop$Action");
 372     CHECK_NULL(cls_action);
 373     jclass cls_xDesktopPeer = (*env)->FindClass(env, "sun/awt/X11/XDesktopPeer");
 374     CHECK_NULL(cls_xDesktopPeer);
 375     jfieldID fld_supportedActions = (*env)->GetStaticFieldID(env, cls_xDesktopPeer, "supportedActions", "Ljava/util/List;");
 376     CHECK_NULL(fld_supportedActions);
 377     jobject supportedActions = (*env)->GetStaticObjectField(env, cls_xDesktopPeer, fld_supportedActions);
 378 
 379     jclass cls_arrayList = (*env)->FindClass(env, "java/util/ArrayList");
 380     CHECK_NULL(cls_arrayList);
 381     jmethodID mid_arrayListAdd = (*env)->GetMethodID(env, cls_arrayList, "add", "(Ljava/lang/Object;)Z");
 382     CHECK_NULL(mid_arrayListAdd);
 383     jmethodID mid_arrayListClear = (*env)->GetMethodID(env, cls_arrayList, "clear", "()V");
 384     CHECK_NULL(mid_arrayListClear);
 385 
 386     (*env)->CallVoidMethod(env, supportedActions, mid_arrayListClear);


 409             int i = 0;
 410             while (schemes[i]) {
 411                 if (strcmp(schemes[i], "http") == 0) {
 412                     ADD_SUPPORTED_ACTION("BROWSE");
 413                     ADD_SUPPORTED_ACTION("MAIL");
 414                     break;
 415                 }
 416                 i++;
 417             }
 418         }
 419     } else {
 420 #ifdef INTERNAL_BUILD
 421         fprintf(stderr, "Cannot load g_vfs_get_supported_uri_schemes\n");
 422 #endif /* INTERNAL_BUILD */
 423     }
 424 
 425 }
 426 /**
 427  * Functions for awt_Desktop.c
 428  */
 429 static gboolean gtk2_show_uri_load(JNIEnv *env) {
 430      gboolean success = FALSE;
 431      dlerror();
 432      const char *gtk_version = fp_gtk_check_version(2, 14, 0);
 433      if (gtk_version != NULL) {
 434          // The gtk_show_uri is available from GTK+ 2.14
 435 #ifdef INTERNAL_BUILD
 436          fprintf (stderr, "The version of GTK is %s. "
 437              "The gtk_show_uri function is supported "
 438              "since GTK+ 2.14.\n", gtk_version);
 439 #endif /* INTERNAL_BUILD */
 440      } else {
 441          // Loading symbols only if the GTK version is 2.14 and higher
 442          fp_gtk_show_uri = dl_symbol("gtk_show_uri");
 443          const char *dlsym_error = dlerror();
 444          if (dlsym_error) {
 445 #ifdef INTERNAL_BUILD
 446              fprintf (stderr, "Cannot load symbol: %s \n", dlsym_error);
 447 #endif /* INTERNAL_BUILD */
 448          } else if (fp_gtk_show_uri == NULL) {
 449 #ifdef INTERNAL_BUILD
 450              fprintf(stderr, "dlsym(gtk_show_uri) returned NULL\n");
 451 #endif /* INTERNAL_BUILD */
 452         } else {
 453             gtk->gtk_show_uri = fp_gtk_show_uri;
 454             update_supported_actions(env);
 455             success = TRUE;
 456         }
 457      }
 458      return success;
 459 }
 460 
 461 /**
 462  * Functions for sun_awt_X11_GtkFileDialogPeer.c
 463  */
 464 static void gtk2_file_chooser_load()
 465 {
 466     fp_gtk_file_chooser_get_filename = dl_symbol(
 467             "gtk_file_chooser_get_filename");
 468     fp_gtk_file_chooser_dialog_new = dl_symbol("gtk_file_chooser_dialog_new");
 469     fp_gtk_file_chooser_set_current_folder = dl_symbol(
 470             "gtk_file_chooser_set_current_folder");
 471     fp_gtk_file_chooser_set_filename = dl_symbol(
 472             "gtk_file_chooser_set_filename");
 473     fp_gtk_file_chooser_set_current_name = dl_symbol(
 474             "gtk_file_chooser_set_current_name");
 475     fp_gtk_file_filter_add_custom = dl_symbol("gtk_file_filter_add_custom");
 476     fp_gtk_file_chooser_set_filter = dl_symbol("gtk_file_chooser_set_filter");
 477     fp_gtk_file_chooser_get_type = dl_symbol("gtk_file_chooser_get_type");
 478     fp_gtk_file_filter_new = dl_symbol("gtk_file_filter_new");
 479     if (fp_gtk_check_version(2, 8, 0) == NULL) {
 480         fp_gtk_file_chooser_set_do_overwrite_confirmation = dl_symbol(
 481                 "gtk_file_chooser_set_do_overwrite_confirmation");
 482     }
 483     fp_gtk_file_chooser_set_select_multiple = dl_symbol(
 484             "gtk_file_chooser_set_select_multiple");
 485     fp_gtk_file_chooser_get_current_folder = dl_symbol(
 486             "gtk_file_chooser_get_current_folder");
 487     fp_gtk_file_chooser_get_filenames = dl_symbol(
 488             "gtk_file_chooser_get_filenames");
 489     fp_gtk_g_slist_length = dl_symbol("g_slist_length");
 490 }
 491 
 492 GtkApi* gtk2_load(JNIEnv *env, const char* lib_name)
 493 {
 494     gboolean result;
 495     int i;
 496     int (*handler)();
 497     int (*io_handler)();
 498     char *gtk_modules_env;
 499 
 500     gtk2_libhandle = dlopen(lib_name, RTLD_LAZY | RTLD_LOCAL);
 501     if (gtk2_libhandle == NULL) {


 502         return FALSE;
 503     }
 504 
 505     gthread_libhandle = dlopen(GTHREAD_LIB_VERSIONED, RTLD_LAZY | RTLD_LOCAL);
 506     if (gthread_libhandle == NULL) {
 507         gthread_libhandle = dlopen(GTHREAD_LIB, RTLD_LAZY | RTLD_LOCAL);
 508         if (gthread_libhandle == NULL)
 509             return FALSE;
 510     }
 511 
 512     if (setjmp(j) == 0)
 513     {
 514         fp_gtk_check_version = dl_symbol("gtk_check_version");
 515         /* Check for GTK 2.2+ */
 516         if (fp_gtk_check_version(2, 2, 0)) {
 517             longjmp(j, NO_SYMBOL_EXCEPTION);
 518         }
 519 
 520         /* GLib */
 521         fp_glib_check_version = dlsym(gtk2_libhandle, "glib_check_version");


 783                     /* no free() on success, putenv() doesn't copy string */
 784                     free(new_env);
 785                 }
 786             }
 787             free(tmp_env);
 788         }
 789     }
 790     /*
 791      * GTK should be initialized with gtk_init_check() before use.
 792      *
 793      * gtk_init_check installs its own error handlers. It is critical that
 794      * we preserve error handler set from AWT. Otherwise we'll crash on
 795      * BadMatch errors which we would normally ignore. The IO error handler
 796      * is preserved here, too, just for consistency.
 797     */
 798     AWT_LOCK();
 799     handler = XSetErrorHandler(NULL);
 800     io_handler = XSetIOErrorHandler(NULL);
 801 
 802     if (fp_gtk_check_version(2, 2, 0) == NULL) {









 803         // Calling g_thread_init() multiple times leads to crash on GLib < 2.24
 804         // We can use g_thread_get_initialized () but it is available only for
 805         // GLib >= 2.20.
 806         gboolean is_g_thread_get_initialized = FALSE;
 807         if (GLIB_CHECK_VERSION(2, 20, 0)) {
 808             is_g_thread_get_initialized = fp_g_thread_get_initialized();
 809         }


 810         if (!is_g_thread_get_initialized) {
 811             fp_g_thread_init(NULL);
 812         }

 813         //According the GTK documentation, gdk_threads_init() should be
 814         //called before gtk_init() or gtk_init_check()
 815         fp_gdk_threads_init();
 816     }


 817     result = (*fp_gtk_init_check)(NULL, NULL);
 818 
 819     XSetErrorHandler(handler);
 820     XSetIOErrorHandler(io_handler);
 821     AWT_UNLOCK();
 822 
 823     /* Initialize widget array. */
 824     for (i = 0; i < _GTK_WIDGET_TYPE_SIZE; i++)
 825     {
 826         gtk2_widgets[i] = NULL;
 827     }
 828 
 829     
 830     if (result) {
 831         GtkApi* gtk = (GtkApi*)malloc(sizeof(GtkApi));
 832         gtk2_init(gtk);
 833         return gtk;
 834     }
 835     return NULL;
 836 }
 837 
 838 int gtk2_unload()
 839 {
 840     int i;
 841     char *gtk2_error;
 842 
 843     if (!gtk2_libhandle)
 844         return TRUE;
 845 
 846     /* Release painting objects */
 847     if (gtk2_white_pixmap != NULL) {
 848         (*fp_g_object_unref)(gtk2_white_pixmap);
 849         (*fp_g_object_unref)(gtk2_black_pixmap);
 850         (*fp_g_object_unref)(gtk2_white_pixbuf);
 851         (*fp_g_object_unref)(gtk2_black_pixbuf);
 852         gtk2_white_pixmap = gtk2_black_pixmap =
 853             gtk2_white_pixbuf = gtk2_black_pixbuf = NULL;
 854     }
 855     gtk2_pixbuf_width = 0;


 859         /* Destroying toplevel widget will destroy all contained widgets */
 860         (*fp_gtk_widget_destroy)(gtk2_window);
 861 
 862         /* Unset some static data so they get reinitialized on next load */
 863         gtk2_window = NULL;
 864     }
 865 
 866     dlerror();
 867     dlclose(gtk2_libhandle);
 868     dlclose(gthread_libhandle);
 869     if ((gtk2_error = dlerror()) != NULL)
 870     {
 871         return FALSE;
 872     }
 873     return TRUE;
 874 }
 875 
 876 /* Dispatch all pending events from the GTK event loop.
 877  * This is needed to catch theme change and update widgets' style.
 878  */
 879 static void flush_gtk_event_loop()
 880 {
 881     while( (*fp_g_main_context_iteration)(NULL, FALSE));
 882 }
 883 
 884 /*
 885  * Initialize components of containment hierarchy. This creates a GtkFixed
 886  * inside a GtkWindow. All widgets get realized.
 887  */
 888 static void init_containers()
 889 {
 890     if (gtk2_window == NULL)
 891     {
 892         gtk2_window = (*fp_gtk_window_new)(GTK_WINDOW_TOPLEVEL);
 893         gtk2_fixed = (GtkFixed *)(*fp_gtk_fixed_new)();
 894         (*fp_gtk_container_add)((GtkContainer*)gtk2_window,
 895                                 (GtkWidget *)gtk2_fixed);
 896         (*fp_gtk_widget_realize)(gtk2_window);
 897         (*fp_gtk_widget_realize)((GtkWidget *)gtk2_fixed);
 898     }
 899 }


 908  * work around this:
 909  * 1) Subclass GdkPixmap and cache translucent objects on client side. This
 910  * requires us to implement parts of X server drawing logic on client side.
 911  * Many X requests can potentially be "translucent"; e.g. XDrawLine with
 912  * fill=tile and a translucent tile is a "translucent" operation, whereas
 913  * XDrawLine with fill=solid is an "opaque" one. Moreover themes can (and some
 914  * do) intermix transparent and opaque operations which makes caching even
 915  * more problematic.
 916  * 2) Use Xorg 32bit ARGB visual when available. GDK has no native support
 917  * for it (as of version 2.6). Also even in JDS 3 Xorg does not support
 918  * these visuals by default, which makes optimizing for them pointless.
 919  * We can consider doing this at a later point when ARGB visuals become more
 920  * popular.
 921  * 3') GTK has plans to use Cairo as its graphical backend (presumably in
 922  * 2.8), and Cairo supports alpha. With it we could also get rid of the
 923  * unnecessary round trip to server and do all the drawing on client side.
 924  * 4) For now we draw to two different pixmaps and restore alpha channel by
 925  * comparing results. This can be optimized by using subclassed pixmap and
 926  * doing the second drawing only if necessary.
 927 */
 928 static void gtk2_init_painting(JNIEnv *env, gint width, gint height)
 929 {
 930     GdkGC *gc;
 931     GdkPixbuf *white, *black;
 932 
 933     init_containers();
 934 
 935     if (gtk2_pixbuf_width < width || gtk2_pixbuf_height < height)
 936     {
 937         white = (*fp_gdk_pixbuf_new)(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
 938         black = (*fp_gdk_pixbuf_new)(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
 939 
 940         if (white == NULL || black == NULL)
 941         {
 942             snprintf(convertionBuffer, CONV_BUFFER_SIZE, "Couldn't create pixbuf of size %dx%d", width, height);
 943             throw_exception(env, "java/lang/RuntimeException", convertionBuffer);
 944             fp_gdk_threads_leave();
 945             return;
 946         }
 947 
 948         if (gtk2_white_pixmap != NULL) {


 968     (*fp_gdk_rgb_gc_set_foreground)(gc, 0xffffff);
 969     (*fp_gdk_draw_rectangle)(gtk2_white_pixmap, gc, TRUE, 0, 0, width, height);
 970     (*fp_g_object_unref)(gc);
 971 
 972     gc = (*fp_gdk_gc_new)(gtk2_black_pixmap);
 973     (*fp_gdk_rgb_gc_set_foreground)(gc, 0x000000);
 974     (*fp_gdk_draw_rectangle)(gtk2_black_pixmap, gc, TRUE, 0, 0, width, height);
 975     (*fp_g_object_unref)(gc);
 976 }
 977 
 978 /*
 979  * Restore image from white and black pixmaps and copy it into destination
 980  * buffer. This method compares two pixbufs taken from white and black
 981  * pixmaps and decodes color and alpha components. Pixbufs are RGB without
 982  * alpha, destination buffer is ABGR.
 983  *
 984  * The return value is the transparency type of the resulting image, either
 985  * one of java_awt_Transparency_OPAQUE, java_awt_Transparency_BITMASK, and
 986  * java_awt_Transparency_TRANSLUCENT.
 987  */
 988 static gint gtk2_copy_image(gint *dst, gint width, gint height)
 989 {
 990     gint i, j, r, g, b;
 991     guchar *white, *black;
 992     gint stride, padding;
 993     gboolean is_opaque = TRUE;
 994     gboolean is_bitmask = TRUE;
 995 
 996     (*fp_gdk_pixbuf_get_from_drawable)(gtk2_white_pixbuf, gtk2_white_pixmap,
 997             NULL, 0, 0, 0, 0, width, height);
 998     (*fp_gdk_pixbuf_get_from_drawable)(gtk2_black_pixbuf, gtk2_black_pixmap,
 999             NULL, 0, 0, 0, 0, width, height);
1000 
1001     white = (*fp_gdk_pixbuf_get_pixels)(gtk2_white_pixbuf);
1002     black = (*fp_gdk_pixbuf_get_pixels)(gtk2_black_pixbuf);
1003     stride = (*fp_gdk_pixbuf_get_rowstride)(gtk2_black_pixbuf);
1004     padding = stride - width * 4;
1005 
1006     for (i = 0; i < height; i++) {
1007         for (j = 0; j < width; j++) {
1008             int r1 = *white++;


1630             h = size.height - ((GtkMisc*)gtk2_widget)->ypad * 2;
1631             w = h = MIN(MIN(w, h), MIN(width,height)) * 0.7;
1632             break;
1633 
1634         default:
1635             w = width;
1636             h = height;
1637             break;
1638     }
1639     x += (width - w) / 2;
1640     y += (height - h) / 2;
1641 
1642     (*fp_gtk_paint_arrow)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1643             shadow_type, NULL, gtk2_widget, detail, arrow_type, fill,
1644             x, y, w, h);
1645     (*fp_gtk_paint_arrow)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1646             shadow_type, NULL, gtk2_widget, detail, arrow_type, fill,
1647             x, y, w, h);
1648 }
1649 
1650 static void gtk2_paint_box(WidgetType widget_type, GtkStateType state_type,
1651                     GtkShadowType shadow_type, const gchar *detail,
1652                     gint x, gint y, gint width, gint height,
1653                     gint synth_state, GtkTextDirection dir)
1654 {
1655     gtk2_widget = gtk2_get_widget(widget_type);
1656 
1657     /*
1658      * The clearlooks engine sometimes looks at the widget's state field
1659      * instead of just the state_type variable that we pass in, so to account
1660      * for those cases we set the widget's state field accordingly.  The
1661      * flags field is similarly important for things like focus/default state.
1662      */
1663     gtk2_widget->state = state_type;
1664 
1665     if (widget_type == HSLIDER_TRACK) {
1666         /*
1667          * For horizontal JSliders with right-to-left orientation, we need
1668          * to set the "inverted" flag to match the native GTK behavior where
1669          * the foreground highlight is on the right side of the slider thumb.
1670          * This is needed especially for the ubuntulooks engine, which looks


1800     gtk2_set_direction(gtk2_widget, GTK_TEXT_DIR_LTR);
1801 }
1802 
1803 void gtk2_paint_box_gap(WidgetType widget_type, GtkStateType state_type,
1804         GtkShadowType shadow_type, const gchar *detail,
1805         gint x, gint y, gint width, gint height,
1806         GtkPositionType gap_side, gint gap_x, gint gap_width)
1807 {
1808     /* Clearlooks needs a real clip area to paint the gap properly */
1809     GdkRectangle area = { x, y, width, height };
1810 
1811     gtk2_widget = gtk2_get_widget(widget_type);
1812     (*fp_gtk_paint_box_gap)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1813             shadow_type, &area, gtk2_widget, detail,
1814             x, y, width, height, gap_side, gap_x, gap_width);
1815     (*fp_gtk_paint_box_gap)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1816             shadow_type, &area, gtk2_widget, detail,
1817             x, y, width, height, gap_side, gap_x, gap_width);
1818 }
1819 
1820 static void gtk2_paint_check(WidgetType widget_type, gint synth_state,
1821         const gchar *detail, gint x, gint y, gint width, gint height)
1822 {
1823     GtkStateType state_type = get_gtk_state_type(widget_type, synth_state);
1824     GtkShadowType shadow_type = get_gtk_shadow_type(widget_type, synth_state);
1825 
1826     gtk2_widget = gtk2_get_widget(widget_type);
1827     init_toggle_widget(widget_type, synth_state);
1828 
1829     (*fp_gtk_paint_check)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1830             shadow_type, NULL, gtk2_widget, detail,
1831             x, y, width, height);
1832     (*fp_gtk_paint_check)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1833             shadow_type, NULL, gtk2_widget, detail,
1834             x, y, width, height);
1835 }
1836 
1837 static void gtk2_paint_diamond(WidgetType widget_type, GtkStateType state_type,
1838         GtkShadowType shadow_type, const gchar *detail,
1839         gint x, gint y, gint width, gint height)
1840 {
1841     gtk2_widget = gtk2_get_widget(widget_type);
1842     (*fp_gtk_paint_diamond)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1843             shadow_type, NULL, gtk2_widget, detail,
1844             x, y, width, height);
1845     (*fp_gtk_paint_diamond)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1846             shadow_type, NULL, gtk2_widget, detail,
1847             x, y, width, height);
1848 }
1849 
1850 static void gtk2_paint_expander(WidgetType widget_type, GtkStateType state_type,
1851         const gchar *detail, gint x, gint y, gint width, gint height,
1852         GtkExpanderStyle expander_style)
1853 {
1854     gtk2_widget = gtk2_get_widget(widget_type);
1855     (*fp_gtk_paint_expander)(gtk2_widget->style, gtk2_white_pixmap,
1856             state_type, NULL, gtk2_widget, detail,
1857             x + width / 2, y + height / 2, expander_style);
1858     (*fp_gtk_paint_expander)(gtk2_widget->style, gtk2_black_pixmap,
1859             state_type, NULL, gtk2_widget, detail,
1860             x + width / 2, y + height / 2, expander_style);
1861 }
1862 
1863 static void gtk2_paint_extension(WidgetType widget_type, GtkStateType state_type,
1864         GtkShadowType shadow_type, const gchar *detail,
1865         gint x, gint y, gint width, gint height, GtkPositionType gap_side)
1866 {
1867     gtk2_widget = gtk2_get_widget(widget_type);
1868     (*fp_gtk_paint_extension)(gtk2_widget->style, gtk2_white_pixmap,
1869             state_type, shadow_type, NULL, gtk2_widget, detail,
1870             x, y, width, height, gap_side);
1871     (*fp_gtk_paint_extension)(gtk2_widget->style, gtk2_black_pixmap,
1872             state_type, shadow_type, NULL, gtk2_widget, detail,
1873             x, y, width, height, gap_side);
1874 }
1875 
1876 static void gtk2_paint_flat_box(WidgetType widget_type, GtkStateType state_type,
1877         GtkShadowType shadow_type, const gchar *detail,
1878         gint x, gint y, gint width, gint height, gboolean has_focus)
1879 {
1880     gtk2_widget = gtk2_get_widget(widget_type);
1881 
1882     if (has_focus)
1883         ((GtkObject*)gtk2_widget)->flags |= GTK_HAS_FOCUS;
1884     else
1885         ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_FOCUS;
1886 
1887     (*fp_gtk_paint_flat_box)(gtk2_widget->style, gtk2_white_pixmap,
1888             state_type, shadow_type, NULL, gtk2_widget, detail,
1889             x, y, width, height);
1890     (*fp_gtk_paint_flat_box)(gtk2_widget->style, gtk2_black_pixmap,
1891             state_type, shadow_type, NULL, gtk2_widget, detail,
1892             x, y, width, height);
1893 }
1894 
1895 static void gtk2_paint_focus(WidgetType widget_type, GtkStateType state_type,
1896         const char *detail, gint x, gint y, gint width, gint height)
1897 {
1898     gtk2_widget = gtk2_get_widget(widget_type);
1899     (*fp_gtk_paint_focus)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1900             NULL, gtk2_widget, detail, x, y, width, height);
1901     (*fp_gtk_paint_focus)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1902             NULL, gtk2_widget, detail, x, y, width, height);
1903 }
1904 
1905 static void gtk2_paint_handle(WidgetType widget_type, GtkStateType state_type,
1906         GtkShadowType shadow_type, const gchar *detail,
1907         gint x, gint y, gint width, gint height, GtkOrientation orientation)
1908 {
1909     gtk2_widget = gtk2_get_widget(widget_type);
1910     (*fp_gtk_paint_handle)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1911             shadow_type, NULL, gtk2_widget, detail,
1912             x, y, width, height, orientation);
1913     (*fp_gtk_paint_handle)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1914             shadow_type, NULL, gtk2_widget, detail,
1915             x, y, width, height, orientation);
1916 }
1917 
1918 static void gtk2_paint_hline(WidgetType widget_type, GtkStateType state_type,
1919         const gchar *detail, gint x, gint y, gint width, gint height)
1920 {
1921     gtk2_widget = gtk2_get_widget(widget_type);
1922     (*fp_gtk_paint_hline)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1923             NULL, gtk2_widget, detail, x, x + width, y);
1924     (*fp_gtk_paint_hline)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1925             NULL, gtk2_widget, detail, x, x + width, y);
1926 }
1927 
1928 static void gtk2_paint_option(WidgetType widget_type, gint synth_state,
1929         const gchar *detail, gint x, gint y, gint width, gint height)
1930 {
1931     GtkStateType state_type = get_gtk_state_type(widget_type, synth_state);
1932     GtkShadowType shadow_type = get_gtk_shadow_type(widget_type, synth_state);
1933 
1934     gtk2_widget = gtk2_get_widget(widget_type);
1935     init_toggle_widget(widget_type, synth_state);
1936 
1937     (*fp_gtk_paint_option)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1938             shadow_type, NULL, gtk2_widget, detail,
1939             x, y, width, height);
1940     (*fp_gtk_paint_option)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1941             shadow_type, NULL, gtk2_widget, detail,
1942             x, y, width, height);
1943 }
1944 
1945 static void gtk2_paint_shadow(WidgetType widget_type, GtkStateType state_type,
1946                        GtkShadowType shadow_type, const gchar *detail,
1947                        gint x, gint y, gint width, gint height,
1948                        gint synth_state, GtkTextDirection dir)
1949 {
1950     gtk2_widget = gtk2_get_widget(widget_type);
1951 
1952     /*
1953      * The clearlooks engine sometimes looks at the widget's state field
1954      * instead of just the state_type variable that we pass in, so to account
1955      * for those cases we set the widget's state field accordingly.  The
1956      * flags field is similarly important for things like focus state.
1957      */
1958     gtk2_widget->state = state_type;
1959 
1960     /*
1961      * Some engines (e.g. clearlooks) will paint the shadow of certain
1962      * widgets (e.g. COMBO_BOX_TEXT_FIELD) differently depending on the
1963      * the text direction.
1964      */
1965     gtk2_set_direction(gtk2_widget, dir);


1975         } else {
1976             ((GtkObject*)gtk2_widget)->flags &= ~GTK_HAS_FOCUS;
1977         }
1978         break;
1979     default:
1980         break;
1981     }
1982 
1983     (*fp_gtk_paint_shadow)(gtk2_widget->style, gtk2_white_pixmap, state_type,
1984             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
1985     (*fp_gtk_paint_shadow)(gtk2_widget->style, gtk2_black_pixmap, state_type,
1986             shadow_type, NULL, gtk2_widget, detail, x, y, width, height);
1987 
1988     /*
1989      * Reset the text direction to the default value so that we don't
1990      * accidentally affect other operations and widgets.
1991      */
1992     gtk2_set_direction(gtk2_widget, GTK_TEXT_DIR_LTR);
1993 }
1994 
1995 static void gtk2_paint_slider(WidgetType widget_type, GtkStateType state_type,
1996         GtkShadowType shadow_type, const gchar *detail,
1997         gint x, gint y, gint width, gint height, GtkOrientation orientation,
1998         gboolean has_focus)
1999 {
2000     gtk2_widget = gtk2_get_widget(widget_type);
2001     (*fp_gtk_paint_slider)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2002             shadow_type, NULL, gtk2_widget, detail,
2003             x, y, width, height, orientation);
2004     (*fp_gtk_paint_slider)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2005             shadow_type, NULL, gtk2_widget, detail,
2006             x, y, width, height, orientation);
2007 }
2008 
2009 static void gtk2_paint_vline(WidgetType widget_type, GtkStateType state_type,
2010         const gchar *detail, gint x, gint y, gint width, gint height)
2011 {
2012     gtk2_widget = gtk2_get_widget(widget_type);
2013     (*fp_gtk_paint_vline)(gtk2_widget->style, gtk2_white_pixmap, state_type,
2014             NULL, gtk2_widget, detail, y, y + height, x);
2015     (*fp_gtk_paint_vline)(gtk2_widget->style, gtk2_black_pixmap, state_type,
2016             NULL, gtk2_widget, detail, y, y + height, x);
2017 }
2018 
2019 static void gtk_paint_background(WidgetType widget_type, GtkStateType state_type,
2020         gint x, gint y, gint width, gint height)
2021 {
2022     gtk2_widget = gtk2_get_widget(widget_type);
2023     (*fp_gtk_style_apply_default_background)(gtk2_widget->style,
2024             gtk2_white_pixmap, TRUE, state_type, NULL, x, y, width, height);
2025     (*fp_gtk_style_apply_default_background)(gtk2_widget->style,
2026             gtk2_black_pixmap, TRUE, state_type, NULL, x, y, width, height);
2027 }
2028 
2029 static GdkPixbuf *gtk2_get_stock_icon(gint widget_type, const gchar *stock_id,
2030         GtkIconSize size, GtkTextDirection direction, const char *detail)
2031 {
2032     init_containers();
2033     gtk2_widget = gtk2_get_widget((widget_type < 0) ? IMAGE : widget_type);
2034     gtk2_widget->state = GTK_STATE_NORMAL;
2035     (*fp_gtk_widget_set_direction)(gtk2_widget, direction);
2036     return (*fp_gtk_widget_render_icon)(gtk2_widget, stock_id, size, detail);
2037 }
2038 
2039 static jboolean gtk2_get_pixbuf_data(JNIEnv *env, GdkPixbuf* pixbuf,
2040                               jmethodID icon_upcall_method, jobject this) {
2041     if (!pixbuf) {
2042         return JNI_FALSE;
2043     }
2044     guchar *pixbuf_data = (*fp_gdk_pixbuf_get_pixels)(pixbuf);
2045     if (pixbuf_data) {
2046         int row_stride = (*fp_gdk_pixbuf_get_rowstride)(pixbuf);
2047         int width = (*fp_gdk_pixbuf_get_width)(pixbuf);
2048         int height = (*fp_gdk_pixbuf_get_height)(pixbuf);
2049         int bps = (*fp_gdk_pixbuf_get_bits_per_sample)(pixbuf);
2050         int channels = (*fp_gdk_pixbuf_get_n_channels)(pixbuf);
2051         gboolean alpha = (*fp_gdk_pixbuf_get_has_alpha)(pixbuf);
2052 
2053         jbyteArray data = (*env)->NewByteArray(env, (row_stride * height));
2054         JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
2055 
2056         (*env)->SetByteArrayRegion(env, data, 0, (row_stride * height),
2057                                    (jbyte *)pixbuf_data);
2058         (*fp_g_object_unref)(pixbuf);
2059 
2060         /* Call the callback method to create the image on the Java side. */
2061         (*env)->CallVoidMethod(env, this, icon_upcall_method, data,
2062                 width, height, row_stride, bps, channels, alpha);
2063         return JNI_TRUE;
2064     }
2065     return JNI_FALSE;
2066 }
2067 
2068 static jboolean gtk2_get_file_icon_data(JNIEnv *env, const char *filename,
2069                  GError **error, jmethodID icon_upcall_method, jobject this) {
2070     GdkPixbuf* pixbuf = fp_gdk_pixbuf_new_from_file(filename, error);
2071     return gtk2_get_pixbuf_data(env, pixbuf, icon_upcall_method, this);
2072 }
2073 
2074 static jboolean gtk2_get_icon_data(JNIEnv *env, gint widget_type,
2075                               const gchar *stock_id, GtkIconSize size,
2076                               GtkTextDirection direction, const char *detail,
2077                               jmethodID icon_upcall_method, jobject this) {
2078     GdkPixbuf* pixbuf = gtk2_get_stock_icon(widget_type, stock_id, size,
2079                                        direction, detail);
2080     return gtk2_get_pixbuf_data(env, pixbuf, icon_upcall_method, this);
2081 }
2082 
2083 /*************************************************/
2084 static gint gtk2_get_xthickness(JNIEnv *env, WidgetType widget_type)
2085 {
2086     init_containers();
2087 
2088     gtk2_widget = gtk2_get_widget(widget_type);
2089     GtkStyle* style = gtk2_widget->style;
2090     return style->xthickness;
2091 }
2092 
2093 static gint gtk2_get_ythickness(JNIEnv *env, WidgetType widget_type)
2094 {
2095     init_containers();
2096 
2097     gtk2_widget = gtk2_get_widget(widget_type);
2098     GtkStyle* style = gtk2_widget->style;
2099     return style->ythickness;
2100 }
2101 
2102 /*************************************************/
2103 static guint8 recode_color(guint16 channel)
2104 {
2105     return (guint8)(channel>>8);
2106 }
2107 
2108 static gint gtk2_get_color_for_state(JNIEnv *env, WidgetType widget_type,
2109                               GtkStateType state_type, ColorType color_type)
2110 {
2111     gint result = 0;
2112     GdkColor *color = NULL;
2113 
2114     init_containers();
2115 
2116     gtk2_widget = gtk2_get_widget(widget_type);
2117     GtkStyle* style = gtk2_widget->style;
2118 
2119     switch (color_type)
2120     {
2121         case FOREGROUND:
2122             color = &(style->fg[state_type]);
2123             break;
2124         case BACKGROUND:
2125             color = &(style->bg[state_type]);
2126             break;
2127         case TEXT_FOREGROUND:
2128             color = &(style->text[state_type]);


2140             color = &(style->mid[state_type]);
2141             break;
2142         case FOCUS:
2143         case BLACK:
2144             color = &(style->black);
2145             break;
2146         case WHITE:
2147             color = &(style->white);
2148             break;
2149     }
2150 
2151     if (color)
2152         result = recode_color(color->red)   << 16 |
2153                  recode_color(color->green) << 8  |
2154                  recode_color(color->blue);
2155 
2156     return result;
2157 }
2158 
2159 /*************************************************/
2160 static jobject create_Boolean(JNIEnv *env, jboolean boolean_value);
2161 static jobject create_Integer(JNIEnv *env, jint int_value);
2162 static jobject create_Long(JNIEnv *env, jlong long_value);
2163 static jobject create_Float(JNIEnv *env, jfloat float_value);
2164 static jobject create_Double(JNIEnv *env, jdouble double_value);
2165 static jobject create_Character(JNIEnv *env, jchar char_value);
2166 static jobject create_Insets(JNIEnv *env, GtkBorder *border);
2167 
2168 static jobject gtk2_get_class_value(JNIEnv *env, WidgetType widget_type,
2169                               const char* key)
2170 {
2171     init_containers();
2172 

2173     gtk2_widget = gtk2_get_widget(widget_type);
2174 
2175     GValue value;
2176     value.g_type = 0;
2177 
2178     GParamSpec* param = (*fp_gtk_widget_class_find_style_property)(
2179                                     ((GTypeInstance*)gtk2_widget)->g_class, key);
2180     if( param )
2181     {
2182         (*fp_g_value_init)( &value, param->value_type );
2183         (*fp_gtk_widget_style_get_property)(gtk2_widget, key, &value);
2184 
2185         if( (*fp_g_type_is_a)( param->value_type, G_TYPE_BOOLEAN ))
2186         {
2187             gboolean val = (*fp_g_value_get_boolean)(&value);
2188             return create_Boolean(env, (jboolean)val);
2189         }
2190         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_CHAR ))
2191         {
2192             gchar val = (*fp_g_value_get_char)(&value);


2273         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_BOXED ))
2274         {
2275             gpointer* val = (*fp_g_value_get_boxed)(&value);
2276             printf( "Boxed: %p\n", val );
2277         }
2278         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_POINTER ))
2279         {
2280             gpointer* val = (*fp_g_value_get_pointer)(&value);
2281             printf( "Pointer: %p\n", val );
2282         }
2283         else if( (*fp_g_type_is_a)( param->value_type, G_TYPE_OBJECT ))
2284         {
2285             GObject* val = (GObject*)(*fp_g_value_get_object)(&value);
2286             printf( "Object: %p\n", val );
2287         }*/
2288     }
2289 
2290     return NULL;
2291 }
2292 
2293 static void gtk2_set_range_value(WidgetType widget_type, jdouble value,
2294                           jdouble min, jdouble max, jdouble visible)
2295 {
2296     GtkAdjustment *adj;
2297 
2298     gtk2_widget = gtk2_get_widget(widget_type);
2299 
2300     adj = (*fp_gtk_range_get_adjustment)((GtkRange *)gtk2_widget);
2301     adj->value = (gdouble)value;
2302     adj->lower = (gdouble)min;
2303     adj->upper = (gdouble)max;
2304     adj->page_size = (gdouble)visible;
2305 }
2306 
2307 /*************************************************/
2308 static jobject create_Object(JNIEnv *env, jmethodID *cid,
2309                              const char* class_name,
2310                              const char* signature,
2311                              jvalue* value)
2312 {
2313     jclass  class;
2314     jobject result;
2315 
2316     class = (*env)->FindClass(env, class_name);
2317     if( class == NULL )
2318         return NULL; /* can't find/load the class, exception thrown */
2319 
2320     if( *cid == NULL)
2321     {
2322         *cid = (*env)->GetMethodID(env, class, "<init>", signature);
2323         if( *cid == NULL )
2324         {
2325             (*env)->DeleteLocalRef(env, class);
2326             return NULL; /* can't find/get the method, exception thrown */
2327         }
2328     }


2391     value.c = char_value;
2392 
2393     return create_Object(env, &cid, "java/lang/Character", "(C)V", &value);
2394 }
2395 
2396 
2397 jobject create_Insets(JNIEnv *env, GtkBorder *border)
2398 {
2399     static jmethodID cid = NULL;
2400     jvalue values[4];
2401 
2402     values[0].i = border->top;
2403     values[1].i = border->left;
2404     values[2].i = border->bottom;
2405     values[3].i = border->right;
2406 
2407     return create_Object(env, &cid, "java/awt/Insets", "(IIII)V", values);
2408 }
2409 
2410 /*********************************************/
2411 static jstring gtk2_get_pango_font_name(JNIEnv *env, WidgetType widget_type)
2412 {
2413     init_containers();
2414 
2415     gtk2_widget = gtk2_get_widget(widget_type);
2416     jstring  result = NULL;
2417     GtkStyle* style = gtk2_widget->style;
2418 
2419     if (style && style->font_desc)
2420     {
2421         gchar* val = (*fp_pango_font_description_to_string)(style->font_desc);
2422         result = (*env)->NewStringUTF(env, val);
2423         (*fp_g_free)( val );
2424     }
2425 
2426     return result;
2427 }
2428 
2429 /***********************************************/
2430 static jobject get_string_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2431 {
2432     jobject result = NULL;
2433     gchar*  strval = NULL;
2434 
2435     (*fp_g_object_get)(settings, key, &strval, NULL);
2436     result = (*env)->NewStringUTF(env, strval);
2437     (*fp_g_free)(strval);
2438 
2439     return result;
2440 }
2441 
2442 static jobject get_integer_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2443 {
2444     gint intval = 0;
2445     (*fp_g_object_get)(settings, key, &intval, NULL);
2446     return create_Integer(env, intval);
2447 }
2448 
2449 static jobject get_boolean_property(JNIEnv *env, GtkSettings* settings, const gchar* key)
2450 {
2451     gint intval = 0;
2452     (*fp_g_object_get)(settings, key, &intval, NULL);
2453     return create_Boolean(env, intval);
2454 }
2455 
2456 static jobject gtk2_get_setting(JNIEnv *env, Setting property)
2457 {
2458     GtkSettings* settings = (*fp_gtk_settings_get_default)();
2459 
2460     switch (property)
2461     {
2462         case GTK_FONT_NAME:
2463             return get_string_property(env, settings, "gtk-font-name");
2464         case GTK_ICON_SIZES:
2465             return get_string_property(env, settings, "gtk-icon-sizes");
2466         case GTK_CURSOR_BLINK:
2467             return get_boolean_property(env, settings, "gtk-cursor-blink");
2468         case GTK_CURSOR_BLINK_TIME:
2469             return get_integer_property(env, settings, "gtk-cursor-blink-time");
2470     }
2471 
2472     return NULL;
2473 }
2474 
2475 static GdkWindow* gtk2_get_window(void *widget) {
2476     return ((GtkWidget*)widget)->window;
2477 }
2478 
2479 void gtk2_init(GtkApi* gtk) {
2480     gtk->version = GTK_2;
2481 
2482     gtk->show_uri_load = &gtk2_show_uri_load;
2483     gtk->unload = &gtk2_unload;
2484     gtk->flush_event_loop = &flush_gtk_event_loop;
2485     gtk->gtk_check_version = fp_gtk_check_version;
2486     gtk->get_setting = &gtk2_get_setting;
2487 
2488     gtk->paint_arrow = &gtk2_paint_arrow;
2489     gtk->paint_box = &gtk2_paint_box;
2490     gtk->paint_box_gap = &gtk2_paint_box_gap;
2491     gtk->paint_expander = &gtk2_paint_expander;
2492     gtk->paint_extension = &gtk2_paint_extension;
2493     gtk->paint_flat_box = &gtk2_paint_flat_box;
2494     gtk->paint_focus = &gtk2_paint_focus;
2495     gtk->paint_handle = &gtk2_paint_handle;
2496     gtk->paint_hline = &gtk2_paint_hline;
2497     gtk->paint_vline = &gtk2_paint_vline;
2498     gtk->paint_option = &gtk2_paint_option;
2499     gtk->paint_shadow = &gtk2_paint_shadow;
2500     gtk->paint_slider = &gtk2_paint_slider;
2501     gtk->paint_background = &gtk_paint_background;
2502     gtk->paint_check = &gtk2_paint_check;
2503     gtk->set_range_value = &gtk2_set_range_value;
2504 
2505     gtk->init_painting = &gtk2_init_painting;
2506     gtk->copy_image = &gtk2_copy_image;
2507 
2508     gtk->get_xthickness = &gtk2_get_xthickness;
2509     gtk->get_ythickness = &gtk2_get_ythickness;
2510     gtk->get_color_for_state = &gtk2_get_color_for_state;
2511     gtk->get_class_value = &gtk2_get_class_value;
2512 
2513     gtk->get_pango_font_name = &gtk2_get_pango_font_name;
2514     gtk->get_icon_data = &gtk2_get_icon_data;
2515     gtk->get_file_icon_data = &gtk2_get_file_icon_data;
2516     gtk->gdk_threads_enter = fp_gdk_threads_enter;
2517     gtk->gdk_threads_leave = fp_gdk_threads_leave;
2518     gtk->gtk_show_uri = fp_gtk_show_uri;
2519     gtk->g_free = fp_g_free;
2520 
2521     gtk->gtk_file_chooser_get_filename = fp_gtk_file_chooser_get_filename;
2522     gtk->gtk_widget_hide = fp_gtk_widget_hide;
2523     gtk->gtk_main_quit = fp_gtk_main_quit;
2524     gtk->gtk_file_chooser_dialog_new = fp_gtk_file_chooser_dialog_new;
2525     gtk->gtk_file_chooser_set_current_folder =
2526                           fp_gtk_file_chooser_set_current_folder;
2527     gtk->gtk_file_chooser_set_filename = fp_gtk_file_chooser_set_filename;
2528     gtk->gtk_file_chooser_set_current_name =
2529                           fp_gtk_file_chooser_set_current_name;
2530     gtk->gtk_file_filter_add_custom = fp_gtk_file_filter_add_custom;
2531     gtk->gtk_file_chooser_set_filter = fp_gtk_file_chooser_set_filter;
2532     gtk->gtk_file_chooser_get_type = fp_gtk_file_chooser_get_type;
2533     gtk->gtk_file_filter_new = fp_gtk_file_filter_new;
2534     gtk->gtk_file_chooser_set_do_overwrite_confirmation =
2535                           fp_gtk_file_chooser_set_do_overwrite_confirmation;
2536     gtk->gtk_file_chooser_set_select_multiple =
2537                           fp_gtk_file_chooser_set_select_multiple;
2538     gtk->gtk_file_chooser_get_current_folder =
2539                           fp_gtk_file_chooser_get_current_folder;
2540     gtk->gtk_file_chooser_get_filenames = fp_gtk_file_chooser_get_filenames;
2541     gtk->gtk_g_slist_length = fp_gtk_g_slist_length;
2542     gtk->g_signal_connect_data = fp_g_signal_connect_data;
2543     gtk->gtk_widget_show = fp_gtk_widget_show;
2544     gtk->gtk_main = fp_gtk_main;
2545     gtk->gtk_main_level = fp_gtk_main_level;
2546     gtk->g_path_get_dirname = fp_g_path_get_dirname;
2547     gtk->gtk_widget_destroy = fp_gtk_widget_destroy;
2548     gtk->gtk_window_present = fp_gtk_window_present;
2549     gtk->gtk_window_move = fp_gtk_window_move;
2550     gtk->gtk_window_resize = fp_gtk_window_resize;
2551     gtk->get_window = &gtk2_get_window;
2552 
2553     gtk->g_object_unref = fp_g_object_unref;
2554     gtk->g_list_append = fp_g_list_append;
2555     gtk->g_list_free = fp_g_list_free;
2556     gtk->g_list_free_full = fp_g_list_free_full;
2557 }
< prev index next >