initial import of ussd-pad
[ussd-widget] / ussd-pad / src / opt / ussd-pad / components / gui / MainWindow.py
1 from com import Component, msgs
2 from ui.Button import Button
3 from ui.Label import Label
4 from ui.layout import Arrangement
5 from ui.Window import Window
6 from theme import theme
7
8 import gtk
9
10
11 _PORTRAIT_ARRANGEMENT = """
12   <arrangement>
13     <widget name="lbl_code" x1="1%" y1="0%" x2="99%" y2="15%"/>
14     
15     <widget name="btn_1" x1="1%" y1="15%" x2="33%" y2="31%"/>
16     <widget name="btn_2" x1="34%" y1="15%" x2="66%" y2="31%"/>
17     <widget name="btn_3" x1="67%" y1="15%" x2="99%" y2="31%"/>
18
19     <widget name="btn_4" x1="1%" y1="32%" x2="33%" y2="48%"/>
20     <widget name="btn_5" x1="34%" y1="32%" x2="66%" y2="48%"/>
21     <widget name="btn_6" x1="67%" y1="32%" x2="99%" y2="48%"/>
22
23     <widget name="btn_7" x1="1%" y1="49%" x2="33%" y2="65%"/>
24     <widget name="btn_8" x1="34%" y1="49%" x2="66%" y2="65%"/>
25     <widget name="btn_9" x1="67%" y1="49%" x2="99%" y2="65%"/>
26
27     <widget name="btn_star" x1="1%" y1="66%" x2="33%" y2="82%"/>
28     <widget name="btn_0" x1="34%" y1="66%" x2="66%" y2="82%"/>
29     <widget name="btn_hash" x1="67%" y1="66%" x2="99%" y2="82%"/>
30
31     <widget name="btn_send" x1="1%" y1="83%" x2="66%" y2="99%"/>
32     <widget name="btn_back" x1="67%" y1="83%" x2="99%" y2="99%"/>
33   </arrangement>
34 """
35
36
37 class MainWindow(Component, Window):
38
39     def __init__(self):
40     
41         self.__current_code = ""
42         
43     
44         Component.__init__(self)
45         Window.__init__(self, Window.TYPE_TOPLEVEL)
46         self.set_title("USSD Pad")
47         self.set_portrait_mode(True)
48         self.connect_closed(self.__on_close_window)
49         
50         self.__arr = Arrangement()
51         self.add(self.__arr)
52         
53         self.__lbl_code = Label("", theme.font_ui_plain, theme.color_ui_text)
54         self.__arr.add(self.__lbl_code, "lbl_code")
55         
56         for lbl, name in [("1", "btn_1"),
57                           ("2", "btn_2"),
58                           ("3", "btn_3"),
59                           ("4", "btn_4"),
60                           ("5", "btn_5"),
61                           ("6", "btn_6"),
62                           ("7", "btn_7"),
63                           ("8", "btn_8"),
64                           ("9", "btn_9"),
65                           ("0", "btn_0"),
66                           ("*", "btn_star"),
67                           ("#", "btn_hash"),
68                           ("SEND", "btn_send"),
69                           ("BACK", "btn_back")]:
70             btn = Button(lbl)
71             btn.connect_clicked(self.__on_button, lbl)
72             self.__arr.add(btn, name)
73         #end for
74     
75         self.__arr.set_xml(_PORTRAIT_ARRANGEMENT)
76     
77         self.set_visible(True)
78
79
80     def set_size(self, w, h):
81     
82         old_w, old_h = self.get_size()
83         if ((w, h) != (old_w, old_h)):
84             Window.set_size(self, w, h)
85             self.__arr.set_geometry(0, 0, w, h)
86
87
88     def render_this(self):
89     
90         w, h = self.get_size()
91         screen = self.get_screen()
92         
93         screen.fill_area(0, 0, w, h, theme.color_ui_background)
94         
95         #self.__arr.set_geometry(0, 0, w, h)
96
97
98     def __on_close_window(self):
99     
100         gtk.main_quit()
101
102
103     def __on_button(self, lbl):
104     
105         if (lbl == "SEND"):
106             msg = self.call_service(msgs.USSD_SVC_SEND, self.__current_code)
107             self.__set_code("")
108             print "MESSAGE", msg
109             self.__lbl_code.set_text(msg or " ")
110
111         elif (lbl == "BACK"):
112             self.__set_code(self.__current_code[:-1])
113
114         else:
115             self.__set_code(self.__current_code + lbl)
116
117         
118     def __set_code(self, new_code):
119     
120         self.__current_code = new_code
121         self.__lbl_code.set_text(new_code or " ")
122