|
Packit |
1470ea |
#!/usr/bin/gjs
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
imports.gi.versions.Gtk = '3.0';
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const GObject = imports.gi.GObject;
|
|
Packit |
1470ea |
const Gtk = imports.gi.Gtk;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class ButtonBoxExample {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itthis
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application({
|
|
Packit |
1470ea |
application_id: 'org.example.jsbuttonbox'
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect 'activate' and 'startup' signals to the callback functions
|
|
Packit |
1470ea |
this.application.connect('activate', this._onActivate.bind(this));
|
|
Packit |
1470ea |
this.application.connect('startup', this._onStartup.bind(this));
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'activate' signal presents windows when active
|
|
Packit |
1470ea |
_onActivate() {
|
|
Packit |
1470ea |
this.window.present();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'startup' signal builds the UI
|
|
Packit |
1470ea |
_onStartup() {
|
|
Packit |
1470ea |
this._buildUI();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Build the application's UI
|
|
Packit |
1470ea |
_buildUI() {
|
|
Packit |
1470ea |
// Create the application window
|
|
Packit |
1470ea |
this.window = new Gtk.ApplicationWindow ({ application: this.application,
|
|
Packit |
1470ea |
window_position: Gtk.WindowPosition.CENTER,
|
|
Packit |
1470ea |
title: "Calculator",
|
|
Packit |
1470ea |
default_width: 350,
|
|
Packit |
1470ea |
default_height: 200,
|
|
Packit |
1470ea |
border_width: 10 });
|
|
Packit |
1470ea |
this.entry = new Gtk.Entry();
|
|
Packit |
1470ea |
this.entry.set_text('0');
|
|
Packit |
1470ea |
// text aligned on the right
|
|
Packit |
1470ea |
this.entry.set_alignment(1);
|
|
Packit |
1470ea |
// the text in the entry cannot be modified by writing in it
|
|
Packit |
1470ea |
this.entry.set_can_focus(false);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// a grid
|
|
Packit |
1470ea |
this.grid = new Gtk.Grid();
|
|
Packit |
1470ea |
this.grid.set_row_spacing(5);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// to attach the entry
|
|
Packit |
1470ea |
this.grid.attach(this.entry, 0, 0, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// the labels for the buttons
|
|
Packit |
1470ea |
this.buttons = [ 7, 8, 9, '/', 4, 5, 6, '*', 1, 2, 3, '-', 'C', 0, '=', '+' ];
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// each row is a ButtonBox, attached to the grid
|
|
Packit |
1470ea |
for (let i = 0; i < 4; i++) {
|
|
Packit |
1470ea |
this.hbox = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL);
|
|
Packit |
1470ea |
this.hbox.set_spacing(5);
|
|
Packit |
1470ea |
this.grid.attach(this.hbox, 0, i + 1, 1, 1);
|
|
Packit |
1470ea |
// each ButtonBox has 4 buttons, connected to the callback function
|
|
Packit |
1470ea |
for (let j= 0; j < 4; j++) {
|
|
Packit |
1470ea |
this.button = new Gtk.Button();
|
|
Packit |
1470ea |
this.buttonLabel = (this.buttons[i * 4 + j].toString());
|
|
Packit |
1470ea |
this.button.set_label(this.buttonLabel);
|
|
Packit |
1470ea |
this.button.set_can_focus(false);
|
|
Packit |
1470ea |
this.button.connect("clicked", this._buttonClicked.bind(this));
|
|
Packit |
1470ea |
this.hbox.add(this.button);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// some variables for the calculations
|
|
Packit |
1470ea |
this.firstNumber = 0;
|
|
Packit |
1470ea |
this.secondNumber = 0;
|
|
Packit |
1470ea |
this.counter = 0;
|
|
Packit |
1470ea |
this.operation = "";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// add the grid to the window
|
|
Packit |
1470ea |
this.window.add(this.grid);
|
|
Packit |
1470ea |
this.window.show_all();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// callback function for all the buttons
|
|
Packit |
1470ea |
_buttonClicked(button) {
|
|
Packit |
1470ea |
this.button = button;
|
|
Packit |
1470ea |
// for the operations
|
|
Packit |
1470ea |
if (this.button.get_label() == '+') {
|
|
Packit |
1470ea |
this.counter += 1
|
|
Packit |
1470ea |
if (this.counter > 1)
|
|
Packit |
1470ea |
this._doOperation();
|
|
Packit |
1470ea |
this.entry.set_text('0');
|
|
Packit |
1470ea |
this.operation = "plus";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this.button.get_label() == '-') {
|
|
Packit |
1470ea |
this.counter += 1;
|
|
Packit |
1470ea |
if (this.counter > 1)
|
|
Packit |
1470ea |
this._doOperation();
|
|
Packit |
1470ea |
this.entry.set_text('0');
|
|
Packit |
1470ea |
this.operation = "minus";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this.button.get_label() == '*') {
|
|
Packit |
1470ea |
this.counter += 1;
|
|
Packit |
1470ea |
if (this.counter > 1)
|
|
Packit |
1470ea |
this._doOperation();
|
|
Packit |
1470ea |
this.entry.set_text('0');
|
|
Packit |
1470ea |
this.operation = "multiplication";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this.button.get_label() == '/') {
|
|
Packit |
1470ea |
this.counter += 1
|
|
Packit |
1470ea |
if (this.counter > 1)
|
|
Packit |
1470ea |
this._doOperation();
|
|
Packit |
1470ea |
this.entry.set_text('0');
|
|
Packit |
1470ea |
this.operation = "division";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// for =
|
|
Packit |
1470ea |
else if (this.button.get_label() == '=') {
|
|
Packit |
1470ea |
this._doOperation();
|
|
Packit |
1470ea |
this.entry.set_text(this.firstNumber.toString());
|
|
Packit |
1470ea |
this.counter = 1;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// for Cancel
|
|
Packit |
1470ea |
else if (this.button.get_label() == 'C') {
|
|
Packit |
1470ea |
this.firstNumber = 0;
|
|
Packit |
1470ea |
this.secondNumber = 0;
|
|
Packit |
1470ea |
this.counter = 0;
|
|
Packit |
1470ea |
this.entry.set_text('0');
|
|
Packit |
1470ea |
this.operation = "";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// for a digit button
|
|
Packit |
1470ea |
else {
|
|
Packit |
1470ea |
this.newDigit = parseInt(this.button.get_label());
|
|
Packit |
1470ea |
if (this.entry.get_text() == "error")
|
|
Packit |
1470ea |
this.number = 0;
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this.number = parseInt(this.entry.get_text());
|
|
Packit |
1470ea |
this.number = this.number * 10 + this.newDigit;
|
|
Packit |
1470ea |
if (this.counter == 0)
|
|
Packit |
1470ea |
this.firstNumber = this.number;
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this.secondNumber = this.number;
|
|
Packit |
1470ea |
this.entry.set_text(this.number.toString());
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_doOperation() {
|
|
Packit |
1470ea |
if (this.operation == "plus") {
|
|
Packit |
1470ea |
this.firstNumber += this.secondNumber;
|
|
Packit |
1470ea |
} else if (this.operation == "minus") {
|
|
Packit |
1470ea |
this.firstNumber -= this.secondNumber;
|
|
Packit |
1470ea |
} else if (this.operation == "multiplication") {
|
|
Packit |
1470ea |
this.firstNumber *= this.secondNumber;
|
|
Packit |
1470ea |
} else if (this.operation == "division") {
|
|
Packit |
1470ea |
if (this.secondNumber != 0) {
|
|
Packit |
1470ea |
this.firstNumber /= this.secondNumber;
|
|
Packit |
1470ea |
} else {
|
|
Packit |
1470ea |
this.firstNumber = 0;
|
|
Packit |
1470ea |
this.secondNumber = 0;
|
|
Packit |
1470ea |
this.counter = 0;
|
|
Packit |
1470ea |
this.entry.set_text("error");
|
|
Packit |
1470ea |
this.operation = "";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
} else {
|
|
Packit |
1470ea |
this.firstNumber = 0;
|
|
Packit |
1470ea |
this.secondNumber = 0;
|
|
Packit |
1470ea |
this.counter = 0;
|
|
Packit |
1470ea |
this.entry.set_text("error");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new ButtonBoxExample();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|