From c6519e4cf281c2b9dade62c806647930868c4abc Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 23 Aug 2013 13:17:28 +0300 Subject: fix window title and icon in SDL2. --- src/client.cpp | 100 +++++++++++++++++++++-------------------------- src/client.h | 2 + src/utils/sdl2helper.cpp | 10 +++++ src/utils/sdl2helper.h | 8 ++++ src/utils/sdlhelper.cpp | 12 ++++++ src/utils/sdlhelper.h | 9 +++++ 6 files changed, 86 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/client.cpp b/src/client.cpp index 090591ff4..f615ec2d2 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -106,6 +106,7 @@ #include "utils/paths.h" #include "utils/physfstools.h" #include "utils/process.h" +#include "utils/sdlhelper.h" #include "utils/translation/translationmanager.h" @@ -483,14 +484,7 @@ void Client::gameInit() SMALL_VERSION); } -#ifdef USE_SDL2 - // +++ need use SDL_SetWindowTitle -#else - SDL_WM_SetCaption(mCaption.c_str(), nullptr); -#endif - const ResourceManager *const resman = ResourceManager::getInstance(); - if (!resman->setWriteDir(mLocalDataDir)) { logger->error(strprintf("%s couldn't be set as home directory! " @@ -563,44 +557,6 @@ void Client::gameInit() resman->addToSearchPath(mLocalDataDir, false); TranslationManager::loadCurrentLang(); - std::string iconFile = branding.getValue("appIcon", "icons/manaplus"); -#ifdef WIN32 - iconFile.append(".ico"); -#else - iconFile.append(".png"); -#endif - iconFile = resman->getPath(iconFile); - logger->log("Loading icon from file: %s", iconFile.c_str()); - -#ifdef WIN32 - static SDL_SysWMinfo pInfo; - SDL_GetWMInfo(&pInfo); - // Attempt to load icon from .ico file - HICON icon = (HICON) LoadImage(nullptr, iconFile.c_str(), - IMAGE_ICON, 64, 64, LR_LOADFROMFILE); - // If it's failing, we load the default resource file. - if (!icon) - icon = LoadIcon(GetModuleHandle(nullptr), "A"); - - if (icon) - SetClassLong(pInfo.window, GCL_HICON, reinterpret_cast(icon)); -#else - mIcon = IMG_Load(iconFile.c_str()); - if (mIcon) - { -#ifdef USE_SDL2 - SDL_SetSurfaceAlphaMod(mIcon, 255); -#else - SDL_SetAlpha(mIcon, SDL_SRCALPHA, SDL_ALPHA_OPAQUE); -#endif -#ifdef USE_SDL2 - // +++ need use SDL_SetWindowIcon -#else - SDL_WM_SetIcon(mIcon, nullptr); -#endif - } -#endif - #if defined(USE_OPENGL) && !defined(ANDROID) && !defined(__APPLE__) if (!mOptions.safeMode && mOptions.test.empty() && !config.getBoolValue("videodetected")) @@ -633,6 +589,9 @@ void Client::gameInit() applyGrabMode(); applyGamma(); + SDL::SetWindowTitle(mainGraphics->getWindow(), mCaption.c_str()); + setIcon(); + mainGraphics->_beginDraw(); Theme::selectSkin(); @@ -2945,11 +2904,8 @@ void Client::setIsMinimized(const bool n) if (!n && client->mNewMessageFlag) { client->mNewMessageFlag = false; -#ifdef USE_SDL2 - // +++ need use SDL_SetWindowTitle -#else - SDL_WM_SetCaption(client->mCaption.c_str(), nullptr); -#endif + SDL::SetWindowTitle(mainGraphics->getWindow(), + client->mCaption.c_str()); } } @@ -2962,11 +2918,8 @@ void Client::newChatMessage() if (!client->mNewMessageFlag && client->mIsMinimized) { // show * on window caption -#ifdef USE_SDL2 - // +++ need use SDL_SetWindowTitle -#else - SDL_WM_SetCaption(("*" + client->mCaption).c_str(), nullptr); -#endif + SDL::SetWindowTitle(mainGraphics->getWindow(), + ("*" + client->mCaption).c_str()); client->mNewMessageFlag = true; } } @@ -3119,3 +3072,40 @@ Window *Client::openErrorDialog(const std::string &header, SOUND_ERROR, false, modal); } } + +void Client::setIcon() +{ + std::string iconFile = branding.getValue("appIcon", "icons/manaplus"); +#ifdef WIN32 + iconFile.append(".ico"); +#else + iconFile.append(".png"); +#endif + iconFile = ResourceManager::getInstance()->getPath(iconFile); + logger->log("Loading icon from file: %s", iconFile.c_str()); + +#ifdef WIN32 + static SDL_SysWMinfo pInfo; + SDL_GetWMInfo(&pInfo); + // Attempt to load icon from .ico file + HICON icon = (HICON) LoadImage(nullptr, iconFile.c_str(), + IMAGE_ICON, 64, 64, LR_LOADFROMFILE); + // If it's failing, we load the default resource file. + if (!icon) + icon = LoadIcon(GetModuleHandle(nullptr), "A"); + + if (icon) + SetClassLong(pInfo.window, GCL_HICON, reinterpret_cast(icon)); +#else + mIcon = IMG_Load(iconFile.c_str()); + if (mIcon) + { +#ifdef USE_SDL2 + SDL_SetSurfaceAlphaMod(mIcon, SDL_ALPHA_OPAQUE); +#else + SDL_SetAlpha(mIcon, SDL_SRCALPHA, SDL_ALPHA_OPAQUE); +#endif + SDL::SetWindowIcon(mainGraphics->getWindow(), mIcon); + } +#endif +} diff --git a/src/client.h b/src/client.h index 44a71b460..318dbb14b 100644 --- a/src/client.h +++ b/src/client.h @@ -380,6 +380,8 @@ private: void checkConfigVersion(); + void setIcon(); + static Client *mInstance; static void bindTextDomain(const char *const name, const char *const path); diff --git a/src/utils/sdl2helper.cpp b/src/utils/sdl2helper.cpp index 7cf624a2c..1ae8b087f 100644 --- a/src/utils/sdl2helper.cpp +++ b/src/utils/sdl2helper.cpp @@ -37,4 +37,14 @@ bool SDL::getAllVideoModes(StringVect &modeList) return true; } +void SDL::SetWindowTitle(SDL_Window *const window, const char *const title) +{ + SDL_SetWindowTitle(window, title); +} + +void SDL::SetWindowIcon(SDL_Window *const window, SDL_Surface *const icon) +{ + SDL_SetWindowIcon(window, icon); +} + #endif // USE_SDL2 diff --git a/src/utils/sdl2helper.h b/src/utils/sdl2helper.h index 101c965b8..bf3c57f0c 100644 --- a/src/utils/sdl2helper.h +++ b/src/utils/sdl2helper.h @@ -26,9 +26,17 @@ #include "localconsts.h" +struct SDL_Surface; +struct SDL_Window; + namespace SDL { bool getAllVideoModes(StringVect &modeList); + + void SetWindowTitle(SDL_Window *const window, const char *const title); + + void SetWindowIcon(SDL_Window *const window, SDL_Surface *const icon); + } // namespace SDL #endif // USE_SDL2 diff --git a/src/utils/sdlhelper.cpp b/src/utils/sdlhelper.cpp index 064989e93..9d28f6c96 100644 --- a/src/utils/sdlhelper.cpp +++ b/src/utils/sdlhelper.cpp @@ -61,4 +61,16 @@ bool SDL::getAllVideoModes(StringVect &modeList) } } +void SDL::SetWindowTitle(SDL_Surface *const window A_UNUSED, + const char *const title) +{ + SDL_WM_SetCaption(title, nullptr); +} + +void SDL::SetWindowIcon(SDL_Surface *const window A_UNUSED, + SDL_Surface *const icon) +{ + SDL_WM_SetIcon(icon, nullptr); +} + #endif // USE_SDL2 diff --git a/src/utils/sdlhelper.h b/src/utils/sdlhelper.h index f81cd449d..9d122f934 100644 --- a/src/utils/sdlhelper.h +++ b/src/utils/sdlhelper.h @@ -29,9 +29,18 @@ #include "localconsts.h" +struct SDL_Surface; + namespace SDL { bool getAllVideoModes(StringVect &modeList); + + void SetWindowTitle(SDL_Surface *const window, + const char *const title); + + void SetWindowIcon(SDL_Surface *const window A_UNUSED, + SDL_Surface *const icon); + } // namespace SDL #endif // USE_SDL2 -- cgit v1.2.3-60-g2f50