summaryrefslogtreecommitdiff
path: root/apps/csharp/calculator/src/calculator.cs
blob: 35e426dece551eedae5e66c1932087915e9f7e53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;

public class Calculator : Efl.Csharp.Application
{
    private Efl.Ui.Text screen;
    private int prevValue = 0;
    private int currValue = 0;
    private char operation = '=';
    private bool mustOverwrite = false;

    // Quits the application
    private void GUIQuitCb(object sender, Efl.Gfx.EntityVisibilityChangedEventArgs ea)
    {
        if (ea.arg == false)
            Efl.App.AppMain.Quit(0);
    }

    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;
       }
    }

    private void ButtonPressedCb(char button)
    {
        if (button >= '0' && button <= '9')
          {
             if (mustOverwrite)
               {
                  screen.SetText("");
                  mustOverwrite = false;
               }
             screen.SetText(screen.GetText() + button.ToString());
          }
        else
          {
             switch (button)
             {
                case 'C':
                   screen.SetText("0");
                   break;
                case '+':
                case '-':
                case '*':
                case '/':
                case '=':
                   if (operation != '=')
                     {
                        Operate();
                        screen.SetText(currValue.ToString());
                     }
                   operation = button;
                   mustOverwrite = true;
                   prevValue = currValue;
                   break;
                default:
                   break;
             }
          }
    }

    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);
    }

    // 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.
    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.
       // Therefore we create a more complex Efl_Ui_Text object and use it as content for the button.
       var label = new Efl.Ui.Text(table);
       label.Editable = false;
       label.Halign = 0.5;
       label.Valign = 0.5;
       label.Color = (r, g, b, 255);
       label.SetText(text);
       label.Font = ("Sans", (Efl.Font.Size)36);
       button.Content = label;
    }

    protected override void OnInitialize(string[] args)
    {
       var win = new Efl.Ui.Win(Efl.App.AppMain);
       win.SetText("EFL Calculator");
       win.Autohide = true;
       win.VisibilityChangedEvent += GUIQuitCb;

       var table = new Efl.Ui.Table(win);
       win.Content = table;
       table.TableSize = (4, 5);
       table.HintSizeMin = new Eina.Size2D(300, 400);

       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);

       screen = new Efl.Ui.Text(table);
       screen.SetText("0");
       screen.Font = ("Sans", (Efl.Font.Size)48);
       screen.Multiline = false;
       screen.Editable = false;
       screen.SelectionAllowed = false;
       table.PackTable(screen, 0, 0, 4, 1);
       screen.Halign = 0.9;
       screen.Valign = 0.5;
       screen.EffectType = Efl.TextStyleEffectType.Glow;
       screen.GlowColor = (128, 128, 128, 128);
       screen.ChangedEvent += ScreenChangedCb;
    }
}

public class Example
{
#if WIN32
    [STAThreadAttribute()]
#endif
    public static void Main()
    {
        var calculator = new Calculator();
        calculator.Launch();
    }
}