Skip to content

Commit

Permalink
Show an icon in the tooltip of a toolbar button for issue #637.
Browse files Browse the repository at this point in the history
  • Loading branch information
gijsbers committed Dec 3, 2022
1 parent 7100470 commit 7b61758
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ SET(ICE_COMMON_SRCS udir.cc upath.cc yapp.cc yxapp.cc ytimer.cc yprefs.cc
ypipereader.cc yxembed.cc yconfig.cc yfont.cc ysvg.cc
ypixmap.cc yimage2.cc yimage_gdk.cc yximage.cc ycolor.cc
ytooltip.cc ylocale.cc yarray.cc yfileio.cc ytime.cc
ystring.cc mstring.cc ref.cc logevent.cc misc.cc)
yicon.cc ystring.cc mstring.cc ref.cc logevent.cc misc.cc)

if(CONFIG_XFREETYPE)
list(APPEND ICE_COMMON_SRCS yfontxft.cc)
Expand All @@ -424,7 +424,7 @@ target_include_directories(ice PUBLIC ${Intl_INCLUDE_DIRS})

SET(ITK_SRCS ymenu.cc ylabel.cc yscrollview.cc ymenuitem.cc
yscrollbar.cc ybutton.cc ylistbox.cc yinputline.cc
globit.cc yicon.cc wmconfig.cc wmsave.cc wpixres.cc)
globit.cc wmconfig.cc wmsave.cc wpixres.cc)

do_auto_inc_headers(ITK_SRCS)
add_library(itk STATIC ${ITK_SRCS})
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ libice_la_SOURCES = \
yfontname.h \
yfontxft.cc \
yfull.h \
yicon.cc \
yicon.h \
yimage.h \
yimage2.cc \
yimage2.h \
Expand Down Expand Up @@ -221,8 +223,6 @@ libitk_la_SOURCES = \
yconfig.h \
ycursor.h \
yfull.h \
yicon.cc \
yicon.h \
yimage.h \
yinputline.cc \
yinputline.h \
Expand Down
12 changes: 8 additions & 4 deletions src/objbutton.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ void ObjectButton::handleButton(const XButtonEvent& up) {
}

void ObjectButton::updateToolTip() {
if (getToolTip() == null) {
xsmart<char> name;
if (fetchTitle(&name)) {
setToolTip(mstring(name));
if (hasToolTip() == false) {
if (fObject) {
setToolTip(fObject->getName(), fObject->getIcon());
} else {
xsmart<char> name;
if (fetchTitle(&name)) {
setToolTip(mstring(name), getIcon());
}
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/themable.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ XIV(unsigned, topSideVerticalOffset, 0)
XIV(unsigned, scrollBarWidth, 16)
XIV(unsigned, scrollBarHeight, 16)

XIV(unsigned, menuIconSize, 16)
XIV(unsigned, smallIconSize, 16)
XIV(unsigned, largeIconSize, 32)
XIV(unsigned, hugeIconSize, 48)

XIV(unsigned, quickSwitchHMargin, 3) // !!!
XIV(unsigned, quickSwitchVMargin, 3) // !!!
XIV(unsigned, quickSwitchIMargin, 4) // !!!
Expand Down
4 changes: 4 additions & 0 deletions src/ybutton.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ void YButton::setIcon(ref<YIcon> icon, int iconSize) {
}
}

ref<YIcon> YButton::getIcon() {
return fIcon;
}

void YButton::setImage(ref<YImage> image) {
if (image != fImage || fIcon != null || fIconSize != 0) {
fImage = image;
Expand Down
1 change: 1 addition & 0 deletions src/ybutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class YButton: public YWindow {
void setAction(YAction action);
void setPopup(YMenu * popup);
void setIcon(ref<YIcon> image, int size);
ref<YIcon> getIcon();
void setImage(ref<YImage> image);
void setText(const mstring &str, int hot = -1);
mstring getText() const { return fText; }
Expand Down
5 changes: 5 additions & 0 deletions src/yprefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ XIV(int, ToolTipTime, 0)
///#warning "move this one back to WM"
XIV(bool, grabRootWindow, true)

XIV(unsigned, menuIconSize, 16)
XIV(unsigned, smallIconSize, 16)
XIV(unsigned, largeIconSize, 32)
XIV(unsigned, hugeIconSize, 48)

XSV(const char *, iconPath,
"/usr/local/share/icons:"
"/usr/local/share/pixmaps:"
Expand Down
33 changes: 27 additions & 6 deletions src/ytooltip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "prefs.h"
#include "yprefs.h"
#include "yrect.h"
#include "yicon.h"

#include <string.h>

Expand Down Expand Up @@ -49,26 +50,46 @@ void YToolTipWindow::paint(Graphics& g, const YRect& /*r*/) {
g.setColor(toolTipFg);
g.drawStringMultiline(fText, TTXMargin, y, width() - 2 * TTXMargin);
}
if (fIcon != null) {
int y = int(height() - hugeIconSize) - TTYMargin;
int x = int(width() - hugeIconSize) / 2;
fIcon->draw(g, x, y, hugeIconSize);
}
}

void YToolTipWindow::setText(const mstring& tip) {
void YToolTipWindow::setText(mstring tip, ref<YIcon> icon) {
fText = tip;
fIcon = icon;
unsigned w = 0, h = 0;
if (toolTipFont) {
YDimension size(toolTipFont->multilineAlloc(fText));
setSize(size.w + 2 * TTXMargin, size.h + 3 + 2 * TTYMargin);
w += size.w;
h += size.h + 3;
}
if (fIcon != null) {
h += hugeIconSize;
w = max(w, hugeIconSize);
}
if (w && h) {
setSize(w + 2 * TTXMargin, h + 2 * TTYMargin);
}
}

void YToolTip::setText(const mstring& tip) {
void YToolTip::setText(mstring tip, ref<YIcon> icon) {
fText = tip;
fIcon = icon;
if (fWindow) {
fWindow->setText(tip);
fWindow->setText(tip, icon);
fWindow->locate(fLocate);
fWindow->repaint();
}
}

bool YToolTip::visible() {
bool YToolTip::nonempty() const {
return fText != null || fIcon != null;
}

bool YToolTip::visible() const {
return fWindow;
}

Expand All @@ -78,7 +99,7 @@ bool YToolTip::handleTimer(YTimer *timer) {
fWindow = null;
}
else {
fWindow->setText(fText);
fWindow->setText(fText, fIcon);
fWindow->locate(fLocate);
fWindow->repaint();
fWindow->show();
Expand Down
10 changes: 6 additions & 4 deletions src/ytooltip.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class YToolTipWindow: public YWindow {
virtual void handleExpose(const XExposeEvent& expose) {}
virtual void repaint();

void setText(const mstring& tip);
void setText(mstring tip, ref<YIcon> icon);
void locate(YWindow* w);

private:
mstring fText;
ref<YIcon> fIcon;

YColorName toolTipBg;
YColorName toolTipFg;
Expand All @@ -30,18 +31,19 @@ class YToolTip: public YTimerListener {

virtual bool handleTimer(YTimer *t);

mstring getText() { return fText; }
void setText(const mstring& tip);
void setText(mstring tip, ref<YIcon> icon);
void enter(YWindow* w);
void leave();
bool visible();
bool visible() const;
bool nonempty() const;

private:
void expose();
YToolTipWindow* window();

mstring fText;
YWindow* fLocate;
ref<YIcon> fIcon;
lazy<YToolTipWindow> fWindow;
lazy<YTimer> fTimer;
};
Expand Down
13 changes: 9 additions & 4 deletions src/ywindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "sysdep.h"
#include "yprefs.h"
#include "yrect.h"
#include "yicon.h"
#include "ascii.h"

#include "ytimer.h"
Expand Down Expand Up @@ -911,16 +912,20 @@ bool YWindow::handleBeginDrag(const XButtonEvent &, const XMotionEvent &) {
return false;
}

void YWindow::setToolTip(const mstring& tip) {
void YWindow::setToolTip(mstring tip) {
setToolTip(tip, null);
}

void YWindow::setToolTip(mstring tip, ref<YIcon> icon) {
if (tip == null) {
fToolTip = null;
} else {
fToolTip->setText(tip);
fToolTip->setText(tip, icon);
}
}

mstring YWindow::getToolTip() {
return fToolTip ? fToolTip->getText() : null;
bool YWindow::hasToolTip() const {
return fToolTip._ptr() != nullptr && fToolTip._ptr()->nonempty();
}

bool YWindow::toolTipVisible() {
Expand Down
6 changes: 4 additions & 2 deletions src/ywindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class YTimer;
class YAutoScroll;
class YRect;
class YRect2;
class YIcon;

struct DesktopScreenInfo {
int screen_number;
Expand Down Expand Up @@ -216,8 +217,9 @@ class YWindow : protected YWindowList, private YWindowNode {
void installAccelerator(unsigned key, unsigned mod, YWindow *win);
void removeAccelerator(unsigned key, unsigned mod, YWindow *win);

mstring getToolTip();
void setToolTip(const mstring &tip);
bool hasToolTip() const;
void setToolTip(mstring tip);
void setToolTip(mstring tip, ref<YIcon> icon);

void mapToGlobal(int &x, int &y);
void mapToLocal(int &x, int &y);
Expand Down

0 comments on commit 7b61758

Please sign in to comment.