blob: 07d91870023116e702f77b1921271c6fc08242b8 [file] [log] [blame]
Benny Prijono9cf138e2006-01-19 03:58:29 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20
21#include <pj/string.h>
Benny Prijono37e8d332006-01-20 21:03:36 +000022#include <pj/unicode.h>
Benny Prijono9cf138e2006-01-19 03:58:29 +000023#include <pj/sock.h>
24#include <pj/log.h>
25
Benny Prijono37e8d332006-01-20 21:03:36 +000026#define WIN32_LEAN_AND_MEAN
27#define NONAMELESSUNION
Benny Prijono9cf138e2006-01-19 03:58:29 +000028#include <windows.h>
29#include <commctrl.h>
Benny Prijono37e8d332006-01-20 21:03:36 +000030#include <tchar.h>
Benny Prijono9cf138e2006-01-19 03:58:29 +000031
Benny Prijono37e8d332006-01-20 21:03:36 +000032#define MAX_LOADSTRING 100
33#define THIS_FILE "main_win32.c"
Benny Prijono9cf138e2006-01-19 03:58:29 +000034
Benny Prijono37e8d332006-01-20 21:03:36 +000035#define IDC_HELLO_WINCE 3
36#define ID_LOGWINDOW 104
Benny Prijono9cf138e2006-01-19 03:58:29 +000037
Benny Prijono9cf138e2006-01-19 03:58:29 +000038
Benny Prijono37e8d332006-01-20 21:03:36 +000039ATOM MyRegisterClass (HINSTANCE, LPTSTR);
40BOOL InitInstance (HINSTANCE, int);
41LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
Benny Prijono9cf138e2006-01-19 03:58:29 +000042
Benny Prijono37e8d332006-01-20 21:03:36 +000043
44extern int param_log_decor; // in test.c
45
46static HINSTANCE hInst;
47static HWND hwndLog;
48static HFONT hFixedFont;
49
Benny Prijono9cf138e2006-01-19 03:58:29 +000050
51static void write_log(int level, const char *data, int len)
52{
Benny Prijono37e8d332006-01-20 21:03:36 +000053 PJ_DECL_UNICODE_TEMP_BUF(wdata,256);
54
55 PJ_UNUSED_ARG(level);
56 PJ_UNUSED_ARG(len);
57 SendMessage(hwndLog, EM_REPLACESEL, FALSE,
58 (LPARAM)PJ_STRING_TO_NATIVE(data,wdata));
Benny Prijono9cf138e2006-01-19 03:58:29 +000059}
60
61
Benny Prijono37e8d332006-01-20 21:03:36 +000062int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
63 LPTSTR lpCmdLine, int nCmdShow)
Benny Prijono9cf138e2006-01-19 03:58:29 +000064{
65 MSG msg;
Benny Prijono9cf138e2006-01-19 03:58:29 +000066
Benny Prijono37e8d332006-01-20 21:03:36 +000067 PJ_UNUSED_ARG(lpCmdLine);
68 PJ_UNUSED_ARG(hPrevInstance);
69
70
71 if (!InitInstance (hInstance, nCmdShow))
Benny Prijono9cf138e2006-01-19 03:58:29 +000072 return FALSE;
Benny Prijono9cf138e2006-01-19 03:58:29 +000073
74 pj_log_set_log_func( &write_log );
Benny Prijono37e8d332006-01-20 21:03:36 +000075 param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR;
Benny Prijono9cf138e2006-01-19 03:58:29 +000076
Benny Prijono37e8d332006-01-20 21:03:36 +000077 // Run the test!
Benny Prijono9cf138e2006-01-19 03:58:29 +000078 test_main();
79
Benny Prijono37e8d332006-01-20 21:03:36 +000080 PJ_LOG(3,(THIS_FILE,""));
81 PJ_LOG(3,(THIS_FILE,"Press ESC to quit"));
82
83 // Message loop, waiting to quit.
84 while (GetMessage(&msg, NULL, 0, 0)) {
85 TranslateMessage(&msg);
86 DispatchMessage(&msg);
Benny Prijono9cf138e2006-01-19 03:58:29 +000087 }
88
Benny Prijono37e8d332006-01-20 21:03:36 +000089 DeleteObject(hFixedFont);
Benny Prijono9cf138e2006-01-19 03:58:29 +000090 return msg.wParam;
91}
92
Benny Prijono37e8d332006-01-20 21:03:36 +000093
94#ifdef _CONSOLE
95int main()
96{
97 return WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOW);
98}
99#endif
100
101
Benny Prijono9cf138e2006-01-19 03:58:29 +0000102ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
103{
104 WNDCLASS wc;
105
106 wc.style = CS_HREDRAW | CS_VREDRAW;
107 wc.lpfnWndProc = (WNDPROC) WndProc;
108 wc.cbClsExtra = 0;
109 wc.cbWndExtra = 0;
110 wc.hInstance = hInstance;
111 ///wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HELLO_WINCE));
112 wc.hIcon = NULL;
113 wc.hCursor = 0;
114 wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
115 wc.lpszMenuName = 0;
116 wc.lpszClassName = szWindowClass;
117
118 return RegisterClass(&wc);
119}
120
Benny Prijono37e8d332006-01-20 21:03:36 +0000121
Benny Prijono9cf138e2006-01-19 03:58:29 +0000122BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
123{
124 HWND hWnd;
Benny Prijono37e8d332006-01-20 21:03:36 +0000125 TCHAR *szTitle = _T("PJSIP Test");
126 TCHAR *szWindowClass = _T("PJSIP_TEST");
127 LOGFONT lf;
128
129
130 memset(&lf, 0, sizeof(lf));
131 lf.lfHeight = 13;
132#if PJ_NATIVE_STRING_IS_UNICODE
133 wcscpy(lf.lfFaceName, _T("Courier New"));
134#else
135 strcpy(lf.lfFaceName, "Lucida Console");
136#endif
137
138 hFixedFont = CreateFontIndirect(&lf);
139 if (!hFixedFont)
140 return FALSE;
141
142 hInst = hInstance;
Benny Prijono9cf138e2006-01-19 03:58:29 +0000143
144 MyRegisterClass(hInstance, szWindowClass);
145
146 hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
Benny Prijono37e8d332006-01-20 21:03:36 +0000147 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
148 CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
Benny Prijono9cf138e2006-01-19 03:58:29 +0000149
150 if (!hWnd)
Benny Prijono9cf138e2006-01-19 03:58:29 +0000151 return FALSE;
Benny Prijono9cf138e2006-01-19 03:58:29 +0000152
153 ShowWindow(hWnd, nCmdShow);
154 UpdateWindow(hWnd);
Benny Prijono37e8d332006-01-20 21:03:36 +0000155
156 if (hwndLog) {
157 SendMessage(hwndLog, WM_SETFONT, (WPARAM) hFixedFont, (LPARAM) 0);
158 ShowWindow(hwndLog, TRUE);
159 }
160
Benny Prijono9cf138e2006-01-19 03:58:29 +0000161 return TRUE;
162}
163
Benny Prijono37e8d332006-01-20 21:03:36 +0000164
Benny Prijono9cf138e2006-01-19 03:58:29 +0000165LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
166{
Benny Prijono9cf138e2006-01-19 03:58:29 +0000167 RECT rt;
168 DWORD dwStyle;
Benny Prijono9cf138e2006-01-19 03:58:29 +0000169
170 switch (message)
171 {
Benny Prijono37e8d332006-01-20 21:03:36 +0000172 case WM_CREATE:
173 // Create text control.
174 GetClientRect(hWnd, &rt);
175 dwStyle = WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
176 WS_BORDER | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL |
177 ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_READONLY;
178 hwndLog = CreateWindow( TEXT("edit"), // class
179 NULL, // window text
180 dwStyle, // style
181 0, // x-left
182 0, // y-top
183 rt.right-rt.left, // w
184 rt.bottom-rt.top, // h
185 hWnd, // parent
186 (HMENU)ID_LOGWINDOW,// id
187 hInst, // instance
188 NULL); // NULL for control.
189 break;
190 case WM_ACTIVATE:
191 if (LOWORD(wParam) == WA_INACTIVE)
192 DestroyWindow(hWnd);
193 break;
194 case WM_CHAR:
195 if (wParam == 27) {
196 DestroyWindow(hWnd);
Benny Prijono9cf138e2006-01-19 03:58:29 +0000197 }
198 break;
Benny Prijono37e8d332006-01-20 21:03:36 +0000199 case WM_CLOSE:
200 DestroyWindow(hWnd);
201 break;
202 case WM_DESTROY:
203 PostQuitMessage(0);
204 break;
205 default:
206 return DefWindowProc(hWnd, message, wParam, lParam);
Benny Prijono9cf138e2006-01-19 03:58:29 +0000207 }
208 return 0;
209}
210