public abstract class Widget extends Object
Container
s.Modifier and Type | Class and Description |
---|---|
static interface |
Widget.ButtonPressEvent
Signal fired when the user clicks one of their mouse buttons.
|
static interface |
Widget.ButtonReleaseEvent
The signal emitted when the user releases a pressed mouse button.
|
static interface |
Widget.Destroy
Signal handler for when a Widget requests that all other code holding
references to it release those references.
|
static interface |
Widget.Draw
The signal emitted when a portion or all of the Widget has been exposed
and needs [re]drawing.
|
static interface |
Widget.EnterNotifyEvent
Signal emitted when the mouse enters the Widget.
|
static interface |
Widget.FocusInEvent
Signal emitted when focus enters this Widget.
|
static interface |
Widget.FocusOutEvent
Signal emitted when the keyboard focus leaves this Widget.
|
static interface |
Widget.Hide
The signal emitted when a Widget is hidden.
|
static interface |
Widget.KeyPressEvent
Handler interface for key press events.
|
static interface |
Widget.KeyReleaseEvent
Handler interface for key release events.
|
static interface |
Widget.LeaveNotifyEvent
Signal emitted when the mouse pointer leaves the Widget.
|
static interface |
Widget.MapEvent
The signal emitted when a Widget becomes visible on the screen.
|
static interface |
Widget.MotionNotifyEvent
Handler interface for mouse motion events.
|
static interface |
Widget.PopupMenu
The signal emitted when the user presses the
Menu
key. |
static interface |
Widget.QueryTooltip
Signal fired when requesting a tooltip.
|
static interface |
Widget.ScrollEvent
Signal emitted when the user turns the mouse wheel.
|
static interface |
Widget.SizeAllocate
Signal fired when a Widget is given its size allocation.
|
static interface |
Widget.UnmapEvent
The signal emitted when a Window becomes invisible.
|
static interface |
Widget.VisibilityNotifyEvent
A signal providing notification that the Widget has been obscured or
unobscured.
|
Modifier and Type | Method and Description |
---|---|
void |
activate()
Cause this Widget to be activated.
|
void |
addEvents(EventMask events)
Enable the given events.
|
void |
connect(Widget.ButtonPressEvent handler)
Hook up a
Widget.ButtonPressEvent handler. |
void |
connect(Widget.ButtonReleaseEvent handler)
Hook up a
Widget.ButtonReleaseEvent handler. |
void |
connect(Widget.Destroy handler)
Hook up a
Widget.Destroy handler. |
void |
connect(Widget.Draw handler)
Hook up a handler to receive
Widget.Draw signals on this
Widget. |
void |
connect(Widget.EnterNotifyEvent handler)
Hook up a handler to receive
Widget.EnterNotifyEvent
signals on this Widget. |
void |
connect(Widget.FocusInEvent handler)
Hook up a handler to receive
Widget.FocusInEvent signals. |
void |
connect(Widget.FocusOutEvent handler)
Hook up a handler to receive
Widget.FocusOutEvent events
on this Widget |
void |
connect(Widget.Hide handler)
Connect a
Widget.Hide handler. |
void |
connect(Widget.KeyPressEvent handler)
Hook up a handler to receive
Widget.KeyPressEvent signals
on this Widget. |
void |
connect(Widget.KeyReleaseEvent handler)
Hook up a handler to receive
Widget.KeyReleaseEvent
signals on this Widget |
void |
connect(Widget.LeaveNotifyEvent handler)
Hook up a handler to receive
Widget.LeaveNotifyEvent
signals. |
void |
connect(Widget.MapEvent handler)
Hook up a
Widget.MapEvent signal handler. |
void |
connect(Widget.MotionNotifyEvent handler)
Hook up a
Widget.MotionNotifyEvent handler. |
void |
connect(Widget.PopupMenu handler)
Hookup a
Widget.PopupMenu handler. |
void |
connect(Widget.QueryTooltip handler)
Connect a tooltip query to the widget.
|
void |
connect(Widget.ScrollEvent handler)
Hook up a
Widget.ScrollEvent handler. |
void |
connect(Widget.SizeAllocate handler)
Hook up a
Widget.SizeAllocate handler. |
void |
connect(Widget.UnmapEvent handler)
Connect a
Widget.UnmapEvent handler. |
void |
connect(Widget.VisibilityNotifyEvent handler)
Connect a
Widget.VisibilityNotifyEvent handler. |
void |
destroy()
Ask everything connected to this object to release its references.
|
int |
getAllocatedHeight()
Retreive the height that has been allocated to this Widget.
|
int |
getAllocatedWidth()
Retreive the width that has been allocated to this Widget, in pixels.
|
Allocation |
getAllocation()
Get the details of the rectangle that represents the size that the
windowing system down to GTK on down to the parent containers of this
Widget have allocated to it.
|
boolean |
getCanDefault()
Get the value of the can-default property.
|
boolean |
getCanFocus()
Get the value of the can-focus property.
|
boolean |
getHasFocus()
Does this Widget currently have the keyboard focus?
|
String |
getName()
Get the name of the Widget, if it has one.
|
Container |
getParent()
Return the Container that this Widget is packed into.
|
int |
getPreferredHeightForWidthMinimum(int width) |
int |
getPreferredHeightForWidthNatural(int width) |
int |
getPreferredHeightMinimum() |
int |
getPreferredHeightNatural() |
int |
getPreferredWidthForHeightMinimum(int height) |
int |
getPreferredWidthForHeightNatural(int height) |
int |
getPreferredWidthMinimum() |
int |
getPreferredWidthNatural() |
SizeRequestMode |
getRequestMode()
Does the Widget prefer that it be given height for a given width, or
vice versa?
|
Requisition |
getRequisition()
Get the size that will be (is being) requested by this Widget.
|
boolean |
getSensitive()
Is this Widget set to be sensitive?
|
StyleContext |
getStyleContext()
Get the
StyleContext of a Widget. |
Widget |
getToplevel()
Get the Widget at the top of the container hierarchy to which this
Widget belongs.
|
Window |
getWindow()
Get the underlying resource being used to power display of, and
interaction with, this Widget.
|
void |
grabAdd()
Make this the current grabbed Widget.
|
void |
grabDefault()
Make this Widget attempt to become the default.
|
void |
grabFocus()
Make this Widget have the keyboard focus for the Window it is within.
|
void |
grabRemove()
Removes the "grab" status from this Widget if it is currently grabbed,
otherwise this does nothing.
|
void |
hide()
Hide the Widget, making it invisible to the user.
|
boolean |
isSensitive()
Will this Widget be shown as sensitive or insensitive?
|
void |
overrideBackground(StateFlags state,
RGBA color)
Adjust the background colour being used when drawing this Widget.
|
void |
overrideColor(StateFlags state,
RGBA color)
Set the colour used for text rendered by this Widget.
|
void |
overrideFont(FontDescription desc)
Set the font used for text rendered by this Widget.
|
void |
queueDraw()
Request that the entire Widget be redrawn.
|
void |
queueDrawArea(int x,
int y,
int width,
int height)
Request that an area of the Widget to be redrawn.
|
void |
realize()
Cause the resources underlying the Widget to be assigned.
|
void |
setAlignHorizontal(Align align)
Set the horizontal positioning of a Widget in its parent Container.
|
void |
setAlignVertical(Align align)
Set the vertical positioning of a Widget in its parent Container.
|
void |
setCanDefault(boolean setting)
Whether this Widget is willing to be the default Widget.
|
void |
setCanFocus(boolean setting)
Whether the Widget can (is willing to) accept the keyboard input focus.
|
void |
setEvents(EventMask events)
Reset the list of events for this Widget.
|
void |
setExpandHorizontal(boolean setting)
Will this Widget be given any additional space that it's parent
Container has?
|
void |
setExpandVertical(boolean setting)
This is the vexpand property.
|
void |
setName(String name)
Every Widget has a name as an optional identifier.
|
void |
setSensitive(boolean sensitive)
Set whether the Widget is greyed out or not.
|
void |
setSizeRequest(int width,
int height)
Set the minimum size that will be requested by this Widget of its
parent Container.
|
void |
setTooltipMarkup(String markup)
Tooltips are notes that will be displayed if a user hovers the mouse
pointer over a Widget.
|
void |
setTooltipText(String text)
Tooltips are notes that will be displayed if a user hovers the mouse
pointer over a Widget.
|
void |
show()
Cause this Widget to be mapped to the screen.
|
void |
showAll()
Cause this Widget, and any Widgets it contains, to be mapped to the
screen.
|
public void activate()
Return
key while the Widget is in focus.
Calling this method on a ToggleButton
will toggle its state,
for example. Whatever signals would normally result from the user
activating this Widget by hand will be emitted.
Note that this method only works if this Widget is activatable; not all are, making this more an optional characteristic that some, but not all Widgets share.
The Widget must already be showing on the screen for this method to
work (ie, you must invoke show()
before you can
activate()
it). Parent Containers must also have been
shown.
UnsupportedOperationException
- if the Widget is not activatable.public void addEvents(EventMask events)
Take care that events are actually received by the underlying GDK
Window resource being used. Such resource is in many cases shared by
several Widgets, so enabling or disabling an event on one of these can
affect all Widgets. If that is a problem for you, EventBox
Widget can be used to ensure only it is affected by this
method.
public void connect(Widget.ButtonPressEvent handler)
Widget.ButtonPressEvent
handler.public void connect(Widget.ButtonReleaseEvent handler)
Widget.ButtonReleaseEvent
handler.public void connect(Widget.Destroy handler)
Widget.Destroy
handler.public void connect(Widget.Draw handler)
Widget.Draw
signals on this
Widget.public void connect(Widget.EnterNotifyEvent handler)
Widget.EnterNotifyEvent
signals on this Widget.public void connect(Widget.FocusInEvent handler)
Widget.FocusInEvent
signals.public void connect(Widget.FocusOutEvent handler)
Widget.FocusOutEvent
events
on this Widgetpublic void connect(Widget.Hide handler)
Widget.Hide
handler.public void connect(Widget.KeyPressEvent handler)
Widget.KeyPressEvent
signals
on this Widget. For general typing this is the one you want, but for
critical events (like pressing Enter
to activate a
Button that is going to delete things, you might want to postpone
action until Widget.KeyReleaseEvent
.public void connect(Widget.KeyReleaseEvent handler)
Widget.KeyReleaseEvent
signals on this Widgetpublic void connect(Widget.LeaveNotifyEvent handler)
Widget.LeaveNotifyEvent
signals.public void connect(Widget.MapEvent handler)
Widget.MapEvent
signal handler.public void connect(Widget.MotionNotifyEvent handler)
Widget.MotionNotifyEvent
handler.public void connect(Widget.PopupMenu handler)
Widget.PopupMenu
handler.public void connect(Widget.QueryTooltip handler)
public void connect(Widget.ScrollEvent handler)
Widget.ScrollEvent
handler.public void connect(Widget.SizeAllocate handler)
Widget.SizeAllocate
handler.public void connect(Widget.UnmapEvent handler)
Widget.UnmapEvent
handler.public void connect(Widget.VisibilityNotifyEvent handler)
Widget.VisibilityNotifyEvent
handler.public void destroy()
remove()
if you don't need the Widget any more.
We didn't expose this for a long time in java-gnome; after all,
memory management of both Java references and GObject Ref counts is
handled automatically by the diabolical cunning of this most excellent
library. It turns out, however, that this does not free()
the GtkWidget's memory; it really only does what it says: ask other
GtkWidget to drop whatever Refs they may be holding to this object.
Thus if this GtkWidget is in a GtkContainer and you call
destroy()
the GtkContainer will drop its Refs to this
GtkWidget thereby breaking its parent-child relationship.
Note that Java's references remain, so the object will not,
in fact, be eligable for finalizing until you drop all your references;
ie, the Java side Proxy Object goes out of scope. Nevertheless calling
this will speed up release of resources associated with a Widget, so
it's a good idea. Once you've done so, finalize()
will be traversed a short time later and the GObject will be
released.
public int getAllocatedHeight()
getAllocatedWidth()
.public int getAllocatedWidth()
Widget.Draw
handlers when they
need to know their canvas size.public Allocation getAllocation()
show()
n. In some circumstances the main loop may need to
have iterated).public boolean getCanDefault()
setCanDefault()
and
grabDefault()
for details.public boolean getCanFocus()
setCanFocus()
and grabFocus()
for details.public boolean getHasFocus()
This can be quite useful when one Widget takes action in a signal
handler which changes the state of another Widget. Take for example two
related Entry Widgets. The second Entry's Entry.Changed
signal will fire when the first Entry's Entry.Changed
handler calls second.setText()
; if it changes the first
Entry then you have an infinite loop on your hands. By checking for
has-focus at the beginning of both handlers, then only the
Widget that the user changed will carry out it's logic; the other will
realize it doesn't have focus and can quickly pass.
public String getName()
Widgets can be identified by a name. You will most likely use this
method a lot if you are using Glade. Every widget there is identified
by a name. It is suggested to use sensible names; see
Glade
for a discussion of the
implications of different nomenaclatures if that's what you are using,
but in any case once you've retrieved the Widget with
getWidget()
you won't
really need to know its name anymore.
public Container getParent()
null
.public int getPreferredHeightForWidthMinimum(int width)
public int getPreferredHeightForWidthNatural(int width)
public int getPreferredHeightMinimum()
public int getPreferredHeightNatural()
public int getPreferredWidthForHeightMinimum(int height)
public int getPreferredWidthForHeightNatural(int height)
public int getPreferredWidthMinimum()
public int getPreferredWidthNatural()
public SizeRequestMode getRequestMode()
Part of GTK 3's height-for-width geometry management system.
public Requisition getRequisition()
In addition to getting the Requisition object for this Widget, this method will also ask the Widget to actually calculate its requirements. This can be a relatively expensive operation as font metrics need to be worked out relative to the Display's physical characteristics (this implies that the request calculation won't have any effect if the Widget is not yet been FIXME to a Screen).
public boolean getSensitive()
The default is true
of course.
The utility of this is somewhat limited, since it only returns the boolean value of this Widget's sensitive property, whereas whether a Widget is displayed sensitive (normal) or insensitive (grayed out) depends on both this property and the settings in the parent Widgets.
public StyleContext getStyleContext()
StyleContext
of a Widget. You will be able to change
the values of the style after getting this object.public Widget getToplevel()
It's is somewhat common to want to find the ultimately enclosing top level Window that this Widget belongs to. Assuming that the Widget has actually been packed into a Container hierarchy that tops out at a Window (or Dialog, etc) then that is what you'll get. So yes, you can do:
w = (Window) obj.getToplevel();as you'll get a ClassCastException or NullPointerException if you're wrong about
obj
being in a Window yet.
To manually walk up the hierarchy one level at a time, use
getParent()
.
this
if the Widget isn't (yet) in a
hierarchy.public Window getWindow()
If you're looking for the top Window in a Widget hierarchy, see
getToplevel()
. This method is to get a reference
to the lower level GDK mechanisms used by this Widget, not to navigate
up a hierarchy of Widgets to find the top-level Window they are packed
into.
If what you need are the event handling facilities that go with Widgets
that have their own native resources, consider creating an
EventBox
and putting this Widget into it.
While it is best to wait until the Widget is mapped to screen and user
visible before manipulating underlying properties, there are rare cases
when you need the [org.gnome.gdk] Window to be not-null
before then; if so, you can call realize()
.
If you call this in a class where you're building Windows, then you
will probably end up having to use the fully qualified name
org.gnome.gdk.Window
when declaring variables. That's
an unavoidable consequence of the class mapping algorithm we used:
GdkWindow
is the name of the underlying type being
returned, and so Window it is.
org.gnome.gdk.Window
associated with this
Widget, or null
if this Widget is (as yet) without
one.public void grabAdd()
Note that being the current grabbed widget means mouse and keyboard events will not be delivered to other widgets, so use this with care.
public void grabDefault()
Enter
.
This will only work if the Widget is activatable (see
activate()
) and if the Widget has its
can-default property enabled with
setCanDefault()
. The Widget also needs
to have already been packed into a hierarchy which tops out at a
Window.
If you're reading this you may in fact be looking for
grabFocus()
which affects where keyboard input is
going, as opposed to this method which affects activation.
public void grabFocus()
Obviously, if this is going to actually have affect, this Widget needs to be in a Window. Furthermore, the Widget needs to be able to take input focus, that is, it must have the can-focus property set (which is inherent to the particular Widget subclass, not something you can change).
public void grabRemove()
grabAdd()
.public void hide()
You can call show()
to make a hidden Widget visible [again].
public boolean isSensitive()
You don't need to use this ordinarily (it's GTK that needs to know!) but if you're curious, well, here you go.
public void overrideBackground(StateFlags state, RGBA color)
public void overrideColor(StateFlags state, RGBA color)
overrideBackground()
.public void overrideFont(FontDescription desc)
public void queueDraw()
queueDrawArea()
for full
details, but you only ever need this if doing your own drawing.public void queueDrawArea(int x, int y, int width, int height)
Widget.Draw
signal being sent to the Widget
asking it to [re]render the area given by the passed in co-ordinates.
You only ever need this if doing your own custom Widget drawing, and then only sometimes.
The redraw will not happen immediately, but rather during the next
iteration of the main loop. Also, note that several such requests may
be combined into a single Widget.Draw
signal by the X
server and GDK.
Be aware that this will invalidate both this Widget and all its
children. If you wish to limit your redrawing, you will be better off
with one of the
invalidate()
calls in the underlying native resource.
public void realize()
In general you don't want to be calling this. This is largely an
internal method; while you can trigger realization manually you rarely
need to. You do need to show the Widget with show()
(or better yet showAll()
on one of its
parents) once you've built it.
Almost anything that you would do that would invole you needing an
[org.gnome.gdk] Window is best done in a Widget.Draw
handler, at which point the Widget is already realized, mapped, and
showing.
public void setAlignHorizontal(Align align)
This is the halign property.
This is part of how the GTK 2.x Alignment Widget has been replaced.
public void setAlignVertical(Align align)
This is the valign property.
public void setCanDefault(boolean setting)
Enter
isn't that useful, setting this
sometimes has an effect on whether or not the theme will draw a
dotted line (or other markup) around a Button to indicate that the
Widget is the current default Widget.
Normally, this setting is false
, though
activatable Widgets will already have it true
.
Disabling can-default will mean that a
grabDefault()
on this Widget will be ignored.
public void setCanFocus(boolean setting)
true
, and others not. The
can-focus property is mostly internal, but it is
occasionally useful to use a widget that can focus in an environment
where you don't want it to take input. Calling
setCanFocus(false)
will do the trick.public void setEvents(EventMask events)
addEvents()
. If you use this, you'll need
to or()
together all the events that this Widget needs to
function.public void setExpandHorizontal(boolean setting)
false
which means that as a
child this Widget will be allocated its natural size reqeust, but not
more.
This is the hexpand property.
public void setExpandVertical(boolean setting)
This is the vexpand property.
public void setName(String name)
If using Glade then these names are used to retrieve widgets from the XML file. You can use this method you set the name of a Widget after it is retrieved.
If you are using Glade then you will most likely never need this method, as you will have them defined in Glade. And if you're not using Glade, then you probably won't need this method either, since you can refer to Widgets you create by reference.
public void setSensitive(boolean sensitive)
This is frequently used on Buttons and MenuItems to show that a given course of action is not currently available, but would be if the user did something different to the application. This can be a terrific hint to assist with discovery, but can be overdone; having everything insensitive and leaving the user no idea what to do next doesn't really help much.
Beware that setting the sensitive property cascades down through the
hierarchy of any children packed into this Widget in the same way that
showAll()
is recursive. While this is usually great
for desensitizing an entire Window, the catch is that if you
resensitize that same Window every Widget within it will
return to being sensitive; there's no "memory" of which might have been
insensitive before. Thus if you heavily use a mix of sensitive and
insensitive states in a Window and desensitize the whole thing while
carrying out input validation in a worker thread, you will be left with
the task of manually restoring the sensitive state of the various sub
components of your UI should you need to return back to that Window to
have the user re-enter something.
sensitive
- true
to have the Widget respond as normal, and
false
to de-activate the Widget and have it
"grey out".public void setSizeRequest(int width, int height)
A major feature of GTK is its adaptability in the face of different languages, fonts, and theme engines, with all of these factors impacting the number of pixels that will be necessary for drawing. The box packing model has each Widget request the size it calculates it needs at runtime of its parent. These requests flow up the Containers the Widget is packed into, with each Container collating the requests from its children. When it reaches the toplevel, GTK negotiates with the X server, and the result is the size allocation for the Window as a whole. The Window proceeds to inform each Container packed into it how much space it has been allocated, leaving it to the Containers to in turn allocate space to each of its children.
The whole point of all this is that in general you are not supposed to interfere with this process. It is virtually impossible to calculate the correct size for a Widget on a given user's desktop ahead of time, so don't try. This method is here for the unusual cases where you need to force a Widget to be a size other than what the default request-allocation process results in.
See Requisition
and Allocation
for further discussion
of how the size-request/size-allocation process works and how you can
get insight into it.
A value of -1
for either width
or
height
will cause that dimension to revert to the
"natural" size, that is, the size that would have been requested if
you'd left things alone.
Passing 0,0
is a special case, meaning "as small as
possible". This will have varying results and may not actually have
much effect.
Incidentally, use setDefaultSize()
for top level Windows, as that method still allows a
user to make the Window smaller than the specified default.
public void setTooltipMarkup(String markup)
markup
- The string with Pango markup you wish to be displayed when
if the tooltip is popped up.public void setTooltipText(String text)
text
- The string of plain text (i.e. without any Pango markup) you
wish to be displayed when if the tooltip is popped up.public void show()
There are a bunch of quirks you need to be aware of:
Window
), it is immediately realized and mapped, and any Widgets within it
that are shown are then realized and mapped.
If you want to show all the widgets in a container, it's actually much
easier to just call showAll()
on the container, rather than
calling show manually one each individual Widget you've added to it.
public void showAll()
Window
after you've
finished all the work necessary to set it up.
Quite frequently you also want to cause a Window to appear on the
screen as well (ie, not be buried under a whole bunch of other
applications' Windows), so calling Window's present()
is usually next.
Don't be surprised if this takes a few hundred milliseconds. Realizing and mapping all the zillion elements that ultimately make up a Window is one of the most resource intensive operations that GTK, GDK, Pango, your X server, and your kernel have to churn through. Sometimes, you just gotta wait.