'Glade Toggle Button save state
so what i am trying to accomplish is that i have a glade project in linux with a toggle button, when a user presses the toggle button and and eventually closes and reopens the project the Toggle state is getting remembered by reading and writing to a XML File. i'm having trouble working with the XML portion i am using TinyXML2 my XML looks like this
<?xml version="1.0" encoding="UTF-8"?>
<heartbeat>
<status>
<onoff>ON</onoff>
</status>
</heartbeat>
So when the Toggle is "ON" it writes ON in the XML Toggle is "OFF" it writes OFF
// * Glade Toggle-Button Event State *
#include <gtk/gtk.h>
#include<stdio.h>
#include "tinyxml2.h"
gboolean on_toggle_button_press_event(GtkWidget *widget, GdkEventButton *event,
gpointer user_data) {
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)) == TRUE) {
return TRUE;
}
return FALSE;
}
void on_run_toggle_active(GObject *obj, GParamSpec *pspec, gpointer user_data) {
g_return_if_fail (user_data != NULL);
GtkLabel *label = GTK_LABEL(user_data);
GtkToggleButton *button = GTK_TOGGLE_BUTTON(obj);
// getting the ACtive state
if (gtk_toggle_button_get_active(button) == TRUE) {
//label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), "Running..");
// gtk_label_set_text (label, "Running..");
} else {
gtk_label_set_text (label, "Idle..");
}
}
gint main(gint argc, gchar **argv) {
GtkLabel *status;
GtkWindow *window;
GtkBuilder *builder;
GtkToggleButton *run_toggle;
GtkToggleButton *kill_toggle;
gtk_init(&argc, &argv);
// Load the Glade File
builder = gtk_builder_new_from_file("glade/main_glade.glade");
window = GTK_WINDOW(gtk_builder_get_object(builder, "window1"));
status = GTK_LABEL(gtk_builder_get_object(builder, "status"));
run_toggle = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "toggle"));
kill_toggle = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "kill"));
g_object_bind_property (G_OBJECT(run_toggle), "active", G_OBJECT(kill_toggle),
"active", G_BINDING_INVERT_BOOLEAN);
g_object_bind_property (G_OBJECT(kill_toggle), "active", G_OBJECT(run_toggle),
"active", G_BINDING_INVERT_BOOLEAN);
g_signal_connect(G_OBJECT(run_toggle), "button-press-event",
G_CALLBACK(on_toggle_button_press_event), NULL);
g_signal_connect(G_OBJECT(kill_toggle), "button-press-event",
G_CALLBACK(on_toggle_button_press_event), NULL);
g_signal_connect(G_OBJECT(run_toggle), "notify::active",
G_CALLBACK(on_run_toggle_active), status);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}
Solution 1:[1]
I don't know if you are committed to using the "TinyXML2" library or if you would consider utilizing the "libxml2" library, as I have a bit more understanding of that library and it was installed as a part of the Linux system I use (Linux Mint). Also, I was able to find a fair amount of examples with the "libxml2" library as a basis for providing some sample code you might want to review. Using your initial code above, I added in functionality to allow for the retrieval and parsing of your XML data along with the output of the current toggle state to the XML file. The amount of code addition made it a bit too large to add it into this answer directly so I placed my sample program out in a Github repository. If you want to go out to the following link, you are more than welcome to copy the code and play with it.
"https://github.com/cschuls/GTK_Samples/tree/main/GTK3/SaveState"
Since your code above did not include your "glade" file, I took a bit of artistic license and developed my own "glade" file that would replicate a scenario to illustrate the data persistence happening in the program. Following is a sample of the window and the corresponding data in the XML file for your review.
I hope this provides you with ideas on how to read and write to your XML file. Even if you desire to stay with the "TinyXML2" library I would guess that the function calls in that library will be similar to the function calls used with the "libxml2" library.
Regards.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | NoDakker |