summaryrefslogtreecommitdiff
path: root/apps/csharp/calculator/src/calculator.cs
blob: e4711c00e0b2732046bb6b9a987e13d3254eda0e (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Simple calculator using an Efl.Ui.Table to place the buttons
*/
using System;

public class Calculator : Efl.Csharp.Application
{
    private Efl.Ui.Text screen;         // Text widget showing current value
    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

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

    // Performs "operation" on "currValue" and "prevValue" and leaves result in "currValue"
    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;
       }
    }

    // Called every time a button is pressed
    private void ButtonPressedCb(char button)
    {
        // If it is a number, append it to current input (or replace it)
        if (button >= '0' && button <= '9')
          {
             if (mustOverwrite)
               {
                  screen.SetText("");
                  mustOverwrite = false;
               }
             screen.SetText(screen.GetText() + button.ToString());
          }
        else
          {
             switch (button)
             {
                case 'C':
                   // Clear current input
                   screen.SetText("0");
                   break;
                case '+':
                case '-':
                case '*':
                case '/':
                case '=':
                   // If there was a pending operation, perform it
                   if (operation != '=')
                     {
                        Operate();
                        screen.SetText(currValue.ToString());
                     }
                   // Store this operation
                   operation = button;
                   mustOverwrite = true;
                   prevValue = currValue;
                   break;
                default:
                   break;
             }
          }
    }

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

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

    // Called on start up. We use it to create the UI.
    protected override void OnInitialize(string[] args)
    {
       // The window
       var win = new Efl.Ui.Win(Efl.App.AppMain);
       win.SetText("EFL Calculator");
       win.Autohide = true;
       win.VisibilityChangedEvent += GUIQuitCb;

       // The table is the main layout
       var table = new Efl.Ui.Table(win);
       win.Content = table;
       table.TableSize = (4, 5);
       table.HintSizeMin = new Eina.Size2D(300, 400);

       // Create all buttons using the AddButton helper
       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);

       // Create a big Efl.Ui.Text screen to display the current input
       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();
    }
}