aswang
1.0
|
00001 /* 00002 Copyright 2003 Joseph Alvis 00003 00004 This file is part of Aswang. 00005 00006 Aswang is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 Aswang is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with Aswang. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "window.h" 00021 00022 using namespace aswang; 00023 00024 window::wmap_type window::wmap; 00025 HINSTANCE window::hInstance; 00026 00027 void window::Add() { 00028 wmap[hWnd] = this; 00029 } 00030 00031 void window::Erase() { 00032 wmap_type::iterator it = wmap.find(hWnd); 00033 if(it != wmap.end()) 00034 it=wmap.erase(it); 00035 } 00036 00037 void window::Create(int x,int y,int width,int height) { 00038 hWnd=CreateWindow(GetClassName(),TEXT(""),style,x,y,width,height,(mParent!=NULL?mParent->hWnd:NULL),NULL,hInstance,NULL); 00039 } 00040 00041 void window::Show(int showCommand) { 00042 ShowWindow(hWnd,showCommand); 00043 } 00044 00045 void window::Transparent(bool t) const { 00046 if(hWnd!=NULL) { 00047 UINT style = GetWindowLong(hWnd,GWL_EXSTYLE); 00048 if(t) 00049 style &= WS_EX_TRANSPARENT; 00050 else 00051 style |= ~WS_EX_TRANSPARENT; 00052 SetWindowLong(hWnd,GWL_EXSTYLE,style); 00053 } 00054 } 00055 00056 window *window::GetWindow(HWND h) { 00057 wmap_type::iterator it = wmap.find(h); 00058 if(it != wmap.end()) 00059 return it->second; 00060 return NULL; 00061 } 00062 00063 AswangStr window::GetText() const { 00064 AswangStr str; 00065 if(hWnd) { 00066 unsigned long len = GetWindowTextLength(hWnd)+1; 00067 _TCHAR *s=new _TCHAR[len]; 00068 GetWindowText(hWnd,s,len); 00069 str=s; 00070 delete[] s; 00071 } 00072 return str; 00073 } 00074 00075 void window::SetText(AswangStr s) const { 00076 if(hWnd) { 00077 SetWindowText(hWnd,s.c_str()); 00078 } 00079 } 00080 00081 long window::GetWindowX() const { 00082 if(hWnd != NULL) { 00083 RECT r; 00084 GetWindowRect(hWnd,&r); 00085 return r.left; 00086 } 00087 return 0; 00088 } 00089 00090 long window::GetWindowY() const { 00091 if(hWnd != NULL) { 00092 RECT r; 00093 GetWindowRect(hWnd,&r); 00094 return r.top; 00095 } 00096 return 0; 00097 } 00098 00099 long window::GetWindowWidth() const { 00100 if(hWnd != NULL) { 00101 RECT r; 00102 GetWindowRect(hWnd,&r); 00103 return (r.right-r.left); 00104 } 00105 return 0; 00106 } 00107 00108 long window::GetWindowHeight() const { 00109 if(hWnd != NULL) { 00110 RECT r; 00111 GetWindowRect(hWnd,&r); 00112 return (r.bottom-r.top); 00113 } 00114 return 0; 00115 } 00116 00117 long window::GetClientWidth() const { 00118 if(hWnd != NULL) { 00119 RECT r; 00120 GetClientRect(hWnd,&r); 00121 return (r.right-r.left); 00122 } 00123 return 0; 00124 } 00125 00126 long window::GetClientHeight() const { 00127 if(hWnd != NULL) { 00128 RECT r; 00129 GetClientRect(hWnd,&r); 00130 return (r.bottom-r.top); 00131 } 00132 return 0; 00133 } 00134 00135 void window::Center() const { 00136 if(hWnd) { 00137 HWND ph = ::GetParent(hWnd); 00138 if(ph==NULL) 00139 ph=GetDesktopWindow(); 00140 RECT pr,tr; 00141 GetClientRect(ph,&pr); 00142 GetWindowRect(hWnd,&tr); 00143 SetWindowPos(hWnd,NULL, 00144 (((pr.right-pr.left)/2)-((tr.right-tr.left)/2)), 00145 (((pr.bottom-pr.top)/2)-((tr.bottom-tr.top)/2)), 00146 0,0,SWP_NOSIZE|SWP_NOZORDER); 00147 } 00148 } 00149 00150 void window::SetAlignment(Alignment a) { 00151 if(hWnd!=NULL) { 00152 long style = GetWindowLong(hWnd,GWL_STYLE); 00153 style &= ~(ES_LEFT|ES_RIGHT|ES_CENTER); 00154 switch(a) { 00155 case ALIGN_LEFT: 00156 style |= ES_LEFT; 00157 break; 00158 case ALIGN_RIGHT: 00159 style |= ES_RIGHT; 00160 break; 00161 case ALIGN_CENTER: 00162 style |= ES_CENTER; 00163 break; 00164 } 00165 SetWindowLong(hWnd,GWL_STYLE,style); 00166 } 00167 } 00168 00169 void window::Invalidate() { 00170 if(hWnd!=NULL) { 00171 RECT r; 00172 GetClientRect(hWnd,&r); 00173 InvalidateRect(hWnd,&r,true); 00174 } 00175 } 00176 00177 void window::Update() { 00178 if(hWnd!=NULL) { 00179 UpdateWindow(hWnd); 00180 } 00181 } 00182 00183 void window::Move(int x,int y) { 00184 if(hWnd!=NULL) { 00185 SetWindowPos(hWnd,NULL,x,y,0,0,SWP_NOZORDER|SWP_NOSIZE); 00186 } 00187 } 00188 00189 void window::Resize(int width,int height) { 00190 if(hWnd!=NULL) { 00191 SetWindowPos(hWnd,NULL,0,0,width,height,SWP_NOZORDER|SWP_NOMOVE); 00192 } 00193 } 00194 00195 void window::SetStyle(unsigned long s) { 00196 style=s; 00197 if(hWnd!=NULL) 00198 SetWindowLong(hWnd,GWL_STYLE,s); 00199 } 00200 00201 unsigned long window::GetStyle() { 00202 if(hWnd!=NULL) 00203 style = GetWindowLong(hWnd,GWL_STYLE); 00204 return style; 00205 } 00206 00207 LRESULT CALLBACK window::WProc(HWND h,UINT msg,WPARAM wParam,LPARAM lParam) { 00208 window *w = GetWindow(h); 00209 if(w != NULL) { 00210 callbackMap_type::iterator cit; 00211 if(!w->callbackMap.empty() && (cit=w->callbackMap.find(msg)) != w->callbackMap.end()) { 00212 window *p = (cit->second.first?GetWindow(cit->second.first):w); 00213 if(p) 00214 return (p->*(cit->second.second))(w,msg,lParam,wParam); 00215 else 00216 w->RemoveCallback(msg); 00217 } 00218 switch(msg) { 00219 case WM_CAPTURECHANGED: 00220 return w->OnMouseOut((HWND)lParam); 00221 case WM_CHAR: 00222 return w->OnChar((TCHAR)wParam,(LONG)lParam); 00223 case WM_CLOSE: 00224 return w->OnClose(); 00225 case WM_COMMAND: 00226 return w->OnCommand((HWND)lParam,HIWORD(wParam),LOWORD(wParam)); 00227 case WM_CTLCOLORBTN: 00228 return w->OnButtonColor((HDC)wParam,(HWND)lParam); 00229 case WM_CTLCOLORDLG: 00230 return w->OnDialogColor((HDC)wParam,(HWND)lParam); 00231 case WM_CTLCOLORSTATIC: 00232 return w->OnStaticColor((HDC)wParam,(HWND)lParam); 00233 case WM_DESTROY: 00234 return w->OnDestroy(h); 00235 case WM_DRAWITEM: 00236 return w->OnDrawItem((LONG)wParam,(DRAWITEMSTRUCT *)lParam); 00237 case WM_ERASEBKGND: 00238 return w->OnEraseBG((HDC)wParam); 00239 case WM_GETDLGCODE: 00240 return w->OnGetDlgCode((LONG)wParam,(MSG *)lParam); 00241 case WM_INITDIALOG: 00242 return w->OnInitDialog((HWND)wParam,(LONG)lParam); 00243 case WM_KEYDOWN: 00244 return w->OnKeyDown((LONG)wParam,(LONG)lParam); 00245 case WM_KEYUP: 00246 return w->OnKeyUp((LONG)wParam,(LONG)lParam); 00247 case WM_LBUTTONDBLCLK: 00248 return w->OnLDClick((LONG)wParam,MAKEPOINTS(lParam)); 00249 case WM_LBUTTONDOWN: 00250 return w->OnLDown((LONG)wParam,MAKEPOINTS(lParam)); 00251 case WM_LBUTTONUP: 00252 return w->OnLUp((LONG)wParam,MAKEPOINTS(lParam)); 00253 case WM_MEASUREITEM: 00254 return w->OnMeasureItem((LONG)wParam,(LPMEASUREITEMSTRUCT)lParam); 00255 case WM_MOUSEMOVE: 00256 return w->OnMouseOver((LONG)wParam,MAKEPOINTS(lParam)); 00257 case WM_MOVE: 00258 return w->OnMove((LONG)wParam,(LONG)lParam); 00259 case WM_NCPAINT: 00260 return w->OnNCPaint((HRGN)wParam); 00261 case WM_NCHITTEST: 00262 return w->OnNCHitTest(MAKEPOINTS(lParam)); 00263 case WM_NOTIFY: 00264 return w->OnNotify((LONG)wParam,(NMHDR *)lParam); 00265 case WM_PAINT: 00266 return w->OnPaint(); 00267 case WM_PARENTNOTIFY: 00268 if(LOWORD(wParam) == WM_CREATE || LOWORD(wParam) == WM_DESTROY) 00269 return w->OnParentNotifyCreate(LOWORD(wParam),HIWORD(wParam),(HWND)lParam); 00270 else 00271 return w->OnParentNotifyClick(LOWORD(wParam),LOWORD(lParam),HIWORD(lParam)); 00272 case WM_RBUTTONDBLCLK: 00273 return w->OnRDClick((LONG)wParam,MAKEPOINTS(lParam)); 00274 case WM_RBUTTONDOWN: 00275 return w->OnRDown((LONG)wParam,MAKEPOINTS(lParam)); 00276 case WM_RBUTTONUP: 00277 return w->OnRUp((LONG)wParam,MAKEPOINTS(lParam)); 00278 case WM_SHOWWINDOW: 00279 return w->OnShow((wParam==1),(LONG)lParam); 00280 case WM_SIZE: 00281 return w->OnSize((LONG)wParam,LOWORD(lParam),HIWORD(lParam)); 00282 case WM_SIZING: 00283 return w->OnSizing((LONG)wParam,(RECT *)lParam); 00284 case WM_TIMER: 00285 return w->OnTimer((LONG)wParam,(TIMERPROC)lParam); 00286 case WM_WINDOWPOSCHANGED: 00287 return w->OnPosChanged((WINDOWPOS *)lParam); 00288 default: 00289 return w->OnFail(msg,wParam,lParam); 00290 } 00291 } 00292 else if(msg == WM_CREATE) { 00293 CREATESTRUCT *cs = (LPCREATESTRUCT) lParam; 00294 if(cs->lpCreateParams != NULL) { 00295 w=(window *)cs->lpCreateParams; 00296 w->hWnd=h; 00297 w->Add(); 00298 callbackMap_type::iterator cit; 00299 if(!w->callbackMap.empty() && (cit=w->callbackMap.find(msg)) != w->callbackMap.end()) { 00300 window *p = (cit->second.first?GetWindow(cit->second.first):w); 00301 if(p) 00302 return (p->*(cit->second.second))(w,msg,lParam,wParam); 00303 else 00304 w->RemoveCallback(msg); 00305 } 00306 } 00307 } 00308 else if(msg == WM_INITDIALOG && lParam!=NULL) { 00309 w=(window *)lParam; 00310 if(w) { 00311 w->hWnd=h; 00312 w->Add(); 00313 callbackMap_type::iterator cit; 00314 if(!w->callbackMap.empty() && (cit=w->callbackMap.find(msg)) != w->callbackMap.end()) { 00315 window *p = (cit->second.first?GetWindow(cit->second.first):w); 00316 if(p) 00317 return (p->*(cit->second.second))(w,msg,lParam,wParam); 00318 else 00319 w->RemoveCallback(msg); 00320 } 00321 return w->OnInitDialog((HWND)wParam,(LONG)lParam); 00322 } 00323 } 00324 return DefWindowProc(h,msg,wParam,lParam); 00325 } 00326 00327 int window::Loop() { 00328 MSG msg; 00329 while(GetMessage(&msg,NULL,0,0) > 0) { 00330 TranslateMessage(&msg); 00331 DispatchMessage(&msg); 00332 } 00333 return (int)msg.wParam; 00334 } 00335 00336 LRESULT window::OnClose() { 00337 if(mParent == NULL) { 00338 PostQuitMessage(0); 00339 return 0; 00340 } 00341 return OnFail(WM_CLOSE,0,0); 00342 } 00343 00344 LRESULT window::OnPaint() { 00345 return OnFail(WM_PAINT,0,0); 00346 } 00347 00348 LRESULT window::OnLDown(unsigned int flags,POINTS p) { 00349 return OnFail(WM_LBUTTONDOWN,flags,(LPARAM)&p); 00350 } 00351 00352 LRESULT window::OnLUp(unsigned int flags,POINTS p) { 00353 return OnFail(WM_LBUTTONUP,flags,(LPARAM)&p); 00354 } 00355 00356 LRESULT window::OnLDClick(unsigned int flags,POINTS p) { 00357 return OnFail(WM_LBUTTONDBLCLK,flags,(LPARAM)&p); 00358 } 00359 00360 LRESULT window::OnRDown(unsigned int flags,POINTS p) { 00361 return OnFail(WM_RBUTTONDOWN,flags,(LPARAM)&p); 00362 } 00363 00364 LRESULT window::OnRUp(unsigned int flags,POINTS p) { 00365 return OnFail(WM_RBUTTONUP,flags,(LPARAM)&p); 00366 } 00367 00368 LRESULT window::OnRDClick(unsigned int flags,POINTS p) { 00369 return OnFail(WM_RBUTTONDBLCLK,flags,(LPARAM)&p); 00370 } 00371 00372 LRESULT window::OnMouseOut(HWND NewWindow) { 00373 return OnFail(WM_CAPTURECHANGED,0,(LPARAM)NewWindow); 00374 } 00375 00376 LRESULT window::OnMouseOver(unsigned int flags,POINTS p) { 00377 return OnFail(WM_MOUSEMOVE,flags,(LPARAM)&p); 00378 } 00379 00380 LRESULT window::OnCommand(HWND win,unsigned short Notify,unsigned short wID) { 00381 WPARAM wParam=MAKELONG(wID,Notify); 00382 return OnFail(WM_COMMAND,wParam,(LPARAM)win); 00383 } 00384 00385 LRESULT window::OnButtonColor(HDC dc,HWND btn) { 00386 if(hWnd != btn) { 00387 window *w = GetWindow(btn); 00388 if(w != NULL) 00389 return w->OnButtonColor(dc,btn); 00390 } 00391 return OnFail(WM_CTLCOLORBTN,(WPARAM)dc,(LPARAM)btn); 00392 } 00393 00402 LRESULT window::OnStaticColor(HDC dc,HWND sttc) { 00403 if(hWnd != sttc) { 00404 window *w = GetWindow(sttc); 00405 if(w != NULL) 00406 return w->OnStaticColor(dc,sttc); 00407 } 00408 return OnFail(WM_CTLCOLORSTATIC,(WPARAM)dc,(LPARAM)sttc); 00409 } 00410 00411 LRESULT window::OnDialogColor(HDC dc,HWND dlg) { 00412 if(hWnd != dlg) { 00413 window *w = GetWindow(dlg); 00414 if(w != NULL) 00415 return w->OnDialogColor(dc,dlg); 00416 } 00417 return OnFail(WM_CTLCOLORDLG,(WPARAM)dc,(LPARAM)dlg); 00418 } 00419 00420 LRESULT window::OnShow(bool show,long Status) { 00421 return OnFail(WM_SHOWWINDOW,show,Status); 00422 } 00423 00424 LRESULT window::OnGetDlgCode(long VirtKey,MSG *m) { 00425 return OnFail(WM_GETDLGCODE,VirtKey,(LPARAM)m); 00426 } 00427 00428 LRESULT window::OnKeyUp(long VirtKey,long flags) { 00429 return OnFail(WM_KEYUP,VirtKey,flags); 00430 } 00431 00432 LRESULT window::OnKeyDown(long VirtKey,long flags) { 00433 return OnFail(WM_KEYDOWN,VirtKey,flags); 00434 } 00435 00436 LRESULT window::OnChar(TCHAR c,long flags) { 00437 return OnFail(WM_CHAR,c,flags); 00438 } 00439 00440 LRESULT window::OnSize(unsigned long flag,unsigned short NewWidth,unsigned short NewHeight) { 00441 return OnFail(WM_SIZE,flag,MAKELONG(NewWidth,NewHeight)); 00442 } 00443 00444 LRESULT window::OnSizing(unsigned long side,RECT *r) { 00445 return OnFail(WM_SIZING,side,(LPARAM)r); 00446 } 00447 00448 LRESULT window::OnNotify(unsigned long ID,NMHDR *nmh) { 00449 return OnFail(WM_NOTIFY,ID,(LPARAM)nmh); 00450 } 00451 00452 LRESULT window::OnInitDialog(HWND win,unsigned long param) { 00453 return OnFail(WM_INITDIALOG,(LPARAM)win,param); 00454 } 00455 00456 LRESULT window::OnDrawItem(unsigned long ID,DRAWITEMSTRUCT *dis) { 00457 return OnFail(WM_DRAWITEM,ID,(LPARAM)dis); 00458 } 00459 00460 LRESULT window::OnMove(unsigned long NewX,unsigned long NewY) { 00461 return OnFail(WM_MOVE,NewX,NewY); 00462 } 00463 00464 LRESULT window::OnNCPaint(HRGN region) { 00465 return OnFail(WM_NCPAINT,(LPARAM)region,0); 00466 } 00467 00468 LRESULT window::OnPosChanged(WINDOWPOS *wpos) { 00469 return OnFail(WM_WINDOWPOSCHANGED,0,(LPARAM)wpos); 00470 } 00471 00472 LRESULT window::OnEraseBG(HDC dc) { 00473 if((bool)bg) { 00474 RECT r; 00475 GetClientRect(hWnd,&r); 00476 FillRect(dc,&r,bg.GetBrush()); 00477 return 1; 00478 } 00479 return OnFail(WM_ERASEBKGND,(LPARAM)dc,0); 00480 } 00481 00482 LRESULT window::OnNCHitTest(POINTS pos) { 00483 return OnFail(WM_NCHITTEST,0,MAKELONG(pos.x,pos.y)); 00484 } 00485 00486 LRESULT window::OnTimer(unsigned long id,TIMERPROC tproc) { 00487 return OnFail(WM_TIMER,id,(LPARAM)tproc); 00488 } 00489 00490 LRESULT window::OnDestroy(HWND win) { 00491 return OnFail(WM_DESTROY,NULL,NULL); 00492 } 00493 00494 LRESULT window::OnParentNotifyCreate(unsigned short event,unsigned short wID,HWND win) { 00495 return OnFail(WM_PARENTNOTIFY,MAKEWPARAM(event,wID),(LPARAM)win); 00496 } 00497 00498 LRESULT window::OnParentNotifyClick(unsigned short event,short x,short y) { 00499 return OnFail(WM_PARENTNOTIFY,MAKEWPARAM(event,0),MAKELPARAM(x,y)); 00500 } 00501 00502 LRESULT window::OnMeasureItem(long ctlID,MEASUREITEMSTRUCT *mis) { 00503 return OnFail(WM_MEASUREITEM,ctlID,(LPARAM)mis); 00504 } 00505 00506 LRESULT window::OnFail(UINT msg,WPARAM wParam,LPARAM lParam) { 00507 return DefWindowProc(hWnd,msg,wParam,lParam); 00508 }