finalcut/src/ftogglebutton.cpp

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