finalcut/src/fbuttongroup.cpp

631 lines
13 KiB
C++
Raw Normal View History

// File: fbuttongroup.cpp
// Provides: class FButtonGroup
2015-05-23 13:35:12 +02:00
#include "fapp.h"
#include "fbuttongroup.h"
#include "fstatusbar.h"
#include "ftogglebutton.h"
//----------------------------------------------------------------------
// class FButtonGroup
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FButtonGroup::FButtonGroup(FWidget* parent)
: FWidget(parent)
, text()
, border(true)
, buttonlist()
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
}
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FButtonGroup::FButtonGroup (const FString& txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, border(true)
, buttonlist()
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);
}
//----------------------------------------------------------------------
FButtonGroup::~FButtonGroup() // destructor
{
FButtonGroup::FButtonList::iterator iter;
if ( buttonlist.empty() )
return;
iter = buttonlist.begin();
while ( iter != buttonlist.end() )
{
(*iter)->setGroup(0);
iter = buttonlist.erase(iter);
}
}
// private methods of FButtonGroup
//----------------------------------------------------------------------
void FButtonGroup::init()
{
setTopPadding(1);
setLeftPadding(1);
setBottomPadding(1);
setRightPadding(1);
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
setForegroundColor (wc.label_fg);
setBackgroundColor (wc.label_bg);
2015-05-23 13:35:12 +02:00
buttonlist.clear(); // no buttons yet
}
//----------------------------------------------------------------------
bool FButtonGroup::isRadioButton(FToggleButton* button) const
{
if ( ! button )
return false;
return bool ( std::strcmp ( button->getClassName()
, const_cast<char*>("FRadioButton") ) == 0 );
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FButtonGroup::directFocus()
{
if ( ! hasFocusedButton() )
{
bool found_checked = false;
if ( hasCheckedButton() && ! buttonlist.empty() )
{
FButtonGroup::FButtonList::const_iterator iter, end;
iter = buttonlist.begin();
end = buttonlist.end();
while ( iter != end )
{
if ( (*iter)->isChecked() )
{
if ( isRadioButton(*iter) )
{
found_checked = true;
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);
(*iter)->setFocus();
2015-05-23 13:35:12 +02:00
if ( focused_widget )
focused_widget->redraw();
focused_widget = getFocusWidget();
if ( focused_widget )
focused_widget->redraw();
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
break;
}
2015-05-23 13:35:12 +02:00
++iter;
}
}
if ( ! found_checked )
{
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);
focusFirstChild();
2015-05-23 13:35:12 +02:00
if ( focused_widget )
focused_widget->redraw();
focused_widget = getFocusWidget();
if ( focused_widget )
focused_widget->redraw();
2015-05-23 13:35:12 +02:00
}
}
if ( statusBar() )
{
statusBar()->drawMessage();
updateTerminal();
flush_out();
}
}
// protected methods of FButtonGroup
//----------------------------------------------------------------------
void FButtonGroup::draw()
{
updateVTerm(false);
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(true);
setColor();
2015-05-23 13:35:12 +02:00
if ( border )
drawBorder();
2015-05-23 13:35:12 +02:00
drawLabel();
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(false);
updateVTerm(true);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
uChar FButtonGroup::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 FButtonGroup::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 FButtonGroup::drawLabel()
{
wchar_t* LabelText;
register wchar_t* src;
register wchar_t* dest;
FString txt;
uInt length;
int hotkeypos;
bool isActive, isNoUnderline;
if ( text.isNull() || text.isEmpty() )
return;
2015-09-22 04:18:20 +02:00
txt = " " + text + " ";
2015-05-23 13:35:12 +02:00
length = txt.getLength();
hotkeypos = -1;
2015-10-17 19:40:43 +02:00
LabelText = new wchar_t[length+1]();
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 (uInt i=0; i < length; i++)
{
if ( (i < length) && (txt[i] == '&') && (hotkeypos == -1) )
{
hotkeypos = int(i);
i++;
src++;
}
*dest++ = *src++;
}
if ( hotkeypos != -1 )
length--;
if ( border )
setPrintPos (2, 1);
2015-05-23 13:35:12 +02:00
else
setPrintPos (0, 1);
2015-05-23 13:35:12 +02:00
if ( isEnabled() )
setColor(wc.label_emphasis_fg, wc.label_bg);
else
setColor(wc.label_inactive_fg, wc.label_inactive_bg);
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_emphasis_fg, wc.label_bg);
}
else
print ( LabelText[z] );
}
2015-05-23 13:35:12 +02:00
delete[] LabelText;
}
// public methods of FButtonGroup
//----------------------------------------------------------------------
void FButtonGroup::hide()
{
int size;
short fg, bg;
2015-05-23 13:35:12 +02:00
char* blank;
FWidget::hide();
FWidget* parent_widget = getParentWidget();
2015-05-23 13:35:12 +02:00
if ( ! buttonlist.empty() )
{
FButtonGroup::FButtonList::const_iterator iter, end;
iter = buttonlist.begin();
end = buttonlist.end();
while ( iter != end )
{
(*iter)->hide();
++iter;
}
}
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();
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';
for (int y=0; y < getHeight(); y++)
2015-05-23 13:35:12 +02:00
{
setPrintPos (1, 1+y);
2015-05-23 13:35:12 +02:00
print (blank);
}
2015-05-23 13:35:12 +02:00
delete[] blank;
}
//----------------------------------------------------------------------
2015-12-21 18:48:38 +01:00
void FButtonGroup::insert (FToggleButton* button)
2015-05-23 13:35:12 +02:00
{
if ( ! button )
return;
if ( button->group() )
2015-05-23 13:35:12 +02:00
button->group()->remove(button);
// setChecked the first FRadioButton
if ( buttonlist.size() == 1 && isRadioButton(*buttonlist.begin()) )
(*buttonlist.begin())->setChecked();
button->setGroup(this);
buttonlist.push_back(button);
button->addCallback
(
"toggled",
_METHOD_CALLBACK (this, &FButtonGroup::cb_buttonToggled)
2015-05-23 13:35:12 +02:00
);
}
//----------------------------------------------------------------------
2015-12-21 18:48:38 +01:00
void FButtonGroup::remove (FToggleButton* button)
2015-05-23 13:35:12 +02:00
{
FButtonGroup::FButtonList::iterator iter;
if ( ! button || buttonlist.empty() )
2015-05-23 13:35:12 +02:00
return;
iter = buttonlist.begin();
while ( iter != buttonlist.end() )
{
if ( (*iter) == button )
{
iter = buttonlist.erase(iter);
button->setGroup(0);
button->delCallback(this);
break;
}
else
++iter;
}
}
//----------------------------------------------------------------------
void FButtonGroup::cb_buttonToggled (FWidget* widget, void*)
{
FToggleButton* button = static_cast<FToggleButton*>(widget);
FButtonGroup::FButtonList::const_iterator iter, end;
if ( ! button->isChecked() )
return;
2015-05-23 13:35:12 +02:00
if ( buttonlist.empty() )
return;
iter = buttonlist.begin();
end = buttonlist.end();
while ( iter != end )
{
if ( (*iter) != button
&& (*iter)->isChecked()
&& isRadioButton(*iter) )
{
(*iter)->unsetChecked();
2015-05-23 13:35:12 +02:00
if ( (*iter)->isVisible() && (*iter)->isShown() )
(*iter)->redraw();
}
2015-05-23 13:35:12 +02:00
++iter;
}
}
//----------------------------------------------------------------------
FToggleButton* FButtonGroup::getFirstButton()
{
if ( ! buttonlist.empty() )
{
FButtonGroup::FButtonList::const_iterator iter, end;
iter = buttonlist.begin();
end = buttonlist.end();
while ( iter != end )
{
if ( (*iter)->isEnabled() && (*iter)->acceptFocus() )
return (*iter);
2015-05-23 13:35:12 +02:00
++iter;
}
}
2015-05-23 13:35:12 +02:00
return 0;
}
//----------------------------------------------------------------------
FToggleButton* FButtonGroup::getLastButton()
{
if ( ! buttonlist.empty() )
{
FButtonGroup::FButtonList::const_iterator iter, begin;
begin = buttonlist.begin();
iter = buttonlist.end();
2015-05-23 13:35:12 +02:00
do
{
--iter;
2015-05-23 13:35:12 +02:00
if ( (*iter)->isEnabled() && (*iter)->acceptFocus() )
return (*iter);
}
while ( iter != begin );
}
2015-05-23 13:35:12 +02:00
return 0;
}
//----------------------------------------------------------------------
bool FButtonGroup::hasFocusedButton()
{
if ( ! buttonlist.empty() )
{
FButtonGroup::FButtonList::const_iterator iter, end;
iter = buttonlist.begin();
end = buttonlist.end();
while ( iter != end )
{
if ( (*iter)->hasFocus() )
return true;
2015-05-23 13:35:12 +02:00
++iter;
}
}
2015-05-23 13:35:12 +02:00
return false;
}
//----------------------------------------------------------------------
bool FButtonGroup::hasCheckedButton()
{
if ( ! buttonlist.empty() )
{
FButtonGroup::FButtonList::const_iterator iter, end;
iter = buttonlist.begin();
end = buttonlist.end();
while ( iter != end )
{
if ( (*iter)->isChecked() )
return true;
2015-05-23 13:35:12 +02:00
++iter;
}
}
2015-05-23 13:35:12 +02:00
return false;
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FButtonGroup::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;
2015-05-23 13:35:12 +02:00
directFocus();
}
//----------------------------------------------------------------------
void FButtonGroup::onAccel (FAccelEvent*)
{
directFocus();
}
//----------------------------------------------------------------------
void FButtonGroup::onFocusIn (FFocusEvent* in_ev)
{
if ( hasCheckedButton() && ! buttonlist.empty() )
{
FButtonGroup::FButtonList::const_iterator iter, end;
iter = buttonlist.begin();
end = buttonlist.end();
while ( iter != end )
{
if ( (*iter)->isChecked() )
{
if ( isRadioButton(*iter) )
{
in_ev->ignore();
FWidget* prev_element = getFocusWidget();
(*iter)->setFocus();
if ( prev_element )
prev_element->redraw();
2015-05-23 13:35:12 +02:00
(*iter)->redraw();
}
2015-05-23 13:35:12 +02:00
break;
}
2015-05-23 13:35:12 +02:00
++iter;
}
}
2015-05-23 13:35:12 +02:00
if ( in_ev->isAccepted() )
{
2016-01-17 02:57:08 +01:00
if ( in_ev->getFocusType() == fc::FocusNextWidget )
2015-05-23 13:35:12 +02:00
{
in_ev->ignore();
FWidget* prev_element = getFocusWidget();
focusFirstChild();
if ( prev_element )
prev_element->redraw();
if ( getFocusWidget() )
getFocusWidget()->redraw();
2015-05-23 13:35:12 +02:00
}
2016-01-17 02:57:08 +01:00
else if ( in_ev->getFocusType() == fc::FocusPreviousWidget )
2015-05-23 13:35:12 +02:00
{
in_ev->ignore();
FWidget* prev_element = getFocusWidget();
focusLastChild();
if ( prev_element )
prev_element->redraw();
if ( getFocusWidget() )
getFocusWidget()->redraw();
2015-05-23 13:35:12 +02:00
}
}
2015-05-23 13:35:12 +02:00
if ( statusBar() )
{
statusBar()->drawMessage();
updateTerminal();
flush_out();
}
}
//----------------------------------------------------------------------
void FButtonGroup::onFocusOut (FFocusEvent*)
2015-09-22 04:18:20 +02:00
{ }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FButtonGroup::setEnable (bool on)
{
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
}
2015-05-23 13:35:12 +02:00
return on;
}
//----------------------------------------------------------------------
bool FButtonGroup::setBorder(bool on)
{
if ( on )
border = true;
else
border = false;
2015-05-23 13:35:12 +02:00
return on;
}
//----------------------------------------------------------------------
void FButtonGroup::setText (const FString& txt)
{
2015-09-22 04:18:20 +02:00
text = txt;
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();
}
}