From 77f39235cf3ad79c5ff7d2b0f4717660476f3cf4 Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Thu, 9 Sep 2010 15:31:54 +0200 Subject: [PATCH] added FbTk::Util::clamp() and simplified related code --- src/ArrowButton.cc | 6 ++++-- src/CurrentWindowCmd.cc | 31 ++++++++++------------------- src/FbTk/Makefile.am | 1 + src/FbTk/MultLayers.cc | 15 ++++---------- src/FbTk/Util.hh | 43 +++++++++++++++++++++++++++++++++++++++++ src/FbWinFrameTheme.cc | 13 ++++--------- src/IconbarTool.cc | 7 ++----- src/Remember.cc | 31 +++++++++++------------------ src/Screen.cc | 29 ++++++++++----------------- src/WindowState.cc | 6 ++---- 10 files changed, 91 insertions(+), 91 deletions(-) create mode 100644 src/FbTk/Util.hh diff --git a/src/ArrowButton.cc b/src/ArrowButton.cc index b764a8fe..e46ffc4c 100644 --- a/src/ArrowButton.cc +++ b/src/ArrowButton.cc @@ -22,6 +22,8 @@ #include "ArrowButton.hh" #include "ButtonTheme.hh" +#include "FbTk/Util.hh" + ArrowButton::ArrowButton(FbTk::FbDrawable::TriangleType arrow_type, const FbTk::FbWindow &parent, int x, int y, @@ -92,6 +94,6 @@ void ArrowButton::updateTheme(const FbTk::Theme &theme) { m_arrowscale = btheme.scale(); if (m_arrowscale == 0) m_arrowscale = 250; // default is 0 => 300 - else if (m_arrowscale < 100) m_arrowscale = 100; // otherwise clamp - else if (m_arrowscale > 100000) m_arrowscale = 100000; // clamp below overflow when *100 + + m_arrowscale = FbTk::Util::clamp(m_arrowscale, 100, 100000); } diff --git a/src/CurrentWindowCmd.cc b/src/CurrentWindowCmd.cc index 4d25d545..e2fdb947 100644 --- a/src/CurrentWindowCmd.cc +++ b/src/CurrentWindowCmd.cc @@ -35,6 +35,7 @@ #include "FbTk/I18n.hh" #include "FbTk/stringstream.hh" #include "FbTk/StringUtil.hh" +#include "FbTk/Util.hh" #ifdef HAVE_CONFIG_H #include "config.h" @@ -250,8 +251,7 @@ void SetHeadCmd::real_execute() { int num = m_head; int total = fbwindow().screen().numHeads(); if (num < 0) num += total + 1; - if (num < 1) num = 1; - if (num > total) num = total; + num = FbTk::Util::clamp(num, 1, total); fbwindow().setOnHead(num); } @@ -259,8 +259,7 @@ void SendToWorkspaceCmd::real_execute() { int num = m_workspace_num; int total = fbwindow().screen().numberOfWorkspaces(); if (num < 0) num += total + 1; - if (num < 1) num = 1; - if (num > total) num = total; + num = FbTk::Util::clamp(num, 1, total); fbwindow().screen().sendToWorkspace(num-1, &fbwindow(), m_take); } @@ -281,8 +280,7 @@ void SendToNextHeadCmd::real_execute() { void GoToTabCmd::real_execute() { int num = m_tab_num; if (num < 0) num += fbwindow().numClients() + 1; - if (num < 1) num = 1; - if (num > fbwindow().numClients()) num = fbwindow().numClients(); + num = FbTk::Util::clamp(num, 1, fbwindow().numClients()); FluxboxWindow::ClientList::iterator it = fbwindow().clientList().begin(); @@ -670,22 +668,13 @@ void SetAlphaCmd::real_execute() { return; } - int new_alpha; - if (m_relative) { - new_alpha = fbwindow().getFocusedAlpha() + m_focus; - if (new_alpha < 0) new_alpha = 0; - if (new_alpha > 255) new_alpha = 255; - fbwindow().setFocusedAlpha(new_alpha); - } else - fbwindow().setFocusedAlpha(m_focus); + fbwindow().setFocusedAlpha(m_relative + ? FbTk::Util::clamp(fbwindow().getFocusedAlpha() + m_focus, 0, 255) + : m_focus); - if (m_un_relative) { - new_alpha = fbwindow().getUnfocusedAlpha() + m_unfocus; - if (new_alpha < 0) new_alpha = 0; - if (new_alpha > 255) new_alpha = 255; - fbwindow().setUnfocusedAlpha(new_alpha); - } else - fbwindow().setUnfocusedAlpha(m_unfocus); + fbwindow().setUnfocusedAlpha(m_un_relative + ? FbTk::Util::clamp(fbwindow().getUnfocusedAlpha() + m_unfocus, 0, 255) + : m_unfocus); } REGISTER_COMMAND_WITH_ARGS(matches, MatchCmd, bool); diff --git a/src/FbTk/Makefile.am b/src/FbTk/Makefile.am index 67be7a0c..45a24b34 100644 --- a/src/FbTk/Makefile.am +++ b/src/FbTk/Makefile.am @@ -64,6 +64,7 @@ libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \ Select2nd.hh STLUtil.hh \ CachedPixmap.hh CachedPixmap.cc \ Slot.hh Signal.hh MemFun.hh RelaySignal.hh SelectArg.hh \ + Util.hh \ ${xpm_SOURCE} \ ${xft_SOURCE} \ ${xmb_SOURCE} \ diff --git a/src/FbTk/MultLayers.cc b/src/FbTk/MultLayers.cc index a9135e3d..6469a146 100644 --- a/src/FbTk/MultLayers.cc +++ b/src/FbTk/MultLayers.cc @@ -26,6 +26,8 @@ #include "App.hh" #include "FbWindow.hh" +#include "Util.hh" + using namespace FbTk; MultLayers::MultLayers(int numlayers) : @@ -56,11 +58,7 @@ XLayerItem *MultLayers::getLowestItemAboveLayer(int layernum) { } void MultLayers::addToTop(XLayerItem &item, int layernum) { - if (layernum < 0) - layernum = 0; - else if (layernum >= static_cast(m_layers.size())) - layernum = m_layers.size()-1; - + layernum = FbTk::Util::clamp(layernum, 0, static_cast(m_layers.size()) - 1); m_layers[layernum]->insert(item); restack(); } @@ -108,12 +106,7 @@ void MultLayers::moveToLayer(XLayerItem &item, int layernum) { if (curr_layer.getLayerNum() == layernum) return; - // clamp layer number - if (layernum < 0) - layernum = 0; - else if (layernum >= static_cast(m_layers.size())) - layernum = m_layers.size()-1; - // remove item from old layer and insert it into the + layernum = FbTk::Util::clamp(layernum, 0, static_cast(m_layers.size()) - 1); item.setLayer(*m_layers[layernum]); } diff --git a/src/FbTk/Util.hh b/src/FbTk/Util.hh new file mode 100644 index 00000000..5e5a9861 --- /dev/null +++ b/src/FbTk/Util.hh @@ -0,0 +1,43 @@ +// Util.hh for fluxbox +// Copyright (c) 2010 Mathias Gumz (akira at fluxbox org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#ifndef FBTK_UTIL_HH +#define FBTK_UTIL_HH + +namespace FbTk { + +namespace Util { + +template +inline T clamp(const T& value, const T& lower, const T& upper) { + if (value < lower) + return lower; + else if (value > upper) + return upper; + return value; +} + +} // end namespace Util + +} // end namespace FbTk + + +#endif // FBTK_UTIL_HH diff --git a/src/FbWinFrameTheme.cc b/src/FbWinFrameTheme.cc index 92cb134d..f01c5465 100644 --- a/src/FbWinFrameTheme.cc +++ b/src/FbWinFrameTheme.cc @@ -21,6 +21,7 @@ #include "FbWinFrameTheme.hh" #include "FbTk/App.hh" +#include "FbTk/Util.hh" #include "IconbarTheme.hh" @@ -88,16 +89,10 @@ bool FbWinFrameTheme::fallback(FbTk::ThemeItem_base &item) { } void FbWinFrameTheme::reconfigTheme() { - if (*m_bevel_width > 20) - *m_bevel_width = 20; - else if (*m_bevel_width < 0) - *m_bevel_width = 0; - - if (*m_handle_width > 200) - *m_handle_width = 200; - else if (*m_handle_width < 0) + *m_bevel_width = FbTk::Util::clamp(*m_bevel_width, 0, 20); + if (*m_handle_width < 0) *m_handle_width = 1; - + *m_handle_width = FbTk::Util::clamp(*m_handle_width, 0, 200); m_button_pic_gc.setForeground(*m_button_color); m_iconbar_theme.reconfigTheme(); } diff --git a/src/IconbarTool.cc b/src/IconbarTool.cc index 85997edd..14835cb5 100644 --- a/src/IconbarTool.cc +++ b/src/IconbarTool.cc @@ -47,6 +47,7 @@ #include "FbTk/ImageControl.hh" #include "FbTk/MacroCommand.hh" #include "FbTk/MenuSeparator.hh" +#include "FbTk/Util.hh" #include #include @@ -389,12 +390,8 @@ void IconbarTool::update(FbTk::Subject *subj) { } m_icon_container.setAlignment(*m_rc_alignment); - // clamp to normal values - if (*m_rc_client_width < 1) - *m_rc_client_width = 10; - else if (*m_rc_client_width > 400) - *m_rc_client_width = 400; + *m_rc_client_width = FbTk::Util::clamp(*m_rc_client_width, 10, 400); m_icon_container.setMaxSizePerClient(*m_rc_client_width); if (subj == &m_focused_theme.reconfigSig() || diff --git a/src/Remember.cc b/src/Remember.cc index f70217ec..54b5b0d8 100644 --- a/src/Remember.cc +++ b/src/Remember.cc @@ -41,6 +41,7 @@ #include "FbTk/Transparent.hh" #include "FbTk/AutoReloadHelper.hh" #include "FbTk/RefCount.hh" +#include "FbTk/Util.hh" #ifdef HAVE_CSTRING #include @@ -512,28 +513,18 @@ int parseApp(ifstream &file, Application &app, string *first_line = 0) { app.rememberDecostate((unsigned int)deco); } else if (str_key == "alpha") { int focused_a, unfocused_a; - if (sscanf(str_label.c_str(), "%i %i", &focused_a, &unfocused_a) == 2) - { - // clamp; - if (focused_a > 255) - focused_a = 255; - if (unfocused_a > 255) - unfocused_a = 255; - if (focused_a <= 0) - focused_a = 0; - if (unfocused_a <= 0) - unfocused_a = 0; - + switch (sscanf(str_label.c_str(), "%i %i", &focused_a, &unfocused_a)) { + case 1: // 'alpha ' + unfocused_a = focused_a; + case 2: // 'alpha ' + focused_a = FbTk::Util::clamp(focused_a, 0, 255); + unfocused_a = FbTk::Util::clamp(unfocused_a, 0, 255); app.rememberAlpha(focused_a, unfocused_a); - } else if (sscanf(str_label.c_str(), "%i", &focused_a) == 1) { - if (focused_a > 255) - focused_a = 255; - if (focused_a <= 0) - focused_a = 0; - app.rememberAlpha(focused_a, focused_a); + break; + default: + had_error = true; + break; } - else - had_error = 1; } else if (str_key == "sticky") { app.rememberStuckstate((strcasecmp(str_label.c_str(), "yes") == 0)); } else if (str_key == "minimized") { diff --git a/src/Screen.cc b/src/Screen.cc index ad245034..58aec0db 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -73,6 +73,7 @@ #include "FbTk/FbString.hh" #include "FbTk/STLUtil.hh" #include "FbTk/KeyUtil.hh" +#include "FbTk/Util.hh" //use GNU extensions #ifndef _GNU_SOURCE @@ -203,6 +204,10 @@ private: FbWinFrame::TabPlacement m_place; }; +void clampMenuDelay(int& delay) { + delay = FbTk::Util::clamp(delay, 0, 5000); +} + } // end anonymous namespace @@ -453,11 +458,8 @@ BScreen::BScreen(FbTk::ResourceManager &rm, focusedWinFrameTheme()->setAlpha(*resource.focused_alpha); unfocusedWinFrameTheme()->setAlpha(*resource.unfocused_alpha); m_menutheme->setAlpha(*resource.menu_alpha); - // clamp values - if (*resource.menu_delay > 5000) - *resource.menu_delay = 5000; - if (*resource.menu_delay < 0) - *resource.menu_delay = 0; + + clampMenuDelay(*resource.menu_delay); m_menutheme->setDelay(*resource.menu_delay); @@ -905,11 +907,7 @@ void BScreen::reconfigure() { unfocusedWinFrameTheme()->setAlpha(*resource.unfocused_alpha); m_menutheme->setAlpha(*resource.menu_alpha); - // clamp values - if (*resource.menu_delay > 5000) - *resource.menu_delay = 5000; - if (*resource.menu_delay < 0) - *resource.menu_delay = 0; + clampMenuDelay(*resource.menu_delay); m_menutheme->setDelay(*resource.menu_delay); @@ -2138,15 +2136,8 @@ pair BScreen::clampToHead(int head, int x, int y, int w, int h) const { int hw = getHeadWidth(head); int hh = getHeadHeight(head); - if (x + w > hx + hw) - x = hx + hw - w; - if (y + h > hy + hh) - y = hy + hh - h; - - if (x < hx) - x = hx; - if (y < hy) - y = hy; + x = FbTk::Util::clamp(x, hx, hx + hw - w); + y = FbTk::Util::clamp(y, hy, hy + hh - h); return make_pair(x,y); } diff --git a/src/WindowState.cc b/src/WindowState.cc index 6632b66e..1e650d8e 100644 --- a/src/WindowState.cc +++ b/src/WindowState.cc @@ -246,10 +246,8 @@ void SizeHints::apply(unsigned int &width, unsigned int &height, w = increaseToMultiple(h * min_aspect_x / min_aspect_y, width_inc); } - unsigned int max_w = make_fit && (width < max_width || max_width == 0) ? - width : max_width; - unsigned int max_h = make_fit && (height < max_height || max_height == 0) ? - height : max_height; + unsigned int max_w = (make_fit && (width < max_width || max_width == 0)) ? width : max_width; + unsigned int max_h = (make_fit && (height < max_height || max_height == 0)) ? height : max_height; // Check maximum size if (max_w > 0 && w + base_width > max_w)