Update gtk4-test

This commit is contained in:
daleclack 2021-02-28 09:35:30 +08:00
parent eca595aa3c
commit 332c97d73c
2 changed files with 24 additions and 11 deletions

Binary file not shown.

View File

@ -3,19 +3,25 @@
GtkWidget *background; GtkWidget *background;
void set_background(GFile *file,int width,int height){
const gchar *filename;
filename=g_file_get_path(file);
g_print("%s",filename);
GdkPixbuf *pixbuf=gdk_pixbuf_new_from_file(filename,NULL);
GdkPixbuf *sized=gdk_pixbuf_scale_simple(pixbuf,width,height,GDK_INTERP_BILINEAR);
gtk_picture_set_pixbuf(GTK_PICTURE(background),sized);
}
void dialog_response(GtkWidget *widget,int response){ void dialog_response(GtkWidget *widget,int response){
//Handle file chooser response and set background //Handle file chooser response and set background
const gchar *filename;
GFile *file; GFile *file;
const gchar *filename;
//g_print("%s\n",filename); //g_print("%s\n",filename);
if(response==GTK_RESPONSE_OK){ if(response==GTK_RESPONSE_OK){
file=gtk_file_chooser_get_file(GTK_FILE_CHOOSER(widget)); file=gtk_file_chooser_get_file(GTK_FILE_CHOOSER(widget));
gtk_picture_set_file(GTK_PICTURE(background),file); //gtk_picture_set_file(GTK_PICTURE(background),file);
/* //filename=g_file_get_path(file);
filename=g_file_get_path(file); set_background(file,640,360);
GdkPixbuf *pixbuf=gdk_pixbuf_new_from_file(filename,NULL);
GdkPixbuf *sized=gdk_pixbuf_scale_simple(pixbuf,640,360,GDK_INTERP_BILINEAR);
gtk_picture_set_pixbuf(GTK_PICTURE(background),sized);*/
} }
gtk_window_destroy(GTK_WINDOW(widget)); gtk_window_destroy(GTK_WINDOW(widget));
} }
@ -45,19 +51,27 @@ void default_background(int width,int height){
background=gtk_picture_new_for_pixbuf(sized); background=gtk_picture_new_for_pixbuf(sized);
} }
/*
void image_resize(GtkWidget *widget,gpointer data){ void image_resize(GtkWidget *widget,gpointer data){
//Get resized width and height //Get resized width and height
int width,height; int width,height;
width=gtk_widget_get_allocated_width(widget); width=gtk_widget_get_allocated_width(widget);
height=gtk_widget_get_allocated_height(widget); height=gtk_widget_get_allocated_height(widget);
GFile *file=gtk_picture_get_file(GTK_PICTURE(background));
if(file==NULL){
//g_print("No File Selected");
default_background(width,height); default_background(width,height);
}else{
set_background(file,width,height);
}
} }
*/
static void gtkmain(GtkApplication *app,gpointer user_data){ static void gtkmain(GtkApplication *app,gpointer user_data){
GtkWidget *window,*overlay,*button,*header; GtkWidget *window,*overlay,*button,*header,*draw_area;
//Window initalize //Window initalize
window=gtk_application_window_new(app); window=gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window),640,360); //gtk_window_set_default_size(GTK_WINDOW(window),640,360);
gtk_window_set_title(GTK_WINDOW(window),"gtk4-test2"); gtk_window_set_title(GTK_WINDOW(window),"gtk4-test2");
//Window icon //Window icon
gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack"); gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack");
@ -71,7 +85,6 @@ static void gtkmain(GtkApplication *app,gpointer user_data){
default_background(640,360); default_background(640,360);
gtk_widget_set_size_request(background,640,360); gtk_widget_set_size_request(background,640,360);
gtk_overlay_set_child(GTK_OVERLAY(overlay),background); gtk_overlay_set_child(GTK_OVERLAY(overlay),background);
//g_signal_connect_after(background,"resized",G_CALLBACK(image_resize),NULL);
//GtkButton //GtkButton
button=gtk_button_new_with_label("Change Background"); button=gtk_button_new_with_label("Change Background");
//gtk_widget_set_size_request(button,200,50); //gtk_widget_set_size_request(button,200,50);