1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/***********************************************************************
* fswitch.cpp - Widget FSwitch *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2015-2021 Markus Gans *
* *
* 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. *
* *
* 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/>. *
***********************************************************************/
#include "final/fevent.h"
#include "final/fwidgetcolors.h"
#include "final/vterm/fcolorpair.h"
#include "final/widget/fswitch.h"
namespace finalcut
{
//----------------------------------------------------------------------
// class FSwitch
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
FSwitch::FSwitch (FWidget* parent)
: FToggleButton{parent}
{
setButtonWidth(11);
}
//----------------------------------------------------------------------
FSwitch::FSwitch (const FString& txt, FWidget* parent)
: FToggleButton{txt, parent}
, switch_offset_pos(txt.getLength() + 1)
{
setButtonWidth(11);
}
//----------------------------------------------------------------------
FSwitch::~FSwitch() noexcept = default; // destructor
// public methods of FSwitch
//----------------------------------------------------------------------
void FSwitch::setText (const FString& txt)
{
FToggleButton::setText(txt);
switch_offset_pos = txt.getLength() + 1;
}
//----------------------------------------------------------------------
void FSwitch::onKeyPress (FKeyEvent* ev)
{
const auto key = ev->key();
if ( key == FKey::Home || key == FKey::Left )
{
setChecked();
ev->accept();
}
else if ( key == FKey::End || key == FKey::Right )
{
unsetChecked();
ev->accept();
}
if ( ev->isAccepted() )
draw();
else
FToggleButton::onKeyPress(ev);
}
//----------------------------------------------------------------------
void FSwitch::onMouseDown (FMouseEvent* ev)
{
FToggleButton::onMouseDown(ev);
if ( ev->getButton() != MouseButton::Left )
return;
button_pressed = true;
draw();
}
//----------------------------------------------------------------------
void FSwitch::onMouseUp (FMouseEvent* ev)
{
FToggleButton::onMouseUp(ev);
if ( ev->getButton() != MouseButton::Left )
return;
button_pressed = false;
draw();
}
// private methods of FSwitch
//----------------------------------------------------------------------
void FSwitch::draw()
{
if ( ! isVisible() )
return;
drawLabel();
drawCheckButton();
FToggleButton::draw();
}
//----------------------------------------------------------------------
void FSwitch::drawCheckButton()
{
print() << FPoint{1 + int(switch_offset_pos), 1};
if ( isChecked() )
drawChecked();
else
drawUnchecked();
}
//----------------------------------------------------------------------
inline void FSwitch::drawChecked()
{
FString on{L" On "};
const FString off{L" Off "};
const auto& wc = getColorTheme();
if ( hasFocus() && ! button_pressed )
{
if ( FVTerm::getFOutput()->isMonochron() )
{
on.setString(L" <On>");
setBold(true);
}
else if ( FVTerm::getFOutput()->getMaxColor() < 16 )
{
setBold(true);
setColor (wc->button_active_focus_fg, wc->button_active_focus_bg);
}
else
setColor (wc->button_hotkey_fg, wc->button_active_focus_bg);
}
else
{
if ( FVTerm::getFOutput()->isMonochron()
|| FVTerm::getFOutput()->getMaxColor() < 16 )
setColor (wc->button_active_focus_fg, wc->button_active_bg);
else
setColor (wc->button_hotkey_fg, wc->button_active_bg);
}
if ( FVTerm::getFOutput()->isMonochron() )
setReverse(false);
print (on);
if ( FVTerm::getFOutput()->isMonochron() )
setReverse(true);
if ( FVTerm::getFOutput()->isMonochron()
|| FVTerm::getFOutput()->getMaxColor() < 16 )
setBold(false);
print() << FColorPair{wc->button_inactive_fg, wc->button_inactive_bg}
<< off;
if ( FVTerm::getFOutput()->isMonochron() )
setReverse(false);
setCursorPos ({3 + int(switch_offset_pos), 1});
}
//----------------------------------------------------------------------
inline void FSwitch::drawUnchecked()
{
const FString on{L" On "};
FString off{L" Off "};
const auto& wc = getColorTheme();
setColor (wc->button_inactive_fg, wc->button_inactive_bg);
if ( FVTerm::getFOutput()->isMonochron() )
setReverse(true);
print (on);
if ( hasFocus() && ! button_pressed )
{
if ( FVTerm::getFOutput()->isMonochron() )
{
off.setString(L"<Off>");
setBold(true);
}
else if ( FVTerm::getFOutput()->getMaxColor() < 16 )
{
setBold(true);
setColor (wc->button_active_focus_fg, wc->button_active_focus_bg);
}
else
setColor (wc->button_hotkey_fg, wc->button_active_focus_bg);
}
else
{
if ( FVTerm::getFOutput()->isMonochron()
|| FVTerm::getFOutput()->getMaxColor() < 16 )
setColor (wc->button_active_focus_fg, wc->button_active_bg);
else
setColor (wc->button_hotkey_fg, wc->button_active_bg);
}
if ( FVTerm::getFOutput()->isMonochron() )
setReverse(false);
print (off);
if ( FVTerm::getFOutput()->isMonochron()
|| FVTerm::getFOutput()->getMaxColor() < 16 )
setBold(false);
setCursorPos ({7 + int(switch_offset_pos), 1});
}
} // namespace finalcut
|