sprint 1-alpha
sprint/gtl/EditableListView.h
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 
00022 #ifndef _SPRINT_ETITABLE_LISTVIEW_H
00023 #define _SPRINT_ETITABLE_LISTVIEW_H
00024 
00025 namespace sprint {
00026         namespace gtl {
00027 
00032 
00035 class EditableListView {
00036       HWND hParent;
00037       HWND hEditBox;
00038       HWND hSecBox;
00039       sprint::ListView m_list; 
00040       int iCurItem, iCurSubItem;
00041 
00042       WNDPROC wpOld; // previous callback
00043       
00044       void OnSaveEdit()
00045       {
00046       char buffer[256];
00047                  
00048       GetWindowText(hEditBox, buffer, 256);
00049       m_list.SetItemText(iCurItem, iCurSubItem, buffer);
00050       }
00051       
00052 static LRESULT APIENTRY SubClass_ListView_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
00053 { 
00054     EditableListView *_this =(EditableListView *)GetProp(hwnd, "_THIS");
00055         switch(uMsg) {
00056         case WM_GETDLGCODE :
00057           return DLGC_WANTALLKEYS;
00058                 case WM_KEYDOWN:
00059                         if (wParam == VK_RETURN)
00060                         {
00061                 // call callback
00062                 _this->OnSaveEdit();
00063                                 DestroyWindow(hwnd);                                                    //      Now I can destroy the edit box
00064                         }               
00065                         else 
00066             if (wParam == VK_ESCAPE)
00067             {  
00068                                 DestroyWindow(hwnd);                                                    //      Abort operation...
00069                                 _this->hEditBox = NULL;
00070             }
00071             else
00072               CallWindowProc(_this->wpOld, hwnd, uMsg, wParam, lParam);
00073                         break;
00074 
00075                 case WM_DESTROY:
00076                         //      Restore the original window procedure
00077                         SetWindowLong(hwnd, GWL_WNDPROC, (LONG) _this->wpOld); 
00078                         _this->hEditBox = NULL;
00079       
00080                         RemoveProp(hwnd, "_THIS");
00081                         break;
00082                 default:
00083                         //      Default messages are passed to the original window procedure
00084                         return CallWindowProc(_this->wpOld, hwnd, uMsg, wParam, lParam); 
00085         }
00086         return 0;
00087 } 
00088       
00089       public:
00090       EditableListView() : hEditBox(NULL), hSecBox(NULL) { }
00091       
00092       void SetParent(HWND hWnd) { hParent = hWnd; }
00093       
00094       static const int ID_Browse = 5001;
00095       static const int ID_Edit = 5000;
00096 
00097       HWND CreateEdit(HWND hWnd, int iItem, int iSubItem)
00098       {
00099          sprint::ListView list(hWnd);
00100 
00101          RECT rc;
00102          char buffer[256];
00103          
00104          list.GetSubItemRect(iItem, iSubItem, 0, &rc);
00105          list.GetItemText(iItem, iSubItem, buffer, 256);
00106                      
00107          iCurItem = iItem;
00108          iCurSubItem = iSubItem;
00109          hEditBox = ::CreateWindowEx(0, ("EDIT"), buffer, WS_BORDER | 
00110                  WS_CHILD | WS_VISIBLE | WS_TABSTOP,
00111                          rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, list, NULL, 
00112                          GetModuleHandle(NULL), 0);
00113          m_list = list;
00114          ::SetProp(hEditBox, "_THIS", reinterpret_cast<void *>(this) );
00115          wpOld = (WNDPROC)::SetWindowLong(hEditBox, GWL_WNDPROC, (LONG) (WNDPROC) SubClass_ListView_WndProc);
00116 
00117          ::SendMessage(hEditBox, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
00118          ::SendMessage(hEditBox, EM_SETSEL, 0, strlen(buffer)); // Text selected
00119          ::SetFocus(hEditBox);
00120          return hEditBox;
00121       }
00122       
00123       void Browse(HWND hWnd)
00124       {
00125       sprint::BrowseForFile *b = new sprint::BrowseForFile();
00126       
00127       b->OpenAndSet(hWnd, ID_Edit);
00128       
00129       delete b;
00130       }
00131       
00132       HWND CreateComboBox(HWND hWnd, int iItem, int iSubItem)
00133       {
00134          sprint::ListView list(hWnd);
00135 
00136          RECT rc;
00137          char buffer[256];
00138          
00139          list.GetSubItemRect(iItem, iSubItem, 0, &rc);
00140          list.GetItemText(iItem, iSubItem, buffer, 256);
00141                      
00142          iCurItem = iItem;
00143          iCurSubItem = iSubItem;
00144          hEditBox = ::CreateWindowEx(0, ("COMBOBOX"), buffer,  
00145                  WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWNLIST ,
00146                          rc.left, rc.top, rc.right - rc.left, 200, list, NULL, 
00147                          GetModuleHandle(NULL), 0);
00148   
00149                                                            
00150 //         ::MoveWindow(hEditBox,  rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE);
00151                  
00152          ::SendMessage(hEditBox, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
00153          ::SendMessage(hEditBox, CB_SETITEMHEIGHT, (WPARAM) -1, rc.bottom - rc.top);
00154                          
00155          m_list = list;
00156          
00157          /*         
00158          ::SetProp(hEditBox, "_THIS", reinterpret_cast<void *>(this) );
00159          wpOld = (WNDPROC)::SetWindowLong(hEditBox, GWL_WNDPROC, (LONG) (WNDPROC) SubClass_ListView_WndProc);
00160          */
00161          ::SetFocus(hEditBox);
00162          
00163          return hEditBox;
00164 
00165       }
00166       
00167       HWND CreateFileBrowser(HWND hWnd, int iItem, int iSubItem)
00168       {
00169          sprint::ListView list(hWnd);
00170 
00171          RECT rc;
00172          char buffer[256];
00173          
00174          list.GetSubItemRect(iItem, iSubItem, 0, &rc);
00175          list.GetItemText(iItem, iSubItem, buffer, 256);
00176          
00177          #if 0
00178          iCurItem = iItem;
00179          iCurSubItem = iSubItem;
00180          hEditBox = ::CreateWindowEx(0, ("EDIT"), buffer, WS_BORDER | 
00181                  WS_CHILD | WS_VISIBLE | WS_TABSTOP,
00182                          rc.left, rc.top, rc.right - rc.left - 24, rc.bottom - rc.top, list, (HMENU) ID_Edit, 
00183                          GetModuleHandle(NULL), 0);
00184          hSecBox = ::CreateWindowEx(0, ("BUTTON"), "...", WS_CHILD | WS_VISIBLE | WS_TABSTOP,
00185                          rc.right-24, rc.top, 24, rc.bottom - rc.top, list, (HMENU) ID_Browse, 
00186                          GetModuleHandle(NULL), 0);
00187          ::SendMessage(hSecBox, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
00188 
00189          m_list = list;
00190          ::SetProp(hEditBox, "_THIS", reinterpret_cast<void *>(this) );
00191          wpOld = (WNDPROC)::SetWindowLong(hEditBox, GWL_WNDPROC, (LONG) (WNDPROC) SubClass_ListView_WndProc);
00192 
00193          ::SendMessage(hEditBox, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
00194          ::SendMessage(hEditBox, EM_SETSEL, 0, strlen(buffer)); // Text selected
00195          ::SetFocus(hEditBox);
00196          return hEditBox;
00197          #else
00198          sprint::OpenDlg dlg;
00199          dlg.SetFilter("All Image File\0*.png;*.jpg\0PNG Image (*.png)\0*.png\0Jpeg Image (*.jpg)\0*.jpg\0Tutti i file\0*.*\0\0\0");
00200          dlg.SetBuffer(buffer, 256);
00201          if(dlg.Open(NULL))
00202             list.SetItemText(iItem, iSubItem, buffer);
00203          // ridisegno la dialog
00204          list.Redraw();
00205          #endif
00206       return NULL;
00207       }
00208       
00209       void CheckActive()
00210       {
00211            if(hEditBox)
00212                  {
00213                   OnSaveEdit();
00214                   
00215                   ::DestroyWindow(hEditBox);
00216                   hEditBox = NULL;
00217                  }
00218            if(hSecBox)
00219                       {
00220                       ::DestroyWindow(hSecBox);
00221                       hSecBox = NULL;
00222                       }
00223       }
00224       
00225       enum Type {
00226            None,
00227            EditBox,
00228            ComboBox,
00229            FileBrowser
00230            };
00231       
00232       
00233       template<class T>
00234       void OnNotify(const LPNMHDR nmhr, T & notify)
00235       {
00236              
00237              if(nmhr->code  == LVN_ITEMACTIVATE)
00238                  {
00239                  LPNMITEMACTIVATE pnmia = reinterpret_cast<LPNMITEMACTIVATE>(nmhr);
00240                  Type ret;
00241                  CheckActive();
00242                  
00243                  ret = notify.GetType(pnmia->iItem, pnmia->iSubItem);
00244                  
00245                  if(ret!=None)
00246                      {
00247                      HWND hWnd = NULL;
00248                      if(ret == EditBox)
00249                           hWnd = CreateEdit(nmhr->hwndFrom, pnmia->iItem, pnmia->iSubItem);
00250                      else
00251                      if(ret == ComboBox)
00252                           hWnd = CreateComboBox(nmhr->hwndFrom, pnmia->iItem, pnmia->iSubItem);
00253                      else
00254                      if(ret == FileBrowser)
00255                           hWnd = CreateFileBrowser(nmhr->hwndFrom, pnmia->iItem, pnmia->iSubItem);
00256                      
00257                      notify.OnInit(hWnd, pnmia->iItem, pnmia->iSubItem);
00258 
00259                      }
00260                  
00261                  }
00262            
00263       }
00264       
00265       };       
00266 
00267 
00268 
00269  }
00270 }
00271 
00272 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines