Skip to content

Commit

Permalink
[client,sdl] add icon to connect dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Novak authored and akallabeth committed Dec 20, 2023
1 parent 35d5842 commit c952fae
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 46 deletions.
43 changes: 25 additions & 18 deletions client/SDL/dialogs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,39 @@ set(SRCS
set(LIBS
${SDL2_LIBRARIES}
font
icon
winpr
)

find_package(SDL2_ttf COMPONENTS)
if (NOT SDL2_ttf_FOUND)
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2_ttf REQUIRED SDL2_ttf)
macro(find_sdl_component name)
find_package(${name})
if (NOT ${name}_FOUND)
find_package(PkgConfig REQUIRED)
pkg_check_modules(${name} REQUIRED ${name})

if (BUILD_SHARED_LIBS)
list(APPEND LIBS ${SDL2_ttf_LIBRARIES})
link_directories(${SDL2_ttf_LIBRARY_DIRS})
include_directories(${SDL2_ttf_INCLUDE_DIRS})
if (BUILD_SHARED_LIBS)
list(APPEND LIBS ${${name}_LIBRARIES})
link_directories(${${name}_LIBRARY_DIRS})
include_directories(${${name}_INCLUDE_DIRS})
else()
list(APPEND LIBS ${${name}_STATIC_LIBRARIES})
link_directories(${${name}_STATIC_LIBRARY_DIRS})
include_directories(${${name}_STATIC_INCLUDE_DIRS})
endif()
else()
list(APPEND LIBS ${SDL2_ttf_STATIC_LIBRARIES})
link_directories(${SDL2_ttf_STATIC_LIBRARY_DIRS})
include_directories(${SDL2_ttf_STATIC_INCLUDE_DIRS})
if (BUILD_SHARED_LIBS)
list(APPEND LIBS ${name}::${name})
else()
list(APPEND LIBS ${name}::${name}-static)
endif()
endif()
else()
if (BUILD_SHARED_LIBS)
list(APPEND LIBS SDL2_ttf::SDL2_ttf)
else()
list(APPEND LIBS SDL2_ttf::SDL2_ttf-static)
endif()
endif()
endmacro()

find_sdl_component(SDL2_ttf)
find_sdl_component(SDL2_image)

add_subdirectory(font)
add_subdirectory(icon)

add_library(dialogs STATIC
${SRCS}
Expand Down
5 changes: 5 additions & 0 deletions client/SDL/dialogs/icon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(icon OBJECT
FreeRDP_Icon.hpp
FreeRDP_Icon.cpp
)
set_property(TARGET font PROPERTY POSITION_INDEPENDENT_CODE ON)
356 changes: 356 additions & 0 deletions client/SDL/dialogs/icon/FreeRDP_Icon.cpp

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions client/SDL/dialogs/icon/FreeRDP_Icon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <vector>
#include <string>

extern const std::vector<unsigned char> FreeRDP_Icon_buffer;
extern const std::string FreeRDP_Icon_buffer_type;
12 changes: 10 additions & 2 deletions client/SDL/dialogs/sdl_connection_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "sdl_connection_dialog.hpp"
#include "../sdl_utils.hpp"
#include "../sdl_freerdp.hpp"
#include "icon/FreeRDP_Icon.hpp"

static const SDL_Color backgroundcolor = { 0x38, 0x36, 0x35, 0xff };
static const SDL_Color textcolor = { 0xd1, 0xcf, 0xcd, 0xff };
Expand Down Expand Up @@ -335,9 +336,16 @@ bool SDLConnectionDialog::createWindow()
return false;
}

SDL_Rect rect = { 0, vpadding, widget_width, total_height - 3 * vpadding - widget_height };
SdlWidget icon = { _renderer,
{ 0, vpadding, widget_width / 4,
total_height - 3 * vpadding - widget_height },
FreeRDP_Icon_buffer,
FreeRDP_Icon_buffer_type };
_list.emplace_back(std::move(icon));
SDL_Rect rect = { widget_width / 4, vpadding, widget_width * 3 / 4,
total_height - 3 * vpadding - widget_height };
auto w = SdlWidget(_renderer, rect, false);
w.set_wrap();
w.set_wrap(true, widget_width);
_list.emplace_back(std::move(w));
rect.y += widget_height + vpadding;

Expand Down
2 changes: 2 additions & 0 deletions client/SDL/dialogs/sdl_connection_dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class SDLConnectionDialog
{
public:
SDLConnectionDialog(rdpContext* context);
SDLConnectionDialog(const SDLConnectionDialog& other) = delete;
SDLConnectionDialog(const SDLConnectionDialog&& other) = delete;
virtual ~SDLConnectionDialog();

bool visible() const;
Expand Down
75 changes: 56 additions & 19 deletions client/SDL/dialogs/sdl_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,53 @@ static const SDL_Color backgroundcolor = { 0x38, 0x36, 0x35, 0xff };
static const Uint32 hpadding = 10;

SdlWidget::SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, bool input)
: _font(nullptr), _rect(rect), _input(input)
: _rect(rect), _input(input)
{
assert(renderer);

_ops = SDL_RWFromConstMem(font_buffer.data(), static_cast<int>(font_buffer.size()));
if (_ops)
_font = TTF_OpenFontRW(_ops, 0, 64);
auto ops = SDL_RWFromConstMem(font_buffer.data(), static_cast<int>(font_buffer.size()));
if (!ops)
widget_log_error(-1, "SDL_RWFromConstMem");
else
{
_font = TTF_OpenFontRW(ops, 1, 64);
widget_log_error(-1, "TTF_OpenFontRW");
}
}

SdlWidget::SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, const std::string& path)
: _rect(rect)
{
_image = IMG_LoadTexture(renderer, path.c_str());
if (!_image)
widget_log_error(-1, "SDL_RWFromConstMem");
}

SdlWidget::SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect,
const std::vector<unsigned char>& image, const std::string& type)
: _rect(rect)
{
auto ops = SDL_RWFromConstMem(image.data(), static_cast<int>(image.size()));
if (!ops)
widget_log_error(-1, "SDL_RWFromConstMem");
else
{
_image = IMG_LoadTextureTyped_RW(renderer, ops, 1, type.c_str());
if (!_image)
widget_log_error(-1, "IMG_LoadTextureTyped_RW");
}
}

SdlWidget::SdlWidget(SdlWidget&& other) noexcept
: _font(std::move(other._font)), _ops(other._ops), _rect(std::move(other._rect)),
_input(other._input), _wrap(other._wrap)
: _font(std::move(other._font)), _rect(std::move(other._rect)), _input(other._input),
_wrap(other._wrap), _text_width(other._text_width), _image(other._image)
{
other._font = nullptr;
other._ops = nullptr;
other._image = nullptr;
}

SDL_Texture* SdlWidget::render(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
SDL_Rect& src, SDL_Rect& dst)
SDL_Texture* SdlWidget::render_text(SDL_Renderer* renderer, const std::string& text,
SDL_Color fgcolor, SDL_Rect& src, SDL_Rect& dst)
{
auto surface = TTF_RenderUTF8_Blended(_font, text.c_str(), fgcolor);
if (!surface)
Expand Down Expand Up @@ -96,13 +124,13 @@ SDL_Texture* SdlWidget::render(SDL_Renderer* renderer, const std::string& text,
return texture;
}

SDL_Texture* SdlWidget::render_wrapped(SDL_Renderer* renderer, const std::string& text,
SDL_Color fgcolor, SDL_Rect& src, SDL_Rect& dst)
SDL_Texture* SdlWidget::render_text_wrapped(SDL_Renderer* renderer, const std::string& text,
SDL_Color fgcolor, SDL_Rect& src, SDL_Rect& dst)
{
Sint32 w = 0;
Sint32 h = 0;
TTF_SizeUTF8(_font, " ", &w, &h);
auto surface = TTF_RenderUTF8_Blended_Wrapped(_font, text.c_str(), fgcolor, 40 * w);
auto surface = TTF_RenderUTF8_Blended_Wrapped(_font, text.c_str(), fgcolor, _text_width);
if (!surface)
{
widget_log_error(-1, "TTF_RenderText_Blended");
Expand Down Expand Up @@ -139,8 +167,7 @@ SDL_Texture* SdlWidget::render_wrapped(SDL_Renderer* renderer, const std::string
SdlWidget::~SdlWidget()
{
TTF_CloseFont(_font);
if (_ops)
SDL_RWclose(_ops);
SDL_DestroyTexture(_image);
}

bool SdlWidget::error_ex(Uint32 res, const char* what, const char* file, size_t line,
Expand Down Expand Up @@ -198,9 +225,10 @@ bool SdlWidget::wrap() const
return _wrap;
}

bool SdlWidget::set_wrap(bool wrap)
bool SdlWidget::set_wrap(bool wrap, size_t width)
{
_wrap = wrap;
_text_width = width;
return _wrap;
}

Expand All @@ -219,15 +247,24 @@ bool SdlWidget::update_text(SDL_Renderer* renderer, const std::string& text, SDL
SDL_Rect dst{};

SDL_Texture* texture = nullptr;
if (_wrap)
texture = render_wrapped(renderer, text, fgcolor, src, dst);
if (_image)
{
texture = _image;
dst = _rect;
auto rc = SDL_QueryTexture(_image, NULL, NULL, &src.w, &src.h);
if (rc < 0)
widget_log_error(rc, "SDL_QueryTexture");
}
else if (_wrap)
texture = render_text_wrapped(renderer, text, fgcolor, src, dst);
else
texture = render(renderer, text, fgcolor, src, dst);
texture = render_text(renderer, text, fgcolor, src, dst);
if (!texture)
return false;

const int rc = SDL_RenderCopy(renderer, texture, &src, &dst);
SDL_DestroyTexture(texture);
if (!_image)
SDL_DestroyTexture(texture);
if (rc < 0)
return !widget_log_error(rc, "SDL_RenderCopy");
return true;
Expand Down
17 changes: 11 additions & 6 deletions client/SDL/dialogs/sdl_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <vector>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>

#if defined(_MSC_VER)
#include <BaseTsd.h>
Expand All @@ -51,6 +52,9 @@ class SdlWidget
{
public:
SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, bool input);
SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, const std::string& path);
SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, const std::vector<unsigned char>& image,
const std::string& type = "SVG");
SdlWidget(SdlWidget&& other) noexcept;
virtual ~SdlWidget();

Expand All @@ -61,7 +65,7 @@ class SdlWidget
SDL_Color bgcolor);

bool wrap() const;
bool set_wrap(bool wrap = true);
bool set_wrap(bool wrap = true, size_t width = 0);
const SDL_Rect& rect() const;

public:
Expand All @@ -72,17 +76,18 @@ class SdlWidget
private:
SdlWidget(const SdlWidget& other) = delete;

SDL_Texture* render(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
SDL_Rect& src, SDL_Rect& dst);
SDL_Texture* render_wrapped(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
SDL_Rect& src, SDL_Rect& dst);
SDL_Texture* render_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
SDL_Rect& src, SDL_Rect& dst);
SDL_Texture* render_text_wrapped(SDL_Renderer* renderer, const std::string& text,
SDL_Color fgcolor, SDL_Rect& src, SDL_Rect& dst);

private:
TTF_Font* _font = nullptr;
SDL_RWops* _ops = nullptr;
SDL_Texture* _image = nullptr;
SDL_Rect _rect;
bool _input = false;
bool _wrap = false;
size_t _text_width = 0;
};

bool clear_window(SDL_Renderer* renderer);
2 changes: 1 addition & 1 deletion client/SDL/sdl_freerdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ static BOOL sdl_pre_connect(freerdp* instance)

sdl->connection_dialog->setTitle("Connecting to '%s'",
freerdp_settings_get_server_name(settings));
sdl->connection_dialog->showInfo("Please wait while the connection is being established");
sdl->connection_dialog->showInfo("The connection is being established\n\nPlease wait...");

if (!sdl_detect_monitors(sdl, &maxWidth, &maxHeight))
return FALSE;
Expand Down

0 comments on commit c952fae

Please sign in to comment.