Add menu for gtk132

This commit is contained in:
daleclack 2022-07-04 10:51:53 +08:00
parent 436dcc45ed
commit 5bb72e6ba7
4 changed files with 74 additions and 40 deletions

View File

@ -30,21 +30,20 @@ set(SOURCE_FILE src/main.cc src/TextEditor.cc)
#Compile Resource
# set(RESOURCE_LIST
# icons/scalable/status/calcapp.svg
# calcapp.ui)
set(RESOURCE_LIST
text_menu.xml)
# compile_gresources(RESOURCE_FILE
# XML_OUT
# TYPE EMBED_C
# RESOURCES ${RESOURCE_LIST}
# PREFIX "/org/gtk/daleclack"
# SOURCE_DIR ${PROJECT_SOURCE_DIR}/res)
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})
add_custom_target(resource ALL DEPENDS ${RESOURCE_FILE})
#For win32 platform,use rc resource and .ico icon
if(WIN32)
@ -53,12 +52,12 @@ if(WIN32)
set_property(SOURCE ../icon.rc APPEND PROPERTY
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico
)
add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} ${SOURCE_FILE})
add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} ${SOURCE_FILE} ${RESOURCE_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_executable(${PROJECT_NAME} ${SOURCE_FILE} ${RESOURCE_FILE})
add_custom_command( TARGET ${PROJECT_NAME}
COMMAND echo \"*\" > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo \"**/*\" > ${CMAKE_BINARY_DIR}/.hgignore)

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="text_menu">
<section>
<item>
<attribute name="label">Open</attribute>
<attribute name="action">win.text_open</attribute>
</item>
<item>
<attribute name="label">Save As</attribute>
<attribute name="action">win.text_save</attribute>
</item>
<item>
<attribute name="label">Copy</attribute>
<attribute name="action">win.text_copy</attribute>
</item>
<item>
<attribute name="label">Paste</attribute>
<attribute name="action">win.text_paste</attribute>
</item>
<item>
<attribute name="label">Clear</attribute>
<attribute name="action">win.text_clear</attribute>
</item>
<item>
<attribute name="label">About</attribute>
<attribute name="action">win.text_about</attribute>
</item>
</section>
</menu>
</interface>

View File

@ -7,18 +7,26 @@
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")
hbox(Gtk::ORIENTATION_HORIZONTAL,5)
{
//Initalize Window
set_default_size(800,450);
set_icon_name("my_textedit");
set_title("Simple Text Editor");
//Initalize HeaderBar
header.set_decoration_layout("close,minimize,maximize:menu");
header.set_show_close_button();
menubtn.set_image_from_icon_name("open-menu");
header.pack_end(menubtn);
header.set_title("Simple Text Editor");
set_titlebar(header);
//Add a menu
menu_builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/text_menu.xml");
auto object = menu_builder->get_object("text_menu");
auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
popover.bind_model(gmenu);
menubtn.set_popover(popover);
//Initalize Text Buffers
buffer1=textview1.get_buffer();
@ -27,20 +35,14 @@ btn_clear("Clear")
//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));
//Add actions and signal handlers
add_action("text_open",sigc::mem_fun(*this,&TextEditor::btnopen_clicked));
add_action("text_save",sigc::mem_fun(*this,&TextEditor::btnsave_clicked));
add_action("text_copy",sigc::mem_fun(*this,&TextEditor::btncopy_clicked));
add_action("text_paste",sigc::mem_fun(*this,&TextEditor::btnpaste_clicked));
add_action("text_clear",sigc::mem_fun(*this,&TextEditor::btnclear_clicked));
//A InfoBar
infobar.add_button("OK",Gtk::RESPONSE_OK);
@ -49,9 +51,6 @@ btn_clear("Clear")
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);
@ -147,7 +146,7 @@ void TextEditor::savedialog_response(int response){
void TextEditor::buffer1_changed(){
//When the text changed,enable the copy button
btn_copy.set_sensitive();
}
void TextEditor::btncopy_clicked(){

View File

@ -2,16 +2,21 @@
#include <gtkmm.h>
class TextEditor : public Gtk::Window{
class TextEditor : public Gtk::ApplicationWindow{
public:
TextEditor();
private:
//Child widgets
Gtk::Box vbox,hbox,btnbox,*infobox;
//Header widgets
Gtk::HeaderBar header;
Gtk::MenuButton menubtn;
Gtk::Popover popover;
Glib::RefPtr<Gtk::Builder> menu_builder;
//Window widgets
Gtk::Box vbox,hbox,*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