Skip to content

Commit

Permalink
Fixed server icon in login dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
zenden2k committed Jul 29, 2024
1 parent 63cf4da commit d739c88
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 307 deletions.
278 changes: 141 additions & 137 deletions Lang/imageuploader.pot

Large diffs are not rendered by default.

282 changes: 143 additions & 139 deletions Lang/locale/ru/LC_MESSAGES/imageuploader.po

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Source/Core/AbstractServerIconCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ std::string AbstractServerIconCache::getIconNameForServer(const std::string& nam
CUploadEngineData* ued = engineList_->byName(name);

std::string serverName = IuStringUtils::Replace(name, "\\", "_");
serverName = IuStringUtils::Replace(name, "/", "_");
serverName = IuStringUtils::Replace(serverName, "/", "_");

std::string iconFileName = iconsDir_ + StrToLower(name) + ".ico";
std::string iconFileName = iconsDir_ + StrToLower(serverName) + ".ico";

if (!IuCoreUtils::FileExists(iconFileName) && ued && !ued->PluginName.empty()) {
iconFileName = iconsDir_ + StrToLower(ued->PluginName) + ".ico";
Expand Down
98 changes: 98 additions & 0 deletions Source/Core/Images/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1502,4 +1502,102 @@ CString ImageFormatGUIDToString(GUID guid) {
return "unknown";
}

/*
Gdiplus::Bitmap* GetIconPixelData(HICON hIcon)
{
ICONINFO iconInfo;
GetIconInfo(hIcon, &iconInfo);
BITMAP iconBmp;
GetObject(iconInfo.hbmColor, sizeof(BITMAP), &iconBmp);
Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(iconBmp.bmWidth, iconBmp.bmHeight, PixelFormat32bppARGB);
bool hasAlpha = false;
{
// We have to read the raw pixels of the bitmap to get proper transparency information
// (not sure why, all we're doing is copying one bitmap into another)
Gdiplus::Bitmap colorBitmap(iconInfo.hbmColor, NULL);
Gdiplus::BitmapData bmpData;
Gdiplus::Rect bmBounds(0, 0, colorBitmap.GetWidth(), colorBitmap.GetHeight());
colorBitmap.LockBits(&bmBounds, Gdiplus::ImageLockModeRead, colorBitmap.GetPixelFormat(), &bmpData);
for (int y = 0; y < colorBitmap.GetHeight(); y++) {
byte* pixelBytes = (byte*)bmpData.Scan0 + y * bmpData.Stride;
for (int x = 0; x < colorBitmap.GetHeight(); x++) {
ARGB* pixel = (ARGB*)(pixelBytes + x * 4);
bitmap->SetPixel(x, y, Gdiplus::Color(*pixel));
hasAlpha = hasAlpha || (pixelBytes[3] > 0 && pixelBytes[3] < 255);
}
}
colorBitmap.UnlockBits(&bmpData);
}
if (!hasAlpha) {
// If there's no alpha transparency information, we need to use the mask
// to turn back on visible pixels
Gdiplus::Bitmap maskBitmap(iconInfo.hbmMask, NULL);
Gdiplus::Color cMask, cBitmap;
for (int y = 0; y < maskBitmap.GetHeight(); y++) {
for (int x = 0; x < maskBitmap.GetWidth(); x++) {
maskBitmap.GetPixel(x, y, &cBitmap);
cBitmap.SetValue(cBitmap.GetValue() | 0xFF000000); // turn alpha to opaque (i.e. 0xFF)
bitmap->SetPixel(x, y, cBitmap);
}
}
}
return bitmap;
}*/

std::pair<std::unique_ptr<Gdiplus::Bitmap>,std::unique_ptr<BYTE[]>> GetIconPixelData(HICON hIcon) {
if (hIcon == NULL){
return {};
}
ICONINFO icInfo = { 0 };

if (!::GetIconInfo(hIcon, &icInfo)) {
return {};
}

BITMAP bitmap;
GetObject(icInfo.hbmColor, sizeof(BITMAP), &bitmap);
std::unique_ptr<Bitmap> pBitmap;
std::unique_ptr<Bitmap> pWrapBitmap;
std::unique_ptr<BYTE[]> data;
if (bitmap.bmBitsPixel != 32) {
pBitmap.reset(Bitmap::FromHICON(hIcon));
} else {
pWrapBitmap.reset(Bitmap::FromHBITMAP(icInfo.hbmColor, NULL));
BitmapData bitmapData;
Rect rcImage(0, 0, pWrapBitmap->GetWidth(), pWrapBitmap->GetHeight());

if (pWrapBitmap->LockBits(&rcImage, ImageLockModeRead, pWrapBitmap->GetPixelFormat(), &bitmapData) == Ok) {
BYTE* srcData {};
size_t resultOffset = 0;
size_t size = std::abs(bitmapData.Stride) * bitmapData.Height;
if (bitmapData.Stride < 0) {
srcData = (BYTE*)bitmapData.Scan0 + bitmapData.Stride * (bitmapData.Height-1);
resultOffset = -bitmapData.Stride * (bitmapData.Height-1);
} else {
srcData = (BYTE*)bitmapData.Scan0;
}

data = std::make_unique<BYTE[]>(size);
memcpy(data.get(), srcData, size);
pBitmap = std::make_unique<Bitmap>(bitmapData.Width, bitmapData.Height, bitmapData.Stride,
PixelFormat32bppARGB, data.get() + resultOffset);
pWrapBitmap->UnlockBits(&bitmapData);
}
}

DeleteObject(icInfo.hbmColor);
DeleteObject(icInfo.hbmMask);

return std::make_pair(std::move(pBitmap), std::move(data));
}

}
29 changes: 14 additions & 15 deletions Source/Core/Images/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

namespace ImageUtils {

enum SaveImageFormat
{
enum SaveImageFormat {
sifJPEG,
sifPNG,
sifGIF,
Expand All @@ -19,11 +18,11 @@ enum SaveImageFormat
sifDetectByExtension,
};

struct ImageInfo
{
struct ImageInfo {
int width, height;

ImageInfo() {
ImageInfo()
{
width = 0;
height = 0;
}
Expand All @@ -43,32 +42,31 @@ CComPtr<IStream> CreateMemStream(const BYTE* pInit, UINT cbInit);
bool CopyBitmapToClipboard(HWND hwnd, HDC dc, Gdiplus::Bitmap* bm, bool preserveAlpha = true);
void Gdip_RemoveAlpha(Gdiplus::Bitmap& source, Gdiplus::Color color);


/**
* @throws IOException, runtime_error
*/
bool MySaveImage(Gdiplus::Bitmap* img, const CString& szFilename, CString& szBuffer, SaveImageFormat Format,
int Quality,
LPCTSTR Folder = 0);
int Quality,
LPCTSTR Folder = 0);

/**
* @throws IOException, runtime_error
*/
bool SaveImageToFile(Gdiplus::Bitmap* img, const CString& fileName, IStream* stream, SaveImageFormat Format,
int Quality, CString* mimeType = nullptr);
int Quality, CString* mimeType = nullptr);
SaveImageFormat GetFormatByFileName(CString filename);
void DrawGradient(Gdiplus::Graphics& gr, Gdiplus::Rect rect, Gdiplus::Color& Color1, Gdiplus::Color& Color2);
void DrawStrokedText(Gdiplus::Graphics& gr, LPCTSTR Text, Gdiplus::RectF Bounds, const Gdiplus::Font& font,
const Gdiplus::Color& ColorText, const Gdiplus::Color& ColorStroke, int HorPos = 0,
int VertPos = 0,
int width = 1);
const Gdiplus::Color& ColorText, const Gdiplus::Color& ColorStroke, int HorPos = 0,
int VertPos = 0,
int width = 1);
void ChangeAlphaChannel(Gdiplus::Bitmap& source, Gdiplus::Bitmap& dest, int sourceChannel, int destChannel);
Gdiplus::Rect MeasureDisplayString(Gdiplus::Graphics& graphics, CString text, Gdiplus::RectF boundingRect,
Gdiplus::Font& font);
Gdiplus::Font& font);
CRect CenterRect(CRect r1, const CRect& intoR2);
std::unique_ptr<Gdiplus::Bitmap> GetThumbnail(Gdiplus::Image* bm, int width, int height, Gdiplus::Size* realSize = 0);
std::unique_ptr<Gdiplus::Bitmap> GetThumbnail(const CString& filename, int width, int height,
Gdiplus::Size* realSize = 0, CString* imageFormat = nullptr);
Gdiplus::Size* realSize = 0, CString* imageFormat = nullptr);
Gdiplus::Size AdaptProportionalSize(const Gdiplus::Size& szMax, const Gdiplus::Size& szReal);
Gdiplus::Size ProportionalSize(const Gdiplus::Size& originalSize, const Gdiplus::Size& newSize);
std::unique_ptr<Gdiplus::Bitmap> BitmapFromMemory(BYTE* data, size_t size);
Expand Down Expand Up @@ -97,6 +95,7 @@ bool SaveBitmapAsWebp(Gdiplus::Bitmap* img, CString fileName, IStream* stream, b

std::unique_ptr<Gdiplus::Font> StringToGdiplusFont(LPCTSTR szBuffer);
CString ImageFormatGUIDToString(GUID guid);
}

std::pair<std::unique_ptr<Gdiplus::Bitmap>, std::unique_ptr<BYTE[]>> GetIconPixelData(HICON hIcon);
}
#endif
5 changes: 0 additions & 5 deletions Source/Core/QtServerIconCache.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "QtServerIconCache.h"

#include <thread>

#include <QFile>

#include "Core/Upload/UploadEngine.h"
Expand Down Expand Up @@ -44,9 +42,6 @@ QtServerIconCache::CacheItem QtServerIconCache::tryIconLoad(const std::string &n
return iconIt->second;
}

CUploadEngineData* ued = engineList_->byName(name);

HICON icon = nullptr;
QString iconFileName = QString::fromStdString(getIconNameForServer(name, true));

if (!QFile::exists(iconFileName)) {
Expand Down
12 changes: 8 additions & 4 deletions Source/Gui/Dialogs/FloatingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,9 @@ void CFloatingWindow::ShowImageUploadedMessage(UploadTask* task, const CString&
obj->fillFromUploadResult(task->uploadResult(), task);

CString code;
CString message;
if (settings->TrayResult == WtlGuiSettings::trJustURL) {
message = TR("(the link has been copied to the clipboard)");
code = url;
} else if (settings->TrayResult == WtlGuiSettings::trLastCodeType) {
GeneratorID generatorId = static_cast<GeneratorID>(settings->CodeLang);
Expand All @@ -934,13 +936,15 @@ void CFloatingWindow::ShowImageUploadedMessage(UploadTask* task, const CString&
}

code = U2W(generator->generate(objects, settings->UseTxtTemplate));
message = TR("(the code has been copied to the clipboard)");
}
WinUtils::CopyTextToClipboard(code);
CString trimmedUrl = WinUtils::TrimString(code, 70);
ShowBaloonTip(trimmedUrl + CString("\r\n")
+ TR("(the link has been copied to the clipboard)")+ CString("\r\n") + TR("Click on this message to view details...") ,
CString trimmedCode = WinUtils::TrimString(code, 70);

ShowBaloonTip(trimmedCode + CString("\r\n")
+ message + CString("\r\n") + TR("Click on this message to view details..."),
TR("Screenshot was uploaded"), 17000, [this] {showLastUploadedCode(); });
CString statusText = TR("Screenshot was uploaded") + CString(_T("\r\n")) + trimmedUrl;
CString statusText = TR("Screenshot was uploaded") + CString(_T("\r\n")) + trimmedCode;
setStatusText(statusText, kStatusHideTimeout);
lastUploadedItem_ = std::move(obj);
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Gui/Dialogs/LoginDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ LRESULT CLoginDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& b
int iconWidth = ::GetSystemMetrics(SM_CXICON);
int iconHeight = ::GetSystemMetrics(SM_CYICON);
serverImage_.SetWindowPos(0, 0, 0, iconWidth, iconHeight, SWP_NOMOVE | SWP_NOZORDER);
serverBitmap_.reset(Gdiplus::Bitmap::FromHICON(serverIcon_));
if (serverBitmap_ && serverBitmap_->GetLastStatus() == Gdiplus::Ok) {
serverImage_.loadImage(0, serverBitmap_.get(), 0, false, GetSysColor(COLOR_BTNFACE), false, true, false);
auto [serverBitmap, data] = ImageUtils::GetIconPixelData(serverIcon_);
if (serverBitmap && serverBitmap->GetLastStatus() == Gdiplus::Ok) {
serverImage_.loadImage(0, serverBitmap.get(), 0, false, GetSysColor(COLOR_BTNFACE), false, true, false);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Source/Gui/Dialogs/LoginDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class CLoginDlg : public CCustomDialogIndirectImpl<CLoginDlg>, public CWinDataEx
CButton loginButton_, logoutButton_;
CMyImage serverImage_;
CIcon serverIcon_;
std::unique_ptr<Gdiplus::Bitmap> serverBitmap_;
bool createNew_;
bool ignoreExistingAccount_;
bool serverSupportsBeforehandAuthorization_, serverSupportsLogout_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void ServerSelectorWidget::updateServerList() {
continue;
}
if (addedItems) {
serverListComboBox->addItem(line);
serverListComboBox->insertSeparator(addedItems);
}
for (int i = 0; i < myEngineList->count(); i++) {
CUploadEngineData* ue = myEngineList->byIndex(i);
Expand Down

0 comments on commit d739c88

Please sign in to comment.