Hi,

this patch enhances the button class of LiTE in order to make things
like toggle buttons possible. Two new button states have been added to
reflect the 'disabled but on' and 'highlighted and on' state. I also
modified the 'simple' example to make use of the new features.

I don't really know whether patches to this package are appreciated at
all or if LiTE is considered dead or superseeded by Disko, respectively?
All I see is that the last commit to the git tree is 4 months old and
that there was not a single thread on this ML about it. In case
development is continued on this, there might be some more patches to
come.

Please let me know what you think.

Daniel

diff --git a/examples/Makefile.am b/examples/Makefile.am
index 855abe2..0787f79 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -41,4 +41,11 @@ EXTRA_DIST = \
 	stop.png		\
 	stop_disabled.png	\
 	stop_highlighted.png	\
-	stop_pressed.png
+	stop_pressed.png	\
+	toggle.png		\
+	toggle_disabled.png	\
+	toggle_highlighted.png	\
+	toggle_pressed.png	\
+	toggle_highlighted_on.png	\
+	toggle_disabled_on.png
+
diff --git a/examples/toggle.png b/examples/toggle.png
new file mode 100644
index 0000000..42e482e
Binary files /dev/null and b/examples/toggle.png differ
diff --git a/examples/toggle_disabled.png b/examples/toggle_disabled.png
new file mode 100644
index 0000000..c752bbf
Binary files /dev/null and b/examples/toggle_disabled.png differ
diff --git a/examples/toggle_disabled_on.png b/examples/toggle_disabled_on.png
new file mode 100644
index 0000000..de2e90d
Binary files /dev/null and b/examples/toggle_disabled_on.png differ
diff --git a/examples/toggle_highlighted.png b/examples/toggle_highlighted.png
new file mode 100644
index 0000000..a25e015
Binary files /dev/null and b/examples/toggle_highlighted.png differ
diff --git a/examples/toggle_highlighted_on.png b/examples/toggle_highlighted_on.png
new file mode 100644
index 0000000..4f48e2d
Binary files /dev/null and b/examples/toggle_highlighted_on.png differ
diff --git a/examples/toggle_pressed.png b/examples/toggle_pressed.png
new file mode 100644
index 0000000..c91af82
Binary files /dev/null and b/examples/toggle_pressed.png differ
diff --git a/lite/button.c b/lite/button.c
index 8a96a7c..482bd4e 100644
--- a/lite/button.c
+++ b/lite/button.c
@@ -42,8 +42,9 @@ struct _LiteButton {
      LiteButtonTheme  *theme;
 
      int               enabled;
+     LiteButtonType    type;
      LiteButtonState   state;
-     IDirectFBSurface *imgsurface[4];
+     IDirectFBSurface *imgsurface[6];
 
      ButtonPressFunc   press;
      void             *press_data;
@@ -92,6 +93,7 @@ lite_new_button(LiteBox         *parent,
      button->box.OnButtonUp     = on_button_up;
 
      button->enabled            = 1;
+     button->type               = LITE_BT_PUSH;
      button->state              = LITE_BS_NORMAL;
      
      *ret_button = button;
@@ -101,6 +103,16 @@ lite_new_button(LiteBox         *parent,
      return DFB_OK;
 }
 
+DFBResult
+lite_set_button_type(LiteButton *button, LiteButtonType type)
+{
+     LITE_NULL_PARAMETER_CHECK(button);
+     LITE_BOX_TYPE_PARAMETER_CHECK(LITE_BOX(button), LITE_TYPE_BUTTON);
+
+     button->type = type;
+     return DFB_OK;
+}
+
 DFBResult 
 lite_enable_button(LiteButton *button, int enabled)
 {
@@ -124,15 +136,8 @@ lite_set_button_state(LiteButton *button, LiteButtonState state)
      LITE_BOX_TYPE_PARAMETER_CHECK(LITE_BOX(button), LITE_TYPE_BUTTON);
 
      /* return if unknown state */
-     switch (state) {
-          case LITE_BS_NORMAL:
-          case LITE_BS_PRESSED:
-          case LITE_BS_HILITE:
-          case LITE_BS_DISABLED:
-               break;
-          default:
-               return DFB_INVARG;
-     }
+     if (state >= LITE_BS_MAX)
+          return DFB_INVARG;
 
      /* no need to update et rest if it's the same state, just return */
      if (button->state == state)
@@ -141,13 +146,23 @@ lite_set_button_state(LiteButton *button, LiteButtonState state)
      button->state = state;
 
      /* need to update the box in case the state changed */
-     if (button->enabled) {
+     if (button->enabled)
           lite_update_box(LITE_BOX(button), NULL);
-     }
 
      return DFB_OK;
 }
 
+DFBResult
+lite_get_button_state(LiteButton *button, LiteButtonState *state)
+{
+     LITE_NULL_PARAMETER_CHECK(button);
+     LITE_NULL_PARAMETER_CHECK(state);
+     LITE_BOX_TYPE_PARAMETER_CHECK(LITE_BOX(button), LITE_TYPE_BUTTON);
+
+     *state = button->state;
+     return DFB_OK;
+}
+
 DFBResult 
 lite_set_button_image(LiteButton *button, 
                       LiteButtonState state, 
@@ -162,15 +177,8 @@ lite_set_button_image(LiteButton *button,
                     state, filename);
 
      /* check for valid state */
-     switch (state) {
-          case LITE_BS_NORMAL:
-          case LITE_BS_PRESSED:
-          case LITE_BS_HILITE:
-          case LITE_BS_DISABLED:
-               break;
-          default:
-               return DFB_INVARG;
-     }
+     if (state >= LITE_BS_MAX)
+          return DFB_INVARG;
 
      /* load the image surface from the absolute file path */
      ret = lite_util_load_image(filename, DSPF_UNKNOWN,
@@ -198,15 +206,8 @@ lite_set_button_image_desc(LiteButton            *button,
                     state, dsc);
 
      /* check for valid button state */
-     switch (state) {
-          case LITE_BS_NORMAL:
-          case LITE_BS_PRESSED:
-          case LITE_BS_HILITE:
-          case LITE_BS_DISABLED:
-               break;
-          default:
-               return DFB_INVARG;
-     }
+     if (state >= LITE_BS_MAX)
+          return DFB_INVARG;
 
      /* load the image description directly */
      ret = lite_util_load_image_desc(&button->imgsurface[state], dsc);
@@ -227,6 +228,10 @@ lite_set_button_image_surface(LiteButton        *button,
      LITE_NULL_PARAMETER_CHECK(button);
      LITE_BOX_TYPE_PARAMETER_CHECK(LITE_BOX(button), LITE_TYPE_BUTTON);
      
+     /* check for valid button state */
+     if (state >= LITE_BS_MAX)
+          return DFB_INVARG;
+
      button->imgsurface[state] = surface;
 
      return DFB_OK;
@@ -252,6 +257,7 @@ lite_on_button_press(LiteButton     *button,
 static DFBResult 
 destroy_button(LiteBox *box)
 {
+     int i;
      LiteButton *button = NULL;
 
      D_ASSERT(box != NULL);
@@ -263,17 +269,11 @@ destroy_button(LiteBox *box)
      if (!button)
           return DFB_FAILURE;
 
-     if (button->imgsurface[LITE_BS_NORMAL])
-          button->imgsurface[LITE_BS_NORMAL]->Release(button->imgsurface[LITE_BS_NORMAL]);
-
-     if (button->imgsurface[LITE_BS_PRESSED])
-          button->imgsurface[LITE_BS_PRESSED]->Release(button->imgsurface[LITE_BS_PRESSED]);
-
-     if (button->imgsurface[LITE_BS_HILITE])
-          button->imgsurface[LITE_BS_HILITE]->Release(button->imgsurface[LITE_BS_HILITE]);
-
-     if (button->imgsurface[LITE_BS_DISABLED])
-          button->imgsurface[LITE_BS_DISABLED]->Release(button->imgsurface[LITE_BS_DISABLED]);
+     for (i = 0; i < LITE_BS_MAX; i++)
+         if (button->imgsurface[i]) {
+             button->imgsurface[i]->Release(button->imgsurface[i]);
+             button->imgsurface[i] = NULL;
+         }
 
      return lite_destroy_box(box);
 }
@@ -281,11 +281,16 @@ destroy_button(LiteBox *box)
 static int 
 on_enter(LiteBox *box, int x, int y)
 {
-     LiteButton *buttonbox = LITE_BUTTON(box);
+     LiteButton *button = LITE_BUTTON(box);
 
      D_ASSERT(box != NULL);
 
-     lite_set_button_state(buttonbox, LITE_BS_HILITE);
+     if (button->type == LITE_BT_TOGGLE && 
+         (button->state == LITE_BS_PRESSED || 
+	  button->state == LITE_BS_DISABLED_ON))
+         lite_set_button_state(button, LITE_BS_HILITE_ON);
+     else
+         lite_set_button_state(button, LITE_BS_HILITE);
 
      return 1;
 }
@@ -293,11 +298,16 @@ on_enter(LiteBox *box, int x, int y)
 static int 
 on_leave(LiteBox *box, int x, int y)
 {
-     LiteButton *buttonbox = LITE_BUTTON(box);
+     LiteButton *button = LITE_BUTTON(box);
 
      D_ASSERT(box != NULL);
 
-     lite_set_button_state(buttonbox, LITE_BS_NORMAL);
+     if (button->type == LITE_BT_TOGGLE && 
+         (button->state == LITE_BS_HILITE_ON ||
+	  button->state == LITE_BS_PRESSED))
+         lite_set_button_state(button, LITE_BS_PRESSED);
+     else
+         lite_set_button_state(button, LITE_BS_NORMAL);
 
      return 1;
 }
@@ -312,7 +322,18 @@ on_button_down(LiteBox                       *box,
      
      D_ASSERT(box != NULL);
 
-     lite_set_button_state(buttonbox, LITE_BS_PRESSED);
+     switch (buttonbox->type) {
+     case LITE_BT_PUSH:
+          lite_set_button_state(buttonbox, LITE_BS_PRESSED);
+          break;
+     case LITE_BT_TOGGLE:
+          if (buttonbox->state == LITE_BS_PRESSED ||
+	      buttonbox->state == LITE_BS_HILITE_ON)
+               lite_set_button_state(buttonbox, LITE_BS_HILITE);
+          else
+               lite_set_button_state(buttonbox, LITE_BS_PRESSED);
+	  break;
+     }
 
      return 1;
 }
@@ -330,8 +351,15 @@ on_button_up(LiteBox                       *box,
      /* do not send button selected messages unless mouse up occurs within
         the bounds of the widget */
      if (x >= 0 && x < box->rect.w && y >= 0 && y < box->rect.h) {
-          lite_set_button_state(buttonbox, LITE_BS_HILITE);
- 
+
+          switch (buttonbox->type) {
+	  case LITE_BT_PUSH:
+               lite_set_button_state(buttonbox, LITE_BS_HILITE);
+               break;
+	  case LITE_BT_TOGGLE:
+               break;
+          }
+
           if (buttonbox->enabled && buttonbox->press)
                buttonbox->press(buttonbox, buttonbox->press_data);
      }
@@ -355,6 +383,7 @@ draw_button(LiteBox         *box,
      box->surface->SetClip(box->surface, NULL);
      box->surface->SetBlittingFlags(box->surface, DSBLIT_BLEND_ALPHACHANNEL);
 
+
      if (button->enabled)
           state = button->state;
      else
diff --git a/lite/button.h b/lite/button.h
index 589dd7b..efaa799 100644
--- a/lite/button.h
+++ b/lite/button.h
@@ -54,10 +54,19 @@ typedef struct _LiteButton LiteButton;
 typedef enum { 
   LITE_BS_NORMAL,                       /**< Button is in a normal draw state */
   LITE_BS_PRESSED,                      /**< Button is in a pressed draw state */
-  LITE_BS_HILITE,                       /**< Button is in a hilite draw state */
-  LITE_BS_DISABLED                      /**< Button is in a disabled draw state */ 
+  LITE_BS_HILITE,                       /**< Button is in a hilite (and off) draw state */
+  LITE_BS_DISABLED,                     /**< Button is in a disabled (and off) draw state */
+  LITE_BS_HILITE_ON,                    /**< Button is in a hilite (and on) draw state */
+  LITE_BS_DISABLED_ON,                  /**< Button is in a disabled (and on) draw state */
+  LITE_BS_MAX
 } LiteButtonState;
 
+/* @brief ButtonType reflects the types a button can be */
+typedef enum {
+  LITE_BT_PUSH,                         /**< Push button (default) */
+  LITE_BT_TOGGLE                        /**< Toggle button */
+} LiteButtonType;
+
 /* @brief LiteButton theme */
 typedef struct _LiteButtonTheme {
      LiteTheme theme;                   /**< base LiTE theme */
@@ -125,6 +134,17 @@ DFBResult lite_new_button(LiteBox         *parent,
  */
 DFBResult lite_enable_button(LiteButton *button, int enabled);
 
+/* @brief Set the LiteButton type
+ * This function will set the LiteButton type based on the ButtonType
+ * constants.
+ * 
+ * @param button                IN:     Valid LiteButton object
+ * @param type                  IN:     LiteButton type
+ *
+ * @return DFBResult            Returns DFB_OK if successful.
+ */
+DFBResult lite_set_button_type(LiteButton *button, LiteButtonType type);
+
 /* @brief Set the LiteButton state
  * This function will set the LiteButton state based on the ButtonState
  * constants.
@@ -136,6 +156,17 @@ DFBResult lite_enable_button(LiteButton *button, int enabled);
  */
 DFBResult lite_set_button_state(LiteButton *button, LiteButtonState state);
 
+/* @brief Get the LiteButton state
+ * This function will return the LiteButton state based on the ButtonState
+ * constants.
+ *
+ * @param button                IN:     Valid LiteButton object
+ * @param state                 OUT:    LiteButton state
+ *
+ * @return DFBResult            Returns LiteButton state
+ */
+DFBResult lite_get_button_state(LiteButton *button, LiteButtonState *state);
+
 /* @brief Set the LiteButton image
  * This function will set the image of a LiteButton. 
  * The file should contain an image type supported by a

Attachment: toggle-img.tgz
Description: Unix tar archive

_______________________________________________
directfb-dev mailing list
directfb-dev@directfb.org
http://mail.directfb.org/cgi-bin/mailman/listinfo/directfb-dev

Reply via email to