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


// コールバック関数を使うための準備
#include "modclbk.as"
    newclbk3 cb_win_delete_event, 3, *on_win_delete_event, CLBKMODE_CDECL@
    newclbk3 cb_cbtn1_toggled, 2, *on_cbtn1_toggled, CLBKMODE_CDECL@
    newclbk3 cb_cbtn2_toggled, 2, *on_cbtn2_toggled, CLBKMODE_CDECL@
    newclbk3 cb_cbtn3_toggled, 2, *on_cbtn3_toggled, CLBKMODE_CDECL@
    newclbk3 cb_cbtn4_toggled, 2, *on_cbtn4_toggled, CLBKMODE_CDECL@
    newclbk3 cb_do_pulse, 0, *do_pulse, 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_add "gtk_container_add" sptr, sptr
#func global gtk_widget_set_size_request "gtk_widget_set_size_request" sptr, int, 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_vbox_new "gtk_vbox_new" int, int
#func global gtk_hbox_new "gtk_hbox_new" int, int
#func global gtk_box_pack_start "gtk_box_pack_start" sptr, sptr, int, int, int
#func global gtk_entry_new "gtk_entry_new" 
#func global gtk_entry_set_text "gtk_entry_set_text" sptr, sptr
#func global gtk_entry_set_visibility "gtk_entry_set_visibility" sptr, int
#func global gtk_entry_set_icon_from_stock "gtk_entry_set_icon_from_stock" sptr, int, sptr
#func global gtk_entry_set_progress_pulse_step "gtk_entry_set_progress_pulse_step" sptr, double
#func global gtk_entry_progress_pulse "gtk_entry_progress_pulse" sptr
#func global gtk_editable_set_editable "gtk_editable_set_editable" sptr, int
#func global gtk_check_button_new_with_label "gtk_check_button_new_with_label" sptr
#func global gtk_toggle_button_set_active "gtk_toggle_button_set_active" sptr, int
#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

#uselib "libglib-2.0-0.dll"
#func global g_timeout_add "g_timeout_add" int, sptr, sptr
#func global g_source_remove "g_source_remove" int

// よく使う関数
#include "hspinet.as"
#module
#defcfunc u str chars_ ; shift-jis文字列をutf-8に変換
    chars = chars_
    nkfcnv@ chars, chars, "Sw"
    return chars
#global

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

    // VBox生成
    gtk_vbox_new FALSE, 6
    vbox = stat

    // エントリ生成
    gtk_entry_new
    ety = stat
    gtk_entry_set_text ety, "Hello, World"

    // HBox生成
    gtk_hbox_new FALSE, 6
    hbox = stat

    // ボタン群生成
    gtk_check_button_new_with_label "Editable"
    cbtn1 = stat
    gtk_toggle_button_set_active cbtn1, TRUE
    g_signal_connect cbtn1, "toggled", cb_cbtn1_toggled, ety

    gtk_check_button_new_with_label "Visible"
    cbtn2 = stat
    gtk_toggle_button_set_active cbtn2, TRUE
    g_signal_connect cbtn2, "toggled", cb_cbtn2_toggled, ety

    gtk_check_button_new_with_label "Pulse"
    cbtn3 = stat
    gtk_toggle_button_set_active cbtn3, FALSE
    g_signal_connect cbtn3, "toggled", cb_cbtn3_toggled, ety

    gtk_check_button_new_with_label "Icon"
    cbtn4 = stat
    gtk_toggle_button_set_active cbtn4, FALSE
    g_signal_connect cbtn4, "toggled", cb_cbtn4_toggled, ety

    // ウィンドウの組み立て
    gtk_box_pack_start hbox, cbtn1, TRUE, TRUE, 0
    gtk_box_pack_start hbox, cbtn2, TRUE, TRUE, 0
    gtk_box_pack_start hbox, cbtn3, TRUE, TRUE, 0
    gtk_box_pack_start hbox, cbtn4, TRUE, TRUE, 0
    gtk_box_pack_start vbox, ety, TRUE, TRUE, 0
    gtk_box_pack_start vbox, hbox, TRUE, TRUE, 0
    gtk_container_add win, vbox

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

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

*on_cbtn1_toggled
    clbkargprotect args_

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

*on_cbtn2_toggled
    clbkargprotect args_

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

*on_cbtn3_toggled
    clbkargprotect args_
    e = args_( 1 )

    gtk_toggle_button_get_active args_( 0 )
    if stat {
        gtk_entry_set_progress_pulse_step e, 0.2
        g_timeout_add 100, cb_do_pulse, NULL
        timeout_id = stat
    }
    else {
        g_source_remove timeout_id
        timeout_id = 0
        gtk_entry_set_progress_pulse_step e, 0
    }
    return

*do_pulse
    gtk_entry_progress_pulse e
    return

*on_cbtn4_toggled
    clbkargprotect args_

#define GTK_STOCK_FIND "gtk-find" ; GtkStockItem
#const GTK_ENTRY_ICON_PRIMARY 0 ; GtkEntryIconPosition
    gtk_toggle_button_get_active args_( 0 )
    if stat {
        stock_id = GTK_STOCK_FIND
    }
    else {
        stock_id = ""
    }
    gtk_entry_set_icon_from_stock args_( 1 ), GTK_ENTRY_ICON_PRIMARY, stock_id
    return

Prev - Table of contents - Next