finalcut/src/fbuttongroup.cpp

680 lines
16 KiB
C++
Raw Normal View History

/************************************************************************
* fbuttongroup.cpp - The FButtonGroup widget organizes FToggleButton *
* widgets in a group. *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2014-2017 Markus Gans *
* *
* The Final Cut is free software; you can redistribute it and/or modify *
* it under the terms of the GNU 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 General Public License for more details. *
* *
* You should have received a copy of the GNU 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
//----------------------------------------------------------------------
// class FButtonGroup
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FButtonGroup::FButtonGroup(FWidget* parent)
: FScrollView(parent)
2015-09-22 04:18:20 +02:00
, text()
, 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)
: FScrollView(parent)
2015-09-22 04:18:20 +02:00
, text(txt)
, 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
{
2017-09-15 01:31:02 +02:00
FObjectIterator iter;
2015-05-23 13:35:12 +02:00
if ( buttonlist.empty() )
return;
iter = buttonlist.begin();
while ( iter != buttonlist.end() )
{
FToggleButton* 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
//----------------------------------------------------------------------
FToggleButton* FButtonGroup::getButton(int index) const
{
2017-09-15 01:31:02 +02:00
constFObjectIterator iter;
2017-03-12 00:29:56 +01:00
index--;
if ( buttonlist.empty() )
return 0;
if ( index < 0 || index >= int(getCount()) )
return 0;
iter = buttonlist.begin();
std::advance (iter, index);
return static_cast<FToggleButton*>(*iter);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FToggleButton* FButtonGroup::getFirstButton()
2015-05-23 13:35:12 +02:00
{
FWidget* widget = FWidget::getFirstFocusableWidget(buttonlist);
FToggleButton* 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
{
FWidget* widget = FWidget::getLastFocusableWidget(buttonlist);
FToggleButton* toggle_button = static_cast<FToggleButton*>(widget);
return toggle_button;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FButtonGroup::setEnable (bool on)
2015-05-23 13:35:12 +02:00
{
FWidget::setEnable(on);
if ( on )
setHotkeyAccelerator();
else
delAccelerator();
return on;
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
{
FToggleButton* button = getButton(index);
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
constFObjectIterator iter, last;
iter = buttonlist.begin();
last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
FToggleButton* 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
constFObjectIterator iter, last;
iter = buttonlist.begin();
last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
FToggleButton* 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()
{
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() )
{
constFObjectIterator iter, last;
2015-05-23 13:35:12 +02:00
iter = buttonlist.begin();
last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
FToggleButton* 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;
2017-08-12 22:55:29 +02:00
try
{
blank = new char[size + 1];
2017-08-12 22:55:29 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
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
{
FWidget::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->getGroup() )
button->getGroup()->remove(button);
2015-05-23 13:35:12 +02:00
// setChecked the first FRadioButton
if ( buttonlist.size() == 1 )
{
2017-09-11 03:06:02 +02:00
FToggleButton* first_button;
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
{
2017-09-15 01:31:02 +02:00
FObjectIterator iter;
2015-05-23 13:35:12 +02:00
if ( ! button || buttonlist.empty() )
2015-05-23 13:35:12 +02:00
return;
iter = buttonlist.begin();
while ( iter != buttonlist.end() )
{
FToggleButton* 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 new_size = scrollgeometry.combined(r);
setScrollSize (new_size.getWidth(), new_size.getHeight());
}
}
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() )
{
constFObjectIterator iter, last;
2015-05-23 13:35:12 +02:00
iter = buttonlist.begin();
last = buttonlist.end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
FToggleButton* 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
{
FWidget* 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();
FWidget* 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();
}
}
//----------------------------------------------------------------------
void FButtonGroup::cb_buttonToggled (FWidget* widget, data_ptr)
2015-05-23 13:35:12 +02:00
{
FToggleButton* button = static_cast<FToggleButton*>(widget);
constFObjectIterator iter, last;
2015-05-23 13:35:12 +02:00
if ( ! button->isChecked() )
return;
if ( buttonlist.empty() )
return;
iter = buttonlist.begin();
last = buttonlist.end();
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
FToggleButton* toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button != button
&& toggle_button->isChecked()
&& isRadioButton(toggle_button) )
{
toggle_button->unsetChecked();
if ( toggle_button->isVisible() && toggle_button->isShown() )
toggle_button->redraw();
}
++iter;
2015-05-23 13:35:12 +02:00
}
}
// protected methods of FButtonGroup
//----------------------------------------------------------------------
uChar FButtonGroup::getHotkey()
{
uInt length;
if ( text.isEmpty() )
return 0;
length = text.getLength();
for (uInt i = 0; i < length; i++)
2015-05-23 13:35:12 +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) )
{
addAccelerator (std::tolower(hotkey));
addAccelerator (std::toupper(hotkey));
// Meta + hotkey
addAccelerator (fc::Fmkey_meta + std::tolower(hotkey));
}
else
addAccelerator (getHotkey());
}
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;
register wchar_t* src;
register wchar_t* dest;
FString txt;
uInt length;
int hotkeypos;
bool isActive, isNoUnderline;
if ( text.isNull() || text.isEmpty() )
return;
txt = " " + text + " ";
length = txt.getLength();
hotkeypos = -1;
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 << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
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);
unsetViewportPrint();
// 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 ( hasBorder() )
FWidget::setPrintPos (2, 1);
2015-05-23 13:35:12 +02:00
else
FWidget::setPrintPos (0, 1);
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);
if ( ! isNoUnderline )
setUnderline();
print ( LabelText[z] );
if ( ! isNoUnderline )
unsetUnderline();
setColor (wc.label_emphasis_fg, wc.label_bg);
}
else
print ( LabelText[z] );
}
setViewportPrint();
delete[] LabelText;
2015-05-23 13:35:12 +02:00
}
// private methods of FButtonGroup
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FButtonGroup::isRadioButton (FToggleButton* button) const
2015-05-23 13:35:12 +02:00
{
if ( ! button )
return false;
return bool ( std::strcmp ( button->getClassName()
, const_cast<char*>("FRadioButton") ) == 0 );
}
//----------------------------------------------------------------------
void FButtonGroup::init()
{
2015-05-23 13:35:12 +02:00
if ( isEnabled() )
flags |= fc::active;
setForegroundColor (wc.label_fg);
setBackgroundColor (wc.label_bg);
setMinimumSize (7, 4);
buttonlist.clear(); // no buttons yet
}
//----------------------------------------------------------------------
void FButtonGroup::directFocus()
{
if ( ! hasFocusedButton() )
2015-05-23 13:35:12 +02:00
{
bool found_checked = false;
if ( hasCheckedButton() && ! buttonlist.empty() )
{
constFObjectIterator iter, last;
iter = buttonlist.begin();
last = buttonlist.end();
while ( iter != last )
{
FToggleButton* toggle_button = static_cast<FToggleButton*>(*iter);
if ( toggle_button->isChecked() )
{
if ( isRadioButton(toggle_button) )
{
found_checked = true;
FWidget* focused_widget = getFocusWidget();
FFocusEvent out (fc::FocusOut_Event);
FApplication::queueEvent(focused_widget, &out);
toggle_button->setFocus();
if ( focused_widget )
focused_widget->redraw();
focused_widget = getFocusWidget();
if ( focused_widget )
focused_widget->redraw();
}
break;
}
++iter;
}
}
if ( ! found_checked )
{
FWidget* focused_widget = getFocusWidget();
FFocusEvent out (fc::FocusOut_Event);
FApplication::queueEvent(focused_widget, &out);
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
}
}