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


// コールバック関数を使うための準備
#include "modclbk.as"
    newclbk3 cb_window_delete_event, 3, *on_window_delete_event, CLBKMODE_CDECL@
    newclbk3 cb_cbtn1_toggled, 2, *on_cbtn1_toggled, CLBKMODE_CDECL@
    newclbk3 cb_cbtn2_toggled, 2, *on_cbtn2_toggled, 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_container_set_border_width "gtk_container_set_border_width" sptr, int
#func global gtk_container_add "gtk_container_add" sptr, sptr
#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_box_new "gtk_box_new" int, int
#func global gtk_box_pack_start "gtk_box_pack_start" sptr, sptr, int, int, int
#func global gtk_adjustment_new "gtk_adjustment_new" double, double, double, double, double, double
#func global gtk_spin_button_new "gtk_spin_button_new" sptr, double, int
#func global gtk_spin_button_set_numeric "gtk_spin_button_set_numeric" sptr, int
#func global gtk_spin_button_set_update_policy "gtk_spin_button_set_update_policy" sptr, int
#func global gtk_check_button_new_with_label "gtk_check_button_new_with_label" sptr
#func global gtk_toggle_button_get_active "gtk_toggle_button_get_active" 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

// よく使う定数
; ヌルポインタ
#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
    gtk_container_set_border_width win, 10
    g_signal_connect win, "delete-event", cb_window_delete_event, NULL

    // Box生成
#const GTK_ORIENTATION_HORIZONTAL 0 ; GtkOrientation
    gtk_box_new GTK_ORIENTATION_HORIZONTAL, 6
    hbox = stat

    // ボタン群生成
    gtk_adjustment_new 0, 0, 1000, 1, 10, 0
    gtk_spin_button_new stat, 1, 0
    sbtn = stat

    gtk_check_button_new_with_label "Numeric"
    cbtn1 = stat
    g_signal_connect cbtn1, "toggled", cb_cbtn1_toggled, sbtn

    gtk_check_button_new_with_label "If valid"
    cbtn2 = stat
    g_signal_connect cbtn2, "toggled", cb_cbtn2_toggled, sbtn

    // ウィンドウの組み立て
    gtk_box_pack_start hbox, sbtn, TRUE, TRUE, 0
    gtk_box_pack_start hbox, cbtn1, TRUE, TRUE, 0
    gtk_box_pack_start hbox, cbtn2, TRUE, TRUE, 0
    gtk_container_add win, hbox

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

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

*on_cbtn1_toggled
    clbkargprotect args_

    gtk_toggle_button_get_active args_( 0 )
    gtk_spin_button_set_numeric args_( 1 ), stat
    return

#enum GTK_UPDATE_ALWAYS = 0 ; GtkSpinButtonUpdatePolicy
#enum GTK_UPDATE_IF_VALID
*on_cbtn2_toggled
    clbkargprotect args_

    gtk_toggle_button_get_active args_( 0 )
    if stat {
        policy = GTK_UPDATE_IF_VALID
    }
    else {
        policy = GTK_UPDATE_ALWAYS
    }
    gtk_spin_button_set_update_policy args_( 1 ), policy
    return

Prev - Table of contents - Next