2019-10-24 03:44:25 -07:00
|
|
|
|
/* Simple calculator using an Efl.Ui.Table to place the buttons
|
|
|
|
|
*/
|
2019-10-23 14:12:18 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
public class Calculator : Efl.Csharp.Application
|
|
|
|
|
{
|
2019-12-13 04:27:06 -08:00
|
|
|
|
private Efl.Ui.Textbox screen; // Text widget showing current value
|
2019-10-24 03:44:25 -07:00
|
|
|
|
private int prevValue = 0; // Value introduced before an operation (first operand)
|
|
|
|
|
private int currValue = 0; // Value currently being introduced (second operand)
|
|
|
|
|
private char operation = '='; // Last operation button pressed
|
|
|
|
|
private bool mustOverwrite = false; // Whether next number must be appended to current input
|
|
|
|
|
// or overwrite it
|
2019-10-23 14:12:18 -07:00
|
|
|
|
|
|
|
|
|
// Quits the application
|
|
|
|
|
private void GUIQuitCb(object sender, Efl.Gfx.EntityVisibilityChangedEventArgs ea)
|
|
|
|
|
{
|
2020-02-07 00:49:57 -08:00
|
|
|
|
if (ea.Arg == false)
|
2019-10-23 14:12:18 -07:00
|
|
|
|
Efl.App.AppMain.Quit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Performs "operation" on "currValue" and "prevValue" and leaves result in "currValue"
|
2019-10-23 14:12:18 -07:00
|
|
|
|
private void Operate()
|
|
|
|
|
{
|
|
|
|
|
switch (operation)
|
|
|
|
|
{
|
|
|
|
|
case '+':
|
|
|
|
|
currValue += prevValue;
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
currValue = prevValue - currValue;
|
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
currValue *= prevValue;
|
|
|
|
|
break;
|
|
|
|
|
case '/':
|
|
|
|
|
currValue = prevValue / currValue;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Called every time a button is pressed
|
2019-10-23 14:12:18 -07:00
|
|
|
|
private void ButtonPressedCb(char button)
|
|
|
|
|
{
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// If it is a number, append it to current input (or replace it)
|
2019-10-23 14:12:18 -07:00
|
|
|
|
if (button >= '0' && button <= '9')
|
|
|
|
|
{
|
|
|
|
|
if (mustOverwrite)
|
|
|
|
|
{
|
|
|
|
|
screen.SetText("");
|
|
|
|
|
mustOverwrite = false;
|
|
|
|
|
}
|
|
|
|
|
screen.SetText(screen.GetText() + button.ToString());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (button)
|
|
|
|
|
{
|
|
|
|
|
case 'C':
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Clear current input
|
2019-10-23 14:12:18 -07:00
|
|
|
|
screen.SetText("0");
|
|
|
|
|
break;
|
|
|
|
|
case '+':
|
|
|
|
|
case '-':
|
|
|
|
|
case '*':
|
|
|
|
|
case '/':
|
|
|
|
|
case '=':
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// If there was a pending operation, perform it
|
2019-10-23 14:12:18 -07:00
|
|
|
|
if (operation != '=')
|
|
|
|
|
{
|
|
|
|
|
Operate();
|
|
|
|
|
screen.SetText(currValue.ToString());
|
|
|
|
|
}
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Store this operation
|
2019-10-23 14:12:18 -07:00
|
|
|
|
operation = button;
|
|
|
|
|
mustOverwrite = true;
|
|
|
|
|
prevValue = currValue;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Called every time the content of the screen changes
|
|
|
|
|
// We use it to sanitize input (remove heading zeros, for example)
|
|
|
|
|
// This makes more sense when the Text widget is editable, since the user
|
|
|
|
|
// is free to type anything.
|
2019-10-23 14:12:18 -07:00
|
|
|
|
private void ScreenChangedCb(object sender, EventArgs ea)
|
|
|
|
|
{
|
|
|
|
|
string text = "";
|
|
|
|
|
string str = screen.GetText();
|
|
|
|
|
int d;
|
|
|
|
|
if (str == "" || str == "-")
|
|
|
|
|
{
|
|
|
|
|
text = "0";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
d = Convert.ToInt32(str);
|
|
|
|
|
text = d.ToString();
|
|
|
|
|
currValue = d;
|
|
|
|
|
}
|
|
|
|
|
catch {}
|
|
|
|
|
}
|
|
|
|
|
if (text != str) screen.SetText(text);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Creates an Efl.Ui.Button and positions it in the given position inside the table
|
|
|
|
|
// The button text is colored with "r, g, b"
|
|
|
|
|
// "text" is what is drawn on the button, which might be a multi-byte unicode string.
|
|
|
|
|
// "command" is a single-char id for the button.
|
2019-10-23 14:12:18 -07:00
|
|
|
|
private void AddButton(Efl.Ui.Table table, string text, char command, int posx, int posy, int r, int g, int b)
|
|
|
|
|
{
|
|
|
|
|
var button = new Efl.Ui.Button(table);
|
|
|
|
|
table.PackTable(button, posx, posy, 1, 1);
|
|
|
|
|
button.ClickedEvent += (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
|
|
|
|
|
ButtonPressedCb(command);
|
|
|
|
|
};
|
|
|
|
|
// Buttons can only have simple text (no font, styles or markup) but can swallow
|
|
|
|
|
// any other object we want.
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Therefore we create a more complex Efl.Ui.Text object and use it as content for the button.
|
2019-12-13 04:27:06 -08:00
|
|
|
|
var label = new Efl.Ui.Textbox(table);
|
2019-10-23 14:12:18 -07:00
|
|
|
|
label.Editable = false;
|
2019-12-17 07:34:34 -08:00
|
|
|
|
label.TextHorizontalAlign = 0.5;
|
|
|
|
|
label.TextVerticalAlign = 0.5;
|
2019-10-23 14:12:18 -07:00
|
|
|
|
label.Color = (r, g, b, 255);
|
|
|
|
|
label.SetText(text);
|
2019-12-13 04:27:06 -08:00
|
|
|
|
label.FontFamily = "Sans";
|
|
|
|
|
label.FontSize = 36;
|
2019-10-23 14:12:18 -07:00
|
|
|
|
button.Content = label;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Called on start up. We use it to create the UI.
|
2019-10-23 14:12:18 -07:00
|
|
|
|
protected override void OnInitialize(string[] args)
|
|
|
|
|
{
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// The window
|
2019-10-23 14:12:18 -07:00
|
|
|
|
var win = new Efl.Ui.Win(Efl.App.AppMain);
|
|
|
|
|
win.SetText("EFL Calculator");
|
|
|
|
|
win.Autohide = true;
|
|
|
|
|
win.VisibilityChangedEvent += GUIQuitCb;
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// The table is the main layout
|
2019-10-23 14:12:18 -07:00
|
|
|
|
var table = new Efl.Ui.Table(win);
|
|
|
|
|
win.Content = table;
|
|
|
|
|
table.TableSize = (4, 5);
|
|
|
|
|
table.HintSizeMin = new Eina.Size2D(300, 400);
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Create all buttons using the AddButton helper
|
2019-10-23 14:12:18 -07:00
|
|
|
|
AddButton(table, "1", '1', 0, 3, 255, 255, 255);
|
|
|
|
|
AddButton(table, "2", '2', 1, 3, 255, 255, 255);
|
|
|
|
|
AddButton(table, "3", '3', 2, 3, 255, 255, 255);
|
|
|
|
|
AddButton(table, "4", '4', 0, 2, 255, 255, 255);
|
|
|
|
|
AddButton(table, "5", '5', 1, 2, 255, 255, 255);
|
|
|
|
|
AddButton(table, "6", '6', 2, 2, 255, 255, 255);
|
|
|
|
|
AddButton(table, "7", '7', 0, 1, 255, 255, 255);
|
|
|
|
|
AddButton(table, "8", '8', 1, 1, 255, 255, 255);
|
|
|
|
|
AddButton(table, "9", '9', 2, 1, 255, 255, 255);
|
|
|
|
|
AddButton(table, "0", '0', 1, 4, 255, 255, 255);
|
|
|
|
|
AddButton(table, "+", '+', 3, 1, 128, 128, 128);
|
|
|
|
|
AddButton(table, "−", '-', 3, 2, 128, 128, 128);
|
|
|
|
|
AddButton(table, "×", '*', 3, 3, 128, 128, 128);
|
|
|
|
|
AddButton(table, "÷", '/', 3, 4, 128, 128, 128);
|
|
|
|
|
AddButton(table, "=", '=', 2, 4, 128, 128, 128);
|
|
|
|
|
AddButton(table, "C", 'C', 0, 4, 0, 0, 0);
|
|
|
|
|
|
2019-10-24 03:44:25 -07:00
|
|
|
|
// Create a big Efl.Ui.Text screen to display the current input
|
2019-12-13 04:27:06 -08:00
|
|
|
|
screen = new Efl.Ui.Textbox(table);
|
2019-10-23 14:12:18 -07:00
|
|
|
|
screen.SetText("0");
|
2019-12-13 04:27:06 -08:00
|
|
|
|
screen.FontFamily = "Sans";
|
|
|
|
|
screen.FontSize = 48;
|
2019-10-23 14:12:18 -07:00
|
|
|
|
screen.Multiline = false;
|
|
|
|
|
screen.Editable = false;
|
|
|
|
|
screen.SelectionAllowed = false;
|
|
|
|
|
table.PackTable(screen, 0, 0, 4, 1);
|
2019-12-17 07:34:34 -08:00
|
|
|
|
screen.TextHorizontalAlign = 0.9;
|
|
|
|
|
screen.TextVerticalAlign = 0.5;
|
2019-12-20 01:00:30 -08:00
|
|
|
|
screen.TextEffectType = Efl.TextStyleEffectType.Glow;
|
|
|
|
|
screen.TextGlowColor = (128, 128, 128, 128);
|
2019-10-23 14:12:18 -07:00
|
|
|
|
screen.ChangedEvent += ScreenChangedCb;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Example
|
|
|
|
|
{
|
|
|
|
|
#if WIN32
|
|
|
|
|
[STAThreadAttribute()]
|
|
|
|
|
#endif
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
var calculator = new Calculator();
|
|
|
|
|
calculator.Launch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|