added FbTk::Util::clamp() and simplified related code
This commit is contained in:
parent
1657374940
commit
77f39235cf
10 changed files with 91 additions and 91 deletions
|
@ -22,6 +22,8 @@
|
||||||
#include "ArrowButton.hh"
|
#include "ArrowButton.hh"
|
||||||
#include "ButtonTheme.hh"
|
#include "ButtonTheme.hh"
|
||||||
|
|
||||||
|
#include "FbTk/Util.hh"
|
||||||
|
|
||||||
ArrowButton::ArrowButton(FbTk::FbDrawable::TriangleType arrow_type,
|
ArrowButton::ArrowButton(FbTk::FbDrawable::TriangleType arrow_type,
|
||||||
const FbTk::FbWindow &parent,
|
const FbTk::FbWindow &parent,
|
||||||
int x, int y,
|
int x, int y,
|
||||||
|
@ -92,6 +94,6 @@ void ArrowButton::updateTheme(const FbTk::Theme &theme) {
|
||||||
|
|
||||||
m_arrowscale = btheme.scale();
|
m_arrowscale = btheme.scale();
|
||||||
if (m_arrowscale == 0) m_arrowscale = 250; // default is 0 => 300
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
#include "FbTk/I18n.hh"
|
#include "FbTk/I18n.hh"
|
||||||
#include "FbTk/stringstream.hh"
|
#include "FbTk/stringstream.hh"
|
||||||
#include "FbTk/StringUtil.hh"
|
#include "FbTk/StringUtil.hh"
|
||||||
|
#include "FbTk/Util.hh"
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
@ -250,8 +251,7 @@ void SetHeadCmd::real_execute() {
|
||||||
int num = m_head;
|
int num = m_head;
|
||||||
int total = fbwindow().screen().numHeads();
|
int total = fbwindow().screen().numHeads();
|
||||||
if (num < 0) num += total + 1;
|
if (num < 0) num += total + 1;
|
||||||
if (num < 1) num = 1;
|
num = FbTk::Util::clamp(num, 1, total);
|
||||||
if (num > total) num = total;
|
|
||||||
fbwindow().setOnHead(num);
|
fbwindow().setOnHead(num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,8 +259,7 @@ void SendToWorkspaceCmd::real_execute() {
|
||||||
int num = m_workspace_num;
|
int num = m_workspace_num;
|
||||||
int total = fbwindow().screen().numberOfWorkspaces();
|
int total = fbwindow().screen().numberOfWorkspaces();
|
||||||
if (num < 0) num += total + 1;
|
if (num < 0) num += total + 1;
|
||||||
if (num < 1) num = 1;
|
num = FbTk::Util::clamp(num, 1, total);
|
||||||
if (num > total) num = total;
|
|
||||||
fbwindow().screen().sendToWorkspace(num-1, &fbwindow(), m_take);
|
fbwindow().screen().sendToWorkspace(num-1, &fbwindow(), m_take);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,8 +280,7 @@ void SendToNextHeadCmd::real_execute() {
|
||||||
void GoToTabCmd::real_execute() {
|
void GoToTabCmd::real_execute() {
|
||||||
int num = m_tab_num;
|
int num = m_tab_num;
|
||||||
if (num < 0) num += fbwindow().numClients() + 1;
|
if (num < 0) num += fbwindow().numClients() + 1;
|
||||||
if (num < 1) num = 1;
|
num = FbTk::Util::clamp(num, 1, fbwindow().numClients());
|
||||||
if (num > fbwindow().numClients()) num = fbwindow().numClients();
|
|
||||||
|
|
||||||
FluxboxWindow::ClientList::iterator it = fbwindow().clientList().begin();
|
FluxboxWindow::ClientList::iterator it = fbwindow().clientList().begin();
|
||||||
|
|
||||||
|
@ -670,22 +668,13 @@ void SetAlphaCmd::real_execute() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int new_alpha;
|
fbwindow().setFocusedAlpha(m_relative
|
||||||
if (m_relative) {
|
? FbTk::Util::clamp(fbwindow().getFocusedAlpha() + m_focus, 0, 255)
|
||||||
new_alpha = fbwindow().getFocusedAlpha() + m_focus;
|
: 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);
|
|
||||||
|
|
||||||
if (m_un_relative) {
|
fbwindow().setUnfocusedAlpha(m_un_relative
|
||||||
new_alpha = fbwindow().getUnfocusedAlpha() + m_unfocus;
|
? FbTk::Util::clamp(fbwindow().getUnfocusedAlpha() + m_unfocus, 0, 255)
|
||||||
if (new_alpha < 0) new_alpha = 0;
|
: m_unfocus);
|
||||||
if (new_alpha > 255) new_alpha = 255;
|
|
||||||
fbwindow().setUnfocusedAlpha(new_alpha);
|
|
||||||
} else
|
|
||||||
fbwindow().setUnfocusedAlpha(m_unfocus);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_COMMAND_WITH_ARGS(matches, MatchCmd, bool);
|
REGISTER_COMMAND_WITH_ARGS(matches, MatchCmd, bool);
|
||||||
|
|
|
@ -64,6 +64,7 @@ libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \
|
||||||
Select2nd.hh STLUtil.hh \
|
Select2nd.hh STLUtil.hh \
|
||||||
CachedPixmap.hh CachedPixmap.cc \
|
CachedPixmap.hh CachedPixmap.cc \
|
||||||
Slot.hh Signal.hh MemFun.hh RelaySignal.hh SelectArg.hh \
|
Slot.hh Signal.hh MemFun.hh RelaySignal.hh SelectArg.hh \
|
||||||
|
Util.hh \
|
||||||
${xpm_SOURCE} \
|
${xpm_SOURCE} \
|
||||||
${xft_SOURCE} \
|
${xft_SOURCE} \
|
||||||
${xmb_SOURCE} \
|
${xmb_SOURCE} \
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
#include "App.hh"
|
#include "App.hh"
|
||||||
#include "FbWindow.hh"
|
#include "FbWindow.hh"
|
||||||
|
|
||||||
|
#include "Util.hh"
|
||||||
|
|
||||||
using namespace FbTk;
|
using namespace FbTk;
|
||||||
|
|
||||||
MultLayers::MultLayers(int numlayers) :
|
MultLayers::MultLayers(int numlayers) :
|
||||||
|
@ -56,11 +58,7 @@ XLayerItem *MultLayers::getLowestItemAboveLayer(int layernum) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void MultLayers::addToTop(XLayerItem &item, int layernum) {
|
void MultLayers::addToTop(XLayerItem &item, int layernum) {
|
||||||
if (layernum < 0)
|
layernum = FbTk::Util::clamp(layernum, 0, static_cast<signed>(m_layers.size()) - 1);
|
||||||
layernum = 0;
|
|
||||||
else if (layernum >= static_cast<signed>(m_layers.size()))
|
|
||||||
layernum = m_layers.size()-1;
|
|
||||||
|
|
||||||
m_layers[layernum]->insert(item);
|
m_layers[layernum]->insert(item);
|
||||||
restack();
|
restack();
|
||||||
}
|
}
|
||||||
|
@ -108,12 +106,7 @@ void MultLayers::moveToLayer(XLayerItem &item, int layernum) {
|
||||||
if (curr_layer.getLayerNum() == layernum)
|
if (curr_layer.getLayerNum() == layernum)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// clamp layer number
|
layernum = FbTk::Util::clamp(layernum, 0, static_cast<signed>(m_layers.size()) - 1);
|
||||||
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
|
|
||||||
item.setLayer(*m_layers[layernum]);
|
item.setLayer(*m_layers[layernum]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
43
src/FbTk/Util.hh
Normal file
43
src/FbTk/Util.hh
Normal file
|
@ -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
|
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
#include "FbWinFrameTheme.hh"
|
#include "FbWinFrameTheme.hh"
|
||||||
#include "FbTk/App.hh"
|
#include "FbTk/App.hh"
|
||||||
|
#include "FbTk/Util.hh"
|
||||||
|
|
||||||
#include "IconbarTheme.hh"
|
#include "IconbarTheme.hh"
|
||||||
|
|
||||||
|
@ -88,16 +89,10 @@ bool FbWinFrameTheme::fallback(FbTk::ThemeItem_base &item) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void FbWinFrameTheme::reconfigTheme() {
|
void FbWinFrameTheme::reconfigTheme() {
|
||||||
if (*m_bevel_width > 20)
|
*m_bevel_width = FbTk::Util::clamp(*m_bevel_width, 0, 20);
|
||||||
*m_bevel_width = 20;
|
if (*m_handle_width < 0)
|
||||||
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_handle_width = 1;
|
*m_handle_width = 1;
|
||||||
|
*m_handle_width = FbTk::Util::clamp(*m_handle_width, 0, 200);
|
||||||
m_button_pic_gc.setForeground(*m_button_color);
|
m_button_pic_gc.setForeground(*m_button_color);
|
||||||
m_iconbar_theme.reconfigTheme();
|
m_iconbar_theme.reconfigTheme();
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@
|
||||||
#include "FbTk/ImageControl.hh"
|
#include "FbTk/ImageControl.hh"
|
||||||
#include "FbTk/MacroCommand.hh"
|
#include "FbTk/MacroCommand.hh"
|
||||||
#include "FbTk/MenuSeparator.hh"
|
#include "FbTk/MenuSeparator.hh"
|
||||||
|
#include "FbTk/Util.hh"
|
||||||
|
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
@ -389,12 +390,8 @@ void IconbarTool::update(FbTk::Subject *subj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
m_icon_container.setAlignment(*m_rc_alignment);
|
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);
|
m_icon_container.setMaxSizePerClient(*m_rc_client_width);
|
||||||
|
|
||||||
if (subj == &m_focused_theme.reconfigSig() ||
|
if (subj == &m_focused_theme.reconfigSig() ||
|
||||||
|
|
|
@ -41,6 +41,7 @@
|
||||||
#include "FbTk/Transparent.hh"
|
#include "FbTk/Transparent.hh"
|
||||||
#include "FbTk/AutoReloadHelper.hh"
|
#include "FbTk/AutoReloadHelper.hh"
|
||||||
#include "FbTk/RefCount.hh"
|
#include "FbTk/RefCount.hh"
|
||||||
|
#include "FbTk/Util.hh"
|
||||||
|
|
||||||
#ifdef HAVE_CSTRING
|
#ifdef HAVE_CSTRING
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
@ -512,28 +513,18 @@ int parseApp(ifstream &file, Application &app, string *first_line = 0) {
|
||||||
app.rememberDecostate((unsigned int)deco);
|
app.rememberDecostate((unsigned int)deco);
|
||||||
} else if (str_key == "alpha") {
|
} else if (str_key == "alpha") {
|
||||||
int focused_a, unfocused_a;
|
int focused_a, unfocused_a;
|
||||||
if (sscanf(str_label.c_str(), "%i %i", &focused_a, &unfocused_a) == 2)
|
switch (sscanf(str_label.c_str(), "%i %i", &focused_a, &unfocused_a)) {
|
||||||
{
|
case 1: // 'alpha <focus>'
|
||||||
// clamp;
|
unfocused_a = focused_a;
|
||||||
if (focused_a > 255)
|
case 2: // 'alpha <focus> <unfocus>'
|
||||||
focused_a = 255;
|
focused_a = FbTk::Util::clamp(focused_a, 0, 255);
|
||||||
if (unfocused_a > 255)
|
unfocused_a = FbTk::Util::clamp(unfocused_a, 0, 255);
|
||||||
unfocused_a = 255;
|
|
||||||
if (focused_a <= 0)
|
|
||||||
focused_a = 0;
|
|
||||||
if (unfocused_a <= 0)
|
|
||||||
unfocused_a = 0;
|
|
||||||
|
|
||||||
app.rememberAlpha(focused_a, unfocused_a);
|
app.rememberAlpha(focused_a, unfocused_a);
|
||||||
} else if (sscanf(str_label.c_str(), "%i", &focused_a) == 1) {
|
break;
|
||||||
if (focused_a > 255)
|
default:
|
||||||
focused_a = 255;
|
had_error = true;
|
||||||
if (focused_a <= 0)
|
break;
|
||||||
focused_a = 0;
|
|
||||||
app.rememberAlpha(focused_a, focused_a);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
had_error = 1;
|
|
||||||
} else if (str_key == "sticky") {
|
} else if (str_key == "sticky") {
|
||||||
app.rememberStuckstate((strcasecmp(str_label.c_str(), "yes") == 0));
|
app.rememberStuckstate((strcasecmp(str_label.c_str(), "yes") == 0));
|
||||||
} else if (str_key == "minimized") {
|
} else if (str_key == "minimized") {
|
||||||
|
|
|
@ -73,6 +73,7 @@
|
||||||
#include "FbTk/FbString.hh"
|
#include "FbTk/FbString.hh"
|
||||||
#include "FbTk/STLUtil.hh"
|
#include "FbTk/STLUtil.hh"
|
||||||
#include "FbTk/KeyUtil.hh"
|
#include "FbTk/KeyUtil.hh"
|
||||||
|
#include "FbTk/Util.hh"
|
||||||
|
|
||||||
//use GNU extensions
|
//use GNU extensions
|
||||||
#ifndef _GNU_SOURCE
|
#ifndef _GNU_SOURCE
|
||||||
|
@ -203,6 +204,10 @@ private:
|
||||||
FbWinFrame::TabPlacement m_place;
|
FbWinFrame::TabPlacement m_place;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void clampMenuDelay(int& delay) {
|
||||||
|
delay = FbTk::Util::clamp(delay, 0, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
} // end anonymous namespace
|
} // end anonymous namespace
|
||||||
|
|
||||||
|
|
||||||
|
@ -453,11 +458,8 @@ BScreen::BScreen(FbTk::ResourceManager &rm,
|
||||||
focusedWinFrameTheme()->setAlpha(*resource.focused_alpha);
|
focusedWinFrameTheme()->setAlpha(*resource.focused_alpha);
|
||||||
unfocusedWinFrameTheme()->setAlpha(*resource.unfocused_alpha);
|
unfocusedWinFrameTheme()->setAlpha(*resource.unfocused_alpha);
|
||||||
m_menutheme->setAlpha(*resource.menu_alpha);
|
m_menutheme->setAlpha(*resource.menu_alpha);
|
||||||
// clamp values
|
|
||||||
if (*resource.menu_delay > 5000)
|
clampMenuDelay(*resource.menu_delay);
|
||||||
*resource.menu_delay = 5000;
|
|
||||||
if (*resource.menu_delay < 0)
|
|
||||||
*resource.menu_delay = 0;
|
|
||||||
|
|
||||||
m_menutheme->setDelay(*resource.menu_delay);
|
m_menutheme->setDelay(*resource.menu_delay);
|
||||||
|
|
||||||
|
@ -905,11 +907,7 @@ void BScreen::reconfigure() {
|
||||||
unfocusedWinFrameTheme()->setAlpha(*resource.unfocused_alpha);
|
unfocusedWinFrameTheme()->setAlpha(*resource.unfocused_alpha);
|
||||||
m_menutheme->setAlpha(*resource.menu_alpha);
|
m_menutheme->setAlpha(*resource.menu_alpha);
|
||||||
|
|
||||||
// clamp values
|
clampMenuDelay(*resource.menu_delay);
|
||||||
if (*resource.menu_delay > 5000)
|
|
||||||
*resource.menu_delay = 5000;
|
|
||||||
if (*resource.menu_delay < 0)
|
|
||||||
*resource.menu_delay = 0;
|
|
||||||
|
|
||||||
m_menutheme->setDelay(*resource.menu_delay);
|
m_menutheme->setDelay(*resource.menu_delay);
|
||||||
|
|
||||||
|
@ -2138,15 +2136,8 @@ pair<int,int> BScreen::clampToHead(int head, int x, int y, int w, int h) const {
|
||||||
int hw = getHeadWidth(head);
|
int hw = getHeadWidth(head);
|
||||||
int hh = getHeadHeight(head);
|
int hh = getHeadHeight(head);
|
||||||
|
|
||||||
if (x + w > hx + hw)
|
x = FbTk::Util::clamp(x, hx, hx + hw - w);
|
||||||
x = hx + hw - w;
|
y = FbTk::Util::clamp(y, hy, hy + hh - h);
|
||||||
if (y + h > hy + hh)
|
|
||||||
y = hy + hh - h;
|
|
||||||
|
|
||||||
if (x < hx)
|
|
||||||
x = hx;
|
|
||||||
if (y < hy)
|
|
||||||
y = hy;
|
|
||||||
|
|
||||||
return make_pair(x,y);
|
return make_pair(x,y);
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,10 +246,8 @@ void SizeHints::apply(unsigned int &width, unsigned int &height,
|
||||||
w = increaseToMultiple(h * min_aspect_x / min_aspect_y, width_inc);
|
w = increaseToMultiple(h * min_aspect_x / min_aspect_y, width_inc);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int max_w = make_fit && (width < max_width || max_width == 0) ?
|
unsigned int max_w = (make_fit && (width < max_width || max_width == 0)) ? width : max_width;
|
||||||
width : max_width;
|
unsigned int max_h = (make_fit && (height < max_height || max_height == 0)) ? height : max_height;
|
||||||
unsigned int max_h = make_fit && (height < max_height || max_height == 0) ?
|
|
||||||
height : max_height;
|
|
||||||
|
|
||||||
// Check maximum size
|
// Check maximum size
|
||||||
if (max_w > 0 && w + base_width > max_w)
|
if (max_w > 0 && w + base_width > max_w)
|
||||||
|
|
Loading…
Reference in a new issue