2017-11-04 07:03:53 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* ftogglebutton.cpp - Intermediate base class for a toggle button *
|
|
|
|
* *
|
|
|
|
* This file is part of the Final Cut widget toolkit *
|
|
|
|
* *
|
2018-02-17 19:12:48 +01:00
|
|
|
* Copyright 2014-2018 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
|
|
|
|
2017-09-17 21:32:46 +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
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
namespace finalcut
|
|
|
|
{
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FToggleButton
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// constructor and destructor
|
|
|
|
//----------------------------------------------------------------------
|
2017-02-20 00:00:53 +01:00
|
|
|
FToggleButton::FToggleButton (FWidget* parent)
|
2015-09-22 04:18:20 +02:00
|
|
|
: FWidget(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
|
|
|
|
2017-07-23 01:19:59 +02:00
|
|
|
if ( parent && parent->isInstanceOf("FButtonGroup") )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2017-10-02 07:32:33 +02:00
|
|
|
setGroup(static_cast<FButtonGroup*>(parent));
|
2016-08-21 21:27:44 +02:00
|
|
|
|
2017-03-12 00:29:56 +01:00
|
|
|
if ( hasGroup() )
|
2016-11-02 00:37:58 +01:00
|
|
|
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)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-09-26 18:01:44 +02:00
|
|
|
FToggleButton::setText(txt); // call own method
|
2018-09-24 04:50:09 +02:00
|
|
|
init();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2017-07-23 01:19:59 +02:00
|
|
|
if ( parent && parent->isInstanceOf("FButtonGroup") )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2017-10-02 07:32:33 +02:00
|
|
|
setGroup(static_cast<FButtonGroup*>(parent));
|
2016-08-21 21:27:44 +02:00
|
|
|
|
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();
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2017-03-12 00:29:56 +01:00
|
|
|
if ( hasGroup() )
|
2016-11-02 00:37:58 +01:00
|
|
|
getGroup()->remove(this);
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
// public methods of FToggleButton
|
2015-05-23 13:35:12 +02:00
|
|
|
//----------------------------------------------------------------------
|
2018-11-20 21:11:04 +01:00
|
|
|
void FToggleButton::setGeometry ( int x, int y
|
|
|
|
, std::size_t w, std::size_t h
|
|
|
|
, bool adjust )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2017-08-06 17:02:19 +02:00
|
|
|
// Set the toggle button geometry
|
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
std::size_t hotkey_mark = ( getHotkey() ) ? 1 : 0;
|
|
|
|
std::size_t min_width = button_width + text.getLength() - hotkey_mark;
|
2015-07-06 22:43:56 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
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);
|
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
FWidget::setGeometry(x, y, w, h, adjust);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-22 23:50:10 +01:00
|
|
|
bool FToggleButton::setNoUnderline (bool enable)
|
2016-11-02 00:37:58 +01:00
|
|
|
{
|
2018-12-22 23:50:10 +01:00
|
|
|
return (flags.no_underline = enable);
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-22 23:50:10 +01:00
|
|
|
bool FToggleButton::setEnable (bool enable)
|
2016-11-02 00:37:58 +01:00
|
|
|
{
|
2018-12-22 23:50:10 +01:00
|
|
|
FWidget::setEnable(enable);
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2018-12-22 23:50:10 +01:00
|
|
|
if ( enable )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
setHotkeyAccelerator();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
if ( hasFocus() )
|
|
|
|
{
|
2016-09-25 23:53:48 +02:00
|
|
|
setForegroundColor (wc.toggle_button_active_focus_fg);
|
|
|
|
setBackgroundColor (wc.toggle_button_active_focus_bg);
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-25 23:53:48 +02:00
|
|
|
setForegroundColor (wc.toggle_button_active_fg);
|
|
|
|
setBackgroundColor (wc.toggle_button_active_bg);
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
else
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
delAccelerator();
|
|
|
|
setForegroundColor (wc.toggle_button_inactive_fg);
|
|
|
|
setBackgroundColor (wc.toggle_button_inactive_bg);
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2018-12-22 23:50:10 +01:00
|
|
|
return enable;
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-22 23:50:10 +01:00
|
|
|
bool FToggleButton::setFocus (bool enable)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-12-22 23:50:10 +01:00
|
|
|
FWidget::setFocus(enable);
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2018-12-22 23:50:10 +01:00
|
|
|
if ( enable )
|
2016-11-02 00:37:58 +01:00
|
|
|
{
|
|
|
|
if ( isEnabled() )
|
|
|
|
{
|
|
|
|
if ( isRadioButton() )
|
|
|
|
focus_inside_group = false;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
setForegroundColor (wc.toggle_button_active_focus_fg);
|
|
|
|
setBackgroundColor (wc.toggle_button_active_focus_bg);
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( getStatusBar() )
|
|
|
|
{
|
2018-12-26 23:41:49 +01:00
|
|
|
const auto& msg = getStatusbarMessage();
|
|
|
|
const auto& curMsg = getStatusBar()->getMessage();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( curMsg != msg )
|
|
|
|
getStatusBar()->setMessage(msg);
|
|
|
|
}
|
2015-10-01 03:48:58 +02:00
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( isEnabled() )
|
2015-10-01 03:48:58 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01: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
|
|
|
}
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2018-12-22 23:50:10 +01:00
|
|
|
return enable;
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-22 23:50:10 +01:00
|
|
|
bool FToggleButton::setChecked (bool enable)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-12-22 23:50:10 +01:00
|
|
|
if ( checked != enable )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-12-22 23:50:10 +01:00
|
|
|
checked = enable;
|
2016-11-02 00:37:58 +01:00
|
|
|
processToggle();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
return checked;
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-03-26 20:40:04 +02:00
|
|
|
void FToggleButton::setText (const FString& txt)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
text = txt;
|
2018-10-14 06:25:33 +02:00
|
|
|
std::size_t hotkey_mark = ( getHotkey() ) ? 1 : 0;
|
2017-11-03 05:04:27 +01:00
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
setWidth(button_width + text.getLength() - hotkey_mark);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( isEnabled() )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
delAccelerator();
|
|
|
|
setHotkeyAccelerator();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::hide()
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-10-14 06:25:33 +02:00
|
|
|
std::size_t size;
|
2018-11-13 02:51:41 +01:00
|
|
|
FColor fg, bg;
|
2018-12-15 00:50:09 +01:00
|
|
|
auto parent_widget = getParentWidget();
|
2016-11-02 00:37:58 +01:00
|
|
|
FWidget::hide();
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
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
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
setColor (fg, bg);
|
|
|
|
size = getWidth();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
if ( size == 0 )
|
2016-11-02 00:37:58 +01:00
|
|
|
return;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2018-12-15 00:50:09 +01:00
|
|
|
auto blank = createBlankArray(size + 1);
|
2016-11-02 00:37:58 +01:00
|
|
|
setPrintPos (1, 1);
|
|
|
|
print (blank);
|
2018-10-20 22:50:35 +02:00
|
|
|
destroyBlankArray (blank);
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FToggleButton::onMouseDown (FMouseEvent* ev)
|
|
|
|
{
|
|
|
|
if ( ev->getButton() != fc::LeftButton )
|
|
|
|
return;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( hasFocus() )
|
|
|
|
return;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2018-12-15 00:50:09 +01:00
|
|
|
auto focused_widget = getFocusWidget();
|
2016-11-02 00:37:58 +01:00
|
|
|
setFocus();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( focused_widget )
|
|
|
|
focused_widget->redraw();
|
2015-10-11 21:56:16 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
redraw();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( getStatusBar() )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
getStatusBar()->drawMessage();
|
|
|
|
updateTerminal();
|
|
|
|
flush_out();
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FToggleButton::onMouseUp (FMouseEvent* ev)
|
|
|
|
{
|
|
|
|
if ( ev->getButton() != fc::LeftButton )
|
|
|
|
return;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( ! getTermGeometry().contains(ev->getTermPos()) )
|
|
|
|
return;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( isRadioButton() )
|
|
|
|
{
|
|
|
|
if ( ! checked )
|
|
|
|
{
|
|
|
|
checked = true;
|
|
|
|
processToggle();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-20 21:11:04 +01:00
|
|
|
checked = ! checked;
|
2016-11-02 00:37:58 +01:00
|
|
|
processToggle();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
2015-10-11 21:56:16 +02:00
|
|
|
|
2016-11-02 00:37:58 +01: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
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::onAccel (FAccelEvent* ev)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( ! isEnabled() )
|
|
|
|
return;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( ! hasFocus() )
|
|
|
|
{
|
2018-12-15 00:50:09 +01:00
|
|
|
auto focused_widget = static_cast<FWidget*>(ev->focusedWidget());
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2017-06-14 01:23:10 +02:00
|
|
|
if ( focused_widget && focused_widget->isWidget() )
|
2017-06-11 17:47:50 +02:00
|
|
|
{
|
|
|
|
setFocus();
|
2017-06-14 01:23:10 +02:00
|
|
|
focused_widget->redraw();
|
2017-06-11 17:47:50 +02:00
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( isRadioButton() )
|
|
|
|
{
|
|
|
|
if ( ! checked )
|
|
|
|
{
|
|
|
|
checked = true;
|
|
|
|
processToggle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-20 21:11:04 +01:00
|
|
|
checked = ! checked;
|
2016-11-02 00:37:58 +01:00
|
|
|
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
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::onFocusIn (FFocusEvent*)
|
2015-07-06 10:50:46 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( getStatusBar() )
|
|
|
|
getStatusBar()->drawMessage();
|
2015-07-06 10:50:46 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::onFocusOut (FFocusEvent* out_ev)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01: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;
|
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( ! focus_inside_group && isRadioButton() )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
focus_inside_group = true;
|
|
|
|
out_ev->ignore();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( out_ev->getFocusType() == fc::FocusNextWidget )
|
|
|
|
getGroup()->focusNextChild();
|
2015-09-20 05:44:50 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( out_ev->getFocusType() == fc::FocusPreviousWidget )
|
|
|
|
getGroup()->focusPrevChild();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
redraw();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
2016-12-18 23:34:11 +01:00
|
|
|
else if ( this == getGroup()->getLastButton()
|
2017-11-26 22:37:18 +01:00
|
|
|
&& out_ev->getFocusType() == fc::FocusNextWidget )
|
2016-08-21 21:27:44 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
out_ev->ignore();
|
|
|
|
getGroup()->focusNextChild();
|
|
|
|
redraw();
|
2016-08-21 21:27:44 +02:00
|
|
|
}
|
2016-12-18 23:34:11 +01:00
|
|
|
else if ( this == getGroup()->getFirstButton()
|
2017-11-26 22:37:18 +01:00
|
|
|
&& out_ev->getFocusType() == fc::FocusPreviousWidget )
|
2016-08-21 21:27:44 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
out_ev->ignore();
|
|
|
|
getGroup()->focusPrevChild();
|
|
|
|
redraw();
|
2016-08-21 21:27:44 +02:00
|
|
|
}
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
// protected methods of FToggleButton
|
2015-05-23 13:35:12 +02:00
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
uChar FToggleButton::getHotkey()
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( text.isEmpty() )
|
|
|
|
return 0;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
std::size_t length = text.getLength();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
for (std::size_t i = 0; i < length; i++)
|
2016-11-02 00:37:58 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-08-27 09:50:30 +02:00
|
|
|
if ( i + 1 < length && text[i] == '&' )
|
2016-11-02 00:37:58 +01:00
|
|
|
return uChar(text[++i]);
|
|
|
|
}
|
|
|
|
catch (const std::out_of_range&)
|
|
|
|
{
|
2017-08-27 09:50:30 +02:00
|
|
|
return 0;
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
return 0;
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::setHotkeyAccelerator()
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-11-21 20:07:08 +01:00
|
|
|
uChar hotkey = getHotkey();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( hotkey )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( std::isalpha(hotkey) || std::isdigit(hotkey) )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-11-21 20:07:08 +01:00
|
|
|
addAccelerator (FKey(std::tolower(hotkey)));
|
|
|
|
addAccelerator (FKey(std::toupper(hotkey)));
|
2016-11-02 00:37:58 +01:00
|
|
|
// Meta + hotkey
|
2018-11-21 20:07:08 +01:00
|
|
|
addAccelerator (fc::Fmkey_meta + FKey(std::tolower(hotkey)));
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
else
|
2016-11-02 00:37:58 +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
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
bool FToggleButton::isRadioButton() const
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2017-07-23 01:19:59 +02:00
|
|
|
return isInstanceOf("FRadioButton");
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
bool FToggleButton::isCheckboxButton() const
|
|
|
|
{
|
2017-07-23 01:19:59 +02:00
|
|
|
return isInstanceOf("FCheckBox");
|
2016-11-02 00:37:58 +01:00
|
|
|
}
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FToggleButton::draw()
|
|
|
|
{
|
2018-11-04 23:00:06 +01:00
|
|
|
if ( flags.focus && getStatusBar() )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2018-12-26 23:41:49 +01:00
|
|
|
const auto& msg = getStatusbarMessage();
|
|
|
|
const auto& curMsg = getStatusBar()->getMessage();
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( curMsg != msg )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
getStatusBar()->setMessage(msg);
|
|
|
|
getStatusBar()->drawMessage();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
// set the cursor to the button
|
|
|
|
if ( isRadioButton() || isCheckboxButton() )
|
|
|
|
setCursorPos (2, 1);
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::drawLabel()
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
wchar_t* LabelText;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( ! isVisible() )
|
2016-10-17 08:44:38 +02:00
|
|
|
return;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( text.isNull() || text.isEmpty() )
|
2015-05-23 13:35:12 +02:00
|
|
|
return;
|
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
std::size_t length = text.getLength();
|
2016-10-17 08:44:38 +02:00
|
|
|
|
2017-08-12 22:55:29 +02:00
|
|
|
try
|
|
|
|
{
|
2017-08-27 09:50:30 +02:00
|
|
|
LabelText = new wchar_t[length + 1]();
|
2017-08-12 22:55:29 +02:00
|
|
|
}
|
|
|
|
catch (const std::bad_alloc& ex)
|
|
|
|
{
|
2018-11-22 21:51:32 +01:00
|
|
|
std::cerr << bad_alloc_str << ex.what() << std::endl;
|
2017-08-12 22:55:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-17 19:12:48 +01:00
|
|
|
FString txt = text;
|
|
|
|
wchar_t* src = const_cast<wchar_t*>(txt.wc_str());
|
|
|
|
wchar_t* dest = const_cast<wchar_t*>(LabelText);
|
2018-12-15 00:50:09 +01:00
|
|
|
auto hotkeypos = getHotkeyPos(src, dest, length);
|
2016-10-17 08:44:38 +02:00
|
|
|
|
2018-10-18 23:50:06 +02:00
|
|
|
if ( hotkeypos != NOT_SET )
|
2016-11-02 00:37:58 +01:00
|
|
|
length--;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
setPrintPos (1 + int(label_offset_pos), 1);
|
2018-02-17 19:12:48 +01:00
|
|
|
drawText (LabelText, hotkeypos, length);
|
2016-11-02 00:37:58 +01:00
|
|
|
delete[] LabelText;
|
|
|
|
}
|
2016-10-17 08:44:38 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FToggleButton::processClick()
|
|
|
|
{
|
|
|
|
emitCallback("clicked");
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::processToggle()
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
emitCallback("toggled");
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::onKeyPress (FKeyEvent* ev)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
if ( ! isEnabled() )
|
2016-10-17 08:44:38 +02:00
|
|
|
return;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2018-11-21 20:07:08 +01:00
|
|
|
FKey key = ev->key();
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
switch ( key )
|
2016-10-17 08:44:38 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
case fc::Fkey_return:
|
|
|
|
case fc::Fkey_enter:
|
|
|
|
case fc::Fkey_space:
|
|
|
|
if ( isRadioButton() )
|
|
|
|
{
|
|
|
|
if ( ! checked )
|
|
|
|
{
|
|
|
|
checked = true;
|
|
|
|
processToggle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-20 21:11:04 +01:00
|
|
|
checked = ! checked;
|
2016-11-02 00:37:58 +01:00
|
|
|
processToggle();
|
|
|
|
}
|
|
|
|
processClick();
|
|
|
|
ev->accept();
|
|
|
|
break;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
case fc::Fkey_down:
|
|
|
|
case fc::Fkey_right:
|
|
|
|
focus_inside_group = true;
|
|
|
|
focusNextChild();
|
|
|
|
ev->accept();
|
|
|
|
break;
|
2016-10-17 08:44:38 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
case fc::Fkey_up:
|
|
|
|
case fc::Fkey_left:
|
|
|
|
focus_inside_group = true;
|
|
|
|
focusPrevChild();
|
|
|
|
ev->accept();
|
|
|
|
break;
|
2016-10-17 08:44:38 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
default:
|
|
|
|
break;
|
2016-10-17 08:44:38 +02:00
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
if ( ev->isAccepted() )
|
2016-10-17 08:44:38 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
draw();
|
|
|
|
updateTerminal();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
// private methods of FToggleButton
|
2015-05-23 13:35:12 +02:00
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::setGroup (FButtonGroup* btngroup)
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
button_group = btngroup;
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
void FToggleButton::init()
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
setGeometry (1, 1, 4, 1, false); // initialize geometry values
|
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( isEnabled() )
|
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-02-17 19:12:48 +01:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-10-17 22:12:52 +02:00
|
|
|
std::size_t FToggleButton::getHotkeyPos ( wchar_t src[]
|
2018-11-21 20:07:08 +01:00
|
|
|
, wchar_t dest[]
|
|
|
|
, std::size_t length )
|
2018-02-17 19:12:48 +01:00
|
|
|
{
|
|
|
|
// find hotkey position in string
|
|
|
|
// + generate a new string without the '&'-sign
|
2018-10-18 23:50:06 +02:00
|
|
|
std::size_t pos = NOT_SET;
|
2018-02-17 19:12:48 +01:00
|
|
|
wchar_t* txt = src;
|
|
|
|
|
2018-10-14 06:25:33 +02:00
|
|
|
for (std::size_t i = 0; i < length; i++)
|
2018-02-17 19:12:48 +01:00
|
|
|
{
|
2018-10-18 23:50:06 +02:00
|
|
|
if ( i < length && txt[i] == L'&' && pos == NOT_SET )
|
2018-02-17 19:12:48 +01:00
|
|
|
{
|
2018-10-17 22:12:52 +02:00
|
|
|
pos = i;
|
2018-02-17 19:12:48 +01:00
|
|
|
i++;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-10-14 06:25:33 +02:00
|
|
|
void FToggleButton::drawText ( wchar_t LabelText[]
|
2018-11-04 23:00:06 +01:00
|
|
|
, std::size_t hotkeypos
|
2018-10-14 06:25:33 +02:00
|
|
|
, std::size_t length )
|
2018-02-17 19:12:48 +01:00
|
|
|
{
|
|
|
|
if ( isMonochron() )
|
|
|
|
setReverse(true);
|
|
|
|
|
|
|
|
if ( isEnabled() )
|
|
|
|
setColor (wc.label_fg, wc.label_bg);
|
|
|
|
else
|
|
|
|
setColor (wc.label_inactive_fg, wc.label_inactive_bg);
|
|
|
|
|
2018-10-17 22:12:52 +02:00
|
|
|
for (std::size_t z = 0; z < length; z++)
|
2018-02-17 19:12:48 +01:00
|
|
|
{
|
2018-11-04 23:00:06 +01:00
|
|
|
if ( (z == hotkeypos) && flags.active )
|
2018-02-17 19:12:48 +01:00
|
|
|
{
|
|
|
|
setColor (wc.label_hotkey_fg, wc.label_hotkey_bg);
|
|
|
|
|
2018-11-04 23:00:06 +01:00
|
|
|
if ( ! flags.no_underline )
|
2018-02-17 19:12:48 +01:00
|
|
|
setUnderline();
|
|
|
|
|
|
|
|
print ( LabelText[z] );
|
|
|
|
|
2018-11-04 23:00:06 +01:00
|
|
|
if ( ! flags.no_underline )
|
2018-02-17 19:12:48 +01:00
|
|
|
unsetUnderline();
|
|
|
|
|
|
|
|
setColor (wc.label_fg, wc.label_bg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
print (LabelText[z]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isMonochron() )
|
2018-11-20 21:11:04 +01:00
|
|
|
setReverse(false);
|
2018-02-17 19:12:48 +01:00
|
|
|
}
|
2018-09-20 23:59:01 +02:00
|
|
|
|
|
|
|
} // namespace finalcut
|