home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-09 | 3.5 KB | 178 lines | [TEXT/KAHL] |
- #include "window.h"
- #include "interface.h"
- #include "main.h"
-
- extern SpeechChannel chan; // from main.cp
- extern Ptr pSpoken; // from main.cp
-
- QuotesWindow::QuotesWindow(QuotesDocument *pqdNew, OSErr &err) {
- pwin = GetNewWindow(WIND_FORTUNE, NULL, (WindowPtr)-1);
- if (pwin) {
- SetWRefCon(pwin, (long)this);
- pqd = pqdNew;
- iQuote = -1;
- pchQuote = NULL;
- hteQuote = NULL;
- hctrlVScroll = NULL;
- } else
- err = notEnoughMemoryErr;
- };
-
- WindowPtr QuotesWindow::window(void) const {
- return pwin;
- };
-
- QuotesWindow::~QuotesWindow(void) {
- if (pwin)
- DisposeWindow(pwin);
- if (pqd)
- delete pqd;
- };
-
- // return a number from 0 to limit-1
- int random(int limit) {
- unsigned int rndSeed = Random();
- rndSeed = rndSeed % limit;
- if (rndSeed < 0 || rndSeed > limit-1)
- return 0;
- else
- return rndSeed;
- }; // random
-
- void QuotesWindow::select(int nQuote) {
- iQuote = nQuote;
- OSErr err = pqd->draw(iQuote);
- if (err == noErr) {
- pchQuote = pqd->text();
- this->invalidate();
- } else
- pchQuote = NULL;
- };
-
- void QuotesWindow::some(void) {
- int iPrior = iQuote;
- int available = this->count();
- iQuote = random(available); // pick a random number
- if (iPrior != iQuote) {
- OSErr err = pqd->draw(iQuote);
- if (err == noErr) {
- pchQuote = pqd->text();
- this->invalidate();
- } else
- pchQuote = NULL;
- }
- };
-
- void SetTextRect(Rect *pr) {
- pr->top += 4;
- pr->left += 4;
- pr->bottom -= 4+15;
- pr->right -= 4+15;
- } // SetTextRect
-
- void QuotesWindow::invalidate(void) {
- CGrafPtr pPort = GetWindowPort(pwin);
- Rect rText = pPort->portRect;
- SetTextRect(&rText);
- SetPort(pwin);
- InvalRect(&rText);
- };
-
- void QuotesWindow::copy(void) {
- if (pchQuote) {
- OSErr err = ZeroScrap();
- if (err == noErr) {
- unsigned long length = GetPtrSize((Ptr)pchQuote)-1;
- err = PutScrap(length, 'TEXT', pchQuote);
- }
- }
- };
-
- void QuotesWindow::draw(void) {
- SetPort(pwin);
- CGrafPtr pPort = GetWindowPort(pwin);
- Rect rText = pPort->portRect;
- EraseRect(&rText);
- SetTextRect(&rText);
- unsigned long length = GetPtrSize((Ptr)pchQuote)-1;
- TETextBox(pchQuote, length, &rText, teJustLeft);
- DrawGrowIcon(pwin);
- };
-
- int QuotesWindow::count(void) const {
- return pqd->available();
- };
-
- void QuotesWindow::first(void) {
- iQuote = 0;
- OSErr err = pqd->draw(iQuote);
- if (err == noErr) {
- pchQuote = pqd->text();
- this->invalidate();
- } else
- pchQuote = NULL;
- };
-
- void QuotesWindow::previous(void) {
- if (iQuote > 0) {
- iQuote--;
- OSErr err = pqd->draw(iQuote);
- if (err == noErr) {
- pchQuote = pqd->text();
- this->invalidate();
- } else
- pchQuote = NULL;
- }
- };
-
- void QuotesWindow::next(void) {
- int available = this->count();
- if (iQuote < available-1) {
- iQuote++;
- OSErr err = pqd->draw(iQuote);
- if (err == noErr) {
- pchQuote = pqd->text();
- this->invalidate();
- } else
- pchQuote = NULL;
- }
- };
-
- void QuotesWindow::last(void) {
- int available = this->count();
- iQuote = available-1;
- OSErr err = pqd->draw(iQuote);
- if (err == noErr) {
- pchQuote = pqd->text();
- this->invalidate();
- } else
- pchQuote = NULL;
- };
-
- int QuotesWindow::current(void) const {
- return iQuote;
- };
-
- void QuotesWindow::speak(void) {
- if (chan == NULL) {
- VoiceSpec VS;
- OSErr err = GetIndVoice(voice, &VS);
- if (err == noErr) {
- err = NewSpeechChannel(&VS, &chan);
- if (err == noErr) {
- long length = GetPtrSize((Ptr)pchQuote);
- pSpoken = NewPtr(length);
- if (pSpoken) {
- for (long i=0; i<length; i++)
- pSpoken[i] = pchQuote[i];
- err = SpeakText(chan, pSpoken, length-1);
- }
- }
- }
- }
- };
-
- Boolean QuotesWindow::save(void) const {
- return pqd->save();
- };
-