1 /* GtkChoicePeer.java -- Implements ChoicePeer with GTK
2 Copyright (C) 1998, 1999, 2005 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., 51 Franklin Street, Fifth Floor, 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. */
39 package gnu.java.awt.peer.gtk;
41 import java.awt.Choice;
42 import java.awt.AWTEvent;
43 import java.awt.event.ItemEvent;
44 import java.awt.peer.ChoicePeer;
46 public class GtkChoicePeer extends GtkComponentPeer
51 public GtkChoicePeer (Choice c)
55 int count = c.getItemCount ();
58 for (int i = 0; i < count; i++)
59 add( c.getItem(i), i );
61 selected = c.getSelectedIndex();
69 native void create ();
71 native int nativeGetSelected ();
73 native void connectSignals ();
75 native void selectNative (int position);
77 native void selectNativeUnlocked (int position);
79 public native void add (String item, int index);
81 native void nativeRemove(int index);
83 native void nativeRemoveAll();
85 public void select (int position)
87 if (Thread.currentThread() == GtkMainThread.mainThread)
88 selectNativeUnlocked (position);
90 selectNative (position);
93 public void remove( int index )
95 // Ensure the triggering of an event when removing item zero if zero is the
96 // selected item, even though the selected index doesn't change.
97 if( index == 0 && selected == 0 )
99 nativeRemove( index );
102 public void removeAll()
104 selected = -1; // we do not want to trigger a select event here.
108 public void addItem (String item, int position)
110 add (item, position);
114 * Callback from the native side on an item-select event,
115 * which posts an event. The event is only posted if it represents an actual
116 * change. Selected is set to the peer's state initially, so that the
117 * first call to select(int) from the constructor will not trigger an event.
120 protected void postChoiceItemEvent ( int index )
122 if( selected != index )
125 postItemEvent (((Choice) awtComponent).getItem( selected ),
131 * Catches the event and calls Choice.select() if the component state
134 public void handleEvent (AWTEvent event)
136 super.handleEvent( event );
137 if( event instanceof ItemEvent )
138 if( ((ItemEvent)event).getItemSelectable() == awtComponent &&
139 ((ItemEvent)event).getStateChange() == ItemEvent.SELECTED )
140 ((Choice)awtComponent).select( selected );