contrib/header_static: a cog in the toolchains to generate ObjCwrapper
[nit.git] / examples / calculator / src / calculator_gtk.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013-2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # GTK calculator
18 module calculator_gtk
19
20 import calculator_logic
21
22 import gtk
23
24 # GTK calculator UI
25 class CalculatorGui
26 super GtkCallable
27
28 private var win: GtkWindow is noinit
29 private var container: GtkGrid is noinit
30
31 private var lbl_disp: GtkLabel is noinit
32 private var but_eq: GtkButton is noinit
33 private var but_dot: GtkButton is noinit
34
35 private var context = new CalculatorContext
36
37 redef fun signal(sender, op)
38 do
39 if op isa Char then # is an operation
40 if op == '.' then
41 but_dot.sensitive = false
42 context.switch_to_decimals
43 else
44 but_dot.sensitive = true
45 context.push_op op
46 end
47 else if op isa Int then # is a number
48 context.push_digit op
49 end
50
51 lbl_disp.text = context.display_text
52 end
53
54 init
55 do
56 init_gtk
57
58 win = new GtkWindow(new GtkWindowType.toplevel)
59 win.connect_destroy_signal_to_quit
60
61 container = new GtkGrid
62 win.add container
63
64 lbl_disp = new GtkLabel("_")
65 container.attach(lbl_disp, 0, 0, 5, 1)
66
67 # Digits
68 for n in [0..9] do
69 var but = new GtkButton.with_label(n.to_s)
70 but.request_size(64, 64)
71 but.signal_connect("clicked", self, n)
72 if n == 0 then
73 container.attach(but, 0, 4, 1, 1)
74 else container.attach(but, (n-1)%3, 3-(n-1)/3, 1, 1)
75 end
76
77 # Operators
78 var r = 1
79 for op in ['+', '-', '*', '/'] do
80 var but = new GtkButton.with_label(op.to_s)
81 but.request_size(64, 64)
82 but.signal_connect("clicked", self, op)
83 container.attach(but, 3, r, 1, 1)
84 r+=1
85 end
86
87 # =
88 but_eq = new GtkButton.with_label("=")
89 but_eq.request_size(64, 64)
90 but_eq.signal_connect("clicked", self, '=')
91 container.attach(but_eq, 4, 3, 1, 2)
92
93 # .
94 but_dot = new GtkButton.with_label(".")
95 but_dot.request_size(64, 64)
96 but_dot.signal_connect("clicked", self, '.')
97 container.attach(but_dot, 1, 4, 1, 1)
98
99 # C
100 var but_c = new GtkButton.with_label("C")
101 but_c.request_size(64, 64)
102 but_c.signal_connect("clicked", self, 'C')
103 container.attach(but_c, 2, 4, 1, 1)
104
105 win.show_all
106 end
107 end
108
109 # Do not show GUI in when testing
110 if "NIT_TESTING".environ == "true" then exit 0
111
112 var app = new CalculatorGui
113 run_gtk