Skip to content

Commit

Permalink
Image Editor: add tooltip for Swap colors button
Browse files Browse the repository at this point in the history
Update COMPILING.MD
  • Loading branch information
zenden2k committed Jun 9, 2024
1 parent 35b1862 commit 56d3f7f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
3 changes: 2 additions & 1 deletion COMPILING.MD
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ Allow creating symlinks (you can do it by turning on Developer Mode on Windows 1
* cmake
* conan
* nuget
* Inno Setup 6
* 7-Zip (7z.exe)
* Inno Setup 6 (iscc.exe)
* WSL 2 & Ubuntu 20.04+

## Prerequisites On WSL (Ubuntu Linux)
Expand Down
34 changes: 31 additions & 3 deletions Source/ImageEditor/Gui/ColorsDelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ColorsDelegate.h"

#include <cassert>
#include <utility>

#include "Gui/GuiTools.h"
#include "Core/i18n/Translator.h"
Expand Down Expand Up @@ -34,7 +35,9 @@ ColorsDelegate::ColorsDelegate(Toolbar* toolbar, int itemIndex, Canvas* canvas)
assert(swapColorsIcon_);
}

SIZE ColorsDelegate::CalcItemSize(Toolbar::Item& item, float dpiScaleX, float dpiScaleY) {
SIZE ColorsDelegate::CalcItemSize(Toolbar::Item& item, int x, int y, float dpiScaleX, float dpiScaleY) {
foregroundRect_ = getForegroundRect(x, y, dpiScaleX, dpiScaleY);
swapColorsButtonRect_ = getSwapColorsRect(x, y, dpiScaleX, dpiScaleY);
SIZE res = { static_cast<LONG>((kSquareSize + kOffset +5 + kPadding)* dpiScaleX), static_cast<LONG>((kSquareSize + kOffset + 10)* dpiScaleY ) };
return res;
}
Expand All @@ -51,7 +54,7 @@ void ColorsDelegate::DrawItem(Toolbar::Item& item, Gdiplus::Graphics* gr, int x,
gr->FillRectangle(&backgroundBrush, backgroundRect_);
gr->DrawRectangle(&borderPen, backgroundRect_);

foregroundRect_ = Rect(int(kPadding*dpiScaleX + x), int(y+ dpiScaleY*4), int(kSquareSize * dpiScaleX), int(kSquareSize * dpiScaleY));
foregroundRect_ = getForegroundRect(x, y, dpiScaleX, dpiScaleY);
gr->FillRectangle(&foregroundBrush, foregroundRect_);
gr->DrawRectangle(&borderPen, foregroundRect_);

Expand All @@ -63,10 +66,23 @@ void ColorsDelegate::DrawItem(Toolbar::Item& item, Gdiplus::Graphics* gr, int x,
//toolbar_->ClientToScreen(&pt2);
backgroundColorButton_.SetWindowPos(0, pt2.x, pt2.y,0,0,/*SWP_NOSIZE*/0);

swapColorsButtonRect_ = Rect(foregroundRect_.GetRight() + 1, foregroundRect_.Y - 5, static_cast<int>(12 * dpiScaleX), static_cast<int>(12 * dpiScaleY));
swapColorsButtonRect_ = getSwapColorsRect(x, y, dpiScaleX, dpiScaleY);
gr->DrawImage(swapColorsIcon_.get(), swapColorsButtonRect_);
}


std::vector<std::pair<RECT, CString>> ColorsDelegate::getSubItemsHints()
{
Gdiplus::Rect rc1 = swapColorsButtonRect_;
RECT rc { rc1.GetLeft(), rc1.GetTop(),
rc1.GetRight(), rc1.GetBottom()
};
std::vector<std::pair<RECT, CString>> res {
{rc, TR("Swap colors")}
};
return res;
}

void ColorsDelegate::setForegroundColor(Gdiplus::Color color ) {
foregroundColor_ = color;
foregroundColorButton_.SetColor(color.ToCOLORREF());
Expand Down Expand Up @@ -112,6 +128,18 @@ void ColorsDelegate::OnBackgroundButtonSelChanged(COLORREF color, BOOL valid ) {
canvas_->setBackgroundColor(backgroundColor_);
}


Gdiplus::Rect ColorsDelegate::getForegroundRect(int x, int y, float dpiScaleX, float dpiScaleY) const
{
return Gdiplus::Rect(int(kPadding * dpiScaleX + x), int(y + dpiScaleY * 4), int(kSquareSize * dpiScaleX), int(kSquareSize * dpiScaleY));
}

Gdiplus::Rect ColorsDelegate::getSwapColorsRect(int x, int y, float dpiScaleX, float dpiScaleY) const
{
Gdiplus::Rect foregroundRect = getForegroundRect(x, y, dpiScaleX, dpiScaleY);
return Gdiplus::Rect(foregroundRect.GetRight() + 1, foregroundRect.Y - 5, static_cast<int>(12 * dpiScaleX), static_cast<int>(12 * dpiScaleY));
}

void ColorsDelegate::swapColors() {
std::swap(foregroundColor_, backgroundColor_);
canvas_->setForegroundColor(foregroundColor_);
Expand Down
8 changes: 5 additions & 3 deletions Source/ImageEditor/Gui/ColorsDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ namespace ImageEditor {
enum {kOffset = 7, kSquareSize = 16, kPadding = 3};

ColorsDelegate(Toolbar* toolbar, int itemIndex, Canvas* canvas);
SIZE CalcItemSize(Toolbar::Item& item, float dpiScaleX, float dpiScaleY) override;
SIZE CalcItemSize(Toolbar::Item& item, int x, int y, float dpiScaleX, float dpiScaleY) override;
void DrawItem(Toolbar::Item& item, Gdiplus::Graphics* gr, int x, int y, float dpiScaleX, float dpiScaleY) override;
std::vector<std::pair<RECT, CString>> getSubItemsHints() override;
void setForegroundColor(Gdiplus::Color color );
void setBackgroundColor(Gdiplus::Color color);
Gdiplus::Color getForegroundColor() const;
Expand Down Expand Up @@ -43,8 +44,9 @@ namespace ImageEditor {
void OnForegroundButtonSelChanged(COLORREF color, BOOL valid );

void OnBackgroundButtonSelChanged(COLORREF color, BOOL valid );

Gdiplus::Rect getForegroundRect(int x, int y, float dpiScaleX, float dpiScaleY) const;
Gdiplus::Rect getSwapColorsRect(int x, int y, float dpiScaleX, float dpiScaleY) const;
};
}

#endif
#endif
2 changes: 1 addition & 1 deletion Source/ImageEditor/Gui/ImageEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ void ImageEditorWindow::createToolbars()
verticalToolbar_.addButton(Toolbar::Item(CString(), loadToolbarIcon(IDB_ICONCOLORPICKERPNG), ID_COLORPICKER,TR("Color chooser")+ CString(_T(" (")) + (char)kColorPickerKey + CString(_T(")")), Toolbar::itButton, true,1));
int index = verticalToolbar_.addButton(Toolbar::Item(CString(), loadToolbarIcon(IDB_ICONUNDOPNG), ID_UNDO,TR("Undo") + CString(L" (Ctrl+Z)"), Toolbar::itButton, false));

Toolbar::Item colorsButton(CString(), loadToolbarIcon(IDB_ICONUNDOPNG), ID_UNDO,CString(), Toolbar::itButton, false);
Toolbar::Item colorsButton(CString(), loadToolbarIcon(IDB_ICONUNDOPNG), ID_UNDO, {}, Toolbar::itButton, false);
colorsDelegate_ = std::make_unique<ColorsDelegate>(&verticalToolbar_, index+1, canvas_.get());
colorsButton.itemDelegate = colorsDelegate_.get();
colorsDelegate_->setBackgroundColor(canvas_->getBackgroundColor());
Expand Down
46 changes: 27 additions & 19 deletions Source/ImageEditor/Gui/Toolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ LRESULT Toolbar::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BO
int x = itemMargin_;
int y = itemMargin_;
for (size_t i = 0; i < buttons_.size(); i++) {
SIZE s = CalcItemSize(&gr, i);
SIZE s = CalcItemSize(&gr, i, x, y);
drawItem(i, &gr, x, y);

if ( orientation_ == orHorizontal ) {
Expand Down Expand Up @@ -531,14 +531,14 @@ LRESULT Toolbar::OnNcHitTest(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/
return 0;
}

SIZE Toolbar::CalcItemSize(Gdiplus::Graphics* gr, int index)
SIZE Toolbar::CalcItemSize(Gdiplus::Graphics* gr, int index, int x, int y)
{
using namespace Gdiplus;
SIZE res={0,0};
Item &item = buttons_[index];

if ( item.itemDelegate ) {
return item.itemDelegate->CalcItemSize(item,dpiScaleX_, dpiScaleY_);
return item.itemDelegate->CalcItemSize(item, x, y, dpiScaleX_, dpiScaleY_);
}

if (showButtonText_ && item.title.GetLength()) {
Expand Down Expand Up @@ -582,7 +582,7 @@ int Toolbar::AutoSize()
Gdiplus::Graphics gr(dc);

for (size_t i = 0; i < buttons_.size(); i++) {
SIZE s = CalcItemSize(&gr, i);
SIZE s = CalcItemSize(&gr, i, x, y);
Item& item = buttons_[i];
Gdiplus::RectF bounds(static_cast<Gdiplus::REAL>(x), static_cast<Gdiplus::REAL>(y), float(s.cx), float(s.cy));
item.rect.left = x;
Expand Down Expand Up @@ -704,7 +704,7 @@ int Toolbar::AutoSize()
void Toolbar::drawItem(int itemIndex, Gdiplus::Graphics* gr, int x, int y)
{
using namespace Gdiplus;
SIZE size = CalcItemSize(gr, itemIndex);
SIZE size = CalcItemSize(gr, itemIndex, x, y);

Item& item = buttons_[itemIndex];

Expand Down Expand Up @@ -798,22 +798,30 @@ void Toolbar::CreateToolTipForItem(size_t index)
{
Item& item = buttons_[index];

if (item.hint.IsEmpty()) {
std::vector<std::pair<RECT, CString>> hints;

if (!item.hint.IsEmpty()) {
hints.emplace_back(item.rect, item.hint);
} else if (item.itemDelegate) {
hints = item.itemDelegate->getSubItemsHints();
} else {
return;
}

TOOLINFO ti = {};
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = m_hWnd;
ti.hinst = _Module.GetModuleInstance();
//CString textBuffer = item.hint;
auto textBuffer = std::make_unique<TCHAR[]>(item.hint.GetLength() + 1);
StringCchCopy(textBuffer.get(), item.hint.GetLength() + 1, item.hint);
ti.lpszText = textBuffer.get();
ti.rect = item.rect;
ti.uId = static_cast<UINT_PTR>(index);
tooltip_.AddTool(&ti);

for (const auto& hint : hints) {
TOOLINFO ti = {};
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = m_hWnd;
ti.hinst = _Module.GetModuleInstance();
//CString textBuffer = item.hint;
auto textBuffer = std::make_unique<TCHAR[]>(hint.second.GetLength() + 1);
StringCchCopy(textBuffer.get(), hint.second.GetLength() + 1, hint.second);
ti.lpszText = textBuffer.get();
ti.rect = hint.first;
ti.uId = static_cast<UINT_PTR>(index);
tooltip_.AddTool(&ti);
}
}

void Toolbar::updateTooltipForItem(size_t index) {
Expand Down
6 changes: 4 additions & 2 deletions Source/ImageEditor/Gui/Toolbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "atlheaders.h"

#include <memory>
#include <utility>
#include <vector>

#include "CustomTrackBarControl.h"
Expand Down Expand Up @@ -65,9 +66,10 @@ class Toolbar : public CWindowImpl<Toolbar> {
class ToolbarItemDelegate {
public:
virtual ~ToolbarItemDelegate() {}
virtual SIZE CalcItemSize(Item& item, float dpiScaleX, float dpiScaleY) = 0;
virtual SIZE CalcItemSize(Item& item, int x, int y, float dpiScaleX, float dpiScaleY) = 0;
virtual void DrawItem(Item& item, Gdiplus::Graphics* gr, int, int y, float dpiScaleX, float dpiScaleY) = 0;
virtual void OnClick(int x, int y, float dpiScaleX, float dpiScaleY){};
virtual std::vector<std::pair<RECT, CString>> getSubItemsHints() { return {}; };
};

explicit Toolbar(Orientation orientation);
Expand Down Expand Up @@ -149,7 +151,7 @@ class Toolbar : public CWindowImpl<Toolbar> {
LRESULT OnArrowTypeComboChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnApplyButtonClicked(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnCancelOperationButtonClicked(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
SIZE CalcItemSize(Gdiplus::Graphics* gr, int index);
SIZE CalcItemSize(Gdiplus::Graphics* gr, int index, int x, int y);
int AutoSize();
void CreateToolTipForItem(size_t index);
void updateTooltipForItem(size_t index);
Expand Down

0 comments on commit 56d3f7f

Please sign in to comment.