home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / EDITVIEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  6.0 KB  |  249 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   include\owl\editview.cpp
  4. //   Implements class TEditView
  5. //----------------------------------------------------------------------------
  6. #pragma hdrignore SECTION
  7. #include <owl\owlpch.h>
  8. #include <owl\editview.h>
  9. #include <owl\docview.rc>
  10. #include <owl\editview.rc>
  11.  
  12. DIAG_DECLARE_GROUP(OwlDocView);        // General Doc/View diagnostic group
  13.  
  14. #if !defined(SECTION) || SECTION == 1
  15.  
  16. #define MAX_EDIT_BUF (30000)  // can't add new chars after 30,000 bytes
  17.  
  18. //
  19. //  class TEditView
  20. //  ----- ---------
  21. //
  22. DEFINE_RESPONSE_TABLE1(TEditView, TEditSearch)
  23.   EV_VN_DOCCLOSED,
  24.   EV_VN_ISWINDOW,
  25.   EV_VN_ISDIRTY,
  26.   EV_VN_COMMIT,
  27.   EV_VN_REVERT,
  28.   EV_WM_NCDESTROY,
  29. END_RESPONSE_TABLE;
  30.  
  31. TEditView::TEditView(TDocument& doc, TWindow* parent)
  32.          : TEditSearch(parent, GetNextViewId()), TView(doc), Origin(0)
  33. {
  34.   Attr.AccelTable = IDA_EDITVIEW;
  35.   SetViewMenu(new TMenuDescr(IDM_EDITVIEW,0,2,0,0,0,1));
  36. }
  37.  
  38. void
  39. TEditView::EvNCDestroy()
  40. {
  41. #if !defined(__WIN32__)
  42.   HGLOBAL hdl = (HGLOBAL)GlobalHandle((UINT)GetWindowWord(GWW_HINSTANCE));
  43. #endif
  44.   TEditSearch::EvNCDestroy();// call TWindow::EvNCDestroy, this may be deleted
  45. #if !defined(__WIN32__)
  46.   if (hdl) {
  47.     GlobalUnlock(hdl);
  48.     GlobalFree(hdl);
  49.   }
  50. #endif
  51. }
  52.  
  53. TEditView::~TEditView()
  54. {
  55. }
  56.  
  57. BOOL
  58. TEditView::VnDocClosed(int omode)
  59. {
  60.   int top;
  61.   UINT selbeg;
  62.   UINT selend;
  63.  
  64.   if (VnIsDirty() || !(omode & ofWrite))  // make sure someone else's write
  65.     return FALSE;
  66.   top = GetFirstVisibleLine();
  67.   TEdit::GetSelection(selbeg, selend);
  68.   TEdit::Clear();
  69.   LoadData();
  70.   Scroll(0, top);
  71.   TEdit::SetSelection(selbeg, selend);
  72.   return TRUE;
  73. }
  74.  
  75. BOOL
  76. TEditView::LoadData()
  77. {
  78.   unsigned long total;
  79.   UINT count, len;
  80.   LPSTR buf;
  81.   istream* inStream;
  82.   BOOL status;
  83.  
  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.   total = inStream->tellg();
  90.   inStream->seekg(0L, ios::beg);
  91.   if (total > MAX_EDIT_BUF)
  92.     count = MAX_EDIT_BUF;
  93.   else
  94.     count = (UINT)total;
  95.   buf = LockBuffer(count + 1);
  96.   if (!buf){
  97.     delete inStream;
  98. //    THROW( TXOutOfMemory() );
  99.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  100.     return FALSE;
  101.   }
  102. #if defined(__SMALL__) || defined(__MEDIUM__)
  103.   char xbuf[512];
  104.   UINT pos = 0;
  105.   do {
  106.     len = (count-pos > sizeof(xbuf) ? sizeof(xbuf) : count-pos);
  107.     inStream->read(xbuf, len);
  108.     if (inStream->gcount() != len)
  109.       break;  // if error test again below
  110.     memcpy(buf+pos, (LPSTR)xbuf, len);
  111.     pos += len;
  112.   } while (pos < count);
  113. #else
  114.   inStream->read(buf, len = count);
  115. #endif
  116.   status = (inStream->gcount() == len);
  117.   buf[count] = 0;  // null 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.   return status;
  123. }
  124.  
  125. BOOL
  126. TEditView::Create()
  127. {
  128.   TRY {
  129.     TEditSearch::Create();   // throws exception TWindow::TXWindow
  130.   }
  131.   CATCH( (TXOwl& x) {
  132.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  133.     NotOK();
  134.     return TRUE;   // cannot return FALSE - throws another exception
  135.   })
  136.   if (Doc->GetDocPath() == 0) {  
  137.     return TRUE;           // new file, no data to display
  138.   }
  139.   if (!LoadData())
  140.     NotOK();
  141.   return TRUE;
  142. }
  143.  
  144. void
  145. TEditView::PerformCreate(int menuOrId)
  146. {
  147.   HINSTANCE hInst;
  148. #if defined(__WIN32__)
  149.   hInst = *GetModule();
  150. #else
  151.   HGLOBAL hdl;
  152.   hdl = ::GlobalAlloc(GHND, 256);  // will grow as needed
  153.   if (!hdl) {
  154. //  Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  155. //  return;
  156.     THROW( TXOutOfMemory() );
  157.   }
  158.   WORD editDS = FP_SEG(GlobalLock(hdl));
  159.   LocalInit(editDS, 0, 0);
  160.   GlobalUnlock(hdl);
  161.   hInst = (HINSTANCE)hdl;
  162. #endif
  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.   UINT count;
  179.   LPSTR buf;
  180.   ostream* outStream;
  181.   BOOL status = FALSE;
  182.  
  183.   if (!force && !(VnIsDirty()))
  184.     return TRUE;
  185.   if ((outStream = Doc->OutStream(ios::out | ios::binary)) == 0) {
  186.     Doc->PostError(IDS_UNABLEOPEN, MB_OK);
  187.     return FALSE;
  188.   }
  189.   outStream->seekp(Origin);
  190.   buf = LockBuffer();
  191.   if (buf) {
  192.     count = strlen(buf);
  193. #if defined(__SMALL__) || defined(__MEDIUM__)
  194.     char xbuf[512];
  195.     UINT len;
  196.     UINT pos = 0;
  197.     do {
  198.       len = count-pos > sizeof(xbuf) ? sizeof(xbuf) : count-pos;
  199.       memcpy((LPSTR)xbuf, buf+pos, len);
  200.       outStream->write(xbuf, len);
  201.       if (!outStream->good())
  202.         break;  // if error test again below
  203.       pos += len;
  204.     } while (pos < count);
  205. #else
  206.     outStream->write(buf, count);
  207. #endif
  208.     status = outStream->good();
  209.     UnlockBuffer(buf);
  210.     ClearModify();   // reset edit control
  211.   }
  212.   delete outStream;
  213.   if (!status)
  214.     Doc->PostError(IDS_WRITEERROR, MB_OK);
  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.