src/widget.cpp

Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006 by Kirill Kolodyazhniy.
00003 * See the file "license.terms" for information on usage and redistribution
00004 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00005 */
00006 
00007 #include "stdinc.h"
00008 #include "icon.h"
00009 #include "eventhandler.h"
00010 #include "widgets.h"
00011 #include "menu.h"
00012 #include "widget.h"
00013 #include "exception.h"
00014 namespace MWidgets
00015 {
00016         /*******************************************************************************/
00017 
00018         Widget::~Widget()
00019         {
00020                 if(m_hWnd!=NULL)
00021                 {
00022                         DestroyWindow(m_hWnd);
00023                         Widgets::Instance()->DelWidget();
00024                 }
00025                 if(m_pOnLButtonDownHandler!=NULL)
00026                         delete m_pOnLButtonDownHandler;
00027                 if(m_pOnRButtonDownHandler!=NULL)
00028                         delete m_pOnRButtonDownHandler;
00029                 if(m_pOnCreateHandler!=NULL)
00030                         delete m_pOnCreateHandler;
00031 
00032                 std::map<UINT,BaseEventHandler*>::iterator i;
00033                 for(i=m_mapHotKeyHendles.begin();i!=m_mapHotKeyHendles.end();i++)
00034                 {
00035                         if(i->second!=NULL)
00036                                 delete i->second;
00037                 }
00038         };
00039         /*******************************************************************************/
00040 
00041         Widget* Widget::GetObjectFromHwnd(HWND hWnd)
00042         {
00043                 return reinterpret_cast<Widget*>(GetWindowLong(hWnd, GWL_USERDATA));
00044         }
00045         /*******************************************************************************/
00046 
00047         LRESULT CALLBACK Widget::WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00048         {
00049                 Widget* pWidget=NULL;
00050                 if(message==WM_NCCREATE)
00051                         SetWindowLong(hWnd, GWL_USERDATA,reinterpret_cast<LONG>((LPCREATESTRUCT(lParam))->lpCreateParams));
00052                 pWidget=GetObjectFromHwnd(hWnd);
00053                 if(pWidget)
00054                         return pWidget->OnMessage(hWnd, message, wParam, lParam);
00055                 else
00056                         return DefWindowProc(hWnd, message, wParam, lParam);
00057         };
00058         /*******************************************************************************/
00059         LRESULT Widget::OnMessage (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00060         {
00061                 RECT rc;
00062                 int xPos;
00063                 int yPos;
00064                 Widget *pWidget;
00065                 HWND tempHwnd;
00066                 switch(message)
00067                 {
00068                         case WM_WINDOWPOSCHANGED:
00069                                 tempHwnd=FindWindowEx(hWnd,NULL,STATUSCLASSNAME,NULL);
00070                                 if(tempHwnd)
00071                                         SetWindowPos(tempHwnd,HWND_TOP,0,0,0,0,SWP_SHOWWINDOW);
00072                                 break;
00073                         case WM_SIZE:
00074                                 GetClientRect(hWnd,&rc);
00075                                 OnSize(rc.left,rc.top,rc.right,rc.bottom);
00076                                 break;
00077                         case WM_HOTKEY:
00078                                 ProcessHotKey((WORD)wParam);
00079                                 return 0;
00080                         case WM_LBUTTONDOWN:
00081                                 xPos = GET_X_LPARAM(lParam);
00082                                 yPos = GET_Y_LPARAM(lParam);
00083                                 OnLeftButtonDown(xPos,yPos,(DWORD)wParam);
00084                                 break;
00085                         case WM_RBUTTONDOWN:
00086                                 xPos = GET_X_LPARAM(lParam);
00087                                 yPos = GET_Y_LPARAM(lParam);
00088                                 OnRightButtonDown(xPos,yPos,(DWORD)wParam);
00089                                 break;
00090                         case WM_CREATE:
00091                                 m_hWnd=hWnd;
00092                                 OnCreate();
00093                                 break;
00094                         case WM_CLOSE:
00095                                 DestroyWindow(m_hWnd);
00096                                 return 0;
00097                         case WM_NCDESTROY:
00098                                 Widgets::Instance()->DelWidget();
00099                                 m_hWnd=NULL;
00100                                 return 0;
00101                         case WM_SYSCOMMAND:
00102                         case WM_COMMAND:
00103                                 if(lParam!=0)//control notification
00104                                 {
00105                                         pWidget=GetObjectFromHwnd((HWND)lParam);
00106                                         if(pWidget)
00107                                                 pWidget->OnCommand(HIWORD(wParam),LOWORD(wParam));
00108                                 }
00109                                 else//menu or accelerator notification
00110                                 {
00111                                         switch(HIWORD(wParam))
00112                                         {
00113                                                 case 1://accelerator
00114                                                         ProcessAccelerators(LOWORD(wParam));
00115                                                         break;
00116                                                 case 0://menu
00117                                                         ProcessMenu(LOWORD(wParam));
00118                                                         break;
00119                                         };
00120                                 }
00121                                 break;
00122                 }
00123                 if(m_prevWndProc)
00124                         return CallWindowProc(m_prevWndProc,hWnd,message,wParam,lParam);
00125                 else
00126                         return DefWindowProc(hWnd, message, wParam, lParam);
00127         };
00128         /*******************************************************************************/
00129         void Widget::OnCommand( WORD ,WORD  )
00130         {
00131     }
00132         /*******************************************************************************/
00133         Widget::Widget()
00134         {
00135                 m_hWnd=NULL;
00136                 m_prevWndProc=NULL;
00137                 m_hContextMenu=NULL;
00138                 m_pOnLButtonDownHandler=NULL;
00139                 m_pOnRButtonDownHandler=NULL;
00140                 m_pOnCreateHandler=NULL;
00141                 m_hInstance=NULL;
00142                 m_pOnSizeHandler=NULL;
00143         }
00144         /*******************************************************************************/
00145 
00146         void Widget::Destroy()
00147         {
00148                 DestroyWindow(m_hWnd);
00149                 m_hWnd=NULL;
00150         };
00151         /*******************************************************************************/
00152 
00153         void Widget::Create(int x,int y,int width,int height,string caption,string classname,DWORD styles,HWND parent,DWORD stylesex)
00154         {
00155                 try
00156                 {
00157                         if(m_hWnd!=NULL)
00158                                 EXCEPT("Widget allready have window!");
00159 
00160                         m_hContextMenu=NULL;
00161 
00162                         m_hInstance=GetModuleHandle(NULL);
00163 
00164                         m_prevWndProc=NULL;
00165 
00166                         m_hWnd = CreateWindowEx(stylesex,classname.c_str(), caption.c_str(),styles, x, y, width, height,parent,NULL, m_hInstance, this);
00167                         if(!m_hWnd)
00168                         {
00169                                 EXCEPT("CreateWindow");
00170                         };
00171                         if(classname.compare("MicroWidget")!=0)
00172                         {
00173                                 SetWindowLong(m_hWnd,GWL_USERDATA,reinterpret_cast<LONG>(this));
00174                                 m_prevWndProc=reinterpret_cast<WNDPROC>(SetWindowLong(m_hWnd,GWL_WNDPROC,reinterpret_cast<LONG>(WindowProcedure)));
00175                         }
00176 
00177 
00178                         Widgets::Instance()->AddWidget();
00179 
00180                 }
00181                 catch(Exception e)
00182                 {
00183                         CATCHEXCEPT(e,"Widget::Widget");
00184                 };
00185         };
00186         /*******************************************************************************/
00187 
00188         HWND Widget::GetHwnd()
00189         {
00190                 return m_hWnd;
00191         };
00192         /*******************************************************************************/
00193 
00194         HINSTANCE Widget::GetHinstance()
00195         {
00196                 return m_hInstance;
00197         };
00198         /*******************************************************************************/
00199 
00200         void Widget::Show()
00201         {
00202                 ShowWindow(m_hWnd,SW_SHOW);
00203                 UpdateWindow(m_hWnd);
00204         };
00205         /*******************************************************************************/
00206 
00207         void Widget::Hide()
00208         {
00209                 ShowWindow(m_hWnd,SW_HIDE);
00210         };
00211         /*******************************************************************************/
00212 
00213         void Widget::SetText(string text)
00214         {
00215                 SetWindowText(m_hWnd,text.c_str());
00216         };
00217         /*******************************************************************************/
00218 
00219         string Widget::GetText()
00220         {
00221                 char *pBuf;
00222                 string str;
00223                 LRESULT len=SendMessage(m_hWnd,WM_GETTEXTLENGTH,0,0);
00224                 if(len>0)
00225                 {
00226                         len++;
00227                         pBuf=new char[len];
00228                         GetWindowText(m_hWnd,pBuf,(int)len);
00229                         str=pBuf;
00230                         delete[] pBuf;
00231                 }
00232                 return str;
00233         };
00234         /*******************************************************************************/
00235 
00236         BOOL CALLBACK Widget::DestroyChildProc(HWND hwnd,LPARAM)
00237         {
00238                 DestroyWindow(hwnd);
00239                 return TRUE;
00240         };
00241         /*******************************************************************************/
00242 
00243         void Widget::DestroyChildWindows()
00244         {
00245                 EnumChildWindows(m_hWnd,DestroyChildProc,0);
00246         };
00247         /*******************************************************************************/
00248         void Widget::ProcessMenu(WORD id)
00249         {
00250                 HMENU hMenu=GetMenu(m_hWnd);
00251                 if(hMenu==NULL)
00252                         hMenu=m_hContextMenu;
00253                 if(hMenu!=NULL)
00254                 {
00255                         MENUITEMINFO info;
00256                         info.cbSize=sizeof(MENUITEMINFO);
00257                         info.fMask=MIIM_DATA;
00258                         info.dwItemData=0;
00259                         GetMenuItemInfo(hMenu,id,FALSE,&info);
00260                         if(info.dwItemData!=0)
00261                         {
00262                                 (reinterpret_cast<BaseEventHandler*>(info.dwItemData))->Call(this);
00263                         }
00264                         m_hContextMenu=NULL;
00265                 }
00266                 else
00267                         EXCEPT("Widget::ProcessMenu - wrong menu");
00268         };
00269         /*******************************************************************************/
00270 
00271         void Widget::SetMenuBar(Menu* pMenu)
00272         {
00273                 SetMenu(m_hWnd,pMenu->GetHMENU());
00274                 DrawMenuBar(m_hWnd);
00275         };
00276         /*******************************************************************************/
00277 
00278         void Widget::RemoveMenuBar()
00279         {
00280                 SetMenu(m_hWnd,NULL);
00281                 DrawMenuBar(m_hWnd);
00282         };
00283         /*******************************************************************************/
00284 
00285         void Widget::OnLeftButtonDown(int x,int y,DWORD buttonMask)
00286         {
00287                 if(m_pOnLButtonDownHandler)
00288                 {
00289                         m_pOnLButtonDownHandler->Call(this,x,y,buttonMask);
00290                 }
00291         };
00292         /*******************************************************************************/
00293 
00294         void Widget::OnRightButtonDown(int x,int y,DWORD buttonMask)
00295         {
00296                 if(m_pOnRButtonDownHandler)
00297                 {
00298                         m_pOnRButtonDownHandler->Call(this,x,y,buttonMask);
00299                 }
00300         };
00301         /*******************************************************************************/
00302         void Widget::ProcessAccelerators(WORD)
00303         {
00304 
00305         };
00306         /*******************************************************************************/
00307 
00308         void Widget::Enable(BOOL bEnable)
00309         {
00310                 EnableWindow(m_hWnd,bEnable);
00311         };
00312         /*******************************************************************************/
00313         bool Widget::IsDestroyed()
00314         {
00315                 return (m_hWnd==NULL);
00316         };
00317         /*******************************************************************************/
00318 
00319         void Widget::OnCreate()
00320         {
00321                 if(m_pOnCreateHandler)
00322                 {
00323                         m_pOnCreateHandler->Call(this);
00324                 }
00325         };
00326         /*******************************************************************************/
00327         void Widget::SetHotKey(UINT fsModifiers,UINT vk,BaseEventHandler* pEventHandler)
00328         {
00329                 char buf[50];
00330                 std::string str;
00331                 itoa(fsModifiers,buf,10);
00332                 str+=buf;
00333                 str+=":";
00334                 itoa(vk,buf,10);
00335                 str+=buf;
00336                 std::map<std::string,UINT>::iterator i;
00337                 i=m_mapHotKeyIds.find(str);
00338                 if(i!=m_mapHotKeyIds.end())
00339                 {
00340                         if(pEventHandler==NULL)
00341                         {
00342                                 UnregisterHotKey(m_hWnd,i->second);
00343                                 if(m_mapHotKeyHendles[i->second])
00344                                         delete m_mapHotKeyHendles[i->second];
00345                                 m_mapHotKeyIds.erase(str);
00346                                 m_mapHotKeyHendles.erase(i->second);
00347                         }
00348                         else
00349                         {
00350                                 if(m_mapHotKeyHendles[i->second])
00351                                         delete m_mapHotKeyHendles[i->second];
00352                                 m_mapHotKeyHendles[i->second]=pEventHandler;
00353                         }
00354                 }
00355                 else
00356                 {
00357                         UINT id=Widgets::Instance()->GetNewId();
00358                         RegisterHotKey(m_hWnd,id,fsModifiers,vk);
00359                         m_mapHotKeyIds[str]=id;
00360                         m_mapHotKeyHendles[id]=pEventHandler;
00361                 }
00362         };
00363         /*******************************************************************************/
00364         void Widget::ProcessHotKey(WORD id)
00365         {
00366                 std::map<UINT,BaseEventHandler*>::iterator i;
00367                 i=m_mapHotKeyHendles.find(id);
00368                 if(i!=m_mapHotKeyHendles.end())
00369                 {
00370                         if(i->second!=NULL)
00371                                 i->second->Call(this);
00372                 }
00373         };
00374         /*******************************************************************************/
00375         HWND Widget::GetParent()
00376         {
00377                 return ::GetParent(m_hWnd);
00378         };
00379         /*******************************************************************************/
00380         void Widget::OnSize(int left,int top,int right,int bottom)
00381         {
00382                 if(m_pOnSizeHandler)
00383                 {
00384                         m_pOnSizeHandler->Call(this,left,top,right,bottom);
00385                 }
00386         };
00387         /*******************************************************************************/
00388         void Widget::Move(int x,int y)
00389         {
00390                 SetWindowPos(m_hWnd,HWND_TOP,x,y,0,0,SWP_NOZORDER|SWP_NOSIZE);
00391         };
00392         /*******************************************************************************/
00393         void Widget::Size(int w,int h)
00394         {
00395                 SetWindowPos(m_hWnd,HWND_TOP,0,0,w,h,SWP_NOZORDER|SWP_NOMOVE);
00396         };
00397         /*******************************************************************************/
00398 };

Generated on Thu Oct 26 13:47:45 2006 for MWidgets by  doxygen 1.4.7