Add Window Size Config window

This commit is contained in:
daleclack 2021-10-30 22:27:40 +08:00
parent ba754c5900
commit b68df9fbb7
5 changed files with 111 additions and 255 deletions

View File

@ -1,6 +1,6 @@
set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.0.0)
project(My_GtkUi VERSION 4.0.0)
project(My_GtkUI VERSION 4.0.0)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../GCR_CMake/macros)
include(GlibCompileResourcesSupport)
@ -70,7 +70,8 @@ compile_gresources(RESOURCE_FILE
# It depends on the output RESOURCE_FILE.
add_custom_target(resource ALL DEPENDS ${RESOURCE_FILE})
set(SOURCES src/main.cpp src/MainWin.cpp src/MainStack.cpp src/LeftPanel.cpp src/GameWin.cpp src/FileWindow.cpp)
set(SOURCES src/main.cpp src/MainWin.cpp src/MainStack.cpp src/LeftPanel.cpp src/GameWin.cpp
src/FileWindow.cpp src/winconf.cpp)
# Step 4:Add the resource to compile list and compile
if(WIN32)
@ -78,9 +79,9 @@ if(WIN32)
set_property(SOURCE icon.rc APPEND PROPERTY
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/icon.ico
)
add_executable(My_GtkUi WIN32 ${app_WINRC} ${SOURCES} ${RESOURCE_FILE})
add_executable(My_GtkUI WIN32 ${app_WINRC} ${SOURCES} ${RESOURCE_FILE})
else()
add_executable(My_GtkUi ${SOURCES} ${RESOURCE_FILE})
add_executable(My_GtkUI ${SOURCES} ${RESOURCE_FILE})
endif(WIN32)
#Add command to generate .gitignore

View File

@ -1,173 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="GtkAdjustment" id="adjustment1">
<property name="upper">9999</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkAdjustment" id="adjustment2">
<property name="upper">9999</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkDialog" id="dialog">
<property name="can-focus">False</property>
<property name="title" translatable="yes">Window size config</property>
<property name="type-hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can-focus">False</property>
<property name="layout-style">end</property>
<child>
<object class="GtkButton" id="btncancel">
<property name="label" translatable="yes">Cancel</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnok">
<property name="label" translatable="yes">OK</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Input window size</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkSpinButton" id="width_spin">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="adjustment">adjustment1</property>
<property name="value">640</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">x</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="height_spin">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="adjustment">adjustment2</property>
<property name="value">480</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnGet">
<property name="label" translatable="yes">Get size</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButtonBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="layout-style">end</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="btn_default">
<property name="label" translatable="yes">Use default config</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-6">btncancel</action-widget>
<action-widget response="-5">btnok</action-widget>
</action-widgets>
</object>
</interface>

View File

@ -1,5 +1,6 @@
#include "MainWin.h"
#include "MainStack.h"
#include "winconf.h"
#include "winpe.xpm"
#include "img7.xpm"
#include "image_types.h"
@ -79,6 +80,9 @@ static void quit_activated(GSimpleAction * action, GVariant * parmeter, gpointer
gtk_window_destroy(GTK_WINDOW(data));
}
static void winsize_activated(GSimpleAction * action, GVariant * parmeter, gpointer data){
MainWin * win = MAIN_WIN(data);
ConfDlg * dialog = conf_dlg_new(GTK_WINDOW(win));
gtk_window_present(GTK_WINDOW(dialog));
}
static void default_background1(GSimpleAction * action, GVariant * parmeter, gpointer data){
@ -181,6 +185,8 @@ static void main_win_init(MainWin * win){
//For Custom Window Size
win->width=800;
win->height=450;
get_config(&(win->width),&(win->height));
g_print("%d %d",win->width,win->height);
//Initailze Window
gtk_window_set_icon_name(GTK_WINDOW(win),"My_GtkUI");

View File

@ -1,91 +1,120 @@
#include <cstdio>
#include "winconf.h"
static void conf_response(GtkWidget *widget,int response,GtkBuilder *builder_conf){
//Get width and height config and put the numbers into winsize.conf file
GObject *width_spin,*height_spin;
width_spin=gtk_builder_get_object(builder_conf,"width_spin");
height_spin=gtk_builder_get_object(builder_conf,"height_spin");
// GObject *entry_width,*entry_height;
// entry_width=gtk_builder_get_object(builder_conf,"entry_width");
// entry_height=gtk_builder_get_object(builder_conf,"entry_height");
struct _ConfDlg{
GtkDialog parent_instance;
GtkWidget * width_spin, * height_spin;
};
G_DEFINE_TYPE(ConfDlg,conf_dlg,GTK_TYPE_DIALOG)
static void conf_dlg_response(GtkDialog * dialog,int response){
if(response==GTK_RESPONSE_OK){
// const gchar *width,*height;
// width=gtk_entry_get_text(GTK_ENTRY(entry_width));
// height=gtk_entry_get_text(GTK_ENTRY(entry_height));
//Get width and height value and save the value to a file
int width,height;
width=gtk_spin_button_get_value_as_int((GtkSpinButton*)width_spin);
height=gtk_spin_button_get_value_as_int((GtkSpinButton*)height_spin);
width=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(CONF_DLG(dialog)->width_spin));
height=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(CONF_DLG(dialog)->height_spin));
freopen("winsize.conf","w",stdout);
g_print("%d %d",width,height);
g_print("width=%d\nheight=%d",width,height);
fclose(stdout);
}
gtk_widget_destroy(widget);
gtk_window_destroy(GTK_WINDOW(dialog));
}
static void set_default(GtkWidget *widget,GtkBuilder *builder_conf){
static void set_default(GtkWidget *widget,ConfDlg * dialog){
//Discard changes and set to default config
GObject *width_spin=gtk_builder_get_object(builder_conf,"width_spin");
gtk_spin_button_set_value((GtkSpinButton*)width_spin,640);
GObject *height_spin=gtk_builder_get_object(builder_conf,"height_spin");
gtk_spin_button_set_value((GtkSpinButton*)height_spin,480);
// GObject *entry_width=gtk_builder_get_object(builder_conf,"entry_width");
// GObject *entry_height=gtk_builder_get_object(builder_conf,"entry_height");
// gtk_entry_set_text(GTK_ENTRY(entry_width),"640");
// gtk_entry_set_text(GTK_ENTRY(entry_height),"360");
gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->width_spin),800);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->height_spin),450);
}
static void get_winsize(GtkWidget *widget,GtkBuilder *builder_conf){
//Get current window size
int width=640,height=360;
//char width1[7],height1[7];
static void get_winsize(GtkWidget *widget,ConfDlg * dialog){
int width=800,height=450;
GtkWindow *window;
//Get dialog window and entry
GObject *dialog=gtk_builder_get_object(builder_conf,"dialog");
GObject *width_spin=gtk_builder_get_object(builder_conf,"width_spin");
//gtk_entry_set_text(GTK_ENTRY(entry_width),"640");
GObject *height_spin=gtk_builder_get_object(builder_conf,"height_spin");
// GObject *entry_width=gtk_builder_get_object(builder_conf,"entry_width");
// GObject *entry_height=gtk_builder_get_object(builder_conf,"entry_height");
//Get main window
window=gtk_window_get_transient_for(GTK_WINDOW(dialog));
gtk_window_get_size(GTK_WINDOW(window),&width,&height);
gtk_spin_button_set_value((GtkSpinButton*)width_spin,width);
gtk_spin_button_set_value((GtkSpinButton*)height_spin,height);
//g_print("%d %d\n",width,height);
// sprintf(width1,"%d",width);
// sprintf(height1,"%d",height);
// gtk_entry_set_text(GTK_ENTRY(entry_width),width1);
// gtk_entry_set_text(GTK_ENTRY(entry_height),height1);
window = gtk_window_get_transient_for(GTK_WINDOW(dialog));
//Get Window Size
width = gtk_widget_get_size(GTK_WIDGET(window),GTK_ORIENTATION_HORIZONTAL);
height = gtk_widget_get_size(GTK_WIDGET(window),GTK_ORIENTATION_VERTICAL);
//Set Value of spin buttons
gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->width_spin),width);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->height_spin),height);
}
void conf_dialog(GtkWidget *widget,GtkBuilder *builder){
//Window config dialog
GtkBuilder *conf_size=gtk_builder_new_from_resource("/gtk42/win_size.ui");
GObject *dialog=gtk_builder_get_object(conf_size,"dialog");
GObject *window=gtk_builder_get_object(builder,"window");
gtk_window_set_transient_for(GTK_WINDOW(dialog),GTK_WINDOW(window));
g_signal_connect(dialog,"response",G_CALLBACK(conf_response),conf_size);
//Set default config
GObject *width_spin=gtk_builder_get_object(conf_size,"width_spin");
gtk_spin_button_set_value((GtkSpinButton*)width_spin,640);
//gtk_entry_set_text(GTK_ENTRY(entry_width),"640");
GObject *height_spin=gtk_builder_get_object(conf_size,"height_spin");
gtk_spin_button_set_value((GtkSpinButton*)height_spin,480);
// GObject *entry_height=gtk_builder_get_object(conf_size,"entry_height");
// gtk_entry_set_text(GTK_ENTRY(entry_height),"360");
//Get window size
GObject *btnGet=gtk_builder_get_object(conf_size,"btnGet");
g_signal_connect(btnGet,"clicked",G_CALLBACK(get_winsize),conf_size);
//Reset to default
GObject *btn_default=gtk_builder_get_object(conf_size,"btn_default");
g_signal_connect(btn_default,"clicked",G_CALLBACK(set_default),conf_size);
gtk_widget_show_all(GTK_WIDGET(dialog));
static void conf_dlg_init(ConfDlg * self){
//Initalize dialog
gtk_window_set_title(GTK_WINDOW(self),"Config");
gtk_dialog_add_buttons(GTK_DIALOG(self),"OK",GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,NULL);
//gtk_window_set_default_size(GTK_WINDOW(self),320,150);
gtk_window_set_icon_name(GTK_WINDOW(self),"My_GtkUI");
//Get Content area
GtkWidget * content_area = gtk_dialog_get_content_area(GTK_DIALOG(self));
//Initalize Spin Button
GtkAdjustment * width_adj, * height_adj;
width_adj = gtk_adjustment_new(800,640,9999,1,1,1);
height_adj = gtk_adjustment_new(450,360,9999,1,1,1);
self->width_spin = gtk_spin_button_new(width_adj,1,0);
self->height_spin = gtk_spin_button_new(height_adj,1,0);
//Add Child Widgets
GtkWidget * label_width, * label_height, * btn_getsize, * btn_default;
GtkWidget * vbox, * box_width, * box_height, * box_utils, * center_box;
//Create Boxes
center_box = gtk_center_box_new();
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
box_width = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
box_height = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
box_utils = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
gtk_widget_set_halign(box_width,GTK_ALIGN_CENTER);
gtk_widget_set_halign(box_height,GTK_ALIGN_CENTER);
gtk_widget_set_hexpand(center_box,TRUE);
gtk_widget_set_vexpand(center_box,TRUE);
//Create Widgets
label_width = gtk_label_new("Width:");
label_height = gtk_label_new("Height");
btn_default = gtk_button_new_with_label("Reset to Default Size");
btn_getsize = gtk_button_new_with_label("Get Current Size");
//Width
gtk_box_append(GTK_BOX(box_width),label_width);
gtk_box_append(GTK_BOX(box_width),self->width_spin);
//Height
gtk_box_append(GTK_BOX(box_height),label_height);
gtk_box_append(GTK_BOX(box_height),self->height_spin);
//Utils
gtk_box_append(GTK_BOX(box_utils),btn_getsize);
gtk_box_append(GTK_BOX(box_utils),btn_default);
gtk_box_append(GTK_BOX(vbox),box_width);
gtk_box_append(GTK_BOX(vbox),box_height);
gtk_box_append(GTK_BOX(vbox),box_utils);
gtk_center_box_set_center_widget(GTK_CENTER_BOX(center_box),vbox);
gtk_box_append(GTK_BOX(content_area),center_box);
//Link Signals
g_signal_connect(btn_default,"clicked",G_CALLBACK(set_default),self);
g_signal_connect(btn_getsize,"clicked",G_CALLBACK(get_winsize),self);
}
static void conf_dlg_class_init(ConfDlgClass * klass){
GTK_DIALOG_CLASS(klass)->response=conf_dlg_response;
}
ConfDlg * conf_dlg_new(GtkWindow * parent){
//Create a new dialog
return (ConfDlg*)g_object_new(conf_dlg_get_type(),"transient-for",parent,
"use-header-bar",TRUE,NULL);
}
void get_config(int *width,int *height){
freopen("winsize.conf","r",stdin);
scanf("%d",width);
scanf("%d",height);
scanf("width=%d",width);
getchar();
scanf("height=%d",height);
fclose(stdin);
}

View File

@ -1,16 +1,9 @@
#ifndef __WIN_CONF_
#define __WIN_CONF_
#pragma once
#include <gtk/gtk.h>
void conf_dialog(GtkWidget *widget,GtkBuilder *builder);
G_DECLARE_FINAL_TYPE(ConfDlg,conf_dlg,CONF,DLG,GtkDialog)
//void conf_response(GtkWidget *widget,int response,GtkBuilder *builder_conf);
//void set_default(GtkWidget *widget,GtkBuilder *builder_conf);
//void get_winsize(GtkWidget *widget,GtkBuilder *buider_conf);
ConfDlg * conf_dlg_new(GtkWindow * parent);
void get_config(int *width,int *height);
#endif