Commit baba70d0 by Fernando Nasser Committed by Fernando Nasser

GtkChoicePeer.java (postItemEvent): Rename to...

2003-12-22  Fernando Nasser  <fnasser@redhat.com>

        * gnu/java/awt/peer/gtk/GtkChoicePeer.java (postItemEvent): Rename to...        (choicePostItemEvent): Change signature to more specific String object.
        * java/awt/Choice.java (add): Generate ItemEvent for the first item
        added.
        (insert): Generate ItemEvent if insertion caused
        selection to change.
        (remove): Generate ItemEvent if removal cause selection to change.
        (removeAll): Change algorithm to prevent generation of ItemEvents.
        * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkChoicePeer.c
        (connect_choice_item_selectable_hook): Change argument type.
        Fix argument value.
        Make sure resources are feed by registering callback.
        (Java_gnu_java_awt_peer_gtk_GtkChoicePeer_append): Adjust call to the
        above function.
        (Java_gnu_java_awt_peer_gtk_GtkChoicePeer_add): Ditto.
        (item_activate): Ditto.
        (Java_gnu_java_awt_peer_gtk_GtkChoicePeer_remove): Destroy removed
        menuitem.
        (item_removed): New function.  Free resources.
        * jni/gtk-peer/gtkpeer.h (item_event_hook_info): Change member type and
        name.
        * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c
        (Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit): Obtain MethodID for
        choicePostItemEvent.

From-SVN: r74979
parent 459ac19f
2003-12-22 Fernando Nasser <fnasser@redhat.com>
* gnu/java/awt/peer/gtk/GtkChoicePeer.java (postItemEvent): Rename to...
(choicePostItemEvent): Change signature to more specific String object.
* java/awt/Choice.java (add): Generate ItemEvent for the first item
added.
(insert): Generate ItemEvent if insertion caused
selection to change.
(remove): Generate ItemEvent if removal cause selection to change.
(removeAll): Change algorithm to prevent generation of ItemEvents.
* jni/gtk-peer/gnu_java_awt_peer_gtk_GtkChoicePeer.c
(connect_choice_item_selectable_hook): Change argument type.
Fix argument value.
Make sure resources are feed by registering callback.
(Java_gnu_java_awt_peer_gtk_GtkChoicePeer_append): Adjust call to the
above function.
(Java_gnu_java_awt_peer_gtk_GtkChoicePeer_add): Ditto.
(item_activate): Ditto.
(Java_gnu_java_awt_peer_gtk_GtkChoicePeer_remove): Destroy removed
menuitem.
(item_removed): New function. Free resources.
* jni/gtk-peer/gtkpeer.h (item_event_hook_info): Change member type and
name.
* jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c
(Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit): Obtain MethodID for
choicePostItemEvent.
2003-12-23 Michael Koch <konqueror@gmx.de> 2003-12-23 Michael Koch <konqueror@gmx.de>
* javax/print/attribute/standard/Copies.java, * javax/print/attribute/standard/Copies.java,
......
...@@ -84,10 +84,10 @@ public class GtkChoicePeer extends GtkComponentPeer ...@@ -84,10 +84,10 @@ public class GtkChoicePeer extends GtkComponentPeer
} }
*/ */
protected void postItemEvent (Object item, int stateChange) protected void choicePostItemEvent (String label, int stateChange)
{ {
if (stateChange == ItemEvent.SELECTED) if (stateChange == ItemEvent.SELECTED)
((Choice) awtComponent).select ((String) item); ((Choice) awtComponent).select (label);
super.postItemEvent (item, stateChange); super.postItemEvent (label, stateChange);
} }
} }
...@@ -171,7 +171,15 @@ add(String item) ...@@ -171,7 +171,15 @@ add(String item)
} }
if (i == 0) if (i == 0)
select (0); {
selectedIndex = 0;
// We must generate an ItemEvent here
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent (
new ItemEvent ((ItemSelectable)this,
ItemEvent.ITEM_STATE_CHANGED,
getItem(0),
ItemEvent.SELECTED));
}
} }
/*************************************************************************/ /*************************************************************************/
...@@ -223,7 +231,15 @@ insert(String item, int index) ...@@ -223,7 +231,15 @@ insert(String item, int index)
} }
if (getItemCount () == 1 || selectedIndex >= index) if (getItemCount () == 1 || selectedIndex >= index)
{
select (0); select (0);
// We must generate an ItemEvent here
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent (
new ItemEvent ((ItemSelectable)this,
ItemEvent.ITEM_STATE_CHANGED,
getItem(0),
ItemEvent.SELECTED));
}
} }
/*************************************************************************/ /*************************************************************************/
...@@ -265,8 +281,16 @@ remove(int index) ...@@ -265,8 +281,16 @@ remove(int index)
cp.remove (index); cp.remove (index);
} }
if (index == selectedIndex) if ((index == selectedIndex) && (getItemCount() > 0))
{
select (0); select (0);
// We must generate an ItemEvent here
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent (
new ItemEvent ((ItemSelectable)this,
ItemEvent.ITEM_STATE_CHANGED,
getItem(0),
ItemEvent.SELECTED));
}
else if (selectedIndex > index) else if (selectedIndex > index)
--selectedIndex; --selectedIndex;
} }
...@@ -281,11 +305,27 @@ removeAll() ...@@ -281,11 +305,27 @@ removeAll()
{ {
int count = getItemCount(); int count = getItemCount();
for (int i = 0; i < count; i++) if (count <= 0)
return;
ChoicePeer cp = (ChoicePeer) peer;
// Select the first item to prevent an spurious ItemEvent to be generated
if (cp != null)
{
cp.select (0);
selectedIndex = 0; // Just to keep consistent
}
for (int i = (count - 1); i >= 0; i--)
{ {
// Always remove 0. // Always remove the last to avoid generation of ItemEvents.
remove(0); pItems.removeElementAt(i);
if (cp != null)
cp.remove (i);
} }
selectedIndex = -1;
} }
/*************************************************************************/ /*************************************************************************/
......
...@@ -41,8 +41,9 @@ exception statement from your version. */ ...@@ -41,8 +41,9 @@ exception statement from your version. */
static void connect_choice_item_selectable_hook (JNIEnv *env, static void connect_choice_item_selectable_hook (JNIEnv *env,
jobject peer_obj, jobject peer_obj,
GtkItem *item, GtkItem *menuitem,
jobject item_obj); const char *label);
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkChoicePeer_create Java_gnu_java_awt_peer_gtk_GtkChoicePeer_create
(JNIEnv *env, jobject obj) (JNIEnv *env, jobject obj)
...@@ -101,14 +102,13 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_append ...@@ -101,14 +102,13 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_append
label = (*env)->GetStringUTFChars (env, item, NULL); label = (*env)->GetStringUTFChars (env, item, NULL);
menuitem = gtk_menu_item_new_with_label (label); menuitem = gtk_menu_item_new_with_label (label);
(*env)->ReleaseStringUTFChars (env, item, label);
gtk_menu_append (menu, menuitem); gtk_menu_append (menu, menuitem);
gtk_widget_show (menuitem); gtk_widget_show (menuitem);
connect_choice_item_selectable_hook (env, obj, connect_choice_item_selectable_hook (env, obj,
GTK_ITEM (menuitem), item); GTK_ITEM (menuitem), label);
(*env)->ReleaseStringUTFChars (env, item, label);
} }
if (need_set_history) if (need_set_history)
...@@ -139,7 +139,8 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_add ...@@ -139,7 +139,8 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_add
menuitem = gtk_menu_item_new_with_label (label); menuitem = gtk_menu_item_new_with_label (label);
gtk_menu_insert (GTK_MENU (menu), menuitem, index); gtk_menu_insert (GTK_MENU (menu), menuitem, index);
gtk_widget_show (menuitem); gtk_widget_show (menuitem);
connect_choice_item_selectable_hook (env, obj, GTK_ITEM (menuitem), item);
connect_choice_item_selectable_hook (env, obj, GTK_ITEM (menuitem), label);
if (need_set_history) if (need_set_history)
gtk_option_menu_set_history (GTK_OPTION_MENU (ptr), 0); gtk_option_menu_set_history (GTK_OPTION_MENU (ptr), 0);
...@@ -155,14 +156,19 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_remove ...@@ -155,14 +156,19 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_remove
{ {
void *ptr; void *ptr;
GtkContainer *menu; GtkContainer *menu;
GtkWidget *menuitem;
GList *children; GList *children;
ptr = NSA_GET_PTR (env, obj); ptr = NSA_GET_PTR (env, obj);
gdk_threads_enter (); gdk_threads_enter ();
menu = GTK_CONTAINER (gtk_option_menu_get_menu (GTK_OPTION_MENU (ptr))); menu = GTK_CONTAINER (gtk_option_menu_get_menu (GTK_OPTION_MENU (ptr)));
children = gtk_container_children (menu); children = gtk_container_children (menu);
gtk_container_remove (menu, GTK_WIDGET (g_list_nth (children, index)->data)); menuitem = GTK_WIDGET (g_list_nth (children, index)->data);
gtk_container_remove (menu, menuitem);
gtk_widget_destroy (menuitem);
gdk_threads_leave (); gdk_threads_leave ();
} }
...@@ -179,38 +185,49 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_select ...@@ -179,38 +185,49 @@ Java_gnu_java_awt_peer_gtk_GtkChoicePeer_select
gdk_threads_leave (); gdk_threads_leave ();
} }
static void static void
item_activate (GtkItem *item __attribute__((unused)), item_activate (GtkItem *item __attribute__((unused)),
struct item_event_hook_info *ie) struct item_event_hook_info *ie)
{ {
gdk_threads_leave (); gdk_threads_leave ();
jstring label = (*gdk_env)->NewStringUTF (gdk_env, ie->label);
(*gdk_env)->CallVoidMethod (gdk_env, ie->peer_obj, (*gdk_env)->CallVoidMethod (gdk_env, ie->peer_obj,
postItemEventID, choicePostItemEventID,
ie->item_obj, label,
(jint) AWT_ITEM_SELECTED); (jint) AWT_ITEM_SELECTED);
gdk_threads_enter (); gdk_threads_enter ();
} }
static void static void
connect_choice_item_selectable_hook (JNIEnv *env, jobject peer_obj, item_removed (gpointer data,
GtkItem *item, jobject item_obj) GClosure gc __attribute__((unused)))
{
struct item_event_hook_info *ie = data;
free (ie->label);
free (ie);
}
static void
connect_choice_item_selectable_hook (JNIEnv *env,
jobject peer_obj,
GtkItem *menuitem,
const char *label)
{ {
struct item_event_hook_info *ie; struct item_event_hook_info *ie;
jobject *peer_objGlobPtr; jobject *peer_objGlobPtr;
jobject *item_objGlobPtr;
ie = (struct item_event_hook_info *) ie = (struct item_event_hook_info *)
malloc (sizeof (struct item_event_hook_info)); malloc (sizeof (struct item_event_hook_info));
peer_objGlobPtr = NSA_GET_GLOBAL_REF (env, peer_obj); peer_objGlobPtr = NSA_GET_GLOBAL_REF (env, peer_obj);
g_assert (peer_objGlobPtr); g_assert (peer_objGlobPtr);
item_objGlobPtr = NSA_GET_GLOBAL_REF (env, item_obj);
g_assert (item_objGlobPtr);
ie->peer_obj = *peer_objGlobPtr; ie->peer_obj = *peer_objGlobPtr;
ie->item_obj = *item_objGlobPtr; ie->label = strdup (label);
g_signal_connect (G_OBJECT (item), "activate", g_signal_connect_data (G_OBJECT (menuitem), "activate",
GTK_SIGNAL_FUNC (item_activate), ie); GTK_SIGNAL_FUNC (item_activate), ie,
(GClosureNotify) item_removed, 0);
} }
...@@ -56,6 +56,7 @@ jmethodID postKeyEventID; ...@@ -56,6 +56,7 @@ jmethodID postKeyEventID;
jmethodID postFocusEventID; jmethodID postFocusEventID;
jmethodID postAdjustmentEventID; jmethodID postAdjustmentEventID;
jmethodID postItemEventID; jmethodID postItemEventID;
jmethodID choicePostItemEventID;
jmethodID postListItemEventID; jmethodID postListItemEventID;
jmethodID postTextEventID; jmethodID postTextEventID;
jmethodID postWindowEventID; jmethodID postWindowEventID;
...@@ -80,7 +81,7 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz) ...@@ -80,7 +81,7 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz)
char **argv; char **argv;
char *homedir, *rcpath = NULL; char *homedir, *rcpath = NULL;
/* jclass gtkgenericpeer; */ /* jclass gtkgenericpeer; */
jclass gtkcomponentpeer, gtkwindowpeer, gtkscrollbarpeer, gtklistpeer, jclass gtkcomponentpeer, gtkchoicepeer, gtkwindowpeer, gtkscrollbarpeer, gtklistpeer,
gtkmenuitempeer, gtktextcomponentpeer, window; gtkmenuitempeer, gtktextcomponentpeer, window;
NSA_INIT (env, clazz); NSA_INIT (env, clazz);
...@@ -139,6 +140,8 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz) ...@@ -139,6 +140,8 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz)
gtkcomponentpeer = (*env)->FindClass (env, gtkcomponentpeer = (*env)->FindClass (env,
"gnu/java/awt/peer/gtk/GtkComponentPeer"); "gnu/java/awt/peer/gtk/GtkComponentPeer");
gtkchoicepeer = (*env)->FindClass (env,
"gnu/java/awt/peer/gtk/GtkChoicePeer");
gtkwindowpeer = (*env)->FindClass (env, gtkwindowpeer = (*env)->FindClass (env,
"gnu/java/awt/peer/gtk/GtkWindowPeer"); "gnu/java/awt/peer/gtk/GtkWindowPeer");
gtkscrollbarpeer = (*env)->FindClass (env, gtkscrollbarpeer = (*env)->FindClass (env,
...@@ -181,6 +184,9 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz) ...@@ -181,6 +184,9 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz)
postItemEventID = (*env)->GetMethodID (env, gtkcomponentpeer, postItemEventID = (*env)->GetMethodID (env, gtkcomponentpeer,
"postItemEvent", "postItemEvent",
"(Ljava/lang/Object;I)V"); "(Ljava/lang/Object;I)V");
choicePostItemEventID = (*env)->GetMethodID (env, gtkchoicepeer,
"choicePostItemEvent",
"(Ljava/lang/String;I)V");
postListItemEventID = (*env)->GetMethodID (env, gtklistpeer, postListItemEventID = (*env)->GetMethodID (env, gtklistpeer,
"postItemEvent", "postItemEvent",
"(II)V"); "(II)V");
......
...@@ -395,6 +395,7 @@ extern jmethodID postExposeEventID; ...@@ -395,6 +395,7 @@ extern jmethodID postExposeEventID;
extern jmethodID postKeyEventID; extern jmethodID postKeyEventID;
extern jmethodID postFocusEventID; extern jmethodID postFocusEventID;
extern jmethodID postAdjustmentEventID; extern jmethodID postAdjustmentEventID;
extern jmethodID choicePostItemEventID;
extern jmethodID postItemEventID; extern jmethodID postItemEventID;
extern jmethodID postListItemEventID; extern jmethodID postListItemEventID;
extern jmethodID postTextEventID; extern jmethodID postTextEventID;
...@@ -424,7 +425,7 @@ jint keyevent_state_to_awt_mods (GdkEvent *event); ...@@ -424,7 +425,7 @@ jint keyevent_state_to_awt_mods (GdkEvent *event);
struct item_event_hook_info struct item_event_hook_info
{ {
jobject peer_obj; jobject peer_obj;
jobject item_obj; const char *label;
}; };
#endif /* __GTKPEER_H */ #endif /* __GTKPEER_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment