⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wptoolbr.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Toolbar window class for Windows and OS/2.
*
****************************************************************************/


#ifdef __OS2_PM__
#define INCL_PM
#define INCL_WINFRAMEMGR
#define INCL_NLS
#define INCL_GPILCIDS
#define INCL_GPIPRIMITIVES
#include <os2.h>
#define __FAR
#else
#include <windows.h>
#endif

#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include "wpi.h"
#include "mem.h"
#include "wptoolbr.h"
#define TOOLBR_DIM  WPI_RECTDIM

typedef struct tool {
    struct tool *next;
    union {
        HBITMAP bitmap;
        WORD    blank_space;
    } u;                        // For compatability with compilers that
                                // don't support anonymous unions
    HBITMAP     depressed;
    WORD        id;
    UINT        flags;
    WORD        state;
    WPI_RECT    area;
} tool;

typedef struct toolbar {
    HWND        hwnd;
    HWND        owner;          // this is actually the parent
    toolhook    hook;
    helphook    helphook;
    WPI_POINT   button_size;
    WPI_POINT   border;
    HBITMAP     background;
    HBRUSH      foreground;
    HBRUSH      bgbrush;
    int         border_width;
    tool        *tool_list;
    char        is_fixed:1;
    char        spare:7;
} toolbar;

#define HNULL                   0

#define BORDER_WIDTH( bar )     1

#ifndef __OS2_PM__
#ifdef __WINDOWS_386__
    #define MAKEPTR( a ) ((void far *)MK_FP32( (void *) a ))
#else
    #define MAKEPTR( a ) ((LPVOID) a)
#endif
#endif

#define BLANK_SIZE( w ) ( (w) / 3 )
#define GET_INFO( w )   ((toolbar *)_wpi_getwindowlong( w, 0 ))
#define SET_INFO( w,i ) (_wpi_setwindowlong( w, 0, (LONG)(LPSTR)i))

static char     toolBarClassRegistered;
static char     *className = "WTool";

static char     gdiObjectsCreated;
static HPEN     blackPen;
static HPEN     btnShadowPen;
static HPEN     btnHighlightPen;
static HPEN     btnFacePen;
static HBRUSH   blackBrush;
static HBRUSH   btnFaceBrush;
static COLORREF btnColour;
static WPI_INST appInst;

static tool     *currTool;
static char     currIsDown;
static WORD     lastID = -1;
static BOOL     mouse_captured = FALSE;
static BOOL     ignore_mousemove = FALSE; // release_capture generates a
                                          // WM_MOUSEMOVE msg
#ifndef __OS2_PM__
static BOOL     round_corners = TRUE;     // Platform has rounded buttons?
#else
static BOOL     round_corners = FALSE;    // Platform has rounded buttons?
#endif

MRESULT CALLBACK ToolBarWndProc( HWND, WPI_MSG, WPI_PARAM1, WPI_PARAM2 );
#if defined(__NT__) || defined(__WINDOWS__)
void WPTB_TransparentBlt (HDC hDC,   UINT x, UINT y, UINT width, UINT height,
                          HDC hDCIn, COLORREF cr);
#endif

static void toolbardestroywindow( HWND hwnd )
{
    hwnd = _wpi_getframe( hwnd );
    _wpi_destroywindow( hwnd );
}

/*
 * findTool - find tool item based on id
 */
static tool *findTool( tool *list, WORD id )
{
    while( list != NULL ) {
        if( list->id == id ) break;
        list = list->next;
    }
    return( list );

} /* findTool */

/*
 * addTool - add an item to the tool bar list
 */
static void addTool( tool **list, tool *t )
{
    tool        *curr, **last;

    last = list;
    for( curr = *list; curr != NULL; curr = curr->next ) {
        last = &curr->next;
    }
    *last = t;
    t->next = NULL;

} /* addTool */

/*
 * deleteTool - delete an item from the tool bar list
 */
static void deleteTool( tool **list, tool *t )
{
    tool        *curr, **last;

    last = list;
    for( curr = *list; curr != NULL; curr = curr->next ) {
        if( curr == t ) {
            *last = t->next;
            t->next = NULL;
            break;
        }
        last = &curr->next;
    }

} /* deleteTool */

/*
 * buttonPosition - get position of a button on the tool bar
 */
static BOOL buttonPosition( HWND hwnd, toolbar *bar, tool *top, WPI_POINT *p )
{
    WPI_RECT    rect;
    int         width, height;
    int         wndheight;
    tool        *curr;
    WPI_POINT   pos;

    _wpi_getclientrect( hwnd, &rect );
    width = _wpi_getwidthrect( rect ) - 2 * bar->border.x;
    height = _wpi_getheightrect( rect ) - 2 * bar->border.y;
    wndheight = _wpi_getheightrect( rect );
    curr = bar->tool_list;

    pos.y = 0;
    while( pos.y + bar->button_size.y <= height ) {
        pos.x = 0;
        while( pos.x + bar->button_size.x <= width ) {
            // we assert curr because top MUST be in the list - the only
            // way curr can be NULL is if top is NULL (bad) or not in the
            // list (also bad).
            assert( curr != NULL );
            if( curr == top ) {
                p->x = pos.x + bar->border.x;
#ifndef __OS2_PM__
                p->y = pos.y + bar->border.y;
#else
                p->y = _wpi_cvth_y(pos.y, wndheight) -
                                        bar->button_size.y - bar->border.y+1;
#endif
                return( TRUE );
            }
            if( curr->flags & ITEM_BLANK ) {
                pos.x += curr->u.blank_space;
            } else {
                pos.x += bar->button_size.x-1;
            }
            curr = curr->next;
        }
        pos.y += bar->button_size.y-1;
    }
    return( FALSE );

} /* buttonPosition */

/*
 * createButtonList - create all buttons on a tool bar
 */
static void createButtonList( HWND hwnd, toolbar *bar, tool *top )
{
    WPI_POINT   pos;

    // top must be an element in the tool_list hanging off of bar
    // we are going to create buttons for all the tools from top
    // to the end of the list
    while( top != NULL ) {
        if( !buttonPosition( hwnd, bar, top, &pos ) ) {
            // no more buttons will fit
            break;
        }
        _wpi_setrectvalues( &(top->area), pos.x, pos.y,
                                                pos.x + bar->button_size.x,
                                                pos.y + bar->button_size.y );
        top = top->next;
    }

} /* createButtonList */

void ToolBarRedrawButtons( struct toolbar *bar )
{
    if( bar ) {
        createButtonList( bar->hwnd, bar, bar->tool_list );
    }
}

/*
 * ToolBarInit - initialize the tool bar
 */
toolbar *ToolBarInit( HWND parent )
{
    COLORREF    clr_btnface;
    COLORREF    clr_btnshadow;
    COLORREF    clr_btnhighlight;
    COLORREF    clr_black;
    toolbar     *bar;
#ifndef __OS2_PM__
        /*
         ****************
         * Windows version of initialization
         ****************
         */
    WNDCLASS    wc;
    HANDLE      instance;

    instance = GET_HINSTANCE( parent );
    appInst = instance;

    if( !toolBarClassRegistered ) {
        wc.style = CS_SAVEBITS | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
        wc.lpfnWndProc = (LPVOID) ToolBarWndProc;
        wc.lpszMenuName = NULL;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = sizeof( LPVOID );
        wc.hInstance = instance;
        wc.hIcon = HNULL;
        wc.hCursor = LoadCursor( (HANDLE) HNULL, IDC_ARROW );
        wc.hbrBackground = (HBRUSH) 0; // (COLOR_BTNFACE + 1);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = className;
        RegisterClass( &wc );
        toolBarClassRegistered = TRUE;
    }
    clr_btnshadow = GetSysColor( COLOR_BTNSHADOW );
    clr_btnhighlight = GetSysColor( COLOR_BTNHIGHLIGHT );
    btnColour = GetSysColor( COLOR_BTNFACE );
    clr_btnface = btnColour;
#if defined(__NT__) || defined(__WINDOWS__)
    clr_black = GetSysColor(COLOR_BTNTEXT);
#endif
#if defined(__NT__)
    {
        OSVERSIONINFO os;

        GetVersionEx(&os);
        if ( os.dwMajorVersion == 4 || (os.dwMajorVersion == 5 && os.dwMinorVersion == 0)) {
            // round_corners = FALSE;
            // Later, when drawing code is adapted
        }
    }
#else
    clr_black = RGB(0, 0, 0);
#endif
#else
        /*
         ******************
         * PM Version of the initialization
         ******************
         */
    int         rc;
    HAB         hab;

    hab = WinQueryAnchorBlock( parent );
    appInst.hab = hab;
    appInst.mod_handle = NULL;

    if( !toolBarClassRegistered ) {
        rc = WinRegisterClass( hab, className,
                                (PFNWP)ToolBarWndProc,
                                CS_MOVENOTIFY | CS_SIZEREDRAW | CS_CLIPSIBLINGS,
                                sizeof(PVOID)  );
        toolBarClassRegistered = TRUE;
    }
    clr_btnshadow = CLR_DARKGRAY;
    clr_btnhighlight = CLR_WHITE;
    clr_btnface = CLR_PALEGRAY;
    clr_black = CLR_BLACK;
    btnColour = CLR_PALEGRAY;
#endif

    bar = (toolbar *)MemAlloc( sizeof( toolbar ) );
    if ( bar ) {
        memset ( bar, 0, sizeof( toolbar ) );
        bar->border_width = 1;
        bar->owner = parent;
    }

    if( !gdiObjectsCreated ) {
        blackPen = _wpi_createpen( PS_SOLID, BORDER_WIDTH(bar), clr_black );
        btnShadowPen = _wpi_createpen( PS_SOLID, BORDER_WIDTH(bar), clr_btnshadow );
        btnHighlightPen = _wpi_createpen( PS_SOLID, BORDER_WIDTH(bar), clr_btnhighlight );
        btnFacePen = _wpi_createpen( PS_SOLID, BORDER_WIDTH(bar), clr_btnface );
        blackBrush = _wpi_createsolidbrush( clr_black );
        btnFaceBrush = _wpi_createsolidbrush( clr_btnface );
        gdiObjectsCreated = TRUE;
    }

    return( bar );

} /* ToolBarInit */

/*
 * ToolBarFini - done with the tool bar
 */
void ToolBarDestroy ( toolbar *bar )
{
    tool        *curr, *tmp;

    if ( bar ) {
        if( bar->hwnd != HNULL ) {
            toolbardestroywindow( bar->hwnd );
        }
        curr = bar->tool_list;
        while( curr != NULL ) {
            tmp = curr;
            curr = curr->next;
            MemFree( tmp );
        }
        if( bar->bgbrush != NULL ) {
            _wpi_deleteobject( bar->bgbrush );
        }
    }

    MemFree( bar );
}

/*
 * ToolBarFini - done with all tool bars
 */
void ToolBarFini( toolbar *bar )
{
    ToolBarDestroy ( bar );

    if( gdiObjectsCreated ) {
        _wpi_deleteobject( blackPen );
        _wpi_deleteobject( btnShadowPen );
        _wpi_deleteobject( btnHighlightPen );
        _wpi_deleteobject( btnFacePen );
        _wpi_deleteobject( blackBrush );
        _wpi_deleteobject( btnFaceBrush );
        gdiObjectsCreated = FALSE;
    }

} /* ToolBarFini */

/*
 * ToolBarAddItem - add a specific bitmap to the tool bar
 */
void ToolBarAddItem( toolbar *bar, TOOLITEMINFO *info )
{
    tool        *t;

    t = (tool *)MemAlloc( sizeof( tool ) );
    if( info->flags & ITEM_BLANK ) {
        t->u.blank_space = info->u.blank_space;
    } else {
        t->u.bitmap = info->u.bmp;
    }
    t->id = info->id;
    t->next = NULL;
    t->flags = info->flags;
    t->depressed = info->depressed;
    t->state = BUTTON_UP;
    addTool( &bar->tool_list, t );
    if( !(info->flags & ITEM_BLANK) && info->u.bmp != HNULL ) {
        createButtonList( bar->hwnd, bar, t );
    }

} /* ToolBarAddItem */

void ToolBarSetState( toolbar *bar, WORD id, WORD state )
{
    tool        *t;

    t = findTool( bar->tool_list, id );
    t->state = state;
    // force the button to be redrawn
    _wpi_invalidaterect( bar->hwnd, &t->area, FALSE );
}

WORD ToolBarGetState( toolbar *bar, WORD id )
{
    tool        *t;

    t = findTool( bar->tool_list, id );
    return( t->state );
}

/*
 * ToolBarDeleteItem - delete an item from the tool bar
 */

⌨️ 快捷键说明

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