Skip to content

Commit dce0d94

Browse files
committed
TreeView: Fix rendering and add callback
1 parent 527ff09 commit dce0d94

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

contrib/vcpkg

Submodule vcpkg updated 1672 files

samples/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ int main(int argc, char *argv[]) {
105105

106106
Widgets::inputText(9, RootPanelId, Rect(100, 350, 100, 40), Alignment::Left);
107107

108-
Widgets::treeView(10, RootPanelId, "tree", Rect(100, 400, 100, 40), nullptr);
108+
Widgets::treeView(10, RootPanelId, "tree", Rect(100, 400, 100, 40));
109109
Widgets::treeItem(11, 10, "Item 1");
110110
Widgets::treeItem(12, 10, "Item 2");
111+
Widgets::setEnableState(12, false);
111112

112113
while (TinyUi::run()) {
113114
TinyUi::render();

src/widgets.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,12 @@ ret_code Widgets::panel(Id id, Id parentId, const char *title, const Rect &rect,
346346
return ResultOk;
347347
}
348348

349-
ret_code Widgets::treeView(Id id, Id parentId, const char *title, const Rect &rect, CallbackI *callback) {
349+
static int onTreeViewItemClicked(uint32_t id, void *data) {
350+
std::cout << "TreeView item clicked: " << id << std::endl;
351+
return 0;
352+
}
353+
354+
ret_code Widgets::treeView(Id id, Id parentId, const char *title, const Rect &rect) {
350355
auto &ctx = TinyUi::getContext();
351356
if (ctx.mSDLContext.mRenderer == nullptr) {
352357
return InvalidRenderHandle;
@@ -361,9 +366,13 @@ ret_code Widgets::treeView(Id id, Id parentId, const char *title, const Rect &re
361366
return ErrorCode;
362367
}
363368

369+
364370
if (title != nullptr) {
365371
widget->mText.assign(title);
366-
}
372+
}
373+
374+
CallbackI *callback = new CallbackI(onTreeViewItemClicked, nullptr, Events::MouseButtonDownEvent);
375+
widget->mCallback = callback;
367376

368377
return ResultOk;
369378
}
@@ -386,12 +395,7 @@ ret_code Widgets::treeItem(Id id, Id parentItemId, const char *text) {
386395
auto &parentRect = parentWidget->mRect;
387396

388397
const int32_t margin = ctx.mStyle.mMargin;
389-
int32_t w = parentRect.width;
390-
if (text != nullptr) {
391-
const size_t numGlyphs = strlen(text);
392-
w = static_cast<int32_t>(numGlyphs) * static_cast<int32_t>(ctx.mSDLContext.mDefaultFont->mSize);
393-
}
394-
398+
const int32_t w = parentRect.width;
395399
const int32_t h = parentRect.height;
396400
size_t numChildren = parentWidget->mChildren.size() + 1;
397401
const Rect rect(parentRect.top.x + margin, parentRect.top.y + numChildren * margin + numChildren * h, w, h);

src/widgets.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ SOFTWARE.
2727

2828
namespace tinyui {
2929

30-
// Forward declarations
30+
// Forward declarations -------------------------------------------------------
3131
struct Context;
3232

3333
/// @brief This enum is used to descripe the widget type-
@@ -230,7 +230,7 @@ struct Widgets {
230230
/// @param rect The rect of the widget.
231231
/// @param callback The callback of the widget.
232232
/// @return ResultOk if the widget was created, ErrorCode if not.
233-
static ret_code treeView(Id id, Id parentId, const char *title, const Rect &rect, CallbackI *callback);
233+
static ret_code treeView(Id id, Id parentId, const char *title, const Rect &rect);
234234

235235
/// @brief Creates a new tree item.
236236
/// @param id The unique id of the tree item.

0 commit comments

Comments
 (0)