1use crate::sys;
2use crate::ui::Ui;
3use crate::widget::table::{
4 SortDirection, TABLE_MAX_COLUMNS, TableBgTarget, TableBuilder, TableColumnFlags,
5 TableColumnIndent, TableColumnIndex, TableColumnRef, TableColumnSetup, TableColumnStateFlags,
6 TableColumnUserData, TableColumnWidth, TableFlags, TableHoveredColumn, TableHoveredRow,
7 TableOptions, TableRowFlags, TableRowIndex, TableSortSpecs, TableToken, assert_current_table,
8 assert_current_table_cell, assert_current_table_has_flags, assert_current_table_row,
9 assert_non_negative_finite_f32, assert_table_column_width_phase, assert_table_setup_phase,
10 assert_valid_table_column, assert_valid_table_column_raw_in, current_table_if_any,
11 resolve_table_column, table_column_count_to_i32, table_freeze_count_to_i32,
12};
13use std::borrow::Cow;
14use std::ffi::CStr;
15
16impl Ui {
18 pub fn table<'ui>(&'ui self, str_id: impl Into<Cow<'ui, str>>) -> TableBuilder<'ui> {
39 TableBuilder::new(self, str_id)
40 }
41 #[must_use = "if return is dropped immediately, table is ended immediately."]
51 #[doc(alias = "BeginTable")]
52 pub fn begin_table(
53 &self,
54 str_id: impl AsRef<str>,
55 column_count: usize,
56 ) -> Option<TableToken<'_>> {
57 self.begin_table_with_flags(str_id, column_count, TableFlags::NONE)
58 }
59
60 #[must_use = "if return is dropped immediately, table is ended immediately."]
66 pub fn begin_table_with_flags(
67 &self,
68 str_id: impl AsRef<str>,
69 column_count: usize,
70 flags: impl Into<TableOptions>,
71 ) -> Option<TableToken<'_>> {
72 self.begin_table_with_sizing(str_id, column_count, flags, [0.0, 0.0], 0.0)
73 }
74
75 #[must_use = "if return is dropped immediately, table is ended immediately."]
84 pub fn begin_table_with_sizing(
85 &self,
86 str_id: impl AsRef<str>,
87 column_count: usize,
88 flags: impl Into<TableOptions>,
89 outer_size: impl Into<[f32; 2]>,
90 inner_width: f32,
91 ) -> Option<TableToken<'_>> {
92 let options = flags.into();
93 options.validate("Ui::begin_table_with_sizing()");
94 assert!(
95 inner_width.is_finite(),
96 "Ui::begin_table_with_sizing() inner_width must be finite"
97 );
98 assert!(
99 !options.flags.contains(TableFlags::SCROLL_X) || inner_width >= 0.0,
100 "Ui::begin_table_with_sizing() inner_width must be non-negative when SCROLL_X is enabled"
101 );
102 let outer_size = outer_size.into();
103 assert!(
104 outer_size[0].is_finite() && outer_size[1].is_finite(),
105 "Ui::begin_table_with_sizing() outer_size must contain finite values"
106 );
107 let str_id_ptr = self.scratch_txt(str_id);
108 let outer_size_vec: sys::ImVec2 = outer_size.into();
109 let column_count = table_column_count_to_i32(column_count);
110
111 let should_render = self.run_with_bound_context(|| {
112 self.assert_can_begin_table();
113 unsafe {
114 sys::igBeginTable(
115 str_id_ptr,
116 column_count,
117 options.raw(),
118 outer_size_vec,
119 inner_width,
120 )
121 }
122 });
123
124 if should_render {
125 Some(TableToken::new(self))
126 } else {
127 None
128 }
129 }
130
131 #[must_use = "if return is dropped immediately, table is ended immediately."]
141 pub fn begin_table_header<Name: AsRef<str>, const N: usize>(
142 &self,
143 str_id: impl AsRef<str>,
144 column_data: [TableColumnSetup<Name>; N],
145 ) -> Option<TableToken<'_>> {
146 self.begin_table_header_with_flags(str_id, column_data, TableFlags::NONE)
147 }
148
149 #[must_use = "if return is dropped immediately, table is ended immediately."]
158 pub fn begin_table_header_with_flags<Name: AsRef<str>, const N: usize>(
159 &self,
160 str_id: impl AsRef<str>,
161 column_data: [TableColumnSetup<Name>; N],
162 flags: impl Into<TableOptions>,
163 ) -> Option<TableToken<'_>> {
164 if let Some(token) = self.begin_table_with_flags(str_id, N, flags) {
165 for column in &column_data {
167 self.table_setup_column_with_indent_and_user_data(
168 &column.name,
169 column.flags,
170 column.width,
171 column.indent,
172 column.user_data,
173 );
174 }
175 self.table_headers_row();
176 Some(token)
177 } else {
178 None
179 }
180 }
181
182 #[doc(alias = "TableSetupColumn")]
189 pub fn table_setup_column(
190 &self,
191 label: impl AsRef<str>,
192 flags: TableColumnFlags,
193 width: Option<TableColumnWidth>,
194 ) {
195 self.table_setup_column_with_indent(label, flags, width, None);
196 }
197
198 pub fn table_setup_column_with_user_data(
204 &self,
205 label: impl AsRef<str>,
206 flags: TableColumnFlags,
207 width: Option<TableColumnWidth>,
208 user_data: impl Into<TableColumnUserData>,
209 ) {
210 self.table_setup_column_with_indent_and_user_data(label, flags, width, None, user_data);
211 }
212
213 pub fn table_setup_column_with_indent(
219 &self,
220 label: impl AsRef<str>,
221 flags: TableColumnFlags,
222 width: Option<TableColumnWidth>,
223 indent: Option<TableColumnIndent>,
224 ) {
225 self.table_setup_column_with_indent_and_user_data(label, flags, width, indent, 0);
226 }
227
228 pub fn table_setup_column_with_indent_and_user_data(
236 &self,
237 label: impl AsRef<str>,
238 flags: TableColumnFlags,
239 width: Option<TableColumnWidth>,
240 indent: Option<TableColumnIndent>,
241 user_data: impl Into<TableColumnUserData>,
242 ) {
243 flags.validate_for_setup(
244 "Ui::table_setup_column_with_indent_and_user_data()",
245 width,
246 indent,
247 );
248 let init_width_or_weight = width.map_or(0.0, TableColumnWidth::value);
249 assert!(
250 init_width_or_weight.is_finite(),
251 "Ui::table_setup_column_with_indent_and_user_data() width or weight must be finite"
252 );
253 let raw_flags = flags.bits()
254 | width.map_or(0, TableColumnWidth::raw_flags)
255 | indent.map_or(0, TableColumnIndent::raw_flags);
256 let user_data = user_data.into().get();
257 let label_ptr = self.scratch_txt(label);
258 self.run_with_bound_context(|| {
259 let table = assert_current_table("Ui::table_setup_column_with_indent_and_user_data()");
260 assert!(
261 unsafe { i32::from((*table).DeclColumnsCount) < (*table).ColumnsCount },
262 "Ui::table_setup_column_with_indent_and_user_data() called more times than the table column count"
263 );
264 assert_table_setup_phase("Ui::table_setup_column_with_indent_and_user_data()");
265 unsafe {
266 sys::igTableSetupColumn(label_ptr, raw_flags, init_width_or_weight, user_data);
267 }
268 });
269 }
270
271 pub fn table_setup_column_fixed_width(
277 &self,
278 label: impl AsRef<str>,
279 flags: TableColumnFlags,
280 width: f32,
281 ) {
282 self.table_setup_column(label, flags, Some(TableColumnWidth::Fixed(width)));
283 }
284
285 pub fn table_setup_column_stretch_weight(
291 &self,
292 label: impl AsRef<str>,
293 flags: TableColumnFlags,
294 weight: f32,
295 ) {
296 self.table_setup_column(label, flags, Some(TableColumnWidth::Stretch(weight)));
297 }
298
299 #[doc(alias = "TableHeadersRow")]
307 pub fn table_headers_row(&self) {
308 self.run_with_bound_context(|| {
309 assert_current_table("Ui::table_headers_row()");
310 self.assert_table_cell_can_change("Ui::table_headers_row()");
311 unsafe {
312 sys::igTableHeadersRow();
313 }
314 });
315 }
316
317 #[doc(alias = "TableNextColumn")]
326 pub fn table_next_column(&self) -> bool {
327 self.run_with_bound_context(|| {
328 self.assert_table_cell_can_change("Ui::table_next_column()");
329 unsafe { sys::igTableNextColumn() }
330 })
331 }
332
333 #[doc(alias = "TableSetColumnIndex")]
342 pub fn table_set_column_index(&self, column: impl Into<TableColumnIndex>) -> bool {
343 let column = column.into();
344 let column_n = column.into_i32("Ui::table_set_column_index()");
345 self.run_with_bound_context(|| {
346 self.assert_table_cell_can_change("Ui::table_set_column_index()");
347 if let Some(table) = current_table_if_any() {
348 assert_valid_table_column_raw_in(table, column_n, "Ui::table_set_column_index()");
349 }
350 unsafe { sys::igTableSetColumnIndex(column_n) }
351 })
352 }
353
354 #[doc(alias = "TableNextRow")]
361 pub fn table_next_row(&self) {
362 self.table_next_row_with_flags(TableRowFlags::NONE, 0.0);
363 }
364
365 pub fn table_next_row_with_flags(&self, flags: TableRowFlags, min_row_height: f32) {
372 assert_non_negative_finite_f32(
373 "Ui::table_next_row_with_flags()",
374 "min_row_height",
375 min_row_height,
376 );
377 self.run_with_bound_context(|| {
378 assert_current_table("Ui::table_next_row_with_flags()");
379 self.assert_table_cell_can_change("Ui::table_next_row_with_flags()");
380 unsafe { sys::igTableNextRow(flags.bits(), min_row_height) };
381 });
382 }
383
384 #[doc(alias = "TableSetupScrollFreeze")]
391 pub fn table_setup_scroll_freeze(&self, frozen_cols: usize, frozen_rows: usize) {
392 let frozen_cols = table_freeze_count_to_i32(
393 "Ui::table_setup_scroll_freeze()",
394 "frozen_cols",
395 frozen_cols,
396 TABLE_MAX_COLUMNS,
397 );
398 let frozen_rows = table_freeze_count_to_i32(
399 "Ui::table_setup_scroll_freeze()",
400 "frozen_rows",
401 frozen_rows,
402 128,
403 );
404 self.run_with_bound_context(|| {
405 assert_table_setup_phase("Ui::table_setup_scroll_freeze()");
406 unsafe { sys::igTableSetupScrollFreeze(frozen_cols, frozen_rows) }
407 });
408 }
409
410 #[doc(alias = "TableHeader")]
416 pub fn table_header(&self, label: impl AsRef<str>) {
417 let label_ptr = self.scratch_txt(label);
418 self.run_with_bound_context(|| {
419 assert_current_table_cell("Ui::table_header()");
420 unsafe { sys::igTableHeader(label_ptr) }
421 });
422 }
423
424 #[doc(alias = "TableGetColumnCount")]
426 pub fn table_get_column_count(&self) -> usize {
427 usize::try_from(self.run_with_bound_context(|| unsafe { sys::igTableGetColumnCount() }))
428 .expect("Dear ImGui returned a negative table column count")
429 }
430
431 #[doc(alias = "TableGetColumnIndex")]
433 pub fn table_get_column_index(&self) -> Option<TableColumnIndex> {
434 self.run_with_bound_context(|| {
435 current_table_if_any()?;
436 let raw = unsafe { sys::igTableGetColumnIndex() };
437 (raw >= 0).then(|| TableColumnIndex::from_i32(raw, "Ui::table_get_column_index()"))
438 })
439 }
440
441 #[doc(alias = "TableGetRowIndex")]
443 pub fn table_get_row_index(&self) -> Option<TableRowIndex> {
444 self.run_with_bound_context(|| {
445 current_table_if_any()?;
446 let raw = unsafe { sys::igTableGetRowIndex() };
447 (raw >= 0).then(|| TableRowIndex::from_i32(raw, "Ui::table_get_row_index()"))
448 })
449 }
450
451 #[doc(alias = "TableGetColumnName")]
460 pub fn table_get_column_name(&self, column: impl Into<TableColumnRef>) -> String {
461 let column = column.into();
462 self.run_with_bound_context(|| {
463 let Some(table) = current_table_if_any() else {
464 return String::new();
465 };
466 let column_n = resolve_table_column(table, column, "Ui::table_get_column_name()");
467 unsafe {
468 let ptr = sys::igTableGetColumnName_Int(column_n);
469 if ptr.is_null() {
470 String::new()
471 } else {
472 CStr::from_ptr(ptr).to_str().unwrap_or("").to_owned()
473 }
474 }
475 })
476 }
477
478 #[doc(alias = "TableGetColumnFlags")]
486 pub fn table_get_column_flags(
487 &self,
488 column: impl Into<TableColumnRef>,
489 ) -> TableColumnStateFlags {
490 let column = column.into();
491 self.run_with_bound_context(|| {
492 let Some(table) = current_table_if_any() else {
493 return TableColumnStateFlags::empty();
494 };
495 let column_n = resolve_table_column(table, column, "Ui::table_get_column_flags()");
496 unsafe { TableColumnStateFlags::from_bits_retain(sys::igTableGetColumnFlags(column_n)) }
497 })
498 }
499
500 #[doc(alias = "TableSetColumnEnabled")]
507 pub fn table_set_column_enabled(&self, column: impl Into<TableColumnRef>, enabled: bool) {
508 let column = column.into();
509 self.run_with_bound_context(|| {
510 let table = assert_current_table_has_flags(
511 TableFlags::HIDEABLE,
512 "Ui::table_set_column_enabled()",
513 );
514 let column_n = resolve_table_column(table, column, "Ui::table_set_column_enabled()");
515 unsafe { sys::igTableSetColumnEnabled(column_n, enabled) }
516 });
517 }
518
519 #[doc(alias = "TableGetHoveredColumn")]
522 pub fn table_get_hovered_column(&self) -> TableHoveredColumn {
523 self.run_with_bound_context(|| {
524 let raw = unsafe { sys::igTableGetHoveredColumn() };
525 if raw < 0 {
526 return TableHoveredColumn::None;
527 }
528 if let Some(table) = current_table_if_any() {
529 let column_count = unsafe { (*table).ColumnsCount };
530 if raw == column_count {
531 return TableHoveredColumn::UnusedSpace;
532 }
533 }
534 TableHoveredColumn::Column(TableColumnIndex::from_i32(
535 raw,
536 "Ui::table_get_hovered_column()",
537 ))
538 })
539 }
540
541 #[doc(alias = "TableSetColumnWidth")]
548 pub fn table_set_column_width(&self, column: impl Into<TableColumnIndex>, width: f32) {
549 assert_non_negative_finite_f32("Ui::table_set_column_width()", "width", width);
550 let column = column.into();
551 self.run_with_bound_context(|| {
552 assert_table_column_width_phase("Ui::table_set_column_width()");
553 let column_n = assert_valid_table_column(column, "Ui::table_set_column_width()");
554 unsafe { sys::igTableSetColumnWidth(column_n, width) }
555 });
556 }
557
558 #[doc(alias = "TableSetBgColor")]
567 pub fn table_set_cell_bg_color_u32(&self, color: u32, column: impl Into<TableColumnRef>) {
568 let column = column.into();
569 self.run_with_bound_context(|| {
570 let table = assert_current_table_row("Ui::table_set_cell_bg_color_u32()");
571 let column_n = resolve_table_column(table, column, "Ui::table_set_cell_bg_color_u32()");
572 unsafe { sys::igTableSetBgColor(TableBgTarget::CellBg as i32, color, column_n) }
573 });
574 }
575
576 pub fn table_set_cell_bg_color(&self, rgba: [f32; 4], column: impl Into<TableColumnRef>) {
582 let col = crate::colors::Color::from_array(rgba).to_imgui_u32();
583 self.table_set_cell_bg_color_u32(col, column);
584 }
585
586 #[doc(alias = "TableSetBgColor")]
592 pub fn table_set_row_bg0_color_u32(&self, color: u32) {
593 self.run_with_bound_context(|| {
594 assert_current_table_row("Ui::table_set_row_bg0_color_u32()");
595 unsafe { sys::igTableSetBgColor(TableBgTarget::RowBg0 as i32, color, -1) }
596 });
597 }
598
599 pub fn table_set_row_bg0_color(&self, rgba: [f32; 4]) {
605 let col = crate::colors::Color::from_array(rgba).to_imgui_u32();
606 self.table_set_row_bg0_color_u32(col);
607 }
608
609 #[doc(alias = "TableSetBgColor")]
615 pub fn table_set_row_bg1_color_u32(&self, color: u32) {
616 self.run_with_bound_context(|| {
617 assert_current_table_row("Ui::table_set_row_bg1_color_u32()");
618 unsafe { sys::igTableSetBgColor(TableBgTarget::RowBg1 as i32, color, -1) }
619 });
620 }
621
622 pub fn table_set_row_bg1_color(&self, rgba: [f32; 4]) {
628 let col = crate::colors::Color::from_array(rgba).to_imgui_u32();
629 self.table_set_row_bg1_color_u32(col);
630 }
631
632 #[doc(alias = "TableGetHoveredRow")]
634 pub fn table_get_hovered_row(&self) -> TableHoveredRow {
635 self.run_with_bound_context(|| {
636 if current_table_if_any().is_none() {
637 return TableHoveredRow::None;
638 }
639 let raw = unsafe { sys::igTableGetHoveredRow() };
640 if raw < 0 {
641 return TableHoveredRow::None;
642 }
643 TableHoveredRow::Row(TableRowIndex::from_i32(raw, "Ui::table_get_hovered_row()"))
644 })
645 }
646
647 #[doc(alias = "TableGetHeaderRowHeight")]
653 pub fn table_get_header_row_height(&self) -> f32 {
654 self.run_with_bound_context(|| {
655 assert_current_table("Ui::table_get_header_row_height()");
656 unsafe { sys::igTableGetHeaderRowHeight() }
657 })
658 }
659
660 #[doc(alias = "TableSetColumnSortDirection")]
667 pub fn table_set_column_sort_direction(
668 &self,
669 column: impl Into<TableColumnIndex>,
670 dir: SortDirection,
671 append_to_sort_specs: bool,
672 ) {
673 let column = column.into();
674 self.run_with_bound_context(|| {
675 let table = assert_current_table("Ui::table_set_column_sort_direction()");
676 let table_flags = TableFlags::from_bits_retain(unsafe { (*table).Flags });
677 assert!(
678 table_flags.contains(TableFlags::SORTABLE),
679 "Ui::table_set_column_sort_direction() requires the current table to have SORTABLE"
680 );
681 if dir == SortDirection::None {
682 assert!(
683 table_flags.contains(TableFlags::SORT_TRISTATE),
684 "Ui::table_set_column_sort_direction() requires SORT_TRISTATE for SortDirection::None"
685 );
686 }
687 let column_n =
688 assert_valid_table_column(column, "Ui::table_set_column_sort_direction()");
689 unsafe {
690 sys::igTableSetColumnSortDirection(column_n, dir.into(), append_to_sort_specs)
691 }
692 });
693 }
694
695 #[doc(alias = "TableGetSortSpecs")]
701 pub fn table_get_sort_specs(&self) -> Option<TableSortSpecs> {
702 self.run_with_bound_context(|| unsafe {
703 let table = current_table_if_any()?;
704 let ptr = sys::igTableGetSortSpecs();
705 if ptr.is_null() {
706 None
707 } else {
708 Some(TableSortSpecs::from_raw(self, table, ptr))
709 }
710 })
711 }
712}