main_win32.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 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. #include <pj/string.h>
  21. #include <pj/unicode.h>
  22. #include <pj/sock.h>
  23. #include <pj/log.h>
  24. #define WIN32_LEAN_AND_MEAN
  25. #define NONAMELESSUNION
  26. #include <windows.h>
  27. #include <commctrl.h>
  28. #include <tchar.h>
  29. #define MAX_LOADSTRING 100
  30. #define THIS_FILE "main_win32.c"
  31. #define IDC_HELLO_WINCE 3
  32. #define ID_LOGWINDOW 104
  33. ATOM MyRegisterClass (HINSTANCE, LPTSTR);
  34. BOOL InitInstance (HINSTANCE, int);
  35. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  36. extern int param_log_decor; // in test.c
  37. static HINSTANCE hInst;
  38. static HWND hwndLog;
  39. static HFONT hFixedFont;
  40. static void write_log(int level, const char *data, int len)
  41. {
  42. PJ_DECL_UNICODE_TEMP_BUF(wdata,256);
  43. PJ_UNUSED_ARG(level);
  44. PJ_UNUSED_ARG(len);
  45. SendMessage(hwndLog, EM_REPLACESEL, FALSE,
  46. (LPARAM)PJ_STRING_TO_NATIVE(data,wdata,256));
  47. }
  48. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  49. LPTSTR lpCmdLine, int nCmdShow)
  50. {
  51. MSG msg;
  52. PJ_UNUSED_ARG(lpCmdLine);
  53. PJ_UNUSED_ARG(hPrevInstance);
  54. if (!InitInstance (hInstance, nCmdShow))
  55. return FALSE;
  56. pj_log_set_log_func( &write_log );
  57. param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR;
  58. // Run the test!
  59. test_main();
  60. PJ_LOG(3,(THIS_FILE,""));
  61. PJ_LOG(3,(THIS_FILE,"Press ESC to quit"));
  62. // Message loop, waiting to quit.
  63. while (GetMessage(&msg, NULL, 0, 0)) {
  64. TranslateMessage(&msg);
  65. DispatchMessage(&msg);
  66. }
  67. DeleteObject(hFixedFont);
  68. return msg.wParam;
  69. }
  70. #ifdef _CONSOLE
  71. int main()
  72. {
  73. return WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOW);
  74. }
  75. #endif
  76. ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
  77. {
  78. WNDCLASS wc;
  79. wc.style = CS_HREDRAW | CS_VREDRAW;
  80. wc.lpfnWndProc = (WNDPROC) WndProc;
  81. wc.cbClsExtra = 0;
  82. wc.cbWndExtra = 0;
  83. wc.hInstance = hInstance;
  84. ///wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HELLO_WINCE));
  85. wc.hIcon = NULL;
  86. wc.hCursor = 0;
  87. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  88. wc.lpszMenuName = 0;
  89. wc.lpszClassName = szWindowClass;
  90. return RegisterClass(&wc);
  91. }
  92. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  93. {
  94. HWND hWnd;
  95. TCHAR *szTitle = _T("PJSIP Test");
  96. TCHAR *szWindowClass = _T("PJSIP_TEST");
  97. LOGFONT lf;
  98. memset(&lf, 0, sizeof(lf));
  99. lf.lfHeight = 13;
  100. #if PJ_NATIVE_STRING_IS_UNICODE
  101. wcscpy(lf.lfFaceName, _T("Courier New"));
  102. #else
  103. strcpy(lf.lfFaceName, "Lucida Console");
  104. #endif
  105. hFixedFont = CreateFontIndirect(&lf);
  106. if (!hFixedFont)
  107. return FALSE;
  108. hInst = hInstance;
  109. MyRegisterClass(hInstance, szWindowClass);
  110. hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
  111. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  112. CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  113. if (!hWnd)
  114. return FALSE;
  115. ShowWindow(hWnd, nCmdShow);
  116. UpdateWindow(hWnd);
  117. if (hwndLog) {
  118. SendMessage(hwndLog, WM_SETFONT, (WPARAM) hFixedFont, (LPARAM) 0);
  119. ShowWindow(hwndLog, TRUE);
  120. }
  121. return TRUE;
  122. }
  123. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  124. {
  125. RECT rt;
  126. DWORD dwStyle;
  127. switch (message)
  128. {
  129. case WM_CREATE:
  130. // Create text control.
  131. GetClientRect(hWnd, &rt);
  132. dwStyle = WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
  133. WS_BORDER | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL |
  134. ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_READONLY;
  135. hwndLog = CreateWindow( TEXT("edit"), // class
  136. NULL, // window text
  137. dwStyle, // style
  138. 0, // x-left
  139. 0, // y-top
  140. rt.right-rt.left, // w
  141. rt.bottom-rt.top, // h
  142. hWnd, // parent
  143. (HMENU)ID_LOGWINDOW,// id
  144. hInst, // instance
  145. NULL); // NULL for control.
  146. break;
  147. case WM_ACTIVATE:
  148. if (LOWORD(wParam) == WA_INACTIVE)
  149. DestroyWindow(hWnd);
  150. break;
  151. case WM_CHAR:
  152. if (wParam == 27) {
  153. DestroyWindow(hWnd);
  154. }
  155. break;
  156. case WM_CLOSE:
  157. DestroyWindow(hWnd);
  158. break;
  159. case WM_DESTROY:
  160. PostQuitMessage(0);
  161. break;
  162. default:
  163. return DefWindowProc(hWnd, message, wParam, lParam);
  164. }
  165. return 0;
  166. }