Skip to content

Commit cc0d4e3

Browse files
committed
Misc: Added an explicit compile-time test for non-scoped IM_ASSERT() macros to redirect users to a solution + fixed our stb wrappers.
+ Nav: Use nav layer enum, comments.
1 parent b7e1b13 commit cc0d4e3

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

docs/CHANGELOG.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Other Changes:
6969
- Window: Fixed a bug with child window inheriting ItemFlags from their parent when the child
7070
window also manipulate the ItemFlags stack. (#3024) [@Stanbroek]
7171
- Font: Fixed non-ASCII space occasionally creating unnecessary empty polygons.
72+
- Misc: Added an explicit compile-time test for non-scoped IM_ASSERT() macros to redirect users
73+
to a solution rather than encourage people to add braces in the codebase.
7274
- Misc: Added additional checks in EndFrame() to verify that io.KeyXXX values have not been
7375
tampered with between NewFrame() and EndFrame().
7476
- Misc, Freetype: Fixed support for IMGUI_STB_RECT_PACK_FILENAME compile time directive

examples/imgui_impl_win32.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ IMGUI_IMPL_API void ImGui_ImplWin32_NewFrame();
1717
//#define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
1818
//#define IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT
1919

20-
// Win32 message handler
21-
// - Intentionally commented out in a '#if 0' block to avoid dragging dependencies on <windows.h>
22-
// - You can COPY this line into your .cpp code to forward declare the function.
20+
// Win32 message handler your application need to call.
21+
// - Intentionally commented out in a '#if 0' block to avoid dragging dependencies on <windows.h> from this helper.
22+
// - You should COPY the line below into your .cpp code to forward declare the function and then you can call it.
2323
#if 0
2424
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
2525
#endif

imgui.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6718,6 +6718,14 @@ static void ImGui::ErrorCheckNewFrameSanityChecks()
67186718
{
67196719
ImGuiContext& g = *GImGui;
67206720

6721+
// Check user IM_ASSERT macro
6722+
// (IF YOU GET A WARNING OR COMPILE ERROR HERE: it means you assert macro is incorrectly defined!
6723+
// If your macro uses multiple statements, it NEEDS to be surrounded by a 'do { ... } while (0)' block.
6724+
// This is a common C/C++ idiom to allow multiple statements macros to be used in control flow blocks.)
6725+
// #define IM_ASSERT(EXPR) SomeCode(EXPR); SomeMoreCode(); // Wrong!
6726+
// #define IM_ASSERT(EXPR) do { SomeCode(EXPR); SomeMoreCode(); } while (0) // Correct!
6727+
if (true) IM_ASSERT(1); else IM_ASSERT(0);
6728+
67216729
// Check user data
67226730
// (We pass an error message in the assert expression to make it visible to programmers who are not using a debugger, as most assert handlers display their argument)
67236731
IM_ASSERT(g.Initialized);
@@ -7631,7 +7639,7 @@ void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_
76317639
}
76327640
else
76337641
{
7634-
if (g.NavLayer == 0 && focus_window)
7642+
if (g.NavLayer == ImGuiNavLayer_Main && focus_window)
76357643
focus_window = NavRestoreLastChildNavWindow(focus_window);
76367644
FocusWindow(focus_window);
76377645
}
@@ -8110,7 +8118,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand)
81108118
// 2017/09/29: FIXME: This now currently only enabled inside menu bars, ideally we'd disable it everywhere. Menus in particular need to catch failure. For general navigation it feels awkward.
81118119
// Disabling it may lead to disconnected graphs when nodes are very spaced out on different axis. Perhaps consider offering this as an option?
81128120
if (result->DistBox == FLT_MAX && dist_axial < result->DistAxial) // Check axial match
8113-
if (g.NavLayer == 1 && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu))
8121+
if (g.NavLayer == ImGuiNavLayer_Menu && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu))
81148122
if ((g.NavMoveDir == ImGuiDir_Left && dax < 0.0f) || (g.NavMoveDir == ImGuiDir_Right && dax > 0.0f) || (g.NavMoveDir == ImGuiDir_Up && day < 0.0f) || (g.NavMoveDir == ImGuiDir_Down && day > 0.0f))
81158123
{
81168124
result->DistAxial = dist_axial;
@@ -8221,7 +8229,7 @@ void ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const Im
82218229
void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags)
82228230
{
82238231
ImGuiContext& g = *GImGui;
8224-
if (g.NavWindow != window || !NavMoveRequestButNoResultYet() || g.NavMoveRequestForward != ImGuiNavForward_None || g.NavLayer != 0)
8232+
if (g.NavWindow != window || !NavMoveRequestButNoResultYet() || g.NavMoveRequestForward != ImGuiNavForward_None || g.NavLayer != ImGuiNavLayer_Main)
82258233
return;
82268234
IM_ASSERT(move_flags != 0); // No points calling this with no wrapping
82278235
ImRect bb_rel = window->NavRectRel[0];
@@ -8466,7 +8474,7 @@ static void ImGui::NavUpdate()
84668474
// Store our return window (for returning from Layer 1 to Layer 0) and clear it as soon as we step back in our own Layer 0
84678475
if (g.NavWindow)
84688476
NavSaveLastChildNavWindowIntoParent(g.NavWindow);
8469-
if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == 0)
8477+
if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == ImGuiNavLayer_Main)
84708478
g.NavWindow->NavLastChildNavWindow = NULL;
84718479

84728480
// Update CTRL+TAB and Windowing features (hold Square to move/resize/etc.)
@@ -8503,7 +8511,7 @@ static void ImGui::NavUpdate()
85038511
if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal))
85048512
ClosePopupToLevel(g.OpenPopupStack.Size - 1, true);
85058513
}
8506-
else if (g.NavLayer != 0)
8514+
else if (g.NavLayer != ImGuiNavLayer_Main)
85078515
{
85088516
// Leave the "menu" layer
85098517
NavRestoreLayer(ImGuiNavLayer_Main);
@@ -8625,7 +8633,7 @@ static void ImGui::NavUpdate()
86258633
g.NavMoveResultOther.Clear();
86268634

86278635
// When we have manually scrolled (without using navigation) and NavId becomes out of bounds, we project its bounding box to the visible area to restart navigation within visible items
8628-
if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == 0)
8636+
if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == ImGuiNavLayer_Main)
86298637
{
86308638
ImGuiWindow* window = g.NavWindow;
86318639
ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1,1), window->InnerRect.Max - window->Pos + ImVec2(1,1));
@@ -8688,7 +8696,7 @@ static void ImGui::NavUpdateMoveResult()
86888696
IM_ASSERT(g.NavWindow && result->Window);
86898697

86908698
// Scroll to keep newly navigated item fully into view.
8691-
if (g.NavLayer == 0)
8699+
if (g.NavLayer == ImGuiNavLayer_Main)
86928700
{
86938701
ImVec2 delta_scroll;
86948702
if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_ScrollToEdge)
@@ -8727,7 +8735,7 @@ static float ImGui::NavUpdatePageUpPageDown()
87278735
ImGuiContext& g = *GImGui;
87288736
if (g.NavMoveDir != ImGuiDir_None || g.NavWindow == NULL)
87298737
return 0.0f;
8730-
if ((g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != 0)
8738+
if ((g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != ImGuiNavLayer_Main)
87318739
return 0.0f;
87328740

87338741
ImGuiWindow* window = g.NavWindow;

imgui_draw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ namespace IMGUI_STB_NAMESPACE
120120
#ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)
121121
#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
122122
#define STBRP_STATIC
123-
#define STBRP_ASSERT(x) IM_ASSERT(x)
123+
#define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0)
124124
#define STBRP_SORT ImQsort
125125
#define STB_RECT_PACK_IMPLEMENTATION
126126
#endif
@@ -135,7 +135,7 @@ namespace IMGUI_STB_NAMESPACE
135135
#ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
136136
#define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x))
137137
#define STBTT_free(x,u) ((void)(u), IM_FREE(x))
138-
#define STBTT_assert(x) IM_ASSERT(x)
138+
#define STBTT_assert(x) do { IM_ASSERT(x); } while(0)
139139
#define STBTT_fmod(x,y) ImFmod(x,y)
140140
#define STBTT_sqrt(x) ImSqrt(x)
141141
#define STBTT_pow(x,y) ImPow(x,y)

imgui_widgets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6178,7 +6178,7 @@ void ImGui::EndMainMenuBar()
61786178
// When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window
61796179
// FIXME: With this strategy we won't be able to restore a NULL focus.
61806180
ImGuiContext& g = *GImGui;
6181-
if (g.CurrentWindow == g.NavWindow && g.NavLayer == 0 && !g.NavAnyRequest)
6181+
if (g.CurrentWindow == g.NavWindow && g.NavLayer == ImGuiNavLayer_Main && !g.NavAnyRequest)
61826182
FocusTopMostWindowUnderOne(g.NavWindow, NULL);
61836183

61846184
End();

misc/freetype/imgui_freetype.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ namespace
280280

281281
#ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)
282282
#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
283-
#define STBRP_ASSERT(x) IM_ASSERT(x)
283+
#define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0)
284284
#define STBRP_STATIC
285285
#define STB_RECT_PACK_IMPLEMENTATION
286286
#endif

0 commit comments

Comments
 (0)