简体   繁体   中英

How to get a locked maximized window in GTK?

I have a GtkApplication based program that must always be shown maximized. That is easily accomplished by using gtk_window_maximize . The problem is you can still double click or drag theGtkHeaderBar to disable the maximized state.

Is there a way to lock the maximized state or to disable the header behavior? Trying to use gtk_window_set_resizable effectively locks the window size down but it also disallows the maximization effect.

I'm using xfwm4 , the default XFCE window manager.

gtk_application_add_window(application, window);
gtk_window_maximize(window);

// Resizing instead of maximizing does not change anything: resetting
// the resizable property shrinks the window to its original size
//gtk_window_set_default_size(window, 1024, 768);
//gtk_window_resize(window, 1024, 768);
//gtk_widget_set_size_request(GTK_WIDGET(window), 1024, 768);

// The following line "demaximizes" the window
//gtk_window_set_resizable(window, FALSE);

// Using a different window type, as suggested by theGtknerd, does
// not help either. The following line makes it behave properly but
// disables the app menu (like any type other than NORMAL).
//gtk_window_set_type_hint(window, GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);

gtk_widget_show_all(GTK_WIDGET(window));

Addendum

I asked on the GTK+ forum and the suggestion I got was to use a fullscreen window for kiosk applications. I tried this approach (I had to rewrite some part of my code because in fullscreen the application headerbar disappears) and it works well.

The original question still stands though.

You may wish to check gtk-window-set-decorated . Note that window types are also useful. Due to your window manager and OS enviroment, there is no single best way to achieve what you are looking for, hence my vague response.

Edit :

You could set the size-request of the window to the current size after maximizing the window. This will block the user from making it smaller again.

And here is an MCVE in Python:

#!/usr/bin/env python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib

window = Gtk.Window()

def on_window_destroy(window):
    Gtk.main_quit()
window.connect('destroy', on_window_destroy)

def lock_window_size():
    size = window.get_allocated_size().allocation
    window.set_size_request(size.width, size.height)
GLib.timeout_add_seconds(1, lock_window_size) # allow for the delay of maximizing, etc

window.show_all()
window.maximize()
Gtk.main()

The issue can be mitigated by disabling some dangerous events, specifically the double click and the drag events on the titlebar. It is an ugly hack though and you can still move the window around by dragging the application menu (or any other widget inside the header bar).

static gboolean
disable_dangerous_events(GtkWidget *widget, GdkEvent *event)
{
    return
        event->type == GDK_MOTION_NOTIFY ||
        event->type == GDK_WINDOW_STATE ?
        GDK_EVENT_STOP : GDK_EVENT_PROPAGATE;
}


...
gtk_application_add_window(application, window);
gtk_window_maximize(window);
g_signal_connect(gs.gtk.window, "event",
                 G_CALLBACK(disable_dangerous_events), NULL);
gtk_widget_show_all(GTK_WIDGET(window));
...

You can still resize the window with shortcut keys but, being run on a touch system, this is not an issue.

您可以使用void gtk_window_fullscreen ()设置Fullscreen

gtk_window_fullscreen (GTK_WINDOW(gtk_widget_get_root_window (widget)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM