imgui_tables.cpp 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588
  1. // dear imgui, v1.80 WIP
  2. // (tables and columns code)
  3. /*
  4. Index of this file:
  5. // [SECTION] Tables: BeginTable, EndTable, etc.
  6. // [SECTION] Tables: Headers
  7. // [SECTION] Tables: Context Menu
  8. // [SECTION] Tables: Settings (.ini data)
  9. // [SECTION] Tables: Garbage Collection
  10. // [SECTION] Tables: Debugging
  11. // [SECTION] Columns, BeginColumns, EndColumns, etc.
  12. */
  13. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  14. #define _CRT_SECURE_NO_WARNINGS
  15. #endif
  16. #include "imgui.h"
  17. #ifndef IMGUI_DISABLE
  18. #ifndef IMGUI_DEFINE_MATH_OPERATORS
  19. #define IMGUI_DEFINE_MATH_OPERATORS
  20. #endif
  21. #include "imgui_internal.h"
  22. // System includes
  23. #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
  24. #include <stddef.h> // intptr_t
  25. #else
  26. #include <stdint.h> // intptr_t
  27. #endif
  28. //-------------------------------------------------------------------------
  29. // Warnings
  30. //-------------------------------------------------------------------------
  31. // Visual Studio warnings
  32. #ifdef _MSC_VER
  33. #pragma warning (disable: 4127) // condition expression is constant
  34. #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
  35. #if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later
  36. #pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types
  37. #endif
  38. #endif
  39. // Clang/GCC warnings with -Weverything
  40. #if defined(__clang__)
  41. #if __has_warning("-Wunknown-warning-option")
  42. #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!
  43. #endif
  44. #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx'
  45. #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
  46. #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.
  47. #pragma clang diagnostic ignored "-Wformat-nonliteral" // warning: format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code.
  48. #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
  49. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0
  50. #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.
  51. #pragma clang diagnostic ignored "-Wenum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_')
  52. #pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated
  53. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
  54. #elif defined(__GNUC__)
  55. #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind
  56. #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked
  57. #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
  58. #endif
  59. //-----------------------------------------------------------------------------
  60. // [SECTION] Tables: BeginTable, EndTable, etc.
  61. //-----------------------------------------------------------------------------
  62. // Typical call flow: (root level is public API):
  63. // - BeginTable() user begin into a table
  64. // | BeginChild() - (if ScrollX/ScrollY is set)
  65. // | TableBeginApplyRequests() - apply queued resizing/reordering/hiding requests
  66. // | - TableSetColumnWidth() - apply resizing width (for mouse resize, often requested by previous frame)
  67. // | - TableUpdateColumnsWeightFromWidth()- recompute columns weights (of stretch columns) from their respective width
  68. // - TableSetupColumn() user submit columns details (optional)
  69. // - TableSetupScrollFreeze() user submit scroll freeze information (optional)
  70. // - TableUpdateLayout() [Internal] automatically called by the FIRST call to TableNextRow() or TableHeadersRow(): lock all widths, columns positions, clipping rectangles
  71. // | TableUpdateDrawChannels() - setup ImDrawList channels
  72. // | TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission
  73. // | TableDrawContextMenu() - draw right-click context menu
  74. //-----------------------------------------------------------------------------
  75. // - TableHeadersRow() or TableHeader() user submit a headers row (optional)
  76. // | TableSortSpecsClickColumn() - when left-clicked: alter sort order and sort direction
  77. // | TableOpenContextMenu() - when right-clicked: trigger opening of the default context menu
  78. // - TableGetSortSpecs() user queries updated sort specs (optional, generally after submitting headers)
  79. // - TableNextRow() user begin into a new row (also automatically called by TableHeadersRow())
  80. // | TableEndRow() - finish existing row
  81. // | TableBeginRow() - add a new row
  82. // - TableSetColumnIndex() / TableNextColumn() user begin into a cell
  83. // | TableEndCell() - close existing column/cell
  84. // | TableBeginCell() - enter into current column/cell
  85. // - [...] user emit contents
  86. //-----------------------------------------------------------------------------
  87. // - EndTable() user ends the table
  88. // | TableDrawBorders() - draw outer borders, inner vertical borders
  89. // | TableReorderDrawChannelsForMerge() - merge draw channels if clipping isn't required
  90. // | EndChild() - (if ScrollX/ScrollY is set)
  91. //-----------------------------------------------------------------------------
  92. // Configuration
  93. static const int TABLE_DRAW_CHANNEL_BG0 = 0;
  94. static const int TABLE_DRAW_CHANNEL_BG1_FROZEN = 1;
  95. static const int TABLE_DRAW_CHANNEL_UNCLIPPED = 2; // When using ImGuiTableFlags_NoClip
  96. static const float TABLE_BORDER_SIZE = 1.0f; // FIXME-TABLE: Currently hard-coded because of clipping assumptions with outer borders rendering.
  97. static const float TABLE_RESIZE_SEPARATOR_HALF_THICKNESS = 4.0f; // Extend outside inner borders.
  98. 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.
  99. // Helper
  100. inline ImGuiTableFlags TableFixFlags(ImGuiTableFlags flags, ImGuiWindow* outer_window)
  101. {
  102. // Adjust flags: set default sizing policy
  103. if ((flags & (ImGuiTableFlags_ColumnsWidthStretch | ImGuiTableFlags_ColumnsWidthFixed)) == 0)
  104. flags |= (flags & ImGuiTableFlags_ScrollX) ? ImGuiTableFlags_ColumnsWidthFixed : ImGuiTableFlags_ColumnsWidthStretch;
  105. // Adjust flags: MultiSortable automatically enable Sortable
  106. if (flags & ImGuiTableFlags_MultiSortable)
  107. flags |= ImGuiTableFlags_Sortable;
  108. // Adjust flags: disable Resizable when using SameWidths (done above enforcing BordersInnerV)
  109. if (flags & ImGuiTableFlags_SameWidths)
  110. flags = (flags & ~ImGuiTableFlags_Resizable) | ImGuiTableFlags_NoKeepColumnsVisible;
  111. // Adjust flags: enforce borders when resizable
  112. if (flags & ImGuiTableFlags_Resizable)
  113. flags |= ImGuiTableFlags_BordersInnerV;
  114. // Adjust flags: disable NoHostExtendY if we have any scrolling going on
  115. if ((flags & ImGuiTableFlags_NoHostExtendY) && (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0)
  116. flags &= ~ImGuiTableFlags_NoHostExtendY;
  117. // Adjust flags: NoBordersInBodyUntilResize takes priority over NoBordersInBody
  118. if (flags & ImGuiTableFlags_NoBordersInBodyUntilResize)
  119. flags &= ~ImGuiTableFlags_NoBordersInBody;
  120. // Adjust flags: disable saved settings if there's nothing to save
  121. if ((flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Sortable)) == 0)
  122. flags |= ImGuiTableFlags_NoSavedSettings;
  123. // Inherit _NoSavedSettings from top-level window (child windows always have _NoSavedSettings set)
  124. #ifdef IMGUI_HAS_DOCK
  125. ImGuiWindow* window_for_settings = outer_window->RootWindowDockStop;
  126. #else
  127. ImGuiWindow* window_for_settings = outer_window->RootWindow;
  128. #endif
  129. if (window_for_settings->Flags & ImGuiWindowFlags_NoSavedSettings)
  130. flags |= ImGuiTableFlags_NoSavedSettings;
  131. return flags;
  132. }
  133. ImGuiTable* ImGui::TableFindByID(ImGuiID id)
  134. {
  135. ImGuiContext& g = *GImGui;
  136. return g.Tables.GetByKey(id);
  137. }
  138. ImGuiTable::ImGuiTable()
  139. {
  140. memset(this, 0, sizeof(*this));
  141. SettingsOffset = -1;
  142. InstanceInteracted = -1;
  143. LastFrameActive = -1;
  144. LastResizedColumn = -1;
  145. ContextPopupColumn = -1;
  146. ReorderColumn = -1;
  147. ResizedColumn = -1;
  148. AutoFitSingleStretchColumn = -1;
  149. HoveredColumnBody = HoveredColumnBorder = -1;
  150. }
  151. ImGuiTable::~ImGuiTable()
  152. {
  153. IM_FREE(RawData);
  154. }
  155. // (Read carefully because this is subtle but it does make sense!)
  156. // About 'outer_size', its meaning needs to differ slightly depending of if we are using ScrollX/ScrollY flags:
  157. // X:
  158. // - outer_size.x < 0.0f -> right align from window/work-rect maximum x edge.
  159. // - outer_size.x = 0.0f -> auto enlarge, use all available space.
  160. // - outer_size.x > 0.0f -> fixed width
  161. // Y with ScrollX/ScrollY: using a child window for scrolling:
  162. // - outer_size.y < 0.0f -> bottom align
  163. // - outer_size.y = 0.0f -> bottom align, consistent with BeginChild(). not recommended unless table is last item in parent window.
  164. // - outer_size.y > 0.0f -> fixed child height. recommended when using Scrolling on any axis.
  165. // Y without scrolling, we output table directly in parent window:
  166. // - outer_size.y < 0.0f -> bottom align (will auto extend, unless NoHostExtendV is set)
  167. // - outer_size.y = 0.0f -> zero minimum height (will auto extend, unless NoHostExtendV is set)
  168. // - outer_size.y > 0.0f -> minimum height (will auto extend, unless NoHostExtendV is set)
  169. // About 'inner_width':
  170. // With ScrollX:
  171. // - inner_width < 0.0f -> *illegal* fit in known width (right align from outer_size.x) <-- weird
  172. // - inner_width = 0.0f -> fit in outer_width: Fixed size columns will take space they need (if avail, otherwise shrink down), Stretch columns becomes Fixed columns.
  173. // - inner_width > 0.0f -> override scrolling width, generally to be larger than outer_size.x. Fixed column take space they need (if avail, otherwise shrink down), Stretch columns share remaining space!
  174. // Without ScrollX:
  175. // - inner_width -> *ignored*
  176. // Details:
  177. // - If you want to use Stretch columns with ScrollX, you generally need to specify 'inner_width' otherwise the concept
  178. // of "available space" doesn't make sense.
  179. // - Even if not really useful, we allow 'inner_width < outer_size.x' for consistency and to facilitate understanding
  180. // of what the value does.
  181. bool ImGui::BeginTable(const char* str_id, int columns_count, ImGuiTableFlags flags, const ImVec2& outer_size, float inner_width)
  182. {
  183. ImGuiID id = GetID(str_id);
  184. return BeginTableEx(str_id, id, columns_count, flags, outer_size, inner_width);
  185. }
  186. // For reference, the average total _allocation count_ for a table is:
  187. // + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables)
  188. // + 1 (for table->RawData allocated below)
  189. // + 1 (for table->ColumnsNames, if names are used)
  190. // + 1 (for table->Splitter._Channels)
  191. // + 2 * active_channels_count (for ImDrawCmd and ImDrawIdx buffers inside channels)
  192. // Where active_channels_count is variable but often == columns_count or columns_count + 1, see TableUpdateDrawChannels() for details.
  193. // Unused channels don't perform their +2 allocations.
  194. static void TableBeginInitMemory(ImGuiTable* table, int columns_count)
  195. {
  196. // Allocate single buffer for our arrays
  197. ImSpanAllocator<3> span_allocator;
  198. span_allocator.ReserveBytes(0, columns_count * sizeof(ImGuiTableColumn));
  199. span_allocator.ReserveBytes(1, columns_count * sizeof(ImS8));
  200. span_allocator.ReserveBytes(2, columns_count * sizeof(ImGuiTableCellData));
  201. table->RawData = IM_ALLOC(span_allocator.GetArenaSizeInBytes());
  202. span_allocator.SetArenaBasePtr(table->RawData);
  203. span_allocator.GetSpan(0, &table->Columns);
  204. span_allocator.GetSpan(1, &table->DisplayOrderToIndex);
  205. span_allocator.GetSpan(2, &table->RowCellData);
  206. memset(table->RowCellData.Data, 0, table->RowCellData.size_in_bytes());
  207. for (int n = 0; n < columns_count; n++)
  208. {
  209. ImGuiTableColumn* column = &table->Columns[n];
  210. *column = ImGuiTableColumn();
  211. column->DisplayOrder = table->DisplayOrderToIndex[n] = (ImS8)n;
  212. column->AutoFitQueue = column->CannotSkipItemsQueue = (1 << 3) - 1; // Fit for three frames
  213. }
  214. }
  215. bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImGuiTableFlags flags, const ImVec2& outer_size, float inner_width)
  216. {
  217. ImGuiContext& g = *GImGui;
  218. ImGuiWindow* outer_window = GetCurrentWindow();
  219. if (outer_window->SkipItems) // Consistent with other tables + beneficial side effect that assert on miscalling EndTable() will be more visible.
  220. return false;
  221. // Sanity checks
  222. IM_ASSERT(columns_count > 0 && columns_count <= IMGUI_TABLE_MAX_COLUMNS && "Only 1..64 columns allowed!");
  223. if (flags & ImGuiTableFlags_ScrollX)
  224. IM_ASSERT(inner_width >= 0.0f);
  225. // If an outer size is specified ahead we will be able to early out when not visible. Exact clipping rules may evolve.
  226. const bool use_child_window = (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0;
  227. const ImVec2 avail_size = GetContentRegionAvail();
  228. ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f);
  229. ImRect outer_rect(outer_window->DC.CursorPos, outer_window->DC.CursorPos + actual_outer_size);
  230. if (use_child_window && IsClippedEx(outer_rect, 0, false))
  231. {
  232. ItemSize(outer_rect);
  233. return false;
  234. }
  235. // Acquire storage for the table
  236. ImGuiTable* table = g.Tables.GetOrAddByKey(id);
  237. const bool table_is_new = (table->ID == 0);
  238. const int instance_no = (table->LastFrameActive != g.FrameCount) ? 0 : table->InstanceCurrent + 1;
  239. const ImGuiID instance_id = id + instance_no;
  240. const ImGuiTableFlags table_last_flags = table->Flags;
  241. if (instance_no > 0)
  242. IM_ASSERT(table->ColumnsCount == columns_count && "BeginTable(): Cannot change columns count mid-frame while preserving same ID");
  243. // Fix flags
  244. flags = TableFixFlags(flags, outer_window);
  245. // Initialize
  246. table->ID = id;
  247. table->Flags = flags;
  248. table->InstanceCurrent = (ImS16)instance_no;
  249. table->LastFrameActive = g.FrameCount;
  250. table->OuterWindow = table->InnerWindow = outer_window;
  251. table->ColumnsCount = columns_count;
  252. table->IsInitializing = false;
  253. table->IsLayoutLocked = false;
  254. table->InnerWidth = inner_width;
  255. table->OuterRect = outer_rect;
  256. table->WorkRect = outer_rect;
  257. // When not using a child window, WorkRect.Max will grow as we append contents.
  258. if (use_child_window)
  259. {
  260. // Ensure no vertical scrollbar appears if we only want horizontal one, to make flag consistent
  261. // (we have no other way to disable vertical scrollbar of a window while keeping the horizontal one showing)
  262. ImVec2 override_content_size(FLT_MAX, FLT_MAX);
  263. if ((flags & ImGuiTableFlags_ScrollX) && !(flags & ImGuiTableFlags_ScrollY))
  264. override_content_size.y = FLT_MIN;
  265. // Ensure specified width (when not specified, Stretched columns will act as if the width == OuterWidth and
  266. // never lead to any scrolling). We don't handle inner_width < 0.0f, we could potentially use it to right-align
  267. // based on the right side of the child window work rect, which would require knowing ahead if we are going to
  268. // have decoration taking horizontal spaces (typically a vertical scrollbar).
  269. if ((flags & ImGuiTableFlags_ScrollX) && inner_width > 0.0f)
  270. override_content_size.x = inner_width;
  271. if (override_content_size.x != FLT_MAX || override_content_size.y != FLT_MAX)
  272. 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));
  273. // Create scrolling region (without border and zero window padding)
  274. ImGuiWindowFlags child_flags = (flags & ImGuiTableFlags_ScrollX) ? ImGuiWindowFlags_HorizontalScrollbar : ImGuiWindowFlags_None;
  275. BeginChildEx(name, instance_id, table->OuterRect.GetSize(), false, child_flags);
  276. table->InnerWindow = g.CurrentWindow;
  277. table->WorkRect = table->InnerWindow->WorkRect;
  278. table->OuterRect = table->InnerWindow->Rect();
  279. IM_ASSERT(table->InnerWindow->WindowPadding.x == 0.0f && table->InnerWindow->WindowPadding.y == 0.0f && table->InnerWindow->WindowBorderSize == 0.0f);
  280. }
  281. // Push a standardized ID for both child-using and not-child-using tables
  282. PushOverrideID(instance_id);
  283. // Backup a copy of host window members we will modify
  284. ImGuiWindow* inner_window = table->InnerWindow;
  285. table->HostIndentX = inner_window->DC.Indent.x;
  286. table->HostClipRect = inner_window->ClipRect;
  287. table->HostSkipItems = inner_window->SkipItems;
  288. table->HostBackupWorkRect = inner_window->WorkRect;
  289. table->HostBackupParentWorkRect = inner_window->ParentWorkRect;
  290. table->HostBackupColumnsOffset = outer_window->DC.ColumnsOffset;
  291. table->HostBackupCursorMaxPos = inner_window->DC.CursorMaxPos;
  292. table->HostBackupItemWidth = outer_window->DC.ItemWidth;
  293. table->HostBackupItemWidthStackSize = outer_window->DC.ItemWidthStack.Size;
  294. inner_window->ParentWorkRect = table->WorkRect;
  295. // Padding and Spacing
  296. // - None ........Content..... Pad .....Content........
  297. // - PadOuter | Pad ..Content..... Pad .....Content.. Pad |
  298. // - PadInner ........Content.. Pad | Pad ..Content........
  299. // - PadOuter+PadInner | Pad ..Content.. Pad | Pad ..Content.. Pad |
  300. const bool pad_outer_x = (flags & ImGuiTableFlags_NoPadOuterX) ? false : (flags & ImGuiTableFlags_PadOuterX) ? true : (flags & ImGuiTableFlags_BordersOuterV) != 0;
  301. const bool pad_inner_x = (flags & ImGuiTableFlags_NoPadInnerX) ? false : true;
  302. const float inner_spacing_for_border = (flags & ImGuiTableFlags_BordersInnerV) ? TABLE_BORDER_SIZE : 0.0f;
  303. const float inner_spacing_explicit = (pad_inner_x && (flags & ImGuiTableFlags_BordersInnerV) == 0) ? g.Style.CellPadding.x : 0.0f;
  304. const float inner_padding_explicit = (pad_inner_x && (flags & ImGuiTableFlags_BordersInnerV) != 0) ? g.Style.CellPadding.x : 0.0f;
  305. const float inner_spacing = inner_spacing_for_border + inner_spacing_explicit;
  306. table->CellSpacingX1 = ImCeil(inner_spacing * 0.5f);
  307. table->CellSpacingX2 = inner_spacing - table->CellSpacingX1;
  308. table->CellPaddingX = inner_padding_explicit;
  309. table->CellPaddingY = g.Style.CellPadding.y;
  310. const float outer_padding_for_border = (flags & ImGuiTableFlags_BordersOuterV) ? TABLE_BORDER_SIZE : 0.0f;
  311. const float outer_padding_explicit = pad_outer_x ? g.Style.CellPadding.x : 0.0f;
  312. table->OuterPaddingX = (outer_padding_for_border + outer_padding_explicit) - table->CellPaddingX;
  313. table->CurrentColumn = -1;
  314. table->CurrentRow = -1;
  315. table->RowBgColorCounter = 0;
  316. table->LastRowFlags = ImGuiTableRowFlags_None;
  317. table->InnerClipRect = (inner_window == outer_window) ? table->WorkRect : inner_window->ClipRect;
  318. table->InnerClipRect.ClipWith(table->WorkRect); // We need this to honor inner_width
  319. table->InnerClipRect.ClipWithFull(table->HostClipRect);
  320. table->InnerClipRect.Max.y = (flags & ImGuiTableFlags_NoHostExtendY) ? ImMin(table->InnerClipRect.Max.y, inner_window->WorkRect.Max.y) : inner_window->ClipRect.Max.y;
  321. // Initial draw cmd starts with a BgClipRect that matches the one of its host, to facilitate merge draw commands by default.
  322. // This is because all our cell highlight are manually clipped with BgClipRect
  323. // Larger at first, if/after unfreezing will become same as tight
  324. table->BgClipRect = table->InnerClipRect;
  325. table->BgClipRectForDrawCmd = table->HostClipRect;
  326. IM_ASSERT(table->BgClipRect.Min.y <= table->BgClipRect.Max.y);
  327. table->RowPosY1 = table->RowPosY2 = table->WorkRect.Min.y; // This is needed somehow
  328. table->RowTextBaseline = 0.0f; // This will be cleared again by TableBeginRow()
  329. table->FreezeRowsRequest = table->FreezeRowsCount = 0; // This will be setup by TableSetupScrollFreeze(), if any
  330. table->FreezeColumnsRequest = table->FreezeColumnsCount = 0;
  331. table->IsUnfrozen = true;
  332. table->DeclColumnsCount = 0;
  333. table->RightMostEnabledColumn = -1;
  334. // Using opaque colors facilitate overlapping elements of the grid
  335. table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong);
  336. table->BorderColorLight = GetColorU32(ImGuiCol_TableBorderLight);
  337. table->BorderX1 = table->InnerClipRect.Min.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : -1.0f);
  338. table->BorderX2 = table->InnerClipRect.Max.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : +1.0f);
  339. // Make table current
  340. const int table_idx = g.Tables.GetIndex(table);
  341. g.CurrentTableStack.push_back(ImGuiPtrOrIndex(table_idx));
  342. g.CurrentTable = table;
  343. outer_window->DC.CurrentTableIdx = table_idx;
  344. if (inner_window != outer_window) // So EndChild() within the inner window can restore the table properly.
  345. inner_window->DC.CurrentTableIdx = table_idx;
  346. if ((table_last_flags & ImGuiTableFlags_Reorderable) && (flags & ImGuiTableFlags_Reorderable) == 0)
  347. table->IsResetDisplayOrderRequest = true;
  348. // Mark as used
  349. if (table_idx >= g.TablesLastTimeActive.Size)
  350. g.TablesLastTimeActive.resize(table_idx + 1, -1.0f);
  351. g.TablesLastTimeActive[table_idx] = (float)g.Time;
  352. table->MemoryCompacted = false;
  353. // Setup memory buffer (clear data if columns count changed)
  354. const int stored_size = table->Columns.size();
  355. if (stored_size != 0 && stored_size != columns_count)
  356. {
  357. IM_FREE(table->RawData);
  358. table->RawData = NULL;
  359. }
  360. if (table->RawData == NULL)
  361. {
  362. TableBeginInitMemory(table, columns_count);
  363. if (table_is_new)
  364. table->IsInitializing = true;
  365. table->IsSortSpecsDirty = table->IsSettingsRequestLoad = true;
  366. table->SettingsOffset = -1;
  367. }
  368. // Load settings
  369. if (table->IsSettingsRequestLoad)
  370. TableLoadSettings(table);
  371. // Handle DPI/font resize
  372. // This is designed to facilitate DPI changes with the assumption that e.g. style.CellPadding has been scaled as well.
  373. // It will also react to changing fonts with mixed results. It doesn't need to be perfect but merely provide a decent transition.
  374. // FIXME-DPI: Provide consistent standards for reference size. Perhaps using g.CurrentDpiScale would be more self explanatory.
  375. // This is will lead us to non-rounded WidthRequest in columns, which should work but is a poorly tested path.
  376. const float new_ref_scale_unit = g.FontSize; // g.Font->GetCharAdvance('A') ?
  377. if (table->RefScale != 0.0f && table->RefScale != new_ref_scale_unit)
  378. {
  379. const float scale_factor = new_ref_scale_unit / table->RefScale;
  380. //IMGUI_DEBUG_LOG("[table] %08X RefScaleUnit %.3f -> %.3f, scaling width by %.3f\n", table->ID, table->RefScaleUnit, new_ref_scale_unit, scale_factor);
  381. for (int n = 0; n < columns_count; n++)
  382. table->Columns[n].WidthRequest = table->Columns[n].WidthRequest * scale_factor;
  383. }
  384. table->RefScale = new_ref_scale_unit;
  385. // Disable output until user calls TableNextRow() or TableNextColumn() leading to the TableUpdateLayout() call..
  386. // This is not strictly necessary but will reduce cases were "out of table" output will be misleading to the user.
  387. // Because we cannot safely assert in EndTable() when no rows have been created, this seems like our best option.
  388. inner_window->SkipItems = true;
  389. // Clear names
  390. // FIXME-TABLES: probably could be done differently...
  391. if (table->ColumnsNames.Buf.Size > 0)
  392. {
  393. table->ColumnsNames.Buf.resize(0);
  394. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  395. {
  396. ImGuiTableColumn* column = &table->Columns[column_n];
  397. column->NameOffset = -1;
  398. }
  399. }
  400. // Apply queued resizing/reordering/hiding requests
  401. TableBeginApplyRequests(table);
  402. return true;
  403. }
  404. // Apply queued resizing/reordering/hiding requests
  405. void ImGui::TableBeginApplyRequests(ImGuiTable* table)
  406. {
  407. // Handle resizing request
  408. // (We process this at the first TableBegin of the frame)
  409. // FIXME-TABLE: Preserve contents width _while resizing down_ until releasing.
  410. // FIXME-TABLE: Contains columns if our work area doesn't allow for scrolling.
  411. if (table->InstanceCurrent == 0)
  412. {
  413. if (table->ResizedColumn != -1 && table->ResizedColumnNextWidth != FLT_MAX)
  414. TableSetColumnWidth(table->ResizedColumn, table->ResizedColumnNextWidth);
  415. table->LastResizedColumn = table->ResizedColumn;
  416. table->ResizedColumnNextWidth = FLT_MAX;
  417. table->ResizedColumn = -1;
  418. // Process auto-fit for single stretch column, which is a special case
  419. // FIXME-TABLE: Would be nice to redistribute available stretch space accordingly to other weights, instead of giving it all to siblings.
  420. if (table->AutoFitSingleStretchColumn != -1)
  421. {
  422. table->Columns[table->AutoFitSingleStretchColumn].AutoFitQueue = 0x00;
  423. TableSetColumnWidth(table->AutoFitSingleStretchColumn, table->Columns[table->AutoFitSingleStretchColumn].WidthAuto);
  424. table->AutoFitSingleStretchColumn = -1;
  425. }
  426. }
  427. // Handle reordering request
  428. // Note: we don't clear ReorderColumn after handling the request.
  429. if (table->InstanceCurrent == 0)
  430. {
  431. if (table->HeldHeaderColumn == -1 && table->ReorderColumn != -1)
  432. table->ReorderColumn = -1;
  433. table->HeldHeaderColumn = -1;
  434. if (table->ReorderColumn != -1 && table->ReorderColumnDir != 0)
  435. {
  436. // We need to handle reordering across hidden columns.
  437. // In the configuration below, moving C to the right of E will lead to:
  438. // ... C [D] E ---> ... [D] E C (Column name/index)
  439. // ... 2 3 4 ... 2 3 4 (Display order)
  440. const int reorder_dir = table->ReorderColumnDir;
  441. IM_ASSERT(reorder_dir == -1 || reorder_dir == +1);
  442. IM_ASSERT(table->Flags & ImGuiTableFlags_Reorderable);
  443. ImGuiTableColumn* src_column = &table->Columns[table->ReorderColumn];
  444. ImGuiTableColumn* dst_column = &table->Columns[(reorder_dir == -1) ? src_column->PrevEnabledColumn : src_column->NextEnabledColumn];
  445. IM_UNUSED(dst_column);
  446. const int src_order = src_column->DisplayOrder;
  447. const int dst_order = dst_column->DisplayOrder;
  448. src_column->DisplayOrder = (ImS8)dst_order;
  449. for (int order_n = src_order + reorder_dir; order_n != dst_order + reorder_dir; order_n += reorder_dir)
  450. table->Columns[table->DisplayOrderToIndex[order_n]].DisplayOrder -= (ImS8)reorder_dir;
  451. IM_ASSERT(dst_column->DisplayOrder == dst_order - reorder_dir);
  452. // Display order is stored in both columns->IndexDisplayOrder and table->DisplayOrder[],
  453. // rebuild the later from the former.
  454. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  455. table->DisplayOrderToIndex[table->Columns[column_n].DisplayOrder] = (ImS8)column_n;
  456. table->ReorderColumnDir = 0;
  457. table->IsSettingsDirty = true;
  458. }
  459. }
  460. // Handle display order reset request
  461. if (table->IsResetDisplayOrderRequest)
  462. {
  463. for (int n = 0; n < table->ColumnsCount; n++)
  464. table->DisplayOrderToIndex[n] = table->Columns[n].DisplayOrder = (ImS8)n;
  465. table->IsResetDisplayOrderRequest = false;
  466. table->IsSettingsDirty = true;
  467. }
  468. }
  469. // Adjust flags: default width mode + stretch columns are not allowed when auto extending
  470. static ImGuiTableColumnFlags TableFixColumnFlags(ImGuiTable* table, ImGuiTableColumnFlags flags)
  471. {
  472. // Sizing Policy
  473. if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0)
  474. {
  475. // FIXME-TABLE: Inconsistent to promote columns to WidthAutoResize
  476. if (table->Flags & ImGuiTableFlags_ColumnsWidthFixed)
  477. flags |= ((table->Flags & ImGuiTableFlags_Resizable) && !(flags & ImGuiTableColumnFlags_NoResize)) ? ImGuiTableColumnFlags_WidthFixed : ImGuiTableColumnFlags_WidthAutoResize;
  478. else
  479. flags |= ImGuiTableColumnFlags_WidthStretch;
  480. }
  481. IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_WidthMask_)); // Check that only 1 of each set is used.
  482. if (flags & ImGuiTableColumnFlags_WidthAutoResize)
  483. flags |= ImGuiTableColumnFlags_NoResize;
  484. // Sorting
  485. if ((flags & ImGuiTableColumnFlags_NoSortAscending) && (flags & ImGuiTableColumnFlags_NoSortDescending))
  486. flags |= ImGuiTableColumnFlags_NoSort;
  487. // Alignment
  488. //if ((flags & ImGuiTableColumnFlags_AlignMask_) == 0)
  489. // flags |= ImGuiTableColumnFlags_AlignCenter;
  490. //IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_AlignMask_)); // Check that only 1 of each set is used.
  491. return flags;
  492. }
  493. static void TableFixColumnSortDirection(ImGuiTableColumn* column)
  494. {
  495. // Initial sort state
  496. if (column->SortDirection == ImGuiSortDirection_None)
  497. column->SortDirection = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? (ImS8)ImGuiSortDirection_Descending : (ImS8)(ImGuiSortDirection_Ascending);
  498. // Handle NoSortAscending/NoSortDescending
  499. if (column->SortDirection == ImGuiSortDirection_Ascending && (column->Flags & ImGuiTableColumnFlags_NoSortAscending))
  500. column->SortDirection = ImGuiSortDirection_Descending;
  501. else if (column->SortDirection == ImGuiSortDirection_Descending && (column->Flags & ImGuiTableColumnFlags_NoSortDescending))
  502. column->SortDirection = ImGuiSortDirection_Ascending;
  503. }
  504. // Minimum column content width (without padding)
  505. static float TableGetMinColumnWidth()
  506. {
  507. ImGuiContext& g = *GImGui;
  508. //return g.Style.ColumnsMinSpacing; // FIXME-TABLE
  509. return g.Style.FramePadding.x * 1.0f;
  510. }
  511. // Layout columns for the frame
  512. // Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() to be called first.
  513. // FIXME-TABLE: Our width (and therefore our WorkRect) will be minimal in the first frame for WidthAutoResize
  514. // columns, increase feedback side-effect with widgets relying on WorkRect.Max.x. Maybe provide a default distribution
  515. // for WidthAutoResize columns?
  516. void ImGui::TableUpdateLayout(ImGuiTable* table)
  517. {
  518. ImGuiContext& g = *GImGui;
  519. IM_ASSERT(table->IsLayoutLocked == false);
  520. table->HoveredColumnBody = -1;
  521. table->HoveredColumnBorder = -1;
  522. // Lock Enabled state and Order
  523. ImGuiTableColumn* last_visible_column = NULL;
  524. bool want_column_auto_fit = false;
  525. table->IsDefaultDisplayOrder = true;
  526. table->ColumnsEnabledCount = 0;
  527. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  528. {
  529. const int column_n = table->DisplayOrderToIndex[order_n];
  530. if (column_n != order_n)
  531. table->IsDefaultDisplayOrder = false;
  532. ImGuiTableColumn* column = &table->Columns[column_n];
  533. if (!(table->Flags & ImGuiTableFlags_Hideable) || (column->Flags & ImGuiTableColumnFlags_NoHide))
  534. column->IsEnabledNextFrame = true;
  535. if (column->IsEnabled != column->IsEnabledNextFrame)
  536. {
  537. column->IsEnabled = column->IsEnabledNextFrame;
  538. table->IsSettingsDirty = true;
  539. if (!column->IsEnabled && column->SortOrder != -1)
  540. table->IsSortSpecsDirty = true;
  541. }
  542. if (column->SortOrder > 0 && !(table->Flags & ImGuiTableFlags_MultiSortable))
  543. table->IsSortSpecsDirty = true;
  544. if (column->AutoFitQueue != 0x00)
  545. want_column_auto_fit = true;
  546. ImU64 index_mask = (ImU64)1 << column_n;
  547. ImU64 display_order_mask = (ImU64)1 << column->DisplayOrder;
  548. if (column->IsEnabled)
  549. {
  550. column->PrevEnabledColumn = column->NextEnabledColumn = -1;
  551. if (last_visible_column)
  552. {
  553. last_visible_column->NextEnabledColumn = (ImS8)column_n;
  554. column->PrevEnabledColumn = (ImS8)table->Columns.index_from_ptr(last_visible_column);
  555. }
  556. column->IndexWithinEnabledSet = table->ColumnsEnabledCount;
  557. table->ColumnsEnabledCount++;
  558. table->EnabledMaskByIndex |= index_mask;
  559. table->EnabledMaskByDisplayOrder |= display_order_mask;
  560. last_visible_column = column;
  561. }
  562. else
  563. {
  564. column->IndexWithinEnabledSet = -1;
  565. table->EnabledMaskByIndex &= ~index_mask;
  566. table->EnabledMaskByDisplayOrder &= ~display_order_mask;
  567. }
  568. IM_ASSERT(column->IndexWithinEnabledSet <= column->DisplayOrder);
  569. }
  570. table->EnabledUnclippedMaskByIndex = table->EnabledMaskByIndex; // Columns will be masked out below when Clipped
  571. table->RightMostEnabledColumn = (ImS8)(last_visible_column ? table->Columns.index_from_ptr(last_visible_column) : -1);
  572. // Disable child window clipping while fitting columns. This is not strictly necessary but makes it possible to avoid
  573. // the column fitting to wait until the first visible frame of the child container (may or not be a good thing).
  574. if (want_column_auto_fit && table->OuterWindow != table->InnerWindow)
  575. table->InnerWindow->SkipItems = false;
  576. if (want_column_auto_fit)
  577. table->IsSettingsDirty = true;
  578. // Compute offset, clip rect for the frame
  579. // (can't make auto padding larger than what WorkRect knows about so right-alignment matches)
  580. const ImRect work_rect = table->WorkRect;
  581. const float min_column_width = TableGetMinColumnWidth();
  582. const float min_column_distance = min_column_width + table->CellPaddingX * 2.0f + table->CellSpacingX1 + table->CellSpacingX2;
  583. int count_fixed = 0;
  584. float sum_weights_stretched = 0.0f; // Sum of all weights for weighted columns.
  585. float sum_width_fixed_requests = 0.0f; // Sum of all width for fixed and auto-resize columns, excluding width contributed by Stretch columns.
  586. float max_width_auto = 0.0f;
  587. table->LeftMostStretchedColumnDisplayOrder = -1;
  588. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  589. {
  590. if (!(table->EnabledMaskByIndex & ((ImU64)1 << column_n)))
  591. continue;
  592. ImGuiTableColumn* column = &table->Columns[column_n];
  593. // Adjust flags: default width mode + weighted columns are not allowed when auto extending
  594. // FIXME-TABLE: Clarify why we need to do this again here and not just in TableSetupColumn()
  595. column->Flags = TableFixColumnFlags(table, column->FlagsIn);
  596. if ((column->Flags & ImGuiTableColumnFlags_IndentMask_) == 0)
  597. column->Flags |= (column_n == 0) ? ImGuiTableColumnFlags_IndentEnable : ImGuiTableColumnFlags_IndentDisable;
  598. // We have a unusual edge case where if the user doesn't call TableGetSortSpecs() but has sorting enabled
  599. // or varying sorting flags, we still want the sorting arrows to honor those flags.
  600. if (table->Flags & ImGuiTableFlags_Sortable)
  601. TableFixColumnSortDirection(column);
  602. // Calculate "ideal" column width for nothing to be clipped.
  603. // Combine width from regular rows + width from headers unless requested not to.
  604. const float content_width_body = (float)ImMax(column->ContentMaxXFrozen, column->ContentMaxXUnfrozen) - column->WorkMinX;
  605. const float content_width_headers = (float)column->ContentMaxXHeadersIdeal - column->WorkMinX;
  606. float width_auto = content_width_body;
  607. if (!(table->Flags & ImGuiTableFlags_NoHeadersWidth) && !(column->Flags & ImGuiTableColumnFlags_NoHeaderWidth))
  608. width_auto = ImMax(width_auto, content_width_headers);
  609. width_auto = ImMax(width_auto, min_column_width);
  610. // Non-resizable columns also submit their requested width
  611. if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && column->InitStretchWeightOrWidth > 0.0f)
  612. if (!(table->Flags & ImGuiTableFlags_Resizable) || !(column->Flags & ImGuiTableColumnFlags_NoResize))
  613. width_auto = ImMax(width_auto, column->InitStretchWeightOrWidth);
  614. column->WidthAuto = width_auto;
  615. if (column->Flags & (ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAutoResize))
  616. {
  617. // Process auto-fit for non-stretched columns
  618. // Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!)
  619. if ((column->AutoFitQueue != 0x00) || ((column->Flags & ImGuiTableColumnFlags_WidthAutoResize) && !column->IsClipped))
  620. column->WidthRequest = width_auto;
  621. // FIXME-TABLE: Increase minimum size during init frame to avoid biasing auto-fitting widgets
  622. // (e.g. TextWrapped) too much. Otherwise what tends to happen is that TextWrapped would output a very
  623. // large height (= first frame scrollbar display very off + clipper would skip lots of items).
  624. // This is merely making the side-effect less extreme, but doesn't properly fixes it.
  625. // FIXME: Move this to ->WidthGiven to avoid temporary lossyless?
  626. if (column->AutoFitQueue > 0x01 && table->IsInitializing)
  627. column->WidthRequest = ImMax(column->WidthRequest, min_column_width * 4.0f); // FIXME-TABLE: Another constant/scale?
  628. count_fixed += 1;
  629. sum_width_fixed_requests += column->WidthRequest;
  630. }
  631. else
  632. {
  633. IM_ASSERT(column->Flags & ImGuiTableColumnFlags_WidthStretch);
  634. const float default_weight = (column->InitStretchWeightOrWidth > 0.0f) ? column->InitStretchWeightOrWidth : 1.0f;
  635. if (column->AutoFitQueue != 0x00)
  636. column->StretchWeight = default_weight;
  637. sum_weights_stretched += column->StretchWeight;
  638. if (table->LeftMostStretchedColumnDisplayOrder == -1 || table->LeftMostStretchedColumnDisplayOrder > column->DisplayOrder)
  639. table->LeftMostStretchedColumnDisplayOrder = (ImS8)column->DisplayOrder;
  640. }
  641. max_width_auto = ImMax(max_width_auto, width_auto);
  642. sum_width_fixed_requests += table->CellPaddingX * 2.0f;
  643. }
  644. table->ColumnsEnabledFixedCount = (ImS8)count_fixed;
  645. // Apply "same widths"
  646. // - When all columns are fixed or columns are of mixed type: use the maximum auto width
  647. // - When all columns are stretch: use same weight
  648. const bool mixed_same_widths = (table->Flags & ImGuiTableFlags_SameWidths) && count_fixed > 0;
  649. if (table->Flags & ImGuiTableFlags_SameWidths)
  650. {
  651. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  652. if (table->EnabledMaskByIndex & ((ImU64)1 << column_n))
  653. {
  654. ImGuiTableColumn* column = &table->Columns[column_n];
  655. if (column->Flags & (ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAutoResize))
  656. {
  657. sum_width_fixed_requests += max_width_auto - column->WidthRequest; // Update old sum
  658. column->WidthRequest = max_width_auto;
  659. }
  660. else
  661. {
  662. sum_weights_stretched += 1.0f - column->StretchWeight; // Update old sum
  663. column->StretchWeight = 1.0f;
  664. if (count_fixed > 0)
  665. column->WidthRequest = max_width_auto;
  666. }
  667. }
  668. }
  669. // Layout
  670. const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsEnabledCount - 1);
  671. const float width_avail = ((table->Flags & ImGuiTableFlags_ScrollX) && table->InnerWidth == 0.0f) ? table->InnerClipRect.GetWidth() : work_rect.GetWidth();
  672. const float width_avail_for_stretched_columns = mixed_same_widths ? 0.0f : width_avail - width_spacings - sum_width_fixed_requests;
  673. float width_remaining_for_stretched_columns = width_avail_for_stretched_columns;
  674. // Apply final width based on requested widths
  675. // Mark some columns as not resizable
  676. int count_resizable = 0;
  677. table->ColumnsTotalWidth = width_spacings;
  678. table->ColumnsAutoFitWidth = width_spacings;
  679. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  680. {
  681. if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)))
  682. continue;
  683. ImGuiTableColumn* column = &table->Columns[table->DisplayOrderToIndex[order_n]];
  684. // Allocate width for stretched/weighted columns
  685. if (column->Flags & ImGuiTableColumnFlags_WidthStretch)
  686. {
  687. // StretchWeight gets converted into WidthRequest
  688. if (!mixed_same_widths)
  689. {
  690. float weight_ratio = column->StretchWeight / sum_weights_stretched;
  691. column->WidthRequest = IM_FLOOR(ImMax(width_avail_for_stretched_columns * weight_ratio, min_column_width) + 0.01f);
  692. width_remaining_for_stretched_columns -= column->WidthRequest;
  693. }
  694. // [Resize Rule 2] Resizing from right-side of a stretch column preceding a fixed column
  695. // needs to forward resizing to left-side of fixed column. We also need to copy the NoResize flag..
  696. if (column->NextEnabledColumn != -1)
  697. if (ImGuiTableColumn* next_column = &table->Columns[column->NextEnabledColumn])
  698. if (next_column->Flags & ImGuiTableColumnFlags_WidthFixed)
  699. column->Flags |= (next_column->Flags & ImGuiTableColumnFlags_NoDirectResize_);
  700. }
  701. // [Resize Rule 1] The right-most Visible column is not resizable if there is at least one Stretch column
  702. // (see comments in TableResizeColumn())
  703. if (column->NextEnabledColumn == -1 && table->LeftMostStretchedColumnDisplayOrder != -1)
  704. column->Flags |= ImGuiTableColumnFlags_NoDirectResize_;
  705. if (!(column->Flags & ImGuiTableColumnFlags_NoResize))
  706. count_resizable++;
  707. // Assign final width, record width in case we will need to shrink
  708. column->WidthGiven = ImFloor(ImMax(column->WidthRequest, min_column_width));
  709. table->ColumnsTotalWidth += column->WidthGiven + table->CellPaddingX * 2.0f;
  710. table->ColumnsAutoFitWidth += column->WidthAuto + table->CellPaddingX * 2.0f;
  711. }
  712. // Redistribute remainder width due to rounding (remainder width is < 1.0f * number of Stretch column).
  713. // Using right-to-left distribution (more likely to match resizing cursor), could be adjusted depending
  714. // on where the mouse cursor is and/or relative weights.
  715. if (width_remaining_for_stretched_columns >= 1.0f && !(table->Flags & ImGuiTableFlags_PreciseWidths))
  716. for (int order_n = table->ColumnsCount - 1; sum_weights_stretched > 0.0f && width_remaining_for_stretched_columns >= 1.0f && order_n >= 0; order_n--)
  717. {
  718. if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)))
  719. continue;
  720. ImGuiTableColumn* column = &table->Columns[table->DisplayOrderToIndex[order_n]];
  721. if (!(column->Flags & ImGuiTableColumnFlags_WidthStretch))
  722. continue;
  723. column->WidthRequest += 1.0f;
  724. column->WidthGiven += 1.0f;
  725. width_remaining_for_stretched_columns -= 1.0f;
  726. }
  727. // Detect hovered column
  728. const ImRect mouse_hit_rect(table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.Max.x, ImMax(table->OuterRect.Max.y, table->OuterRect.Min.y + table->LastOuterHeight));
  729. const bool is_hovering_table = ItemHoverable(mouse_hit_rect, 0);
  730. // Setup final position, offset and clipping rectangles
  731. int visible_n = 0;
  732. float offset_x = (table->FreezeColumnsCount > 0) ? table->OuterRect.Min.x : work_rect.Min.x;
  733. offset_x += table->OuterPaddingX;
  734. offset_x -= table->CellSpacingX1;
  735. ImRect host_clip_rect = table->InnerClipRect;
  736. //host_clip_rect.Max.x += table->CellPaddingX + table->CellSpacingX2;
  737. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  738. {
  739. const int column_n = table->DisplayOrderToIndex[order_n];
  740. ImGuiTableColumn* column = &table->Columns[column_n];
  741. column->NavLayerCurrent = (ImS8)((table->FreezeRowsCount > 0 || column_n < table->FreezeColumnsCount) ? ImGuiNavLayer_Menu : ImGuiNavLayer_Main);
  742. if (table->FreezeColumnsCount > 0 && table->FreezeColumnsCount == visible_n)
  743. offset_x += work_rect.Min.x - table->OuterRect.Min.x;
  744. if ((table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)) == 0)
  745. {
  746. // Hidden column: clear a few fields and we are done with it for the remainder of the function.
  747. // We set a zero-width clip rect but set Min.y/Max.y properly to not interfere with the clipper.
  748. column->MinX = column->MaxX = column->WorkMinX = column->ClipRect.Min.x = column->ClipRect.Max.x = offset_x;
  749. column->WidthGiven = 0.0f;
  750. column->ClipRect.Min.y = work_rect.Min.y;
  751. column->ClipRect.Max.y = FLT_MAX;
  752. column->ClipRect.ClipWithFull(host_clip_rect);
  753. column->IsClipped = column->IsSkipItems = true;
  754. column->ItemWidth = 1.0f;
  755. continue;
  756. }
  757. // Maximum width
  758. float max_width = FLT_MAX;
  759. if (table->Flags & ImGuiTableFlags_ScrollX)
  760. {
  761. // Frozen columns can't reach beyond visible width else scrolling will naturally break.
  762. if (order_n < table->FreezeColumnsRequest)
  763. {
  764. max_width = (table->InnerClipRect.Max.x - (table->FreezeColumnsRequest - order_n) * min_column_distance) - offset_x;
  765. max_width = max_width - table->OuterPaddingX - table->CellPaddingX - table->CellSpacingX2;
  766. }
  767. }
  768. else if ((table->Flags & ImGuiTableFlags_NoKeepColumnsVisible) == 0)
  769. {
  770. // If horizontal scrolling if disabled, we apply a final lossless shrinking of columns in order to make
  771. // sure they are all visible. Because of this we also know that all of the columns will always fit in
  772. // table->WorkRect and therefore in table->InnerRect (because ScrollX is off)
  773. // FIXME-TABLE: This is solved incorrectly but also quite a difficult problem to fix as we also want ClipRect width to match.
  774. // See "table_width_distrib" and "table_width_keep_visible" tests
  775. max_width = table->WorkRect.Max.x - (table->ColumnsEnabledCount - column->IndexWithinEnabledSet - 1) * min_column_distance - offset_x;
  776. //max_width -= table->CellSpacingX1;
  777. max_width -= table->CellSpacingX2;
  778. max_width -= table->CellPaddingX * 2.0f;
  779. max_width -= table->OuterPaddingX;
  780. }
  781. column->WidthGiven = ImMin(column->WidthGiven, max_width);
  782. // Minimum width
  783. column->WidthGiven = ImMax(column->WidthGiven, ImMin(column->WidthRequest, min_column_width));
  784. // Lock all our positions
  785. // - ClipRect.Min.x: Because merging draw commands doesn't compare min boundaries, we make ClipRect.Min.x match left bounds to be consistent regardless of merging.
  786. // - ClipRect.Max.x: using WorkMaxX instead of MaxX (aka including padding) is detrimental to visibility in very-small column.
  787. // - FIXME-TABLE: We want equal width columns to have equal (ClipRect.Max.x - WorkMinX) width, which means ClipRect.max.x cannot stray off host_clip_rect.Max.x else right-most column may appear shorter.
  788. column->MinX = offset_x;
  789. column->MaxX = offset_x + column->WidthGiven + table->CellSpacingX1 + table->CellSpacingX2 + table->CellPaddingX * 2.0f;
  790. column->WorkMinX = column->MinX + table->CellPaddingX + table->CellSpacingX1;
  791. column->WorkMaxX = column->MaxX - table->CellPaddingX - table->CellSpacingX2; // Expected max
  792. column->ItemWidth = ImFloor(column->WidthGiven * 0.65f);
  793. column->ClipRect.Min.x = column->MinX;
  794. column->ClipRect.Min.y = work_rect.Min.y;
  795. column->ClipRect.Max.x = column->MaxX; // column->WorkMaxX;
  796. column->ClipRect.Max.y = FLT_MAX;
  797. column->ClipRect.ClipWithFull(host_clip_rect);
  798. column->IsClipped = (column->ClipRect.Max.x <= column->ClipRect.Min.x) && (column->AutoFitQueue & 1) == 0 && (column->CannotSkipItemsQueue & 1) == 0;
  799. if (column->IsClipped)
  800. table->EnabledUnclippedMaskByIndex &= ~((ImU64)1 << column_n); // Columns with the _WidthAutoResize sizing policy will never be updated then.
  801. column->IsSkipItems = !column->IsEnabled || table->HostSkipItems;
  802. // Detect hovered column
  803. if (is_hovering_table && g.IO.MousePos.x >= column->ClipRect.Min.x && g.IO.MousePos.x < column->ClipRect.Max.x)
  804. table->HoveredColumnBody = (ImS8)column_n;
  805. // Alignment
  806. // FIXME-TABLE: This align based on the whole column width, not per-cell, and therefore isn't useful in
  807. // many cases (to be able to honor this we might be able to store a log of cells width, per row, for
  808. // visible rows, but nav/programmatic scroll would have visible artifacts.)
  809. //if (column->Flags & ImGuiTableColumnFlags_AlignRight)
  810. // column->WorkMinX = ImMax(column->WorkMinX, column->MaxX - column->ContentWidthRowsUnfrozen);
  811. //else if (column->Flags & ImGuiTableColumnFlags_AlignCenter)
  812. // column->WorkMinX = ImLerp(column->WorkMinX, ImMax(column->StartX, column->MaxX - column->ContentWidthRowsUnfrozen), 0.5f);
  813. // Reset content width variables
  814. column->ContentMaxXFrozen = column->ContentMaxXUnfrozen = column->WorkMinX;
  815. column->ContentMaxXHeadersUsed = column->ContentMaxXHeadersIdeal = column->WorkMinX;
  816. // Don't decrement auto-fit counters until container window got a chance to submit its items
  817. if (table->HostSkipItems == false)
  818. {
  819. column->AutoFitQueue >>= 1;
  820. column->CannotSkipItemsQueue >>= 1;
  821. }
  822. if (visible_n < table->FreezeColumnsCount)
  823. host_clip_rect.Min.x = ImMax(host_clip_rect.Min.x, column->MaxX + TABLE_BORDER_SIZE);
  824. offset_x += column->WidthGiven + table->CellSpacingX1 + table->CellSpacingX2 + table->CellPaddingX * 2.0f;
  825. visible_n++;
  826. }
  827. // Detect/store when we are hovering the unused space after the right-most column (so e.g. context menus can react on it)
  828. if (is_hovering_table && table->HoveredColumnBody == -1)
  829. {
  830. float unused_x1 = table->WorkRect.Min.x;
  831. if (table->RightMostEnabledColumn != -1)
  832. unused_x1 = ImMax(unused_x1, table->Columns[table->RightMostEnabledColumn].ClipRect.Max.x);
  833. if (g.IO.MousePos.x >= unused_x1)
  834. table->HoveredColumnBody = (ImS8)table->ColumnsCount;
  835. }
  836. // Clear Resizable flag if none of our column are actually resizable (either via an explicit _NoResize flag,
  837. // either because of using _WidthAutoResize/_WidthStretch).
  838. // This will hide the resizing option from the context menu.
  839. if (count_resizable == 0 && (table->Flags & ImGuiTableFlags_Resizable))
  840. table->Flags &= ~ImGuiTableFlags_Resizable;
  841. // Allocate draw channels
  842. TableUpdateDrawChannels(table);
  843. // Borders
  844. if (table->Flags & ImGuiTableFlags_Resizable)
  845. TableUpdateBorders(table);
  846. // Reset fields after we used them in TableSetupResize()
  847. table->LastFirstRowHeight = 0.0f;
  848. table->IsLayoutLocked = true;
  849. table->IsUsingHeaders = false;
  850. // Context menu
  851. if (table->IsContextPopupOpen && table->InstanceCurrent == table->InstanceInteracted)
  852. {
  853. const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID);
  854. if (BeginPopupEx(context_menu_id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings))
  855. {
  856. TableDrawContextMenu(table);
  857. EndPopup();
  858. }
  859. else
  860. {
  861. table->IsContextPopupOpen = false;
  862. }
  863. }
  864. // Initial state
  865. ImGuiWindow* inner_window = table->InnerWindow;
  866. if (table->Flags & ImGuiTableFlags_NoClip)
  867. table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_UNCLIPPED);
  868. else
  869. inner_window->DrawList->PushClipRect(inner_window->ClipRect.Min, inner_window->ClipRect.Max, false);
  870. // Sanitize and build sort specs before we have a change to use them for display.
  871. // This path will only be exercised when sort specs are modified before header rows (e.g. init or visibility change)
  872. if (table->IsSortSpecsDirty && (table->Flags & ImGuiTableFlags_Sortable))
  873. TableSortSpecsBuild(table);
  874. }
  875. // Process interaction on resizing borders. Actual size change will be applied in EndTable()
  876. // - Set table->HoveredColumnBorder with a short delay/timer to reduce feedback noise
  877. // - Submit ahead of table contents and header, use ImGuiButtonFlags_AllowItemOverlap to prioritize widgets
  878. // overlapping the same area.
  879. void ImGui::TableUpdateBorders(ImGuiTable* table)
  880. {
  881. ImGuiContext& g = *GImGui;
  882. IM_ASSERT(table->Flags & ImGuiTableFlags_Resizable);
  883. // At this point OuterRect height may be zero or under actual final height, so we rely on temporal coherency and
  884. // use the final height from last frame. Because this is only affecting _interaction_ with columns, it is not
  885. // really problematic (whereas the actual visual will be displayed in EndTable() and using the current frame height).
  886. // Actual columns highlight/render will be performed in EndTable() and not be affected.
  887. const float hit_half_width = TABLE_RESIZE_SEPARATOR_HALF_THICKNESS;
  888. const float hit_y1 = table->OuterRect.Min.y;
  889. const float hit_y2_body = ImMax(table->OuterRect.Max.y, hit_y1 + table->LastOuterHeight);
  890. const float hit_y2_head = hit_y1 + table->LastFirstRowHeight;
  891. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  892. {
  893. if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)))
  894. continue;
  895. const int column_n = table->DisplayOrderToIndex[order_n];
  896. ImGuiTableColumn* column = &table->Columns[column_n];
  897. if (column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoDirectResize_))
  898. continue;
  899. // ImGuiTableFlags_NoBordersInBodyUntilResize will be honored in TableDrawBorders()
  900. const float border_y2_hit = (table->Flags & ImGuiTableFlags_NoBordersInBody) ? hit_y2_head : hit_y2_body;
  901. if ((table->Flags & ImGuiTableFlags_NoBordersInBody) && table->IsUsingHeaders == false)
  902. continue;
  903. ImGuiID column_id = TableGetColumnResizeID(table, column_n, table->InstanceCurrent);
  904. ImRect hit_rect(column->MaxX - hit_half_width, hit_y1, column->MaxX + hit_half_width, border_y2_hit);
  905. //GetForegroundDrawList()->AddRect(hit_rect.Min, hit_rect.Max, IM_COL32(255, 0, 0, 100));
  906. KeepAliveID(column_id);
  907. bool hovered = false, held = false;
  908. bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick);
  909. if (pressed && IsMouseDoubleClicked(0))
  910. {
  911. TableSetColumnWidthAutoSingle(table, column_n);
  912. ClearActiveID();
  913. held = hovered = false;
  914. }
  915. if (held)
  916. {
  917. table->ResizedColumn = (ImS8)column_n;
  918. table->InstanceInteracted = table->InstanceCurrent;
  919. }
  920. if ((hovered && g.HoveredIdTimer > TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER) || held)
  921. {
  922. table->HoveredColumnBorder = (ImS8)column_n;
  923. SetMouseCursor(ImGuiMouseCursor_ResizeEW);
  924. }
  925. }
  926. }
  927. void ImGui::EndTable()
  928. {
  929. ImGuiContext& g = *GImGui;
  930. ImGuiTable* table = g.CurrentTable;
  931. IM_ASSERT(table != NULL && "Only call EndTable() if BeginTable() returns true!");
  932. // This assert would be very useful to catch a common error... unfortunately it would probably trigger in some
  933. // cases, and for consistency user may sometimes output empty tables (and still benefit from e.g. outer border)
  934. //IM_ASSERT(table->IsLayoutLocked && "Table unused: never called TableNextRow(), is that the intent?");
  935. // If the user never got to call TableNextRow() or TableNextColumn(), we call layout ourselves to ensure all our
  936. // code paths are consistent (instead of just hoping that TableBegin/TableEnd will work), get borders drawn, etc.
  937. if (!table->IsLayoutLocked)
  938. TableUpdateLayout(table);
  939. const ImGuiTableFlags flags = table->Flags;
  940. ImGuiWindow* inner_window = table->InnerWindow;
  941. ImGuiWindow* outer_window = table->OuterWindow;
  942. IM_ASSERT(inner_window == g.CurrentWindow);
  943. IM_ASSERT(outer_window == inner_window || outer_window == inner_window->ParentWindow);
  944. if (table->IsInsideRow)
  945. TableEndRow(table);
  946. // Context menu in columns body
  947. if (flags & ImGuiTableFlags_ContextMenuInBody)
  948. if (table->HoveredColumnBody != -1 && !ImGui::IsAnyItemHovered() && ImGui::IsMouseReleased(ImGuiMouseButton_Right))
  949. TableOpenContextMenu((int)table->HoveredColumnBody);
  950. // Finalize table height
  951. inner_window->DC.CursorMaxPos = table->HostBackupCursorMaxPos;
  952. if (inner_window != outer_window)
  953. {
  954. table->OuterRect.Max.y = ImMax(table->OuterRect.Max.y, inner_window->Pos.y + inner_window->Size.y);
  955. inner_window->DC.CursorMaxPos.y = table->RowPosY2;
  956. }
  957. else if (!(flags & ImGuiTableFlags_NoHostExtendY))
  958. {
  959. table->OuterRect.Max.y = ImMax(table->OuterRect.Max.y, inner_window->DC.CursorPos.y);
  960. inner_window->DC.CursorMaxPos.y = table->RowPosY2;
  961. }
  962. table->WorkRect.Max.y = ImMax(table->WorkRect.Max.y, table->OuterRect.Max.y);
  963. table->LastOuterHeight = table->OuterRect.GetHeight();
  964. if (!(flags & ImGuiTableFlags_NoClip))
  965. inner_window->DrawList->PopClipRect();
  966. inner_window->ClipRect = inner_window->DrawList->_ClipRectStack.back();
  967. // Draw borders
  968. if ((flags & ImGuiTableFlags_Borders) != 0)
  969. TableDrawBorders(table);
  970. table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0);
  971. // Store content width reference for each column (before attempting to merge draw calls)
  972. const float backup_outer_cursor_pos_x = outer_window->DC.CursorPos.x;
  973. const float backup_outer_max_pos_x = outer_window->DC.CursorMaxPos.x;
  974. const float backup_inner_max_pos_x = inner_window->DC.CursorMaxPos.x;
  975. float max_pos_x = backup_inner_max_pos_x;
  976. if (table->RightMostEnabledColumn != -1)
  977. max_pos_x = ImMax(max_pos_x, table->Columns[table->RightMostEnabledColumn].MaxX);
  978. #if 0
  979. // Strip out dummy channel draw calls
  980. // We have no way to prevent user submitting direct ImDrawList calls into a hidden column (but ImGui:: calls will be clipped out)
  981. // (The problem with this approach is we are effectively making it harder for users watching metrics to spot wasted vertices)
  982. if (table->DummyDrawChannel != (ImU8)-1)
  983. {
  984. ImDrawChannel* dummy_channel = &table->DrawSplitter._Channels[table->DummyDrawChannel];
  985. dummy_channel->_CmdBuffer.resize(0);
  986. dummy_channel->_IdxBuffer.resize(0);
  987. }
  988. #endif
  989. // Flatten channels and merge draw calls
  990. if ((table->Flags & ImGuiTableFlags_NoClip) == 0)
  991. TableReorderDrawChannelsForMerge(table);
  992. table->DrawSplitter.Merge(inner_window->DrawList);
  993. if (!(table->Flags & ImGuiTableFlags_ScrollX) && inner_window != outer_window)
  994. {
  995. inner_window->Scroll.x = 0.0f;
  996. }
  997. else if (table->LastResizedColumn != -1 && table->ResizedColumn == -1 && inner_window->ScrollbarX && table->InstanceInteracted == table->InstanceCurrent)
  998. {
  999. // When releasing a column being resized, scroll to keep the resulting column in sight
  1000. const float neighbor_width_to_keep_visible = TableGetMinColumnWidth() + table->CellPaddingX * 2.0f;
  1001. ImGuiTableColumn* column = &table->Columns[table->LastResizedColumn];
  1002. if (column->MaxX < table->InnerClipRect.Min.x)
  1003. SetScrollFromPosX(inner_window, column->MaxX - inner_window->Pos.x - neighbor_width_to_keep_visible, 1.0f);
  1004. else if (column->MaxX > table->InnerClipRect.Max.x)
  1005. SetScrollFromPosX(inner_window, column->MaxX - inner_window->Pos.x + neighbor_width_to_keep_visible, 1.0f);
  1006. }
  1007. // Apply resizing/dragging at the end of the frame
  1008. if (table->ResizedColumn != -1 && table->InstanceCurrent == table->InstanceInteracted)
  1009. {
  1010. ImGuiTableColumn* column = &table->Columns[table->ResizedColumn];
  1011. const float new_x2 = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + TABLE_RESIZE_SEPARATOR_HALF_THICKNESS);
  1012. const float new_width = ImFloor(new_x2 - column->MinX - table->CellSpacingX1 - table->CellPaddingX * 2.0f);
  1013. table->ResizedColumnNextWidth = new_width;
  1014. }
  1015. // Layout in outer window
  1016. IM_ASSERT_USER_ERROR(inner_window->IDStack.back() == table->ID + table->InstanceCurrent, "Mismatching PushID/PopID!");
  1017. IM_ASSERT_USER_ERROR(outer_window->DC.ItemWidthStack.Size >= table->HostBackupItemWidthStackSize, "Too many PopItemWidth!");
  1018. PopID();
  1019. inner_window->WorkRect = table->HostBackupWorkRect;
  1020. inner_window->ParentWorkRect = table->HostBackupParentWorkRect;
  1021. inner_window->SkipItems = table->HostSkipItems;
  1022. outer_window->DC.CursorPos = table->OuterRect.Min;
  1023. outer_window->DC.ItemWidth = table->HostBackupItemWidth;
  1024. outer_window->DC.ItemWidthStack.Size = table->HostBackupItemWidthStackSize;
  1025. outer_window->DC.ColumnsOffset = table->HostBackupColumnsOffset;
  1026. if (inner_window != outer_window)
  1027. {
  1028. EndChild();
  1029. }
  1030. else
  1031. {
  1032. ImVec2 item_size = table->OuterRect.GetSize();
  1033. item_size.x = table->ColumnsTotalWidth;
  1034. ItemSize(item_size);
  1035. }
  1036. // Override EndChild/ItemSize max extent with our own to enable auto-resize on the X axis when possible
  1037. // FIXME-TABLE: This can be improved (e.g. for Fixed columns we don't want to auto AutoFitWidth? or propagate window auto-fit to table?)
  1038. if (table->Flags & ImGuiTableFlags_ScrollX)
  1039. {
  1040. inner_window->DC.CursorMaxPos.x = max_pos_x; // Set contents width for scrolling
  1041. outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos_x, backup_outer_cursor_pos_x + table->ColumnsTotalWidth + inner_window->ScrollbarSizes.x); // For auto-fit
  1042. }
  1043. else
  1044. {
  1045. outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos_x, table->WorkRect.Min.x + table->ColumnsAutoFitWidth); // For auto-fit
  1046. }
  1047. // Save settings
  1048. if (table->IsSettingsDirty)
  1049. TableSaveSettings(table);
  1050. // Clear or restore current table, if any
  1051. IM_ASSERT(g.CurrentWindow == outer_window && g.CurrentTable == table);
  1052. g.CurrentTableStack.pop_back();
  1053. g.CurrentTable = g.CurrentTableStack.Size ? g.Tables.GetByIndex(g.CurrentTableStack.back().Index) : NULL;
  1054. outer_window->DC.CurrentTableIdx = g.CurrentTable ? g.Tables.GetIndex(g.CurrentTable) : -1;
  1055. }
  1056. // FIXME-TABLE: This is a mess, need to redesign how we render borders.
  1057. void ImGui::TableDrawBorders(ImGuiTable* table)
  1058. {
  1059. ImGuiWindow* inner_window = table->InnerWindow;
  1060. ImGuiWindow* outer_window = table->OuterWindow;
  1061. table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_BG0);
  1062. if (inner_window->Hidden || !table->HostClipRect.Overlaps(table->InnerClipRect))
  1063. return;
  1064. ImDrawList* inner_drawlist = inner_window->DrawList;
  1065. ImDrawList* outer_drawlist = outer_window->DrawList;
  1066. // Draw inner border and resizing feedback
  1067. const float border_size = TABLE_BORDER_SIZE;
  1068. const float draw_y1 = table->OuterRect.Min.y;
  1069. const float draw_y2_body = table->OuterRect.Max.y;
  1070. const float draw_y2_head = table->IsUsingHeaders ? ((table->FreezeRowsCount >= 1 ? table->OuterRect.Min.y : table->WorkRect.Min.y) + table->LastFirstRowHeight) : draw_y1;
  1071. if (table->Flags & ImGuiTableFlags_BordersInnerV)
  1072. {
  1073. for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
  1074. {
  1075. if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)))
  1076. continue;
  1077. const int column_n = table->DisplayOrderToIndex[order_n];
  1078. ImGuiTableColumn* column = &table->Columns[column_n];
  1079. const bool is_hovered = (table->HoveredColumnBorder == column_n);
  1080. const bool is_resized = (table->ResizedColumn == column_n) && (table->InstanceInteracted == table->InstanceCurrent);
  1081. const bool is_resizable = (column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoDirectResize_)) == 0;
  1082. if (column->MaxX > table->InnerClipRect.Max.x && !is_resized)// && is_hovered)
  1083. continue;
  1084. if (column->NextEnabledColumn == -1 && !is_resizable)
  1085. continue;
  1086. if (column->MaxX <= column->ClipRect.Min.x) // FIXME-TABLE FIXME-STYLE: Assume BorderSize==1, this is problematic if we want to increase the border size..
  1087. continue;
  1088. // Draw in outer window so right-most column won't be clipped
  1089. // Always draw full height border when being resized/hovered, or on the delimitation of frozen column scrolling.
  1090. ImU32 col;
  1091. float draw_y2;
  1092. if (is_hovered || is_resized || (table->FreezeColumnsCount != -1 && table->FreezeColumnsCount == order_n + 1))
  1093. {
  1094. draw_y2 = draw_y2_body;
  1095. col = is_resized ? GetColorU32(ImGuiCol_SeparatorActive) : is_hovered ? GetColorU32(ImGuiCol_SeparatorHovered) : table->BorderColorStrong;
  1096. }
  1097. else
  1098. {
  1099. draw_y2 = (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) ? draw_y2_head : draw_y2_body;
  1100. col = (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) ? table->BorderColorStrong : table->BorderColorLight;
  1101. }
  1102. if (draw_y2 > draw_y1)
  1103. inner_drawlist->AddLine(ImVec2(column->MaxX, draw_y1), ImVec2(column->MaxX, draw_y2), col, border_size);
  1104. }
  1105. }
  1106. // Draw outer border
  1107. // FIXME-TABLE: could use AddRect or explicit VLine/HLine helper?
  1108. if (table->Flags & ImGuiTableFlags_BordersOuter)
  1109. {
  1110. // Display outer border offset by 1 which is a simple way to display it without adding an extra draw call
  1111. // (Without the offset, in outer_window it would be rendered behind cells, because child windows are above their
  1112. // parent. In inner_window, it won't reach out over scrollbars. Another weird solution would be to display part
  1113. // of it in inner window, and the part that's over scrollbars in the outer window..)
  1114. // Either solution currently won't allow us to use a larger border size: the border would clipped.
  1115. ImRect outer_border = table->OuterRect;
  1116. const ImU32 outer_col = table->BorderColorStrong;
  1117. if (inner_window != outer_window) // FIXME-TABLE
  1118. outer_border.Expand(1.0f);
  1119. if ((table->Flags & ImGuiTableFlags_BordersOuter) == ImGuiTableFlags_BordersOuter)
  1120. {
  1121. outer_drawlist->AddRect(outer_border.Min, outer_border.Max, outer_col, 0.0f, ~0, border_size);
  1122. }
  1123. else if (table->Flags & ImGuiTableFlags_BordersOuterV)
  1124. {
  1125. outer_drawlist->AddLine(outer_border.Min, ImVec2(outer_border.Min.x, outer_border.Max.y), outer_col, border_size);
  1126. outer_drawlist->AddLine(ImVec2(outer_border.Max.x, outer_border.Min.y), outer_border.Max, outer_col, border_size);
  1127. }
  1128. else if (table->Flags & ImGuiTableFlags_BordersOuterH)
  1129. {
  1130. outer_drawlist->AddLine(outer_border.Min, ImVec2(outer_border.Max.x, outer_border.Min.y), outer_col, border_size);
  1131. outer_drawlist->AddLine(ImVec2(outer_border.Min.x, outer_border.Max.y), outer_border.Max, outer_col, border_size);
  1132. }
  1133. }
  1134. if ((table->Flags & ImGuiTableFlags_BordersInnerH) && table->RowPosY2 < table->OuterRect.Max.y)
  1135. {
  1136. // Draw bottom-most row border
  1137. const float border_y = table->RowPosY2;
  1138. if (border_y >= table->BgClipRect.Min.y && border_y < table->BgClipRect.Max.y)
  1139. inner_drawlist->AddLine(ImVec2(table->BorderX1, border_y), ImVec2(table->BorderX2, border_y), table->BorderColorLight, border_size);
  1140. }
  1141. }
  1142. static void TableUpdateColumnsWeightFromWidth(ImGuiTable* table)
  1143. {
  1144. IM_ASSERT(table->LeftMostStretchedColumnDisplayOrder != -1);
  1145. // Measure existing quantity
  1146. float visible_weight = 0.0f;
  1147. float visible_width = 0.0f;
  1148. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1149. {
  1150. ImGuiTableColumn* column = &table->Columns[column_n];
  1151. if (!column->IsEnabled || !(column->Flags & ImGuiTableColumnFlags_WidthStretch))
  1152. continue;
  1153. visible_weight += column->StretchWeight;
  1154. visible_width += column->WidthRequest;
  1155. }
  1156. IM_ASSERT(visible_weight > 0.0f && visible_width > 0.0f);
  1157. // Apply new weights
  1158. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1159. {
  1160. ImGuiTableColumn* column = &table->Columns[column_n];
  1161. if (!column->IsEnabled || !(column->Flags & ImGuiTableColumnFlags_WidthStretch))
  1162. continue;
  1163. column->StretchWeight = ((column->WidthRequest + 0.0f) / visible_width) * visible_weight;
  1164. }
  1165. }
  1166. // 'width' = inner column width, without padding
  1167. void ImGui::TableSetColumnWidth(int column_n, float width)
  1168. {
  1169. ImGuiContext& g = *GImGui;
  1170. ImGuiTable* table = g.CurrentTable;
  1171. IM_ASSERT(table != NULL && table->IsLayoutLocked == false);
  1172. IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount);
  1173. ImGuiTableColumn* column_0 = &table->Columns[column_n];
  1174. float column_0_width = width;
  1175. // Constraints
  1176. const float min_width = TableGetMinColumnWidth();
  1177. float max_width_0 = FLT_MAX;
  1178. if (!(table->Flags & ImGuiTableFlags_ScrollX))
  1179. max_width_0 = (table->WorkRect.Max.x - column_0->MinX) - (table->ColumnsEnabledCount - (column_0->IndexWithinEnabledSet + 1)) * min_width;
  1180. column_0_width = ImClamp(column_0_width, min_width, max_width_0);
  1181. // Compare both requested and actual given width to avoid overwriting requested width when column is stuck (minimum size, bounded)
  1182. if (column_0->WidthGiven == column_0_width || column_0->WidthRequest == column_0_width)
  1183. return;
  1184. ImGuiTableColumn* column_1 = (column_0->NextEnabledColumn != -1) ? &table->Columns[column_0->NextEnabledColumn] : NULL;
  1185. // In this surprisingly not simple because of how we support mixing Fixed and Stretch columns.
  1186. // When forwarding resize from Wn| to Fn+1| we need to be considerate of the _NoResize flag on Fn+1.
  1187. // FIXME-TABLE: Find a way to rewrite all of this so interactions feel more consistent for the user.
  1188. // Scenarios:
  1189. // - F1 F2 F3 resize from F1| or F2| --> ok: alter ->WidthRequested of Fixed column. Subsequent columns will be offset.
  1190. // - F1 F2 F3 resize from F3| --> ok: alter ->WidthRequested of Fixed column. If active, ScrollX extent can be altered.
  1191. // - 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 Stretch column will always be minimal size.
  1192. // - F1 F2 W3 resize from W3| --> ok: no-op (disabled by Resize Rule 1)
  1193. // - W1 W2 W3 resize from W1| or W2| --> FIXME
  1194. // - W1 W2 W3 resize from W3| --> ok: no-op (disabled by Resize Rule 1)
  1195. // - W1 F2 F3 resize from F3| --> ok: no-op (disabled by Resize Rule 1)
  1196. // - W1 F2 resize from F2| --> ok: no-op (disabled by Resize Rule 1)
  1197. // - W1 W2 F3 resize from W1| or W2| --> ok
  1198. // - W1 F2 W3 resize from W1| or F2| --> FIXME
  1199. // - F1 W2 F3 resize from W2| --> ok
  1200. // - W1 F2 F3 resize from W1| --> ok: equivalent to resizing |F2. F3 will not move. (forwarded by Resize Rule 2)
  1201. // - W1 F2 F3 resize from F2| --> FIXME should resize F2, F3 and not have effect on W1 (Stretch columns are _before_ the Fixed column).
  1202. // Rules:
  1203. // - [Resize Rule 1] Can't resize from right of right-most visible column if there is any Stretch column. Implemented in TableUpdateLayout().
  1204. // - [Resize Rule 2] Resizing from right-side of a Stretch column before a fixed column forward sizing to left-side of fixed column.
  1205. // - [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.
  1206. table->IsSettingsDirty = true;
  1207. if (column_0->Flags & ImGuiTableColumnFlags_WidthFixed)
  1208. {
  1209. // [Resize Rule 3] If we are are followed by a fixed column and we have a Stretch column before, we need to ensure
  1210. // that our left border won't move, which we can do by making sure column_a/column_b resizes cancels each others.
  1211. if (column_1 && (column_1->Flags & ImGuiTableColumnFlags_WidthFixed))
  1212. if (table->LeftMostStretchedColumnDisplayOrder != -1 && table->LeftMostStretchedColumnDisplayOrder < column_0->DisplayOrder)
  1213. {
  1214. // (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
  1215. float column_1_width = ImMax(column_1->WidthRequest - (column_0_width - column_0->WidthRequest), min_width);
  1216. column_0_width = column_0->WidthRequest + column_1->WidthRequest - column_1_width;
  1217. column_1->WidthRequest = column_1_width;
  1218. }
  1219. // Apply
  1220. //IMGUI_DEBUG_LOG("TableSetColumnWidth(%d, %.1f->%.1f)\n", column_0_idx, column_0->WidthRequested, column_0_width);
  1221. column_0->WidthRequest = column_0_width;
  1222. }
  1223. else if (column_0->Flags & ImGuiTableColumnFlags_WidthStretch)
  1224. {
  1225. // We can also use previous column if there's no next one
  1226. if (column_1 == NULL)
  1227. column_1 = (column_0->PrevEnabledColumn != -1) ? &table->Columns[column_0->PrevEnabledColumn] : NULL;
  1228. if (column_1 == NULL)
  1229. return;
  1230. if (column_1->Flags & ImGuiTableColumnFlags_WidthFixed)
  1231. {
  1232. // [Resize Rule 2]
  1233. float off = (column_0->WidthGiven - column_0_width);
  1234. float column_1_width = column_1->WidthGiven + off;
  1235. column_1->WidthRequest = ImMax(min_width, column_1_width);
  1236. }
  1237. else
  1238. {
  1239. // (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
  1240. float column_1_width = ImMax(column_1->WidthRequest - (column_0_width - column_0->WidthRequest), min_width);
  1241. column_0_width = column_0->WidthRequest + column_1->WidthRequest - column_1_width;
  1242. column_1->WidthRequest = column_1_width;
  1243. column_0->WidthRequest = column_0_width;
  1244. TableUpdateColumnsWeightFromWidth(table);
  1245. }
  1246. }
  1247. }
  1248. // Allocate draw channels. Called by TableUpdateLayout()
  1249. // - We allocate them following storage order instead of display order so reordering columns won't needlessly
  1250. // increase overall dormant memory cost.
  1251. // - We isolate headers draw commands in their own channels instead of just altering clip rects.
  1252. // This is in order to facilitate merging of draw commands.
  1253. // - After crossing FreezeRowsCount, all columns see their current draw channel changed to a second set of channels.
  1254. // - We only use the dummy draw channel so we can push a null clipping rectangle into it without affecting other
  1255. // channels, while simplifying per-row/per-cell overhead. It will be empty and discarded when merged.
  1256. // - We allocate 1 or 2 background draw channels. This is because we know PushTableBackground() is only used for
  1257. // horizontal spanning. If we allowed vertical spanning we'd need one background draw channel per merge group (1-4).
  1258. // Draw channel allocation (before merging):
  1259. // - NoClip --> 2+D+1 channels: bg0 + bg1 + foreground (same clip rect == 1 draw call) (FIXME-TABLE: could merge bg1 and foreground?)
  1260. // - Clip --> 2+D+N channels
  1261. // - FreezeRows --> 2+D+N*2 (unless scrolling value is zero)
  1262. // - FreezeRows || FreezeColunns --> 3+D+N*2 (unless scrolling value is zero)
  1263. // Where D is 1 if any column is clipped or hidden (dummy channel) otherwise 0.
  1264. void ImGui::TableUpdateDrawChannels(ImGuiTable* table)
  1265. {
  1266. const int freeze_row_multiplier = (table->FreezeRowsCount > 0) ? 2 : 1;
  1267. const int channels_for_row = (table->Flags & ImGuiTableFlags_NoClip) ? 1 : table->ColumnsEnabledCount;
  1268. const int channels_for_bg = 1 + 1 * freeze_row_multiplier;
  1269. const int channels_for_dummy = (table->ColumnsEnabledCount < table->ColumnsCount || table->EnabledUnclippedMaskByIndex != table->EnabledMaskByIndex) ? +1 : 0;
  1270. const int channels_total = channels_for_bg + (channels_for_row * freeze_row_multiplier) + channels_for_dummy;
  1271. table->DrawSplitter.Split(table->InnerWindow->DrawList, channels_total);
  1272. table->DummyDrawChannel = (ImU8)((channels_for_dummy > 0) ? channels_total - 1 : -1);
  1273. table->Bg1DrawChannelCurrent = TABLE_DRAW_CHANNEL_BG1_FROZEN;
  1274. table->Bg1DrawChannelUnfrozen = (ImU8)((table->FreezeRowsCount > 0) ? 2 + channels_for_row : TABLE_DRAW_CHANNEL_BG1_FROZEN);
  1275. int draw_channel_current = 2;
  1276. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1277. {
  1278. ImGuiTableColumn* column = &table->Columns[column_n];
  1279. if (!column->IsClipped)
  1280. {
  1281. column->DrawChannelFrozen = (ImU8)(draw_channel_current);
  1282. column->DrawChannelUnfrozen = (ImU8)(draw_channel_current + (table->FreezeRowsCount > 0 ? channels_for_row + 1 : 0));
  1283. if (!(table->Flags & ImGuiTableFlags_NoClip))
  1284. draw_channel_current++;
  1285. }
  1286. else
  1287. {
  1288. column->DrawChannelFrozen = column->DrawChannelUnfrozen = table->DummyDrawChannel;
  1289. }
  1290. column->DrawChannelCurrent = column->DrawChannelFrozen;
  1291. }
  1292. }
  1293. // This function reorder draw channels based on matching clip rectangle, to facilitate merging them. Called by EndTable().
  1294. //
  1295. // Columns where the contents didn't stray off their local clip rectangle can be merged. To achieve
  1296. // this we merge their clip rect and make them contiguous in the channel list, so they can be merged
  1297. // by the call to DrawSplitter.Merge() following to the call to this function.
  1298. //
  1299. // We reorder draw commands by arranging them into a maximum of 4 distinct groups:
  1300. //
  1301. // 1 group: 2 groups: 2 groups: 4 groups:
  1302. // [ 0. ] no freeze [ 0. ] row freeze [ 01 ] col freeze [ 01 ] row+col freeze
  1303. // [ .. ] or no scroll [ 2. ] and v-scroll [ .. ] and h-scroll [ 23 ] and v+h-scroll
  1304. //
  1305. // Each column itself can use 1 channel (row freeze disabled) or 2 channels (row freeze enabled).
  1306. // When the contents of a column didn't stray off its limit, we move its channels into the corresponding group
  1307. // based on its position (within frozen rows/columns groups or not).
  1308. // At the end of the operation our 1-4 groups will each have a ImDrawCmd using the same ClipRect.
  1309. //
  1310. // This function assume that each column are pointing to a distinct draw channel,
  1311. // otherwise merge_group->ChannelsCount will not match set bit count of merge_group->ChannelsMask.
  1312. //
  1313. // Column channels will not be merged into one of the 1-4 groups in the following cases:
  1314. // - The contents stray off its clipping rectangle (we only compare the MaxX value, not the MinX value).
  1315. // Direct ImDrawList calls won't be taken into account by default, if you use them make sure the ImGui:: bounds
  1316. // matches, by e.g. calling SetCursorScreenPos().
  1317. // - The channel uses more than one draw command itself. We drop all our attempt at merging stuff here..
  1318. // we could do better but it's going to be rare and probably not worth the hassle.
  1319. // Columns for which the draw channel(s) haven't been merged with other will use their own ImDrawCmd.
  1320. //
  1321. // This function is particularly tricky to understand.. take a breath.
  1322. void ImGui::TableReorderDrawChannelsForMerge(ImGuiTable* table)
  1323. {
  1324. ImGuiContext& g = *GImGui;
  1325. ImDrawListSplitter* splitter = &table->DrawSplitter;
  1326. const bool has_freeze_v = (table->FreezeRowsCount > 0);
  1327. const bool has_freeze_h = (table->FreezeColumnsCount > 0);
  1328. // Track which groups we are going to attempt to merge, and which channels goes into each group.
  1329. struct MergeGroup
  1330. {
  1331. ImRect ClipRect;
  1332. int ChannelsCount;
  1333. ImBitArray<IMGUI_TABLE_MAX_DRAW_CHANNELS> ChannelsMask;
  1334. };
  1335. int merge_group_mask = 0x00;
  1336. MergeGroup merge_groups[4];
  1337. memset(merge_groups, 0, sizeof(merge_groups));
  1338. // 1. Scan channels and take note of those which can be merged
  1339. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1340. {
  1341. if (!(table->EnabledUnclippedMaskByIndex & ((ImU64)1 << column_n)))
  1342. continue;
  1343. ImGuiTableColumn* column = &table->Columns[column_n];
  1344. const int merge_group_sub_count = has_freeze_v ? 2 : 1;
  1345. for (int merge_group_sub_n = 0; merge_group_sub_n < merge_group_sub_count; merge_group_sub_n++)
  1346. {
  1347. const int channel_no = (merge_group_sub_n == 0) ? column->DrawChannelFrozen : column->DrawChannelUnfrozen;
  1348. // Don't attempt to merge if there are multiple draw calls within the column
  1349. ImDrawChannel* src_channel = &splitter->_Channels[channel_no];
  1350. if (src_channel->_CmdBuffer.Size > 0 && src_channel->_CmdBuffer.back().ElemCount == 0)
  1351. src_channel->_CmdBuffer.pop_back();
  1352. if (src_channel->_CmdBuffer.Size != 1)
  1353. continue;
  1354. // Find out the width of this merge group and check if it will fit in our column
  1355. // (note that we assume that rendering didn't stray on the left direction. we should need a CursorMinPos to detect it)
  1356. if (!(column->Flags & ImGuiTableColumnFlags_NoClip))
  1357. {
  1358. float content_max_x;
  1359. if (!has_freeze_v)
  1360. content_max_x = ImMax(column->ContentMaxXUnfrozen, column->ContentMaxXHeadersUsed); // No row freeze
  1361. else if (merge_group_sub_n == 0)
  1362. content_max_x = ImMax(column->ContentMaxXFrozen, column->ContentMaxXHeadersUsed); // Row freeze: use width before freeze
  1363. else
  1364. content_max_x = column->ContentMaxXUnfrozen; // Row freeze: use width after freeze
  1365. if (content_max_x > column->ClipRect.Max.x)
  1366. continue;
  1367. }
  1368. const int merge_group_n = (has_freeze_h && column_n < table->FreezeColumnsCount ? 0 : 1) + (has_freeze_v && merge_group_sub_n == 0 ? 0 : 2);
  1369. IM_ASSERT(channel_no < IMGUI_TABLE_MAX_DRAW_CHANNELS);
  1370. MergeGroup* merge_group = &merge_groups[merge_group_n];
  1371. if (merge_group->ChannelsCount == 0)
  1372. merge_group->ClipRect = ImRect(+FLT_MAX, +FLT_MAX, -FLT_MAX, -FLT_MAX);
  1373. merge_group->ChannelsMask.SetBit(channel_no);
  1374. merge_group->ChannelsCount++;
  1375. merge_group->ClipRect.Add(src_channel->_CmdBuffer[0].ClipRect);
  1376. merge_group_mask |= (1 << merge_group_n);
  1377. }
  1378. // Invalidate current draw channel
  1379. // (we don't clear DrawChannelFrozen/DrawChannelUnfrozen solely to facilitate debugging/later inspection of data)
  1380. column->DrawChannelCurrent = (ImU8)-1;
  1381. }
  1382. // [DEBUG] Display merge groups
  1383. #if 0
  1384. if (g.IO.KeyShift)
  1385. for (int merge_group_n = 0; merge_group_n < IM_ARRAYSIZE(merge_groups); merge_group_n++)
  1386. {
  1387. MergeGroup* merge_group = &merge_groups[merge_group_n];
  1388. if (merge_group->ChannelsCount == 0)
  1389. continue;
  1390. char buf[32];
  1391. ImFormatString(buf, 32, "MG%d:%d", merge_group_n, merge_group->ChannelsCount);
  1392. ImVec2 text_pos = merge_group->ClipRect.Min + ImVec2(4, 4);
  1393. ImVec2 text_size = CalcTextSize(buf, NULL);
  1394. GetForegroundDrawList()->AddRectFilled(text_pos, text_pos + text_size, IM_COL32(0, 0, 0, 255));
  1395. GetForegroundDrawList()->AddText(text_pos, IM_COL32(255, 255, 0, 255), buf, NULL);
  1396. GetForegroundDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 255, 0, 255));
  1397. }
  1398. #endif
  1399. // 2. Rewrite channel list in our preferred order
  1400. if (merge_group_mask != 0)
  1401. {
  1402. // We skip channel 0 (Bg0) and 1 (Bg1 frozen) from the shuffling since they won't move - see channels allocation in TableUpdateDrawChannels().
  1403. const int LEADING_DRAW_CHANNELS = 2;
  1404. g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - LEADING_DRAW_CHANNELS); // Use shared temporary storage so the allocation gets amortized
  1405. ImDrawChannel* dst_tmp = g.DrawChannelsTempMergeBuffer.Data;
  1406. ImBitArray<IMGUI_TABLE_MAX_DRAW_CHANNELS> remaining_mask; // We need 132-bit of storage
  1407. remaining_mask.ClearBits();
  1408. remaining_mask.SetBitRange(LEADING_DRAW_CHANNELS, splitter->_Count - 1);
  1409. remaining_mask.ClearBit(table->Bg1DrawChannelUnfrozen);
  1410. IM_ASSERT(has_freeze_v == false || table->Bg1DrawChannelUnfrozen != TABLE_DRAW_CHANNEL_BG1_FROZEN);
  1411. int remaining_count = splitter->_Count - (has_freeze_v ? LEADING_DRAW_CHANNELS + 1 : LEADING_DRAW_CHANNELS);
  1412. //ImRect host_rect = (table->InnerWindow == table->OuterWindow) ? table->InnerClipRect : table->HostClipRect;
  1413. ImRect host_rect = table->HostClipRect;
  1414. for (int merge_group_n = 0; merge_group_n < IM_ARRAYSIZE(merge_groups); merge_group_n++)
  1415. {
  1416. if (int merge_channels_count = merge_groups[merge_group_n].ChannelsCount)
  1417. {
  1418. MergeGroup* merge_group = &merge_groups[merge_group_n];
  1419. ImRect merge_clip_rect = merge_group->ClipRect;
  1420. // Extend outer-most clip limits to match those of host, so draw calls can be merged even if
  1421. // outer-most columns have some outer padding offsetting them from their parent ClipRect.
  1422. // The principal cases this is dealing with are:
  1423. // - On a same-window table (not scrolling = single group), all fitting columns ClipRect -> will extend and match host ClipRect -> will merge
  1424. // - Columns can use padding and have left-most ClipRect.Min.x and right-most ClipRect.Max.x != from host ClipRect -> will extend and match host ClipRect -> will merge
  1425. // FIXME-TABLE FIXME-WORKRECT: We are wasting a merge opportunity on tables without scrolling if column doesn't fit
  1426. // within host clip rect, solely because of the half-padding difference between window->WorkRect and window->InnerClipRect.
  1427. if ((merge_group_n & 1) == 0 || !has_freeze_h)
  1428. merge_clip_rect.Min.x = ImMin(merge_clip_rect.Min.x, host_rect.Min.x);
  1429. if ((merge_group_n & 2) == 0 || !has_freeze_v)
  1430. merge_clip_rect.Min.y = ImMin(merge_clip_rect.Min.y, host_rect.Min.y);
  1431. if ((merge_group_n & 1) != 0)
  1432. merge_clip_rect.Max.x = ImMax(merge_clip_rect.Max.x, host_rect.Max.x);
  1433. if ((merge_group_n & 2) != 0 && (table->Flags & ImGuiTableFlags_NoHostExtendY) == 0)
  1434. merge_clip_rect.Max.y = ImMax(merge_clip_rect.Max.y, host_rect.Max.y);
  1435. #if 0
  1436. GetOverlayDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 0, 0, 200), 0.0f, ~0, 1.0f);
  1437. GetOverlayDrawList()->AddLine(merge_group->ClipRect.Min, merge_clip_rect.Min, IM_COL32(255, 100, 0, 200));
  1438. GetOverlayDrawList()->AddLine(merge_group->ClipRect.Max, merge_clip_rect.Max, IM_COL32(255, 100, 0, 200));
  1439. #endif
  1440. remaining_count -= merge_group->ChannelsCount;
  1441. for (int n = 0; n < IM_ARRAYSIZE(remaining_mask.Storage); n++)
  1442. remaining_mask.Storage[n] &= ~merge_group->ChannelsMask.Storage[n];
  1443. for (int n = 0; n < splitter->_Count && merge_channels_count != 0; n++)
  1444. {
  1445. // Copy + overwrite new clip rect
  1446. if (!merge_group->ChannelsMask.TestBit(n))
  1447. continue;
  1448. merge_group->ChannelsMask.ClearBit(n);
  1449. merge_channels_count--;
  1450. ImDrawChannel* channel = &splitter->_Channels[n];
  1451. IM_ASSERT(channel->_CmdBuffer.Size == 1 && merge_clip_rect.Contains(ImRect(channel->_CmdBuffer[0].ClipRect)));
  1452. channel->_CmdBuffer[0].ClipRect = merge_clip_rect.ToVec4();
  1453. memcpy(dst_tmp++, channel, sizeof(ImDrawChannel));
  1454. }
  1455. }
  1456. // Make sure Bg1DrawChannelUnfrozen appears in the middle of our groups (whereas Bg0 and Bg1 frozen are fixed to 0 and 1)
  1457. if (merge_group_n == 1 && has_freeze_v)
  1458. memcpy(dst_tmp++, &splitter->_Channels[table->Bg1DrawChannelUnfrozen], sizeof(ImDrawChannel));
  1459. }
  1460. // Append unmergeable channels that we didn't reorder at the end of the list
  1461. for (int n = 0; n < splitter->_Count && remaining_count != 0; n++)
  1462. {
  1463. if (!remaining_mask.TestBit(n))
  1464. continue;
  1465. ImDrawChannel* channel = &splitter->_Channels[n];
  1466. memcpy(dst_tmp++, channel, sizeof(ImDrawChannel));
  1467. remaining_count--;
  1468. }
  1469. IM_ASSERT(dst_tmp == g.DrawChannelsTempMergeBuffer.Data + g.DrawChannelsTempMergeBuffer.Size);
  1470. memcpy(splitter->_Channels.Data + LEADING_DRAW_CHANNELS, g.DrawChannelsTempMergeBuffer.Data, (splitter->_Count - LEADING_DRAW_CHANNELS) * sizeof(ImDrawChannel));
  1471. }
  1472. }
  1473. // We use a default parameter of 'init_width_or_weight == -1',
  1474. // - with ImGuiTableColumnFlags_WidthFixed, width <= 0 --> init width == auto
  1475. // - with ImGuiTableColumnFlags_WidthFixed, width > 0 --> init width == manual
  1476. // - with ImGuiTableColumnFlags_WidthStretch, weight < 0 --> init weight == 1.0f
  1477. // - with ImGuiTableColumnFlags_WidthStretch, weight >= 0 --> init weight == custom
  1478. // Widths are specified _without_ CellPadding. So if you specify a width of 100.0f the column will be 100.0f+Padding*2.0f and you can fit a 100.0-wide item in it.
  1479. void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, float init_width_or_weight, ImGuiID user_id)
  1480. {
  1481. ImGuiContext& g = *GImGui;
  1482. ImGuiTable* table = g.CurrentTable;
  1483. IM_ASSERT(table != NULL && "Need to call TableSetupColumn() after BeginTable()!");
  1484. IM_ASSERT(table->IsLayoutLocked == false && "Need to call call TableSetupColumn() before first row!");
  1485. IM_ASSERT(table->DeclColumnsCount >= 0 && table->DeclColumnsCount < table->ColumnsCount && "Called TableSetupColumn() too many times!");
  1486. ImGuiTableColumn* column = &table->Columns[table->DeclColumnsCount];
  1487. table->DeclColumnsCount++;
  1488. // When passing a width automatically enforce WidthFixed policy
  1489. // (vs TableFixColumnFlags would default to WidthAutoResize)
  1490. // (we write to FlagsIn which is a little misleading, another solution would be to pass init_width_or_weight to TableFixColumnFlags)
  1491. if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0)
  1492. if ((table->Flags & ImGuiTableFlags_ColumnsWidthFixed) && (init_width_or_weight > 0.0f))
  1493. flags |= ImGuiTableColumnFlags_WidthFixed;
  1494. column->UserID = user_id;
  1495. column->FlagsIn = flags;
  1496. column->Flags = TableFixColumnFlags(table, column->FlagsIn);
  1497. flags = column->Flags;
  1498. // Initialize defaults
  1499. if (flags & ImGuiTableColumnFlags_WidthStretch)
  1500. IM_ASSERT(init_width_or_weight != 0.0f && "Need to provide a valid weight!");
  1501. column->InitStretchWeightOrWidth = init_width_or_weight;
  1502. if (table->IsInitializing && column->WidthRequest < 0.0f && column->StretchWeight < 0.0f)
  1503. {
  1504. // Init width or weight
  1505. if ((flags & ImGuiTableColumnFlags_WidthFixed) && init_width_or_weight > 0.0f)
  1506. column->WidthRequest = init_width_or_weight;
  1507. if (flags & ImGuiTableColumnFlags_WidthStretch)
  1508. column->StretchWeight = (init_width_or_weight > 0.0f) ? init_width_or_weight : 1.0f;
  1509. // Disable auto-fit if an explicit fixed width has been specified
  1510. if (init_width_or_weight > 0.0f)
  1511. column->AutoFitQueue = 0x00;
  1512. }
  1513. if (table->IsInitializing)
  1514. {
  1515. // Init default visibility/sort state
  1516. if ((flags & ImGuiTableColumnFlags_DefaultHide) && (table->SettingsLoadedFlags & ImGuiTableFlags_Hideable) == 0)
  1517. column->IsEnabled = column->IsEnabledNextFrame = false;
  1518. if (flags & ImGuiTableColumnFlags_DefaultSort && (table->SettingsLoadedFlags & ImGuiTableFlags_Sortable) == 0)
  1519. {
  1520. column->SortOrder = 0; // Multiple columns using _DefaultSort will be reordered when building the sort specs.
  1521. column->SortDirection = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? (ImS8)ImGuiSortDirection_Descending : (ImU8)(ImGuiSortDirection_Ascending);
  1522. }
  1523. }
  1524. // Store name (append with zero-terminator in contiguous buffer)
  1525. IM_ASSERT(column->NameOffset == -1);
  1526. if (label != NULL && label[0] != 0)
  1527. {
  1528. column->NameOffset = (ImS16)table->ColumnsNames.size();
  1529. table->ColumnsNames.append(label, label + strlen(label) + 1);
  1530. }
  1531. }
  1532. void ImGui::TableSetupScrollFreeze(int columns, int rows)
  1533. {
  1534. ImGuiContext& g = *GImGui;
  1535. ImGuiTable* table = g.CurrentTable;
  1536. IM_ASSERT(table != NULL && "Need to call TableSetupColumn() after BeginTable()!");
  1537. IM_ASSERT(table->IsLayoutLocked == false && "Need to call TableSetupColumn() before first row!");
  1538. IM_ASSERT(columns >= 0 && columns < IMGUI_TABLE_MAX_COLUMNS);
  1539. IM_ASSERT(rows >= 0 && rows < 128); // Arbitrary limit
  1540. table->FreezeColumnsRequest = (table->Flags & ImGuiTableFlags_ScrollX) ? (ImS8)columns : 0;
  1541. table->FreezeColumnsCount = (table->InnerWindow->Scroll.x != 0.0f) ? table->FreezeColumnsRequest : 0;
  1542. table->FreezeRowsRequest = (table->Flags & ImGuiTableFlags_ScrollY) ? (ImS8)rows : 0;
  1543. table->FreezeRowsCount = (table->InnerWindow->Scroll.y != 0.0f) ? table->FreezeRowsRequest : 0;
  1544. table->IsUnfrozen = (table->FreezeRowsCount == 0); // Make sure this is set before TableUpdateLayout() so ImGuiListClipper can benefit from it.b
  1545. }
  1546. // Starts into the first cell of a new row
  1547. void ImGui::TableNextRow(ImGuiTableRowFlags row_flags, float row_min_height)
  1548. {
  1549. ImGuiContext& g = *GImGui;
  1550. ImGuiTable* table = g.CurrentTable;
  1551. if (!table->IsLayoutLocked)
  1552. TableUpdateLayout(table);
  1553. if (table->IsInsideRow)
  1554. TableEndRow(table);
  1555. table->LastRowFlags = table->RowFlags;
  1556. table->RowFlags = row_flags;
  1557. table->RowMinHeight = row_min_height;
  1558. TableBeginRow(table);
  1559. // We honor min_row_height requested by user, but cannot guarantee per-row maximum height,
  1560. // because that would essentially require a unique clipping rectangle per-cell.
  1561. table->RowPosY2 += table->CellPaddingY * 2.0f;
  1562. table->RowPosY2 = ImMax(table->RowPosY2, table->RowPosY1 + row_min_height);
  1563. // Disable output until user calls TableNextColumn()
  1564. table->InnerWindow->SkipItems = true;
  1565. }
  1566. // [Internal] Called by TableNextRow()!
  1567. void ImGui::TableBeginRow(ImGuiTable* table)
  1568. {
  1569. ImGuiWindow* window = table->InnerWindow;
  1570. IM_ASSERT(!table->IsInsideRow);
  1571. // New row
  1572. table->CurrentRow++;
  1573. table->CurrentColumn = -1;
  1574. table->RowBgColor[0] = table->RowBgColor[1] = IM_COL32_DISABLE;
  1575. table->RowCellDataCurrent = -1;
  1576. table->IsInsideRow = true;
  1577. // Begin frozen rows
  1578. float next_y1 = table->RowPosY2;
  1579. if (table->CurrentRow == 0 && table->FreezeRowsCount > 0)
  1580. next_y1 = window->DC.CursorPos.y = table->OuterRect.Min.y;
  1581. table->RowPosY1 = table->RowPosY2 = next_y1;
  1582. table->RowTextBaseline = 0.0f;
  1583. table->RowIndentOffsetX = window->DC.Indent.x - table->HostIndentX; // Lock indent
  1584. window->DC.PrevLineTextBaseOffset = 0.0f;
  1585. window->DC.CursorMaxPos.y = next_y1;
  1586. // Making the header BG color non-transparent will allow us to overlay it multiple times when handling smooth dragging.
  1587. if (table->RowFlags & ImGuiTableRowFlags_Headers)
  1588. {
  1589. TableSetBgColor(ImGuiTableBgTarget_RowBg0, GetColorU32(ImGuiCol_TableHeaderBg));
  1590. if (table->CurrentRow == 0)
  1591. table->IsUsingHeaders = true;
  1592. }
  1593. }
  1594. // [Internal] Called by TableNextRow()!
  1595. void ImGui::TableEndRow(ImGuiTable* table)
  1596. {
  1597. ImGuiContext& g = *GImGui;
  1598. ImGuiWindow* window = g.CurrentWindow;
  1599. IM_ASSERT(window == table->InnerWindow);
  1600. IM_ASSERT(table->IsInsideRow);
  1601. if (table->CurrentColumn != -1)
  1602. TableEndCell(table);
  1603. // Position cursor at the bottom of our row so it can be used for e.g. clipping calculation. However it is
  1604. // likely that the next call to TableBeginCell() will reposition the cursor to take account of vertical padding.
  1605. window->DC.CursorPos.y = table->RowPosY2;
  1606. // Row background fill
  1607. const float bg_y1 = table->RowPosY1;
  1608. const float bg_y2 = table->RowPosY2;
  1609. const bool unfreeze_rows_actual = (table->CurrentRow + 1 == table->FreezeRowsCount);
  1610. const bool unfreeze_rows_request = (table->CurrentRow + 1 == table->FreezeRowsRequest);
  1611. if (table->CurrentRow == 0)
  1612. table->LastFirstRowHeight = bg_y2 - bg_y1;
  1613. const bool is_visible = (bg_y2 >= table->InnerClipRect.Min.y && bg_y1 <= table->InnerClipRect.Max.y);
  1614. if (is_visible)
  1615. {
  1616. // Decide of background color for the row
  1617. ImU32 bg_col0 = 0;
  1618. ImU32 bg_col1 = 0;
  1619. if (table->RowBgColor[0] != IM_COL32_DISABLE)
  1620. bg_col0 = table->RowBgColor[0];
  1621. else if (table->Flags & ImGuiTableFlags_RowBg)
  1622. bg_col0 = GetColorU32((table->RowBgColorCounter & 1) ? ImGuiCol_TableRowBgAlt : ImGuiCol_TableRowBg);
  1623. if (table->RowBgColor[1] != IM_COL32_DISABLE)
  1624. bg_col1 = table->RowBgColor[1];
  1625. // Decide of top border color
  1626. ImU32 border_col = 0;
  1627. const float border_size = TABLE_BORDER_SIZE;
  1628. if (table->CurrentRow > 0 || table->InnerWindow == table->OuterWindow)
  1629. if (table->Flags & ImGuiTableFlags_BordersInnerH)
  1630. border_col = (table->LastRowFlags & ImGuiTableRowFlags_Headers) ? table->BorderColorStrong : table->BorderColorLight;
  1631. const bool draw_cell_bg_color = table->RowCellDataCurrent >= 0;
  1632. const bool draw_strong_bottom_border = unfreeze_rows_actual;
  1633. if ((bg_col0 | bg_col1 | border_col) != 0 || draw_strong_bottom_border || draw_cell_bg_color)
  1634. {
  1635. // In theory we could call SetWindowClipRectBeforeSetChannel() but since we know TableEndRow() is
  1636. // always followed by a change of clipping rectangle we perform the smallest overwrite possible here.
  1637. if ((table->Flags & ImGuiTableFlags_NoClip) == 0)
  1638. window->DrawList->_CmdHeader.ClipRect = table->BgClipRectForDrawCmd.ToVec4();
  1639. table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_BG0);
  1640. }
  1641. // Draw row background
  1642. // We soft/cpu clip this so all backgrounds and borders can share the same clipping rectangle
  1643. if (bg_col0 || bg_col1)
  1644. {
  1645. ImRect row_rect(table->WorkRect.Min.x, bg_y1, table->WorkRect.Max.x, bg_y2);
  1646. row_rect.ClipWith(table->BgClipRect);
  1647. if (bg_col0 != 0 && row_rect.Min.y < row_rect.Max.y)
  1648. window->DrawList->AddRectFilled(row_rect.Min, row_rect.Max, bg_col0);
  1649. if (bg_col1 != 0 && row_rect.Min.y < row_rect.Max.y)
  1650. window->DrawList->AddRectFilled(row_rect.Min, row_rect.Max, bg_col1);
  1651. }
  1652. // Draw cell background color
  1653. if (draw_cell_bg_color)
  1654. {
  1655. ImGuiTableCellData* cell_data_end = &table->RowCellData[table->RowCellDataCurrent];
  1656. for (ImGuiTableCellData* cell_data = &table->RowCellData[0]; cell_data <= cell_data_end; cell_data++)
  1657. {
  1658. const ImGuiTableColumn* column = &table->Columns[cell_data->Column];
  1659. ImRect cell_bg_rect = TableGetCellBgRect(table, cell_data->Column);
  1660. cell_bg_rect.ClipWith(table->BgClipRect);
  1661. cell_bg_rect.Min.x = ImMax(cell_bg_rect.Min.x, column->ClipRect.Min.x); // So that first column after frozen one gets clipped
  1662. cell_bg_rect.Max.x = ImMin(cell_bg_rect.Max.x, column->MaxX);
  1663. window->DrawList->AddRectFilled(cell_bg_rect.Min, cell_bg_rect.Max, cell_data->BgColor);
  1664. }
  1665. }
  1666. // Draw top border
  1667. if (border_col && bg_y1 >= table->BgClipRect.Min.y && bg_y1 < table->BgClipRect.Max.y)
  1668. window->DrawList->AddLine(ImVec2(table->BorderX1, bg_y1), ImVec2(table->BorderX2, bg_y1), border_col, border_size);
  1669. // Draw bottom border at the row unfreezing mark (always strong)
  1670. if (draw_strong_bottom_border && bg_y2 >= table->BgClipRect.Min.y && bg_y2 < table->BgClipRect.Max.y)
  1671. window->DrawList->AddLine(ImVec2(table->BorderX1, bg_y2), ImVec2(table->BorderX2, bg_y2), table->BorderColorStrong, border_size);
  1672. }
  1673. // End frozen rows (when we are past the last frozen row line, teleport cursor and alter clipping rectangle)
  1674. // We need to do that in TableEndRow() instead of TableBeginRow() so the list clipper can mark end of row and
  1675. // get the new cursor position.
  1676. if (unfreeze_rows_request)
  1677. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1678. {
  1679. ImGuiTableColumn* column = &table->Columns[column_n];
  1680. column->NavLayerCurrent = (ImS8)((column_n < table->FreezeColumnsCount) ? ImGuiNavLayer_Menu : ImGuiNavLayer_Main);
  1681. }
  1682. if (unfreeze_rows_actual)
  1683. {
  1684. IM_ASSERT(table->IsUnfrozen == false);
  1685. table->IsUnfrozen = true;
  1686. // BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect
  1687. float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y);
  1688. table->BgClipRect.Min.y = table->BgClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y);
  1689. table->BgClipRect.Max.y = table->BgClipRectForDrawCmd.Max.y = window->InnerClipRect.Max.y;
  1690. table->Bg1DrawChannelCurrent = table->Bg1DrawChannelUnfrozen;
  1691. IM_ASSERT(table->BgClipRectForDrawCmd.Min.y <= table->BgClipRectForDrawCmd.Max.y);
  1692. float row_height = table->RowPosY2 - table->RowPosY1;
  1693. table->RowPosY2 = window->DC.CursorPos.y = table->WorkRect.Min.y + table->RowPosY2 - table->OuterRect.Min.y;
  1694. table->RowPosY1 = table->RowPosY2 - row_height;
  1695. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1696. {
  1697. ImGuiTableColumn* column = &table->Columns[column_n];
  1698. column->DrawChannelCurrent = column->DrawChannelUnfrozen;
  1699. column->ClipRect.Min.y = table->BgClipRectForDrawCmd.Min.y;
  1700. }
  1701. // Update cliprect ahead of TableBeginCell() so clipper can access to new ClipRect->Min.y
  1702. SetWindowClipRectBeforeSetChannel(window, table->Columns[0].ClipRect);
  1703. table->DrawSplitter.SetCurrentChannel(window->DrawList, table->Columns[0].DrawChannelCurrent);
  1704. }
  1705. if (!(table->RowFlags & ImGuiTableRowFlags_Headers))
  1706. table->RowBgColorCounter++;
  1707. table->IsInsideRow = false;
  1708. }
  1709. // [Internal] Called by TableNextColumn()!
  1710. // This is called very frequently, so we need to be mindful of unnecessary overhead.
  1711. // FIXME-TABLE FIXME-OPT: Could probably shortcut some things for non-active or clipped columns.
  1712. void ImGui::TableBeginCell(ImGuiTable* table, int column_n)
  1713. {
  1714. ImGuiTableColumn* column = &table->Columns[column_n];
  1715. ImGuiWindow* window = table->InnerWindow;
  1716. table->CurrentColumn = column_n;
  1717. // Start position is roughly ~~ CellRect.Min + CellPadding + Indent
  1718. float start_x = column->WorkMinX;
  1719. if (column->Flags & ImGuiTableColumnFlags_IndentEnable)
  1720. start_x += table->RowIndentOffsetX; // ~~ += window.DC.Indent.x - table->HostIndentX, except we locked it for the row.
  1721. window->DC.CursorPos.x = start_x;
  1722. window->DC.CursorPos.y = table->RowPosY1 + table->CellPaddingY;
  1723. window->DC.CursorMaxPos.x = window->DC.CursorPos.x;
  1724. window->DC.ColumnsOffset.x = start_x - window->Pos.x - window->DC.Indent.x; // FIXME-WORKRECT
  1725. window->DC.CurrLineTextBaseOffset = table->RowTextBaseline;
  1726. window->DC.LastItemId = 0;
  1727. window->DC.NavLayerCurrent = (ImGuiNavLayer)column->NavLayerCurrent;
  1728. window->WorkRect.Min.y = window->DC.CursorPos.y;
  1729. window->WorkRect.Min.x = column->WorkMinX;
  1730. window->WorkRect.Max.x = column->WorkMaxX;
  1731. window->DC.ItemWidth = column->ItemWidth;
  1732. // To allow ImGuiListClipper to function we propagate our row height
  1733. if (!column->IsEnabled)
  1734. window->DC.CursorPos.y = ImMax(window->DC.CursorPos.y, table->RowPosY2);
  1735. window->SkipItems = column->IsSkipItems;
  1736. if (table->Flags & ImGuiTableFlags_NoClip)
  1737. {
  1738. // FIXME: if we end up drawing all borders/bg in EndTable, could remove this and just assert that channel hasn't changed.
  1739. table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_UNCLIPPED);
  1740. //IM_ASSERT(table->DrawSplitter._Current == TABLE_DRAW_CHANNEL_UNCLIPPED);
  1741. }
  1742. else
  1743. {
  1744. // FIXME-TABLE: Could avoid this if draw channel is dummy channel?
  1745. SetWindowClipRectBeforeSetChannel(window, column->ClipRect);
  1746. table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
  1747. }
  1748. }
  1749. // [Internal] Called by TableNextRow()/TableNextColumn()!
  1750. void ImGui::TableEndCell(ImGuiTable* table)
  1751. {
  1752. ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
  1753. ImGuiWindow* window = table->InnerWindow;
  1754. // Report maximum position so we can infer content size per column.
  1755. float* p_max_pos_x;
  1756. if (table->RowFlags & ImGuiTableRowFlags_Headers)
  1757. p_max_pos_x = &column->ContentMaxXHeadersUsed; // Useful in case user submit contents in header row that is not a TableHeader() call
  1758. else
  1759. p_max_pos_x = table->IsUnfrozen ? &column->ContentMaxXUnfrozen : &column->ContentMaxXFrozen;
  1760. *p_max_pos_x = ImMax(*p_max_pos_x, window->DC.CursorMaxPos.x);
  1761. table->RowPosY2 = ImMax(table->RowPosY2, window->DC.CursorMaxPos.y + table->CellPaddingY);
  1762. column->ItemWidth = window->DC.ItemWidth;
  1763. // Propagate text baseline for the entire row
  1764. // FIXME-TABLE: Here we propagate text baseline from the last line of the cell.. instead of the first one.
  1765. table->RowTextBaseline = ImMax(table->RowTextBaseline, window->DC.PrevLineTextBaseOffset);
  1766. }
  1767. // Append into the next column/cell
  1768. bool ImGui::TableNextColumn()
  1769. {
  1770. ImGuiContext& g = *GImGui;
  1771. ImGuiTable* table = g.CurrentTable;
  1772. if (!table)
  1773. return false;
  1774. if (table->IsInsideRow && table->CurrentColumn + 1 < table->ColumnsCount)
  1775. {
  1776. if (table->CurrentColumn != -1)
  1777. TableEndCell(table);
  1778. TableBeginCell(table, table->CurrentColumn + 1);
  1779. }
  1780. else
  1781. {
  1782. TableNextRow();
  1783. TableBeginCell(table, 0);
  1784. }
  1785. // FIXME-TABLE: it is likely to alter layout if user skips a columns contents based on clipping.
  1786. int column_n = table->CurrentColumn;
  1787. return (table->EnabledUnclippedMaskByIndex & ((ImU64)1 << column_n)) != 0;
  1788. }
  1789. bool ImGui::TableSetColumnIndex(int column_n)
  1790. {
  1791. ImGuiContext& g = *GImGui;
  1792. ImGuiTable* table = g.CurrentTable;
  1793. if (!table)
  1794. return false;
  1795. if (table->CurrentColumn != column_n)
  1796. {
  1797. if (table->CurrentColumn != -1)
  1798. TableEndCell(table);
  1799. IM_ASSERT(column_n >= 0 && table->ColumnsCount);
  1800. TableBeginCell(table, column_n);
  1801. }
  1802. // FIXME-TABLE: it is likely to alter layout if user skips a columns contents based on clipping.
  1803. return (table->EnabledUnclippedMaskByIndex & ((ImU64)1 << column_n)) != 0;
  1804. }
  1805. int ImGui::TableGetColumnCount()
  1806. {
  1807. ImGuiContext& g = *GImGui;
  1808. ImGuiTable* table = g.CurrentTable;
  1809. return table ? table->ColumnsCount : 0;
  1810. }
  1811. const char* ImGui::TableGetColumnName(int column_n)
  1812. {
  1813. ImGuiContext& g = *GImGui;
  1814. ImGuiTable* table = g.CurrentTable;
  1815. if (!table)
  1816. return NULL;
  1817. if (column_n < 0)
  1818. column_n = table->CurrentColumn;
  1819. return TableGetColumnName(table, column_n);
  1820. }
  1821. bool ImGui::TableGetColumnIsEnabled(int column_n)
  1822. {
  1823. ImGuiContext& g = *GImGui;
  1824. ImGuiTable* table = g.CurrentTable;
  1825. if (!table)
  1826. return false;
  1827. if (column_n < 0)
  1828. column_n = table->CurrentColumn;
  1829. return (table->EnabledMaskByIndex & ((ImU64)1 << column_n)) == 0;
  1830. }
  1831. void ImGui::TableSetColumnIsEnabled(int column_n, bool hidden)
  1832. {
  1833. ImGuiContext& g = *GImGui;
  1834. ImGuiTable* table = g.CurrentTable;
  1835. IM_ASSERT(table != NULL && table->IsLayoutLocked == false);
  1836. if (column_n < 0)
  1837. column_n = table->CurrentColumn;
  1838. IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount);
  1839. table->Columns[column_n].IsEnabledNextFrame = !hidden;
  1840. }
  1841. int ImGui::TableGetColumnIndex()
  1842. {
  1843. ImGuiContext& g = *GImGui;
  1844. ImGuiTable* table = g.CurrentTable;
  1845. if (!table)
  1846. return 0;
  1847. return table->CurrentColumn;
  1848. }
  1849. // Note: for row coloring we use ->RowBgColorCounter which is the same value without counting header rows
  1850. int ImGui::TableGetRowIndex()
  1851. {
  1852. ImGuiContext& g = *GImGui;
  1853. ImGuiTable* table = g.CurrentTable;
  1854. if (!table)
  1855. return 0;
  1856. return table->CurrentRow;
  1857. }
  1858. // Return the cell rectangle based on currently known height.
  1859. // - Important: we generally don't know our row height until the end of the row, so Max.y will be incorrect in many situations.
  1860. // The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it.
  1861. // - Important: if ImGuiTableFlags_PadOuterX is set but ImGuiTableFlags_PadInnerX is not set, the outer-most left and right
  1862. // columns report a small offset so their CellBgRect can extend up to the outer border.
  1863. ImRect ImGui::TableGetCellBgRect(const ImGuiTable* table, int column_n)
  1864. {
  1865. const ImGuiTableColumn* column = &table->Columns[column_n];
  1866. float x1 = column->MinX;
  1867. float x2 = column->MaxX;
  1868. if (column->PrevEnabledColumn == -1)
  1869. x1 -= table->CellSpacingX1;
  1870. if (column->NextEnabledColumn == -1)
  1871. x2 += table->CellSpacingX2;
  1872. return ImRect(x1, table->RowPosY1, x2, table->RowPosY2);
  1873. }
  1874. const char* ImGui::TableGetColumnName(const ImGuiTable* table, int column_n)
  1875. {
  1876. const ImGuiTableColumn* column = &table->Columns[column_n];
  1877. if (column->NameOffset == -1)
  1878. return "";
  1879. return &table->ColumnsNames.Buf[column->NameOffset];
  1880. }
  1881. // Return the resizing ID for the right-side of the given column.
  1882. ImGuiID ImGui::TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no)
  1883. {
  1884. IM_ASSERT(column_n < table->ColumnsCount);
  1885. ImGuiID id = table->ID + 1 + (instance_no * table->ColumnsCount) + column_n;
  1886. return id;
  1887. }
  1888. // Disable clipping then auto-fit, will take 2 frames
  1889. // (we don't take a shortcut for unclipped columns to reduce inconsistencies when e.g. resizing multiple columns)
  1890. void ImGui::TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n)
  1891. {
  1892. // Single auto width uses auto-fit
  1893. ImGuiTableColumn* column = &table->Columns[column_n];
  1894. if (!column->IsEnabled)
  1895. return;
  1896. column->CannotSkipItemsQueue = (1 << 0);
  1897. column->AutoFitQueue = (1 << 1);
  1898. if (column->Flags & ImGuiTableColumnFlags_WidthStretch)
  1899. table->AutoFitSingleStretchColumn = (ImS8)column_n;
  1900. }
  1901. void ImGui::TableSetColumnWidthAutoAll(ImGuiTable* table)
  1902. {
  1903. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  1904. {
  1905. ImGuiTableColumn* column = &table->Columns[column_n];
  1906. if (!column->IsEnabled)
  1907. continue;
  1908. column->CannotSkipItemsQueue = (1 << 0);
  1909. column->AutoFitQueue = (1 << 1);
  1910. }
  1911. }
  1912. void ImGui::TablePushBackgroundChannel()
  1913. {
  1914. ImGuiContext& g = *GImGui;
  1915. ImGuiWindow* window = g.CurrentWindow;
  1916. ImGuiTable* table = g.CurrentTable;
  1917. // Optimization: avoid SetCurrentChannel() + PushClipRect()
  1918. table->HostBackupClipRect = window->ClipRect;
  1919. SetWindowClipRectBeforeSetChannel(window, table->BgClipRectForDrawCmd);
  1920. table->DrawSplitter.SetCurrentChannel(window->DrawList, table->Bg1DrawChannelCurrent);
  1921. }
  1922. void ImGui::TablePopBackgroundChannel()
  1923. {
  1924. ImGuiContext& g = *GImGui;
  1925. ImGuiWindow* window = g.CurrentWindow;
  1926. ImGuiTable* table = g.CurrentTable;
  1927. ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
  1928. // Optimization: avoid PopClipRect() + SetCurrentChannel()
  1929. SetWindowClipRectBeforeSetChannel(window, table->HostBackupClipRect);
  1930. table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
  1931. }
  1932. // Output context menu into current window (generally a popup)
  1933. // FIXME-TABLE: Ideally this should be writable by the user. Full programmatic access to that data?
  1934. void ImGui::TableDrawContextMenu(ImGuiTable* table)
  1935. {
  1936. ImGuiContext& g = *GImGui;
  1937. ImGuiWindow* window = g.CurrentWindow;
  1938. if (window->SkipItems)
  1939. return;
  1940. bool want_separator = false;
  1941. const int column_n = (table->ContextPopupColumn >= 0 && table->ContextPopupColumn < table->ColumnsCount) ? table->ContextPopupColumn : -1;
  1942. ImGuiTableColumn* column = (column_n != -1) ? &table->Columns[column_n] : NULL;
  1943. // Sizing
  1944. if (table->Flags & ImGuiTableFlags_Resizable)
  1945. {
  1946. if (column != NULL)
  1947. {
  1948. const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled;
  1949. if (MenuItem("Size column to fit", NULL, false, can_resize))
  1950. TableSetColumnWidthAutoSingle(table, column_n);
  1951. }
  1952. const char* size_all_desc;
  1953. if (table->ColumnsEnabledFixedCount == table->ColumnsEnabledCount)
  1954. size_all_desc = "Size all columns to fit"; // All fixed
  1955. else if (table->ColumnsEnabledFixedCount == 0)
  1956. size_all_desc = "Size all columns to default"; // All stretch
  1957. else
  1958. size_all_desc = "Size all columns to fit/default"; // Mixed
  1959. if (MenuItem(size_all_desc, NULL))
  1960. TableSetColumnWidthAutoAll(table);
  1961. want_separator = true;
  1962. }
  1963. // Ordering
  1964. if (table->Flags & ImGuiTableFlags_Reorderable)
  1965. {
  1966. if (MenuItem("Reset order", NULL, false, !table->IsDefaultDisplayOrder))
  1967. table->IsResetDisplayOrderRequest = true;
  1968. want_separator = true;
  1969. }
  1970. // Sorting
  1971. // (modify TableOpenContextMenu() to add _Sortable flag if enabling this)
  1972. #if 0
  1973. if ((table->Flags & ImGuiTableFlags_Sortable) && column != NULL && (column->Flags & ImGuiTableColumnFlags_NoSort) == 0)
  1974. {
  1975. if (want_separator)
  1976. Separator();
  1977. want_separator = true;
  1978. bool append_to_sort_specs = g.IO.KeyShift;
  1979. if (MenuItem("Sort in Ascending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Ascending, (column->Flags & ImGuiTableColumnFlags_NoSortAscending) == 0))
  1980. TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Ascending, append_to_sort_specs);
  1981. if (MenuItem("Sort in Descending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Descending, (column->Flags & ImGuiTableColumnFlags_NoSortDescending) == 0))
  1982. TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Descending, append_to_sort_specs);
  1983. }
  1984. #endif
  1985. // Hiding / Visibility
  1986. if (table->Flags & ImGuiTableFlags_Hideable)
  1987. {
  1988. if (want_separator)
  1989. Separator();
  1990. want_separator = true;
  1991. PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true);
  1992. for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
  1993. {
  1994. ImGuiTableColumn* other_column = &table->Columns[other_column_n];
  1995. const char* name = TableGetColumnName(table, other_column_n);
  1996. if (name == NULL || name[0] == 0)
  1997. name = "<Unknown>";
  1998. // Make sure we can't hide the last active column
  1999. bool menu_item_active = (other_column->Flags & ImGuiTableColumnFlags_NoHide) ? false : true;
  2000. if (other_column->IsEnabled && table->ColumnsEnabledCount <= 1)
  2001. menu_item_active = false;
  2002. if (MenuItem(name, NULL, other_column->IsEnabled, menu_item_active))
  2003. other_column->IsEnabledNextFrame = !other_column->IsEnabled;
  2004. }
  2005. PopItemFlag();
  2006. }
  2007. }
  2008. // Use -1 to open menu not specific to a given column.
  2009. void ImGui::TableOpenContextMenu(int column_n)
  2010. {
  2011. ImGuiContext& g = *GImGui;
  2012. ImGuiTable* table = g.CurrentTable;
  2013. if (column_n == -1 && table->CurrentColumn != -1) // When called within a column automatically use this one (for consistency)
  2014. column_n = table->CurrentColumn;
  2015. if (column_n == table->ColumnsCount) // To facilitate using with TableGetHoveredColumn()
  2016. column_n = -1;
  2017. IM_ASSERT(column_n >= -1 && column_n < table->ColumnsCount);
  2018. if (table->Flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
  2019. {
  2020. table->IsContextPopupOpen = true;
  2021. table->ContextPopupColumn = (ImS8)column_n;
  2022. table->InstanceInteracted = table->InstanceCurrent;
  2023. const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID);
  2024. OpenPopupEx(context_menu_id, ImGuiPopupFlags_None);
  2025. }
  2026. }
  2027. // This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn().
  2028. // The intent is that advanced users willing to create customized headers would not need to use this helper
  2029. // and can create their own! For example: TableHeader() may be preceeded by Checkbox() or other custom widgets.
  2030. // See 'Demo->Tables->Custom headers' for a demonstration of implementing a custom version of this.
  2031. void ImGui::TableHeadersRow()
  2032. {
  2033. ImGuiStyle& style = ImGui::GetStyle();
  2034. ImGuiContext& g = *GImGui;
  2035. ImGuiTable* table = g.CurrentTable;
  2036. IM_ASSERT(table != NULL && "Need to call TableHeadersRow() after BeginTable()!");
  2037. // Calculate row height (for the unlikely case that labels may be are multi-line)
  2038. // If we didn't do that, uneven header height would work but their highlight won't cover the full row height.
  2039. float row_height = GetTextLineHeight();
  2040. const float row_y1 = GetCursorScreenPos().y;
  2041. const int columns_count = TableGetColumnCount();
  2042. for (int column_n = 0; column_n < columns_count; column_n++)
  2043. if (TableGetColumnIsEnabled(column_n))
  2044. row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(column_n)).y);
  2045. row_height += style.CellPadding.y * 2.0f;
  2046. // Open row
  2047. TableNextRow(ImGuiTableRowFlags_Headers, row_height);
  2048. if (table->HostSkipItems) // Merely an optimization, you may skip in your own code.
  2049. return;
  2050. // This for loop is constructed to not make use of internal functions,
  2051. // as this is intended to be a base template to copy and build from.
  2052. for (int column_n = 0; column_n < columns_count; column_n++)
  2053. {
  2054. if (!TableSetColumnIndex(column_n))
  2055. continue;
  2056. // [DEBUG] Test custom user elements
  2057. #if 0
  2058. if (column_n < 2)
  2059. {
  2060. static bool b[2] = {};
  2061. PushID(column_n);
  2062. PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
  2063. Checkbox("##", &b[column_n]);
  2064. PopStyleVar();
  2065. PopID();
  2066. SameLine(0.0f, style.ItemInnerSpacing.x);
  2067. }
  2068. #endif
  2069. // Push an id to allow unnamed labels (generally accidental, but let's behave nicely with them)
  2070. // - in your own code you may omit the PushID/PopID all-together, provided you know they won't collide
  2071. // - table->InstanceCurrent is only >0 when we use multiple BeginTable/EndTable calls with same identifier.
  2072. const char* name = TableGetColumnName(column_n);
  2073. PushID(table->InstanceCurrent * table->ColumnsCount + column_n);
  2074. TableHeader(name);
  2075. PopID();
  2076. }
  2077. // Allow opening popup from the right-most section after the last column.
  2078. // FIXME-TABLE: TableOpenContextMenu() is not public yet.
  2079. ImVec2 mouse_pos = ImGui::GetMousePos();
  2080. if (IsMouseReleased(1) && TableGetHoveredColumn() == columns_count)
  2081. if (mouse_pos.y >= row_y1 && mouse_pos.y < row_y1 + row_height)
  2082. TableOpenContextMenu(-1); // Will open a non-column-specific popup.
  2083. }
  2084. // Emit a column header (text + optional sort order)
  2085. // We cpu-clip text here so that all columns headers can be merged into a same draw call.
  2086. // Note that because of how we cpu-clip and display sorting indicators, you _cannot_ use SameLine() after a TableHeader()
  2087. // FIXME-TABLE: Style confusion between CellPadding.y and FramePadding.y
  2088. void ImGui::TableHeader(const char* label)
  2089. {
  2090. ImGuiContext& g = *GImGui;
  2091. ImGuiWindow* window = g.CurrentWindow;
  2092. if (window->SkipItems)
  2093. return;
  2094. ImGuiTable* table = g.CurrentTable;
  2095. IM_ASSERT(table != NULL && "Need to call TableHeader() after BeginTable()!");
  2096. IM_ASSERT(table->CurrentColumn != -1);
  2097. const int column_n = table->CurrentColumn;
  2098. ImGuiTableColumn* column = &table->Columns[column_n];
  2099. // Label
  2100. if (label == NULL)
  2101. label = "";
  2102. const char* label_end = FindRenderedTextEnd(label);
  2103. ImVec2 label_size = CalcTextSize(label, label_end, true);
  2104. ImVec2 label_pos = window->DC.CursorPos;
  2105. // If we already got a row height, there's use that.
  2106. // FIXME-TABLE-PADDING: Problem if the correct outer-padding CellBgRect strays off our ClipRect
  2107. ImRect cell_r = TableGetCellBgRect(table, column_n);
  2108. float label_height = ImMax(label_size.y, table->RowMinHeight - table->CellPaddingY * 2.0f);
  2109. // Keep header highlighted when context menu is open.
  2110. const bool selected = (table->IsContextPopupOpen && table->ContextPopupColumn == column_n && table->InstanceInteracted == table->InstanceCurrent);
  2111. ImGuiID id = window->GetID(label);
  2112. ImRect bb(cell_r.Min.x, cell_r.Min.y, cell_r.Max.x, ImMax(cell_r.Max.y, cell_r.Min.y + label_height + g.Style.CellPadding.y * 2.0f));
  2113. ItemSize(ImVec2(0.0f, label_height)); // Don't declare unclipped width, it'll be fed ContentMaxPosHeadersIdeal
  2114. if (!ItemAdd(bb, id))
  2115. return;
  2116. //GetForegroundDrawList()->AddRect(cell_r.Min, cell_r.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG]
  2117. //GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG]
  2118. bool hovered, held;
  2119. bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None);
  2120. if (hovered || selected)
  2121. {
  2122. const ImU32 col = GetColorU32(held ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
  2123. //RenderFrame(bb.Min, bb.Max, col, false, 0.0f);
  2124. TableSetBgColor(ImGuiTableBgTarget_CellBg, col, table->CurrentColumn);
  2125. RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding);
  2126. }
  2127. if (held)
  2128. table->HeldHeaderColumn = (ImS8)column_n;
  2129. window->DC.CursorPos.y -= g.Style.ItemSpacing.y * 0.5f;
  2130. // Drag and drop to re-order columns.
  2131. // FIXME-TABLE: Scroll request while reordering a column and it lands out of the scrolling zone.
  2132. if (held && (table->Flags & ImGuiTableFlags_Reorderable) && IsMouseDragging(0) && !g.DragDropActive)
  2133. {
  2134. // While moving a column it will jump on the other side of the mouse, so we also test for MouseDelta.x
  2135. table->ReorderColumn = (ImS8)column_n;
  2136. table->InstanceInteracted = table->InstanceCurrent;
  2137. // We don't reorder: through the frozen<>unfrozen line, or through a column that is marked with ImGuiTableColumnFlags_NoReorder.
  2138. if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < cell_r.Min.x)
  2139. if (ImGuiTableColumn* prev_column = (column->PrevEnabledColumn != -1) ? &table->Columns[column->PrevEnabledColumn] : NULL)
  2140. if (!((column->Flags | prev_column->Flags) & ImGuiTableColumnFlags_NoReorder))
  2141. if ((column->IndexWithinEnabledSet < table->FreezeColumnsRequest) == (prev_column->IndexWithinEnabledSet < table->FreezeColumnsRequest))
  2142. table->ReorderColumnDir = -1;
  2143. if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > cell_r.Max.x)
  2144. if (ImGuiTableColumn* next_column = (column->NextEnabledColumn != -1) ? &table->Columns[column->NextEnabledColumn] : NULL)
  2145. if (!((column->Flags | next_column->Flags) & ImGuiTableColumnFlags_NoReorder))
  2146. if ((column->IndexWithinEnabledSet < table->FreezeColumnsRequest) == (next_column->IndexWithinEnabledSet < table->FreezeColumnsRequest))
  2147. table->ReorderColumnDir = +1;
  2148. }
  2149. // Sort order arrow
  2150. float w_arrow = 0.0f;
  2151. float w_sort_text = 0.0f;
  2152. float ellipsis_max = cell_r.Max.x;
  2153. if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort))
  2154. {
  2155. const float ARROW_SCALE = 0.65f;
  2156. w_arrow = ImFloor(g.FontSize * ARROW_SCALE + g.Style.FramePadding.x);// table->CellPadding.x);
  2157. if (column->SortOrder != -1)
  2158. {
  2159. w_sort_text = 0.0f;
  2160. char sort_order_suf[8];
  2161. if (column->SortOrder > 0)
  2162. {
  2163. ImFormatString(sort_order_suf, IM_ARRAYSIZE(sort_order_suf), "%d", column->SortOrder + 1);
  2164. w_sort_text = g.Style.ItemInnerSpacing.x + CalcTextSize(sort_order_suf).x;
  2165. }
  2166. float x = ImMax(cell_r.Min.x, cell_r.Max.x - w_arrow - w_sort_text);
  2167. ellipsis_max -= w_arrow + w_sort_text;
  2168. float y = label_pos.y;
  2169. ImU32 col = GetColorU32(ImGuiCol_Text);
  2170. if (column->SortOrder > 0)
  2171. {
  2172. PushStyleColor(ImGuiCol_Text, GetColorU32(ImGuiCol_Text, 0.70f));
  2173. RenderText(ImVec2(x + g.Style.ItemInnerSpacing.x, y), sort_order_suf);
  2174. PopStyleColor();
  2175. x += w_sort_text;
  2176. }
  2177. RenderArrow(window->DrawList, ImVec2(x, y), col, column->SortDirection == ImGuiSortDirection_Ascending ? ImGuiDir_Up : ImGuiDir_Down, ARROW_SCALE);
  2178. }
  2179. // Handle clicking on column header to adjust Sort Order
  2180. if (pressed && table->ReorderColumn != column_n)
  2181. {
  2182. // Set new sort direction
  2183. // - If the PreferSortDescending flag is set, we will default to a Descending direction on the first click.
  2184. // - Note that the PreferSortAscending flag is never checked, it is essentially the default and therefore a no-op.
  2185. ImGuiSortDirection sort_direction;
  2186. if (column->SortOrder == -1)
  2187. sort_direction = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? ImGuiSortDirection_Descending : ImGuiSortDirection_Ascending;
  2188. else
  2189. sort_direction = (column->SortDirection == ImGuiSortDirection_Ascending) ? ImGuiSortDirection_Descending : ImGuiSortDirection_Ascending;
  2190. TableSetColumnSortDirection(column_n, sort_direction, g.IO.KeyShift);
  2191. }
  2192. }
  2193. // Render clipped label. Clipping here ensure that in the majority of situations, all our header cells will
  2194. // be merged into a single draw call.
  2195. //window->DrawList->AddCircleFilled(ImVec2(ellipsis_max, label_pos.y), 40, IM_COL32_WHITE);
  2196. 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);
  2197. const bool text_clipped = label_size.x > (ellipsis_max - label_pos.x);
  2198. if (text_clipped && hovered && g.HoveredIdNotActiveTimer > g.TooltipSlowDelay)
  2199. SetTooltip("%.*s", (int)(label_end - label), label);
  2200. // We feed our unclipped width to the column without writing on CursorMaxPos, so that column is still considering for merging.
  2201. float max_pos_x = label_pos.x + label_size.x + w_sort_text + w_arrow;
  2202. column->ContentMaxXHeadersUsed = ImMax(column->ContentMaxXHeadersUsed, column->WorkMaxX);
  2203. column->ContentMaxXHeadersIdeal = ImMax(column->ContentMaxXHeadersIdeal, max_pos_x);
  2204. // We don't use BeginPopupContextItem() because we want the popup to stay up even after the column is hidden
  2205. if (IsMouseReleased(1) && IsItemHovered())
  2206. TableOpenContextMenu(column_n);
  2207. }
  2208. // Note that the NoSortAscending/NoSortDescending flags are processed in TableSortSpecsSanitize(), and they may change/revert
  2209. // the value of SortDirection. We could technically also do it here but it would be unnecessary and duplicate code.
  2210. void ImGui::TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs)
  2211. {
  2212. ImGuiContext& g = *GImGui;
  2213. ImGuiTable* table = g.CurrentTable;
  2214. if (!(table->Flags & ImGuiTableFlags_MultiSortable))
  2215. append_to_sort_specs = false;
  2216. ImS8 sort_order_max = 0;
  2217. if (append_to_sort_specs)
  2218. for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
  2219. sort_order_max = ImMax(sort_order_max, table->Columns[other_column_n].SortOrder);
  2220. ImGuiTableColumn* column = &table->Columns[column_n];
  2221. column->SortDirection = (ImS8)sort_direction;
  2222. if (column->SortOrder == -1 || !append_to_sort_specs)
  2223. column->SortOrder = append_to_sort_specs ? sort_order_max + 1 : 0;
  2224. for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
  2225. {
  2226. ImGuiTableColumn* other_column = &table->Columns[other_column_n];
  2227. if (other_column != column && !append_to_sort_specs)
  2228. other_column->SortOrder = -1;
  2229. TableFixColumnSortDirection(other_column);
  2230. }
  2231. table->IsSettingsDirty = true;
  2232. table->IsSortSpecsDirty = true;
  2233. }
  2234. // Return NULL if no sort specs (most often when ImGuiTableFlags_Sortable is not set)
  2235. // You can sort your data again when 'SpecsChanged == true'. It will be true with sorting specs have changed since
  2236. // last call, or the first time.
  2237. // Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable()!
  2238. ImGuiTableSortSpecs* ImGui::TableGetSortSpecs()
  2239. {
  2240. ImGuiContext& g = *GImGui;
  2241. ImGuiTable* table = g.CurrentTable;
  2242. IM_ASSERT(table != NULL);
  2243. if (!(table->Flags & ImGuiTableFlags_Sortable))
  2244. return NULL;
  2245. // Require layout (in case TableHeadersRow() hasn't been called) as it may alter IsSortSpecsDirty in some paths.
  2246. if (!table->IsLayoutLocked)
  2247. TableUpdateLayout(table);
  2248. if (table->IsSortSpecsDirty)
  2249. TableSortSpecsBuild(table);
  2250. return table->SortSpecs.SpecsCount ? &table->SortSpecs : NULL;
  2251. }
  2252. bool ImGui::TableGetColumnIsSorted(int column_n)
  2253. {
  2254. ImGuiContext& g = *GImGui;
  2255. ImGuiTable* table = g.CurrentTable;
  2256. if (!table)
  2257. return false;
  2258. if (column_n < 0)
  2259. column_n = table->CurrentColumn;
  2260. ImGuiTableColumn* column = &table->Columns[column_n];
  2261. return (column->SortOrder != -1);
  2262. }
  2263. // Return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered.
  2264. int ImGui::TableGetHoveredColumn()
  2265. {
  2266. ImGuiContext& g = *GImGui;
  2267. ImGuiTable* table = g.CurrentTable;
  2268. if (!table)
  2269. return -1;
  2270. return (int)table->HoveredColumnBody;
  2271. }
  2272. void ImGui::TableSetBgColor(ImGuiTableBgTarget bg_target, ImU32 color, int column_n)
  2273. {
  2274. ImGuiContext& g = *GImGui;
  2275. ImGuiTable* table = g.CurrentTable;
  2276. IM_ASSERT(bg_target != ImGuiTableBgTarget_None);
  2277. if (color == IM_COL32_DISABLE)
  2278. color = 0;
  2279. // We cannot draw neither the cell or row background immediately as we don't know the row height at this point in time.
  2280. switch (bg_target)
  2281. {
  2282. case ImGuiTableBgTarget_CellBg:
  2283. {
  2284. if (table->RowPosY1 > table->InnerClipRect.Max.y) // Discard
  2285. return;
  2286. if (column_n == -1)
  2287. column_n = table->CurrentColumn;
  2288. if ((table->EnabledUnclippedMaskByIndex & ((ImU64)1 << column_n)) == 0)
  2289. return;
  2290. if (table->RowCellDataCurrent < 0 || table->RowCellData[table->RowCellDataCurrent].Column != column_n)
  2291. table->RowCellDataCurrent++;
  2292. ImGuiTableCellData* cell_data = &table->RowCellData[table->RowCellDataCurrent];
  2293. cell_data->BgColor = color;
  2294. cell_data->Column = (ImS8)column_n;
  2295. break;
  2296. }
  2297. case ImGuiTableBgTarget_RowBg0:
  2298. case ImGuiTableBgTarget_RowBg1:
  2299. {
  2300. if (table->RowPosY1 > table->InnerClipRect.Max.y) // Discard
  2301. return;
  2302. IM_ASSERT(column_n == -1);
  2303. int bg_idx = (bg_target == ImGuiTableBgTarget_RowBg1) ? 1 : 0;
  2304. table->RowBgColor[bg_idx] = color;
  2305. break;
  2306. }
  2307. default:
  2308. IM_ASSERT(0);
  2309. }
  2310. }
  2311. void ImGui::TableSortSpecsSanitize(ImGuiTable* table)
  2312. {
  2313. IM_ASSERT(table->Flags & ImGuiTableFlags_Sortable);
  2314. // Clear SortOrder from hidden column and verify that there's no gap or duplicate.
  2315. int sort_order_count = 0;
  2316. ImU64 sort_order_mask = 0x00;
  2317. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  2318. {
  2319. ImGuiTableColumn* column = &table->Columns[column_n];
  2320. if (column->SortOrder != -1 && !column->IsEnabled)
  2321. column->SortOrder = -1;
  2322. if (column->SortOrder == -1)
  2323. continue;
  2324. sort_order_count++;
  2325. sort_order_mask |= ((ImU64)1 << column->SortOrder);
  2326. IM_ASSERT(sort_order_count < (int)sizeof(sort_order_mask) * 8);
  2327. }
  2328. const bool need_fix_linearize = ((ImU64)1 << sort_order_count) != (sort_order_mask + 1);
  2329. const bool need_fix_single_sort_order = (sort_order_count > 1) && !(table->Flags & ImGuiTableFlags_MultiSortable);
  2330. if (need_fix_linearize || need_fix_single_sort_order)
  2331. {
  2332. ImU64 fixed_mask = 0x00;
  2333. for (int sort_n = 0; sort_n < sort_order_count; sort_n++)
  2334. {
  2335. // Fix: Rewrite sort order fields if needed so they have no gap or duplicate.
  2336. // (e.g. SortOrder 0 disappeared, SortOrder 1..2 exists --> rewrite then as SortOrder 0..1)
  2337. int column_with_smallest_sort_order = -1;
  2338. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  2339. if ((fixed_mask & ((ImU64)1 << (ImU64)column_n)) == 0 && table->Columns[column_n].SortOrder != -1)
  2340. if (column_with_smallest_sort_order == -1 || table->Columns[column_n].SortOrder < table->Columns[column_with_smallest_sort_order].SortOrder)
  2341. column_with_smallest_sort_order = column_n;
  2342. IM_ASSERT(column_with_smallest_sort_order != -1);
  2343. fixed_mask |= ((ImU64)1 << column_with_smallest_sort_order);
  2344. table->Columns[column_with_smallest_sort_order].SortOrder = (ImS8)sort_n;
  2345. // Fix: Make sure only one column has a SortOrder if ImGuiTableFlags_MultiSortable is not set.
  2346. if (need_fix_single_sort_order)
  2347. {
  2348. sort_order_count = 1;
  2349. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  2350. if (column_n != column_with_smallest_sort_order)
  2351. table->Columns[column_n].SortOrder = -1;
  2352. break;
  2353. }
  2354. }
  2355. }
  2356. // Fallback default sort order (if no column had the ImGuiTableColumnFlags_DefaultSort flag)
  2357. if (sort_order_count == 0)
  2358. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  2359. {
  2360. ImGuiTableColumn* column = &table->Columns[column_n];
  2361. if (column->IsEnabled && !(column->Flags & ImGuiTableColumnFlags_NoSort))
  2362. {
  2363. sort_order_count = 1;
  2364. column->SortOrder = 0;
  2365. TableFixColumnSortDirection(column);
  2366. break;
  2367. }
  2368. }
  2369. table->SortSpecsCount = (ImS8)sort_order_count;
  2370. }
  2371. void ImGui::TableSortSpecsBuild(ImGuiTable* table)
  2372. {
  2373. IM_ASSERT(table->IsSortSpecsDirty);
  2374. TableSortSpecsSanitize(table);
  2375. // Write output
  2376. table->SortSpecsData.resize(table->SortSpecsCount);
  2377. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  2378. {
  2379. ImGuiTableColumn* column = &table->Columns[column_n];
  2380. if (column->SortOrder == -1)
  2381. continue;
  2382. ImGuiTableSortSpecsColumn* sort_spec = &table->SortSpecsData[column->SortOrder];
  2383. sort_spec->ColumnUserID = column->UserID;
  2384. sort_spec->ColumnIndex = (ImU8)column_n;
  2385. sort_spec->SortOrder = (ImU8)column->SortOrder;
  2386. sort_spec->SortDirection = column->SortDirection;
  2387. }
  2388. table->SortSpecs.Specs = table->SortSpecsData.Data;
  2389. table->SortSpecs.SpecsCount = table->SortSpecsData.Size;
  2390. table->SortSpecs.SpecsDirty = true; // Mark as dirty for user
  2391. table->IsSortSpecsDirty = false; // Mark as not dirty for us
  2392. }
  2393. //-------------------------------------------------------------------------
  2394. // [SECTION] Tables: Settings (.ini data)
  2395. //-------------------------------------------------------------------------
  2396. // - TableSettingsInit() [Internal]
  2397. // - TableSettingsCalcChunkSize() [Internal]
  2398. // - TableSettingsCreate() [Internal]
  2399. // - TableSettingsFindByID() [Internal]
  2400. // - TableSettingsClearByID() [Internal]
  2401. // - TableGetBoundSettings() [Internal]
  2402. // - TableSaveSettings() [Internal]
  2403. // - TableLoadSettings() [Internal]
  2404. // - TableSettingsHandler_ClearAll() [Internal]
  2405. // - TableSettingsHandler_ApplyAll() [Internal]
  2406. // - TableSettingsHandler_ReadOpen() [Internal]
  2407. // - TableSettingsHandler_ReadLine() [Internal]
  2408. // - TableSettingsHandler_WriteAll() [Internal]
  2409. // - TableSettingsInstallHandler() [Internal]
  2410. //-------------------------------------------------------------------------
  2411. // [Init] 1: TableSettingsHandler_ReadXXXX() Load and parse .ini file into TableSettings.
  2412. // [Main] 2: TableLoadSettings() When table is created, bind Table to TableSettings, serialize TableSettings data into Table.
  2413. // [Main] 3: TableSaveSettings() When table properties are modified, serialize Table data into bound or new TableSettings, mark .ini as dirty.
  2414. // [Main] 4: TableSettingsHandler_WriteAll() When .ini file is dirty (which can come from other source), save TableSettings into .ini file.
  2415. //-------------------------------------------------------------------------
  2416. // Clear and initialize empty settings instance
  2417. static void TableSettingsInit(ImGuiTableSettings* settings, ImGuiID id, int columns_count, int columns_count_max)
  2418. {
  2419. IM_PLACEMENT_NEW(settings) ImGuiTableSettings();
  2420. ImGuiTableColumnSettings* settings_column = settings->GetColumnSettings();
  2421. for (int n = 0; n < columns_count_max; n++, settings_column++)
  2422. IM_PLACEMENT_NEW(settings_column) ImGuiTableColumnSettings();
  2423. settings->ID = id;
  2424. settings->ColumnsCount = (ImS8)columns_count;
  2425. settings->ColumnsCountMax = (ImS8)columns_count_max;
  2426. settings->WantApply = true;
  2427. }
  2428. static size_t TableSettingsCalcChunkSize(int columns_count)
  2429. {
  2430. return sizeof(ImGuiTableSettings) + (size_t)columns_count * sizeof(ImGuiTableColumnSettings);
  2431. }
  2432. ImGuiTableSettings* ImGui::TableSettingsCreate(ImGuiID id, int columns_count)
  2433. {
  2434. ImGuiContext& g = *GImGui;
  2435. ImGuiTableSettings* settings = g.SettingsTables.alloc_chunk(TableSettingsCalcChunkSize(columns_count));
  2436. TableSettingsInit(settings, id, columns_count, columns_count);
  2437. return settings;
  2438. }
  2439. // Find existing settings
  2440. ImGuiTableSettings* ImGui::TableSettingsFindByID(ImGuiID id)
  2441. {
  2442. // FIXME-OPT: Might want to store a lookup map for this?
  2443. ImGuiContext& g = *GImGui;
  2444. for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings))
  2445. if (settings->ID == id)
  2446. return settings;
  2447. return NULL;
  2448. }
  2449. void ImGui::TableSettingsClearByID(ImGuiID id)
  2450. {
  2451. if (ImGuiTableSettings* settings = TableSettingsFindByID(id))
  2452. settings->ID = 0;
  2453. }
  2454. // Get settings for a given table, NULL if none
  2455. ImGuiTableSettings* ImGui::TableGetBoundSettings(ImGuiTable* table)
  2456. {
  2457. if (table->SettingsOffset != -1)
  2458. {
  2459. ImGuiContext& g = *GImGui;
  2460. ImGuiTableSettings* settings = g.SettingsTables.ptr_from_offset(table->SettingsOffset);
  2461. IM_ASSERT(settings->ID == table->ID);
  2462. if (settings->ColumnsCountMax >= table->ColumnsCount)
  2463. return settings; // OK
  2464. settings->ID = 0; // Invalidate storage, we won't fit because of a count change
  2465. }
  2466. return NULL;
  2467. }
  2468. void ImGui::TableSaveSettings(ImGuiTable* table)
  2469. {
  2470. table->IsSettingsDirty = false;
  2471. if (table->Flags & ImGuiTableFlags_NoSavedSettings)
  2472. return;
  2473. // Bind or create settings data
  2474. ImGuiContext& g = *GImGui;
  2475. ImGuiTableSettings* settings = TableGetBoundSettings(table);
  2476. if (settings == NULL)
  2477. {
  2478. settings = TableSettingsCreate(table->ID, table->ColumnsCount);
  2479. table->SettingsOffset = g.SettingsTables.offset_from_ptr(settings);
  2480. }
  2481. settings->ColumnsCount = (ImS8)table->ColumnsCount;
  2482. // Serialize ImGuiTable/ImGuiTableColumn into ImGuiTableSettings/ImGuiTableColumnSettings
  2483. IM_ASSERT(settings->ID == table->ID);
  2484. IM_ASSERT(settings->ColumnsCount == table->ColumnsCount && settings->ColumnsCountMax >= settings->ColumnsCount);
  2485. ImGuiTableColumn* column = table->Columns.Data;
  2486. ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings();
  2487. bool save_ref_scale = false;
  2488. settings->SaveFlags = ImGuiTableFlags_None;
  2489. for (int n = 0; n < table->ColumnsCount; n++, column++, column_settings++)
  2490. {
  2491. const float width_or_weight = (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? column->StretchWeight : column->WidthRequest;
  2492. column_settings->WidthOrWeight = width_or_weight;
  2493. column_settings->Index = (ImS8)n;
  2494. column_settings->DisplayOrder = column->DisplayOrder;
  2495. column_settings->SortOrder = column->SortOrder;
  2496. column_settings->SortDirection = column->SortDirection;
  2497. column_settings->IsEnabled = column->IsEnabled;
  2498. column_settings->IsStretch = (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? 1 : 0;
  2499. if ((column->Flags & ImGuiTableColumnFlags_WidthStretch) == 0)
  2500. save_ref_scale = true;
  2501. // We skip saving some data in the .ini file when they are unnecessary to restore our state.
  2502. // Note that fixed width where initial width was derived from auto-fit will always be saved as InitStretchWeightOrWidth will be 0.0f.
  2503. // FIXME-TABLE: We don't have logic to easily compare SortOrder to DefaultSortOrder yet so it's always saved when present.
  2504. if (width_or_weight != column->InitStretchWeightOrWidth)
  2505. settings->SaveFlags |= ImGuiTableFlags_Resizable;
  2506. if (column->DisplayOrder != n)
  2507. settings->SaveFlags |= ImGuiTableFlags_Reorderable;
  2508. if (column->SortOrder != -1)
  2509. settings->SaveFlags |= ImGuiTableFlags_Sortable;
  2510. if (column->IsEnabled != ((column->Flags & ImGuiTableColumnFlags_DefaultHide) == 0))
  2511. settings->SaveFlags |= ImGuiTableFlags_Hideable;
  2512. }
  2513. settings->SaveFlags &= table->Flags;
  2514. settings->RefScale = save_ref_scale ? table->RefScale : 0.0f;
  2515. MarkIniSettingsDirty();
  2516. }
  2517. void ImGui::TableLoadSettings(ImGuiTable* table)
  2518. {
  2519. ImGuiContext& g = *GImGui;
  2520. table->IsSettingsRequestLoad = false;
  2521. if (table->Flags & ImGuiTableFlags_NoSavedSettings)
  2522. return;
  2523. // Bind settings
  2524. ImGuiTableSettings* settings;
  2525. if (table->SettingsOffset == -1)
  2526. {
  2527. settings = TableSettingsFindByID(table->ID);
  2528. if (settings == NULL)
  2529. return;
  2530. if (settings->ColumnsCount != table->ColumnsCount) // Allow settings if columns count changed. We could otherwise decide to return...
  2531. table->IsSettingsDirty = true;
  2532. table->SettingsOffset = g.SettingsTables.offset_from_ptr(settings);
  2533. }
  2534. else
  2535. {
  2536. settings = TableGetBoundSettings(table);
  2537. }
  2538. table->SettingsLoadedFlags = settings->SaveFlags;
  2539. table->RefScale = settings->RefScale;
  2540. // Serialize ImGuiTableSettings/ImGuiTableColumnSettings into ImGuiTable/ImGuiTableColumn
  2541. ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings();
  2542. for (int data_n = 0; data_n < settings->ColumnsCount; data_n++, column_settings++)
  2543. {
  2544. int column_n = column_settings->Index;
  2545. if (column_n < 0 || column_n >= table->ColumnsCount)
  2546. continue;
  2547. ImGuiTableColumn* column = &table->Columns[column_n];
  2548. if (settings->SaveFlags & ImGuiTableFlags_Resizable)
  2549. {
  2550. if (column_settings->IsStretch)
  2551. column->StretchWeight = column_settings->WidthOrWeight;
  2552. else
  2553. column->WidthRequest = column_settings->WidthOrWeight;
  2554. column->AutoFitQueue = 0x00;
  2555. }
  2556. if (settings->SaveFlags & ImGuiTableFlags_Reorderable)
  2557. column->DisplayOrder = column_settings->DisplayOrder;
  2558. else
  2559. column->DisplayOrder = (ImS8)column_n;
  2560. column->IsEnabled = column->IsEnabledNextFrame = column_settings->IsEnabled;
  2561. column->SortOrder = column_settings->SortOrder;
  2562. column->SortDirection = column_settings->SortDirection;
  2563. }
  2564. // FIXME-TABLE: Need to validate .ini data
  2565. for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
  2566. table->DisplayOrderToIndex[table->Columns[column_n].DisplayOrder] = (ImS8)column_n;
  2567. }
  2568. static void TableSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*)
  2569. {
  2570. ImGuiContext& g = *ctx;
  2571. for (int i = 0; i != g.Tables.GetSize(); i++)
  2572. g.Tables.GetByIndex(i)->SettingsOffset = -1;
  2573. g.SettingsTables.clear();
  2574. }
  2575. // Apply to existing windows (if any)
  2576. static void TableSettingsHandler_ApplyAll(ImGuiContext* ctx, ImGuiSettingsHandler*)
  2577. {
  2578. ImGuiContext& g = *ctx;
  2579. for (int i = 0; i != g.Tables.GetSize(); i++)
  2580. {
  2581. ImGuiTable* table = g.Tables.GetByIndex(i);
  2582. table->IsSettingsRequestLoad = true;
  2583. table->SettingsOffset = -1;
  2584. }
  2585. }
  2586. static void* TableSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name)
  2587. {
  2588. ImGuiID id = 0;
  2589. int columns_count = 0;
  2590. if (sscanf(name, "0x%08X,%d", &id, &columns_count) < 2)
  2591. return NULL;
  2592. if (ImGuiTableSettings* settings = ImGui::TableSettingsFindByID(id))
  2593. {
  2594. if (settings->ColumnsCountMax >= columns_count)
  2595. {
  2596. TableSettingsInit(settings, id, columns_count, settings->ColumnsCountMax); // Recycle
  2597. return settings;
  2598. }
  2599. settings->ID = 0; // Invalidate storage, we won't fit because of a count change
  2600. }
  2601. return ImGui::TableSettingsCreate(id, columns_count);
  2602. }
  2603. static void TableSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line)
  2604. {
  2605. // "Column 0 UserID=0x42AD2D21 Width=100 Visible=1 Order=0 Sort=0v"
  2606. ImGuiTableSettings* settings = (ImGuiTableSettings*)entry;
  2607. float f = 0.0f;
  2608. int column_n = 0, r = 0, n = 0;
  2609. if (sscanf(line, "RefScale=%f", &f) == 1) { settings->RefScale = f; return; }
  2610. if (sscanf(line, "Column %d%n", &column_n, &r) == 1)
  2611. {
  2612. if (column_n < 0 || column_n >= settings->ColumnsCount)
  2613. return;
  2614. line = ImStrSkipBlank(line + r);
  2615. char c = 0;
  2616. ImGuiTableColumnSettings* column = settings->GetColumnSettings() + column_n;
  2617. column->Index = (ImS8)column_n;
  2618. if (sscanf(line, "UserID=0x%08X%n", (ImU32*)&n, &r)==1) { line = ImStrSkipBlank(line + r); column->UserID = (ImGuiID)n; }
  2619. if (sscanf(line, "Width=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->WidthOrWeight = (float)n; column->IsStretch = 0; settings->SaveFlags |= ImGuiTableFlags_Resizable; }
  2620. if (sscanf(line, "Weight=%f%n", &f, &r) == 1) { line = ImStrSkipBlank(line + r); column->WidthOrWeight = f; column->IsStretch = 1; settings->SaveFlags |= ImGuiTableFlags_Resizable; }
  2621. if (sscanf(line, "Visible=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->IsEnabled = (ImU8)n; settings->SaveFlags |= ImGuiTableFlags_Hideable; }
  2622. if (sscanf(line, "Order=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->DisplayOrder = (ImS8)n; settings->SaveFlags |= ImGuiTableFlags_Reorderable; }
  2623. 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; }
  2624. }
  2625. }
  2626. static void TableSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf)
  2627. {
  2628. ImGuiContext& g = *ctx;
  2629. for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings))
  2630. {
  2631. if (settings->ID == 0) // Skip ditched settings
  2632. continue;
  2633. // TableSaveSettings() may clear some of those flags when we establish that the data can be stripped
  2634. // (e.g. Order was unchanged)
  2635. const bool save_size = (settings->SaveFlags & ImGuiTableFlags_Resizable) != 0;
  2636. const bool save_visible = (settings->SaveFlags & ImGuiTableFlags_Hideable) != 0;
  2637. const bool save_order = (settings->SaveFlags & ImGuiTableFlags_Reorderable) != 0;
  2638. const bool save_sort = (settings->SaveFlags & ImGuiTableFlags_Sortable) != 0;
  2639. if (!save_size && !save_visible && !save_order && !save_sort)
  2640. continue;
  2641. buf->reserve(buf->size() + 30 + settings->ColumnsCount * 50); // ballpark reserve
  2642. buf->appendf("[%s][0x%08X,%d]\n", handler->TypeName, settings->ID, settings->ColumnsCount);
  2643. if (settings->RefScale != 0.0f)
  2644. buf->appendf("RefScale=%g\n", settings->RefScale);
  2645. ImGuiTableColumnSettings* column = settings->GetColumnSettings();
  2646. for (int column_n = 0; column_n < settings->ColumnsCount; column_n++, column++)
  2647. {
  2648. // "Column 0 UserID=0x42AD2D21 Width=100 Visible=1 Order=0 Sort=0v"
  2649. buf->appendf("Column %-2d", column_n);
  2650. if (column->UserID != 0) buf->appendf(" UserID=%08X", column->UserID);
  2651. if (save_size && column->IsStretch) buf->appendf(" Weight=%.4f", column->WidthOrWeight);
  2652. if (save_size && !column->IsStretch) buf->appendf(" Width=%d", (int)column->WidthOrWeight);
  2653. if (save_visible) buf->appendf(" Visible=%d", column->IsEnabled);
  2654. if (save_order) buf->appendf(" Order=%d", column->DisplayOrder);
  2655. if (save_sort && column->SortOrder != -1) buf->appendf(" Sort=%d%c", column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? 'v' : '^');
  2656. buf->append("\n");
  2657. }
  2658. buf->append("\n");
  2659. }
  2660. }
  2661. void ImGui::TableSettingsInstallHandler(ImGuiContext* context)
  2662. {
  2663. ImGuiContext& g = *context;
  2664. ImGuiSettingsHandler ini_handler;
  2665. ini_handler.TypeName = "Table";
  2666. ini_handler.TypeHash = ImHashStr("Table");
  2667. ini_handler.ClearAllFn = TableSettingsHandler_ClearAll;
  2668. ini_handler.ReadOpenFn = TableSettingsHandler_ReadOpen;
  2669. ini_handler.ReadLineFn = TableSettingsHandler_ReadLine;
  2670. ini_handler.ApplyAllFn = TableSettingsHandler_ApplyAll;
  2671. ini_handler.WriteAllFn = TableSettingsHandler_WriteAll;
  2672. g.SettingsHandlers.push_back(ini_handler);
  2673. }
  2674. //-------------------------------------------------------------------------
  2675. // [SECTION] Tables: Garbage Collection
  2676. //-------------------------------------------------------------------------
  2677. // - TableRemove() [Internal]
  2678. // - TableGcCompactTransientBuffers() [Internal]
  2679. // - TableGcCompactSettings() [Internal]
  2680. //-------------------------------------------------------------------------
  2681. // Remove Table (currently only used by TestEngine)
  2682. void ImGui::TableRemove(ImGuiTable* table)
  2683. {
  2684. //IMGUI_DEBUG_LOG("TableRemove() id=0x%08X\n", table->ID);
  2685. ImGuiContext& g = *GImGui;
  2686. int table_idx = g.Tables.GetIndex(table);
  2687. //memset(table->RawData.Data, 0, table->RawData.size_in_bytes());
  2688. //memset(table, 0, sizeof(ImGuiTable));
  2689. g.Tables.Remove(table->ID, table);
  2690. g.TablesLastTimeActive[table_idx] = -1.0f;
  2691. }
  2692. // Free up/compact internal Table buffers for when it gets unused
  2693. void ImGui::TableGcCompactTransientBuffers(ImGuiTable* table)
  2694. {
  2695. //IMGUI_DEBUG_LOG("TableGcCompactTransientBuffers() id=0x%08X\n", table->ID);
  2696. ImGuiContext& g = *GImGui;
  2697. IM_ASSERT(table->MemoryCompacted == false);
  2698. table->DrawSplitter.ClearFreeMemory();
  2699. table->SortSpecsData.clear();
  2700. table->SortSpecs.Specs = NULL;
  2701. table->IsSortSpecsDirty = true;
  2702. table->ColumnsNames.clear();
  2703. table->MemoryCompacted = true;
  2704. for (int n = 0; n < table->ColumnsCount; n++)
  2705. table->Columns[n].NameOffset = -1;
  2706. g.TablesLastTimeActive[g.Tables.GetIndex(table)] = -1.0f;
  2707. }
  2708. // Compact and remove unused settings data (currently only used by TestEngine)
  2709. void ImGui::TableGcCompactSettings()
  2710. {
  2711. ImGuiContext& g = *GImGui;
  2712. int required_memory = 0;
  2713. for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings))
  2714. if (settings->ID != 0)
  2715. required_memory += (int)TableSettingsCalcChunkSize(settings->ColumnsCount);
  2716. if (required_memory == g.SettingsTables.Buf.Size)
  2717. return;
  2718. ImChunkStream<ImGuiTableSettings> new_chunk_stream;
  2719. new_chunk_stream.Buf.reserve(required_memory);
  2720. for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings))
  2721. if (settings->ID != 0)
  2722. memcpy(new_chunk_stream.alloc_chunk(TableSettingsCalcChunkSize(settings->ColumnsCount)), settings, TableSettingsCalcChunkSize(settings->ColumnsCount));
  2723. g.SettingsTables.swap(new_chunk_stream);
  2724. }
  2725. //-------------------------------------------------------------------------
  2726. // [SECTION] Tables: Debugging
  2727. //-------------------------------------------------------------------------
  2728. // - DebugNodeTable() [Internal]
  2729. //-------------------------------------------------------------------------
  2730. #ifndef IMGUI_DISABLE_METRICS_WINDOW
  2731. void ImGui::DebugNodeTable(ImGuiTable* table)
  2732. {
  2733. char buf[512];
  2734. char* p = buf;
  2735. const char* buf_end = buf + IM_ARRAYSIZE(buf);
  2736. const bool is_active = (table->LastFrameActive >= ImGui::GetFrameCount() - 2);
  2737. ImFormatString(p, buf_end - p, "Table 0x%08X (%d columns, in '%s')%s", table->ID, table->ColumnsCount, table->OuterWindow->Name, is_active ? "" : " *Inactive*");
  2738. if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); }
  2739. bool open = TreeNode(table, "%s", buf);
  2740. if (!is_active) { PopStyleColor(); }
  2741. if (IsItemHovered())
  2742. GetForegroundDrawList()->AddRect(table->OuterRect.Min, table->OuterRect.Max, IM_COL32(255, 255, 0, 255));
  2743. if (!open)
  2744. return;
  2745. BulletText("OuterRect: Pos: (%.1f,%.1f) Size: (%.1f,%.1f)", table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.GetWidth(), table->OuterRect.GetHeight());
  2746. BulletText("ColumnsWidth: %.1f, AutoFitWidth: %.1f, InnerWidth: %.1f%s", table->ColumnsTotalWidth, table->ColumnsAutoFitWidth, table->InnerWidth, table->InnerWidth == 0.0f ? " (auto)" : "");
  2747. BulletText("CellPaddingX: %.1f, CellSpacingX: %.1f/%.1f, OuterPaddingX: %.1f", table->CellPaddingX, table->CellSpacingX1, table->CellSpacingX2, table->OuterPaddingX);
  2748. BulletText("HoveredColumnBody: %d, HoveredColumnBorder: %d", table->HoveredColumnBody, table->HoveredColumnBorder);
  2749. BulletText("ResizedColumn: %d, ReorderColumn: %d, HeldHeaderColumn: %d", table->ResizedColumn, table->ReorderColumn, table->HeldHeaderColumn);
  2750. //BulletText("BgDrawChannels: %d/%d", 0, table->BgDrawChannelUnfrozen);
  2751. for (int n = 0; n < table->ColumnsCount; n++)
  2752. {
  2753. ImGuiTableColumn* column = &table->Columns[n];
  2754. const char* name = TableGetColumnName(table, n);
  2755. ImFormatString(buf, IM_ARRAYSIZE(buf),
  2756. "Column %d order %d name '%s': offset %+.2f to %+.2f\n"
  2757. "Visible: %d, Clipped: %d, SkipItems: %d, DrawChannels: %d,%d\n"
  2758. "WidthGiven: %.1f, Request/Auto: %.1f/%.1f, StretchWeight: %.3f\n"
  2759. "MinX: %.1f, MaxX: %.1f (%+.1f), ClipRect: %.1f to %.1f (+%.1f)\n"
  2760. "ContentWidth: %.1f,%.1f, HeadersUsed/Ideal %.1f/%.1f\n"
  2761. "Sort: %d%s, UserID: 0x%08X, Flags: 0x%04X: %s%s%s%s..",
  2762. n, column->DisplayOrder, name, column->MinX - table->WorkRect.Min.x, column->MaxX - table->WorkRect.Min.x,
  2763. column->IsEnabled, column->IsClipped, column->IsSkipItems, column->DrawChannelFrozen, column->DrawChannelUnfrozen,
  2764. column->WidthGiven, column->WidthRequest, column->WidthAuto, column->StretchWeight,
  2765. column->MinX, column->MaxX, column->MaxX - column->MinX, column->ClipRect.Min.x, column->ClipRect.Max.x, column->ClipRect.Max.x - column->ClipRect.Min.x,
  2766. column->ContentMaxXFrozen - column->WorkMinX, column->ContentMaxXUnfrozen - column->WorkMinX, column->ContentMaxXHeadersUsed - column->WorkMinX, column->ContentMaxXHeadersIdeal - column->WorkMinX,
  2767. column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? " (Asc)" : (column->SortDirection == ImGuiSortDirection_Descending) ? " (Des)" : "", column->UserID, column->Flags,
  2768. (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? "WidthStretch " : "",
  2769. (column->Flags & ImGuiTableColumnFlags_WidthFixed) ? "WidthFixed " : "",
  2770. (column->Flags & ImGuiTableColumnFlags_WidthAutoResize) ? "WidthAutoResize " : "",
  2771. (column->Flags & ImGuiTableColumnFlags_NoResize) ? "NoResize " : "");
  2772. Bullet();
  2773. Selectable(buf);
  2774. if (IsItemHovered())
  2775. {
  2776. ImRect r(column->MinX, table->OuterRect.Min.y, column->MaxX, table->OuterRect.Max.y);
  2777. GetForegroundDrawList()->AddRect(r.Min, r.Max, IM_COL32(255, 255, 0, 255));
  2778. }
  2779. }
  2780. if (ImGuiTableSettings* settings = TableGetBoundSettings(table))
  2781. DebugNodeTableSettings(settings);
  2782. TreePop();
  2783. }
  2784. void ImGui::DebugNodeTableSettings(ImGuiTableSettings* settings)
  2785. {
  2786. if (!TreeNode((void*)(intptr_t)settings->ID, "Settings 0x%08X (%d columns)", settings->ID, settings->ColumnsCount))
  2787. return;
  2788. BulletText("SaveFlags: 0x%08X", settings->SaveFlags);
  2789. BulletText("ColumnsCount: %d (max %d)", settings->ColumnsCount, settings->ColumnsCountMax);
  2790. for (int n = 0; n < settings->ColumnsCount; n++)
  2791. {
  2792. ImGuiTableColumnSettings* column_settings = &settings->GetColumnSettings()[n];
  2793. ImGuiSortDirection sort_dir = (column_settings->SortOrder != -1) ? (ImGuiSortDirection)column_settings->SortDirection : ImGuiSortDirection_None;
  2794. BulletText("Column %d Order %d SortOrder %d %s Vis %d %s %7.3f UserID 0x%08X",
  2795. n, column_settings->DisplayOrder, column_settings->SortOrder,
  2796. (sort_dir == ImGuiSortDirection_Ascending) ? "Asc" : (sort_dir == ImGuiSortDirection_Descending) ? "Des" : "---",
  2797. column_settings->IsEnabled, column_settings->IsStretch ? "Weight" : "Width ", column_settings->WidthOrWeight, column_settings->UserID);
  2798. }
  2799. TreePop();
  2800. }
  2801. #endif // #ifndef IMGUI_DISABLE_METRICS_WINDOW
  2802. //-------------------------------------------------------------------------
  2803. // [SECTION] Columns, BeginColumns, EndColumns, etc.
  2804. // (This is a legacy API, prefer using BeginTable/EndTable!)
  2805. //-------------------------------------------------------------------------
  2806. // - SetWindowClipRectBeforeSetChannel() [Internal]
  2807. // - GetColumnIndex()
  2808. // - GetColumnsCount()
  2809. // - GetColumnOffset()
  2810. // - GetColumnWidth()
  2811. // - SetColumnOffset()
  2812. // - SetColumnWidth()
  2813. // - PushColumnClipRect() [Internal]
  2814. // - PushColumnsBackground() [Internal]
  2815. // - PopColumnsBackground() [Internal]
  2816. // - FindOrCreateColumns() [Internal]
  2817. // - GetColumnsID() [Internal]
  2818. // - BeginColumns()
  2819. // - NextColumn()
  2820. // - EndColumns()
  2821. // - Columns()
  2822. //-------------------------------------------------------------------------
  2823. // [Internal] Small optimization to avoid calls to PopClipRect/SetCurrentChannel/PushClipRect in sequences,
  2824. // they would meddle many times with the underlying ImDrawCmd.
  2825. // Instead, we do a preemptive overwrite of clipping rectangle _without_ altering the command-buffer and let
  2826. // the subsequent single call to SetCurrentChannel() does it things once.
  2827. void ImGui::SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect)
  2828. {
  2829. ImVec4 clip_rect_vec4 = clip_rect.ToVec4();
  2830. window->ClipRect = clip_rect;
  2831. window->DrawList->_CmdHeader.ClipRect = clip_rect_vec4;
  2832. window->DrawList->_ClipRectStack.Data[window->DrawList->_ClipRectStack.Size - 1] = clip_rect_vec4;
  2833. }
  2834. int ImGui::GetColumnIndex()
  2835. {
  2836. ImGuiWindow* window = GetCurrentWindowRead();
  2837. return window->DC.CurrentColumns ? window->DC.CurrentColumns->Current : 0;
  2838. }
  2839. int ImGui::GetColumnsCount()
  2840. {
  2841. ImGuiWindow* window = GetCurrentWindowRead();
  2842. return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1;
  2843. }
  2844. float ImGui::GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm)
  2845. {
  2846. return offset_norm * (columns->OffMaxX - columns->OffMinX);
  2847. }
  2848. float ImGui::GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset)
  2849. {
  2850. return offset / (columns->OffMaxX - columns->OffMinX);
  2851. }
  2852. static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f;
  2853. static float GetDraggedColumnOffset(ImGuiOldColumns* columns, int column_index)
  2854. {
  2855. // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing
  2856. // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning.
  2857. ImGuiContext& g = *GImGui;
  2858. ImGuiWindow* window = g.CurrentWindow;
  2859. IM_ASSERT(column_index > 0); // We are not supposed to drag column 0.
  2860. IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index));
  2861. float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x;
  2862. x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing);
  2863. if ((columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths))
  2864. x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing);
  2865. return x;
  2866. }
  2867. float ImGui::GetColumnOffset(int column_index)
  2868. {
  2869. ImGuiWindow* window = GetCurrentWindowRead();
  2870. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2871. if (columns == NULL)
  2872. return 0.0f;
  2873. if (column_index < 0)
  2874. column_index = columns->Current;
  2875. IM_ASSERT(column_index < columns->Columns.Size);
  2876. const float t = columns->Columns[column_index].OffsetNorm;
  2877. const float x_offset = ImLerp(columns->OffMinX, columns->OffMaxX, t);
  2878. return x_offset;
  2879. }
  2880. static float GetColumnWidthEx(ImGuiOldColumns* columns, int column_index, bool before_resize = false)
  2881. {
  2882. if (column_index < 0)
  2883. column_index = columns->Current;
  2884. float offset_norm;
  2885. if (before_resize)
  2886. offset_norm = columns->Columns[column_index + 1].OffsetNormBeforeResize - columns->Columns[column_index].OffsetNormBeforeResize;
  2887. else
  2888. offset_norm = columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm;
  2889. return ImGui::GetColumnOffsetFromNorm(columns, offset_norm);
  2890. }
  2891. float ImGui::GetColumnWidth(int column_index)
  2892. {
  2893. ImGuiContext& g = *GImGui;
  2894. ImGuiWindow* window = g.CurrentWindow;
  2895. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2896. if (columns == NULL)
  2897. return GetContentRegionAvail().x;
  2898. if (column_index < 0)
  2899. column_index = columns->Current;
  2900. return GetColumnOffsetFromNorm(columns, columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm);
  2901. }
  2902. void ImGui::SetColumnOffset(int column_index, float offset)
  2903. {
  2904. ImGuiContext& g = *GImGui;
  2905. ImGuiWindow* window = g.CurrentWindow;
  2906. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2907. IM_ASSERT(columns != NULL);
  2908. if (column_index < 0)
  2909. column_index = columns->Current;
  2910. IM_ASSERT(column_index < columns->Columns.Size);
  2911. const bool preserve_width = !(columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths) && (column_index < columns->Count - 1);
  2912. const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f;
  2913. if (!(columns->Flags & ImGuiOldColumnFlags_NoForceWithinWindow))
  2914. offset = ImMin(offset, columns->OffMaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index));
  2915. columns->Columns[column_index].OffsetNorm = GetColumnNormFromOffset(columns, offset - columns->OffMinX);
  2916. if (preserve_width)
  2917. SetColumnOffset(column_index + 1, offset + ImMax(g.Style.ColumnsMinSpacing, width));
  2918. }
  2919. void ImGui::SetColumnWidth(int column_index, float width)
  2920. {
  2921. ImGuiWindow* window = GetCurrentWindowRead();
  2922. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2923. IM_ASSERT(columns != NULL);
  2924. if (column_index < 0)
  2925. column_index = columns->Current;
  2926. SetColumnOffset(column_index + 1, GetColumnOffset(column_index) + width);
  2927. }
  2928. void ImGui::PushColumnClipRect(int column_index)
  2929. {
  2930. ImGuiWindow* window = GetCurrentWindowRead();
  2931. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2932. if (column_index < 0)
  2933. column_index = columns->Current;
  2934. ImGuiOldColumnData* column = &columns->Columns[column_index];
  2935. PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false);
  2936. }
  2937. // Get into the columns background draw command (which is generally the same draw command as before we called BeginColumns)
  2938. void ImGui::PushColumnsBackground()
  2939. {
  2940. ImGuiWindow* window = GetCurrentWindowRead();
  2941. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2942. if (columns->Count == 1)
  2943. return;
  2944. // Optimization: avoid SetCurrentChannel() + PushClipRect()
  2945. columns->HostBackupClipRect = window->ClipRect;
  2946. SetWindowClipRectBeforeSetChannel(window, columns->HostInitialClipRect);
  2947. columns->Splitter.SetCurrentChannel(window->DrawList, 0);
  2948. }
  2949. void ImGui::PopColumnsBackground()
  2950. {
  2951. ImGuiWindow* window = GetCurrentWindowRead();
  2952. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  2953. if (columns->Count == 1)
  2954. return;
  2955. // Optimization: avoid PopClipRect() + SetCurrentChannel()
  2956. SetWindowClipRectBeforeSetChannel(window, columns->HostBackupClipRect);
  2957. columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1);
  2958. }
  2959. ImGuiOldColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id)
  2960. {
  2961. // We have few columns per window so for now we don't need bother much with turning this into a faster lookup.
  2962. for (int n = 0; n < window->ColumnsStorage.Size; n++)
  2963. if (window->ColumnsStorage[n].ID == id)
  2964. return &window->ColumnsStorage[n];
  2965. window->ColumnsStorage.push_back(ImGuiOldColumns());
  2966. ImGuiOldColumns* columns = &window->ColumnsStorage.back();
  2967. columns->ID = id;
  2968. return columns;
  2969. }
  2970. ImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count)
  2971. {
  2972. ImGuiWindow* window = GetCurrentWindow();
  2973. // Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget.
  2974. // In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer.
  2975. PushID(0x11223347 + (str_id ? 0 : columns_count));
  2976. ImGuiID id = window->GetID(str_id ? str_id : "columns");
  2977. PopID();
  2978. return id;
  2979. }
  2980. void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFlags flags)
  2981. {
  2982. ImGuiContext& g = *GImGui;
  2983. ImGuiWindow* window = GetCurrentWindow();
  2984. IM_ASSERT(columns_count >= 1);
  2985. IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported
  2986. // Acquire storage for the columns set
  2987. ImGuiID id = GetColumnsID(str_id, columns_count);
  2988. ImGuiOldColumns* columns = FindOrCreateColumns(window, id);
  2989. IM_ASSERT(columns->ID == id);
  2990. columns->Current = 0;
  2991. columns->Count = columns_count;
  2992. columns->Flags = flags;
  2993. window->DC.CurrentColumns = columns;
  2994. columns->HostCursorPosY = window->DC.CursorPos.y;
  2995. columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x;
  2996. columns->HostInitialClipRect = window->ClipRect;
  2997. columns->HostBackupParentWorkRect = window->ParentWorkRect;
  2998. window->ParentWorkRect = window->WorkRect;
  2999. // Set state for first column
  3000. // We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect
  3001. const float column_padding = g.Style.ItemSpacing.x;
  3002. const float half_clip_extend_x = ImFloor(ImMax(window->WindowPadding.x * 0.5f, window->WindowBorderSize));
  3003. const float max_1 = window->WorkRect.Max.x + column_padding - ImMax(column_padding - window->WindowPadding.x, 0.0f);
  3004. const float max_2 = window->WorkRect.Max.x + half_clip_extend_x;
  3005. columns->OffMinX = window->DC.Indent.x - column_padding + ImMax(column_padding - window->WindowPadding.x, 0.0f);
  3006. columns->OffMaxX = ImMax(ImMin(max_1, max_2) - window->Pos.x, columns->OffMinX + 1.0f);
  3007. columns->LineMinY = columns->LineMaxY = window->DC.CursorPos.y;
  3008. // Clear data if columns count changed
  3009. if (columns->Columns.Size != 0 && columns->Columns.Size != columns_count + 1)
  3010. columns->Columns.resize(0);
  3011. // Initialize default widths
  3012. columns->IsFirstFrame = (columns->Columns.Size == 0);
  3013. if (columns->Columns.Size == 0)
  3014. {
  3015. columns->Columns.reserve(columns_count + 1);
  3016. for (int n = 0; n < columns_count + 1; n++)
  3017. {
  3018. ImGuiOldColumnData column;
  3019. column.OffsetNorm = n / (float)columns_count;
  3020. columns->Columns.push_back(column);
  3021. }
  3022. }
  3023. for (int n = 0; n < columns_count; n++)
  3024. {
  3025. // Compute clipping rectangle
  3026. ImGuiOldColumnData* column = &columns->Columns[n];
  3027. float clip_x1 = IM_ROUND(window->Pos.x + GetColumnOffset(n));
  3028. float clip_x2 = IM_ROUND(window->Pos.x + GetColumnOffset(n + 1) - 1.0f);
  3029. column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX);
  3030. column->ClipRect.ClipWithFull(window->ClipRect);
  3031. }
  3032. if (columns->Count > 1)
  3033. {
  3034. columns->Splitter.Split(window->DrawList, 1 + columns->Count);
  3035. columns->Splitter.SetCurrentChannel(window->DrawList, 1);
  3036. PushColumnClipRect(0);
  3037. }
  3038. // We don't generally store Indent.x inside ColumnsOffset because it may be manipulated by the user.
  3039. float offset_0 = GetColumnOffset(columns->Current);
  3040. float offset_1 = GetColumnOffset(columns->Current + 1);
  3041. float width = offset_1 - offset_0;
  3042. PushItemWidth(width * 0.65f);
  3043. window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f);
  3044. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  3045. window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding;
  3046. }
  3047. void ImGui::NextColumn()
  3048. {
  3049. ImGuiWindow* window = GetCurrentWindow();
  3050. if (window->SkipItems || window->DC.CurrentColumns == NULL)
  3051. return;
  3052. ImGuiContext& g = *GImGui;
  3053. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  3054. if (columns->Count == 1)
  3055. {
  3056. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  3057. IM_ASSERT(columns->Current == 0);
  3058. return;
  3059. }
  3060. // Next column
  3061. if (++columns->Current == columns->Count)
  3062. columns->Current = 0;
  3063. PopItemWidth();
  3064. // Optimization: avoid PopClipRect() + SetCurrentChannel() + PushClipRect()
  3065. // (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them),
  3066. ImGuiOldColumnData* column = &columns->Columns[columns->Current];
  3067. SetWindowClipRectBeforeSetChannel(window, column->ClipRect);
  3068. columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1);
  3069. const float column_padding = g.Style.ItemSpacing.x;
  3070. columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);
  3071. if (columns->Current > 0)
  3072. {
  3073. // Columns 1+ ignore IndentX (by canceling it out)
  3074. // FIXME-COLUMNS: Unnecessary, could be locked?
  3075. window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + column_padding;
  3076. }
  3077. else
  3078. {
  3079. // New row/line: column 0 honor IndentX.
  3080. window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f);
  3081. columns->LineMinY = columns->LineMaxY;
  3082. }
  3083. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  3084. window->DC.CursorPos.y = columns->LineMinY;
  3085. window->DC.CurrLineSize = ImVec2(0.0f, 0.0f);
  3086. window->DC.CurrLineTextBaseOffset = 0.0f;
  3087. // FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup.
  3088. float offset_0 = GetColumnOffset(columns->Current);
  3089. float offset_1 = GetColumnOffset(columns->Current + 1);
  3090. float width = offset_1 - offset_0;
  3091. PushItemWidth(width * 0.65f);
  3092. window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding;
  3093. }
  3094. void ImGui::EndColumns()
  3095. {
  3096. ImGuiContext& g = *GImGui;
  3097. ImGuiWindow* window = GetCurrentWindow();
  3098. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  3099. IM_ASSERT(columns != NULL);
  3100. PopItemWidth();
  3101. if (columns->Count > 1)
  3102. {
  3103. PopClipRect();
  3104. columns->Splitter.Merge(window->DrawList);
  3105. }
  3106. const ImGuiOldColumnFlags flags = columns->Flags;
  3107. columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);
  3108. window->DC.CursorPos.y = columns->LineMaxY;
  3109. if (!(flags & ImGuiOldColumnFlags_GrowParentContentsSize))
  3110. window->DC.CursorMaxPos.x = columns->HostCursorMaxPosX; // Restore cursor max pos, as columns don't grow parent
  3111. // Draw columns borders and handle resize
  3112. // The IsBeingResized flag ensure we preserve pre-resize columns width so back-and-forth are not lossy
  3113. bool is_being_resized = false;
  3114. if (!(flags & ImGuiOldColumnFlags_NoBorder) && !window->SkipItems)
  3115. {
  3116. // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers.
  3117. const float y1 = ImMax(columns->HostCursorPosY, window->ClipRect.Min.y);
  3118. const float y2 = ImMin(window->DC.CursorPos.y, window->ClipRect.Max.y);
  3119. int dragging_column = -1;
  3120. for (int n = 1; n < columns->Count; n++)
  3121. {
  3122. ImGuiOldColumnData* column = &columns->Columns[n];
  3123. float x = window->Pos.x + GetColumnOffset(n);
  3124. const ImGuiID column_id = columns->ID + ImGuiID(n);
  3125. const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH;
  3126. const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2));
  3127. KeepAliveID(column_id);
  3128. if (IsClippedEx(column_hit_rect, column_id, false))
  3129. continue;
  3130. bool hovered = false, held = false;
  3131. if (!(flags & ImGuiOldColumnFlags_NoResize))
  3132. {
  3133. ButtonBehavior(column_hit_rect, column_id, &hovered, &held);
  3134. if (hovered || held)
  3135. g.MouseCursor = ImGuiMouseCursor_ResizeEW;
  3136. if (held && !(column->Flags & ImGuiOldColumnFlags_NoResize))
  3137. dragging_column = n;
  3138. }
  3139. // Draw column
  3140. const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator);
  3141. const float xi = IM_FLOOR(x);
  3142. window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col);
  3143. }
  3144. // Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame.
  3145. if (dragging_column != -1)
  3146. {
  3147. if (!columns->IsBeingResized)
  3148. for (int n = 0; n < columns->Count + 1; n++)
  3149. columns->Columns[n].OffsetNormBeforeResize = columns->Columns[n].OffsetNorm;
  3150. columns->IsBeingResized = is_being_resized = true;
  3151. float x = GetDraggedColumnOffset(columns, dragging_column);
  3152. SetColumnOffset(dragging_column, x);
  3153. }
  3154. }
  3155. columns->IsBeingResized = is_being_resized;
  3156. window->WorkRect = window->ParentWorkRect;
  3157. window->ParentWorkRect = columns->HostBackupParentWorkRect;
  3158. window->DC.CurrentColumns = NULL;
  3159. window->DC.ColumnsOffset.x = 0.0f;
  3160. window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
  3161. }
  3162. void ImGui::Columns(int columns_count, const char* id, bool border)
  3163. {
  3164. ImGuiWindow* window = GetCurrentWindow();
  3165. IM_ASSERT(columns_count >= 1);
  3166. ImGuiOldColumnFlags flags = (border ? 0 : ImGuiOldColumnFlags_NoBorder);
  3167. //flags |= ImGuiOldColumnFlags_NoPreserveWidths; // NB: Legacy behavior
  3168. ImGuiOldColumns* columns = window->DC.CurrentColumns;
  3169. if (columns != NULL && columns->Count == columns_count && columns->Flags == flags)
  3170. return;
  3171. if (columns != NULL)
  3172. EndColumns();
  3173. if (columns_count != 1)
  3174. BeginColumns(id, columns_count, flags);
  3175. }
  3176. //-------------------------------------------------------------------------
  3177. #endif // #ifndef IMGUI_DISABLE