readline!

This commit is contained in:
Wolfgang Thaller 2012-03-30 10:01:55 +02:00
parent 04fcc13550
commit f370137c88
5 changed files with 78 additions and 4 deletions

View File

@ -1,5 +1,9 @@
set(CMAKE_CXX_FLAGS "-std=c++11")
add_executable(Test test.cc Console.cc)
add_executable(Test
test.cc
Console.cc
Console.h
MacUtils.h)
add_custom_command(
OUTPUT Test.dsk
COMMAND ${MAKE_APPL} -c Test

View File

@ -1,16 +1,18 @@
#include "Console.h"
#include "MacUtils.h"
#include "Events.h"
Console *Console::currentInstance = NULL;
Console::Console(GrafPtr port)
: consolePort(port)
{
PortSetter setport(consolePort);
Rect r = {2,2,340,510};
bounds = r;
SetPort(port);
consolePort = port;
TextFont(9);
TextSize(9);
MoveTo(10,10);
cellSizeY = 10;
cellSizeX = CharWidth('M');
@ -39,6 +41,7 @@ void Console::DrawCell(short x, short y)
void Console::Draw()
{
PortSetter setport(consolePort);
//PashortRect(&r);
for(short row = 0; row < rows; ++row)
@ -63,6 +66,8 @@ void Console::ScrollUp(short n)
void Console::putch(char c)
{
PortSetter setport(consolePort);
//Debugger();
switch(c)
{
@ -84,3 +89,44 @@ void Console::putch(char c)
}
}
std::string Console::ReadLine()
{
std::string buffer;
EventRecord event;
char c;
do
{
do
{
while(!GetOSEvent(everyEvent, &event))
;
} while(event.what != keyDown && event.what != autoKey);
c = event.message & charCodeMask;
if(c == '\r')
c = '\n';
if(c == '\b')
{
cursorX--;
putch(' ');
cursorX--;
buffer.substr(0,buffer.size()-1);
continue;
//c = 'X';
}
if(c == 127)
{
c = 'Y';
}
putch(c);
buffer += std::string(1,c);
} while(c != '\n');
return buffer;
}

View File

@ -1,5 +1,6 @@
#include <Quickdraw.h>
#include <vector>
#include <string>
class Console
{
@ -7,6 +8,7 @@ public:
Console(GrafPtr port);
void Draw();
void putch(char c);
std::string ReadLine();
static Console *currentInstance;
private:

19
App2/MacUtils.h Normal file
View File

@ -0,0 +1,19 @@
#include <Quickdraw.h>
class PortSetter
{
GrafPtr save;
public:
PortSetter(GrafPtr port)
{
::GetPort(&save);
::SetPort(port);
}
~PortSetter()
{
::SetPort(save);
}
};

View File

@ -127,6 +127,9 @@ int main(int argc, char** argv)
std::cout << "Hello, world: " << i << std::endl;
std::cout << std::flush;
std::cout << "Say something: " << std::flush;
console.ReadLine();
for(int i = 0; i < 5; i++)
{
std::cout << "Exception speed test: " << std::flush;