MainTable.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // MainTable.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "MainTable.h"
  6. #include "DatabaseUtilities.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMainTable
  14. IMPLEMENT_DYNAMIC(CMainTable, CDaoRecordset)
  15. CMainTable::CMainTable(CDaoDatabase* pdb)
  16. : CDaoRecordset(pdb)
  17. {
  18. //{{AFX_FIELD_INIT(CMainTable)
  19. m_lID = 0;
  20. m_lDate = 0;
  21. m_strText = _T("");
  22. m_lShortCut = 0;
  23. m_lDontAutoDelete = 0;
  24. m_lTotalCopySize = 0;
  25. m_bIsGroup = FALSE;
  26. m_lParentID = 0;
  27. m_dOrder = 0;
  28. m_lDataID = 0;
  29. m_nFieldCount = m_nFields = 10;
  30. //}}AFX_FIELD_INIT
  31. m_nDefaultType = dbOpenDynaset;
  32. m_bBindFields = true;
  33. }
  34. CString CMainTable::GetDefaultDBName()
  35. {
  36. return GetDBName();
  37. }
  38. CString CMainTable::GetDefaultSQL()
  39. {
  40. return _T("[Main]");
  41. }
  42. void CMainTable::DoFieldExchange(CDaoFieldExchange* pFX)
  43. {
  44. // make sure this isn't called when we aren't using bound fields
  45. VERIFY( m_bBindFields == true );
  46. //{{AFX_FIELD_MAP(CMainTable)
  47. pFX->SetFieldType(CDaoFieldExchange::outputColumn);
  48. DFX_Long(pFX, _T("[lID]"), m_lID);
  49. DFX_Long(pFX, _T("[lDate]"), m_lDate);
  50. DFX_Text(pFX, _T("[mText]"), m_strText);
  51. DFX_Long(pFX, _T("[lShortCut]"), m_lShortCut);
  52. DFX_Long(pFX, _T("[lDontAutoDelete]"), m_lDontAutoDelete);
  53. DFX_Long(pFX, _T("[lTotalCopySize]"), m_lTotalCopySize);
  54. // GROUPS
  55. DFX_Bool(pFX, _T("[bIsGroup]"), m_bIsGroup);
  56. DFX_Long(pFX, _T("[lParentID]"), m_lParentID);
  57. DFX_Double(pFX, _T("[dOrder]"), m_dOrder);
  58. // sharing data
  59. DFX_Long(pFX, _T("[lDataID]"), m_lDataID);
  60. //}}AFX_FIELD_MAP
  61. }
  62. void CMainTable::Open(int nOpenType , LPCTSTR lpszSql , int nOptions)
  63. {
  64. m_pDatabase = theApp.EnsureOpenDB();
  65. OnQuery();
  66. CDaoRecordset::Open(nOpenType, lpszSql, nOptions);
  67. }
  68. void CMainTable::Requery()
  69. {
  70. OnQuery();
  71. CDaoRecordset::Requery();
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CMainTable member functions
  75. CString CMainTable::GetDisplayText( int nMaxLines )
  76. {
  77. CString text = m_strText;
  78. // assign tabs to 2 spaces (rather than the default 8)
  79. text.Replace("\t", " ");
  80. if( g_Opt.m_bDescShowLeadingWhiteSpace )
  81. return text;
  82. // else, remove the leading indent from every line.
  83. // get the lines
  84. CString token;
  85. CStringArray tokens;
  86. CTokenizer tokenizer(text,"\r\n");
  87. for( int nLines=0; nLines < nMaxLines && tokenizer.Next(token); nLines++ )
  88. {
  89. tokens.Add( token );
  90. }
  91. // remove each line's indent
  92. char chFirst;
  93. CString line;
  94. int count = tokens.GetSize();
  95. text = "";
  96. for( int i=0; i < count; i++ )
  97. {
  98. line = tokens.ElementAt(i);
  99. chFirst = line.GetAt(0);
  100. if( chFirst == ' ' || chFirst == '\t' )
  101. {
  102. text += "» "; // show indication that the line is modified
  103. line.TrimLeft();
  104. text += line;
  105. }
  106. else
  107. text += line;
  108. text += "\n";
  109. }
  110. return text;
  111. }
  112. // assigns the new autoincr ID to m_lID
  113. void CMainTable::AddNew()
  114. {
  115. CDaoRecordset::AddNew();
  116. // get the new, automatically assigned ID
  117. COleVariant varID;
  118. GetFieldValue("lID", varID);
  119. m_lID = varID.lVal;
  120. }
  121. void CMainTable::OnQuery()
  122. {
  123. }
  124. bool CMainTable::SetBindFields(bool bVal)
  125. {
  126. bool bOld = m_bBindFields;
  127. m_bBindFields = bVal;
  128. if(m_bBindFields)
  129. m_nFields = m_nFieldCount;
  130. else
  131. m_nFields = 0;
  132. return bOld;
  133. }
  134. // copies the current source record to this current record
  135. void CMainTable::CopyRec(CMainTable& source)
  136. {
  137. // m_lID = source.m_lID;
  138. m_lDate = source.m_lDate;
  139. m_strText = source.m_strText;
  140. // m_lShortCut = source.m_lShortCut; // don't copy the shortcut
  141. m_lDontAutoDelete = source.m_lDontAutoDelete;
  142. m_lTotalCopySize = source.m_lTotalCopySize;
  143. m_lDataID = source.m_lDataID;
  144. m_bIsGroup = source.m_bIsGroup;
  145. m_lParentID = source.m_lParentID;
  146. m_dOrder = source.m_dOrder;
  147. }
  148. // makes a new copy of the current record and moves to the copy record
  149. void CMainTable::NewCopyRec()
  150. {
  151. if( IsBOF() || IsEOF() )
  152. return;
  153. CMainTable temp;
  154. temp.CopyRec( *this ); // temporary copy
  155. AddNew(); // overridden to fetch the autoincr lID
  156. CopyRec( temp );
  157. Update();
  158. }
  159. // only deletes from Main
  160. BOOL CMainTable::DeleteAll()
  161. {
  162. BOOL bRet = FALSE;
  163. try
  164. {
  165. theApp.EnsureOpenDB();
  166. theApp.m_pDatabase->Execute("DELETE * FROM Main", dbFailOnError);
  167. bRet = TRUE;
  168. }
  169. catch(CDaoException* e)
  170. {
  171. AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  172. e->Delete();
  173. }
  174. return bRet;
  175. }
  176. void CMainTable::LoadAcceleratorKeys( CAccels& accels )
  177. {
  178. CMainTable recset;
  179. try
  180. {
  181. recset.Open("SELECT * FROM Main WHERE lShortCut > 0");
  182. CAccel a;
  183. while(!recset.IsEOF())
  184. {
  185. a.Cmd = recset.m_lID;
  186. a.Key = recset.m_lShortCut;
  187. accels.AddAccel( a );
  188. recset.MoveNext();
  189. }
  190. }
  191. catch(CDaoException* e)
  192. {
  193. e->Delete();
  194. }
  195. }
  196. /*
  197. //HACCEL CMainTable::LoadAcceleratorKeys()
  198. //{
  199. // CMainTable recset;
  200. //
  201. // try
  202. // {
  203. // recset.Open("SELECT * FROM Main WHERE lShortCut > 0");
  204. //
  205. // CArray<ACCEL, ACCEL> keys;
  206. //
  207. // while(!recset.IsEOF())
  208. // {
  209. // ACCEL me;
  210. // me.cmd = (USHORT)recset.m_lID;
  211. // me.fVirt = 0;
  212. // if( HIBYTE(recset.m_lShortCut) & HOTKEYF_SHIFT ) me.fVirt |= FSHIFT;
  213. // if( HIBYTE(recset.m_lShortCut) & HOTKEYF_CONTROL ) me.fVirt |= FCONTROL;
  214. // if( HIBYTE(recset.m_lShortCut) & HOTKEYF_ALT ) me.fVirt |= FALT;
  215. // me.fVirt |= FVIRTKEY;
  216. // me.key = LOBYTE(recset.m_lShortCut);
  217. //
  218. // keys.Add(me);
  219. //
  220. // recset.MoveNext();
  221. // }
  222. //
  223. // if(keys.GetSize() > 0)
  224. // return CreateAcceleratorTable(keys.GetData(), keys.GetSize());
  225. // }
  226. // catch(CDaoException* e)
  227. // {
  228. // e->Delete();
  229. // }
  230. //
  231. // return NULL;
  232. //}
  233. */
  234. void CMainTable::Open(LPCTSTR lpszFormat,...)
  235. {
  236. m_pDatabase = theApp.EnsureOpenDB();
  237. CString csText;
  238. va_list vlist;
  239. ASSERT(AfxIsValidString(lpszFormat));
  240. va_start(vlist,lpszFormat);
  241. csText.FormatV(lpszFormat,vlist);
  242. va_end(vlist);
  243. Open(AFX_DAO_USE_DEFAULT_TYPE, csText, 0);
  244. }
  245. /////////////////////////////////////////////////////////////////////////////
  246. // CMainTable diagnostics
  247. #ifdef _DEBUG
  248. void CMainTable::AssertValid() const
  249. {
  250. CDaoRecordset::AssertValid();
  251. }
  252. void CMainTable::Dump(CDumpContext& dc) const
  253. {
  254. CDaoRecordset::Dump(dc);
  255. }
  256. #endif //_DEBUG