00001 #ifndef BASIC_WINDOW_IMPLEMENTATION_FILE
00002 #define BASIC_WINDOW_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "basic_window.h"
00019 #include "basic_window.rh"
00020
00021 #include <basis/convert_utf.h>
00022 #include <basis/istring.h>
00023 #include <basis/log_base.h>
00024
00025 #undef LOG
00026 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00027
00028 basic_window::basic_window(application_instance instance, const istring &window_title,
00029 const istring &window_class)
00030 : _instance(instance),
00031 _window_title(new istring(window_title)),
00032 _window_class(new istring(window_class))
00033 {
00034 }
00035
00036 basic_window::~basic_window()
00037 {
00038 WHACK(_window_title);
00039 WHACK(_window_class);
00040 }
00041
00042 ATOM basic_window::register_class(u_int *icon_id, u_int *menu_id,
00043 u_int *small_icon_id)
00044 {
00045 if (!small_icon_id) small_icon_id = icon_id;
00046
00047 WNDCLASSEX wcex;
00048
00049 wcex.cbSize = sizeof(WNDCLASSEX);
00050
00051 wcex.style = CS_HREDRAW | CS_VREDRAW;
00052 wcex.lpfnWndProc = (WNDPROC)WndProc;
00053 wcex.cbClsExtra = 0;
00054 wcex.cbWndExtra = 0;
00055 wcex.hInstance = _instance;
00056 wcex.hIcon = LoadIcon(_instance, (LPCTSTR)icon_id);
00057 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
00058 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
00059 wcex.lpszMenuName = (LPCTSTR)menu_id;
00060 to_unicode_persist(temp_class, *_window_class);
00061 wcex.lpszClassName = temp_class;
00062 wcex.hIconSm = LoadIcon(_instance, (LPCTSTR)small_icon_id);
00063
00064 return RegisterClassEx(&wcex);
00065 }
00066
00067 bool basic_window::create(int nCmdShow)
00068 {
00069 FUNCDEF("create");
00070 HWND hWnd = CreateWindow(to_unicode_temp(*_window_class),
00071 to_unicode_temp(*_window_title), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
00072 0, CW_USEDEFAULT, 0, NULL, NULL, _instance, NULL);
00073
00074 if (!hWnd) {
00075 LOG(istring("failed to create the window: system error is ")
00076 + portable::system_error_text(portable::system_error()));
00077 return false;
00078 }
00079
00080 ShowWindow(hWnd, nCmdShow);
00081
00082
00083 UpdateWindow(hWnd);
00084
00085
00086 return true;
00087 }
00088
00089
00090
00091
00092 LRESULT CALLBACK basic_window::WndProc(HWND hWnd, UINT message,
00093 WPARAM wParam, LPARAM lParam)
00094 {
00095 int wmId, wmEvent;
00096 PAINTSTRUCT ps;
00097 HDC hdc;
00098
00099 switch (message)
00100 {
00101 case WM_COMMAND:
00102 wmId = LOWORD(wParam);
00103 wmEvent = HIWORD(wParam);
00104
00105 switch (wmId)
00106 {
00107 case ID_APP_EXIT:
00108
00110
00111 PostMessage(hWnd, WM_CLOSE, NIL, NIL);
00112
00113 break;
00114 default:
00115 return DefWindowProc(hWnd, message, wParam, lParam);
00116 }
00117 break;
00118 case WM_PAINT:
00119 hdc = BeginPaint(hWnd, &ps);
00120
00121
00122
00123
00124 EndPaint(hWnd, &ps);
00125 break;
00126 case WM_DESTROY:
00127 PostQuitMessage(0);
00128 break;
00129 default:
00130 return DefWindowProc(hWnd, message, wParam, lParam);
00131 }
00132 return 0;
00133 }
00134
00135
00136 #endif //BASIC_WINDOW_IMPLEMENTATION_FILE
00137