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


// コールバック関数を使うための準備
#include "modclbk.as"
    newclbk3 cb_win_delete_event, 3, *on_win_delete_event, CLBKMODE_CDECL@
    newclbk3 cb_cmb1_changed, 2, *on_cmb1_changed, CLBKMODE_CDECL@
    newclbk3 cb_cmb2_changed, 2, *on_cmb2_changed, CLBKMODE_CDECL@
    newclbk3 cb_cmb3_changed, 2, *on_cmb3_changed, CLBKMODE_CDECL@
    newclbk3 cb_cmb4_changed, 2, *on_cmb4_changed, CLBKMODE_CDECL@
    newclbk3 cb_cmb5_changed, 2, *on_cmb5_changed, 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_vbox_new "gtk_vbox_new" int, int
#func global gtk_box_pack_start "gtk_box_pack_start" sptr, sptr, int, int, int

#func global gtk_list_store_new1 "gtk_list_store_new" int, int
#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_set1 "gtk_list_store_set" sptr, sptr, int, sptr, int
#func global gtk_list_store_set2 "gtk_list_store_set" sptr, sptr, int, sptr, int, sptr, int
#func global gtk_tree_model_get1 "gtk_tree_model_get" sptr, sptr, int, sptr, int

#func global gtk_combo_box_text_new "gtk_combo_box_text_new"
#func global gtk_combo_box_text_new_with_entry "gtk_combo_box_text_new_with_entry"
#func global gtk_combo_box_text_append "gtk_combo_box_text_append" sptr, str, str
#func global gtk_combo_box_text_append_text "gtk_combo_box_text_append_text" sptr, str
#func global gtk_combo_box_text_get_active_text "gtk_combo_box_text_get_active_text" sptr
#func global gtk_combo_box_new_with_model "gtk_combo_box_new_with_model" sptr
#func global gtk_combo_box_new_with_model_and_entry "gtk_combo_box_new_with_model_and_entry" sptr
#func global gtk_combo_box_set_active "gtk_combo_box_set_active" sptr, int
#func global gtk_combo_box_set_entry_text_column "gtk_combo_box_set_entry_text_column" sptr, int
#func global gtk_combo_box_get_active "gtk_combo_box_get_active" sptr
#func global gtk_combo_box_get_active_id "gtk_combo_box_get_active_id" sptr
#func global gtk_combo_box_get_active_iter "gtk_combo_box_get_active_iter" sptr, sptr
#func global gtk_combo_box_get_model "gtk_combo_box_get_model" sptr
#func global gtk_combo_box_set_wrap_width "gtk_combo_box_set_wrap_width" sptr, int
#func global gtk_combo_box_get_wrap_width "gtk_combo_box_get_wrap_width" sptr
#func global gtk_cell_layout_pack_start "gtk_cell_layout_pack_start" sptr, sptr, int
#func global gtk_cell_renderer_text_new "gtk_cell_renderer_text_new"
#func global gtk_cell_renderer_pixbuf_new "gtk_cell_renderer_pixbuf_new"
#func global gtk_cell_layout_add_attribute "gtk_cell_layout_add_attribute" sptr, sptr, str, 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

#uselib "libgdk_pixbuf-2.0-0.dll"
#func global gdk_pixbuf_get_type "gdk_pixbuf_get_type"
#func global gdk_pixbuf_new_from_file "gdk_pixbuf_new_from_file" str, sptr

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

// よく使う定数
; ヌルポインタ
#const NULL 0
; 真偽値
#const FALSE 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

    // VBox生成
    gtk_vbox_new FALSE, 5
    vbx = stat

    // コンボボックス1(テキスト専用)生成
    gtk_combo_box_text_new
    cmb1 = stat
    gtk_combo_box_text_append cmb1, "1", "item1(1)"
    gtk_combo_box_text_append cmb1, "2", "item2(1)"
    gtk_combo_box_set_active cmb1, 0
    g_signal_connect cmb1, "changed", cb_cmb1_changed, NULL

    // コンボボックス2(テキスト専用、エントリ付き)生成
    gtk_combo_box_text_new_with_entry
    cmb2 = stat
    gtk_combo_box_text_append_text cmb2, "item1(2)"
    gtk_combo_box_text_append_text cmb2, "item2(2)"
    gtk_combo_box_set_active cmb2, 0
    g_signal_connect cmb2, "changed", cb_cmb2_changed, NULL

    // TreeIter格納用変数作成
    sdim struct_itr, ( 4 * 4 ) ; 4*4 = GtkTreeIter構造体サイズ
    itr = varptr( struct_itr )

    // コンボボックス用セルレンダラ群生成
    gtk_cell_renderer_text_new
    ren3 = stat
    gtk_cell_renderer_pixbuf_new
    ren5_p = stat
    gtk_cell_renderer_text_new
    ren5_t = stat

    // コンボボックス3用モデル(データ)生成
#define G_TYPE_STRING G_TYPE_MAKE_FUNDAMENTAL(16) ; GObject - Type Information
#define ctype G_TYPE_MAKE_FUNDAMENTAL(%1) (%1 << 2)
#const COL_MODEL3_TEXT 0
    gtk_list_store_new1 1, G_TYPE_STRING
    mdl3 = stat
    gtk_list_store_append mdl3, itr
    gtk_list_store_set1 mdl3, itr, COL_MODEL3_TEXT, "item1(3)", -1
    gtk_list_store_append mdl3, itr
    gtk_list_store_set1 mdl3, itr, COL_MODEL3_TEXT, "item2(3)", -1

    // コンボボックス3生成
    gtk_combo_box_new_with_model mdl3
    cmb3 = stat
    gtk_cell_layout_pack_start cmb3, ren3, 0
    gtk_cell_layout_add_attribute cmb3, ren3, "text", COL_MODEL3_TEXT
    gtk_combo_box_set_active cmb3, 0
    g_signal_connect cmb3, "changed", cb_cmb3_changed, NULL

    // コンボボックス4用モデル(データ)生成
#const COL_MODEL4_TEXT 0
    gtk_list_store_new1 1, G_TYPE_STRING
    mdl4 = stat
    gtk_list_store_append mdl4, itr
    gtk_list_store_set1 mdl4, itr, COL_MODEL4_TEXT, "item1(4)", -1
    gtk_list_store_append mdl4, itr
    gtk_list_store_set1 mdl4, itr, COL_MODEL4_TEXT, "item2(4)", -1

    // コンボボックス4(エントリ付き)生成
    gtk_combo_box_new_with_model_and_entry mdl4
    cmb4 = stat
    gtk_combo_box_set_entry_text_column cmb4, COL_MODEL4_TEXT
    gtk_combo_box_set_active cmb4, 0
    g_signal_connect cmb4, "changed", cb_cmb4_changed, NULL

    // コンボボックス5用モデル(データ)生成
#const COL_MODEL5_PIXBUF 0
#const COL_MODEL5_TEXT 1
    gdk_pixbuf_get_type
    gtk_list_store_new2 2, stat, G_TYPE_STRING
    mdl5 = stat
    gdk_pixbuf_new_from_file "cat_s.jpg", varptr( p_error )
    pix = stat
    gtk_list_store_append mdl5, itr
    gtk_list_store_set2 mdl5, itr, COL_MODEL5_PIXBUF, pix, COL_MODEL5_TEXT, "item1(5)", -1
    gtk_list_store_append mdl5, itr
    gtk_list_store_set2 mdl5, itr, COL_MODEL5_PIXBUF, pix, COL_MODEL5_TEXT, "item2(5)", -1

    // コンボボックス5生成
    gtk_combo_box_new_with_model mdl5
    cmb5 = stat
    gtk_cell_layout_pack_start cmb5, ren5_p, 0
    gtk_cell_layout_add_attribute cmb5, ren5_p, "pixbuf", COL_MODEL5_PIXBUF
    gtk_cell_layout_pack_start cmb5, ren5_t, 0
    gtk_cell_layout_add_attribute cmb5, ren5_t, "text", COL_MODEL5_TEXT
;   gtk_combo_box_set_wrap_width cmb5, 2
    gtk_combo_box_set_active cmb5, 0
    g_signal_connect cmb5, "changed", cb_cmb5_changed, NULL

    // ウィンドウの組み立て
    gtk_box_pack_start vbx, cmb1, TRUE, TRUE, 0
    gtk_box_pack_start vbx, cmb2, TRUE, TRUE, 0
    gtk_box_pack_start vbx, cmb3, TRUE, TRUE, 0
    gtk_box_pack_start vbx, cmb4, TRUE, TRUE, 0
    gtk_box_pack_start vbx, cmb5, TRUE, TRUE, 0
    gtk_container_add win, vbx

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

/* シグナルハンドラ */
#const VARTYPE_STRING 2

*on_win_delete_event
    gtk_main_quit
    return

#const LEN_COMBOBOX_ID 5
*on_cmb1_changed
    gtk_combo_box_get_active_id cmb1
    ptr = stat
    dupptr txt, ptr, LEN_COMBOBOX_ID, VARTYPE_STRING
    mes "active id: " + txt

    gtk_combo_box_text_get_active_text cmb1
    ptr = stat
    gosub *print_active_text
    return

*on_cmb2_changed
    gtk_combo_box_get_active cmb2
    mes "active index: " + stat

    gtk_combo_box_text_get_active_text cmb2
    ptr = stat
    gosub *print_active_text
    return

*on_cmb3_changed
    gtk_combo_box_get_active cmb3
    mes "active index: " + stat

    ptr = NULL
    gtk_combo_box_get_active_iter cmb3, itr
    gtk_tree_model_get1 mdl3, itr, COL_MODEL3_TEXT, varptr( ptr ), -1
    gosub *print_active_text
    return

*on_cmb4_changed
    gtk_combo_box_get_active cmb4
    mes "active index: " + stat

    ptr = NULL
    gtk_combo_box_get_active_iter cmb4, itr
    gtk_tree_model_get1 mdl4, itr, COL_MODEL4_TEXT, varptr( ptr ), -1
    gosub *print_active_text
    return

*on_cmb5_changed
    gtk_combo_box_get_active cmb5
    mes "active index: " + stat

    ptr = NULL
    gtk_combo_box_get_active_iter cmb5, itr
    gtk_tree_model_get1 mdl5, itr, COL_MODEL5_TEXT, varptr( ptr ), -1
    gosub *print_active_text
    return

#const LEN_COMBOBOX_TEXT 10
*print_active_text
    if ptr {
        dupptr txt, ptr, LEN_COMBOBOX_TEXT, VARTYPE_STRING
        mes "active text: " + txt
        g_free ptr
    }
    return

Prev - Table of contents - Next