mirror of https://github.com/daleclack/My_GtkUi
Add code for calc app
This commit is contained in:
parent
1e89576b8e
commit
b2d302318e
|
@ -15,7 +15,8 @@ set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
|
||||||
include(CPack)
|
include(CPack)
|
||||||
include_directories(.)
|
include_directories(.)
|
||||||
include_directories(..)
|
include_directories(..)
|
||||||
include_directories(src/core src/ui src/apps src/panels src/image_app src/game24_app)
|
include_directories(src/core src/ui src/apps src/panels src/image_app
|
||||||
|
src/game24_app src/calc_app)
|
||||||
|
|
||||||
find_package (PkgConfig REQUIRED)
|
find_package (PkgConfig REQUIRED)
|
||||||
pkg_check_modules (GTKMM3 REQUIRED gtkmm-3.0)
|
pkg_check_modules (GTKMM3 REQUIRED gtkmm-3.0)
|
||||||
|
@ -27,7 +28,8 @@ set(SOURCES src/core/main.cc src/core/MyWin.cc src/core/MyStack.cc src/cfgfile/c
|
||||||
src/draw_app/drawing.cc src/file_app/FileWindow.cc src/game_app/Game.cc
|
src/draw_app/drawing.cc src/file_app/FileWindow.cc src/game_app/Game.cc
|
||||||
src/game24_app/Game24.cc src/game24_app/Game24Win.cc
|
src/game24_app/Game24.cc src/game24_app/Game24Win.cc
|
||||||
src/image_app/ImageApp.cc src/image_app/MyImage.cc
|
src/image_app/ImageApp.cc src/image_app/MyImage.cc
|
||||||
src/run_app/Runner.cc src/text_app/TextEditor.cc)
|
src/run_app/Runner.cc src/text_app/TextEditor.cc
|
||||||
|
src/calc_app/calc.cc src/calc_app/CalcApp.cc)
|
||||||
|
|
||||||
#Compile resources with GCR_CMake
|
#Compile resources with GCR_CMake
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<gresources>
|
||||||
|
<gresource prefix="/org/gtk/daleclack">
|
||||||
|
<file preprocess="xml-stripblanks">menubar.xml</file>
|
||||||
|
<file preprocess="xml-stripblanks">mainmenu.xml</file>
|
||||||
|
<file preprocess="xml-stripblanks">image_appmenu.xml</file>
|
||||||
|
<file preprocess="xml-stripblanks">stack.ui</file>
|
||||||
|
<file preprocess="xml-stripblanks">prefs_stack.ui</file>
|
||||||
|
<file preprocess="xml-stripblanks">mydock.ui</file>
|
||||||
|
<file preprocess="xml-stripblanks">game1.ui</file>
|
||||||
|
<file preprocess="xml-stripblanks">game24.ui</file>
|
||||||
|
<file>style.css</file>
|
||||||
|
<file>reset.css</file>
|
||||||
|
<file>dock_style.css</file>
|
||||||
|
<file>folder.svg</file>
|
||||||
|
<file>folder-images.svg</file>
|
||||||
|
<file>image_file.svg</file>
|
||||||
|
<file>gnome-fs-regular.svg</file>
|
||||||
|
<file>icons/16x16/actions/finder_audio.svg</file>
|
||||||
|
<file>icons/16x16/actions/finder-battery.svg</file>
|
||||||
|
<file>icons/16x16/actions/finder-computer.svg</file>
|
||||||
|
<file>icons/16x16/actions/finder-wifi.svg</file>
|
||||||
|
<file>icons/16x16/actions/My_GtkUI.png</file>
|
||||||
|
<file>icons/16x16/actions/open-menu.svg</file>
|
||||||
|
<file>icons/16x16/actions/list-add.svg</file>
|
||||||
|
<file>icons/16x16/actions/list-remove.svg</file>
|
||||||
|
<file>icons/48x48/actions/dialog-error.png</file>
|
||||||
|
<file>icons/48x48/actions/game.png</file>
|
||||||
|
<file>icons/48x48/actions/game_running.png</file>
|
||||||
|
<file>icons/48x48/actions/btnabout.png</file>
|
||||||
|
<file>icons/48x48/actions/My_GtkUI.png</file>
|
||||||
|
<file>icons/scalable/status/24game.svg</file>
|
||||||
|
<file>icons/scalable/status/24game_running.svg</file>
|
||||||
|
<file>icons/scalable/status/audacious_whitesur.svg</file>
|
||||||
|
<file>icons/scalable/status/my_prefs.svg</file>
|
||||||
|
<file>icons/scalable/status/my_prefs_running.svg</file>
|
||||||
|
<file>icons/scalable/status/drawing_app.svg</file>
|
||||||
|
<file>icons/scalable/status/drawing_app_running.svg</file>
|
||||||
|
<file>icons/scalable/status/finder-find.svg</file>
|
||||||
|
<file>icons/scalable/status/file-app.svg</file>
|
||||||
|
<file>icons/scalable/status/file-app_running.svg</file>
|
||||||
|
<file>icons/scalable/status/display_prefs.svg</file>
|
||||||
|
<file>icons/scalable/status/myrunner.svg</file>
|
||||||
|
<file>icons/scalable/status/image_app.svg</file>
|
||||||
|
<file>icons/scalable/status/image_app_running.svg</file>
|
||||||
|
<file>icons/scalable/status/my_textedit.svg</file>
|
||||||
|
<file>icons/scalable/status/my_textedit_running.svg</file>
|
||||||
|
<file>icons/scalable/status/vlc_whitesur.svg</file>
|
||||||
|
<file>icons/scalable/status/my_trash.svg</file>
|
||||||
|
<file>icons/scalable/status/circle.svg</file>
|
||||||
|
<file>icons/scalable/status/freehand.svg</file>
|
||||||
|
<file>icons/scalable/status/line.svg</file>
|
||||||
|
<file>icons/scalable/status/rectangle.svg</file>
|
||||||
|
</gresource>
|
||||||
|
</gresources>
|
|
@ -0,0 +1,30 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <gtkmm.h>
|
||||||
|
|
||||||
|
class CalcApp : public Gtk::Window
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static CalcApp *create();
|
||||||
|
CalcApp(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_Glade);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Main GtkBuilder
|
||||||
|
Glib::RefPtr<Gtk::Builder> ref_builder;
|
||||||
|
|
||||||
|
// Calculator Class
|
||||||
|
int pos;
|
||||||
|
bool focused, winned;
|
||||||
|
Glib::ustring tmp;
|
||||||
|
|
||||||
|
// Child widgets
|
||||||
|
Gtk::Entry *entry_ans;
|
||||||
|
Gtk::Button *btns[16], *btnback, *btnclear, *btnanswer;
|
||||||
|
|
||||||
|
// Signal Handlers
|
||||||
|
void btns_clicked(Gtk::Button *button);
|
||||||
|
void btnclear_clicked();
|
||||||
|
void btnback_clicked();
|
||||||
|
void btnanswer_clicked();
|
||||||
|
void entry_ans_focus();
|
||||||
|
};
|
|
@ -0,0 +1,117 @@
|
||||||
|
#include "CalcApp.hh"
|
||||||
|
#include "calc.hh"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
CalcApp::CalcApp(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_Glade)
|
||||||
|
: Gtk::Window(cobject),
|
||||||
|
ref_builder(ref_Glade),
|
||||||
|
winned(true)
|
||||||
|
{
|
||||||
|
set_icon_name("Calculator");
|
||||||
|
|
||||||
|
// Get Widgets
|
||||||
|
//ref_builder->get_widget("label_numbers", label_numbers);
|
||||||
|
ref_builder->get_widget("entry_ans", entry_ans);
|
||||||
|
ref_builder->get_widget("btnanswer", btnanswer);
|
||||||
|
ref_builder->get_widget("btnback",btnback);
|
||||||
|
ref_builder->get_widget("btnclear",btnclear);
|
||||||
|
ref_builder->get_widget("btn0", btns[0]);
|
||||||
|
ref_builder->get_widget("btn1", btns[1]);
|
||||||
|
ref_builder->get_widget("btn2", btns[2]);
|
||||||
|
ref_builder->get_widget("btn3", btns[3]);
|
||||||
|
ref_builder->get_widget("btn4", btns[4]);
|
||||||
|
ref_builder->get_widget("btn5", btns[5]);
|
||||||
|
ref_builder->get_widget("btn6", btns[6]);
|
||||||
|
ref_builder->get_widget("btn7", btns[7]);
|
||||||
|
ref_builder->get_widget("btn8", btns[8]);
|
||||||
|
ref_builder->get_widget("btn9", btns[9]);
|
||||||
|
ref_builder->get_widget("btnadd", btns[10]);
|
||||||
|
ref_builder->get_widget("btnsubb", btns[11]);
|
||||||
|
ref_builder->get_widget("btnmul", btns[12]);
|
||||||
|
ref_builder->get_widget("btndiv", btns[13]);
|
||||||
|
ref_builder->get_widget("btnleft", btns[14]);
|
||||||
|
ref_builder->get_widget("btnright", btns[15]);
|
||||||
|
|
||||||
|
// Link Signals
|
||||||
|
btnclear->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnclear_clicked));
|
||||||
|
btnback->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnback_clicked));
|
||||||
|
btnanswer->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnanswer_clicked));
|
||||||
|
for (int i = 0; i < 16; i++)
|
||||||
|
{
|
||||||
|
btns[i]->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &CalcApp::btns_clicked), btns[i]));
|
||||||
|
}
|
||||||
|
entry_ans->signal_grab_focus().connect(sigc::mem_fun(*this, &CalcApp::entry_ans_focus));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalcApp::btns_clicked(Gtk::Button *button)
|
||||||
|
{
|
||||||
|
// Get Original text and add number from button
|
||||||
|
|
||||||
|
// 1.Get text buffer for insert
|
||||||
|
auto buffer = entry_ans->get_buffer();
|
||||||
|
|
||||||
|
// 2.Get text to insert
|
||||||
|
Glib::ustring label = button->get_label();
|
||||||
|
|
||||||
|
if (focused)
|
||||||
|
{
|
||||||
|
pos = entry_ans->get_position();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3.Get Position
|
||||||
|
buffer->insert_text(pos, label);
|
||||||
|
pos++;
|
||||||
|
focused = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalcApp::entry_ans_focus()
|
||||||
|
{
|
||||||
|
focused = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalcApp::btnclear_clicked(){
|
||||||
|
// Clear the text
|
||||||
|
entry_ans->set_text("");
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalcApp::btnback_clicked(){
|
||||||
|
// Get Original text and add number from button
|
||||||
|
|
||||||
|
// 1.Get text buffer for insert
|
||||||
|
auto buffer = entry_ans->get_buffer();
|
||||||
|
|
||||||
|
// 2.Get Position to delete
|
||||||
|
if (focused)
|
||||||
|
{
|
||||||
|
pos = entry_ans->get_position();
|
||||||
|
}
|
||||||
|
|
||||||
|
// std::cout<<pos<<std::endl;
|
||||||
|
pos--;
|
||||||
|
|
||||||
|
// 3.Delete text in the position of cursor
|
||||||
|
buffer->delete_text(pos,1);
|
||||||
|
focused = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalcApp::btnanswer_clicked(){
|
||||||
|
// Get expression
|
||||||
|
Glib::ustring text = entry_ans->get_text();
|
||||||
|
//std::cout<<text<<std::endl;
|
||||||
|
|
||||||
|
// Calculation
|
||||||
|
int result = calc_expression_value(text.c_str());
|
||||||
|
char result_str[40];
|
||||||
|
sprintf(result_str,"%d",result);
|
||||||
|
entry_ans->set_text(Glib::ustring(result_str));
|
||||||
|
}
|
||||||
|
|
||||||
|
CalcApp *CalcApp::create()
|
||||||
|
{
|
||||||
|
// Create a new window
|
||||||
|
CalcApp *window = nullptr;
|
||||||
|
auto builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/window.ui");
|
||||||
|
builder->get_widget_derived("window", window);
|
||||||
|
|
||||||
|
return window;
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include "calc.hh"
|
||||||
|
|
||||||
|
static int pos = 0;
|
||||||
|
|
||||||
|
|
||||||
|
int calc_factor_value(const char * factor){
|
||||||
|
int result = 0;
|
||||||
|
char c = factor[pos];
|
||||||
|
if( c == '('){ // if expression has '(', calculate the expression in the '()'
|
||||||
|
pos++;
|
||||||
|
result = calc_expression_value(factor);
|
||||||
|
pos++;
|
||||||
|
}else{
|
||||||
|
while(isdigit(c)){ // Get the number to calculate
|
||||||
|
result = 10 * result + c - '0';
|
||||||
|
pos++;
|
||||||
|
c = factor[pos];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int calc_term_value(const char * term){
|
||||||
|
int result = calc_factor_value(term); // Get the first number
|
||||||
|
while(true){
|
||||||
|
//pos++; // Calc the multiplication and divide
|
||||||
|
char op = term[pos];
|
||||||
|
if(op == '*' || op == '/'){
|
||||||
|
pos++;
|
||||||
|
int value = calc_factor_value(term); // Get another value
|
||||||
|
if(op == '*') result *= value;
|
||||||
|
else result /= value;
|
||||||
|
}else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int calc_expression_value(const char * expression){
|
||||||
|
int result = calc_term_value(expression); // Calc the result of expression
|
||||||
|
//bool more = true;
|
||||||
|
while(true){
|
||||||
|
char op = expression[pos];
|
||||||
|
if(op == '+' || op == '-'){ // Calculate for add and subb
|
||||||
|
pos++;
|
||||||
|
int value = calc_term_value(expression);
|
||||||
|
if( op == '+') result += value;
|
||||||
|
else result -= value;
|
||||||
|
}else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
int calc_expression_value(const char * expression);
|
Loading…
Reference in New Issue