little code improvement

This commit is contained in:
Markus Gans 2021-03-18 18:42:21 +01:00
parent 7ae9f38229
commit 0e7488f646
8 changed files with 134 additions and 115 deletions

View File

@ -22,7 +22,7 @@
2021-02-09 Markus Gans <guru.mail@muenster.de> 2021-02-09 Markus Gans <guru.mail@muenster.de>
* Added support for combined unicode characters * Added support for combined unicode characters
* Added a unit test for the FTermBuffera class * Added a unit test for the FTermBuffer class
* Added a unit test for the FTterm functions * Added a unit test for the FTterm functions
2020-12-31 Markus Gans <guru.mail@muenster.de> 2020-12-31 Markus Gans <guru.mail@muenster.de>

View File

@ -3,7 +3,7 @@
* * * *
* This file is part of the FINAL CUT widget toolkit * * This file is part of the FINAL CUT widget toolkit *
* * * *
* Copyright 2015-2020 Markus Gans * * Copyright 2015-2021 Markus Gans *
* * * *
* FINAL CUT is free software; you can redistribute it and/or modify * * FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -1085,15 +1085,10 @@ void FMenu::keypressMenuBar (FKeyEvent* ev) const
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FMenu::hotkeyMenu (FKeyEvent* ev) inline bool FMenu::hotkeyFound (FKey hotkey, const FKeyEvent& ev) const
{
for (auto&& item : getItemList())
{
if ( item->hasHotkey() )
{ {
bool found{false}; bool found{false};
FKey hotkey = item->getHotkey(); const FKey key = ev.key();
const FKey key = ev->key();
if ( hotkey > 0xff00 && hotkey < 0xff5f ) // full-width character if ( hotkey > 0xff00 && hotkey < 0xff5f ) // full-width character
hotkey -= 0xfee0; hotkey -= 0xfee0;
@ -1107,20 +1102,31 @@ bool FMenu::hotkeyMenu (FKeyEvent* ev)
else if ( hotkey == key ) else if ( hotkey == key )
found = true; found = true;
if ( found ) return found;
{ }
if ( item->hasMenu() )
{
auto sub_menu = item->getMenu();
unselectItem();
item->setSelected();
setSelectedItem (item);
redraw();
//----------------------------------------------------------------------
bool FMenu::hotkeyMenu (FKeyEvent* ev)
{
auto try_to_open_submenu = [this] (FMenu* sub_menu)
{
if ( ! sub_menu->isShown() ) if ( ! sub_menu->isShown() )
openSubMenu (sub_menu, SELECT_ITEM); openSubMenu (sub_menu, SELECT_ITEM);
sub_menu->redraw(); sub_menu->redraw();
};
for (auto&& item : getItemList())
{
if ( item->hasHotkey() && hotkeyFound(item->getHotkey(), *ev) )
{
if ( item->hasMenu() )
{
unselectItem();
item->setSelected();
setSelectedItem (item);
redraw();
try_to_open_submenu (item->getMenu());
} }
else else
{ {
@ -1136,7 +1142,6 @@ bool FMenu::hotkeyMenu (FKeyEvent* ev)
return true; return true;
} }
} }
}
return false; return false;
} }
@ -1264,24 +1269,30 @@ inline void FMenu::drawCheckMarkPrefix (const FMenuItem* m_item)
if ( ! has_checkable_items ) if ( ! has_checkable_items )
return; return;
if ( is_checkable ) auto print_bullet = [this] ()
{
if ( is_checked )
{
if ( is_radio_btn )
{ {
if ( FTerm::isNewFont() ) if ( FTerm::isNewFont() )
print (UniChar::NF_Bullet); // NF_Bullet ● print (UniChar::NF_Bullet); // NF_Bullet ●
else else
print (UniChar::BlackCircle); // BlackCircle ● print (UniChar::BlackCircle); // BlackCircle ●
} };
else
auto print_check_mark = [this] ()
{ {
if ( FTerm::isNewFont() ) if ( FTerm::isNewFont() )
print (UniChar::NF_check_mark); // NF_check_mark ✓ print (UniChar::NF_check_mark); // NF_check_mark ✓
else else
print (UniChar::SquareRoot); // SquareRoot √ print (UniChar::SquareRoot); // SquareRoot √
} };
if ( is_checkable )
{
if ( is_checked )
{
if ( is_radio_btn )
print_bullet();
else
print_check_mark();
} }
else else
{ {

View File

@ -3,7 +3,7 @@
* * * *
* This file is part of the FINAL CUT widget toolkit * * This file is part of the FINAL CUT widget toolkit *
* * * *
* Copyright 2015-2020 Markus Gans * * Copyright 2015-2021 Markus Gans *
* * * *
* FINAL CUT is free software; you can redistribute it and/or modify * * FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -105,21 +105,7 @@ void FMenuBar::onKeyPress (FKeyEvent* ev)
if ( sel_item->hasMenu() ) if ( sel_item->hasMenu() )
{ {
auto menu = sel_item->getMenu(); openMenu (sel_item);
sel_item->openMenu();
menu->selectFirstItem();
auto first_item = menu->getSelectedItem();
if ( first_item )
first_item->setFocus();
menu->redraw();
if ( getStatusBar() )
getStatusBar()->drawMessage();
redraw();
drop_down = true;
} }
else if ( key == FKey::Return || key == FKey::Enter ) else if ( key == FKey::Return || key == FKey::Enter )
{ {
@ -727,6 +713,26 @@ void FMenuBar::selectMenuItem (FMenuItem* item)
} }
} }
//----------------------------------------------------------------------
void FMenuBar::openMenu (const FMenuItem* sel_item)
{
auto menu = sel_item->getMenu();
sel_item->openMenu();
menu->selectFirstItem();
auto first_item = menu->getSelectedItem();
if ( first_item )
first_item->setFocus();
menu->redraw();
if ( getStatusBar() )
getStatusBar()->drawMessage();
redraw();
drop_down = true;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FMenuBar::activateMenu (const FMenuItem* item) bool FMenuBar::activateMenu (const FMenuItem* item)
{ {

View File

@ -323,23 +323,24 @@ FString getFullWidth (const FString& str)
// Converts half-width to full-width characters // Converts half-width to full-width characters
FString s{str}; FString s{str};
auto table_search = [] (wchar_t& c)
{
constexpr std::size_t HALF = 0; constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1; constexpr std::size_t FULL = 1;
for (auto&& c : s)
{
if ( c > L'\x20' && c < L'\x7f' ) // half-width ASCII
{
c += 0xfee0;
}
else
{
for (auto&& entry : fc::halfwidth_fullwidth) for (auto&& entry : fc::halfwidth_fullwidth)
{ {
if ( entry[HALF] == c ) // found if ( entry[HALF] == c ) // found
c = entry[FULL]; c = entry[FULL];
} }
} };
for (auto&& c : s)
{
if ( c > L'\x20' && c < L'\x7f' ) // half-width ASCII
c += 0xfee0;
else
table_search(c);
} }
return s; return s;
@ -351,23 +352,24 @@ FString getHalfWidth (const FString& str)
// Converts full-width to half-width characters // Converts full-width to half-width characters
FString s{str}; FString s{str};
auto table_search = [] (wchar_t& c)
{
constexpr std::size_t HALF = 0; constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1; constexpr std::size_t FULL = 1;
for (auto&& c : s)
{
if ( c > L'\xff00' && c < L'\xff5f' ) // full-width ASCII
{
c -= 0xfee0;
}
else
{
for (auto&& entry : fc::halfwidth_fullwidth) for (auto&& entry : fc::halfwidth_fullwidth)
{ {
if ( entry[FULL] == c ) // found if ( entry[FULL] == c ) // found
c = entry[HALF]; c = entry[HALF];
} }
} };
for (auto&& c : s)
{
if ( c > L'\xff00' && c < L'\xff5f' ) // full-width ASCII
c -= 0xfee0;
else
table_search(c);
} }
return s; return s;

View File

@ -3,7 +3,7 @@
* * * *
* This file is part of the FINAL CUT widget toolkit * * This file is part of the FINAL CUT widget toolkit *
* * * *
* Copyright 2015-2020 Markus Gans * * Copyright 2015-2021 Markus Gans *
* * * *
* FINAL CUT is free software; you can redistribute it and/or modify * * FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -1726,10 +1726,13 @@ void FWidget::determineDesktopSize()
// Determine width and height of the terminal // Determine width and height of the terminal
detectTermSize(); detectTermSize();
wsize.setRect(1, 1, getDesktopWidth(), getDesktopHeight()); auto width = getDesktopWidth();
auto height = getDesktopHeight();
wsize.setRect(1, 1, width, height);
adjust_wsize = wsize; adjust_wsize = wsize;
woffset.setRect(0, 0, getDesktopWidth(), getDesktopHeight()); woffset.setRect(0, 0, width, height);
wclient_offset = woffset; auto r = internal::var::root_widget;
wclient_offset.setRect(r->padding.left, r->padding.top, width, height);
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -1846,17 +1849,7 @@ inline void FWidget::insufficientSpaceAdjust()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FWidget::KeyPressEvent (FKeyEvent* kev) void FWidget::KeyPressEvent (FKeyEvent* kev)
{ {
FWidget* widget(this); auto change_focus = [this] (const FKey key)
while ( widget )
{
widget->onKeyPress(kev);
if ( ! kev->isAccepted() )
{
const FKey key = kev->key();
if ( [this, &key] ()
{ {
if ( isFocusNextKey(key) ) if ( isFocusNextKey(key) )
return focusNextChild(); return focusNextChild();
@ -1864,11 +1857,16 @@ void FWidget::KeyPressEvent (FKeyEvent* kev)
return focusPrevChild(); return focusPrevChild();
return false; return false;
}() ) };
FWidget* widget(this);
while ( widget )
{ {
widget->onKeyPress(kev);
if ( ! kev->isAccepted() && change_focus(kev->key()) )
return; return;
}
}
if ( kev->isAccepted() if ( kev->isAccepted()
|| widget->isRootWidget() || widget->isRootWidget()

View File

@ -199,6 +199,7 @@ class FMenu : public FWindow, public FMenuList
bool selectNextItem(); bool selectNextItem();
bool selectPrevItem(); bool selectPrevItem();
void keypressMenuBar (FKeyEvent*) const; void keypressMenuBar (FKeyEvent*) const;
bool hotkeyFound (FKey, const FKeyEvent&) const;
bool hotkeyMenu (FKeyEvent*); bool hotkeyMenu (FKeyEvent*);
void draw() override; void draw() override;
void drawItems(); void drawItems();

View File

@ -3,7 +3,7 @@
* * * *
* This file is part of the FINAL CUT widget toolkit * * This file is part of the FINAL CUT widget toolkit *
* * * *
* Copyright 2015-2020 Markus Gans * * Copyright 2015-2021 Markus Gans *
* * * *
* FINAL CUT is free software; you can redistribute it and/or modify * * FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -132,6 +132,7 @@ class FMenuBar : public FWindow, public FMenuList
void drawLeadingSpace (std::size_t&); void drawLeadingSpace (std::size_t&);
void drawTrailingSpace (std::size_t&); void drawTrailingSpace (std::size_t&);
void adjustItems() const; void adjustItems() const;
void openMenu (const FMenuItem*);
bool activateMenu (const FMenuItem*); bool activateMenu (const FMenuItem*);
bool clickItem (FMenuItem*); bool clickItem (FMenuItem*);
void unselectMenuItem (FMenuItem*); void unselectMenuItem (FMenuItem*);

View File

@ -49,7 +49,7 @@
<< ": Not enough memory to alloc " \ << ": Not enough memory to alloc " \
<< (object_name) \ << (object_name) \
<< " in " \ << " in " \
<< __func__ << std::endl; << __func__ << std::endl // ;
using uChar = unsigned char; using uChar = unsigned char;
using uShort = unsigned short; using uShort = unsigned short;