testing-repository/Gtk3/gtk35/main.cpp

71 lines
2.8 KiB
C++
Raw Normal View History

2021-01-16 13:37:09 +08:00
#include <gtk/gtk.h>
2021-01-20 09:36:23 +08:00
void fileopen_dialog(GtkWidget *widget,gpointer data){
2021-01-19 13:32:31 +08:00
GtkFileChooserAction action=GTK_FILE_CHOOSER_ACTION_OPEN;
GtkWidget *dialog=gtk_file_chooser_dialog_new("Open File",GTK_WINDOW(data),action,
"Cencel",GTK_RESPONSE_CANCEL,"OK",GTK_RESPONSE_OK,NULL);
gint res;
res=gtk_dialog_run(GTK_DIALOG(dialog));
if(res==GTK_RESPONSE_OK){
char *filename;
filename=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
g_print("%s\n",filename);
}
gtk_widget_destroy(dialog);
}
2021-01-20 09:36:23 +08:00
void filesave_dialog(GtkWidget *widget,gpointer data){
GtkFileChooserAction action=GTK_FILE_CHOOSER_ACTION_SAVE;
GtkWidget *dialog=gtk_file_chooser_dialog_new("Save File",GTK_WINDOW(data),action,
2021-01-20 13:44:30 +08:00
"Cancel",GTK_RESPONSE_CANCEL,"OK",GTK_RESPONSE_OK,NULL);
2021-01-20 09:36:23 +08:00
gint res;
res=gtk_dialog_run(GTK_DIALOG(dialog));
if(res==GTK_RESPONSE_OK){
char *filename;
filename=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
g_print("%s\n",filename);
}
gtk_widget_destroy(dialog);
}
void folder_dialog(GtkWidget *widget,gpointer data){
GtkFileChooserAction action=GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
GtkWidget *dialog=gtk_file_chooser_dialog_new("Open Folder",GTK_WINDOW(data),action,
"Cencel",GTK_RESPONSE_CANCEL,"OK",GTK_RESPONSE_OK,NULL);
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),"~");
gint res;
res=gtk_dialog_run(GTK_DIALOG(dialog));
if(res==GTK_RESPONSE_OK){
char *filename;
filename=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
g_print("%s\n",filename);
}
gtk_widget_destroy(dialog);
}
2021-01-16 13:37:09 +08:00
static void gtkmain(GtkApplication *app,gpointer user_data){
2021-01-19 13:32:31 +08:00
//Main window
2021-01-20 09:36:23 +08:00
GtkBuilder *builder=gtk_builder_new_from_resource("/gtk35/window.ui");
2021-01-16 13:37:09 +08:00
GObject *window=gtk_builder_get_object(builder,"window");
2021-01-20 09:36:23 +08:00
GdkPixbuf *icon=gdk_pixbuf_new_from_resource("/gtk35/icon.jpg",NULL);
2021-01-20 13:44:30 +08:00
gtk_window_set_icon(GTK_WINDOW(window),icon);
2021-01-16 13:37:09 +08:00
gtk_application_add_window(app,GTK_WINDOW(window));
2021-01-20 09:36:23 +08:00
//fileopen dialog button
2021-01-19 13:32:31 +08:00
GObject *btn_dialog=gtk_builder_get_object(builder,"btnchoose");
2021-01-20 09:36:23 +08:00
g_signal_connect(btn_dialog,"clicked",G_CALLBACK(fileopen_dialog),window);
//filesave dialog button
GObject *btn_save=gtk_builder_get_object(builder,"btnsave");
g_signal_connect(btn_save,"clicked",G_CALLBACK(filesave_dialog),window);
//folder choose button
GObject *btn_folder=gtk_builder_get_object(builder,"btnfolder");
g_signal_connect(btn_folder,"clicked",G_CALLBACK(folder_dialog),window);
2021-01-16 13:37:09 +08:00
gtk_widget_show_all(GTK_WIDGET(window));
}
int main(int argc,char *argv[]){
GtkApplication *app;
app=gtk_application_new("com.github.daleclack.gtk35",G_APPLICATION_FLAGS_NONE);
g_signal_connect(app,"activate",G_CALLBACK(gtkmain),NULL);
return g_application_run(G_APPLICATION(app),argc,argv);
2021-01-20 13:44:30 +08:00
}