#include "GL/TextView.h"
#include "GL/Font.h"
#include "GL/ImmediateMode.h"
BEGIN_NAMESPACE
TextView::TextView(int a_x, int a_y, int a_w, int a_h, const char* a_name)
: Panel(a_x, a_y, a_w, a_h, a_name)
{
}
void TextView::draw()
{
drawPanel();
unsigned textIndex = 0;
int lastIndex = 0;
float tw = 0;
const int fontSize = 14;
const int tabWidth = 4;
int row = 1;
int nextX = 0;
while (textIndex < text.size()) {
int oldIndex = textIndex;
bool broke = false;
while (text[textIndex] && !isspace(text[textIndex])) {
tw += charWidth(text[textIndex]);
textIndex++;
if ( tw*fontSize > (w-15) ) {
broke = true;
break;
}
}
if ( broke || textIndex >= text.size() || text[textIndex] == '\n' || text[textIndex] == '\t' ) {
textIndex++;
int s = oldIndex - lastIndex;
if ( !broke || s == 0 )
s = textIndex - lastIndex;
//if (text[textIndex-1] == '\n' || text[textIndex-1] == '\t')
//s--;
char *buf = new char[s+1];
memcpy(buf, &text[lastIndex], s);
buf[s] = 0;
glDrawText(nextX + x+8+1, y+30+row*20, buf, 0xff000000, 0xffffffff, fontSize);
//glDrawShadowText(nextX + x+8, y+30+row*20, buf);
if (text[textIndex-1] == '\t') {
tw += tabWidth;
nextX = int(tw*fontSize);
} else {
nextX = 0;
row++;
tw = 0;
}
lastIndex += s;
textIndex = lastIndex;
delete[] buf;
} else {
textIndex++;
tw += charWidth(text[textIndex]);
}
}
// int tw = textWidth(m_outputs[i].data()) * 17;//fontSize;
}
END_NAMESPACE