Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkFileDialogPeer.c
blobb9fd54abbcdc994f9ca5052405b323d5f7e72681
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)
9 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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
24 combination.
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. */
39 #include "gtkpeer.h"
40 #include "gnu_java_awt_peer_gtk_GtkComponentPeer.h"
41 #include "gnu_java_awt_peer_gtk_GtkFileDialogPeer.h"
43 #define AWT_FILEDIALOG_LOAD 0
44 #define AWT_FILEDIALOG_SAVE 1
46 static void handle_response_cb (GtkDialog *dialog,
47 gint responseId,
48 jobject peer_obj);
50 static jmethodID gtkSetFilenameID;
51 static jmethodID gtkHideFileDialogID;
52 static jmethodID gtkDisposeFileDialogID;
53 static jmethodID filenameFilterCallbackID;
55 void
56 cp_gtk_filedialog_init_jni (void)
58 jclass gtkfiledialogpeer;
60 gtkfiledialogpeer =
61 (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
62 "gnu/java/awt/peer/gtk/GtkFileDialogPeer");
64 gtkDisposeFileDialogID =
65 (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(),
66 gtkfiledialogpeer,
67 "gtkDisposeFileDialog", "()V");
69 gtkHideFileDialogID =
70 (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(),
71 gtkfiledialogpeer,
72 "gtkHideFileDialog", "()V");
74 gtkSetFilenameID =
75 (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(),
76 gtkfiledialogpeer,
77 "gtkSetFilename",
78 "(Ljava/lang/String;)V");
80 filenameFilterCallbackID =
81 (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(),
82 gtkfiledialogpeer,
83 "filenameFilterCallback",
84 "(Ljava/lang/String;)Z");
88 * Make a new file selection dialog
91 JNIEXPORT void JNICALL
92 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_create
93 (JNIEnv *env, jobject obj, jobject parent, int mode)
95 void *parentp;
96 gpointer widget;
98 gdk_threads_enter ();
100 /* Create global reference and save it for future use */
101 NSA_SET_GLOBAL_REF (env, obj);
103 parentp = NSA_GET_PTR(env, parent);
105 if (mode == AWT_FILEDIALOG_LOAD)
106 widget = gtk_file_chooser_dialog_new
107 ("Open File",
108 GTK_WINDOW(parentp),
109 GTK_FILE_CHOOSER_ACTION_OPEN,
110 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
111 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
112 NULL);
113 else
115 widget = gtk_file_chooser_dialog_new
116 ("Save File",
117 GTK_WINDOW(parentp),
118 GTK_FILE_CHOOSER_ACTION_SAVE,
119 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
120 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
121 NULL);
122 #if GTK_MINOR_VERSION >= 8
123 gtk_file_chooser_set_do_overwrite_confirmation
124 (GTK_FILE_CHOOSER (widget), TRUE);
125 #endif
129 /* GtkFileChooserDialog is not modal by default */
130 gtk_window_set_modal (GTK_WINDOW (widget), TRUE);
132 /* We must add this window to the group so input in the others are
133 disable while it is being shown */
134 gtk_window_group_add_window (cp_gtk_global_window_group,
135 GTK_WINDOW (widget));
137 NSA_SET_PTR (env, obj, widget);
139 gdk_threads_leave ();
142 JNIEXPORT void JNICALL
143 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_connectSignals
144 (JNIEnv *env, jobject obj)
146 void *ptr = NULL;
147 jobject *gref = NULL;
149 gdk_threads_enter ();
151 ptr = NSA_GET_PTR (env, obj);
152 gref = NSA_GET_GLOBAL_REF (env, obj);
154 /* FileDialog signals */
155 g_signal_connect (G_OBJECT (ptr), "response",
156 G_CALLBACK (handle_response_cb), *gref);
158 /* Component signals */
159 cp_gtk_component_connect_signals (G_OBJECT (ptr), gref);
161 gdk_threads_leave ();
164 JNIEXPORT jstring JNICALL
165 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeGetDirectory
166 (JNIEnv *env, jobject obj)
168 void *ptr;
169 const char *str;
171 gdk_threads_enter ();
173 ptr = NSA_GET_PTR (env, obj);
175 str = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER(ptr));
177 gdk_threads_leave ();
179 return (*env)->NewStringUTF(env, str);
183 /* This function interfaces with the Java callback method of the same name.
184 This function extracts the filename from the GtkFileFilterInfo object,
185 and passes it to the Java method. The Java method will call the filter's
186 accept() method and will give back the return value. */
187 static gboolean filename_filter_cb (const GtkFileFilterInfo *filter_info,
188 gpointer obj)
190 jstring *filename;
191 gboolean accepted;
193 filename = (*cp_gtk_gdk_env())->NewStringUTF(cp_gtk_gdk_env(), filter_info->filename);
195 accepted = (*cp_gtk_gdk_env())->CallBooleanMethod(cp_gtk_gdk_env(), obj,
196 filenameFilterCallbackID,
197 filename);
199 return accepted;
202 JNIEXPORT void JNICALL
203 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetFilenameFilter
204 (JNIEnv *env, jobject obj, jobject filter_obj __attribute__((unused)))
206 void *ptr;
207 GtkFileFilter *filter;
209 gdk_threads_enter ();
211 ptr = NSA_GET_PTR (env, obj);
213 filter = gtk_file_filter_new();
214 gtk_file_filter_add_custom(filter, GTK_FILE_FILTER_FILENAME,
215 filename_filter_cb, obj, NULL);
217 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(ptr), filter);
219 gdk_threads_leave ();
222 JNIEXPORT void JNICALL
223 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetDirectory
224 (JNIEnv *env, jobject obj, jstring directory)
226 void *ptr;
227 const char *str;
229 gdk_threads_enter ();
231 ptr = NSA_GET_PTR (env, obj);
233 str = (*env)->GetStringUTFChars (env, directory, 0);
235 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(ptr), str);
237 (*env)->ReleaseStringUTFChars (env, directory, str);
239 gdk_threads_leave ();
242 JNIEXPORT void JNICALL
243 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetFile
244 (JNIEnv *env, jobject obj, jstring filename)
246 void *ptr;
247 const char *str;
249 gdk_threads_enter ();
251 ptr = NSA_GET_PTR (env, obj);
253 str = (*env)->GetStringUTFChars (env, filename, 0);
255 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (ptr), str);
257 (*env)->ReleaseStringUTFChars (env, filename, str);
259 gdk_threads_leave ();
262 static void
263 handle_response_cb (GtkDialog *dialog __attribute__((unused)),
264 gint responseId,
265 jobject peer_obj)
267 void *ptr;
268 G_CONST_RETURN gchar *fileName;
269 jstring str_fileName = NULL;
271 /* We only need this for the case when the user closed the window,
272 or clicked ok or cancel. */
273 if (responseId != GTK_RESPONSE_DELETE_EVENT
274 && responseId != GTK_RESPONSE_ACCEPT
275 && responseId != GTK_RESPONSE_CANCEL)
276 return;
278 ptr = NSA_GET_PTR (cp_gtk_gdk_env(), peer_obj);
280 if (responseId == GTK_RESPONSE_DELETE_EVENT)
282 /* We can dispose of the dialog now (and unblock show) */
283 (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer_obj,
284 gtkDisposeFileDialogID);
286 return;
289 if (responseId == GTK_RESPONSE_ACCEPT)
291 fileName = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (GTK_WIDGET (ptr)));
292 str_fileName = (*cp_gtk_gdk_env())->NewStringUTF (cp_gtk_gdk_env(), fileName);
295 /* Set the Java object field 'file' with this value. */
296 (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer_obj,
297 gtkSetFilenameID, str_fileName);
299 /* We can hide the dialog now (and unblock show) */
300 (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer_obj,
301 gtkHideFileDialogID);