GTK+のウィンドウ位置の保存と復元のテストをVala言語に移植
以前「ウィンドウの位置とサイズを記憶し、壊さずに隠した後で再表示を行う」でPyGTKを用いて行ったテストをVala言語で書いたものを貼り付ける。
大まかな要領は同じだが、ソースの形としては見やすくなっているような気もする。
[任意]ファイル名: savegeometrytest.vala
using GLib; using Gtk; using Gdk; /* * valac --pkg gtk+-2.0 -o savegeometrytest savegeometrytest.vala */ namespace SaveGeometryTest { /* 座標 */ struct Pos { public int x; public int y; } /* サイズ */ struct Size { public int width; public int height; } class TestWindow : Gtk.Window { Pos pos; Size size; Gtk.CheckMenuItem item_win; Gtk.TextView textview; Gtk.ScrolledWindow sw; public TestWindow (Gtk.CheckMenuItem item_win) { this.item_win = item_win; this.title = "Test window"; this.set_size_request (300, 180); // 最小サイズ /* スクロールバーの付いたテキストビューを1つ配置 */ this.textview = new Gtk.TextView (); this.sw = new Gtk.ScrolledWindow (null, null); this.sw.add (this.textview); this.sw.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC); this.add (this.sw); /* シグナル */ this.delete_event += this.on_delete; /* 座標とサイズの記憶(out指定で代入される) */ this.get_position (out this.pos.x, out this.pos.y); // X座標とY座標 this.get_size (out this.size.width, out this.size.height); // 幅と高さ } /* 「閉じる」ボタンが押されたら座標とサイズを保存して隠す */ bool on_delete () { this.get_position (out this.pos.x, out this.pos.y); this.get_size (out this.size.width, out this.size.height); /* メニュー項目のチェックを外す */ this.item_win.active = false; /* 隠しつつ、trueを返して破壊を防ぐ */ return this.hide_on_delete (); // Gtk.Widgetクラスのメンバ関数 } /* 非表示なら位置とサイズを復元して表示する・表示中なら隠す */ public bool visible_restored // (書き込み専用)プロパティとして定義 { set // 代入時の処理(読み込み時の処理は定義しない) { if (value) // 値(引数)がtrueのとき { this.move (this.pos.x, this.pos.y); this.resize (this.size.width, this.size.height); this.show_all (); } else this.delete_event (new Gdk.Event (Gdk.EventType.DELETE)); } } } class TrayMenu : Gtk.Menu { Gtk.ImageMenuItem item_quit; Gtk.CheckMenuItem item_win; TestWindow win; public TrayMenu () { this.item_quit = new Gtk.ImageMenuItem.from_stock (Gtk.STOCK_QUIT, null); this.item_win = new Gtk.CheckMenuItem.with_mnemonic ("_Window"); this.prepend (this.item_quit); this.prepend (new Gtk.SeparatorMenuItem ()); this.prepend (this.item_win); this.win = new TestWindow (this.item_win); /* チェックの状態に応じてウィンドウの表示状態を変更 */ this.item_win.activate += (source) => { bool visible = source.active; this.win.visible_restored = visible; }; this.item_quit.activate += Gtk.main_quit; this.show_all (); } public void activate_item_win () { this.item_win.activate (); } } class MainClass { static TrayMenu menu; public static void main (string[] args) { Gtk.init (ref args); Gtk.StatusIcon trayicon = new Gtk.StatusIcon.from_stock (Gtk.STOCK_DIALOG_INFO); //trayicon.tooltip_text = "test"; // GTK+ 2.16以上 MainClass.menu = new TrayMenu (); /* メニューのポップアップ処理 */ trayicon.popup_menu += (source, button, activate_time) => { MainClass.menu.popup (null, null, source.position_menu, button, activate_time); }; /* トレイアイコンのクリックでウィンドウの表示/非表示のメニュー項目を実行 */ trayicon.activate += () => MainClass.menu.activate_item_win (); Gtk.main (); } } }
関連記事:
参考URL:
- Valadoc: Gtk.Window.get_position
- Valadoc: Gtk.Window.get_size
- Valadoc: Gtk.Window.move
- Valadoc: Gtk.Window.resize
使用したバージョン:
- Vala 0.7.4