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 *
@ -1084,57 +1084,62 @@ void FMenu::keypressMenuBar (FKeyEvent* ev) const
mbar->onKeyPress(ev); mbar->onKeyPress(ev);
} }
//----------------------------------------------------------------------
inline bool FMenu::hotkeyFound (FKey hotkey, const FKeyEvent& ev) const
{
bool found{false};
const FKey key = ev.key();
if ( hotkey > 0xff00 && hotkey < 0xff5f ) // full-width character
hotkey -= 0xfee0;
if ( std::isalpha(int(hotkey)) || std::isdigit(int(hotkey)) )
{
if ( FKey(std::tolower(int(hotkey))) == key
|| FKey(std::toupper(int(hotkey))) == key )
found = true;
}
else if ( hotkey == key )
found = true;
return found;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FMenu::hotkeyMenu (FKeyEvent* ev) bool FMenu::hotkeyMenu (FKeyEvent* ev)
{ {
auto try_to_open_submenu = [this] (FMenu* sub_menu)
{
if ( ! sub_menu->isShown() )
openSubMenu (sub_menu, SELECT_ITEM);
sub_menu->redraw();
};
for (auto&& item : getItemList()) for (auto&& item : getItemList())
{ {
if ( item->hasHotkey() ) if ( item->hasHotkey() && hotkeyFound(item->getHotkey(), *ev) )
{ {
bool found{false}; if ( item->hasMenu() )
FKey hotkey = item->getHotkey();
const FKey key = ev->key();
if ( hotkey > 0xff00 && hotkey < 0xff5f ) // full-width character
hotkey -= 0xfee0;
if ( std::isalpha(int(hotkey)) || std::isdigit(int(hotkey)) )
{ {
if ( FKey(std::tolower(int(hotkey))) == key unselectItem();
|| FKey(std::toupper(int(hotkey))) == key ) item->setSelected();
found = true; setSelectedItem (item);
redraw();
try_to_open_submenu (item->getMenu());
} }
else if ( hotkey == key ) else
found = true;
if ( found )
{ {
if ( item->hasMenu() ) unselectItem();
{ hideSubMenus();
auto sub_menu = item->getMenu(); hide();
unselectItem(); hideSuperMenus();
item->setSelected();
setSelectedItem (item);
redraw();
if ( ! sub_menu->isShown() )
openSubMenu (sub_menu, SELECT_ITEM);
sub_menu->redraw();
}
else
{
unselectItem();
hideSubMenus();
hide();
hideSuperMenus();
ev->accept();
item->processClicked();
}
ev->accept(); ev->accept();
return true; item->processClicked();
} }
ev->accept();
return true;
} }
} }
@ -1264,24 +1269,30 @@ inline void FMenu::drawCheckMarkPrefix (const FMenuItem* m_item)
if ( ! has_checkable_items ) if ( ! has_checkable_items )
return; return;
auto print_bullet = [this] ()
{
if ( FTerm::isNewFont() )
print (UniChar::NF_Bullet); // NF_Bullet ●
else
print (UniChar::BlackCircle); // BlackCircle ●
};
auto print_check_mark = [this] ()
{
if ( FTerm::isNewFont() )
print (UniChar::NF_check_mark); // NF_check_mark ✓
else
print (UniChar::SquareRoot); // SquareRoot √
};
if ( is_checkable ) if ( is_checkable )
{ {
if ( is_checked ) if ( is_checked )
{ {
if ( is_radio_btn ) if ( is_radio_btn )
{ print_bullet();
if ( FTerm::isNewFont() )
print (UniChar::NF_Bullet); // NF_Bullet ●
else
print (UniChar::BlackCircle); // BlackCircle ●
}
else else
{ print_check_mark();
if ( FTerm::isNewFont() )
print (UniChar::NF_check_mark); // NF_check_mark ✓
else
print (UniChar::SquareRoot); // SquareRoot √
}
} }
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};
constexpr std::size_t HALF = 0; auto table_search = [] (wchar_t& c)
constexpr std::size_t FULL = 1; {
constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1;
for (auto&& entry : fc::halfwidth_fullwidth)
{
if ( entry[HALF] == c ) // found
c = entry[FULL];
}
};
for (auto&& c : s) for (auto&& c : s)
{ {
if ( c > L'\x20' && c < L'\x7f' ) // half-width ASCII if ( c > L'\x20' && c < L'\x7f' ) // half-width ASCII
{
c += 0xfee0; c += 0xfee0;
}
else else
{ table_search(c);
for (auto&& entry : fc::halfwidth_fullwidth)
{
if ( entry[HALF] == c ) // found
c = entry[FULL];
}
}
} }
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};
constexpr std::size_t HALF = 0; auto table_search = [] (wchar_t& c)
constexpr std::size_t FULL = 1; {
constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1;
for (auto&& entry : fc::halfwidth_fullwidth)
{
if ( entry[FULL] == c ) // found
c = entry[HALF];
}
};
for (auto&& c : s) for (auto&& c : s)
{ {
if ( c > L'\xff00' && c < L'\xff5f' ) // full-width ASCII if ( c > L'\xff00' && c < L'\xff5f' ) // full-width ASCII
{
c -= 0xfee0; c -= 0xfee0;
}
else else
{ table_search(c);
for (auto&& entry : fc::halfwidth_fullwidth)
{
if ( entry[FULL] == c ) // found
c = entry[HALF];
}
}
} }
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,29 +1849,24 @@ inline void FWidget::insufficientSpaceAdjust()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FWidget::KeyPressEvent (FKeyEvent* kev) void FWidget::KeyPressEvent (FKeyEvent* kev)
{ {
auto change_focus = [this] (const FKey key)
{
if ( isFocusNextKey(key) )
return focusNextChild();
else if ( isFocusPrevKey(key) )
return focusPrevChild();
return false;
};
FWidget* widget(this); FWidget* widget(this);
while ( widget ) while ( widget )
{ {
widget->onKeyPress(kev); widget->onKeyPress(kev);
if ( ! kev->isAccepted() ) if ( ! kev->isAccepted() && change_focus(kev->key()) )
{ return;
const FKey key = kev->key();
if ( [this, &key] ()
{
if ( isFocusNextKey(key) )
return focusNextChild();
else if ( isFocusPrevKey(key) )
return focusPrevChild();
return false;
}() )
{
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;