guicontr.c

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

C
839
字号
        info = GUIGetControlByHwnd( wnd, hwnd );
    }
    if( info == NULL ) {
        return( 0L );
    }
    call_back = info->call_back;
    switch( message ) {
    case WM_ERASEBKGND:
        hdc = _wpi_getpres( hwnd );
        _wpi_getupdaterect( hwnd, &rect );
        _wpi_fillrect( hdc, &rect, NULL, wnd->bk_brush );
        _wpi_releasepres( hwnd, hdc );
        break;
    }
    return( (WPI_MRESULT)_wpi_callwindowproc( (WPI_WNDPROC)call_back, hwnd, message, wparam, lparam ) );
}

WPI_PROC GUIDoSubClass( HWND hwnd, gui_control_class control_class )
{
    WPI_PROC old;
    WPI_PROC new;

    //GUICtl3dSubclassCtl( hwnd );

    switch( control_class ) {
    case GUI_EDIT_COMBOBOX :
        return( GUISubClassEditCombobox( hwnd ) );
    default :
        if( GUIControls[control_class].call_back == NULL ) {
            return( NULL );
        }
        new = _wpi_makeprocinstance( (WPI_PROC) GUIControls[control_class].call_back,
                                GUIMainHInst );
        old = _wpi_subclasswindow( hwnd, new );
        return( old );
    }
}

LONG GUISetControlStyle( gui_control_info *info )
{
    LONG        ret_style;

    ret_style = GUIControls[info->control_class].style;

    /* The GUI library has a group marked by GUI_GROUP on the first and
     * last group item.  Windows has the WS_GROUP style mark the start
     * of each group.  So, I mark everything as WS_GROUP, excluding
     * the second, third, etc. member of a GUI_GROUP.  That way,
     * everything is in a group of one, except GUI_GROUPs, which are
     * grouped properly.
     */
    if( !( info->scroll & GUI_CONTROL_INIT_INVISIBLE ) ) {
        ret_style |= WS_VISIBLE;
    }
#ifndef __OS2_PM__
    if( info->scroll & GUI_VSCROLL ) {
        ret_style |= WS_VSCROLL;
    }
    if( info->scroll & GUI_HSCROLL ) {
        ret_style |= WS_HSCROLL;
    }
#endif
    if( info->style & GUI_EDIT_INVISIBLE ) {
#ifndef __OS2_PM__
        ret_style |= ES_PASSWORD;
#else
        ret_style |= ES_UNREADABLE;
#endif
    }
    if( info->style & GUI_CONTROL_LEFTNOWORDWRAP ) {
        ret_style |= SS_LEFTNOWORDWRAP;
    }
    if( info->style & GUI_CONTROL_CENTRE ) {
        ret_style |= SS_CENTER;
    }
    if( info->style & GUI_CONTROL_NOPREFIX ) {
#ifndef __OS2_PM__
        ret_style |= SS_NOPREFIX;
#else
        ret_style &= ~DT_MNEMONIC;
#endif
    }
    if( info->style & GUI_CONTROL_MULTILINE ) {
        ret_style |= ES_MULTILINE;
    }
    if( info->style & GUI_CONTROL_WANTRETURN ) {
        ret_style |= ES_WANTRETURN;
    }
    if( info->style & GUI_CONTROL_3STATE ) {
        ret_style |= BS_3STATE;
    }
    if( info->style & GUI_TAB_GROUP ) {
        ret_style |= WS_TABSTOP;
    }

    switch( info->control_class ) {
    case GUI_LISTBOX :
        if( info->style & GUI_CONTROL_NOINTEGRALHEIGHT ) {
            ret_style |= LBS_NOINTEGRALHEIGHT;
        }
        if( info->style & GUI_CONTROL_SORTED ) {
            ret_style |= LBS_SORT;
        }
#ifndef __OS2_PM__
        if( info->style & GUI_CONTROL_WANTKEYINPUT ) {
            ret_style |= LBS_WANTKEYBOARDINPUT;
        }
#endif
#ifdef __OS2_PM__
        if( info->scroll & GUI_HSCROLL ) {
            ret_style |= LS_HORZSCROLL;
        }
#endif
        break;
    case GUI_COMBOBOX :
    case GUI_EDIT_COMBOBOX :
        if( info->style & GUI_CONTROL_NOINTEGRALHEIGHT ) {
            ret_style |= CBS_NOINTEGRALHEIGHT;
        }
        if( info->style & GUI_CONTROL_SORTED ) {
            ret_style |= CBS_SORT;
        }
        break;
#ifdef __OS2_PM__
    case GUI_EDIT:
        if( info->style & GUI_CONTROL_READONLY ) {
            ret_style |= ES_READONLY;
        }
        break;
    case GUI_EDIT_MLE:
        if( info->style & GUI_CONTROL_READONLY ) {
            ret_style |= MLS_READONLY;
        }
        break;
#else
    case GUI_EDIT:
    case GUI_EDIT_MLE:
        if( info->style & GUI_CONTROL_READONLY ) {
            ret_style |= ES_READONLY;
        }
        break;
#endif
    }

    return( ret_style );
}

static HWND CreateControl( gui_control_info *info, gui_window *parent,
                           gui_coord pos, gui_coord size )
{
    DWORD       style;
    HWND        hwnd;
    char        *new_text;
    void        *pctldata;
#ifdef __OS2_PM__
    ENTRYFDATA  edata;
#endif
#if defined(__NT__) || defined(WILLOWS)
    DWORD       xstyle;
    char        *classname;
#endif

    pctldata = NULL;
    new_text = _wpi_menutext2pm( info->text );

    style = GUISetControlStyle( info );
    if( info->text != NULL ) {
        switch( info->control_class ) {
        case GUI_LISTBOX :
            style |= WS_CAPTION;
            break;
        }
    }

#ifdef __OS2_PM__
    if( info->control_class == GUI_EDIT ) {
        edata.cb = sizeof(ENTRYFDATA);
        edata.cchEditLimit = 2048;
        edata.ichMinSel = 0;
        edata.ichMaxSel = 0;
        pctldata = &edata;
    }
#endif

    style &= ~WS_VISIBLE;  /* create invisible -- if GUIAlloc fails, window
                            * will never show
                            */
    if( parent != NULL ) {
        style |= WS_CHILD;
    }

#if defined(__NT__) || defined(WILLOWS)
    #if !defined(WS_EX_CLIENTEDGE)
    #define WS_EX_CLIENTEDGE    0x00000200L
    #endif

    xstyle = 0L;
    // We do this crud to get 3d edges on edit controls, listboxes, and
    // comboboxes -rnk 07/07/95

    if( LOBYTE(LOWORD(GetVersion())) >= 4) {
        /* In W95 and later we don't want this crud any more... RR 2003.12.8 */

        classname = GUIControls[info->control_class].classname;
        if( lstrcmpi( classname, "Edit" ) == 0 ||
            lstrcmpi( classname, "Listbox" ) == 0 ||
            lstrcmpi( classname, "Combobox" ) == 0 ) {
            xstyle = WS_EX_CLIENTEDGE;
        }
    }

    hwnd = CreateWindowEx( xstyle, GUIControls[info->control_class].classname,
        new_text, style, pos.x, pos.y, size.x, size.y, parent->hwnd,
        (HMENU)info->id, GUIMainHInst, pctldata );

    /* From here to #else, new by RR 2003.12.05 */

    if ( hwnd != NULL ) {
        /* Set the standard font for the new control                */
        /* Use system supplied font, so we do not need to worry     */
        /* about cleaning it up later (no DeleteObject() necessary) */
        HFONT setFont;

        if( LOBYTE(LOWORD(GetVersion())) >= 4 ) {
            /* New shell active, Win95 or later */
            setFont = (HFONT) GetStockObject( DEFAULT_GUI_FONT );
        } else {
            /* MSDN on net tells SYSTEM_FONT should be Tahoma on W2K    */
            /* and later. Does not appear to be correct (tested XP SP1) */
            setFont = (HFONT) GetStockObject( SYSTEM_FONT );
        }

        SendMessage( hwnd, WM_SETFONT, (WPARAM)setFont, (LPARAM)0 );
    }
#else
    _wpi_createanywindow( GUIControls[info->control_class].classname,
                          new_text, style, pos.x, pos.y, size.x, size.y,
                          parent->hwnd, (HMENU)info->id, GUIMainHInst,
                          pctldata, &hwnd, info->id, &hwnd );
#endif

    if( new_text ) {
        _wpi_freemenutext( new_text );
    }

    return( hwnd );
}

/*
 * GUIAddControl - add the given control to the parent window
 */

bool GUIAddControl( gui_control_info *info, gui_colour_set *plain,
                    gui_colour_set *standout )
{
    gui_coord           pos;
    gui_coord           size;
    HWND                hwnd;
    control_item        *item;
    gui_window          *parent;

    plain = plain;
    standout = standout;
    parent = info->parent;
    GUICalcLocation( &info->rect, &pos, &size, parent->hwnd );
    hwnd = CreateControl( info, parent, pos, size );
    if( hwnd == NULLHANDLE ) {
        return( FALSE );
    }
    item = GUIControlInsert( parent, info->control_class, hwnd, info, NULL );
    if( item == NULL ) {
        return( FALSE );
    }
    if( GUIGetCtrlWnd( parent->hwnd ) == NULL ) {
        if( !GUIInsertCtrlWnd( parent ) ) {
            GUIControlDelete( parent, info->id );
            return( FALSE );
        }
    }
    GUIInitControl( item, parent, NULL );
#ifndef __OS2_PM__
    if( info->control_class == GUI_DEFPUSH_BUTTON ) {
        GUISendMessage( parent->hwnd, DM_SETDEFID, info->id, 0 );
    }
#endif
    if( !( info->style & GUI_CONTROL_INIT_INVISIBLE ) ) {
        _wpi_showwindow( hwnd, SW_SHOW );
    }
    return( TRUE );
}

void GUIEnumControls( gui_window *wnd, CONTRENUMCALLBACK *func, void *param )
{
    control_item        *curr;

    for( curr = wnd->controls; curr != NULL; curr = curr->next ) {
        (*func)( wnd, curr->id, param );
    }
}

bool GUICheckRadioButton( gui_window *wnd, unsigned id )
{
    control_item        *curr;
    bool                in_group;
    bool                found_id;
    unsigned            first;
    unsigned            last;
    bool                done;

    first = id;
    last = id;
    in_group = FALSE;
    found_id = FALSE;
    done = FALSE;
    for( curr = wnd->controls; !done && ( curr != NULL ); curr = curr->next ) {
        if( curr->id == id ) {
            found_id = TRUE;
        }
        if( curr->style & GUI_GROUP ) {
            in_group = !in_group;
            if( in_group ) {
                first = curr->id;
            } else {
                last = curr->id;
                if( found_id ) {
                    done = TRUE;
                }
            }
        }
    }
    if( !done ) {
        return( FALSE );
    }
    in_group = FALSE;
    for( curr = wnd->controls; curr != NULL; curr = curr->next ) {
        if( curr->id == first ) {
            in_group = TRUE;
        }
        if( in_group ) {
            if( curr->id == id ) {
                GUISendMessage( curr->hwnd, BM_SETCHECK, (WPI_PARAM1)TRUE, 0 );
            } else {
                GUISendMessage( curr->hwnd, BM_SETCHECK, (WPI_PARAM1)FALSE, 0 );
            }
        }
        if( curr->id == last ) {
            return( TRUE );
        }
    }
    return( FALSE );
}

bool GUIDeleteControl( gui_window *wnd, unsigned id )
{
    control_item        *control;

    control = GUIGetControlByID( wnd, id );
    if( control != NULL ) {
        _wpi_destroywindow( control->hwnd );
        GUIControlDelete( wnd, id );
        return( TRUE );
    }
    return( FALSE );
}

bool GUILimitEditText( gui_window *wnd, unsigned id, int len )
{
    control_item        *control;
    HWND                hwnd;

    control = GUIGetControlByID( wnd, id );
    if( control != NULL ) {
        hwnd = _wpi_getdlgitem( wnd->hwnd, id );
        if( control->control_class == GUI_EDIT ) {
            _wpi_sendmessage( hwnd, EM_LIMITTEXT, len, 0 );
#ifdef __OS2_PM__
        } else if( len > 0  &&  control->control_class == GUI_EDIT_MLE ) {
            _wpi_sendmessage( hwnd, MLM_SETTEXTLIMIT, len, 0 );
#endif
        }
        return( TRUE );
    }

    return( FALSE );
}

static void ShowControl( gui_window *wnd, unsigned id, int show_flag )
{
    control_item        *control;

    control = GUIGetControlByID( wnd, id );
    if( control != NULL ) {
        _wpi_showwindow( control->hwnd, show_flag );
    }
}

void GUIHideControl( gui_window *wnd, unsigned id )
{
    ShowControl( wnd, id, SW_HIDE );
}

void GUIShowControl( gui_window *wnd, unsigned id )
{
    ShowControl( wnd, id, SW_SHOW );
}

bool GUIIsControlVisible( gui_window *wnd, unsigned id )
{
    control_item        *control;

    control = GUIGetControlByID( wnd, id );
    if( control != NULL ) {
        if( _wpi_iswindowvisible( control->hwnd ) ) {
            return( TRUE );
        }
    }
    return( FALSE );
}

⌨️ 快捷键说明

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