📄 cliprect.c
字号:
//// cliprect.c: the Clip Rect module.// // Copyright (C) 1999, 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*/// Create date: 1999/3/26//// Modify records://// Who When Where For What Status//-----------------------------------------------------------------------------//// TODO:// #include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <errno.h>#include "common.h"#include "gdi.h"#include "cliprect.h"#include "gal.h"#ifndef lintstatic char fileid[] = "$Id: cliprect.c,v 1.2 2000/06/19 01:54:16 weiym Exp $";#endif/************************** Free Clip Rect List operation ********************/BOOL GUIAPI InitFreeClipRectList(PFREECLIPRECTLIST pList, size_t size){ if( !(pList->heap = malloc (sizeof(CLIPRECT)*size))) return FALSE; pList->free = 0; pthread_mutex_init (&pList->lock, NULL); pList->head = pList->tail = NULL; pList->size = size; return TRUE;}PCLIPRECT GUIAPI ClipRectAlloc(PFREECLIPRECTLIST pList){ PCLIPRECT pRect; pthread_mutex_lock (&pList->lock); if (pList->head) { pRect = pList->head; pList->head = pRect->next; } else { if (pList->free < pList->size) { pRect = pList->heap + pList->free; pRect->fromheap = TRUE; pList->free ++; } else { pRect = malloc (sizeof(CLIPRECT)); if (pRect == NULL) fprintf (stderr, "GDI error: alloc clip rect failure!\n"); else pRect->fromheap = FALSE; } } pthread_mutex_unlock (&pList->lock); return pRect;}void GUIAPI FreeClipRect(PFREECLIPRECTLIST pList, CLIPRECT* pRect){ pthread_mutex_lock (&pList->lock); pRect->next = NULL; if (pList->head) { pList->tail->next = (PCLIPRECT)pRect; pList->tail = (PCLIPRECT)pRect; } else { pList->head = pList->tail = (PCLIPRECT)pRect; } pthread_mutex_unlock (&pList->lock);}void GUIAPI EmptyFreeClipRectList(PFREECLIPRECTLIST pList){ PCLIPRECT pRect, pTemp; pthread_mutex_lock (&pList->lock); pRect = pList->head; while (pRect) { pTemp = pRect->next; if (!pRect->fromheap) free (pRect); pRect = pTemp; } pList->head = pList->tail = NULL; pList->free = 0; pthread_mutex_unlock (&pList->lock);}void GUIAPI DestroyFreeClipRectList(PFREECLIPRECTLIST pList){ EmptyFreeClipRectList (pList); free (pList->heap);}/************************** clip region operation ****************************/static void EvaluateBoundRect(PCLIPRGN pRgn){ PCLIPRECT pCRect, pTemp; pCRect = pRgn->head; if (pCRect == NULL) { SetRectEmpty (&pRgn->rcBound); return; } pRgn->rcBound = pCRect->rc; pCRect = pCRect->next; while (pCRect) { if(pRgn->rcBound.left > pCRect->rc.left) pRgn->rcBound.left = pCRect->rc.left; if(pRgn->rcBound.top > pCRect->rc.top) pRgn->rcBound.top = pCRect->rc.top; if(pRgn->rcBound.right < pCRect->rc.right) pRgn->rcBound.right = pCRect->rc.right; if(pRgn->rcBound.bottom < pCRect->rc.bottom) pRgn->rcBound.bottom = pCRect->rc.bottom; pCRect = pCRect->next; } // Remove empty clip rects. pCRect = pRgn->head; while (pCRect->next) { if (IsRectEmpty (&pCRect->next->rc)) { pTemp = pCRect->next; pCRect->next = pTemp->next; FreeClipRect (pRgn->pFreeList, pTemp); } else pCRect = pCRect->next; } pRgn->tail = pCRect; if (IsRectEmpty (&pRgn->head->rc)) { pTemp = pRgn->head; pRgn->head = pTemp->next; FreeClipRect (pRgn->pFreeList, pTemp); }}void GUIAPI InitClipRgn(PCLIPRGN pRgn, PFREECLIPRECTLIST pFreeList){ SetRectEmpty (&pRgn->rcBound); pRgn->head = NULL; pRgn->tail = NULL; pRgn->pFreeList = pFreeList; // read-only field.}void GUIAPI GetClipRgnBoundRect(PCLIPRGN pRgn, PRECT pRect){ *pRect = pRgn->rcBound;}BOOL GUIAPI SetClipRgn(PCLIPRGN pRgn, const RECT* pRect){ PCLIPRECT pClipRect; RECT rcTemp; rcTemp = *pRect; if (IsRectEmpty (&rcTemp)) return FALSE; NormalizeRect (&rcTemp); // empty rgn first EmptyClipRgn (pRgn); // get a new clip rect from free list pClipRect = ClipRectAlloc (pRgn->pFreeList); // set clip rect pClipRect->rc = rcTemp; pClipRect->next = NULL; pRgn->head = pRgn->tail = pClipRect; pRgn->rcBound = rcTemp; return TRUE;} BOOL GUIAPI IsEmptyClipRgn(const CLIPRGN* pRgn){ if (pRgn->head == NULL) return TRUE; return FALSE;}void GUIAPI EmptyClipRgn(PCLIPRGN pRgn){ PCLIPRECT pCRect, pTemp; pCRect = pRgn->head; while (pCRect) { pTemp = pCRect->next; FreeClipRect (pRgn->pFreeList, pCRect); pCRect = pTemp; } SetRectEmpty (&pRgn->rcBound); pRgn->head = NULL; pRgn->tail = NULL;}BOOL GUIAPI ClipRgnCopy (PCLIPRGN pDstRgn, const CLIPRGN* pSrcRgn){ PCLIPRECT pcr; PCLIPRECT pnewcr; // return false if the destination region is not a empty one. if (pDstRgn->head) return FALSE; if (!(pcr = pSrcRgn->head)) return TRUE; pnewcr = ClipRectAlloc (pDstRgn->pFreeList); pDstRgn->head = pnewcr; pnewcr->rc = pcr->rc; while (pcr->next) { pcr = pcr->next; pnewcr->next = ClipRectAlloc (pDstRgn->pFreeList); pnewcr = pnewcr->next; pnewcr->rc = pcr->rc; } pnewcr->next = NULL; pDstRgn->tail = pnewcr; pDstRgn->rcBound = pSrcRgn->rcBound; return TRUE;}BOOL GUIAPI ClipRgnIntersect (PCLIPRGN pRstRgn, const CLIPRGN* pRgn1, const CLIPRGN* pRgn2){ RECT rc; PCLIPRECT pnewcr; PCLIPRECT pcr1, pcr2; if (pRstRgn->head) return FALSE; if (!(pcr1 = pRgn1->head)) return TRUE; if (!(pcr2 = pRgn2->head)) return TRUE; pnewcr = ClipRectAlloc (pRstRgn->pFreeList); pnewcr->next = NULL; pRstRgn->head = pnewcr; while (pcr1) { while (pcr2) { if (IntersectRect (&rc, &pcr1->rc, &pcr2->rc)) { pnewcr->rc = rc; pnewcr->next = ClipRectAlloc (pRstRgn->pFreeList); } pcr2 = pcr2->next; } pcr2 = pRgn2->head; pcr1 = pcr1->next; } pRstRgn->tail = pnewcr; if (pnewcr->next) { FreeClipRect(pRstRgn->pFreeList, pnewcr->next); pnewcr->next = NULL; } EvaluateBoundRect(pRstRgn); return TRUE;}BOOL GUIAPI AddClipRect (PCLIPRGN pRgn, const RECT* pRect){ PCLIPRECT pClipRect; RECT rcTemp; rcTemp = *pRect; if (IsRectEmpty (&rcTemp)) return FALSE; NormalizeRect (&rcTemp); pClipRect = ClipRectAlloc (pRgn->pFreeList); pClipRect->rc = rcTemp; pClipRect->next = NULL; if (pRgn->head) { pRgn->tail->next = pClipRect; pRgn->tail = pClipRect; } else { pRgn->head = pRgn->tail = pClipRect; } return TRUE;}BOOL GUIAPI IntersectClipRect(PCLIPRGN pRgn, const RECT* pRect){ PCLIPRECT pCRect; RECT rcTemp; rcTemp = *pRect; if (IsRectEmpty (&rcTemp)) return FALSE; NormalizeRect (&rcTemp); // intersect pCRect = pRgn->head; while (pCRect) { if (!IntersectRect (&pCRect->rc, &pCRect->rc, &rcTemp)) SetRectEmpty(&pCRect->rc); pCRect = pCRect->next; } EvaluateBoundRect (pRgn); return TRUE;}BOOL GUIAPI SubtractClipRect(PCLIPRGN pRgn, const RECT* pRect){ PCLIPRECT pCRect, pSaved, pTemp; RECT rc[4], rcTemp, rcInflated; int nCount; PRECT src, erc; int i; if (IsRectEmpty (pRect)) return FALSE; rcInflated = *pRect; erc = &rcInflated; NormalizeRect (erc); if (!DoesIntersect(&pRgn->rcBound, erc)) { return FALSE; } pCRect = pRgn->head; while (pCRect) { src = &pCRect->rc; if (!IntersectRect(&rcTemp, src, erc)) { pCRect = pCRect->next; continue; } pSaved = pCRect->next; nCount = 0; if(erc->top > src->top) { rc[nCount].left = src->left; rc[nCount].top = src->top; rc[nCount].right = src->right; rc[nCount].bottom = erc->top; nCount++; src->top = erc->top; } if(erc->bottom < src->bottom) { rc[nCount].top = erc->bottom; rc[nCount].left = src->left; rc[nCount].right = src->right; rc[nCount].bottom = src->bottom; nCount++; src->bottom = erc->bottom; } if(erc->left > src->left) { rc[nCount].left = src->left; rc[nCount].top = src->top; rc[nCount].right = erc->left; rc[nCount].bottom = src->bottom; nCount++; } if(erc->right < src->right) { rc[nCount].left = erc->right; rc[nCount].top = src->top; rc[nCount].right = src->right; rc[nCount].bottom = src->bottom; nCount++; } if (nCount == 0) SetRectEmpty (&pCRect->rc); else if(nCount > 0) pCRect->rc = rc[0]; for(i = 1; i<nCount; i++) { pTemp = ClipRectAlloc(pRgn->pFreeList); pTemp->rc = rc[i]; pCRect->next = pTemp; pCRect = pTemp; } pCRect->next = pSaved; pCRect = pSaved; } EvaluateBoundRect (pRgn); return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -