guirdlg.c

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

C
676
字号
                styles |= GUI_CONTROL_3STATE | GUI_AUTOMATIC;
            }
            break;
        case GUI_RADIO_BUTTON:
            if( ( ctl->Style & 0xf ) == BS_AUTORADIOBUTTON ) {
                styles |= GUI_AUTOMATIC;
            }
            break;
        case GUI_LISTBOX:
            if( ctl->Style & LBS_NOINTEGRALHEIGHT ) {
                styles |= GUI_CONTROL_NOINTEGRALHEIGHT;
            }
            if( ctl->Style & LBS_SORT ) {
                styles |= GUI_CONTROL_SORTED;
            }
            break;
        case GUI_STATIC:
            if( ctl->Style & SS_NOPREFIX ) {
                styles |= GUI_CONTROL_NOPREFIX;
            }
            if( ( ctl->Style & 0xf ) == SS_CENTER ) {
                styles |= GUI_CONTROL_CENTRE;
            }
            if( ( ctl->Style & 0xf ) == SS_LEFTNOWORDWRAP ) {
                styles |= GUI_CONTROL_LEFTNOWORDWRAP;
            }
            break;
        case GUI_EDIT_COMBOBOX:
        case GUI_COMBOBOX:
            if( ctl->Style & CBS_NOINTEGRALHEIGHT ) {
                styles |= GUI_CONTROL_NOINTEGRALHEIGHT;
            }
            if( ctl->Style & CBS_SORT ) {
                styles |= GUI_CONTROL_SORTED;
            }
            break;
        case GUI_EDIT:
        case GUI_EDIT_MLE:
            if( ctl->Style & ES_MULTILINE ) {
                styles |= GUI_CONTROL_MULTILINE;
            }
            if( ctl->Style & ES_WANTRETURN ) {
                styles |= GUI_CONTROL_WANTRETURN;
            }
            break;
        default :
            break;
    }

    return( styles );
}

#define CHK_BSTYLE(s,m) ( ( s & m ) == m )

static gui_control_class GetControlClass( DialogBoxControl *ctl )
{
    gui_control_class   control_class;

    control_class = GUI_BAD_CLASS;

    if( ctl && ctl->ClassID && ( ctl->ClassID->Class & 0x80 ) ) {
        switch( ctl->ClassID->Class ) {
            case CLASS_BUTTON:
                control_class = GUI_PUSH_BUTTON;
                if( CHK_BSTYLE( ctl->Style, BS_GROUPBOX ) ) {
                    control_class = GUI_GROUPBOX;
                } else if( CHK_BSTYLE( ctl->Style, BS_AUTORADIOBUTTON ) ||
                           CHK_BSTYLE( ctl->Style, BS_RADIOBUTTON ) ) {
                    control_class = GUI_RADIO_BUTTON;
                } else if( CHK_BSTYLE( ctl->Style, BS_AUTOCHECKBOX ) ||
                           CHK_BSTYLE( ctl->Style, BS_CHECKBOX ) ||
                           CHK_BSTYLE( ctl->Style, BS_3STATE ) ||
                           CHK_BSTYLE( ctl->Style, BS_AUTO3STATE ) ) {
                    control_class = GUI_CHECK_BOX;
                } else if( CHK_BSTYLE( ctl->Style, BS_DEFPUSHBUTTON ) ) {
                    control_class = GUI_DEFPUSH_BUTTON;
                }
                break;
            case CLASS_EDIT:
                control_class = GUI_EDIT;
                if( ctl->Style & ES_MULTILINE ) {
                    control_class = GUI_EDIT_MLE;
                }
                break;
            case CLASS_STATIC:
                control_class = GUI_STATIC;
                break;
            case CLASS_LISTBOX:
                control_class = GUI_LISTBOX;
                break;
            case CLASS_SCROLLBAR:
                control_class = GUI_SCROLLBAR;
                break;
            case CLASS_COMBOBOX:
                control_class = GUI_EDIT_COMBOBOX;
                if( ctl->Style & CBS_DROPDOWNLIST ) {
                    control_class = GUI_COMBOBOX;
                }
                break;
        }
    }

    return( control_class );
}

static bool DialogBoxControl2GUI( DialogBoxControl *ctl,
                                  gui_control_info *gci )
{
    SAREA               area;
    bool                ok;

    ok = ( ctl && gci );

    if( ok ) {
        // initialize the create struct
        memset( gci, 0, sizeof( gui_control_info ) );

        // set the control class
        gci->control_class = GetControlClass( ctl );
        ok = ( gci->control_class != GUI_BAD_CLASS );
    }

    if( ok ) {
        // set the control style
        gci->style = GetControlStyles( ctl, gci->control_class );
    }

    if( ok ) {
        // set the initial text. NULL is ok
        gci->text = ResNameOrOrdinalToStr( ctl->Text, 10 );

        // set the control id
        gci->id = ctl->ID;

        // set the scroll styles
        gci->scroll = GUI_NOSCROLL;
        if( ctl->Style & WS_HSCROLL ) {
            gci->scroll = GUI_HSCROLL;
        }
        if( ctl->Style & WS_VSCROLL ) {
            gci->scroll = GUI_VSCROLL;
        }

        // set the control postion
        area.row = ctl->Size.y / DLG_Y_MULT;
        area.col = ctl->Size.x / DLG_X_MULT;
        area.width = max( 1, ( ( ctl->Size.width + DLG_X_MULT/2 ) / DLG_X_MULT ) );
        area.height = max( 1, ( ( ctl->Size.height + DLG_Y_MULT/2 ) / DLG_Y_MULT ) );
        ok = GUIScreenToScaleRectR( &area, &gci->rect );
    }

    if( !ok ) {
        if( gci ) {
            if( gci->text ) {
                GUIMemFree( gci->text );
            }
        }
    }

    return( ok );
}

static gui_create_info *DialogBoxHeader2GUI( DialogBoxHeader *hdr )
{
    gui_create_info     *gci;
    SAREA               area;
    SAREA               bounding;
    bool                ok;

    ok = ( hdr != NULL );

    if( ok ) {
        gci = (gui_create_info *)GUIMemAlloc( sizeof( gui_create_info ) );
        ok = ( gci != NULL );
    }

    if( ok ) {
        // initialize the create struct
        memset( gci, 0, sizeof( gui_create_info ) );

        // set the initial text
        GUIStrDup( hdr->Caption, &gci->text ); // NULL text is ok

        // set the dialog postion remembering to add the size of the frame
        GUIGetScreenArea( &bounding );
        area.width = hdr->Size.width / DLG_X_MULT + 2;
        area.height = hdr->Size.height / DLG_Y_MULT + 2;
        area.row = max( 0, ( bounding.height - area.height ) / 2 );
        area.col = max( 0, ( bounding.width - area.width ) / 2 );
        ok = GUIScreenToScaleRect( &area, &gci->rect );
    }

    if( ok ) {
        // set the scroll styles
        gci->scroll = GUI_NOSCROLL;
        if( hdr->Style & WS_HSCROLL ) {
            gci->scroll = GUI_HSCROLL;
        }
        if( hdr->Style & WS_VSCROLL ) {
            gci->scroll = GUI_VSCROLL;
        }
        // set the window styles
        gci->style = GUI_NONE;
        if( hdr->Style & WS_THICKFRAME ) {
            gci->style = GUI_RESIZEABLE;
        }
        if( hdr->Style & WS_THICKFRAME ) {
            gci->style = GUI_RESIZEABLE;
        }
        if( hdr->Style & WS_MINIMIZEBOX ) {
            gci->style = GUI_MINIMIZE;
        }
        if( hdr->Style & WS_MAXIMIZEBOX ) {
            gci->style = GUI_MAXIMIZE;
        }
        if( hdr->Style & WS_SYSMENU ) {
            gci->style = GUI_SYSTEM_MENU;
        }
        if( hdr->Style & WS_POPUP ) {
            gci->style = GUI_POPUP;
        }
    }

    if( !ok ) {
        if( gci ) {
            if( gci->text ) {
                GUIMemFree( gci->text );
            }
            GUIMemFree( gci );
            gci = NULL;
        }
    }

    return( gci );
}

bool GUICreateDialogFromRes( int id, gui_window *parent, GUICALLBACK cb,
                             void *extra )
{
    DialogBoxHeader     *hdr;
    DialogBoxControl    *cntls;
    gui_create_info     *gui_dlg;
    gui_control_info    *gui_cntls;
    uint_8              *data;
    int                 size;
    int                 index;
    int                 last_was_radio;
    bool                ok;

    hdr = NULL;
    cntls = NULL;
    data = NULL;
    gui_dlg = NULL;
    gui_cntls = NULL;
    size = 0;

    ok = ( cb != NULL );

    if( ok ) {
        ok = GUILoadDialogTemplate( id, (char **)&data, &size );
    }

    if( ok ) {
        ok = Template2Dlg( &hdr, &cntls, data, size );
    }

    if( ok ) {
        gui_dlg = DialogBoxHeader2GUI( hdr );
        ok = ( gui_dlg != NULL );
    }

    if( ok ) {
        gui_cntls = (gui_control_info *)
            GUIMemAlloc( sizeof( gui_control_info ) * hdr->NumOfItems );
        ok = ( gui_cntls != NULL );
    }

    last_was_radio = -1;
    if( ok ) {
        memset( gui_cntls, 0, sizeof( gui_control_info ) * hdr->NumOfItems );
        for( index = 0; ok && index < hdr->NumOfItems; index++ ) {
            ok = DialogBoxControl2GUI( &cntls[index], &gui_cntls[index] );
            if( ok ) {
                if( gui_cntls[index].control_class == GUI_RADIO_BUTTON ) {
                    if( last_was_radio != 1 ) {
                        gui_cntls[index].style |= GUI_GROUP;
                    }
                    last_was_radio = 1;
                } else {
                    if( last_was_radio == 1 ) {
                        gui_cntls[index-1].style |= GUI_GROUP;
                    }
                    last_was_radio = 0;
                }
            }
        }
    }

    if( ok ) {
        gui_dlg->parent = parent;
        gui_dlg->call_back = cb;
        gui_dlg->extra = extra;
        ok = GUICreateDialog( gui_dlg, hdr->NumOfItems, gui_cntls );
    }

    if( gui_cntls ) {
        for( index = 0; ok && index < hdr->NumOfItems; index++ ) {
            GUIMemFree( gui_cntls[index].text );
        }
        GUIMemFree( gui_cntls );
    }

    if( gui_dlg ) {
        if( gui_dlg->text ) {
            GUIMemFree( gui_dlg->text );
        }
        GUIMemFree( gui_dlg );
    }

    if( cntls ) {
        for( index = 0; ok && index < hdr->NumOfItems; index++ ) {
            GUIFreeDialogBoxControlPtrs( &cntls[index] );
        }
        GUIMemFree( cntls );
    }

    if( hdr ) {
        GUIFreeDialogBoxHeader( hdr );
    }

    if( data ) {
        GUIMemFree( data );
    }

    return( ok );
}

⌨️ 快捷键说明

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