📄 mmicalculatormain.c
字号:
T_EDITOR_DATA editor_data;
#endif
char* running_total_out;
char debug_buffer[50];
TRACE_FUNCTION("calc_DialogCB()");
switch( e )
{ //when window first created
case CALC_INIT:
{ //set running total to 0 and display in editor
TRACE_EVENT("CALC-INIT-JGG");
running_total = 0;
data->menu_options_win = NULL;
/* SPR#1428 - SH - New editor: calc starts with 0 */
#ifdef NEW_EDITOR
sprintf(data->buffer, "%s", "0"); /* SH - calc starts off with '0'*/
#else
sprintf(data->buffer, "%s", ""); /* SH - calc starts off with nothing*/
#endif
calcSetEditor(win);//set editor with default attribute values
/* SPR#1428 - SH - New Editor changes: use calc editor */
#ifdef NEW_EDITOR
AUI_edit_SetEvents(&data->editor_data, 0, TRUE, FOREVER, (T_AUI_EDIT_CB)calcGetNumberCB);
data->editor_win = AUI_calc_Start(win, &data->editor_data);
#else /* NEW_EDITOR */
data->editor_data.Callback = (T_EDIT_CB)calcGetNumberCB;
data->editor_win = editor_start(win, &(data->editor_data)); /* start the editor */
#endif /* NEW_EDITOR */
winShow(data->win);
}
break;
//when an arithmetic operator has been selected from the calc menu
case CALC_ENTER_OPERAND:
{
TRACE_EVENT("CALC_ENTER_OPERAND");
/* This memory alloc seems to fix a problem where running_total_out
* would become an empty string after sevaral running calculations */
running_total_out = (char*)ALLOC_MEMORY(22*sizeof(char)); /* SH - change malloc to ALLOC_MEMORY */
running_total_out[0] = '\0';
/* Display the first operand and the operator symbol
* above the operand to be entered */
if ( (long)calcGetRunningTotal() == calcGetRunningTotal())
sprintf((char*)running_total_out, "%d", (long)calcGetRunningTotal());
else
sprintf((char*)running_total_out, "%f", calcGetRunningTotal());
if (running_total_out[8] == '.')
running_total_out[8] = '\0';
else
running_total_out[9] = '\0';
sprintf((char*)display_buffer,"%s %c", running_total_out, operatorSymbol(data->operation));
FREE_MEMORY((void*)running_total_out, 22*sizeof(char)); /* SH - change free to FREE_MEMORY */
/* SPR#1428 - SH - Calc now starts off with '0' */
#ifdef NEW_EDITOR
sprintf(data->buffer, "%s", "0"); /* SH - calc starts off with '0'*/
#else
sprintf(data->buffer, "%s", ""); /* SH - calc starts off with nothing*/
#endif
calcSetEditor(win);
/* SPR#1428 - SH - New Editor changes */
#ifdef NEW_EDITOR
AUI_edit_SetEvents(&data->editor_data, 0, TRUE, FOREVER, (T_AUI_EDIT_CB)calcGetOperandCB);
AUI_edit_SetTextStr(&data->editor_data, TxtSoftOptions, TxtDelete, TxtCalculator, (UBYTE *)display_buffer);
data->input_number_win = AUI_calc_Start(win, &data->editor_data);
#else /* NEW_EDITOR */
data->editor_data.TextString = display_buffer;
data->editor_data.editor_attr.win.px = 0;
data->editor_data.editor_attr.win.py = Mmi_layout_line(3); //30
data->editor_data.editor_attr.win.sx = SCREEN_SIZE_X;
data->editor_data.editor_attr.win.sy = numberLineHeight()+2;
data->editor_data.Callback = (T_EDIT_CB)calcGetOperandCB;
data->input_number_win = editor_start(win, &(data->editor_data)); /* start the editor */
#endif /* NEW_EDITOR */
winShow(data->win);
/* Marcus: Issue 1039: 05/09/2002: Start */
if (data->menu_options_win != NULL)
{
bookMenuDestroy( data->menu_options_win );
data->menu_options_win = NULL;
}
/* Marcus: Issue 1039: 05/09/2002: End */
}
break;
//when "Equals" selected from calc menu
case CALC_DISPLAY_RESULT:
{
//if running total out of display range, set it to 0
if ((calcGetRunningTotal() > MAX_CALC_TOTAL) || (calcGetRunningTotal() < MIN_CALC_TOTAL))
running_total =0;
//Convert running total double to string
//if integer total, don't display any decimal places
if ( (long)calcGetRunningTotal() == calcGetRunningTotal())
sprintf(data->buffer, "%d", (long) calcGetRunningTotal());
else //if a floating-point total, display as many decimal places as will fit
sprintf(data->buffer, "%f", (double) calcGetRunningTotal());
//if last number in display is a decimal point
if (data->buffer[8] == '.')
data->buffer[8] = '\0';//remove it
data->buffer[9] = '\0'; //ensure string is properly terminated
//ensure string is no longer than 9 chars
calcSetEditor(win);
/* SPR#1428 - SH - New Editor changes */
#ifdef NEW_EDITOR
AUI_edit_SetEvents(&data->editor_data, 0, TRUE, FOREVER, (T_AUI_EDIT_CB)calcGetNumberCB);
AUI_calc_Start(win, &data->editor_data);
#else /* NEW_EDITOR */
data->editor_data.Callback = (T_EDIT_CB)calcGetNumberCB;
editor_start(win, &(data->editor_data)); /* start the editor */
#endif /* NEW_EDITOR */
/* Marcus: Issue 1039: 05/09/2002: Start */
if (data->menu_options_win != NULL)
{
bookMenuDestroy( data->menu_options_win );
data->menu_options_win = NULL;
}
/* Marcus: Issue 1039: 05/09/2002: End */
winShow(data->win);
}
break;
default:
{
TRACE_EVENT("calc_DialogCB(): Unknown Event");
}
break;
}
}
/*******************************************************************************
$Function: calcGetNumberCB
$Description: Callback function for the editor window
$Returns: nothing
$Arguments: Window, identifier (not used), reason (not used)
*******************************************************************************/
static void calcGetNumberCB( T_MFW_HND win, USHORT Identifier, SHORT reason)
{
T_MFW_WIN *win_data = ( (T_MFW_HDR *) win )->data;
tCalcData *data = (tCalcData *) win_data->user;
float after_decimal = 0;
int digits_after_point = 0;
int i;
char* end;
char debug[40];
TRACE_FUNCTION("calcGetNumberCB()");
switch (reason )
{
case INFO_KCD_LEFT:
{ TRACE_EVENT("Left button pressed in calculator");
//get the number entered before the decimal point
running_total = strtol(data->buffer, &end, 10);
if (strlen(end) != 0)
{ //if decimal point entered
if ((end[0] == '.') && isdigit(end[1]))//get number after decimal point
{
end++;
digits_after_point = strlen(end);
after_decimal = strtol(end, &end, 10);
}
//convert number after decimal point to it's actual fractional value
for (i=0; i < digits_after_point; i++)
after_decimal = after_decimal/10;
//add whole number and fraction together
running_total = running_total + after_decimal;
}
if (data->menu_options_win != NULL)
bookMenuDestroy( data->menu_options_win );
//start the calculator option menu and kill this window
data->menu_options_win = bookMenuStart( data->win, calcOptionMenuAttributes(),0);
SEND_EVENT(data->menu_options_win, ADD_CALLBACK, NULL, (void *)calc_menu_cb);
}
break;
case INFO_KCD_HUP:
case INFO_KCD_RIGHT:
{
TRACE_EVENT("Right button pressed in calculator");
calc_destroy(win);
}
break;
default:
{
/* otherwise no action to be performed
*/
/*calc_destroy(win); */ /*SH - do not destroy calc for other key */
break;
}
}
}
/*******************************************************************************
$Function: calcGetOperandCB
$Description: Callback function for the editor window, when second number in operation
is entered.
$Returns: nothing
$Arguments: Window, identifier (not used), reason (not used)
*******************************************************************************/
static void calcGetOperandCB( T_MFW_HND win, USHORT Identifier, SHORT reason)
{
T_MFW_WIN *win_data = ( (T_MFW_HDR *) win )->data;
tCalcData *data = (tCalcData *) win_data->user;
char* buffer;
float operand = 0;
float after_decimal = 0;
int digits_after_point = 0;
int i;
char* end;
char debug[40];
TRACE_FUNCTION("calcGetOperandCB()");
TRACE_EVENT_P2("ID, reason: %d, %d", Identifier, reason);
switch (reason )
{
case INFO_KCD_LEFT:
/* SPR#1428 - SH - New Editor: Can use '*' or centre button as 'equals', to show result */
#ifdef NEW_EDITOR
#ifdef COLOURDISPLAY
case INFO_KCD_SELECT:
#endif /* COLOURDISPLAY */
case INFO_KCD_STAR:
#endif /* NEW_EDITOR */
{ //Get whole number before decimal point
operand = strtol(data->buffer, &end, 10);
//if buffer not pointing at an empty string now
if (strlen(end) != 0)
{ //if decimal point entered
if ((end[0] == '.') && isdigit(end[1]))//get number after decimal point
{
end++;
digits_after_point = strlen(end);
after_decimal = strtol(end, &end, 10);
sprintf(debug,"Digits after decimal: %f.", after_decimal);
}
//convert number after decimal point to an appropriate fraction
for (i=0; i < digits_after_point; i++)
after_decimal = after_decimal/10;
//add whole and fraction together
operand = operand + after_decimal;
}
//perform the arithmetic function requested
switch (data->operation)
{
case PLUS: running_total = running_total + operand; break;
case MINUS: running_total = running_total - operand;break;
case MULTIPLY: running_total = running_total * operand;break;
case DIVIDE: running_total = running_total / operand;break;
default: TRACE_EVENT("Unknown calc operation"); break;
}
/* SPR#1428 - SH - New Editor: Can use '*' as 'equals', to show result */
#ifdef NEW_EDITOR
#ifdef COLOURDISPLAY
if (reason==INFO_KCD_STAR || reason==INFO_KCD_SELECT)
#else /* COLOURDISPLAY */
if (reason==INFO_KCD_STAR)
#endif /* COLOURDISPLAY */
{
TRACE_EVENT("Star key - show result");
/* Star didn't destroy window automatically, do it here */
if (data->menu_options_win != NULL)
{
bookMenuDestroy( data->menu_options_win );
data->menu_options_win = NULL;
}
if (data->input_number_win)
{
AUI_calc_Destroy(data->input_number_win);
data->input_number_win = NULL;
}
SEND_EVENT(data->win, CALC_DISPLAY_RESULT, 0, 0);
}
else
#endif /* NEW_EDITOR */
{
//Show option menu
if (data->menu_options_win != NULL)
bookMenuDestroy( data->menu_options_win );
data->menu_options_win = bookMenuStart( data->win, calcOptionMenuAttributes(),0);
}
}
break;
case INFO_KCD_HUP:
case INFO_KCD_RIGHT:
{
TRACE_EVENT("Right button pressed in calculator");
calc_destroy(win);
}
break;
default:
{
/* otherwise no action to be performed
*/
break;
}
}
}
static void calc_menu_cb(T_MFW_HND parent_win, UBYTE identifier, UBYTE reason)
{
T_MFW_WIN *win_data = ( (T_MFW_HDR *) parent_win )->data;
tCalcData* data = (tCalcData *) win_data->user;
TRACE_FUNCTION("calc_menu_cb()");
//set menu window to NULL to prevent any dealloc problems
data->menu_options_win = NULL;
calc_destroy(parent_win);
}
/*******************************************************************************
$Function: calcSetEditor
$Description: Sets the editor attributes to defaults for the calculator module
$Returns: nothing
$Arguments: Window
*******************************************************************************/
void calcSetEditor(T_MFW_HND win)
{
T_MFW_WIN *win_data = ( (T_MFW_HDR *) win )->data;
tCalcData* data = (tCalcData *) win_data->user;
TRACE_FUNCTION("calcSetEditor()");
/* SPR#1428 - SH - New Editor changes */
#ifdef NEW_EDITOR
{
AUI_edit_SetDefault(&data->editor_data);
AUI_edit_SetDisplay(&data->editor_data, ZONE_FULL_SK_TITLE, COLOUR_EDITOR, EDITOR_FONT);
AUI_edit_SetBuffer(&data->editor_data, ATB_DCS_ASCII, (UBYTE *)data->buffer, 10);
AUI_edit_SetMode(&data->editor_data, 0, ED_CURSOR_NONE);
AUI_edit_SetEvents(&data->editor_data, 0, TRUE, FOREVER, NULL);
AUI_edit_SetTextStr(&data->editor_data, TxtSoftOptions, TxtDelete, TxtCalculator, NULL);
AUI_edit_SetFormatAttr(&data->editor_data, DS_ALIGN_RIGHT);
}
#else /* NEW_EDITOR */
editor_attr_init(&((data->editor_data).editor_attr), 0, edtCurBar1, 0, data->buffer, 10, COLOUR_EDITOR);
editor_data_init( &data->editor_data, NULL, TxtSoftOptions, TxtDelete, NULL, 1, CALC_MODE, FOREVER);
data->editor_data.hide = FALSE;
data->editor_data.Identifier = 0xFFFF ;
data->editor_data.destroyEditor = TRUE;
#endif /* NEW_EDITOR */
return;
}
/*******************************************************************************
$Function: calc_destroy
$Description: Destroys the calculator editor window and frees up memory
$Returns: nothing
$Arguments: Window
*******************************************************************************/
void calc_destroy(MfwHnd own_window)
{
T_MFW_WIN * win_data;
tCalcData * data = NULL;
TRACE_EVENT ("calc_destroy()");
if (own_window)
{
win_data = ((T_MFW_HDR *)own_window)->data;
if (win_data != NULL) //PATCH TB
data = (tCalcData *)win_data->user;
if (data)
{
win_delete (data->win);
/* Marcus: Issue 1039: 05/09/2002: Start */
if (data->menu_options_win != NULL)
{
bookMenuDestroy( data->menu_options_win );
data->menu_options_win = NULL;
}
/* Marcus: Issue 1039: 05/09/2002: End */
// Free Memory
FREE_MEMORY ((void *)data, sizeof (tCalcData));
}
else
{
TRACE_EVENT ("calc_destroy() called twice");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -