testing-repository/Gtk4/gtk4-test/src/main.cpp

73 lines
2.8 KiB
C++
Raw Normal View History

2021-02-04 13:35:08 +08:00
#include <gtk/gtk.h>
2021-02-04 14:10:45 +08:00
#include <res/winpe.xpm>
2021-02-04 13:35:08 +08:00
GtkWidget *background;
void dialog_response(GtkWidget *widget,int response){
2021-02-05 09:08:58 +08:00
//Handle file chooser response and set background
2021-02-04 13:35:08 +08:00
const gchar *filename;
GFile *file;
//g_print("%s\n",filename);
if(response==GTK_RESPONSE_OK){
2021-02-04 14:10:45 +08:00
file=gtk_file_chooser_get_file(GTK_FILE_CHOOSER(widget));
filename=g_file_get_path(file);
2021-02-04 13:35:08 +08:00
GdkPixbuf *pixbuf=gdk_pixbuf_new_from_file(filename,NULL);
GdkPixbuf *sized=gdk_pixbuf_scale_simple(pixbuf,480,360,GDK_INTERP_BILINEAR);
2021-02-05 08:47:09 +08:00
gtk_picture_set_pixbuf(GTK_PICTURE(background),sized);
2021-02-04 13:35:08 +08:00
}
gtk_window_destroy(GTK_WINDOW(widget));
}
void change_background(GtkWidget *widget,GtkWindow *parent){
2021-02-05 09:08:58 +08:00
//Change background
2021-02-04 13:35:08 +08:00
GtkWidget *dialog;
GtkFileChooserAction action=GTK_FILE_CHOOSER_ACTION_OPEN;
2021-02-05 09:08:58 +08:00
dialog=gtk_file_chooser_dialog_new("Choose a image File",parent,action,
2021-02-04 13:35:08 +08:00
"OK",GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,NULL);
2021-02-05 09:08:58 +08:00
//Use GtkFileFilter to select image file
GtkFileFilter *filter=gtk_file_filter_new();
gtk_file_filter_add_pattern(filter,"*.png");
gtk_file_filter_add_pattern(filter,"*.jpg");
gtk_file_filter_add_pattern(filter,"*.jpeg");
gtk_file_filter_add_pattern(filter,"*.bmp");
gtk_file_filter_add_pattern(filter,"*.xpm");
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog),filter);
2021-02-04 13:35:08 +08:00
gtk_widget_show(dialog);
g_signal_connect(dialog,"response",G_CALLBACK(dialog_response),NULL);
}
static void gtkmain(GtkApplication *app,gpointer user_data){
GtkWidget *window,*fixed,*button;
//Window initalize
window=gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window),480,360);
gtk_window_set_title(GTK_WINDOW(window),"Gtk4 test");
2021-02-07 10:56:07 +08:00
//Window icon
gtk_window_set_icon_name(GTK_WINDOW(window),"gtk3-demo");
2021-02-04 13:35:08 +08:00
//GtkFixed
fixed=gtk_fixed_new();
//Image as background
2021-02-04 14:10:45 +08:00
GdkPixbuf *pixbuf=gdk_pixbuf_new_from_xpm_data(winpe);
GdkPixbuf *sized=gdk_pixbuf_scale_simple(pixbuf,480,360,GDK_INTERP_BILINEAR);
2021-02-05 08:47:09 +08:00
background=gtk_picture_new_for_pixbuf(sized);
2021-02-05 08:49:05 +08:00
gtk_widget_set_size_request(background,480,360);
2021-02-04 13:35:08 +08:00
gtk_fixed_put(GTK_FIXED(fixed),background,0,0);
//GtkButton
button=gtk_button_new_with_label("Change Background");
gtk_widget_set_size_request(button,200,50);
gtk_fixed_put(GTK_FIXED(fixed),button,140,240);
g_signal_connect(button,"clicked",G_CALLBACK(change_background),window);
//Show all widget
gtk_window_set_child(GTK_WINDOW(window),fixed);
gtk_widget_show(window);
}
int main(int argc,char *argv[]){
int status;
GtkApplication *app=gtk_application_new("com.daleclack.gtk4.test",G_APPLICATION_FLAGS_NONE);
g_signal_connect(app,"activate",G_CALLBACK(gtkmain),NULL);
status=g_application_run(G_APPLICATION(app),argc,argv);
g_object_unref(app);
return status;
}