diff --git a/Documentation/Reference/README.md b/Documentation/Reference/README.md index e28e030..3388c8d 100644 --- a/Documentation/Reference/README.md +++ b/Documentation/Reference/README.md @@ -93,6 +93,7 @@ - [banner_on_click_cb(ptr_userData_)](methods/banner_on_click_cb(ptr_userData_).md) - [button_on_click_cb(ptr_userData_)](methods/button_on_click_cb(ptr_userData_).md) - [checkbutton_on_toggle_cb(ptr_userData_)](methods/checkbutton_on_toggle_cb(ptr_userData_).md) +- [comborow_on_change_cb(ptr_parameter_userData_)](methods/comborow_on_change_cb(ptr_parameter_userData_).md) - [entryrow_on_submit_cb(ptr_userData_)](methods/entryrow_on_submit_cb(ptr_userData_).md) - [entryrow_on_update_cb(ptr_userData_)](methods/entryrow_on_update_cb(ptr_userData_).md) - [filedialog_on_open_cb(ptr_file_userData_)](methods/filedialog_on_open_cb(ptr_file_userData_).md) diff --git a/Documentation/Reference/classes/ComboRow.md b/Documentation/Reference/classes/ComboRow.md index ef34b64..f35bd28 100644 --- a/Documentation/Reference/classes/ComboRow.md +++ b/Documentation/Reference/classes/ComboRow.md @@ -9,6 +9,14 @@ Adw.ComboRow The identifier of the string list. +### `strings` + +The content. + +### `handler` + +The handler gets called when the selection changes. + ## Methods ### `init(title:subtitle:)` @@ -23,7 +31,29 @@ Append a string. - Parameter string: The string. - Returns: The combo row. +### `remove(at:)` + +Remove a string. +- Parameter index: The index. +- Returns: The combo row. + +### `getContent()` + +Get the string content. +- Returns: The content. + ### `selected()` Get the selected row. -- Returns: The string of the selected row.. +- Returns: The string of the selected row. + +### `select(at:)` + +Set the selected row. +- Parameter index: The index of the new selection. + +### `onChange(handler:)` + +Set a handler for when the selection changes. +- Parameter handler: The handlers. +- Returns: The combo row. diff --git a/Documentation/Reference/methods/comborow_on_change_cb(ptr_parameter_userData_).md b/Documentation/Reference/methods/comborow_on_change_cb(ptr_parameter_userData_).md new file mode 100644 index 0000000..4a43cc5 --- /dev/null +++ b/Documentation/Reference/methods/comborow_on_change_cb(ptr_parameter_userData_).md @@ -0,0 +1,7 @@ +### `comborow_on_change_cb(ptr:parameter:userData:)` + +Handle when the combo row's selection changes. +- Parameters: + - ptr: The pointer. + - parameter: The additional data. + - userData: The combo row data. diff --git a/Sources/CGTUI/shim.h b/Sources/CGTUI/shim.h index 8e0b5d3..46ce741 100644 --- a/Sources/CGTUI/shim.h +++ b/Sources/CGTUI/shim.h @@ -18,6 +18,8 @@ button_on_click_cb (void *, void *); static void checkbutton_on_toggle_cb (void *, void *); static void +comborow_on_change_cb (void *, void *, void *); +static void entryrow_on_submit_cb (void *, void *); static void entryrow_on_update_cb (void *, void *); @@ -971,6 +973,37 @@ gtui_create_comborow (uint64_t list) return row; } +static void +gtui_comborow_init_signals (uint64_t cr, uint64_t data) +{ + AdwComboRow *comborow; + + g_assert_nonnull (cr); + g_assert_nonnull (data); + g_assert (ADW_IS_COMBO_ROW (ADW_COMBO_ROW ((void *)cr))); + + comborow = ADW_COMBO_ROW (cr); + swift_retain (data); + g_signal_connect (comborow, "notify::selected", G_CALLBACK (comborow_on_change_cb), (void *)data); +} + +static char * +gtui_comborow_get_selected (uint64_t row, uint64_t list) +{ + g_assert (ADW_IS_COMBO_ROW (ADW_COMBO_ROW ((void *)row))); + g_assert (GTK_IS_STRING_LIST (GTK_STRING_LIST ((void *)list))); + + return gtk_string_list_get_string (list, adw_combo_row_get_selected (row)); +} + +static void +gtui_comborow_set_selected (uint64_t row, int position) +{ + g_assert (ADW_IS_COMBO_ROW (ADW_COMBO_ROW ((void *)row))); + + adw_combo_row_set_selected (row, position); +} + static uint64_t gtui_create_expanderrow () { @@ -2139,6 +2172,14 @@ gtui_stringlist_append (uint64_t list, const char *str) gtk_string_list_take (list, strdup (str)); } +static void +gtui_stringlist_remove (uint64_t list, int position) +{ + g_assert (GTK_IS_STRING_LIST (list)); + + gtk_string_list_remove (list, position); +} + static void gtui_box_remove (uint64_t box, uint64_t widget) { diff --git a/Sources/Libadwaita/Adwaita/ComboRow.swift b/Sources/Libadwaita/Adwaita/ComboRow.swift index 292e8c4..8e0b172 100644 --- a/Sources/Libadwaita/Adwaita/ComboRow.swift +++ b/Sources/Libadwaita/Adwaita/ComboRow.swift @@ -12,6 +12,10 @@ public class ComboRow: ActionRow { /// The identifier of the string list. private let stringListPtr: UInt64 + /// The content. + private var strings: [String] = [] + /// The handler gets called when the selection changes. + var handler: () -> Void = { } /// Initialize a combo row. /// - Parameters: @@ -21,6 +25,8 @@ public class ComboRow: ActionRow { self.stringListPtr = gtui_create_stringlist() super.init() self.nativePtr = gtui_create_comborow(self.stringListPtr) + let selfAddr = unsafeBitCast(self, to: UInt64.self) + gtui_comborow_init_signals(self.nativePtr, selfAddr) _ = self.title(title) _ = self.subtitle(subtitle) } @@ -30,17 +36,63 @@ public class ComboRow: ActionRow { /// - Returns: The combo row. public func append(_ string: String) -> ComboRow { gtui_stringlist_append(self.stringListPtr, string.cString) + strings.append(string) return self } + /// Remove a string. + /// - Parameter index: The index. + /// - Returns: The combo row. + public func remove(at index: Int) -> ComboRow { + gtui_stringlist_remove(self.stringListPtr, index.cInt) + strings.remove(at: index) + return self + } + + /// Get the string content. + /// - Returns: The content. + public func getContent() -> [String] { + strings + } + /// Get the selected row. - /// - Returns: The string of the selected row.. + /// - Returns: The string of the selected row. public func selected() -> String { - let str = gtui_stringlist_get_selected(self.nativePtr, self.stringListPtr) + let str = gtui_comborow_get_selected(self.nativePtr, self.stringListPtr) if let str { return String(cString: str) } else { return "" } } + + /// Set the selected row. + /// - Parameter index: The index of the new selection. + public func select(at index: Int) { + gtui_comborow_set_selected(self.nativePtr, index.cInt) + } + + /// Set a handler for when the selection changes. + /// - Parameter handler: The handlers. + /// - Returns: The combo row. + public func onChange(handler: @escaping () -> Void) -> ComboRow { + self.handler = handler + return self + } + +} + +/// Handle when the combo row's selection changes. +/// - Parameters: +/// - ptr: The pointer. +/// - parameter: The additional data. +/// - userData: The combo row data. +@_cdecl("comborow_on_change_cb") +func comborow_on_change_cb( + ptr: UnsafeMutableRawPointer, + parameter: UnsafeMutableRawPointer, + userData: UnsafeMutableRawPointer +) { + let row = Unmanaged.fromOpaque(userData).takeUnretainedValue() + row.handler() } diff --git a/Tests/main.swift b/Tests/main.swift index 59274ea..4097a77 100644 --- a/Tests/main.swift +++ b/Tests/main.swift @@ -252,13 +252,16 @@ public class MyApplication: Application { let stack = Stack().append(content1, transition: .slideUp) .append(content2, transition: .slideUp) preferencesWindow.setDefaultSize(width: 500, height: 400) + let combo = ComboRow(title: "ComboRow", subtitle: "Description") preferencesWindow.add( page: .init(name: "Hello", icon: .default(icon: .daytimeSunrise), description: "Hello world?") .add( group: group .add(ActionRow(title: "ActionRow", subtitle: "Description").addSuffix(stack)) .add( - ComboRow(title: "ComboRow", subtitle: "Description").append("Hello").append("World") + combo.append("Hello").append("World").onChange { + print(combo.selected()) + } ) .headerSuffix( Button("Add")