Я много чего перепробовал, но не могу изменить изображение кнопкой. В моем случае я бы хотел изменить изображение кнопки, позволяющей перевести мое приложение в полноэкранный режим и наоборот. Я пытался использовать несколько функций в документации, а также на форумах, но, похоже, ничего не получалось. Есть ли у вас какие-либо решения, чтобы предложить мне? Спасибо за вашу помощь:)
Ниже последнее, что я попробовал, прежде чем положиться на вас:
#include <gtk/gtk.h>
#include <time.h>
#include <err.h>
typedef struct Application Application;
struct Application
{
//Builder
GtkBuilder *builder;
//Windows
GtkWidget *splashscreen;
GtkWidget *window_main;
//Buttons
GtkWidget *button_close;
GtkWidget *button_fullscreen;
//Image
GtkWidget *img_fullscreen;
GtkWidget *img_leave_fullscreen;
//Label
GtkLabel *progresslabel;
//ProgressBar
GtkProgressBar *progressbar;
//Error
GError *error;
//Data
int is_fullscreen;
};
//Doesn't work fine :( Impossible to change picture in the button
void change_fullscreen_mode(gpointer app_data){
Application *app = app_data;
/* Check if picture is fullscreen or not */
if(app->is_fullscreen == 1)
{
// try to assign appropriate picture to the button
gtk_button_set_image(GTK_BUTTON(app->button_fullscreen),
app->img_leave_fullscreen);
//unfullscreen the window of course
gtk_window_unfullscreen(GTK_WINDOW(app->window_main));
//set the variable that stands for this state
app->is_fullscreen = 0;
}
else
{
// try to assign appropriate picture to the button
gtk_button_set_image(GTK_BUTTON(app->button_fullscreen),
app->img_fullscreen);
//fullscreen the window of course
gtk_window_fullscreen(GTK_WINDOW(app->window_main));
//set the variable that stands for this state
app->is_fullscreen = 1;
}
}
int main(int argc, char *argv[])
{
//Init gtk
gtk_init(&argc, &argv);
Application app;
app.error = NULL;
app.is_fullscreen = 1;
//Create builder
app.builder = gtk_builder_new();
if( ! gtk_builder_add_from_file( app.builder, "app.glade", &(app.error)))
{
g_warning( "%s", app.error->message );
g_free( app.error );
return(1);
}
//Get back gtk object
app.splashscreen = GTK_WIDGET(gtk_builder_get_object(app.builder,
"splashscreen"));
app.progresslabel = GTK_LABEL(gtk_builder_get_object(app.builder,
"progresslabel"));
app.progressbar = GTK_PROGRESS_BAR(gtk_builder_get_object(app.builder,
"progressbar"));
app.window_main = GTK_WIDGET(gtk_builder_get_object(app.builder,
"window_main"));
app.button_close = GTK_WIDGET(gtk_builder_get_object(app.builder,
"button_close"));
app.button_fullscreen = GTK_WIDGET(gtk_builder_get_object(app.builder,
"button_fullscreen"));
//assign a picture for each mode
app.img_leave_fullscreen =
gtk_image_new_from_file("icons/tools/gtk-leave-fullscreen18x18.png");
app.img_fullscreen =
gtk_image_new_from_file("icons/tools/gtk-fullscreen18x18.png");
//fill the container with one picture (works find)
gtk_container_add(GTK_CONTAINER(app.button_fullscreen),
app.img_leave_fullscreen);
...
//Connect signals
gtk_builder_connect_signals(app.builder, NULL); //builder
g_signal_connect(app.window_main, "destroy", G_CALLBACK(gtk_main_quit),
NULL);
g_signal_connect(app.button_close, "clicked",
G_CALLBACK(gtk_main_quit), NULL);
//quit splashscreen
g_signal_connect (app.splashscreen, "destroy", G_CALLBACK (gtk_main_quit),
NULL);
//change fullscreen mode with the corresponding button
g_signal_connect(G_OBJECT(app.button_fullscreen), "clicked",
G_CALLBACK(change_fullscreen_mode), &app);
g_object_unref(app.builder);
gtk_widget_hide(app.window_main);
gtk_window_fullscreen(GTK_WINDOW(app.splashscreen));
gtk_widget_show(app.splashscreen);
eventstartCommand = g_timeout_add(5, on_start_commands, &app);
gtk_main();
return 0;
}