18.2.1 サンプルプログラムの全体


// コールバック関数を使うための準備
#include "modclbk.as"
    newclbk3 cb_win_delete_event, 3, *on_win_delete_event, CLBKMODE_CDECL@
    newclbk3 cb_model_row_deleted, 3, *on_model_row_deleted, CLBKMODE_CDECL@
    newclbk3 cb_model_row_inserted, 4, *on_model_row_inserted, CLBKMODE_CDECL@

// GTK+の関数を使うための準備
#uselib "libgtk-3-0.dll"
#func global gtk_init "gtk_init" sptr, sptr
#func global gtk_window_new "gtk_window_new" int
#func global gtk_widget_show_all "gtk_widget_show_all" sptr
#func global gtk_main "gtk_main"
#func global gtk_main_quit "gtk_main_quit"
#func global gtk_container_add "gtk_container_add" sptr, sptr

#func global gtk_list_store_new2 "gtk_list_store_new" int, int, int
#func global gtk_list_store_append "gtk_list_store_append" sptr, sptr
#func global gtk_list_store_set2 "gtk_list_store_set" sptr, sptr, sptr, sptr, sptr, sptr, int
#func global gtk_tree_path_to_string "gtk_tree_path_to_string" sptr
#func global gtk_image_new "gtk_image_new"
#func global gtk_widget_render_icon_pixbuf "gtk_widget_render_icon_pixbuf" sptr, sptr, int
#func global gtk_icon_view_new_with_model "gtk_icon_view_new_with_model" sptr
#func global gtk_icon_view_set_pixbuf_column "gtk_icon_view_set_pixbuf_column" sptr, int
#func global gtk_icon_view_set_text_column "gtk_icon_view_set_text_column" sptr, int
#func global gtk_icon_view_set_reorderable "gtk_icon_view_set_reorderable" sptr, int

#uselib "libgobject-2.0-0.dll"
#define g_signal_connect(%1, %2, %3, %4) g_signal_connect_data %1, %2, %3, %4, 0, 0
#func global g_signal_connect_data "g_signal_connect_data" sptr, str, sptr, sptr, int, int
#func global g_object_unref "g_object_unref" sptr

#uselib "libgdk_pixbuf-2.0-0.dll"
#func global gdk_pixbuf_get_type "gdk_pixbuf_get_type"

#uselib "libglib-2.0-0.dll"
#func global g_free "g_free" sptr

// よく使う定数
#const NULL 0 ; ヌルポインタ
#const TRUE 1 ; 真

    // GTK+初期化
    gtk_init NULL, NULL

    // ウィンドウ生成
#const GTK_WINDOW_TOPLEVEL 0 ; GtkWindowType
    gtk_window_new GTK_WINDOW_TOPLEVEL
    win = stat
    g_signal_connect win, "delete-event", cb_win_delete_event, NULL

    // アイコンビュー用データ
    ; GtkTreeIter格納用変数作成
    sdim struct_itr, ( 4 * 4 ) ; 4*4 = GtkTreeIter構造体サイズ
    itr = varptr( struct_itr )

    ; モデル生成
#define G_TYPE_STRING G_TYPE_MAKE_FUNDAMENTAL(16) ; GObject - Type Information
#define ctype G_TYPE_MAKE_FUNDAMENTAL(%1) (%1 << 2)
    gdk_pixbuf_get_type
    gtk_list_store_new2 2, stat, G_TYPE_STRING
    model = stat

    ; GtkListStoreにデータをセット
#define GTK_STOCK_CUT "gtk-cut" ; GtkStockItem
#define GTK_STOCK_COPY "gtk-copy"
#define GTK_STOCK_PASTE "gtk-paste"
#const GTK_ICON_SIZE_DND 5 ; GtkIconSize
#const COLUMN_ICON 0 ; GtkListStoreデータの項目インデックス
#const COLUMN_NAME 1
    icons = GTK_STOCK_CUT, GTK_STOCK_COPY, GTK_STOCK_PASTE
    repeat length( icons )
        gtk_list_store_append model, itr
        gtk_image_new
        gtk_widget_render_icon_pixbuf stat, icons( cnt ), GTK_ICON_SIZE_DND
        pixbuf = stat
        gtk_list_store_set2 model, itr, COLUMN_ICON, pixbuf, COLUMN_NAME, icons( cnt ), -1
        g_object_unref pixbuf
    loop

    ; GtkListStoreにシグナルハンドラを設定
    g_signal_connect model, "row-deleted", cb_model_row_deleted, NULL
    g_signal_connect model, "row-inserted", cb_model_row_inserted, NULL

    // アイコンビュー生成
    gtk_icon_view_new_with_model model
    iview = stat
    gtk_icon_view_set_pixbuf_column iview, COLUMN_ICON
    gtk_icon_view_set_text_column iview, COLUMN_NAME
    gtk_icon_view_set_reorderable iview, TRUE

    // ウィンドウの組み立て
    gtk_container_add win, iview

    // ウィンドウの表示とメインループの開始
    gtk_widget_show_all win
    gtk_main
    end

/* シグナルハンドラ */
*on_win_delete_event
    gtk_main_quit
    return

*on_model_row_deleted
    mes "on_model_row_deleted"
    clbkargprotect args_
    tp = args_( 1 )
    gosub *mes_tree_path
    return

*on_model_row_inserted
    mes "on_model_row_inserted"
    clbkargprotect args_
    tp = args_( 1 )
    gosub *mes_tree_path
    return

/* サブルーチン */
*mes_tree_path
    gtk_tree_path_to_string tp
    ptr = stat
    dupptr str_tp, ptr, 10, 2
    mes "TreePath: " + str_tp
    g_free ptr
    return

Prev - Table of contents - Next