blob: f18c63cc69623bd4c70398d9ddb4bc1955aa0c7d [file] [log] [blame]
Benny Prijono9cf138e2006-01-19 03:58:29 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9cf138e2006-01-19 03:58:29 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "test.h"
21
22#include <pj/string.h>
Benny Prijono37e8d332006-01-20 21:03:36 +000023#include <pj/unicode.h>
Benny Prijono9cf138e2006-01-19 03:58:29 +000024#include <pj/sock.h>
25#include <pj/log.h>
26
Benny Prijono37e8d332006-01-20 21:03:36 +000027#define WIN32_LEAN_AND_MEAN
28#define NONAMELESSUNION
Benny Prijono9cf138e2006-01-19 03:58:29 +000029#include <windows.h>
30#include <commctrl.h>
Benny Prijono37e8d332006-01-20 21:03:36 +000031#include <tchar.h>
Benny Prijono9cf138e2006-01-19 03:58:29 +000032
Benny Prijono37e8d332006-01-20 21:03:36 +000033#define MAX_LOADSTRING 100
34#define THIS_FILE "main_win32.c"
Benny Prijono9cf138e2006-01-19 03:58:29 +000035
Benny Prijono37e8d332006-01-20 21:03:36 +000036#define IDC_HELLO_WINCE 3
37#define ID_LOGWINDOW 104
Benny Prijono9cf138e2006-01-19 03:58:29 +000038
Benny Prijono9cf138e2006-01-19 03:58:29 +000039
Benny Prijono37e8d332006-01-20 21:03:36 +000040ATOM MyRegisterClass (HINSTANCE, LPTSTR);
41BOOL InitInstance (HINSTANCE, int);
42LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
Benny Prijono9cf138e2006-01-19 03:58:29 +000043
Benny Prijono37e8d332006-01-20 21:03:36 +000044
45extern int param_log_decor; // in test.c
46
47static HINSTANCE hInst;
48static HWND hwndLog;
49static HFONT hFixedFont;
50
Benny Prijono9cf138e2006-01-19 03:58:29 +000051
52static void write_log(int level, const char *data, int len)
53{
Benny Prijono37e8d332006-01-20 21:03:36 +000054 PJ_DECL_UNICODE_TEMP_BUF(wdata,256);
55
56 PJ_UNUSED_ARG(level);
57 PJ_UNUSED_ARG(len);
58 SendMessage(hwndLog, EM_REPLACESEL, FALSE,
Benny Prijono8d317a02006-03-22 11:49:19 +000059 (LPARAM)PJ_STRING_TO_NATIVE(data,wdata,256));
Benny Prijono9cf138e2006-01-19 03:58:29 +000060}
61
62
Benny Prijono37e8d332006-01-20 21:03:36 +000063int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
64 LPTSTR lpCmdLine, int nCmdShow)
Benny Prijono9cf138e2006-01-19 03:58:29 +000065{
66 MSG msg;
Benny Prijono9cf138e2006-01-19 03:58:29 +000067
Benny Prijono37e8d332006-01-20 21:03:36 +000068 PJ_UNUSED_ARG(lpCmdLine);
69 PJ_UNUSED_ARG(hPrevInstance);
70
71
72 if (!InitInstance (hInstance, nCmdShow))
Benny Prijono9cf138e2006-01-19 03:58:29 +000073 return FALSE;
Benny Prijono9cf138e2006-01-19 03:58:29 +000074
75 pj_log_set_log_func( &write_log );
Benny Prijono37e8d332006-01-20 21:03:36 +000076 param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR;
Benny Prijono9cf138e2006-01-19 03:58:29 +000077
Benny Prijono37e8d332006-01-20 21:03:36 +000078 // Run the test!
Benny Prijono9cf138e2006-01-19 03:58:29 +000079 test_main();
80
Benny Prijono37e8d332006-01-20 21:03:36 +000081 PJ_LOG(3,(THIS_FILE,""));
82 PJ_LOG(3,(THIS_FILE,"Press ESC to quit"));
83
84 // Message loop, waiting to quit.
85 while (GetMessage(&msg, NULL, 0, 0)) {
86 TranslateMessage(&msg);
87 DispatchMessage(&msg);
Benny Prijono9cf138e2006-01-19 03:58:29 +000088 }
89
Benny Prijono37e8d332006-01-20 21:03:36 +000090 DeleteObject(hFixedFont);
Benny Prijono9cf138e2006-01-19 03:58:29 +000091 return msg.wParam;
92}
93
Benny Prijono37e8d332006-01-20 21:03:36 +000094
95#ifdef _CONSOLE
96int main()
97{
98 return WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOW);
99}
100#endif
101
102
Benny Prijono9cf138e2006-01-19 03:58:29 +0000103ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
104{
105 WNDCLASS wc;
106
107 wc.style = CS_HREDRAW | CS_VREDRAW;
108 wc.lpfnWndProc = (WNDPROC) WndProc;
109 wc.cbClsExtra = 0;
110 wc.cbWndExtra = 0;
111 wc.hInstance = hInstance;
112 ///wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HELLO_WINCE));
113 wc.hIcon = NULL;
114 wc.hCursor = 0;
115 wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
116 wc.lpszMenuName = 0;
117 wc.lpszClassName = szWindowClass;
118
119 return RegisterClass(&wc);
120}
121
Benny Prijono37e8d332006-01-20 21:03:36 +0000122
Benny Prijono9cf138e2006-01-19 03:58:29 +0000123BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
124{
125 HWND hWnd;
Benny Prijono37e8d332006-01-20 21:03:36 +0000126 TCHAR *szTitle = _T("PJSIP Test");
127 TCHAR *szWindowClass = _T("PJSIP_TEST");
128 LOGFONT lf;
129
130
131 memset(&lf, 0, sizeof(lf));
132 lf.lfHeight = 13;
133#if PJ_NATIVE_STRING_IS_UNICODE
134 wcscpy(lf.lfFaceName, _T("Courier New"));
135#else
136 strcpy(lf.lfFaceName, "Lucida Console");
137#endif
138
139 hFixedFont = CreateFontIndirect(&lf);
140 if (!hFixedFont)
141 return FALSE;
142
143 hInst = hInstance;
Benny Prijono9cf138e2006-01-19 03:58:29 +0000144
145 MyRegisterClass(hInstance, szWindowClass);
146
147 hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
Benny Prijono37e8d332006-01-20 21:03:36 +0000148 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
149 CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
Benny Prijono9cf138e2006-01-19 03:58:29 +0000150
151 if (!hWnd)
Benny Prijono9cf138e2006-01-19 03:58:29 +0000152 return FALSE;
Benny Prijono9cf138e2006-01-19 03:58:29 +0000153
154 ShowWindow(hWnd, nCmdShow);
155 UpdateWindow(hWnd);
Benny Prijono37e8d332006-01-20 21:03:36 +0000156
157 if (hwndLog) {
158 SendMessage(hwndLog, WM_SETFONT, (WPARAM) hFixedFont, (LPARAM) 0);
159 ShowWindow(hwndLog, TRUE);
160 }
161
Benny Prijono9cf138e2006-01-19 03:58:29 +0000162 return TRUE;
163}
164
Benny Prijono37e8d332006-01-20 21:03:36 +0000165
Benny Prijono9cf138e2006-01-19 03:58:29 +0000166LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
167{
Benny Prijono9cf138e2006-01-19 03:58:29 +0000168 RECT rt;
169 DWORD dwStyle;
Benny Prijono9cf138e2006-01-19 03:58:29 +0000170
171 switch (message)
172 {
Benny Prijono37e8d332006-01-20 21:03:36 +0000173 case WM_CREATE:
174 // Create text control.
175 GetClientRect(hWnd, &rt);
176 dwStyle = WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
177 WS_BORDER | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL |
178 ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_READONLY;
179 hwndLog = CreateWindow( TEXT("edit"), // class
180 NULL, // window text
181 dwStyle, // style
182 0, // x-left
183 0, // y-top
184 rt.right-rt.left, // w
185 rt.bottom-rt.top, // h
186 hWnd, // parent
187 (HMENU)ID_LOGWINDOW,// id
188 hInst, // instance
189 NULL); // NULL for control.
190 break;
191 case WM_ACTIVATE:
192 if (LOWORD(wParam) == WA_INACTIVE)
193 DestroyWindow(hWnd);
194 break;
195 case WM_CHAR:
196 if (wParam == 27) {
197 DestroyWindow(hWnd);
Benny Prijono9cf138e2006-01-19 03:58:29 +0000198 }
199 break;
Benny Prijono37e8d332006-01-20 21:03:36 +0000200 case WM_CLOSE:
201 DestroyWindow(hWnd);
202 break;
203 case WM_DESTROY:
204 PostQuitMessage(0);
205 break;
206 default:
207 return DefWindowProc(hWnd, message, wParam, lParam);
Benny Prijono9cf138e2006-01-19 03:58:29 +0000208 }
209 return 0;
210}
211