Emit a column header (text + optional sort order) We cpu-clip text here so that all columns headers can be merged into a same draw call. Note that because of how we cpu-clip and display sorting indicators, you _cannot_ use SameLine() after a TableHeader()
| 2932 | // We cpu-clip text here so that all columns headers can be merged into a same draw call. |
| 2933 | // Note that because of how we cpu-clip and display sorting indicators, you _cannot_ use SameLine() after a TableHeader() |
| 2934 | void ImGui::TableHeader(const char* label) |
| 2935 | { |
| 2936 | ImGuiContext& g = *GImGui; |
| 2937 | ImGuiWindow* window = g.CurrentWindow; |
| 2938 | if (window->SkipItems) |
| 2939 | return; |
| 2940 | |
| 2941 | ImGuiTable* table = g.CurrentTable; |
| 2942 | IM_ASSERT(table != NULL && "Need to call TableHeader() after BeginTable()!"); |
| 2943 | IM_ASSERT(table->CurrentColumn != -1); |
| 2944 | const int column_n = table->CurrentColumn; |
| 2945 | ImGuiTableColumn* column = &table->Columns[column_n]; |
| 2946 | |
| 2947 | // Label |
| 2948 | if (label == NULL) |
| 2949 | label = ""; |
| 2950 | const char* label_end = FindRenderedTextEnd(label); |
| 2951 | ImVec2 label_size = CalcTextSize(label, label_end, true); |
| 2952 | ImVec2 label_pos = window->DC.CursorPos; |
| 2953 | |
| 2954 | // If we already got a row height, there's use that. |
| 2955 | // FIXME-TABLE: Padding problem if the correct outer-padding CellBgRect strays off our ClipRect? |
| 2956 | ImRect cell_r = TableGetCellBgRect(table, column_n); |
| 2957 | float label_height = ImMax(label_size.y, table->RowMinHeight - table->RowCellPaddingY * 2.0f); |
| 2958 | |
| 2959 | // Calculate ideal size for sort order arrow |
| 2960 | float w_arrow = 0.0f; |
| 2961 | float w_sort_text = 0.0f; |
| 2962 | char sort_order_suf[4] = ""; |
| 2963 | const float ARROW_SCALE = 0.65f; |
| 2964 | if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort)) |
| 2965 | { |
| 2966 | w_arrow = ImFloor(g.FontSize * ARROW_SCALE + g.Style.FramePadding.x); |
| 2967 | if (column->SortOrder > 0) |
| 2968 | { |
| 2969 | ImFormatString(sort_order_suf, IM_ARRAYSIZE(sort_order_suf), "%d", column->SortOrder + 1); |
| 2970 | w_sort_text = g.Style.ItemInnerSpacing.x + CalcTextSize(sort_order_suf).x; |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | // We feed our unclipped width to the column without writing on CursorMaxPos, so that column is still considering for merging. |
| 2975 | float max_pos_x = label_pos.x + label_size.x + w_sort_text + w_arrow; |
| 2976 | column->ContentMaxXHeadersUsed = ImMax(column->ContentMaxXHeadersUsed, column->WorkMaxX); |
| 2977 | column->ContentMaxXHeadersIdeal = ImMax(column->ContentMaxXHeadersIdeal, max_pos_x); |
| 2978 | |
| 2979 | // Keep header highlighted when context menu is open. |
| 2980 | const bool selected = (table->IsContextPopupOpen && table->ContextPopupColumn == column_n && table->InstanceInteracted == table->InstanceCurrent); |
| 2981 | ImGuiID id = window->GetID(label); |
| 2982 | 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)); |
| 2983 | ItemSize(ImVec2(0.0f, label_height)); // Don't declare unclipped width, it'll be fed ContentMaxPosHeadersIdeal |
| 2984 | if (!ItemAdd(bb, id)) |
| 2985 | return; |
| 2986 | |
| 2987 | //GetForegroundDrawList()->AddRect(cell_r.Min, cell_r.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG] |
| 2988 | //GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG] |
| 2989 | |
| 2990 | // Using AllowOverlap mode because we cover the whole cell, and we want user to be able to submit subsequent items. |
| 2991 | bool hovered, held; |