Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions samples/demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ int updateProgressbar(Id, void *instance) {
}

auto *widget = (Widget*) instance;
auto *eventPayload = (EventPayload*) widget->mContent;
if (eventPayload->type == EventDataType::FillState) {
if (auto *eventPayload = (EventPayload *)widget->mContent; eventPayload->type == EventDataType::FillState) {
FilledState *state = (FilledState*) (eventPayload->payload);
uint32_t tick = TinyUi::getTicks();
uint32_t diff = tick - LastTick;
Expand All @@ -77,13 +76,12 @@ int updateProgressbar(Id, void *instance) {
}

int main(int argc, char *argv[]) {
Style style = TinyUi::getDefaultStyle();
if (!TinyUi::createContext("Sample-Screen", style)) {
if (Style style = TinyUi::getDefaultStyle(); !TinyUi::createContext("Sample-Screen", style)) {
return -1;
}

if (TinyUi::initScreen(20, 20, 1024, 768) == -1) {
auto &ctx = TinyUi::getContext();
const auto &ctx = TinyUi::getContext();
ctx.mLogger(LogSeverity::Error, "Cannot init screen");
return ErrorCode;
}
Expand Down
9 changes: 3 additions & 6 deletions samples/hello_world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

static constexpr Id RootPanelId = 1;

static constexpr Id NextPanelId = 100;

int quit(Id, void *instance) {
if (instance == nullptr) {
return ErrorCode;
Expand All @@ -41,21 +39,20 @@
}

int main(int argc, char *argv[]) {
Style style = TinyUi::getDefaultStyle();
if (!TinyUi::createContext("Sample-Screen", style)) {
if (Style style = TinyUi::getDefaultStyle(); !TinyUi::createContext("Sample-Screen", style)) {
return -1;
}

if (TinyUi::initScreen(20, 20, 1024, 768) == -1) {
auto &ctx = TinyUi::getContext();
const auto &ctx = TinyUi::getContext();
ctx.mLogger(LogSeverity::Error, "Cannot init screen");
return ErrorCode;
}

constexpr int32_t ButtonHeight = 20;
Widgets::panel(RootPanelId, 0, "Sample-Dialog", Rect(90, 5, 220, 60), nullptr);
auto &ctx = TinyUi::getContext();
CallbackI *dynamicQuitCallback = new CallbackI(quit, (void*) &ctx);
auto *dynamicQuitCallback = new CallbackI(quit, (void*) &ctx);

Check failure on line 55 in samples/hello_world/main.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace the use of "new" with an operation that automatically manages the memory.

See more on https://sonarcloud.io/project/issues?id=kimkulling_tiny_ui&issues=AZ17Z9fjTAyY_RG9QMDY&open=AZ17Z9fjTAyY_RG9QMDY&pullRequest=29

Widgets::label(2, RootPanelId, "Hi, World!", Rect(100, 10, 200, ButtonHeight), Alignment::Center);
Widgets::button(3, RootPanelId, "Quit", Rect(100, 30, 200, ButtonHeight), dynamicQuitCallback);
Expand Down
10 changes: 5 additions & 5 deletions src/backends/sdl2_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@
return ResultOk;
}

SDLContext *sdlCtx = getBackendContext(ctx);
if (sdlCtx->mRenderer != nullptr) {
if (SDLContext *sdlCtx = getBackendContext(ctx); sdlCtx->mRenderer != nullptr) {
SDL_DestroyRenderer(sdlCtx->mRenderer);
sdlCtx->mRenderer = nullptr;
}
Expand Down Expand Up @@ -276,6 +275,7 @@
SDLContext *sdlCtx = SDLContext::create();
if (sdlCtx->mWindow != nullptr) {
ctx.mLogger(LogSeverity::Error, "Already created.");
sdlCtx->destroy();
return ErrorCode;
}

Expand Down Expand Up @@ -448,7 +448,7 @@
return ResultOk;
}

bool Renderer::update(Context &ctx) {
bool Renderer::update(const Context &ctx) {
if (!ctx.mCreated) {
return false;
}
Expand Down Expand Up @@ -513,7 +513,7 @@
std::cerr << "*ERR*: %s\n" << errorMsg << "\n";
return nullptr;
}
SurfaceImpl *surfaceImpl = new SurfaceImpl;
auto *surfaceImpl = new SurfaceImpl;
surfaceImpl->mSurface = surface;

return surfaceImpl;
Expand All @@ -526,8 +526,8 @@
}
}

ret_code Renderer::getSurfaceInfo(Context &ctx, int32_t &w, int32_t &h) {

Check warning on line 529 in src/backends/sdl2_renderer.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make the type of this parameter a reference-to-const. The current type of "ctx" is "struct tinyui::Context &".

See more on https://sonarcloud.io/project/issues?id=kimkulling_tiny_ui&issues=AZ17Z9aiTAyY_RG9QMDW&open=AZ17Z9aiTAyY_RG9QMDW&pullRequest=29
SDLContext *sdlCtx = (SDLContext *) ctx.mBackendCtx->mHandle;
const auto *sdlCtx = (const SDLContext *) ctx.mBackendCtx->mHandle;
if (sdlCtx->mSurface == nullptr) {
return ErrorCode;
}
Expand Down
2 changes: 1 addition & 1 deletion src/backends/sdl2_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct Renderer {
static ret_code endRender(Context &ctx);
static ret_code createRenderTexture(Context &ctx, int w, int h, SDL_Texture **texture);
static ret_code closeScreen(Context &ctx);
static bool update(Context &ctx);
static bool update(const Context &ctx);
static SurfaceImpl *createSurfaceImpl(unsigned char *data, int w, int h, int bytesPerPixel, int pitch);
static void releaseSurfaceImpl(SurfaceImpl *surfaceImpl);
static ret_code getSurfaceInfo(Context &ctx, int32_t &w, int32_t &h);
Expand Down
6 changes: 3 additions & 3 deletions src/tinyui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void log_message(LogSeverity severity, const char *message) {
Context *gCtx = nullptr;

Context *Context::create(const char *title, const Style &style) {
Context *ctx = new Context;
auto *ctx = new Context;
ctx->mLogger = log_message;
ctx->mAppTitle = title;
ctx->mWindowsTitle = title;
Expand Down Expand Up @@ -130,7 +130,7 @@ ret_code TinyUi::getSurfaceCenter(int32_t &x, int32_t &y) {
}

bool TinyUi::run() {
auto &ctx = getContext();
const auto &ctx = getContext();
if (!ctx.mUpdateCallbackList.empty()) {
for (auto it = ctx.mUpdateCallbackList.begin(); it != ctx.mUpdateCallbackList.end(); ++it) {
(*it)->mfuncCallback[Events::UpdateEvent](1, (*it)->mInstance);
Expand All @@ -150,7 +150,7 @@ ret_code TinyUi::endRender() {
}

void TinyUi::render() {
auto &ctx = getContext();
const auto &ctx = getContext();
beginRender(ctx.mStyle.mClearColor);
Widgets::renderWidgets();
endRender();
Expand Down
28 changes: 24 additions & 4 deletions src/tinyui.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@
# define _CRT_SECURE_NO_WARNINGS
#endif

#if defined(_WIN32) || defined(_WIN64)
# define TINYUI_WINDOWS
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN // Minimal windows header
# endif // WIN32_LEAN_AND_MEAN
#elif defined(__gnu_linux__)
# define TINYUI_GNU_LINUX
#elif defined(__APPLE__) || defined(__MACH__)
# error "Currently not supported platform"
#elif defined(__ANDROID__)
# define TINYUIE_ANDROID
#endif

#ifdef TINYUI_WINDOWS
# include <windows.h>

Check warning on line 77 in src/tinyui.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

non-portable path to file '<Windows.h>'; specified path differs in case from file name on disk

See more on https://sonarcloud.io/project/issues?id=kimkulling_tiny_ui&issues=AZ14c3dLHM_hxeUmicHw&open=AZ14c3dLHM_hxeUmicHw&pullRequest=29
# include <Commdlg.h>

Check warning on line 78 in src/tinyui.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

non-portable path to file '<commdlg.h>'; specified path differs in case from file name on disk

See more on https://sonarcloud.io/project/issues?id=kimkulling_tiny_ui&issues=AZ14c3dLHM_hxeUmicHx&open=AZ14c3dLHM_hxeUmicHx&pullRequest=29
#endif

// Forward declarations -------------------------------------------------------

namespace tinyui {
Expand All @@ -78,6 +96,10 @@
/// @brief The return code type used in the ui library.
using ret_code = int32_t;

/// @brief The operation was cancelled.
static constexpr ret_code OpCancelled = -5;
/// @brief The context is invalid.
static constexpr ret_code InvalidContext = -4;
/// @brief The invalid render handle return code.
static constexpr ret_code InvalidRenderHandle = -3;
/// @brief The invalid handle return code.
Expand Down Expand Up @@ -220,14 +242,12 @@
top.y = r.top.y;
}

const int x2_ = top.x + r.width;
if (bottom.x < x2_) {
if (const int x2_ = top.x + r.width; bottom.x < x2_) {
bottom.x = x2_;
width = r.width;
}

const int y2_ = bottom.y + r.height;
if (bottom.y < y2_) {
if (const int y2_ = bottom.y + r.height; bottom.y < y2_) {
bottom.y = y2_;
height = r.height;
}
Expand Down
104 changes: 96 additions & 8 deletions src/widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
return ErrorCode;
}

Context *ctx = static_cast<Context *>(instance);
auto *ctx = static_cast<Context *>(instance);
ctx->mFocus = Widgets::findWidget(id, ctx->mRoot);

return ResultOk;
Expand Down Expand Up @@ -399,8 +399,7 @@
return InvalidRenderHandle;
}

Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Panel);
if (child == nullptr) {
if (const Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Panel); child == nullptr) {
return ErrorCode;
}

Expand All @@ -412,14 +411,17 @@
if (treeView == nullptr) {
return ErrorCode;
}

for (size_t i = 0; i < treeView->mChildren.size(); ++i ) {
Widget *child = treeView->mChildren[i];
if (child == nullptr) {
continue;
}
child->mEnabled = !child->mEnabled;
}

std::cout << "TreeView item clicked: " << id << std::endl;

return 0;
}

Expand All @@ -432,7 +434,7 @@
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}

Widget *widget = createWidget(ctx, id, parentId, rect, WidgetType::TreeView);
if (widget == nullptr) {
return ErrorCode;
Expand Down Expand Up @@ -461,12 +463,12 @@
return InvalidRenderHandle;
}

Widget *parentWidget = findWidget(parentItemId, ctx.mRoot);
const Widget *parentWidget = findWidget(parentItemId, ctx.mRoot);
if (parentWidget == nullptr) {
return ErrorCode;
}

auto &parentRect = parentWidget->mRect;
const auto &parentRect = parentWidget->mRect;

const int32_t margin = ctx.mStyle.mMargin;
const int32_t w = parentRect.width;
Expand Down Expand Up @@ -579,7 +581,7 @@
if (payload == nullptr) {
break;
}
FilledState *state = reinterpret_cast<FilledState *>(payload->payload);
const auto *state = reinterpret_cast<FilledState *>(payload->payload);

Check warning on line 584 in src/widgets.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace "reinterpret_cast" with a safer operation.

See more on https://sonarcloud.io/project/issues?id=kimkulling_tiny_ui&issues=AZ17Z9eDTAyY_RG9QMDX&open=AZ17Z9eDTAyY_RG9QMDX&pullRequest=29
if (state == nullptr) {
break;
}
Expand Down Expand Up @@ -771,7 +773,7 @@

bool Widgets::isEnabled(Id id) {
auto &ctx = TinyUi::getContext();
Widget *widget = findWidget(id, ctx.mRoot);
const Widget *widget = findWidget(id, ctx.mRoot);
if (widget != nullptr) {
return widget->isEnabled();
}
Expand Down Expand Up @@ -806,4 +808,90 @@
return true;
}

static constexpr size_t BufferSize = 1024;

ret_code Widgets::getOpenFileDialog(const char *title, const char *extensions, std::string &filename) {
filename.clear();
#ifdef TINYUI_WINDOWS
// Init data
char szFile[BufferSize] = { '\0' };

Check warning on line 817 in src/widgets.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "std::string" instead of a C-style char array.

See more on https://sonarcloud.io/project/issues?id=kimkulling_tiny_ui&issues=AZ14c3aYHM_hxeUmicHu&open=AZ14c3aYHM_hxeUmicHu&pullRequest=29
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFile;

// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrTitle = title;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = extensions;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = nullptr;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = nullptr;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box.
if (::GetOpenFileName(&ofn) == TRUE) {
filename = ofn.lpstrFile;
} else {
return OpCancelled;
}
#else
char buffer[BufferSize] = { '\0' };
FILE *f = popen("zenity --file-selection", "r");
if (f == nullptr) {
return OpCancelled;
}
fgets(buffer, BufferSize, f);
filename = buffer;
#endif // TINYUI_WINDOWS

return ResultOk;
}

ret_code Widgets::getSaveFileDialog(const char *title, const char *extensions, std::string &filename) {
filename.clear();

#ifdef TINYUI_WINDOWS
char szFile[BufferSize] = { '\0' };

Check warning on line 858 in src/widgets.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "std::string" instead of a C-style char array.

See more on https://sonarcloud.io/project/issues?id=kimkulling_tiny_ui&issues=AZ14c3aYHM_hxeUmicHv&open=AZ14c3aYHM_hxeUmicHv&pullRequest=29
// Initialize OPENFILENAME
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFile;

// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrTitle = title;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = extensions;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = nullptr;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = nullptr;
ofn.Flags = OFN_PATHMUSTEXIST;

// Display the Open dialog box.
if (TRUE == GetSaveFileName(&ofn)) {
filename = ofn.lpstrFile;
} else {
return OpCancelled;
}
#else
FILE *f = popen("zenity --file-selection", "w");
if (f == nullptr) {
return OpCancelled;
}
char buffer[BufferSize] = { '\0' };
fgets(buffer, BufferSize, f);

filename = buffer;
#endif // TINYUI_WINDOWS

return ResultOk;
}

} // namespace tinyui
14 changes: 14 additions & 0 deletions src/widgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ struct Widgets {
/// @brief Will close a child window.
/// @return true if successful.
static bool endChild();

/// @brief Will open a open-file-dialog.
/// @param title The title of the dialog.
/// @param extensions The allowed file extensions (e.g. "txt;pdf;docx").
/// @param filename The selected filename.
/// @return true if a file was selected, false if the dialog was canceled.
static ret_code getOpenFileDialog(const char *title, const char *extensions, std::string &filename);

/// @brief Will open a save-file-dialog.
/// @param title The title of the dialog.
/// @param extensions The allowed file extensions (e.g. "txt;pdf;docx").
/// @param filename The selected filename.
/// @return true if a file was selected, false if the dialog was canceled.
static ret_code getSaveFileDialog(const char *title, const char *extensions, std::string &filename);
};

} // namespace tinyui
Loading