Retro68/Console/retro/ConsoleWindow.cc

157 lines
4.5 KiB
C++
Raw Normal View History

2015-10-15 22:52:50 +00:00
/*
2020-01-12 21:09:04 +00:00
Copyright 2012-2020 Wolfgang Thaller, Davide Bucci
2015-10-15 22:52:50 +00:00
This file is part of Retro68.
Retro68 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Retro68 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
2015-10-15 22:52:50 +00:00
You should have received a copy of the GNU General Public License
along with Retro68. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ConsoleWindow.h"
#include "Events.h"
#include <unordered_map>
#include <cstring>
#include <TextUtils.h>
2015-10-15 22:52:50 +00:00
2019-01-08 23:12:47 +00:00
using namespace retro;
2015-10-15 22:52:50 +00:00
namespace
{
std::unordered_map<WindowPtr, ConsoleWindow*> *windows = NULL;
2015-10-15 22:52:50 +00:00
}
ConsoleWindow::ConsoleWindow(Rect r, ConstStr255Param title)
{
GrafPtr port;
win = NewWindow(NULL, &r, "\pRetro68 Console", true, 0, (WindowPtr)-1, true, 0);
2015-10-15 22:52:50 +00:00
#if !TARGET_API_MAC_CARBON
port = win;
Rect portRect = port->portRect;
2015-10-15 22:52:50 +00:00
#else
port = GetWindowPort(win);
Rect portRect;
GetPortBounds(port, &portRect);
2015-10-15 22:52:50 +00:00
#endif
SetPort(port);
EraseRect(&portRect);
2015-10-15 22:52:50 +00:00
if(!windows)
windows = new std::unordered_map<WindowPtr, ConsoleWindow*>();
(*windows)[win] = this;
2015-10-15 22:52:50 +00:00
Init(port, portRect);
2015-10-15 22:52:50 +00:00
}
ConsoleWindow::~ConsoleWindow()
{
windows->erase(win);
DisposeWindow(win);
2015-10-15 22:52:50 +00:00
}
void ConsoleWindow::setWindowName(std::string newName)
{
Str255 pname;
#if TARGET_API_MAC_CARBON
// Carbon has the new, sane version.
c2pstrcpy(pname,newName.c_str());
#else
// It is also availble in various glue code libraries and
// in some versions of InterfaceLib, but it's confusing.
// Using the inplace variant, c2pstr, isn't much better than
// doing things by hand:
strncpy((char *)&pname[1],newName.c_str(),255);
pname[0] = newName.length();
#endif
SetWTitle(win, pname);
}
2015-10-15 22:52:50 +00:00
char ConsoleWindow::WaitNextChar()
{
EventRecord event;
WindowPtr eventWin;
ConsoleWindow *realConsole;
2015-10-15 22:52:50 +00:00
#if TARGET_API_MAC_CARBON
Rect *boundsPtr = NULL;
2015-10-15 22:52:50 +00:00
#else
Rect *boundsPtr = &qd.screenBits.bounds;
2015-10-15 22:52:50 +00:00
#endif
do
{
#if TARGET_API_MAC_CARBON
#define SystemTask()
#endif
SystemTask();
Idle();
while(!GetNextEvent(everyEvent, &event))
{
SystemTask();
Idle();
}
switch(event.what)
{
case updateEvt:
eventWin = (WindowPtr)event.message;
realConsole = (*windows)[(WindowPtr)event.message];
if(realConsole)
{
Rect updateRect;
BeginUpdate(eventWin);
2015-10-16 16:02:50 +00:00
#if TARGET_API_MAC_CARBON
RgnHandle rgn = NewRgn();
GetPortVisibleRegion(GetWindowPort(eventWin), rgn);
GetRegionBounds(rgn, &updateRect);
DisposeRgn(rgn);
2015-10-16 16:02:50 +00:00
#else
updateRect = (*qd.thePort->visRgn)->rgnBBox; // Life was simple back then.
2015-10-16 16:02:50 +00:00
#endif
realConsole->Draw(updateRect);
EndUpdate(eventWin);
}
break;
case mouseDown:
switch(FindWindow(event.where, &eventWin))
{
case inDrag:
DragWindow(eventWin, event.where, boundsPtr);
break;
case inGrow:
{
long growResult = GrowWindow(eventWin, event.where, boundsPtr);
SizeWindow(eventWin, growResult & 0xFFFF, growResult >> 16, false);
Reshape(Rect {0, 0, (short) (growResult >> 16), (short) (growResult & 0xFFFF) });
}
break;
2020-01-12 21:09:04 +00:00
case inGoAway:
{
if (TrackGoAway(eventWin,event.where))
exit(0);
}
2020-01-12 23:20:40 +00:00
break;
}
break;
}
} while(event.what != keyDown && event.what != autoKey);
return event.message & charCodeMask;
2015-10-15 22:52:50 +00:00
}