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


// コールバック関数を使うための準備
#include "modclbk.as"
    newclbk3 cb_win_delete_event, 3, *on_win_delete_event, CLBKMODE_CDECL@
    newclbk3 cb_btn1_notify_active, 3, *on_btn1_notifyactive, CLBKMODE_CDECL@
    newclbk3 cb_btn2_notify_active, 3, *on_btn2_notifyactive, 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_switch_new "gtk_switch_new"
#func global gtk_switch_set_active "gtk_switch_set_active" sptr, int
#func global gtk_switch_get_active "gtk_switch_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_switch_new
    btn1 = stat
    gtk_switch_set_active btn1, FALSE
    g_signal_connect btn1, "notify::active", cb_btn1_notify_active, varptr( num_btn1 )

    num_btn2 = 2
    gtk_switch_new
    btn2 = stat
    gtk_switch_set_active btn2, TRUE
    g_signal_connect btn2, "notify::active", cb_btn2_notify_active, varptr( num_btn2 )

    // ウィンドウの組み立て
    gtk_box_pack_start hbox, btn1, TRUE, TRUE, 0
    gtk_box_pack_start hbox, btn2, 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_notifyactive
    gosub *show_message
    return

*on_btn2_notifyactive
    gosub *show_message
    return

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

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

Prev - Table of contents - Next