src/button.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 "image.h"
00009 #include "icon.h"
00010 #include "eventhandler.h"
00011 #include "widgets.h"
00012 #include "menu.h"
00013 #include "widget.h"
00014 #include "button.h"
00015 #include "exception.h"
00016 namespace MWidgets
00017 {
00018         /*******************************************************************************/
00019         Button::Button():Widget()
00020         {
00021                 m_bFlat=false;
00022                 m_pOnClickHandler=NULL;
00023                 m_bMouseHover=false;
00024         };
00025         /*******************************************************************************/
00026         Button::~Button()
00027         {
00028                 if(m_pOnClickHandler!=NULL)
00029                         delete m_pOnClickHandler;
00030         };
00031         /*******************************************************************************/
00032         void Button::Create(Widget *parent,int x,int y,int w,int h,string caption,string tooltip,bool bflat)
00033         {
00034                 Widget::Create(x,y,w,h,caption,"BUTTON",WS_CHILD|BS_PUSHBUTTON|WS_TABSTOP|WS_CLIPSIBLINGS,parent->GetHwnd());
00035                 m_bFlat=false;
00036                 if(bflat)
00037                 {
00038                         LONG styles=GetWindowLong(m_hWnd,GWL_STYLE);
00039                         styles|=BS_FLAT;
00040                         SetWindowLong(m_hWnd,GWL_STYLE,styles);
00041                         m_bFlat=true;
00042                 }
00043 
00044                 m_sToolTip=tooltip;
00045                 TOOLINFO ti;                            // struct specifying info about tool in ToolTip control
00046                 RECT rect;                  // for client area coordinates
00047 
00048                 /* CREATE A TOOLTIP WINDOW */
00049                 m_hWndTT = CreateWindowEx(WS_EX_TOPMOST,
00050                 TOOLTIPS_CLASS,
00051                 NULL,
00052                 WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
00053                 CW_USEDEFAULT,
00054                 CW_USEDEFAULT,
00055                 CW_USEDEFAULT,
00056                 CW_USEDEFAULT,
00057                 m_hWnd,
00058                 NULL,
00059                 m_hInstance,
00060                 NULL
00061                 );
00062 
00063                 SetWindowPos(m_hWndTT,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
00064 
00065                 /* GET COORDINATES OF THE MAIN CLIENT AREA */
00066                 GetClientRect (m_hWnd, &rect);
00067 
00068                 /* INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE */
00069                 ti.cbSize = sizeof(TOOLINFO);
00070                 ti.uFlags = TTF_SUBCLASS;
00071                 ti.hwnd = m_hWnd;
00072                 ti.hinst = m_hInstance;
00073                 ti.uId = 0;
00074                 ti.lpszText = (LPSTR)m_sToolTip.c_str();
00075                 // ToolTip control will cover the whole window
00076                 ti.rect.left = rect.left;
00077                 ti.rect.top = rect.top;
00078                 ti.rect.right = rect.right;
00079                 ti.rect.bottom = rect.bottom;
00080 
00081                 /* SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW */
00082                 SendMessage(m_hWndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
00083 
00084         };
00085         /*******************************************************************************/
00086         LRESULT Button::OnMessage (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00087         {
00088                 if(message==WM_MOUSELEAVE)
00089                 {
00090                         m_bMouseHover=false;
00091                         InvalidateRect(hWnd,NULL,TRUE);
00092                 }
00093                 else if(message==WM_PAINT)
00094                 {
00095                         if(m_bFlat&&m_bMouseHover)
00096                         {
00097                                 PAINTSTRUCT ps;
00098                                 RECT rc;
00099                                 GetClientRect(hWnd,&rc);
00100                                 HDC hdc=BeginPaint(hWnd,&ps);
00101                                 HPEN p=CreatePen(PS_DOT,1,RGB(0,0,0));
00102                                 SelectObject(hdc,p);
00103                                 MoveToEx(hdc,2,2,NULL);
00104                                 LineTo(hdc,rc.right-3,2);
00105                                 LineTo(hdc,rc.right-3,rc.bottom-3);
00106                                 LineTo(hdc,2,rc.bottom-3);
00107                                 LineTo(hdc,2,2);
00108                                 DeleteObject(p);
00109                                 EndPaint(hWnd,&ps);
00110                         };
00111                 }
00112                 else if( message==WM_LBUTTONDOWN || message==WM_MOUSEMOVE   ||
00113             message==WM_LBUTTONUP   || message==WM_RBUTTONDOWN ||
00114             message==WM_MBUTTONDOWN || message==WM_RBUTTONUP   ||
00115             message==WM_MBUTTONUP)
00116                 {
00117                         if(m_bFlat&&message==WM_MOUSEMOVE)
00118                         {
00119                                 TRACKMOUSEEVENT tme;
00120                                 tme.cbSize=sizeof(TRACKMOUSEEVENT);
00121                                 tme.dwFlags=TME_LEAVE;
00122                                 tme.dwHoverTime=0;
00123                                 tme.hwndTrack=hWnd;
00124                                 TrackMouseEvent(&tme);
00125                                 if(!m_bMouseHover)
00126                                         InvalidateRect(hWnd,NULL,TRUE);
00127                                 m_bMouseHover=true;
00128                         }
00129                         MSG msg;
00130                         msg.hwnd=m_hWnd;
00131                         msg.lParam=lParam;
00132                         msg.wParam=wParam;
00133                         msg.message=message;
00134                         SendMessage(m_hWndTT,TTM_RELAYEVENT, 0,(LPARAM)&msg );
00135                 }
00136                 return Widget::OnMessage(hWnd, message, wParam, lParam);
00137         };
00138         /*******************************************************************************/
00139         void Button::OnCommand( WORD code,WORD  )
00140         {
00141                 if(code==BN_CLICKED)
00142                         OnClick();
00143         };
00144         /*******************************************************************************/
00145         void Button::OnClick()
00146         {
00147                 if(m_pOnClickHandler)
00148                 {
00149                         m_pOnClickHandler->Call(this);
00150                 }
00151         };
00152         /*******************************************************************************/
00153         void Button::SetImage(Image* pImage)
00154         {
00155                 LONG styles=GetWindowLong(m_hWnd,GWL_STYLE);
00156                 styles|=BS_BITMAP;
00157                 SetWindowLong(m_hWnd,GWL_STYLE,styles);
00158                 SendMessage(m_hWnd,BM_SETIMAGE ,IMAGE_BITMAP,(LPARAM)pImage->GetHBITMAP());
00159         };
00160         /*******************************************************************************/
00161         WIDGET_TYPE Button::GetType()
00162         {
00163                 return BUTTON;
00164         };
00165         /*******************************************************************************/
00166         void Button::SetPressed(bool val)
00167         {
00168                 SendMessage(m_hWnd,BM_SETSTATE,val,0);
00169         };
00170         /*******************************************************************************/
00171 
00172 };

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