finalcut/src/fbutton.cpp

755 lines
18 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fbutton.cpp - Widget FButton *
* *
* This file is part of the Final Cut widget toolkit *
* *
2019-01-16 16:00:15 +01:00
* Copyright 2012-2019 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* The Final Cut is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* The Final Cut 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
2015-05-23 13:35:12 +02:00
#include "final/fapplication.h"
#include "final/fcolorpair.h"
#include "final/fevent.h"
#include "final/fbutton.h"
#include "final/fstatusbar.h"
2015-05-23 13:35:12 +02:00
namespace finalcut
{
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FButton
//----------------------------------------------------------------------
// constructors and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FButton::FButton(FWidget* parent)
: FWidget(parent)
{
init();
}
//----------------------------------------------------------------------
FButton::FButton (const FString& txt, FWidget* parent)
: FWidget(parent)
, text{txt}
2015-09-22 04:18:20 +02:00
{
init();
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
}
// FButton operator
//----------------------------------------------------------------------
FButton& FButton::operator = (const FString& s)
{
setText(s);
return *this;
}
2015-05-23 13:35:12 +02:00
// public methods of FButton
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-11-07 22:06:58 +01:00
void FButton::setForegroundColor (FColor color)
2015-05-23 13:35:12 +02:00
{
FWidget::setForegroundColor(color);
updateButtonColor();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2018-11-07 22:06:58 +01:00
void FButton::setBackgroundColor (FColor color)
2015-05-23 13:35:12 +02:00
{
FWidget::setBackgroundColor(color);
updateButtonColor();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-11-07 22:06:58 +01:00
void FButton::setHotkeyForegroundColor (FColor color)
{
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_hotkey_fg = color;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-11-07 22:06:58 +01:00
void FButton::setFocusForegroundColor (FColor color)
{
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_focus_fg = color;
updateButtonColor();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2018-11-07 22:06:58 +01:00
void FButton::setFocusBackgroundColor (FColor color)
2015-05-23 13:35:12 +02:00
{
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_focus_bg = color;
updateButtonColor();
2015-05-23 13:35:12 +02:00
}
2015-09-22 04:18:20 +02:00
//----------------------------------------------------------------------
2018-11-07 22:06:58 +01:00
void FButton::setInactiveForegroundColor (FColor color)
2015-09-22 04:18:20 +02:00
{
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_inactive_fg = color;
updateButtonColor();
2015-09-22 04:18:20 +02:00
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-11-07 22:06:58 +01:00
void FButton::setInactiveBackgroundColor (FColor color)
2015-05-23 13:35:12 +02:00
{
// valid colors -1..254
if ( color == fc::Default || color >> 8 == 0 )
button_inactive_bg = color;
2015-05-23 13:35:12 +02:00
updateButtonColor();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-12-22 23:50:10 +01:00
bool FButton::setNoUnderline (bool enable)
{
return (setFlags().no_underline = enable);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-12-22 23:50:10 +01:00
bool FButton::setEnable (bool enable)
{
2018-12-22 23:50:10 +01:00
FWidget::setEnable(enable);
2015-05-23 13:35:12 +02:00
2018-12-22 23:50:10 +01:00
if ( enable )
setHotkeyAccelerator();
2015-05-23 13:35:12 +02:00
else
delAccelerator();
updateButtonColor();
2018-12-22 23:50:10 +01:00
return enable;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-12-22 23:50:10 +01:00
bool FButton::setFocus (bool enable)
{
2018-12-22 23:50:10 +01:00
FWidget::setFocus(enable);
2015-05-23 13:35:12 +02:00
2018-12-22 23:50:10 +01:00
if ( enable )
2015-05-23 13:35:12 +02:00
{
if ( isEnabled() )
{
if ( getStatusBar() )
2015-05-23 13:35:12 +02:00
{
2018-12-26 23:41:49 +01:00
const auto& msg = getStatusbarMessage();
const auto& curMsg = getStatusBar()->getMessage();
if ( curMsg != msg )
getStatusBar()->setMessage(msg);
2015-05-23 13:35:12 +02:00
}
}
}
else
2015-05-23 13:35:12 +02:00
{
if ( isEnabled() && getStatusBar() )
getStatusBar()->clearMessage();
2015-05-23 13:35:12 +02:00
}
updateButtonColor();
2018-12-22 23:50:10 +01:00
return enable;
}
//----------------------------------------------------------------------
2018-12-22 23:50:10 +01:00
bool FButton::setFlat (bool enable)
{
return (setFlags().flat = enable);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-12-22 23:50:10 +01:00
bool FButton::setShadow (bool enable)
{
2018-12-22 23:50:10 +01:00
if ( enable
&& getEncoding() != fc::VT100
&& getEncoding() != fc::ASCII )
{
setFlags().shadow = true;
setShadowSize(FSize(1, 1));
}
else
{
setFlags().shadow = false;
setShadowSize(FSize(0, 0));
}
2015-05-23 13:35:12 +02:00
return getFlags().shadow;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2018-12-22 23:50:10 +01:00
bool FButton::setDown (bool enable)
{
2018-12-22 23:50:10 +01:00
if ( button_down != enable )
{
2018-12-22 23:50:10 +01:00
button_down = enable;
redraw();
}
2018-12-22 23:50:10 +01:00
return enable;
}
//----------------------------------------------------------------------
void FButton::setText (const FString& txt)
{
2018-03-21 00:02:43 +01:00
if ( txt.isNull() )
2019-08-25 22:16:00 +02:00
text.setString("");
2018-03-21 00:02:43 +01:00
else
2019-08-25 22:16:00 +02:00
text.setString(txt);
2015-05-23 13:35:12 +02:00
detectHotkey();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::hide()
{
2019-08-25 22:16:00 +02:00
FColor fg{}, bg{};
auto parent_widget = getParentWidget();
FWidget::hide();
2015-05-23 13:35:12 +02:00
if ( parent_widget )
{
fg = parent_widget->getForegroundColor();
bg = parent_widget->getBackgroundColor();
}
2015-05-23 13:35:12 +02:00
else
{
const auto& wc = getFWidgetColors();
fg = wc.dialog_fg;
bg = wc.dialog_bg;
}
2015-05-23 13:35:12 +02:00
setColor (fg, bg);
2019-08-25 22:16:00 +02:00
std::size_t s = hasShadow() ? 1 : 0;
std::size_t f = isFlat() ? 1 : 0;
std::size_t size = getWidth() + s + (f << 1);
if ( size == 0 )
return;
char* blank = createBlankArray(size + 1);
2019-08-25 22:16:00 +02:00
for (std::size_t y{0}; y < getHeight() + s + (f << 1); y++)
2015-05-23 13:35:12 +02:00
{
print() << FPoint(1 - int(f), 1 + int(y - f)) << blank;
}
2018-10-20 22:50:35 +02:00
destroyBlankArray (blank);
}
//----------------------------------------------------------------------
void FButton::onKeyPress (FKeyEvent* ev)
{
if ( ! isEnabled() )
return;
2018-11-21 20:07:08 +01:00
FKey key = ev->key();
switch ( key )
{
case fc::Fkey_return:
case fc::Fkey_enter:
case fc::Fkey_space:
if ( click_animation )
{
setDown();
addTimer(click_time);
}
processClick();
ev->accept();
break;
default:
break;
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FButton::onMouseDown (FMouseEvent* ev)
{
if ( ev->getButton() != fc::LeftButton )
{
setUp();
return;
}
if ( ! hasFocus() )
{
auto focused_widget = getFocusWidget();
setFocus();
2015-05-23 13:35:12 +02:00
if ( focused_widget )
focused_widget->redraw();
if ( getStatusBar() )
getStatusBar()->drawMessage();
}
2019-08-25 22:16:00 +02:00
FPoint tPos(ev->getTermPos());
if ( getTermGeometry().contains(tPos) )
setDown();
}
//----------------------------------------------------------------------
void FButton::onMouseUp (FMouseEvent* ev)
{
if ( ev->getButton() != fc::LeftButton )
return;
if ( button_down )
2015-05-23 13:35:12 +02:00
{
setUp();
if ( getTermGeometry().contains(ev->getTermPos()) )
processClick();
}
}
//----------------------------------------------------------------------
void FButton::onMouseMove (FMouseEvent* ev)
{
if ( ev->getButton() != fc::LeftButton )
return;
2019-08-25 22:16:00 +02:00
FPoint tPos(ev->getTermPos());
if ( click_animation )
{
if ( getTermGeometry().contains(tPos) )
setDown();
else
setUp();
2015-05-23 13:35:12 +02:00
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::onTimer (FTimerEvent* ev)
{
delTimer(ev->getTimerId());
setUp();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::onAccel (FAccelEvent* ev)
{
if ( ! isEnabled() )
return;
if ( ! hasFocus() )
2015-05-23 13:35:12 +02:00
{
auto focused_widget = static_cast<FWidget*>(ev->focusedWidget());
2017-06-14 01:23:10 +02:00
if ( focused_widget && focused_widget->isWidget() )
2017-06-11 17:47:50 +02:00
{
setFocus();
2017-06-14 01:23:10 +02:00
focused_widget->redraw();
2017-06-11 17:47:50 +02:00
if ( click_animation )
setDown();
else
redraw();
if ( getStatusBar() )
getStatusBar()->drawMessage();
}
2015-05-23 13:35:12 +02:00
}
else if ( click_animation )
setDown();
2015-05-23 13:35:12 +02:00
if ( click_animation )
addTimer(click_time);
2015-10-11 21:56:16 +02:00
processClick();
ev->accept();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::onFocusIn (FFocusEvent*)
{
if ( getStatusBar() )
getStatusBar()->drawMessage();
}
//----------------------------------------------------------------------
void FButton::onFocusOut (FFocusEvent*)
{
if ( getStatusBar() )
2015-05-23 13:35:12 +02:00
{
getStatusBar()->clearMessage();
getStatusBar()->drawMessage();
}
}
// private methods of FButton
//----------------------------------------------------------------------
void FButton::init()
{
const auto& wc = getFWidgetColors();
setForegroundColor (wc.button_active_fg);
setBackgroundColor (wc.button_active_bg);
setShadow();
if ( ! text.isEmpty() )
detectHotkey();
}
//----------------------------------------------------------------------
void FButton::setHotkeyAccelerator()
{
2019-01-16 16:00:15 +01:00
FKey hotkey = getHotkey(text);
if ( hotkey > 0xff00 && hotkey < 0xff5f ) // full-width character
hotkey -= 0xfee0;
if ( hotkey )
{
2019-01-16 16:00:15 +01:00
if ( std::isalpha(int(hotkey)) || std::isdigit(int(hotkey)) )
{
2019-01-16 16:00:15 +01:00
addAccelerator (FKey(std::tolower(int(hotkey))));
addAccelerator (FKey(std::toupper(int(hotkey))));
// Meta + hotkey
2019-01-16 16:00:15 +01:00
addAccelerator (fc::Fmkey_meta + FKey(std::tolower(int(hotkey))));
}
else
2019-01-16 16:00:15 +01:00
addAccelerator (hotkey);
}
else
delAccelerator();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline void FButton::detectHotkey()
{
if ( isEnabled() )
{
delAccelerator();
setHotkeyAccelerator();
}
}
//----------------------------------------------------------------------
inline std::size_t FButton::clickAnimationIndent (FWidget* parent_widget)
{
if ( ! button_down || ! click_animation )
return 0;
// noshadow + indent one character to the right
if ( getFlags().flat )
clearFlatBorder();
2018-03-14 00:53:28 +01:00
else if ( hasShadow() )
clearShadow();
if ( parent_widget )
setColor ( parent_widget->getForegroundColor()
, parent_widget->getBackgroundColor() );
for (int y{1}; y <= int(getHeight()); y++)
{
print() << FPoint(1, y) << ' '; // clear one left █
}
return 1;
}
//----------------------------------------------------------------------
inline void FButton::clearRightMargin (FWidget* parent_widget)
{
if ( button_down || isNewFont() )
return;
// Restore the right background after button down
if ( parent_widget )
setColor ( parent_widget->getForegroundColor()
, parent_widget->getBackgroundColor() );
2019-08-25 22:16:00 +02:00
for (int y{1}; y <= int(getHeight()); y++)
{
if ( isMonochron() )
setReverse(true); // Light background
print() << FPoint(1 + int(getWidth()), y) << ' '; // clear right
if ( getFlags().active && isMonochron() )
setReverse(false); // Dark background
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
inline void FButton::drawMarginLeft()
{
// Print left margin
2015-05-23 13:35:12 +02:00
setColor (getForegroundColor(), button_bg);
2015-05-23 13:35:12 +02:00
2019-08-25 22:16:00 +02:00
for (std::size_t y{0}; y < getHeight(); y++)
2015-05-23 13:35:12 +02:00
{
print() << FPoint(1 + int(indent), 1 + int(y));
if ( isMonochron() && active_focus && y == vcenter_offset )
print (fc::BlackRightPointingPointer); // ►
else
print (space_char); // full block █
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline void FButton::drawMarginRight()
{
// Print right margin
2019-08-25 22:16:00 +02:00
for (std::size_t y{0}; y < getHeight(); y++)
2015-05-23 13:35:12 +02:00
{
print() << FPoint(int(getWidth() + indent), 1 + int(y));
2015-05-23 13:35:12 +02:00
if ( isMonochron() && active_focus && y == vcenter_offset )
print (fc::BlackLeftPointingPointer); // ◄
else
print (space_char); // full block █
}
}
//----------------------------------------------------------------------
inline void FButton::drawTopBottomBackground()
{
// Print top and bottom button background
if ( getHeight() < 2 )
return;
2015-05-23 13:35:12 +02:00
2019-08-25 22:16:00 +02:00
for (std::size_t y{0}; y < vcenter_offset; y++)
{
print() << FPoint(2 + int(indent), 1 + int(y));
2019-08-25 22:16:00 +02:00
for (std::size_t x{1}; x < getWidth() - 1; x++)
print (space_char); // █
}
2015-05-23 13:35:12 +02:00
2019-08-25 22:16:00 +02:00
for (std::size_t y{vcenter_offset + 1}; y < getHeight(); y++)
{
print() << FPoint(2 + int(indent), 1 + int(y));
2019-08-25 22:16:00 +02:00
for (std::size_t x{1}; x < getWidth() - 1; x++)
print (space_char); // █
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
inline void FButton::drawButtonTextLine (wchar_t button_text[])
{
2019-08-25 22:16:00 +02:00
std::size_t pos{};
print() << FPoint(2 + int(indent), 1 + int(vcenter_offset))
<< FColorPair (button_fg, button_bg);
if ( getWidth() < column_width + 1 )
center_offset = 0;
else
center_offset = (getWidth() - column_width - 1) / 2;
// Print button text line
for (pos = 0; pos < center_offset; pos++)
print (space_char); // █
if ( hotkeypos == NOT_SET )
setCursorPos (FPoint ( 2 + int(center_offset)
, 1 + int(vcenter_offset) )); // first character
2015-05-23 13:35:12 +02:00
else
setCursorPos (FPoint ( 2 + int(center_offset + hotkeypos)
, 1 + int(vcenter_offset) )); // hotkey
if ( ! getFlags().active && isMonochron() )
2018-11-20 21:11:04 +01:00
setReverse(true); // Light background
if ( active_focus && (isMonochron() || getMaxColor() < 16) )
setBold();
2015-05-23 13:35:12 +02:00
for ( std::size_t z{0}, columns{0}
; pos < center_offset + column_width && columns + 2 < getWidth()
; z++)
2015-05-23 13:35:12 +02:00
{
if ( z == hotkeypos && getFlags().active )
{
setColor (button_hotkey_fg, button_bg);
if ( ! active_focus && getMaxColor() < 16 )
setBold();
2015-05-23 13:35:12 +02:00
if ( ! getFlags().no_underline )
setUnderline();
2015-05-23 13:35:12 +02:00
print (button_text[z]);
2015-05-23 13:35:12 +02:00
if ( ! active_focus && getMaxColor() < 16 )
unsetBold();
2015-05-23 13:35:12 +02:00
if ( ! getFlags().no_underline )
unsetUnderline();
2015-09-20 05:44:50 +02:00
setColor (button_fg, button_bg);
}
else
{
print (button_text[z]);
}
auto char_width = getColumnWidth (button_text[z]);
columns += char_width;
pos += char_width;
2015-05-23 13:35:12 +02:00
}
if ( column_width + 1 >= getWidth() )
{
// Print ellipsis
print() << FPoint(int(getWidth() + indent) - 2, 1) << "..";
}
if ( active_focus && (isMonochron() || getMaxColor() < 16) )
unsetBold();
2015-05-23 13:35:12 +02:00
for (pos = center_offset + column_width; pos < getWidth() - 2; pos++)
print (space_char); // █
}
//----------------------------------------------------------------------
void FButton::draw()
{
2019-08-25 22:16:00 +02:00
wchar_t* button_text{};
auto parent_widget = getParentWidget();
auto txtlength = text.getLength();
column_width = getColumnWidth(text);
space_char = int(' ');
active_focus = getFlags().active && getFlags().focus;
try
{
button_text = new wchar_t[txtlength + 1]();
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
return;
2015-05-23 13:35:12 +02:00
}
if ( isMonochron() )
setReverse(true); // Light background
2015-05-23 13:35:12 +02:00
// Click animation preprocessing
indent = clickAnimationIndent (parent_widget);
// Clear right margin after animation
clearRightMargin (parent_widget);
if ( ! getFlags().active && isMonochron() )
space_char = fc::MediumShade; // ▒ simulates greyed out at Monochron
if ( isMonochron() && (getFlags().active || getFlags().focus) )
setReverse(false); // Dark background
if ( getFlags().flat && ! button_down )
drawFlatBorder();
hotkeypos = finalcut::getHotkeyPos(text.wc_str(), button_text, uInt(txtlength));
if ( hotkeypos != NOT_SET )
column_width--;
if ( getHeight() >= 2 )
vcenter_offset = (getHeight() - 1) / 2;
else
vcenter_offset = 0;
// Print left margin
drawMarginLeft();
// Print button text line
drawButtonTextLine(button_text);
// Print right margin
drawMarginRight();
// Print top and bottom button background
drawTopBottomBackground();
// Draw button shadow
if ( ! getFlags().flat && getFlags().shadow && ! button_down )
drawShadow();
2015-05-23 13:35:12 +02:00
if ( isMonochron() )
setReverse(false); // Dark background
2015-05-23 13:35:12 +02:00
delete[] button_text;
updateStatusBar();
}
//----------------------------------------------------------------------
void FButton::updateStatusBar()
{
if ( ! getFlags().focus || ! getStatusBar() )
return;
2015-05-23 13:35:12 +02:00
2018-12-26 23:41:49 +01:00
const auto& msg = getStatusbarMessage();
const auto& curMsg = getStatusBar()->getMessage();
if ( curMsg != msg )
{
getStatusBar()->setMessage(msg);
getStatusBar()->drawMessage();
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FButton::updateButtonColor()
2015-05-23 13:35:12 +02:00
{
if ( isEnabled() )
{
if ( hasFocus() )
{
button_fg = button_focus_fg;
button_bg = button_focus_bg;
}
else
{
button_fg = getForegroundColor();
2017-09-11 03:06:02 +02:00
button_bg = getBackgroundColor();
}
2015-05-23 13:35:12 +02:00
}
else // inactive
2015-05-23 13:35:12 +02:00
{
button_fg = button_inactive_fg;
button_bg = button_inactive_bg;
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FButton::processClick()
2015-05-23 13:35:12 +02:00
{
emitCallback("clicked");
2015-05-23 13:35:12 +02:00
}
} // namespace finalcut