home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / EDITVIEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  6.0 KB  |  249 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implements class TEditView
  6. //----------------------------------------------------------------------------
  7. #pragma hdrignore SECTION
  8. #include <owl/owlpch.h>
  9. #include <owl/editview.h>
  10. #include <owl/docview.rh>
  11. #include <owl/editview.rh>
  12.  
  13. DIAG_DECLARE_GROUP(OwlDocView);        // General Doc/View diagnostic group
  14.  
  15. #if !defined(SECTION) || SECTION == 1
  16.  
  17. #define MAX_EDIT_BUF (30000)  // can't add new chars after 30,000 bytes
  18.  
  19. //
  20. //  class TEditView
  21. //  ----- ---------
  22. //
  23. DEFINE_RESPONSE_TABLE1(TEditView, TEditSearch)
  24.   EV_VN_DOCCLOSED,
  25.   EV_VN_ISWINDOW,
  26.   EV_VN_ISDIRTY,
  27.   EV_VN_COMMIT,
  28.   EV_VN_REVERT,
  29.   EV_WM_NCDESTROY,
  30. END_RESPONSE_TABLE;
  31.  
  32. TEditView::TEditView(TDocument& doc, TWindow* parent)
  33. :
  34.   TEditSearch(parent, GetNextViewId()),
  35.   TView(doc),
  36.   Origin(0)
  37. {
  38.   Attr.AccelTable = IDA_EDITVIEW;
  39.   if (::FindResource(*GetModule(), TResId(IDM_EDITVIEW), RT_MENU))
  40.     SetViewMenu(new TMenuDescr(IDM_EDITVIEW, 0,2,0,0,0,1, GetModule()));
  41. }
  42.  
  43. void
  44. TEditView::EvNCDestroy()
  45. {
  46. #if !defined(BI_PLAT_WIN32)
  47.   HGLOBAL hdl = (HGLOBAL)::GlobalHandle((uint)GetWindowWord(GWW_HINSTANCE));
  48. #endif
  49.   TEditSearch::EvNCDestroy();// call TWindow::EvNCDestroy, this may be deleted
  50. #if !defined(BI_PLAT_WIN32)
  51.   if (hdl) {
  52.     ::GlobalUnlock(hdl);
  53.     ::GlobalFree(hdl);
  54.   }
  55. #endif
  56. }
  57.  
  58. TEditView::~TEditView()
  59. {
  60. }
  61.  
  62. bool
  63. TEditView::VnDocClosed(int omode)
  64. {
  65.   if (VnIsDirty() || !(omode & ofWrite))  // make sure someone else's write
  66.     return false;
  67.  
  68.   int top = GetFirstVisibleLine();
  69.   uint selbeg;
  70.   uint selend;
  71.   TEdit::GetSelection(selbeg, selend);
  72.   TEdit::Clear();
  73.   LoadData();
  74.   Scroll(0, top);
  75.   TEdit::SetSelection(selbeg, selend);
  76.  
  77.   return true;
  78. }
  79.  
  80. bool
  81. TEditView::LoadData()
  82. {
  83.   istream* inStream;
  84.   if ((inStream = Doc->InStream(ios::in | ios::binary)) == 0) {
  85.     Doc->PostError(IDS_UNABLEOPEN, MB_OK);
  86.     return false;
  87.   }
  88.   inStream->seekg(0L, ios::end);
  89.   unsigned long total = inStream->tellg();
  90.   inStream->seekg(0L, ios::beg);
  91.   uint count = (total > MAX_EDIT_BUF) ? MAX_EDIT_BUF : (uint)total;
  92.   char far* buf = LockBuffer(count + 1);
  93.   if (!buf) {
  94.     delete inStream;
  95. //    THROW( TXOutOfMemory() );
  96.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  97.     return false;
  98.   }
  99.  
  100.   uint len;
  101. #if defined(BI_DATA_NEAR)
  102.   char xbuf[512];
  103.   uint pos = 0;
  104.   do {
  105.     len = (count-pos > sizeof(xbuf) ? sizeof(xbuf) : count-pos);
  106.     inStream->read(xbuf, len);
  107.     if (inStream->gcount() != len)
  108.       break;  // if error test again below
  109.     memcpy(buf+pos, (char far*)xbuf, len);
  110.     pos += len;
  111.   } while (pos < count);
  112. #else
  113.   inStream->read(buf, len = count);
  114. #endif
  115.  
  116.   bool status = (inStream->gcount() == len);
  117.   buf[count] = 0;    // 0 terminate buffer
  118.   UnlockBuffer(buf, true);
  119.   delete inStream;   // close file in case process switch
  120.   if (!status)
  121.     Doc->PostError(IDS_READERROR, MB_OK);
  122.  
  123.   return status;
  124. }
  125.  
  126. bool
  127. TEditView::Create()
  128. {
  129.   TRY {
  130.     TEditSearch::Create();   // throws exception TWindow::TXWindow
  131.   }
  132.   CATCH( (TXOwl& x) {
  133.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  134.     NotOK();
  135.     return true;   // cannot return false - throws another exception
  136.   })
  137.   if (Doc->GetDocPath() == 0) {  
  138.     return true;           // new file, no data to display
  139.   }
  140.   if (!LoadData())
  141.     NotOK();
  142.   return true;
  143. }
  144.  
  145. void
  146. TEditView::PerformCreate(int menuOrId)
  147. {
  148. #if defined(BI_PLAT_WIN32)
  149.   HINSTANCE hInst = *GetModule();
  150. #else
  151.   HGLOBAL hdl = ::GlobalAlloc(GHND, 256);  // will grow as needed
  152.   if (!hdl) {
  153. //  Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  154. //  return;
  155.     THROW( TXOutOfMemory() );
  156.   }
  157.   uint16 editDS = FP_SEG(::GlobalLock(hdl));
  158.   ::LocalInit(editDS, 0, 0);
  159.   ::GlobalUnlock(hdl);
  160.   HINSTANCE hInst = (HINSTANCE)hdl;
  161. #endif
  162.  
  163. // if (doc readonly || size too large) Attr.Style |= ES_READONLY;
  164.   HWindow = CreateWindowEx(Attr.ExStyle,
  165.                            GetClassName(),
  166.                            Title,
  167.                            Attr.Style,
  168.                            Attr.X, Attr.Y, Attr.W, Attr.H,
  169.                            Parent ? Parent->HWindow : 0,
  170.                            (HMENU)menuOrId,
  171.                            hInst,
  172.                            Attr.Param);
  173. }
  174.  
  175. bool
  176. TEditView::VnCommit(bool force)
  177. {
  178.   if (!force && !(VnIsDirty()))
  179.     return true;
  180.  
  181.   ostream* outStream;
  182.   if ((outStream = Doc->OutStream(ios::out | ios::binary)) == 0) {
  183.     Doc->PostError(IDS_UNABLEOPEN, MB_OK);
  184.     return false;
  185.   }
  186.   outStream->seekp(Origin);
  187.  
  188.   bool status = false;
  189.   char far* buf = LockBuffer();
  190.   if (buf) {
  191.     uint count = strlen(buf);
  192. #if defined(BI_DATA_NEAR)
  193.     char xbuf[512];
  194.     uint len;
  195.     uint pos = 0;
  196.     do {
  197.       len = count-pos > sizeof(xbuf) ? sizeof(xbuf) : count-pos;
  198.       memcpy((char far*)xbuf, buf+pos, len);
  199.       outStream->write(xbuf, len);
  200.       if (!outStream->good())
  201.         break;  // if error test again below
  202.       pos += len;
  203.     } while (pos < count);
  204. #else
  205.     outStream->write(buf, count);
  206. #endif
  207.     status = ToBool(outStream->good());
  208.     UnlockBuffer(buf);
  209.     ClearModify();   // reset edit control
  210.   }
  211.   delete outStream;
  212.   if (!status)
  213.     Doc->PostError(IDS_WRITEERROR, MB_OK);
  214.  
  215.   return status;
  216. }
  217.  
  218. bool
  219. TEditView::VnRevert(bool clear)
  220. {
  221.   TEdit::Clear();
  222.   ClearModify();   // reset edit control
  223.   return clear ? true : LoadData();
  224. }
  225.  
  226. #endif
  227. #if !defined(SECTION) || SECTION == 2
  228.  
  229. IMPLEMENT_STREAMABLE2(TEditView, TEditSearch, TView);
  230.  
  231. void*
  232. TEditView::Streamer::Read(ipstream& is, uint32 /*version*/) const
  233. {
  234.   ReadBaseObject((TEditSearch*)GetObject(), is);
  235.   ReadBaseObject((TView*)GetObject(), is);
  236.   is >> GetObject()->Origin;
  237.   return GetObject();
  238. }
  239.  
  240. void
  241. TEditView::Streamer::Write(opstream& os) const
  242. {
  243.   WriteBaseObject((TEditSearch*)GetObject(), os);
  244.   WriteBaseObject((TView*)GetObject(), os);
  245.   os << GetObject()->Origin;
  246. }
  247.  
  248. #endif
  249.