dear_imgui/
columns.rs

1#![allow(
2    clippy::cast_possible_truncation,
3    clippy::cast_sign_loss,
4    clippy::as_conversions
5)]
6use crate::Ui;
7use crate::sys;
8use bitflags::bitflags;
9
10bitflags! {
11    /// Flags for old columns system
12    #[repr(transparent)]
13    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14    pub struct OldColumnFlags: i32 {
15        /// No flags
16        const NONE = sys::ImGuiOldColumnFlags_None as i32;
17        /// Disable column dividers
18        const NO_BORDER = sys::ImGuiOldColumnFlags_NoBorder as i32;
19        /// Disable resizing columns by dragging dividers
20        const NO_RESIZE = sys::ImGuiOldColumnFlags_NoResize as i32;
21        /// Disable column width preservation when the total width changes
22        const NO_PRESERVE_WIDTHS = sys::ImGuiOldColumnFlags_NoPreserveWidths as i32;
23        /// Disable forcing columns to fit within window
24        const NO_FORCE_WITHIN_WINDOW = sys::ImGuiOldColumnFlags_NoForceWithinWindow as i32;
25        /// Restore pre-1.51 behavior of extending the parent window contents size
26        const GROW_PARENT_CONTENTS_SIZE = sys::ImGuiOldColumnFlags_GrowParentContentsSize as i32;
27    }
28}
29
30impl Default for OldColumnFlags {
31    fn default() -> Self {
32        OldColumnFlags::NONE
33    }
34}
35
36/// # Columns
37impl Ui {
38    /// Creates columns layout.
39    ///
40    /// # Arguments
41    /// * `count` - Number of columns (must be >= 1)
42    /// * `id` - Optional ID for the columns (can be empty string)
43    /// * `border` - Whether to draw borders between columns
44    #[doc(alias = "Columns")]
45    pub fn columns(&self, count: i32, id: impl AsRef<str>, border: bool) {
46        unsafe { sys::igColumns(count, self.scratch_txt(id), border) }
47    }
48
49    /// Begin columns layout with advanced flags.
50    ///
51    /// # Arguments
52    /// * `id` - ID for the columns
53    /// * `count` - Number of columns (must be >= 1)
54    /// * `flags` - Column flags
55    #[doc(alias = "BeginColumns")]
56    pub fn begin_columns(&self, id: impl AsRef<str>, count: i32, flags: OldColumnFlags) {
57        unsafe { sys::igBeginColumns(self.scratch_txt(id), count, flags.bits()) }
58    }
59
60    /// End columns layout.
61    #[doc(alias = "EndColumns")]
62    pub fn end_columns(&self) {
63        unsafe { sys::igEndColumns() }
64    }
65
66    /// Switches to the next column.
67    ///
68    /// If the current row is finished, switches to first column of the next row
69    #[doc(alias = "NextColumn")]
70    pub fn next_column(&self) {
71        unsafe { sys::igNextColumn() }
72    }
73
74    /// Returns the index of the current column
75    #[doc(alias = "GetColumnIndex")]
76    pub fn current_column_index(&self) -> i32 {
77        unsafe { sys::igGetColumnIndex() }
78    }
79
80    /// Returns the width of the current column (in pixels)
81    #[doc(alias = "GetColumnWidth")]
82    pub fn current_column_width(&self) -> f32 {
83        unsafe { sys::igGetColumnWidth(-1) }
84    }
85
86    /// Returns the width of the given column (in pixels)
87    #[doc(alias = "GetColumnWidth")]
88    pub fn column_width(&self, column_index: i32) -> f32 {
89        unsafe { sys::igGetColumnWidth(column_index) }
90    }
91
92    /// Sets the width of the current column (in pixels)
93    #[doc(alias = "SetColumnWidth")]
94    pub fn set_current_column_width(&self, width: f32) {
95        unsafe { sys::igSetColumnWidth(-1, width) };
96    }
97
98    /// Sets the width of the given column (in pixels)
99    #[doc(alias = "SetColumnWidth")]
100    pub fn set_column_width(&self, column_index: i32, width: f32) {
101        unsafe { sys::igSetColumnWidth(column_index, width) };
102    }
103
104    /// Returns the offset of the current column (in pixels from the left side of the content region)
105    #[doc(alias = "GetColumnOffset")]
106    pub fn current_column_offset(&self) -> f32 {
107        unsafe { sys::igGetColumnOffset(-1) }
108    }
109
110    /// Returns the offset of the given column (in pixels from the left side of the content region)
111    #[doc(alias = "GetColumnOffset")]
112    pub fn column_offset(&self, column_index: i32) -> f32 {
113        unsafe { sys::igGetColumnOffset(column_index) }
114    }
115
116    /// Sets the offset of the current column (in pixels from the left side of the content region)
117    #[doc(alias = "SetColumnOffset")]
118    pub fn set_current_column_offset(&self, offset_x: f32) {
119        unsafe { sys::igSetColumnOffset(-1, offset_x) };
120    }
121
122    /// Sets the offset of the given column (in pixels from the left side of the content region)
123    #[doc(alias = "SetColumnOffset")]
124    pub fn set_column_offset(&self, column_index: i32, offset_x: f32) {
125        unsafe { sys::igSetColumnOffset(column_index, offset_x) };
126    }
127
128    /// Returns the current amount of columns
129    #[doc(alias = "GetColumnsCount")]
130    pub fn column_count(&self) -> i32 {
131        unsafe { sys::igGetColumnsCount() }
132    }
133
134    // ============================================================================
135    // Advanced column utilities
136    // ============================================================================
137
138    /// Push column clip rect for the given column index.
139    /// This is useful for custom drawing within columns.
140    #[doc(alias = "PushColumnClipRect")]
141    pub fn push_column_clip_rect(&self, column_index: i32) {
142        unsafe { sys::igPushColumnClipRect(column_index) }
143    }
144
145    /// Push columns background for drawing.
146    #[doc(alias = "PushColumnsBackground")]
147    pub fn push_columns_background(&self) {
148        unsafe { sys::igPushColumnsBackground() }
149    }
150
151    /// Pop columns background.
152    #[doc(alias = "PopColumnsBackground")]
153    pub fn pop_columns_background(&self) {
154        unsafe { sys::igPopColumnsBackground() }
155    }
156
157    /// Get columns ID for the given string ID and count.
158    #[doc(alias = "GetColumnsID")]
159    pub fn get_columns_id(&self, str_id: impl AsRef<str>, count: i32) -> u32 {
160        unsafe { sys::igGetColumnsID(self.scratch_txt(str_id), count) }
161    }
162
163    // ============================================================================
164    // Column state utilities
165    // ============================================================================
166
167    /// Check if any column is being resized.
168    /// Note: This is a placeholder implementation as the underlying C++ function is not available
169    pub fn is_any_column_resizing(&self) -> bool {
170        // TODO: Implement when the proper C++ binding is available
171        // The ImGui_GetCurrentWindow function is not available in our bindings
172        false
173    }
174
175    /// Get the total width of all columns.
176    pub fn get_columns_total_width(&self) -> f32 {
177        let count = self.column_count();
178        if count <= 0 {
179            return 0.0;
180        }
181
182        let mut total_width = 0.0;
183        for i in 0..count {
184            total_width += self.column_width(i);
185        }
186        total_width
187    }
188
189    /// Set all columns to equal width.
190    pub fn set_columns_equal_width(&self) {
191        let count = self.column_count();
192        if count <= 1 {
193            return;
194        }
195
196        let total_width = self.get_columns_total_width();
197        let equal_width = total_width / count as f32;
198
199        for i in 0..count {
200            self.set_column_width(i, equal_width);
201        }
202    }
203
204    /// Get column width as a percentage of total width.
205    pub fn get_column_width_percentage(&self, column_index: i32) -> f32 {
206        let total_width = self.get_columns_total_width();
207        if total_width <= 0.0 {
208            return 0.0;
209        }
210
211        let column_width = self.column_width(column_index);
212        (column_width / total_width) * 100.0
213    }
214
215    /// Set column width as a percentage of total width.
216    pub fn set_column_width_percentage(&self, column_index: i32, percentage: f32) {
217        let total_width = self.get_columns_total_width();
218        if total_width <= 0.0 {
219            return;
220        }
221
222        let new_width = (total_width * percentage) / 100.0;
223        self.set_column_width(column_index, new_width);
224    }
225}