windowing/windowserver/test/TClick/LOGWIN.CPP
changeset 103 2717213c588a
equal deleted inserted replaced
98:bf7481649c98 103:2717213c588a
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Extracted from DEBLOGWN.CPP
       
    15 // Code to run the WIN32 windows with the output
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <e32base.h>
       
    21 #include <emulator.h>
       
    22 #include "LOGWIN.H"
       
    23 
       
    24 GLDEF_D struct TSharedMemory GSharedMemory;
       
    25 //GLDEF_D struct HWND__ *Hwnd;
       
    26 //LOCAL_D RSemaphore Sem;
       
    27 
       
    28 const TUint KHeapSize=0x8000;
       
    29 
       
    30 
       
    31 TInt numLines(const CArrayVarSeg<TText> &aTextArray, TBool aOutOfMemory)
       
    32 	{
       
    33 	return (TInt)(aOutOfMemory)?
       
    34 				aTextArray.Count():
       
    35 				aTextArray.Count()-1;
       
    36 	}
       
    37 
       
    38 TInt numVisibleLines(TInt aHeight, TInt aTextHeight)
       
    39 	{
       
    40 	return aHeight/aTextHeight;
       
    41 	}
       
    42 
       
    43 TInt32 __stdcall WndProc(struct HWND__ *aHwnd, TUint aMessage, TUint wParam, TInt32 lParam)
       
    44     {
       
    45     HDC hdc;
       
    46     PAINTSTRUCT ps;
       
    47 	HFONT hfont;
       
    48     RECT rect;
       
    49 	TInt i,
       
    50 		paintMin,
       
    51 		paintMax;
       
    52 	static LOGFONT logFont;
       
    53 	static TEXTMETRIC tm;
       
    54 	static TInt textHeight,
       
    55 		width,
       
    56 		height,
       
    57 		scrollMin=0,
       
    58 		scrollMax,
       
    59 		numLinesAbove=scrollMin,
       
    60 		prevNumLinesAbove;
       
    61 	static CArrayVarSeg<TText> *pmsg;
       
    62 	static TBool outOfMemory=EFalse;
       
    63 
       
    64     switch (aMessage)
       
    65         {
       
    66     case WM_CREATE:
       
    67 		hdc=GetDC(aHwnd);
       
    68 
       
    69 		pmsg=new(ELeave) CArrayVarSeg<TText>(20);
       
    70 		{
       
    71 		TBuf<0x20> errorMsg;
       
    72 		errorMsg=_L("ERROR: out of memory");
       
    73 		errorMsg.ZeroTerminate();
       
    74 		TRAPD(err,pmsg->AppendL(*errorMsg.Ptr(), (errorMsg.Length()+1)*sizeof(TText)));
       
    75 		if (err!=KErrNone)
       
    76 			return(err);
       
    77 		}
       
    78 		GetTextMetrics(hdc, &tm);
       
    79 		textHeight=tm.tmHeight+tm.tmExternalLeading;
       
    80         GetClientRect(aHwnd, &rect);
       
    81 		width=rect.right;
       
    82 		height=rect.bottom;
       
    83 		scrollMax=numLines(*pmsg, outOfMemory);
       
    84 		SetScrollPos(aHwnd, SB_VERT, numLinesAbove, TRUE);
       
    85 		SetScrollRange(aHwnd, SB_VERT, scrollMin, scrollMax, FALSE);
       
    86 		logFont.lfHeight=8;
       
    87 		wsprintf(logFont.lfFaceName, (LPCTSTR)_S("courier"));
       
    88 
       
    89 		ReleaseDC(aHwnd, hdc);
       
    90 		return 0;
       
    91 	case WM_USER+EAppendText:
       
    92 		{
       
    93 		TRAPD(err,pmsg->InsertL(pmsg->Count()-1, *(TText *)lParam, wParam));
       
    94 		if (err!=KErrNone)
       
    95 			{
       
    96 			pmsg->Delete(0, Max(0, (TInt)pmsg->Count()-1));
       
    97 			numLinesAbove=scrollMax=scrollMin;
       
    98 			SetScrollPos(aHwnd, SB_VERT, numLinesAbove, TRUE);
       
    99 			SetScrollRange(aHwnd, SB_VERT, scrollMin, scrollMax, FALSE);
       
   100 			hdc=GetDC(aHwnd);
       
   101 			PatBlt(hdc, 0, 0, width, height, WHITENESS);
       
   102 			ReleaseDC(aHwnd, hdc);
       
   103 			return(err);
       
   104 			}
       
   105 		// adjust the scroll position so that the last line added is visible
       
   106 		if (numLinesAbove<numLines(*pmsg, outOfMemory)-numVisibleLines(height, textHeight))
       
   107 			{
       
   108 			prevNumLinesAbove=numLinesAbove;
       
   109 			numLinesAbove=numLines(*pmsg, outOfMemory)-numVisibleLines(height, textHeight);
       
   110 			scrollMax=numLines(*pmsg, outOfMemory);
       
   111 			SendMessage(aHwnd, WM_USER+EScrollToPos, 0, 0L);
       
   112 			}
       
   113 		else
       
   114 			{
       
   115 			hdc=GetDC(aHwnd);
       
   116 			hfont=(HFONT)SelectObject(hdc, CreateFontIndirect(&logFont));
       
   117 			TText *text=&((*pmsg)[scrollMax]);
       
   118 			TextOut(hdc, 0, (scrollMax-numLinesAbove)*textHeight, (LPCTSTR)text, User::StringLength(text));
       
   119 			scrollMax=numLines(*pmsg, outOfMemory);
       
   120 			ReleaseDC(aHwnd, hdc);
       
   121 			}
       
   122 		return KErrNone;
       
   123 		}
       
   124 	case WM_USER+EScrollToPos:
       
   125 		SetScrollPos(aHwnd, SB_VERT, numLinesAbove, TRUE);
       
   126 		SetScrollRange(aHwnd, SB_VERT, scrollMin, scrollMax, FALSE);
       
   127 
       
   128 		hdc=GetDC(aHwnd);
       
   129 		hfont=(HFONT)SelectObject(hdc, CreateFontIndirect(&logFont));
       
   130 
       
   131 		if (numLinesAbove>prevNumLinesAbove)
       
   132 		// scrolling towards end, therefore text moves up on screen
       
   133 			{
       
   134 			TInt numLinesToDraw=numLinesAbove-prevNumLinesAbove;
       
   135 			TInt numLinesToBlt=numVisibleLines(height, textHeight)-numLinesToDraw;
       
   136 			if (numLinesToBlt>0)
       
   137 				ScrollWindow(aHwnd, 0, -numLinesToDraw*textHeight, NULL, NULL);
       
   138 			PatBlt(hdc, 0, numLinesToBlt*textHeight, width, numLinesToDraw*textHeight, WHITENESS);
       
   139 			paintMin=Max(Min(numLinesAbove+numLinesToBlt, scrollMax), scrollMin);
       
   140 			paintMax=Min(paintMin+numLinesToDraw, scrollMax);
       
   141 			for (i=paintMin; i<paintMax; i++)
       
   142 				{
       
   143 				TText *text=&((*pmsg)[i]);
       
   144 				TextOut(hdc, 0, (i-numLinesAbove)*textHeight, (LPCTSTR)text, User::StringLength(text));
       
   145 				}
       
   146 			}
       
   147 		else
       
   148 		// scrolling towards beginning, therefore text moves down on screen
       
   149 			{
       
   150 			TInt numLinesToDraw=prevNumLinesAbove-numLinesAbove;
       
   151 			TInt numLinesToBlt=numVisibleLines(height, textHeight)-numLinesToDraw;
       
   152 			if (numLinesToBlt>0)
       
   153 				ScrollWindow(aHwnd, 0, numLinesToDraw*textHeight, NULL, NULL);
       
   154 			PatBlt(hdc, 0, 0, width, numLinesToDraw*textHeight, WHITENESS);
       
   155 			paintMin=Max(Min(numLinesAbove, scrollMax), scrollMin);
       
   156 			paintMax=Min(paintMin+numLinesToDraw, scrollMax);
       
   157 			for (i=paintMin; i<paintMax; i++)
       
   158 				{
       
   159 				TText *text=&((*pmsg)[i]);
       
   160 				TextOut(hdc, 0, (i-numLinesAbove)*textHeight, (LPCTSTR)text, User::StringLength(text));
       
   161 				}
       
   162 			}
       
   163 
       
   164 		DeleteObject(hfont);
       
   165 		ReleaseDC(aHwnd, hdc);
       
   166 		return 0;
       
   167     case WM_PAINT:
       
   168 		hdc=BeginPaint(aHwnd, &ps);
       
   169 		hfont=(HFONT)SelectObject(hdc, CreateFontIndirect(&logFont));
       
   170 
       
   171 		paintMin=Max(scrollMin, numLinesAbove);
       
   172 		paintMax=Min(numLines(*pmsg, outOfMemory), numLinesAbove+numVisibleLines(height, textHeight));
       
   173 		for (i=paintMin; i<paintMax; i++)
       
   174 				{
       
   175 				TText *text=&((*pmsg)[i]);
       
   176 				TextOut(hdc, 0, (i-numLinesAbove)*textHeight, (LPCTSTR)text, User::StringLength(text));
       
   177 				}
       
   178 
       
   179 		DeleteObject(hfont);
       
   180 		EndPaint(aHwnd, &ps);
       
   181         return 0;
       
   182     case WM_SIZE:
       
   183         width=LOWORD(lParam);
       
   184         height=HIWORD(lParam);
       
   185 		return 0;
       
   186     case WM_VSCROLL:
       
   187 		prevNumLinesAbove=numLinesAbove;
       
   188         switch (LOWORD(wParam))
       
   189 			{
       
   190 		case SB_TOP:
       
   191 			numLinesAbove=scrollMin;
       
   192 			break;
       
   193 		case SB_BOTTOM:
       
   194 			numLinesAbove=scrollMax;
       
   195 			break;
       
   196 		case SB_LINEUP:
       
   197 			numLinesAbove--;
       
   198 			break;
       
   199 		case SB_LINEDOWN:
       
   200 			numLinesAbove++;
       
   201 			break;
       
   202 		case SB_PAGEUP:
       
   203 			numLinesAbove-=numVisibleLines(height, textHeight);
       
   204 			break;
       
   205 		case SB_PAGEDOWN:
       
   206 			numLinesAbove+=numVisibleLines(height, textHeight);
       
   207 			break;
       
   208 		case SB_THUMBTRACK:
       
   209 			numLinesAbove=HIWORD(wParam);
       
   210 			break;
       
   211 			}
       
   212 		numLinesAbove=max(scrollMin, min(numLinesAbove, scrollMax));
       
   213 
       
   214 		if (numLinesAbove!=prevNumLinesAbove)
       
   215 			SendMessage(aHwnd, WM_USER+EScrollToPos, 0, 0L);
       
   216 		return 0;
       
   217     case WM_KEYDOWN:
       
   218 		switch (wParam)
       
   219 			{
       
   220 		case VK_HOME:
       
   221 			SendMessage(aHwnd, WM_VSCROLL, SB_TOP, 0L);
       
   222 			break;
       
   223 		case VK_END:
       
   224 			SendMessage(aHwnd, WM_VSCROLL, SB_BOTTOM, 0L);
       
   225 			break;
       
   226 		case VK_PRIOR:
       
   227 			SendMessage(aHwnd, WM_VSCROLL, SB_PAGEUP, 0L);
       
   228 			break;
       
   229 		case VK_NEXT:
       
   230 			SendMessage(aHwnd, WM_VSCROLL, SB_PAGEDOWN, 0L);
       
   231 			break;
       
   232 		case VK_UP:
       
   233 			SendMessage(aHwnd, WM_VSCROLL, SB_LINEUP, 0L);
       
   234 			break;
       
   235 		case VK_DOWN:
       
   236 			SendMessage(aHwnd, WM_VSCROLL, SB_LINEDOWN, 0L);
       
   237 			break;
       
   238 			}
       
   239 		return 0;
       
   240 	case WM_DESTROY:
       
   241 		delete pmsg;
       
   242 		PostQuitMessage(0);
       
   243 		return 0;
       
   244 		}
       
   245 
       
   246 	return DefWindowProc(aHwnd, aMessage, wParam, lParam);
       
   247 	}
       
   248 
       
   249 TInt logWinMain(TAny *)
       
   250     {
       
   251     MSG msg;
       
   252     WNDCLASS wndclass;
       
   253     const TText *szAppName=_S("Window Server Log");
       
   254 
       
   255     wndclass.style=CS_HREDRAW|CS_VREDRAW;
       
   256     wndclass.lpfnWndProc=WndProc;
       
   257     wndclass.cbClsExtra=0;
       
   258     wndclass.cbWndExtra=0;
       
   259     wndclass.hInstance=NULL;
       
   260     wndclass.hIcon=LoadIcon(NULL, IDI_APPLICATION);
       
   261     wndclass.hCursor=LoadCursor(NULL, IDC_ARROW);
       
   262 	//wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
       
   263 	wndclass.hbrBackground=(HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
       
   264     wndclass.lpszMenuName=NULL;
       
   265     wndclass.lpszClassName=(LPCTSTR)szAppName;
       
   266 
       
   267     RegisterClass(&wndclass);
       
   268 
       
   269 	GSharedMemory.iHwnd=CreateWindow((LPCTSTR)szAppName,
       
   270                     (LPCTSTR)szAppName,
       
   271 					WS_OVERLAPPEDWINDOW,
       
   272 					//WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_MINIMIZE,
       
   273                     CW_USEDEFAULT,
       
   274                     CW_USEDEFAULT,
       
   275                     CW_USEDEFAULT,
       
   276                     CW_USEDEFAULT,
       
   277                     NULL,
       
   278                     NULL,
       
   279                     NULL,
       
   280                     NULL);
       
   281 
       
   282 	ShowWindow(GSharedMemory.iHwnd, SW_SHOWMINNOACTIVE);
       
   283 	//ShowWindow(GSharedMemory.iHwnd, SW_MINIMIZE);
       
   284 	UpdateWindow(GSharedMemory.iHwnd);
       
   285 	GSharedMemory.iStartSemaphore.Signal(); // allows logging to start now that the window, etc. has been set up
       
   286 	Emulator::Escape();
       
   287     while (GetMessage(&msg, NULL, 0, 0))
       
   288         {
       
   289         TranslateMessage(&msg);
       
   290         DispatchMessage(&msg);
       
   291         }
       
   292 	Emulator::Reenter();
       
   293     return msg.wParam;
       
   294     }
       
   295 
       
   296 GLDEF_C void CreateLogWinThreadL()
       
   297 	{
       
   298 	_LIT(KLogWin,"LogingWindow");
       
   299 	RThread wsThread;
       
   300 	GSharedMemory.iStartSemaphore.CreateLocal(0);
       
   301 	User::LeaveIfError(wsThread.Create(KLogWin,logWinMain,KDefaultStackSize,KHeapSize,KHeapSize,NULL));
       
   302 	wsThread.Resume();
       
   303 	GSharedMemory.iStartSemaphore.Wait();
       
   304 	}