src/grid.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 "grid.h"
00015 #include "exception.h"
00016 namespace MWidgets
00017 {
00018         /*******************************************************************************/
00019         Cell::Cell()
00020         {
00021                 text="X";
00022                 pData=NULL;
00023                 forecolor=RGB(0,0,0);
00024                 backcolor=RGB(255,255,255);
00025                 m_bBCChanged=false;
00026         };
00027         /*******************************************************************************/
00028         Grid::Grid():Widget()
00029         {
00030                 m_hWndEdit=NULL;
00031                 m_EditWndProc=NULL;
00032                 m_hHorzSizeCur=NULL;
00033                 m_hVertSizeCur=NULL;
00034                 m_hHorzSizeCur=NULL;
00035                 m_hCrossCur=NULL;
00036                 //table
00037                 m_nFixedColCount=0;
00038                 m_nFixedRowCount=0;
00039                 m_hFont=NULL;
00040                 SetFont("Terminal",9,false,false,false);
00041                 m_TextAlign=ALIGN_CENTER;
00042                 m_iLeftMargin=2;
00043                 m_iTopMargin=2;
00044                 m_iRightMargin=2;
00045                 m_iBottomMargin=2;
00046                 m_iDefColWidth=50;
00047                 m_iDefRowHeight=20;
00048                 m_bResizeMode=false;
00049                 m_bOnResizeArea=false;
00050                 m_iResizeRow=-1;
00051                 m_iResizeCol=-1;
00052                 m_iPrevResizeVal=-1;
00053                 //cell
00054                 m_iCurRow=0;
00055                 m_iCurCol=0;
00056                 m_iSelRow=0;
00057                 m_iSelCol=0;
00058 
00059                 //drawing
00060                 m_dwSelctionOP=0x00A000C9;
00061                 m_hSelectionBrush=CreateSolidBrush(RGB(150,150,200));
00062                 m_hBlackPen3=CreatePen(PS_SOLID,3,RGB(0,0,0));
00063                 m_hBlackPen2=CreatePen(PS_SOLID,2,RGB(0,0,0));
00064                 m_hBlackPen1=CreatePen(PS_SOLID,1,RGB(0,0,0));
00065                 m_hWhitePen=CreatePen(PS_SOLID,1,RGB(255,255,255));
00066                 m_dwDefFixedCellColor=RGB(0xDB,0xD8,0xD1);
00067                 m_dwDefCellColor=RGB(255,255,255);
00068                 m_iBroderWidth=1;
00069                 m_iFixedColWidth=0;
00070                 m_iFixedRowHeight=0;
00071                 m_iTableWidth=0;
00072                 m_iTableHeight=0;
00073 
00074                 //scroll
00075                 m_iMinVisRow=-1;
00076                 m_iMaxVisRow=-1;
00077                 m_iMinVisCol=-1;
00078                 m_iMaxVisCol=-1;
00079 
00080                 m_bEditingMode=false;
00081                 m_bSelectionMode=false;
00082                 m_bColSeletion=false;
00083                 m_bRowSeletion=false;
00084 
00085                 //cols/rows moving
00086                 m_bColRowOnMoveArea=false;
00087                 m_bRowMovingMode=false;
00088                 m_bColMovingMode=false;
00089                 m_regColMoving=NULL;
00090                 m_regRowMoving=NULL;
00091 
00092                 //moving/copieng cells
00093                 m_bCellOnMoveArea=false;
00094                 m_bCellOnCopiengArea=false;
00095                 m_bCellMovingMode=false;
00096                 m_bCellCopiengMode=false;
00097                 m_regCellMoving=NULL;
00098                 m_regCellCopieng=NULL;
00099         };
00100         /*******************************************************************************/
00101         Grid::~Grid()
00102         {
00103                 if(m_regCellMoving)
00104                         DeleteObject(m_regCellMoving);
00105                 if(m_regCellCopieng)
00106                         DeleteObject(m_regCellCopieng);
00107                 if(m_regColMoving)
00108                         DeleteObject(m_regColMoving);
00109                 if(m_regRowMoving)
00110                         DeleteObject(m_regRowMoving);
00111                 if(m_hSelectionBrush)
00112                         DeleteObject(m_hSelectionBrush);
00113                 if(m_hCrossCur)
00114                         DeleteObject(m_hCrossCur);
00115                 if(m_hMovingCur)
00116                         DeleteObject(m_hMovingCur);
00117                 if(m_hHorzSizeCur)
00118                         DeleteObject(m_hHorzSizeCur);
00119                 if(m_hVertSizeCur)
00120                         DeleteObject(m_hVertSizeCur);
00121                 if(m_hBlackPen3)
00122                         DeleteObject(m_hBlackPen3);
00123                 if(m_hBlackPen2)
00124                         DeleteObject(m_hBlackPen2);
00125                 if(m_hBlackPen1)
00126                         DeleteObject(m_hBlackPen1);
00127                 if(m_hWhitePen)
00128                         DeleteObject(m_hWhitePen);
00129                 if(m_hFont)
00130                         DeleteObject(m_hFont);
00131         };
00132         /*******************************************************************************/
00133         void Grid::Create(Widget *parent,int x,int y,int w,int h)
00134         {
00135                 try
00136                 {
00137                         Widget::Create(x,y,w,h,"","MicroWidget",WS_CHILD|WS_TABSTOP|WS_CLIPSIBLINGS|WS_BORDER,parent->GetHwnd());
00138 
00139                         m_hWndEdit=CreateWindow("EDIT","",WS_CHILD|WS_BORDER|WS_VISIBLE|ES_AUTOHSCROLL|ES_RIGHT,0,0,0,0,m_hWnd,NULL,m_hInstance,NULL);
00140                         if(!m_hWndEdit)
00141                         {
00142                                 EXCEPT("CreateWindow");
00143                         };
00144                         ShowWindow(m_hWndEdit,SW_HIDE);
00145                         SetWindowLong(m_hWndEdit,GWL_USERDATA,reinterpret_cast<LONG>(this));
00146                         m_EditWndProc=reinterpret_cast<WNDPROC>(SetWindowLong(m_hWndEdit,GWL_WNDPROC,reinterpret_cast<LONG>(EditWindowProcedure)));
00147 
00148                         m_hVertSizeCur=LoadCursor(NULL,IDC_SIZENS);
00149                         if(!m_hVertSizeCur)
00150                         {
00151                                 EXCEPT("LoadCursor");
00152                         };
00153                         m_hHorzSizeCur=LoadCursor(NULL,IDC_SIZEWE);
00154                         if(!m_hHorzSizeCur)
00155                         {
00156                                 EXCEPT("LoadCursor");
00157                         };
00158                         m_hMovingCur=LoadCursor(NULL,IDC_SIZEALL);
00159                         if(!m_hMovingCur)
00160                         {
00161                                 EXCEPT("LoadCursor");
00162                         };
00163                         m_hCrossCur=LoadCursor(NULL,IDC_CROSS);
00164                         if(!m_hCrossCur)
00165                         {
00166                                 EXCEPT("LoadCursor");
00167                         };
00168                 }
00169                 catch(Exception e)
00170                 {
00171                         CATCHEXCEPT(e,"Grid::Create");
00172                 };
00173         };
00174         /*******************************************************************************/
00175         WIDGET_TYPE Grid::GetType()
00176         {
00177                 return GRID;
00178         };
00179         /*******************************************************************************/
00180         void Grid::SetSize(int rows,int cols)
00181         {
00182                 int nAddedRows=rows-(int)m_vecTable.size();
00183                 if(nAddedRows!=0)
00184                 {
00185                         m_vecTable.resize(rows);
00186                         m_vecRowsHeights.resize(rows);
00187                         if(nAddedRows>0)
00188                         {
00189                                 unsigned startRow=(unsigned)(m_vecRowsHeights.size()-nAddedRows-1);
00190                                 if((int)m_vecRowsHeights.size()==nAddedRows)
00191                                         startRow=0;
00192                                 for(unsigned i=startRow;i<m_vecRowsHeights.size();i++)
00193                                 {
00194                                         m_vecRowsHeights[i]=m_iDefRowHeight;
00195                                         m_iTableHeight+=m_iDefRowHeight;
00196                                 }
00197                         }
00198                 }
00199                 if(m_vecTable.size()>0)
00200                 {
00201                         int nAddedCols=cols-(int)m_vecTable[0].size();
00202                         if(nAddedCols!=0)
00203                         {
00204                                 for(unsigned i=0;i<m_vecTable.size();i++)
00205                                         m_vecTable[i].resize(cols);
00206                                 m_vecColsWidths.resize(cols);
00207                                 if(nAddedCols>0)
00208                                 {
00209                                         unsigned startCol=(unsigned)(m_vecColsWidths.size()-nAddedCols-1);
00210                                         if((int)m_vecColsWidths.size()==nAddedCols)
00211                                                 startCol=0;
00212                                         for(unsigned i=startCol;i<m_vecColsWidths.size();i++)
00213                                         {
00214                                                 m_vecColsWidths[i]=m_iDefColWidth;
00215                                                 m_iTableWidth+=m_iDefColWidth;
00216                                         }
00217                                 }
00218                         }
00219                 }
00220 
00221         };
00222         /*******************************************************************************/
00223         void Grid::SetFixedRowCount(int val)
00224         {
00225                 try
00226                 {
00227                         if(m_vecRowsHeights.size()<1)
00228                                 EXCEPT("Row out of bounds");
00229                         m_nFixedRowCount=val;
00230                         m_iFixedRowHeight=0;
00231                         for(int i=0;i<m_nFixedRowCount;i++)
00232                                 m_iFixedRowHeight+=m_vecRowsHeights[i]+m_iBroderWidth;
00233                         if(m_iCurRow<val)
00234                         {
00235                                 m_iCurRow=val;
00236                                 m_iSelRow=val;
00237                         }
00238                 }
00239                 catch(Exception e)
00240                 {
00241                         CATCHEXCEPT(e,"Grid::SetFixedRowCount");
00242                 };
00243         };
00244         /*******************************************************************************/
00245         void Grid::SetFixedColCount(int val)
00246         {
00247                 try
00248                 {
00249                         if(m_vecColsWidths.size()<1)
00250                                 EXCEPT("Col out of bounds");
00251                         m_nFixedColCount=val;
00252                         m_iFixedColWidth=0;
00253                         for(int i=0;i<m_nFixedColCount;i++)
00254                                 m_iFixedColWidth+=m_vecColsWidths[i]+m_iBroderWidth;
00255 
00256                         if(m_iCurCol<val)
00257                         {
00258                                 m_iCurCol=val;
00259                                 m_iSelCol=val;
00260                         }
00261                 }
00262                 catch(Exception e)
00263                 {
00264                         CATCHEXCEPT(e,"Grid::SetFixedColCount");
00265                 };
00266         };
00267         /*******************************************************************************/
00268         void Grid::SetFont(string name,int size,bool bold,bool italic,bool underline)
00269         {
00270                 if(m_hFont)
00271                         DeleteObject(m_hFont);
00272                 int fnWeight=FW_NORMAL;
00273                 if(bold)
00274                         fnWeight=FW_BOLD;
00275                 DWORD fdwItalic=FALSE;
00276                 if(italic)
00277                         fdwItalic=TRUE;
00278                 DWORD fdwUnderline=FALSE;
00279                 if(underline)
00280                         fdwUnderline=TRUE;
00281                 m_hFont=CreateFont(size,0,0,0,fnWeight,fdwItalic,fdwUnderline,0,0,0,0,0,0,name.c_str());
00282         };
00283         /*******************************************************************************/
00284         void Grid::SetTextAlign(TextAlign align)
00285         {
00286                 m_TextAlign=align;
00287         };
00288         /*******************************************************************************/
00289         void Grid::SetRowHeight(int index,int h)
00290         {
00291                 try
00292                 {
00293                         if(index<(int)m_vecRowsHeights.size())
00294                         {
00295                                 int prevHeight=m_vecRowsHeights[index];
00296                                 m_vecRowsHeights[index]=h;
00297                                 if(index<m_nFixedRowCount)
00298                                 {
00299                                         m_iFixedRowHeight-=prevHeight;
00300                                         m_iFixedRowHeight+=m_vecRowsHeights[index];
00301                                 }
00302                                 m_iTableHeight-=prevHeight;
00303                                 m_iTableHeight+=m_vecRowsHeights[index];
00304                         }
00305                         else
00306                                 EXCEPT("Row number out of bounds");
00307                 }
00308                 catch(Exception e)
00309                 {
00310                         CATCHEXCEPT(e,"Grid::SetRowHeight");
00311                 };
00312         };
00313         /*******************************************************************************/
00314         void Grid::SetColWidth(int index,int w)
00315         {
00316                 try
00317                 {
00318                         if(index<(int)m_vecColsWidths.size())
00319                         {
00320                                 int prevWidth=m_vecColsWidths[index];
00321                                 m_vecColsWidths[index]=w;
00322                                 if(index<m_nFixedColCount)
00323                                 {
00324                                         m_iFixedColWidth-=prevWidth;
00325                                         m_iFixedColWidth+=m_vecColsWidths[index];
00326                                 }
00327                                 m_iTableWidth-=prevWidth;
00328                                 m_iTableWidth+=m_vecColsWidths[index];
00329                         }
00330                         else
00331                                 EXCEPT("Col number out of bounds");
00332                 }
00333                 catch(Exception e)
00334                 {
00335                         CATCHEXCEPT(e,"Grid::SetColWidth");
00336                 };
00337         };
00338         /*******************************************************************************/
00339         void Grid::SetTextMargins(int l,int t,int r,int b)
00340         {
00341                 m_iLeftMargin=l;
00342                 m_iTopMargin=t;
00343                 m_iRightMargin=r;
00344                 m_iBottomMargin=b;
00345         };
00346         /*******************************************************************************/
00347         void Grid::SetItemText(int row,int col,string text)
00348         {
00349                 try
00350                 {
00351                         if(row<(int)m_vecTable.size())
00352                         {
00353                                 if(col<(int)m_vecTable[row].size())
00354                                 {
00355                                         m_vecTable[row][col].text=text;
00356                                         if(m_sMinStr.length()==0)
00357                                                 m_sMinStr=text;
00358                                         else if(text.length()<m_sMinStr.length())
00359                                                 m_sMinStr=text;
00360                                         if(m_sMaxStr.length()==0)
00361                                                 m_sMaxStr=text;
00362                                         else if(text.length()>m_sMaxStr.length())
00363                                                 m_sMaxStr=text;
00364                                 }
00365                                 else
00366                                         EXCEPT("Col number out of bounds");
00367                         }
00368                         else
00369                                 EXCEPT("Row number out of bounds");
00370                 }
00371                 catch(Exception e)
00372                 {
00373                         CATCHEXCEPT(e,"Grid::SetItemText");
00374                 };
00375         };
00376         /*******************************************************************************/
00377         void Grid::SetItemData(int row,int col,void* data)
00378         {
00379                 try
00380                 {
00381                         if(row<(int)m_vecTable.size())
00382                         {
00383                                 if(col<(int)m_vecTable[row].size())
00384                                 {
00385                                         m_vecTable[row][col].pData=data;
00386                                 }
00387                                 else
00388                                         EXCEPT("Col number out of bounds");
00389                         }
00390                         else
00391                                 EXCEPT("Row number out of bounds");
00392                 }
00393                 catch(Exception e)
00394                 {
00395                         CATCHEXCEPT(e,"Grid::SetItemData");
00396                 };
00397         };
00398         /*******************************************************************************/
00399         void Grid::SetItemForeColor(int row,int col,DWORD color)
00400         {
00401                 try
00402                 {
00403                         if(row<(int)m_vecTable.size())
00404                         {
00405                                 if(col<(int)m_vecTable[row].size())
00406                                 {
00407                                         m_vecTable[row][col].forecolor=color;
00408                                 }
00409                                 else
00410                                         EXCEPT("Col number out of bounds");
00411                         }
00412                         else
00413                                 EXCEPT("Row number out of bounds");
00414                 }
00415                 catch(Exception e)
00416                 {
00417                         CATCHEXCEPT(e,"Grid::SetItemForeColor");
00418                 };
00419         };
00420         /*******************************************************************************/
00421         void Grid::SetItemBackColor(int row,int col,DWORD color)
00422         {
00423                 try
00424                 {
00425                         if(row<(int)m_vecTable.size())
00426                         {
00427                                 if(col<(int)m_vecTable[row].size())
00428                                 {
00429                                         m_vecTable[row][col].backcolor=color;
00430                                         m_vecTable[row][col].m_bBCChanged=true;
00431                                 }
00432                                 else
00433                                         EXCEPT("Col number out of bounds");
00434                         }
00435                         else
00436                                 EXCEPT("Row number out of bounds");
00437                 }
00438                 catch(Exception e)
00439                 {
00440                         CATCHEXCEPT(e,"Grid::SetItemBackColor");
00441                 };
00442         };
00443         /*******************************************************************************/
00444         int  Grid::GetFocusCellRow()
00445         {
00446                 return m_iCurRow;
00447         };
00448         /*******************************************************************************/
00449         int  Grid::GetFocusCellCol()
00450         {
00451                 return m_iCurCol;
00452         };
00453         /*******************************************************************************/
00454         void Grid::Refresh(bool bRecalculateVisibles)
00455         {
00456                 if(bRecalculateVisibles)
00457                         RecalculateVisibles();
00458                 InvalidateRect(m_hWnd,NULL,TRUE);
00459                 UpdateWindow(m_hWnd);
00460         };
00461         /*******************************************************************************/
00462         void Grid::DrawFixed(HDC hdc,RECT* cellRect,int r,int c,int w,int h)
00463         {
00464                 HBRUSH b;
00465                 UINT format;
00466                 RECT textRect;
00467                 if(cellRect->right<=(w+m_vecColsWidths[c])&&cellRect->bottom<=(h+m_vecRowsHeights[r]))
00468                 {
00469                         format=DT_VCENTER|DT_SINGLELINE ;
00470                         Cell cell=m_vecTable[r][c];
00471                         {
00472                                 if(cell.m_bBCChanged)
00473                                         b=CreateSolidBrush(cell.backcolor);
00474                                 else
00475                                         b=CreateSolidBrush(m_dwDefFixedCellColor);
00476 
00477                                 SelectObject(hdc,b);
00478                                 SelectObject(hdc,m_hBlackPen1);
00479                                 Rectangle(hdc,cellRect->left-1,cellRect->top-1,cellRect->right+1,cellRect->bottom+1);
00480 
00481                                 SelectObject(hdc,m_hWhitePen);
00482                                 Rectangle(hdc,cellRect->left,cellRect->top,cellRect->right,cellRect->bottom);
00483 
00484                                 DeleteObject(b);
00485                                 switch(m_TextAlign)
00486                                 {
00487                                         case ALIGN_LEFT:
00488                                                 format|=DT_LEFT;
00489                                                 break;
00490                                         case ALIGN_RIGHT:
00491                                                 format|=DT_RIGHT;
00492                                                 break;
00493                                         case ALIGN_CENTER:
00494                                                 format|=DT_CENTER;
00495                                                 break;
00496                                 };
00497                                 if(cell.m_bBCChanged)
00498                                         SetBkColor(hdc,cell.backcolor);
00499                                 else
00500                                         SetBkColor(hdc,m_dwDefFixedCellColor);
00501 
00502                                 SetTextColor(hdc,cell.forecolor);
00503                                 textRect=(*cellRect);
00504                                 textRect.left+=m_iLeftMargin;
00505                                 textRect.top+=m_iTopMargin;
00506                                 textRect.right-=m_iRightMargin;
00507                                 textRect.bottom-=m_iBottomMargin;
00508                                 DrawText(hdc,cell.text.c_str(),(int)cell.text.length(),&textRect,format);
00509                                 //draw selection rect
00510                                 if(((r>=m_iCurRow&&r<=m_iSelRow)||(r>=m_iSelRow&&r<=m_iCurRow))||((c>=m_iCurCol&&c<=m_iSelCol)||(c>=m_iSelCol&&c<=m_iCurCol)))
00511                                 {
00512                                         SelectObject(hdc,m_hSelectionBrush);
00513                                         PatBlt(hdc,cellRect->left-1,cellRect->top-1,cellRect->right-cellRect->left+2,cellRect->bottom-cellRect->top+2,m_dwSelctionOP);
00514                                 }
00515                         }
00516                 }
00517         };
00518         /*******************************************************************************/
00519         void Grid::DrawNormal(HDC hdc,RECT* cellRect,int r,int c,int w,int h)
00520         {
00521                 HBRUSH b;
00522                 UINT format;
00523                 RECT textRect;
00524                 if(r<(int)m_vecTable.size()&&c<(int)m_vecTable[r].size())
00525                 {
00526                         if(cellRect->right<=(w+m_vecColsWidths[c])&&cellRect->bottom<=(h+m_vecRowsHeights[r]))
00527                         {
00528                                 format=DT_VCENTER|DT_SINGLELINE ;
00529                                 Cell cell=m_vecTable[r][c];
00530                                 if(cell.m_bBCChanged)
00531                                         b=CreateSolidBrush(cell.backcolor);
00532                                 else
00533                                         b=CreateSolidBrush(m_dwDefCellColor);
00534                                 SelectObject(hdc,b);
00535                                 SelectObject(hdc,m_hBlackPen1);
00536                                 Rectangle(hdc,cellRect->left-1,cellRect->top-1,cellRect->right+1,cellRect->bottom+1);
00537 
00538                                 DeleteObject(b);
00539                                 switch(m_TextAlign)
00540                                 {
00541                                         case ALIGN_LEFT:
00542                                                 format|=DT_LEFT;
00543                                                 break;
00544                                         case ALIGN_RIGHT:
00545                                                 format|=DT_RIGHT;
00546                                                 break;
00547                                         case ALIGN_CENTER:
00548                                                 format|=DT_CENTER;
00549                                                 break;
00550                                 };
00551                                 if(cell.m_bBCChanged)
00552                                         SetBkColor(hdc,cell.backcolor);
00553                                 else
00554                                         SetBkColor(hdc,m_dwDefCellColor);
00555 
00556                                 SetTextColor(hdc,cell.forecolor);
00557 
00558                                 textRect=(*cellRect);
00559                                 textRect.left+=m_iLeftMargin;
00560                                 textRect.top+=m_iTopMargin;
00561                                 textRect.right-=m_iRightMargin;
00562                                 textRect.bottom-=m_iBottomMargin;
00563                                 DRAWTEXTPARAMS pars;
00564                                 pars.cbSize=sizeof(DRAWTEXTPARAMS);
00565                                 pars.iLeftMargin=m_iLeftMargin;
00566                                 pars.iRightMargin=m_iRightMargin;
00567                                 pars.iTabLength=0;
00568                                 pars.uiLengthDrawn=(UINT)cell.text.length();
00569                                 DrawTextEx(hdc,(char*)cell.text.c_str(),(int)cell.text.length(),&textRect,format,&pars);
00570 
00571                                 //draw selection rect
00572                                 if(((r>=m_iCurRow&&r<=m_iSelRow)||(r>=m_iSelRow&&r<=m_iCurRow))&&((c>=m_iCurCol&&c<=m_iSelCol)||(c>=m_iSelCol&&c<=m_iCurCol)))
00573                                 {
00574                                         if(r!=m_iCurRow||c!=m_iCurCol)
00575                                         {
00576                                                 SelectObject(hdc,m_hSelectionBrush);
00577                                                 PatBlt(hdc,cellRect->left-1,cellRect->top-1,cellRect->right-cellRect->left+2,cellRect->bottom-cellRect->top+2,m_dwSelctionOP);
00578                                         }
00579                                 }
00580                         }
00581                 }
00582         };
00583         /*******************************************************************************/
00584         void Grid::Draw()
00585         {
00586                 RECT selCRfr;
00587                 RECT selCRfc;
00588                 RECT selCR;
00589                 RECT clRect;
00590                 RECT cellRect;
00591                 PAINTSTRUCT ps;
00592                 HBRUSH b;
00593                 int w;
00594                 int h;
00595 
00596 
00597                 GetClientRect(m_hWnd,&clRect);
00598                 w=clRect.right-clRect.left;
00599                 h=clRect.bottom-clRect.top;
00600                 BeginPaint(m_hWnd,&ps);
00601                 HDC hdc = CreateCompatibleDC(ps.hdc);
00602                 HBITMAP hbmMem, hbmOld;
00603                 hbmMem = CreateCompatibleBitmap(ps.hdc,w, h);
00604                 hbmOld = (HBITMAP)SelectObject(hdc, hbmMem);
00605 
00606 
00607                 cellRect.left=m_iBroderWidth;
00608                 cellRect.top=m_iBroderWidth;
00609                 cellRect.right=0;
00610                 cellRect.bottom=0;
00611                 if(m_hFont)
00612                         SelectObject(hdc,m_hFont);
00613                 //clear bg
00614                 b=CreateSolidBrush(RGB(0xDB,0xD8,0xD1));
00615                 FillRect(hdc,&clRect,b);
00616                 DeleteObject(b);
00617 
00618                 if(m_vecColsWidths.size()>0&&m_vecRowsHeights.size()>0)
00619                 {
00620 
00621                         //draw fixed rows&cols
00622                         int r=0;
00623                         for(r=0;r<m_nFixedRowCount;r++)
00624                         {
00625                                 cellRect.left=m_iBroderWidth;
00626                                 for(int c=0;c<m_nFixedColCount;c++)
00627                                 {
00628                                         cellRect.right=cellRect.left+m_vecColsWidths[c];
00629                                         cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
00630 
00631                                         DrawFixed(hdc,&cellRect,r,c,w,h);
00632 
00633                                         cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
00634                                 }
00635                                 cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
00636                         }
00637                         cellRect.left=m_iBroderWidth;
00638                         cellRect.top=m_iBroderWidth;
00639                         cellRect.right=0;
00640                         cellRect.bottom=0;
00641                         //draw fixed rows
00642                         selCRfr.left=INT_MAX;
00643                         selCRfr.top=INT_MAX;
00644                         selCRfr.right=0;
00645                         selCRfr.bottom=0;
00646                         for(r=0;r<m_nFixedRowCount;r++)
00647                         {
00648                                 cellRect.left=m_iBroderWidth+m_iFixedColWidth;
00649                                 for(int c1=m_nFixedColCount;c1<=m_iMaxVisCol+1;c1++)
00650                                 {
00651                                         {
00652                                                 int c=c1+m_iMinVisCol;
00653                                                 if(c>=m_iMinVisCol&&c<=m_iMaxVisCol+1)
00654                                                 {
00655                                                         if(c<(int)m_vecTable[r].size())
00656                                                         {
00657                                                                 cellRect.right=cellRect.left+m_vecColsWidths[c];
00658                                                                 cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
00659 
00660                                                                 DrawFixed(hdc,&cellRect,r,c,w,h);
00661                                                                 if(((r>=m_iCurRow&&r<=m_iSelRow)||(r>=m_iSelRow&&r<=m_iCurRow))||((c>=m_iCurCol&&c<=m_iSelCol)||(c>=m_iSelCol&&c<=m_iCurCol)))
00662                                                                 {
00663                                                                         if(cellRect.left<selCRfr.left)
00664                                                                                 selCRfr.left=cellRect.left;
00665                                                                         if(cellRect.right<selCRfr.left)
00666                                                                                 selCRfr.left=cellRect.right;
00667 
00668                                                                         if(cellRect.top<selCRfr.top)
00669                                                                                 selCRfr.top=cellRect.top;
00670                                                                         if(cellRect.bottom<selCRfr.top)
00671                                                                                 selCRfr.top=cellRect.bottom;
00672 
00673                                                                         if(cellRect.left>selCRfr.right)
00674                                                                                 selCRfr.right=cellRect.left;
00675                                                                         if(cellRect.right>selCRfr.right)
00676                                                                                 selCRfr.right=cellRect.right;
00677 
00678                                                                         if(cellRect.top>selCRfr.bottom)
00679                                                                                 selCRfr.bottom=cellRect.top;
00680                                                                         if(cellRect.bottom>selCRfr.bottom)
00681                                                                                 selCRfr.bottom=cellRect.bottom;
00682                                                                 }
00683 
00684                                                                 cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
00685                                                         }
00686                                                 }
00687 
00688                                         }
00689                                 }
00690                                 cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
00691                         }
00692 
00693 
00694                         //draw fixed cols
00695                         selCRfc.left=INT_MAX;
00696                         selCRfc.top=INT_MAX;
00697                         selCRfc.right=0;
00698                         selCRfc.bottom=0;
00699                         cellRect.top=m_iFixedRowHeight+m_iBroderWidth;
00700                         for(int c=0;c<m_nFixedColCount;c++)
00701                         {
00702                                 cellRect.left=m_iBroderWidth;
00703                                 for(int r1=m_nFixedRowCount;r1<=m_iMaxVisRow+1;r1++)
00704                                 {
00705                                         {
00706                                                 r=r1+m_iMinVisRow;
00707                                                 if(r>=m_iMinVisRow&&r<=m_iMaxVisRow+1)
00708                                                 {
00709 
00710                                                         if(r<(int)m_vecTable.size())
00711                                                         {
00712                                                                 cellRect.right=cellRect.left+m_vecColsWidths[c];
00713                                                                 cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
00714 
00715                                                                 DrawFixed(hdc,&cellRect,r,c,w,h);
00716                                                                 if(((r>=m_iCurRow&&r<=m_iSelRow)||(r>=m_iSelRow&&r<=m_iCurRow))||((c>=m_iCurCol&&c<=m_iSelCol)||(c>=m_iSelCol&&c<=m_iCurCol)))
00717                                                                 {
00718                                                                         if(cellRect.left<selCRfc.left)
00719                                                                                 selCRfc.left=cellRect.left;
00720                                                                         if(cellRect.right<selCRfc.left)
00721                                                                                 selCRfc.left=cellRect.right;
00722 
00723                                                                         if(cellRect.top<selCRfc.top)
00724                                                                                 selCRfc.top=cellRect.top;
00725                                                                         if(cellRect.bottom<selCRfc.top)
00726                                                                                 selCRfc.top=cellRect.bottom;
00727 
00728                                                                         if(cellRect.left>selCRfc.right)
00729                                                                                 selCRfc.right=cellRect.left;
00730                                                                         if(cellRect.right>selCRfc.right)
00731                                                                                 selCRfc.right=cellRect.right;
00732 
00733                                                                         if(cellRect.top>selCRfc.bottom)
00734                                                                                 selCRfc.bottom=cellRect.top;
00735                                                                         if(cellRect.bottom>selCRfc.bottom)
00736                                                                                 selCRfc.bottom=cellRect.bottom;
00737                                                                 }
00738                                                                 cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
00739                                                         }
00740                                                 }
00741 
00742                                         }
00743                                 }
00744                                 cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
00745                         }
00746 
00747 
00748                         //draw other
00749 
00750                         cellRect.top=m_iFixedRowHeight+m_iBroderWidth;
00751 
00752                         selCR.left=INT_MAX;
00753                         selCR.top=INT_MAX;
00754                         selCR.right=0;
00755                         selCR.bottom=0;
00756 
00757                         for(int r=m_nFixedRowCount+m_iMinVisRow;r<=m_iMaxVisRow+1;r++)
00758                         {
00759                                 cellRect.left=m_iBroderWidth+m_iFixedColWidth;
00760                                 for(int c=m_nFixedColCount+m_iMinVisCol;c<=m_iMaxVisCol+1;c++)
00761                                 {
00762                                         cellRect.right=cellRect.left+m_vecColsWidths[c];
00763                                         cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
00764 
00765                                         DrawNormal(hdc,&cellRect,r,c,w,h);
00766                                         if(((r>=m_iCurRow&&r<=m_iSelRow)||(r>=m_iSelRow&&r<=m_iCurRow))&&((c>=m_iCurCol&&c<=m_iSelCol)||(c>=m_iSelCol&&c<=m_iCurCol)))
00767                                         {
00768                                                 if(cellRect.left<selCR.left)
00769                                                         selCR.left=cellRect.left;
00770                                                 if(cellRect.right<selCR.left)
00771                                                         selCR.left=cellRect.right;
00772 
00773                                                 if(cellRect.top<selCR.top)
00774                                                         selCR.top=cellRect.top;
00775                                                 if(cellRect.bottom<selCR.top)
00776                                                         selCR.top=cellRect.bottom;
00777 
00778                                                 if(cellRect.left>selCR.right)
00779                                                         selCR.right=cellRect.left;
00780                                                 if(cellRect.right>selCR.right)
00781                                                         selCR.right=cellRect.right;
00782 
00783                                                 if(cellRect.top>selCR.bottom)
00784                                                         selCR.bottom=cellRect.top;
00785                                                 if(cellRect.bottom>selCR.bottom)
00786                                                         selCR.bottom=cellRect.bottom;
00787                                         }
00788                                         cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
00789                                 }
00790                                 cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
00791                         }
00792 
00793                         b=CreateSolidBrush(RGB(0,0,0));
00794                         SelectObject(hdc,m_hBlackPen3);
00795                         SelectObject(hdc,b);
00796 
00797 
00798                         if(m_iCurRow>=(m_iMinVisRow+m_nFixedRowCount))
00799                         {
00800                                 MoveToEx(hdc,selCR.left,selCR.top,NULL);
00801                                 LineTo(hdc,selCR.right,selCR.top);
00802                         }
00803 
00804                         if(m_iCurCol<=m_iMaxVisCol)
00805                         {
00806                                 MoveToEx(hdc,selCR.right,selCR.top,NULL);
00807                                 LineTo(hdc,selCR.right,selCR.bottom);
00808                         }
00809 
00810                         if(m_iCurRow<=m_iMaxVisRow)
00811                         {
00812                                 MoveToEx(hdc,selCR.right,selCR.bottom,NULL);
00813                                 LineTo(hdc,selCR.left,selCR.bottom);
00814                         }
00815 
00816                         if(m_iCurCol>=(m_iMinVisCol+m_nFixedColCount))
00817                         {
00818                                 MoveToEx(hdc,selCR.left,selCR.bottom,NULL);
00819                                 LineTo(hdc,selCR.left,selCR.top);
00820                         }
00821 
00822                         Rectangle(hdc,selCR.right-3,selCR.bottom-3,selCR.right+3,selCR.bottom+3);
00823                         if(m_regCellCopieng)
00824                                 DeleteObject(m_regCellCopieng);
00825                         m_regCellCopieng=CreateRectRgn(selCR.right-3,selCR.bottom-3,selCR.right+3,selCR.bottom+3);
00826                         if(m_regCellMoving)
00827                                 DeleteObject(m_regCellMoving);
00828                         m_regCellMoving=CreateRectRgn(selCR.left-1,selCR.top-1,selCR.right+1,selCR.bottom+1);
00829                         HRGN r2=CreateRectRgn(selCR.left+1,selCR.top+1,selCR.right-1,selCR.bottom-1);
00830                         CombineRgn(m_regCellMoving,m_regCellMoving,r2,RGN_DIFF);
00831                         DeleteObject(r2);
00832 
00833                         //draw fixed selections
00834                         SelectObject(hdc,m_hBlackPen2);
00835                         //draw selection rect for fixed rows
00836                         MoveToEx(hdc,selCRfr.left,selCRfr.top,NULL);
00837                         LineTo(hdc,selCRfr.right,selCRfr.top);
00838                         LineTo(hdc,selCRfr.right,selCRfr.bottom);
00839                         LineTo(hdc,selCRfr.left,selCRfr.bottom);
00840                         LineTo(hdc,selCRfr.left,selCRfr.top);
00841                         Rectangle(hdc,selCRfr.left,selCRfr.top,selCRfr.left+6,selCRfr.top+6);
00842                         if(m_regRowMoving)
00843                                 DeleteObject(m_regRowMoving);
00844                         m_regRowMoving=CreateRectRgn(selCRfr.left,selCRfr.top,selCRfr.left+6,selCRfr.top+6);
00845 
00846                         //draw selection rect for fixed cols
00847                         MoveToEx(hdc,selCRfc.left,selCRfc.top,NULL);
00848                         LineTo(hdc,selCRfc.right,selCRfc.top);
00849                         LineTo(hdc,selCRfc.right,selCRfc.bottom);
00850                         LineTo(hdc,selCRfc.left,selCRfc.bottom);
00851                         LineTo(hdc,selCRfc.left,selCRfc.top);
00852                         Rectangle(hdc,selCRfc.left,selCRfc.top,selCRfc.left+6,selCRfc.top+6);
00853                         if(m_regColMoving)
00854                                 DeleteObject(m_regColMoving);
00855                         m_regColMoving=CreateRectRgn(selCRfc.left,selCRfc.top,selCRfc.left+6,selCRfc.top+6);
00856                         DeleteObject(b);
00857                 }
00858 
00859 
00860                 BitBlt(ps.hdc,clRect.left, clRect.top,clRect.right-clRect.left, clRect.bottom-clRect.top,hdc,0, 0,SRCCOPY);
00861 
00862                 SelectObject(hdc, hbmOld);
00863                 DeleteObject(hbmMem);
00864                 DeleteDC(hdc);
00865 
00866                 EndPaint(m_hWnd,&ps);
00867         };
00868         /*******************************************************************************/
00869         LRESULT CALLBACK Grid::EditWindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00870         {
00871 
00872                 Grid* pGrid=NULL;
00873                 pGrid=(Grid*)GetObjectFromHwnd(hWnd);
00874                 if(pGrid)
00875                         return pGrid->EditOnMessage(hWnd, message, wParam, lParam);
00876                 else
00877                         return DefWindowProc(hWnd, message, wParam, lParam);
00878         };
00879         /*******************************************************************************/
00880         LRESULT CALLBACK Grid::EditOnMessage (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00881         {
00882                 char buf[1024];
00883                 string str;
00884                 switch(message)
00885                 {
00886                         case WM_KEYDOWN:
00887                                 switch(wParam)
00888                                 {
00889                                         case VK_UP:
00890                                                 HideCellEdit();
00891                                                 if((m_iCurRow-1)>m_nFixedRowCount-1&&(m_iCurRow-1)<(int)m_vecRowsHeights.size())
00892                                                         m_iCurRow--;
00893                                                 Refresh();
00894                                                 break;
00895                                         case VK_DOWN:
00896                                                 HideCellEdit();
00897                                                 if((m_iCurRow+1)>m_nFixedRowCount-1&&(m_iCurRow+1)<(int)m_vecRowsHeights.size())
00898                                                         m_iCurRow++;
00899                                                 Refresh();
00900                                                 break;
00901                                         case VK_RETURN:
00902                                                 HideCellEdit();
00903                                                 Refresh();
00904                                                 break;
00905                                         case VK_ESCAPE:
00906                                                 str=GetItemText(m_iCurRow,m_iCurCol);
00907                                                 SetWindowText(hWnd,str.c_str());
00908                                                 HideCellEdit();
00909                                                 Refresh();
00910                                                 break;
00911                                         default:
00912                                                 return m_EditWndProc(hWnd, message, wParam, lParam);
00913                                 }
00914                                 break;
00915                         case WM_CHAR: //disable ding sound
00916                                 switch(wParam)
00917                                 {
00918                                         case VK_RETURN:
00919                                                 return 0 ;
00920                                         case VK_ESCAPE:
00921                                                 return 0 ;
00922                                 }
00923                                 return m_EditWndProc(hWnd, message, wParam, lParam);
00924                                 break;
00925                         case WM_KILLFOCUS:
00926                                 GetWindowText(hWnd,buf,1024);
00927                                 SetItemText(m_iCurRow,m_iCurCol,buf);
00928                                 m_bEditingMode=false;
00929                         default:
00930                                 if(m_EditWndProc)
00931                                         return m_EditWndProc(hWnd, message, wParam, lParam);
00932                 }
00933                 return 0;
00934         };
00935 
00936         /*******************************************************************************/
00937         LRESULT Grid::OnMessage (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00938         {
00939                 int xPos,yPos;
00940                 int nRow,nCol;
00941                 int fwKeys,zDelta;
00942                 RECT rc;
00943                 POINT pt;
00944                 TRACKMOUSEEVENT eventTrack;
00945                 eventTrack.cbSize=sizeof(TRACKMOUSEEVENT);
00946                 eventTrack.dwFlags=TME_NONCLIENT|TME_HOVER;
00947                 eventTrack.dwHoverTime=50;
00948                 eventTrack.hwndTrack=m_hWnd;
00949 
00950                 switch(message)
00951                 {
00952                         case WM_ERASEBKGND:
00953                                 return 1;
00954                                 break;                  
00955                         case WM_NCMOUSEMOVE:                                                            
00956                         case WM_NCMOUSEHOVER:           
00957                                 eventTrack.dwFlags=TME_NONCLIENT|TME_HOVER;
00958                                 TrackMouseEvent(&eventTrack);
00959                                 xPos = GET_X_LPARAM(lParam);
00960                                 yPos = GET_Y_LPARAM(lParam);
00961                                 if(m_bSelectionMode)
00962                                 {
00963                                         bool moved=false;
00964                                         pt.x=xPos;
00965                                         pt.y=yPos;
00966                                         ScreenToClient(m_hWnd,&pt);
00967                                         GetClientRect(m_hWnd,&rc);
00968                                         if(pt.x>rc.right)
00969                                         {
00970                                                 if(!m_bRowSeletion)
00971                                                 {
00972                                                         if((m_iSelCol+1)>m_nFixedColCount-1&&(m_iSelCol+1)<(int)m_vecColsWidths.size())
00973                                                         {
00974                                                                 m_iSelCol++;
00975                                                                 moved=true;
00976                                                         }
00977                                                 }                                               
00978                                         }                                       
00979                                         if(pt.y>rc.bottom)
00980                                         {
00981                                                 if(!m_bColSeletion)
00982                                                 {
00983                                                         if((m_iSelRow+1)>m_nFixedRowCount-1&&(m_iSelRow+1)<(int)m_vecRowsHeights.size())
00984                                                         {
00985                                                                 m_iSelRow++;
00986                                                                 moved=true;
00987                                                         }
00988                                                 }
00989                                                 
00990                                         }
00991                                         if(moved)
00992                                         {
00993                                                 if(!m_bColSeletion&&!m_bRowSeletion)
00994                                                         EnsureVisible(m_iSelRow,m_iSelCol);
00995                                                 else if(m_bColSeletion)
00996                                                         EnsureVisible(m_iMinVisRow+m_nFixedRowCount,m_iSelCol);
00997                                                 else if(m_bRowSeletion)
00998                                                         EnsureVisible(m_iSelRow,m_iMinVisCol+m_nFixedColCount);
00999 
01000                                                 Refresh();
01001                                         }
01002                                 }
01003                                 break;
01004                         case WM_MOUSEHOVER:                                                             
01005                         case WM_MOUSEMOVE:
01006                                 eventTrack.dwFlags=TME_HOVER;
01007                                 TrackMouseEvent(&eventTrack);
01008                                 xPos = GET_X_LPARAM(lParam);
01009                                 yPos = GET_Y_LPARAM(lParam);
01010                                 if(m_bSelectionMode)
01011                                 {
01012                                         GetCellByPoint(xPos,yPos,&nRow,&nCol);
01013                                         if(nRow>0&&nCol>0)
01014                                         {
01015                                                 if(!m_bColSeletion&&!m_bRowSeletion)
01016                                                 {
01017                                                         m_iSelRow=nRow; 
01018                                                         m_iSelCol=nCol;
01019                                                         EnsureVisible(m_iSelRow,m_iSelCol);
01020                                                 }
01021                                                 else if(m_bColSeletion&&message==WM_MOUSEMOVE)
01022                                                 {
01023                                                         m_iCurCol=nCol;
01024                                                 }
01025                                                 else if(m_bRowSeletion&&message==WM_MOUSEMOVE)
01026                                                 {
01027                                                         m_iSelRow=nRow; 
01028                                                 }                                               
01029                                                 Refresh();
01030                                         }
01031                                         else
01032                                         {               
01033                                                 if(yPos<m_iFixedRowHeight)
01034                                                 {
01035                                                         if(!m_bColSeletion)
01036                                                         {
01037                                                                 if((m_iSelRow-1)>m_nFixedRowCount-1&&(m_iSelRow-1)<(int)m_vecRowsHeights.size())
01038                                                                         m_iSelRow--;
01039                                                                 if(m_bRowSeletion)
01040                                                                         EnsureVisible(m_iSelRow,m_iMinVisCol+m_nFixedColCount);
01041                                                                 else
01042                                                                         EnsureVisible(m_iSelRow,m_iSelCol);
01043                                                         }       
01044                                                         else if(m_bColSeletion&&message==WM_MOUSEMOVE)
01045                                                         {
01046                                                                 GetFixedCellByPoint(xPos,yPos,&nRow,&nCol);
01047                                                                 if(nCol>=m_nFixedColCount)
01048                                                                         m_iSelCol=nCol;
01049                                                         }
01050                                                         Refresh();
01051                                                 }
01052                                                 if(xPos<m_iFixedColWidth)
01053                                                 {
01054                                                         if(!m_bRowSeletion)
01055                                                         {
01056                                                                 if((m_iSelCol-1)>m_nFixedColCount-1&&(m_iSelCol-1)<(int)m_vecColsWidths.size())
01057                                                                         m_iSelCol--;                                    
01058                                                                 if(m_bColSeletion)
01059                                                                         EnsureVisible(m_iMinVisRow+m_nFixedRowCount,m_iSelCol);
01060                                                                 else
01061                                                                         EnsureVisible(m_iSelRow,m_iSelCol);
01062                                                         }
01063                                                         else if(m_bRowSeletion&&message==WM_MOUSEMOVE)
01064                                                         {
01065                                                                 GetFixedCellByPoint(xPos,yPos,&nRow,&nCol);
01066                                                                 if(nRow>=m_nFixedRowCount)
01067                                                                         m_iSelRow=nRow;
01068                                                         }
01069                                                         Refresh();
01070                                                 }
01071 
01072                                         }
01073                                 }
01074                                 else
01075                                 {
01076                                         TestColRowMoving(xPos,yPos);
01077                                         if(m_bRowMovingMode)
01078                                         {
01079                                                 SetCursor(m_hMovingCur);
01080                                         }
01081                                         else if(m_bColMovingMode)
01082                                         {
01083                                                 SetCursor(m_hMovingCur);
01084                                         }
01085                                         if(!m_bColRowOnMoveArea)
01086                                                 TestColRowResizing(xPos,yPos);
01087                                         if(m_bResizeMode)
01088                                         {
01089                                                 if(m_iResizeCol>0)
01090                                                 {
01091                                                         SetCursor(m_hHorzSizeCur);
01092                                                         ResizeCol(xPos);
01093                                                 }
01094                                                 else if(m_iResizeRow>0)
01095                                                 {
01096                                                         SetCursor(m_hVertSizeCur);
01097                                                         ResizeRow(yPos);
01098                                                 }
01099                                         }                               
01100                                         TestCellMovingCopieng(xPos,yPos);
01101                                 }
01102                                 break;                  
01103                         case WM_LBUTTONDOWN:
01104                                 SetFocus(m_hWnd);
01105                                 xPos = GET_X_LPARAM(lParam);
01106                                 yPos = GET_Y_LPARAM(lParam);
01107                                 if(m_bOnResizeArea||m_bColRowOnMoveArea)
01108                                 {
01109                                         if(m_iResizeCol>0)
01110                                         {
01111                                                 m_bResizeMode=true;
01112                                                 m_iPrevResizeVal=xPos;
01113                                                 SetCursor(m_hHorzSizeCur);
01114                                         }
01115                                         else if(m_iResizeRow>0)
01116                                         {
01117                                                 m_bResizeMode=true;
01118                                                 m_iPrevResizeVal=yPos;
01119                                                 SetCursor(m_hVertSizeCur);
01120                                         }
01121                                         if(m_bColRowOnMoveArea)
01122                                         {
01123                                                 SetCursor(m_hMovingCur);
01124                                         }
01125                                 }
01126                                 else
01127                                 {
01128                                         GetCellByPoint(xPos,yPos,&nRow,&nCol);
01129                                         if(nRow!=-1&&nCol!=-1)
01130                                         {
01131                                                 HideCellEdit();
01132                                                 if(!(GetKeyState(VK_SHIFT)&0x80))
01133                                                 {
01134                                                         m_iCurRow=nRow;
01135                                                         m_iCurCol=nCol;
01136                                                 }
01137                                                 m_iSelRow=nRow;
01138                                                 m_iSelCol=nCol;
01139                                                 Refresh();
01140                                                 m_bSelectionMode=true;
01141                                         }
01142                                         else
01143                                         {
01144                                                 GetFixedCellByPoint(xPos,yPos,&nRow,&nCol);
01145                                                 if(nRow!=-1&&nCol!=-1)
01146                                                 {
01147                                                         if(nRow>=m_nFixedRowCount)
01148                                                         {
01149                                                                 m_iCurRow=nRow;
01150                                                                 m_iCurCol=m_nFixedColCount;
01151                                                                 m_iSelRow=m_iCurRow;
01152                                                                 m_iSelCol=m_vecColsWidths.size();
01153                                                                 m_bRowSeletion=true;
01154                                                         }
01155                                                         else if(nCol>=m_nFixedColCount)
01156                                                         {
01157                                                                 m_iCurRow=m_nFixedRowCount;
01158                                                                 m_iCurCol=nCol;
01159                                                                 m_iSelRow=m_vecRowsHeights.size();
01160                                                                 m_iSelCol=m_iCurCol;
01161                                                                 m_bColSeletion=true;
01162                                                         }                                                       
01163                                                 }                                               
01164                                                 Refresh();
01165                                                 m_bSelectionMode=true;
01166                                         }
01167                                 }
01168                                 break;
01169                         case WM_NCLBUTTONUP:                                            
01170                         case WM_LBUTTONUP:              
01171                                 if(m_bSelectionMode)
01172                                 {                                       
01173                                         m_bSelectionMode=false;
01174                                         m_bColSeletion=false;
01175                                         m_bRowSeletion=false;
01176                                 }
01177                                 else
01178                                 {
01179                                         m_bRowMovingMode=false;
01180                                         m_bColMovingMode=false;
01181                                         m_bResizeMode=false;
01182                                         m_iPrevResizeVal=-1;
01183                                 }
01184                                 break;
01185                         case WM_KEYDOWN:
01186                                 switch(wParam)
01187                                 {
01188                                         case VK_LEFT:
01189                                                 if(GetKeyState(VK_SHIFT)&0x80)
01190                                                 {
01191                                                         if((m_iSelCol-1)>m_nFixedColCount-1&&(m_iSelCol-1)<(int)m_vecColsWidths.size())
01192                                                                 m_iSelCol--;
01193                                                 }
01194                                                 else if((m_iCurCol-1)>m_nFixedColCount-1&&(m_iCurCol-1)<(int)m_vecColsWidths.size())
01195                                                 {
01196                                                         m_iCurCol--;
01197                                                         m_iSelCol=m_iCurCol;
01198                                                         m_iSelRow=m_iCurRow;
01199                                                 }
01200 
01201                                                 EnsureVisible(m_iSelRow,m_iSelCol);
01202 
01203                                                 if(m_iSelCol<(m_iMinVisCol+m_nFixedColCount))
01204                                                 {
01205                                                         if(m_vecTable.size()>0)
01206                                                         {
01207                                                                 if((m_iMinVisCol-1)>=0&&(m_iMinVisCol-1)<(int)m_vecTable[m_vecTable.size()-1].size())
01208                                                                 {
01209                                                                         while(m_iSelCol<(m_iMinVisCol+m_nFixedColCount))
01210                                                                         {
01211                                                                                 m_iMinVisCol-=1;
01212                                                                                 RecalculateVisibles();
01213                                                                         }
01214                                                                         SetScrollPos(m_hWnd,SB_HORZ,m_iMinVisCol,TRUE);
01215                                                                         Refresh();
01216                                                                 }
01217                                                         }
01218                                                 }
01219                                                 Refresh();
01220                                                 break;
01221                                         case VK_UP:
01222                                                 if(GetKeyState(VK_SHIFT)&0x80)
01223                                                 {
01224                                                         if((m_iSelRow-1)>m_nFixedRowCount-1&&(m_iSelRow-1)<(int)m_vecRowsHeights.size())
01225                                                                 m_iSelRow--;
01226                                                 }
01227                                                 else if((m_iCurRow-1)>m_nFixedRowCount-1&&(m_iCurRow-1)<(int)m_vecRowsHeights.size())
01228                                                 {
01229                                                         m_iCurRow--;
01230                                                         m_iSelCol=m_iCurCol;
01231                                                         m_iSelRow=m_iCurRow;
01232                                                 }
01233 
01234                                                 EnsureVisible(m_iSelRow,m_iSelCol);
01235 
01236                                                 if(m_iSelRow<(m_iMinVisRow+m_nFixedRowCount))
01237                                                 {
01238                                                         if(m_vecTable.size()>0)
01239                                                         {
01240                                                                 if((m_iMinVisRow-1)>=0&&(m_iMinVisRow-1)<(int)m_vecTable.size())
01241                                                                 {
01242                                                                         while(m_iSelRow<(m_iMinVisRow+m_nFixedRowCount))
01243                                                                         {
01244                                                                                 m_iMinVisRow-=1;
01245                                                                                 RecalculateVisibles();
01246                                                                         }
01247                                                                         SetScrollPos(m_hWnd,SB_VERT,m_iMinVisRow,TRUE);
01248                                                                         Refresh();
01249                                                                 }
01250                                                         }
01251                                                 }
01252                                                 Refresh();
01253                                                 break;
01254                                         case VK_RIGHT:
01255                                                 if(GetKeyState(VK_SHIFT)&0x80)
01256                                                 {
01257                                                         if((m_iSelCol+1)>m_nFixedColCount-1&&(m_iSelCol+1)<(int)m_vecColsWidths.size())
01258                                                                 m_iSelCol++;
01259                                                 }
01260                                                 else if((m_iCurCol+1)>m_nFixedColCount-1&&(m_iCurCol+1)<(int)m_vecColsWidths.size())
01261                                                 {
01262                                                         m_iCurCol++;
01263                                                         m_iSelCol=m_iCurCol;
01264                                                         m_iSelRow=m_iCurRow;
01265                                                 }
01266 
01267                                                 EnsureVisible(m_iSelRow,m_iSelCol);
01268 
01269                                                 if(m_iSelCol>m_iMaxVisCol)
01270                                                 {
01271                                                         if(m_vecTable.size()>0)
01272                                                         {
01273                                                                 if((m_iMinVisCol+1)>=0&&(m_iMinVisCol+1)<(int)m_vecTable[m_vecTable.size()-1].size())
01274                                                                 {
01275                                                                         while(m_iSelCol>m_iMaxVisCol)
01276                                                                         {
01277                                                                                 m_iMinVisCol+=1;
01278                                                                                 RecalculateVisibles();
01279                                                                         }
01280                                                                         SetScrollPos(m_hWnd,SB_HORZ,m_iMinVisCol,TRUE);
01281                                                                         Refresh();
01282                                                                 }
01283                                                         }
01284                                                 }
01285                                                 Refresh();
01286                                                 break;
01287                                         case VK_DOWN:
01288                                                 if(GetKeyState(VK_SHIFT)&0x80)
01289                                                 {
01290                                                         if((m_iSelRow+1)>m_nFixedRowCount-1&&(m_iSelRow+1)<(int)m_vecRowsHeights.size())
01291                                                                 m_iSelRow++;
01292                                                 }
01293                                                 else if((m_iCurRow+1)>m_nFixedRowCount-1&&(m_iCurRow+1)<(int)m_vecRowsHeights.size())
01294                                                 {
01295                                                         m_iCurRow++;
01296                                                         m_iSelCol=m_iCurCol;
01297                                                         m_iSelRow=m_iCurRow;
01298                                                 }
01299 
01300                                                 EnsureVisible(m_iSelRow,m_iSelCol);
01301 
01302                                                 if(m_iSelRow>m_iMaxVisRow)
01303                                                 {
01304                                                         if(m_vecTable.size()>0)
01305                                                         {
01306                                                                 if((m_iMinVisRow+1)>=0&&(m_iMinVisRow+1)<(int)m_vecTable.size())
01307                                                                 {
01308                                                                         while(m_iSelRow>m_iMaxVisRow)
01309                                                                         {
01310                                                                                 m_iMinVisRow+=1;
01311                                                                                 RecalculateVisibles();
01312                                                                         }
01313                                                                         SetScrollPos(m_hWnd,SB_VERT,m_iMinVisRow,TRUE);
01314                                                                         Refresh();
01315                                                                 }
01316                                                         }
01317                                                 }
01318                                                 Refresh();
01319                                                 break;
01320                                         case VK_F2:
01321                                                 ShowCellEdit(false);
01322                                                 break;
01323                                         case VK_DELETE:
01324                                                 SetItemText(m_iCurRow,m_iCurCol,"");
01325                                                 Refresh();
01326                                                 break;
01327                                         default:
01328                                                 {
01329                                                         MSG msg;
01330                                                         msg.message=message;
01331                                                         msg.hwnd=hWnd;
01332                                                         msg.lParam=lParam;
01333                                                         msg.wParam=wParam;
01334                                                         TranslateMessage(&msg);
01335                                                 }
01336                                 }
01337                                 break;
01338                         case WM_CHAR:
01339                                 {
01340                                         string str;
01341                                         char ch=(char)wParam;
01342                                         if(ch>31)
01343                                         {
01344                                                 str+=ch;
01345                                                 m_bEditingMode=true;
01346                                                 ShowCellEdit(true,str);
01347                                         }
01348                                 }
01349                                 break;
01350                         case WM_MOUSEWHEEL:
01351                                 xPos = GET_X_LPARAM(lParam);
01352                                 yPos = GET_Y_LPARAM(lParam);
01353                                 fwKeys = GET_KEYSTATE_WPARAM(wParam);
01354                                 zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
01355                                 {
01356                                         HideCellEdit();
01357                                         int min,max;
01358                                         int CurPos;
01359                                         SCROLLBARINFO horzInfo;
01360                                         horzInfo.cbSize = sizeof(SCROLLBARINFO);
01361                                         GetScrollBarInfo(m_hWnd,OBJID_HSCROLL,&horzInfo);
01362                                         if((xPos>=horzInfo.rcScrollBar.left&&xPos<=horzInfo.rcScrollBar.right)&&(yPos>=horzInfo.rcScrollBar.top&&yPos<=horzInfo.rcScrollBar.bottom))
01363                                         {
01364                                                 GetScrollRange(m_hWnd,SB_HORZ,&min,&max);
01365                                                 CurPos = GetScrollPos(m_hWnd,SB_HORZ);
01366                                                 CurPos+=(-zDelta/120);
01367                                                 if(m_vecTable.size()>0)
01368                                                 {
01369                                                         if(CurPos>=0&&CurPos<max)
01370                                                         {
01371                                                                 SetScrollPos(m_hWnd,SB_HORZ,CurPos,TRUE);
01372                                                                 m_iMinVisCol=CurPos;
01373                                                                 RecalculateVisibles();
01374                                                         }
01375                                                 }
01376                                         }
01377                                         else
01378                                         {
01379                                                 GetScrollRange(m_hWnd,SB_VERT,&min,&max);
01380                                                 CurPos = GetScrollPos(m_hWnd,SB_VERT);
01381                                                 CurPos+=(-zDelta/120);
01382                                                 if(m_vecTable.size()>0)
01383                                                 {
01384                                                         if(CurPos>=0&&CurPos<max)
01385                                                         {
01386                                                                 SetScrollPos(m_hWnd,SB_VERT,CurPos,TRUE);
01387                                                                 m_iMinVisRow=CurPos;
01388                                                                 RecalculateVisibles();
01389                                                         }
01390                                                 }
01391                                         }
01392 
01393                                         Refresh();
01394                                 }
01395                                 break;
01396                         case WM_HSCROLL:
01397                                 {
01398                                         HideCellEdit();
01399                                         int mmin,mmax;
01400                                         GetScrollRange(m_hWnd,SB_HORZ,&mmin,&mmax);
01401                                         int CurPos = GetScrollPos(m_hWnd,SB_HORZ);
01402 
01403                                         UINT nSBCode=LOWORD(wParam);
01404                                         UINT nPos=HIWORD(wParam);
01405                                         // Determine the new position of scroll box.
01406                                         switch (nSBCode)
01407                                         {
01408                                                 case SB_LEFT:      // Scroll to far left.
01409                                                         CurPos = 0;
01410                                                         break;
01411 
01412                                                 case SB_RIGHT:      // Scroll to far right.
01413                                                         CurPos = mmax;
01414                                                         break;
01415 
01416                                                 case SB_ENDSCROLL:   // End scroll.
01417                                                         break;
01418 
01419                                                 case SB_LINELEFT:      // Scroll left.
01420                                                         if (CurPos > mmin)
01421                                                                 CurPos--;
01422                                                         break;
01423 
01424                                                 case SB_LINERIGHT:   // Scroll right.
01425                                                         if (CurPos < mmax)
01426                                                                 CurPos++;
01427                                                         break;
01428 
01429                                                 case SB_PAGELEFT:    // Scroll one page left.
01430                                                         {
01431                                                                 // Get the page size.
01432                                                                 SCROLLINFO   info;
01433                                                                 info.cbSize=sizeof(SCROLLINFO);
01434                                                                 info.fMask=SIF_PAGE;
01435                                                                 GetScrollInfo(m_hWnd,SB_HORZ,&info);
01436 
01437                                                                 if (CurPos > mmin)
01438                                                                         CurPos = max(mmin, CurPos - (int) info.nPage);
01439                                                         }
01440                                                         break;
01441 
01442                                                 case SB_PAGERIGHT:      // Scroll one page right
01443                                                         {
01444                                                                 // Get the page size.
01445                                                                 SCROLLINFO   info;
01446                                                                 info.cbSize=sizeof(SCROLLINFO);
01447                                                                 info.fMask=SIF_PAGE;
01448                                                                 GetScrollInfo(m_hWnd,SB_HORZ,&info);
01449 
01450                                                                 if (CurPos < mmax)
01451                                                                         CurPos = min(mmax, CurPos + (int) info.nPage);
01452                                                         }
01453                                                         break;
01454 
01455                                                 case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
01456                                                         CurPos = nPos;      // of the scroll box at the end of the drag operation.
01457                                                         break;
01458 
01459                                                 case SB_THUMBTRACK:   // Drag scroll box to specified position. nPos is the
01460                                                         CurPos = nPos;     // position that the scroll box has been dragged to.
01461                                                         break;
01462                                         }
01463 
01464                                         // Set the new position of the thumb (scroll box).
01465 
01466                                         if(m_vecTable.size()>0)
01467                                         {
01468                                                 if(CurPos>=0&&CurPos<mmax)
01469                                                 {
01470                                                         SetScrollPos(m_hWnd,SB_HORZ,CurPos,TRUE);
01471                                                         m_iMinVisCol=CurPos;
01472                                                         RecalculateVisibles();
01473                                                 }
01474                                         }
01475 
01476                                         Refresh();
01477                                 }
01478                                 break;
01479                         case WM_VSCROLL:
01480                                 {
01481                                         HideCellEdit();
01482                                         int mmin,mmax;
01483                                         GetScrollRange(m_hWnd,SB_VERT,&mmin,&mmax);
01484                                         int CurPos = GetScrollPos(m_hWnd,SB_VERT);
01485 
01486                                         UINT nSBCode=LOWORD(wParam);
01487                                         UINT nPos=HIWORD(wParam);
01488                                         // Determine the new position of scroll box.
01489                                         switch (nSBCode)
01490                                         {
01491                                                 case SB_LEFT:      // Scroll to far left.
01492                                                         CurPos = 0;
01493                                                         break;
01494 
01495                                                 case SB_RIGHT:      // Scroll to far right.
01496                                                         CurPos = mmax;
01497                                                         break;
01498 
01499                                                 case SB_ENDSCROLL:   // End scroll.
01500                                                         break;
01501 
01502                                                 case SB_LINELEFT:      // Scroll left.
01503                                                         if (CurPos > mmin)
01504                                                                 CurPos--;
01505                                                         break;
01506 
01507                                                 case SB_LINERIGHT:   // Scroll right.
01508                                                         if (CurPos < mmax)
01509                                                                 CurPos++;
01510                                                         break;
01511 
01512                                                 case SB_PAGELEFT:    // Scroll one page left.
01513                                                         {
01514                                                                 // Get the page size.
01515                                                                 SCROLLINFO   info;
01516                                                                 info.cbSize=sizeof(SCROLLINFO);
01517                                                                 info.fMask=SIF_PAGE;
01518                                                                 GetScrollInfo(m_hWnd,SB_VERT,&info);
01519 
01520                                                                 if (CurPos > mmin)
01521                                                                         CurPos = max(mmin, CurPos - (int) info.nPage);
01522                                                         }
01523                                                         break;
01524 
01525                                                 case SB_PAGERIGHT:      // Scroll one page right
01526                                                         {
01527                                                                 // Get the page size.
01528                                                                 SCROLLINFO   info;
01529                                                                 info.cbSize=sizeof(SCROLLINFO);
01530                                                                 info.fMask=SIF_PAGE;
01531                                                                 GetScrollInfo(m_hWnd,SB_VERT,&info);
01532 
01533                                                                 if (CurPos < mmax)
01534                                                                         CurPos = min(mmax, CurPos + (int) info.nPage);
01535                                                         }
01536                                                         break;
01537 
01538                                                 case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
01539                                                         CurPos = nPos;      // of the scroll box at the end of the drag operation.
01540                                                         break;
01541 
01542                                                 case SB_THUMBTRACK:   // Drag scroll box to specified position. nPos is the
01543                                                         CurPos = nPos;     // position that the scroll box has been dragged to.
01544                                                         break;
01545                                         }
01546 
01547                                         // Set the new position of the thumb (scroll box).
01548                                         if(m_vecTable.size()>0)
01549                                         {
01550                                                 if(CurPos>=0&&CurPos<mmax)
01551                                                 {
01552                                                         SetScrollPos(m_hWnd,SB_VERT,CurPos,TRUE);
01553                                                         m_iMinVisRow=CurPos;
01554                                                         RecalculateVisibles();
01555                                                 }
01556                                         }
01557 
01558                                         Refresh();
01559                                 }
01560                                 break;
01561                         case WM_PAINT:
01562                                 Draw();
01563                                 break;
01564                         case WM_DESTROY:
01565                                 PostQuitMessage(0);
01566                                 break;
01567                         default:
01568                                 return Widget::OnMessage(hWnd, message, wParam, lParam);
01569                 }
01570                 return 0;
01571         };
01572         /*******************************************************************************/
01573         void Grid::RecalculateVisibles(int w,int h)
01574         {
01575                 RECT clRect;
01576                 if(w<0||h<0)
01577                 {
01578                         GetClientRect(m_hWnd,&clRect);
01579                         w=clRect.right-clRect.left;
01580                         h=clRect.bottom-clRect.top;
01581                 }
01582                 RECT cellRect;
01583                 bool bShowVScroll=false;
01584                 bool bShowHScroll=false;
01585                 int rl=0,cl=0;
01586                 if(m_vecTable.size()>0)
01587                 {
01588                         rl=(int)m_vecTable.size();
01589                         cl=(int)m_vecTable[m_vecTable.size()-1].size();
01590                 }
01591 
01592                 if(m_iMinVisCol<0)
01593                         m_iMinVisCol=0;
01594                 if(m_iMinVisRow<0)
01595                         m_iMinVisRow=0;
01596 
01597 
01598                 cellRect.top=m_iFixedRowHeight+m_iBroderWidth;
01599                 for(unsigned r=m_iMinVisRow+m_nFixedRowCount;r<m_vecTable.size();r++)
01600                 {
01601                         cellRect.left=m_iBroderWidth+m_iFixedColWidth;
01602                         for(unsigned c=m_iMinVisCol+m_nFixedColCount;c<m_vecTable[r].size();c++)
01603                         {
01604                                 cellRect.right=cellRect.left+m_vecColsWidths[c];
01605                                 cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
01606                                 if(cellRect.right>w)
01607                                 {
01608                                         if(!bShowHScroll)
01609                                         {
01610                                                 bShowHScroll=true;
01611                                                 cl=c-1;
01612                                         }
01613                                 }
01614                                 if(cellRect.bottom>h)
01615                                 {
01616                                         if(!bShowVScroll)
01617                                         {
01618                                                 bShowVScroll=true;
01619                                                 rl=r-1;
01620                                         }
01621                                 }
01622                                 if(bShowVScroll&&bShowHScroll)
01623                                         goto end;
01624 
01625                                 cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
01626                         }
01627                         cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
01628                 }
01629         end:
01630                 m_iMaxVisRow=rl;
01631                 m_iMaxVisCol=cl;
01632 
01633                 if(m_iTableWidth>w)
01634                 {
01635                         int lim=(int)m_vecTable[0].size()-m_iMaxVisCol+m_iMinVisCol;
01636                         ShowScrollBar(m_hWnd,SB_HORZ,TRUE);
01637                         int pos = GetScrollPos(m_hWnd,SB_HORZ);
01638                         if(pos>lim)
01639                                 pos=lim;
01640                         SetScrollRange(m_hWnd,SB_HORZ,0,lim,TRUE);
01641                         SetScrollPos(m_hWnd,SB_HORZ,pos,TRUE);
01642                 }
01643                 if(m_iTableWidth<=w)
01644                 {
01645                         ShowScrollBar(m_hWnd,SB_HORZ,FALSE);
01646                         m_iMinVisCol=0;
01647                 }
01648 
01649                 if(m_iTableHeight>h)
01650                 {
01651                         int lim=(int)m_vecTable.size()-m_iMaxVisRow+m_iMinVisRow;
01652                         ShowScrollBar(m_hWnd,SB_VERT,TRUE);
01653                         int pos = GetScrollPos(m_hWnd,SB_VERT);
01654                         if(pos>lim)
01655                                 pos=lim;
01656                         SetScrollRange(m_hWnd,SB_VERT,0,lim,TRUE);
01657                         SetScrollPos(m_hWnd,SB_VERT,pos,TRUE);
01658                 }
01659                 if(m_iTableHeight<=h)
01660                 {
01661                         ShowScrollBar(m_hWnd,SB_VERT,FALSE);
01662                         m_iMinVisRow=0;
01663                 }
01664 
01665         };
01666         /*******************************************************************************/
01667         void Grid::OnSize(int left,int top,int right,int bottom)
01668         {
01669                 RecalculateVisibles(right-left,bottom-top);
01670                 Refresh();
01671         };
01672         /*******************************************************************************/
01673         void Grid::SetDefColWidth(int w)
01674         {
01675                 m_iDefColWidth=w;
01676         };
01677         /*******************************************************************************/
01678         void Grid::SetDefRowHeight(int h)
01679         {
01680                 m_iDefRowHeight=h;
01681         };
01682         /*******************************************************************************/
01683         void Grid::GetCellByPoint(int x, int y,int* row,int* col)
01684         {
01685                 *row=-1;
01686                 *col=-1;
01687                 RECT cellRect;
01688 
01689                 int fixedColWidth=0;
01690                 int fixedRowHeight=0;
01691                 if(m_iMinVisCol)
01692                 {
01693                         fixedColWidth+=m_iFixedColWidth;
01694                 }
01695                 if(m_iMinVisRow)
01696                 {
01697                         fixedRowHeight+=m_iFixedRowHeight;
01698                 }
01699                 int startCol=0;
01700                 int startRow=0;
01701                 if(m_iMinVisCol)
01702                         startCol=m_iMinVisCol+m_nFixedColCount;
01703                 if(m_iMinVisRow)
01704                         startRow=m_iMinVisRow+m_nFixedRowCount;
01705 
01706                 cellRect.top=fixedRowHeight+m_iBroderWidth;
01707                 for(unsigned r=startRow;r<m_vecTable.size();r++)
01708                 {
01709                         cellRect.left=m_iBroderWidth+fixedColWidth;
01710                         for(unsigned c=startCol;c<m_vecTable[r].size();c++)
01711                         {
01712                                 cellRect.right=cellRect.left+m_vecColsWidths[c];
01713                                 cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
01714 
01715                                 if((int)r>=m_nFixedRowCount&&(int)c>=m_nFixedColCount)
01716                                 {
01717                                         if(x>(cellRect.left-m_iBroderWidth)&&x<(cellRect.right+m_iBroderWidth))
01718                                                 *col=c;
01719                                         if(y>(cellRect.top-m_iBroderWidth)&&y<(cellRect.bottom+m_iBroderWidth))
01720                                                 *row=r;
01721                                         if((*col)>=0&&(*row)>=0)
01722                                                 return;
01723                                 }
01724 
01725                                 cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
01726                         }
01727                         cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
01728                 }
01729 
01730         };
01731         /*******************************************************************************/
01732         void Grid::ShowCellEdit(bool replace,string str)
01733         {
01734                 RECT cellRect;
01735 
01736                 EnsureVisible(m_iCurRow,m_iCurCol);
01737 
01738                 GetCellRect(m_iCurRow,m_iCurCol,&cellRect);
01739                 string text;
01740                 if(replace)
01741                         text=str;
01742                 else
01743                         text=GetItemText(m_iCurRow,m_iCurCol);
01744                 MoveWindow(m_hWndEdit,cellRect.left,cellRect.top,cellRect.right-cellRect.left,cellRect.bottom-cellRect.top,TRUE);
01745                 SetWindowText(m_hWndEdit,text.c_str());
01746                 SendMessage(m_hWndEdit,EM_SETSEL,0,(LPARAM)-1);
01747                 SendMessage(m_hWndEdit,EM_SETSEL,(WPARAM)-1,0);
01748                 ShowWindow(m_hWndEdit,SW_SHOW);
01749                 UpdateWindow(m_hWndEdit);
01750                 SetFocus(m_hWndEdit);
01751         };
01752         /*******************************************************************************/
01753         void Grid::HideCellEdit()
01754         {
01755                 ShowWindow(m_hWndEdit,SW_HIDE);
01756         };
01757         /*******************************************************************************/
01758         string Grid::GetItemText(int row,int col)
01759         {
01760                 try
01761                 {
01762                         if(row<(int)m_vecTable.size())
01763                         {
01764                                 if(col<(int)m_vecTable[row].size())
01765                                 {
01766                                         return m_vecTable[row][col].text;
01767                                 }
01768                                 else
01769                                         EXCEPT("Col number out of bounds");
01770                         }
01771                         else
01772                                 EXCEPT("Row number out of bounds");
01773                 }
01774                 catch(Exception e)
01775                 {
01776                         CATCHEXCEPT(e,"Grid::GetItemText");
01777                 };
01778         };
01779         /*******************************************************************************/
01780         void Grid::GetCellRect(int row,int col,LPRECT pRect)
01781         {
01782                 pRect->left=0;
01783                 pRect->top=0;
01784                 pRect->right=0;
01785                 pRect->bottom=0;
01786                 RECT cellRect;
01787 
01788                 int fixedColWidth=0;
01789                 int fixedRowHeight=0;
01790                 if(m_iMinVisCol)
01791                 {
01792                         fixedColWidth+=m_iFixedColWidth;
01793                 }
01794                 if(m_iMinVisRow)
01795                 {
01796                         fixedRowHeight+=m_iFixedRowHeight;
01797                 }
01798                 int startCol=0;
01799                 int startRow=0;
01800                 if(m_iMinVisCol)
01801                         startCol=m_iMinVisCol+m_nFixedColCount;
01802                 if(m_iMinVisRow)
01803                         startRow=m_iMinVisRow+m_nFixedRowCount;
01804 
01805                 cellRect.top=fixedRowHeight+m_iBroderWidth;
01806                 for(unsigned r=startRow;r<m_vecTable.size();r++)
01807                 {
01808                         cellRect.left=m_iBroderWidth+fixedColWidth;
01809                         for(unsigned c=startCol;c<m_vecTable[r].size();c++)
01810                         {
01811                                 cellRect.right=cellRect.left+m_vecColsWidths[c];
01812                                 cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
01813 
01814                                 if((int)r==row&&(int)c==col)
01815                                         (*pRect)=cellRect;
01816 
01817                                 cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
01818                         }
01819                         cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
01820                 }
01821         };
01822         /*******************************************************************************/
01823         void Grid::TestColRowResizing(int x,int y)
01824         {
01825                 char buf[100];
01826                 sprintf(buf,"x:%d y:%d",x,y);
01827                 SetWindowText(GetParent(),buf);
01828 
01829                 int r,c;
01830                 int rowh,colw;
01831                 bool bFound=false;
01832                 if(!m_bResizeMode&&!m_bEditingMode&&!m_bColMovingMode&&!m_bRowMovingMode&&!m_bCellCopiengMode&&!m_bCellMovingMode)
01833                 {
01834                         if(x<m_iFixedColWidth+2||y<m_iFixedRowHeight+2)
01835                         {
01836                                 rowh=0;
01837                                 bFound=false;
01838                                 for(r=0;r<m_nFixedRowCount;r++)
01839                                 {
01840                                         rowh+=m_iBroderWidth+m_vecRowsHeights[r];
01841                                         if(abs(y-rowh)<2&&y>(m_iFixedRowHeight+2))
01842                                         {
01843                                                 SetCursor(m_hVertSizeCur);
01844                                                 m_iResizeRow=r;
01845                                                 m_iResizeCol=-1;
01846                                                 bFound=true;
01847                                                 break;
01848                                         }
01849                                 }
01850                                 if(!bFound)
01851                                 {
01852                                         for(r=m_iMinVisRow+m_nFixedRowCount;r<(int)m_vecTable.size();r++)
01853                                         {
01854                                                 rowh+=m_iBroderWidth+m_vecRowsHeights[r];
01855                                                 if(abs(y-rowh)<2&&y>(m_iFixedRowHeight+2))
01856                                                 {
01857                                                         SetCursor(m_hVertSizeCur);
01858                                                         m_iResizeRow=r;
01859                                                         m_iResizeCol=-1;
01860                                                         bFound=true;
01861                                                         break;
01862                                                 }
01863                                         }
01864                                 }
01865                                 //found cols
01866                                 if(!bFound)
01867                                 {
01868                                         colw=0;
01869                                         for(c=0;c<m_nFixedColCount;c++)
01870                                         {
01871                                                 colw+=m_iBroderWidth+m_vecColsWidths[c];
01872                                                 if(abs(x-colw)<2&&x>(m_iFixedColWidth+2))
01873                                                 {
01874                                                         SetCursor(m_hHorzSizeCur);
01875                                                         m_iResizeRow=-1;
01876                                                         m_iResizeCol=c;
01877                                                         bFound=true;
01878                                                         break;
01879                                                 }
01880                                         }
01881                                         if(!bFound)
01882                                         {
01883                                                 if(m_vecTable.size()>0)
01884                                                 {
01885                                                         for(c=m_iMinVisCol+m_nFixedColCount;c<(int)m_vecTable[0].size();c++)
01886                                                         {
01887                                                                 colw+=m_iBroderWidth+m_vecColsWidths[c];
01888                                                                 if(abs(x-colw)<2&&x>(m_iFixedColWidth+2))
01889                                                                 {
01890                                                                         SetCursor(m_hHorzSizeCur);
01891                                                                         m_iResizeRow=-1;
01892                                                                         m_iResizeCol=c;
01893                                                                         bFound=true;
01894                                                                         break;
01895                                                                 }
01896                                                         }
01897                                                 }
01898                                         }
01899                                 }
01900                         }
01901                         m_bOnResizeArea=bFound;
01902                 }
01903         };
01904         /*******************************************************************************/
01905         void Grid::ResizeRow(int newPos)
01906         {
01907                 if(m_iPrevResizeVal>0)
01908                 {
01909                         int curH=m_vecRowsHeights[m_iResizeRow];
01910                         curH+=newPos-m_iPrevResizeVal;
01911                         if(curH<2)
01912                                 curH=2;
01913                         m_vecRowsHeights[m_iResizeRow]=curH;
01914                         m_iPrevResizeVal=newPos;
01915                         Refresh();
01916                 }
01917         };
01918         /*******************************************************************************/
01919         void Grid::ResizeCol(int newPos)
01920         {
01921                 if(m_iPrevResizeVal>0)
01922                 {
01923                         int curW=m_vecColsWidths[m_iResizeCol];
01924                         curW+=newPos-m_iPrevResizeVal;
01925                         if(curW<2)
01926                                 curW=2;
01927                         m_vecColsWidths[m_iResizeCol]=curW;
01928                         m_iPrevResizeVal=newPos;
01929                         Refresh();
01930                 }
01931         };
01932         /*******************************************************************************/
01933         void Grid::ResizeToMinSize()
01934         {
01935                 SIZE size;
01936                 HDC hdc;
01937                 hdc=GetDC(m_hWnd);
01938                 HGDIOBJ oldFont=NULL;
01939                 if(hdc)
01940                 {
01941                         if(m_hFont)
01942                                 oldFont=SelectObject(hdc,m_hFont);
01943 
01944                         if(GetTextExtentPoint32(hdc,m_sMinStr.c_str(),(int)m_sMinStr.length(),&size))
01945                         {
01946                                 for(unsigned r=0;r<m_vecTable.size();r++)
01947                                 {
01948                                         if((int)r>=m_nFixedRowCount)
01949                                                 SetRowHeight(r,size.cy+m_iTopMargin*2+m_iBottomMargin*2);
01950                                         for(unsigned c=0;c<m_vecTable[r].size();c++)
01951                                         {
01952                                                 if((int)c>=m_nFixedColCount)
01953                                                         SetColWidth(c,size.cx+m_iLeftMargin*2+m_iRightMargin*2);
01954                                         }
01955                                 }
01956                         };
01957                         if(m_hFont)
01958                                 SelectObject(hdc,oldFont);
01959 
01960                 }
01961         };
01962         /*******************************************************************************/
01963         void Grid::ResizeToMaxSize()
01964         {
01965                 SIZE size;
01966                 HDC hdc;
01967                 hdc=GetDC(m_hWnd);
01968                 HGDIOBJ oldFont=NULL;
01969                 if(hdc)
01970                 {
01971                         if(m_hFont)
01972                                 oldFont=SelectObject(hdc,m_hFont);
01973 
01974                         if(GetTextExtentPoint32(hdc,m_sMaxStr.c_str(),(int)m_sMaxStr.length(),&size))
01975                         {
01976                                 for(unsigned r=0;r<m_vecTable.size();r++)
01977                                 {
01978                                         if((int)r>=m_nFixedRowCount)
01979                                                 SetRowHeight(r,size.cy+m_iTopMargin*2+m_iBottomMargin*2);
01980                                         for(unsigned c=0;c<m_vecTable[r].size();c++)
01981                                         {
01982                                                 if((int)c>=m_nFixedColCount)
01983                                                         SetColWidth(c,size.cx+m_iLeftMargin*2+m_iRightMargin*2);
01984                                         }
01985                                 }
01986                         };
01987                         if(m_hFont)
01988                                 SelectObject(hdc,oldFont);
01989 
01990                 }
01991         };
01992         /*******************************************************************************/
01993         void Grid::ResizeToAlignCol()
01994         {
01995                 SIZE size;
01996                 HDC hdc;
01997                 string str;
01998                 string maxstr;
01999                 size_t len=0;
02000                 hdc=GetDC(m_hWnd);
02001                 unsigned c=0,r=0;
02002                 int height=20;
02003                 HGDIOBJ oldFont=NULL;
02004                 if(hdc)
02005                 {
02006                         if(m_hFont)
02007                                 oldFont=SelectObject(hdc,m_hFont);
02008                         if(m_vecTable.size())
02009                         {
02010                                 if(GetTextExtentPoint32(hdc,"test",4,&size))
02011                                 {
02012                                         height=size.cy+m_iTopMargin*2+m_iBottomMargin*2;
02013                                         for(c=0;c<m_vecTable[0].size();c++)
02014                                         {
02015                                                 len=0;
02016                                                 for(r=0;r<m_vecTable.size();r++)
02017                                                 {
02018                                                         if((int)r>=m_nFixedRowCount)
02019                                                                 SetRowHeight(r,height);
02020                                                         str=GetItemText(r,c);
02021                                                         char* text=(char*)str.c_str();
02022                                                         int textlen=(int)str.length();
02023                                                         if(GetTextExtentPoint32(hdc,text,textlen,&size))
02024                                                         {
02025                                                                 if((size.cx+m_iLeftMargin*2+m_iRightMargin*2)>(long)len)
02026                                                                 {
02027                                                                         len=size.cx+m_iLeftMargin*2+m_iRightMargin*2;
02028                                                                 }
02029                                                         }
02030                                                 }
02031                                                 if(len>0)
02032                                                 {
02033                                                         if((int)c>=m_nFixedColCount)
02034                                                                 SetColWidth(c,(int)len);
02035                                                 }
02036                                         }
02037                                 }
02038                         }
02039                         if(m_hFont)
02040                                 SelectObject(hdc,oldFont);
02041                 }
02042         };
02043         /*******************************************************************************/
02044         void Grid::EnsureVisible(int row,int col)
02045         {
02046                 //if(col>m_iMaxVisCol)
02047                 //{
02048                 //      m_iMinVisCol=col;
02049                 //      SetScrollPos(m_hWnd,SB_HORZ,m_iMinVisCol,TRUE);
02050                 //}
02051                 //if(row>m_iMaxVisRow)
02052                 //{
02053                 //      m_iMinVisRow=row;
02054                 //      SetScrollPos(m_hWnd,SB_VERT,m_iMinVisRow,TRUE);
02055                 //}
02056                 //if(col<(m_iMinVisCol+m_nFixedColCount))
02057                 //{
02058                 //      m_iMinVisCol=col-m_nFixedColCount;
02059                 //      SetScrollPos(m_hWnd,SB_HORZ,m_iMinVisCol,TRUE);
02060                 //}
02061                 //if(row<(m_iMinVisRow+m_nFixedRowCount))
02062                 //{
02063                 //      m_iMinVisRow=row-m_nFixedRowCount;
02064                 //      SetScrollPos(m_hWnd,SB_VERT,m_iMinVisRow,TRUE);
02065                 //}
02066 
02067                 if(col>m_iMaxVisCol)
02068                 {
02069                         if((m_iMaxVisCol+1)>=0&&(m_iMaxVisCol+1)<(int)m_vecTable[m_vecTable.size()-1].size())
02070                         {
02071                                 if((m_iMinVisCol+1)>=0&&(m_iMinVisCol+1)<(int)m_vecTable[m_vecTable.size()-1].size())
02072                                 {
02073                                         while(col>m_iMaxVisCol)
02074                                         {
02075                                                 m_iMinVisCol+=1;
02076                                                 RecalculateVisibles();
02077                                         }
02078                                         SetScrollPos(m_hWnd,SB_HORZ,m_iMinVisCol,TRUE);
02079                                         Refresh();
02080                                 }
02081                         }
02082                 }
02083                 else if(col<(m_iMinVisCol+m_nFixedColCount))
02084                 {
02085                         if((m_iMaxVisCol-1)>=0&&(m_iMaxVisCol-1)<(int)m_vecTable[m_vecTable.size()-1].size())
02086                         {
02087                                 if(((m_iMinVisCol+m_nFixedColCount)-1)>=0&&((m_iMinVisCol+m_nFixedColCount)-1)<(int)m_vecTable[m_vecTable.size()-1].size())
02088                                 {
02089                                         while(col<(m_iMinVisCol+m_nFixedColCount))
02090                                         {
02091                                                 m_iMinVisCol-=1;
02092                                                 RecalculateVisibles();
02093                                         }
02094                                         SetScrollPos(m_hWnd,SB_HORZ,m_iMinVisCol,TRUE);
02095                                         Refresh();
02096                                 }
02097                         }
02098                 }
02099                 if(row>m_iMaxVisRow)
02100                 {
02101                         if((m_iMaxVisRow+1)>=0&&(m_iMaxVisRow+1)<(int)m_vecTable.size())
02102                         {
02103                                 if((m_iMinVisRow+1)>=0&&(m_iMinVisRow+1)<(int)m_vecTable.size())
02104                                 {
02105                                         while(row>m_iMaxVisRow)
02106                                         {
02107                                                 m_iMinVisRow+=1;
02108                                                 RecalculateVisibles();
02109                                         }
02110                                         SetScrollPos(m_hWnd,SB_VERT,m_iMinVisRow,TRUE);
02111                                         Refresh();
02112                                 }
02113                         }
02114                 }
02115                 else if(row<m_iMinVisRow+m_nFixedRowCount)
02116                 {
02117                         if((m_iMaxVisRow-1)>=0&&(m_iMaxVisRow-1)<(int)m_vecTable.size())
02118                         {
02119                                 if(((m_iMinVisRow+m_nFixedRowCount)-1)>=0&&((m_iMinVisRow+m_nFixedRowCount)-1)<(int)m_vecTable.size())
02120                                 {
02121                                         while(row<(m_iMinVisRow+m_nFixedRowCount))
02122                                         {
02123                                                 m_iMinVisRow-=1;
02124                                                 RecalculateVisibles();
02125                                         }
02126                                         SetScrollPos(m_hWnd,SB_VERT,m_iMinVisRow,TRUE);
02127                                         Refresh();
02128                                 }
02129                         }
02130                 }
02131                 RecalculateVisibles();
02132         };
02133         /*******************************************************************************/
02134         void Grid::SetDefCellColor(DWORD color)
02135         {
02136                 m_dwDefCellColor=color;
02137         };
02138         /*******************************************************************************/
02139         void Grid::SetDefFixedCellColor(DWORD color)
02140         {
02141                 m_dwDefFixedCellColor=color;
02142         };
02143         /*******************************************************************************/
02144         void Grid::TestColRowMoving(int x,int y)
02145         {
02146                 bool bFound=false;
02147                 if(!m_bResizeMode&&!m_bEditingMode&&!m_bColMovingMode&&!m_bRowMovingMode&&!m_bCellCopiengMode&&!m_bCellMovingMode)
02148                 {
02149                         if(m_regColMoving)
02150                         {
02151                                 if(PtInRegion(m_regColMoving,x,y))
02152                                 {
02153                                         SetCursor(m_hMovingCur);
02154                                         bFound=true;
02155                                 }
02156                         }
02157                         if(m_regRowMoving)
02158                         {
02159                                 if(PtInRegion(m_regRowMoving,x,y))
02160                                 {
02161                                         SetCursor(m_hMovingCur);
02162                                         bFound=true;
02163                                 }
02164                         }
02165                         m_iResizeCol=-1;
02166                         m_iResizeRow=-1;
02167                         m_bColRowOnMoveArea=bFound;
02168                 }
02169         };
02170         /*******************************************************************************/
02171         void Grid::TestCellMovingCopieng(int x,int y)
02172         {
02173                 m_bCellOnCopiengArea=false;
02174                 m_bCellOnMoveArea=false;
02175                 if(!m_bResizeMode&&!m_bEditingMode&&!m_bColMovingMode&&!m_bRowMovingMode&&!m_bCellCopiengMode&&!m_bCellMovingMode)
02176                 {
02177                         if(m_regCellCopieng)
02178                         {
02179                                 if(PtInRegion(m_regCellCopieng,x,y))
02180                                 {
02181                                         SetCursor(m_hCrossCur);
02182                                         m_bCellOnCopiengArea=true;
02183                                         m_iResizeCol=-1;
02184                                         m_iResizeRow=-1;
02185                                         return;
02186                                 }
02187                         }
02188                         if(m_regCellMoving)
02189                         {
02190                                 if(PtInRegion(m_regCellMoving,x,y))
02191                                 {
02192                                         SetCursor(m_hMovingCur);
02193                                         m_bCellOnMoveArea=true;
02194                                         m_iResizeCol=-1;
02195                                         m_iResizeRow=-1;
02196                                         return;
02197                                 }
02198                         }
02199 
02200                 }
02201         };
02202         /*******************************************************************************/
02203         void Grid::GetFixedCellByPoint(int x, int y,int* row,int* col)  
02204         {
02205                 *row=-1;
02206                 *col=-1;
02207                 RECT cellRect;
02208                 
02209                 
02210                 cellRect.top=m_iBroderWidth;
02211                 for(int r=0;r<m_nFixedRowCount;r++)
02212                 {
02213                         cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
02214                         if(y>(cellRect.top-m_iBroderWidth)&&y<(cellRect.bottom+m_iBroderWidth))
02215                                 *row=r;
02216                         cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
02217                 }               
02218 
02219                 cellRect.left=m_iBroderWidth;
02220                 for(int c=0;c<m_nFixedColCount;c++)
02221                 {
02222                         cellRect.right=cellRect.left+m_vecColsWidths[c];
02223                         if(x>(cellRect.left-m_iBroderWidth)&&x<(cellRect.right+m_iBroderWidth))
02224                                 *col=c;
02225                         cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
02226                 }
02227                 if(*row==-1||*col==-1)
02228                 {
02229                         int fixedColWidth=0;
02230                         int fixedRowHeight=0;
02231                         if(m_iMinVisCol)
02232                         {
02233                                 fixedColWidth+=m_iFixedColWidth;
02234                         }
02235                         if(m_iMinVisRow)
02236                         {
02237                                 fixedRowHeight+=m_iFixedRowHeight;
02238                         }
02239                         int startCol=0;
02240                         int startRow=0;
02241                         if(m_iMinVisCol)
02242                                 startCol=m_iMinVisCol+m_nFixedColCount;
02243                         if(m_iMinVisRow)
02244                                 startRow=m_iMinVisRow+m_nFixedRowCount;
02245 
02246                         cellRect.top=fixedRowHeight+m_iBroderWidth;
02247                         for(unsigned r=startRow;r<m_vecTable.size();r++)
02248                         {
02249                                 cellRect.left=m_iBroderWidth+fixedColWidth;
02250                                 for(unsigned c=startCol;c<m_vecTable[r].size();c++)
02251                                 {
02252                                         cellRect.right=cellRect.left+m_vecColsWidths[c];
02253                                         cellRect.bottom=cellRect.top+m_vecRowsHeights[r];
02254 
02255                                         if((int)r>=m_nFixedRowCount&&(int)c>=m_nFixedColCount)
02256                                         {
02257                                                 if(x>(cellRect.left-m_iBroderWidth)&&x<(cellRect.right+m_iBroderWidth)&&*col==-1)
02258                                                         *col=c;
02259                                                 if(y>(cellRect.top-m_iBroderWidth)&&y<(cellRect.bottom+m_iBroderWidth)&&*row==-1)
02260                                                         *row=r;
02261                                                 if((*col)>=0&&(*row)>=0)
02262                                                         return;
02263                                         }
02264 
02265                                         cellRect.left+=m_vecColsWidths[c]+m_iBroderWidth;
02266                                 }
02267                                 cellRect.top+=m_vecRowsHeights[r]+m_iBroderWidth;
02268                         }
02269                 }
02270         };
02271         /*******************************************************************************/
02272 
02273 
02274 };

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