v1cont / yad

Yet Another Dialog
GNU General Public License v3.0
657 stars 58 forks source link

Add toggle-action for lists #141

Open wishdev opened 3 years ago

wishdev commented 3 years ago

Adds the toggle-action option to allow for a command to run when a checkbox or radio button is toggled in a list.

Uses the same code to call the command as select-action so I pulled that logic out and refactored it into a method that is called in all 3 spots.

v1cont commented 2 years ago

thanks, i look at this. but at first sight i cannot see how user can understand what column was triggered? there can be more than one check/radio column and must be the easy way to inform callback about this

wishdev commented 2 years ago

Thanks for taking a look.

My use case completely missed that concept (I've got a single check box on a row so I didn't even think about that option). The column number is available so let me see what the best option would be to get that passed back.

Misko-2083 commented 1 year ago

You can create a custom GtkCellRenderer that displays a check button and use it as the header cell renderer for the column.

// Define a custom cell renderer that displays a check button
GtkCellRenderer *check_renderer = gtk_cell_renderer_toggle_new();
g_object_set(check_renderer, "activatable", TRUE, NULL);

// Create a GtkTreeViewColumn and set the header cell renderer to the custom cell renderer
GtkTreeViewColumn *column = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(column, "Check Column");

gtk_tree_view_column_set_widget(column, gtk_label_new("Check Column Header"));
gtk_tree_view_column_pack_start(column, check_renderer, TRUE);
gtk_tree_view_column_add_attribute(column, check_renderer, "active", COLUMN_NUMBER); // Replace COLUMN_NUMBER with the actual column number

// Connect the "toggled" signal of the custom cell renderer to a callback function that toggles the check buttons in the column
g_signal_connect(check_renderer, "toggled", G_CALLBACK(on_check_button_toggled), list_store);

COLUMN_NUMBER variable should be set to the actual column number that contains the check buttons.

The callback function could be something like this:

void on_check_button_toggled(GtkCellRendererToggle *cell_renderer, gchar *path_str, gpointer user_data) {
    GtkListStore *list_store = GTK_LIST_STORE(user_data);
    gint column_number = COLUMN_NUMBER; // Replace COLUMN_NUMBER with the actual column number

    GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
    GtkTreeIter iter;
    gtk_tree_model_get_iter(GTK_TREE_MODEL(list_store), &iter, path);
    gtk_tree_path_free(path);

    gboolean checked;
    gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter, column_number, &checked, -1);

    checked = !checked;
    gtk_list_store_set(list_store, &iter, column_number, checked, -1);
}