BeRTOS
editint.c
Go to the documentation of this file.
00001 
00038 #include "editint.h"
00039 
00040 #include <cfg/macros.h>
00041 
00042 #include <dt/dwidget.h>
00043 #include <dt/dtag.h>
00044 #include <dt/dnotifier.h>
00045 
00046 #include <drv/lcd_text.h>
00047 
00051 void editint_init(DEditInt *e, dpos_t pos, dpos_t size, dcontext_t *context, int *value, int min, int max)
00052 {
00053     // Initialize superclass
00054     widget_init(&e->widget, pos, size, context);
00055 
00056     // Override superclass methods
00057     e->widget.notifier.update = (update_func_ptr)editint_update;
00058 
00059     // Init instance
00060     e->value = value;
00061     e->min = min;
00062     e->max = max;
00063     e->style = EDIS_DEFAULT;
00064     e->draw = editint_draw;
00065 }
00066 
00070 void editint_update(DEditInt *e, dtag_t tag, dval_t _val)
00071 {
00072     bool changed = false;
00073     int val = (int)_val;
00074 
00075     switch (tag)
00076     {
00077     case TAG_SETVALUE:
00078         *e->value = MINMAX(e->min, val, e->max);
00079         changed = true;
00080         break;
00081 
00082     /* Increments the integer by val */
00083     case TAG_UP:
00084         if (e->style & EDIS_WRAP)
00085         {
00086             if (*e->value + val > e->max)
00087                 *e->value = (*e->value + val - e->min) % (e->max - e->min + 1) + e->min;
00088             else
00089                 *e->value += val;
00090         }
00091         else
00092             *e->value = MIN(*e->value + val, e->max);
00093         changed = true;
00094         break;
00095     /* Decrements the integer by val */
00096     case TAG_DOWN:
00097         if (e->style & EDIS_WRAP)
00098         {
00099             if (*e->value - val < e->min)
00100                 *e->value = e->max - (e->max - (*e->value - val)) % (e->max - e->min + 1);
00101             else
00102                 *e->value -= val;
00103         }
00104         else
00105             *e->value = MAX(*e->value - val, e->min);
00106         changed = true;
00107         break;
00108 
00109     default:
00110         break;
00111     }
00112 
00113     if (changed)
00114     {
00115         e->draw(e);
00116         dnotify_targets(&e->widget.notifier, TAG_SETVALUE, (dval_t)*e->value);
00117     }
00118 }
00119 
00123 void editint_draw(DEditInt *e)
00124 {
00125     lcd_printf((Layer *)e->widget.context, (lcdpos_t)e->widget.pos, LCD_NORMAL,"%*d", (int)e->widget.size, *e->value);
00126 }