mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-10 19:03:30 +00:00
Sizing a child area to fit its content? --> group with border #1496
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#1496
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The
BeginChild()function requires anImVec2to specify the size of the child. The problem is, I don't know the size of the content at the point where I have to make theBeginChild()call.I have tried passing in
ImVec2(0,0)andImVec2(-1,-1), but they both cause the child to size to its parent instead of its content.What did work was
ImVec2(0, -ImGui::GetContentRegionAvail().y)until this morning when I pulled the latest changes. Now I get an Assert error on lineimgui.cpp:4523(IM_ASSERT(window_size_x_set_by_api && window_size_y_set_by_api);).What I am looking for is just a boarder around some widgets. I don't want any scrolling inside the border (child area), it should just take whatever size it needs and cause its parent to scroll if necessary. Is there a way to do this?
Thanks,
Matthew
Hello @dnotq,
Interestingly a few people haven't been trying to do the same thing. It's quite overkill to use a child window for that so I'd prefer if we had a better solution. I'll still investigate that changed with this assert.
I haven't thought it very much, but you may try something along the line of:
I agree we could ideally have primitive to make that sort of things easier.
(Also linking #1395.)
Thanks for the suggestion, I will try to use a group and then draw a rectangle (hopefully I can get rounded corners?)
I agree, using a child window just to get a border seemed like overkill to me too, but I did not see any other controls that offered a border around other content.
Just for reference, before I pulled the latest changes with the assert change I mentioned, using the negative value for the y coordinate gave the desired result (at least in the y axis, I would still like the x axis to shirk too):
However, specifying a -y value (other than -1) stopped working after I pulled, and if I specify (0,0) or (-1,-1), I now get this:
The first area always fills the parent and causes a scroll bar, the other child areas are squashed. I did search the issues here on github before posting and found several posts related to this, but nothing seemed to do what I was looking for. Also, I'm not sure what the
ImGuiWindowFlags_AlwaysAutoResizeflag is for since it never seemed to do what I expected (i.e. I'm not sure when I would use it, but it made no difference in this case if I passed it along with the call toBeginChild()).This is for a server of sorts, and when a connection is made each area expands with additional details and other data. I would like the child area to always be fully expanded (never have scroll bars) and only the main parent window should ever scroll.
The functionality I'm after is very similar to the Collapsing Group, however add a border, and lose the title and the ability to close the group.
Matthew
@dnotq did you find a good way? I'm also looking for some sort of auto-sized panels that enclose their contents in a box growing/shrinking dynamically; ideally with collapsible header too.
I'm trying the suggested aproach Begin/EndGroup() + AddRect(); and with some extra padding around the returned rectangle I can get a nice area; but I can't really add a background color to the panel; I can only draw on top of all the existing gui? Is there a way to reverse drawing order?
Maybe there's a way to "run" the layout part of what's inside the Begin/EndGroup() without actually drawing, just to get the final height beforehand? The problem is a lot of these panels offer different gui sliders / options depending on other panels / values in or out of the panel, so its quite hard to nail down the height without actually running all the imgui code.
Edit: Full GUI shot to get a sense of what I'm trying to do here; visually nesting these "panels" would be super useful.
I don't remember what I ended up doing, it has been a while since I messed with that code. I think the group might have done the trick, but I'm not sure (I don't have access to that code right now). I seem to recall realizing that I did not need a full-blown child window. Sorry I can't me of more help.
Matthew
i just discovering this now, with a similar need to @armadillu. i hacked around it by drawing twice, first to get the measurements, then blanking it out with a rectangle, then drawing over it again. not totally ideal but it works for me, so i thought i'd share if anyone found this thread looking to do the same thing 😃
Could you fix this reply with GetItemRectMax()
Well, I spend a fairly large amount of time on a similar issue.
I wanted to bunch up a few widgets into sections, but I wanted the frame background or another background.
None of the solution I have seen here work.
Best I could do is to put a white rectangle on top of the elements.
Using the GetBackgroundDrawList() instead, showed to rectangles at all.
Edit: Version without line trough text is here https://github.com/ocornut/imgui/issues/1496#issuecomment-655048353

My 3 cents:

I end up with something I think looks decent enough and keep other widgets in bay.
Borders is drawn in the middle of the frame and frame is based on
ImGui::GetFrameHeight(). Top has more spacing because of the group name.You do have a background that looks decent!
Thanks a bunch! Will try that ASAP.
The weird thing about this is that the collapsible header already supports all this correctly IMO.
I tried @thedmd solution, it looks fine, but the line is crossing the text (text needs background cover)

Edit: Update to work with ImGui 1.14 WIP (17301)
@gamelaster I solved that:
I get several errors about "ContentsRegionRect" not being available - was this deprecated?
I'm really looking for this effect and would love to use the code provided by @thedmd , any ideas?
edit: looks like it was just a typo, I'm assuming he meant "ContentRegionRect"
edit2: it's mostly working, although it cuts off the text for "InputDouble" fields - https://i.imgur.com/4Gge2jY.png
edit3: fix seems to be SetNextItemWidth - https://i.imgur.com/fRFt4sE.png
I'll leave these edits up in case it helps the next person :)
@PcChip I updated code to work with current ImGui
Hello, I have been trying to get this this working using
Begin/EndGroupand adding Filled Rectangle using DrawList API. Because I am using filled rectangle, the Rect gets drawn on top of the content. Is there a way to change it below the content?Screenshot (Text is below the Rect):
I solved it by separating the draw list channels into two parts and writing the rectangle to channel 0:
After playing around with this API for my use-case, I do not think adding rectangle is the way to go. I need to support two things
Instead of reinventing the wheel here, I want to create a widget based on child windows that autogrows to content instead of autogrowing to parent. So, I wrote a logic that calculates the height based on group and sets the window size using
ImGui::SetWindowSize. By debugging, I can see that the height is correct insideImGui::EndChild; however, the height of the window does not change. Here is the code that I got:Is this not possible to resize the child window while rendering the contents of it? (similar to resizing a normal window).
EDIT: After trying with both windows and groups + draw list API, and for the time being I went with the groups + draw list API for no reason other than it was faster to implemen this:
The only thing that I want to figure out for this is to add scrolling and allow providing a maximum height. One caveat of this approach is that, I cannot nest this widget within itself because the width is always calculated according to the window, not parent widget; however it is something that does not bother me at the moment.
@thedmd : thanks for the solution, it's a perfect fit and begs to be copy/pasted ; however it is not clear to me what license applies to code posted in here since it's not part of a PR (I'm assuming the CLA applies to PRs, but not code posted inside issues).
Could you clarify the licensing for this code ?
Thanks
It is free to use. I put it in public domain.
You can apply any license you wish after making your own copy. If necessary, this can treated also as permission to do exactly that.
Does this solve your problem?
Yes, thank you very much :-)