Add gtk103

This commit is contained in:
daleclack 2021-08-18 18:49:15 +08:00
parent 74f3bb9da1
commit 01e869aaf1
6 changed files with 230 additions and 0 deletions

View File

@ -0,0 +1,32 @@
#A Simple Project Test
project('gtk103', 'cpp',
default_options : ['c_std=c17', 'cpp_std=c++17'])
#Initalize variants
# gnome=import('gnome')
#Compile Resource
# gresources = gnome.compile_resources(
# 'resources', 'res/gtk91.resource.xml',
# source_dir: 'res',
# c_name: 'resources'
# )
#The Gtkmm Library as a dependency
gtkdep = dependency('gtkmm-3.0')
#Additional include dirs
dir_include = include_directories('..')
#source files
src=['src/main.cc', 'src/MyWin.cc', 'src/SetDialog.cc', '../cfgfile2/cfgfile.cc']
#Use Different Build Opinions in windows and Linux
if host_machine.system() == 'windows'
win=import('windows')
icon_res=win.compile_resources('../icon.rc')
executable('gtk103', icon_res, src, dependencies : gtkdep,
win_subsystem : 'windows', include_directories : dir_include)
else
executable('gtk103', src, dependencies : gtkdep, include_directories : dir_include)
endif

View File

@ -0,0 +1,55 @@
#include "MyWin.hh"
#include "winpe.xpm"
MyWin::MyWin()
:btnset("Setting"),
dialog1(*this)
{
//Initalize window
int width=640,height=360;
set_icon_name("org.gtk.daleclack");
set_title("CfgFile Test2");
//Add Image and button
m_overlay.add(m_back);
btnset.set_halign(Gtk::ALIGN_CENTER);
btnset.set_valign(Gtk::ALIGN_CENTER);
m_overlay.add_overlay(btnset);
add(m_overlay);
//Read Config File,else use default background
std::string filename("config");
if(readCfgFile(filename,configs)){
std::string backname=configs["background"];
if(backname.empty()){
default_background();
}else{
auto pixbuf=Gdk::Pixbuf::create_from_file(backname);
auto sized=pixbuf->scale_simple(width,height,Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(m_back.gobj(),sized->gobj());
pixbuf.reset();
sized.reset();
}
}else{
default_background();
}
dialog1.set_config(&configs,&m_back);
btnset.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::btnset_clicked));
//Show Everything
show_all_children();
}
void MyWin::btnset_clicked(){
dialog1.show_all();
}
void MyWin::default_background(){
auto pixbuf=Gdk::Pixbuf::create_from_xpm_data(winpe);
auto sized=pixbuf->scale_simple(640,360,Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(m_back.gobj(),sized->gobj());
pixbuf.reset();
sized.reset();
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <gtkmm.h>
#include "SetDialog.hh"
class MyWin : public Gtk::Window{
public:
MyWin();
private:
//Child Widgets
Gtk::Overlay m_overlay;
Gtk::Image m_back;
Gtk::Button btnset;
//Setting Dialog and config
conf_map configs;
SetDialog dialog1;
//Signal Handlers
void btnset_clicked();
void default_background();
};

View File

@ -0,0 +1,84 @@
#include "SetDialog.hh"
SetDialog::SetDialog(Gtk::Window &parent)
:vbox(Gtk::ORIENTATION_VERTICAL,5),
hbox(Gtk::ORIENTATION_HORIZONTAL,5),
btnback("BackGround")
{
Gtk::Label label1("BackGround File Name:");
//Ininalize Dialog
set_title("Settings");
set_default_size(300,150);
set_transient_for(parent);
add_button("OK",Gtk::RESPONSE_OK);
add_button("Cancel",Gtk::RESPONSE_CANCEL);
//Add Widgets
auto pbox=get_content_area();
vbox.set_halign(Gtk::ALIGN_CENTER);
vbox.set_valign(Gtk::ALIGN_CENTER);
pbox->pack_start(vbox);
//Button Box
hbox.pack_start(entry_back,Gtk::PACK_SHRINK);
hbox.pack_start(btnback,Gtk::PACK_SHRINK);
vbox.pack_start(label1,Gtk::PACK_SHRINK);
vbox.pack_start(hbox,Gtk::PACK_SHRINK);
//Signals
btnback.signal_clicked().connect(sigc::mem_fun(*this,&SetDialog::btnback_clicked));
}
void SetDialog::set_config(conf_map * src_config,Gtk::Image * src_image){
//Set Config to the Dialog
configs1=src_config;
img_back=src_image;
std::string filename=(*src_config)["background"];
entry_back.set_text(filename);
}
void SetDialog::on_response(int response_id){
if(response_id == Gtk::RESPONSE_OK){
Glib::ustring backname=entry_back.get_text();
//Change Background
if(!backname.empty()){
auto pixbuf=Gdk::Pixbuf::create_from_file(backname);
auto sized=pixbuf->scale_simple(640,360,Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(img_back->gobj(),sized->gobj());
pixbuf.reset();
sized.reset();
}
//Write filename to config file
outfile.open("config",std::ios_base::out);
if(outfile.is_open()){
outfile<<"background="<<backname<<std::endl;
}
outfile.close();
}
hide();
}
void SetDialog::btnback_clicked(){
//Create a Dialog
dialog=Gtk::FileChooserNative::create("Open a Image File",*this,
Gtk::FILE_CHOOSER_ACTION_OPEN,"OK","Cancel");
dialog->signal_response().connect(sigc::mem_fun(*this,&SetDialog::dialog_reponse));
//Add a FileFilter
auto filter_any=Gtk::FileFilter::create();
filter_any->set_name("Any Files");
filter_any->add_pattern("*");
dialog->add_filter(filter_any);
dialog->show();
}
void SetDialog::dialog_reponse(int response_id){
if(response_id == Gtk::RESPONSE_ACCEPT){
//Set filename to entry
Glib::ustring filename=dialog->get_filename();
entry_back.set_text(filename);
}
dialog.reset();
}

View File

@ -0,0 +1,30 @@
#pragma once
#include <gtkmm.h>
#include "../cfgfile2/cfgfile.hh"
class SetDialog : public Gtk::Dialog{
public:
SetDialog(Gtk::Window & parent);
void set_config(conf_map * src_config,Gtk::Image * src_image);
protected:
void on_response(int response_id) override;
private:
//Child widgets
Gtk::Box vbox,hbox;
Gtk::Button btnback;
Gtk::Entry entry_back;
Gtk::Image * img_back;
//File Chooser Dialog
Glib::RefPtr<Gtk::FileChooserNative> dialog;
//File Config
std::fstream outfile;
conf_map *configs1;
//Signal Handlers
void btnload_clicked();
void btnback_clicked();
void dialog_reponse(int response_id);
};

View File

@ -0,0 +1,7 @@
#include "MyWin.hh"
int main(int argc,char **argv){
auto app=Gtk::Application::create(argc,argv,"org.gtk.daleclack");
MyWin window;
return app->run(window);
}