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

📄 ebedit.c

📁 基于minigui开发的一套图形控件系统
💻 C
📖 第 1 页 / 共 4 页
字号:
// $Id: ebedit.c,v 1.3 2005/07/26 02:28:32 tangjb Exp $
//
// edit.c: the Single Line Edit Control module.
//
// Copyright (C) 1999, 2000, 2001, 2002 Wei Yongming.
// 
// Current maintainer: Wei Yongming.
//

/*
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Library General Public
**  License as published by the Free Software Foundation; either
**  version 2 of the License, or (at your option) any later version.
**
**  This library 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
**  Library General Public License for more details.
**
**  You should have received a copy of the GNU Library General Public
**  License along with this library; if not, write to the Free
**  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
**  MA 02111-1307, USA
*/

/*
**  Alternatively, the contents of this file may be used under the terms 
**  of the Mozilla Public License (the "MPL License") in which case the
**  provisions of the MPL License are applicable instead of those above.
*/

// Note:
//  Although there was a version by Zhao Jianghua, this version of
//  EDIT control is written by Wei Yongming from scratch.
//
// Create date: 1999/8/26
//
// Modify records:
//
//  Who             When        Where       For What                Status
//-----------------------------------------------------------------------------
//  WEI Yongming    2000/02/24  Tsinghua    Add MPL License         Finished
//
//
// TODO:
//    * Selection.
//    * Undo.

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

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "ebedit.h"
#include "ctrlmisc.h"
#include "ebcontrol.h"

static int EBEditCtrlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam);

BOOL RegisterEBEditControl (void)
{
    // This control class has two names: "edit" and "sledit"

    WNDCLASS WndClass;

    WndClass.spClassName = CTRL_EBEDIT;
    WndClass.dwStyle     = WS_NONE;
    WndClass.dwExStyle   = WS_EX_NONE;
    WndClass.hCursor     = GetSystemCursor (IDC_IBEAM);
    WndClass.iBkColor    = PIXEL_lightwhite;
    WndClass.WinProc     = EBEditCtrlProc;

    return RegisterWindowClass (&WndClass);
}

void EBEditControlCleanup (void)
{

    UnregisterWindowClass (CTRL_EBEDIT);
    return;
}

static inline int edtGetOutWidth (HWND hWnd,PEBEDITDATA2 pEBEditData)
{
    RECT rect;
    GetClientRect(hWnd,&rect);
    return rect.right-rect.left-pEBEditData->leftMargin-pEBEditData->rightMargin; 
 
}

static int edtGetStartDispPosAtEnd (HWND hWnd,
            PEBEDITDATA2 pEBEditData)
{
    int         nOutWidth = edtGetOutWidth (hWnd,pEBEditData);
    int         endPos  = pEBEditData->dataEnd;
    int         newStartPos = pEBEditData->startPos;
    const char* buffer = pEBEditData->buffer;
    /*int fontsize = GetSysCharWidth();
    PLOGFONT pfont = GetWindowFont(hWnd);
    if(pfont)
    	fontsize = GetFontCharWidth(pfont);
    	*/

    while (TRUE) {
    	if(New_GetStrDispWidth(hWnd,&buffer[newStartPos],endPos-newStartPos) < nOutWidth)
    		break;
        //if ((endPos - newStartPos) * fontsize < nOutWidth)
        //    break;
        
        if ((BYTE)buffer [newStartPos] > 0xA0) {
            newStartPos ++;
            if (newStartPos < pEBEditData->dataEnd) {
                if ((BYTE)buffer [newStartPos] > 0xA0)
                    newStartPos ++;
            }
        }
        else
            newStartPos ++;
    }

    return newStartPos;
    
}

static int edtGetOffset (HWND hWnd,const PEBEDITDATA2 pEBEditData, int x)
{
#if 0
	int i,cx = 0;
	int len_first_char = 0;
	const char* buffer = pEBEditData->buffer;
	
	PLOGFONT pfont = GetWindowFont(hWnd);
	for (i = pEBEditData->startPos; i < pEBEditData->dataEnd; )
	{
		len_first_char = my_get_First_extent(pfont,(char*)&buffer[i],
					pEBEditData->dataEnd-i,&cx);	
		if(!len_first_char)
			i++;
		else
			i += len_first_char;
		if(cx > x)
			return i-len_first_char;
		else if(cx == x)
			return i;	
	}
	return i;
	
#else
    int i;
    int newOff = 0;
    int nTextWidth = 0;
    const char* buffer = pEBEditData->buffer;
    int fontsize = GetSysCCharWidth();
    PLOGFONT pfont = GetWindowFont(hWnd);
    if(pfont)
    	fontsize = GetFontCCharWidth(pfont);
    	
    x -= pEBEditData->leftMargin;
    for (i = pEBEditData->startPos; i < pEBEditData->dataEnd; i++) {
        if ((nTextWidth + (fontsize >> 1)) >= x)
            break;

        if ((BYTE)buffer [i] > 0xA0) {
            i++;
            if (i < pEBEditData->dataEnd) {
                if ((BYTE)buffer [i] > 0xA0) {
                    nTextWidth += fontsize;
                    newOff += 2;
                }
                else
                    i --;
            }
            else {
                nTextWidth += GetFontSingleCharWidth(pfont,(char*)&buffer[i],1);
                newOff ++;
            }
        }
        else {
            nTextWidth += GetFontSingleCharWidth(pfont,(char*)&buffer[i],1);
            newOff ++;
        }

    }

    return newOff;
#endif

}


static int edtGetDispLen (HWND hWnd)
{
#if 0
 	PEBEDITDATA2 pEBEditData = (PEBEDITDATA2)GetWindowAdditionalData2 (hWnd);
	int nOutWidth = edtGetOutWidth (hWnd,pEBEditData);
	return edtGetOffset (hWnd,pEBEditData, nOutWidth);
	
#else
    int 			i, n = 0;
    int 			nTextWidth = 0;
    PEBEDITDATA2 	pEBEditData = (PEBEDITDATA2)GetWindowAdditionalData2 (hWnd);
    int 			nOutWidth = edtGetOutWidth (hWnd,pEBEditData);
    const char* 	buffer = pEBEditData->buffer;
    int 			fontsize = GetSysCCharWidth();
    PLOGFONT 		pfont = GetWindowFont(hWnd);
	
    if(pfont)
    	fontsize = GetFontCCharWidth(pfont);
      
    for (i = pEBEditData->startPos; i < pEBEditData->dataEnd; i++) 
	{
        if ((BYTE)buffer [i] > 0xA0) 
		{
            i++;
            if (i < pEBEditData->dataEnd) 
			{
                if ((BYTE)buffer [i] > 0xA0) 
				{
                    nTextWidth += fontsize;
                    n += 2;
                }
                else
                    i--;
            }
            else 
			{
                nTextWidth += GetFontSingleCharWidth(pfont,(char*)&buffer[i],1);
                n++;
            }
        }
        else 
		{
            nTextWidth += GetFontSingleCharWidth(pfont,(char*)&buffer[i],1);
            n++;
        }

        if (nTextWidth > nOutWidth)
            break;
    }

    return n;
#endif

}


static BOOL edtIsACCharBeforePosition (const char* string, int pos)
{
    if (pos < 2)
        return FALSE;

    if ((BYTE)string [pos - 2] > 0xA0 && (BYTE)string [pos - 1] > 0xA0)
        return TRUE;

    return FALSE;
}

static BOOL edtIsACCharAtPosition (const char* string, int len, int pos)
{
    if (pos > (len - 2))
        return FALSE;

    if ((BYTE)string [pos] > 0xA0 && (BYTE)string [pos + 1] > 0xA0)
        return TRUE;

    return FALSE;
}

static void edtOnEraseBackground (HWND hWnd, DWORD dwStyle, HDC hdc, const RECT* pClipRect)
{
    RECT rcTemp;
    BOOL fGetDC = FALSE;
    PEBEDITDATA2 pEBEditData = (PEBEDITDATA2) GetWindowAdditionalData2(hWnd);
	
    if (hdc == 0) 
	{
        hdc = BeginPaint(hWnd);//GetClientDC (hWnd)
        fGetDC = TRUE;
    }

    if(dwStyle & EES_BKIMAGEMASK)
    {
    	BITMAP bmp;
        memset(&bmp,0,sizeof(BITMAP));
        if(pEBEditData && pEBEditData->data)
        {
            if (pClipRect)
	    	ClipRectIntersect (hdc, pClipRect);
            switch(dwStyle & EES_BKIMAGEMASK)
            {
            	case EES_BKBITMAP:
             		FillBoxWithBitmap(hdc, 0, 0, 0, 0,(PBITMAP)(pEBEditData->data));
            		break;
				
            	case EES_BKBMPFILE:
            	{
            		char * szimage = (char *)(pEBEditData->data);
            		if(!LoadBitmap( HDC_SCREEN, &bmp, szimage))
            		{
 		            	if(dwStyle &EES_BKALPHA)
		            	{
		            		unsigned char r,g,b,a;
		            		Pixel2RGBA(HDC_SCREEN,GetWindowBkColor (hWnd),&r,&g,&b,&a);	
		            		bmp.bmType |= BMP_TYPE_ALPHACHANNEL;
		            		bmp.bmAlpha = a;
							FillBoxWithBitmap(hdc, 0, 0, 0, 0,&bmp);
		            	}
		            	else
		            	{
		            		FillBoxWithBitmap(hdc, 0, 0, 0, 0,&bmp);
		            	}
		            	UnloadBitmap(&bmp);
            		}
            	}
            	break;
				
            	case EES_BKICON:
            		DrawIcon (hdc, 0, 0, 0, 0, (HICON)(pEBEditData->data));
            		break;
					
            	default:
            	break;
            }
        }
    }
    else
    {
    	int fheight = GetSysCharHeight();
    	PLOGFONT pfont = GetWindowFont(hWnd);
	    if(pfont)
	    	fheight = pfont->size;
	  
	    GetClientRect (hWnd, &rcTemp);
	
	    if (pClipRect)
	        ClipRectIntersect (hdc, pClipRect);
		
	    if (dwStyle & WS_DISABLED) 
	        SetBrushColor (hdc, PIXEL_lightgray);
	    else 
		{
	        SetBrushColor (hdc, GetWindowBkColor(hWnd));
			SetBkColor (hdc, GetWindowBkColor(hWnd));
	    }
		
//edit by tjb 2004-4-4
//修改原因:背景需要透明时,应做相关处理
		if (dwStyle & EES_BKTRANSPARENT)
		{
		//	SetBrushColor(hdc, RGBA2Pixel(hdc, 1, 1, 1, 0));
			SetBkMode(hdc, BM_TRANSPARENT);
			FillBox (hdc, rcTemp.left, rcTemp.top, RECTW (rcTemp), RECTH (rcTemp));
		}
		else
	    	FillBox (hdc, rcTemp.left, rcTemp.top, RECTW (rcTemp), RECTH (rcTemp));

	    if ((dwStyle & EES_STYLESMASK) == EES_BASELINE) 
		{
	        SetPenColor (hdc, pEBEditData->textcolor);
	        DrawHDotLine (hdc, pEBEditData->leftMargin, 
	                    pEBEditData->topMargin + fheight + 1,
	                    RECTW (rcTemp) - pEBEditData->leftMargin - pEBEditData->rightMargin);
	    }
    }

	if ((dwStyle & EES_STYLESMASK) == EES_USEBORDER)
	{
	//	gal_pixel	oldcolor;
		RECT		rect;
		
		GetClientRect(hWnd, &rect);
		
		if (pEBEditData->bordercolor != PIXEL_invalid)
		{

⌨️ 快捷键说明

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