Add gtk132
This commit is contained in:
parent
dc67163bac
commit
436dcc45ed
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
set(CMAKE_CXX_STANDARD 17)
|
||||
cmake_minimum_required(VERSION 3.0.0)
|
||||
project(gtk131_calc2 VERSION 1.0.0)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../GCR_CMake/macros)
|
||||
include(GlibCompileResourcesSupport)
|
||||
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
|
||||
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
|
||||
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
|
||||
|
||||
include(CPack)
|
||||
include_directories(.)
|
||||
include_directories(..)
|
||||
|
||||
#Find PkgConfig to use gtkmm3
|
||||
find_package (PkgConfig REQUIRED)
|
||||
pkg_check_modules (GTKMM3 REQUIRED gtkmm-3.0)
|
||||
include_directories (${GTKMM3_INCLUDE_DIRS})
|
||||
link_directories (${GTKMM3_LIBRARY_DIRS})
|
||||
|
||||
#Find Gettext
|
||||
find_package (Gettext REQUIRED)
|
||||
set(PO_DIR ${CMAKE_BINARY_DIR}/po/zh_CN/LC_MESSAGES)
|
||||
|
||||
#Source files
|
||||
set(SOURCE_FILE src/main.cc src/TextEditor.cc)
|
||||
|
||||
#Compile Resource
|
||||
|
||||
# set(RESOURCE_LIST
|
||||
# icons/scalable/status/calcapp.svg
|
||||
# calcapp.ui)
|
||||
|
||||
# compile_gresources(RESOURCE_FILE
|
||||
# XML_OUT
|
||||
# TYPE EMBED_C
|
||||
# RESOURCES ${RESOURCE_LIST}
|
||||
# PREFIX "/org/gtk/daleclack"
|
||||
# SOURCE_DIR ${PROJECT_SOURCE_DIR}/res)
|
||||
|
||||
# Add a custom target to the makefile. Now make builds our resource file.
|
||||
# It depends on the output RESOURCE_FILE.
|
||||
|
||||
# add_custom_target(resource ALL DEPENDS ${RESOURCE_FILE})
|
||||
|
||||
#For win32 platform,use rc resource and .ico icon
|
||||
if(WIN32)
|
||||
SET(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
|
||||
set(app_WINRC ../icon.rc)
|
||||
set_property(SOURCE ../icon.rc APPEND PROPERTY
|
||||
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico
|
||||
)
|
||||
add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} ${SOURCE_FILE})
|
||||
add_custom_command( TARGET ${PROJECT_NAME}
|
||||
COMMAND echo * > ${CMAKE_BINARY_DIR}/.gitignore
|
||||
COMMAND echo **/* > ${CMAKE_BINARY_DIR}/.hgignore)
|
||||
else()
|
||||
add_executable(${PROJECT_NAME} ${SOURCE_FILE})
|
||||
add_custom_command( TARGET ${PROJECT_NAME}
|
||||
COMMAND echo \"*\" > ${CMAKE_BINARY_DIR}/.gitignore
|
||||
COMMAND echo \"**/*\" > ${CMAKE_BINARY_DIR}/.hgignore)
|
||||
endif(WIN32)
|
||||
|
||||
#Add command to generate .gitignore and .mo files
|
||||
# add_custom_command( TARGET ${PROJECT_NAME}
|
||||
# COMMAND mkdir -p ${PO_DIR}
|
||||
# COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} ${CMAKE_SOURCE_DIR}/po/zh_CN.po -o ${PO_DIR}/${PROJECT_NAME}.mo)
|
||||
|
||||
SET (CMAKE_EXTRA_CXX_FLAGS ${GTKMM3_CFLAGS_OTHER})
|
||||
target_link_libraries (${PROJECT_NAME} ${GTKMM3_LIBRARIES} -lpthread)
|
|
@ -0,0 +1,196 @@
|
|||
#include "TextEditor.hh"
|
||||
#include "text_types.hh"
|
||||
#include <fstream>
|
||||
|
||||
// Only for build in this repository
|
||||
#define text_globs supported_globs
|
||||
|
||||
TextEditor::TextEditor()
|
||||
:vbox(Gtk::ORIENTATION_VERTICAL,5),
|
||||
hbox(Gtk::ORIENTATION_HORIZONTAL,5),
|
||||
btnbox(Gtk::ORIENTATION_VERTICAL,5),
|
||||
btn_copy("Copy"),
|
||||
btn_paste("Paste"),
|
||||
btn_open("Open"),
|
||||
btn_save("Save"),
|
||||
btn_clear("Clear")
|
||||
{
|
||||
//Initalize Window
|
||||
set_default_size(800,450);
|
||||
set_icon_name("my_textedit");
|
||||
set_title("Simple Text Editor");
|
||||
|
||||
//Initalize Text Buffers
|
||||
buffer1=textview1.get_buffer();
|
||||
buffer1->signal_changed().connect(sigc::mem_fun(*this,&TextEditor::buffer1_changed));
|
||||
|
||||
//Pack Widgets
|
||||
sw1.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
|
||||
sw1.add(textview1);
|
||||
btnbox.set_valign(Gtk::ALIGN_CENTER);
|
||||
btnbox.pack_start(btn_copy,Gtk::PACK_SHRINK);
|
||||
btnbox.pack_start(btn_paste,Gtk::PACK_SHRINK);
|
||||
btnbox.pack_start(btn_open,Gtk::PACK_SHRINK);
|
||||
btnbox.pack_start(btn_save,Gtk::PACK_SHRINK);
|
||||
btnbox.pack_start(btn_clear,Gtk::PACK_SHRINK);
|
||||
hbox.pack_start(sw1);
|
||||
hbox.pack_start(btnbox,Gtk::PACK_SHRINK);
|
||||
|
||||
btn_open.signal_clicked().connect(sigc::mem_fun(*this,&TextEditor::btnopen_clicked));
|
||||
btn_save.signal_clicked().connect(sigc::mem_fun(*this,&TextEditor::btnsave_clicked));
|
||||
btn_copy.signal_clicked().connect(sigc::mem_fun(*this,&TextEditor::btncopy_clicked));
|
||||
btn_paste.signal_clicked().connect(sigc::mem_fun(*this,&TextEditor::btnpaste_clicked));
|
||||
btn_clear.signal_clicked().connect(sigc::mem_fun(*this,&TextEditor::btnclear_clicked));
|
||||
|
||||
//A InfoBar
|
||||
infobar.add_button("OK",Gtk::RESPONSE_OK);
|
||||
infobar.signal_response().connect(sigc::mem_fun(*this,&TextEditor::infobar_response));
|
||||
infobox=dynamic_cast<Gtk::Box*>(infobar.get_content_area());
|
||||
infobox->pack_start(label1);
|
||||
vbox.pack_start(infobar,Gtk::PACK_SHRINK);
|
||||
|
||||
//Disable Copy button
|
||||
btn_copy.set_sensitive(false);
|
||||
|
||||
//Show everything
|
||||
vbox.pack_start(hbox);
|
||||
add(vbox);
|
||||
show_all_children();
|
||||
infobar.hide();
|
||||
}
|
||||
|
||||
void TextEditor::btnopen_clicked(){
|
||||
//Create a dialog
|
||||
dialog=Gtk::FileChooserNative::create("Open a text file",*this,
|
||||
Gtk::FILE_CHOOSER_ACTION_OPEN,"OK","Cancel");
|
||||
|
||||
dialog->signal_response().connect(sigc::mem_fun(*this,&TextEditor::opendialog_response));
|
||||
|
||||
//Add Filters
|
||||
auto filter=Gtk::FileFilter::create();
|
||||
filter->set_name("Text Files");
|
||||
if(mimetype_supported()){
|
||||
filter->add_mime_type("text/*");
|
||||
}else{
|
||||
for(int i = 0; text_globs != NULL && text_globs[i] != NULL; i++){
|
||||
const char *glob = text_globs[i];
|
||||
filter->add_pattern(glob);
|
||||
}
|
||||
}
|
||||
dialog->add_filter(filter);
|
||||
|
||||
auto filter_any=Gtk::FileFilter::create();
|
||||
filter_any->set_name("Any Files");
|
||||
filter_any->add_pattern("*");
|
||||
dialog->add_filter(filter_any);
|
||||
|
||||
dialog->show();
|
||||
}
|
||||
|
||||
void TextEditor::opendialog_response(int response){
|
||||
if(response==Gtk::RESPONSE_ACCEPT){
|
||||
//Load Contents of a file
|
||||
auto file=dialog->get_file();
|
||||
char * contents;
|
||||
gsize length;
|
||||
if(file->load_contents(contents,length)){
|
||||
buffer1->set_text(contents);
|
||||
}
|
||||
}
|
||||
dialog.reset();
|
||||
}
|
||||
|
||||
void TextEditor::btnsave_clicked(){
|
||||
//Create a dialog
|
||||
dialog=Gtk::FileChooserNative::create("Save file",*this,
|
||||
Gtk::FILE_CHOOSER_ACTION_SAVE,"OK","Cancel");
|
||||
|
||||
dialog->signal_response().connect(sigc::mem_fun(*this,&TextEditor::savedialog_response));
|
||||
|
||||
//Add Filters
|
||||
auto filter=Gtk::FileFilter::create();
|
||||
filter->set_name("Text Files");
|
||||
if(mimetype_supported()){
|
||||
filter->add_mime_type("text/*");
|
||||
}else{
|
||||
for(int i = 0; text_globs != NULL && text_globs[i] != NULL; i++){
|
||||
const char *glob = text_globs[i];
|
||||
filter->add_pattern(glob);
|
||||
}
|
||||
}
|
||||
dialog->add_filter(filter);
|
||||
|
||||
auto filter_any=Gtk::FileFilter::create();
|
||||
filter_any->set_name("Any Files");
|
||||
filter_any->add_pattern("*");
|
||||
dialog->add_filter(filter_any);
|
||||
|
||||
dialog->show();
|
||||
}
|
||||
|
||||
void TextEditor::savedialog_response(int response){
|
||||
if(response==Gtk::RESPONSE_ACCEPT){
|
||||
//Get Filename
|
||||
auto file=dialog->get_file();
|
||||
std::string filename=file->get_path();
|
||||
//Get Text
|
||||
Glib::ustring text;
|
||||
text=buffer1->get_text();
|
||||
//Save to a file
|
||||
std::ofstream outfile;
|
||||
outfile.open(filename,std::ios_base::out);
|
||||
outfile<<text;
|
||||
outfile.close();
|
||||
}
|
||||
dialog.reset();
|
||||
}
|
||||
|
||||
void TextEditor::buffer1_changed(){
|
||||
//When the text changed,enable the copy button
|
||||
btn_copy.set_sensitive();
|
||||
}
|
||||
|
||||
void TextEditor::btncopy_clicked(){
|
||||
//Get Text
|
||||
Glib::ustring text;
|
||||
Gtk::TextBuffer::iterator start,end;
|
||||
if(buffer1->get_selection_bounds(start,end)){
|
||||
text=buffer1->get_text(start,end);
|
||||
}else{
|
||||
text=buffer1->get_text();
|
||||
}
|
||||
|
||||
//Get Clipboard and set text
|
||||
auto refClipboard=Gtk::Clipboard::get();
|
||||
refClipboard->set_text(text);
|
||||
|
||||
//Show InfoBar
|
||||
label1.set_label("The Text is copyed");
|
||||
infobar.show();
|
||||
}
|
||||
|
||||
void TextEditor::btnpaste_clicked(){
|
||||
//Get ClipBoard
|
||||
auto refClipboard=Gtk::Clipboard::get();
|
||||
refClipboard->request_text(sigc::mem_fun(*this,&TextEditor::clipboard_receive));
|
||||
}
|
||||
|
||||
void TextEditor::clipboard_receive(const Glib::ustring &text){
|
||||
if(buffer1->insert_interactive_at_cursor(text)){
|
||||
//Show InfoBar
|
||||
label1.set_label("The Text is Pasted at cursor position");
|
||||
infobar.show();
|
||||
}else{
|
||||
//Show InfoBar
|
||||
label1.set_label("Text Paste Error!");
|
||||
infobar.show();
|
||||
}
|
||||
}
|
||||
|
||||
void TextEditor::btnclear_clicked(){
|
||||
buffer1->set_text("");
|
||||
}
|
||||
|
||||
void TextEditor::infobar_response(int response){
|
||||
infobar.hide();
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <gtkmm.h>
|
||||
|
||||
class TextEditor : public Gtk::Window{
|
||||
public:
|
||||
TextEditor();
|
||||
private:
|
||||
//Child widgets
|
||||
Gtk::Box vbox,hbox,btnbox,*infobox;
|
||||
Gtk::ScrolledWindow sw1,sw2;
|
||||
Glib::RefPtr<Gtk::TextBuffer> buffer1;
|
||||
Gtk::TextView textview1;
|
||||
Gtk::Button btn_copy,btn_paste,btn_open,btn_save,btn_clear;
|
||||
Gtk::InfoBar infobar;
|
||||
Gtk::Label label1;
|
||||
//File Dialog
|
||||
Glib::RefPtr<Gtk::FileChooserNative> dialog;
|
||||
//Signal Handlers
|
||||
void btnopen_clicked();
|
||||
void opendialog_response(int response);
|
||||
void btnsave_clicked();
|
||||
void savedialog_response(int response);
|
||||
void btncopy_clicked();
|
||||
void btnpaste_clicked();
|
||||
void btnclear_clicked();
|
||||
void buffer1_changed();
|
||||
void clipboard_receive(const Glib::ustring &text);
|
||||
void infobar_response(int response);
|
||||
};
|
|
@ -0,0 +1,7 @@
|
|||
#include "TextEditor.hh"
|
||||
|
||||
int main(int argc,char **argv){
|
||||
auto app = Gtk::Application::create(argc,argv,"org.gtk.daleclack");
|
||||
TextEditor textwin;
|
||||
return app->run(textwin);
|
||||
}
|
Loading…
Reference in New Issue