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

📄 gwidgets.cpp

📁 一个非常有用的开源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	}	int nCount = m_pRows->GetSize();	for(n = 0; n < nCount; n++)		delete((GWidget**)m_pRows->GetPointer(n));	m_pRows->Clear();	m_dirty = true;}/*virtual*/ void GWidgetGrid::OnVertScroll(GWidgetVertScrollBar* pScrollBar){	m_dirty = true; // todo: only update the rows	Draw(NULL);}/*virtual*/ void GWidgetGrid::OnHorizScroll(GWidgetHorizScrollBar* pScrollBar){	m_dirty = true; // todo: only update the rows	Draw(NULL);}/*virtual*/ GWidgetAtomic* GWidgetGrid::FindAtomicWidget(int x, int y){	GRect* pRect = m_pVertScrollBar->GetRect();	if(x >= pRect->x)		return m_pVertScrollBar->FindAtomicWidget(x - pRect->x, y - pRect->y);	pRect = m_pHorizScrollBar->GetRect();	if(y >= pRect->y)		return m_pHorizScrollBar->FindAtomicWidget(x - pRect->x, y - pRect->y);	int xOrig = x;	int yOrig = y;	x += m_pHorizScrollBar->GetPos();	GWidget** pRow;	if(y < m_nRowHeight)		pRow = m_pColumnHeaders;	else	{		y += m_pVertScrollBar->GetPos();		y -= m_nRowHeight;		y /= m_nRowHeight;		if(y >= 0 && y < m_pRows->GetSize())			pRow = (GWidget**)m_pRows->GetPointer(y);		else			return NULL;	}	GWidget* pWidget = NULL;	int nColLeft = 0;	int n;	for(n = 0; n < m_nColumns; n++)	{		nColLeft += m_nColumnWidths[n];		if(nColLeft > x)		{			pWidget = pRow[n];			break;		}	}	if(pWidget)	{		if(pWidget->IsAtomicWidget())			return (GWidgetAtomic*)pWidget;		else		{			GRect* pRect = pWidget->GetRect();			return ((GWidgetGroup*)pWidget)->FindAtomicWidget(xOrig - pRect->x, yOrig - pRect->y);		}	}	return NULL;}// ----------------------------------------------------------------------GWidgetFileSystemBrowser::GWidgetFileSystemBrowser(GWidgetGroup* pParent, int x, int y, int w, int h, const char* szExtension) : GWidgetGroup(pParent, x, y, w, h){	int nPathHeight = m_pStyle->GetListBoxLineHeight();	GString s;	m_pPath = new GWidgetTextLabel(this, 0, 0, w, nPathHeight, &s, 0xff8888ff);	m_pPath->SetBackgroundColor(0xff000000);	m_pListItems = new GPointerArray(64);	m_pFiles = new GWidgetGrid(this, m_pListItems, 3, 0, nPathHeight, w, h - nPathHeight);	// Column Headers	m_pFiles->SetColumnWidth(0, 300);	m_pFiles->SetColumnWidth(1, 50);	m_pFiles->SetColumnWidth(2, 50);	GWidgetTextButton* pButton;	s.Copy(L"Filename");	pButton = new GWidgetTextButton(m_pFiles, 0, 0, 300, 20, &s);	m_pFiles->SetColumnHeader(0, pButton);	s.Copy(L"Size");	pButton = new GWidgetTextButton(m_pFiles, 0, 0, 50, 20, &s);	m_pFiles->SetColumnHeader(1, pButton);	s.Copy(L"Date");	pButton = new GWidgetTextButton(m_pFiles, 0, 0, 50, 20, &s);	m_pFiles->SetColumnHeader(2, pButton);	// Extension	int nExtLen = 0;	if(szExtension)		nExtLen = strlen(szExtension);	if(nExtLen > 0)	{		m_szExtension = new char[nExtLen + 1];		strcpy(m_szExtension, szExtension);	}	else		m_szExtension = NULL;	ReloadFileList();}/*virtual*/ GWidgetFileSystemBrowser::~GWidgetFileSystemBrowser(){	delete(m_pListItems);	delete(m_szExtension);}/*virtual*/ void GWidgetFileSystemBrowser::Draw(GWidgetGroupWithCanvas* pTarget){	m_pPath->Draw(pTarget);	m_pFiles->Draw(pTarget);}void GWidgetFileSystemBrowser::AddFilename(bool bDir, const char* szFilename){	int nRows = m_pFiles->GetRows()->GetSize();	m_pFiles->AddBlankRow();	GString s;	s.Copy(szFilename);	GWidgetTextLabel* pLabel = new GWidgetTextLabel(m_pFiles, 0, 0, 100, 20, &s, bDir ? 0xffffffff : 0xff44ffaa);	m_pFiles->SetWidget(0, nRows, pLabel);}void GWidgetFileSystemBrowser::ReloadFileList(){	m_pFiles->FlushItems();	{		getcwd(m_szPath, 256);		m_pPath->SetText(m_szPath);		if(strlen(m_szPath) >#ifdef WIN32							3)#else // WIN32							1)#endif // !WIN32		AddFilename(true, "..");	}	// Dirs	{		GDirList dl(false, false, true, false);		while(true)		{			const char* szDir = dl.GetNext();			if(!szDir)				break;			AddFilename(true, szDir);		}	}	// Files	{		char szExt[256];		GDirList dl(false, true, false, false);		while(true)		{			const char* szFilename = dl.GetNext();			if(!szFilename)				break;			if(m_szExtension)			{				_splitpath(szFilename, NULL, NULL, NULL, szExt);				if(stricmp(szExt, m_szExtension) != 0)					continue;			}			AddFilename(false, szFilename);		}	}}/*virtual*/ void GWidgetFileSystemBrowser::OnClickTextLabel(GWidgetTextLabel* pLabel){	GString* pText = pLabel->GetText();	int nTempBufLen = pText->GetLength() + 1;	GTEMPBUF(char, szFilename, nTempBufLen);	pText->GetAnsi(szFilename);	strcat(m_szPath, "/");	strcat(m_szPath, szFilename);	if(chdir(m_szPath) == 0)	{		m_pFiles->SetVScrollPos(0);		ReloadFileList();		Draw(NULL);	}	else	{		if(m_pParent)			m_pParent->OnSelectFilename(this, m_szPath);	}}// ----------------------------------------------------------------------GWidgetPolarLineGraph::GWidgetPolarLineGraph(GWidgetGroup* pParent, int x, int y, int w, int h, int nValues): GWidgetAtomic(pParent, x, y, w, h){	m_image.SetSize(w, h);	m_nValues = nValues;	m_cForeground = 0xffff88ff;	m_cBackground = 0x00000000; // transparent	m_cShading = 0xff4422ff;	m_cSelected = 0xffffffff;	m_nSelected = -1;	m_pValues = new float[nValues];	int i;	for(i = 0; i < nValues; i++)		m_pValues[i] = .5;	m_dirty = true;}/*virtual*/ GWidgetPolarLineGraph::~GWidgetPolarLineGraph(){	delete(m_pValues);}inline int GetWedgeIndex(int x, int y, int nValues){	return (int)(atan2((double)x, (double)-y) * nValues / (2 * PI) + .5 + nValues) % nValues;}/*virtual*/ void GWidgetPolarLineGraph::Grab(int x, int y){	x -= m_image.GetWidth() / 2;	y -= m_image.GetHeight() / 2;	SetSelection(GetWedgeIndex(x, y, m_nValues));}/*virtual*/ void GWidgetPolarLineGraph::Release(){}void GWidgetPolarLineGraph::Update(){	m_dirty = false;	m_image.Clear(m_cBackground);	int xCenter = m_image.GetWidth() / 2;	int yCenter = m_image.GetHeight() / 2;	int radius = MIN(xCenter, yCenter) - 2;	double x, y, xx, yy;	double xPrev = m_pValues[m_nValues - 1] * sin(2 * PI * (m_nValues - 1) / m_nValues) * radius + xCenter;	double yPrev = m_pValues[m_nValues - 1] * -cos(2 * PI * (m_nValues - 1) / m_nValues) * radius + yCenter;	GColor c;	int i;	for(i = 0; i < m_nValues; i++)	{		if(i == m_nSelected || ((i + m_nValues - 1) % m_nValues) == m_nSelected)			c = m_cSelected;		else			c = m_cShading;		x = m_pValues[i] * sin(2 * PI * i / m_nValues) * radius + xCenter;		y = m_pValues[i] * -cos(2 * PI * i / m_nValues) * radius + yCenter;		m_image.FillTriangle(xCenter, yCenter, (int)x, (int)y, (int)xPrev, (int)yPrev, c);		xPrev = x;		yPrev = y;	}	double xxPrev = sin(2 * PI * (m_nValues - 1) / m_nValues) * radius + xCenter;	double yyPrev = -cos(2 * PI * (m_nValues - 1) / m_nValues) * radius + yCenter;	for(i = 0; i < m_nValues; i++)	{		if(i == m_nSelected)			c = m_cSelected;		else			c = m_cForeground;		xx = sin(2 * PI * i / m_nValues) * radius;		yy = -cos(2 * PI * i / m_nValues) * radius;		x = xx * m_pValues[i] + xCenter;		y = yy * m_pValues[i] + yCenter;		xx += xCenter;		yy += yCenter;		m_image.DrawLine((int)x, (int)y, (int)xx, (int)yy, c);		m_image.DrawLine((int)x, (int)y, (int)xPrev, (int)yPrev, m_cForeground);		m_image.DrawLine((int)xx, (int)yy, (int)xxPrev, (int)yyPrev, m_cForeground);		m_image.DrawCircle((int)x, (int)y, 3, 0xffffccdd);		xxPrev = xx;		yyPrev = yy;		xPrev = x;		yPrev = y;	}}GImage* GWidgetPolarLineGraph::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 GWidgetPolarLineGraph::SetSize(int w, int h){	m_rect.w = w;	m_rect.h = h;	m_image.SetSize(w, h);	m_dirty = true;}void GWidgetPolarLineGraph::SetValueCount(int n){	delete(m_pValues);	m_pValues = new float[n];	m_nValues = n;	int i;	for(i = 0; i < n; i++)		m_pValues[i] = .5;}void GWidgetPolarLineGraph::SetValue(int n, float value){	GAssert(n >= 0 && n < m_nValues, "out of range");	m_pValues[n] = value;}void GWidgetPolarLineGraph::SetSelection(int n){	GAssert(n >= 0 && n < m_nValues, "out of range");	m_nSelected = n;	m_dirty = true;	Draw(NULL);	m_pParent->OnChangePolarLineGraphSelection(this);}// ----------------------------------------------------------------------GWidgetPolarBarGraph::GWidgetPolarBarGraph(GWidgetGroup* pParent, int x, int y, int w, int h, int nValues): GWidgetAtomic(pParent, x, y, w, h){	m_image.SetSize(w, h);	m_nValues = nValues;	m_cForeground = 0xff000000;	m_cBackground = 0x00000000; // transparent	m_cSelected = 0xffaaaaaa;	m_nSelected = -1;	m_pValues = new float[nValues];	int i;	for(i = 0; i < nValues; i++)		m_pValues[i] = .5;	m_dirty = true;}/*virtual*/ GWidgetPolarBarGraph::~GWidgetPolarBarGraph(){	delete(m_pValues);}/*virtual*/ void GWidgetPolarBarGraph::Grab(int x, int y){	x -= m_image.GetWidth() / 2;	y -= m_image.GetHeight() / 2;	SetSelection(GetWedgeIndex(x, y, m_nValues));}/*virtual*/ void GWidgetPolarBarGraph::Release(){}void GWidgetPolarBarGraph::Update(){	m_dirty = false;	m_image.Clear(m_cBackground);	int xCenter = m_image.GetWidth() / 2;	int yCenter = m_image.GetHeight() / 2;	int radius = MIN(xCenter, yCenter) - 2;	int radiusSquared = radius * radius;	GTEMPBUF(int, pDistances, sizeof(int) * m_nValues);	int x, y, n, nDist;	GColor c;	double d, dBrightness;	for(n = 0; n < m_nValues; n++)	{		d = m_pValues[n] * radius;		pDistances[n] = (int)(d * d);	}	for(y = 1 - yCenter; y < yCenter; y++)	{		for(x = 1 - xCenter; x < xCenter; x++)		{			nDist = x * x + y * y;			if(nDist > radiusSquared)				continue;			d = atan2((double)x, (double)-y) * m_nValues / (2 * PI) + .5 + m_nValues;			n = ((int)d) % m_nValues;			if(nDist > pDistances[n])			{				m_image.SetPixel(xCenter + x, yCenter + y, (n == m_nSelected) ? m_cSelected : m_cForeground);				continue;			}			dBrightness = d - (int)d;			GAssert(dBrightness >= 0 && dBrightness <= 1, "out of range");			dBrightness = dBrightness * 4 * (1.0 - dBrightness);			c = GetSpectrumColor((float)n / m_nValues);			c = MultiplyBrightness(c, (float)dBrightness);			if(n == m_nSelected && gRed(c) + gGreen(c) + gBlue(c) < 128)				c = m_cSelected;			m_image.SetPixel(xCenter + x, yCenter + y, c);		}	}}GImage* GWidgetPolarBarGraph::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 GWidgetPolarBarGraph::SetSize(int w, int h){	m_rect.w = w;	m_rect.h = h;	m_image.SetSize(w, h);	m_dirty = true;}void GWidgetPolarBarGraph::SetValueCount(int n){	delete(m_pValues);	m_pValues = new float[n];	m_nValues = n;	int i;	for(i = 0; i < n; i++)		m_pValues[i] = .5;}void GWidgetPolarBarGraph::SetValue(int n, float value){	GAssert(n >= 0 && n < m_nValues, "out of range");	m_pValues[n] = value;}void GWidgetPolarBarGraph::SetSelection(int n){	GAssert(n >= 0 && n < m_nValues, "out of range");	m_nSelected = n;	m_dirty = true;	Draw(NULL);	m_pParent->OnChangePolarBarGraphSelection(this);}// ----------------------------------------------------------------------GWidgetVertSlider::GWidgetVertSlider(GWidgetGroup* pParent, int x, int y, int w, int h): GWidgetGroupWithCanvas(pParent, x, y, w, h){	m_fPos = .5;	m_pAboveTab = new GWidgetSliderTab(this, 0, 0, w, 0, true, GWidgetSliderTab::SliderArea);	m_pTab = new GWidgetSliderTab(this, 0, 0, w, 0, true, GWidgetSliderTab::SliderNub);	m_pBelowTab = new GWidgetSliderTab(this, 0, 0, w, 0, true, GWidgetSliderTab::SliderArea);}/*virtual*/ GWidgetVertSlider::~GWidgetVertSlider(){}void GWidgetVertSlider::SetSize(int w, int h){	m_rect.w = w;	m_rect.h = h;	m_image.SetSize(w, h);	m_dirty = true;}void GWidgetVertSlider::SetPos(float f){	m_fPos = f;	m_dirty = true;	Draw(NULL);}/*virtual*/ void GWidgetVertSlider::Update(){	m_dirty = false;	// Calculations	int wid = m_image.GetWidth();	int hgt = m_image.GetHeight();	GAssert(hgt > wid, "disproportioned vertical slider");	int nTabSize = wid / 2;	if(m_fPos < 0)		m_fPos = 0;	else if(m_fPos > 1)		m_fPos = 1;	int nTabPos = hgt - nTabSize - (int)(m_fPos * (hgt - nTabSize));	// Position the three tab areas	m_pAboveTab->SetPos(0, 0);	m_pAboveTab->SetSize(m_rect.w, nTabPos);	m_pTab->SetPos(0, nTabPos);	m_pTab->SetSize(m_rect.w, nTabSize);	m_pBelowTab->SetPos(0, nTabPos + nTabSize);	m_pBelowTab->SetSize(m_rect.w, hgt - (nTabPos + nTabSize));	// Draw everything	m_image.Clear(0xff000000);	m_pAboveTab->Draw(this);	m_pTab->Draw(this);	m_pBelowTab->Draw(this);}/*virtual*/ void GWidgetVertSlider::OnClickTab(GWidgetSliderTab* pTab){	if(pTab == m_pAboveTab)	{		m_fPos -= (float).2;		if(m_fPos < 0)			m_fPos = 0;		m_dirty = true;		if(m_pParent)			m_pParent->OnVertSliderMove(this);	}	else if(pTab == m_pBelowTab)	{		m_fPos += (float).2;		if(m_fPos > 1)			m_fPos = 1;		m_dirty = true;		if(m_pParent)			m_pParent->OnVertSliderMove(this);	}}/*virtual*/ void GWidgetVertSlider::OnSlideTab(GWidgetSliderTab* pTab, int dx, int dy){	if(pTab != m_pTab)		return;	int wid = m_image.GetWidth();	int hgt = m_image.GetHeight();	int nTabSize = wid / 2;	m_fPos -= (float)dy / (hgt - nTabSize);	if(m_fPos < 0)		m_fPos = 0;	else if(m_fPos > 1)		m_fPos = 1;	m_dirty = true;	Draw(NULL);	if(m_pParent)		m_pParent->OnVertSliderMove(this);}// ----------------------------------------------------------------------GWidgetMagnitudeGraph::GWidgetMagnitudeGraph(GWidgetGroup* pParent, int x, int y, int w, int h): GWidgetAtomic(pParent, x, y, w, h){	m_image.SetSize(w, h);	m_cForeground = 0xffff7799;	m_cBackground = 0xff000000;	m_cGridLines = 0xff777777;	m_pValues = NULL;	m_nValues = 0;	m_bOwnValues = false;	m_nStartIndex = 0;	m_dirty = true;}/*virtual*/ GWidgetMagnitudeGraph::~GWidgetMagnitudeGraph(){	if(m_bOwnValues)		delete(m_pValues);}/*virtual*/ void GWidgetMagnitudeGraph::Grab(int x, int y){}/*virtual*/ void GWidgetMagnitudeGraph::Release(){}void GWidgetMagnitudeGraph::Update(){	m_dirty = false;	m_image.Clear(m_cBackground);	// Draw grid lines	int i, j, y;	for(i = 0; i <= 10; i++)	{		y = i * (m_image.GetHeight() - 1) / 10;		m_image.DrawLine(0, y, m_image.GetWidth() - 1, y, m_cGridLines);	}	// Draw the graph	if(m_pValues && m_nValues > 0)	{		for(i = m_image.GetWidth() - 1; i >= 0; i--)		{			j = i * m_nValues / m_image.GetWidth();			j += m_nStartIndex;			if(j >= m_nValues)				j -= m_nValues;			y = m_image.GetHeight() - 1 - MAX((int)0, MIN((int)m_image.GetHeight() - 1, 						(int)(m_pValues[j] * (m_image.GetHeight() - 1))					));			m_image.DrawLine(i, m_image.GetHeight() - 1, i, y, m_cForeground);		}	}}GImage* GWidgetMagnitudeGraph::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 GWidgetMagnitudeGraph::SetValues(float* pValues, int nValues, bool bOwn){	if(m_bOwnValues)		delete(m_pValues);	m_pValues = pValues;	m_bOwnValues = bOwn;	m_nValues = nValues;	m_dirty = true;}void GWidgetMagnitudeGraph::SetStartIndex(int n){	GAssert(n >= 0 && n < m_nValues, "out of range");	m_nStartIndex = n;	m_dirty = true;}

⌨️ 快捷键说明

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