class Gtk::Widget

Overview

The base class for all widgets.

Gtk::Widget is the base class all widgets in GTK derive from. It manages the widget lifecycle, layout, states and style.

Height-for-width Geometry Management

GTK uses a height-for-width (and width-for-height) geometry management system. Height-for-width means that a widget can change how much vertical space it needs, depending on the amount of horizontal space that it is given (and similar for width-for-height). The most common example is a label that reflows to fill up the available width, wraps to fewer lines, and therefore needs less height.

Height-for-width geometry management is implemented in GTK by way of two virtual methods:

There are some important things to keep in mind when implementing height-for-width and when using it in widget implementations.

If you implement a direct Gtk::Widget subclass that supports height-for-width or width-for-height geometry management for itself or its child widgets, the Gtk::Widget#request_mode virtual function must be implemented as well and return the widget's preferred request mode. The default implementation of this virtual function returns %GTK_SIZE_REQUEST_CONSTANT_SIZE, which means that the widget will only ever get -1 passed as the for_size value to its Gtk::Widget#measure implementation.

The geometry management system will query a widget hierarchy in only one orientation at a time. When widgets are initially queried for their minimum sizes it is generally done in two initial passes in the Gtk::SizeRequestMode chosen by the toplevel.

For example, when queried in the normal %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH mode:

First, the default minimum and natural width for each widget in the interface will be computed using #gtk_widget_measure with an orientation of %GTK_ORIENTATION_HORIZONTAL and a for_size of -1. Because the preferred widths for each widget depend on the preferred widths of their children, this information propagates up the hierarchy, and finally a minimum and natural width is determined for the entire toplevel. Next, the toplevel will use the minimum width to query for the minimum height contextual to that width using #gtk_widget_measure with an orientation of %GTK_ORIENTATION_VERTICAL and a for_size of the just computed width. This will also be a highly recursive operation. The minimum height for the minimum width is normally used to set the minimum size constraint on the toplevel.

After the toplevel window has initially requested its size in both dimensions it can go on to allocate itself a reasonable size (or a size previously specified with Gtk::Window#default_size=). During the recursive allocation process it’s important to note that request cycles will be recursively executed while widgets allocate their children. Each widget, once allocated a size, will go on to first share the space in one orientation among its children and then request each child's height for its target allocated width or its width for allocated height, depending. In this way a Gtk::Widget will typically be requested its size a number of times before actually being allocated a size. The size a widget is finally allocated can of course differ from the size it has requested. For this reason, Gtk::Widget caches a small number of results to avoid re-querying for the same sizes in one allocation cycle.

If a widget does move content around to intelligently use up the allocated size then it must support the request in both Gtk::SizeRequestModes even if the widget in question only trades sizes in a single orientation.

For instance, a Gtk::Label that does height-for-width word wrapping will not expect to have Gtk::Widget#measure with an orientation of %GTK_ORIENTATION_VERTICAL called because that call is specific to a width-for-height request. In this case the label must return the height required for its own minimum possible width. By following this rule any widget that handles height-for-width or width-for-height requests will always be allocated at least enough space to fit its own content.

Here are some examples of how a %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH widget generally deals with width-for-height requests:

WARNING ⚠️ The following code is in c ⚠️

static void
foo_widget_measure (Gtk::Widget      *widget,
                    Gtk::Orientation  orientation,
                    int             for_size,
                    int            *minimum_size,
                    int            *natural_size,
                    int            *minimum_baseline,
                    int            *natural_baseline)
{
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      // Calculate minimum and natural width
    }
  else // VERTICAL
    {
      if (i_am_in_height_for_width_mode)
        {
          int min_width, dummy;

          // First, get the minimum width of our widget
          GTK_WIDGET_GET_CLASS (widget)->measure (widget, GTK_ORIENTATION_HORIZONTAL, -1,
                                                  &min_width, &dummy, &dummy, &dummy);

          // Now use the minimum width to retrieve the minimum and natural height to display
          // that width.
          GTK_WIDGET_GET_CLASS (widget)->measure (widget, GTK_ORIENTATION_VERTICAL, min_width,
                                                  minimum_size, natural_size, &dummy, &dummy);
        }
      else
        {
          // ... some widgets do both.
        }
    }
}

Often a widget needs to get its own request during size request or allocation. For example, when computing height it may need to also compute width. Or when deciding how to use an allocation, the widget may need to know its natural size. In these cases, the widget should be careful to call its virtual methods directly, like in the code example above.

It will not work to use the wrapper function Gtk::Widget#measure inside your own Gtk::Widget#size_allocate implementation. These return a request adjusted by Gtk::SizeGroup, the widget's align and expand flags, as well as its CSS style.

If a widget used the wrappers inside its virtual method implementations, then the adjustments (such as widget margins) would be applied twice. GTK therefore does not allow this and will warn if you try to do it.

Of course if you are getting the size request for another widget, such as a child widget, you must use #gtk_widget_measure; otherwise, you would not properly consider widget margins, Gtk::SizeGroup, and so forth.

GTK also supports baseline vertical alignment of widgets. This means that widgets are positioned such that the typographical baseline of widgets in the same row are aligned. This happens if a widget supports baselines, has a vertical alignment of %GTK_ALIGN_BASELINE, and is inside a widget that supports baselines and has a natural “row” that it aligns to the baseline, or a baseline assigned to it by the grandparent.

Baseline alignment support for a widget is also done by the Gtk::Widget#measure virtual function. It allows you to report both a minimum and natural size.

If a widget ends up baseline aligned it will be allocated all the space in the parent as if it was %GTK_ALIGN_FILL, but the selected baseline can be found via #gtk_widget_get_allocated_baseline. If the baseline has a value other than -1 you need to align the widget such that the baseline appears at the position.

Gtk::Widget as Gtk::Buildable

The Gtk::Widget implementation of the Gtk::Buildable interface supports various custom elements to specify additional aspects of widgets that are not directly expressed as properties.

If the widget uses a Gtk::LayoutManager, Gtk::Widget supports a custom <layout> element, used to define layout properties:

WARNING ⚠️ The following code is in xml ⚠️

<object class="Gtk::Grid" id="my_grid">
  <child>
    <object class="Gtk::Label" id="label1">
      <property name="label">Description</property>
      <layout>
        <property name="column">0</property>
        <property name="row">0</property>
        <property name="row-span">1</property>
        <property name="column-span">1</property>
      </layout>
    </object>
  </child>
  <child>
    <object class="Gtk::Entry" id="description_entry">
      <layout>
        <property name="column">1</property>
        <property name="row">0</property>
        <property name="row-span">1</property>
        <property name="column-span">1</property>
      </layout>
    </object>
  </child>
</object>

Gtk::Widget allows style information such as style classes to be associated with widgets, using the custom <style> element:

WARNING ⚠️ The following code is in xml ⚠️

<object class="Gtk::Button" id="button1">
  <style>
    <class name="my-special-button-class"/>
    <class name="dark-button"/>
  </style>
</object>

Gtk::Widget allows defining accessibility information, such as properties, relations, and states, using the custom <accessibility> element:

WARNING ⚠️ The following code is in xml ⚠️

<object class="Gtk::Button" id="button1">
  <accessibility>
    <property name="label">Download</property>
    <relation name="labelled-by">label1</relation>
  </accessibility>
</object>

Building composite widgets from template XML

Gtk::Widgetexposes some facilities to automate the procedure of creating composite widgets using "templates".

To create composite widgets with Gtk::Builder XML, one must associate the interface description with the widget class at class initialization time using Gtk::WidgetClass#template=.

The interface description semantics expected in composite template descriptions is slightly different from regular Gtk::Builder XML.

Unlike regular interface descriptions, Gtk::WidgetClass#template= will expect a <template> tag as a direct child of the toplevel <interface> tag. The <template> tag must specify the “class” attribute which must be the type name of the widget. Optionally, the “parent” attribute may be specified to specify the direct parent type of the widget type, this is ignored by Gtk::Builder but required for UI design tools like Glade to introspect what kind of properties and internal children exist for a given type when the actual type does not exist.

The XML which is contained inside the <template> tag behaves as if it were added to the <object> tag defining the widget itself. You may set properties on a widget by inserting <property> tags into the <template> tag, and also add <child> tags to add children and extend a widget in the normal way you would with <object> tags.

Additionally, <object> tags can also be added before and after the initial <template> tag in the normal way, allowing one to define auxiliary objects which might be referenced by other widgets declared as children of the <template> tag.

An example of a template definition:

WARNING ⚠️ The following code is in xml ⚠️

<interface>
  <template class="FooWidget" parent="Gtk::Box">
    <property name="orientation">horizontal</property>
    <property name="spacing">4</property>
    <child>
      <object class="Gtk::Button" id="hello_button">
        <property name="label">Hello World</property>
        <signal name="clicked" handler="hello_button_clicked" object="FooWidget" swapped="yes"/>
      </object>
    </child>
    <child>
      <object class="Gtk::Button" id="goodbye_button">
        <property name="label">Goodbye World</property>
      </object>
    </child>
  </template>
</interface>

Typically, you'll place the template fragment into a file that is bundled with your project, using GResource. In order to load the template, you need to call Gtk::WidgetClass#template_from_resource= from the class initialization of your Gtk::Widget type:

WARNING ⚠️ The following code is in c ⚠️

static void
foo_widget_class_init (FooWidgetClass *klass)
{
  // ...

  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass),
                                               "/com/example/ui/foowidget.ui");
}

You will also need to call Gtk::Widget#init_template from the instance initialization function:

WARNING ⚠️ The following code is in c ⚠️

static void
foo_widget_init (FooWidget *self)
{
  // ...
  gtk_widget_init_template (GTK_WIDGET (self));
}

You can access widgets defined in the template using the #gtk_widget_get_template_child function, but you will typically declare a pointer in the instance private data structure of your type using the same name as the widget in the template definition, and call Gtk::WidgetClass#bind_template_child_full (or one of its wrapper macros Gtk::widget_class_bind_template_child and Gtk::widget_class_bind_template_child_private) with that name, e.g.

WARNING ⚠️ The following code is in c ⚠️

typedef struct {
  Gtk::Widget *hello_button;
  Gtk::Widget *goodbye_button;
} FooWidgetPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (FooWidget, foo_widget, GTK_TYPE_BOX)

static void
foo_widget_class_init (FooWidgetClass *klass)
{
  // ...
  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass),
                                               "/com/example/ui/foowidget.ui");
  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass),
                                                FooWidget, hello_button);
  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass),
                                                FooWidget, goodbye_button);
}

static void
foo_widget_init (FooWidget *widget)
{

}

You can also use Gtk::WidgetClass#bind_template_callback_full (or is wrapper macro Gtk::widget_class_bind_template_callback) to connect a signal callback defined in the template with a function visible in the scope of the class, e.g.

WARNING ⚠️ The following code is in c ⚠️

// the signal handler has the instance and user data swapped
// because of the swapped="yes" attribute in the template XML
static void
hello_button_clicked (FooWidget *self,
                      Gtk::Button *button)
{
  g_print ("Hello, world!\n");
}

static void
foo_widget_class_init (FooWidgetClass *klass)
{
  // ...
  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass),
                                               "/com/example/ui/foowidget.ui");
  gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (klass), hello_button_clicked);
}

Included Modules

Direct Known Subclasses

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/widget.cr
bindings/gtk/widget.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Gtk::ConstraintTarget

to_unsafe to_unsafe

Constructor methods inherited from module Gtk::ConstraintTarget

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::ConstraintTarget

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from module Gtk::Buildable

buildable_id : String | Nil buildable_id, to_unsafe to_unsafe

Constructor methods inherited from module Gtk::Buildable

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::Buildable

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from module Gtk::Accessible

accessible_role : Gtk::AccessibleRole accessible_role, accessible_role=(value : Gtk::AccessibleRole) : Gtk::AccessibleRole accessible_role=, reset_property(property : Gtk::AccessibleProperty) : Nil reset_property, reset_relation(relation : Gtk::AccessibleRelation) : Nil reset_relation, reset_state(state : Gtk::AccessibleState) : Nil reset_state, to_unsafe to_unsafe, update_property(properties : Enumerable(Gtk::AccessibleProperty), values : Enumerable(_)) : Nil update_property, update_relation(relations : Enumerable(Gtk::AccessibleRelation), values : Enumerable(_)) : Nil update_relation, update_state(states : Enumerable(Gtk::AccessibleState), values : Enumerable(_)) : Nil update_state

Constructor methods inherited from module Gtk::Accessible

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::Accessible

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from class GObject::InitiallyUnowned

==(other : self) ==, hash(hasher) hash

Constructor methods inherited from class GObject::InitiallyUnowned

new new

Class methods inherited from class GObject::InitiallyUnowned

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new #

Initialize a new Widget.


[View source]
def self.new(*, accessible_role : Gtk::AccessibleRole | Nil = nil, can_focus : Bool | Nil = nil, can_target : Bool | Nil = nil, css_classes : Enumerable(String) | Nil = nil, css_name : String | Nil = nil, cursor : Gdk::Cursor | Nil = nil, focus_on_click : Bool | Nil = nil, focusable : Bool | Nil = nil, halign : Gtk::Align | Nil = nil, has_default : Bool | Nil = nil, has_focus : Bool | Nil = nil, has_tooltip : Bool | Nil = nil, height_request : Int32 | Nil = nil, hexpand : Bool | Nil = nil, hexpand_set : Bool | Nil = nil, layout_manager : Gtk::LayoutManager | Nil = nil, margin_bottom : Int32 | Nil = nil, margin_end : Int32 | Nil = nil, margin_start : Int32 | Nil = nil, margin_top : Int32 | Nil = nil, name : String | Nil = nil, opacity : Float64 | Nil = nil, overflow : Gtk::Overflow | Nil = nil, parent : Gtk::Widget | Nil = nil, receives_default : Bool | Nil = nil, root : Gtk::Root | Nil = nil, scale_factor : Int32 | Nil = nil, sensitive : Bool | Nil = nil, tooltip_markup : String | Nil = nil, tooltip_text : String | Nil = nil, valign : Gtk::Align | Nil = nil, vexpand : Bool | Nil = nil, vexpand_set : Bool | Nil = nil, visible : Bool | Nil = nil, width_request : Int32 | Nil = nil) #

[View source]

Class Method Detail

def self.default_direction : Gtk::TextDirection #

Obtains the current default reading direction.

See Gtk::Widget#default_direction=.


[View source]
def self.default_direction=(dir : Gtk::TextDirection) : Nil #

Sets the default reading direction for widgets.

See Gtk::Widget#direction=.


[View source]
def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def action_set_enabled(action_name : String, enabled : Bool) : Nil #

Enable or disable an action installed with gtk_widget_class_install_action().


[View source]
def activate : Bool #

For widgets that can be “activated” (buttons, menu items, etc.), this function activates them.

The activation will emit the signal set using Gtk::WidgetClass#activate_signal= during class initialization.

Activation is what happens when you press Enter on a widget during key navigation.

If you wish to handle the activation keybinding yourself, it is recommended to use Gtk::WidgetClass#add_shortcut with an action created with Gtk::SignalAction.new.

If widget isn't activatable, the function returns false.


[View source]
def activate_action(name : String, args : _ | Nil) : Bool #

Looks up the action in the action groups associated with widget and its ancestors, and activates it.

This is a wrapper around Gtk::Widget#activate_action_variant that constructs the args variant according to format_string.


[View source]
def activate_default : Nil #

Activates the default.activate action from widget.


[View source]
def add_controller(controller : Gtk::EventController) : Nil #

Adds controller to widget so that it will receive events.

You will usually want to call this function right after creating any kind of Gtk::EventController.


[View source]
def add_css_class(css_class : String) : Nil #

Adds a style class to widget.

After calling this function, the widgets style will match for css_class, according to CSS matching rules.

Use Gtk::Widget#remove_css_class to remove the style again.


[View source]
def add_mnemonic_label(label : Gtk::Widget) : Nil #

Adds a widget to the list of mnemonic labels for this widget.

See Gtk::Widget#list_mnemonic_labels. Note the list of mnemonic labels for the widget is cleared when the widget is destroyed, so the caller must make sure to update its internal state at this point as well.


[View source]
def add_tick_callback(callback : Gtk::TickCallback) : UInt32 #

Queues an animation frame update and adds a callback to be called before each frame.

Until the tick callback is removed, it will be called frequently (usually at the frame rate of the output device or as quickly as the application can be repainted, whichever is slower). For this reason, is most suitable for handling graphics that change every frame or every few frames. The tick callback does not automatically imply a relayout or repaint. If you want a repaint or relayout, and aren’t changing widget properties that would trigger that (for example, changing the text of a Gtk::Label), then you will have to call Gtk::Widget#queue_resize or Gtk::Widget#queue_draw yourself.

Gdk::FrameClock#frame_time should generally be used for timing continuous animations and Gdk::FrameTimings#predicted_presentation_time if you are trying to display isolated frames at particular times.

This is a more convenient alternative to connecting directly to the Gdk::FrameClock::#update signal of Gdk::FrameClock, since you don't have to worry about when a Gdk::FrameClock is assigned to a widget.


[View source]
def allocate(width : Int32, height : Int32, baseline : Int32, transform : Gsk::Transform | Nil) : Nil #

This function is only used by Gtk::Widget subclasses, to assign a size, position and (optionally) baseline to their child widgets.

In this function, the allocation and baseline may be adjusted. The given allocation will be forced to be bigger than the widget's minimum size, as well as at least 0×0 in size.

For a version that does not take a transform, see Gtk::Widget#size_allocate.


[View source]
def allocated_baseline : Int32 #

Returns the baseline that has currently been allocated to widget.

This function is intended to be used when implementing handlers for the Gtk::WidgetClass.snapshot() function, and when allocating child widgets in Gtk::WidgetClass.size_allocate().


[View source]
def allocated_height : Int32 #

Returns the height that has currently been allocated to widget.


[View source]
def allocated_width : Int32 #

Returns the width that has currently been allocated to widget.


[View source]
def allocation : Gdk::Rectangle #

Retrieves the widget’s allocation.

Note, when implementing a layout container: a widget’s allocation will be its “adjusted” allocation, that is, the widget’s parent typically calls Gtk::Widget#size_allocate with an allocation, and that allocation is then adjusted (to handle margin and alignment for example) before assignment to the widget. Gtk::Widget#allocation returns the adjusted allocation that was actually assigned to the widget. The adjusted allocation is guaranteed to be completely contained within the Gtk::Widget#size_allocate allocation, however.

So a layout container is guaranteed that its children stay inside the assigned bounds, but not that they have exactly the bounds the container assigned.


[View source]
def ancestor(widget_type : UInt64) : Gtk::Widget | Nil #

Gets the first ancestor of widget with type widget_type.

For example, gtk_widget_get_ancestor (widget, GTK_TYPE_BOX) gets the first Gtk::Box that’s an ancestor of widget. No reference will be added to the returned widget; it should not be unreferenced.

Note that unlike Gtk::Widget#is_ancestor?, this function considers widget to be an ancestor of itself.


[View source]
def can_focus : Bool #

Determines whether the input focus can enter widget or any of its children.

See Gtk::Widget#focusable=.


[View source]
def can_focus=(can_focus : Bool) : Nil #

Specifies whether the input focus can enter the widget or any of its children.

Applications should set can_focus to false to mark a widget as for pointer/touch use only.

Note that having can_focus be true is only one of the necessary conditions for being focusable. A widget must also be sensitive and focusable and not have an ancestor that is marked as not can-focus in order to receive input focus.

See Gtk::Widget#grab_focus for actually setting the input focus on a widget.


[View source]
def can_focus? : Bool #

[View source]
def can_target : Bool #

Queries whether widget can be the target of pointer events.


[View source]
def can_target=(can_target : Bool) : Nil #

Sets whether widget can be the target of pointer events.


[View source]
def can_target? : Bool #

[View source]
def child_focus(direction : Gtk::DirectionType) : Bool #

Called by widgets as the user moves around the window using keyboard shortcuts.

The direction argument indicates what kind of motion is taking place (up, down, left, right, tab forward, tab backward).

This function calls the Gtk::Widget#focus virtual function; widgets can override the virtual function in order to implement appropriate focus behavior.

The default focus() virtual function for a widget should return TRUE if moving in direction left the focus on a focusable location inside that widget, and FALSE if moving in direction moved the focus outside the widget. When returning TRUE, widgets normallycall Gtk::Widget#grab_focus to place the focus accordingly; when returning FALSE, they don’t modify the current focus location.

This function is used by custom widget implementations; if you're writing an app, you’d use Gtk::Widget#grab_focus to move the focus to a particular widget.


[View source]
def child_visible : Bool #

Gets the value set with gtk_widget_set_child_visible().

If you feel a need to use this function, your code probably needs reorganization.

This function is only useful for container implementations and should never be called by an application.


[View source]
def child_visible=(child_visible : Bool) : Nil #

Sets whether widget should be mapped along with its parent.

The child visibility can be set for widget before it is added to a container with Gtk::Widget#parent=, to avoid mapping children unnecessary before immediately unmapping them. However it will be reset to its default state of true when the widget is removed from a container.

Note that changing the child visibility of a widget does not queue a resize on the widget. Most of the time, the size of a widget is computed from all visible children, whether or not they are mapped. If this is not the case, the container can queue a resize itself.

This function is only useful for container implementations and should never be called by an application.


[View source]
def children : Iterator(Widget) #

Returns an Iterator over widget children. Call .to_a on it if you need to allocate an array with all children.

widget.children.with_index do |child, i|
  puts "Hi, I'm child #{i}, #{child}"
end

[View source]
def clipboard : Gdk::Clipboard #

Gets the clipboard object for widget.

This is a utility function to get the clipboard object for the Gdk::Display that widget is using.

Note that this function always works, even when widget is not realized yet.


[View source]
def compute_bounds(target : Gtk::Widget) : Graphene::Rect #

Computes the bounds for widget in the coordinate space of target.

FIXME Explain what "bounds" are.

If the operation is successful, true is returned. If widget has no bounds or the bounds cannot be expressed in target's coordinate space (for example if both widgets are in different windows), false is returned and bounds is set to the zero rectangle.

It is valid for widget and target to be the same widget.


[View source]
def compute_expand(orientation : Gtk::Orientation) : Bool #

Computes whether a container should give this widget extra space when possible.

Containers should check this, rather than looking at Gtk::Widget#hexpand or Gtk::Widget#vexpand.

This function already checks whether the widget is visible, so visibility does not need to be checked separately. Non-visible widgets are not expanded.

The computed expand value uses either the expand setting explicitly set on the widget itself, or, if none has been explicitly set, the widget may expand if some of its children do.


[View source]
def compute_point(target : Gtk::Widget, point : Graphene::Point) : Graphene::Point #

Translates the given point in widget's coordinates to coordinates relative to target’s coordinate system.

In order to perform this operation, both widgets must share a common ancestor.


[View source]
def compute_transform(target : Gtk::Widget) : Graphene::Matrix #

Computes a matrix suitable to describe a transformation from widget's coordinate system into target's coordinate system.

The transform can not be computed in certain cases, for example when widget and target do not share a common ancestor. In that case out_transform gets set to the identity matrix.


[View source]
def contains(x : Float64, y : Float64) : Bool #

Tests if the point at (@x, y) is contained in widget.

The coordinates for (@x, y) must be in widget coordinates, so (0, 0) is assumed to be the top left of widget's content area.


[View source]
def create_pango_context : Pango::Context #

Creates a new Pango::Context with the appropriate font map, font options, font description, and base direction for drawing text for this widget.

See also Gtk::Widget#pango_context.


[View source]
def create_pango_layout(text : String | Nil) : Pango::Layout #

Creates a new Pango::Layout with the appropriate font map, font description, and base direction for drawing text for this widget.

If you keep a Pango::Layout created in this way around, you need to re-create it when the widget Pango::Context is replaced. This can be tracked by listening to changes of the Gtk::Widget#root property on the widget.


[View source]
def css_classes : Enumerable(String) #

Returns the list of style classes applied to widget.


[View source]
def css_classes=(classes : Enumerable(String)) : Nil #

Clear all style classes applied to widget and replace them with classes.


[View source]
def css_name : String #

Returns the CSS name that is used for self.


[View source]
def css_name=(value : String) : String #

[View source]
def cursor : Gdk::Cursor | Nil #

Queries the cursor set on widget.

See Gtk::Widget#cursor= for details.


[View source]
def cursor=(cursor : Gdk::Cursor | Nil) : Nil #

Sets the cursor to be shown when pointer devices point towards widget.

If the cursor is NULL, widget will use the cursor inherited from the parent widget.


[View source]
def cursor_from_name=(name : String | Nil) : Nil #

Sets a named cursor to be shown when pointer devices point towards widget.

This is a utility function that creates a cursor via Gdk::Cursor#new_from_name and then sets it on widget with Gtk::Widget#cursor=. See those functions for details.

On top of that, this function allows name to be nil, which will do the same as calling Gtk::Widget#cursor= with a nil cursor.


[View source]
def destroy_signal #

[View source]
def direction : Gtk::TextDirection #

Gets the reading direction for a particular widget.

See Gtk::Widget#direction=.


[View source]
def direction=(dir : Gtk::TextDirection) : Nil #

Sets the reading direction on a particular widget.

This direction controls the primary direction for widgets containing text, and also the direction in which the children of a container are packed. The ability to set the direction is present in order so that correct localization into languages with right-to-left reading directions can be done. Generally, applications will let the default reading direction present, except for containers where the containers are arranged in an order that is explicitly visual rather than logical (such as buttons for text justification).

If the direction is set to %GTK_TEXT_DIR_NONE, then the value set by Gtk::Widget#default_direction= will be used.


[View source]
def direction_changed_signal #

[View source]
def display : Gdk::Display #

Get the Gdk::Display for the toplevel window associated with this widget.

This function can only be called after the widget has been added to a widget hierarchy with a Gtk::Window at the top.

In general, you should only create display specific resources when a widget has been realized, and you should free those resources when the widget is unrealized.


[View source]
def drag_check_threshold(start_x : Int32, start_y : Int32, current_x : Int32, current_y : Int32) : Bool #

Checks to see if a drag movement has passed the GTK drag threshold.


[View source]
def error_bell : Nil #

Notifies the user about an input-related error on this widget.

If the [property@Gtk.Settings:gtk-error-bell] setting is true, it calls Gdk::Surface#beep, otherwise it does nothing.

Note that the effect of Gdk::Surface#beep can be configured in many ways, depending on the windowing backend and the desktop environment or window manager that is used.


[View source]
def first_child : Gtk::Widget | Nil #

Returns the widgets first child.

This API is primarily meant for widget implementations.


[View source]
def focus_child : Gtk::Widget | Nil #

Returns the current focus child of widget.


[View source]
def focus_child=(child : Gtk::Widget | Nil) : Nil #

Set child as the current focus child of widget.

This function is only suitable for widget implementations. If you want a certain widget to get the input focus, call Gtk::Widget#grab_focus on it.


[View source]
def focus_on_click : Bool #

Returns whether the widget should grab focus when it is clicked with the mouse.

See Gtk::Widget#focus_on_click=.


[View source]
def focus_on_click=(focus_on_click : Bool) : Nil #

Sets whether the widget should grab focus when it is clicked with the mouse.

Making mouse clicks not grab focus is useful in places like toolbars where you don’t want the keyboard focus removed from the main area of the application.


[View source]
def focus_on_click? : Bool #

[View source]
def focusable : Bool #

Determines whether widget can own the input focus.

See Gtk::Widget#focusable=.


[View source]
def focusable=(focusable : Bool) : Nil #

Specifies whether widget can own the input focus.

Widget implementations should set focusable to true in their init() function if they want to receive keyboard input.

Note that having focusable be true is only one of the necessary conditions for being focusable. A widget must also be sensitive and can-focus and not have an ancestor that is marked as not can-focus in order to receive input focus.

See Gtk::Widget#grab_focus for actually setting the input focus on a widget.


[View source]
def focusable? : Bool #

[View source]
def font_map : Pango::FontMap | Nil #

Gets the font map of widget.

See Gtk::Widget#font_map=.


[View source]
def font_map=(font_map : Pango::FontMap | Nil) : Nil #

Sets the font map to use for Pango rendering.

The font map is the object that is used to look up fonts. Setting a custom font map can be useful in special situations, e.g. when you need to add application-specific fonts to the set of available fonts.

When not set, the widget will inherit the font map from its parent.


[View source]
def font_options : Cairo::FontOptions | Nil #

Returns the cairo_::font_options_t of widget.

Seee Gtk::Widget#font_options=.


[View source]
def font_options=(options : Cairo::FontOptions | Nil) : Nil #

Sets the cairo_::font_options_t used for Pango rendering in this widget.

When not set, the default font options for the Gdk::Display will be used.


[View source]
def frame_clock : Gdk::FrameClock | Nil #

Obtains the frame clock for a widget.

The frame clock is a global “ticker” that can be used to drive animations and repaints. The most common reason to get the frame clock is to call Gdk::FrameClock#frame_time, in order to get a time to use for animating. For example you might record the start of the animation with an initial value from Gdk::FrameClock#frame_time, and then update the animation by calling Gdk::FrameClock#frame_time again during each repaint.

Gdk::FrameClock#request_phase will result in a new frame on the clock, but won’t necessarily repaint any widgets. To repaint a widget, you have to use Gtk::Widget#queue_draw which invalidates the widget (thus scheduling it to receive a draw on the next frame). gtk_widget_queue_draw() will also end up requesting a frame on the appropriate frame clock.

A widget’s frame clock will not change while the widget is mapped. Reparenting a widget (which implies a temporary unmap) can change the widget’s frame clock.

Unrealized widgets do not have a frame clock.


[View source]
def grab_focus : Bool #

Causes widget to have the keyboard focus for the Gtk::Window it's inside.

If widget is not focusable, or its Gtk::Widget#grab_focus implementation cannot transfer the focus to a descendant of widget that is focusable, it will not take focus and false will be returned.

Calling Gtk::Widget#grab_focus on an already focused widget is allowed, should not have an effect, and return true.


[View source]
def halign : Gtk::Align #

Gets the horizontal alignment of widget.

For backwards compatibility reasons this method will never return %GTK_ALIGN_BASELINE, but instead it will convert it to %GTK_ALIGN_FILL. Baselines are not supported for horizontal alignment.


[View source]
def halign=(align : Gtk::Align) : Nil #

Sets the horizontal alignment of widget.


[View source]
def has_css_class(css_class : String) : Bool #

Returns whether css_class is currently applied to widget.


[View source]
def has_default : Bool #

Determines whether widget is the current default widget within its toplevel.


[View source]
def has_default? : Bool #

[View source]
def has_focus : Bool #

Determines if the widget has the global input focus.

See Gtk::Widget#is_focus? for the difference between having the global input focus, and only having the focus within a toplevel.


[View source]
def has_focus? : Bool #

[View source]
def has_tooltip : Bool #

Returns the current value of the has-tooltip property.


[View source]
def has_tooltip=(has_tooltip : Bool) : Nil #

Sets the has-tooltip property on widget to has_tooltip.


[View source]
def has_tooltip? : Bool #

[View source]
def has_visible_focus : Bool #

Determines if the widget should show a visible indication that it has the global input focus.

This is a convenience function that takes into account whether focus indication should currently be shown in the toplevel window of widget. See Gtk::Window#focus_visible for more information about focus indication.

To find out if the widget has the global input focus, use Gtk::Widget#has_focus.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def height : Int32 #

Returns the content height of the widget.

This function returns the height passed to its size-allocate implementation, which is the height you should be using in Gtk::Widget#snapshot.

For pointer events, see Gtk::Widget#contains.


[View source]
def height_request : Int32 #

[View source]
def height_request=(value : Int32) : Int32 #

[View source]
def hexpand : Bool #

Gets whether the widget would like any available extra horizontal space.

When a user resizes a Gtk::Window, widgets with expand=TRUE generally receive the extra space. For example, a list or scrollable area or document in your window would often be set to expand.

Containers should use Gtk::Widget#compute_expand rather than this function, to see whether a widget, or any of its children, has the expand flag set. If any child of a widget wants to expand, the parent may ask to expand also.

This function only looks at the widget’s own hexpand flag, rather than computing whether the entire widget tree rooted at this widget wants to expand.


[View source]
def hexpand=(expand : Bool) : Nil #

Sets whether the widget would like any available extra horizontal space.

When a user resizes a Gtk::Window, widgets with expand=TRUE generally receive the extra space. For example, a list or scrollable area or document in your window would often be set to expand.

Call this function to set the expand flag if you would like your widget to become larger horizontally when the window has extra room.

By default, widgets automatically expand if any of their children want to expand. (To see if a widget will automatically expand given its current children and state, call Gtk::Widget#compute_expand. A container can decide how the expandability of children affects the expansion of the container by overriding the compute_expand virtual method on Gtk::Widget.).

Setting hexpand explicitly with this function will override the automatic expand behavior.

This function forces the widget to expand or not to expand, regardless of children. The override occurs because Gtk::Widget#hexpand= sets the hexpand-set property (see Gtk::Widget#hexpand_set=) which causes the widget’s hexpand value to be used, rather than looking at children and widget state.


[View source]
def hexpand? : Bool #

[View source]
def hexpand_set : Bool #

Gets whether gtk_widget_set_hexpand() has been used to explicitly set the expand flag on this widget.

If Gtk::Widget#hexpand property is set, then it overrides any computed expand value based on child widgets. If #hexpand is not set, then the expand value depends on whether any children of the widget would like to expand.

There are few reasons to use this function, but it’s here for completeness and consistency.


[View source]
def hexpand_set=(set : Bool) : Nil #

Sets whether the hexpand flag will be used.

The [property@Gtk.Widget:hexpand-set] property will be set automatically when you call Gtk::Widget#hexpand= to set hexpand, so the most likely reason to use this function would be to unset an explicit expand flag.

If hexpand is set, then it overrides any computed expand value based on child widgets. If hexpand is not set, then the expand value depends on whether any children of the widget would like to expand.

There are few reasons to use this function, but it’s here for completeness and consistency.


[View source]
def hexpand_set? : Bool #

[View source]
def hide : Nil #

Reverses the effects of gtk_widget_show().

This is causing the widget to be hidden (invisible to the user).


[View source]
def hide_signal #

[View source]
def in_destruction : Bool #

Returns whether the widget is currently being destroyed.

This information can sometimes be used to avoid doing unnecessary work.


[View source]
def init_template : Nil #

Creates and initializes child widgets defined in templates.

This function must be called in the instance initializer for any class which assigned itself a template using Gtk::WidgetClass#template=.

It is important to call this function in the instance initializer of a Gtk::Widget subclass and not in GObject.constructed() or GObject.constructor() for two reasons:

  • derived widgets will assume that the composite widgets defined by its parent classes have been created in their relative instance initializers
  • when calling g_object_new() on a widget with composite templates, it’s important to build the composite widgets before the construct properties are set. Properties passed to g_object_new() should take precedence over properties set in the private template XML

A good rule of thumb is to call this function as the first thing in an instance initialization function.


[View source]
def insert_action_group(name : String, group : Gio::ActionGroup | Nil) : Nil #

Inserts group into widget.

Children of widget that implement Gtk::Actionable can then be associated with actions in group by setting their “action-name” to prefix.action-name.

Note that inheritance is defined for individual actions. I.e. even if you insert a group with prefix prefix, actions with the same prefix will still be inherited from the parent, unless the group contains an action with the same name.

If group is nil, a previously inserted group for name is removed from widget.


[View source]
def insert_after(parent : Gtk::Widget, previous_sibling : Gtk::Widget | Nil) : Nil #

Inserts widget into the child widget list of parent.

It will be placed after previous_sibling, or at the beginning if previous_sibling is nil.

After calling this function, gtk_widget_get_prev_sibling(widget) will return previous_sibling.

If parent is already set as the parent widget of widget, this function can also be used to reorder widget in the child widget list of parent.

This API is primarily meant for widget implementations; if you are just using a widget, you must use its own API for adding children.


[View source]
def insert_before(parent : Gtk::Widget, next_sibling : Gtk::Widget | Nil) : Nil #

Inserts widget into the child widget list of parent.

It will be placed before next_sibling, or at the end if next_sibling is nil.

After calling this function, gtk_widget_get_next_sibling(widget) will return next_sibling.

If parent is already set as the parent widget of widget, this function can also be used to reorder widget in the child widget list of parent.

This API is primarily meant for widget implementations; if you are just using a widget, you must use its own API for adding children.


[View source]
def is_ancestor(ancestor : Gtk::Widget) : Bool #

Determines whether widget is somewhere inside ancestor, possibly with intermediate containers.


[View source]
def is_drawable : Bool #

Determines whether widget can be drawn to.

A widget can be drawn if it is mapped and visible.


[View source]
def is_focus : Bool #

Determines if the widget is the focus widget within its toplevel.

This does not mean that the [property@Gtk.Widget:has-focus] property is necessarily set; [property@Gtk.Widget:has-focus] will only be set if the toplevel widget additionally has the global input focus.


[View source]
def is_sensitive : Bool #

Returns the widget’s effective sensitivity.

This means it is sensitive itself and also its parent widget is sensitive.


[View source]
def is_visible : Bool #

Determines whether the widget and all its parents are marked as visible.

This function does not check if the widget is obscured in any way.

See also Gtk::Widget#visible and Gtk::Widget#visible=.


[View source]
def keynav_failed(direction : Gtk::DirectionType) : Bool #

Emits the ::keynav-failed signal on the widget.

This function should be called whenever keyboard navigation within a single widget hits a boundary.

The return value of this function should be interpreted in a way similar to the return value of Gtk::Widget#child_focus. When true is returned, stay in the widget, the failed keyboard navigation is OK and/or there is nowhere we can/should move the focus to. When false is returned, the caller should continue with keyboard navigation outside the widget, e.g. by calling Gtk::Widget#child_focus on the widget’s toplevel.

The default [signal@Gtk.Widget::keynav-failed] handler returns false for %GTK_DIR_TAB_FORWARD and %GTK_DIR_TAB_BACKWARD. For the other values of Gtk::DirectionType it returns true.

Whenever the default handler returns true, it also calls Gtk::Widget#error_bell to notify the user of the failed keyboard navigation.

A use case for providing an own implementation of ::keynav-failed (either by connecting to it or by overriding it) would be a row of Gtk::Entry widgets where the user should be able to navigate the entire row with the cursor keys, as e.g. known from user interfaces that require entering license keys.


[View source]
def keynav_failed_signal #

[View source]
def last_child : Gtk::Widget | Nil #

Returns the widgets last child.

This API is primarily meant for widget implementations.


[View source]
def layout_manager : Gtk::LayoutManager | Nil #

Retrieves the layout manager used by widget.

See Gtk::Widget#layout_manager=.


[View source]
def layout_manager=(layout_manager : Gtk::LayoutManager | Nil) : Nil #

Sets the layout manager delegate instance that provides an implementation for measuring and allocating the children of widget.


[View source]
def list_mnemonic_labels : GLib::List #

Returns the widgets for which this widget is the target of a mnemonic.

Typically, these widgets will be labels. See, for example, Gtk::Label#mnemonic_widget=.

The widgets in the list are not individually referenced. If you want to iterate through the list and perform actions involving callbacks that might destroy the widgets, you must call g_list_foreach (result, (GFunc)g_object_ref, NULL) first, and then unref all the widgets afterwards.


[View source]
def map : Nil #

Causes a widget to be mapped if it isn’t already.

This function is only for use in widget implementations.


[View source]
def map_signal #

[View source]
def mapped : Bool #

Whether the widget is mapped.


[View source]
def margin_bottom : Int32 #

Gets the bottom margin of widget.


[View source]
def margin_bottom=(margin : Int32) : Nil #

Sets the bottom margin of widget.


[View source]
def margin_end : Int32 #

Gets the end margin of widget.


[View source]
def margin_end=(margin : Int32) : Nil #

Sets the end margin of widget.


[View source]
def margin_start : Int32 #

Gets the start margin of widget.


[View source]
def margin_start=(margin : Int32) : Nil #

Sets the start margin of widget.


[View source]
def margin_top : Int32 #

Gets the top margin of widget.


[View source]
def margin_top=(margin : Int32) : Nil #

Sets the top margin of widget.


[View source]
def measure(orientation : Gtk::Orientation, for_size : Int32) : Nil #

Measures widget in the orientation orientation and for the given for_size.

As an example, if orientation is %GTK_ORIENTATION_HORIZONTAL and for_size is 300, this functions will compute the minimum and natural width of widget if it is allocated at a height of 300 pixels.

See Gtk::Widget’s geometry management section for a more details on implementing Gtk::WidgetClass.measure().


[View source]
def mnemonic_activate(group_cycling : Bool) : Bool #

Emits the ::mnemonic-activate signal.

See [signal@Gtk.Widget::mnemonic-activate].


[View source]
def mnemonic_activate_signal #

[View source]
def move_focus_signal #

[View source]
def name : String #

Retrieves the name of a widget.

See Gtk::Widget#name= for the significance of widget names.


[View source]
def name=(name : String) : Nil #

Sets a widgets name.

Setting a name allows you to refer to the widget from a CSS file. You can apply a style to widgets with a particular name in the CSS file. See the documentation for the CSS syntax (on the same page as the docs for Gtk::StyleContext.

Note that the CSS syntax has certain special characters to delimit and represent elements in a selector (period, #, >, *...), so using these will make your widget impossible to match by name. Any combination of alphanumeric symbols, dashes and underscores will suffice.


[View source]
def native : Gtk::Native | Nil #

Returns the nearest Gtk::Native ancestor of widget.

This function will return nil if the widget is not contained inside a widget tree with a native ancestor.

Gtk::Native widgets will return themselves here.


[View source]
def next_sibling : Gtk::Widget | Nil #

Returns the widgets next sibling.

This API is primarily meant for widget implementations.


[View source]
def observe_children : Gio::ListModel #

Returns a GListModel to track the children of widget.

Calling this function will enable extra internal bookkeeping to track children and emit signals on the returned listmodel. It may slow down operations a lot.

Applications should try hard to avoid calling this function because of the slowdowns.


[View source]
def observe_controllers : Gio::ListModel #

Returns a GListModel to track the Gtk::EventControllers of widget.

Calling this function will enable extra internal bookkeeping to track controllers and emit signals on the returned listmodel. It may slow down operations a lot.

Applications should try hard to avoid calling this function because of the slowdowns.


[View source]
def opacity : Float64 #

#Fetches the requested opacity for this widget.

See Gtk::Widget#opacity=.


[View source]
def opacity=(opacity : Float64) : Nil #

Request the widget to be rendered partially transparent.

An opacity of 0 is fully transparent and an opacity of 1 is fully opaque.

Opacity works on both toplevel widgets and child widgets, although there are some limitations: For toplevel widgets, applying opacity depends on the capabilities of the windowing system. On X11, this has any effect only on X displays with a compositing manager, see gdk_display_is_composited(). On Windows and Wayland it should always work, although setting a window’s opacity after the window has been shown may cause some flicker.

Note that the opacity is inherited through inclusion — if you set a toplevel to be partially translucent, all of its content will appear translucent, since it is ultimatively rendered on that toplevel. The opacity value itself is not inherited by child widgets (since that would make widgets deeper in the hierarchy progressively more translucent). As a consequence, Gtk::Popovers and other Gtk::Native widgets with their own surface will use their own opacity value, and thus by default appear non-translucent, even if they are attached to a toplevel that is translucent.


[View source]
def overflow : Gtk::Overflow #

Returns the widgets overflow value.


[View source]
def overflow=(overflow : Gtk::Overflow) : Nil #

Sets how widget treats content that is drawn outside the widget's content area.

See the definition of Gtk::Overflow for details.

This setting is provided for widget implementations and should not be used by application code.

The default value is %GTK_OVERFLOW_VISIBLE.


[View source]
def pango_context : Pango::Context #

Gets a Pango::Context with the appropriate font map, font description, and base direction for this widget.

Unlike the context returned by Gtk::Widget#create_pango_context, this context is owned by the widget (it can be used until the screen for the widget changes or the widget is removed from its toplevel), and will be updated to match any changes to the widget’s attributes. This can be tracked by listening to changes of the Gtk::Widget#root property on the widget.


[View source]
def parent : Gtk::Widget | Nil #

Returns the parent widget of widget.


[View source]
def parent=(parent : Gtk::Widget) : Nil #

Sets parent as the parent widget of widget.

This takes care of details such as updating the state and style of the child to reflect its new location and resizing the parent. The opposite function is Gtk::Widget#unparent.

This function is useful only when implementing subclasses of Gtk::Widget.


[View source]
def pick(x : Float64, y : Float64, flags : Gtk::PickFlags) : Gtk::Widget | Nil #

Finds the descendant of widget closest to the point (@x, y).

The point must be given in widget coordinates, so (0, 0) is assumed to be the top left of widget's content area.

Usually widgets will return nil if the given coordinate is not contained in widget checked via Gtk::Widget#contains. Otherwise they will recursively try to find a child that does not return nil. Widgets are however free to customize their picking algorithm.

This function is used on the toplevel to determine the widget below the mouse cursor for purposes of hover highlighting and delivering events.


[View source]
def preferred_size : Gtk::Requisition #

Retrieves the minimum and natural size of a widget, taking into account the widget’s preference for height-for-width management.

This is used to retrieve a suitable size by container widgets which do not impose any restrictions on the child placement. It can be used to deduce toplevel window and menu sizes as well as child widgets in free-form containers such as Gtk::Fixed.

Handle with care. Note that the natural height of a height-for-width widget will generally be a smaller size than the minimum height, since the required height for the natural width is generally smaller than the required height for the minimum width.

Use #gtk_widget_measure if you want to support baseline alignment.


[View source]
def prev_sibling : Gtk::Widget | Nil #

Returns the widgets previous sibling.

This API is primarily meant for widget implementations.


[View source]
def primary_clipboard : Gdk::Clipboard #

Gets the primary clipboard of widget.

This is a utility function to get the primary clipboard object for the Gdk::Display that widget is using.

Note that this function always works, even when widget is not realized yet.


[View source]
def query_tooltip_signal #

[View source]
def queue_allocate : Nil #

Flags the widget for a rerun of the Gtk::Widget#size_allocate function.

Use this function instead of Gtk::Widget#queue_resize when the widget's size request didn't change but it wants to reposition its contents.

An example user of this function is Gtk::Widget#halign=.

This function is only for use in widget implementations.


[View source]
def queue_draw : Nil #

Schedules this widget to be redrawn in the paint phase of the current or the next frame.

This means widget's Gtk::Widget#snapshot implementation will be called.


[View source]
def queue_resize : Nil #

Flags a widget to have its size renegotiated.

This should be called when a widget for some reason has a new size request. For example, when you change the text in a Gtk::Label, the label queues a resize to ensure there’s enough space for the new text.

Note that you cannot call gtk_widget_queue_resize() on a widget from inside its implementation of the Gtk::Widget#size_allocate virtual method. Calls to gtk_widget_queue_resize() from inside Gtk::Widget#size_allocate will be silently ignored.

This function is only for use in widget implementations.


[View source]
def realize : Nil #

Creates the GDK resources associated with a widget.

Normally realization happens implicitly; if you show a widget and all its parent containers, then the widget will be realized and mapped automatically.

Realizing a widget requires all the widget’s parent widgets to be realized; calling this function realizes the widget’s parents in addition to widget itself. If a widget is not yet inside a toplevel window when you realize it, bad things will happen.

This function is primarily used in widget implementations, and isn’t very useful otherwise. Many times when you think you might need it, a better approach is to connect to a signal that will be called after the widget is realized automatically, such as Gtk::Widget::#realize.


[View source]
def realize_signal #

[View source]
def realized : Bool #

Determines whether widget is realized.


[View source]
def receives_default : Bool #

Determines whether widget is always treated as the default widget within its toplevel when it has the focus, even if another widget is the default.

See Gtk::Widget#receives_default=.


[View source]
def receives_default=(receives_default : Bool) : Nil #

Specifies whether widget will be treated as the default widget within its toplevel when it has the focus, even if another widget is the default.


[View source]
def receives_default? : Bool #

[View source]
def remove_controller(controller : Gtk::EventController) : Nil #

Removes controller from widget, so that it doesn't process events anymore.

It should not be used again.

Widgets will remove all event controllers automatically when they are destroyed, there is normally no need to call this function.


[View source]
def remove_css_class(css_class : String) : Nil #

Removes a style from widget.

After this, the style of widget will stop matching for css_class.


[View source]
def remove_mnemonic_label(label : Gtk::Widget) : Nil #

Removes a widget from the list of mnemonic labels for this widget.

See Gtk::Widget#list_mnemonic_labels. The widget must have previously been added to the list with Gtk::Widget#add_mnemonic_label.


[View source]
def remove_tick_callback(id : UInt32) : Nil #

Removes a tick callback previously registered with gtk_widget_add_tick_callback().


[View source]
def request_mode : Gtk::SizeRequestMode #

Gets whether the widget prefers a height-for-width layout or a width-for-height layout.

Single-child widgets generally propagate the preference of their child, more complex widgets need to request something either in context of their children or in context of their allocation capabilities.


[View source]
def root : Gtk::Root | Nil #

Returns the Gtk::Root widget of widget.

This function will return nil if the widget is not contained inside a widget tree with a root widget.

Gtk::Root widgets will return themselves here.


[View source]
def scale_factor : Int32 #

Retrieves the internal scale factor that maps from window coordinates to the actual device pixels.

On traditional systems this is 1, on high density outputs, it can be a higher value (typically 2).

See Gdk::Surface#scale_factor.


[View source]
def sensitive : Bool #

Returns the widget’s sensitivity.

This function returns the value that has been set using Gtk::Widget#sensitive=).

The effective sensitivity of a widget is however determined by both its own and its parent widget’s sensitivity. See Gtk::Widget#is_sensitive?.


[View source]
def sensitive=(sensitive : Bool) : Nil #

Sets the sensitivity of a widget.

A widget is sensitive if the user can interact with it. Insensitive widgets are “grayed out” and the user can’t interact with them. Insensitive widgets are known as “inactive”, “disabled”, or “ghosted” in some other toolkits.


[View source]
def sensitive? : Bool #

[View source]
def set_size_request(width : Int32, height : Int32) : Nil #

Sets the minimum size of a widget.

That is, the widget’s size request will be at least width by height. You can use this function to force a widget to be larger than it normally would be.

In most cases, Gtk::Window#default_size= is a better choice for toplevel windows than this function; setting the default size will still allow users to shrink the window. Setting the size request will force them to leave the window at least as large as the size request.

Note the inherent danger of setting any fixed size - themes, translations into other languages, different fonts, and user action can all change the appropriate size for a given widget. So, it's basically impossible to hardcode a size that will always be correct.

The size request of a widget is the smallest size a widget can accept while still functioning well and drawing itself correctly. However in some strange cases a widget may be allocated less than its requested size, and in many cases a widget may be allocated more space than it requested.

If the size request in a given direction is -1 (unset), then the “natural” size request of the widget will be used instead.

The size request set here does not include any margin from the properties [property@Gtk.Widget:margin-start], [property@Gtk.Widget:margin-end], [property@Gtk.Widget:margin-top], and [property@Gtk.Widget:margin-bottom], but it does include pretty much all other padding or border properties set by any subclass of Gtk::Widget.


[View source]
def set_state_flags(flags : Gtk::StateFlags, clear : Bool) : Nil #

Turns on flag values in the current widget state.

Typical widget states are insensitive, prelighted, etc.

This function accepts the values %GTK_STATE_FLAG_DIR_LTR and %GTK_STATE_FLAG_DIR_RTL but ignores them. If you want to set the widget's direction, use Gtk::Widget#direction=.

This function is for use in widget implementations.


[View source]
def settings : Gtk::Settings #

Gets the settings object holding the settings used for this widget.

Note that this function can only be called when the Gtk::Widget is attached to a toplevel, since the settings object is specific to a particular Gdk::Display. If you want to monitor the widget for changes in its settings, connect to the notify::display signal.


[View source]
def should_layout : Bool #

Returns whether widget should contribute to the measuring and allocation of its parent.

This is false for invisible children, but also for children that have their own surface.


[View source]
def show : Nil #

Flags a widget to be displayed.

Any widget that isn’t shown will not appear on the screen.

Remember that you have to show the containers containing a widget, in addition to the widget itself, before it will appear onscreen.

When a toplevel container is shown, it is immediately realized and mapped; other shown widgets are realized and mapped when their toplevel container is realized and mapped.


[View source]
def show_signal #

[View source]
def size(orientation : Gtk::Orientation) : Int32 #

Returns the content width or height of the widget.

Which dimension is returned depends on orientation.

This is equivalent to calling Gtk::Widget#width for %GTK_ORIENTATION_HORIZONTAL or Gtk::Widget#height for %GTK_ORIENTATION_VERTICAL, but can be used when writing orientation-independent code, such as when implementing Gtk::Orientable widgets.


[View source]
def size_allocate(allocation : Gdk::Rectangle, baseline : Int32) : Nil #

Allocates widget with a transformation that translates the origin to the position in allocation.

This is a simple form of Gtk::Widget#allocate.


[View source]
def size_request : Nil #

Gets the size request that was explicitly set for the widget using gtk_widget_set_size_request().

A value of -1 stored in width or height indicates that that dimension has not been set explicitly and the natural requisition of the widget will be used instead. See Gtk::Widget#size_request=. To get the size a widget will actually request, call Gtk::Widget#measure instead of this function.


[View source]
def snapshot_child(child : Gtk::Widget, snapshot : Gtk::Snapshot) : Nil #

Snapshot the a child of widget.

When a widget receives a call to the snapshot function, it must send synthetic Gtk::Widget#snapshot calls to all children. This function provides a convenient way of doing this. A widget, when it receives a call to its Gtk::Widget#snapshot function, calls gtk_widget_snapshot_child() once for each child, passing in the snapshot the widget received.

gtk_widget_snapshot_child() takes care of translating the origin of snapshot, and deciding whether the child needs to be snapshot.

This function does nothing for children that implement Gtk::Native.


[View source]
def state_flags : Gtk::StateFlags #

Returns the widget state as a flag set.

It is worth mentioning that the effective %GTK_STATE_FLAG_INSENSITIVE state will be returned, that is, also based on parent insensitivity, even if widget itself is sensitive.

Also note that if you are looking for a way to obtain the Gtk::StateFlags to pass to a Gtk::StyleContext method, you should look at Gtk::StyleContext#state.


[View source]
def state_flags_changed_signal #

[View source]
def style_context : Gtk::StyleContext #

Returns the style context associated to widget.

The returned object is guaranteed to be the same for the lifetime of widget.


[View source]
def template_child(widget_type : UInt64, name : String) : GObject::Object #

Fetch an object build from the template XML for widget_type in this widget instance.

This will only report children which were previously declared with Gtk::WidgetClass#bind_template_child_full or one of its variants.

This function is only meant to be called for code which is private to the widget_type which declared the child and is meant for language bindings which cannot easily make use of the GObject structure offsets.


[View source]
def template_child(name : String) : GObject::Object #

Convenient function, same as #template_child(self.class.g_type, name).


[View source]
def tooltip_markup : String | Nil #

Gets the contents of the tooltip for widget.

If the tooltip has not been set using Gtk::Widget#tooltip_markup=, this function returns nil.


[View source]
def tooltip_markup=(value : String) : String #

[View source]
def tooltip_markup=(markup : String | Nil) : Nil #

Sets markup as the contents of the tooltip, which is marked up with Pango markup.

This function will take care of setting the [property@Gtk.Widget:has-tooltip] as a side effect, and of the default handler for the [signal@Gtk.Widget::query-tooltip] signal.

See also Gtk::Tooltip#markup=.


[View source]
def tooltip_text : String | Nil #

Gets the contents of the tooltip for widget.

If the widget's tooltip was set using Gtk::Widget#tooltip_markup=, this function will return the escaped text.


[View source]
def tooltip_text=(value : String) : String #

[View source]
def tooltip_text=(text : String | Nil) : Nil #

Sets text as the contents of the tooltip.

If text contains any markup, it will be escaped.

This function will take care of setting [property@Gtk.Widget:has-tooltip] as a side effect, and of the default handler for the [signal@Gtk.Widget::query-tooltip] signal.

See also Gtk::Tooltip#text=.


[View source]
def translate_coordinates(dest_widget : Gtk::Widget, src_x : Float64, src_y : Float64) : Bool #

Translate coordinates relative to src_widget’s allocation to coordinates relative to dest_widget’s allocations.

In order to perform this operation, both widget must share a common ancestor.


[View source]
def trigger_tooltip_query : Nil #

Triggers a tooltip query on the display where the toplevel of widget is located.


[View source]
def unmap : Nil #

Causes a widget to be unmapped if it’s currently mapped.

This function is only for use in widget implementations.


[View source]
def unmap_signal #

[View source]
def unparent : Nil #

Dissociate widget from its parent.

This function is only for use in widget implementations, typically in dispose.


[View source]
def unrealize : Nil #

Causes a widget to be unrealized (frees all GDK resources associated with the widget).

This function is only useful in widget implementations.


[View source]
def unrealize_signal #

[View source]
def unset_state_flags(flags : Gtk::StateFlags) : Nil #

Turns off flag values for the current widget state.

See Gtk::Widget#state_flags=.

This function is for use in widget implementations.


[View source]
def valign : Gtk::Align #

Gets the vertical alignment of widget.


[View source]
def valign=(align : Gtk::Align) : Nil #

Sets the vertical alignment of widget.


[View source]
def vexpand : Bool #

Gets whether the widget would like any available extra vertical space.

See Gtk::Widget#hexpand for more detail.


[View source]
def vexpand=(expand : Bool) : Nil #

Sets whether the widget would like any available extra vertical space.

See Gtk::Widget#hexpand= for more detail.


[View source]
def vexpand? : Bool #

[View source]
def vexpand_set : Bool #

Gets whether gtk_widget_set_vexpand() has been used to explicitly set the expand flag on this widget.

See Gtk::Widget#hexpand_set for more detail.


[View source]
def vexpand_set=(set : Bool) : Nil #

Sets whether the vexpand flag will be used.

See Gtk::Widget#hexpand_set= for more detail.


[View source]
def vexpand_set? : Bool #

[View source]
def visible : Bool #

Determines whether the widget is visible.

If you want to take into account whether the widget’s parent is also marked as visible, use Gtk::Widget#is_visible? instead.

This function does not check if the widget is obscured in any way.

See Gtk::Widget#visible=.


[View source]
def visible=(visible : Bool) : Nil #

Sets the visibility state of widget.

Note that setting this to true doesn’t mean the widget is actually viewable, see Gtk::Widget#visible.

This function simply calls Gtk::Widget#show or Gtk::Widget#hide but is nicer to use when the visibility of the widget depends on some condition.


[View source]
def visible? : Bool #

[View source]
def width : Int32 #

Returns the content width of the widget.

This function returns the width passed to its size-allocate implementation, which is the width you should be using in Gtk::Widget#snapshot.

For pointer events, see Gtk::Widget#contains.


[View source]
def width_request : Int32 #

[View source]
def width_request=(value : Int32) : Int32 #

[View source]