guixdlg.c

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

C
951
字号

    switch( field->typ ) {
    case FLD_HOT :
        if( !GUICreateHot( info, (a_hot_spot_field *)field ) ) {
            return( FALSE );
        }
        break;
    case FLD_RADIO :
        radio = (a_radio * )GUIMemAlloc( sizeof( a_radio ) );
        field->ptr = radio;
        if( ( radio == NULL ) || !GUIStrDup( info->text, &radio->str ) ) {
            if( group_allocated ) {
                CleanUpRadioGroups();
            }
            return( FALSE );
        }
        radio->value = GUI_FIRST_USER_EVENT + info->id;
        radio->group = RadioGroup;
        if( ( info->style & GUI_CHECKED ) &&
            ( info->style & GUI_AUTOMATIC ) ) {
            RadioGroup->def = radio->value;
            RadioGroup->value = radio->value;
        }
        break;
    case FLD_CHECK :
        check = (a_check * )GUIMemAlloc( sizeof( a_check ) );
        field->ptr = check;
        if( ( check == NULL ) || !GUIStrDup( info->text, &check->str ) ) {
            return( FALSE );
        }
        check->val = 0;
        if( ( info->style & GUI_CHECKED ) &&
            ( info->style & GUI_AUTOMATIC ) ) {
            check->def = TRUE;
        } else {
            check->def = FALSE;
        }
        break;
    case FLD_COMBOBOX :
        combo_box = (a_combo_box * )GUIMemAlloc( sizeof( a_combo_box ) );
        if( combo_box == NULL ) {
            return( FALSE );
        }
        field->ptr = combo_box;
        combo_box->edit.buffer = NULL;
        combo_box->edit.length = 0;
        if( !GUIFillInListBox( &combo_box->list ) ) {
            return( FALSE );
        }
        if( info->text != NULL ) {
            if( !GUISetEditText( &combo_box->edit, info->text, FALSE ) ) {
                return( FALSE );
            }
        }
        combo_box->perm = FALSE;
        break;
    case FLD_EDIT :
    case FLD_INVISIBLE_EDIT :
        edit_control = (an_edit_control * )GUIMemAlloc( sizeof( an_edit_control ) );
        if( edit_control == NULL ) {
            return( FALSE );
        }
        field->ptr = edit_control;
        edit_control->buffer = NULL;
        GUISetEditText( edit_control, info->text, field->typ != FLD_EDIT );
        break;
    case FLD_PULLDOWN :
    case FLD_LISTBOX :
        field->ptr = (void *)GUICreateListBox();
        if( field->ptr == NULL ) {
            return( FALSE );
        }
        break;
    case FLD_EDIT_MLE:
        field->ptr = (void *)GUICreateEditMLE( info->text );
        if( field->ptr == NULL ) {
            return( FALSE );
        }
        break;
    case FLD_TEXT :
    case FLD_FRAME :
        if( !GUIStrDup( info->text, (char **)&field->ptr ) ) {
            return( FALSE );
        }
        break;
    default :
        return( FALSE );
        break;
    }
    if( (info->style & GUI_GROUP)  &&
        (info->control_class == GUI_RADIO_BUTTON) ) {
        if( Group && !group_allocated ) {
            RadioGroup = NULL;
            Group = FALSE;
        }
    }
    return( TRUE );
}

bool GUIDeleteField( gui_window *wnd, unsigned id )
{
    dialog_node *dialog;
    a_dialog    *ui_dialog;
    int         index;
    VFIELD      *new_fields;
    VFIELD      *field;
    int         new_index;
    int         i;
    gui_control *control;

    dialog = GUIGetDlgByWnd( wnd );
    if( dialog == NULL ) {
        return( FALSE );
    }
    ui_dialog = dialog->dialog;
    field = GUIGetField( wnd, id );
    if( GetIndexOfField( ui_dialog, field, dialog->num_controls, &index ) ) {
        GUIDoFreeField( field, NULL );
        new_fields = (VFIELD *)GUIMemAlloc( sizeof( VFIELD ) * dialog->num_controls );
        for( i=0; i <= dialog->num_controls; i++ ) {
            new_index = i;
            if( i != index ) {
                if( i > index ) {
                    new_index--;
                    control = GUIGetControlByIndex( wnd, i );
                    if( control != NULL ) {
                        control->index = new_index;
                    }
                }
                memcpy( &new_fields[new_index], &ui_dialog->fields[i], sizeof( VFIELD ) );
            }
            if( ui_dialog->other == &ui_dialog->fields[i] ) {
                if( i == index ) {
                    ui_dialog->other = NULL;
                } else {
                    ui_dialog->other = &new_fields[new_index];
                }
            }
            if( ui_dialog->curr == &ui_dialog->fields[i] ) {
                if( i == index ) {
                    ui_dialog->curr = NULL;
                } else {
                    ui_dialog->curr = &new_fields[new_index];
                }
            }
            if( ui_dialog->first == &ui_dialog->fields[i] ) {
                if( i == index ) {
                    ui_dialog->curr = NULL;
                } else {
                    ui_dialog->first = &new_fields[new_index];
                }
            }
        }
        GUIMemFree( ui_dialog->fields );
        ui_dialog->fields = new_fields;
        dialog->num_controls--;
        return( TRUE );
    }
    return( FALSE );
}

gui_control *GUIAddAControl( gui_control_info *info, gui_window *wnd )
{
    dialog_node *dialog;
    VFIELD      *new_field;
    gui_control *control;

    dialog = GUIGetDlgByWnd( wnd );
    if( dialog == NULL ) {
        return( NULL );
    }
    if( !ResetFieldSize( dialog, dialog->num_controls + 1 ) ) {
        return( NULL );
    }
    new_field = &dialog->dialog->fields[dialog->num_controls];
    if( !GUIDoAddControl( info, wnd, new_field ) ) {
        ResetFieldSize( dialog, dialog->num_controls );
        return( NULL );
    }

    control = GUIInsertControl( wnd, info, dialog->num_controls );
    if( control == NULL ) {
        GUIDoFreeField( new_field, NULL );
        ResetFieldSize( dialog, dialog->num_controls );
    }
    return( control );
}

static void EditNotify( gui_key key, a_dialog *ui_dialog, gui_window *wnd )
{
    gui_key_control     key_control;

    if( ui_dialog->curr != NULL ) {
        switch( ui_dialog->curr->typ ) {
        case FLD_EDIT :
        case FLD_INVISIBLE_EDIT :
            key_control.key_state.key = key;
            GUIGetKeyState( &key_control.key_state.state );
            key_control.id = GUIGetControlId( wnd, ui_dialog->curr );
            if( key_control.id != (unsigned)NULL ) {
                GUIEVENTWND( wnd, GUI_KEY_CONTROL, &key_control );
            }
        }
    }
}

bool GUIResizeDialog( gui_window *wnd, SAREA *new_area )
{
    a_dialog    *ui_dialog;

    ui_dialog = GUIGetDialog( wnd );
    if( ui_dialog != NULL ) {
        uiresizedialog( ui_dialog, new_area );
        return( TRUE );
    }
    return( FALSE );
}

static void CheckNotify( a_dialog *ui_dialog, gui_window *wnd )
{
    unsigned    id;

    if( ui_dialog->curr != NULL ) {
        id = GUIGetControlId( wnd, ui_dialog->curr );
        if( id != (unsigned)NULL ) {
            GUIEVENTWND( wnd, GUI_CONTROL_CLICKED, &id );
        }
    }
}

static void ListNotify( EVENT ev, a_dialog *ui_dialog, gui_window *wnd )
{
    gui_event   gui_ev;
    a_list      *list;
    unsigned    id;

    if( ui_dialog->curr != NULL ) {
        list = GUIGetList( ui_dialog->curr );
        id = GUIGetControlId( wnd, ui_dialog->curr );
        if( id != (unsigned)NULL ) {
            switch( ev ) {
            case EV_LIST_BOX_CHANGED :
                gui_ev = GUI_CONTROL_CLICKED;
                break;
            case EV_LIST_BOX_DCLICK :
                gui_ev = GUI_CONTROL_DCLICKED;
                break;
            case EV_LIST_BOX_CLOSED :
                gui_ev = GUI_CONTROL_CLICKED;
                break;
            default :
                return;
            }
            GUIEVENTWND( wnd, gui_ev, &id );
        }
    }
}

void GUIFocusChangeNotify( a_dialog *ui_dialog )
{
    unsigned    id;
    gui_window  *wnd;
    dialog_node *node;

    node = GetDialog( ui_dialog );
    if( node != NULL ) {
        wnd = node->wnd;
        if( ( ui_dialog->other != NULL ) && ( wnd != NULL ) ) {
            switch( ui_dialog->other->typ ) {
            case FLD_EDIT :
            case FLD_INVISIBLE_EDIT :
            case FLD_PULLDOWN :
            case FLD_LISTBOX :
            case FLD_COMBOBOX :
            case FLD_EDIT_MLE :
                id = GUIGetControlId( wnd, ui_dialog->other );
                GUIEVENTWND( wnd, GUI_CONTROL_NOT_ACTIVE, &id );
            }
        }
    }
}

EVENT GUIProcessControlNotify( EVENT ev, a_dialog *ui_dialog, gui_window *wnd )
{
    unsigned    id;

    switch( ev ) {
    case EV_CHECK_BOX_CLICK :
        CheckNotify( ui_dialog, wnd );
        return( EV_NO_EVENT );
    case EV_LIST_BOX_DCLICK :
    case EV_LIST_BOX_CHANGED :
    case EV_LIST_BOX_CLOSED :
        ListNotify( ev, ui_dialog, wnd );
        return( EV_NO_EVENT );
    case EV_CURSOR_UP :
        EditNotify( GUI_KEY_UP, ui_dialog, wnd );
        return( EV_NO_EVENT );
    case EV_CURSOR_DOWN :
        EditNotify( GUI_KEY_DOWN, ui_dialog, wnd );
        return( EV_NO_EVENT );
    default :
        if( ev >= GUI_FIRST_USER_EVENT ) {
            id = GETID( ev );
            GUIEVENTWND( wnd, GUI_CONTROL_CLICKED, &id );
            return( EV_NO_EVENT );
        }
        return( ev );
    }
}

/*
 * GUIXCreateDialog -- create a dialog window
 */

bool GUIXCreateDialog( gui_create_info *dialog, gui_window *wnd,
                       int num_controls, gui_control_info *info_controls,
                       bool sys, long dlg_id )
{
    EVENT       ev;
    int         i;
    a_dialog    *ui_dialog;
    VFIELD      *fields;
    char        *title;
    VFIELD      *focus;
    int         size;
    bool        colours_set;

    if( dlg_id != -1 ) {
        if( !GUICreateDialogFromRes( dlg_id, dialog->parent,
                                     dialog->call_back, dialog->extra ) ) {
            return( FALSE );
        }
        GUIMemFree( wnd );
        return( TRUE );
    }

    sys = sys;
    RadioGroup = NULL;
    Group = FALSE;
    fields = NULL;
    title = NULL;
    ui_dialog = NULL;
    colours_set = FALSE;

    wnd->flags |= DIALOG;
    if( !GUISetupStruct( wnd, dialog, TRUE ) ) {
        return( FALSE );
    }

    size = ( num_controls + 1 ) * sizeof( VFIELD );
    fields = (VFIELD *)GUIMemAlloc( size );
    if( fields == NULL ) {
       return( FALSE );
    }
    memset( fields, 0, size );
    focus = NULL;
    for( i = 0; i < num_controls; i++ ) {
        uiyield();
        if( !GUIDoAddControl( &info_controls[i], wnd, &fields[i] ) ) {
            GUIFreeDialog( ui_dialog, fields, title, colours_set, TRUE );
            return( FALSE );
        } else {
            if( ( focus == NULL ) && ( info_controls[i].style & GUI_FOCUS ) ) {
                focus = &fields[i];
            }
        }
    }
    CleanUpRadioGroups();
    fields[num_controls].typ = FLD_VOID; /* mark end of list */

    if( !GUIStrDup( dialog->text, &title ) ) {
        GUIFreeDialog( ui_dialog, fields, title, colours_set, TRUE );
        return( FALSE );
    }
    colours_set = GUISetDialColours();
    ui_dialog = uibegdialog( title, fields, wnd->screen.area.height,
                             wnd->screen.area.width, wnd->screen.area.row,
                             wnd->screen.area.col );
    if( ui_dialog == NULL ) {
        GUIFreeDialog( ui_dialog, fields, title, colours_set, TRUE );
        return( FALSE );
    }
    if( focus != NULL ) {
        uidialogsetcurr( ui_dialog, focus );
    }
    if( !InsertDialog( wnd, ui_dialog, num_controls, title, colours_set ) ) {
        GUIFreeDialog( ui_dialog, fields, title, colours_set, TRUE );
        return( FALSE );
    }
    for( i = 0; i < num_controls; i++ ) {
        uiyield();
        GUIInsertControl( wnd, &info_controls[i], i );
    }
    GUIEVENTWND( wnd, GUI_INIT_DIALOG, NULL );
    uipushlist( NULL );
    uipushlist( GUIUserEvents );
    GUIPushControlEvents();
    uipushlist( DlgEvents );
    while( ( GetDialog( ui_dialog ) != NULL ) ) {
        ev = uidialog( ui_dialog );
        switch( ev ) {
        case EV_KILL_UI:
            uiforceevadd( EV_KILL_UI );
        case EV_ESCAPE:
            GUIEVENTWND( wnd, GUI_DIALOG_ESCAPE, NULL );
            GUICloseDialog( wnd );
            break;
        default :
            GUIProcessControlNotify( ev, ui_dialog, wnd );
        }
    }
    return( TRUE );
}

void GUIFreeDialog( a_dialog *dialog, VFIELD *fields, char *title,
                    bool colours_set, bool is_dialog )
{
    if( dialog != NULL ) {
        if( is_dialog ) {
            uienddialog( dialog );
        } else {
            uifreedialog( dialog );
        }
    }
    if( colours_set ) {
        GUIResetDialColours();
    }
    FreeFields( fields );
    GUIMemFree( title );
}

/*
 * GUICloseDialog -- close the given dialog box
 */

void GUICloseDialog( gui_window *wnd )
{
    VFIELD      *fields;
    a_dialog    *dialog;
    char        *name;
    dialog_node *node;
    bool        colours_set;

    node = GUIGetDlgByWnd( wnd );
    if( node != NULL ) {
        dialog = node->dialog;
        fields = dialog->fields;
        name = node->name;
        colours_set = node->colours_set;
    }
    GUIPopControlEvents();
    uipoplist( /* DlgEvents */ );
    uipoplist( /* GUIUserEvents */ );
    uipoplist( /* NULL */ );
    GUIDestroyDialog( wnd );
}

bool GUIGetDlgRect( gui_window *wnd, SAREA *area )
{
    a_dialog    *ui_dialog;

    if( GUI_IS_DIALOG( wnd ) ) {
        ui_dialog = GUIGetDialog( wnd );
        if( ui_dialog != NULL ) {
            uigetdialogarea( ui_dialog, area );
        } else {
            COPYAREA( wnd->screen.area, *area );
        }
        GUIAdjustDialogArea( area, +1 );
        return( TRUE );
    }
    return( FALSE );
}

⌨️ 快捷键说明

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