📄 toolbr.c
字号:
/****************************************************************************
*
* 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 class for Windows.
*
****************************************************************************/
#include <windows.h>
#include <string.h>
#include <assert.h>
#include "mem.h"
#include "toolbr.h"
#include "win1632.h"
typedef struct tool {
struct tool *next;
union {
HBITMAP bitmap;
WORD blank_space;
};
HBITMAP depressed;
UINT id;
UINT flags;
WORD state;
RECT area;
} tool;
typedef struct toolbar {
HWND hwnd;
HWND owner;
toolhook hook;
helphook helphook;
POINT button_size;
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
#ifdef __WINDOWS_386__
#define __FAR__ __far
#define MAKEPTR( a ) ((void far *)MK_FP32( (void *) a ))
#else
#define __FAR__
#define MAKEPTR( a ) ((LPVOID) a)
#endif
#if defined(__WINDOWS_386__)
#define WINEXP FAR PASCAL
#elif defined(__NT__)
#define WINEXP __export __stdcall
#else
#define WINEXP __export FAR PASCAL
#endif
#define BLANK_SIZE( w ) ( (w) / 3 )
#define GET_INFO( w ) ((toolbar *)GetWindowLong( w, 0 ))
#define SET_INFO( w,i ) (SetWindowLong( w, 0, (LONG)(LPSTR)i))
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 crButtonFace;
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
#if defined(__NT__) || defined(__WINDOWS__)
void TB_TransparentBlt( HDC, UINT, UINT, UINT, UINT, HDC, COLORREF );
#endif
LONG WINEXP ToolBarWndProc( HWND, unsigned, UINT, LONG );
/*
* 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, POINT *p )
{
RECT rect;
int width, height;
tool *curr;
POINT pos;
GetClientRect( hwnd, &rect );
width = rect.right - rect.left - 2 * bar->border.x;
height = rect.bottom - rect.top - 2 * bar->border.y;
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;
p->y = pos.y + bar->border.y;
return( TRUE );
}
if( curr->flags & ITEM_BLANK ) {
pos.x += curr->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 )
{
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;
}
top->area.top = pos.y;
top->area.left = pos.x;
top->area.bottom = pos.y + bar->button_size.y;
top->area.right = pos.x + bar->button_size.x;
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 )
{
WNDCLASS wc;
toolbar *bar;
HANDLE instance;
instance = GET_HINSTANCE( parent );
if( !GetClassInfo( instance, className, &wc ) ) {
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = (WNDPROC)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;
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
RegisterClass( &wc );
}
bar = (toolbar *)MemAlloc( sizeof( toolbar ) );
if ( bar ) {
bar->border_width = 1;
bar->owner = parent;
}
if( !gdiObjectsCreated ) {
blackPen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNTEXT ) );
btnShadowPen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNSHADOW ) );
btnHighlightPen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNHIGHLIGHT ) );
btnFacePen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNFACE ) );
blackBrush = GetStockObject( BLACK_BRUSH );
btnFaceBrush = CreateSolidBrush( GetSysColor( COLOR_BTNFACE ) );
gdiObjectsCreated = TRUE;
}
return( bar );
} /* ToolBarInit */
/*
* ToolBarChangeSysColors - fiddle with what ToolBar believes
* are the system colours.
* new: stopped fiddeling!
*/
void ToolBarChangeSysColors( COLORREF tbFace,
COLORREF tbHighlight, COLORREF tbShadow )
{
if( gdiObjectsCreated ) {
DeleteObject( blackPen );
DeleteObject( btnShadowPen );
DeleteObject( btnHighlightPen );
DeleteObject( btnFacePen );
DeleteObject( btnFaceBrush );
}
blackPen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNTEXT ) );
btnShadowPen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNSHADOW ) );
btnHighlightPen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNHIGHLIGHT ) );
btnFacePen = CreatePen( PS_SOLID, BORDER_WIDTH( bar ),
GetSysColor( COLOR_BTNFACE ) );
crButtonFace = GetSysColor( COLOR_BTNFACE );
btnFaceBrush = CreateSolidBrush( crButtonFace );
gdiObjectsCreated = TRUE;
}
/*
* ToolBarDestroy - done with the tool bar
*/
void ToolBarDestroy ( toolbar *bar )
{
tool *curr, *tmp;
if ( bar ) {
if( bar->hwnd != HNULL ) {
DestroyWindow( bar->hwnd );
}
curr = bar->tool_list;
while( curr != NULL ) {
tmp = curr;
curr = curr->next;
MemFree( tmp );
}
if( bar->bgbrush != NULL ) {
DeleteObject( bar->bgbrush );
}
MemFree( bar );
}
}
/*
* ToolBarFini - done with all tool bars
*/
void ToolBarFini( toolbar *bar )
{
ToolBarDestroy ( bar );
if( gdiObjectsCreated ) {
DeleteObject( blackPen );
DeleteObject( btnShadowPen );
DeleteObject( btnHighlightPen );
DeleteObject( btnFacePen );
DeleteObject( blackBrush );
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->blank_space = info->blank_space;
} else {
t->bitmap = info->bmp;
}
t->id = info->id;
t->next = NULL;
t->flags = info->flags;
t->depressed = info->depressed;
t->state = BUTTON_UP;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -