Add gtk94

This commit is contained in:
daleclack 2021-07-09 20:07:41 +08:00
parent 286d5925e3
commit 996384ddd4
4 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,26 @@
#A Simple Project Test
project('gtk94', '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')
#Use Different Build Opinions in windows and Linux
if host_machine.system() == 'windows'
win=import('windows')
icon_res=win.compile_resources('icon.rc')
executable('gtk94', icon_res, 'src/main.cc', 'src/MyWin.cc', dependencies : gtkdep,
win_subsystem : 'windows')
else
executable('gtk94', 'src/main.cc', 'src/MyWin.cc', dependencies : gtkdep)
endif

View File

@ -0,0 +1,88 @@
#include "MyWin.hh"
MyWin::MyWin()
:vbox(Gtk::ORIENTATION_VERTICAL,5),
hbox(Gtk::ORIENTATION_HORIZONTAL,5),
btnbox(Gtk::ORIENTATION_VERTICAL,5),
btn_copy("Copy"),
btn_paste("Paste")
{
//Initalize Window
set_default_size(800,450);
set_icon_name("org.gtk.daleclack");
//Initalize Text Buffers
buffer1=textview1.get_buffer();
buffer2=textview2.get_buffer();
buffer1->signal_changed().connect(sigc::mem_fun(*this,&MyWin::buffer1_changed));
//Pack Widgets
sw1.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
sw2.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
sw1.add(textview1);
sw2.add(textview2);
btnbox.set_valign(Gtk::ALIGN_CENTER);
btnbox.pack_start(btn_copy,Gtk::PACK_SHRINK);
btnbox.pack_start(btn_paste,Gtk::PACK_SHRINK);
hbox.pack_start(sw1);
hbox.pack_start(btnbox,Gtk::PACK_SHRINK);
hbox.pack_start(sw2);
btn_copy.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::btncopy_clicked));
btn_paste.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::btnpaste_clicked));
//Disable Buttons
btn_copy.set_sensitive(false);
btn_paste.set_sensitive(false);
//A InfoBar
infobar.add_button("OK",Gtk::RESPONSE_OK);
infobar.signal_response().connect(sigc::mem_fun(*this,&MyWin::infobar_response));
infobox=dynamic_cast<Gtk::Box*>(infobar.get_content_area());
infobox->pack_start(label1);
vbox.pack_start(infobar,Gtk::PACK_SHRINK);
//Show everything
vbox.pack_start(hbox);
add(vbox);
show_all_children();
infobar.hide();
}
void MyWin::buffer1_changed(){
//When the text changed,enable the copy button
btn_copy.set_sensitive();
}
void MyWin::btncopy_clicked(){
//Get Text
Glib::ustring text;
text=buffer1->get_text();
//Get Clipboard and set text
auto refClipboard=Gtk::Clipboard::get();
refClipboard->set_text(text);
btn_paste.set_sensitive();
//Show InfoBar
label1.set_label("The Text is copyed");
infobar.show();
}
void MyWin::btnpaste_clicked(){
//Get ClipBoard
auto refClipboard=Gtk::Clipboard::get();
refClipboard->request_text(sigc::mem_fun(*this,&MyWin::clipboard_receive));
//Show InfoBar
label1.set_label("The Text is Pasted");
infobar.show();
}
void MyWin::clipboard_receive(const Glib::ustring &text){
buffer2->set_text(text);
}
void MyWin::infobar_response(int response){
infobar.hide();
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <gtkmm.h>
class MyWin : public Gtk::Window{
public:
MyWin();
private:
//Child widgets
Gtk::Box vbox,hbox,btnbox,*infobox;
Gtk::ScrolledWindow sw1,sw2;
Glib::RefPtr<Gtk::TextBuffer> buffer1,buffer2;
Gtk::TextView textview1,textview2;
Gtk::Button btn_copy,btn_paste;
Gtk::InfoBar infobar;
Gtk::Label label1;
//Signal Handlers
void btncopy_clicked();
void btnpaste_clicked();
void buffer1_changed();
void clipboard_receive(const Glib::ustring &text);
void infobar_response(int response);
};

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);
}