📄 calculator.c
字号:
#define CALC_SYM_ZERO (UI_character_type)'0'
#define CALC_SYM_NULL (UI_character_type)'\0'
#define CALC_RESERVED_SPACE_FOR_OPERATOR 10
/*
* Typedef
*/
typedef struct
{
S32 x;
S32 y;
PU8 image;
} calc_img_struct;
typedef struct
{
DOUBLE Operand1;
DOUBLE Operand2;
#ifndef __MMI_SLIM_CALCULATOR__
DOUBLE MemValue;
#endif
calc_img_struct OperatorImg[CALC_NUM_BUTTONS];
UI_character_type Operand1Buf[CALC_MAX_DIGITS + 2];
UI_character_type Operand2Buf[CALC_MAX_DIGITS + 2];
U8 Operand1Len;
U8 Operand2Len;
U8 Operator;
U8 CurrHilite;
U8 ResultState;
U8 RSKState;
U8 ExecFlag; /* if just after execution */
U8 OpToExecute; /* operator to execute */
U8 IsRSKPress;
U8 ComputeType;
} calc_context_struct;
typedef enum
{
CALC_OP_PLUS = 0,
CALC_OP_MINUS,
CALC_OP_MULT,
CALC_OP_DIV,
CALC_OP_EQUAL,
#ifndef __MMI_SLIM_CALCULATOR__
CALC_OP_MP,
CALC_OP_MM,
CALC_OP_MC,
CALC_OP_MR,
#endif /* __MMI_SLIM_CALCULATOR__ */
CALC_OP_NONE
} CALC_OPER_ENUM;
#ifdef __MMI_TOUCH_CALCULATOR__
typedef enum
{
TOUCH_CALC_KEY0 = 0,
TOUCH_CALC_KEY1,
TOUCH_CALC_KEY2,
TOUCH_CALC_KEY3,
TOUCH_CALC_KEY4,
TOUCH_CALC_KEY5,
TOUCH_CALC_KEY6,
TOUCH_CALC_KEY7,
TOUCH_CALC_KEY8,
TOUCH_CALC_KEY9,
TOUCH_CALC_KEY_DOT = 20 /* pound key or hash key */
} TOUCH_CALC_NUM_KEY_ENUM;
typedef enum
{
TOUCH_CALC_OP_PLUS = CALC_OP_PLUS,
TOUCH_CALC_OP_MINUS = CALC_OP_MINUS,
TOUCH_CALC_OP_MULT = CALC_OP_MULT,
TOUCH_CALC_OP_DIV = CALC_OP_DIV,
TOUCH_CALC_OP_MP = CALC_OP_MP,
TOUCH_CALC_OP_MM = CALC_OP_MM,
TOUCH_CALC_OP_MC = CALC_OP_MC,
TOUCH_CALC_OP_MR = CALC_OP_MR,
TOUCH_CALC_OP_EQUAL = CALC_OP_EQUAL
} TOUCH_CALC_SYM_KEY_ENUM;
typedef enum
{
TOUCH_CALC_PEN_NONE,
TOUCH_CALC_PEN_LSK,
TOUCH_CALC_PEN_RSK,
TOUCH_CALC_PEN_NUM_KEY
} touch_calc_pen_state_enum;
const S32 TouchCalcKeys[CALC_TOUCH_NROWS][CALC_TOUCH_NCOLUMNS] =
{
{TOUCH_CALC_OP_MP, TOUCH_CALC_OP_MM, TOUCH_CALC_OP_MR, TOUCH_CALC_OP_MC},
{TOUCH_CALC_KEY7, TOUCH_CALC_KEY8, TOUCH_CALC_KEY9, TOUCH_CALC_OP_DIV},
{TOUCH_CALC_KEY4, TOUCH_CALC_KEY5, TOUCH_CALC_KEY6, TOUCH_CALC_OP_MULT},
{TOUCH_CALC_KEY1, TOUCH_CALC_KEY2, TOUCH_CALC_KEY3, TOUCH_CALC_OP_MINUS},
{TOUCH_CALC_KEY0, TOUCH_CALC_KEY_DOT, TOUCH_CALC_OP_EQUAL, TOUCH_CALC_OP_PLUS}
};
const MMI_ID_TYPE calculator_key_image[] =
{
IMG_ID_TOUCH_CALC_MP,
IMG_ID_TOUCH_CALC_MM,
IMG_ID_TOUCH_CALC_MR,
IMG_ID_TOUCH_CALC_MC,
IMG_ID_TOUCH_CALC_KEY7,
IMG_ID_TOUCH_CALC_KEY8,
IMG_ID_TOUCH_CALC_KEY9,
IMG_ID_TOUCH_CALC_DIVIDE,
IMG_ID_TOUCH_CALC_KEY4,
IMG_ID_TOUCH_CALC_KEY5,
IMG_ID_TOUCH_CALC_KEY6,
IMG_ID_TOUCH_CALC_MULTIPLY,
IMG_ID_TOUCH_CALC_KEY1,
IMG_ID_TOUCH_CALC_KEY2,
IMG_ID_TOUCH_CALC_KEY3,
IMG_ID_TOUCH_CALC_MINUS,
IMG_ID_TOUCH_CALC_KEY0,
IMG_ID_TOUCH_CALC_KEY_DOT,
IMG_ID_TOUCH_CALC_KEY_EQUAL,
IMG_ID_TOUCH_CALC_PLUS
};
#endif /* __MMI_TOUCH_CALCULATOR__ */
/*
* Local Variable
*/
const color gCalcResultScrColor = {255, 234, 182, 100};
const color gCalcResultBorderColor = {255, 186, 64, 100};
const color gCalcTextColor = {0, 0, 0, 100};
#ifdef __MMI_TOUCH_CALCULATOR__
/* start vijay 20050305 */
const color gTouchCalcResultScrColor = {209, 232, 254, 100};
/* end vijay */
#endif /* __MMI_TOUCH_CALCULATOR__ */
extern const U8 gCalcImgCoor[][2];
extern U8 matrix_main_menu_highlight_bitmap_data[]; /* 041205 Calvin added image cache buffer for calculator */
/*
* Local Function
*/
#ifdef __MMI_TOUCH_CALCULATOR__
void mmi_touchcalc_pen_down_hdlr(mmi_pen_point_struct point);
void mmi_touchcalc_pen_up_hdlr(mmi_pen_point_struct point);
void mmi_touchcalc_pen_move_hdlr(mmi_pen_point_struct point);
void mmi_touchcalc_pen_repeat_hdlr(mmi_pen_point_struct point);
void mmi_touchcalc_pen_long_tap_hdlr(mmi_pen_point_struct point);
#endif /* __MMI_TOUCH_CALCULATOR__ */
U8 mmi_calc_del_scr_callback(void *p);
/*
* Global Variable
*/
calc_context_struct *g_calc_cntx = NULL;
#ifdef __MMI_TOUCH_CALCULATOR__
calculator_keypad_struct calculator_keypad;
touch_calc_pen_state_enum calc_pen_on_object = TOUCH_CALC_PEN_NONE;
#define CALC_PEN_DOWN_ON_KEYPAD 0x0001
BOOL TOUCH_CALC_SELECT_KEY = TRUE;
#endif /* __MMI_TOUCH_CALCULATOR__ */
/*
* Global Function
*/
/* Button co-ordinates for the on screen calculator keypad */
#endif /* __MMI_CALCULATOR__ */
#define CALC_UTILITY
/*****************************************************************************
* FUNCTION
* CalcDouble2String
* DESCRIPTION
*
* PARAMETERS
* num [IN]
* result [?]
* digits [IN]
* RETURNS
* void
*****************************************************************************/
void CalcDouble2String(DOUBLE num, S8 *result, U8 digits)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 precision;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
precision = CalcComputePrecision(num, digits);
gui_float_string(num, precision, result);
}
/*****************************************************************************
* FUNCTION
* gui_atof
* DESCRIPTION
* Convert string to float
* PARAMETERS
* s [IN] String to be converted
* RETURNS
* float value of the string
*****************************************************************************/
DOUBLE gui_atof(UI_string_type s)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S8 ansii_str[CALC_MAX_BUFF_LEN];
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
UnicodeToAnsii((S8*) ansii_str, (S8*) s);
return atof(ansii_str);
}
/*****************************************************************************
* FUNCTION
* gui_float_string
* DESCRIPTION
* Convert float to string
* PARAMETERS
* dob_val [IN] Value to be converted.
* prec_val [IN] Number of precision.
* out_buffer [IN/OUT] Pointer to the result string
* RETURNS
* void
*****************************************************************************/
void gui_float_string(DOUBLE dob_val, S32 prec_val, S8 *out_buffer)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S8 ansii_str[CALC_MAX_BUFF_LEN];
S8 fmt[8];
U8 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
memset(ansii_str, 0, CALC_MAX_BUFF_LEN);
memset(fmt, 0, 8);
sprintf(fmt, "%%.%df", prec_val);
sprintf(ansii_str, fmt, dob_val);
/* to prevent 123.250000 */
if (prec_val > 0)
{
for (i = CALC_MAX_BUFF_LEN - 1; i != 0; i--)
{
if (ansii_str[i] == 0)
{
continue;
}
else if (ansii_str[i] == '0')
{
ansii_str[i] = 0;
}
else
{
break;
}
}
}
/* to prevent -0 */
if (strlen(ansii_str) == 2 && ansii_str[0] == '-' && ansii_str[1] == '0')
{
ansii_str[0] = '0';
ansii_str[1] = 0;
}
AnsiiToUnicodeString(out_buffer, ansii_str);
}
/*****************************************************************************
* FUNCTION
* CalcComputePrecision
* DESCRIPTION
* To retrive the length of precision part of a given number.
* PARAMETERS
* result [IN] The number to be computed.
* max_digits [IN] Maximum digits of the number
* RETURNS
* void
*****************************************************************************/
S32 CalcComputePrecision(DOUBLE result, S16 max_digits)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
S32 prec_val = 0;
U8 str[CALC_MAX_BUFF_LEN];
S8 fmt[8];
S8 decimal_length = 0;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (result < 0)
{
result = -(result);
}
/*
* str[] = x.xxxxxxxxxxxe+/-xxx
* ex: 0.0000000001 = 1.00000000000e-010
* 646464646464 = 6.46464646464e+011
*/
memset(fmt, 0, 8);
sprintf(fmt, "%%%d.%de", max_digits, (max_digits - 2));
memset(str, '\0', CALC_MAX_BUFF_LEN);
sprintf((S8*) str, fmt, result);
if (str[max_digits] == 'e')
{
S32 j;
memset(fmt, 0, 8);
memcpy(fmt, str + (max_digits + 2), 3);
prec_val = atoi(fmt);
/* Out of boundary */
if (prec_val >= max_digits - 1)
{
return 0;
}
if (str[max_digits + 1] == '-')
{
j = (max_digits - 1) - prec_val;
}
else
{
j = max_digits - 1;
}
for (; j > 1; j--)
{
if (str[j] != '0')
{
decimal_length = (j - 1);
break;
}
}
/* |result| < 1 */
if (str[max_digits + 1] == '-')
{
prec_val += decimal_length;
}
/* |result| >= 1 */
else if (str[max_digits + 1] == '+')
{
if (decimal_length > prec_val)
{
prec_val = decimal_length - prec_val;
}
else
{
prec_val = 0;
}
}
}
return prec_val;
}
#ifdef __MMI_CALCULATOR__
#define CALC_INITIALIZE
/*****************************************************************************
* FUNCTION
* HighlightCalcMenu
* DESCRIPTION
* Highlight handler of Calculator menu item.
* Register key handlers.
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
void HighlightCalcMenu(void)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
SetLeftSoftkeyFunction(CalcPreEntryApp, KEY_EVENT_UP);
SetKeyHandler(CalcPreEntryApp, KEY_RIGHT_ARROW, KEY_EVENT_DOWN);
SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -