finalcut/src/ftogglebutton.cpp

660 lines
14 KiB
C++
Raw Normal View History

// File: ftogglebutton.cpp
// Provides: class FToggleButton
2015-05-23 13:35:12 +02:00
#include "fapp.h"
#include "fbuttongroup.h"
#include "fstatusbar.h"
#include "ftogglebutton.h"
//----------------------------------------------------------------------
// class FToggleButton
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
FToggleButton::FToggleButton (FWidget* parent)
2015-09-22 04:18:20 +02:00
: FWidget(parent)
, checked(false)
, label_offset_pos(0)
, button_width(0)
, button_group(0)
, focus_inside_group(true)
, text()
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
if ( parent && std::strcmp ( parent->getClassName()
, const_cast<char*>("FButtonGroup") ) == 0 )
2015-05-23 13:35:12 +02:00
{
setGroup( static_cast<FButtonGroup*>(parent) );
2017-03-12 00:29:56 +01:00
if ( hasGroup() )
getGroup()->insert(this); // insert into button group
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
2015-12-21 18:48:38 +01:00
FToggleButton::FToggleButton (const FString& txt, FWidget* parent)
2015-09-22 04:18:20 +02:00
: FWidget(parent)
, checked(false)
, label_offset_pos(0)
, button_width(0)
, button_group(0)
, focus_inside_group(true)
, text()
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
setText(txt);
if ( parent && std::strcmp ( parent->getClassName()
, const_cast<char*>("FButtonGroup") ) == 0 )
2015-05-23 13:35:12 +02:00
{
setGroup( static_cast<FButtonGroup*>(parent) );
2017-03-12 00:29:56 +01:00
if ( hasGroup() )
getGroup()->insert(this); // insert into button group
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
FToggleButton::~FToggleButton() // destructor
{
2015-11-12 01:33:16 +01:00
delAccelerator();
2017-03-12 00:29:56 +01:00
if ( hasGroup() )
getGroup()->remove(this);
2015-05-23 13:35:12 +02:00
}
// public methods of FToggleButton
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FToggleButton::setGeometry (int x, int y, int w, int h, bool adjust)
2015-05-23 13:35:12 +02:00
{
int min_width = button_width + int(text.getLength());
if ( w < min_width )
w = min_width;
2015-05-23 13:35:12 +02:00
2017-03-12 00:29:56 +01:00
const FRect geometry(x, y, w, h);
if ( hasGroup() )
getGroup()->checkScrollSize(geometry);
FWidget::setGeometry(x, y, w, h, adjust);
}
//----------------------------------------------------------------------
bool FToggleButton::setNoUnderline (bool on)
{
if ( on )
flags |= fc::no_underline;
else
flags &= ~fc::no_underline;
return on;
}
//----------------------------------------------------------------------
bool FToggleButton::setEnable (bool on)
{
FWidget::setEnable(on);
if ( on )
2015-05-23 13:35:12 +02:00
{
2016-01-24 14:53:09 +01:00
flags |= fc::active;
setHotkeyAccelerator();
2015-05-23 13:35:12 +02:00
if ( hasFocus() )
{
setForegroundColor (wc.toggle_button_active_focus_fg);
setBackgroundColor (wc.toggle_button_active_focus_bg);
2015-05-23 13:35:12 +02:00
}
else
{
setForegroundColor (wc.toggle_button_active_fg);
setBackgroundColor (wc.toggle_button_active_bg);
2015-05-23 13:35:12 +02:00
}
}
else
2015-05-23 13:35:12 +02:00
{
flags &= ~fc::active;
delAccelerator();
setForegroundColor (wc.toggle_button_inactive_fg);
setBackgroundColor (wc.toggle_button_inactive_bg);
2015-05-23 13:35:12 +02:00
}
return on;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FToggleButton::setFocus (bool on)
2015-05-23 13:35:12 +02:00
{
FWidget::setFocus(on);
2015-05-23 13:35:12 +02:00
if ( on )
{
flags |= fc::focus;
2015-05-23 13:35:12 +02:00
if ( isEnabled() )
{
if ( isRadioButton() )
focus_inside_group = false;
2015-05-23 13:35:12 +02:00
setForegroundColor (wc.toggle_button_active_focus_fg);
setBackgroundColor (wc.toggle_button_active_focus_bg);
2015-05-23 13:35:12 +02:00
if ( getStatusBar() )
{
2017-03-17 22:59:06 +01:00
const FString msg = getStatusbarMessage();
const FString curMsg = getStatusBar()->getMessage();
2015-05-23 13:35:12 +02:00
if ( curMsg != msg )
getStatusBar()->setMessage(msg);
}
2015-10-01 03:48:58 +02:00
}
}
else
{
flags &= ~fc::focus;
if ( isEnabled() )
2015-10-01 03:48:58 +02:00
{
setForegroundColor (wc.toggle_button_active_fg);
setBackgroundColor (wc.toggle_button_active_bg);
if ( getStatusBar() )
getStatusBar()->clearMessage();
2015-10-01 03:48:58 +02:00
}
}
return on;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FToggleButton::setChecked (bool on)
2015-05-23 13:35:12 +02:00
{
if ( checked != on )
2015-05-23 13:35:12 +02:00
{
checked = on;
processToggle();
2015-05-23 13:35:12 +02:00
}
return checked;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2017-03-17 22:59:06 +01:00
void FToggleButton::setText (const FString txt)
2015-05-23 13:35:12 +02:00
{
text = txt;
setWidth(button_width + int(text.getLength()));
if ( isEnabled() )
2015-05-23 13:35:12 +02:00
{
delAccelerator();
setHotkeyAccelerator();
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FToggleButton::hide()
2015-05-23 13:35:12 +02:00
{
int size;
short fg, bg;
char* blank;
FWidget* parent_widget = getParentWidget();
2015-05-23 13:35:12 +02:00
FWidget::hide();
if ( parent_widget )
{
fg = parent_widget->getForegroundColor();
bg = parent_widget->getBackgroundColor();
}
else
{
fg = wc.dialog_fg;
bg = wc.dialog_bg;
}
2015-05-23 13:35:12 +02:00
setColor (fg, bg);
size = getWidth();
2015-05-23 13:35:12 +02:00
if ( size < 0 )
return;
2015-05-23 13:35:12 +02:00
blank = new char[size+1];
std::memset(blank, ' ', uLong(size));
blank[size] = '\0';
setPrintPos (1, 1);
print (blank);
delete[] blank;
}
//----------------------------------------------------------------------
void FToggleButton::onMouseDown (FMouseEvent* ev)
{
if ( ev->getButton() != fc::LeftButton )
return;
if ( hasFocus() )
return;
2015-05-23 13:35:12 +02:00
FWidget* focused_widget = getFocusWidget();
FFocusEvent out (fc::FocusOut_Event);
FApplication::queueEvent(focused_widget, &out);
setFocus();
2015-05-23 13:35:12 +02:00
if ( focused_widget )
focused_widget->redraw();
2015-10-11 21:56:16 +02:00
redraw();
2015-05-23 13:35:12 +02:00
if ( getStatusBar() )
2015-05-23 13:35:12 +02:00
{
getStatusBar()->drawMessage();
updateTerminal();
flush_out();
}
}
//----------------------------------------------------------------------
void FToggleButton::onMouseUp (FMouseEvent* ev)
{
if ( ev->getButton() != fc::LeftButton )
return;
if ( ! getTermGeometry().contains(ev->getTermPos()) )
return;
if ( isRadioButton() )
{
if ( ! checked )
{
checked = true;
processToggle();
2015-05-23 13:35:12 +02:00
}
}
else
{
checked = not checked;
processToggle();
2015-05-23 13:35:12 +02:00
}
2015-10-11 21:56:16 +02:00
redraw();
processClick();
2015-05-23 13:35:12 +02:00
}
2017-03-12 00:29:56 +01:00
//----------------------------------------------------------------------
void FToggleButton::onWheel (FWheelEvent* ev)
{
if ( ! hasGroup() )
return;
getGroup()->onWheel(ev);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FToggleButton::onAccel (FAccelEvent* ev)
2015-05-23 13:35:12 +02:00
{
if ( ! isEnabled() )
return;
2015-05-23 13:35:12 +02:00
if ( ! hasFocus() )
{
FWidget* focused_widget = static_cast<FWidget*>(ev->focusedWidget());
FFocusEvent out (fc::FocusOut_Event);
FApplication::queueEvent(focused_widget, &out);
setFocus();
2015-05-23 13:35:12 +02:00
if ( focused_widget )
focused_widget->redraw();
}
2015-05-23 13:35:12 +02:00
if ( isRadioButton() )
{
if ( ! checked )
{
checked = true;
processToggle();
}
}
else
{
checked = not checked;
processToggle();
}
redraw();
if ( getStatusBar() )
{
getStatusBar()->drawMessage();
updateTerminal();
flush_out();
}
processClick();
ev->accept();
2015-05-23 13:35:12 +02:00
}
2015-07-06 10:50:46 +02:00
//----------------------------------------------------------------------
void FToggleButton::onFocusIn (FFocusEvent*)
2015-07-06 10:50:46 +02:00
{
if ( getStatusBar() )
getStatusBar()->drawMessage();
2015-07-06 10:50:46 +02:00
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FToggleButton::onFocusOut (FFocusEvent* out_ev)
2015-05-23 13:35:12 +02:00
{
if ( getStatusBar() )
{
getStatusBar()->clearMessage();
getStatusBar()->drawMessage();
}
2015-05-23 13:35:12 +02:00
2017-03-12 00:29:56 +01:00
if ( ! hasGroup() )
2015-05-23 13:35:12 +02:00
return;
if ( ! focus_inside_group && isRadioButton() )
2015-05-23 13:35:12 +02:00
{
focus_inside_group = true;
out_ev->ignore();
2015-05-23 13:35:12 +02:00
if ( out_ev->getFocusType() == fc::FocusNextWidget )
getGroup()->focusNextChild();
2015-09-20 05:44:50 +02:00
if ( out_ev->getFocusType() == fc::FocusPreviousWidget )
getGroup()->focusPrevChild();
2015-05-23 13:35:12 +02:00
redraw();
2015-05-23 13:35:12 +02:00
}
else if ( this == getGroup()->getLastButton()
&& out_ev->getFocusType() == fc::FocusNextWidget )
{
out_ev->ignore();
getGroup()->focusNextChild();
redraw();
}
else if ( this == getGroup()->getFirstButton()
&& out_ev->getFocusType() == fc::FocusPreviousWidget )
{
out_ev->ignore();
getGroup()->focusPrevChild();
redraw();
}
2015-05-23 13:35:12 +02:00
}
// protected methods of FToggleButton
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
uChar FToggleButton::getHotkey()
2015-05-23 13:35:12 +02:00
{
uInt length;
if ( text.isEmpty() )
return 0;
length = text.getLength();
2015-05-23 13:35:12 +02:00
for (uInt i=0; i < length; i++)
{
try
{
if ( (i+1 < length) && (text[i] == '&') )
return uChar(text[++i]);
}
catch (const std::out_of_range&)
{
return 0;;
}
}
return 0;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FToggleButton::setHotkeyAccelerator()
2015-05-23 13:35:12 +02:00
{
int hotkey = getHotkey();
2015-05-23 13:35:12 +02:00
if ( hotkey )
2015-05-23 13:35:12 +02:00
{
if ( std::isalpha(hotkey) || std::isdigit(hotkey) )
2015-05-23 13:35:12 +02:00
{
addAccelerator (std::tolower(hotkey));
addAccelerator (std::toupper(hotkey));
// Meta + hotkey
addAccelerator (fc::Fmkey_meta + std::tolower(hotkey));
2015-05-23 13:35:12 +02:00
}
else
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
}
//----------------------------------------------------------------------
bool FToggleButton::isRadioButton() const
2015-05-23 13:35:12 +02:00
{
return ( std::strcmp ( getClassName()
, const_cast<char*>("FRadioButton") ) == 0 );
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FToggleButton::isCheckboxButton() const
{
return ( std::strcmp ( getClassName()
, const_cast<char*>("FCheckBox") ) == 0 );
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FToggleButton::draw()
{
bool isFocus = ((flags & fc::focus) != 0);
if ( isFocus && getStatusBar() )
2015-05-23 13:35:12 +02:00
{
2017-03-17 22:59:06 +01:00
const FString msg = getStatusbarMessage();
const FString curMsg = getStatusBar()->getMessage();
2015-05-23 13:35:12 +02:00
if ( curMsg != msg )
2015-05-23 13:35:12 +02:00
{
getStatusBar()->setMessage(msg);
getStatusBar()->drawMessage();
2015-05-23 13:35:12 +02:00
}
}
// set the cursor to the button
if ( isRadioButton() || isCheckboxButton() )
setCursorPos (2, 1);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FToggleButton::drawLabel()
2015-05-23 13:35:12 +02:00
{
uInt length, i;
int hotkeypos;
FString txt;
wchar_t* LabelText;
register wchar_t* src;
register wchar_t* dest;
bool isActive, isNoUnderline;
2015-05-23 13:35:12 +02:00
if ( ! isVisible() )
return;
if ( text.isNull() || text.isEmpty() )
2015-05-23 13:35:12 +02:00
return;
length = text.getLength();
hotkeypos = -1;
LabelText = new wchar_t[length+1]();
txt = text;
src = const_cast<wchar_t*>(txt.wc_str());
dest = const_cast<wchar_t*>(LabelText);
isActive = ((flags & fc::active) != 0);
isNoUnderline = ((flags & fc::no_underline) != 0);
// find hotkey position in string
// + generate a new string without the '&'-sign
for (i=0; i < length; i++)
2015-05-23 13:35:12 +02:00
{
if ( (i < length) && (txt[i] == '&') && (hotkeypos == -1) )
2015-05-23 13:35:12 +02:00
{
hotkeypos = int(i);
i++;
src++;
2015-05-23 13:35:12 +02:00
}
*dest++ = *src++;
}
if ( hotkeypos != -1 )
length--;
2015-05-23 13:35:12 +02:00
setPrintPos (1 + label_offset_pos, 1);
if ( isMonochron() )
setReverse(true);
if ( isEnabled() )
setColor (wc.label_fg, wc.label_bg);
else
setColor (wc.label_inactive_fg, wc.label_inactive_bg);
2015-05-23 13:35:12 +02:00
for (int z=0; z < int(length); z++)
{
if ( (z == hotkeypos) && isActive )
2015-05-23 13:35:12 +02:00
{
setColor (wc.label_hotkey_fg, wc.label_hotkey_bg);
if ( ! isNoUnderline )
setUnderline();
print ( LabelText[z] );
if ( ! isNoUnderline )
unsetUnderline();
setColor (wc.label_fg, wc.label_bg);
2015-05-23 13:35:12 +02:00
}
else
print (LabelText[z]);
}
if ( isMonochron() )
setReverse(false);
delete[] LabelText;
}
//----------------------------------------------------------------------
void FToggleButton::processClick()
{
emitCallback("clicked");
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FToggleButton::processToggle()
2015-05-23 13:35:12 +02:00
{
emitCallback("toggled");
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FToggleButton::onKeyPress (FKeyEvent* ev)
2015-05-23 13:35:12 +02:00
{
int key;
2015-05-23 13:35:12 +02:00
if ( ! isEnabled() )
return;
key = ev->key();
switch ( key )
{
case fc::Fkey_return:
case fc::Fkey_enter:
case fc::Fkey_space:
if ( isRadioButton() )
{
if ( ! checked )
{
checked = true;
processToggle();
}
}
else
{
checked = not checked;
processToggle();
}
processClick();
ev->accept();
break;
case fc::Fkey_down:
case fc::Fkey_right:
focus_inside_group = true;
focusNextChild();
ev->accept();
break;
case fc::Fkey_up:
case fc::Fkey_left:
focus_inside_group = true;
focusPrevChild();
ev->accept();
break;
default:
break;
}
if ( ev->isAccepted() )
{
draw();
updateTerminal();
2015-05-23 13:35:12 +02:00
}
}
// private methods of FToggleButton
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FToggleButton::setGroup (FButtonGroup* btngroup)
2015-05-23 13:35:12 +02:00
{
button_group = btngroup;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FToggleButton::init()
2015-05-23 13:35:12 +02:00
{
setGeometry (1, 1, 4, 1, false); // initialize geometry values
if ( hasFocus() )
flags = fc::focus;
2015-05-23 13:35:12 +02:00
if ( isEnabled() )
{
flags |= fc::active;
if ( hasFocus() )
{
setForegroundColor (wc.toggle_button_active_focus_fg);
setBackgroundColor (wc.toggle_button_active_focus_bg);
}
else
{
setForegroundColor (wc.toggle_button_active_fg);
setBackgroundColor (wc.toggle_button_active_bg);
}
}
else // inactive
{
setForegroundColor (wc.label_inactive_fg);
setBackgroundColor (wc.label_inactive_bg);
2015-05-23 13:35:12 +02:00
}
}