From 95e283587193f051aa07efaaa1b7ec0e1573ce3b Mon Sep 17 00:00:00 2001
From: Pim van Pelt <pim@ipng.nl>
Date: Fri, 1 Dec 2017 16:16:57 +0100
Subject: [PATCH] Draw images as blue roundrects; Add labels to non-imagery
 widgets, if they are set

---
 src/main.c           |  2 ++
 src/widget_default.c | 29 ++++++++++++++++++++++++-----
 src/widget_name.c    |  2 +-
 src/widget_time.c    |  3 ++-
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/main.c b/src/main.c
index 756b25f..3c3922f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,6 +4,8 @@
 #include "mgos.h"
 #include "mgos_pwm.h"
 #include "mongoose-touch.h"
+#include "fonts/FreeSerifBold9pt7b.h"
+#include "fonts/FreeMonoBold9pt7b.h"
 
 struct screen_t *screen = NULL;
 
diff --git a/src/widget_default.c b/src/widget_default.c
index ea79239..54470b7 100644
--- a/src/widget_default.c
+++ b/src/widget_default.c
@@ -1,18 +1,37 @@
 #include "mgos.h"
 #include "mongoose-touch.h"
 
+extern GFXfont FreeSerifBold9pt7b;
+
 static void widget_default_draw(struct widget_t *w, uint16_t color) {
   if (!w)
     return;
   mgos_ili9341_set_window(w->x, w->y, w->x+w->w-1, w->y+w->h-1);
 
   mgos_ili9341_set_fgcolor565(color);
-  mgos_ili9341_drawRoundRect(0, 0, w->w, w->h, 8);
+ 
+  if (w->img) {
+    mgos_ili9341_set_fgcolor565(ILI9341_BLUE);
+    mgos_ili9341_drawRoundRect(0, 0, w->w, w->h, 16);
+  } else if (w->label) {
+    int16_t text_width, text_height;
+    uint16_t x, y;
+
+    mgos_ili9341_drawRoundRect(0, 0, w->w, w->h, 8);
+    mgos_ili9341_set_font(&FreeSerifBold9pt7b);
+    text_width=mgos_ili9341_getStringWidth(w->label);
+    text_height=mgos_ili9341_getStringHeight(w->label);
+    if (text_width>w->w)
+      x=0;
+    else
+      x=(w->w-text_width)/2;
+    if (text_height>w->h)
+      y=0;
+    else
+      y=(w->h-text_height)/2;
+    mgos_ili9341_print(x, y, w->label);
+  }
 
-/*
-  if (w->img)
-    mgos_ili9341_png(0, 0, w->img);
-*/
 }
 
 void widget_default_ev(int ev, struct widget_t *w, void *ev_data) {
diff --git a/src/widget_name.c b/src/widget_name.c
index 30b1c8d..18aa254 100644
--- a/src/widget_name.c
+++ b/src/widget_name.c
@@ -3,7 +3,6 @@
 #include "mgos_wifi.h"
 #include "mgos_net.h"
 #include "mongoose-touch.h"
-#include "fonts/FreeMonoBold9pt7b.h"
 
 #define WIDGET_NAME_NAME    0
 #define WIDGET_NAME_IPADDR  1
@@ -13,6 +12,7 @@
 
 static uint8_t what = WIDGET_NAME_NAME;
 extern struct screen_t *screen;
+extern GFXfont FreeMonoBold9pt7b;
 
 static void widget_name_render(struct widget_t *w, void *ev_data) {
   char namestring[21];
diff --git a/src/widget_time.c b/src/widget_time.c
index 3aaeb07..db10ef1 100644
--- a/src/widget_time.c
+++ b/src/widget_time.c
@@ -1,6 +1,7 @@
 #include "mgos.h"
 #include "mongoose-touch.h"
-#include "fonts/FreeSerifBold9pt7b.h"
+
+extern GFXfont FreeSerifBold9pt7b;
 
 static void widget_time_render(struct widget_t *w, void *ev_data) {
   char tmp_buff[32];