2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkTextAreaPeer.c
blob71a789ed37f9f80d34f16bdb8443dc697a9370bb
1 /* gtktextareapeer.c -- Native implementation of GtkTextAreaPeer
2 Copyright (C) 1998, 1999, 2003 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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_GtkTextAreaPeer.h"
42 #define TEXT_FROM_SW(obj) (GTK_TEXT_VIEW(GTK_SCROLLED_WINDOW (obj)->container.child))
43 JNIEXPORT void JNICALL
44 Java_gnu_java_awt_peer_gtk_GtkTextAreaPeer_create
45 (JNIEnv *env, jobject obj, jint scroll)
47 GtkWidget *text, *sw;
49 /* Create global reference and save it for future use */
50 NSA_SET_GLOBAL_REF (env, obj);
52 gdk_threads_enter ();
54 text = gtk_text_view_new ();
55 gtk_widget_show (text);
57 sw = gtk_scrolled_window_new (NULL, NULL);
58 gtk_container_add (GTK_CONTAINER (sw), text);
60 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
61 /* horizontal scrollbar */
62 (scroll == AWT_TEXTAREA_SCROLLBARS_BOTH
63 || scroll == AWT_TEXTAREA_SCROLLBARS_HORIZONTAL_ONLY) ?
64 GTK_POLICY_ALWAYS : GTK_POLICY_NEVER,
65 /* vertical scrollbar */
66 (scroll == AWT_TEXTAREA_SCROLLBARS_BOTH
67 || scroll == AWT_TEXTAREA_SCROLLBARS_VERTICAL_ONLY) ?
68 GTK_POLICY_ALWAYS : GTK_POLICY_NEVER);
70 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text),
71 (scroll == AWT_TEXTAREA_SCROLLBARS_BOTH
72 || scroll == AWT_TEXTAREA_SCROLLBARS_HORIZONTAL_ONLY)
73 ? GTK_WRAP_NONE : GTK_WRAP_WORD);
75 gdk_threads_leave ();
77 NSA_SET_PTR (env, obj, sw);
80 JNIEXPORT void JNICALL
81 Java_gnu_java_awt_peer_gtk_GtkTextAreaPeer_gtkTextGetSize
82 (JNIEnv *env, jobject obj, jintArray jdims)
84 void *ptr;
85 jint *dims;
86 GtkWidget *text;
87 GtkRequisition requisition;
89 ptr = NSA_GET_PTR (env, obj);
91 dims = (*env)->GetIntArrayElements (env, jdims, 0);
92 dims[0] = dims[1] = 0;
94 gdk_threads_enter ();
96 text = GTK_WIDGET (TEXT_FROM_SW (ptr));
98 gtk_widget_size_request(GTK_WIDGET (text), &requisition);
99 dims[0] = requisition.width;
100 dims[1] = requisition.height;
102 gdk_threads_leave ();
104 (*env)->ReleaseIntArrayElements (env, jdims, dims, 0);
108 JNIEXPORT void JNICALL
109 Java_gnu_java_awt_peer_gtk_GtkTextAreaPeer_insert
110 (JNIEnv *env, jobject obj, jstring contents, jint position)
112 GtkTextBuffer *buf;
113 GtkTextIter iter;
114 GtkWidget *text;
115 void *ptr;
116 const char *str;
117 int pos=position;
119 ptr = NSA_GET_PTR (env, obj);
120 str = (*env)->GetStringUTFChars (env, contents, NULL);
122 gdk_threads_enter ();
124 text = GTK_WIDGET (TEXT_FROM_SW (ptr));
126 buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text));
127 gtk_text_buffer_get_iter_at_offset (buf, &iter, pos);
128 gtk_text_buffer_insert (buf, &iter, str, strlen (str));
130 gdk_threads_leave ();
132 (*env)->ReleaseStringUTFChars (env, contents, str);
135 JNIEXPORT void JNICALL
136 Java_gnu_java_awt_peer_gtk_GtkTextAreaPeer_replaceRange
137 (JNIEnv *env, jobject obj, jstring contents, jint start, jint end)
139 GtkWidget *text;
140 GtkTextBuffer *buf;
141 GtkTextIter iter, startIter, endIter;
142 void *ptr;
143 const char *str;
144 int mystart = start;
145 int myend = end;
147 ptr = NSA_GET_PTR (env, obj);
148 str = (*env)->GetStringUTFChars (env, contents, NULL);
150 gdk_threads_enter ();
152 text = GTK_WIDGET (TEXT_FROM_SW (ptr));
154 buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text));
156 gtk_text_buffer_get_iter_at_offset (buf, &startIter, mystart);
157 gtk_text_buffer_get_iter_at_offset (buf, &endIter, myend);
158 gtk_text_buffer_delete (buf, &startIter, &endIter);
160 gtk_text_buffer_get_iter_at_offset (buf, &iter, mystart);
161 gtk_text_buffer_insert(buf, &iter, str, strlen (str));
163 gdk_threads_leave ();
164 (*env)->ReleaseStringUTFChars (env, contents, str);
167 JNIEXPORT void JNICALL
168 Java_gnu_java_awt_peer_gtk_GtkTextAreaPeer_gtkSetFont
169 (JNIEnv *env, jobject obj, jstring name, jint style, jint size)
171 const char *font_name;
172 void *ptr;
173 GtkWidget *text;
174 PangoFontDescription *font_desc;
176 ptr = NSA_GET_PTR (env, obj);
178 text = GTK_WIDGET (TEXT_FROM_SW (ptr));
180 font_name = (*env)->GetStringUTFChars (env, name, NULL);
182 gdk_threads_enter();
184 font_desc = pango_font_description_from_string (font_name);
185 pango_font_description_set_size (font_desc, size * PANGO_SCALE);
187 if (style & AWT_STYLE_BOLD)
188 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
190 if (style & AWT_STYLE_ITALIC)
191 pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
193 gtk_widget_modify_font (GTK_WIDGET(text), font_desc);
195 pango_font_description_free (font_desc);
197 gdk_threads_leave();
199 (*env)->ReleaseStringUTFChars (env, name, font_name);