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

📄 gwidgets.cpp

📁 一个非常有用的开源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	{		m_nPos += m_nViewSize;		if(m_nPos > m_nModelSize - m_nViewSize)			m_nPos = m_nModelSize - m_nViewSize;		m_dirty = true;		if(m_pParent)			m_pParent->OnVertScroll(this);	}}/*virtual*/ void GWidgetVertScrollBar::OnSlideTab(GWidgetSliderTab* pTab, int dx, int dy){	if(pTab != m_pTab)		return;	m_nPos += dy * m_nModelSize / m_nViewSize;	if(m_nPos < 0)		m_nPos = 0;	else if(m_nPos > m_nModelSize - m_nViewSize)		m_nPos = m_nModelSize - m_nViewSize;	m_dirty = true;	Draw(NULL);	if(m_pParent)		m_pParent->OnVertScroll(this);}// ----------------------------------------------------------------------GWidgetTextBox::GWidgetTextBox(GWidgetGroup* pParent, int x, int y, int w, int h): GWidgetAtomic(pParent, x, y, w, h){	m_image.SetSize(w, h);	m_dirty = true;	m_bGotFocus = false;	m_bPassword = false;	m_nAnchorPos = 0;	m_nCursorPos = 0;	m_nMouseDelta = 0;}/*virtual*/ GWidgetTextBox::~GWidgetTextBox(){}void GWidgetTextBox::SetText(const char* szText){	m_text.Copy(szText);	m_nAnchorPos = m_text.GetLength();	m_nCursorPos = m_nAnchorPos;	m_dirty = true;	m_pParent->OnTextBoxTextChanged(this);	Draw(NULL);}GImage* GWidgetTextBox::GetImage(GRect* pOutRect){	if(m_dirty)		Update();	pOutRect->x = 0;	pOutRect->y = 0;	pOutRect->w = m_image.GetWidth();	pOutRect->h = m_image.GetHeight();	return &m_image;}void GWidgetTextBox::Update(){	m_dirty = false;	// Draw the background area	int w = m_image.GetWidth();	int h = m_image.GetHeight();	m_pStyle->DrawVertCurvedInSurface(&m_image, 0, 0, w, h);	m_image.DrawBox(0, 0, w - 1, h - 1, m_bGotFocus ? m_pStyle->GetTextBoxBorderColor() : m_pStyle->GetTextBoxSelectedTextColor(), false);	// Draw the text	int nTempBufLen = m_text.GetLength() + 1;	GTEMPBUF(char, szText, nTempBufLen);	m_text.GetAnsi(szText);	GRect r;	r.x = 1;	r.y = 3;	r.w = w - 2;	r.h = h - 4;	if(m_bPassword)	{		int i;		for(i = 0; szText[i] != '\0'; i++)			szText[i] = '#';	}	m_image.DrawHardText(&r, szText, m_pStyle->GetTextBoxTextColor(), 1);	// Draw the cursor or selection	if(!m_bGotFocus)		return; // don't waste time drawing the cursor for inactive text boxes	int nSelStart = m_nAnchorPos;	int nSelEnd = m_nCursorPos;	if(nSelEnd < nSelStart)	{		int nTmp = nSelEnd;		nSelEnd = nSelStart;		nSelStart = nTmp;	}	szText[nSelEnd] = '\0';	int nSelEndPos = m_image.MeasureHardTextWidth(r.h, szText, 1) + r.x;	if(nSelEndPos > w - 3)		nSelEndPos = w - 3;	if(nSelStart == nSelEnd)		m_pStyle->DrawCursor(&m_image, nSelEndPos, 2, 2, h - 5);	else	{		szText[nSelStart] = '\0';		int nSelStartPos = m_image.MeasureHardTextWidth(r.h, szText, 1) + r.x;		r.x = nSelStartPos;		r.w = nSelEndPos - nSelStartPos;		m_image.InvertRect(&r);	}}/*virtual*/ void GWidgetTextBox::OnChar(char c){	if(c == '\b')	{		if(m_nAnchorPos == m_nCursorPos)		{			if(m_nAnchorPos <= 0)				return;			m_nAnchorPos--;		}		if(m_nCursorPos < m_nAnchorPos)		{			int nTmp = m_nCursorPos;			m_nCursorPos = m_nAnchorPos;			m_nAnchorPos = nTmp;		}		m_text.Remove(m_nAnchorPos, m_nCursorPos - m_nAnchorPos);		m_nCursorPos = m_nAnchorPos;	}	else if(c == '\r')	{		m_pParent->OnTextBoxPressEnter(this);		return;	}	else	{		if(m_nAnchorPos != m_nCursorPos)		{			if(m_nCursorPos < m_nAnchorPos)			{				int nTmp = m_nCursorPos;				m_nCursorPos = m_nAnchorPos;				m_nAnchorPos = nTmp;			}			m_text.Remove(m_nAnchorPos, m_nCursorPos - m_nAnchorPos);			m_nCursorPos = m_nAnchorPos;		}		if(m_nCursorPos >= m_text.GetLength())            m_text.Add(c);		else			m_text.InsertChar(m_nCursorPos, (wchar_t)c);		m_nCursorPos++;		m_nAnchorPos++;	}	m_pParent->OnTextBoxTextChanged(this);	m_dirty = true;	Draw(NULL);}/*virtual*/ void GWidgetTextBox::Grab(int x, int y){	int nTempBufLen = m_text.GetLength() + 1;	GTEMPBUF(char, szText, nTempBufLen);	m_text.GetAnsi(szText);	m_nMouseDelta = 0;	m_nAnchorPos = m_image.CountHardTextChars(x - 1 + 3, m_image.GetHeight() - 4, szText, 1);	m_nCursorPos = m_nAnchorPos;	m_dirty = true;	Draw(NULL);}/*virtual*/ void GWidgetTextBox::Release(){}/*virtual*/ void GWidgetTextBox::OnMouseMove(int dx, int dy){	m_nMouseDelta += dx;	int nNewCursorPos = m_nAnchorPos + m_nMouseDelta / 6;	if(nNewCursorPos < 0)		nNewCursorPos = 0;	if(nNewCursorPos > m_text.GetLength())		nNewCursorPos = m_text.GetLength();	if(nNewCursorPos != m_nCursorPos)	{		m_nCursorPos = nNewCursorPos;		m_dirty = true;		Draw(NULL);	}}/*virtual*/ void GWidgetTextBox::OnGetFocus(){	m_bGotFocus = true;	m_dirty = true;	Draw(NULL);}/*virtual*/ void GWidgetTextBox::OnLoseFocus(){	m_bGotFocus = false;	m_dirty = true;	Draw(NULL);}// ----------------------------------------------------------------------GWidgetListBoxItem::GWidgetListBoxItem(GWidgetListBox* pParent, const wchar_t* wszText)	: GWidgetAtomic((GWidgetGroup*)pParent, 0, 0, 0, 0){	m_sText = new GString();	m_sText->Copy(wszText);	m_nIndex = pParent->GetSize() - 1;	pParent->SetItemRect(&m_rect, m_nIndex);}/*virtual*/ GWidgetListBoxItem::~GWidgetListBoxItem(){	delete(m_sText);}/*virtual*/ void GWidgetListBoxItem::Grab(int x, int y){	((GWidgetListBox*)m_pParent)->OnGrabItem(m_nIndex);}/*virtual*/ void GWidgetListBoxItem::Draw(GWidgetGroupWithCanvas* pTarget){	((GWidgetListBox*)m_pParent)->Draw(pTarget); // todo: this isn't a very efficient way to do this}// ----------------------------------------------------------------------GWidgetListBox::GWidgetListBox(GWidgetGroup* pParent, int x, int y, int w, int h): GWidgetGroupWithCanvas(pParent, x, y, w, h){	m_nSelectedIndex = -1;	m_nScrollPos = 0;	m_eBaseColor = blue;}/*virtual*/ GWidgetListBox::~GWidgetListBox(){}void GWidgetListBox::SetSelection(int n){	m_nSelectedIndex = n;	m_dirty = true;}void GWidgetListBox::SetScrollPos(int n){	m_nScrollPos = n;	m_dirty = true;}void GWidgetListBox::SetSize(int w, int h){	m_rect.w = w;	m_rect.h = h;	m_image.SetSize(w, h);	m_dirty = true;}/*virtual*/ void GWidgetListBox::Update(){	m_dirty = false;	// Calculations	int w = m_image.GetWidth();	int h = m_image.GetHeight();	// Draw the background area	switch(m_eBaseColor)	{		case red: m_pStyle->DrawHorizCurvedInSurface(&m_image, 0, 0, w, h, 255, 0, 0); break;		case yellow: m_pStyle->DrawHorizCurvedInSurface(&m_image, 0, 0, w, h, 255, 255, 0); break;		case green: m_pStyle->DrawHorizCurvedInSurface(&m_image, 0, 0, w, h, 0, 255, 0); break;		case cyan: m_pStyle->DrawHorizCurvedInSurface(&m_image, 0, 0, w, h, 0, 255, 255); break;		case blue: m_pStyle->DrawHorizCurvedInSurface(&m_image, 0, 0, w, h, 0, 0, 255); break;		case magenta: m_pStyle->DrawHorizCurvedInSurface(&m_image, 0, 0, w, h, 255, 0, 255); break;	}	// Calculate rects	GRect r;	int nItemHeight = m_pStyle->GetListBoxLineHeight(); 	//int nFirstItem = m_nScrollPos / nItemHeight;	r.x = 1;	r.y = 1 - (m_nScrollPos % nItemHeight);	r.w = w - 2;	r.h = nItemHeight;	bool bScrollBar = false;	if(m_nScrollPos > 0 || m_pWidgets->GetSize() * nItemHeight > h)		bScrollBar = true;	int nScrollBarWidth = m_pStyle->GetDefaultScrollBarSize();	if(bScrollBar)	{		if(w / nScrollBarWidth < 3)			nScrollBarWidth = w / 3;		r.w -= nScrollBarWidth;	}	// Draw the items	char szAnsi[256];	GWidgetListBoxItem* pItem;	int nCount = m_pWidgets->GetSize();	int n;	GString* pText;	for(n = 0; n < nCount; n++)	{		if(r.y >= h)			break;		pItem = GetItem(n);		pText = pItem->GetText();		pText->GetAnsi(szAnsi, 256);		if(n == m_nSelectedIndex)		{			m_pStyle->DrawVertCurvedOutSurface(&m_image, r.x, r.y, r.w, r.h);			r.y += 2;			r.h -= 2;			m_image.DrawHardText(&r, szAnsi, m_pStyle->GetTextBoxSelectedTextColor(), 1);			r.y -= 2;			r.h += 2;		}		else		{			r.y += 2;			r.h -= 2;			m_image.DrawHardText(&r, szAnsi, m_pStyle->GetTextBoxTextColor(), 1);			r.y -= 2;			r.h += 2;		}		r.y += r.h;	}/*	// Draw the scroll bar	if(bScrollBar)	{		r.x = w - 1 - nScrollBarWidth;		r.y = 1;		r.w = nScrollBarWidth;		r.h = h - 2;		GWidgetVertScrollBar::Draw(&m_image, &r, m_pStyle, m_nScrollPos, h, m_pWidgets->GetSize() * nItemHeight);	}*/	// Draw the border	m_image.DrawBox(0, 0, w - 1, h - 1, m_pStyle->GetTextBoxBorderColor(), false);}void GWidgetListBox::OnGrabItem(int nIndex){	SetSelection(nIndex);	m_dirty = true; // todo: only redraw the list item	Draw(NULL);	if(m_pParent)		m_pParent->OnChangeListSelection(this);}GWidgetListBoxItem* GWidgetListBox::GetItem(int n){	return (GWidgetListBoxItem*)m_pWidgets->GetPointer(n);}int GWidgetListBox::GetSize(){	return m_pWidgets->GetSize();}void GWidgetListBox::SetItemRect(GRect* pRect, int nIndex){	int nItemHeight = m_pStyle->GetListBoxLineHeight(); 	pRect->Set(0, nIndex * nItemHeight, m_rect.w, nItemHeight);	m_dirty = true;}void GWidgetListBox::Clear(){	int n;	for(n = 0; n < m_pWidgets->GetSize(); n++)	{		GWidget* pWidget = GetChildWidget(n);//		if(pWidget == m_pVertScrollBar)//			continue;//		if(pWidget == m_pHorizScrollBar)//			continue;		delete(pWidget);		n--;	}	m_dirty = true;}// ----------------------------------------------------------------------GWidgetGrid::GWidgetGrid(GWidgetGroup* pParent, GPointerArray* pRows, int nColumns, int x, int y, int w, int h): GWidgetGroupWithCanvas(pParent, x, y, w, h){	m_nColumns = nColumns;	m_pRows = pRows;	m_nRowHeight = 20;	m_pColumnHeaders = new GWidget*[m_nColumns];	m_nColumnWidths = new int[m_nColumns];	int n;	for(n = 0; n < nColumns; n++)	{		m_pColumnHeaders[n] = NULL;		m_nColumnWidths[n] = 80;	}	int nScrollBarSize = m_pStyle->GetDefaultScrollBarSize();	m_pVertScrollBar = new GWidgetVertScrollBar(this, w - nScrollBarSize, 0, nScrollBarSize, h, h - m_nRowHeight - nScrollBarSize, h);	m_pHorizScrollBar = new GWidgetHorizScrollBar(this, 0, h - nScrollBarSize, w - nScrollBarSize, nScrollBarSize, w - nScrollBarSize, 80 * nColumns);}/*virtual*/ GWidgetGrid::~GWidgetGrid(){	delete(m_pColumnHeaders);	delete(m_nColumnWidths);}void GWidgetGrid::SetHScrollPos(int n){	m_pHorizScrollBar->SetPos(n);	m_dirty = true;}void GWidgetGrid::SetVScrollPos(int n){	m_pVertScrollBar->SetPos(n);	m_dirty = true;}void GWidgetGrid::SetSize(int w, int h){	m_rect.w = w;	m_rect.h = h;	m_image.SetSize(w, h);	m_dirty = true;}void GWidgetGrid::AddBlankRow(){	GWidget** pNewRow = new GWidget*[m_nColumns];	int n;	for(n = 0; n < m_nColumns; n++)		pNewRow[n] = NULL;	m_pRows->AddPointer(pNewRow);	m_dirty = true;}GWidget* GWidgetGrid::GetWidget(int col, int row){	GAssert(col >= 0 && col < m_nColumns, "out of range");	GWidget** pRow = (GWidget**)m_pRows->GetPointer(row);	return pRow[col];}void GWidgetGrid::SetWidget(int col, int row, GWidget* pWidget){	GAssert(col >= 0 && col < m_nColumns, "out of range");	GWidget** pRow = (GWidget**)m_pRows->GetPointer(row);	pRow[col] = pWidget;	int nColPos = 0;	int n;	for(n = 0; n < col; n++)		nColPos += m_nColumnWidths[n];	pWidget->SetPos(nColPos, (row + 1) * m_nRowHeight);	// todo: figure out how to handle the dirty flag here}GWidget* GWidgetGrid::GetColumnHeader(int col){	GAssert(col >= 0 && col < m_nColumns, "out of range");	return m_pColumnHeaders[col];}void GWidgetGrid::SetColumnHeader(int col, GWidget* pWidget){	GAssert(col >= 0 && col < m_nColumns, "out of range");	m_pColumnHeaders[col] = pWidget;	int nColPos = 0;	int n;	for(n = 0; n < col; n++)		nColPos += m_nColumnWidths[n];	pWidget->SetPos(nColPos, 0);	// todo: figure out how to handle the dirty flag here}int GWidgetGrid::GetColumnWidth(int col){	GAssert(col >= 0 && col < m_nColumns, "out of range");	return m_nColumnWidths[col];}void GWidgetGrid::SetColumnWidth(int col, int nWidth){	GAssert(col >= 0 && col < m_nColumns, "out of range");	m_nColumnWidths[col] = nWidth;	m_dirty = true;}/*virtual*/ void GWidgetGrid::Update(){	m_dirty = false;	// Calculations	int w = m_image.GetWidth() - m_pStyle->GetDefaultScrollBarSize();	int h = m_image.GetHeight() - m_pStyle->GetDefaultScrollBarSize();	// Draw the background area	m_image.Clear(0x00000000);	// Draw columns	int nColumnStart = 0;	int nColumnWidth;	int nLeftPos;	int nLeftClip, nRightClip;	int nVertPos, nVertHeight, nModelVertPos;	GWidget* pWidget;	GRect r;	int nHScrollPos = m_pHorizScrollBar->GetPos();	int nVScrollPos = m_pVertScrollBar->GetPos();	GImage* pImage;	int n, i;	for(n = 0; n < m_nColumns; n++)	{		nColumnWidth = m_nColumnWidths[n];		if(nColumnStart + nColumnWidth > nHScrollPos)		{			// Calculate left clip amount			nLeftPos = nColumnStart - nHScrollPos;			if(nLeftPos >= w)				break;			if(nLeftPos + nColumnWidth <= w)				nRightClip = 0;			else				nRightClip = (nLeftPos + nColumnWidth) - w;			if(nLeftPos >= 0)				nLeftClip = 0;			else			{				nLeftClip = -nLeftPos;				nLeftPos = 0;			}			// Draw the header widget			pWidget = m_pColumnHeaders[n];			if(pWidget)			{				pImage = pWidget->GetImage(&r);				if(pImage)				{					r.w = MIN(r.w, nColumnWidth - nRightClip);					if(r.h > m_nRowHeight)						r.h = m_nRowHeight;					r.x += nLeftClip;					r.w -= nLeftClip;					if(r.w > 0)						m_image.Blit(nLeftPos, 0, pImage, &r);				}			}			// Draw all the widgets in the column			nVertPos = m_nRowHeight;			nVertHeight = m_nRowHeight - (nVScrollPos % m_nRowHeight);			i = nVScrollPos / m_nRowHeight;			nModelVertPos = i * m_nRowHeight;			while(nVertPos < h)			{				if(i >= m_pRows->GetSize())					break;				if(nModelVertPos + m_nRowHeight > nVScrollPos)				{					pWidget = ((GWidget**)m_pRows->GetPointer(i))[n];					if(pWidget)					{						pImage = pWidget->GetImage(&r);						if(pImage)						{							r.w = MIN(r.w, nColumnWidth - nRightClip);							if(r.h > m_nRowHeight)								r.h = m_nRowHeight;							r.x += nLeftClip;							r.w -= nLeftClip;							r.y += (m_nRowHeight - nVertHeight);							r.h -= (m_nRowHeight - nVertHeight);							if(nVertPos + r.h > h)								r.h -= (nVertPos + r.h - h);							if(r.w > 0)								m_image.Blit(nLeftPos, nVertPos, pImage, &r);						}					}				}				nVertPos += nVertHeight;				nVertHeight = m_nRowHeight;				nModelVertPos += m_nRowHeight;				i++;			}		}		nColumnStart += nColumnWidth;	}	while(n < m_nColumns)	{		nColumnStart += m_nColumnWidths[n];		n++;	}	// Draw the scroll bars	m_pVertScrollBar->SetModelSize(MAX(m_pRows->GetSize() * m_nRowHeight, m_rect.h));	m_pVertScrollBar->Draw(this);	m_pHorizScrollBar->SetModelSize(MAX(nColumnStart, m_rect.w));	m_pHorizScrollBar->Draw(this);}void GWidgetGrid::FlushItems(){	bool bCol;	int n, i;	for(n = 0; n < m_pWidgets->GetSize(); n++)	{		GWidget* pWidget = (GWidget*)m_pWidgets->GetPointer(n);		if(pWidget == m_pVertScrollBar)			continue;		if(pWidget == m_pHorizScrollBar)			continue;		bCol = false;		for(i = 0; i < m_nColumns; i++)		{			if(m_pColumnHeaders[i] == pWidget)			{				bCol = true;				break;			}		}		if(bCol)			continue;		delete(pWidget);		n--;

⌨️ 快捷键说明

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