home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / Quotes 1.0 / src / window.cp < prev    next >
Encoding:
Text File  |  1995-12-09  |  3.5 KB  |  178 lines  |  [TEXT/KAHL]

  1. #include "window.h"
  2. #include "interface.h"
  3. #include "main.h"
  4.  
  5. extern SpeechChannel chan; // from main.cp
  6. extern Ptr pSpoken; // from main.cp
  7.  
  8. QuotesWindow::QuotesWindow(QuotesDocument *pqdNew, OSErr &err) {
  9.     pwin = GetNewWindow(WIND_FORTUNE, NULL, (WindowPtr)-1);
  10.     if (pwin) {
  11.         SetWRefCon(pwin, (long)this);
  12.         pqd = pqdNew;
  13.         iQuote = -1;
  14.         pchQuote = NULL;
  15.         hteQuote = NULL;
  16.         hctrlVScroll = NULL;
  17.     } else
  18.         err = notEnoughMemoryErr;
  19. };
  20.  
  21. WindowPtr QuotesWindow::window(void) const {
  22.     return pwin;
  23. };
  24.  
  25. QuotesWindow::~QuotesWindow(void) {
  26.     if (pwin)
  27.         DisposeWindow(pwin);
  28.     if (pqd)
  29.         delete pqd;
  30. };
  31.  
  32. // return a number from 0 to limit-1
  33. int random(int limit) {
  34.     unsigned int rndSeed = Random();
  35.     rndSeed = rndSeed % limit;
  36.     if (rndSeed < 0 || rndSeed > limit-1)
  37.         return 0;
  38.     else
  39.         return rndSeed;
  40. }; // random
  41.  
  42. void QuotesWindow::select(int nQuote) {
  43.     iQuote = nQuote;
  44.     OSErr err = pqd->draw(iQuote);
  45.     if (err == noErr) {
  46.         pchQuote = pqd->text();
  47.         this->invalidate();
  48.     } else
  49.         pchQuote = NULL;
  50. };
  51.  
  52. void QuotesWindow::some(void) {
  53.     int iPrior = iQuote;
  54.     int available = this->count();
  55.     iQuote = random(available); // pick a random number
  56.     if (iPrior != iQuote) {
  57.         OSErr err = pqd->draw(iQuote);
  58.         if (err == noErr) {
  59.             pchQuote = pqd->text();
  60.             this->invalidate();
  61.         } else
  62.             pchQuote = NULL;
  63.     }
  64. };
  65.  
  66. void SetTextRect(Rect *pr) {
  67.     pr->top += 4;
  68.     pr->left += 4;
  69.     pr->bottom -= 4+15;
  70.     pr->right -= 4+15;
  71. } // SetTextRect
  72.  
  73. void QuotesWindow::invalidate(void) {
  74.     CGrafPtr pPort = GetWindowPort(pwin);
  75.     Rect rText = pPort->portRect;
  76.     SetTextRect(&rText);
  77.     SetPort(pwin);
  78.     InvalRect(&rText);
  79. };
  80.  
  81. void QuotesWindow::copy(void) {
  82.     if (pchQuote) {
  83.         OSErr err = ZeroScrap();
  84.         if (err == noErr) {
  85.             unsigned long length = GetPtrSize((Ptr)pchQuote)-1;
  86.             err = PutScrap(length, 'TEXT', pchQuote);
  87.         }
  88.     }
  89. };
  90.  
  91. void QuotesWindow::draw(void) {
  92.     SetPort(pwin);
  93.     CGrafPtr pPort = GetWindowPort(pwin);
  94.     Rect rText = pPort->portRect;
  95.     EraseRect(&rText);
  96.     SetTextRect(&rText);
  97.     unsigned long length = GetPtrSize((Ptr)pchQuote)-1;
  98.     TETextBox(pchQuote, length, &rText, teJustLeft);
  99.     DrawGrowIcon(pwin);
  100. };
  101.  
  102. int QuotesWindow::count(void) const {
  103.     return pqd->available();
  104. };
  105.  
  106. void QuotesWindow::first(void) {
  107.     iQuote = 0;
  108.     OSErr err = pqd->draw(iQuote);
  109.     if (err == noErr) {
  110.         pchQuote = pqd->text();
  111.         this->invalidate();
  112.     } else
  113.         pchQuote = NULL;
  114. };
  115.  
  116. void QuotesWindow::previous(void) {
  117.     if (iQuote > 0) {
  118.         iQuote--;
  119.         OSErr err = pqd->draw(iQuote);
  120.         if (err == noErr) {
  121.             pchQuote = pqd->text();
  122.             this->invalidate();
  123.         } else
  124.             pchQuote = NULL;
  125.     }
  126. };
  127.  
  128. void QuotesWindow::next(void) {
  129.     int available = this->count();
  130.     if (iQuote < available-1) {
  131.         iQuote++;
  132.         OSErr err = pqd->draw(iQuote);
  133.         if (err == noErr) {
  134.             pchQuote = pqd->text();
  135.             this->invalidate();
  136.         } else
  137.             pchQuote = NULL;
  138.     }
  139. };
  140.  
  141. void QuotesWindow::last(void) {
  142.     int available = this->count();
  143.     iQuote = available-1;
  144.     OSErr err = pqd->draw(iQuote);
  145.     if (err == noErr) {
  146.         pchQuote = pqd->text();
  147.         this->invalidate();
  148.     } else
  149.         pchQuote = NULL;
  150. };
  151.  
  152. int QuotesWindow::current(void) const {
  153.     return iQuote;
  154. };
  155.  
  156. void QuotesWindow::speak(void) {
  157.     if (chan == NULL) {
  158.         VoiceSpec VS;
  159.         OSErr err = GetIndVoice(voice, &VS);
  160.         if (err == noErr) {
  161.             err = NewSpeechChannel(&VS, &chan);
  162.             if (err == noErr) {
  163.                 long length = GetPtrSize((Ptr)pchQuote);
  164.                 pSpoken = NewPtr(length);
  165.                 if (pSpoken) {
  166.                     for (long i=0; i<length; i++)
  167.                         pSpoken[i] = pchQuote[i];
  168.                     err = SpeakText(chan, pSpoken, length-1);
  169.                 }
  170.             }
  171.         }
  172.     }
  173. };
  174.  
  175. Boolean QuotesWindow::save(void) const {
  176.     return pqd->save();
  177. };
  178.