all repos — fluxbox @ 77f39235cf3ad79c5ff7d2b0f4717660476f3cf4

custom fork of the fluxbox windowmanager

added FbTk::Util::clamp() and simplified related code
Mathias Gumz akira at fluxbox dot org
commit

77f39235cf3ad79c5ff7d2b0f4717660476f3cf4

parent

1657374940998176c7b63eb3296265fe6fbb5458

M src/ArrowButton.ccsrc/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 @@ const ButtonTheme &btheme = static_cast<const ButtonTheme &>(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); }
M src/CurrentWindowCmd.ccsrc/CurrentWindowCmd.cc

@@ -35,6 +35,7 @@ #include "FbTk/CommandParser.hh"

#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 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 @@ fbwindow().setDefaultAlpha();

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);
M src/FbTk/Makefile.amsrc/FbTk/Makefile.am

@@ -64,6 +64,7 @@ TypeAhead.hh SearchResult.hh SearchResult.cc ITypeAheadable.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} \
M src/FbTk/MultLayers.ccsrc/FbTk/MultLayers.cc

@@ -26,6 +26,8 @@ #include "XLayerItem.hh"

#include "App.hh" #include "FbWindow.hh" +#include "Util.hh" + using namespace FbTk; MultLayers::MultLayers(int numlayers) :

@@ -56,11 +58,7 @@

} void MultLayers::addToTop(XLayerItem &item, int layernum) { - if (layernum < 0) - layernum = 0; - else if (layernum >= static_cast<signed>(m_layers.size())) - layernum = m_layers.size()-1; - + layernum = FbTk::Util::clamp(layernum, 0, static_cast<signed>(m_layers.size()) - 1); m_layers[layernum]->insert(item); restack(); }

@@ -108,12 +106,7 @@ // do nothing if the item already is in the requested layer

if (curr_layer.getLayerNum() == layernum) return; - // clamp layer number - if (layernum < 0) - layernum = 0; - else if (layernum >= static_cast<signed>(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<signed>(m_layers.size()) - 1); item.setLayer(*m_layers[layernum]); }
A 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<typename T> +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
M src/FbWinFrameTheme.ccsrc/FbWinFrameTheme.cc

@@ -21,6 +21,7 @@ // DEALINGS IN THE SOFTWARE.

#include "FbWinFrameTheme.hh" #include "FbTk/App.hh" +#include "FbTk/Util.hh" #include "IconbarTheme.hh"

@@ -88,16 +89,10 @@ return false;

} 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(); }
M src/IconbarTool.ccsrc/IconbarTool.cc

@@ -47,6 +47,7 @@ #include "FbTk/SimpleCommand.hh"

#include "FbTk/ImageControl.hh" #include "FbTk/MacroCommand.hh" #include "FbTk/MenuSeparator.hh" +#include "FbTk/Util.hh" #include <typeinfo> #include <iterator>

@@ -389,12 +390,8 @@ return;

} 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() ||
M src/Remember.ccsrc/Remember.cc

@@ -41,6 +41,7 @@ #include "FbTk/stringstream.hh"

#include "FbTk/Transparent.hh" #include "FbTk/AutoReloadHelper.hh" #include "FbTk/RefCount.hh" +#include "FbTk/Util.hh" #ifdef HAVE_CSTRING #include <cstring>

@@ -512,28 +513,18 @@ else

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 <focus>' + unfocused_a = focused_a; + case 2: // 'alpha <focus> <unfocus>' + 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") {
M src/Screen.ccsrc/Screen.cc

@@ -73,6 +73,7 @@ #include "FbTk/Compose.hh"

#include "FbTk/FbString.hh" #include "FbTk/STLUtil.hh" #include "FbTk/KeyUtil.hh" +#include "FbTk/Util.hh" //use GNU extensions #ifndef _GNU_SOURCE

@@ -202,6 +203,10 @@ private:

BScreen &m_screen; FbWinFrame::TabPlacement m_place; }; + +void clampMenuDelay(int& delay) { + delay = FbTk::Util::clamp(delay, 0, 5000); +} } // end anonymous namespace

@@ -453,11 +458,8 @@

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 @@ 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);

@@ -2138,15 +2136,8 @@ int hy = getHeadY(head);

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); }
M src/WindowState.ccsrc/WindowState.cc

@@ -246,10 +246,8 @@ if (min_aspect_y > 0 && w * min_aspect_y < min_aspect_x * h)

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)