finalcut/src/fbutton.cpp

722 lines
16 KiB
C++
Raw Normal View History

// File: fbutton.cpp
// Provides: class FButton
2015-05-23 13:35:12 +02:00
#include "fapp.h"
#include "fbutton.h"
#include "fstatusbar.h"
//----------------------------------------------------------------------
// class FButton
//----------------------------------------------------------------------
// constructors and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FButton::FButton(FWidget* parent)
: FWidget(parent)
, text()
, button_down(false)
, click_animation(true)
, click_time(150)
, button_fg(wc.button_active_fg)
, button_bg(wc.button_active_bg)
, button_hotkey_fg(wc.button_hotkey_fg)
, button_focus_fg(wc.button_active_focus_fg)
, button_focus_bg(wc.button_active_focus_bg)
, button_inactive_fg(wc.button_inactive_fg)
, button_inactive_bg(wc.button_inactive_bg)
{
init();
}
//----------------------------------------------------------------------
FButton::FButton (const FString& txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, button_down(false)
, click_animation(true)
, click_time(150)
, button_fg(wc.button_active_fg)
, button_bg(wc.button_active_bg)
, button_hotkey_fg(wc.button_hotkey_fg)
, button_focus_fg(wc.button_active_focus_fg)
, button_focus_bg(wc.button_active_focus_bg)
, button_inactive_fg(wc.button_inactive_fg)
, button_inactive_bg(wc.button_inactive_bg)
{
init();
detectHotkey();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FButton::~FButton() // destructor
{
2015-11-12 01:33:16 +01:00
delAccelerator();
2015-12-19 20:49:01 +01:00
delOwnTimer();
2015-05-23 13:35:12 +02:00
}
// private methods of FButton
//----------------------------------------------------------------------
void FButton::init()
{
setForegroundColor (wc.button_active_fg);
setBackgroundColor (wc.button_active_bg);
2015-05-23 13:35:12 +02:00
if ( hasFocus() )
2016-01-24 14:53:09 +01:00
flags = fc::focus;
2015-05-23 13:35:12 +02:00
if ( isEnabled() )
2016-01-24 14:53:09 +01:00
flags |= fc::active;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
uChar FButton::getHotkey()
{
int length;
if ( text.isEmpty() )
return 0;
length = int(text.getLength());
for (int i=0; i < length; i++)
2015-09-30 22:39:02 +02:00
{
try
{
if ( (i+1 < length) && (text[uInt(i)] == '&') )
return uChar(text[uInt(++i)]);
}
catch (const std::out_of_range&)
{
return 0;;
}
}
2015-05-23 13:35:12 +02:00
return 0;
}
//----------------------------------------------------------------------
void FButton::setHotkeyAccelerator()
{
int hotkey = getHotkey();
if ( hotkey )
{
if ( isalpha(hotkey) || isdigit(hotkey) )
{
2015-11-12 01:33:16 +01:00
addAccelerator (tolower(hotkey));
addAccelerator (toupper(hotkey));
2015-05-23 13:35:12 +02:00
// Meta + hotkey
2015-11-12 01:33:16 +01:00
addAccelerator (fc::Fmkey_meta + tolower(hotkey));
2015-05-23 13:35:12 +02:00
}
else
2015-11-12 01:33:16 +01:00
addAccelerator (getHotkey());
2015-05-23 13:35:12 +02:00
}
else
2015-11-12 01:33:16 +01:00
delAccelerator();
2015-05-23 13:35:12 +02:00
}
2015-09-22 04:18:20 +02:00
//----------------------------------------------------------------------
inline void FButton::detectHotkey()
2015-09-22 04:18:20 +02:00
{
if ( isEnabled() )
{
2015-11-12 01:33:16 +01:00
delAccelerator();
2015-09-22 04:18:20 +02:00
setHotkeyAccelerator();
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::draw()
{
register wchar_t* src;
register wchar_t* dest;
wchar_t* ButtonText;
FString txt;
int d, i, j, x, mono_offset, margin;
int length, hotkeypos, hotkey_offset, space;
2015-09-20 05:44:50 +02:00
bool is_ActiveFocus, is_Active, is_Focus, is_Flat;
bool is_NonFlatShadow, is_NoUnderline;
2015-05-23 13:35:12 +02:00
length = int(text.getLength());
hotkeypos = -1;
hotkey_offset = 0;
space = int(' ');
2015-10-11 21:56:16 +02:00
if ( isMonochron() || getMaxColor() < 16 )
2015-10-17 19:40:43 +02:00
ButtonText = new wchar_t[length+3]();
2015-05-23 13:35:12 +02:00
else
2015-10-17 19:40:43 +02:00
ButtonText = new wchar_t[length+1]();
2015-05-23 13:35:12 +02:00
txt = FString(text);
2015-05-23 13:35:12 +02:00
src = const_cast<wchar_t*>(txt.wc_str());
dest = const_cast<wchar_t*>(ButtonText);
2016-01-24 14:53:09 +01:00
is_ActiveFocus = (flags & (fc::active+fc::focus)) == (fc::active+fc::focus);
is_Active = ((flags & fc::active) != 0);
is_Focus = ((flags & fc::focus) != 0);
2015-09-20 05:44:50 +02:00
is_Flat = isFlat();
2016-01-24 14:53:09 +01:00
is_NonFlatShadow = ((flags & (fc::shadow+fc::flat)) == fc::shadow);
is_NoUnderline = ((flags & fc::no_underline) != 0);
2015-05-23 13:35:12 +02:00
setUpdateVTerm(false);
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(true);
if ( button_down && click_animation )
2015-05-23 13:35:12 +02:00
{
// noshadow + indent one character to the right
2015-09-20 05:44:50 +02:00
if ( is_Flat )
2015-05-23 13:35:12 +02:00
clearFlatBorder();
clearShadow();
2015-10-10 04:01:22 +02:00
setColor ( parentWidget()->getForegroundColor()
, parentWidget()->getBackgroundColor() );
2015-05-23 13:35:12 +02:00
for (int y=1; y <= height; y++)
{
gotoxy (xpos+xmin-1, ypos+ymin-2+y);
print (' '); // clear one left █
}
d = 1;
}
else
d = 0;
2015-09-20 05:44:50 +02:00
if ( ! is_Active && isMonochron() )
2015-05-23 13:35:12 +02:00
space = fc::MediumShade; // ▒
2015-10-23 23:57:00 +02:00
if ( isMonochron() && is_ActiveFocus )
2015-05-23 13:35:12 +02:00
{
txt = "<" + txt + ">";
length = int(txt.getLength());
src = const_cast<wchar_t*>(txt.wc_str());
mono_offset = 2;
}
else
mono_offset = 0;
// find hotkey position in string
// + generate a new string without the '&'-sign
for (i=0; i < length; i++)
{
if ( (i < length) && (txt[uInt(i)] == '&') && (hotkeypos == -1) )
{
hotkeypos = i;
i++;
src++;
}
*dest++ = *src++;
}
if ( hotkeypos != -1 )
hotkey_offset = 1;
if ( (length - hotkey_offset + mono_offset - hotkey_offset) <= width )
margin = 1;
else
margin = 0;
2015-09-20 05:44:50 +02:00
if ( isMonochron() && (is_Active || is_Focus) )
2015-10-11 21:56:16 +02:00
setReverse(false);
2015-05-23 13:35:12 +02:00
2015-09-20 05:44:50 +02:00
if ( is_Flat )
2015-05-23 13:35:12 +02:00
{
if ( margin == 1 )
{
setColor (foregroundColor, button_bg);
2015-09-24 00:41:43 +02:00
for (int y=0; y < height; y++)
2015-05-23 13:35:12 +02:00
{
gotoxy (xpos+xmin-1+d, ypos+ymin-1+y);
print (space); // full block █
}
}
if ( ! button_down )
drawFlatBorder();
}
else if ( ! isMonochron() )
{
2015-07-09 23:29:51 +02:00
setColor (button_bg, parentWidget()->getBackgroundColor());
2015-05-23 13:35:12 +02:00
gotoxy (xpos+xmin-1+d, ypos+ymin-1);
for (int y=1; y <= height; y++)
{
// Cygwin terminal use IBM Codepage 850
2015-09-22 04:18:20 +02:00
if ( isCygwinTerminal() )
2015-05-23 13:35:12 +02:00
print (fc::FullBlock); // █
else if ( isTeraTerm() )
print (0xdb);
2015-05-23 13:35:12 +02:00
else
print (fc::RightHalfBlock); // ▐
gotoxy (xpos+xmin-1+d, ypos+ymin-1+y);
}
}
if ( ! button_down
&& ! isNewFont()
2015-09-20 05:44:50 +02:00
&& (is_Flat || ! hasShadow() || isMonochron()) )
2015-05-23 13:35:12 +02:00
{
// clear the right █ from button down
2015-10-10 04:01:22 +02:00
setColor ( parentWidget()->getForegroundColor()
, parentWidget()->getBackgroundColor() );
2015-05-23 13:35:12 +02:00
for (int y=1; y <= height; y++)
{
if ( isMonochron() )
2015-10-11 21:56:16 +02:00
setReverse(true);
2015-05-23 13:35:12 +02:00
gotoxy (xpos+xmin-1+width, ypos+ymin-2+y);
print (' '); // clear right
if ( isMonochron() )
2015-10-11 21:56:16 +02:00
setReverse(false);
2015-05-23 13:35:12 +02:00
}
}
if ( hotkeypos != -1 )
length--;
i = width - length - 1;
i = int(i / 2);
if ( height > 1 )
j = int((height-1) / 2);
else
j=0;
gotoxy (xpos+xmin-1+margin+d, ypos+ymin-1+j);
setColor (button_fg, button_bg);
for (x=0; x < i; x++)
print (space); // █
if ( hotkeypos == -1 )
2015-10-10 04:01:22 +02:00
setCursorPos ( xpos+xmin-1+margin+i
, ypos+ymin-1+j ); // first character
2015-05-23 13:35:12 +02:00
else
2015-10-10 04:01:22 +02:00
setCursorPos ( xpos+xmin-1+margin+i+hotkeypos
, ypos+ymin-1+j ); // hotkey
2015-05-23 13:35:12 +02:00
2015-10-24 13:38:58 +02:00
if ( is_ActiveFocus && (isMonochron() || getMaxColor() < 16) )
2015-05-23 13:35:12 +02:00
setBold();
2015-05-23 13:35:12 +02:00
for (int z=0; x < i+length && z < width; z++,x++)
{
2015-09-20 05:44:50 +02:00
if ( (z == hotkeypos) && is_Active )
2015-05-23 13:35:12 +02:00
{
setColor (button_hotkey_fg, button_bg);
2015-10-24 13:38:58 +02:00
if ( ! is_ActiveFocus && getMaxColor() < 16 )
setBold();
2015-09-20 05:44:50 +02:00
if ( ! is_NoUnderline )
2015-05-23 13:35:12 +02:00
setUnderline();
print ( ButtonText[z] );
2015-10-24 13:38:58 +02:00
if ( ! is_ActiveFocus && getMaxColor() < 16 )
unsetBold();
2015-09-20 05:44:50 +02:00
if ( ! is_NoUnderline )
2015-05-23 13:35:12 +02:00
unsetUnderline();
setColor (button_fg, button_bg);
}
else
{
print ( ButtonText[z] );
}
}
2015-10-24 13:38:58 +02:00
if ( is_ActiveFocus && (isMonochron() || getMaxColor() < 16) )
2015-05-23 13:35:12 +02:00
unsetBold();
2015-05-23 13:35:12 +02:00
for (x=i+length; x < width-1; x++)
print (space); // █
if ( height > 1 )
{
for (i=0; i < j; i++)
{
gotoxy (xpos+xmin+d, ypos+ymin-1+i);
2015-09-24 00:41:43 +02:00
for (int z=1; z < width; z++)
2015-05-23 13:35:12 +02:00
print (space); // █
}
for (i=j+1; i < height; i++)
{
gotoxy (xpos+xmin+d, ypos+ymin-1+i);
2015-09-24 00:41:43 +02:00
for (int z=1; z < width; z++)
2015-05-23 13:35:12 +02:00
print (space); // █
}
}
if ( isMonochron() )
2015-10-11 21:56:16 +02:00
setReverse(true);
2015-05-23 13:35:12 +02:00
2015-09-20 05:44:50 +02:00
if ( is_NonFlatShadow && ! button_down )
2015-05-23 13:35:12 +02:00
{
2015-10-10 04:01:22 +02:00
setColor ( parentWidget()->getForegroundColor()
, parentWidget()->getBackgroundColor() );
2015-05-23 13:35:12 +02:00
print(' '); // restore background after button down
drawShadow();
}
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(false);
2015-05-23 13:35:12 +02:00
setUpdateVTerm(true);
delete[] ButtonText;
2015-09-20 05:44:50 +02:00
if ( is_Focus && statusBar() )
2015-05-23 13:35:12 +02:00
{
FString msg = getStatusbarMessage();
FString curMsg = statusBar()->getMessage();
if ( curMsg != msg )
{
statusBar()->setMessage(msg);
statusBar()->drawMessage();
}
}
}
//----------------------------------------------------------------------
void FButton::updateButtonColor()
{
if ( isEnabled() )
{
if ( hasFocus() )
{
button_fg = button_focus_fg;
button_bg = button_focus_bg;
}
else
{
button_fg = foregroundColor;
button_bg = backgroundColor;
}
}
else // inactive
{
button_fg = button_inactive_fg;
button_bg = button_inactive_bg;
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::processClick()
{
emitCallback("clicked");
}
// public methods of FButton
//----------------------------------------------------------------------
void FButton::setForegroundColor (short color)
{
FWidget::setForegroundColor(color);
updateButtonColor();
}
//----------------------------------------------------------------------
void FButton::setBackgroundColor (short color)
{
FWidget::setBackgroundColor(color);
updateButtonColor();
}
//----------------------------------------------------------------------
void FButton::setHotkeyForegroundColor (short color)
{
2016-01-10 00:56:52 +01:00
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_hotkey_fg = color;
}
void FButton::setFocusForegroundColor (short color)
{
2016-01-10 00:56:52 +01:00
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_focus_fg = color;
updateButtonColor();
}
//----------------------------------------------------------------------
void FButton::setFocusBackgroundColor (short color)
{
2016-01-10 00:56:52 +01:00
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_focus_bg = color;
updateButtonColor();
}
//----------------------------------------------------------------------
void FButton::setInactiveForegroundColor (short color)
{
2016-01-10 00:56:52 +01:00
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_inactive_fg = color;
updateButtonColor();
}
//----------------------------------------------------------------------
void FButton::setInactiveBackgroundColor (short color)
{
2016-01-10 00:56:52 +01:00
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_inactive_bg = color;
updateButtonColor();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::hide()
{
int s, f, size;
short fg, bg;
2015-05-23 13:35:12 +02:00
char* blank;
FWidget::hide();
fg = parentWidget()->getForegroundColor();
bg = parentWidget()->getBackgroundColor();
setColor (fg, bg);
s = hasShadow() ? 1 : 0;
f = isFlat() ? 1 : 0;
size = width + s + (f << 1);
blank = new char[size+1];
memset(blank, ' ', uLong(size));
blank[size] = '\0';
for (int y=0; y < height+s+(f << 1); y++)
{
gotoxy (xpos+xmin-1-f, ypos+ymin-1+y-f);
print (blank);
}
delete[] blank;
}
//----------------------------------------------------------------------
2015-09-15 23:07:24 +02:00
bool FButton::setNoUnderline (bool on)
2015-05-23 13:35:12 +02:00
{
if ( on )
2016-01-24 14:53:09 +01:00
flags |= fc::no_underline;
2015-05-23 13:35:12 +02:00
else
2016-01-24 14:53:09 +01:00
flags &= ~fc::no_underline;
2015-05-23 13:35:12 +02:00
return on;
}
//----------------------------------------------------------------------
2015-09-15 23:07:24 +02:00
bool FButton::setEnable (bool on)
2015-05-23 13:35:12 +02:00
{
FWidget::setEnable(on);
if ( on )
{
2016-01-24 14:53:09 +01:00
flags |= fc::active;
2015-05-23 13:35:12 +02:00
setHotkeyAccelerator();
}
else
{
2016-01-24 14:53:09 +01:00
flags &= ~fc::active;
2015-11-12 01:33:16 +01:00
delAccelerator();
2015-05-23 13:35:12 +02:00
}
updateButtonColor();
2015-05-23 13:35:12 +02:00
return on;
}
//----------------------------------------------------------------------
2015-09-15 23:07:24 +02:00
bool FButton::setFocus (bool on)
2015-05-23 13:35:12 +02:00
{
FWidget::setFocus(on);
if ( on )
{
2016-01-24 14:53:09 +01:00
flags |= fc::focus;
2015-05-23 13:35:12 +02:00
if ( isEnabled() )
{
if ( statusBar() )
{
FString msg = getStatusbarMessage();
FString curMsg = statusBar()->getMessage();
if ( curMsg != msg )
statusBar()->setMessage(msg);
}
}
}
else
{
2016-01-24 14:53:09 +01:00
flags &= ~fc::focus;
2015-05-23 13:35:12 +02:00
if ( isEnabled() && statusBar() )
statusBar()->clearMessage();
2015-05-23 13:35:12 +02:00
}
updateButtonColor();
2015-05-23 13:35:12 +02:00
return on;
}
//----------------------------------------------------------------------
2015-09-15 23:07:24 +02:00
bool FButton::setFlat (bool on)
2015-05-23 13:35:12 +02:00
{
if ( on )
2016-01-24 14:53:09 +01:00
flags |= fc::flat;
2015-05-23 13:35:12 +02:00
else
2016-01-24 14:53:09 +01:00
flags &= ~fc::flat;
2015-05-23 13:35:12 +02:00
return on;
}
//----------------------------------------------------------------------
2015-09-15 23:07:24 +02:00
bool FButton::setShadow (bool on)
2015-05-23 13:35:12 +02:00
{
if ( on
&& (Encoding != fc::VT100 || isTeraTerm() )
&& Encoding != fc::ASCII )
2016-01-24 14:53:09 +01:00
flags |= fc::shadow;
2015-05-23 13:35:12 +02:00
else
2016-01-24 14:53:09 +01:00
flags &= ~fc::shadow;
2015-05-23 13:35:12 +02:00
return on;
}
//----------------------------------------------------------------------
2015-09-15 23:07:24 +02:00
bool FButton::setDown (bool on)
2015-05-23 13:35:12 +02:00
{
if ( button_down != on )
{
button_down = on;
redraw();
}
return on;
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FButton::onKeyPress (FKeyEvent* ev)
2015-05-23 13:35:12 +02:00
{
int key;
if ( ! isEnabled() )
return;
2015-09-20 05:44:50 +02:00
key = ev->key();
2015-05-23 13:35:12 +02:00
switch ( key )
{
case fc::Fkey_return:
case fc::Fkey_enter:
case fc::Fkey_space:
if ( click_animation )
{
setDown();
addTimer(click_time);
}
2015-05-23 13:35:12 +02:00
processClick();
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
void FButton::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
{
setUp();
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();
if ( statusBar() )
statusBar()->drawMessage();
}
2015-09-20 05:44:50 +02:00
FPoint gPos = ev->getGlobalPos();
2015-05-23 13:35:12 +02:00
if ( getGeometryGlobal().contains(gPos) )
setDown();
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FButton::onMouseUp (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 ( button_down )
{
setUp();
2015-09-20 05:44:50 +02:00
if ( getGeometryGlobal().contains(ev->getGlobalPos()) )
2015-05-23 13:35:12 +02:00
processClick();
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FButton::onMouseMove (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;
2015-09-20 05:44:50 +02:00
FPoint gPos = ev->getGlobalPos();
if ( click_animation )
{
if ( getGeometryGlobal().contains(gPos) )
setDown();
else
setUp();
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FButton::onTimer (FTimerEvent* ev)
{
delTimer(ev->timerId());
setUp();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FButton::onAccel (FAccelEvent* ev)
2015-05-23 13:35:12 +02:00
{
if ( isEnabled() )
{
if ( ! hasFocus() )
{
2015-09-20 05:44:50 +02:00
FWidget* focused_widget = static_cast<FWidget*>(ev->focusedWidget());
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);
setFocus();
if ( focused_widget )
focused_widget->redraw();
if ( click_animation )
setDown();
else
redraw();
2015-05-23 13:35:12 +02:00
if ( statusBar() )
statusBar()->drawMessage();
}
else if ( click_animation )
2015-05-23 13:35:12 +02:00
setDown();
if ( click_animation )
addTimer(click_time);
2015-05-23 13:35:12 +02:00
processClick();
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FButton::onFocusIn (FFocusEvent*)
{
if ( statusBar() )
statusBar()->drawMessage();
}
//----------------------------------------------------------------------
void FButton::onFocusOut (FFocusEvent*)
{
if ( statusBar() )
{
statusBar()->clearMessage();
statusBar()->drawMessage();
}
}
//----------------------------------------------------------------------
void FButton::setText (const FString& txt)
{
if ( txt )
text = txt;
else
text = "";
2015-09-22 04:18:20 +02:00
detectHotkey();
2015-05-23 13:35:12 +02:00
}