home *** CD-ROM | disk | FTP | other *** search
- /* ============
- * PedWindow.cc
- * ============
- */
-
- #include "PedestalDebugging.h"
-
- #include <Windows.h>
-
- #include "PedWindow.hh"
- #include "PedPane.hh"
- #include "PedAgent.hh"
-
- PedWindow::PedWindow()
- : mPersistent(false), macWindow(NULL), mStorage(NULL), mProcID(documentProc)
- , mPane(NULL), mAgent(NULL)
- {
- Rect bounds = {100, 200, 300, 400};
- mBounds = bounds;
- unsigned char *title = "\pUntitled";
- ::BlockMoveData(title, mTitle, title[0] + 1);
- }
-
- PedWindow::PedWindow(PedAgent *inAgent)
- : mPersistent(false), macWindow(NULL), mStorage(NULL), mProcID(documentProc)
- , mPane(NULL), mAgent(inAgent)
- {
- inAgent->retain();
- inAgent->SetWindow(this);
-
- Rect bounds = {100, 200, 300, 400};
- mBounds = bounds;
- unsigned char *title = "\pUntitled";
- ::BlockMoveData(title, mTitle, title[0] + 1);
- }
-
- PedWindow::~PedWindow()
- {
- if (mAgent) {
- mAgent->release();
- mAgent = NULL;
- }
- if (mPane) {
- mPane->release();
- mPane = NULL;
- }
- #if 0
- if (!mDeleting) {
- mDeleting = true;
- if (macWindow)
- Close();
- }
- #endif
- // As long as a window is open, it should be retained,
- // and we should not be destructed outside of release().
- if (macWindow) {
- DebugBeep();
- throw;
- }
- }
-
- void
- PedWindow::Open()
- {
- if (!macWindow) {
- retain();
- // TODO: Support monochrome machines
- macWindow = ::NewCWindow(NULL, &mBounds, mTitle, true, mProcID, WindowPtr(-1), true, (long)this);
- ((WindowPeek)macWindow)->windowKind = kPedestalWindowKind;
- //Rect clip = macWindow->portRect;
- //::ClipRect(&clip);
- }
- Focus();
- Select();
- if (mPane)
- mPane->Open();
- }
-
- void
- PedWindow::Open(const Rect &inBounds, const Str255 inTitle)
- {
- SetBounds(inBounds);
- SetTitle(inTitle);
- Open();
- }
-
- void
- PedWindow::Close()
- {
- if (mPane)
- mPane->Close();
- if (macWindow) {
- if (mStorage) {
- ::CloseWindow(macWindow);
- // delete mStorage;
- // mStorage = NULL;
- } else
- ::DisposeWindow(macWindow);
- macWindow = NULL;
- release();
- }
- }
-
- void
- PedWindow::Dispose()
- {
- retain(); // Not sure if this is necessary, but it can't hurt
- Close();
- if (mPane) {
- mPane->Dispose();
- SetPane(NULL);
- }
- if (mAgent) mAgent->release();
- mAgent = NULL;
- release();
- }
-
- PedPane *
- PedWindow::Pane()
- {
- return mPane;
- }
-
- void
- PedWindow::SetPane(PedPane *inPane)
- {
- PedPane *oldPane = mPane;
- if (inPane) inPane->retain();
- mPane = inPane;
- if (oldPane) oldPane->release();
-
- if (mPane)
- mPane->Resize(mBounds.right - mBounds.left, mBounds.bottom - mBounds.top);
- }
-
- void
- PedWindow::Focus()
- {
- if (macWindow)
- ::SetPort(macWindow);
- }
-
- void
- PedWindow::Activate()
- {
- Focus();
-
- if (macWindow) {
- Rect invalid;
- invalid = macWindow->portRect;
- invalid.top = invalid.bottom - 15;
- ::InvalRect(&invalid);
-
- invalid = macWindow->portRect;
- invalid.left = invalid.right - 15;
- invalid.bottom = invalid.bottom - 15;
- ::InvalRect(&invalid);
- }
-
- if (mPane)
- mPane->Activate();
- }
-
- void
- PedWindow::Deactivate()
- {
- Focus();
-
- if (macWindow) {
- Rect invalid;
- invalid = macWindow->portRect;
- invalid.top = invalid.bottom - 15;
- ::InvalRect(&invalid);
-
- invalid = macWindow->portRect;
- invalid.left = invalid.right - 15;
- invalid.bottom = invalid.bottom - 15;
- ::InvalRect(&invalid);
- }
-
- if (mPane)
- mPane->Deactivate();
- }
-
- void
- PedWindow::Refresh()
- {
- if (macWindow) {
- ::SetPort(macWindow);
- ::InvalRect(&macWindow->portRect);
- }
- }
-
- void
- PedWindow::Update()
- {
- if (!macWindow) return;
-
- ::SetPort(macWindow);
- ::BeginUpdate(macWindow);
- //DrawContent();
- Rect clip = macWindow->portRect;
- ::ClipRect(&clip);
- if (mPane) {
- ::EraseRect(&macWindow->portRect);
- ::DrawGrowIcon(macWindow);
- clip.right -= 15;
- clip.bottom -= 15;
- //::ClipRect(&clip);
- mPane->DrawContent();
- } else {
- // This will flicker, but we're just pointing out paneless windows.
- ::PaintRect(&macWindow->portRect);
- clip.left = clip.right - 15;
- clip.top = clip.bottom - 15;
- //::ClipRect(&clip);
- ::DrawGrowIcon(macWindow);
- clip = macWindow->portRect;
- //::ClipRect(&clip);
- }
- ::EndUpdate(macWindow);
- }
-
- #if 0
- void
- PedWindow::DrawContent()
- {
- if (macWindow) {
- Rect clip = macWindow->portRect;
- ::ClipRect(&clip);
- ::EraseRect(&macWindow->portRect);
- ::DrawGrowIcon(macWindow);
- clip.right -= 15;
- clip.bottom -= 15;
- ::ClipRect(&clip);
- if (mPane)
- mPane->DrawContent();
- }
- }
- }
- #endif
-
- void
- PedWindow::Select()
- {
- if (!macWindow) return;
-
- ::SelectWindow(macWindow);
- }
-
- void
- PedWindow::Resize(short inWidth, short inHeight)
- {
- short oldWidth, oldHeight;
- Rect rect, invalid;
-
- if (macWindow) {
- rect = macWindow->portRect;
- oldWidth = rect.right - rect.left;
- oldHeight = rect.bottom - rect.top;
- ::SizeWindow(macWindow, inWidth, inHeight, true);
- ::SetPort(macWindow);
-
- mBounds.right = mBounds.left + inWidth;
- mBounds.bottom = mBounds.top + inHeight;
-
- // Invalidate old scrollbar regions if expanding;
- // invalidate new scrollbar regions if shrinking.
- if (inWidth > oldWidth) {
- invalid = rect;
- invalid.left = invalid.right - 15;
- ::InvalRect(&invalid);
- } else if (inWidth < oldWidth) {
- invalid = macWindow->portRect;
- invalid.left = invalid.right - 15;
- ::InvalRect(&invalid);
- }
- if (inHeight > oldHeight) {
- invalid = rect;
- invalid.top = invalid.bottom - 15;
- ::InvalRect(&invalid);
- } else if (inHeight < oldHeight) {
- invalid = macWindow->portRect;
- invalid.top = invalid.bottom - 15;
- ::InvalRect(&invalid);
- }
-
- if (mPane)
- mPane->Resize(inWidth, inHeight);
- }
- }
-
- #if 0
- void
- PedWindow::Resize(short inWidth, short inHeight)
- {
- short oldWidth, oldHeight;
- Rect rect, invalid;
-
- if (macWindow) {
- rect = macWindow->portRect;
- oldWidth = rect.right - rect.left;
- oldHeight = rect.bottom - rect.top;
- ::SizeWindow(macWindow, inWidth, inHeight, true);
- ::SetPort(macWindow);
-
- mBounds.right = mBounds.left + inWidth;
- mBounds.bottom = mBounds.top + inHeight;
-
- // Invalidate old scrollbar regions if expanding;
- // invalidate new scrollbar regions if shrinking.
- if (inWidth > oldWidth) {
- invalid = rect;
- invalid.left = invalid.right - 15;
- ::InvalRect(&invalid);
- } else if (inWidth < oldWidth) {
- invalid = macWindow->portRect;
- invalid.left = invalid.right - 15;
- ::InvalRect(&invalid);
- }
- if (inHeight > oldHeight) {
- invalid = rect;
- invalid.top = invalid.bottom - 15;
- ::InvalRect(&invalid);
- } else if (inHeight < oldHeight) {
- invalid = macWindow->portRect;
- invalid.top = invalid.bottom - 15;
- ::InvalRect(&invalid);
- }
-
- if (mPane)
- mPane->Resize(inWidth - 15, inHeight - 15);
- }
- }
- #endif
-
- void
- PedWindow::GetFrame(Rect &outFrame)
- {
- if (macWindow) {
- outFrame = macWindow->portRect;
- } else {
- ::SetRect(&outFrame, 0, 0,
- mBounds.right - mBounds.left,
- mBounds.bottom - mBounds.top );
- }
- }
-
- void
- PedWindow::GetBounds(Rect &outBounds)
- {
- outBounds = mBounds;
- }
-
- void
- PedWindow::SetBounds(const Rect &inBounds)
- {
- mBounds = inBounds;
- }
-
- void
- PedWindow::GetTitle(Str255 outItemText)
- {
- if (macWindow)
- ::GetWTitle(macWindow, outItemText);
- else
- ::BlockMoveData(mTitle, outItemText, mTitle[0]);
- }
-
- void
- PedWindow::SetTitle(const Str255 inItemText)
- {
- ::BlockMoveData(inItemText, mTitle, inItemText[0] + 1);
- if (macWindow)
- ::SetWTitle(macWindow, inItemText);
- }
-
- PedAgent *
- PedWindow::Agent()
- {
- return mAgent;
- }
-
- void
- PedWindow::SetAgent(PedAgent *inAgent)
- {
- if (inAgent)
- inAgent->retain();
- PedAgent *oldAgent = mAgent;
- mAgent = inAgent;
- if (oldAgent)
- oldAgent->release();
- }
-
- void
- PedWindow::DispatchNullEvent(EventRecord &inEvent)
- {
- if (mPane)
- mPane->DispatchNullEvent(inEvent);
- }
-
- void
- PedWindow::DispatchClickEvent(EventRecord &inEvent)
- {
- if (!macWindow) throw;
-
- if (macWindow != ::FrontWindow())
- ::SelectWindow(macWindow);
- else {
- ::SetPort(macWindow);
- if (mPane)
- mPane->DispatchClickEvent(inEvent);
- else {
- Rect dot;
-
- ::SetPort(macWindow);
- ::GlobalToLocal(&inEvent.where);
- dot.top = inEvent.where.v -2;
- dot.left = inEvent.where.h - 2;
- dot.bottom = inEvent.where.v + 2;
- dot.right = inEvent.where.h + 2;
- ::InvertOval(&dot);
- }
- }
- }
-
- void
- PedWindow::DispatchKey(EventRecord &inEvent)
- {
- if (mAgent)
- mAgent->ProcessKey(inEvent);
- else if (mPane)
- mPane->DispatchKey(inEvent);
- }
-
- void
- PedWindow::ProcessGoAway(EventRecord &inEvent)
- {
- ::SetPort(macWindow);
- if (::TrackGoAway(macWindow, inEvent.where)) {
- HandleClose();
- }
- }
-
- void
- PedWindow::HandleClose()
- {
- if (mAgent) {
- mAgent->CloseWindow();
- } else {
- Close();
- }
- }
-