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


// コールバック関数を使うための準備
#include "modclbk.as"
    newclbk3 cb_win_delete_event, 3, *on_win_delete_event, CLBKMODE_CDECL@
    newclbk3 cb_iview_selection_changed, 2, *on_iview_selection_changed, CLBKMODE_CDECL@
    newclbk3 cb_iview_item_activated, 3, *on_iview_item_activated, CLBKMODE_CDECL@
    newclbk3 cb_g_list_free_full, 1, *cb_g_list_free_full, 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_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_selection_mode "gtk_icon_view_set_selection_mode" sptr, int
#func global gtk_tree_path_to_string "gtk_tree_path_to_string" sptr
#func global gtk_tree_path_free "gtk_tree_path_free" sptr
#func global gtk_icon_view_get_selected_items "gtk_icon_view_get_selected_items" sptr

#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 "libglib-2.0-0.dll"
#func global g_list_length "g_list_length" sptr
#func global g_list_nth_data "g_list_nth_data"  sptr, int
#func global g_list_free_full "g_list_free_full" sptr, sptr
#func global g_free "g_free" sptr


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

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

    // 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 )

    ; GtkListStore生成
#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
    iconnames = GTK_STOCK_CUT, GTK_STOCK_COPY, GTK_STOCK_PASTE
    repeat length( iconnames )
        gtk_list_store_append model, itr
        gtk_image_new
        gtk_widget_render_icon_pixbuf stat, iconnames( cnt ), GTK_ICON_SIZE_DND
        pixbuf = stat
        gtk_list_store_set2 model, itr, COLUMN_ICON, pixbuf, COLUMN_NAME, iconnames( cnt ), -1
        g_object_unref pixbuf
    loop

    // アイコンビュー生成
#const GTK_SELECTION_MULTIPLE 3 ; GtkSelectionMode
    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_selection_mode iview, GTK_SELECTION_MULTIPLE
    g_signal_connect iview, "selection-changed", cb_iview_selection_changed, NULL
    g_signal_connect iview, "item-activated", cb_iview_item_activated, NULL

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

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

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

*on_iview_selection_changed
    gtk_icon_view_get_selected_items iview
    treepaths = stat
    g_list_length treepaths
    repeat stat
        g_list_nth_data treepaths, cnt
        treepath = stat
        gtk_tree_path_to_string treepath
        ptr = stat
        dupptr str_tp, ptr, 10, 2
        mes "selected: " + str_tp
        g_free ptr
    loop
    g_list_free_full treepaths, cb_g_list_free_full
    return

*on_iview_item_activated
    clbkargprotect args_
    gtk_tree_path_to_string args_( 1 )
    ptr = stat
    dupptr str_tp, ptr, 10, 2
    mes "activated: " + str_tp
    g_free ptr
    gosub *on_iview_selection_changed
    return

/* GList開放用コールバック関数 */
*cb_g_list_free_full
    clbkargprotect args_
    gtk_tree_path_free args_( 0 )
    return

Prev - Table of contents - Next