Add integrated keyboard for gtk137

This commit is contained in:
daleclack 2022-08-01 15:24:27 +08:00
parent 2d3269b80e
commit 3ee9eac8bf
4 changed files with 500 additions and 6 deletions

View File

@ -31,6 +31,7 @@ set(SOURCE_FILE src/main.cc src/TextEditor.cc)
#Compile Resource
set(RESOURCE_LIST
expender.ui
text_menu.xml)
compile_gresources(RESOURCE_FILE

View File

@ -0,0 +1,440 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkExpander" id="key_expend">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="resize-toplevel">True</property>
<child>
<!-- n-columns=24 n-rows=3 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="row-homogeneous">True</property>
<property name="column-homogeneous">True</property>
<child>
<object class="GtkButton" id="btn16">
<property name="label" translatable="yes">Q</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn22">
<property name="label" translatable="yes">W</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">4</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn4">
<property name="label" translatable="yes">E</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">6</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn17">
<property name="label" translatable="yes">R</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">8</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn19">
<property name="label" translatable="yes">T</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">10</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn24">
<property name="label" translatable="yes">Y</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">12</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn20">
<property name="label" translatable="yes">U</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">14</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn8">
<property name="label" translatable="yes">I</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">16</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn14">
<property name="label" translatable="yes">O</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">18</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn15">
<property name="label" translatable="yes">P</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">20</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn0">
<property name="label" translatable="yes">A</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn25">
<property name="label" translatable="yes">Z</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">4</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn18">
<property name="label" translatable="yes">S</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">5</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn3">
<property name="label" translatable="yes">D</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">7</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn5">
<property name="label" translatable="yes">F</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">9</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn6">
<property name="label" translatable="yes">G</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">11</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn7">
<property name="label" translatable="yes">H</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">13</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn9">
<property name="label" translatable="yes">J</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">15</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn10">
<property name="label" translatable="yes">K</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">17</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn11">
<property name="label" translatable="yes">L</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">19</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="btn_caps">
<property name="label" translatable="yes">CapsLK</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="width">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn23">
<property name="label" translatable="yes">X</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">6</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn2">
<property name="label" translatable="yes">C</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">8</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn21">
<property name="label" translatable="yes">V</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">10</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn1">
<property name="label" translatable="yes">B</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">12</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn13">
<property name="label" translatable="yes">N</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">14</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn12">
<property name="label" translatable="yes">M</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">16</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="btnshift">
<property name="label" translatable="yes">Shift</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
<property name="width">4</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btntab">
<property name="label" translatable="yes">tab</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnenter">
<property name="label" translatable="yes">enter</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">21</property>
<property name="top-attach">1</property>
<property name="width">3</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Keyboard</property>
</object>
</child>
</object>
</interface>

View File

@ -3,6 +3,7 @@
#include "../json_nlohmann/json.hpp"
#include <fstream>
#include <iostream>
#include <string>
using json = nlohmann::json;
@ -27,7 +28,7 @@ TextEditor::TextEditor()
json_file.close();
// Initalize Window
vbox.set_size_request(width, height);
set_default_size(width, height);
set_icon_name("my_textedit");
// Initalize HeaderBar
@ -90,17 +91,61 @@ TextEditor::TextEditor()
infobox = dynamic_cast<Gtk::Box *>(infobar.get_content_area());
infobox->pack_start(label1);
vbox.pack_start(infobar, Gtk::PACK_SHRINK);
vbox.pack_start(hbox);
// Save config when the window is closed
signal_delete_event().connect(sigc::mem_fun(*this, &TextEditor::window_delete_event));
// Add Intergated keyboard
expend_builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/expender.ui");
expend_builder->get_widget("key_expend", expender);
expend_builder->get_widget("btnshift", btnshift);
expend_builder->get_widget("btn_caps", btncaps);
expend_builder->get_widget("btntab", btntab);
expend_builder->get_widget("btnenter", btnenter);
vbox.pack_start(*expender, Gtk::PACK_SHRINK);
// Get alphabet buttons
for(int i = 0; i < 26; i++){
char name[10];
sprintf(name, "btn%d", i);
expend_builder->get_widget(name, btns[i]);
btns[i]->signal_clicked().connect(sigc::bind(
sigc::mem_fun(*this, &TextEditor::key_pressed),
btns[i]
));
}
btntab->signal_clicked().connect(sigc::mem_fun(*this, &TextEditor::btntab_clicked));
btnenter->signal_clicked().connect(sigc::mem_fun(*this, &TextEditor::btnenter_clicked));
// Show everything
vbox.pack_start(hbox);
add(vbox);
show_all_children();
infobar.hide();
}
void TextEditor::key_pressed(Gtk::Button *button){
auto label = button->get_label();
Glib::ustring::size_type pos = 0,len = 1;
char buf[2];
if(btncaps->get_active() || btnshift->get_active()){
btnshift->set_active(false);
}else{
sprintf(buf, "%c", label[0] + 32);
label.replace(pos, len, buf);
}
//std::cout << label << std::endl;
buffer1->insert_at_cursor(label);
}
void TextEditor::btntab_clicked(){
buffer1->insert_at_cursor("\t");
}
void TextEditor::btnenter_clicked(){
buffer1->insert_at_cursor("\n");
}
bool TextEditor::window_delete_event(GdkEventAny *event)
{
// Create json raw data
@ -110,8 +155,8 @@ bool TextEditor::window_delete_event(GdkEventAny *event)
})");
// Override config in json file
data["width"] = vbox.get_width();
data["height"] = vbox.get_height();
data["width"] = sw1.get_width();
data["height"] = sw1.get_height();
// Output json data to file
std::fstream outfile;

View File

@ -13,7 +13,7 @@ private:
Gtk::MenuButton menubtn;
Gtk::Popover popover;
Gtk::ToggleButton search_button;
Glib::RefPtr<Gtk::Builder> menu_builder;
Glib::RefPtr<Gtk::Builder> menu_builder, expend_builder;
// SearchBar
Gtk::SearchBar searchbar;
@ -25,11 +25,14 @@ private:
// Window widgets
Gtk::Box vbox, hbox, *infobox;
Gtk::ScrolledWindow sw1, sw2;
Gtk::ScrolledWindow sw1;
Glib::RefPtr<Gtk::TextBuffer> buffer1;
Gtk::TextView textview1;
Gtk::InfoBar infobar;
Gtk::Label label1;
Gtk::Expander *expender;
Gtk::Button *btns[26], *btntab, *btnenter;
Gtk::ToggleButton *btnshift, *btncaps;
// File Dialog
Glib::RefPtr<Gtk::FileChooserNative> dialog;
@ -59,6 +62,11 @@ private:
void search_forward();
void search_backward();
// Keyboard press
void key_pressed(Gtk::Button *button);
void btntab_clicked();
void btnenter_clicked();
// Other Signal Handlers
void about_activated();
};