imgui_tables.cpp 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910
  1. // dear imgui, v1.80 WIP
  2. // (tables and columns code)
  3. /*
  4. *
  5. * Index of this file:
  6. *
  7. * // [SECTION] Widgets: BeginTable, EndTable, etc.
  8. * // [SECTION] Widgets: Columns, BeginColumns, EndColumns, etc.
  9. *
  10. */
  11. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  12. #define _CRT_SECURE_NO_WARNINGS
  13. #endif
  14. #include "imgui.h"
  15. #ifndef IMGUI_DISABLE
  16. #ifndef IMGUI_DEFINE_MATH_OPERATORS
  17. #define IMGUI_DEFINE_MATH_OPERATORS
  18. #endif
  19. #include "imgui_internal.h"
  20. #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
  21. #include <stddef.h> // intptr_t
  22. #else
  23. #include <stdint.h> // intptr_t
  24. #endif
  25. // Visual Studio warnings
  26. #ifdef _MSC_VER
  27. #pragma warning (disable: 4127) // condition expression is constant
  28. #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
  29. #if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later
  30. #pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types
  31. #endif
  32. #endif
  33. // Clang/GCC warnings with -Weverything
  34. #if defined(__clang__)
  35. #if __has_warning("-Wunknown-warning-option")
  36. #pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great!
  37. #endif
  38. #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx'
  39. #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
  40. #pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok.
  41. #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
  42. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0
  43. #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double.
  44. #pragma clang diagnostic ignored "-Wenum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_')
  45. #pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated
  46. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
  47. #elif defined(__GNUC__)
  48. #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind
  49. #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead
  50. #endif
  51. //-----------------------------------------------------------------------------
  52. // [SECTION] Widgets: BeginTable, EndTable, etc.
  53. //-----------------------------------------------------------------------------
  54. //-----------------------------------------------------------------------------
  55. // Typical call flow: (root level is public API):
  56. // - BeginTable() user begin into a table
  57. // - BeginChild() - (if ScrollX/ScrollY is set)
  58. // - TableBeginUpdateColumns() - apply resize/order requests, lock columns active state, order
  59. // - TableSetupColumn() user submit columns details (optional)
  60. // - TableAutoHeaders() or TableHeader() user submit a headers row (optional)
  61. // - TableSortSpecsClickColumn() - when clicked: alter sort order and sort direction
  62. // - TableGetSortSpecs() user queries updated sort specs (optional)
  63. // - TableNextRow() / TableNextCell() user begin into the first row, also automatically called by TableAutoHeaders()
  64. // - TableUpdateLayout() - called by the FIRST call to TableNextRow()!
  65. // - TableUpdateDrawChannels() - setup ImDrawList channels
  66. // - TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission
  67. // - TableDrawContextMenu() - draw right-click context menu
  68. // - TableEndCell() - close existing cell if not the first time
  69. // - TableBeginCell() - enter into current cell
  70. // - [...] user emit contents
  71. // - EndTable() user ends the table
  72. // - TableDrawBorders() - draw outer borders, inner vertical borders
  73. // - TableDrawMergeChannels() - merge draw channels if clipping isn't required
  74. // - TableSetColumnWidth() - apply resizing width
  75. // - TableUpdateColumnsWeightFromWidth() - recompute columns weights (of weighted columns) from their respective width
  76. // - EndChild() - (if ScrollX/ScrollY is set)
  77. //-----------------------------------------------------------------------------
  78. // Configuration
  79. static const float TABLE_RESIZE_SEPARATOR_HALF_THICKNESS = 4.0f; // Extend outside inner borders.
  80. static const float TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER = 0.06f; // Delay/timer before making the hover feedback (color+cursor) visible because tables/columns tends to be more cramped.
  81. // Helper
  82. inline ImGuiTableFlags TableFixFlags(ImGuiTableFlags flags)
  83. {
  84. // Adjust flags: set default sizing policy
  85. if ((flags & ImGuiTableFlags_SizingPolicyMaskX_) == 0)
  86. flags |= (flags & ImGuiTableFlags_ScrollX) ? ImGuiTableFlags_SizingPolicyFixedX : ImGuiTableFlags_SizingPolicyStretchX;
  87. // Adjust flags: MultiSortable automatically enable Sortable
  88. if (flags & ImGuiTableFlags_MultiSortable)
  89. flags |= ImGuiTableFlags_Sortable;
  90. // Adjust flags: disable saved settings if there's nothing to save
  91. if ((flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Sortable)) == 0)
  92. flags |= ImGuiTableFlags_NoSavedSettings;
  93. // Adjust flags: enforce borders when resizable
  94. if (flags & ImGuiTableFlags_Resizable)
  95. flags |= ImGuiTableFlags_BordersVInner;
  96. // Adjust flags: disable top rows freezing if there's no scrolling
  97. // In theory we could want to assert if ScrollFreeze was set without the corresponding scroll flag, but that would hinder demos.
  98. if ((flags & ImGuiTableFlags_ScrollX) == 0)
  99. flags &= ~ImGuiTableFlags_ScrollFreezeColumnsMask_;
  100. if ((flags & ImGuiTableFlags_ScrollY) == 0)
  101. flags &= ~ImGuiTableFlags_ScrollFreezeRowsMask_;
  102. // Adjust flags: disable NoHostExtendY if we have any scrolling going on
  103. if ((flags & ImGuiTableFlags_NoHostExtendY) && (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0)
  104. flags &= ~ImGuiTableFlags_NoHostExtendY;
  105. // Adjust flags: we don't support NoClipX with (FreezeColumns > 0), we could with some work but it doesn't appear to be worth the effort
  106. if (flags & ImGuiTableFlags_ScrollFreezeColumnsMask_)
  107. flags &= ~ImGuiTableFlags_NoClipX;
  108. return flags;
  109. }
  110. // About 'outer_size':
  111. // The meaning of outer_size needs to differ slightly depending of if we are using ScrollX/ScrollY flags.
  112. // With ScrollX/ScrollY: using a child window for scrolling:
  113. // - outer_size.y < 0.0f -> bottom align
  114. // - outer_size.y = 0.0f -> bottom align: consistent with BeginChild(), best to preserve (0,0) default arg
  115. // - outer_size.y > 0.0f -> fixed child height
  116. // Without scrolling, we output table directly in parent window:
  117. // - outer_size.y < 0.0f -> bottom align (will auto extend, unless NoHostExtendV is set)
  118. // - outer_size.y = 0.0f -> zero minimum height (will auto extend, unless NoHostExtendV is set)
  119. // - outer_size.y > 0.0f -> minimum height (will auto extend, unless NoHostExtendV is set)
  120. // About: 'inner_width':
  121. // With ScrollX:
  122. // - inner_width < 0.0f -> *illegal* fit in known width (right align from outer_size.x) <-- weird
  123. // - inner_width = 0.0f -> auto enlarge: *only* fixed size columns, which will take space they need (proportional columns becomes fixed columns) <-- desired default :(
  124. // - inner_width > 0.0f -> fit in known width: fixed column take space they need if possible (otherwise shrink down), proportional columns share remaining space.
  125. // Without ScrollX:
  126. // - inner_width < 0.0f -> fit in known width (right align from outer_size.x) <-- desired default
  127. // - inner_width = 0.0f -> auto enlarge: will emit contents size in parent window
  128. // - inner_width > 0.0f -> fit in known width (bypass outer_size.x, permitted but not useful, should instead alter outer_width)
  129. // FIXME-TABLE: This is currently not very useful.
  130. // FIXME-TABLE: Replace enlarge vs fixed width by a flag.
  131. // Even if not really useful, we allow 'inner_scroll_width < outer_size.x' for consistency and to facilitate understanding of what the value does.
  132. bool ImGui::BeginTable(const char* str_id, int columns_count, ImGuiTableFlags flags, const ImVec2& outer_size, float inner_width)
  133. {
  134. ImGuiID id = GetID(str_id);
  135. return BeginTableEx(str_id, id, columns_count, flags, outer_size, inner_width);
  136. }
  137. bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImGuiTableFlags flags, const ImVec2& outer_size, float inner_width)
  138. {
  139. ImGuiContext& g = *GImGui;
  140. ImGuiWindow* outer_window = GetCurrentWindow();
  141. if (outer_window->SkipItems) // Consistent with other tables + beneficial side effect that assert on miscalling EndTable() will be more visible.
  142. return false;
  143. // Sanity checks
  144. IM_ASSERT(columns_count > 0 && columns_count < IMGUI_TABLE_MAX_COLUMNS && "Only 0..63 columns allowed!");
  145. if (flags & ImGuiTableFlags_ScrollX)
  146. IM_ASSERT(inner_width >= 0.0f);
  147. const bool use_child_window = (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0;
  148. const ImVec2 avail_size = GetContentRegionAvail();
  149. ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f);
  150. ImRect outer_rect(outer_window->DC.CursorPos, outer_window->DC.CursorPos + actual_outer_size);
  151. // If an outer size is specified ahead we will be able to early out when not visible. Exact clipping rules may evolve.
  152. if (use_child_window && IsClippedEx(outer_rect, 0, false))
  153. {
  154. ItemSize(outer_rect);
  155. return false;
  156. }
  157. flags = TableFixFlags(flags);
  158. if (outer_window->Flags & ImGuiWindowFlags_NoSavedSettings)
  159. flags |= ImGuiTableFlags_NoSavedSettings;
  160. // Acquire storage for the table
  161. ImGuiTable* table = g.Tables.GetOrAddByKey(id);
  162. const ImGuiTableFlags table_last_flags = table->Flags;
  163. const int instance_no = (table->LastFrameActive != g.FrameCount) ? 0 : table->InstanceNo + 1;
  164. const ImGuiID instance_id = id + instance_no;
  165. if (instance_no > 0)
  166. IM_ASSERT(table->ColumnsCount == columns_count && "BeginTable(): Cannot change columns count mid-frame while preserving same ID");
  167. // Initialize
  168. table->ID = id;
  169. table->Flags = flags;
  170. table->InstanceNo = (ImS16)instance_no;
  171. table->LastFrameActive = g.FrameCount;
  172. table->OuterWindow = table->InnerWindow = outer_window;
  173. table->ColumnsCount = columns_count;
  174. table->ColumnsNames.Buf.resize(0);
  175. table->IsInitializing = false;
  176. table->IsLayoutLocked = false;
  177. table->InnerWidth = inner_width;
  178. table->OuterRect = outer_rect;
  179. table->WorkRect = outer_rect;
  180. if (use_child_window)
  181. {
  182. // Ensure no vertical scrollbar appears if we only want horizontal one, to make flag consistent (we have no other way to disable vertical scrollbar of a window while keeping the horizontal one showing)
  183. ImVec2 override_content_size(FLT_MAX, FLT_MAX);
  184. if ((flags & ImGuiTableFlags_ScrollX) && !(flags & ImGuiTableFlags_ScrollY))
  185. override_content_size.y = FLT_MIN;
  186. // Ensure specified width (when not specified, Stretched columns will act as if the width == OuterWidth and never lead to any scrolling)
  187. // We don't handle inner_width < 0.0f, we could potentially use it to right-align based on the right side of the child window work rect,
  188. // which would require knowing ahead if we are going to have decoration taking horizontal spaces (typically a vertical scrollbar).
  189. if (inner_width != 0.0f)
  190. override_content_size.x = inner_width;
  191. if (override_content_size.x != FLT_MAX || override_content_size.y != FLT_MAX)
  192. SetNextWindowContentSize(ImVec2(override_content_size.x != FLT_MAX ? override_content_size.x : 0.0f, override_content_size.y != FLT_MAX ? override_content_size.y : 0.0f));
  193. // Create scrolling region (without border = zero window padding)
  194. ImGuiWindowFlags child_flags = (flags & ImGuiTableFlags_ScrollX) ? ImGuiWindowFlags_HorizontalScrollbar : ImGuiWindowFlags_None;
  195. BeginChildEx(name, instance_id, table->OuterRect.GetSize(), false, child_flags);
  196. table->InnerWindow = g.CurrentWindow;
  197. table->WorkRect = table->InnerWindow->WorkRect;
  198. table->OuterRect = table->InnerWindow->Rect();
  199. }
  200. else
  201. {
  202. // WorkRect.Max will grow as we append contents.
  203. PushID(instance_id);
  204. }
  205. // Backup a copy of host window members we will modify
  206. ImGuiWindow* inner_window = table->InnerWindow;
  207. table->HostIndentX = inner_window->DC.Indent.x;
  208. table->HostClipRect = inner_window->ClipRect;
  209. table->HostSkipItems = inner_window->SkipItems;
  210. table->HostWorkRect = inner_window->WorkRect;
  211. table->HostCursorMaxPos = inner_window->DC.CursorMaxPos;
  212. // Borders
  213. // - None ........Content..... Pad .....Content........
  214. // - VOuter | Pad ..Content..... Pad .....Content.. Pad | // FIXME-TABLE: Not handled properly
  215. // - VInner ........Content.. Pad | Pad ..Content........ // FIXME-TABLE: Not handled properly
  216. // - VOuter+VInner | Pad ..Content.. Pad | Pad ..Content.. Pad |
  217. const bool has_cell_padding_x = (flags & ImGuiTableFlags_BordersVOuter) != 0;
  218. table->CellPaddingX1 = has_cell_padding_x ? g.Style.CellPadding.x + 1.0f : 0.0f;
  219. table->CellPaddingX2 = has_cell_padding_x ? g.Style.CellPadding.x : 0.0f;
  220. table->CellPaddingY = g.Style.CellPadding.y;
  221. table->CellSpacingX = has_cell_padding_x ? 0.0f : g.Style.CellPadding.x;
  222. table->CurrentColumn = -1;
  223. table->CurrentRow = -1;
  224. table->RowBgColorCounter = 0;
  225. table->LastRowFlags = ImGuiTableRowFlags_None;
  226. table->InnerClipRect = (inner_window == outer_window) ? table->WorkRect : inner_window->ClipRect;
  227. table->InnerClipRect.ClipWith(table->WorkRect); // We need this to honor inner_width
  228. table->InnerClipRect.ClipWith(table->HostClipRect);
  229. table->InnerClipRect.Max.y = (flags & ImGuiTableFlags_NoHostExtendY) ? table->WorkRect.Max.y : inner_window->ClipRect.Max.y;
  230. table->BackgroundClipRect = table->InnerClipRect;
  231. table->RowPosY1 = table->RowPosY2 = table->WorkRect.Min.y; // This is needed somehow
  232. table->RowTextBaseline = 0.0f; // This will be cleared again by TableBeginRow()
  233. table->FreezeRowsRequest = (ImS8)((flags & ImGuiTableFlags_ScrollFreezeRowsMask_) >> ImGuiTableFlags_ScrollFreezeRowsShift_);
  234. table->FreezeRowsCount = (inner_window->Scroll.y != 0.0f) ? table->FreezeRowsRequest : 0;
  235. table->FreezeColumnsRequest = (ImS8)((flags & ImGuiTableFlags_ScrollFreezeColumnsMask_) >> ImGuiTableFlags_ScrollFreezeColumnsShift_);
  236. table->FreezeColumnsCount = (inner_window->Scroll.x != 0.0f) ? table->FreezeColumnsRequest : 0;
  237. table->IsFreezeRowsPassed = (table->FreezeRowsCount == 0);
  238. table->DeclColumnsCount = 0;
  239. table->HoveredColumnBody = -1;
  240. table->HoveredColumnBorder = -1;
  241. table->RightMostActiveColumn = -1;
  242. // Using opaque colors facilitate overlapping elements of the grid
  243. table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong);
  244. table->BorderColorLight = GetColorU32(ImGuiCol_TableBorderLight);
  245. table->BorderX1 = table->InnerClipRect.Min.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : -1.0f);
  246. table->BorderX2 = table->InnerClipRect.Max.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : +1.0f);
  247. // Make table current
  248. g.CurrentTableStack.push_back(ImGuiPtrOrIndex(g.Tables.GetIndex(table)));
  249. g.CurrentTable = table;
  250. outer_window->DC.CurrentTable = table;
  251. if ((table_last_flags & ImGuiTableFlags_Reorderable) && !(flags & ImGuiTableFlags_Reorderable))
  252. table->IsResetDisplayOrderRequest = true;
  253. // Clear data if columns count changed
  254. if (table->Columns.Size != 0 && table->Columns.Size != columns_count)
  255. {
  256. table->Columns.resize(0);
  257. table->DisplayOrder.resize(0);
  258. }
  259. // Setup default columns state
  260. if (table->Columns.Size == 0)
  261. {
  262. table->IsInitializing = table->IsSettingsRequestLoad = table->IsSortSpecsDirty = true;
  263. table->Columns.reserve(columns_count);
  264. table->DisplayOrder.reserve(columns_count);
  265. for (int n = 0; n < columns_count; n++)
  266. {
  267. ImGuiTableColumn column;
  268. column.IndexDisplayOrder = (ImS8)n;
  269. table->Columns.push_back(column);
  270. table->DisplayOrder.push_back(column.IndexDisplayOrder);
  271. }
  272. }
  273. // Load settings
  274. if (table->IsSettingsRequestLoad)
  275. TableLoadSettings(table);
  276. // Disable output until user calls TableNextRow() or TableNextCell() leading to the TableUpdateLayout() call..
  277. // This is not strictly necessary but will reduce cases were misleading "out of table" output will be confusing to the user.
  278. // Because we cannot safely assert in EndTable() when no rows have been created, this seems like our best option.
  279. inner_window->SkipItems = true;
  280. // Update/lock which columns will be Active for the frame
  281. TableBeginUpdateColumns(table);
  282. return true;
  283. }
  284. void ImGui::TableBeginUpdateColumns(ImGuiTable* table)
  285. {
  286. // Handle resizing request
  287. // (We process this at the first TableBegin of the frame)
  288. // FIXME-TABLE: Preserve contents width _while resizing down_ until releasing.
  289. // FIXME-TABLE: Contains columns if our work area doesn't allow for scrolling.
  290. if (table->InstanceNo == 0)
  291. {
  292. if (table->ResizedColumn != -1 && table->ResizedColumnNextWidth != FLT_MAX)
  293. TableSetColumnWidth(table, &table->Columns[table->ResizedColumn], table->ResizedColumnNextWidth);
  294. table->LastResizedColumn = table->ResizedColumn;
  295. table->ResizedColumnNextWidth = FLT_MAX;
  296. table->ResizedColumn = -1;
  297. }
  298. // Handle reordering request
  299. // Note: we don't clear ReorderColumn after handling the request.
  300. if (table->InstanceNo == 0)
  301. {
  302. if (table->HeldHeaderColumn == -1 && table->ReorderColumn != -1)
  303. table->ReorderColumn = -1;
  304. table->HeldHeaderColumn = -1;
  305. if (table->ReorderColumn != -1 && table->ReorderColumnDir != 0)
  306. {
  307. // We need to handle reordering across hidden columns.
  308. // In the configuration below, moving C to the right of E will lead to:
  309. // ... C [D] E ---> ... [D] E C (Column name/index)
  310. // ... 2 3 4 ... 2 3 4 (Display order)
  311. const int reorder_dir = table->ReorderColumnDir;
  312. IM_ASSERT(reorder_dir == -1 || reorder_dir == +1);
  313. IM_ASSERT(table->Flags & ImGuiTableFlags_Reorderable);
  314. ImGuiTableColumn* src_column = &table->Columns[table->ReorderColumn];
  315. ImGuiTableColumn* dst_column = &table->Columns[(reorder_dir == -1) ? src_column->PrevActiveColumn : src_column->NextActiveColumn];
  316. IM_UNUSED(dst_column);
  317. const int src_order = src_column->IndexDisplayOrder;
  318. const int dst_order = dst_column->IndexDisplayOrder;
  319. src_column->IndexDisplayOrder = (ImS8)dst_order;
  320. for (int order_n = src_order + reorder_dir; order_n != dst_order + reorder_dir; order_n += reorder_dir)
  321. table->Columns[table->DisplayOrder[order_n]].IndexDisplayOrder -= (ImS8)reorder_dir;
  322. IM_ASSERT(dst_column->IndexDisplayOrder == dst_order - reorder_dir);
  323. // Display order is stored in both columns->IndexDisplayOrder and table->DisplayOrder[], rebuild the later from the former.
  324. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  325. table->DisplayOrder[table->Columns[column_n].IndexDisplayOrder] = (ImS8)column_n;
  326. table->ReorderColumnDir = 0;
  327. table->IsSettingsDirty = true;
  328. }
  329. }
  330. // Handle display order reset request
  331. if (table->IsResetDisplayOrderRequest)
  332. {
  333. for (int n = 0; n < table->ColumnsCount; n++)
  334. table->DisplayOrder[n] = table->Columns[n].IndexDisplayOrder = (ImU8)n;
  335. table->IsResetDisplayOrderRequest = false;
  336. table->IsSettingsDirty = true;
  337. }
  338. // Setup and lock Active state and order
  339. table->ColumnsActiveCount = 0;
  340. table->IsDefaultDisplayOrder = true;
  341. ImGuiTableColumn* last_active_column = NULL;
  342. bool want_column_auto_fit = false;
  343. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  344. {
  345. const int column_n = table->DisplayOrder[order_n];
  346. if (column_n != order_n)
  347. table->IsDefaultDisplayOrder = false;
  348. ImGuiTableColumn* column = &table->Columns[column_n];
  349. column->NameOffset = -1;
  350. if (!(table->Flags & ImGuiTableFlags_Hideable) || (column->Flags & ImGuiTableColumnFlags_NoHide))
  351. column->NextIsActive = true;
  352. if (column->IsActive != column->NextIsActive)
  353. {
  354. column->IsActive = column->NextIsActive;
  355. table->IsSettingsDirty = true;
  356. if (!column->IsActive && column->SortOrder != -1)
  357. table->IsSortSpecsDirty = true;
  358. }
  359. if (column->SortOrder > 0 && !(table->Flags & ImGuiTableFlags_MultiSortable))
  360. table->IsSortSpecsDirty = true;
  361. if (column->AutoFitQueue != 0x00)
  362. want_column_auto_fit = true;
  363. ImU64 index_mask = (ImU64)1 << column_n;
  364. ImU64 display_order_mask = (ImU64)1 << column->IndexDisplayOrder;
  365. if (column->IsActive)
  366. {
  367. column->PrevActiveColumn = column->NextActiveColumn = -1;
  368. if (last_active_column)
  369. {
  370. last_active_column->NextActiveColumn = (ImS8)column_n;
  371. column->PrevActiveColumn = (ImS8)table->Columns.index_from_ptr(last_active_column);
  372. }
  373. column->IndexWithinActiveSet = (ImS8)table->ColumnsActiveCount;
  374. table->ColumnsActiveCount++;
  375. table->ActiveMaskByIndex |= index_mask;
  376. table->ActiveMaskByDisplayOrder |= display_order_mask;
  377. last_active_column = column;
  378. }
  379. else
  380. {
  381. column->IndexWithinActiveSet = -1;
  382. table->ActiveMaskByIndex &= ~index_mask;
  383. table->ActiveMaskByDisplayOrder &= ~display_order_mask;
  384. }
  385. IM_ASSERT(column->IndexWithinActiveSet <= column->IndexDisplayOrder);
  386. }
  387. table->VisibleMaskByIndex = table->ActiveMaskByIndex; // Columns will be masked out by TableUpdateLayout() when Clipped
  388. table->RightMostActiveColumn = (ImS8)(last_active_column ? table->Columns.index_from_ptr(last_active_column) : -1);
  389. // Disable child window clipping while fitting columns. This is not strictly necessary but makes it possible to avoid
  390. // the column fitting to wait until the first visible frame of the child container (may or not be a good thing).
  391. if (want_column_auto_fit && table->OuterWindow != table->InnerWindow)
  392. table->InnerWindow->SkipItems = false;
  393. }
  394. void ImGui::TableUpdateDrawChannels(ImGuiTable* table)
  395. {
  396. // Allocate draw channels.
  397. // - We allocate them following the storage order instead of the display order so reordering won't needlessly increase overall dormant memory cost
  398. // - We isolate headers draw commands in their own channels instead of just altering clip rects. This is in order to facilitate merging of draw commands.
  399. // - After crossing FreezeRowsCount, all columns see their current draw channel increased.
  400. // - We only use the dummy draw channel so we can push a null clipping rectangle into it without affecting other channels, while simplifying per-row/per-cell overhead. It will be empty and discarded when merged.
  401. // Draw channel allocation (before merging):
  402. // - NoClip --> 1+1 channels: background + foreground (same clip rect == 1 draw call)
  403. // - Clip --> 1+N channels
  404. // - FreezeRows || FreezeColumns --> 1+N*2 (unless scrolling value is zero)
  405. // - FreezeRows && FreezeColunns --> 2+N*2 (unless scrolling value is zero)
  406. const int freeze_row_multiplier = (table->FreezeRowsCount > 0) ? 2 : 1;
  407. const int channels_for_row = (table->Flags & ImGuiTableFlags_NoClipX) ? 1 : table->ColumnsActiveCount;
  408. const int channels_for_background = 1;
  409. const int channels_for_dummy = (table->ColumnsActiveCount < table->ColumnsCount || table->VisibleMaskByIndex != table->ActiveMaskByIndex) ? +1 : 0;
  410. const int channels_total = channels_for_background + (channels_for_row * freeze_row_multiplier) + channels_for_dummy;
  411. table->DrawSplitter.Split(table->InnerWindow->DrawList, channels_total);
  412. table->DummyDrawChannel = channels_for_dummy ? (ImS8)(channels_total - 1) : -1;
  413. int draw_channel_current = 1;
  414. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  415. {
  416. ImGuiTableColumn* column = &table->Columns[column_n];
  417. if (!column->IsClipped)
  418. {
  419. column->DrawChannelRowsBeforeFreeze = (ImS8)(draw_channel_current);
  420. column->DrawChannelRowsAfterFreeze = (ImS8)(draw_channel_current + (table->FreezeRowsCount > 0 ? channels_for_row : 0));
  421. if (!(table->Flags & ImGuiTableFlags_NoClipX))
  422. draw_channel_current++;
  423. }
  424. else
  425. {
  426. column->DrawChannelRowsBeforeFreeze = column->DrawChannelRowsAfterFreeze = table->DummyDrawChannel;
  427. }
  428. column->DrawChannelCurrent = column->DrawChannelRowsBeforeFreeze;
  429. }
  430. }
  431. // Adjust flags: default width mode + weighted columns are not allowed when auto extending
  432. static ImGuiTableColumnFlags TableFixColumnFlags(ImGuiTable* table, ImGuiTableColumnFlags flags)
  433. {
  434. // Sizing Policy
  435. if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0)
  436. {
  437. if (table->Flags & ImGuiTableFlags_SizingPolicyFixedX)
  438. flags |= ((table->Flags & ImGuiTableFlags_Resizable) && !(flags & ImGuiTableColumnFlags_NoResize)) ? ImGuiTableColumnFlags_WidthFixed : ImGuiTableColumnFlags_WidthAlwaysAutoResize;
  439. else
  440. flags |= ImGuiTableColumnFlags_WidthStretch;
  441. }
  442. IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_WidthMask_)); // Check that only 1 of each set is used.
  443. if ((flags & ImGuiTableColumnFlags_WidthAlwaysAutoResize))// || ((flags & ImGuiTableColumnFlags_WidthStretch) && (table->Flags & ImGuiTableFlags_SizingPolicyStretchX)))
  444. flags |= ImGuiTableColumnFlags_NoResize;
  445. //if ((flags & ImGuiTableColumnFlags_WidthStretch) && (table->Flags & ImGuiTableFlags_SizingPolicyFixedX))
  446. // flags = (flags & ~ImGuiTableColumnFlags_WidthMask_) | ImGuiTableColumnFlags_WidthFixed;
  447. // Sorting
  448. if ((flags & ImGuiTableColumnFlags_NoSortAscending) && (flags & ImGuiTableColumnFlags_NoSortDescending))
  449. flags |= ImGuiTableColumnFlags_NoSort;
  450. // Alignment
  451. //if ((flags & ImGuiTableColumnFlags_AlignMask_) == 0)
  452. // flags |= ImGuiTableColumnFlags_AlignCenter;
  453. //IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_AlignMask_)); // Check that only 1 of each set is used.
  454. return flags;
  455. }
  456. static void TableFixColumnSortDirection(ImGuiTableColumn* column)
  457. {
  458. // Handle NoSortAscending/NoSortDescending
  459. if (column->SortDirection == ImGuiSortDirection_Ascending && (column->Flags & ImGuiTableColumnFlags_NoSortAscending))
  460. column->SortDirection = ImGuiSortDirection_Descending;
  461. else if (column->SortDirection == ImGuiSortDirection_Descending && (column->Flags & ImGuiTableColumnFlags_NoSortDescending))
  462. column->SortDirection = ImGuiSortDirection_Ascending;
  463. }
  464. static float TableGetMinColumnWidth()
  465. {
  466. ImGuiContext& g = *GImGui;
  467. // return g.Style.ColumnsMinSpacing;
  468. return g.Style.FramePadding.x * 3.0f;
  469. }
  470. // Layout columns for the frame
  471. // Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() to be called first.
  472. // FIXME-TABLE: Our width (and therefore our WorkRect) will be minimal in the first frame for WidthAlwaysAutoResize columns,
  473. // increase feedback side-effect with widgets relying on WorkRect.Max.x. Maybe provide a default distribution for WidthAlwaysAutoResize columns?
  474. void ImGui::TableUpdateLayout(ImGuiTable* table)
  475. {
  476. IM_ASSERT(table->IsLayoutLocked == false);
  477. // Compute offset, clip rect for the frame
  478. const ImRect work_rect = table->WorkRect;
  479. const float padding_auto_x = table->CellPaddingX1; // Can't make auto padding larger than what WorkRect knows about so right-alignment matches.
  480. const float min_column_width = TableGetMinColumnWidth();
  481. int count_fixed = 0;
  482. float width_fixed = 0.0f;
  483. float total_weights = 0.0f;
  484. table->LeftMostStretchedColumnDisplayOrder = -1;
  485. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  486. {
  487. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  488. continue;
  489. const int column_n = table->DisplayOrder[order_n];
  490. ImGuiTableColumn* column = &table->Columns[column_n];
  491. // Adjust flags: default width mode + weighted columns are not allowed when auto extending
  492. // FIXME-TABLE: Clarify why we need to do this again here and not just in TableSetupColumn()
  493. column->Flags = TableFixColumnFlags(table, column->FlagsIn);
  494. if ((column->Flags & ImGuiTableColumnFlags_IndentMask_) == 0)
  495. column->Flags |= (column_n == 0) ? ImGuiTableColumnFlags_IndentEnable : ImGuiTableColumnFlags_IndentDisable;
  496. // We have a unusual edge case where if the user doesn't call TableGetSortSpecs() but has sorting enabled
  497. // or varying sorting flags, we still want the sorting arrows to honor those flags.
  498. if (table->Flags & ImGuiTableFlags_Sortable)
  499. TableFixColumnSortDirection(column);
  500. if (column->Flags & (ImGuiTableColumnFlags_WidthAlwaysAutoResize | ImGuiTableColumnFlags_WidthFixed))
  501. {
  502. // Latch initial size for fixed columns
  503. count_fixed += 1;
  504. const bool init_size = (column->AutoFitQueue != 0x00) || (column->Flags & ImGuiTableColumnFlags_WidthAlwaysAutoResize);
  505. if (init_size)
  506. {
  507. // Combine width from regular rows + width from headers unless requested not to
  508. float width_request = (float)ImMax(column->ContentWidthRowsFrozen, column->ContentWidthRowsUnfrozen);
  509. if (!(table->Flags & ImGuiTableFlags_NoHeadersWidth) && !(column->Flags & ImGuiTableColumnFlags_NoHeaderWidth))
  510. width_request = ImMax(width_request, (float)column->ContentWidthHeadersDesired);
  511. column->WidthRequested = ImMax(width_request + padding_auto_x, min_column_width);
  512. // FIXME-TABLE: Increase minimum size during init frame to avoid biasing auto-fitting widgets (e.g. TextWrapped) too much.
  513. // Otherwise what tends to happen is that TextWrapped would output a very large height (= first frame scrollbar display very off + clipper would skip lots of items)
  514. // This is merely making the side-effect less extreme, but doesn't properly fixes it.
  515. if (column->AutoFitQueue > 0x01 && table->IsInitializing)
  516. column->WidthRequested = ImMax(column->WidthRequested, min_column_width * 4.0f);
  517. }
  518. width_fixed += column->WidthRequested;
  519. }
  520. else
  521. {
  522. IM_ASSERT(column->Flags & ImGuiTableColumnFlags_WidthStretch);
  523. IM_ASSERT(column->ResizeWeight > 0.0f);
  524. total_weights += column->ResizeWeight;
  525. if (table->LeftMostStretchedColumnDisplayOrder == -1)
  526. table->LeftMostStretchedColumnDisplayOrder = (ImS8)column->IndexDisplayOrder;
  527. }
  528. }
  529. // Layout
  530. const float width_spacings = table->CellSpacingX * (table->ColumnsActiveCount - 1);
  531. float width_avail;
  532. if ((table->Flags & ImGuiTableFlags_ScrollX) && (table->InnerWidth == 0.0f))
  533. width_avail = table->InnerClipRect.GetWidth() - width_spacings - 1.0f;
  534. else
  535. width_avail = work_rect.GetWidth() - width_spacings - 1.0f; // Remove -1.0f to cancel out the +1.0f we are doing in EndTable() to make last column line visible
  536. const float width_avail_for_stretched_columns = width_avail - width_fixed;
  537. float width_remaining_for_stretched_columns = width_avail_for_stretched_columns;
  538. // Apply final width based on requested widths
  539. // Mark some columns as not resizable
  540. int count_resizable = 0;
  541. table->ColumnsTotalWidth = width_spacings;
  542. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  543. {
  544. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  545. continue;
  546. ImGuiTableColumn* column = &table->Columns[table->DisplayOrder[order_n]];
  547. // Allocate width for stretched/weighted columns
  548. if (column->Flags & ImGuiTableColumnFlags_WidthStretch)
  549. {
  550. float weight_ratio = column->ResizeWeight / total_weights;
  551. column->WidthRequested = IM_FLOOR(ImMax(width_avail_for_stretched_columns * weight_ratio, min_column_width) + 0.01f);
  552. width_remaining_for_stretched_columns -= column->WidthRequested;
  553. // [Resize Rule 2] Resizing from right-side of a weighted column before a fixed column froward sizing to left-side of fixed column
  554. // We also need to copy the NoResize flag..
  555. if (column->NextActiveColumn != -1)
  556. if (ImGuiTableColumn* next_column = &table->Columns[column->NextActiveColumn])
  557. if (next_column->Flags & ImGuiTableColumnFlags_WidthFixed)
  558. column->Flags |= (next_column->Flags & ImGuiTableColumnFlags_NoDirectResize_);
  559. }
  560. // [Resize Rule 1] The right-most active column is not resizable if there is at least one Stretch column (see comments in TableResizeColumn().)
  561. if (column->NextActiveColumn == -1 && table->LeftMostStretchedColumnDisplayOrder != -1)
  562. column->Flags |= ImGuiTableColumnFlags_NoDirectResize_;
  563. if (!(column->Flags & ImGuiTableColumnFlags_NoResize))
  564. count_resizable++;
  565. // Assign final width, record width in case we will need to shrink
  566. column->WidthGiven = ImFloor(ImMax(column->WidthRequested, min_column_width));
  567. table->ColumnsTotalWidth += column->WidthGiven;
  568. }
  569. #if 0
  570. const float width_excess = table->ColumnsTotalWidth - work_rect.GetWidth();
  571. if ((table->Flags & ImGuiTableFlags_SizingPolicyStretchX) && width_excess > 0.0f)
  572. {
  573. // Shrink widths when the total does not fit
  574. // FIXME-TABLE: This is working but confuses/conflicts with manual resizing.
  575. // FIXME-TABLE: Policy to shrink down below below ideal/requested width if there's no room?
  576. g.ShrinkWidthBuffer.resize(table->ColumnsActiveCount);
  577. for (int order_n = 0, active_n = 0; order_n < table->ColumnsCount; order_n++)
  578. {
  579. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  580. continue;
  581. const int column_n = table->DisplayOrder[order_n];
  582. g.ShrinkWidthBuffer[active_n].Index = column_n;
  583. g.ShrinkWidthBuffer[active_n].Width = table->Columns[column_n].WidthGiven;
  584. active_n++;
  585. }
  586. ShrinkWidths(g.ShrinkWidthBuffer.Data, g.ShrinkWidthBuffer.Size, width_excess);
  587. for (int n = 0; n < g.ShrinkWidthBuffer.Size; n++)
  588. table->Columns[g.ShrinkWidthBuffer.Data[n].Index].WidthGiven = ImMax(g.ShrinkWidthBuffer.Data[n].Width, min_column_size);
  589. // FIXME: Need to alter table->ColumnsTotalWidth
  590. }
  591. else
  592. #endif
  593. // Redistribute remainder width due to rounding (remainder width is < 1.0f * number of Stretch column)
  594. // Using right-to-left distribution (more likely to match resizing cursor), could be adjusted depending where the mouse cursor is and/or relative weights.
  595. // FIXME-TABLE: May be simpler to store floating width and floor final positions only
  596. // FIXME-TABLE: Make it optional? User might prefer to preserve pixel perfect same size?
  597. if (width_remaining_for_stretched_columns >= 1.0f)
  598. for (int order_n = table->ColumnsCount - 1; total_weights > 0.0f && width_remaining_for_stretched_columns >= 1.0f && order_n >= 0; order_n--)
  599. {
  600. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  601. continue;
  602. ImGuiTableColumn* column = &table->Columns[table->DisplayOrder[order_n]];
  603. if (!(column->Flags & ImGuiTableColumnFlags_WidthStretch))
  604. continue;
  605. column->WidthRequested += 1.0f;
  606. column->WidthGiven += 1.0f;
  607. width_remaining_for_stretched_columns -= 1.0f;
  608. }
  609. // Setup final position, offset and clipping rectangles
  610. int active_n = 0;
  611. float offset_x = (table->FreezeColumnsCount > 0) ? table->OuterRect.Min.x : work_rect.Min.x;
  612. ImRect host_clip_rect = table->InnerClipRect;
  613. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  614. {
  615. const int column_n = table->DisplayOrder[order_n];
  616. ImGuiTableColumn* column = &table->Columns[column_n];
  617. if (table->FreezeColumnsCount > 0 && table->FreezeColumnsCount == active_n)
  618. offset_x += work_rect.Min.x - table->OuterRect.Min.x;
  619. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  620. {
  621. // Hidden column: clear a few fields and we are done with it for the remainder of the function.
  622. // We set a zero-width clip rect however we pay attention to set Min.y/Max.y properly to not interfere with the clipper.
  623. column->MinX = column->MaxX = offset_x;
  624. column->StartXRows = column->StartXHeaders = offset_x;
  625. column->WidthGiven = 0.0f;
  626. column->ClipRect.Min.x = offset_x;
  627. column->ClipRect.Min.y = work_rect.Min.y;
  628. column->ClipRect.Max.x = offset_x;
  629. column->ClipRect.Max.y = FLT_MAX;
  630. column->ClipRect.ClipWithFull(host_clip_rect);
  631. column->IsClipped = column->SkipItems = true;
  632. continue;
  633. }
  634. float max_x = FLT_MAX;
  635. if (table->Flags & ImGuiTableFlags_ScrollX)
  636. {
  637. // Frozen columns can't reach beyond visible width else scrolling will naturally break.
  638. if (order_n < table->FreezeColumnsRequest)
  639. max_x = table->InnerClipRect.Max.x - (table->FreezeColumnsRequest - order_n) * min_column_width;
  640. }
  641. else
  642. {
  643. // If horizontal scrolling if disabled, we apply a final lossless shrinking of columns in order to make sure they are all visible.
  644. // Because of this we also know that all of the columns will always fit in table->WorkRect and therefore in table->InnerRect (because ScrollX is off)
  645. max_x = table->WorkRect.Max.x - (table->ColumnsActiveCount - (column->IndexWithinActiveSet + 1)) * min_column_width;
  646. }
  647. if (offset_x + column->WidthGiven > max_x)
  648. column->WidthGiven = ImMax(max_x - offset_x, min_column_width);
  649. column->MinX = offset_x;
  650. column->MaxX = column->MinX + column->WidthGiven;
  651. //// A one pixel padding on the right side makes clipping more noticeable and contents look less cramped.
  652. column->ClipRect.Min.x = column->MinX;
  653. column->ClipRect.Min.y = work_rect.Min.y;
  654. column->ClipRect.Max.x = column->MaxX;// -1.0f;
  655. column->ClipRect.Max.y = FLT_MAX;
  656. column->ClipRect.ClipWithFull(host_clip_rect);
  657. column->IsClipped = (column->ClipRect.Max.x <= column->ClipRect.Min.x) && (column->AutoFitQueue & 1) == 0 && (column->CannotSkipItemsQueue & 1) == 0;
  658. column->SkipItems = column->IsClipped || table->HostSkipItems;
  659. if (column->IsClipped)
  660. {
  661. // Columns with the _WidthAlwaysAutoResize sizing policy will never be updated then.
  662. table->VisibleMaskByIndex &= ~((ImU64)1 << column_n);
  663. }
  664. else
  665. {
  666. // Starting cursor position
  667. column->StartXRows = column->StartXHeaders = column->MinX + table->CellPaddingX1;
  668. // Alignment
  669. // FIXME-TABLE: This align based on the whole column width, not per-cell, and therefore isn't useful in many cases.
  670. // (To be able to honor this we might be able to store a log of cells width, per row, for visible rows, but nav/programmatic scroll would have visible artifacts.)
  671. //if (column->Flags & ImGuiTableColumnFlags_AlignRight)
  672. // column->StartXRows = ImMax(column->StartXRows, column->MaxX - column->ContentWidthRowsUnfrozen);
  673. //else if (column->Flags & ImGuiTableColumnFlags_AlignCenter)
  674. // column->StartXRows = ImLerp(column->StartXRows, ImMax(column->StartXRows, column->MaxX - column->ContentWidthRowsUnfrozen), 0.5f);
  675. // Reset content width variables
  676. const float initial_max_pos_x = column->MinX + table->CellPaddingX1;
  677. column->ContentMaxPosRowsFrozen = column->ContentMaxPosRowsUnfrozen = initial_max_pos_x;
  678. column->ContentMaxPosHeadersUsed = column->ContentMaxPosHeadersDesired = initial_max_pos_x;
  679. }
  680. // Don't decrement auto-fit counters until container window got a chance to submit its items
  681. if (table->HostSkipItems == false)
  682. {
  683. column->AutoFitQueue >>= 1;
  684. column->CannotSkipItemsQueue >>= 1;
  685. }
  686. if (active_n < table->FreezeColumnsCount)
  687. host_clip_rect.Min.x = ImMax(host_clip_rect.Min.x, column->MaxX + 2.0f);
  688. offset_x += column->WidthGiven + table->CellSpacingX;
  689. active_n++;
  690. }
  691. // Clear Resizable flag if none of our column are actually resizable (either via an explicit _NoResize flag, either because of using _WidthAlwaysAutoResize/_WidthStretch)
  692. // This will hide the resizing option from the context menu.
  693. if (count_resizable == 0 && (table->Flags & ImGuiTableFlags_Resizable))
  694. table->Flags &= ~ImGuiTableFlags_Resizable;
  695. // Allocate draw channels
  696. TableUpdateDrawChannels(table);
  697. // Borders
  698. if (table->Flags & ImGuiTableFlags_Resizable)
  699. TableUpdateBorders(table);
  700. // Reset fields after we used them in TableSetupResize()
  701. table->LastFirstRowHeight = 0.0f;
  702. table->IsLayoutLocked = true;
  703. table->IsUsingHeaders = false;
  704. // Context menu
  705. if (table->IsContextPopupOpen && table->InstanceNo == table->InstanceInteracted)
  706. {
  707. if (BeginPopup("##TableContextMenu"))
  708. {
  709. TableDrawContextMenu(table, table->ContextPopupColumn);
  710. EndPopup();
  711. }
  712. else
  713. {
  714. table->IsContextPopupOpen = false;
  715. }
  716. }
  717. // Initial state
  718. ImGuiWindow* inner_window = table->InnerWindow;
  719. if (table->Flags & ImGuiTableFlags_NoClipX)
  720. table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 1);
  721. else
  722. inner_window->DrawList->PushClipRect(inner_window->ClipRect.Min, inner_window->ClipRect.Max, false);
  723. }
  724. // Process interaction on resizing borders. Actual size change will be applied in EndTable()
  725. // - Set table->HoveredColumnBorder with a short delay/timer to reduce feedback noise
  726. // - Submit ahead of table contents and header, use ImGuiButtonFlags_AllowItemOverlap to prioritize widgets overlapping the same area.
  727. void ImGui::TableUpdateBorders(ImGuiTable* table)
  728. {
  729. ImGuiContext& g = *GImGui;
  730. IM_ASSERT(table->Flags & ImGuiTableFlags_Resizable);
  731. // At this point OuterRect height may be zero or under actual final height, so we rely on temporal coherency and use
  732. // the final height from last frame. Because this is only affecting _interaction_ with columns, it is not really problematic.
  733. // (whereas the actual visual will be displayed in EndTable() and using the current frame height)
  734. // Actual columns highlight/render will be performed in EndTable() and not be affected.
  735. const bool borders_full_height = (table->IsUsingHeaders == false) || (table->Flags & ImGuiTableFlags_BordersVFullHeight);
  736. const float hit_half_width = TABLE_RESIZE_SEPARATOR_HALF_THICKNESS;
  737. const float hit_y1 = table->OuterRect.Min.y;
  738. const float hit_y2_full = ImMax(table->OuterRect.Max.y, hit_y1 + table->LastOuterHeight);
  739. const float hit_y2 = borders_full_height ? hit_y2_full : (hit_y1 + table->LastFirstRowHeight);
  740. const float mouse_x_hover_body = (g.IO.MousePos.y >= hit_y1 && g.IO.MousePos.y < hit_y2_full) ? g.IO.MousePos.x : FLT_MAX;
  741. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  742. {
  743. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  744. continue;
  745. const int column_n = table->DisplayOrder[order_n];
  746. ImGuiTableColumn* column = &table->Columns[column_n];
  747. // Detect hovered column:
  748. // - we perform an unusually low-level check here.. not using IsMouseHoveringRect() to avoid touch padding.
  749. // - we don't care about the full set of IsItemHovered() feature either.
  750. if (mouse_x_hover_body >= column->MinX && mouse_x_hover_body < column->MaxX)
  751. table->HoveredColumnBody = (ImS8)column_n;
  752. if (column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoDirectResize_))
  753. continue;
  754. ImGuiID column_id = table->ID + (table->InstanceNo * table->ColumnsCount) + column_n;
  755. ImRect hit_rect(column->MaxX - hit_half_width, hit_y1, column->MaxX + hit_half_width, hit_y2);
  756. //GetForegroundDrawList()->AddRect(hit_rect.Min, hit_rect.Max, IM_COL32(255, 0, 0, 100));
  757. KeepAliveID(column_id);
  758. bool hovered = false, held = false;
  759. bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick);
  760. if (pressed && IsMouseDoubleClicked(0) && !(column->Flags & ImGuiTableColumnFlags_WidthStretch))
  761. {
  762. // FIXME-TABLE: Double-clicking on column edge could auto-fit weighted column?
  763. TableSetColumnAutofit(table, column_n);
  764. ClearActiveID();
  765. held = hovered = false;
  766. }
  767. if (held)
  768. {
  769. table->ResizedColumn = (ImS8)column_n;
  770. table->InstanceInteracted = table->InstanceNo;
  771. }
  772. if ((hovered && g.HoveredIdTimer > TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER) || held)
  773. {
  774. table->HoveredColumnBorder = (ImS8)column_n;
  775. SetMouseCursor(ImGuiMouseCursor_ResizeEW);
  776. }
  777. }
  778. }
  779. void ImGui::EndTable()
  780. {
  781. ImGuiContext& g = *GImGui;
  782. ImGuiTable* table = g.CurrentTable;
  783. IM_ASSERT(table != NULL && "Only call EndTable() is BeginTable() returns true!");
  784. // This assert would be very useful to catch a common error... unfortunately it would probably trigger in some cases,
  785. // and for consistency user may sometimes output empty tables (and still benefit from e.g. outer border)
  786. //IM_ASSERT(table->IsLayoutLocked && "Table unused: never called TableNextRow(), is that the intent?");
  787. // If the user never got to call TableNextRow() or TableNextCell(), we call layout ourselves to ensure all our
  788. // code paths are consistent (instead of just hoping that TableBegin/TableEnd will work), get borders drawn, etc.
  789. if (!table->IsLayoutLocked)
  790. TableUpdateLayout(table);
  791. const ImGuiTableFlags flags = table->Flags;
  792. ImGuiWindow* inner_window = table->InnerWindow;
  793. ImGuiWindow* outer_window = table->OuterWindow;
  794. IM_ASSERT(inner_window == g.CurrentWindow);
  795. IM_ASSERT(outer_window == inner_window || outer_window == inner_window->ParentWindow);
  796. if (table->IsInsideRow)
  797. TableEndRow(table);
  798. // Finalize table height
  799. inner_window->SkipItems = table->HostSkipItems;
  800. inner_window->DC.CursorMaxPos = table->HostCursorMaxPos;
  801. if (inner_window != outer_window)
  802. {
  803. table->OuterRect.Max.y = ImMax(table->OuterRect.Max.y, inner_window->Pos.y + inner_window->Size.y);
  804. inner_window->DC.CursorMaxPos.y = table->RowPosY2;
  805. }
  806. else if (!(flags & ImGuiTableFlags_NoHostExtendY))
  807. {
  808. table->OuterRect.Max.y = ImMax(table->OuterRect.Max.y, inner_window->DC.CursorPos.y);
  809. inner_window->DC.CursorMaxPos.y = table->RowPosY2;
  810. }
  811. table->WorkRect.Max.y = ImMax(table->WorkRect.Max.y, table->OuterRect.Max.y);
  812. table->LastOuterHeight = table->OuterRect.GetHeight();
  813. // Store content width reference for each column
  814. float max_pos_x = inner_window->DC.CursorMaxPos.x;
  815. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  816. {
  817. ImGuiTableColumn* column = &table->Columns[column_n];
  818. // Store content width (for both Headers and Rows)
  819. //float ref_x = column->MinX;
  820. float ref_x_rows = column->StartXRows - table->CellPaddingX1;
  821. float ref_x_headers = column->StartXHeaders - table->CellPaddingX1;
  822. column->ContentWidthRowsFrozen = (ImS16)ImMax(0.0f, column->ContentMaxPosRowsFrozen - ref_x_rows);
  823. column->ContentWidthRowsUnfrozen = (ImS16)ImMax(0.0f, column->ContentMaxPosRowsUnfrozen - ref_x_rows);
  824. column->ContentWidthHeadersUsed = (ImS16)ImMax(0.0f, column->ContentMaxPosHeadersUsed - ref_x_headers);
  825. column->ContentWidthHeadersDesired = (ImS16)ImMax(0.0f, column->ContentMaxPosHeadersDesired - ref_x_headers);
  826. if (table->ActiveMaskByIndex & ((ImU64)1 << column_n))
  827. max_pos_x = ImMax(max_pos_x, column->MaxX);
  828. }
  829. // Add an extra 1 pixel so we can see the last column vertical line if it lies on the right-most edge.
  830. inner_window->DC.CursorMaxPos.x = max_pos_x + 1;
  831. if (!(flags & ImGuiTableFlags_NoClipX))
  832. inner_window->DrawList->PopClipRect();
  833. inner_window->ClipRect = inner_window->DrawList->_ClipRectStack.back();
  834. // Draw borders
  835. if ((flags & ImGuiTableFlags_Borders) != 0)
  836. TableDrawBorders(table);
  837. // Flatten channels and merge draw calls
  838. table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0);
  839. TableDrawMergeChannels(table);
  840. // When releasing a column being resized, scroll to keep the resulting column in sight
  841. const float min_column_width = TableGetMinColumnWidth();
  842. if (!(table->Flags & ImGuiTableFlags_ScrollX) && inner_window != outer_window)
  843. {
  844. inner_window->Scroll.x = 0.0f;
  845. }
  846. else if (table->LastResizedColumn != -1 && table->ResizedColumn == -1 && inner_window->ScrollbarX && table->InstanceInteracted == table->InstanceNo)
  847. {
  848. ImGuiTableColumn* column = &table->Columns[table->LastResizedColumn];
  849. if (column->MaxX < table->InnerClipRect.Min.x)
  850. SetScrollFromPosX(inner_window, column->MaxX - inner_window->Pos.x - min_column_width, 1.0f);
  851. else if (column->MaxX > table->InnerClipRect.Max.x)
  852. SetScrollFromPosX(inner_window, column->MaxX - inner_window->Pos.x + min_column_width, 1.0f);
  853. }
  854. // Apply resizing/dragging at the end of the frame
  855. if (table->ResizedColumn != -1)
  856. {
  857. ImGuiTableColumn* column = &table->Columns[table->ResizedColumn];
  858. const float new_x2 = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + TABLE_RESIZE_SEPARATOR_HALF_THICKNESS);
  859. const float new_width = ImFloor(new_x2 - column->MinX);
  860. table->ResizedColumnNextWidth = new_width;
  861. }
  862. // Layout in outer window
  863. inner_window->WorkRect = table->HostWorkRect;
  864. inner_window->SkipItems = table->HostSkipItems;
  865. outer_window->DC.CursorPos = table->OuterRect.Min;
  866. outer_window->DC.ColumnsOffset.x = 0.0f;
  867. if (inner_window != outer_window)
  868. {
  869. // Override EndChild's ItemSize with our own to enable auto-resize on the X axis when possible
  870. float backup_outer_cursor_pos_x = outer_window->DC.CursorPos.x;
  871. EndChild();
  872. outer_window->DC.CursorMaxPos.x = backup_outer_cursor_pos_x + table->ColumnsTotalWidth + 1.0f + inner_window->ScrollbarSizes.x;
  873. }
  874. else
  875. {
  876. PopID();
  877. ImVec2 item_size = table->OuterRect.GetSize();
  878. item_size.x = table->ColumnsTotalWidth;
  879. ItemSize(item_size);
  880. }
  881. // Save settings
  882. if (table->IsSettingsDirty)
  883. TableSaveSettings(table);
  884. // Clear or restore current table, if any
  885. IM_ASSERT(g.CurrentWindow == outer_window);
  886. IM_ASSERT(g.CurrentTable == table);
  887. outer_window->DC.CurrentTable = NULL;
  888. g.CurrentTableStack.pop_back();
  889. g.CurrentTable = g.CurrentTableStack.Size ? g.Tables.GetByIndex(g.CurrentTableStack.back().Index) : NULL;
  890. }
  891. // FIXME-TABLE: This is a mess, need to redesign how we render borders.
  892. void ImGui::TableDrawBorders(ImGuiTable* table)
  893. {
  894. ImGuiWindow* inner_window = table->InnerWindow;
  895. ImGuiWindow* outer_window = table->OuterWindow;
  896. table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0);
  897. if (inner_window->Hidden || !table->HostClipRect.Overlaps(table->InnerClipRect))
  898. return;
  899. ImDrawList* inner_drawlist = inner_window->DrawList;
  900. ImDrawList* outer_drawlist = outer_window->DrawList;
  901. // Draw inner border and resizing feedback
  902. const float draw_y1 = table->OuterRect.Min.y;
  903. float draw_y2_base = (table->FreezeRowsCount >= 1 ? table->OuterRect.Min.y : table->WorkRect.Min.y) + table->LastFirstRowHeight;
  904. float draw_y2_full = table->OuterRect.Max.y;
  905. ImU32 border_base_col;
  906. if (!table->IsUsingHeaders || (table->Flags & ImGuiTableFlags_BordersVFullHeight))
  907. {
  908. draw_y2_base = draw_y2_full;
  909. border_base_col = table->BorderColorLight;
  910. }
  911. else
  912. {
  913. border_base_col = table->BorderColorStrong;
  914. }
  915. if ((table->Flags & ImGuiTableFlags_BordersVOuter) && (table->InnerWindow == table->OuterWindow))
  916. inner_drawlist->AddLine(ImVec2(table->OuterRect.Min.x, draw_y1), ImVec2(table->OuterRect.Min.x, draw_y2_base), border_base_col, 1.0f);
  917. if (table->Flags & ImGuiTableFlags_BordersVInner)
  918. {
  919. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  920. {
  921. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  922. continue;
  923. const int column_n = table->DisplayOrder[order_n];
  924. ImGuiTableColumn* column = &table->Columns[column_n];
  925. const bool is_hovered = (table->HoveredColumnBorder == column_n);
  926. const bool is_resized = (table->ResizedColumn == column_n) && (table->InstanceInteracted == table->InstanceNo);
  927. const bool is_resizable = (column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoDirectResize_)) == 0;
  928. bool draw_right_border = (column->MaxX <= table->InnerClipRect.Max.x) || (is_resized || is_hovered);
  929. if (column->NextActiveColumn == -1 && !is_resizable)
  930. draw_right_border = false;
  931. if (draw_right_border && column->MaxX > column->ClipRect.Min.x) // FIXME-TABLE FIXME-STYLE: Assume BorderSize==1, this is problematic if we want to increase the border size..
  932. {
  933. // Draw in outer window so right-most column won't be clipped
  934. // Always draw full height border when:
  935. // - not using headers
  936. // - user specify ImGuiTableFlags_BordersFullHeight
  937. // - being interacted with
  938. // - on the delimitation of frozen column scrolling
  939. const ImU32 col = is_resized ? GetColorU32(ImGuiCol_SeparatorActive) : is_hovered ? GetColorU32(ImGuiCol_SeparatorHovered) : border_base_col;
  940. float draw_y2 = draw_y2_base;
  941. if (is_hovered || is_resized || (table->FreezeColumnsCount != -1 && table->FreezeColumnsCount == order_n + 1))
  942. draw_y2 = draw_y2_full;
  943. inner_drawlist->AddLine(ImVec2(column->MaxX, draw_y1), ImVec2(column->MaxX, draw_y2), col, 1.0f);
  944. }
  945. }
  946. }
  947. // Draw outer border
  948. if (table->Flags & ImGuiTableFlags_BordersOuter)
  949. {
  950. // Display outer border offset by 1 which is a simple way to display it without adding an extra draw call
  951. // (Without the offset, in outer_window it would be rendered behind cells, because child windows are above their parent.
  952. // In inner_window, it won't reach out over scrollbars. Another weird solution would be to display part of it in inner window,
  953. // and the part that's over scrollbars in the outer window..)
  954. // Either solution currently won't allow us to use a larger border size: the border would clipped.
  955. ImRect outer_border = table->OuterRect;
  956. const ImU32 outer_col = table->BorderColorStrong;
  957. if (inner_window != outer_window)
  958. outer_border.Expand(1.0f);
  959. if ((table->Flags & ImGuiTableFlags_BordersOuter) == ImGuiTableFlags_BordersOuter)
  960. outer_drawlist->AddRect(outer_border.Min, outer_border.Max, outer_col);
  961. else if (table->Flags & ImGuiTableFlags_BordersVOuter)
  962. {
  963. outer_drawlist->AddLine(outer_border.Min, ImVec2(outer_border.Min.x, outer_border.Max.y), outer_col);
  964. outer_drawlist->AddLine(ImVec2(outer_border.Max.x, outer_border.Min.y), outer_border.Max, outer_col);
  965. }
  966. else if (table->Flags & ImGuiTableFlags_BordersHOuter)
  967. {
  968. outer_drawlist->AddLine(outer_border.Min, ImVec2(outer_border.Max.x, outer_border.Min.y), outer_col);
  969. outer_drawlist->AddLine(ImVec2(outer_border.Min.x, outer_border.Max.y), outer_border.Max, outer_col);
  970. }
  971. }
  972. if ((table->Flags & ImGuiTableFlags_BordersHInner) && table->RowPosY2 < table->OuterRect.Max.y)
  973. {
  974. // Draw bottom-most row border
  975. const float border_y = table->RowPosY2;
  976. if (border_y >= table->BackgroundClipRect.Min.y && border_y < table->BackgroundClipRect.Max.y)
  977. inner_drawlist->AddLine(ImVec2(table->BorderX1, border_y), ImVec2(table->BorderX2, border_y), table->BorderColorLight);
  978. }
  979. }
  980. static void TableUpdateColumnsWeightFromWidth(ImGuiTable* table)
  981. {
  982. IM_ASSERT(table->LeftMostStretchedColumnDisplayOrder != -1);
  983. // Measure existing quantity
  984. float visible_weight = 0.0f;
  985. float visible_width = 0.0f;
  986. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  987. {
  988. ImGuiTableColumn* column = &table->Columns[column_n];
  989. if (!column->IsActive || !(column->Flags & ImGuiTableColumnFlags_WidthStretch))
  990. continue;
  991. visible_weight += column->ResizeWeight;
  992. visible_width += column->WidthRequested;
  993. }
  994. IM_ASSERT(visible_weight > 0.0f && visible_width > 0.0f);
  995. // Apply new weights
  996. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  997. {
  998. ImGuiTableColumn* column = &table->Columns[column_n];
  999. if (!column->IsActive || !(column->Flags & ImGuiTableColumnFlags_WidthStretch))
  1000. continue;
  1001. column->ResizeWeight = (column->WidthRequested + 0.0f) / visible_width;
  1002. }
  1003. }
  1004. void ImGui::TableSetColumnWidth(ImGuiTable* table, ImGuiTableColumn* column_0, float column_0_width)
  1005. {
  1006. // Constraints
  1007. float min_width = TableGetMinColumnWidth();
  1008. float max_width_0 = FLT_MAX;
  1009. if (!(table->Flags & ImGuiTableFlags_ScrollX))
  1010. max_width_0 = (table->WorkRect.Max.x - column_0->MinX) - (table->ColumnsActiveCount - (column_0->IndexWithinActiveSet + 1)) * min_width;
  1011. column_0_width = ImClamp(column_0_width, min_width, max_width_0);
  1012. // Compare both requested and actual given width to avoid overwriting requested width when column is stuck (minimum size, bounded)
  1013. if (column_0->WidthGiven == column_0_width || column_0->WidthRequested == column_0_width)
  1014. return;
  1015. ImGuiTableColumn* column_1 = (column_0->NextActiveColumn != -1) ? &table->Columns[column_0->NextActiveColumn] : NULL;
  1016. // In this surprisingly not simple because of how we support mixing Fixed and Stretch columns.
  1017. // When forwarding resize from Wn| to Fn+1| we need to be considerate of the _NoResize flag on Fn+1.
  1018. // FIXME-TABLE: Find a way to rewrite all of this so interactions feel more consistent for the user.
  1019. // Scenarios:
  1020. // - F1 F2 F3 resize from F1| or F2| --> ok: alter ->WidthRequested of Fixed column. Subsequent columns will be offset.
  1021. // - F1 F2 F3 resize from F3| --> ok: alter ->WidthRequested of Fixed column. If active, ScrollX extent can be altered.
  1022. // - F1 F2 W3 resize from F1| or F2| --> ok: alter ->WidthRequested of Fixed column. If active, ScrollX extent can be altered, but it doesn't make much sense as the Weighted column will always be minimal size.
  1023. // - F1 F2 W3 resize from W3| --> ok: no-op (disabled by Resize Rule 1)
  1024. // - W1 W2 W3 resize from W1| or W2| --> FIXME
  1025. // - W1 W2 W3 resize from W3| --> ok: no-op (disabled by Resize Rule 1)
  1026. // - W1 F2 F3 resize from F3| --> ok: no-op (disabled by Resize Rule 1)
  1027. // - W1 F2 resize from F2| --> ok: no-op (disabled by Resize Rule 1)
  1028. // - W1 W2 F3 resize from W1| or W2| --> ok
  1029. // - W1 F2 W3 resize from W1| or F2| --> FIXME
  1030. // - F1 W2 F3 resize from W2| --> ok
  1031. // - W1 F2 F3 resize from W1| --> ok: equivalent to resizing |F2. F3 will not move. (forwarded by Resize Rule 2)
  1032. // - W1 F2 F3 resize from F2| --> FIXME should resize F2, F3 and not have effect on W1 (Stretch columns are _before_ the Fixed column).
  1033. // Rules:
  1034. // - [Resize Rule 1] Can't resize from right of right-most visible column if there is any Stretch column. Implemented in TableUpdateLayout().
  1035. // - [Resize Rule 2] Resizing from right-side of a Stretch column before a fixed column froward sizing to left-side of fixed column.
  1036. // - [Resize Rule 3] If we are are followed by a fixed column and we have a Stretch column before, we need to ensure that our left border won't move.
  1037. if (column_0->Flags & ImGuiTableColumnFlags_WidthFixed)
  1038. {
  1039. // [Resize Rule 3] If we are are followed by a fixed column and we have a Stretch column before, we need to
  1040. // ensure that our left border won't move, which we can do by making sure column_a/column_b resizes cancels each others.
  1041. if (column_1 && (column_1->Flags & ImGuiTableColumnFlags_WidthFixed))
  1042. if (table->LeftMostStretchedColumnDisplayOrder != -1 && table->LeftMostStretchedColumnDisplayOrder < column_0->IndexDisplayOrder)
  1043. {
  1044. // (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
  1045. float column_1_width = ImMax(column_1->WidthRequested - (column_0_width - column_0->WidthRequested), min_width);
  1046. column_0_width = column_0->WidthRequested + column_1->WidthRequested - column_1_width;
  1047. column_1->WidthRequested = column_1_width;
  1048. }
  1049. // Apply
  1050. //IMGUI_DEBUG_LOG("TableSetColumnWidth(%d, %.1f->%.1f)\n", column_0_idx, column_0->WidthRequested, column_0_width);
  1051. column_0->WidthRequested = column_0_width;
  1052. }
  1053. else if (column_0->Flags & ImGuiTableColumnFlags_WidthStretch)
  1054. {
  1055. // [Resize Rule 2]
  1056. if (column_1 && (column_1->Flags & ImGuiTableColumnFlags_WidthFixed))
  1057. {
  1058. float off = (column_0->WidthGiven - column_0_width);
  1059. float column_1_width = column_1->WidthGiven + off;
  1060. column_1->WidthRequested = ImMax(min_width, column_1_width);
  1061. return;
  1062. }
  1063. // (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
  1064. float column_1_width = ImMax(column_1->WidthRequested - (column_0_width - column_0->WidthRequested), min_width);
  1065. column_0_width = column_0->WidthRequested + column_1->WidthRequested - column_1_width;
  1066. column_1->WidthRequested = column_1_width;
  1067. column_0->WidthRequested = column_0_width;
  1068. TableUpdateColumnsWeightFromWidth(table);
  1069. }
  1070. table->IsSettingsDirty = true;
  1071. }
  1072. // Columns where the contents didn't stray off their local clip rectangle can be merged into a same draw command.
  1073. // To achieve this we merge their clip rect and make them contiguous in the channel list so they can be merged.
  1074. // So here we'll reorder the draw cmd which can be merged, by arranging them into a maximum of 4 distinct groups:
  1075. //
  1076. // 1 group: 2 groups: 2 groups: 4 groups:
  1077. // [ 0. ] no freeze [ 0. ] row freeze [ 01 ] col freeze [ 01 ] row+col freeze
  1078. // [ .. ] or no scroll [ 1. ] and v-scroll [ .. ] and h-scroll [ 23 ] and v+h-scroll
  1079. //
  1080. // Each column itself can use 1 channel (row freeze disabled) or 2 channels (row freeze enabled).
  1081. // When the contents of a column didn't stray off its limit, we move its channels into the corresponding group
  1082. // based on its position (within frozen rows/columns set or not).
  1083. // At the end of the operation our 1-4 groups will each have a ImDrawCmd using the same ClipRect, and they will be merged by the DrawSplitter.Merge() call.
  1084. //
  1085. // Column channels will not be merged into one of the 1-4 groups in the following cases:
  1086. // - The contents stray off its clipping rectangle (we only compare the MaxX value, not the MinX value).
  1087. // Direct ImDrawList calls won't be noticed so if you use them make sure the ImGui:: bounds matches, by e.g. calling SetCursorScreenPos().
  1088. // - The channel uses more than one draw command itself (we drop all our merging stuff here.. we could do better but it's going to be rare)
  1089. //
  1090. // This function is particularly tricky to understand.. take a breath.
  1091. void ImGui::TableDrawMergeChannels(ImGuiTable* table)
  1092. {
  1093. ImGuiContext& g = *GImGui;
  1094. ImDrawListSplitter* splitter = &table->DrawSplitter;
  1095. const bool is_frozen_v = (table->FreezeRowsCount > 0);
  1096. const bool is_frozen_h = (table->FreezeColumnsCount > 0);
  1097. int merge_set_mask = 0;
  1098. int merge_set_channels_count[4] = { 0 };
  1099. ImU64 merge_set_channels_mask[4] = { 0 };
  1100. ImRect merge_set_clip_rect[4];
  1101. for (int n = 0; n < IM_ARRAYSIZE(merge_set_clip_rect); n++)
  1102. merge_set_clip_rect[n] = ImVec4(+FLT_MAX, +FLT_MAX, -FLT_MAX, -FLT_MAX);
  1103. bool merge_set_all_fit_within_inner_rect = (table->Flags & ImGuiTableFlags_NoHostExtendY) == 0;
  1104. // 1. Scan channels and take note of those who can be merged
  1105. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  1106. {
  1107. if (!(table->ActiveMaskByDisplayOrder & ((ImU64)1 << order_n)))
  1108. continue;
  1109. const int column_n = table->DisplayOrder[order_n];
  1110. ImGuiTableColumn* column = &table->Columns[column_n];
  1111. const int merge_set_sub_count = is_frozen_v ? 2 : 1;
  1112. for (int merge_set_sub_n = 0; merge_set_sub_n < merge_set_sub_count; merge_set_sub_n++)
  1113. {
  1114. const int channel_no = (merge_set_sub_n == 0) ? column->DrawChannelRowsBeforeFreeze : column->DrawChannelRowsAfterFreeze;
  1115. // Don't attempt to merge if there are multiple calls within the column
  1116. ImDrawChannel* src_channel = &splitter->_Channels[channel_no];
  1117. if (src_channel->_CmdBuffer.Size > 0 && src_channel->_CmdBuffer.back().ElemCount == 0)
  1118. src_channel->_CmdBuffer.pop_back();
  1119. if (src_channel->_CmdBuffer.Size != 1)
  1120. continue;
  1121. // Find out the width of this merge set and check if it will fit in our column.
  1122. float width_contents;
  1123. if (merge_set_sub_count == 1) // No row freeze (same as testing !is_frozen_v)
  1124. width_contents = ImMax(column->ContentWidthRowsUnfrozen, column->ContentWidthHeadersUsed);
  1125. else if (merge_set_sub_n == 0) // Row freeze: use width before freeze
  1126. width_contents = ImMax(column->ContentWidthRowsFrozen, column->ContentWidthHeadersUsed);
  1127. else // Row freeze: use width after freeze
  1128. width_contents = column->ContentWidthRowsUnfrozen;
  1129. if (width_contents > column->WidthGiven && !(column->Flags & ImGuiTableColumnFlags_NoClipX))
  1130. continue;
  1131. const int dst_merge_set_n = (is_frozen_h && column_n < table->FreezeColumnsCount ? 0 : 2) + (is_frozen_v ? merge_set_sub_n : 1);
  1132. IM_ASSERT(merge_set_channels_count[dst_merge_set_n] < (int)sizeof(merge_set_channels_mask[dst_merge_set_n]) * 8);
  1133. merge_set_mask |= (1 << dst_merge_set_n);
  1134. merge_set_channels_mask[dst_merge_set_n] |= (ImU64)1 << channel_no;
  1135. merge_set_channels_count[dst_merge_set_n]++;
  1136. merge_set_clip_rect[dst_merge_set_n].Add(src_channel->_CmdBuffer[0].ClipRect);
  1137. // If we end with a single set and hosted by the outer window, we'll attempt to merge our draw command with
  1138. // the existing outer window command. But we can only do so if our columns all fit within the expected clip rect,
  1139. // otherwise clipping will be incorrect when ScrollX is disabled.
  1140. // FIXME-TABLE FIXME-WORKRECT: We are wasting a merge opportunity on tables without scrolling if column don't fit within host clip rect, solely because of the half-padding difference between window->WorkRect and window->InnerClipRect
  1141. // 2019/10/22: (1) This is breaking table_2_draw_calls but I cannot seem to repro what it is attempting to fix...
  1142. // cf git fce2e8dc "Fixed issue with clipping when outerwindow==innerwindow / support ScrollH without ScrollV."
  1143. // 2019/10/22: (2) Clamping code in TableUpdateLayout() seemingly made this not necessary...
  1144. #if 0
  1145. if (column->MinX < table->InnerClipRect.Min.x || column->MaxX > table->InnerClipRect.Max.x)
  1146. merge_set_all_fit_within_inner_rect = false;
  1147. #endif
  1148. }
  1149. // Invalidate current draw channel (we don't clear DrawChannelBeforeRowFreeze/DrawChannelAfterRowFreeze solely to facilitate debugging)
  1150. column->DrawChannelCurrent = -1;
  1151. }
  1152. // 2. Rewrite channel list in our preferred order
  1153. if (merge_set_mask != 0)
  1154. {
  1155. // Use shared temporary storage so the allocation gets amortized
  1156. g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - 1);
  1157. ImDrawChannel* dst_tmp = g.DrawChannelsTempMergeBuffer.Data;
  1158. ImU64 remaining_mask = ((splitter->_Count < 64) ? ((ImU64)1 << splitter->_Count) - 1 : ~(ImU64)0) & ~1;
  1159. const bool may_extend_clip_rect_to_host_rect = ImIsPowerOfTwo(merge_set_mask);
  1160. for (int merge_set_n = 0; merge_set_n < 4; merge_set_n++)
  1161. if (merge_set_channels_count[merge_set_n])
  1162. {
  1163. ImU64 merge_channels_mask = merge_set_channels_mask[merge_set_n];
  1164. ImRect merge_clip_rect = merge_set_clip_rect[merge_set_n];
  1165. if (may_extend_clip_rect_to_host_rect)
  1166. {
  1167. //GetOverlayDrawList()->AddRect(table->HostClipRect.Min, table->HostClipRect.Max, IM_COL32(255, 0, 0, 200), 0.0f, ~0, 3.0f);
  1168. //GetOverlayDrawList()->AddRect(table->InnerClipRect.Min, table->InnerClipRect.Max, IM_COL32(0, 255, 0, 200), 0.0f, ~0, 1.0f);
  1169. //GetOverlayDrawList()->AddRect(merge_clip_rect.Min, merge_clip_rect.Max, IM_COL32(255, 0, 0, 200), 0.0f, ~0, 2.0f);
  1170. merge_clip_rect.Add(merge_set_all_fit_within_inner_rect ? table->HostClipRect : table->InnerClipRect);
  1171. //GetOverlayDrawList()->AddRect(merge_clip_rect.Min, merge_clip_rect.Max, IM_COL32(0, 255, 0, 200));
  1172. }
  1173. remaining_mask &= ~merge_channels_mask;
  1174. for (int n = 0; n < splitter->_Count && merge_channels_mask != 0; n++)
  1175. {
  1176. // Copy + overwrite new clip rect
  1177. const ImU64 n_mask = (ImU64)1 << n;
  1178. if ((merge_channels_mask & n_mask) == 0)
  1179. continue;
  1180. ImDrawChannel* channel = &splitter->_Channels[n];
  1181. IM_ASSERT(channel->_CmdBuffer.Size == 1 && merge_clip_rect.Contains(ImRect(channel->_CmdBuffer[0].ClipRect)));
  1182. channel->_CmdBuffer[0].ClipRect = *(ImVec4*)&merge_clip_rect;
  1183. memcpy(dst_tmp++, channel, sizeof(ImDrawChannel));
  1184. merge_channels_mask &= ~n_mask;
  1185. }
  1186. }
  1187. // Append channels that we didn't reorder at the end of the list
  1188. for (int n = 0; n < splitter->_Count && remaining_mask != 0; n++)
  1189. {
  1190. const ImU64 n_mask = (ImU64)1 << n;
  1191. if ((remaining_mask & n_mask) == 0)
  1192. continue;
  1193. ImDrawChannel* channel = &splitter->_Channels[n];
  1194. memcpy(dst_tmp++, channel, sizeof(ImDrawChannel));
  1195. remaining_mask &= ~n_mask;
  1196. }
  1197. IM_ASSERT(dst_tmp == g.DrawChannelsTempMergeBuffer.Data + g.DrawChannelsTempMergeBuffer.Size);
  1198. memcpy(splitter->_Channels.Data + 1, g.DrawChannelsTempMergeBuffer.Data, (splitter->_Count - 1) * sizeof(ImDrawChannel));
  1199. }
  1200. // 3. Actually merge (channels using the same clip rect will be contiguous and naturally merged)
  1201. splitter->Merge(table->InnerWindow->DrawList);
  1202. }
  1203. // We use a default parameter of 'init_width_or_weight == -1'
  1204. // ImGuiTableColumnFlags_WidthFixed, width <= 0 --> init width == auto
  1205. // ImGuiTableColumnFlags_WidthFixed, width > 0 --> init width == manual
  1206. // ImGuiTableColumnFlags_WidthStretch, weight < 0 --> init weight == 1.0f
  1207. // ImGuiTableColumnFlags_WidthStretch, weight >= 0 --> init weight == custom
  1208. // Use a different API?
  1209. void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, float init_width_or_weight, ImGuiID user_id)
  1210. {
  1211. ImGuiContext& g = *GImGui;
  1212. ImGuiTable* table = g.CurrentTable;
  1213. IM_ASSERT(table != NULL && "Need to call TableSetupColumn() after BeginTable()!");
  1214. IM_ASSERT(!table->IsLayoutLocked && "Need to call call TableSetupColumn() before first row!");
  1215. IM_ASSERT(table->DeclColumnsCount >= 0 && table->DeclColumnsCount < table->ColumnsCount && "Called TableSetupColumn() too many times!");
  1216. ImGuiTableColumn* column = &table->Columns[table->DeclColumnsCount];
  1217. table->DeclColumnsCount++;
  1218. // When passing a width automatically enforce WidthFixed policy (vs TableFixColumnFlags would default to WidthAlwaysAutoResize)
  1219. // (we write down to FlagsIn which is a little misleading, another solution would be to pass init_width_or_weight to TableFixColumnFlags)
  1220. if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0)
  1221. if ((table->Flags & ImGuiTableFlags_SizingPolicyFixedX) && (init_width_or_weight > 0.0f))
  1222. flags |= ImGuiTableColumnFlags_WidthFixed;
  1223. column->UserID = user_id;
  1224. column->FlagsIn = flags;
  1225. column->Flags = TableFixColumnFlags(table, column->FlagsIn);
  1226. flags = column->Flags;
  1227. // Initialize defaults
  1228. if (table->IsInitializing && !table->IsSettingsLoaded)
  1229. {
  1230. // Init width or weight
  1231. // Disable auto-fit if a default fixed width has been specified
  1232. if ((flags & ImGuiTableColumnFlags_WidthFixed) && init_width_or_weight > 0.0f)
  1233. {
  1234. column->WidthRequested = init_width_or_weight;
  1235. column->AutoFitQueue = 0x00;
  1236. }
  1237. if (flags & ImGuiTableColumnFlags_WidthStretch)
  1238. {
  1239. IM_ASSERT(init_width_or_weight < 0.0f || init_width_or_weight > 0.0f);
  1240. column->ResizeWeight = (init_width_or_weight < 0.0f ? 1.0f : init_width_or_weight);
  1241. }
  1242. else
  1243. {
  1244. column->ResizeWeight = 1.0f;
  1245. }
  1246. // Init default visibility/sort state
  1247. if (flags & ImGuiTableColumnFlags_DefaultHide)
  1248. column->IsActive = column->NextIsActive = false;
  1249. if (flags & ImGuiTableColumnFlags_DefaultSort)
  1250. column->SortOrder = 0; // Multiple columns using _DefaultSort will be reordered when building the sort specs.
  1251. }
  1252. // Store name (append with zero-terminator in contiguous buffer)
  1253. IM_ASSERT(column->NameOffset == -1);
  1254. if (label != NULL)
  1255. {
  1256. column->NameOffset = (ImS16)table->ColumnsNames.size();
  1257. table->ColumnsNames.append(label, label + strlen(label) + 1);
  1258. }
  1259. }
  1260. // Starts into the first cell of a new row
  1261. void ImGui::TableNextRow(ImGuiTableRowFlags row_flags, float min_row_height)
  1262. {
  1263. ImGuiContext& g = *GImGui;
  1264. ImGuiTable* table = g.CurrentTable;
  1265. if (table->CurrentRow == -1)
  1266. TableUpdateLayout(table);
  1267. else if (table->IsInsideRow)
  1268. TableEndRow(table);
  1269. table->LastRowFlags = table->RowFlags;
  1270. table->RowFlags = row_flags;
  1271. TableBeginRow(table);
  1272. // We honor min_row_height requested by user, but cannot guarantee per-row maximum height,
  1273. // because that would essentially require a unique clipping rectangle per-cell.
  1274. table->RowPosY2 += table->CellPaddingY * 2.0f;
  1275. table->RowPosY2 = ImMax(table->RowPosY2, table->RowPosY1 + min_row_height);
  1276. TableBeginCell(table, 0);
  1277. }
  1278. // [Internal]
  1279. void ImGui::TableBeginRow(ImGuiTable* table)
  1280. {
  1281. ImGuiWindow* window = table->InnerWindow;
  1282. IM_ASSERT(!table->IsInsideRow);
  1283. // New row
  1284. table->CurrentRow++;
  1285. table->CurrentColumn = -1;
  1286. table->RowBgColor = IM_COL32_DISABLE;
  1287. table->IsInsideRow = true;
  1288. // Begin frozen rows
  1289. float next_y1 = table->RowPosY2;
  1290. if (table->CurrentRow == 0 && table->FreezeRowsCount > 0)
  1291. next_y1 = window->DC.CursorPos.y = table->OuterRect.Min.y;
  1292. table->RowPosY1 = table->RowPosY2 = next_y1;
  1293. table->RowTextBaseline = 0.0f;
  1294. window->DC.PrevLineTextBaseOffset = 0.0f;
  1295. window->DC.CursorMaxPos.y = next_y1;
  1296. // Making the header BG color non-transparent will allow us to overlay it multiple times when handling smooth dragging.
  1297. if (table->RowFlags & ImGuiTableRowFlags_Headers)
  1298. {
  1299. table->RowBgColor = GetColorU32(ImGuiCol_TableHeaderBg);
  1300. if (table->CurrentRow == 0)
  1301. table->IsUsingHeaders = true;
  1302. }
  1303. }
  1304. // [Internal]
  1305. void ImGui::TableEndRow(ImGuiTable* table)
  1306. {
  1307. ImGuiContext& g = *GImGui;
  1308. ImGuiWindow* window = g.CurrentWindow;
  1309. IM_ASSERT(window == table->InnerWindow);
  1310. IM_ASSERT(table->IsInsideRow);
  1311. TableEndCell(table);
  1312. // Position cursor at the bottom of our row so it can be used for e.g. clipping calculation.
  1313. // However it is likely that the next call to TableBeginCell() will reposition the cursor to take account of vertical padding.
  1314. window->DC.CursorPos.y = table->RowPosY2;
  1315. // Row background fill
  1316. const float bg_y1 = table->RowPosY1;
  1317. const float bg_y2 = table->RowPosY2;
  1318. if (table->CurrentRow == 0)
  1319. table->LastFirstRowHeight = bg_y2 - bg_y1;
  1320. if (table->CurrentRow >= 0 && bg_y2 >= table->InnerClipRect.Min.y && bg_y1 <= table->InnerClipRect.Max.y)
  1321. {
  1322. // Decide of background color for the row
  1323. ImU32 bg_col = 0;
  1324. if (table->RowBgColor != IM_COL32_DISABLE)
  1325. bg_col = table->RowBgColor;
  1326. else if (table->Flags & ImGuiTableFlags_RowBg)
  1327. bg_col = GetColorU32((table->RowBgColorCounter & 1) ? ImGuiCol_TableRowBgAlt : ImGuiCol_TableRowBg);
  1328. // Decide of top border color
  1329. ImU32 border_col = 0;
  1330. if (table->CurrentRow != 0 || table->InnerWindow == table->OuterWindow)
  1331. {
  1332. if (table->Flags & ImGuiTableFlags_BordersHInner)
  1333. {
  1334. //if (table->CurrentRow == 0 && table->InnerWindow == table->OuterWindow)
  1335. // border_col = table->BorderOuterColor;
  1336. //else
  1337. if (table->CurrentRow > 0)// && !(table->LastRowFlags & ImGuiTableRowFlags_Headers))
  1338. border_col = (table->LastRowFlags & ImGuiTableRowFlags_Headers) ? table->BorderColorStrong : table->BorderColorLight;
  1339. }
  1340. else
  1341. {
  1342. //if (table->RowFlags & ImGuiTableRowFlags_Headers)
  1343. // border_col = table->BorderOuterColor;
  1344. }
  1345. }
  1346. if (bg_col != 0 || border_col != 0)
  1347. table->DrawSplitter.SetCurrentChannel(window->DrawList, 0);
  1348. // Draw background
  1349. // We soft/cpu clip this so all backgrounds and borders can share the same clipping rectangle
  1350. if (bg_col)
  1351. {
  1352. ImRect bg_rect(table->WorkRect.Min.x, bg_y1, table->WorkRect.Max.x, bg_y2);
  1353. bg_rect.ClipWith(table->BackgroundClipRect);
  1354. if (bg_rect.Min.y < bg_rect.Max.y)
  1355. window->DrawList->AddRectFilledMultiColor(bg_rect.Min, bg_rect.Max, bg_col, bg_col, bg_col, bg_col);
  1356. }
  1357. // Draw top border
  1358. const float border_y = bg_y1;
  1359. if (border_col && border_y >= table->BackgroundClipRect.Min.y && border_y < table->BackgroundClipRect.Max.y)
  1360. window->DrawList->AddLine(ImVec2(table->BorderX1, border_y), ImVec2(table->BorderX2, border_y), border_col);
  1361. }
  1362. const bool unfreeze_rows = (table->CurrentRow + 1 == table->FreezeRowsCount && table->FreezeRowsCount > 0);
  1363. // Draw bottom border (always strong)
  1364. const bool draw_separating_border = unfreeze_rows;// || (table->RowFlags & ImGuiTableRowFlags_Headers);
  1365. if (draw_separating_border)
  1366. if (bg_y2 >= table->BackgroundClipRect.Min.y && bg_y2 < table->BackgroundClipRect.Max.y)
  1367. window->DrawList->AddLine(ImVec2(table->BorderX1, bg_y2), ImVec2(table->BorderX2, bg_y2), table->BorderColorStrong);
  1368. // End frozen rows (when we are past the last frozen row line, teleport cursor and alter clipping rectangle)
  1369. // We need to do that in TableEndRow() instead of TableBeginRow() so the list clipper can mark end of row and get the new cursor position.
  1370. if (unfreeze_rows)
  1371. {
  1372. IM_ASSERT(table->IsFreezeRowsPassed == false);
  1373. table->IsFreezeRowsPassed = true;
  1374. table->DrawSplitter.SetCurrentChannel(window->DrawList, 0);
  1375. ImRect r;
  1376. r.Min.x = table->InnerClipRect.Min.x;
  1377. r.Min.y = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y);
  1378. r.Max.x = table->InnerClipRect.Max.x;
  1379. r.Max.y = window->InnerClipRect.Max.y;
  1380. table->BackgroundClipRect = r;
  1381. float row_height = table->RowPosY2 - table->RowPosY1;
  1382. table->RowPosY2 = window->DC.CursorPos.y = table->WorkRect.Min.y + table->RowPosY2 - table->OuterRect.Min.y;
  1383. table->RowPosY1 = table->RowPosY2 - row_height;
  1384. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1385. {
  1386. ImGuiTableColumn* column = &table->Columns[column_n];
  1387. column->DrawChannelCurrent = column->DrawChannelRowsAfterFreeze;
  1388. column->ClipRect.Min.y = r.Min.y;
  1389. }
  1390. }
  1391. if (!(table->RowFlags & ImGuiTableRowFlags_Headers))
  1392. table->RowBgColorCounter++;
  1393. table->IsInsideRow = false;
  1394. }
  1395. // [Internal] Called by TableNextRow()TableNextCell()!
  1396. // This is called a lot, so we need to be mindful of unnecessary overhead.
  1397. // FIXME-TABLE FIXME-OPT: Could probably shortcut some things for non-active or clipped columns.
  1398. void ImGui::TableBeginCell(ImGuiTable* table, int column_no)
  1399. {
  1400. table->CurrentColumn = column_no;
  1401. ImGuiTableColumn* column = &table->Columns[column_no];
  1402. ImGuiWindow* window = table->InnerWindow;
  1403. float start_x = (table->RowFlags & ImGuiTableRowFlags_Headers) ? column->StartXHeaders : column->StartXRows;
  1404. if (column->Flags & ImGuiTableColumnFlags_IndentEnable)
  1405. start_x += window->DC.Indent.x - table->HostIndentX;
  1406. window->DC.LastItemId = 0;
  1407. window->DC.CursorPos.x = start_x;
  1408. window->DC.CursorPos.y = table->RowPosY1 + table->CellPaddingY;
  1409. window->DC.CursorMaxPos.x = window->DC.CursorPos.x;
  1410. window->DC.ColumnsOffset.x = start_x - window->Pos.x - window->DC.Indent.x; // FIXME-WORKRECT
  1411. window->DC.CurrLineTextBaseOffset = table->RowTextBaseline;
  1412. window->WorkRect.Min.y = window->DC.CursorPos.y;
  1413. window->WorkRect.Min.x = column->MinX + table->CellPaddingX1;
  1414. window->WorkRect.Max.x = column->MaxX - table->CellPaddingX2;
  1415. // To allow ImGuiListClipper to function we propagate our row height
  1416. if (!column->IsActive)
  1417. window->DC.CursorPos.y = ImMax(window->DC.CursorPos.y, table->RowPosY2);
  1418. window->SkipItems = column->SkipItems;
  1419. if (table->Flags & ImGuiTableFlags_NoClipX)
  1420. {
  1421. table->DrawSplitter.SetCurrentChannel(window->DrawList, 1);
  1422. }
  1423. else
  1424. {
  1425. table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
  1426. //window->ClipRect = column->ClipRect;
  1427. //IM_ASSERT(column->ClipRect.Max.x > column->ClipRect.Min.x && column->ClipRect.Max.y > column->ClipRect.Min.y);
  1428. //window->DrawList->_ClipRectStack.back() = ImVec4(column->ClipRect.Min.x, column->ClipRect.Min.y, column->ClipRect.Max.x, column->ClipRect.Max.y);
  1429. //window->DrawList->UpdateClipRect();
  1430. window->DrawList->PopClipRect();
  1431. window->DrawList->PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false);
  1432. //IMGUI_DEBUG_LOG("%d (%.0f,%.0f)(%.0f,%.0f)\n", column_no, column->ClipRect.Min.x, column->ClipRect.Min.y, column->ClipRect.Max.x, column->ClipRect.Max.y);
  1433. window->ClipRect = window->DrawList->_ClipRectStack.back();
  1434. }
  1435. }
  1436. // [Internal] Called by TableNextRow()/TableNextCell()!
  1437. void ImGui::TableEndCell(ImGuiTable* table)
  1438. {
  1439. ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
  1440. ImGuiWindow* window = table->InnerWindow;
  1441. // Report maximum position so we can infer content size per column.
  1442. float* p_max_pos_x;
  1443. if (table->RowFlags & ImGuiTableRowFlags_Headers)
  1444. p_max_pos_x = &column->ContentMaxPosHeadersUsed; // Useful in case user submit contents in header row that is not a TableHeader() call
  1445. else
  1446. p_max_pos_x = table->IsFreezeRowsPassed ? &column->ContentMaxPosRowsUnfrozen : &column->ContentMaxPosRowsFrozen;
  1447. *p_max_pos_x = ImMax(*p_max_pos_x, window->DC.CursorMaxPos.x);
  1448. table->RowPosY2 = ImMax(table->RowPosY2, window->DC.CursorMaxPos.y + table->CellPaddingY);
  1449. // Propagate text baseline for the entire row
  1450. // FIXME-TABLE: Here we propagate text baseline from the last line of the cell.. instead of the first one.
  1451. table->RowTextBaseline = ImMax(table->RowTextBaseline, window->DC.PrevLineTextBaseOffset);
  1452. }
  1453. // Append into the next cell
  1454. // FIXME-TABLE: Wrapping to next row should be optional?
  1455. bool ImGui::TableNextCell()
  1456. {
  1457. ImGuiContext& g = *GImGui;
  1458. ImGuiTable* table = g.CurrentTable;
  1459. if (table->CurrentColumn != -1 && table->CurrentColumn + 1 < table->ColumnsCount)
  1460. {
  1461. TableEndCell(table);
  1462. TableBeginCell(table, table->CurrentColumn + 1);
  1463. }
  1464. else
  1465. {
  1466. TableNextRow();
  1467. }
  1468. int column_n = table->CurrentColumn;
  1469. return (table->VisibleMaskByIndex & ((ImU64)1 << column_n)) != 0;
  1470. }
  1471. const char* ImGui::TableGetColumnName(int column_n)
  1472. {
  1473. ImGuiContext& g = *GImGui;
  1474. ImGuiTable* table = g.CurrentTable;
  1475. if (!table)
  1476. return NULL;
  1477. if (column_n < 0)
  1478. column_n = table->CurrentColumn;
  1479. return TableGetColumnName(table, column_n);
  1480. }
  1481. bool ImGui::TableGetColumnIsVisible(int column_n)
  1482. {
  1483. ImGuiContext& g = *GImGui;
  1484. ImGuiTable* table = g.CurrentTable;
  1485. if (!table)
  1486. return false;
  1487. if (column_n < 0)
  1488. column_n = table->CurrentColumn;
  1489. return (table->VisibleMaskByIndex & ((ImU64)1 << column_n)) != 0;
  1490. }
  1491. int ImGui::TableGetColumnIndex()
  1492. {
  1493. ImGuiContext& g = *GImGui;
  1494. ImGuiTable* table = g.CurrentTable;
  1495. if (!table)
  1496. return 0;
  1497. return table->CurrentColumn;
  1498. }
  1499. bool ImGui::TableSetColumnIndex(int column_idx)
  1500. {
  1501. ImGuiContext& g = *GImGui;
  1502. ImGuiTable* table = g.CurrentTable;
  1503. if (!table)
  1504. return false;
  1505. if (table->CurrentColumn != column_idx)
  1506. {
  1507. if (table->CurrentColumn != -1)
  1508. TableEndCell(table);
  1509. IM_ASSERT(column_idx >= 0 && table->ColumnsCount);
  1510. TableBeginCell(table, column_idx);
  1511. }
  1512. return (table->VisibleMaskByIndex & ((ImU64)1 << column_idx)) != 0;
  1513. }
  1514. // Return the cell rectangle based on currently known height.
  1515. // Important: we generally don't know our row height until the end of the row, so Max.y will be incorrect in many situations.
  1516. // The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it.
  1517. ImRect ImGui::TableGetCellRect()
  1518. {
  1519. ImGuiContext& g = *GImGui;
  1520. ImGuiTable* table = g.CurrentTable;
  1521. ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
  1522. return ImRect(column->MinX, table->RowPosY1, column->MaxX, table->RowPosY2);
  1523. }
  1524. const char* ImGui::TableGetColumnName(ImGuiTable* table, int column_no)
  1525. {
  1526. ImGuiTableColumn* column = &table->Columns[column_no];
  1527. if (column->NameOffset == -1)
  1528. return NULL;
  1529. return &table->ColumnsNames.Buf[column->NameOffset];
  1530. }
  1531. void ImGui::TableSetColumnAutofit(ImGuiTable* table, int column_no)
  1532. {
  1533. // Disable clipping then auto-fit, will take 2 frames
  1534. // (we don't take a shortcut for unclipped columns to reduce inconsistencies when e.g. resizing multiple columns)
  1535. ImGuiTableColumn* column = &table->Columns[column_no];
  1536. column->CannotSkipItemsQueue = (1 << 0);
  1537. column->AutoFitQueue = (1 << 1);
  1538. }
  1539. void ImGui::PushTableBackground()
  1540. {
  1541. ImGuiContext& g = *GImGui;
  1542. ImGuiWindow* window = g.CurrentWindow;
  1543. ImGuiTable* table = g.CurrentTable;
  1544. table->DrawSplitter.SetCurrentChannel(window->DrawList, 0);
  1545. PushClipRect(table->HostClipRect.Min, table->HostClipRect.Max, false);
  1546. }
  1547. void ImGui::PopTableBackground()
  1548. {
  1549. ImGuiContext& g = *GImGui;
  1550. ImGuiWindow* window = g.CurrentWindow;
  1551. ImGuiTable* table = g.CurrentTable;
  1552. ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
  1553. table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
  1554. PopClipRect();
  1555. }
  1556. // Output context menu into current window (generally a popup)
  1557. // FIXME-TABLE: Ideally this should be writable by the user. Full programmatic access to that data?
  1558. void ImGui::TableDrawContextMenu(ImGuiTable* table, int selected_column_n)
  1559. {
  1560. ImGuiContext& g = *GImGui;
  1561. ImGuiWindow* window = g.CurrentWindow;
  1562. if (window->SkipItems)
  1563. return;
  1564. bool want_separator = false;
  1565. selected_column_n = ImClamp(selected_column_n, -1, table->ColumnsCount - 1);
  1566. // Sizing
  1567. if (table->Flags & ImGuiTableFlags_Resizable)
  1568. {
  1569. if (ImGuiTableColumn* selected_column = (selected_column_n != -1) ? &table->Columns[selected_column_n] : NULL)
  1570. {
  1571. const bool can_resize = !(selected_column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_WidthStretch)) && selected_column->IsActive;
  1572. if (MenuItem("Size column to fit", NULL, false, can_resize))
  1573. TableSetColumnAutofit(table, selected_column_n);
  1574. }
  1575. if (MenuItem("Size all columns to fit", NULL))
  1576. {
  1577. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1578. {
  1579. ImGuiTableColumn* column = &table->Columns[column_n];
  1580. if (column->IsActive)
  1581. TableSetColumnAutofit(table, column_n);
  1582. }
  1583. }
  1584. want_separator = true;
  1585. }
  1586. // Ordering
  1587. if (table->Flags & ImGuiTableFlags_Reorderable)
  1588. {
  1589. if (MenuItem("Reset order", NULL, false, !table->IsDefaultDisplayOrder))
  1590. table->IsResetDisplayOrderRequest = true;
  1591. want_separator = true;
  1592. }
  1593. // Hiding / Visibility
  1594. if (table->Flags & ImGuiTableFlags_Hideable)
  1595. {
  1596. if (want_separator)
  1597. Separator();
  1598. want_separator = false;
  1599. PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true);
  1600. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1601. {
  1602. ImGuiTableColumn* column = &table->Columns[column_n];
  1603. const char* name = TableGetColumnName(table, column_n);
  1604. if (name == NULL)
  1605. name = "<Unknown>";
  1606. // Make sure we can't hide the last active column
  1607. bool menu_item_active = (column->Flags & ImGuiTableColumnFlags_NoHide) ? false : true;
  1608. if (column->IsActive && table->ColumnsActiveCount <= 1)
  1609. menu_item_active = false;
  1610. if (MenuItem(name, NULL, column->IsActive, menu_item_active))
  1611. column->NextIsActive = !column->IsActive;
  1612. }
  1613. PopItemFlag();
  1614. }
  1615. }
  1616. // This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn().
  1617. // The intent is that advanced users willing to create customized headers would not need to use this helper and may create their own.
  1618. // However presently this function uses too many internal structures/calls.
  1619. void ImGui::TableAutoHeaders()
  1620. {
  1621. ImGuiContext& g = *GImGui;
  1622. ImGuiWindow* window = g.CurrentWindow;
  1623. ImGuiTable* table = g.CurrentTable;
  1624. IM_ASSERT(table != NULL && "Need to call TableAutoHeaders() after BeginTable()!");
  1625. const int columns_count = table->ColumnsCount;
  1626. // Calculate row height (for the unlikely case that labels may be are multi-line)
  1627. float row_height = GetTextLineHeight();
  1628. for (int column_n = 0; column_n < columns_count; column_n++)
  1629. if (TableGetColumnIsVisible(column_n))
  1630. row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(column_n)).y);
  1631. row_height += g.Style.CellPadding.y * 2.0f;
  1632. // Open row
  1633. TableNextRow(ImGuiTableRowFlags_Headers, row_height);
  1634. if (table->HostSkipItems) // Merely an optimization
  1635. return;
  1636. // This for loop is constructed to not make use of internal functions,
  1637. // as this is intended to be a base template to copy and build from.
  1638. int open_context_popup = INT_MAX;
  1639. for (int column_n = 0; column_n < columns_count; column_n++)
  1640. {
  1641. if (!TableSetColumnIndex(column_n))
  1642. continue;
  1643. const char* name = TableGetColumnName(column_n);
  1644. // [DEBUG] Test custom user elements
  1645. #if 0
  1646. if (column_n < 2)
  1647. {
  1648. static bool b[2] = {};
  1649. PushID(column_n);
  1650. PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
  1651. Checkbox("##", &b[column_n]);
  1652. PopStyleVar();
  1653. PopID();
  1654. SameLine(0.0f, g.Style.ItemInnerSpacing.x);
  1655. }
  1656. #endif
  1657. // [DEBUG]
  1658. //if (g.IO.KeyCtrl) { static char buf[32]; name = buf; ImGuiTableColumn* c = &table->Columns[column_n]; if (c->Flags & ImGuiTableColumnFlags_WidthStretch) ImFormatString(buf, 32, "%.3f>%.1f", c->ResizeWeight, c->WidthGiven); else ImFormatString(buf, 32, "%.1f", c->WidthGiven); }
  1659. // Push an id to allow unnamed labels (generally accidental, but let's behave nicely with them)
  1660. PushID(table->InstanceNo * table->ColumnsCount + column_n);
  1661. TableHeader(name);
  1662. PopID();
  1663. // We don't use BeginPopupContextItem() because we want the popup to stay up even after the column is hidden
  1664. if (IsMouseReleased(1) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup))
  1665. open_context_popup = column_n;
  1666. }
  1667. // FIXME-TABLE: This is not user-land code any more + need to explain WHY this is here!
  1668. window->SkipItems = table->HostSkipItems;
  1669. // Allow opening popup from the right-most section after the last column
  1670. // FIXME-TABLE: This is not user-land code any more... perhaps instead we should expose hovered column.
  1671. // and allow some sort of row-centric IsItemHovered() for full flexibility?
  1672. float unused_x1 = table->WorkRect.Min.x;
  1673. if (table->RightMostActiveColumn != -1)
  1674. unused_x1 = ImMax(unused_x1, table->Columns[table->RightMostActiveColumn].MaxX);
  1675. if (unused_x1 < table->WorkRect.Max.x)
  1676. {
  1677. // FIXME: We inherit ClipRect/SkipItem from last submitted column (active or not), let's temporarily override it.
  1678. // Because we don't perform any rendering here we just overwrite window->ClipRect used by logic.
  1679. window->ClipRect = table->InnerClipRect;
  1680. ImVec2 backup_cursor_max_pos = window->DC.CursorMaxPos;
  1681. window->DC.CursorPos = ImVec2(unused_x1, table->RowPosY1);
  1682. ImVec2 size = ImVec2(table->WorkRect.Max.x - window->DC.CursorPos.x, table->RowPosY2 - table->RowPosY1);
  1683. if (size.x > 0.0f && size.y > 0.0f)
  1684. {
  1685. InvisibleButton("##RemainingSpace", size);
  1686. window->DC.CursorPos.y -= g.Style.ItemSpacing.y;
  1687. window->DC.CursorMaxPos = backup_cursor_max_pos; // Don't feed back into the width of the Header row
  1688. // We don't use BeginPopupContextItem() because we want the popup to stay up even after the column is hidden
  1689. if (IsMouseReleased(1) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup))
  1690. open_context_popup = -1;
  1691. }
  1692. window->ClipRect = window->DrawList->_ClipRectStack.back();
  1693. }
  1694. // Open Context Menu
  1695. if (open_context_popup != INT_MAX)
  1696. if (table->Flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
  1697. {
  1698. table->IsContextPopupOpen = true;
  1699. table->ContextPopupColumn = (ImS8)open_context_popup;
  1700. table->InstanceInteracted = table->InstanceNo;
  1701. OpenPopup("##TableContextMenu");
  1702. }
  1703. }
  1704. // Emit a column header (text + optional sort order)
  1705. // We cpu-clip text here so that all columns headers can be merged into a same draw call.
  1706. // FIXME-TABLE: Should hold a selection state.
  1707. // FIXME-TABLE: Style confusion between CellPadding.y and FramePadding.y
  1708. void ImGui::TableHeader(const char* label)
  1709. {
  1710. ImGuiContext& g = *GImGui;
  1711. ImGuiWindow* window = g.CurrentWindow;
  1712. if (window->SkipItems)
  1713. return;
  1714. ImGuiTable* table = g.CurrentTable;
  1715. IM_ASSERT(table != NULL && "Need to call TableAutoHeaders() after BeginTable()!");
  1716. IM_ASSERT(table->CurrentColumn != -1);
  1717. const int column_n = table->CurrentColumn;
  1718. ImGuiTableColumn* column = &table->Columns[column_n];
  1719. // Label
  1720. if (label == NULL)
  1721. label = "";
  1722. const char* label_end = FindRenderedTextEnd(label);
  1723. ImVec2 label_size = CalcTextSize(label, label_end, true);
  1724. ImVec2 label_pos = window->DC.CursorPos;
  1725. // If we already got a row height, there's use that.
  1726. ImRect cell_r = TableGetCellRect();
  1727. float label_height = ImMax(label_size.y, cell_r.GetHeight() - g.Style.CellPadding.y * 2.0f);
  1728. //GetForegroundDrawList()->AddRect(cell_r.Min, cell_r.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG]
  1729. ImRect work_r = cell_r;
  1730. work_r.Min.x = window->DC.CursorPos.x;
  1731. work_r.Max.y = work_r.Min.y + label_height;
  1732. float ellipsis_max = work_r.Max.x;
  1733. // Selectable
  1734. PushID(label);
  1735. // FIXME-TABLE: Fix when padding are disabled.
  1736. //window->DC.CursorPos.x = column->MinX + table->CellPadding.x;
  1737. // Keep header highlighted when context menu is open. (FIXME-TABLE: however we cannot assume the ID of said popup if it has been created by the user...)
  1738. const bool selected = (table->IsContextPopupOpen && table->ContextPopupColumn == column_n && table->InstanceInteracted == table->InstanceNo);
  1739. const bool pressed = Selectable("", selected, ImGuiSelectableFlags_DrawHoveredWhenHeld, ImVec2(0.0f, label_height));
  1740. const bool held = IsItemActive();
  1741. if (held)
  1742. table->HeldHeaderColumn = (ImS8)column_n;
  1743. window->DC.CursorPos.y -= g.Style.ItemSpacing.y * 0.5f;
  1744. // Drag and drop: re-order columns. Frozen columns are not reorderable.
  1745. // FIXME-TABLE: Scroll request while reordering a column and it lands out of the scrolling zone.
  1746. if (held && (table->Flags & ImGuiTableFlags_Reorderable) && IsMouseDragging(0) && !g.DragDropActive)
  1747. {
  1748. // While moving a column it will jump on the other side of the mouse, so we also test for MouseDelta.x
  1749. table->ReorderColumn = (ImS8)column_n;
  1750. table->InstanceInteracted = table->InstanceNo;
  1751. if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < cell_r.Min.x)
  1752. if (column->PrevActiveColumn != -1 && (column->IndexWithinActiveSet < table->FreezeColumnsRequest) == (table->Columns[column->PrevActiveColumn].IndexWithinActiveSet < table->FreezeColumnsRequest))
  1753. table->ReorderColumnDir = -1;
  1754. if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > cell_r.Max.x)
  1755. if (column->NextActiveColumn != -1 && (column->IndexWithinActiveSet < table->FreezeColumnsRequest) == (table->Columns[column->NextActiveColumn].IndexWithinActiveSet < table->FreezeColumnsRequest))
  1756. table->ReorderColumnDir = +1;
  1757. }
  1758. // Sort order arrow
  1759. float w_arrow = 0.0f;
  1760. float w_sort_text = 0.0f;
  1761. if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort))
  1762. {
  1763. const float ARROW_SCALE = 0.75f;
  1764. w_arrow = ImFloor(g.FontSize * ARROW_SCALE + g.Style.FramePadding.x);// table->CellPadding.x);
  1765. if (column->SortOrder != -1)
  1766. {
  1767. w_sort_text = 0.0f;
  1768. char sort_order_suf[8];
  1769. if (column->SortOrder > 0)
  1770. {
  1771. ImFormatString(sort_order_suf, IM_ARRAYSIZE(sort_order_suf), "%d", column->SortOrder + 1);
  1772. w_sort_text = g.Style.ItemInnerSpacing.x + CalcTextSize(sort_order_suf).x;
  1773. }
  1774. float x = ImMax(cell_r.Min.x, work_r.Max.x - w_arrow - w_sort_text);
  1775. ellipsis_max -= w_arrow + w_sort_text;
  1776. float y = label_pos.y;
  1777. ImU32 col = GetColorU32(ImGuiCol_Text);
  1778. if (column->SortOrder > 0)
  1779. {
  1780. PushStyleColor(ImGuiCol_Text, GetColorU32(ImGuiCol_Text, 0.70f));
  1781. RenderText(ImVec2(x + g.Style.ItemInnerSpacing.x, y), sort_order_suf);
  1782. PopStyleColor();
  1783. x += w_sort_text;
  1784. }
  1785. RenderArrow(window->DrawList, ImVec2(x, y), col, column->SortDirection == ImGuiSortDirection_Ascending ? ImGuiDir_Down : ImGuiDir_Up, ARROW_SCALE);
  1786. }
  1787. // Handle clicking on column header to adjust Sort Order
  1788. if (pressed && table->ReorderColumn != column_n)
  1789. TableSortSpecsClickColumn(table, column, g.IO.KeyShift);
  1790. }
  1791. // Render clipped label
  1792. // Clipping here ensure that in the majority of situations, all our header cells will be merged into a single draw call.
  1793. //window->DrawList->AddCircleFilled(ImVec2(ellipsis_max, label_pos.y), 40, IM_COL32_WHITE);
  1794. RenderTextEllipsis(window->DrawList, label_pos, ImVec2(ellipsis_max, label_pos.y + label_height + g.Style.FramePadding.y), ellipsis_max, ellipsis_max, label, label_end, &label_size);
  1795. // We feed our unclipped width to the column without writing on CursorMaxPos, so that column is still considering for merging.
  1796. // FIXME-TABLE: Clarify policies of how label width and potential decorations (arrows) fit into auto-resize of the column
  1797. float max_pos_x = label_pos.x + label_size.x + w_sort_text + w_arrow;
  1798. column->ContentMaxPosHeadersUsed = ImMax(column->ContentMaxPosHeadersUsed, work_r.Max.x);// ImMin(max_pos_x, work_r.Max.x));
  1799. column->ContentMaxPosHeadersDesired = ImMax(column->ContentMaxPosHeadersDesired, max_pos_x);
  1800. PopID();
  1801. }
  1802. void ImGui::TableSortSpecsClickColumn(ImGuiTable* table, ImGuiTableColumn* clicked_column, bool add_to_existing_sort_orders)
  1803. {
  1804. if (!(table->Flags & ImGuiTableFlags_MultiSortable))
  1805. add_to_existing_sort_orders = false;
  1806. ImS8 sort_order_max = 0;
  1807. if (add_to_existing_sort_orders)
  1808. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1809. sort_order_max = ImMax(sort_order_max, table->Columns[column_n].SortOrder);
  1810. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1811. {
  1812. ImGuiTableColumn* column = &table->Columns[column_n];
  1813. if (column == clicked_column)
  1814. {
  1815. // Set new sort direction and sort order
  1816. // - If the PreferSortDescending flag is set, we will default to a Descending direction on the first click.
  1817. // - Note that the PreferSortAscending flag is never checked, it is essentially the default and therefore a no-op.
  1818. // - Note that the NoSortAscending/NoSortDescending flags are processed in TableSortSpecsSanitize(), and they may change/revert
  1819. // the value of SortDirection. We could technically also do it here but it would be unnecessary and duplicate code.
  1820. if (column->SortOrder == -1)
  1821. column->SortDirection = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? (ImS8)ImGuiSortDirection_Descending : (ImU8)(ImGuiSortDirection_Ascending);
  1822. else
  1823. column->SortDirection = (ImU8)((column->SortDirection == ImGuiSortDirection_Ascending) ? ImGuiSortDirection_Descending : ImGuiSortDirection_Ascending);
  1824. if (column->SortOrder == -1 || !add_to_existing_sort_orders)
  1825. column->SortOrder = add_to_existing_sort_orders ? sort_order_max + 1 : 0;
  1826. }
  1827. else
  1828. {
  1829. if (!add_to_existing_sort_orders)
  1830. column->SortOrder = -1;
  1831. }
  1832. TableFixColumnSortDirection(column);
  1833. }
  1834. table->IsSettingsDirty = true;
  1835. table->IsSortSpecsDirty = true;
  1836. }
  1837. // Return NULL if no sort specs.
  1838. // You can sort your data again when 'SpecsChanged == true'. It will be true with sorting specs have changed since last call, or the first time.
  1839. // Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable()!
  1840. const ImGuiTableSortSpecs* ImGui::TableGetSortSpecs()
  1841. {
  1842. ImGuiContext& g = *GImGui;
  1843. ImGuiTable* table = g.CurrentTable;
  1844. IM_ASSERT(table != NULL);
  1845. if (!(table->Flags & ImGuiTableFlags_Sortable))
  1846. return NULL;
  1847. // Flatten sort specs into user facing data
  1848. const bool was_dirty = table->IsSortSpecsDirty;
  1849. if (was_dirty)
  1850. {
  1851. TableSortSpecsSanitize(table);
  1852. // Write output
  1853. table->SortSpecsData.resize(table->SortSpecsCount);
  1854. table->SortSpecs.ColumnsMask = 0x00;
  1855. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1856. {
  1857. ImGuiTableColumn* column = &table->Columns[column_n];
  1858. if (column->SortOrder == -1)
  1859. continue;
  1860. ImGuiTableSortSpecsColumn* sort_spec = &table->SortSpecsData[column->SortOrder];
  1861. sort_spec->ColumnUserID = column->UserID;
  1862. sort_spec->ColumnIndex = (ImU8)column_n;
  1863. sort_spec->SortOrder = (ImU8)column->SortOrder;
  1864. sort_spec->SortSign = (column->SortDirection == ImGuiSortDirection_Ascending) ? +1 : -1;
  1865. sort_spec->SortDirection = column->SortDirection;
  1866. table->SortSpecs.ColumnsMask |= (ImU64)1 << column_n;
  1867. }
  1868. }
  1869. // User facing data
  1870. table->SortSpecs.Specs = table->SortSpecsData.Data;
  1871. table->SortSpecs.SpecsCount = table->SortSpecsData.Size;
  1872. table->SortSpecs.SpecsChanged = was_dirty;
  1873. table->IsSortSpecsDirty = false;
  1874. return table->SortSpecs.SpecsCount ? &table->SortSpecs : NULL;
  1875. }
  1876. bool ImGui::TableGetColumnIsSorted(int column_n)
  1877. {
  1878. ImGuiContext& g = *GImGui;
  1879. ImGuiTable* table = g.CurrentTable;
  1880. if (!table)
  1881. return false;
  1882. if (column_n < 0)
  1883. column_n = table->CurrentColumn;
  1884. ImGuiTableColumn* column = &table->Columns[column_n];
  1885. return (column->SortOrder != -1);
  1886. }
  1887. void ImGui::TableSortSpecsSanitize(ImGuiTable* table)
  1888. {
  1889. // Clear SortOrder from hidden column and verify that there's no gap or duplicate.
  1890. int sort_order_count = 0;
  1891. ImU64 sort_order_mask = 0x00;
  1892. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1893. {
  1894. ImGuiTableColumn* column = &table->Columns[column_n];
  1895. if (column->SortOrder != -1 && !column->IsActive)
  1896. column->SortOrder = -1;
  1897. if (column->SortOrder == -1)
  1898. continue;
  1899. sort_order_count++;
  1900. sort_order_mask |= ((ImU64)1 << column->SortOrder);
  1901. IM_ASSERT(sort_order_count < (int)sizeof(sort_order_mask) * 8);
  1902. }
  1903. const bool need_fix_linearize = ((ImU64)1 << sort_order_count) != (sort_order_mask + 1);
  1904. const bool need_fix_single_sort_order = (sort_order_count > 1) && !(table->Flags & ImGuiTableFlags_MultiSortable);
  1905. if (need_fix_linearize || need_fix_single_sort_order)
  1906. {
  1907. ImU64 fixed_mask = 0x00;
  1908. for (int sort_n = 0; sort_n < sort_order_count; sort_n++)
  1909. {
  1910. // Fix: Rewrite sort order fields if needed so they have no gap or duplicate.
  1911. // (e.g. SortOrder 0 disappeared, SortOrder 1..2 exists --> rewrite then as SortOrder 0..1)
  1912. int column_with_smallest_sort_order = -1;
  1913. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1914. if ((fixed_mask & ((ImU64)1 << (ImU64)column_n)) == 0 && table->Columns[column_n].SortOrder != -1)
  1915. if (column_with_smallest_sort_order == -1 || table->Columns[column_n].SortOrder < table->Columns[column_with_smallest_sort_order].SortOrder)
  1916. column_with_smallest_sort_order = column_n;
  1917. IM_ASSERT(column_with_smallest_sort_order != -1);
  1918. fixed_mask |= ((ImU64)1 << column_with_smallest_sort_order);
  1919. table->Columns[column_with_smallest_sort_order].SortOrder = (ImS8)sort_n;
  1920. // Fix: Make sure only one column has a SortOrder if ImGuiTableFlags_MultiSortable is not set.
  1921. if (need_fix_single_sort_order)
  1922. {
  1923. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1924. if (column_n != column_with_smallest_sort_order)
  1925. table->Columns[column_n].SortOrder = -1;
  1926. break;
  1927. }
  1928. }
  1929. }
  1930. // Fallback default sort order (if no column has the ImGuiTableColumnFlags_DefaultSort flag)
  1931. if (sort_order_count == 0 && table->IsInitializing)
  1932. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1933. {
  1934. ImGuiTableColumn* column = &table->Columns[column_n];
  1935. if (!(column->Flags & ImGuiTableColumnFlags_NoSort) && column->IsActive)
  1936. {
  1937. sort_order_count = 1;
  1938. column->SortOrder = 0;
  1939. break;
  1940. }
  1941. }
  1942. table->SortSpecsCount = (ImS8)sort_order_count;
  1943. }
  1944. //-------------------------------------------------------------------------
  1945. // TABLE - .ini settings
  1946. //-------------------------------------------------------------------------
  1947. // [Init] 1: TableSettingsHandler_ReadXXXX() Load and parse .ini file into TableSettings.
  1948. // [Main] 2: TableLoadSettings() When table is created, bind Table to TableSettings, serialize TableSettings data into Table.
  1949. // [Main] 3: TableSaveSettings() When table properties are modified, serialize Table data into bound or new TableSettings, mark .ini as dirty.
  1950. // [Main] 4: TableSettingsHandler_WriteAll() When .ini file is dirty (which can come from other source), save TableSettings into .ini file.
  1951. //-------------------------------------------------------------------------
  1952. static ImGuiTableSettings* CreateTableSettings(ImGuiID id, int columns_count)
  1953. {
  1954. ImGuiContext& g = *GImGui;
  1955. ImGuiTableSettings* settings = g.SettingsTables.alloc_chunk(sizeof(ImGuiTableSettings) + (size_t)columns_count * sizeof(ImGuiTableColumnSettings));
  1956. IM_PLACEMENT_NEW(settings) ImGuiTableSettings();
  1957. ImGuiTableColumnSettings* settings_column = settings->GetColumnSettings();
  1958. for (int n = 0; n < columns_count; n++, settings_column++)
  1959. IM_PLACEMENT_NEW(settings_column) ImGuiTableColumnSettings();
  1960. settings->ID = id;
  1961. settings->ColumnsCount = settings->ColumnsCountMax = (ImS8)columns_count;
  1962. return settings;
  1963. }
  1964. static ImGuiTableSettings* FindTableSettingsByID(ImGuiID id)
  1965. {
  1966. // FIXME-OPT: Might want to store a lookup map for this?
  1967. ImGuiContext& g = *GImGui;
  1968. for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings))
  1969. if (settings->ID == id)
  1970. return settings;
  1971. return NULL;
  1972. }
  1973. ImGuiTableSettings* ImGui::TableFindSettings(ImGuiTable* table)
  1974. {
  1975. if (table->SettingsOffset == -1)
  1976. return NULL;
  1977. ImGuiContext& g = *GImGui;
  1978. ImGuiTableSettings* settings = g.SettingsTables.ptr_from_offset(table->SettingsOffset);
  1979. IM_ASSERT(settings->ID == table->ID);
  1980. if (settings->ColumnsCountMax < table->ColumnsCount)
  1981. {
  1982. settings->ID = 0; // Ditch storage if we won't fit because of a count change
  1983. return NULL;
  1984. }
  1985. return settings;
  1986. }
  1987. void ImGui::TableSaveSettings(ImGuiTable* table)
  1988. {
  1989. table->IsSettingsDirty = false;
  1990. if (table->Flags & ImGuiTableFlags_NoSavedSettings)
  1991. return;
  1992. // Bind or create settings data
  1993. ImGuiContext& g = *GImGui;
  1994. ImGuiTableSettings* settings = TableFindSettings(table);
  1995. if (settings == NULL)
  1996. {
  1997. settings = CreateTableSettings(table->ID, table->ColumnsCount);
  1998. table->SettingsOffset = g.SettingsTables.offset_from_ptr(settings);
  1999. }
  2000. settings->ColumnsCount = (ImS8)table->ColumnsCount;
  2001. // Serialize ImGuiTableSettings/ImGuiTableColumnSettings --> ImGuiTable/ImGuiTableColumn
  2002. IM_ASSERT(settings->ID == table->ID);
  2003. IM_ASSERT(settings->ColumnsCount == table->ColumnsCount && settings->ColumnsCountMax >= settings->ColumnsCount);
  2004. ImGuiTableColumn* column = table->Columns.Data;
  2005. ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings();
  2006. // FIXME-TABLE: Logic to avoid saving default widths?
  2007. settings->SaveFlags = ImGuiTableFlags_Resizable;
  2008. for (int n = 0; n < table->ColumnsCount; n++, column++, column_settings++)
  2009. {
  2010. //column_settings->WidthOrWeight = column->WidthRequested; // FIXME-WIP
  2011. column_settings->Index = (ImS8)n;
  2012. column_settings->DisplayOrder = column->IndexDisplayOrder;
  2013. column_settings->SortOrder = column->SortOrder;
  2014. column_settings->SortDirection = column->SortDirection;
  2015. column_settings->Visible = column->IsActive;
  2016. // We skip saving some data in the .ini file when they are unnecessary to restore our state
  2017. // FIXME-TABLE: We don't have logic to easily compare SortOrder to DefaultSortOrder yet.
  2018. if (column->IndexDisplayOrder != n)
  2019. settings->SaveFlags |= ImGuiTableFlags_Reorderable;;
  2020. if (column_settings->SortOrder != -1)
  2021. settings->SaveFlags |= ImGuiTableFlags_Sortable;
  2022. if (column_settings->Visible != ((column->Flags & ImGuiTableColumnFlags_DefaultHide) == 0))
  2023. settings->SaveFlags |= ImGuiTableFlags_Hideable;
  2024. }
  2025. settings->SaveFlags &= table->Flags;
  2026. MarkIniSettingsDirty();
  2027. }
  2028. void ImGui::TableLoadSettings(ImGuiTable* table)
  2029. {
  2030. ImGuiContext& g = *GImGui;
  2031. table->IsSettingsRequestLoad = false;
  2032. if (table->Flags & ImGuiTableFlags_NoSavedSettings)
  2033. return;
  2034. // Bind settings
  2035. ImGuiTableSettings* settings;
  2036. if (table->SettingsOffset == -1)
  2037. {
  2038. settings = FindTableSettingsByID(table->ID);
  2039. if (settings == NULL)
  2040. return;
  2041. table->SettingsOffset = g.SettingsTables.offset_from_ptr(settings);
  2042. }
  2043. else
  2044. {
  2045. settings = g.SettingsTables.ptr_from_offset(table->SettingsOffset);
  2046. }
  2047. table->IsSettingsLoaded = true;
  2048. settings->SaveFlags = table->Flags;
  2049. // Serialize ImGuiTable/ImGuiTableColumn --> ImGuiTableSettings/ImGuiTableColumnSettings
  2050. ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings();
  2051. for (int data_n = 0; data_n < settings->ColumnsCount; data_n++, column_settings++)
  2052. {
  2053. int column_n = column_settings->Index;
  2054. if (column_n < 0 || column_n >= table->ColumnsCount)
  2055. continue;
  2056. ImGuiTableColumn* column = &table->Columns[column_n];
  2057. //column->WidthRequested = column_settings->WidthOrWeight; // FIXME-WIP
  2058. if (column_settings->DisplayOrder != -1)
  2059. column->IndexDisplayOrder = column_settings->DisplayOrder;
  2060. if (column_settings->SortOrder != -1)
  2061. {
  2062. column->SortOrder = column_settings->SortOrder;
  2063. column->SortDirection = column_settings->SortDirection;
  2064. }
  2065. column->IsActive = column->NextIsActive = column_settings->Visible;
  2066. }
  2067. // FIXME-TABLE: Need to validate .ini data
  2068. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  2069. table->DisplayOrder[table->Columns[column_n].IndexDisplayOrder] = (ImU8)column_n;
  2070. }
  2071. void* ImGui::TableSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name)
  2072. {
  2073. ImGuiID id = 0;
  2074. int columns_count = 0;
  2075. if (sscanf(name, "0x%08X,%d", &id, &columns_count) < 2)
  2076. return NULL;
  2077. return CreateTableSettings(id, columns_count);
  2078. }
  2079. void ImGui::TableSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line)
  2080. {
  2081. // "Column 0 UserID=0x42AD2D21 Width=100 Visible=1 Order=0 Sort=0v"
  2082. ImGuiTableSettings* settings = (ImGuiTableSettings*)entry;
  2083. int column_n = 0, r = 0, n = 0;
  2084. if (sscanf(line, "Column %d%n", &column_n, &r) == 1) { line = ImStrSkipBlank(line + r); } else { return; }
  2085. if (column_n < 0 || column_n >= settings->ColumnsCount)
  2086. return;
  2087. char c = 0;
  2088. ImGuiTableColumnSettings* column = settings->GetColumnSettings() + column_n;
  2089. column->Index = (ImS8)column_n;
  2090. if (sscanf(line, "UserID=0x%08X%n", (ImU32*)&n, &r) == 1) { line = ImStrSkipBlank(line + r); column->UserID = (ImGuiID)n; }
  2091. if (sscanf(line, "Width=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); /* .. */ settings->SaveFlags |= ImGuiTableFlags_Resizable; }
  2092. if (sscanf(line, "Visible=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->Visible = (ImU8)n; settings->SaveFlags |= ImGuiTableFlags_Hideable; }
  2093. if (sscanf(line, "Order=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->DisplayOrder = (ImS8)n; settings->SaveFlags |= ImGuiTableFlags_Reorderable; }
  2094. if (sscanf(line, "Sort=%d%c%n", &n, &c, &r) == 2) { line = ImStrSkipBlank(line + r); column->SortOrder = (ImS8)n; column->SortDirection = (c == '^') ? ImGuiSortDirection_Descending : ImGuiSortDirection_Ascending; settings->SaveFlags |= ImGuiTableFlags_Sortable; }
  2095. }
  2096. void ImGui::TableSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf)
  2097. {
  2098. ImGuiContext& g = *ctx;
  2099. for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings))
  2100. {
  2101. if (settings->ID == 0) // Skip ditched settings
  2102. continue;
  2103. // TableSaveSettings() may clear some of those flags when we establish that the data can be stripped (e.g. Order was unchanged)
  2104. const bool save_size = (settings->SaveFlags & ImGuiTableFlags_Resizable) != 0;
  2105. const bool save_visible = (settings->SaveFlags & ImGuiTableFlags_Hideable) != 0;
  2106. const bool save_order = (settings->SaveFlags & ImGuiTableFlags_Reorderable) != 0;
  2107. const bool save_sort = (settings->SaveFlags & ImGuiTableFlags_Sortable) != 0;
  2108. if (!save_size && !save_visible && !save_order && !save_sort)
  2109. continue;
  2110. buf->reserve(buf->size() + 30 + settings->ColumnsCount * 50); // ballpark reserve
  2111. buf->appendf("[%s][0x%08X,%d]\n", handler->TypeName, settings->ID, settings->ColumnsCount);
  2112. ImGuiTableColumnSettings* column = settings->GetColumnSettings();
  2113. for (int column_n = 0; column_n < settings->ColumnsCount; column_n++, column++)
  2114. {
  2115. // "Column 0 UserID=0x42AD2D21 Width=100 Visible=1 Order=0 Sort=0v"
  2116. if (column->UserID != 0)
  2117. buf->appendf("Column %-2d UserID=%08X", column_n, column->UserID);
  2118. else
  2119. buf->appendf("Column %-2d", column_n);
  2120. if (save_size) buf->appendf(" Width=%d", 0);// (int)settings_column->WidthOrWeight); // FIXME-TABLE
  2121. if (save_visible) buf->appendf(" Visible=%d", column->Visible);
  2122. if (save_order) buf->appendf(" Order=%d", column->DisplayOrder);
  2123. if (save_sort && column->SortOrder != -1) buf->appendf(" Sort=%d%c", column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? 'v' : '^');
  2124. buf->append("\n");
  2125. }
  2126. buf->append("\n");
  2127. }
  2128. }
  2129. //-------------------------------------------------------------------------
  2130. // TABLE - Debugging
  2131. //-------------------------------------------------------------------------
  2132. // - DebugNodeTable() [Internal]
  2133. //-------------------------------------------------------------------------
  2134. #ifndef IMGUI_DISABLE_METRICS_WINDOW
  2135. void ImGui::DebugNodeTable(ImGuiTable* table)
  2136. {
  2137. char buf[256];
  2138. char* p = buf;
  2139. const char* buf_end = buf + IM_ARRAYSIZE(buf);
  2140. ImFormatString(p, buf_end - p, "Table 0x%08X (%d columns, in '%s')", table->ID, table->ColumnsCount, table->OuterWindow->Name);
  2141. bool open = TreeNode(table, "%s", buf);
  2142. if (IsItemHovered())
  2143. GetForegroundDrawList()->AddRect(table->OuterRect.Min, table->OuterRect.Max, IM_COL32(255, 255, 0, 255));
  2144. if (open)
  2145. {
  2146. for (int n = 0; n < table->ColumnsCount; n++)
  2147. {
  2148. ImGuiTableColumn* column = &table->Columns[n];
  2149. const char* name = TableGetColumnName(table, n);
  2150. BulletText("Column %d order %d name '%s': +%.1f to +%.1f\n"
  2151. "Active: %d, Clipped: %d, DrawChannels: %d,%d\n"
  2152. "WidthGiven/Requested: %.1f/%.1f, Weight: %.2f\n"
  2153. "UserID: 0x%08X, Flags: 0x%04X: %s%s%s%s..",
  2154. n, column->IndexDisplayOrder, name ? name : "NULL", column->MinX - table->WorkRect.Min.x, column->MaxX - table->WorkRect.Min.x,
  2155. column->IsActive, column->IsClipped, column->DrawChannelRowsBeforeFreeze, column->DrawChannelRowsAfterFreeze,
  2156. column->WidthGiven, column->WidthRequested, column->ResizeWeight,
  2157. column->UserID, column->Flags,
  2158. (column->Flags & ImGuiTableColumnFlags_WidthFixed) ? "WidthFixed " : "",
  2159. (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? "WidthStretch " : "",
  2160. (column->Flags & ImGuiTableColumnFlags_WidthAlwaysAutoResize) ? "WidthAlwaysAutoResize " : "",
  2161. (column->Flags & ImGuiTableColumnFlags_NoResize) ? "NoResize " : "");
  2162. }
  2163. ImGuiTableSettings* settings = TableFindSettings(table);
  2164. if (settings && TreeNode("Settings"))
  2165. {
  2166. BulletText("SaveFlags: 0x%08X", settings->SaveFlags);
  2167. BulletText("ColumnsCount: %d (max %d)", settings->ColumnsCount, settings->ColumnsCountMax);
  2168. for (int n = 0; n < settings->ColumnsCount; n++)
  2169. {
  2170. ImGuiTableColumnSettings* column_settings = &settings->GetColumnSettings()[n];
  2171. BulletText("Column %d Order %d SortOrder %d Visible %d UserID 0x%08X WidthOrWeight %.3f",
  2172. n, column_settings->DisplayOrder, column_settings->SortOrder, column_settings->Visible, column_settings->UserID, column_settings->WidthOrWeight);
  2173. }
  2174. TreePop();
  2175. }
  2176. TreePop();
  2177. }
  2178. }
  2179. #endif // #ifndef IMGUI_DISABLE_METRICS_WINDOW
  2180. //-------------------------------------------------------------------------
  2181. //-------------------------------------------------------------------------
  2182. // [SECTION] Widgets: Columns, BeginColumns, EndColumns, etc.
  2183. // (This is a legacy API, prefer using BeginTable/EndTable!)
  2184. //-------------------------------------------------------------------------
  2185. // - SetWindowClipRectBeforeSetChannel() [Internal]
  2186. // - GetColumnIndex()
  2187. // - GetColumnsCount()
  2188. // - GetColumnOffset()
  2189. // - GetColumnWidth()
  2190. // - SetColumnOffset()
  2191. // - SetColumnWidth()
  2192. // - PushColumnClipRect() [Internal]
  2193. // - PushColumnsBackground() [Internal]
  2194. // - PopColumnsBackground() [Internal]
  2195. // - FindOrCreateColumns() [Internal]
  2196. // - GetColumnsID() [Internal]
  2197. // - BeginColumns()
  2198. // - NextColumn()
  2199. // - EndColumns()
  2200. // - Columns()
  2201. //-------------------------------------------------------------------------
  2202. // [Internal] Small optimization to avoid calls to PopClipRect/SetCurrentChannel/PushClipRect in sequences,
  2203. // they would meddle many times with the underlying ImDrawCmd.
  2204. // Instead, we do a preemptive overwrite of clipping rectangle _without_ altering the command-buffer and let
  2205. // the subsequent single call to SetCurrentChannel() does it things once.
  2206. void ImGui::SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect)
  2207. {
  2208. ImVec4 clip_rect_vec4 = clip_rect.ToVec4();
  2209. window->ClipRect = clip_rect;
  2210. window->DrawList->_CmdHeader.ClipRect = clip_rect_vec4;
  2211. window->DrawList->_ClipRectStack.Data[window->DrawList->_ClipRectStack.Size - 1] = clip_rect_vec4;
  2212. }
  2213. int ImGui::GetColumnIndex()
  2214. {
  2215. ImGuiWindow* window = GetCurrentWindowRead();
  2216. return window->DC.CurrentColumns ? window->DC.CurrentColumns->Current : 0;
  2217. }
  2218. int ImGui::GetColumnsCount()
  2219. {
  2220. ImGuiWindow* window = GetCurrentWindowRead();
  2221. return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1;
  2222. }
  2223. float ImGui::GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm)
  2224. {
  2225. return offset_norm * (columns->OffMaxX - columns->OffMinX);
  2226. }
  2227. float ImGui::GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset)
  2228. {
  2229. return offset / (columns->OffMaxX - columns->OffMinX);
  2230. }
  2231. static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f;
  2232. static float GetDraggedColumnOffset(ImGuiOldColumns* columns, int column_index)
  2233. {
  2234. // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing
  2235. // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning.
  2236. ImGuiContext& g = *GImGui;
  2237. ImGuiWindow* window = g.CurrentWindow;
  2238. IM_ASSERT(column_index > 0); // We are not supposed to drag column 0.
  2239. IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index));
  2240. float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x;
  2241. x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing);
  2242. if ((columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths))
  2243. x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing);
  2244. return x;
  2245. }
  2246. float ImGui::GetColumnOffset(int column_index)
  2247. {
  2248. ImGuiWindow* window = GetCurrentWindowRead();
  2249. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2250. if (columns == NULL)
  2251. return 0.0f;
  2252. if (column_index < 0)
  2253. column_index = columns->Current;
  2254. IM_ASSERT(column_index < columns->Columns.Size);
  2255. const float t = columns->Columns[column_index].OffsetNorm;
  2256. const float x_offset = ImLerp(columns->OffMinX, columns->OffMaxX, t);
  2257. return x_offset;
  2258. }
  2259. static float GetColumnWidthEx(ImGuiOldColumns* columns, int column_index, bool before_resize = false)
  2260. {
  2261. if (column_index < 0)
  2262. column_index = columns->Current;
  2263. float offset_norm;
  2264. if (before_resize)
  2265. offset_norm = columns->Columns[column_index + 1].OffsetNormBeforeResize - columns->Columns[column_index].OffsetNormBeforeResize;
  2266. else
  2267. offset_norm = columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm;
  2268. return ImGui::GetColumnOffsetFromNorm(columns, offset_norm);
  2269. }
  2270. float ImGui::GetColumnWidth(int column_index)
  2271. {
  2272. ImGuiContext& g = *GImGui;
  2273. ImGuiWindow* window = g.CurrentWindow;
  2274. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2275. if (columns == NULL)
  2276. return GetContentRegionAvail().x;
  2277. if (column_index < 0)
  2278. column_index = columns->Current;
  2279. return GetColumnOffsetFromNorm(columns, columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm);
  2280. }
  2281. void ImGui::SetColumnOffset(int column_index, float offset)
  2282. {
  2283. ImGuiContext& g = *GImGui;
  2284. ImGuiWindow* window = g.CurrentWindow;
  2285. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2286. IM_ASSERT(columns != NULL);
  2287. if (column_index < 0)
  2288. column_index = columns->Current;
  2289. IM_ASSERT(column_index < columns->Columns.Size);
  2290. const bool preserve_width = !(columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths) && (column_index < columns->Count - 1);
  2291. const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f;
  2292. if (!(columns->Flags & ImGuiOldColumnFlags_NoForceWithinWindow))
  2293. offset = ImMin(offset, columns->OffMaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index));
  2294. columns->Columns[column_index].OffsetNorm = GetColumnNormFromOffset(columns, offset - columns->OffMinX);
  2295. if (preserve_width)
  2296. SetColumnOffset(column_index + 1, offset + ImMax(g.Style.ColumnsMinSpacing, width));
  2297. }
  2298. void ImGui::SetColumnWidth(int column_index, float width)
  2299. {
  2300. ImGuiWindow* window = GetCurrentWindowRead();
  2301. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2302. IM_ASSERT(columns != NULL);
  2303. if (column_index < 0)
  2304. column_index = columns->Current;
  2305. SetColumnOffset(column_index + 1, GetColumnOffset(column_index) + width);
  2306. }
  2307. void ImGui::PushColumnClipRect(int column_index)
  2308. {
  2309. ImGuiWindow* window = GetCurrentWindowRead();
  2310. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2311. if (column_index < 0)
  2312. column_index = columns->Current;
  2313. ImGuiOldColumnData* column = &columns->Columns[column_index];
  2314. PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false);
  2315. }
  2316. // Get into the columns background draw command (which is generally the same draw command as before we called BeginColumns)
  2317. void ImGui::PushColumnsBackground()
  2318. {
  2319. ImGuiWindow* window = GetCurrentWindowRead();
  2320. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2321. if (columns->Count == 1)
  2322. return;
  2323. // Optimization: avoid SetCurrentChannel() + PushClipRect()
  2324. columns->HostBackupClipRect = window->ClipRect;
  2325. SetWindowClipRectBeforeSetChannel(window, columns->HostInitialClipRect);
  2326. columns->Splitter.SetCurrentChannel(window->DrawList, 0);
  2327. }
  2328. void ImGui::PopColumnsBackground()
  2329. {
  2330. ImGuiWindow* window = GetCurrentWindowRead();
  2331. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2332. if (columns->Count == 1)
  2333. return;
  2334. // Optimization: avoid PopClipRect() + SetCurrentChannel()
  2335. SetWindowClipRectBeforeSetChannel(window, columns->HostBackupClipRect);
  2336. columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1);
  2337. }
  2338. ImGuiOldColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id)
  2339. {
  2340. // We have few columns per window so for now we don't need bother much with turning this into a faster lookup.
  2341. for (int n = 0; n < window->ColumnsStorage.Size; n++)
  2342. if (window->ColumnsStorage[n].ID == id)
  2343. return &window->ColumnsStorage[n];
  2344. window->ColumnsStorage.push_back(ImGuiOldColumns());
  2345. ImGuiOldColumns* columns = &window->ColumnsStorage.back();
  2346. columns->ID = id;
  2347. return columns;
  2348. }
  2349. ImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count)
  2350. {
  2351. ImGuiWindow* window = GetCurrentWindow();
  2352. // Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget.
  2353. // In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer.
  2354. PushID(0x11223347 + (str_id ? 0 : columns_count));
  2355. ImGuiID id = window->GetID(str_id ? str_id : "columns");
  2356. PopID();
  2357. return id;
  2358. }
  2359. void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFlags flags)
  2360. {
  2361. ImGuiContext& g = *GImGui;
  2362. ImGuiWindow* window = GetCurrentWindow();
  2363. IM_ASSERT(columns_count >= 1);
  2364. IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported
  2365. // Acquire storage for the columns set
  2366. ImGuiID id = GetColumnsID(str_id, columns_count);
  2367. ImGuiOldColumns* columns = FindOrCreateColumns(window, id);
  2368. IM_ASSERT(columns->ID == id);
  2369. columns->Current = 0;
  2370. columns->Count = columns_count;
  2371. columns->Flags = flags;
  2372. window->DC.CurrentColumns = columns;
  2373. columns->HostCursorPosY = window->DC.CursorPos.y;
  2374. columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x;
  2375. columns->HostInitialClipRect = window->ClipRect;
  2376. columns->HostBackupParentWorkRect = window->ParentWorkRect;
  2377. window->ParentWorkRect = window->WorkRect;
  2378. // Set state for first column
  2379. // We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect
  2380. const float column_padding = g.Style.ItemSpacing.x;
  2381. const float half_clip_extend_x = ImFloor(ImMax(window->WindowPadding.x * 0.5f, window->WindowBorderSize));
  2382. const float max_1 = window->WorkRect.Max.x + column_padding - ImMax(column_padding - window->WindowPadding.x, 0.0f);
  2383. const float max_2 = window->WorkRect.Max.x + half_clip_extend_x;
  2384. columns->OffMinX = window->DC.Indent.x - column_padding + ImMax(column_padding - window->WindowPadding.x, 0.0f);
  2385. columns->OffMaxX = ImMax(ImMin(max_1, max_2) - window->Pos.x, columns->OffMinX + 1.0f);
  2386. columns->LineMinY = columns->LineMaxY = window->DC.CursorPos.y;
  2387. // Clear data if columns count changed
  2388. if (columns->Columns.Size != 0 && columns->Columns.Size != columns_count + 1)
  2389. columns->Columns.resize(0);
  2390. // Initialize default widths
  2391. columns->IsFirstFrame = (columns->Columns.Size == 0);
  2392. if (columns->Columns.Size == 0)
  2393. {
  2394. columns->Columns.reserve(columns_count + 1);
  2395. for (int n = 0; n < columns_count + 1; n++)
  2396. {
  2397. ImGuiOldColumnData column;
  2398. column.OffsetNorm = n / (float)columns_count;
  2399. columns->Columns.push_back(column);
  2400. }
  2401. }
  2402. for (int n = 0; n < columns_count; n++)
  2403. {
  2404. // Compute clipping rectangle
  2405. ImGuiOldColumnData* column = &columns->Columns[n];
  2406. float clip_x1 = IM_ROUND(window->Pos.x + GetColumnOffset(n));
  2407. float clip_x2 = IM_ROUND(window->Pos.x + GetColumnOffset(n + 1) - 1.0f);
  2408. column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX);
  2409. column->ClipRect.ClipWithFull(window->ClipRect);
  2410. }
  2411. if (columns->Count > 1)
  2412. {
  2413. columns->Splitter.Split(window->DrawList, 1 + columns->Count);
  2414. columns->Splitter.SetCurrentChannel(window->DrawList, 1);
  2415. PushColumnClipRect(0);
  2416. }
  2417. // We don't generally store Indent.x inside ColumnsOffset because it may be manipulated by the user.
  2418. float offset_0 = GetColumnOffset(columns->Current);
  2419. float offset_1 = GetColumnOffset(columns->Current + 1);
  2420. float width = offset_1 - offset_0;
  2421. PushItemWidth(width * 0.65f);
  2422. window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f);
  2423. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  2424. window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding;
  2425. }
  2426. void ImGui::NextColumn()
  2427. {
  2428. ImGuiWindow* window = GetCurrentWindow();
  2429. if (window->SkipItems || window->DC.CurrentColumns == NULL)
  2430. return;
  2431. ImGuiContext& g = *GImGui;
  2432. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2433. if (columns->Count == 1)
  2434. {
  2435. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  2436. IM_ASSERT(columns->Current == 0);
  2437. return;
  2438. }
  2439. // Next column
  2440. if (++columns->Current == columns->Count)
  2441. columns->Current = 0;
  2442. PopItemWidth();
  2443. // Optimization: avoid PopClipRect() + SetCurrentChannel() + PushClipRect()
  2444. // (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them),
  2445. ImGuiOldColumnData* column = &columns->Columns[columns->Current];
  2446. SetWindowClipRectBeforeSetChannel(window, column->ClipRect);
  2447. columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1);
  2448. const float column_padding = g.Style.ItemSpacing.x;
  2449. columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);
  2450. if (columns->Current > 0)
  2451. {
  2452. // Columns 1+ ignore IndentX (by canceling it out)
  2453. // FIXME-COLUMNS: Unnecessary, could be locked?
  2454. window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + column_padding;
  2455. }
  2456. else
  2457. {
  2458. // New row/line: column 0 honor IndentX.
  2459. window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f);
  2460. columns->LineMinY = columns->LineMaxY;
  2461. }
  2462. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  2463. window->DC.CursorPos.y = columns->LineMinY;
  2464. window->DC.CurrLineSize = ImVec2(0.0f, 0.0f);
  2465. window->DC.CurrLineTextBaseOffset = 0.0f;
  2466. // FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup.
  2467. float offset_0 = GetColumnOffset(columns->Current);
  2468. float offset_1 = GetColumnOffset(columns->Current + 1);
  2469. float width = offset_1 - offset_0;
  2470. PushItemWidth(width * 0.65f);
  2471. window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding;
  2472. }
  2473. void ImGui::EndColumns()
  2474. {
  2475. ImGuiContext& g = *GImGui;
  2476. ImGuiWindow* window = GetCurrentWindow();
  2477. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2478. IM_ASSERT(columns != NULL);
  2479. PopItemWidth();
  2480. if (columns->Count > 1)
  2481. {
  2482. PopClipRect();
  2483. columns->Splitter.Merge(window->DrawList);
  2484. }
  2485. const ImGuiOldColumnFlags flags = columns->Flags;
  2486. columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);
  2487. window->DC.CursorPos.y = columns->LineMaxY;
  2488. if (!(flags & ImGuiOldColumnFlags_GrowParentContentsSize))
  2489. window->DC.CursorMaxPos.x = columns->HostCursorMaxPosX; // Restore cursor max pos, as columns don't grow parent
  2490. // Draw columns borders and handle resize
  2491. // The IsBeingResized flag ensure we preserve pre-resize columns width so back-and-forth are not lossy
  2492. bool is_being_resized = false;
  2493. if (!(flags & ImGuiOldColumnFlags_NoBorder) && !window->SkipItems)
  2494. {
  2495. // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers.
  2496. const float y1 = ImMax(columns->HostCursorPosY, window->ClipRect.Min.y);
  2497. const float y2 = ImMin(window->DC.CursorPos.y, window->ClipRect.Max.y);
  2498. int dragging_column = -1;
  2499. for (int n = 1; n < columns->Count; n++)
  2500. {
  2501. ImGuiOldColumnData* column = &columns->Columns[n];
  2502. float x = window->Pos.x + GetColumnOffset(n);
  2503. const ImGuiID column_id = columns->ID + ImGuiID(n);
  2504. const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH;
  2505. const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2));
  2506. KeepAliveID(column_id);
  2507. if (IsClippedEx(column_hit_rect, column_id, false))
  2508. continue;
  2509. bool hovered = false, held = false;
  2510. if (!(flags & ImGuiOldColumnFlags_NoResize))
  2511. {
  2512. ButtonBehavior(column_hit_rect, column_id, &hovered, &held);
  2513. if (hovered || held)
  2514. g.MouseCursor = ImGuiMouseCursor_ResizeEW;
  2515. if (held && !(column->Flags & ImGuiOldColumnFlags_NoResize))
  2516. dragging_column = n;
  2517. }
  2518. // Draw column
  2519. const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator);
  2520. const float xi = IM_FLOOR(x);
  2521. window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col);
  2522. }
  2523. // Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame.
  2524. if (dragging_column != -1)
  2525. {
  2526. if (!columns->IsBeingResized)
  2527. for (int n = 0; n < columns->Count + 1; n++)
  2528. columns->Columns[n].OffsetNormBeforeResize = columns->Columns[n].OffsetNorm;
  2529. columns->IsBeingResized = is_being_resized = true;
  2530. float x = GetDraggedColumnOffset(columns, dragging_column);
  2531. SetColumnOffset(dragging_column, x);
  2532. }
  2533. }
  2534. columns->IsBeingResized = is_being_resized;
  2535. window->WorkRect = window->ParentWorkRect;
  2536. window->ParentWorkRect = columns->HostBackupParentWorkRect;
  2537. window->DC.CurrentColumns = NULL;
  2538. window->DC.ColumnsOffset.x = 0.0f;
  2539. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  2540. }
  2541. void ImGui::Columns(int columns_count, const char* id, bool border)
  2542. {
  2543. ImGuiWindow* window = GetCurrentWindow();
  2544. IM_ASSERT(columns_count >= 1);
  2545. ImGuiOldColumnFlags flags = (border ? 0 : ImGuiOldColumnFlags_NoBorder);
  2546. //flags |= ImGuiOldColumnFlags_NoPreserveWidths; // NB: Legacy behavior
  2547. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2548. if (columns != NULL && columns->Count == columns_count && columns->Flags == flags)
  2549. return;
  2550. if (columns != NULL)
  2551. EndColumns();
  2552. if (columns_count != 1)
  2553. BeginColumns(id, columns_count, flags);
  2554. }
  2555. //-------------------------------------------------------------------------
  2556. #endif // #ifndef IMGUI_DISABLE