簡體   English   中英

在 GTK 中保存/恢復 window position

[英]Save/restore window position in GTK

我想在程序啟動之間保存/恢復 window position。 window 可以最大化。 這也應該保存。 問題是在最大化(縮放)時保存 window position。

所以細節(我不關心保存,這是非常簡單的任務)。 我需要獲取 window 正常 state 的 x、y、寬度、高度的方法,並標記 window 是否最大化。 不幸的是 gdk_window_get_size/gdk_window_get_position 返回實際的 window 位置。

在 Windows 下使用 GetWindowPlacement 和 rcNormalPosition 可以輕松解決此問題。 但我需要 Mac OS X Cocoa(至少)或純 GTK 的解決方案。

為此,我所做的是使用在調整大小時發出 Gtk.Window 的信號,通過 gsettings 我保存值並恢復它以以相同的方式啟動應用程序。 這是 vala + GTK+ 中的一個示例:

using Gtk;
using GLib;

public class Prueba : Window {

    public void on_resize (Window win)
    {
        int width;
        int height;

        win.get_size (out width, out height);

        /* GSETTINGS CONFIG */
        string settings_dir = Path.get_dirname (".");

        SettingsSchemaSource sss = new SettingsSchemaSource.from_directory (settings_dir, null, false);
        SettingsSchema schema = sss.lookup ("apps.test-gs", false);     

        if (sss.lookup == null)
        {
            stdout.printf ("GLIB.GIO.SCHEMA: ID not found.");
        }

        GLib.Settings config = new GLib.Settings.full (schema, null, null);
        /* GSETTINGS FIN */

        stdout.printf ("RESOLUTION: %dx%d\n", width, height);

        config.set_int ("width", width);
        config.set_int ("height", height);
    }

    public void on_destroy (Window ventana)
    {

        stderr.printf ("APPLICATION EXIT!\n");
        Gtk.main_quit ();
    }

    public Prueba ()
    {
        this.title = "Prueba GTK+";

        string settings_dir = Path.get_dirname (".");
        SettingsSchemaSource sss = new SettingsSchemaSource.from_directory (settings_dir, null, false);
        SettingsSchema schema = sss.lookup ("apps.test-gs", false);

        if (sss.lookup == null)
        {
            stdout.printf ("GLIB.GIO.SCHEMA: ID not found.");
        }

        GLib.Settings settings = new GLib.Settings.full (schema, null, null);

        this.set_default_size (settings.get_int("width"), settings.get_int("height"));
        this.border_width = 10;
        this.window_position = WindowPosition.CENTER;

        Button b = new Button.with_label ("SALIR");

        b.clicked.connect (()=>{
                this.on_destroy (this);
            });

        this.add (b);
        this.show_all ();
    }

    public static int main (string[] args)
    {
        Gtk.init (ref args);

        Prueba app = new Prueba ();

        app.destroy.connect (()=> {

            app.on_destroy (app);

        });

        app.size_allocate.connect (()=>{
                app.on_resize (app);
        });

        Gtk.main ();
        return 0;
    }

}

和這個模式:

<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
    <schema id="apps.test-gs" path="/apps/test-gs/">
        <key type="b" name="check-test">
            <default>false</default>
            <summary>Check de mi app TEST</summary>
            <description>Es un ejemplo de BOOLEANO de mi APP TEST</description>
        </key>
        <key type="i" name="width">
            <default>300</default>
            <summary>Pixels del Ancho</summary>
            <description>Valor del Ancho del Programa TEST</description>
        </key>
        <key type="i" name="height">
            <default>100</default>
            <summary>Pixels del Alto</summary>
            <description>Valor del Alto del Programa TEST</description>
        </key>
    </schema>
</schemalist>

祝你好運!

Window Programming Guide 中的Saving Window Position中描述了 Cocoa* 解決方案。 讓您的應用程序委托執行以下操作:

[window setFrameAutosaveName:@"MyWindowFrame"];

使 window 在幀發生變化時自動將其幀保存到用戶默認值中。 然后在下次發射時,

[window setFrameUsingName:@"MyWindowFrame"];

window 知道它是否“縮放” (最大化):

[window isZoomed];

因此,您可以隨時獲取此標志以及框架:

// Gives you an NSRect in screen coordinates
[window frame];

根據文檔, isZoomed並通過windowWillUseStandardFrame:defaultFrame:與窗口的委托進行zoom檢查。 我不確定您想要 go 到 Cocoa 有多深,但這看起來像是您在評論中提到的情況的一個選項。 如果 window 是使用縮放幀創建的,我認為 window 代表 object 可以通過這種方法為幀提供合理的默認值。


*和Obj-C,因為我不知道C++。

我剛剛使用pygobject實現了這樣的事情; 它不是 C++ 但它仍然有用。

你可以在這里找到代碼。

我已經為 python GNOME 應用程序使用了 GNOME Builder 的默認模板,因此如果您使用它設置項目,它應該非常容易復制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM