00001
00002
00003 #include "vgui_win32_window.h"
00004 #include "vgui_win32_utils.h"
00005 #include <vgui/vgui.h>
00006 #include <vgui/vgui_command.h>
00007
00008 BEGIN_MESSAGE_MAP(vgui_win32_window, vgui_win32_cmdtarget)
00009 END_MESSAGE_MAP()
00010
00011 LRESULT vgui_win32_window::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00012 {
00013 if ( OnCmdMsg(message, wParam, lParam) )
00014 return 1;
00015 else
00016 return DefWndProc(hwnd, message, wParam, lParam);
00017 }
00018
00019 LRESULT vgui_win32_window::DefWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00020 {
00021
00022 return 0;
00023 }
00024
00025 vgui_win32_window::vgui_win32_window(HINSTANCE hInst, const char *appName,
00026 int width, int height, vgui_menu const &menubar, char const *title)
00027 : cx_(width), cy_(height), first_show(true)
00028 {
00029 initWindow(hInst, appName, width, height, title);
00030
00031
00032 hMenu_ = vgui_win32_utils::instance()->vgui_menu_to_win32ex(menubar, callbacks, &hAccel_);
00033 SetMenu(hwnd_, hMenu_);
00034 }
00035
00036 vgui_win32_window::vgui_win32_window(HINSTANCE hInst, const char *appName,
00037 int width, int height, char const *title)
00038 : cx_(width), cy_(height), first_show(true)
00039 {
00040 initWindow(hInst, appName, width, height, title);
00041 }
00042
00043 void vgui_win32_window::initWindow(HINSTANCE hInst, const char *appName,
00044 int width, int height, char const *title)
00045 {
00046 hMenu_ = NULL;
00047 hAccel_ = NULL;
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 int cxSizeFrame = GetSystemMetrics(SM_CXSIZEFRAME);
00058 int cySizeFrame = GetSystemMetrics(SM_CYSIZEFRAME);
00059 int cyCaption = GetSystemMetrics(SM_CYCAPTION);
00060 int cyMenu = GetSystemMetrics(SM_CYMENU);
00061 int cxScreenSize = GetSystemMetrics(SM_CXFULLSCREEN);
00062 int cyScreenSize = GetSystemMetrics(SM_CYFULLSCREEN);
00063 int cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
00064 int cyHScroll = GetSystemMetrics(SM_CYHSCROLL);
00065
00066
00067 int win_width = width+2*cxSizeFrame+3;
00068 int win_height = height+cyCaption+cyMenu+2*cySizeFrame;
00069
00070
00071 if ( win_width > cxScreenSize ) {
00072 win_width = cxScreenSize;
00073 cx_ = cxScreenSize-(2*cxSizeFrame);
00074 }
00075 if ( win_height > cyScreenSize ) {
00076 win_height = cyScreenSize;
00077 cy_ = cyScreenSize-(cyCaption+cyMenu+2*cySizeFrame);
00078 }
00079
00080 hwnd_ = CreateWindow(appName,
00081 title,
00082 WS_OVERLAPPEDWINDOW,
00083 CW_USEDEFAULT,
00084 CW_USEDEFAULT,
00085 win_width,
00086 win_height,
00087 NULL,
00088 NULL,
00089 hInst,
00090 NULL
00091 );
00092 adaptor_ = new vgui_win32_adaptor(hwnd_, this);
00093 statusbar_ = new vgui_win32_statusbar(hwnd_);
00094 vgui::out.rdbuf(statusbar_->statusbuf);
00095
00096
00097
00098 ShowWindow(hwnd_, SW_SHOWNORMAL);
00099 UpdateWindow(hwnd_);
00100 }
00101
00102 vgui_win32_window::~vgui_win32_window()
00103 {
00104 delete statusbar_;
00105 statusbar_ = 0;
00106
00107 DestroyMenu(hMenu_);
00108 hMenu_ = NULL;
00109 callbacks.clear();
00110
00111 DestroyAcceleratorTable(hAccel_);
00112 }
00113
00114
00115 void vgui_win32_window::show()
00116 {
00117 ShowWindow(hwnd_, SW_SHOW);
00118 UpdateWindow(hwnd_);
00119
00120
00121
00122
00123
00124
00125
00126 if ( first_show ) {
00127 PostMessage(hwnd_, WM_SIZE, 0, MAKELPARAM((WORD)(cx_+3),(WORD)(cy_) ));
00128 PostMessage(hwnd_, WM_PAINT, 0, 0);
00129 first_show = false;
00130 }
00131 }
00132
00133
00134
00135 void vgui_win32_window::reshape(unsigned w, unsigned h)
00136 {
00137 cx_ = w; cy_ = h;
00138 MoveWindow(hwnd_, wx_, wy_, cx_, cy_, TRUE);
00139 }
00140
00141
00142
00143 void vgui_win32_window::reposition(int x, int y)
00144 {
00145 wx_ = x; wy_ = y;
00146 MoveWindow(hwnd_, wx_, wy_, cx_, cy_, TRUE);
00147 }
00148
00149
00150
00151 void vgui_win32_window::set_menubar(vgui_menu const &menu)
00152 {
00153
00154 if ( hMenu_ ) {
00155 DestroyMenu(hMenu_);
00156 hMenu_ = NULL;
00157 }
00158 callbacks.clear();
00159
00160
00161 hMenu_ = vgui_win32_utils::instance()->vgui_menu_to_win32ex(menu, callbacks, &hAccel_);
00162 SetMenu(hwnd_, hMenu_);
00163
00164 DrawMenuBar(hwnd_);
00165 }
00166
00167
00168
00169 void vgui_win32_window::set_statusbar(bool on)
00170 {
00171 if ( statusbar_ )
00172 statusbar_->setVisible(on);
00173 }
00174
00175
00176
00177 void vgui_win32_window::enable_hscrollbar(bool b)
00178 {
00179 EnableScrollBar(hwnd_, SB_HORZ, b ? ESB_ENABLE_BOTH : ESB_DISABLE_BOTH);
00180 ShowScrollBar(hwnd_, SB_HORZ, b);
00181 }
00182
00183
00184
00185 void vgui_win32_window::enable_vscrollbar(bool b)
00186 {
00187 EnableScrollBar(hwnd_, SB_VERT, b ? ESB_ENABLE_BOTH : ESB_DISABLE_BOTH);
00188 ShowScrollBar(hwnd_, SB_VERT, b);
00189 }
00190
00191
00192 int vgui_win32_window::set_hscrollbar(int pos)
00193 {
00194 SCROLLINFO si;
00195 int oldPos;
00196
00197 si.cbSize = sizeof(si);
00198 si.fMask = SIF_POS;
00199
00200 GetScrollInfo(hwnd_, SB_HORZ, &si);
00201 oldPos = si.nPos;
00202
00203 si.nPos = pos;
00204 SetScrollInfo(hwnd_, SB_HORZ, &si, TRUE);
00205
00206 return oldPos;
00207 }
00208
00209
00210 int vgui_win32_window::set_vscrollbar(int pos)
00211 {
00212 SCROLLINFO si;
00213 int oldPos;
00214
00215 si.cbSize = sizeof(si);
00216 si.fMask = SIF_POS;
00217
00218 GetScrollInfo(hwnd_, SB_VERT, &si);
00219 oldPos = si.nPos;
00220
00221 si.nPos = pos;
00222 SetScrollInfo(hwnd_, SB_VERT, &si, TRUE);
00223
00224 return oldPos;
00225 }
00226
00227 void vgui_win32_window::menu_dispatcher(int menuId)
00228 {
00229 int item_count = callbacks.size();
00230
00231
00232
00233 if ( menuId >= MENU_ID_START && menuId < MENU_ID_START+item_count )
00234 callbacks[menuId-MENU_ID_START]->execute();
00235 }
00236
00237
00238 BOOL vgui_win32_window::OnCmdMsg(UINT message, WPARAM wParam, LPARAM lParam)
00239 {
00240 if ( adaptor_ )
00241 adaptor_->OnCmdMsg(message, wParam, lParam);
00242
00243 switch (message) {
00244 case WM_SIZE:
00245 cx_ = LOWORD(lParam);
00246 cy_ = HIWORD(lParam);
00247
00248 SendMessage(statusbar_->getWindowHandle(), WM_SIZE, wParam, lParam);
00249 break;
00250
00251 case WM_COMMAND:
00252 menu_dispatcher(LOWORD(wParam));
00253 return TRUE;
00254
00255 case WM_CLOSE:
00256 delete adaptor_;
00257 adaptor_ = 0;
00258 break;
00259
00260
00261
00262
00263 }
00264
00265 return FALSE;
00266 }
00267