blob: 5f9a0cdf1fefc7dd15039cb033a1c8f063b14b5c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/******************************************************************************
2*(C) Copyright 2014 Marvell International Ltd.
3* All Rights Reserved
4******************************************************************************/
5/*******************************************************************************
6 *
7 * Filename: Button.c
8 *
9 * Authors: Boaz Sommer
10 *
11 * Description: A Button class
12 *
13 * HISTORY:
14 *
15 *
16 *
17 * Notes:
18 *
19 ******************************************************************************/
20#include <stdarg.h>
21#include <stdlib.h>
22#include <string.h>
23#include <pthread.h>
24#include <errno.h>
25#include <signal.h>
26#include <math.h>
27#include "Image.h"
28#include "Button.h"
29#include "TextBox.h"
30#include "mgui_config.h"
31#include "mgui_utils.h"
32
33typedef struct S_BUTTON_PRIV
34{
35 void *dfb;
36 IMAGE buttonImage;
37 TEXTBOX *text;
38 OBJECT_RECT buttonRect;
39 pthread_t buttonThreadHandle;
40 long eventHandle;
41}BUTTON_PRIV;
42
43#if 0
44static void _ButtonEventHandler (int event, void *params)
45{
46 ButtonOnClickParams *onClick = (ButtonOnClickParams *)params;
47
48 MGUI_DMSG("Button is clicked\n");
49
50 if (onClick && onClick->cb)
51 onClick->cb(event, onClick->cb_data);
52
53}
54#endif
55
56void ButtonSetFromArray(BUTTON *pt, int key)
57{
58 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
59
60 if (button) {
61 ImageSetFromArray(button->buttonImage, key);
62 ImageGetGeometry(button->buttonImage, &button->buttonRect);
63 }
64}
65
66void ButtonSetArray(BUTTON *pt, const IMAGE_ARRAY_ITEM *array, int size)
67{
68 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
69
70 if (button) {
71 ImageSetArray(button->buttonImage, array, size);
72 }
73}
74
75BUTTON *ButtonInit (void *dfb)
76{
77 BUTTON_PRIV *button = (BUTTON_PRIV *)calloc(1, sizeof(*button));
78
79 if (button)
80 {
81 button->dfb = dfb;
82 GuiAddObject(button->dfb, button, (ObjectDrawFunc)ButtonDraw);
83 button->buttonImage = ImageInit(dfb);
84 }
85
86 return (BUTTON *)button;
87}
88void ButtonDeinit (BUTTON *pt)
89{
90 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
91
92 if (button)
93 {
94#if 0
95 if (button->eventHandle)
96 DirectFbUnregisterEventHandler(button->dfb, button->eventHandle);
97#endif
98 if (button->text)
99 TextBoxDeinit(button->text);
100
101 ImageDeinit(button->buttonImage);
102
103 GuiRemoveObject(button->dfb, button);
104
105
106 free (button);
107 }
108}
109
110
111void ButtonSetup2(BUTTON *pt, OBJECT_RECT rect)
112{
113 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
114 if (button) {
115 button->buttonRect = rect;
116 ImageSetup2(button->buttonImage, button->buttonRect);
117 }
118}
119
120void ButtonSetup (BUTTON *pt, int lx, int ly)
121{
122 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
123
124 if (button)
125 {
126 button->buttonRect.lx = lx;
127 button->buttonRect.ly = ly;
128 /*
129 button->buttonRect.w = w;
130 button->buttonRect.lx = h;
131 */
132 MGUI_DMSG("Button rect: (x,y,w,h) = (%d,%d,%d,%d)\n", lx, ly, button->buttonRect.w, button->buttonRect.h);
133 ImageSetup(button->buttonImage,
134 button->buttonRect.lx,
135 button->buttonRect.ly,
136 button->buttonRect.w,
137 button->buttonRect.h);
138 }
139}
140
141void ButtonDraw (BUTTON *pt)
142{
143 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
144
145 if (button)
146 {
147 //ImageDraw(button->buttonImage);
148 if (button->text)
149 TextBoxDraw(button->text);
150 }
151 else
152 MGUI_DMSG("invalid button\n");
153}
154void ButtonSetupOnClick(BUTTON *pt, ButtonOnClickParams *onClick)
155{
156#if 1
157 UNUSED(pt);
158 UNUSED(onClick);
159#else
160 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
161 unsigned long mask = EVENT_SET(DIRECTFB_EVT_PRESS) | EVENT_SET(DIRECTFB_EVT_RELEASE);
162
163 if (button)
164 {
165 button->eventHandle = DirectFbRegisterEventHandler(button->dfb,
166 &button->buttonRect,
167 mask ,
168 _ButtonEventHandler,
169 (void *)onClick);
170 }
171#endif
172}
173
174void ButtonGetGeometry (BUTTON pt, OBJECT_RECT *rect)
175{
176 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
177 if (button)
178 {
179 ImageGetGeometry(button->buttonImage, rect);
180 }
181}
182
183
184int ButtonGetWidth (BUTTON *pt)
185{
186 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
187
188 if (button)
189 return (button->buttonRect.w);
190 else
191 return 0;
192}
193
194int ButtonGetHeight (BUTTON *pt)
195{
196 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
197
198 if (button)
199 return (button->buttonRect.h);
200 else
201 return 0;
202}
203
204void ButtonSetText (BUTTON *pt, const char *text, const char *font_path)
205{
206 BUTTON_PRIV *button = (BUTTON_PRIV *)pt;
207 int height;
208
209 if (button)
210 {
211 height = button->buttonRect.h *.45;
212 button->text = TextBoxInit(button->dfb, height, -1, font_path);
213 TextBoxSetup(button->text, text, button->buttonRect.lx + button->buttonRect.w/2,
214 button->buttonRect.ly + (button->buttonRect.h * 1.5 / 2), TA_CENTER, COLOR_BLACK);
215 }
216}