Class: Gtk::Notebook

Inherits:
Widget
  • Object
show all
Extended by:
GLib::Deprecatable
Defined in:
lib/gtk4/deprecated.rb

Overview

GtkNotebook is a container whose children are pages switched between using tabs.

An example GtkNotebook

There are many configuration options for GtkNotebook. Among other things, you can choose on which edge the tabs appear (see [methodGtk.Notebook.set_tab_pos]), whether, if there are too many tabs to fit the notebook should be made bigger or scrolling arrows added (see [methodGtk.Notebook.set_scrollable]), and whether there will be a popup menu allowing the users to switch pages. (see [methodGtk.Notebook.popup_enable]).

GtkNotebook as GtkBuildable

The GtkNotebook implementation of the GtkBuildable interface supports placing children into tabs by specifying “tab” as the “type” attribute of a <child> element. Note that the content of the tab must be created before the tab can be filled. A tab child can be specified without specifying a <child> type attribute.

To add a child widget in the notebooks action area, specify "action-start" or “action-end” as the “type” attribute of the <child> element.

An example of a UI definition fragment with GtkNotebook:

<object class="GtkNotebook">
  <child>
    <object class="GtkLabel" id="notebook-content">
      <property name="label">Content</property>
    </object>
  </child>
  <child type="tab">
    <object class="GtkLabel" id="notebook-tab">
      <property name="label">Tab</property>
    </object>
  </child>
</object>

CSS nodes

notebook
├── header.top
│   ├── [<action widget>]
│   ├── tabs
│   │   ├── [arrow]
│   │   ├── tab
│   │   │   ╰── <tab label>
┊   ┊   ┊
│   │   ├── tab[.reorderable-page]
│   │   │   ╰── <tab label>
│   │   ╰── [arrow]
│   ╰── [<action widget>]
│
╰── stack
    ├── <child>
    ┊
    ╰── <child>

GtkNotebook has a main CSS node with name notebook, a subnode with name header and below that a subnode with name tabs which contains one subnode per tab with name tab.

If action widgets are present, their CSS nodes are placed next to the tabs node. If the notebook is scrollable, CSS nodes with name arrow are placed as first and last child of the tabs node.

The main node gets the .frame style class when the notebook has a border (see [methodGtk.Notebook.set_show_border]).

The header node gets one of the style class .top, .bottom, .left or .right, depending on where the tabs are placed. For reorderable pages, the tab node gets the .reorderable-page class.

A tab node gets the .dnd style class while it is moved with drag-and-drop.

The nodes are always arranged from left-to-right, regardless of text direction.

Accessibility

GtkNotebook uses the following roles:

  • %GTK_ACCESSIBLE_ROLE_GROUP for the notebook widget
  • %GTK_ACCESSIBLE_ROLE_TAB_LIST for the list of tabs
  • %GTK_ACCESSIBLE_ROLE_TAB role for each tab
  • %GTK_ACCESSIBLE_ROLE_TAB_PANEL for each page

Instance Method Summary collapse

Methods inherited from Widget

#accessible_role, #accessible_role=, #action_set_enabled, #activate, #activate_action, #activate_action_variant, #activate_default, #activate_signal, #activate_signal=, #activate_signal_from_name=, #add_binding, #add_binding_action, #add_binding_signal, #add_controller, #add_css_class, #add_mnemonic_label, #add_shortcut, #add_tick_callback, #allocate, #allocated_baseline, #allocated_height, #allocated_width, #baseline, #bind_template_callback_full, #bind_template_child, #bind_template_child_full, #can_focus, #can_focus=, #can_focus?, #can_target, #can_target=, #can_target?, #child_focus, #child_visible, #child_visible=, #children, #clipboard, #compute_bounds, #compute_expand, #compute_point, #compute_transform, #contains, #create_pango_context, #create_pango_layout, #css_classes, #css_classes=, #css_name, #css_name=, #cursor, #cursor=, #cursor_from_name=, default_direction, default_direction=, #direction, #direction=, #display, #dispose_template, #drag_check_threshold, #error_bell, #first_child, #focus_child, #focus_child=, #focus_on_click, #focus_on_click=, #focus_on_click?, #focusable, #focusable=, #focusable?, #font_map, #font_map=, #font_options, #font_options=, #frame_clock, #get_allocation, #get_ancestor, #get_color, #get_preferred_size, #get_size, #get_size_request, #get_template_child, #grab_focus, #halign, #halign=, #has_css_class, #has_default, #has_default=, #has_default?, #has_focus, #has_focus=, #has_focus?, #has_tooltip, #has_tooltip=, #has_tooltip?, #has_visible_focus, have_template?, #height, #height_request, #height_request=, #hexpand, #hexpand=, #hexpand?, #hexpand_set, #hexpand_set=, #hexpand_set?, #hide, #in_destruction, #init_template, #insert_action_group, #insert_action_group_raw, #insert_after, #insert_before, #install_action, #install_property_action, #is_ancestor, #is_drawable, #is_focus, #is_sensitive, #is_visible, #keynav_failed, #last_child, #layout_manager, #layout_manager=, #layout_manager_type, #layout_manager_type=, #list_mnemonic_labels, #map, #mapped, #margin_bottom, #margin_bottom=, #margin_end, #margin_end=, #margin_start, #margin_start=, #margin_top, #margin_top=, #measure, #mnemonic_activate, #name, #name=, #native, #next_sibling, #observe_children, #observe_controllers, #opacity, #opacity=, #overflow, #overflow=, #pango_context, #parent, #parent=, #pick, #prev_sibling, #primary_clipboard, #query_action, #queue_allocate, #queue_draw, #queue_resize, #realize, #realized, #receives_default, #receives_default=, #receives_default?, #remove_controller, #remove_css_class, #remove_mnemonic_label, #remove_tick_callback, #request_mode, #root, #root=, #scale_factor, #scale_factor=, #sensitive, #sensitive=, #sensitive?, #set_size_request, #set_size_request_raw, #set_state_flags, #set_template, #set_template_raw, #settings, #should_layout, #show, #size_allocate, #snapshot_child, #state_flags, #style_context, #style_context_raw, #template=, template_children, #template_from_resource=, #template_scope=, #tooltip_markup, #tooltip_markup=, #tooltip_text, #tooltip_text=, #translate_coordinates, #translate_coordinates_raw, #trigger_tooltip_query, #unmap, #unparent, #unrealize, #unset_state_flags, #valign, #valign=, #vexpand, #vexpand=, #vexpand?, #vexpand_set, #vexpand_set=, #vexpand_set?, #visible, #visible=, #visible?, #width, #width_request, #width_request=

Constructor Details

#initializeGtk::Widget

Creates a new GtkNotebook widget with no pages.

Instance Method Details

#append_page(child, tab_label) ⇒ Integer

Appends a page to notebook.

Parameters:

  • child (Gtk::Widget)

    the GtkWidget to use as the contents of the page

  • tab_label (Gtk::Widget)

    the GtkWidget to be used as the label for the page, or nil to use the default label, “page N”

Returns:

  • (Integer)

    the index (starting from 0) of the appended page in the notebook, or -1 if function fails

#append_page_menu(child, tab_label, menu_label) ⇒ Integer

Appends a page to notebook, specifying the widget to use as the label in the popup menu.

Parameters:

  • child (Gtk::Widget)

    the GtkWidget to use as the contents of the page

  • tab_label (Gtk::Widget)

    the GtkWidget to be used as the label for the page, or nil to use the default label, “page N”

  • menu_label (Gtk::Widget)

    the widget to use as a label for the page-switch menu, if that is enabled. If nil, and tab_label is a GtkLabel or nil, then the menu label will be a newly created label with the same text as tab_label; if tab_label is not a GtkLabel, menu_label must be specified if the page-switch menu is to be used.

Returns:

  • (Integer)

    the index (starting from 0) of the appended page in the notebook, or -1 if function fails

#current_pageInteger

Returns the page number of the current page.

Returns:

  • (Integer)

    the index (starting from 0) of the current page in the notebook. If the notebook has no pages, then -1 will be returned.

#current_page=(page_num) ⇒ nil

Switches to the page number page_num.

Note that due to historical reasons, GtkNotebook refuses to switch to a page unless the child widget is visible. Therefore, it is recommended to show child widgets before adding them to a notebook.

Parameters:

  • page_num (Integer)

    index of the page to switch to, starting from 0. If negative, the last page will be used. If greater than the number of pages in the notebook, nothing will be done.

Returns:

  • (nil)

#detach_tab(child) ⇒ nil

Removes the child from the notebook.

This function is very similar to [methodGtk.Notebook.remove_page], but additionally informs the notebook that the removal is happening as part of a tab DND operation, which should not be cancelled.

Parameters:

Returns:

  • (nil)

#enable_popup=(enable_popup) ⇒ Boolean

If true, pressing the right mouse button on the notebook shows a page switching menu.

Parameters:

  • enable_popup (Boolean)

Returns:

  • (Boolean)

    enable-popup

  • (Boolean)

    enable-popup

#enable_popup?Boolean

If true, pressing the right mouse button on the notebook shows a page switching menu.

Returns:

  • (Boolean)

    enable-popup

#get_action_widget(pack_type) ⇒ Gtk::Widget

Gets one of the action widgets.

See [methodGtk.Notebook.set_action_widget].

Parameters:

  • pack_type (Gtk::PackType)

    pack type of the action widget to receive

Returns:

  • (Gtk::Widget)

    The action widget with the given pack_type or nil when this action widget has not been set

#get_menu_label(child) ⇒ Gtk::Widget

Retrieves the menu label widget of the page containing child.

Parameters:

  • child (Gtk::Widget)

    a widget contained in a page of notebook

Returns:

  • (Gtk::Widget)

    the menu label, or nil if the notebook page does not have a menu label other than the default (the tab label).

#get_menu_label_text(child) ⇒ String

Retrieves the text of the menu label for the page containing child.

Parameters:

  • child (Gtk::Widget)

    the child widget of a page of the notebook.

Returns:

  • (String)

    the text of the tab label, or nil if the widget does not have a menu label other than the default menu label, or the menu label widget is not a GtkLabel. The string is owned by the widget and must not be freed.

#get_nth_page(page_num) ⇒ Gtk::Widget

Returns the child widget contained in page number page_num. is out of bounds

Parameters:

  • page_num (Integer)

    the index of a page in the notebook, or -1 to get the last page

Returns:

  • (Gtk::Widget)

    the child widget, or nil if page_num

#get_page(child) ⇒ Gtk::NotebookPage

Returns the GtkNotebookPage for child.

Parameters:

Returns:

#get_tab_detachable(child) ⇒ Boolean

Returns whether the tab contents can be detached from notebook.

Parameters:

Returns:

  • (Boolean)

    true if the tab is detachable.

#get_tab_label(child) ⇒ Gtk::Widget

Returns the tab label widget for the page child.

nil is returned if child is not in notebook or if no tab label has specifically been set for child.

Parameters:

Returns:

#get_tab_label_text(child) ⇒ String

Retrieves the text of the tab label for the page containing child.

Parameters:

  • child (Gtk::Widget)

    a widget contained in a page of notebook

Returns:

  • (String)

    the text of the tab label, or nil if the tab label widget is not a GtkLabel. The string is owned by the widget and must not be freed.

#get_tab_reorderable(child) ⇒ Boolean

Gets whether the tab can be reordered via drag and drop or not.

Parameters:

Returns:

  • (Boolean)

    true if the tab is reorderable.

#group_nameString

Group name for tab drag and drop.

Returns:

  • (String)

    group-name

#group_name=(group_name) ⇒ String

Group name for tab drag and drop.

Parameters:

  • group_name (String)

Returns:

  • (String)

    group-name

  • (String)

    group-name

#insert_page(child, tab_label, position) ⇒ Integer

Insert a page into notebook at the given position.

Parameters:

  • child (Gtk::Widget)

    the GtkWidget to use as the contents of the page

  • tab_label (Gtk::Widget)

    the GtkWidget to be used as the label for the page, or nil to use the default label, “page N”

  • position (Integer)

    the index (starting at 0) at which to insert the page, or -1 to append the page after all other pages

Returns:

  • (Integer)

    the index (starting from 0) of the inserted page in the notebook, or -1 if function fails

#insert_page_menu(child, tab_label, menu_label, position) ⇒ Integer

Insert a page into notebook at the given position, specifying the widget to use as the label in the popup menu.

Parameters:

  • child (Gtk::Widget)

    the GtkWidget to use as the contents of the page

  • tab_label (Gtk::Widget)

    the GtkWidget to be used as the label for the page, or nil to use the default label, “page N”

  • menu_label (Gtk::Widget)

    the widget to use as a label for the page-switch menu, if that is enabled. If nil, and tab_label is a GtkLabel or nil, then the menu label will be a newly created label with the same text as tab_label; if tab_label is not a GtkLabel, menu_label must be specified if the page-switch menu is to be used.

  • position (Integer)

    the index (starting at 0) at which to insert the page, or -1 to append the page after all other pages.

Returns:

  • (Integer)

    the index (starting from 0) of the inserted page in the notebook

#n_pagesInteger

Gets the number of pages in a notebook.

Returns:

  • (Integer)

    the number of pages in the notebook

#next_pagenil

Switches to the next page.

Nothing happens if the current page is the last page.

Returns:

  • (nil)

#pageInteger

The index of the current page.

Returns:

  • (Integer)

    page

#page=(page) ⇒ Integer

The index of the current page.

Parameters:

  • page (Integer)

Returns:

  • (Integer)

    page

  • (Integer)

    page

#page_num(child) ⇒ Integer

Finds the index of the page which contains the given child widget.

Parameters:

Returns:

  • (Integer)

    the index of the page containing child, or -1 if child is not in the notebook

#pagesGio::ListModel

A selection model with the pages.

Returns:

  • (Gio::ListModel)

    pages

#pages=(pages) ⇒ Gio::ListModel

A selection model with the pages.

Parameters:

  • pages (Gio::ListModel)

Returns:

  • (Gio::ListModel)

    pages

  • (Gio::ListModel)

    pages

Disables the popup menu.

Returns:

  • (nil)

Enables the popup menu.

If the user clicks with the right mouse button on the tab labels, a menu with all the pages will be popped up.

Returns:

  • (nil)

#prepend_page(child, tab_label) ⇒ Integer

Prepends a page to notebook.

Parameters:

  • child (Gtk::Widget)

    the GtkWidget to use as the contents of the page

  • tab_label (Gtk::Widget)

    the GtkWidget to be used as the label for the page, or nil to use the default label, “page N”

Returns:

  • (Integer)

    the index (starting from 0) of the prepended page in the notebook, or -1 if function fails

#prepend_page_menu(child, tab_label, menu_label) ⇒ Integer

Prepends a page to notebook, specifying the widget to use as the label in the popup menu.

Parameters:

  • child (Gtk::Widget)

    the GtkWidget to use as the contents of the page

  • tab_label (Gtk::Widget)

    the GtkWidget to be used as the label for the page, or nil to use the default label, “page N”

  • menu_label (Gtk::Widget)

    the widget to use as a label for the page-switch menu, if that is enabled. If nil, and tab_label is a GtkLabel or nil, then the menu label will be a newly created label with the same text as tab_label; if tab_label is not a GtkLabel, menu_label must be specified if the page-switch menu is to be used.

Returns:

  • (Integer)

    the index (starting from 0) of the prepended page in the notebook, or -1 if function fails

#prev_pagenil

Switches to the previous page.

Nothing happens if the current page is the first page.

Returns:

  • (nil)

#remove_page(page_num) ⇒ nil

Removes a page from the notebook given its index in the notebook.

Parameters:

  • page_num (Integer)

    the index of a notebook page, starting from 0. If -1, the last page will be removed.

Returns:

  • (nil)

#reorder_child(child, position) ⇒ nil

Reorders the page containing child, so that it appears in position position.

If position is greater than or equal to the number of children in the list or negative, child will be moved to the end of the list.

Parameters:

  • child (Gtk::Widget)

    the child to move

  • position (Integer)

    the new position, or -1 to move to the end

Returns:

  • (nil)

#scrollableBoolean

Returns whether the tab label area has arrows for scrolling.

Returns:

  • (Boolean)

    true if arrows for scrolling are present

#scrollable=(scrollable) ⇒ Boolean

If true, scroll arrows are added if there are too many pages to fit.

Parameters:

  • scrollable (Boolean)

Returns:

  • (Boolean)

    scrollable

  • (Boolean)

    scrollable

#scrollable?Boolean

If true, scroll arrows are added if there are too many pages to fit.

Returns:

  • (Boolean)

    scrollable

#set_action_widget(widget, pack_type) ⇒ nil

Sets widget as one of the action widgets.

Depending on the pack type the widget will be placed before or after the tabs. You can use a GtkBox if you need to pack more than one widget on the same side.

Parameters:

Returns:

  • (nil)

#set_menu_label(child, menu_label) ⇒ nil

Changes the menu label for the page containing child.

Parameters:

Returns:

  • (nil)

#set_menu_label_text(child, menu_text) ⇒ nil

Creates a new label and sets it as the menu label of child.

Parameters:

  • child (Gtk::Widget)

    the child widget

  • menu_text (String)

    the label text

Returns:

  • (nil)

#set_tab_detachable(child, detachable) ⇒ nil

Sets whether the tab can be detached from notebook to another notebook or widget.

Note that two notebooks must share a common group identifier (see [methodGtk.Notebook.set_group_name]) to allow automatic tabs interchange between them.

If you want a widget to interact with a notebook through DnD (i.e.: accept dragged tabs from it) it must be set as a drop destination by adding to it a [classGtk.DropTarget] controller that accepts the GType GTK_TYPE_NOTEBOOK_PAGE. The :value of said drop target will be preloaded with a [classGtk.NotebookPage] object that corresponds to the dropped tab, so you can process the value via ::accept or ::drop signals.

Note that you should use [methodGtk.Notebook.detach_tab] instead of [methodGtk.Notebook.remove_page] if you want to remove the tab from the source notebook as part of accepting a drop. Otherwise, the source notebook will think that the dragged tab was removed from underneath the ongoing drag operation, and will initiate a drag cancel animation.

static void
on_drag_data_received (GtkWidget        *widget,
                       GdkDrop          *drop,
                       GtkSelectionData *data,
                       guint             time,
                       gpointer          user_data)
{
  GtkDrag *drag;
  GtkWidget *notebook;
  GtkWidget **child;

  drag = gtk_drop_get_drag (drop);
  notebook = g_object_get_data (drag, "gtk-notebook-drag-origin");
  child = (void*) gtk_selection_data_get_data (data);

  // process_widget (*child);

  gtk_notebook_detach_tab (GTK_NOTEBOOK (notebook), *child);
}

If you want a notebook to accept drags from other widgets, you will have to set your own DnD code to do it.

Parameters:

  • child (Gtk::Widget)

    a child GtkWidget

  • detachable (Boolean)

    whether the tab is detachable or not

Returns:

  • (nil)

#set_tab_label(child, tab_label) ⇒ nil

Changes the tab label for child.

If nil is specified for tab_label, then the page will have the label “page N”.

Parameters:

  • child (Gtk::Widget)

    the page

  • tab_label (Gtk::Widget)

    the tab label widget to use, or nil for default tab label

Returns:

  • (nil)

#set_tab_label_text(child, tab_text) ⇒ nil

Creates a new label and sets it as the tab label for the page containing child.

Parameters:

  • child (Gtk::Widget)

    the page

  • tab_text (String)

    the label text

Returns:

  • (nil)

#set_tab_reorderable(child, reorderable) ⇒ nil

Sets whether the notebook tab can be reordered via drag and drop or not.

Parameters:

  • child (Gtk::Widget)

    a child GtkWidget

  • reorderable (Boolean)

    whether the tab is reorderable or not

Returns:

  • (nil)

#show_borderBoolean

Returns whether a bevel will be drawn around the notebook pages.

Returns:

  • (Boolean)

    true if the bevel is drawn

#show_border=(show_border) ⇒ Boolean

Whether the border should be shown.

Parameters:

  • show_border (Boolean)

Returns:

  • (Boolean)

    show-border

  • (Boolean)

    show-border

#show_border?Boolean

Whether the border should be shown.

Returns:

  • (Boolean)

    show-border

#show_tabsBoolean

Returns whether the tabs of the notebook are shown.

Returns:

  • (Boolean)

    true if the tabs are shown

#show_tabs=(show_tabs) ⇒ Boolean

Whether tabs should be shown.

Parameters:

  • show_tabs (Boolean)

Returns:

  • (Boolean)

    show-tabs

  • (Boolean)

    show-tabs

#show_tabs?Boolean

Whether tabs should be shown.

Returns:

  • (Boolean)

    show-tabs

#tab_posGtk::PositionType

Which side of the notebook holds the tabs.

Returns:

#tab_pos=(tab_pos) ⇒ Gtk::PositionType

Which side of the notebook holds the tabs.

Parameters:

Returns: