/
layout.go
778 lines (664 loc) · 20.3 KB
/
layout.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
// main_layout.go
package main
import (
"fmt"
"os"
"strconv"
_ "unsafe"
gdk "github.com/mattn/go-gtk/gdk"
_ "github.com/mattn/go-gtk/glib"
gtk "github.com/mattn/go-gtk/gtk"
)
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
// Constants
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// UI constants
const (
// For window size
UI_Width = 640
UI_Height = 360
// for Positioning
No_Resize = false // No resize in Pack#()
No_Shrink = false // No shrink in Pack#()
Homogeneous = false
Heterogeneous = true
Default_Spacing = 1
)
// Radix Converting
const (
CVT_HEX = 16
CVT_DEC = 10
CVT_OCT = 8
)
// Presence of Operator : Operator Cursor
const (
EXIST = true
NONE = false
)
// Operation Codes
const (
OP_XXX = iota
OP_ADD
OP_SUB
OP_MUL
OP_DIV
OP_MOD
OP_AND
OP_OR
OP_XOR
OP_NOT
OP_LSHFT
OP_RSHFT
)
// Main function.
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func main() {
defer catch() // catch Panic
Start()
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
// Type : UI
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
type UI struct {
// GUI components
Win *gtk.Window // Main Window
Calc_Frame *gtk.Frame // Frame for Calculation
Nums_Frame *gtk.Frame // Frame for Number buttons
Oper_Frame *gtk.Frame // Frame for Operator buttons
Lbl_prev *gtk.Label // Label for previous result
Lbl_lhs *gtk.Label // Label for Left hand side value
Lbl_rhs *gtk.Label // Label for Right hand side value
Btn_map map[string]*gtk.Button // Map of Buttons
// Member for Operation
op_code int // Operation Code
radix int // Current radix
csr bool // NONE / EXIST
prev int // previous result
lhs int // operand type is int
rhs int // operand type is int
Ch_Event chan interface{} // Channel for event streaming
}
// Constructor function
// 1. Create Components
// 2. Setup Layout
// 3. Event - Callback
func (this *UI) Construct() {
defer catch()
// 1. Creating Components
// ---- ---- ---- ---- ---- ---- ---- ----
// Create the Main Window
// Set title & size
this.Win = gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
if this.Win == nil {
panic("UI::Construct() : Window allocation Failed")
}
window := this.Win // Window aliasin
window.SetTitle("0x_Calculator") // SetTitle
this.Calc_Frame = gtk.NewFrame("Calculation")
this.Nums_Frame = gtk.NewFrame("Numbers")
this.Oper_Frame = gtk.NewFrame("Operation")
this.Lbl_prev = gtk.NewLabel("(Previous)")
this.Lbl_lhs = gtk.NewLabel("(LHS)")
this.Lbl_rhs = gtk.NewLabel("(RHS)")
this.Btn_map = make(map[string]*gtk.Button)
this.Ch_Event = make(chan interface{})
this.csr = NONE // There is no operator
this.radix = CVT_DEC // Decimal radix format
this.op_code = OP_XXX // Nothing
// 2. Setup Layout
// ---- ---- ---- ---- ---- ---- ---- ----
this.init_Calc()
this.init_Nums()
this.init_Oper()
this.put_frames()
// 3. Event - Callback connection
// ---- ---- ---- ---- ---- ---- ---- ----
this.init_Events()
// 4. Left overs
// ---- ---- ---- ---- ---- ---- ---- ----
window.SetSizeRequest(UI_Width, UI_Height)
window.ShowAll()
}
// Destructor function
func (this *UI) Destruct() {
// Panic handler for Safe clean up
defer catch()
fmt.Println("UI::Destruct() : this UI is destructing...")
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
// UI : Initializers
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Frame - Calculation
// This frame contains radix(16,10,8) and result labels
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) init_Calc() {
// In this function, the designated frame is Calc_Frame
_Frame := this.Calc_Frame
if _Frame == nil {
panic("UI::init_Calc() : nil Frame received")
}
// (inner) Box of Calculation
fm_calc_box := gtk.NewHBox(false, 1)
if fm_calc_box == nil {
panic("UI::init_Calc() : HBox allocation Failed")
}
_Frame.Add(fm_calc_box)
// Box for Radix Buttons.
box_rdx := gtk.NewVBox(false, 1)
if box_rdx == nil {
panic("UI::init_Calc() : VBox allocation Failed")
}
btn_hex := button("Hex") // [Hex] : Hexadecimal
btn_dec := button("Dec") // [Dec] : Decimal
btn_oct := button("Oct") // [Oct] : Octal
box_rdx.Add(btn_hex)
box_rdx.Add(btn_dec)
box_rdx.Add(btn_oct)
// Insert radix buttons into the map
this.Btn_map["Hex"] = btn_hex
this.Btn_map["Dec"] = btn_dec
this.Btn_map["Oct"] = btn_oct
// Box for Result Labels
box_labels := gtk.NewVBox(false, 1)
if box_labels == nil {
panic("UI::init_Calc() : VBox allocation Failed")
}
// Place previous result
box_labels.Add(this.Lbl_prev)
// Place left and right operand
box_LnR := gtk.NewHBox(false, 3)
if box_LnR == nil {
panic("UI::init_Calc() : HBox allocation Failed")
}
box_LnR.Add(this.Lbl_lhs)
box_LnR.Add(this.Lbl_rhs)
box_labels.Add(box_LnR)
// Add both Boxes (Radix & Result) to frame box
fm_calc_box.Add(box_rdx)
fm_calc_box.Add(box_labels)
fmt.Println("UI::init_Calc() done.")
}
// Frame - Numbers
// This frame contains number buttons for calculation
// Hexadecimal : 0 ~ 9, A ~ F
// Decimal : 0 ~ 9
// Octal : 0 ~ 7
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) init_Nums() {
// In this function, the designated frame is Nums_Frame
_Frame := this.Nums_Frame
if _Frame == nil {
panic("UI::init_Nums() : nil Frame received")
}
// (inner) Box of Numbers
fm_nums_box := gtk.NewVBox(false, 1)
if fm_nums_box == nil {
panic("UI::init_Nums() : VBox allocation Failed")
}
// Add to given frame
_Frame.Add(fm_nums_box)
// Table Initialization
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
tbl_nums := gtk.NewTable(5, 4, false)
if tbl_nums == nil {
panic("UI::init_Nums() : Table allocation Failed")
}
// Jagged slice of buttons?
// nums := [][]*gtk.Button{}
// Button for Number
num := [...]*gtk.Button{
// 0~7 : Oct
button("0"), button("1"), button("2"), button("3"),
button("4"), button("5"), button("6"), button("7"),
// 0~9 : Dec
button("8"), button("9"),
// A~F : Hex
button("A"), button("B"), button("C"),
button("D"), button("E"), button("F"),
}
// Insert all Buttons to map
for idx, btn := range num {
//fmt.Println("UI::init_Nums() : Index :\t", idx, "Button :\t", btn.GetLabel())
s_idx := strconv.Itoa(idx)
this.Btn_map[s_idx] = btn
}
// Place buttons into the table
tbl_nums.Attach(num[0], 0, 1, 3, 4, gtk.FILL, gtk.FILL, 1, 1) // 0
tbl_nums.Attach(num[1], 0, 1, 2, 3, gtk.FILL, gtk.FILL, 1, 1) // 1
tbl_nums.Attach(num[2], 1, 2, 2, 3, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[3], 2, 3, 2, 3, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[4], 0, 1, 1, 2, gtk.FILL, gtk.FILL, 1, 1) // 4
tbl_nums.Attach(num[5], 1, 2, 1, 2, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[6], 2, 3, 1, 2, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[7], 0, 1, 0, 1, gtk.FILL, gtk.FILL, 1, 1) // 7
tbl_nums.Attach(num[8], 1, 2, 0, 1, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[9], 2, 3, 0, 1, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[10], 3, 4, 2, 3, gtk.FILL, gtk.FILL, 1, 1) // A
tbl_nums.Attach(num[11], 4, 5, 2, 3, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[12], 3, 4, 1, 2, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[13], 4, 5, 1, 2, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[14], 3, 4, 0, 1, gtk.FILL, gtk.FILL, 1, 1)
tbl_nums.Attach(num[15], 4, 5, 0, 1, gtk.FILL, gtk.FILL, 1, 1) // F
// Add the table to box
fm_nums_box.Add(tbl_nums)
fmt.Println("UI::init_Nums() done.")
}
// Frame - Operations
// This frame contains operations.
// ADD, SUB, MUL, DIV, MOD
// AND, OR, XOR, NOT
// LSHFT, RSHFT
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) init_Oper() {
// In this function, the designated frame is Oper_Frame
_Frame := this.Oper_Frame
if _Frame == nil {
panic("UI::init_Oper() : nil Frame received")
}
// (inner) Box of Operations
fm_oper_box := gtk.NewVBox(false, 1)
if fm_oper_box == nil {
panic("UI::init_Nums() : VBox allocation Failed")
}
_Frame.Add(fm_oper_box)
tbl_opers := gtk.NewTable(5, 3, false)
if tbl_opers == nil {
panic("UI::init_Nums() : Table allocation Failed")
}
// Operation Buttons
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// 'oper' is Jagged slice of buttons
oper := [][]*gtk.Button{}
// slice of Arithmetic
oper_arit := []*gtk.Button{
button("ADD"), button("SUB"), button("MUL"), button("DIV"),
button("MOD")}
// slice of Bitwise
oper_bit := []*gtk.Button{
button("AND"), button("OR"), button("XOR"), button("NOT")}
// slice of Bit Shift
oper_shft := []*gtk.Button{
button("LSHIFT"), button("RSHIFT")}
// Compose the jagged slice
oper = append(oper, oper_arit)
oper = append(oper, oper_bit)
oper = append(oper, oper_shft)
// Iterate jagged slice and place them into the table
for r, btn_slice := range oper {
// r : row
// btn_slice : slice of buttons
for c, btn := range btn_slice {
// c : column
// btn == btn_slice[c] == oper[row][col]
// Place the button to table
tbl_opers.Attach(btn, uint(c), uint(c)+1, uint(r), uint(r)+1,
gtk.FILL, gtk.FILL, 1, 1)
}
}
// "Do calculate" button!
btn_done := button("=")
tbl_opers.Attach(btn_done, uint(4), uint(4)+1, uint(2), uint(2)+1,
gtk.FILL, gtk.FILL, 1, 1)
// Insert all buttons to button map
this.Btn_map["="] = btn_done
this.Btn_map["ADD"] = oper_arit[0]
this.Btn_map["SUB"] = oper_arit[1]
this.Btn_map["MUL"] = oper_arit[2]
this.Btn_map["DIV"] = oper_arit[3]
this.Btn_map["MOD"] = oper_arit[4]
this.Btn_map["AND"] = oper_bit[0]
this.Btn_map["OR"] = oper_bit[1]
this.Btn_map["XOR"] = oper_bit[2]
this.Btn_map["NOT"] = oper_bit[3]
this.Btn_map["LSHFT"] = oper_shft[0]
this.Btn_map["RSHFT"] = oper_shft[1]
fm_oper_box.Add(tbl_opers)
fmt.Println("UI::init_Oper() done.")
}
// Locate frames on the window
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) put_frames() {
box_win := gtk.NewVBox(Homogeneous, Default_Spacing)
if box_win == nil {
panic("UI::put_frames() : VBox allocation Failed")
}
vpan1 := gtk.NewVPaned()
if vpan1 == nil {
panic("UI::put_frames() : VPaned allocation failed")
}
vpan1.Pack1(this.Calc_Frame, No_Resize, No_Shrink) // Calc : Top half
hpan1 := gtk.NewHPaned()
if hpan1 == nil {
panic("UI::put_frames() : HPaned allocation failed")
}
hpan1.Pack1(this.Nums_Frame, No_Resize, No_Shrink) // Nums : Bottom-Left
hpan1.Pack2(this.Oper_Frame, No_Resize, No_Shrink) // Oper : Bottom-Right
vpan1.Pack2(hpan1, No_Resize, No_Shrink)
box_win.Add(vpan1)
if this.Win == nil {
panic("UI::put_frames() : nil Window received")
}
// Place all Layout
this.Win.Add(box_win)
fmt.Println("UI::put_frames() done.")
}
// UI Event - Callback binding
// Window : "destroy"
// Button : "clicked"
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) init_Events() {
// on Exit -> Quit the program
this.Win.Connect("destroy", gtk.MainQuit)
// Set Listening : Keyboard
this.Win.SetEvents(int(gdk.BUTTON_PRESS_MASK))
// Map name aliasing
btn := this.Btn_map
// Format Buttons
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
btn["Hex"].Connect("clicked", func() { this.switch_radix(16) })
btn["Dec"].Connect("clicked", func() { this.switch_radix(10) })
btn["Oct"].Connect("clicked", func() { this.switch_radix(8) })
// Number Buttons
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
btn["0"].Connect("clicked", func() { this.handle_num(0) })
btn["1"].Connect("clicked", func() { this.handle_num(1) })
btn["2"].Connect("clicked", func() { this.handle_num(2) })
btn["3"].Connect("clicked", func() { this.handle_num(3) })
btn["4"].Connect("clicked", func() { this.handle_num(4) })
btn["5"].Connect("clicked", func() { this.handle_num(5) })
btn["6"].Connect("clicked", func() { this.handle_num(6) })
btn["7"].Connect("clicked", func() { this.handle_num(7) })
btn["8"].Connect("clicked", func() { this.handle_num(8) })
btn["9"].Connect("clicked", func() { this.handle_num(9) })
btn["10"].Connect("clicked", func() { this.handle_num(10) })
btn["11"].Connect("clicked", func() { this.handle_num(11) })
btn["12"].Connect("clicked", func() { this.handle_num(12) })
btn["13"].Connect("clicked", func() { this.handle_num(13) })
btn["14"].Connect("clicked", func() { this.handle_num(14) })
btn["15"].Connect("clicked", func() { this.handle_num(15) })
// Operator Buttons
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
btn["ADD"].Connect("clicked", func() { this.handle_arith(OP_ADD) })
btn["SUB"].Connect("clicked", func() { this.handle_arith(OP_SUB) })
btn["MUL"].Connect("clicked", func() { this.handle_arith(OP_MUL) })
btn["DIV"].Connect("clicked", func() { this.handle_arith(OP_DIV) })
btn["MOD"].Connect("clicked", func() { this.handle_arith(OP_MOD) })
btn["AND"].Connect("clicked", func() { this.handle_bit(OP_AND) })
btn["OR"].Connect("clicked", func() { this.handle_bit(OP_OR) })
btn["XOR"].Connect("clicked", func() { this.handle_bit(OP_XOR) })
btn["NOT"].Connect("clicked", func() { this.handle_bit(OP_NOT) })
btn["LSHFT"].Connect("clicked", func() { this.handle_shft(OP_LSHFT) })
btn["RSHFT"].Connect("clicked", func() { this.handle_shft(OP_RSHFT) })
// Do calculate Button
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
this.Btn_map["="].Connect("clicked", func() { this.calculate() })
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
// UI : Control Functions
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Switch the radix format.
// Supports only 3.
// 16(hex), 10(dec), 8(oct)
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) switch_radix(_rdx int) {
//prev := this.mode
switch _rdx {
// Change to Octal format
case 8:
this.radix = CVT_OCT
oct_pr, _ := Dec_to_OctStr(this.prev, 0)
oct_l, oct_r := Dec_to_OctStr(this.lhs, this.rhs)
this.set_prev(oct_pr)
this.set_left(oct_l)
this.set_right(oct_r)
fmt.Println("Radix : ", this.radix)
// Change to Decimal format
case 10:
this.radix = CVT_DEC
// Chance all value to decimal notation
this.set_prev(strconv.Itoa(this.prev))
this.set_left(strconv.Itoa(this.lhs))
this.set_right(strconv.Itoa(this.rhs))
fmt.Println("Radix : ", this.radix)
// Change to Hexadecimal format
case 16:
this.radix = CVT_HEX
hex_pr, _ := Dec_to_HexStr(this.prev, 0)
hex_l, hex_r := Dec_to_HexStr(this.lhs, this.rhs)
// Chance all value to decimal notation
this.set_prev(hex_pr)
this.set_left(hex_l)
this.set_right(hex_r)
fmt.Println("Radix : ", this.radix)
default:
break
}
}
// Handler for Number Buttons
// 0(0x0) ~ 15(0xF)
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) handle_num(_val int) {
if this.csr == false {
// Writing Left hand side
res := this.lhs * this.radix
res += _val
this.lhs = res
this.set_left(strconv.Itoa(this.lhs))
} else {
// Writing Right hand side
res := this.rhs * this.radix
res += _val
this.rhs = res
this.set_right(strconv.Itoa(this.rhs))
}
fmt.Println("Handle : Num : ", _val)
}
// Handler for Arithmetic operation
// ADD, SUB, MUL, DIV, MOD
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) handle_arith(_code int) {
if this.csr == NONE {
// move focus from left to right
this.csr = EXIST
// store the operation code
this.op_code = _code
} else {
// Calculate and save at left hand side
switch this.op_code {
case OP_ADD:
this.lhs = this.lhs + this.rhs
fmt.Println("Handle : ADD")
case OP_SUB:
this.lhs = this.lhs - this.rhs
fmt.Println("Handle : SUB")
case OP_MUL:
this.lhs = this.lhs * this.rhs
fmt.Println("Handle : MUL")
case OP_DIV:
this.lhs = this.lhs / this.rhs
fmt.Println("Handle : DIV")
case OP_MOD:
this.lhs = this.lhs % this.rhs
fmt.Println("Handle : MOD")
default:
return
}
// store the new operation code
this.op_code = _code
this.csr = EXIST
// reset right hand side
this.rhs = 0
// Refresh the labels
this.Lbl_prev.SetLabel(strconv.Itoa(this.prev))
this.Lbl_lhs.SetLabel(strconv.Itoa(this.lhs))
this.Lbl_rhs.SetLabel(strconv.Itoa(this.lhs))
}
}
// Handler for Bitwise operation
// AND, OR, XOR, NOT
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) handle_bit(_code int) {
if this.csr == NONE {
// move focus from left to right
this.csr = EXIST
// store the operation code
this.op_code = _code
} else {
// Calculate and save at left hand side
switch this.op_code {
case OP_AND:
this.lhs = this.lhs & this.rhs
fmt.Println("Handle : AND")
case OP_OR:
this.lhs = this.lhs | this.rhs
fmt.Println("Handle : SUB")
case OP_XOR:
this.lhs = this.lhs ^ this.rhs
fmt.Println("Handle : MUL")
case OP_NOT:
this.lhs = ^this.lhs
this.rhs = ^this.rhs
fmt.Println("Handle : DIV")
default:
return
}
// store the new operation code
this.op_code = _code
this.csr = EXIST
// reset right hand side
this.rhs = 0
// Refresh the labels
this.Lbl_prev.SetLabel(strconv.Itoa(this.prev))
this.Lbl_lhs.SetLabel(strconv.Itoa(this.lhs))
this.Lbl_rhs.SetLabel(strconv.Itoa(this.lhs))
}
}
// Handler for Shift operation
// Left Shift, Right Shift
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) handle_shft(_code int) {
switch _code {
case OP_LSHFT:
fmt.Println("Handle : L-shift")
case OP_RSHFT:
fmt.Println("Handle : R-shift")
default:
return
}
}
// Calculate the result based on current information
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func (this *UI) calculate() {
// handle the code :
// Calculate the inner value
switch this.op_code {
case OP_ADD:
this.prev = this.lhs + this.rhs
case OP_SUB:
this.prev = this.lhs - this.rhs
case OP_MUL:
this.prev = this.lhs * this.rhs
case OP_DIV:
this.prev = this.lhs / this.rhs
case OP_MOD:
this.prev = this.lhs % this.rhs
case OP_AND:
this.prev = this.lhs & this.rhs
case OP_OR:
this.prev = this.lhs | this.rhs
case OP_XOR:
this.prev = this.lhs ^ this.rhs
case OP_NOT:
this.prev = ^this.prev
this.lhs = ^this.lhs
this.rhs = ^this.rhs
default:
return
}
// Refresh the labels
this.set_prev(strconv.Itoa(this.prev))
this.set_left(strconv.Itoa(this.lhs))
this.set_right(strconv.Itoa(this.rhs))
// Reset the Operation Code and Operator Cursor
this.op_code = OP_XXX
this.csr = NONE
}
// Label Update Functions
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Change the Label of previous result
func (this *UI) set_prev(_str string) {
this.Lbl_prev.SetLabel(_str)
}
// Change the Label of left operand
func (this *UI) set_left(_str string) {
this.Lbl_lhs.SetLabel(_str)
}
// Change the Label of right operand
func (this *UI) set_right(_str string) {
this.Lbl_rhs.SetLabel(_str)
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
// Utilities
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Default Panic Handelr
func catch() {
s := recover()
if s != nil {
// print the content
fmt.Println(s)
}
}
// wrapper of gtk.NewButton() with label
func button(_lbl string) *gtk.Button {
return gtk.NewButtonWithLabel(_lbl)
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
// GUI exports
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func Start() {
defer catch() // Panic Handler
// Initialize GTK
gtk.Init(&os.Args)
// Window Setup
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
main_ui := new(UI)
main_ui.Construct()
defer main_ui.Destruct()
// Notification
fmt.Println("Starting the UI...")
// Start the Program
gtk.Main()
}
// Ready for event streaming
/*
func Setup_Events(_Window *gtk.Window) chan interface{} {
defer catch()
if _Window == nil {
panic("init_Events() : nil window received")
}
// on Exit -> Quit the program
_Window.Connect("destroy", gtk.MainQuit)
ev_chan := make(chan interface{})
_Window.Connect("key-press-event", func(ctx *glib.CallbackContext) {
arg := ctx.Args(0)
ev_chan <- *(**gdk.EventKey)(unsafe.Pointer(&arg))
})
// Set the keyboard events
_Window.SetEvents(int(gdk.BUTTON_PRESS_MASK))
return ev_chan
}
*/
/*
// Initialte evnets
event := Setup_Events(win)
go func() {
for {
e := <-event
switch ev := e.(type) {
case *gdk.EventKey:
fmt.Println("key-press-event:", ev.Keyval)
break
}
}
}()
*/