testing-repository/Gtk3/gtk28/test.cpp

129 lines
4.1 KiB
C++
Raw Normal View History

2020-12-20 11:22:22 +08:00
#include "test.h"
2020-12-23 18:22:17 +08:00
#include <cstdio>
#include <cstring>
2020-12-22 19:33:36 +08:00
#include "360.xpm"
#include "gtkwin.h"
2020-12-20 11:22:22 +08:00
void print(GtkWidget *widget,gpointer data){
2020-12-26 10:54:00 +08:00
const gchar *filename;
2020-12-27 10:19:41 +08:00
//Use the combo box id to check the selection
2020-12-26 10:54:00 +08:00
int id=gtk_combo_box_get_active(GTK_COMBO_BOX(data));
switch(id){
case 0:
2020-12-26 11:11:22 +08:00
filename="config";break;
2020-12-26 10:54:00 +08:00
case 1:
2020-12-26 11:11:22 +08:00
filename="config_1";break;
case 2:
2020-12-26 10:54:00 +08:00
filename="config_2";break;
}
2020-12-21 18:37:01 +08:00
char str[57];
2020-12-27 11:18:15 +08:00
FILE *fp;
fp=fopen(filename,"r");
if(fp==NULL){
MsgBox("Error","The config file not exist!\nUse Change config menu for a config");
}else{
fgets(str,57,fp);
MsgBox("Test",str);
}
2020-12-21 18:37:01 +08:00
fclose(stdin);
2020-12-20 11:22:22 +08:00
}
2020-12-20 22:14:40 +08:00
void quit(GtkWidget *widget,gpointer data){
gtk_widget_destroy((GtkWidget *)data);
}
2020-12-21 18:37:01 +08:00
2020-12-22 19:33:36 +08:00
void about_activate(GtkWidget *widget,gpointer data){
2020-12-24 19:39:34 +08:00
MsgBox("About gtk(28)","gtk28 by daleclack\n2020 Xe Corporation");
2020-12-22 19:33:36 +08:00
}
2020-12-26 11:11:22 +08:00
void config_activate(GtkWidget *widget,gpointer data){
InputBox("Input config","config");
}
2020-12-25 13:35:42 +08:00
void config1_activate(GtkWidget *widget,gpointer data){
InputBox("Input config1","config_1");
2020-12-23 21:07:43 +08:00
}
2020-12-25 23:44:06 +08:00
void config2_activate(GtkWidget *widget,gpointer data){
InputBox("Input config2","config_2");
}
2020-12-25 13:35:42 +08:00
void InputBox(const gchar *content,const char *filename){
2020-12-27 10:19:41 +08:00
//Initalize InputBox
2020-12-21 18:37:01 +08:00
const gchar *str;
2020-12-23 18:22:17 +08:00
int response;
2020-12-21 18:37:01 +08:00
GtkWidget *dialog=gtk_dialog_new();
GtkWindow *_dialog=GTK_WINDOW(dialog);
GtkDialog *dialog1=GTK_DIALOG(dialog);
gtk_window_set_default_size(_dialog,300,150);
gtk_window_set_position(_dialog,GTK_WIN_POS_CENTER);
gtk_window_set_title(_dialog,"Change config");
2020-12-23 18:22:17 +08:00
gtk_dialog_add_buttons(dialog1,"Cancel",GTK_RESPONSE_CANCEL,"OK",GTK_RESPONSE_OK,NULL);
2020-12-21 18:37:01 +08:00
2020-12-27 10:19:41 +08:00
//Get content_area and put entry and label to the dialog
2020-12-21 18:37:01 +08:00
GtkWidget *content_area=gtk_dialog_get_content_area(GTK_DIALOG(_dialog));
GtkContainer *_content_area=GTK_CONTAINER(content_area);
2020-12-25 13:35:42 +08:00
GtkWidget *label=gtk_label_new(" ");
gtk_container_add(_content_area,label);
label=gtk_label_new(content);
gtk_container_add(_content_area,label);
label=gtk_label_new(" ");
2020-12-21 18:37:01 +08:00
GtkWidget *entry=gtk_entry_new();
2020-12-23 21:07:43 +08:00
gtk_entry_set_text(GTK_ENTRY(entry),"default config");
2020-12-26 19:29:58 +08:00
g_signal_connect(entry,"activate",G_CALLBACK(entry_activate),(gpointer)dialog);
2020-12-21 18:37:01 +08:00
gtk_container_add(_content_area,label);
gtk_container_add(_content_area,entry);
gtk_widget_show_all(content_area);
2020-12-23 18:22:17 +08:00
response=gtk_dialog_run(dialog1);
2020-12-27 10:19:41 +08:00
//Check the response and update the file with the "OK" Response
2020-12-23 18:22:17 +08:00
if(response==GTK_RESPONSE_OK){
str=gtk_entry_get_text(GTK_ENTRY(entry));
2020-12-27 11:18:15 +08:00
if(strlen(str)!=0){
freopen(filename,"w",stdout);
g_print(str);
fclose(stdout);
gtk_widget_destroy(dialog);
MsgBox("Change config","Config changed!\nPlease Restart the application");
}else{
gtk_widget_destroy(dialog);
}
2020-12-23 18:22:17 +08:00
}else{
2020-12-21 18:37:01 +08:00
gtk_widget_destroy(dialog);
2020-12-23 18:22:17 +08:00
}
2020-12-21 18:37:01 +08:00
}
2020-12-22 19:33:36 +08:00
2020-12-26 19:29:58 +08:00
void entry_activate(GtkWidget *widget,gpointer data){
gtk_dialog_response(GTK_DIALOG(data),GTK_RESPONSE_OK);
}
2020-12-24 19:39:34 +08:00
void MsgBox(const gchar *title,const gchar *msg){
2020-12-27 10:19:41 +08:00
//Initalize the MsgBox
2020-12-22 19:33:36 +08:00
GtkWidget *dialog=gtk_dialog_new();
GtkWindow *win=GTK_WINDOW(dialog);
gtk_window_set_default_size(win,300,150);
gtk_window_set_position(win,GTK_WIN_POS_CENTER);
2020-12-24 19:39:34 +08:00
gtk_window_set_title(win,title);
2020-12-22 19:33:36 +08:00
gtk_dialog_add_button(GTK_DIALOG(dialog),"OK",GTK_RESPONSE_OK);
2020-12-27 10:19:41 +08:00
//Get GtkDialog's content_area and put widgets
2020-12-22 19:33:36 +08:00
GtkWidget *content_area=gtk_dialog_get_content_area(GTK_DIALOG(dialog));
GtkContainer *_content_area=GTK_CONTAINER(content_area);
GtkWidget *label=gtk_label_new(" ");
gtk_container_add(_content_area,label);
HBox hbox;
label=gtk_label_new(" ");
Icon msg_icon;
msg_icon.get_image(_60,50,50);
hbox.pack_start(label,FALSE,FALSE,5);
hbox.pack_start(msg_icon.img1,FALSE,FALSE,5);
label=gtk_label_new(msg);
hbox.pack_start(label,FALSE,FALSE,5);
2020-12-27 10:19:41 +08:00
//Add a GtkBox to the dialog and run
2020-12-22 19:33:36 +08:00
gtk_container_add(_content_area,hbox.hbox);
gtk_widget_show_all(content_area);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}