finalcut/src/fbuttongroup.cpp

640 lines
15 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fbuttongroup.cpp - The FButtonGroup widget organizes FToggleButton *
* widgets in a group. *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2014-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/fbuttongroup.h"
#include "final/fstatusbar.h"
#include "final/ftogglebutton.h"
2015-05-23 13:35:12 +02:00
namespace finalcut
{
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FButtonGroup
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FButtonGroup::FButtonGroup(FWidget* parent)
: FScrollView(parent)
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)
: FScrollView(parent)
, text{txt}
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
{
if ( buttonlist.empty() )
return;
auto iter = buttonlist.begin();
2015-05-23 13:35:12 +02:00
while ( iter != buttonlist.end() )
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
toggle_button->setGroup(0);
2015-05-23 13:35:12 +02:00
iter = buttonlist.erase(iter);
}
}
// public methods of FButtonGroup
2017-03-12 00:29:56 +01:00
//----------------------------------------------------------------------
2017-11-03 05:04:27 +01:00
FToggleButton* FButtonGroup::getButton (int index) const
2017-03-12 00:29:56 +01:00
{
if ( buttonlist.empty() )
return 0;
if ( index <= 0 || index > int(getCount()) )
2017-03-12 00:29:56 +01:00
return 0;
auto iter = buttonlist.begin();
std::advance (iter, index - 1);
2017-03-12 00:29:56 +01:00
return static_cast<FToggleButton*>(*iter);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FToggleButton* FButtonGroup::getFirstButton()
2015-05-23 13:35:12 +02:00
{
auto widget = FWidget::getFirstFocusableWidget(buttonlist);
auto toggle_button = static_cast<FToggleButton*>(widget);
return toggle_button;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FToggleButton* FButtonGroup::getLastButton()
2015-05-23 13:35:12 +02:00
{
auto widget = FWidget::getLastFocusableWidget(buttonlist);
auto toggle_button = static_cast<FToggleButton*>(widget);
return toggle_button;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2018-12-22 23:50:10 +01:00
bool FButtonGroup::setEnable (bool enable)
2015-05-23 13:35:12 +02:00
{
2018-12-22 23:50:10 +01:00
FWidget::setEnable(enable);
2018-12-22 23:50:10 +01:00
if ( enable )
setHotkeyAccelerator();
else
delAccelerator();
2018-12-22 23:50:10 +01:00
return enable;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FButtonGroup::setText (const FString& txt)
2015-05-23 13:35:12 +02:00
{
text = txt;
2015-05-23 13:35:12 +02:00
if ( isEnabled() )
2015-05-23 13:35:12 +02:00
{
2015-11-12 01:33:16 +01:00
delAccelerator();
setHotkeyAccelerator();
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2017-03-12 00:29:56 +01:00
bool FButtonGroup::isChecked (int index) const
{
auto button = getButton(index);
2017-03-12 00:29:56 +01:00
if ( button )
return button->isChecked();
else
return false;
}
//----------------------------------------------------------------------
bool FButtonGroup::hasFocusedButton() const
2015-05-23 13:35:12 +02:00
{
if ( buttonlist.empty() )
return false;
2015-05-23 13:35:12 +02:00
auto iter = buttonlist.begin();
auto last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button->hasFocus() )
return true;
++iter;
2015-05-23 13:35:12 +02:00
}
return false;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2017-03-12 00:29:56 +01:00
bool FButtonGroup::hasCheckedButton() const
{
if ( buttonlist.empty() )
return false;
2015-05-23 13:35:12 +02:00
auto iter = buttonlist.begin();
auto last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button->isChecked() )
return true;
++iter;
2015-05-23 13:35:12 +02:00
}
return false;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FButtonGroup::hide()
{
std::size_t size;
2018-11-13 02:51:41 +01:00
FColor fg, bg;
2015-05-23 13:35:12 +02:00
FWidget::hide();
auto parent_widget = getParentWidget();
2015-05-23 13:35:12 +02:00
if ( ! buttonlist.empty() )
{
auto iter = buttonlist.begin();
auto last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
toggle_button->hide();
2015-05-23 13:35:12 +02:00
++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;
auto blank = createBlankArray(size + 1);
for (int y = 0; y < int(getHeight()); y++)
2015-05-23 13:35:12 +02:00
{
FWidget::setPrintPos (FPoint(1, 1 + y));
2015-05-23 13:35:12 +02:00
print (blank);
}
2018-10-20 22:50:35 +02:00
destroyBlankArray (blank);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
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->getGroup() )
button->getGroup()->remove(button);
2015-05-23 13:35:12 +02:00
// setChecked the first FRadioButton
if ( buttonlist.size() == 1 )
{
auto first_button = static_cast<FToggleButton*>(*buttonlist.begin());
if ( isRadioButton(first_button) )
first_button->setChecked();
}
2015-05-23 13:35:12 +02:00
button->setGroup(this);
buttonlist.push_back(button);
button->addCallback
(
"toggled",
F_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
{
if ( ! button || buttonlist.empty() )
2015-05-23 13:35:12 +02:00
return;
auto iter = buttonlist.begin();
2015-05-23 13:35:12 +02:00
while ( iter != buttonlist.end() )
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button == button )
2015-05-23 13:35:12 +02:00
{
iter = buttonlist.erase(iter);
button->setGroup(0);
button->delCallback(this);
break;
}
else
++iter;
}
}
2017-03-12 00:29:56 +01:00
//----------------------------------------------------------------------
void FButtonGroup::checkScrollSize (FToggleButton* button)
{
// Check and adjust the scroll size
checkScrollSize (button->getGeometry());
}
//----------------------------------------------------------------------
void FButtonGroup::checkScrollSize (const FRect& r)
{
// Check and adjust the scroll size
FRect scrollgeometry (1, 1, getScrollWidth(), getScrollHeight());
if ( ! scrollgeometry.contains(r) )
{
FRect r_combined = scrollgeometry.combined(r);
setScrollSize (r_combined.getSize());
2017-03-12 00:29:56 +01:00
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
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() )
{
auto iter = buttonlist.begin();
auto last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button->isChecked() )
2015-05-23 13:35:12 +02:00
{
if ( isRadioButton(toggle_button) )
2015-05-23 13:35:12 +02:00
{
auto prev_element = getFocusWidget();
2017-03-12 00:29:56 +01:00
in_ev->ignore();
toggle_button->setFocus();
2017-03-12 00:29:56 +01:00
FFocusEvent cfi (fc::ChildFocusIn_Event);
FApplication::sendEvent(this, &cfi);
FFocusEvent in (fc::FocusIn_Event);
FApplication::sendEvent(toggle_button, &in);
if ( prev_element )
prev_element->redraw();
toggle_button->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;
}
}
2015-05-23 13:35:12 +02:00
if ( in_ev->isAccepted() )
{
in_ev->ignore();
auto prev_element = getFocusWidget();
2016-01-17 02:57:08 +01:00
if ( in_ev->getFocusType() == fc::FocusNextWidget )
2015-05-23 13:35:12 +02:00
focusFirstChild();
2016-01-17 02:57:08 +01:00
else if ( in_ev->getFocusType() == fc::FocusPreviousWidget )
2015-05-23 13:35:12 +02:00
focusLastChild();
if ( prev_element )
prev_element->redraw();
if ( getFocusWidget() )
getFocusWidget()->redraw();
2015-05-23 13:35:12 +02:00
}
if ( getStatusBar() )
2015-05-23 13:35:12 +02:00
{
getStatusBar()->drawMessage();
2015-05-23 13:35:12 +02:00
updateTerminal();
flush_out();
}
}
// protected methods of FButtonGroup
//----------------------------------------------------------------------
void FButtonGroup::setHotkeyAccelerator()
{
2019-01-16 16:00:15 +01:00
FKey hotkey = getHotkey(text);
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
}
//----------------------------------------------------------------------
void FButtonGroup::draw()
2015-05-23 13:35:12 +02:00
{
if ( isMonochron() )
setReverse(true);
setColor();
clearArea();
if ( isMonochron() )
setReverse(false);
FScrollView::draw();
drawLabel();
}
//----------------------------------------------------------------------
void FButtonGroup::drawLabel()
{
wchar_t* LabelText;
if ( text.isNull() || text.isEmpty() )
return;
FString txt = " " + text + " ";
std::size_t length = txt.getLength();
2017-08-12 22:55:29 +02:00
try
{
LabelText = new wchar_t[length + 1]();
2017-08-12 22:55:29 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
2017-08-12 22:55:29 +02:00
return;
}
wchar_t* src = const_cast<wchar_t*>(txt.wc_str());
wchar_t* dest = const_cast<wchar_t*>(LabelText);
unsetViewportPrint();
auto hotkeypos = getHotkeyPos(src, dest, length);
if ( hotkeypos != NOT_SET )
length--;
if ( hasBorder() )
FWidget::setPrintPos (FPoint(2, 1));
else
FWidget::setPrintPos (FPoint(0, 1));
drawText (LabelText, hotkeypos, length);
setViewportPrint();
delete[] LabelText;
}
// private methods of FButtonGroup
//----------------------------------------------------------------------
bool FButtonGroup::isRadioButton (FToggleButton* button) const
{
if ( ! button )
return false;
return bool ( std::strcmp ( button->getClassName()
, C_STR("FRadioButton") ) == 0 );
}
//----------------------------------------------------------------------
void FButtonGroup::init()
{
setForegroundColor (wc.label_fg);
setBackgroundColor (wc.label_bg);
setMinimumSize (FSize(7, 4));
buttonlist.clear(); // no buttons yet
}
//----------------------------------------------------------------------
std::size_t FButtonGroup::getHotkeyPos ( wchar_t src[]
, wchar_t dest[]
, std::size_t length )
{
// find hotkey position in string
// + generate a new string without the '&'-sign
std::size_t pos = NOT_SET;
wchar_t* txt = src;
for (std::size_t i = 0; i < length; i++)
{
if ( i < length && txt[i] == L'&' && pos == NOT_SET )
{
pos = i;
i++;
src++;
}
2017-11-03 05:04:27 +01:00
*dest++ = *src++;
}
return pos;
}
//----------------------------------------------------------------------
void FButtonGroup::drawText ( wchar_t LabelText[]
, std::size_t hotkeypos
, std::size_t length )
{
if ( isMonochron() )
setReverse(true);
if ( isEnabled() )
setColor(wc.label_emphasis_fg, wc.label_bg);
else
setColor(wc.label_inactive_fg, wc.label_inactive_bg);
for (std::size_t z = 0; z < length; z++)
{
if ( (z == hotkeypos) && flags.active )
{
setColor (wc.label_hotkey_fg, wc.label_hotkey_bg);
if ( ! flags.no_underline )
setUnderline();
print (LabelText[z]);
if ( ! flags.no_underline )
unsetUnderline();
setColor (wc.label_emphasis_fg, wc.label_bg);
}
else
print (LabelText[z]);
}
if ( isMonochron() )
setReverse(true);
}
//----------------------------------------------------------------------
void FButtonGroup::directFocus()
{
if ( ! hasFocusedButton() )
2015-05-23 13:35:12 +02:00
{
bool found_checked = false;
if ( hasCheckedButton() && ! buttonlist.empty() )
{
auto iter = buttonlist.begin();
auto last = buttonlist.end();
while ( iter != last )
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button->isChecked() )
{
if ( isRadioButton(toggle_button) )
{
found_checked = true;
auto focused_widget = getFocusWidget();
toggle_button->setFocus();
if ( focused_widget )
focused_widget->redraw();
focused_widget = getFocusWidget();
if ( focused_widget )
focused_widget->redraw();
}
break;
}
++iter;
}
}
if ( ! found_checked )
{
auto focused_widget = getFocusWidget();
focusFirstChild();
if ( focused_widget )
focused_widget->redraw();
focused_widget = getFocusWidget();
if ( focused_widget )
focused_widget->redraw();
}
}
if ( getStatusBar() )
{
getStatusBar()->drawMessage();
updateTerminal();
flush_out();
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FButtonGroup::cb_buttonToggled (FWidget* widget, FDataPtr)
{
auto button = static_cast<FToggleButton*>(widget);
if ( ! button->isChecked() )
return;
if ( buttonlist.empty() )
return;
auto iter = buttonlist.begin();
auto last = buttonlist.end();
while ( iter != last )
{
auto toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button != button
&& toggle_button->isChecked()
&& isRadioButton(toggle_button) )
{
toggle_button->unsetChecked();
if ( toggle_button->isShown() )
toggle_button->redraw();
}
++iter;
}
}
} // namespace finalcut