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

📄 button.c

📁 在ADS环境下MiniGUI的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
** $Id: button.c,v 1.58 2004/06/26 07:49:31 weiym Exp $
**
** button.c: the Button Control module.
**
** Copyright (C) 2003 Feynman Software.
** Copyright (C) 1999 ~ 2002 Wei Yongming.
**
** Current maintainer: Wei Yongming.
**
** Note:
**   Originally by Zhao Jianghua.
**
** Create date: 1999/8/23
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
** Modify records:
**
**  Who             When        Where       For What                Status
**-----------------------------------------------------------------------------
**  WEI Yongming    1999/10/27  Tsinghua    Notify Message          Fininshed
**  WEI Yongming    2000/02/24  Tsinghua    Add MPL License         Finished
**
** TODO:
*/ 

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

#include "common.h"
#include "minigui.h"
#include "gdi.h"
#include "window.h"
#include "control.h"
#include "cliprect.h"
#include "internals.h"
#include "ctrlclass.h"

#ifdef _CTRL_BUTTON

#include "button.h"
#include "ctrlmisc.h"

#ifdef _FLAT_WINDOW_STYLE

#define BTN_WIDTH_BORDER    1

#ifndef _GRAY_SCREEN
#define myDrawButton(hdc, hwnd, x0, y0, x1, y1, flags, fillc) \
                 DrawFlatControlFrameEx(hdc, hwnd, x0, y0, x1, y1, 0, flags, fillc)
#else
#define myDrawButton(hdc, hwnd, x0, y0, x1, y1, flags, fillc) \
                 DrawFlatControlFrameEx(hdc, hwnd, x0, y0, x1, y1, 3, flags, fillc)
#endif

#define FocusRect(hdc, x, y, w, h)

#elif defined (_PHONE_WINDOW_STYLE)

#define BTN_WIDTH_BORDER    2

#define myDrawButton btnDrawPhoneButton

#define FocusRect(hdc, x, y, w, h)

#else

#define BTN_WIDTH_BORDER    4

#define myDrawButton Draw3DControlFrameEx

#endif

#ifdef _PHONE_WINDOW_STYLE
#   define BTN_WIDTH_BMP       16
#   define BTN_HEIGHT_BMP      16
#   define BTN_INTER_BMPTEXT   2
#else
#   define BTN_WIDTH_BMP       14
#   define BTN_HEIGHT_BMP      13
#   define BTN_INTER_BMPTEXT   2
#endif

#define BUTTON_STATUS(pCtrl)   (((PBUTTONDATA)(pCtrl->dwAddData2))->status)
#define BUTTON_DATA(pCtrl)     (((PBUTTONDATA)(pCtrl->dwAddData2))->data)

static const BITMAP* bmp_button;

#define BUTTON_BMP              bmp_button

#ifdef _PHONE_WINDOW_STYLE

static const BITMAP* bmp_pushbutton;
static const BITMAP* bmp_pushedbutton;

#define PUSHBUTTON_BMP          bmp_pushbutton
#define PUSHEDBUTTON_BMP        bmp_pushedbutton
#define BTN_WIDTH_CORNER        2

#endif

static int ButtonCtrlProc (HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam);

BOOL RegisterButtonControl (void)
{
    WNDCLASS WndClass;

#ifdef _PHONE_WINDOW_STYLE
    if ((bmp_button = GetStockBitmap (STOCKBMP_BUTTON, 0, 0)) == NULL)
        return FALSE;

    if ((bmp_pushbutton = GetStockBitmap (STOCKBMP_PUSHBUTTON, 0, 0)) == NULL)
        return FALSE;

    if ((bmp_pushedbutton = GetStockBitmap (STOCKBMP_PUSHEDBUTTON, 0, 0)) == NULL)
        return FALSE;
#else
    if ((bmp_button = GetStockBitmap (STOCKBMP_BUTTON, -1, -1)) == NULL)
        return FALSE;
#endif

    WndClass.spClassName = CTRL_BUTTON;
    WndClass.dwStyle     = WS_NONE;
    WndClass.dwExStyle   = WS_EX_NONE;
    WndClass.hCursor     = GetSystemCursor (IDC_ARROW);
    WndClass.iBkColor    = GetWindowElementColor (BKC_CONTROL_DEF);
    WndClass.WinProc     = ButtonCtrlProc;

    return AddNewControlClass (&WndClass) == ERR_OK;
}

#if 0
void ButtonControlCleanup (void)
{
    // do nothing.
}
#endif

#ifdef _PHONE_WINDOW_STYLE

static void btnDrawPhoneButton (HDC hdc, HWND hwnd, int l, int t, int r, int b, DWORD flags, gal_pixel fillc)
{
    int x;
    const BITMAP* bmp = ((flags & DF_3DBOX_STATEMASK) == DF_3DBOX_NORMAL)?PUSHBUTTON_BMP:PUSHEDBUTTON_BMP;

    FillBoxWithBitmapPart (hdc, l, t,
                BTN_WIDTH_CORNER, bmp_pushbutton->bmHeight,
                0, 0, bmp, 0, 0);

    for (x = l + BTN_WIDTH_CORNER; x < r - BTN_WIDTH_CORNER; x += BTN_WIDTH_CORNER) {
        FillBoxWithBitmapPart (hdc, x, t,
                BTN_WIDTH_CORNER, bmp_pushbutton->bmHeight,
                0, 0, bmp, BTN_WIDTH_CORNER, 0);
    }

    FillBoxWithBitmapPart (hdc, r - BTN_WIDTH_CORNER, t,
                BTN_WIDTH_CORNER, bmp_pushbutton->bmHeight,
                0, 0, bmp, bmp_pushbutton->bmWidth - BTN_WIDTH_CORNER, 0);
}

#endif

static void btnGetRects (HWND hWnd, DWORD dwStyle,
                                    RECT* prcClient, 
                                    RECT* prcText, 
                                    RECT* prcBitmap)
{
    GetClientRect (hWnd, prcClient);
#ifndef _PHONE_WINDOW_STYLE
    prcClient->right --;
    prcClient->bottom --;
#endif

    SetRect (prcText, (prcClient->left   + BTN_WIDTH_BORDER),
                      (prcClient->top    + BTN_WIDTH_BORDER),
                      (prcClient->right  - BTN_WIDTH_BORDER),
                      (prcClient->bottom - BTN_WIDTH_BORDER));

    SetRectEmpty (prcBitmap);

    if ( ((dwStyle & BS_TYPEMASK) < BS_CHECKBOX)
            || (dwStyle & BS_PUSHLIKE))
        return;

    if (dwStyle & BS_LEFTTEXT) {
        SetRect (prcText, prcClient->left + 1,
                          prcClient->top + 1,
                          prcClient->right - BTN_WIDTH_BMP - BTN_INTER_BMPTEXT,
                          prcClient->bottom - 1);
        SetRect (prcBitmap, prcClient->right - BTN_WIDTH_BMP,
                            prcClient->top + 1,
                            prcClient->right - 1,
                            prcClient->bottom - 1);
    }
    else {
        SetRect (prcText, prcClient->left + BTN_WIDTH_BMP + BTN_INTER_BMPTEXT,
                          prcClient->top + 1,
                          prcClient->right - 1,
                          prcClient->bottom - 1);
        SetRect (prcBitmap, prcClient->left + 1,
                            prcClient->top + 1,
                            prcClient->left + BTN_WIDTH_BMP,
                            prcClient->bottom - 1);
    }

}

static void btnGetTextBoundsRect (PCONTROL pCtrl, HDC hdc, DWORD dwStyle, 
                const RECT* prcText, RECT* prcBounds)
{
    UINT uFormat;
  
    *prcBounds = *prcText;

    if (dwStyle & BS_MULTLINE)
        uFormat = DT_WORDBREAK;
    else
        uFormat = DT_SINGLELINE;

    if ((dwStyle & BS_TYPEMASK) == BS_PUSHBUTTON
            || (dwStyle & BS_TYPEMASK) == BS_DEFPUSHBUTTON
            || (dwStyle & BS_PUSHLIKE))
        uFormat |= DT_CENTER | DT_VCENTER;
    else {
        uFormat = DT_TOP | DT_LEFT;

        if ((dwStyle & BS_ALIGNMASK) == BS_LEFT)
            uFormat = DT_WORDBREAK | DT_LEFT;
        else if ((dwStyle & BS_ALIGNMASK) == BS_RIGHT)
            uFormat = DT_WORDBREAK | DT_RIGHT;
        else if ((dwStyle & BS_ALIGNMASK) == BS_CENTER)
            uFormat = DT_WORDBREAK | DT_CENTER;
            
        if ((dwStyle & BS_ALIGNMASK) == BS_TOP)
            uFormat = DT_SINGLELINE | DT_TOP;
        else if ((dwStyle & BS_ALIGNMASK) == BS_BOTTOM)
            uFormat = DT_SINGLELINE | DT_BOTTOM;
        else if ((dwStyle & BS_ALIGNMASK) == BS_VCENTER)
            uFormat = DT_SINGLELINE | DT_VCENTER;
    }

    uFormat |= DT_CALCRECT;
    
    DrawText (hdc, pCtrl->spCaption, -1, prcBounds, uFormat);
}

static void btnPaintContent (PCONTROL pCtrl, HDC hdc, DWORD dwStyle, 
                             RECT* prcText)
{
    BOOL pushbutton = FALSE;

    switch (dwStyle & BS_CONTENTMASK)
    {
        case BS_TEXT:
        case BS_LEFTTEXT:
        {
            UINT uFormat;
            
            if (dwStyle & BS_MULTLINE)
                uFormat = DT_WORDBREAK;
            else
                uFormat = DT_SINGLELINE;

            if ((dwStyle & BS_TYPEMASK) == BS_PUSHBUTTON
                    || (dwStyle & BS_TYPEMASK) == BS_DEFPUSHBUTTON
                    || (dwStyle & BS_PUSHLIKE)) {
                pushbutton = TRUE;
                uFormat |= DT_CENTER | DT_VCENTER;
            }
            else {
                uFormat = DT_TOP | DT_LEFT;

                if ((dwStyle & BS_ALIGNMASK) == BS_LEFT)
                    uFormat = DT_WORDBREAK | DT_LEFT;
                else if ((dwStyle & BS_ALIGNMASK) == BS_RIGHT)
                    uFormat = DT_WORDBREAK | DT_RIGHT;
                else if ((dwStyle & BS_ALIGNMASK) == BS_CENTER)
                    uFormat = DT_WORDBREAK | DT_CENTER;
            
                if ((dwStyle & BS_ALIGNMASK) == BS_TOP)
                    uFormat = DT_SINGLELINE | DT_TOP;
                else if ((dwStyle & BS_ALIGNMASK) == BS_BOTTOM)
                    uFormat = DT_SINGLELINE | DT_BOTTOM;
                else if ((dwStyle & BS_ALIGNMASK) == BS_VCENTER)
                    uFormat = DT_SINGLELINE | DT_VCENTER;
            }
            
            SetBkColor (hdc, GetWindowBkColor ((HWND)pCtrl));
            if (dwStyle & WS_DISABLED) {
                RECT rc = *prcText;
                
                SetBkMode (hdc, BM_TRANSPARENT);
#if 0
                SetTextColor (hdc, PIXEL_lightwhite);
                OffsetRect (prcText, 2, 2);
                DrawText (hdc, pCtrl->spCaption, -1, prcText, uFormat);
#endif
                SetTextColor (hdc, GetWindowElementColorEx ((HWND)pCtrl, FGC_CONTROL_DISABLED));
                DrawText (hdc, pCtrl->spCaption, -1, &rc, uFormat);
            }
            else {
                SetBkMode (hdc, BM_TRANSPARENT);
                if (pushbutton && (BUTTON_STATUS(pCtrl) & BST_PUSHED
                            || BUTTON_STATUS(pCtrl) & BST_CHECKED)) {
                    SetTextColor (hdc, GetWindowElementColorEx ((HWND)pCtrl, FGC_BUTTON_PUSHED));
                    SetBkColor (hdc, GetWindowElementColorEx ((HWND)pCtrl, FGC_BUTTON_NORMAL));
                }
                else {
                    SetTextColor (hdc, GetWindowElementColorEx ((HWND)pCtrl, FGC_BUTTON_NORMAL));
                    SetBkColor (hdc, GetWindowElementColorEx ((HWND)pCtrl, FGC_BUTTON_PUSHED));
                }
                OffsetRect (prcText, 1, 1);
                DrawText (hdc, pCtrl->spCaption, -1, prcText, uFormat);
            }
        }
        break;
        
        case BS_BITMAP:
            if (BUTTON_DATA(pCtrl)) {
                int x = prcText->left;
                int y = prcText->top;
                int w = RECTWP (prcText);
                int h = RECTHP (prcText);
                PBITMAP bmp = (PBITMAP)(BUTTON_DATA(pCtrl));

                if (dwStyle & BS_REALSIZEIMAGE) {
                    x += (w - bmp->bmWidth) >> 1;
                    y += (h - bmp->bmHeight) >> 1;
                    w = h = 0;
                }
                FillBoxWithBitmap (hdc, x, y, w, h, bmp);
            }
        break;
         
        case BS_ICON:
            if (BUTTON_DATA(pCtrl)) {
                int x = prcText->left;
                int y = prcText->top;
                int w = RECTWP (prcText);
                int h = RECTHP (prcText);
                HICON icon = (HICON)(BUTTON_DATA(pCtrl));

                if (dwStyle & BS_REALSIZEIMAGE) {
                    int iconw, iconh;

                    GetIconSize (icon, &iconw, &iconh);
                    x += (w - iconw) >> 1;
                    y += (h - iconh) >> 1;
                    w = h = 0;
                }

                DrawIcon (hdc, x, y, w, h, icon);
            }
        break;
    }
}

static void btnPaintNormalButton (PCONTROL pCtrl, HDC hdc, DWORD dwStyle,
                RECT* prcClient, RECT* prcText, RECT* prcBitmap)
{
    int bmp_left = prcBitmap->left;
    int bmp_top = prcBitmap->top + (prcBitmap->bottom - BTN_HEIGHT_BMP) / 2;

    switch (dwStyle & BS_TYPEMASK)
    {
        case BS_DEFPUSHBUTTON:
#ifndef _FLAT_WINDOW_STYLE
            myDrawButton (hdc, (HWND)pCtrl, prcClient->left, prcClient->top,
                                prcClient->right, prcClient->bottom, 
                                DF_3DBOX_NORMAL | DF_3DBOX_FILL, 
                                GetWindowBkColor ((HWND)pCtrl));
#else
            SetPenColor (hdc, GetWindowElementColorEx ((HWND)pCtrl, FGC_CONTROL_NORMAL));
            Rectangle (hdc, prcClient->left,
                            prcClient->top,
                            prcClient->right,
                            prcClient->bottom);
            myDrawButton (hdc, (HWND)pCtrl, prcClient->left + 1, prcClient->top + 1,
                                prcClient->right - 1, prcClient->bottom - 1, 
                                DF_3DBOX_NORMAL | DF_3DBOX_FILL, 
                                GetWindowBkColor ((HWND)pCtrl));
#endif
        break;

        case BS_PUSHBUTTON:
            myDrawButton (hdc, (HWND)pCtrl, prcClient->left, prcClient->top,
                                prcClient->right, prcClient->bottom, 
                                DF_3DBOX_NORMAL | DF_3DBOX_FILL,
                                GetWindowBkColor ((HWND)pCtrl));
        break;

        case BS_AUTORADIOBUTTON:
        case BS_RADIOBUTTON:
            if (dwStyle & BS_PUSHLIKE) {
                myDrawButton (hdc, (HWND)pCtrl, prcClient->left, prcClient->top,
                                prcClient->right, prcClient->bottom, 
                                DF_3DBOX_NORMAL | DF_3DBOX_FILL, 
                                GetWindowBkColor ((HWND)pCtrl));
                break;
            }

            if (dwStyle & WS_DISABLED) {
                FillBoxWithBitmapPart (hdc, bmp_left, bmp_top,
                                       BTN_WIDTH_BMP, BTN_HEIGHT_BMP,
                                       0, 0,
                                       BUTTON_BMP,
                                       (BTN_WIDTH_BMP * 2), BTN_HEIGHT_BMP);
            }
            else {

⌨️ 快捷键说明

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