FloatEdit.cpp

00001 /* 
00002  *      Copyright (C) 2003-2005 Gabest
00003  *      http://www.gabest.org
00004  *
00005  *  This Program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2, or (at your option)
00008  *  any later version.
00009  *   
00010  *  This Program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  *  GNU General Public License for more details.
00014  *   
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with GNU Make; see the file COPYING.  If not, write to
00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
00018  *  http://www.gnu.org/copyleft/gpl.html
00019  *
00020  */
00021 
00022 #include "stdafx.h"
00023 #include "floatedit.h"
00024 
00025 // CFloatEdit
00026 
00027 IMPLEMENT_DYNAMIC(CFloatEdit, CEdit)
00028 
00029 bool CFloatEdit::GetFloat(float& f)
00030 {
00031         CString s;
00032         GetWindowText(s);
00033         return(_stscanf(s, _T("%f"), &f) == 1);
00034 }
00035 
00036 double CFloatEdit::operator = (double d)
00037 {
00038         CString s;
00039         s.Format(_T("%.4f"), d);
00040         SetWindowText(s);
00041         return(d);
00042 }
00043 
00044 CFloatEdit::operator double()
00045 {
00046         CString s;
00047         GetWindowText(s);
00048         float f;
00049         return(_stscanf(s, _T("%f"), &f) == 1 ? f : 0);
00050 }
00051 
00052 BEGIN_MESSAGE_MAP(CFloatEdit, CEdit)
00053         ON_WM_CHAR()
00054 END_MESSAGE_MAP()
00055 
00056 void CFloatEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
00057 {
00058         if(!(nChar >= '0' && nChar <= '9' || nChar == '.' || nChar == '\b'))
00059                 return;
00060 
00061         CString str;
00062         GetWindowText(str);
00063 
00064         if(nChar == '.' && (str.Find('.') >= 0 || str.IsEmpty()))
00065                 return;
00066 
00067         int nStartChar, nEndChar;
00068         GetSel(nStartChar, nEndChar);
00069 
00070         if(nChar == '\b' && nStartChar <= 0)
00071                 return;
00072 
00073         CEdit::OnChar(nChar, nRepCnt, nFlags);
00074 }
00075 
00076 // CIntEdit
00077 
00078 IMPLEMENT_DYNAMIC(CIntEdit, CEdit)
00079 
00080 BEGIN_MESSAGE_MAP(CIntEdit, CEdit)
00081         ON_WM_CHAR()
00082 END_MESSAGE_MAP()
00083 
00084 void CIntEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
00085 {
00086         if(!(nChar >= '0' && nChar <= '9' || nChar == '-' || nChar == '\b'))
00087                 return;
00088 
00089         CString str;
00090         GetWindowText(str);
00091 
00092         if(nChar == '-' && !str.IsEmpty() && str[0] == '-')
00093                 return;
00094 
00095         int nStartChar, nEndChar;
00096         GetSel(nStartChar, nEndChar);
00097 
00098         if(nChar == '\b' && nStartChar <= 0)
00099                 return;
00100 
00101         if(nChar == '-' && (nStartChar != 0 || nEndChar != 0))
00102                 return;
00103 
00104         CEdit::OnChar(nChar, nRepCnt, nFlags);
00105 }
00106 
00107 // CHexEdit
00108 
00109 IMPLEMENT_DYNAMIC(CHexEdit, CEdit)
00110 
00111 bool CHexEdit::GetDWORD(DWORD& dw)
00112 {
00113         CString s;
00114         GetWindowText(s);
00115         return(_stscanf(s, _T("%x"), &dw) == 1);
00116 }
00117 
00118 DWORD CHexEdit::operator = (DWORD dw)
00119 {
00120         CString s;
00121         s.Format(_T("%08x"), dw);
00122         SetWindowText(s);
00123         return(dw);
00124 }
00125 
00126 CHexEdit::operator DWORD()
00127 {
00128         CString s;
00129         GetWindowText(s);
00130         DWORD dw;
00131         return(_stscanf(s, _T("%x"), &dw) == 1 ? dw : 0);
00132 }
00133 
00134 BEGIN_MESSAGE_MAP(CHexEdit, CEdit)
00135         ON_WM_CHAR()
00136 END_MESSAGE_MAP()
00137 
00138 void CHexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
00139 {
00140         if(!(nChar >= 'A' && nChar <= 'F' || nChar >= 'a' && nChar <= 'f'
00141         || nChar >= '0' && nChar <= '9' || nChar == '\b'))
00142                 return;
00143 
00144         CString str;
00145         GetWindowText(str);
00146 
00147         int nStartChar, nEndChar;
00148         GetSel(nStartChar, nEndChar);
00149 
00150         if(nChar == '\b' && nStartChar <= 0)
00151                 return;
00152 
00153         if(nChar != '\b' && nEndChar - nStartChar == 0 && str.GetLength() >= 8)
00154                 return;
00155 
00156         CEdit::OnChar(nChar, nRepCnt, nFlags);
00157 }

Generated on Tue Dec 13 14:46:50 2005 for guliverkli by  doxygen 1.4.5