示例#1
0
文件: GLApi.cpp 项目: DashW/Ingenuity
void Api::DebugMessageCallback(unsigned source, unsigned type, unsigned id,
	unsigned severity, int length, const char* message, const void* userParam)
{
	std::string shortMessage = message;
	std::wstring wideMessage(shortMessage.begin(), shortMessage.end());

	OutputDebugString(wideMessage.c_str());
	OutputDebugString(L"\n");
}
	BOOL Win32PopupDialog::ShowMessageBox(HWND hwnd)
	{
		BOOL fSuccess = FALSE;
		HDC hdc = GetDC(hwnd);

		if (hdc)
		{
			NONCLIENTMETRICSW nonClientMetrics = { sizeof(NONCLIENTMETRICSW) };
			if (SystemParametersInfoW(
				SPI_GETNONCLIENTMETRICS, 0, &nonClientMetrics, 0))
			{
				DialogTemplate tmp;
				std::wstring wideTitle(::UTF8ToWide(title));
				std::wstring wideInputText;
				if (this->showInputText)
					wideInputText.assign(::UTF8ToWide(inputText));

				int controlCount = 2;	// at minimum, static label and OK button
				if (this->showCancelButton)
					controlCount++;
				if (this->showInputText)
					controlCount++;

				int messageLines = 0;
				int newlines = CountMatches(message, "\n");
				int tabs = CountMatches(message, "\t");
				int spaces = CountMatches(message, " ");

				if (newlines == 0 && tabs == 0 && spaces == 0)
				{
					std::string tempMessage(message);
					if (tempMessage.length() != 0)
					{
						int insertAt = 60;
						int count = 0;

						std::string::iterator it = tempMessage.begin();
						for (; it < tempMessage.end(); it++)
						{
							count++;
							if (count == 60)
							{	
								count = 0;
								message.insert(insertAt, "\n");
								insertAt += 60;
							}
						}
					}
				}
				
				std::wstring wideMessage(::UTF8ToWide(message));
				messageLines = message.length() / 60;
				messageLines += ((int) ceil((double)tabs / 14));
				messageLines += newlines;
				
				if (tabs == 0 || newlines == 0)
					messageLines++;

				int labelHeight = 14;
				int width = 200;
				int margin = 10;
				int buttonWidth = 50;
				int buttonHeight = 14;
				int inputHeight = 14;
				int messageHeight = (messageLines * 12) + (messageLines * margin);
				int height = messageHeight + 56;
				
				if (!this->showInputText)
				{
					height -= (inputHeight + margin);
				}

				// Write out the extended dialog template header
				tmp.Write<WORD>(1); // dialog version
				tmp.Write<WORD>(0xFFFF); // extended dialog template
				tmp.Write<DWORD>(0); // help ID
				tmp.Write<DWORD>(0); // extended style
				tmp.Write<DWORD>(WS_CAPTION | WS_BORDER | DS_ABSALIGN | DS_SETFONT);
				tmp.Write<WORD>(controlCount); // number of controls
				tmp.Write<WORD>(32); // X
				tmp.Write<WORD>(32); // Y
				tmp.Write<WORD>(width); // width
				tmp.Write<WORD>(height); // height
				tmp.WriteString(L""); // no menu
				tmp.WriteString(L""); // default dialog class
				//tmp.WriteString(pszTitle); // title
				tmp.WriteString(wideTitle.c_str()); // title

				// Next comes the font description.
				// See text for discussion of fancy formula.
				
				if (nonClientMetrics.lfMessageFont.lfHeight < 0)
				{
					int dpi = GetDeviceCaps(hdc, LOGPIXELSY);
					nonClientMetrics.lfMessageFont.lfHeight =
						-MulDiv(nonClientMetrics.lfMessageFont.lfHeight, 72, dpi);
				}
				tmp.Write<WORD>((WORD)nonClientMetrics.lfMessageFont.lfHeight); // point
				tmp.Write<WORD>((WORD)nonClientMetrics.lfMessageFont.lfWeight); // weight
				tmp.Write<BYTE>(nonClientMetrics.lfMessageFont.lfItalic); // Italic
				tmp.Write<BYTE>(nonClientMetrics.lfMessageFont.lfCharSet); // CharSet
				tmp.WriteString(nonClientMetrics.lfMessageFont.lfFaceName);

				// First control - static label
				tmp.AlignToDword();
				tmp.Write<DWORD>(0); // help id
				tmp.Write<DWORD>(0); // window extended style
				tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | SS_LEFT); // style
				tmp.Write<WORD>(margin); // x
				tmp.Write<WORD>(margin); // y
				tmp.Write<WORD>(width - (2 * margin)); // width
				//tmp.Write<WORD>(labelHeight); // height
				tmp.Write<WORD>(messageHeight); // height
				tmp.Write<DWORD>(-1); // control ID
				tmp.Write<DWORD>(0x0082FFFF); // static
				//tmp.Write<DWORD>(SS_LEFT);
				tmp.WriteString(wideMessage.c_str()); // text
				tmp.Write<WORD>(0); // no extra data

				// Second control - the OK button.
				tmp.AlignToDword();
				tmp.Write<DWORD>(0); // help id
				tmp.Write<DWORD>(0); // window extended style
				tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON); // style
				tmp.Write<WORD>(width - margin - buttonWidth); // x
				tmp.Write<WORD>(height - margin - buttonHeight); // y
				tmp.Write<WORD>(buttonWidth); // width
				tmp.Write<WORD>(buttonHeight); // height
				tmp.Write<DWORD>(IDCANCEL); // control ID
				tmp.Write<DWORD>(0x0080FFFF); // button class atom
				tmp.WriteString(L"Cancel"); // text
				tmp.Write<WORD>(0); // no extra data

				if (this->showCancelButton)
				{
					// The Cancel button
					tmp.AlignToDword();
					tmp.Write<DWORD>(0); // help id
					tmp.Write<DWORD>(0); // window extended style
					tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON); // style
					tmp.Write<WORD>(width - 2 * margin - 2 * buttonWidth); // x
					tmp.Write<WORD>(height - margin - buttonHeight); // y
					tmp.Write<WORD>(buttonWidth); // width
					tmp.Write<WORD>(buttonHeight); // height
					tmp.Write<DWORD>(IDOK); // control ID
					tmp.Write<DWORD>(0x0080FFFF); // button class atom
					tmp.WriteString(L"OK"); // text
					tmp.Write<WORD>(0); // no extra data
				}

				if (this->showInputText)
				{
					// The input field
					tmp.AlignToDword();
					tmp.Write<DWORD>(0); // help id
					tmp.Write<DWORD>(0); // window extended style
					tmp.Write<DWORD>(ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE); // style
					tmp.Write<WORD>(margin); // x
					tmp.Write<WORD>(margin + labelHeight + margin); // y
					tmp.Write<WORD>(width - (2 * margin)); // width
					tmp.Write<WORD>(inputHeight); // height
					tmp.Write<DWORD>(ID_INPUT_FIELD); // control ID
					tmp.Write<DWORD>(0x0081FFFF); // edit  class atom
					tmp.WriteString(wideInputText.c_str()); // text
					tmp.Write<WORD>(0); // no extra data
				}

				// Template is ready - go display it.
				fSuccess = DialogBoxIndirect(
					GetModuleHandle(NULL),
					tmp.Template(),
					hwnd,
					&Win32PopupDialog::Callback) >= 0;
			}
			ReleaseDC(NULL, hdc); // fixed 11 May
		}
		return fSuccess;
	}