Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 1 | /* $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 Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 22 | #include <pj/unicode.h> |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 23 | #include <pj/sock.h> |
| 24 | #include <pj/log.h> |
| 25 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 26 | #define WIN32_LEAN_AND_MEAN |
| 27 | #define NONAMELESSUNION |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 28 | #include <windows.h> |
| 29 | #include <commctrl.h> |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 30 | #include <tchar.h> |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 31 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 32 | #define MAX_LOADSTRING 100 |
| 33 | #define THIS_FILE "main_win32.c" |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 34 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 35 | #define IDC_HELLO_WINCE 3 |
| 36 | #define ID_LOGWINDOW 104 |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 37 | |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 38 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 39 | ATOM MyRegisterClass (HINSTANCE, LPTSTR); |
| 40 | BOOL InitInstance (HINSTANCE, int); |
| 41 | LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 42 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 43 | |
| 44 | extern int param_log_decor; // in test.c |
| 45 | |
| 46 | static HINSTANCE hInst; |
| 47 | static HWND hwndLog; |
| 48 | static HFONT hFixedFont; |
| 49 | |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 50 | |
| 51 | static void write_log(int level, const char *data, int len) |
| 52 | { |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 53 | 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 Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 62 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
| 63 | LPTSTR lpCmdLine, int nCmdShow) |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 64 | { |
| 65 | MSG msg; |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 66 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 67 | PJ_UNUSED_ARG(lpCmdLine); |
| 68 | PJ_UNUSED_ARG(hPrevInstance); |
| 69 | |
| 70 | |
| 71 | if (!InitInstance (hInstance, nCmdShow)) |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 72 | return FALSE; |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 73 | |
| 74 | pj_log_set_log_func( &write_log ); |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 75 | param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR; |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 76 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 77 | // Run the test! |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 78 | test_main(); |
| 79 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 80 | 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 Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 89 | DeleteObject(hFixedFont); |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 90 | return msg.wParam; |
| 91 | } |
| 92 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 93 | |
| 94 | #ifdef _CONSOLE |
| 95 | int main() |
| 96 | { |
| 97 | return WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOW); |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 102 | ATOM 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 Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 121 | |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 122 | BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) |
| 123 | { |
| 124 | HWND hWnd; |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 125 | 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 Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 143 | |
| 144 | MyRegisterClass(hInstance, szWindowClass); |
| 145 | |
| 146 | hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE, |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 147 | CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, |
| 148 | CW_USEDEFAULT, NULL, NULL, hInstance, NULL); |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 149 | |
| 150 | if (!hWnd) |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 151 | return FALSE; |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 152 | |
| 153 | ShowWindow(hWnd, nCmdShow); |
| 154 | UpdateWindow(hWnd); |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 155 | |
| 156 | if (hwndLog) { |
| 157 | SendMessage(hwndLog, WM_SETFONT, (WPARAM) hFixedFont, (LPARAM) 0); |
| 158 | ShowWindow(hwndLog, TRUE); |
| 159 | } |
| 160 | |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 161 | return TRUE; |
| 162 | } |
| 163 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 164 | |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 165 | LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 166 | { |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 167 | RECT rt; |
| 168 | DWORD dwStyle; |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 169 | |
| 170 | switch (message) |
| 171 | { |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 172 | 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 Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 197 | } |
| 198 | break; |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame^] | 199 | 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 Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |