1 #include "demo_guix_smart_watch.h"
2 
3 #define MSG_COUNT 4
4 
5 /* Extern message template definition.  */
6 extern GX_STUDIO_WIDGET message_template_define;
7 
8 /* Define message information structure.  */
9 typedef struct MESSAGE_INFO_STRUCT {
10     GX_RESOURCE_ID                     day;
11     INT                                hour;
12     INT                                minute;
13     GX_RESOURCE_ID                     am_pm;
14     GX_STRING                          msg;
15 }MSG_INFO;
16 
17 /* Define message widgets.  */
18 MESSAGE_TEMPLATE_CONTROL_BLOCK msg_widget_list[MSG_COUNT];
19 
20 /* Define message content.  */
21 static GX_CONST CHAR msg_1[] = "Missed Call\r (414) 785-7858";
22 static GX_CONST CHAR msg_2[] = "Hi Alex! We haven't seen you in a while. Open your BistroFast App for 1 $10 off $20+ reward! Don't wait, it expires in 5 days!";
23 static GX_CONST CHAR msg_3[] = "Missed Call\r (414) 785-7858";
24 static GX_CONST CHAR msg_4[] = "Hi Alex,\r We'd like to let you know that an update to your item 2UiKit is now availlable in your Downloads pages.";
25 
26 /* Define message information.  */
27 MSG_INFO msg_info_list[MSG_COUNT] = {
28     {GX_STRING_ID_TODAY, 7, 52, GX_STRING_ID_AM, {msg_1, sizeof(msg_1) - 1}},
29     {GX_STRING_ID_YESTERDAY, 4, 37, GX_STRING_ID_PM, {msg_2, sizeof(msg_2) - 1}},
30     {GX_STRING_ID_YESTERDAY, 5, 11, GX_STRING_ID_PM, {msg_3, sizeof(msg_3) - 1}},
31     {GX_STRING_ID_YESTERDAY, 5, 40, GX_STRING_ID_PM, {msg_4, sizeof(msg_4) - 1}},
32 };
33 
34 /******************************************************************************************/
35 /* Position message frame window children.                                                */
36 /******************************************************************************************/
position_children(GX_WIDGET * parent)37 static VOID position_children(GX_WIDGET *parent)
38 {
39     GX_WIDGET *child;
40     INT left = parent->gx_widget_size.gx_rectangle_left;
41     INT top = parent->gx_widget_size.gx_rectangle_top;
42 
43     child = parent->gx_widget_first_child;
44     while (child)
45     {
46         if (!(child->gx_widget_status & GX_STATUS_NONCLIENT))
47         {
48 
49             gx_widget_shift(child, left - child->gx_widget_size.gx_rectangle_left, top - child->gx_widget_size.gx_rectangle_top, GX_FALSE);
50             top = child->gx_widget_size.gx_rectangle_bottom + 1;
51         }
52 
53         child = child->gx_widget_next;
54     }
55 }
56 
57 /******************************************************************************************/
58 /* Calculate the height of the specified text.                                            */
59 /******************************************************************************************/
calculate_text_height(GX_STRING * string,GX_VALUE client_width,GX_RESOURCE_ID font_id)60 static INT calculate_text_height(GX_STRING *string, GX_VALUE client_width, GX_RESOURCE_ID font_id)
61 {
62     GX_FONT  *font;
63     GX_VALUE  text_width = 0;
64     GX_VALUE  text_height = 0;
65     GX_STRING tmp;
66     GX_CHAR   tmp_buffer[2];
67     GX_VALUE  tmp_width;
68     UINT      index;
69 
70     gx_widget_font_get(&main_screen, font_id, &font);
71 
72     if (!font)
73     {
74         return 0;
75     }
76 
77     tmp.gx_string_ptr = tmp_buffer;
78     tmp.gx_string_length = 1;
79     tmp_buffer[1] = '\0';
80 
81     for (index = 0; index < string->gx_string_length; index++)
82     {
83         tmp_buffer[0] = string->gx_string_ptr[index];
84 
85         gx_system_string_width_get_ext(font, &tmp, &tmp_width);
86         text_width += tmp_width;
87 
88         if ((tmp_buffer[0] == '\r') || (index == string->gx_string_length - 1))
89         {
90             text_height += font->gx_font_line_height * (1 + text_width / client_width);
91             text_width = 0;
92         }
93     }
94 
95     return text_height;
96 }
97 
98 /******************************************************************************************/
99 /* Update message list.                                                                   */
100 /******************************************************************************************/
screen_meesage_update()101 static VOID screen_meesage_update()
102 {
103     INT index;
104     GX_WIDGET *parent = (GX_WIDGET *)&message_screen.message_screen_message_frame;
105     MESSAGE_TEMPLATE_CONTROL_BLOCK *msg_widget;
106     GX_MULTI_LINE_TEXT_VIEW *view;
107     MSG_INFO *info;
108     GX_RECTANGLE size;
109     INT text_height;
110     GX_VALUE width;
111     GX_BOOL created;
112 
113     for (index = 0; index < MSG_COUNT; index++)
114     {
115         msg_widget = &msg_widget_list[index];
116         info = &msg_info_list[index];
117 
118         /* Test if the widget has been created.  */
119         gx_widget_created_test(msg_widget, &created);
120 
121         if (!created)
122         {
123             /* Create message widget.  */
124             gx_studio_widget_create((GX_BYTE*)msg_widget, &message_template_define, GX_NULL);
125 
126             /* Attach message bar and message text view to the message frame window.  */
127             gx_widget_attach(parent, &msg_widget->message_template_message_bar);
128             gx_widget_attach(parent, &msg_widget->message_template_text_view);
129         }
130 
131         /* Set message content. */
132         gx_prompt_text_id_set(&msg_widget->message_template_day_of_week, info->day);
133         gx_numeric_prompt_value_set(&msg_widget->message_template_hour, info->hour);
134         gx_numeric_prompt_value_set(&msg_widget->message_template_minute, info->minute);
135         gx_prompt_text_id_set(&msg_widget->message_template_am_pm, info->am_pm);
136         gx_multi_line_text_view_text_set_ext(&msg_widget->message_template_text_view, &info->msg);
137 
138         /* Resize multi line text view to fit message content.  */
139         view = &msg_widget->message_template_text_view;
140         gx_widget_width_get(view, &width);
141         text_height = calculate_text_height(&info->msg, width, view->gx_multi_line_text_view_font_id);
142         size = view->gx_widget_size;
143         size.gx_rectangle_bottom = size.gx_rectangle_top + text_height + (view->gx_multi_line_text_view_whitespace << 1);
144         gx_widget_resize(view, &size);
145     }
146 
147     position_children(parent);
148 }
149 
150 /******************************************************************************************/
151 /* Override the default event processing of "message_screen_template" to handle signals   */
152 /* from my child widgets.                                                                 */
153 /******************************************************************************************/
message_screen_event_process(GX_WINDOW * window,GX_EVENT * event_ptr)154 UINT message_screen_event_process(GX_WINDOW* window, GX_EVENT* event_ptr)
155 {
156     switch (event_ptr->gx_event_type)
157     {
158     case GX_EVENT_SHOW:
159 
160         /* Update message list.  */
161         screen_meesage_update();
162         break;
163 
164     default:
165         break;
166     }
167 
168     return screen_template_event_process(window, event_ptr);
169 }