guixdisp.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 498 行 · 第 1/2 页

C
498
字号
static bool GetNumStringControls( int *num_controls, char *old_message,
                                  string_info **info )
{
    gui_rect            scale;
    gui_text_metrics    metrics;
    int                 max_width;
    char                *tmp_n;              /* temp newline character */
    int                 len;
    char                *start;
    char                *end_char;
    char                *message;
    char                *new_message;

    new_message = TabFilter( old_message );     /* expand out tab characters */
    message = new_message;

    if( message == NULL ) {
        *num_controls = 0;
        return( FALSE );
    }
    GUIGetScale( &scale );
    GUIGetDlgTextMetrics( &metrics );
    if( strlen( old_message ) > 256 ) {
    // If message is long, go to wider box
        max_width = ( 7 * ( scale.width / metrics.avg.x ) ) / 8;
    } else {
        max_width = ( 2 * ( scale.width / metrics.avg.x ) ) / 3;
    }
    *info           = NULL;
    tmp_n           = NULL;
    *num_controls   = 0;

    for( ;message != tmp_n; ) {
        tmp_n = message + strcspn(message, "\n\r");

        if( tmp_n-message < max_width ) {
            start   = message;
            len     = tmp_n-start;
            if( *tmp_n == '\0' ) {
                message = tmp_n;      /* at the end of original string */
            } else {
                message = tmp_n+1;   /* skip over newline */
            }
        } else {                     /* search back for space */
            for(end_char = message+max_width; (message < end_char) && (*end_char != ' '); --end_char );
            if( end_char == message ) {
                start   = message;           /* no spaces found */
                len     = max_width;
                message += max_width;
            } else {
                start   = message;
                len     = end_char-start+1;
                message = end_char+1;      /* skip over blank */
            }
        }                                  /* add new line to error box */
        (*num_controls)+=1;
        *info = ( string_info * )GUIMemRealloc( *info, sizeof( string_info ) *
                                            (*num_controls) );
        if( *info == NULL ) return( FALSE );
        (*info)[*num_controls - 1].length = len;
        if( !GUIStrnDup( start, &((*info)[*num_controls - 1].text), len ) ) {
            return( FALSE );
        }
    } /* for */

    GUIMemFree( new_message );     /* allocated in TabFilter routine */
    return( TRUE );
}

/*
 * UpdateCols -- increase number of columns needed according to size
 *               and location of control
 */

static int UpdateCols( gui_control_info *control_info, int cols )
{
    cols = max( cols, control_info->rect.x  - DLG_COL_0 +
                control_info->rect.width - DLG_COL_0 );
    return( cols );
}

/*
 * AdjustVert -- adjust the vertical position of buttons and icons according
 *               to the number of lines of text ( also adjusts cols )
 */

int AdjustVert( int *cols, control_types controls_to_use,
                gui_control_info *control_info, int num_controls,
                int num_string_controls )
{
    int num_buttons;
    int i;
    int j;

    if( !MessagesInitialized ) {
        InitMessageControls();
        MessagesInitialized = TRUE;
    }

    i = num_string_controls;
    num_buttons = 0;
    for( j = 0; j < NUM_CONTROL_TYPES; j++ ) {
        if( ( i < num_controls ) &&
            ( controls_to_use & MessageControls[j].type ) ) {
            memcpy( &control_info[i], &MessageControls[j].control,
                    sizeof( gui_control_info ) );
            switch( control_info[i].control_class ) {
            case GUI_PUSH_BUTTON :
            case GUI_DEFPUSH_BUTTON :
                num_buttons ++;
                control_info[i].rect.y = DLG_ROW( BUTTON_ROW +
                                            num_string_controls  - 1 );
                break;
            case GUI_STATIC :
                control_info[i].rect.y = DLG_ROW( ICON_ROW +
                                    ( num_string_controls - 1 ) / 2 );
                break;
            default :
                break;
            }
            *cols = UpdateCols( &control_info[i], *cols );
            i++;
        }
    }
    return( num_buttons );
}

/*
 * CentreButtons -- centre the buttons horizontally
 */

static void CentreButtons( int cols, int num_buttons,
                           gui_control_info *control_info, int num_controls )
{
    int button_number;
    int space_per_button;
    int i;

    button_number = 0;
    if( num_buttons > 0 ) {
        space_per_button = cols / num_buttons;
    }
    for( i = 0; i < num_controls; i++ ) {
        switch( control_info[i].control_class ) {
        case GUI_PUSH_BUTTON :
        case GUI_DEFPUSH_BUTTON :
            button_number++;
            control_info[i].rect.x = DLG_COL( space_per_button
              * button_number - ( ( space_per_button + BUTTON_WIDTH ) / 2 ) );
            break;
        default :
            break;
        }
    }
}

/*
 * GUIDisplayMessage -- display the message, return the user's response
 */

gui_message_return GUIDisplayMessage( gui_window *wnd,
                                      char *message, char *caption,
                                      gui_message_type type )
{
    int                 rows;
    int                 cols;
    gui_control_info    *control_info;
    int                 num_controls;
    int                 num_string_controls;
    gui_message_return  ret;
    int                 i;
    control_types       controls_to_use;
    int                 mess_length;
    int                 capt_length;
    string_info         *strings;
    int                 num_buttons;

    wnd = wnd;
    if( message != NULL ) {
        mess_length = strlen( message );
    } else {
        mess_length = 0;
    }
    if( caption != NULL ) {
        capt_length = strlen( caption );
    } else {
        capt_length = 0;
    }

    /* figure out the number of icon and button controls and which ones */
    num_controls = 0;
    controls_to_use = 0;
    for( i = 0; i < NUM_STYLES; i++ ) {
        if( type & ControlsNeeded[i].type ) {
            num_controls += ControlsNeeded[i].num_controls;
            controls_to_use |= ControlsNeeded[i].controls;
        }
    }

    /* figure out how manu GUI_STATIC controls are required for the text */
    num_string_controls = 0;
    if( !GetNumStringControls( &num_string_controls, message, &strings ) ) {
        return( GUI_RET_CANCEL );
    }

    num_controls += num_string_controls;
    control_info = (gui_control_info *)GUIMemAlloc( sizeof( gui_control_info ) *
                                                 num_controls );
    if( control_info == NULL ) {
        return( GUI_RET_CANCEL );
    }

    cols = capt_length + 2;

    /* create GUI_STATIC controls, as many as required */
    if( num_string_controls > 0 ) {
        for( i = 0; i < num_string_controls; i++ ) {
            uiyield();
            StaticMessage.text = strings[i].text;
            StaticMessage.rect.width = DLG_COL( strings[i].length );
            StaticMessage.rect.y = DLG_ROW( TEXT_ROW + i );
            memcpy( &control_info[i], &StaticMessage, sizeof( gui_control_info ) );
            cols = UpdateCols( &control_info[i], cols );
        }
    }
    rows = num_string_controls + BUTTON_ROW + 1;

    /* adjust the vertical position of buttons and icons according to the
     * number of lines of text ( also adjusts cols )
     */
    num_buttons = AdjustVert( &cols, controls_to_use, control_info, num_controls,
                              num_string_controls );

    if( cols < ( num_buttons * ( BUTTON_WIDTH + 4 ) ) ) {
        cols = num_buttons * ( BUTTON_WIDTH + 4 ) ;
    }
    /* centre the buttons horizontally */
    CentreButtons( cols, num_buttons, control_info, num_controls );
    ret = GUI_RET_CANCEL; /* default -- if escape hit */
    GUIDlgOpen( caption, rows, cols, control_info, num_controls,
                &DisplayMessage, &ret );
    for( i = 0; i < num_string_controls; i++ ) {
        GUIMemFree( strings[i].text );
    }
    GUIMemFree( strings );
    GUIMemFree( control_info );
    return( ret );
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?