sprint 1-alpha
sprint/gtl/TWin.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  *  GTL: GUI Template Library 
00003  *    A Cross Platform C++ Wrapper for Win32 users
00004  *  Copyright (C) 2007-2011 Paolo Medici (www.pmx.it)
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library 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 GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  *
00020  *******************************************************************************/
00021  
00027 #ifndef _SPRINT_TWIN_H
00028 #define _SPRINT_TWIN_H
00029 
00030 #ifdef WIN32
00031 # include <windows.h>
00032 #endif
00033 
00034 #include <string>
00035 
00036 #include "Window.h"
00037 #include "DC.h"
00038 #include "Bitmap.h"
00039 #include "Font.h"
00040 #include "Brush.h"
00041 
00042 // gui template Library
00043 namespace sprint {
00044         namespace gtl {
00045 
00047 
00049 class CDefaultWindowTraits {
00050         public:
00053 
00054                 static DWORD CreateWindow_ExStyle() { return 0;}
00056                 static DWORD CreateWindow_Style() { return WS_OVERLAPPEDWINDOW; }
00058                 static DWORD WndClass_Style() { return 0; }             
00060                 static HICON WndClass_Icon(HINSTANCE hInst) { return ::LoadIcon (NULL, IDI_APPLICATION); }              
00062                 static HICON WndClass_IconSm(HINSTANCE hInst) { return ::LoadIcon (NULL, IDI_APPLICATION); }            
00064                 static HCURSOR WndClass_Cursor(HINSTANCE hInst) { return ::LoadCursor (NULL, IDC_ARROW); }              
00067 };
00068 
00072 class CChildWindowTraits {
00073         public:
00075                 static DWORD CreateWindow_ExStyle() { return 0;}
00077                 static DWORD CreateWindow_Style() { return WS_CHILD; }
00079                 static DWORD WndClass_Style() { return 0; }             
00081                 static HICON WndClass_Icon(HINSTANCE hInst) { return NULL; }            
00083                 static HICON WndClass_IconSm(HINSTANCE hInst) { return NULL; }          
00085                 static HCURSOR WndClass_Cursor(HINSTANCE hInst) { return NULL; }                
00086 };
00087         
00090 template<class T = CWindow>
00091 struct TWindowManager: public CInstance {
00092 
00094     TWindowManager(HINSTANCE hInst) : CInstance(hInst)
00095                 {
00096                         WNDCLASSEX wincl;        /* Data structure for the windowclass */
00097                         
00098                         wincl.hInstance = hInst;
00099             wincl.lpszClassName = T::ClassName();
00100             wincl.lpfnWndProc = T::WindowProcedure;      /* This function is called by windows */
00101             wincl.style = T::WndClass_Style();                 /* Catch double-clicks */
00102             wincl.cbSize = sizeof (WNDCLASSEX);
00103         
00104             /* Use default icon and mouse-pointer */
00105             wincl.hIcon = T::WndClass_Icon(hInst);
00106             wincl.hIconSm = T::WndClass_IconSm(hInst);
00107             wincl.hCursor = T::WndClass_Cursor(hInst);
00108             wincl.lpszMenuName = NULL;                 /* No menu */
00109             wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
00110             wincl.cbWndExtra = sizeof(T*);                      /* structure or the window instance */
00111             /* Use Windows's default color as the background of the window */
00112             wincl.hbrBackground = NULL;
00113 
00114                         ::RegisterClassEx (&wincl);
00115                 }
00117                 ~TWindowManager() {}
00118 
00119                 static T * This(HWND hwnd) { return reinterpret_cast < T *> ( GetWindowLong(hwnd,0) ); }
00120 
00121                 static T * OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) 
00122                                          {
00123                                                 SetWindowLong(hwnd, 0, (LONG) lpCreateStruct->lpCreateParams);
00124                                                 return reinterpret_cast < T *>(lpCreateStruct->lpCreateParams);
00125                                           }
00126                 
00128     T *Create(
00129     LPCTSTR lpWindowName,       
00130     int x,      
00131     int y,      
00132     int nWidth, 
00133     int nHeight,        
00134     HWND hWndParent = HWND_DESKTOP,     
00135     HMENU hMenu = NULL, 
00136     DWORD dwStyle = T::CreateWindow_Style(),    
00137                 DWORD dwExStyle = T::CreateWindow_ExStyle()     
00138      )
00139                 {
00140                 T *wnd = new T(m_inst);
00141                 
00142     ::CreateWindowEx(dwExStyle, 
00143                                                         T::ClassName(), 
00144                                                         lpWindowName,
00145                                                         dwStyle,
00146                                                         x,
00147                                                         y,
00148                                                         nWidth,
00149                                                         nHeight,
00150                                                         hWndParent,
00151                                                         hMenu,
00152                                                         m_inst,
00153                                                         reinterpret_cast<LPVOID>(wnd) );
00154                                                         
00155                  return wnd;
00156                 }
00157 
00158 };
00159 
00161 #define BEGIN_COMMAND_MAP  case WM_COMMAND:  \
00162                                                                                                          wnd = Mgr::This(hwnd); \
00163                                                                                                    switch(LOWORD(wParam)) \
00164                                                                                                    {            \
00165 
00166 #define END_COMMAND_MAP         }       \
00167                                                                                                         return 0;
00168 
00172 #define HANDLE_COMMAND(id,fn)   case id: wnd->fn(); break;
00173 
00174 #define HANDLE_COMMAND_(id,fn,param)    case id: wnd->fn(param); break;
00175 
00179 #define HANDLE_CHECKBOX(id,fn)  case id: wnd->fn( SendMessage((HWND) lParam, BM_GETCHECK, 0,0)==BST_CHECKED); break;
00180 
00184 #define HANDLE_EDITBOX(id,fn)   case id: wnd->fn( HIWORD(wParam) ); break;
00185 
00189 #define HANDLE_EDITBOX_EX(id,fn)        case id: wnd->fn( (HWND) lParam, HIWORD(wParam) ); break;
00190 
00191 #define DECLARE_WINDOW_OBJECT(T)        static const char *ClassName() { return #T; }   \
00192                                                                                                                                         typedef T This_t;
00193 
00195 #define BEGIN_MSG_MAP    static LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) \
00196                                                                                                  { \
00197                                                                                            typedef gtl::TWindowManager<This_t> Mgr;     \
00198                                                                                                  This_t *wnd; \
00199                                                                                                  switch (message)               \
00200                                                                                                  {      \
00201                                                                                                  case WM_CREATE:        \
00202                                                                                                         {       \
00203                                                                                                         LPCREATESTRUCT lpCreateStruct = reinterpret_cast<LPCREATESTRUCT>(lParam) ;      \
00204                                                                                                         wnd = Mgr::OnCreate(hwnd, lpCreateStruct); \
00205                                                                                                         wnd->SetHwnd(hwnd);     \
00206                                                                                                         return wnd->OnCreate(lpCreateStruct);   \
00207                                                                                                         }       
00208 
00210 #define END_MSG_MAP          default: \
00211                                                                                                  return DefWindowProc (hwnd, message, wParam, lParam);  \
00212                                                                                                  }      \
00213                                                                                                  return 0;      \
00214                                                                                                  }
00215 
00217 #define HANDLE_ON_PAINT(fn)               case WM_PAINT:                Mgr::This(hwnd)->fn();  return 0;
00218 #define HANDLE_ON_COMMAND(fn)             case WM_COMMAND:                      Mgr::This(hwnd)->fn((int) LOWORD(wParam), (HWND) lParam, (UINT) HIWORD(wParam));        return 0;
00219 #define HANDLE_ON_DESTROY(fn)             case WM_DESTROY:                      Mgr::This(hwnd)->fn();  return 0;
00220 #define HANDLE_ON_LBUTTONDOWN(fn)         case WM_LBUTTONDOWN:          Mgr::This(hwnd)->fn(false, (int)LOWORD(lParam), (int)HIWORD(lParam), wParam); return 0;
00221 #define HANDLE_ON_MOUSEMOVE(fn)           case WM_MOUSEMOVE:            Mgr::This(hwnd)->fn((int)LOWORD(lParam), (int)HIWORD(lParam), wParam); return 0;
00222 #define HANDLE_ON_LBUTTONDBLCLK(fn)       case WM_LBUTTONDBLCLK:        Mgr::This(hwnd)->fn(true, (int)LOWORD(lParam), (int)HIWORD(lParam), wParam); return 0;
00223 #define HANDLE_ON_LBUTTONUP(fn)           case WM_LBUTTONUP:            Mgr::This(hwnd)->fn((int)LOWORD(lParam), (int)HIWORD(lParam), (UINT)wParam); return 0;
00224 #define HANDLE_ON_TIMER(fn)                       case WM_TIMER:                        Mgr::This(hwnd)->fn(wParam);                    return 0;
00225 #define HANDLE_ON_SETFONT(fn)             case WM_SETFONT:                      Mgr::This(hwnd)->fn((HFONT)wParam);                     return 0;
00226 
00227 
00229 
00230 #define HAVE_ON_PAINT                                   HANDLE_ON_PAINT(OnPaint)
00231 
00232 #define HAVE_ON_COMMAND                   HANDLE_ON_COMMAND(OnCommand)
00233 
00234 #define HAVE_ON_DESTROY                         HANDLE_ON_DESTROY(OnDestroy)
00235 
00236 #define HAVE_ON_LBUTTONDOWN             HANDLE_ON_LBUTTONDOWN(OnLButtonDown)
00237 
00238 #define HAVE_ON_LBUTTONDBLCLK   HANDLE_ON_LBUTTONDBLCLK(OnLButtonDown)
00239 
00240 #define HAVE_ON_MOUSEMOVE       HANDLE_ON_MOUSEMOVE(OnMouseMove)
00241 
00242 #define HAVE_ON_LBUTTONUP                       HANDLE_ON_LBUTTONUP(OnLButtonUp)
00243 
00244 #define HAVE_ON_TIMER                                   HANDLE_ON_TIMER(OnTimer)
00245 
00246 #define HAVE_ON_SETFONT                                 HANDLE_ON_SETFONT(OnSetFont)
00247 
00248 
00249 }
00250 }
00251 
00252 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines