Glade GtkHeaderBar, add two children on both sides? - gtk

I have searched existing questions, and the answer said change the pack type but the answer did not give details. I could add a button and change its pack type to "End", but if I try to add another child to set its pack type to "Start", I get the following error message that it needs some sort of placeholder. How do I do that? How do I add, in the example below, add another button on the left side of the header bar?
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow">
<property name="can-focus">False</property>
<property name="default-width">440</property>
<child>
<placeholder/>
</child>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="title" translatable="yes">Damn complex</property>
<property name="subtitle" translatable="yes">why</property>
<property name="show-close-button">True</property>
<child>
<object class="GtkButton">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="pack-type">end</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
PS: I could achieve that by adding children programmatically in C#, but I still have not found a way to do this within Glade.
private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
{
builder.Autoconnect(this);
DeleteEvent += Window_DeleteEvent;
var btn1 = new Button() { Label = "Left"};
var btn2 = new Button() { Label = "Right" };
header1.PackStart(btn1);
header1.PackEnd(btn2);
header1.ShowAll();
}

I found a way. Change "Number of Items" to 2, and then add 2 buttons. Change each button's pack type.

Related

Set gtk property using vala

I am trying to make my first app in vala. I made a ui in glade with a headerbar, now I want the headerbar to show the window controls. I searched how to set a gtk property with vala but couldn't really find an answer. I could find the property which I needed to set but not how to set it in vala. The current code of my main application in vala now is this:
using Gtk;
public static int main(string[] args){
Gtk.init (ref args);
try{
var builder = new Builder();
builder.add_from_file ("Headerbar.ui");
builder.connect_signals (null);
var window = builder.get_object ("main_window") as Window;
var headerbar = builder.get_object ("headerbar") as Gtk.HeaderBar;
headerbar.set_show_close_button(true);
window.show_all ();
Gtk.main ();
} catch (Error e){
stderr.printf ("Kon ui niet laden: %s\n", e.message);
return 1;
}
return 0;
}
The code of my ui file is this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkApplicationWindow" id="main_window">
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="sayhello">
<property name="label" translatable="yes">Say hello! </property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
</child>
</object>
</child>
When i compile the code it does not give any errors. But when i run the application, the window controls aren't showed in the header bar and i get the following error in the terminal:
(main:15420): Gtk-CRITICAL **: gtk_header_bar_set_show_close_button: assertion 'GTK_IS_HEADER_BAR (bar)' failed.
How can i properly set the property so that the headerbar shows the window controls.

no method named `connect_activate` when using the gtk crate

I am trying to write simple GTK application in Rust, but faced with problem that I cannot add signal to menu item. There is simplified code to reproduce problem:
Glade file ("interface.glade"):
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkMenuBar" id="menubar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">File</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="FileMenu">
<property name="label">gtk-new</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>
Rust code ("main.rs"):
extern crate gtk;
mod example {
use gtk;
use gtk::traits::*;
use gtk::signal::Inhibit;
use gtk::widgets::{
Builder,
MenuItem
};
use gtk::Window;
pub fn main() {
gtk::init().unwrap_or_else(|_| panic!("Failed to initialize GTK."));
let builder = Builder::new_from_file("./interface.glade").unwrap();
let window: Window = builder.get_object("window1").unwrap();
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(true)
});
let file_menu: MenuItem = builder.get_object("FileMenu").unwrap();
file_menu.connect_activate(|_| {
println!("Activated");
});
window.show_all();
gtk::main();
}
}
fn main() {
example::main()
}
And when I try to compile it, I get an error:
src/main.rs:24:19: 26:11 error: no method named `connect_activate` found for type `gtk::widgets::menu_item::MenuItem` in the current scope
src/main.rs:24 file_menu.connect_activate(|_| {
src/main.rs:25 println!("Activated");
src/main.rs:26 });
src/main.rs:24:19: 26:11 note: the method `connect_activate` exists but the following trait bounds were not satisfied: `gtk::widgets::menu_item::MenuItem : gtk::traits::button::ButtonTrait`
Am I doing something wrong?
Am I doing something wrong?
Yep! You aren't reading and addressing the error message:
the method connect_activate exists but the following trait bounds were not satisfied: gtk::widgets::menu_item::MenuItem : gtk::traits::button::ButtonTrait
Granted, this error message is worded in an obtuse manner. It's saying that the type MenuItem does not implement the trait ButtonTrait. To be honest, this is the first time I've see this particular wording of the error message, so I might be a bit wrong. If you check out the documentation(1) for MenuItem though, you can see that it does not implement ButtonTrait. This precludes you from calling the method.
I don't know what a suitable workaround is. I don't see any examples that use MenuItem. The 3 linked example projects don't use it either. It's entirely possible that it simply hasn't had all of the appropriate traits implemented yet. Perhaps this would be a good chance for you to get some commits into an up-and-coming project! ^_^
I also couldn't find any fallback methods or traits that would allow you to call connect directly.
(1): I wish I could link to the docs, but there are no officially-hosted versions I can find.

Properly structure and highlight a GtkPopoverMenu using PyGObject

I am trying to make an example of a proper Gtk.HeaderBar with Gtk.PopoverMenus that shows how the different widgets are used. I looked at a lot of examples and code, but can't figure out, how to work the Gtk.ModelButton.
Especially this sentence makes no sense to me:
When the action is specified via the “action-name” and “action-target” properties, the role of the button (i.e. whether it is a plain, check or radio button) is determined by the type of the action and doesn't have to be explicitly specified with the “role” property.
In any case, here is my attempt to just use normal widgets in the PopoverMenu, leading to inconsistent highlighting (the whole row should be highlighted):
So my question is: How are ModelButtons subclassed, so they appear as proper PopoverMenu-widgets?
Here is the Python code:
from gi.repository import Gtk, Gio
class HeaderBarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="HeaderBar & PopoverMenu")
self.set_border_width(10)
self.set_default_size(400, 400)
builder = Gtk.Builder()
objects = builder.add_objects_from_file("popovermenu_layout.xml", ("pom_options", ""))
pom_opt = builder.get_object("pom_options")
builder.connect_signals(self)
hb = Gtk.HeaderBar()
hb.set_show_close_button(True)
hb.props.title = "HeaderBar & PopoverMenu"
self.set_titlebar(hb)
def on_click(button, popovermenu):
"""
Toggles the respective popovermenu.
"""
if popovermenu.get_visible():
popovermenu.hide()
else:
popovermenu.show_all()
button_opt = Gtk.Button()
icon = Gio.ThemedIcon(name="format-justify-fill-symbolic")
image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
button_opt.add(image)
hb.pack_end(button_opt)
pom_opt.set_relative_to(button_opt)
button_opt.connect("clicked", on_click, pom_opt)
self.add(Gtk.TextView())
def print_something(self, modelbutton, event):
print("you pressed a button")
def night_mode_switcher(self, switch, state):
Gtk.Settings.get_default().set_property("gtk-application-prefer-dark-theme", state)
win = HeaderBarWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
And here is the model for the PopoverMenu:
<interface>
<object class="GtkPopoverMenu" id ="pom_options">
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="margin">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkModelButton" id="mb_print">
<property name="visible">True</property>
<property name="text" translatable="yes">Print</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="button-press-event" handler="print_something" swapped="no"/>
</object>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">checkbutton</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
</child>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSwitch" id="switch1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="state-set" handler="night_mode_switcher" swapped="no"/>
<property name="margin_start">9</property>
<property name="margin_end">9</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkModelButton" id="mb_night">
<property name="text" translatable="yes">Night Mode</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_start">9</property>
<property name="margin_end">9</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
I managed to solve most of what my question was. Most importantly it seems that one has to use Gtk.Application and Gtk.ApplicationWindow. I can't say that I fully understand why, but that makes it possible to add Actions in the form of Gio.SimpleActions to the application. Gtk.Builder parses the XML of the menu, and one can simply add it tot the menu using the set_popover-method. Then for each action defined in the XML (don't forget the app.-prefix in the XML) a Gio.SimpleAction is created (without the app.-prefix as name) in the Python-code and added to the application. I got a normal button, and a checkbutton to work. Still struggling with the radiobutton, but that might be another question.
Here is the Python-code:
from gi.repository import Gio, Gtk, GLib
import sys
class MainApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self,
application_id="needs.dot",
flags=Gio.ApplicationFlags.FLAGS_NONE)
#https://developer.gnome.org/gio/unstable/GApplication.html#g-application-id-is-valid
self.connect("activate", self.activate_window)
def activate_window(self, app):
"""
The activate signal of Gtk.Application passes the MainApplication class
to the window. The window is then set as a window of that class.
"""
self.window = Gtk.ApplicationWindow()
self.window.set_default_size(500, 400)
self.hb = Gtk.HeaderBar()
self.hb.set_show_close_button(True)
self.hb.props.title = "HeaderBar & PopOverMenu"
self.window.set_titlebar(self.hb)
button_settings = Gtk.MenuButton()
icon = Gio.ThemedIcon(name="format-justify-fill-symbolic")
image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
button_settings.add(image)
self.hb.pack_end(button_settings)
self.builder = Gtk.Builder()
self.builder.add_from_file("popovermenu_layout.xml")
pom_options = self.builder.get_object("pom_options")
button_settings.set_popover(pom_options)
#self.builder.connect_signals(self) #Not needed because of using actions?
app.add_window(self.window)
#Connects to the action. The first part of the XML name is left away:
#<property name="action-name">app.print</property>
#becomes simply "print".
action_print = Gio.SimpleAction.new("print", None)
action_print.connect("activate", self.print_something)
app.add_action(action_print)
#app.toggle becomes -> toggle
action_toggle = Gio.SimpleAction.new_stateful("toggle", None, GLib.Variant.new_boolean(False))
action_toggle.connect("change-state", self.toggle_toggled)
app.add_action(action_toggle)
btn = Gtk.Button("Button")
self.window.add(btn)
self.window.show_all()
def print_something(self, action, variable):
print("something")
def toggle_toggled(self, action, state):
action.set_state(state)
Gtk.Settings.get_default().set_property("gtk-application-prefer-dark-theme", state)
def on_action_quit_activated(self, action):
self.app.quit()
if __name__ == "__main__":
"""
Creates an instance of the MainApplication class that inherits from
Gtk.Application.
"""
app = MainApplication()
app.run(sys.argv)
and the XML-file for the menu:
<interface>
<object class="GtkPopoverMenu" id ="pom_options">
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="margin">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkModelButton" id="mb_print">
<property name="visible">True</property>
<property name="action-name">app.print</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="label" translatable="yes">Print</property>
</object>
</child>
<child>
<object class="GtkModelButton" id="mp_toggle">
<property name="visible">True</property>
<property name="action-name">app.toggle</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="label" translatable="yes">Night Mode</property>
</object>
</child>
</object>
</child>
</object>
</interface>

Gtk, custom modal dialog

I am trying to make sustom modal dialog with glade.
For this I use "dialog box" from "toplevels", set transient_for_window and modal variables properly. Then I put widgets to table all packed into dialog-vbox.
And this works OK, as expected.
But, when I need more complex GUI, like I need now, and pack into dialog-vbox scrolledwindow> viewport> aspectframe> table> and then place widgets in table this dialog becomes no more modal! After some playing I conclude that it is obvious that problems with modality starts with scrolledwindow.
Is it allowed to put such complex GUI in dialog? If yes - why my dialog lost modality with gtkscrollwindow, if no, how people do such tasks? In my case I'm trying to make modal dialog which can be connected to different projects (say reusable).
GUI:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkDialog" id="dialog1">
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="type_hint">dialog</property>
<property name="skip_taskbar_hint">True</property>
<child internal-child="vbox">
<object class="GtkVBox" id="dialog-vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkHButtonBox" id="dialog-action_area1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">OK</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<object class="GtkViewport" id="viewport1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkTable" id="table1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="n_rows">10</property>
<property name="n_columns">2</property>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
<property name="invisible_char_set">True</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="primary_icon_sensitive">True</property>
<property name="secondary_icon_sensitive">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="0">button1</action-widget>
<action-widget response="0">button2</action-widget>
</action-widgets>
</object>
</interface>
C:
int dlgmodal(GtkWidget *mainwindow, char* fmw)
{
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "external0.glade", NULL);
dialog1 = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
entry1 = GTK_WIDGET(gtk_builder_get_object(builder, "entry1"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(G_OBJECT(builder));
gtk_entry_set_text(GTK_ENTRY(entry1), fmw);
gtk_window_set_modal(GTK_WINDOW(GTK_DIALOG(dialog1)), TRUE);
gtk_window_set_transient_for(GTK_WINDOW(GTK_DIALOG(dialog1)), GTK_WINDOW(mainwindow));
//
int x, y;
gdk_window_get_origin(GDK_WINDOW(GTK_WIDGET(mainwindow)->window), &x, &y);
gtk_window_move(GTK_WINDOW(dialog1), x+8, y);
//
int dlgresponse;
dlgresponse = gtk_dialog_run(GTK_DIALOG(dialog1));
gtk_widget_destroy(GTK_WIDGET(GTK_DIALOG(dialog1)));
return dlgresponse;
}

Why does Gtk2::Builder mix up my signals?

I'm having this perl code:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use Gtk2 '-init';
my $data;
my $builder_file = "lists.glade";
my $builder = Gtk2::Builder->new();
$builder->add_from_file( $builder_file )
or die "Couldn't read $builder_file";
$builder->connect_signals( undef );
my $window = $builder->get_object( "top_window" )
or die "Error while setting up GUI";
$window->show_all();
# Dealloc builder
$builder = undef;
Gtk2->main();
sub top_window_destroy_cb
{
Gtk2->main_quit();
}
sub on_radiobutton1_group_changed
{
my ($self, $choice) = #_;
print Dumper($self, $choice, $choice->get_text());
}
and this builder file:
<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="top_window">
<signal name="destroy" handler="top_window_destroy_cb"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkVButtonBox" id="vbuttonbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkRadioButton" id="radiobutton1">
<property name="label" translatable="yes">Pera</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<signal name="group_changed" handler="on_radiobutton1_group_changed" object="choice"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobutton2">
<property name="label" translatable="yes">Mika</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<property name="group">radiobutton1</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="choice">
<property name="visible">True</property>
<property name="label" translatable="yes">Choice</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
But it falsely connects "on_radiobutton1_group_changed" with "destroy" signal from "top_window". I know this because it executes "print Dumper..." statement only when closing window. Does anyone has idea what I'm doing wrong?
The "group-changed" signal
Emitted when the group of radio buttons that a radio button belongs to changes. This is emitted when a radio button switches from being alone to being part of a group of 2 or more buttons, or vice-versa, and when a button is moved from one group of 2 or more buttons to a different one, but not when the composition of the group that a button belongs to changes.
It really doesn't sound like what you want. I'm guessing it is called on destruction of the window.
I added the following sub
sub on_radiobutton_toggled {
my $self = shift;
if ($self->get_active) {
print Dumper($self, $self->get_label);
}
}
and set the each radiobutton's toggled signal (under GtkToggleButton) to on_radiobutton_toggled, which now does what you want.