1 /* gtkfiledialogpeer.c -- Native implementation of GtkFileDialogPeer
2 Copyright (C) 1998, 1999, 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 #include "gnu_java_awt_peer_gtk_GtkComponentPeer.h"
41 #include "gnu_java_awt_peer_gtk_GtkFileDialogPeer.h"
43 static void handle_response (GtkDialog
*dialog
,
48 * Make a new file selection dialog
51 JNIEXPORT
void JNICALL
52 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_create
53 (JNIEnv
*env
, jobject obj
, jobject parent
)
58 /* Create global reference and save it for future use */
59 NSA_SET_GLOBAL_REF (env
, obj
);
61 parentp
= NSA_GET_PTR(env
, parent
);
65 /* FIXME: we should be using the default gnome-vfs backend but it is
66 not currently thread-safe. See:
67 http://bugzilla.gnome.org/show_bug.cgi?id=166852 */
68 widget
= gtk_file_chooser_dialog_new_with_backend
71 GTK_FILE_CHOOSER_ACTION_OPEN
,
73 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
74 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
77 /* GtkFileSelect is not modal by default */
78 gtk_window_set_modal (GTK_WINDOW (widget
), TRUE
);
80 /* We must add this window to the group so input in the others are
81 disable while it is being shown */
82 gtk_window_group_add_window (global_gtk_window_group
, GTK_WINDOW (widget
));
86 NSA_SET_PTR (env
, obj
, widget
);
89 JNIEXPORT
void JNICALL
90 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_connectSignals
91 (JNIEnv
*env
, jobject obj
)
93 void *ptr
= NSA_GET_PTR (env
, obj
);
94 jobject
*gref
= NSA_GET_GLOBAL_REF (env
, obj
);
99 g_signal_connect (G_OBJECT (GTK_DIALOG (ptr
)),
101 GTK_SIGNAL_FUNC (handle_response
), *gref
);
103 gdk_threads_leave ();
105 /* Connect the superclass signals. */
106 Java_gnu_java_awt_peer_gtk_GtkComponentPeer_connectSignals (env
, obj
);
109 JNIEXPORT jstring JNICALL
110 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeGetDirectory
111 (JNIEnv
*env
, jobject obj
)
116 ptr
= NSA_GET_PTR (env
, obj
);
118 gdk_threads_enter ();
120 str
= gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER(ptr
));
122 gdk_threads_leave ();
124 return (*env
)->NewStringUTF(env
, str
);
128 /* This function interfaces with the Java callback method of the same name.
129 This function extracts the filename from the GtkFileFilterInfo object,
130 and passes it to the Java method. The Java method will call the filter's
131 accept() method and will give back the return value. */
132 static gboolean
filenameFilterCallback (const GtkFileFilterInfo
*filter_info
,
140 cx
= (*gdk_env())->GetObjectClass (gdk_env(), (jobject
) obj
);
141 id
= (*gdk_env())->GetMethodID (gdk_env(), cx
, "filenameFilterCallback",
142 "(Ljava/lang/String;)Z");
144 filename
= (*gdk_env())->NewStringUTF(gdk_env(), filter_info
->filename
);
147 accepted
= (*gdk_env())->CallBooleanMethod(gdk_env(), obj
, id
, filename
);
153 JNIEXPORT
void JNICALL
154 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetFilenameFilter
155 (JNIEnv
*env
, jobject obj
, jobject filter_obj
__attribute__((unused
)))
158 GtkFileFilter
*filter
;
160 ptr
= NSA_GET_PTR (env
, obj
);
162 gdk_threads_enter ();
164 filter
= gtk_file_filter_new();
165 gtk_file_filter_add_custom(filter
, GTK_FILE_FILTER_FILENAME
,
166 filenameFilterCallback
, obj
, NULL
);
168 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(ptr
), filter
);
170 gdk_threads_leave ();
173 JNIEXPORT
void JNICALL
174 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetDirectory
175 (JNIEnv
*env
, jobject obj
, jstring directory
)
180 ptr
= NSA_GET_PTR (env
, obj
);
182 str
= (*env
)->GetStringUTFChars (env
, directory
, 0);
184 gdk_threads_enter ();
185 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(ptr
), str
);
186 gdk_threads_leave ();
188 (*env
)->ReleaseStringUTFChars (env
, directory
, str
);
191 JNIEXPORT
void JNICALL
192 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetFile
193 (JNIEnv
*env
, jobject obj
, jstring filename
)
198 ptr
= NSA_GET_PTR (env
, obj
);
200 str
= (*env
)->GetStringUTFChars (env
, filename
, 0);
202 gdk_threads_enter ();
203 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (ptr
), str
);
204 gdk_threads_leave ();
206 (*env
)->ReleaseStringUTFChars (env
, filename
, str
);
210 handle_response (GtkDialog
*dialog
__attribute__((unused
)),
214 static int isDisposeIDSet
= 0;
215 static int isIDSet
= 0;
216 static jmethodID gtkSetFilenameID
;
217 static jmethodID hideID
;
218 static jmethodID disposeID
;
220 G_CONST_RETURN gchar
*fileName
;
221 jstring str_fileName
= NULL
;
223 /* We only need this for the case when the user closed the window,
224 or clicked ok or cancel. */
225 if (responseId
!= GTK_RESPONSE_DELETE_EVENT
226 && responseId
!= GTK_RESPONSE_ACCEPT
227 && responseId
!= GTK_RESPONSE_CANCEL
)
230 ptr
= NSA_GET_PTR (gdk_env(), peer_obj
);
232 if (responseId
== GTK_RESPONSE_DELETE_EVENT
)
236 jclass cx
= (*gdk_env())->GetObjectClass (gdk_env(), peer_obj
);
237 disposeID
= (*gdk_env())->GetMethodID (gdk_env(), cx
, "gtkDisposeFileDialog", "()V");
241 gdk_threads_leave ();
243 /* We can dispose of the dialog now (and unblock show) */
244 (*gdk_env())->CallVoidMethod (gdk_env(), peer_obj
, disposeID
);
246 gdk_threads_enter ();
250 if (responseId
== GTK_RESPONSE_ACCEPT
) {
251 fileName
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (GTK_WIDGET (ptr
)));
252 str_fileName
= (*gdk_env())->NewStringUTF (gdk_env(), fileName
);
257 jclass cx
= (*gdk_env())->GetObjectClass (gdk_env(), peer_obj
);
258 hideID
= (*gdk_env())->GetMethodID (gdk_env(), cx
, "gtkHideFileDialog", "()V");
259 gtkSetFilenameID
= (*gdk_env())->GetMethodID (gdk_env(), cx
,
260 "gtkSetFilename", "(Ljava/lang/String;)V");
264 gdk_threads_leave ();
266 /* Set the Java object field 'file' with this value. */
267 (*gdk_env())->CallVoidMethod (gdk_env(), peer_obj
, gtkSetFilenameID
, str_fileName
);
269 /* We can hide the dialog now (and unblock show) */
270 (*gdk_env())->CallVoidMethod (gdk_env(), peer_obj
, hideID
);
272 gdk_threads_enter ();