年末年始の空き時間を使って覚書ページまとめと Tips 追加をやっている。
ついでに HDD 内の古いサンプルコードを整理。
ほぼ PyGtk コード、ほとんどもう役に立たない…
GTK3 には Pixmap なんてもう無いっつーのとか。
千以上あったけどやっと 2/3 くらいに減らした。
でも中には今まで気がつかなかったコードも見つかる。
多重起動防止でアプリを最前面にもっていく方法が今まで解らなかった。
gtk_window_activate_focus ()
gtk_window_present ()
どうやらこの二つを呼べばアクティブ化できるようだ。
以前書いた GtkApplication を使った Vala コードを書き換えて実験。
using Gtk;
/*
* Prevent multiple window Sample
**/
public class TestWin : Window {
private Notebook note;
private TextView[] view;
public TestWin ( Gtk.Application app ) {
this.set_application ( app );
this.set_title( "TestWin" );
note = new Notebook ();
this.add ( note );
this.resize ( 320, 240 );
this.show_all ();
}
public void CreateNew () {
var tab_label = new Label ( "new.txt" );
var text_view = new TextView ();
view += text_view;
note.append_page (text_view, tab_label);
this.show_all ();
// New Page Activate
note.set_current_page ( view.length - 1 );
}
public void CreateTab ( File[] files ) {
foreach ( var file in files ) {
var tab_label = new Label( file.get_basename () );
var text_view = new TextView ();
view += text_view;
note.append_page(text_view, tab_label);
}
this.show_all();
// Last Page Activate
note.set_current_page ( view.length - 1 );
}
}
public class App : Gtk.Application {
private TestWin win = null;
public App () {
Object (application_id:"apps.test.helloworld", flags:ApplicationFlags.HANDLES_OPEN );
}
public override void activate () {
if ( win == null ) {
win = new TestWin( this );
}
win.CreateNew ();
// SetForegroundWindow
win.activate_focus();
win.present();
}
public override void open ( File[] files, string hint ) {
if ( win == null ) {
win = new TestWin( this );
}
win.CreateTab ( files );
// SetForegroundWindow
win.activate_focus();
win.present();
}
}
public class Main {
public static int main ( string[] args ) {
Gtk.init ( ref args );
var app = new App ();
app.run ( args );
return 0;
}
}
ついでに追加タブもアクティブにするコードも入れてみた。
うん、今のところこれでいけるようだ。
多重起動防止アプリ以外ではどうでもいいことなんだけどね。
コンパイルするの面倒くさいよ、早く PyGI で使えるようになってくれ。


