upseudosupport.cpp

来自「WinCVS 源码,流行的CVS客户端源码程序」· C++ 代码 · 共 969 行 · 第 1/2 页

CPP
969
字号
#include "stdafx.h"

#include <stdlib.h>
#include <Errors.h>

#include "UPseudoSupport.h"
#include "UPopup.h"
#include "UDialogTEView.h"
#include "UEditText.h"
#include "UWindow.h"
#include "USlider.h"

#include "uconsole.h"

#define MA_CHECK(cond) Assertion_AC(cond, #cond)

extern "C" char *strdup(const char *str)
{
	if(str == 0L)
		return 0L;
		
	size_t len = strlen(str);
	char *res = (char *)malloc((len + 1) * sizeof(char));
	if(res == 0L)
		throw CException_AC(mFulErr);
	strcpy(res, str);
	return res;
}

#undef Inherited
#define Inherited TBehavior

MA_DEFINE_CLASS_M1(TPseudoWinBehavior, Inherited);

TPseudoWinBehavior::TPseudoWinBehavior() : TBehavior(kPseudoWinBehavior), fWidID(-1)
{
}

TPseudoWinBehavior::~TPseudoWinBehavior()
{
}

void TPseudoWinBehavior::DoEvent(EventNumber eventNumber, TEventHandler* source, TEvent* event)
{
	// find out the command who fired the event (if any)
	int cmdid = -1;
	
	// the typing event is originated by a sub-view of an TEditText field
	TEventHandler* ourSource = source;
	if(eventNumber == cTyping && MA_MEMBER(ourSource, TDialogTEView))
	{
		ourSource = ((TDialogTEView *)ourSource)->fEditText;
	}
	
	if(fWidID != -1)
		cmdid = UEventSendMessage(fWidID, EV_FETCHCMD, 0, ourSource);
	
	if(cmdid != -1) switch(eventNumber)
	{
		case mBecameTarget:
		case mResignedTarget:
			UEventNotifyMessage(EV_UPDATEFOCUS, eventNumber == mResignedTarget, ourSource);
			break;
		case mButtonHit:
		case mRadioHit:
		case mCheckBoxHit:
			UEventSendMessage(fWidID, EV_CMD, cmdid, 0L);
			break;
		case mHScrollBarHit:
		case mVScrollBarHit:
			if(MA_MEMBER(ourSource, TSlider) || MA_MEMBER(ourSource, TScrollBar))
			{
				TCtlMgr *slider = (TCtlMgr *)ourSource;
				UEventSendMessage(fWidID, EV_SCROLLCHANGE, cmdid, (void *)slider->GetValue());
			}
			break;
		case mPopupHit:
			if(MA_MEMBER(ourSource, TPopup))
			{
				TPopup *popup = (TPopup *)ourSource;
				short cur = popup->GetCurrentItem();
				CStr255_AC pstr = popup->GetItemText(cur);
				UPStr content(pstr);
				UEventSendMessage(fWidID, EV_COMBO_SEL, UMAKEINT(cmdid, cur - 1),
					(void *)(const char *)content);
			}
			break;
		case mControlHit:
			if(MA_MEMBER(ourSource, TSlider))
			{
				TSlider *slider = (TSlider *)ourSource;
				UEventSendMessage(fWidID, EV_SCROLLCHANGE, cmdid, (void *)slider->GetValue());
			}
			break;
		case cTyping:
			if(MA_MEMBER(ourSource, TEditText))
			{
				UEventSendMessage(fWidID, EV_EDITCHANGE, cmdid, 0L);
			}
			break;
		case TPseudoTextListView::kSelectionChanged:
		case TPseudoTextListView::kMultiClickSelection:
			if(MA_MEMBER(ourSource, TPseudoTextListView))
			{
				TPseudoTextListView *txt = (TPseudoTextListView *)ourSource;
				short entry = txt->FirstSelectedItem();
				CChar255_AC name;
				if(entry > 0)
					name = txt->GetItemText(entry);
					
				UEventSendMessage(fWidID,
					eventNumber == TPseudoTextListView::kSelectionChanged ?
					EV_LIST_SELECTING : EV_LIST_DBLCLICK, UMAKEINT(cmdid, entry - 1),
					(void *)(const char *)name);
			}
			break;
	}
	
	Inherited::DoEvent(eventNumber, source, event);
}


#undef Inherited
#define Inherited TTextListView
MA_DEFINE_CLASS_M0(TPseudoTextListView);

TPseudoTextListView::TPseudoTextListView()
{
}



TPseudoTextListView::~TPseudoTextListView()
{
}


IDType TPseudoTextListView::GetStandardSignature(void) const
{
	return TPseudoTextListView::kTextStringListViewSignature;
}

CStr255_AC TPseudoTextListView::GetItemText(GridCoordinate anItem) const
{
	MA_CHECK(this->fNumOfRows == this->fStringContainer.size());
	MA_CHECK(1 <= anItem && this->fStringContainer.size() >= anItem);

	CStr255_AC aString = this->fStringContainer[anItem - 1];
	return aString;
}

void TPseudoTextListView::InsStringBefore(short anItem, const char * string)
{
	MA_CHECK(1 <= anItem && this->fStringContainer.size() >= anItem - 1);

	std::vector<CStr255_AC>::iterator iter = this->fStringContainer.end();

	try
	{
		iter = this->fStringContainer.insert(
					this->fStringContainer.begin() + (anItem - 1), string);
	}
	catch(CException_AC& theException)
	{
		if (iter != this->fStringContainer.end())
		{
			this->fStringContainer.erase(iter);
				// Put it back like it was.  This will maintain internal consistency.
		}
		throw;
	}

	this->InsItemBefore(anItem, 1);
}

void TPseudoTextListView::InsStringFirst(const char * string)
{
	this->InsStringBefore(1, string);
}

void TPseudoTextListView::InsStringLast(const char * string)
{
	this->InsStringBefore(this->fNumOfRows + 1, string);
}

void TPseudoTextListView::DelItemAt(GridCoordinate anItem, GridCoordinate numOfItems)
{
	this->fStringContainer.erase(
			this->fStringContainer.begin() + (anItem - 1),
			this->fStringContainer.begin() + (anItem - 1 + numOfItems));

	this->Inherited::DelItemAt(anItem, numOfItems);
}

void TPseudoTextListView::RemoveAll(void)
{
	this->DelItemFirst(this->fNumOfRows);
}

bool TPseudoTextListView::DoMouseCommand_SelectCells(GridCell whichCell, TToolboxEvent * event)
{
	MA_CHECK(nil != event);

	bool selectionChanged = false;
		// Will be true if the selection changed.

	if (this->CanSelectCell(whichCell))
	{
		bool shifted = event->IsShiftKeyPressed();
		bool commandKey = event->IsCommandKeyPressed();

		if (commandKey)
		{
			this->SelectCell(whichCell, !this->fSingleSelection,
					kHighlight, !this->IsCellSelected(whichCell));

			selectionChanged = true;
		}
		else if (shifted && !this->fSingleSelection)
		{
			GridCell runner = whichCell;

			if (this->IsCellSelected(whichCell))
			{
				for ( ; runner.v <= this->fNumOfRows; runner.v++)
				{
					if (!this->IsCellSelected(runner))
					{
						break;
					}

					this->SelectCell(runner, kExtend, kHighlight, kDeSelect);
				}
			}
			else
			{
				for ( ; runner.v > 0; runner.v--)
				{
					if (this->IsCellSelected(runner))
					{
						break;
					}

					this->SelectCell(runner, kExtend, kHighlight, kSelect);
				}
			}

			selectionChanged = true;
		}
		else if (!this->IsCellSelected(whichCell))
		{
			this->SelectCell(whichCell, kDontExtend, kHighlight, kSelect);

			selectionChanged = true;
		}
	}

	return selectionChanged;
}

void TPseudoTextListView::DoMouseCommand(CViewPoint & theMouse, TToolboxEvent * event, CPoint_AC)
{
	GridCell aCell;

	if (badChoice != this->IdentifyPoint(theMouse, aCell))
	{
		this->DoMouseCommand_SelectCells(aCell, event);
			// First be sure to select/deselect the cell they clicked on.

		if (event->GetClickCount() > 1)
		{
			this->HandleEvent(kMultiClickSelection, this, event);
		}
	}
}

void TPseudoTextListView::DoKeyEvent(TToolboxEvent* event)// override 
{
	Boolean handledCharacter = false;

	if (this->IsEnabled() && this->Focus())		// if view is not enabled then we don't take ANY keystrokes
	{
		GridCell selected;
		unsigned char ch = event->GetText()[1];
		
		if (ch == chUp)
		{
			selected = this->FirstSelectedCell();
			selected.v -= 1;
			 
			this->DoMouseCommand_SelectCells(selected, event);
			
			handledCharacter = true;
		}
		else if (ch == chDown)
		{
			selected = this->LastSelectedCell();
			selected.v += 1;
			 
			this->DoMouseCommand_SelectCells(selected, event);
			
			handledCharacter = true;
		}
	}

	if (!handledCharacter)
		Inherited::DoKeyEvent(event);
}

void TPseudoTextListView::SetSelection(const TGridViewDesignator* cellsToSelect,
						bool extendSelection, bool highlight, bool select)
{
	this->Inherited::SetSelection(cellsToSelect, extendSelection, highlight, select);

	this->Changed(kSelectionChanged, this);
	this->HandleEvent(kSelectionChanged, this, nil);
}

#undef Inherited
#define Inherited TView

MA_DEFINE_CLASS_M1(TPseudoCustomView, Inherited);

TPseudoCustomView::TPseudoCustomView()
{
}

TPseudoCustomView::~TPseudoCustomView()
{
}

void TPseudoCustomView::Draw(const CViewRect& area)
{
	TWindow *wind = GetWindow();
	if(wind != 0L)
	{
		TPseudoWinBehavior *pseudo = dynamic_cast_AC(TPseudoWinBehavior*,
			wind->GetBehaviorWithIdentifier(TPseudoWinBehavior::kPseudoWinBehavior));
		if(pseudo != 0L)
		{
			int cmdid = UEventSendMessage(pseudo->GetWidID(), EV_FETCHCMD, 0, this);
			if(cmdid != -1)
				UEventSendMessage(pseudo->GetWidID(), EV_CUSTOM_DRAW, cmdid, this);
		}
	}
}


#undef Inherited
#define Inherited TEditText

MA_DEFINE_CLASS_M1(TPseudoDoubleNumberText, Inherited);

TPseudoDoubleNumberText::TPseudoDoubleNumberText() : fMaximum(DBL_MAX), fMinimum(DBL_MIN),
	fPrecision(kDefaultPrecision)
{
}

TPseudoDoubleNumberText::~TPseudoDoubleNumberText()
{
}

IDType TPseudoDoubleNumberText::GetStandardSignature() const
{
	return kStdDoubleNumberText;
}

void TPseudoDoubleNumberText::ReadFields(CStream_AC* aStream)
{
	Inherited::ReadFields(aStream);

	CStr255_AC theString;
	theString = aStream->ReadString(255);
	sscanf(CChar255_AC(theString), "%lf", &fMinimum);
	theString = aStream->ReadString(255);
	sscanf(CChar255_AC(theString), "%lf", &fMaximum);
	fPrecision = aStream->ReadInteger();
}

void TPseudoDoubleNumberText::WriteFields(CStream_AC* aStream) const
{
	Inherited::WriteFields(aStream);

	char tmp[255];
	sprintf(tmp, "%lf", fMinimum);
	aStream->WriteString(CStr255_AC(tmp));
	sprintf(tmp, "%lf", fMaximum);
	aStream->WriteString(CStr255_AC(tmp));
	aStream->WriteLong(fPrecision);
}


double TPseudoDoubleNumberText::GetValue()
{
	double theValue = 0.0;
	CStr255_AC theString = GetText();

	if (!theString.IsEmpty())
	{
		sscanf(CChar255_AC(theString), "%lf", &theValue);
	}

	return theValue;
}

static void NumberToString_US(double n, UStr & s, int decimalPlaces)
{
	char tmp[255];
	if(decimalPlaces > 0)
		sprintf(tmp, "%0.*f", decimalPlaces, n);
	else
		sprintf(tmp, "%ld", round(n));
	s = tmp;
}

void TPseudoDoubleNumberText::SetValue(double newValue, bool redraw)
{
	newValue = fMinimum >= newValue ? fMinimum : newValue;
	newValue = newValue <= fMaximum ? newValue : fMaximum;

	UStr aString;
	NumberToString_US(newValue, aString, fPrecision);

	CStr255_AC tempString = aString; 
	SetText(tempString, redraw);
}

long TPseudoDoubleNumberText::GetValidationError()
{
	CStr255_AC theString = GetText();

	if (!theString.IsEmpty())
	{
		double extValue;
		bool valid = sscanf(CChar255_AC(theString), "%lf", &extValue) == 1;

		if (!valid)
			return kNonNumericCharacters;
		else if (extValue < fMinimum)
			return kValueTooSmall;
		else if (extValue > fMaximum)
			return kValueTooLarge;
	}
	
	return kValidValue;
}

#undef Inherited
#define Inherited TBehavior

MA_DEFINE_CLASS_M1(TPseudoAppBehavior, Inherited);

TPseudoAppBehavior::TPseudoAppBehavior() : TBehavior()
{
	SetIdleFreq(0); // ASAP
}

TPseudoAppBehavior::~TPseudoAppBehavior()
{
}

bool TPseudoAppBehavior::DoIdle(IdlePhase phase)
{
	::UEventGiveTime();
	return false;
}

void TPseudoAppBehavior::DoMenuCommand(CommandNumber aCommandNumber)
{
	if(UMacAppMenu::sMAMenuWidID != -1)
	{
		int handledFlag;
		int res = UEventSendMessageExt(UMacAppMenu::sMAMenuWidID, EV_CMD,
			aCommandNumber, 0L, &handledFlag);
		if(res == 0 && handledFlag)
			return;
	}

	// regular MacApp handling
	Inherited::DoMenuCommand(aCommandNumber);
}

void TPseudoAppBehavior::DoSetupMenus()
{
	UWidget::DoValidUI(false);

⌨️ 快捷键说明

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