bool ONScripter::executeSystemLoad()
{
    current_font = &menu_font;

    text_info.fill( 0, 0, 0, 0 );
        
    menu_font.num_xy[0] = (strlen(save_item_name)+1)/2+2+13;
    menu_font.num_xy[1] = num_save_file+2;
    menu_font.top_xy[0] = (screen_width * screen_ratio2 / screen_ratio1 - menu_font.num_xy[0] * menu_font.pitch_xy[0]) / 2;
    menu_font.top_xy[1] = (screen_height * screen_ratio2 / screen_ratio1  - menu_font.num_xy[1] * menu_font.pitch_xy[1]) / 2;
    menu_font.setXY( (menu_font.num_xy[0] - strlen( load_menu_name ) / 2) / 2, 0 );
    uchar3 color = {0xff, 0xff, 0xff};
    drawString( load_menu_name, color, &menu_font, true, accumulation_surface, NULL, &text_info );
    menu_font.newLine();
    menu_font.newLine();
        
    flush( refreshMode() );
        
    bool nofile_flag;
    char *buffer = new char[ strlen( save_item_name ) + 31 + 1 ];

    SaveFileInfo save_file_info;
    for ( unsigned int i=1 ; i<=num_save_file ; i++ ){
        searchSaveFile( save_file_info, i );
        menu_font.setXY( (menu_font.num_xy[0] - (strlen( save_item_name ) / 2 + 15) ) / 2 );

        if ( save_file_info.valid ){
            sprintf( buffer, MESSAGE_SAVE_EXIST,
                     save_item_name,
                     save_file_info.sjis_no,
                     save_file_info.sjis_month,
                     save_file_info.sjis_day,
                     save_file_info.sjis_hour,
                     save_file_info.sjis_minute );
            nofile_flag = false;
        }
        else{
            sprintf( buffer, MESSAGE_SAVE_EMPTY,
                     save_item_name,
                     save_file_info.sjis_no );
            nofile_flag = true;
        }
        ButtonLink *button = getSelectableSentence( buffer, &menu_font, false, nofile_flag );
        root_button_link.insert( button );
        button->no = i;
        flush( refreshMode() );
    }
    delete[] buffer;

    refreshMouseOverButton();

    event_mode = WAIT_BUTTON_MODE;
    int file_no = 0;
    while(1){
        waitEventSub(-1);

        if ( current_button_state.button > 0 ){
            file_no = current_button_state.button;
            searchSaveFile( save_file_info, file_no );
            if ( !save_file_info.valid ) continue;
        }

        if (current_button_state.button != 0) break;
    }

    if ( current_button_state.button > 0 ){
        deleteButtonLink();

        if (executeSystemYesNo( SYSTEM_LOAD, file_no )){
            current_font = &sentence_font;
            system_menu_mode = NULL; // for fadeout in mp3stopCommand()
            if ( loadSaveFile( file_no ) )
                return false;

            leaveSystemCall( false );
            refreshSurface(backup_surface, NULL, REFRESH_NORMAL_MODE);
            saveon_flag = true;
            internal_saveon_flag = true;
            text_on_flag = false;
            indent_offset = 0;
            line_enter_status = 0;
            page_enter_status = 0;
            string_buffer_offset = 0;
            break_flag = false;

            flushEvent();

            if (loadgosub_label)
                gosubReal( loadgosub_label, script_h.getCurrent() );

            return true;
        }

        return false;
    }

    deleteButtonLink();
    leaveSystemCall();
    
    return false;
}
void PonscripterLabel::executeSystemYesNo()
{
    current_font = &menu_font;
    if (event_mode & WAIT_BUTTON_MODE) {
        if (current_button_state.button == 0) return;

        event_mode = IDLE_EVENT_MODE;

        deleteButtons();

        if (current_button_state.button == 1) { // yes is selected
	    playSound(menuselectvoice_file_name[MENUSELECTVOICE_YES],
		      SOUND_WAVE | SOUND_OGG, false, MIX_WAVE_CHANNEL);

            if (yesno_caller == SYSTEM_SAVE) {
                saveSaveFile(yesno_selected_file_no);
                leaveSystemCall();
            }
            else if (yesno_caller == SYSTEM_LOAD) {
                current_font = &sentence_font;
                if (loadSaveFile(yesno_selected_file_no)) {
                    system_menu_mode = yesno_caller;
                    advancePhase();
                    return;
                }

                leaveSystemCall(false);
                saveon_flag = true;
                internal_saveon_flag = true;
                text_on_flag  = false;
                indent_offset = 0;
                line_enter_status    = 0;
                string_buffer_offset = 0;
                string_buffer_restore = -1;
		break_flag = false;

                if (loadgosub_label)
                    gosubReal(loadgosub_label, script_h.getCurrent());

                readToken();
            }
            else if (yesno_caller == SYSTEM_RESET) {
                resetCommand("reset");
                readToken();
                event_mode = IDLE_EVENT_MODE;
                leaveSystemCall(false);
            }
            else if (yesno_caller == SYSTEM_END) {
                endCommand("end");
            }
        }
        else {
	    playSound(menuselectvoice_file_name[MENUSELECTVOICE_NO],
		      SOUND_WAVE | SOUND_OGG, false, MIX_WAVE_CHANNEL);

            system_menu_mode = yesno_caller & 0xf;
            if (yesno_caller == SYSTEM_RESET)
                leaveSystemCall();

            advancePhase();
        }
    }
    else {
        text_info.fill(0, 0, 0, 0);
	pstring name;

        if (yesno_caller == SYSTEM_SAVE) {
            SaveFileInfo save_file_info;
            searchSaveFile(save_file_info, yesno_selected_file_no);
            processMessage(name, locale.message_save_confirm, save_file_info);
        }
        else if (yesno_caller == SYSTEM_LOAD) {
            SaveFileInfo save_file_info;
            searchSaveFile(save_file_info, yesno_selected_file_no);
            processMessage(name, locale.message_load_confirm, save_file_info);
        }
        else if (yesno_caller == SYSTEM_RESET)
            name = locale.message_reset_confirm;
        else if (yesno_caller == SYSTEM_END)
            name = locale.message_end_confirm;

        current_font->area_x = int (ceil(current_font->StringAdvance(name)));
        current_font->area_y = current_font->line_top(4);
        current_font->top_x  = (screen_width * screen_ratio2 / screen_ratio1 - current_font->area_x) / 2;
        current_font->top_y  = (screen_height * screen_ratio2 / screen_ratio1 - current_font->area_y) / 2;
        current_font->SetXY(0, 0);

	buttons[0] = getSelectableSentence(name, current_font, false);

        flush(refreshMode());

        float yes_len = current_font->StringAdvance(locale.message_yes),
              no_len  = current_font->StringAdvance(locale.message_no);

        name = locale.message_yes;
        current_font->SetXY(float (current_font->area_x) / 4 - yes_len / 2,
			current_font->line_top(2));
	buttons[1] = getSelectableSentence(name, current_font, false);

        name = locale.message_no;
        current_font->SetXY(float (current_font->area_x) * 3 / 4 - no_len / 2,
			current_font->line_top(2));
        buttons[2] = getSelectableSentence(name, current_font, false);

        flush(refreshMode());

        event_mode = WAIT_BUTTON_MODE;
        refreshMouseOverButton();
    }
}
bool ONScripterLabel::executeSystemLoad()
{
    current_font = &menu_font;

    text_info.fill( 0, 0, 0, 0 );

    menu_font.num_xy[0] = (strlen(save_item_name)+1)/2+2+13;
    menu_font.num_xy[1] = num_save_file+2;
    menu_font.top_xy[0] = (ContractPos(screen_width) - menu_font.num_xy[0] * menu_font.pitch_xy[0]) / 2;
    menu_font.top_xy[1] = (ContractPos(screen_height) - menu_font.num_xy[1] * menu_font.pitch_xy[1]) / 2;
    menu_font.setXY( (menu_font.num_xy[0] - (strlen( load_menu_name )+1) / 2) / 2, 0 );
    //Mion: fixed the menu title bug noted in the past by Seung Park:
    // the menu title must be drawn close to last during refresh,
    // not in the textwindow, since there could be sprites above the
    // window if windowback is used.
    if (system_menu_title){
        dirty_rect.add( system_menu_title->pos );
        delete system_menu_title;
        system_menu_title = NULL;
    }
    system_menu_title = getSentence( load_menu_name, &menu_font, 1, false );
    menu_font.newLine();

    flush( refreshMode() );

    bool nofile_flag;
    int slen = strlen(save_item_name);
    char *buffer = new char[ slen + (slen % 2) + 30 + 3 ];

    SaveFileInfo save_file_info;
    for ( unsigned int i=1 ; i<=num_save_file ; i++ ){
        searchSaveFile( save_file_info, i );
        menu_font.setXY( (menu_font.num_xy[0] - ((strlen( save_item_name )+1) / 2 + 15) ) / 2 );

        if ( save_file_info.valid ){
            sprintf( buffer, getMessageString(MESSAGE_SAVE_EXIST),
                     save_item_name,
                     save_file_info.sjis_no,
                     save_file_info.sjis_month,
                     save_file_info.sjis_day,
                     save_file_info.sjis_hour,
                     save_file_info.sjis_minute );
            nofile_flag = false;
        }
        else{
            sprintf( buffer, getMessageString(MESSAGE_SAVE_EMPTY),
                     save_item_name,
                     save_file_info.sjis_no );
            nofile_flag = true;
        }
        ButtonLink *button = getSelectableSentence( buffer, &menu_font, false, nofile_flag );
        root_button_link.insert( button );
        button->no = i;
        flush( refreshMode() );
    }
    delete[] buffer;

    refreshMouseOverButton();

    event_mode = WAIT_BUTTON_MODE;
    do {
        waitEventSub(-1);
        if (current_button_state.button > 0){
            int file_no = current_button_state.button;
            searchSaveFile( save_file_info, file_no );
            if ( !save_file_info.valid ){
                refreshMouseOverButton();
                current_button_state.button = 0;
            }
        }
    } while (current_button_state.button == 0);

    deleteButtonLink();
    if ( system_menu_title ) {
        dirty_rect.add( system_menu_title->pos );
        delete system_menu_title;
        system_menu_title = NULL;
    }

    if ( current_button_state.button > 0 ){
        int file_no = current_button_state.button;
        searchSaveFile( save_file_info, file_no );
        if ( !save_file_info.valid ){
            refreshMouseOverButton();
            return false;
        }

        if (executeSystemYesNo( SYSTEM_LOAD, file_no )){
            current_font = &sentence_font;
            if ( loadSaveFile( file_no ) )
                return false;

            leaveSystemCall( false );
            saveon_flag = true;
            internal_saveon_flag = true;
            text_on_flag = false;
            indent_offset = 0;
            line_enter_status = 0;
            page_enter_status = 0;
            string_buffer_offset = 0;
            break_flag = false;

            if (loadgosub_label)
                gosubReal( loadgosub_label, script_h.getCurrent() );
            return true;
        }

        return false;
    }

    leaveSystemCall();
    
    return false;
}