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

Print this page




  42 JNIEXPORT void JNICALL Java_sun_awt_X11_GtkFileDialogPeer_initIDs
  43 (JNIEnv *env, jclass cx)
  44 {
  45     filenameFilterCallbackMethodID = (*env)->GetMethodID(env, cx,
  46             "filenameFilterCallback", "(Ljava/lang/String;)Z");
  47     DASSERT(filenameFilterCallbackMethodID != NULL);
  48     CHECK_NULL(filenameFilterCallbackMethodID);
  49 
  50     setFileInternalMethodID = (*env)->GetMethodID(env, cx,
  51             "setFileInternal", "(Ljava/lang/String;[Ljava/lang/String;)V");
  52     DASSERT(setFileInternalMethodID != NULL);
  53     CHECK_NULL(setFileInternalMethodID);
  54 
  55     widgetFieldID = (*env)->GetFieldID(env, cx, "widget", "J");
  56     DASSERT(widgetFieldID != NULL);
  57 }
  58 
  59 static gboolean filenameFilterCallback(const GtkFileFilterInfo * filter_info, gpointer obj)
  60 {
  61     JNIEnv *env;
  62     jclass cx;
  63     jstring filename;
  64 
  65     env = (JNIEnv *) JNU_GetEnv(jvm, JNI_VERSION_1_2);
  66 
  67     filename = (*env)->NewStringUTF(env, filter_info->filename);
  68     JNU_CHECK_EXCEPTION_RETURN(env, FALSE);
  69 
  70     return (*env)->CallBooleanMethod(env, obj, filenameFilterCallbackMethodID,
  71             filename);
  72 }
  73 
  74 static void quit(JNIEnv * env, jobject jpeer, gboolean isSignalHandler)
  75 {
  76     GtkWidget * dialog = (GtkWidget*)jlong_to_ptr(
  77             (*env)->GetLongField(env, jpeer, widgetFieldID));
  78 
  79     if (dialog != NULL)
  80     {
  81         // Callbacks from GTK signals are made within the GTK lock
  82         // So, within a signal handler there is no need to call


 140 (JNIEnv * env, jobject jpeer, jint x, jint y, jint width, jint height, jint op)
 141 {
 142     GtkWindow* dialog;
 143 
 144     fp_gdk_threads_enter();
 145 
 146     dialog = (GtkWindow*)jlong_to_ptr(
 147         (*env)->GetLongField(env, jpeer, widgetFieldID));
 148 
 149     if (dialog != NULL) {
 150         if (x >= 0 && y >= 0) {
 151             fp_gtk_window_move(dialog, (gint)x, (gint)y);
 152         }
 153         if (width > 0 && height > 0) {
 154             fp_gtk_window_resize(dialog, (gint)width, (gint)height);
 155         }
 156     }
 157 
 158     fp_gdk_threads_leave();
 159 }
 160 
 161 /**
 162  * Convert a GSList to an array of filenames (without the parent folder)
 163  */
 164 static jobjectArray toFilenamesArray(JNIEnv *env, GSList* list)
 165 {
 166     jstring str;
 167     jclass stringCls;
 168     GSList *iterator;
 169     jobjectArray array;
 170     int i;
 171     char* entry;
 172 
 173     if (NULL == list) {
 174         return NULL;
 175     }
 176 
 177     stringCls = (*env)->FindClass(env, "java/lang/String");
 178     if (stringCls == NULL) {
 179         (*env)->ExceptionClear(env);
 180         JNU_ThrowInternalError(env, "Could not get java.lang.String class");
 181         return NULL;


 182     }
 183 
 184     array = (*env)->NewObjectArray(env, fp_gtk_g_slist_length(list), stringCls, NULL);
 185     if (array == NULL) {
 186         (*env)->ExceptionClear(env);
 187         JNU_ThrowInternalError(env, "Could not instantiate array files array");
 188         return NULL;
 189     }

 190 
 191     i = 0;
 192     for (iterator = list; iterator; iterator = iterator->next) {
 193         entry = (char*) iterator->data;
 194         entry = strrchr(entry, '/') + 1;
 195         str = (*env)->NewStringUTF(env, entry);
 196         if (str && !(*env)->ExceptionCheck(env)) {
 197             (*env)->SetObjectArrayElement(env, array, i, str);
 198         }
 199         i++;





 200     }
 201 
 202     return array;
 203 }
 204 
 205 /**
 206  * Convert a GSList to an array of filenames (with the parent folder)
 207  */
 208 static jobjectArray toPathAndFilenamesArray(JNIEnv *env, GSList* list)
 209 {
 210     jstring str;
 211     jclass stringCls;
 212     GSList *iterator;
 213     jobjectArray array;
 214     int i;
 215     char* entry;
 216 

 217 
 218     if (list == NULL) {
 219         return NULL;
 220     }
 221 
 222     stringCls = (*env)->FindClass(env, "java/lang/String");
 223     if (stringCls == NULL) {
 224         (*env)->ExceptionClear(env);
 225         JNU_ThrowInternalError(env, "Could not get java.lang.String class");
 226         return NULL;
 227     }
 228 
 229     array = (*env)->NewObjectArray(env, fp_gtk_g_slist_length(list), stringCls, NULL);
 230     if (array == NULL) {
 231         (*env)->ExceptionClear(env);
 232         JNU_ThrowInternalError(env, "Could not instantiate array files array");
 233         return NULL;
 234     }
 235 
 236     i = 0;
 237     for (iterator = list; iterator; iterator = iterator->next) {
 238         entry = (char*) iterator->data;




 239 
 240         //check for leading slash.
 241         if (entry[0] == '/') {







 242             entry++;
 243         }
 244 
 245         str = (*env)->NewStringUTF(env, entry);
 246         if (str && !(*env)->ExceptionCheck(env)) {
 247             (*env)->SetObjectArrayElement(env, array, i, str);
 248         }
 249         i++;
 250     }
 251 

 252     return array;
 253 }
 254 
 255 static void handle_response(GtkWidget* aDialog, gint responseId, gpointer obj)
 256 {
 257     JNIEnv *env;
 258     char *current_folder;
 259     GSList *filenames;
 260     jclass cx;
 261     jstring jcurrent_folder;
 262     jobjectArray jfilenames;
 263 
 264     env = (JNIEnv *) JNU_GetEnv(jvm, JNI_VERSION_1_2);
 265     current_folder = NULL;
 266     filenames = NULL;
 267     gboolean full_path_names = FALSE;
 268 
 269     if (responseId == GTK_RESPONSE_ACCEPT) {
 270         current_folder = fp_gtk_file_chooser_get_current_folder(
 271                 GTK_FILE_CHOOSER(aDialog));
 272         if (current_folder == NULL) {
 273             full_path_names = TRUE;
 274         }
 275         filenames = fp_gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(aDialog));
 276     }
 277     if (full_path_names) {
 278         //This is a hack for use with "Recent Folders" in gtk where each
 279         //file could have its own directory.
 280         jfilenames = toPathAndFilenamesArray(env, filenames);
 281         jcurrent_folder = (*env)->NewStringUTF(env, "/");
 282     } else {
 283         jfilenames = toFilenamesArray(env, filenames);
 284         jcurrent_folder = (*env)->NewStringUTF(env, current_folder);
 285     }
 286     if (!(*env)->ExceptionCheck(env)) {
 287         (*env)->CallVoidMethod(env, obj, setFileInternalMethodID,
 288                                jcurrent_folder, jfilenames);
 289     }
 290     fp_g_free(current_folder);
 291     quit(env, (jobject)obj, TRUE);
 292 }
 293 
 294 /*
 295  * Class:     sun_awt_X11_GtkFileDialogPeer
 296  * Method:    run
 297  * Signature: (Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/io/FilenameFilter;ZII)V
 298  */
 299 JNIEXPORT void JNICALL
 300 Java_sun_awt_X11_GtkFileDialogPeer_run(JNIEnv * env, jobject jpeer,
 301         jstring jtitle, jint mode, jstring jdir, jstring jfile,
 302         jobject jfilter, jboolean multiple, int x, int y)
 303 {
 304     GtkWidget *dialog = NULL;
 305     GtkFileFilter *filter;
 306 
 307     if (jvm == NULL) {
 308         (*env)->GetJavaVM(env, &jvm);
 309         JNU_CHECK_EXCEPTION(env);
 310     }




  42 JNIEXPORT void JNICALL Java_sun_awt_X11_GtkFileDialogPeer_initIDs
  43 (JNIEnv *env, jclass cx)
  44 {
  45     filenameFilterCallbackMethodID = (*env)->GetMethodID(env, cx,
  46             "filenameFilterCallback", "(Ljava/lang/String;)Z");
  47     DASSERT(filenameFilterCallbackMethodID != NULL);
  48     CHECK_NULL(filenameFilterCallbackMethodID);
  49 
  50     setFileInternalMethodID = (*env)->GetMethodID(env, cx,
  51             "setFileInternal", "(Ljava/lang/String;[Ljava/lang/String;)V");
  52     DASSERT(setFileInternalMethodID != NULL);
  53     CHECK_NULL(setFileInternalMethodID);
  54 
  55     widgetFieldID = (*env)->GetFieldID(env, cx, "widget", "J");
  56     DASSERT(widgetFieldID != NULL);
  57 }
  58 
  59 static gboolean filenameFilterCallback(const GtkFileFilterInfo * filter_info, gpointer obj)
  60 {
  61     JNIEnv *env;

  62     jstring filename;
  63 
  64     env = (JNIEnv *) JNU_GetEnv(jvm, JNI_VERSION_1_2);
  65 
  66     filename = (*env)->NewStringUTF(env, filter_info->filename);
  67     JNU_CHECK_EXCEPTION_RETURN(env, FALSE);
  68 
  69     return (*env)->CallBooleanMethod(env, obj, filenameFilterCallbackMethodID,
  70             filename);
  71 }
  72 
  73 static void quit(JNIEnv * env, jobject jpeer, gboolean isSignalHandler)
  74 {
  75     GtkWidget * dialog = (GtkWidget*)jlong_to_ptr(
  76             (*env)->GetLongField(env, jpeer, widgetFieldID));
  77 
  78     if (dialog != NULL)
  79     {
  80         // Callbacks from GTK signals are made within the GTK lock
  81         // So, within a signal handler there is no need to call


 139 (JNIEnv * env, jobject jpeer, jint x, jint y, jint width, jint height, jint op)
 140 {
 141     GtkWindow* dialog;
 142 
 143     fp_gdk_threads_enter();
 144 
 145     dialog = (GtkWindow*)jlong_to_ptr(
 146         (*env)->GetLongField(env, jpeer, widgetFieldID));
 147 
 148     if (dialog != NULL) {
 149         if (x >= 0 && y >= 0) {
 150             fp_gtk_window_move(dialog, (gint)x, (gint)y);
 151         }
 152         if (width > 0 && height > 0) {
 153             fp_gtk_window_resize(dialog, (gint)width, (gint)height);
 154         }
 155     }
 156 
 157     fp_gdk_threads_leave();
 158 }
 159 /*
 160  * baseDir should be freed by user.

 161  */
 162 static gboolean isFromSameDirectory(GSList* list, gchar** baseDir) {







 163 
 164     GSList *it = list;
 165     gchar* prevDir = NULL;
 166     gboolean isAllDirsSame = TRUE;
 167 
 168     while (it) {
 169         gchar* dir = fp_g_path_get_dirname((gchar*) it->data);
 170 
 171         if (prevDir && strcmp(prevDir, dir) != 0) {
 172             isAllDirsSame = FALSE;
 173             fp_g_free(dir);
 174             break;
 175         }
 176 
 177         if (!prevDir) {
 178             prevDir = strdup(dir);



 179         }
 180         fp_g_free(dir);
 181 
 182         it = it->next;






 183     }
 184 
 185     if (isAllDirsSame) {
 186         *baseDir = prevDir;
 187     } else {
 188         free(prevDir);
 189         *baseDir = strdup("/");
 190     }
 191 
 192     return isAllDirsSame;
 193 }
 194 
 195 /**
 196  * Convert a GSList to an array of filenames
 197  */
 198 static jobjectArray toFilenamesArray(JNIEnv *env, GSList* list, jstring* jcurrent_folder)
 199 {
 200     jstring str;
 201     jclass stringCls;
 202     GSList *iterator;
 203     jobjectArray array;
 204     int i;
 205     gchar* entry;
 206     gchar * baseDir;
 207     gboolean isFromSameDir;
 208 
 209     if (list == NULL) {
 210         return NULL;
 211     }
 212 
 213     stringCls = (*env)->FindClass(env, "java/lang/String");
 214     if (stringCls == NULL) {


 215         return NULL;
 216     }
 217 
 218     array = (*env)->NewObjectArray(env, fp_gtk_g_slist_length(list), stringCls, NULL);
 219     if (array == NULL) {


 220         return NULL;
 221     }
 222 
 223     isFromSameDir = isFromSameDirectory(list, &baseDir);
 224 
 225     *jcurrent_folder = (*env)->NewStringUTF(env, baseDir);
 226     if (*jcurrent_folder == NULL) {
 227         free(baseDir);
 228         return NULL;
 229     }
 230 
 231     for (iterator = list, i=0;
 232             iterator;
 233             iterator = iterator->next, i++) {
 234 
 235         entry = (gchar*) iterator->data;
 236 
 237         if (isFromSameDir) {
 238             entry = strrchr(entry, '/') + 1;
 239         } else if (entry[0] == '/') {
 240             entry++;
 241         }
 242 
 243         str = (*env)->NewStringUTF(env, entry);
 244         if (str && !(*env)->ExceptionCheck(env)) {
 245             (*env)->SetObjectArrayElement(env, array, i, str);
 246         }

 247     }
 248 
 249     free(baseDir);
 250     return array;
 251 }
 252 
 253 static void handle_response(GtkWidget* aDialog, gint responseId, gpointer obj)
 254 {
 255     JNIEnv *env;

 256     GSList *filenames;
 257     jstring jcurrent_folder = NULL;

 258     jobjectArray jfilenames;
 259 
 260     env = (JNIEnv *) JNU_GetEnv(jvm, JNI_VERSION_1_2);

 261     filenames = NULL;

 262 
 263     if (responseId == GTK_RESPONSE_ACCEPT) {





 264         filenames = fp_gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(aDialog));
 265     }
 266 
 267     jfilenames = toFilenamesArray(env, filenames, &jcurrent_folder);
 268 






 269     if (!(*env)->ExceptionCheck(env)) {
 270         (*env)->CallVoidMethod(env, obj, setFileInternalMethodID,
 271                                jcurrent_folder, jfilenames);
 272     }
 273 
 274     quit(env, (jobject)obj, TRUE);
 275 }
 276 
 277 /*
 278  * Class:     sun_awt_X11_GtkFileDialogPeer
 279  * Method:    run
 280  * Signature: (Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/io/FilenameFilter;ZII)V
 281  */
 282 JNIEXPORT void JNICALL
 283 Java_sun_awt_X11_GtkFileDialogPeer_run(JNIEnv * env, jobject jpeer,
 284         jstring jtitle, jint mode, jstring jdir, jstring jfile,
 285         jobject jfilter, jboolean multiple, int x, int y)
 286 {
 287     GtkWidget *dialog = NULL;
 288     GtkFileFilter *filter;
 289 
 290     if (jvm == NULL) {
 291         (*env)->GetJavaVM(env, &jvm);
 292         JNU_CHECK_EXCEPTION(env);
 293     }