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