finalcut/src/ftextview.cpp

802 lines
16 KiB
C++
Raw Normal View History

// File: ftextview.cpp
// Provides: class FTextView
2015-05-23 13:35:12 +02:00
#include "fstatusbar.h"
#include "ftextview.h"
//----------------------------------------------------------------------
// class FTextView
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FTextView::FTextView(FWidget* parent)
: FWidget(parent)
, data()
, VBar(0)
, HBar(0)
, xoffset(0)
, yoffset(0)
, nf_offset(0)
, maxLineWidth(0)
2015-05-23 13:35:12 +02:00
{
2015-09-22 04:18:20 +02:00
init();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FTextView::~FTextView() // destructor
{
delete VBar;
delete HBar;
}
// private methods of FTextView
//----------------------------------------------------------------------
void FTextView::init()
{
nf_offset = isNewFont() ? 1 : 0;
foregroundColor = wc.dialog_fg;
backgroundColor = wc.dialog_bg;
VBar = new FScrollbar(fc::vertical, this);
VBar->setMinimum(0);
VBar->setValue(0);
VBar->hide();
HBar = new FScrollbar(fc::horizontal, this);
HBar->setMinimum(0);
HBar->setValue(0);
HBar->hide();
VBar->addCallback
(
"change-value",
_METHOD_CALLBACK (this, &FTextView::cb_VBarChange)
2015-05-23 13:35:12 +02:00
);
HBar->addCallback
(
"change-value",
_METHOD_CALLBACK (this, &FTextView::cb_HBarChange)
2015-05-23 13:35:12 +02:00
);
}
//----------------------------------------------------------------------
void FTextView::draw()
{
setUpdateVTerm(false);
setColor (foregroundColor, backgroundColor);
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(true);
2015-05-23 13:35:12 +02:00
if ( ! isNewFont() )
drawBorder();
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(false);
2015-05-23 13:35:12 +02:00
if ( VBar->isVisible() )
VBar->redraw();
2015-05-23 13:35:12 +02:00
if ( HBar->isVisible() )
HBar->redraw();
setUpdateVTerm(true);
2015-05-23 13:35:12 +02:00
drawText();
if ( hasFocus() && statusBar() )
{
FString msg = getStatusbarMessage();
FString curMsg = statusBar()->getMessage();
2015-05-23 13:35:12 +02:00
if ( curMsg != msg )
{
2015-09-24 00:41:43 +02:00
setUpdateVTerm(false);
2015-05-23 13:35:12 +02:00
statusBar()->setMessage(msg);
statusBar()->drawMessage();
2015-09-24 00:41:43 +02:00
setUpdateVTerm(true);
2015-05-23 13:35:12 +02:00
}
}
2015-10-23 23:57:00 +02:00
setCursorPos(1,1);
2015-09-24 00:41:43 +02:00
updateTerminal();
flush_out();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FTextView::drawText()
{
uInt start, end;
if ( data.empty() || height < 4 || width < 5 )
return;
2015-05-23 13:35:12 +02:00
start = 0;
end = uInt(height+nf_offset-2);
2015-05-23 13:35:12 +02:00
if ( end > getRows() )
end = getRows();
setUpdateVTerm(false);
setColor (foregroundColor, backgroundColor);
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(true);
2015-05-23 13:35:12 +02:00
for (uInt y=start; y < end; y++)
{
uInt i, len;
FString line;
const wchar_t* line_str;
2015-05-23 13:35:12 +02:00
gotoxy (xpos+xmin, ypos+ymin-nf_offset+int(y));
line = data[y+uInt(yoffset)].mid ( uInt(1 + xoffset)
, uInt(width - nf_offset - 2) );
line_str = line.wc_str();
len = line.getLength();
2015-05-23 13:35:12 +02:00
for (i=0; i < len; i++)
{
wchar_t ch = line_str[i];
bool utf8 = (Encoding == fc::UTF8) ? true : false;
// only printable and 1 column per character
if ( ( (utf8 && iswprint(wint_t(ch)))
|| (!utf8 && ch < 256 && isprint(ch)) )
&& wcwidth(ch) == 1 )
{
print (ch);
}
2015-05-23 13:35:12 +02:00
else
print ('.');
}
2015-09-24 00:41:43 +02:00
for (; i < uInt(width - nf_offset - 2); i++)
2015-05-23 13:35:12 +02:00
print (' ');
}
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(false);
2015-05-23 13:35:12 +02:00
setUpdateVTerm(true);
}
//----------------------------------------------------------------------
void FTextView::processChanged()
{
emitCallback("changed");
}
// protected methods of FTextView
//----------------------------------------------------------------------
void FTextView::adjustSize()
{
FWidget::adjustSize();
2015-09-24 00:41:43 +02:00
VBar->setMaximum (int(getRows()) - height + 2 - nf_offset);
VBar->setPageSize (int(getRows()), height - 2 + nf_offset);
VBar->setX (width);
VBar->setHeight (height - 2 + nf_offset, false);
2015-05-23 13:35:12 +02:00
VBar->resize();
2015-09-24 00:41:43 +02:00
HBar->setMaximum (int(maxLineWidth) - width + nf_offset + 2);
HBar->setPageSize (int(maxLineWidth), width - nf_offset - 2);
HBar->setY (height);
HBar->setWidth (width - 2, false);
2015-05-23 13:35:12 +02:00
HBar->resize();
2015-09-24 00:41:43 +02:00
if ( int(getRows()) < height + nf_offset - 1 )
2015-05-23 13:35:12 +02:00
VBar->hide();
else
VBar->setVisible();
2015-09-24 00:41:43 +02:00
if ( int(maxLineWidth) < width-nf_offset - 1 )
2015-05-23 13:35:12 +02:00
HBar->hide();
else
HBar->setVisible();
}
// public methods of FTextView
//----------------------------------------------------------------------
void FTextView::hide()
{
int n, size;
short fg, bg;
2015-05-23 13:35:12 +02:00
char* blank;
FWidget::hide();
fg = getParentWidget()->getForegroundColor();
bg = getParentWidget()->getBackgroundColor();
2015-05-23 13:35:12 +02:00
setColor (fg, bg);
n = isNewFont() ? 1 : 0;
size = width + n;
blank = new char[size+1];
memset(blank, ' ', uLong(size));
blank[size] = '\0';
for (int y=0; y < height; y++)
{
gotoxy (xpos+xmin-1, ypos+ymin-1+y);
print (blank);
}
2015-05-23 13:35:12 +02:00
delete[] blank;
flush_out();
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FTextView::onKeyPress (FKeyEvent* ev)
2015-05-23 13:35:12 +02:00
{
int last_line = int(getRows());
2015-09-20 05:44:50 +02:00
int key = ev->key();
2015-05-23 13:35:12 +02:00
switch ( key )
{
case fc::Fkey_up:
if ( yoffset > 0 )
yoffset--;
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
break;
case fc::Fkey_down:
2015-09-24 00:41:43 +02:00
if ( yoffset + height + nf_offset <= last_line + 1 )
2015-05-23 13:35:12 +02:00
yoffset++;
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
break;
case fc::Fkey_right:
2015-09-24 00:41:43 +02:00
if ( xoffset + width - nf_offset <= int(maxLineWidth) + 1 )
2015-05-23 13:35:12 +02:00
xoffset++;
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
break;
case fc::Fkey_left:
if ( xoffset > 0 )
xoffset--;
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
break;
case fc::Fkey_ppage:
yoffset -= height-2;
2015-05-23 13:35:12 +02:00
if ( yoffset < 0 )
yoffset = 0;
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
break;
case fc::Fkey_npage:
if ( last_line >= height )
yoffset += height-2;
2015-05-23 13:35:12 +02:00
if ( yoffset > last_line - height - nf_offset + 2 )
yoffset = last_line - height - nf_offset + 2;
2015-05-23 13:35:12 +02:00
if ( yoffset < 0 )
yoffset = 0;
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
break;
case fc::Fkey_home:
yoffset = 0;
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
break;
case fc::Fkey_end:
if ( last_line >= height )
yoffset = last_line - height - nf_offset + 2;
2015-09-20 05:44:50 +02:00
ev->accept();
break;
default:
2015-05-23 13:35:12 +02:00
break;
}
2015-09-20 05:44:50 +02:00
if ( ev->isAccepted() )
2015-05-23 13:35:12 +02:00
{
if ( isVisible() )
drawText();
VBar->setValue (yoffset);
2015-05-23 13:35:12 +02:00
if ( VBar->isVisible() )
VBar->drawBar();
2015-05-23 13:35:12 +02:00
HBar->setValue (xoffset);
2015-05-23 13:35:12 +02:00
if ( HBar->isVisible() )
HBar->drawBar();
2015-05-23 13:35:12 +02:00
updateTerminal();
2015-09-24 00:41:43 +02:00
flush_out();
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FTextView::onMouseDown (FMouseEvent* ev)
2015-05-23 13:35:12 +02:00
{
2016-01-17 02:57:08 +01:00
if ( ev->getButton() != fc::LeftButton )
2015-05-23 13:35:12 +02:00
return;
if ( ! hasFocus() )
{
FWidget* focused_widget = getFocusWidget();
2016-01-17 02:57:08 +01:00
FFocusEvent out (fc::FocusOut_Event);
2015-05-23 13:35:12 +02:00
FApplication::queueEvent(focused_widget, &out);
2015-09-22 04:18:20 +02:00
setFocus();
2015-05-23 13:35:12 +02:00
if ( focused_widget )
focused_widget->redraw();
2015-05-23 13:35:12 +02:00
if ( statusBar() )
statusBar()->drawMessage();
}
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FTextView::onWheel (FWheelEvent* ev)
2015-05-23 13:35:12 +02:00
{
int last_line = int(getRows());
2015-09-20 05:44:50 +02:00
int wheel = ev->getWheel();
2015-05-23 13:35:12 +02:00
switch ( wheel )
{
2016-01-17 02:57:08 +01:00
case fc::WheelUp:
2015-05-23 13:35:12 +02:00
if ( yoffset == 0 )
break;
2015-05-23 13:35:12 +02:00
yoffset -= 4;
2015-05-23 13:35:12 +02:00
if ( yoffset < 0 )
yoffset=0;
2015-05-23 13:35:12 +02:00
break;
2016-01-17 02:57:08 +01:00
case fc::WheelDown:
2015-09-20 05:44:50 +02:00
{
int yoffset_end = last_line - height - nf_offset + 2;
2015-09-20 05:44:50 +02:00
if ( yoffset_end < 0 )
yoffset_end = 0;
2015-09-20 05:44:50 +02:00
if ( yoffset == yoffset_end )
break;
2015-09-20 05:44:50 +02:00
yoffset += 4;
2015-09-20 05:44:50 +02:00
if ( yoffset > yoffset_end )
yoffset = yoffset_end;
}
break;
default:
2015-05-23 13:35:12 +02:00
break;
}
if ( isVisible() )
drawText();
VBar->setValue (yoffset);
2015-05-23 13:35:12 +02:00
if ( VBar->isVisible() )
VBar->drawBar();
2015-05-23 13:35:12 +02:00
HBar->setValue (xoffset);
2015-05-23 13:35:12 +02:00
if ( HBar->isVisible() )
HBar->drawBar();
2015-05-23 13:35:12 +02:00
updateTerminal();
}
//----------------------------------------------------------------------
void FTextView::onFocusIn (FFocusEvent*)
{
if ( statusBar() )
statusBar()->drawMessage();
}
//----------------------------------------------------------------------
void FTextView::onFocusOut (FFocusEvent*)
{
if ( statusBar() )
{
statusBar()->clearMessage();
statusBar()->drawMessage();
}
}
//----------------------------------------------------------------------
void FTextView::cb_VBarChange (FWidget*, void*)
{
int distance = 1;
int last_line = int(getRows());
int yoffset_before = yoffset;
int scrollType = VBar->getScrollType();
switch ( scrollType )
{
case FScrollbar::scrollPageBackward:
distance = height+nf_offset-2;
2015-10-01 03:48:58 +02:00
// fall through
2015-05-23 13:35:12 +02:00
case FScrollbar::scrollStepBackward:
yoffset -= distance;
2015-05-23 13:35:12 +02:00
if ( yoffset < 0 )
yoffset = 0;
2015-05-23 13:35:12 +02:00
break;
case FScrollbar::scrollPageForward:
distance = height+nf_offset-2;
2015-10-01 03:48:58 +02:00
// fall through
2015-05-23 13:35:12 +02:00
case FScrollbar::scrollStepForward:
yoffset += distance;
2015-05-23 13:35:12 +02:00
if ( yoffset > last_line - height - nf_offset + 2 )
yoffset = last_line - height - nf_offset + 2;
2015-05-23 13:35:12 +02:00
break;
case FScrollbar::scrollJump:
{
int val = VBar->getValue();
2015-05-23 13:35:12 +02:00
if ( yoffset == val )
break;
2015-05-23 13:35:12 +02:00
yoffset = val;
2015-05-23 13:35:12 +02:00
if ( yoffset > last_line - height - nf_offset + 2 )
yoffset = last_line - height - nf_offset + 2;
2015-05-23 13:35:12 +02:00
if ( yoffset < 0 )
yoffset = 0;
2015-05-23 13:35:12 +02:00
break;
}
case FScrollbar::scrollWheelUp:
{
2016-01-17 02:57:08 +01:00
FWheelEvent wheel_ev (fc::MouseWheel_Event, FPoint(2,2), fc::WheelUp);
2015-05-23 13:35:12 +02:00
onWheel(&wheel_ev);
break;
}
case FScrollbar::scrollWheelDown:
{
2016-01-17 02:57:08 +01:00
FWheelEvent wheel_ev (fc::MouseWheel_Event, FPoint(2,2), fc::WheelDown);
2015-05-23 13:35:12 +02:00
onWheel(&wheel_ev);
break;
}
2015-09-20 05:44:50 +02:00
default:
break;
2015-05-23 13:35:12 +02:00
}
if ( isVisible() )
{
drawText();
updateTerminal();
}
if ( scrollType >= FScrollbar::scrollStepBackward
&& scrollType <= FScrollbar::scrollPageForward )
{
VBar->setValue (yoffset);
2015-05-23 13:35:12 +02:00
if ( VBar->isVisible() && yoffset_before != yoffset )
VBar->drawBar();
2015-05-23 13:35:12 +02:00
updateTerminal();
}
}
//----------------------------------------------------------------------
void FTextView::cb_HBarChange (FWidget*, void*)
{
int distance = 1;
int xoffset_before = xoffset;
2015-09-24 00:41:43 +02:00
int xoffset_end = int(maxLineWidth) - width + nf_offset + 4;
2015-05-23 13:35:12 +02:00
int scrollType = HBar->getScrollType();
switch ( scrollType )
{
case FScrollbar::scrollPageBackward:
2015-09-24 00:41:43 +02:00
distance = width - nf_offset - 4;
2015-10-01 03:48:58 +02:00
// fall through
2015-05-23 13:35:12 +02:00
case FScrollbar::scrollStepBackward:
xoffset -= distance;
2015-05-23 13:35:12 +02:00
if ( xoffset < 0 )
xoffset = 0;
2015-05-23 13:35:12 +02:00
break;
case FScrollbar::scrollPageForward:
2015-09-24 00:41:43 +02:00
distance = width - nf_offset - 4;
2015-10-01 03:48:58 +02:00
// fall through
2015-05-23 13:35:12 +02:00
case FScrollbar::scrollStepForward:
xoffset += distance;
2015-09-24 00:41:43 +02:00
if ( xoffset > int(maxLineWidth) - width + nf_offset + 4 )
xoffset = int(maxLineWidth) - width + nf_offset + 4;
2015-05-23 13:35:12 +02:00
if ( xoffset < 0 )
xoffset = 0;
2015-05-23 13:35:12 +02:00
break;
case FScrollbar::scrollJump:
{
int val = HBar->getValue();
2015-05-23 13:35:12 +02:00
if ( xoffset == val )
break;
2015-05-23 13:35:12 +02:00
xoffset = val;
2015-09-24 00:41:43 +02:00
if ( xoffset > int(maxLineWidth) - width + nf_offset + 4 )
xoffset = int(maxLineWidth) - width + nf_offset + 4;
2015-05-23 13:35:12 +02:00
if ( xoffset < 0 )
xoffset = 0;
2015-05-23 13:35:12 +02:00
break;
}
case FScrollbar::scrollWheelUp:
if ( xoffset == 0 )
break;
2015-05-23 13:35:12 +02:00
xoffset -= 4;
2015-05-23 13:35:12 +02:00
if ( xoffset < 0 )
xoffset=0;
2015-05-23 13:35:12 +02:00
break;
case FScrollbar::scrollWheelDown:
if ( xoffset == xoffset_end )
break;
2015-05-23 13:35:12 +02:00
xoffset += 4;
2015-05-23 13:35:12 +02:00
if ( xoffset > xoffset_end )
xoffset = xoffset_end;
2015-05-23 13:35:12 +02:00
break;
2015-09-20 05:44:50 +02:00
default:
break;
2015-05-23 13:35:12 +02:00
}
if ( isVisible() )
{
drawText();
updateTerminal();
}
if ( scrollType >= FScrollbar::scrollStepBackward
&& scrollType <= FScrollbar::scrollWheelDown )
{
HBar->setValue (xoffset);
2015-05-23 13:35:12 +02:00
if ( HBar->isVisible() && xoffset_before != xoffset )
HBar->drawBar();
2015-05-23 13:35:12 +02:00
updateTerminal();
}
}
//----------------------------------------------------------------------
void FTextView::setGeometry (int x, int y, int w, int h, bool adjust)
{
FWidget::setGeometry(x, y, w, h, adjust);
if ( isNewFont() )
{
2015-09-24 00:41:43 +02:00
VBar->setGeometry (width, 1, 2, height-1);
HBar->setGeometry (1, height, width-2, 1);
2015-05-23 13:35:12 +02:00
}
else
{
2015-09-24 00:41:43 +02:00
VBar->setGeometry (width, 2, 1, height-2);
HBar->setGeometry (2, height, width-2, 1);
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FTextView::setPosition (int pos)
{
int last_line = int(getRows());
2015-05-23 13:35:12 +02:00
if ( pos < 0 || pos > last_line - height + 2 )
return;
yoffset = pos;
if ( isVisible() )
drawText();
VBar->setValue (yoffset);
2015-05-23 13:35:12 +02:00
if ( VBar->isVisible() )
VBar->drawBar();
flush_out();
}
//----------------------------------------------------------------------
void FTextView::setText (const FString& str)
{
clear();
insert(str, -1);
}
//----------------------------------------------------------------------
FString FTextView::getText() const
{
uInt len, rows, idx;
if ( data.empty() )
return FString("");
len = 0;
rows = getRows();
2015-05-23 13:35:12 +02:00
for (uInt i=0 ; i < rows; i++)
len += data[i].getLength() + 1;
FString s(len + 1);
idx = 0;
2015-05-23 13:35:12 +02:00
for (uInt i=0 ; i < rows; i++)
{
const wchar_t* p = data[i].wc_str();
2015-05-23 13:35:12 +02:00
if ( p )
{
while ( (s[idx++] = *p++) != 0 );
s[idx-1] = '\n';
}
else
{
s[idx++] = '\n';
}
}
2015-05-23 13:35:12 +02:00
s[idx-1] = 0;
return s;
}
//----------------------------------------------------------------------
void FTextView::append (const FString& str)
{
insert(str, -1);
}
//----------------------------------------------------------------------
void FTextView::insert (const FString& str, int pos)
{
stringLines::iterator iter;
stringLines text_split;
FString s;
uLong end;
if ( pos < 0 || pos >= int(getRows()) )
pos = int(getRows());
if ( str.isEmpty() )
s = "\n";
else
s = FString(str).rtrim().expandTabs(tabstop);
iter = data.begin();
text_split = s.split("\r\n");
end = text_split.size();
for (uInt i=0; i < end; i++)
{
uInt len;
text_split[i] = text_split[i].removeBackspaces()
.removeDel()
.replaceControlCodes()
.rtrim();
len = text_split[i].getLength();
2015-05-23 13:35:12 +02:00
if ( len > maxLineWidth )
{
maxLineWidth = len;
2015-05-23 13:35:12 +02:00
if ( len > uInt(width-nf_offset-2) )
{
2015-09-24 00:41:43 +02:00
HBar->setMaximum (int(maxLineWidth) - width + nf_offset + 2);
HBar->setPageSize (int(maxLineWidth), width - nf_offset - 2);
2015-05-23 13:35:12 +02:00
HBar->calculateSliderValues();
2015-05-23 13:35:12 +02:00
if ( ! HBar->isVisible() )
HBar->setVisible();
}
}
}
data.insert (iter + pos, text_split.begin(), text_split.end());
2015-09-24 00:41:43 +02:00
VBar->setMaximum (int(getRows()) - height + 2 - nf_offset);
VBar->setPageSize (int(getRows()), height - 2 + nf_offset);
2015-05-23 13:35:12 +02:00
VBar->calculateSliderValues();
2015-09-24 00:41:43 +02:00
if ( ! VBar->isVisible() && int(getRows()) >= height + nf_offset - 1 )
2015-05-23 13:35:12 +02:00
VBar->setVisible();
2015-09-24 00:41:43 +02:00
if ( VBar->isVisible() && int(getRows()) < height + nf_offset - 1 )
2015-05-23 13:35:12 +02:00
VBar->hide();
2015-05-23 13:35:12 +02:00
processChanged();
}
//----------------------------------------------------------------------
void FTextView::replaceRange (const FString& str, int start, int end)
{
stringLines::iterator iter;
if ( start > end )
return;
2015-05-23 13:35:12 +02:00
if ( start < 0 || start >= int(getRows()) )
return;
2015-05-23 13:35:12 +02:00
if ( end < 0 || end >= int(getRows()) )
return;
iter = data.begin();
data.erase (iter+start, iter+end+1);
if ( ! str.isNull() )
insert(str, start);
}
//----------------------------------------------------------------------
void FTextView::clear()
{
int size;
char* blank;
data.clear();
xoffset = 0;
yoffset = 0;
maxLineWidth = 0;
VBar->setMinimum(0);
VBar->setValue(0);
VBar->hide();
HBar->setMinimum(0);
HBar->setValue(0);
HBar->hide();
// clear list from screen
setColor (foregroundColor, backgroundColor);
size = width - 2;
blank = new char[size+1];
memset(blank, ' ', uLong(size));
blank[size] = '\0';
for (int y=0; y < height+nf_offset-2; y++)
{
gotoxy (xpos+xmin, ypos+ymin-nf_offset+y);
print (blank);
}
2015-05-23 13:35:12 +02:00
delete[] blank;
processChanged();
}