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


// コールバック関数を使うための準備
#include "modclbk.as"
    newclbk3 cb_win_delete_event, 3, *on_win_delete_event, CLBKMODE_CDECL@
    newclbk3 cb_btn1_toggled, 2, *on_btn1_toggled, CLBKMODE_CDECL@
    newclbk3 cb_btn2_toggled, 2, *on_btn2_toggled, CLBKMODE_CDECL@
    newclbk3 cb_btn3_toggled, 2, *on_btn3_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_radio_button_new_with_label_from_widget "gtk_radio_button_new_with_label_from_widget" sptr, sptr
#func global gtk_radio_button_new_with_mnemonic_from_widget "gtk_radio_button_new_with_mnemonic_from_widget" sptr, 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_win_delete_event, NULL

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

    // ボタン群生成
    num_btn1 = 1
    gtk_radio_button_new_with_label_from_widget 0, "Button " + num_btn1
    btn1 = stat
    g_signal_connect btn1, "toggled", cb_btn1_toggled , varptr( num_btn1 )

    num_btn2 = 2
    gtk_radio_button_new_with_label_from_widget btn1, "Button _" + num_btn2
    btn2 = stat
    g_signal_connect btn2, "toggled", cb_btn2_toggled, varptr( num_btn2 )

    num_btn3 = 3
    gtk_radio_button_new_with_mnemonic_from_widget btn1, "Button _" + num_btn3
    btn3 = stat
    g_signal_connect btn3, "toggled", cb_btn3_toggled, varptr( num_btn3 )

    // ウィンドウの組み立て
    gtk_box_pack_start hbox, btn1, TRUE, TRUE, 0
    gtk_box_pack_start hbox, btn2, TRUE, TRUE, 0
    gtk_box_pack_start hbox, btn3, TRUE, TRUE, 0
    gtk_container_add win, hbox

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

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

*on_btn1_toggled
    gosub *show_message
    return

*on_btn2_toggled
    gosub *show_message
    return

*on_btn3_toggled
    gosub *show_message
    return

*show_message
    clbkargprotect args_
    btn = args_( 0 )
    dupptr num_btn, args_( 1 ), 4

    gtk_toggle_button_get_active btn
    if stat {
        text_state = "ON"
    }
    else {
        text_state = "OFF"
    }
    mes "Button " + num_btn + "が" + text_state + "になりました。"
    return

Prev - Table of contents - Next