Add small calculator GUI demo.
Subversion-branch: /trunk/chocolate-doom Subversion-revision: 523
This commit is contained in:
parent
f7bf1488d8
commit
b03198720a
2 changed files with 164 additions and 1 deletions
|
|
@ -2,7 +2,7 @@
|
|||
AM_CFLAGS = @SDL_CFLAGS@ -I../src
|
||||
|
||||
noinst_LIBRARIES=libtextscreen.a
|
||||
bin_PROGRAMS=guitest
|
||||
bin_PROGRAMS=guitest calculator
|
||||
|
||||
libtextscreen_a_SOURCES = \
|
||||
txt_checkbox.c txt_checkbox.h \
|
||||
|
|
@ -24,3 +24,6 @@ libtextscreen_a_SOURCES = \
|
|||
guitest_LDADD = @SDL_LIBS@ libtextscreen.a
|
||||
guitest_SOURCES = guitest.c
|
||||
|
||||
calculator_LDADD = @SDL_LIBS@ libtextscreen.a
|
||||
calculator_SOURCES = calculator.c
|
||||
|
||||
|
|
|
|||
160
textscreen/calculator.c
Normal file
160
textscreen/calculator.c
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "txt_button.h"
|
||||
#include "txt_desktop.h"
|
||||
#include "txt_label.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
OP_NONE,
|
||||
OP_PLUS,
|
||||
OP_MINUS,
|
||||
OP_MULT,
|
||||
OP_DIV,
|
||||
} operator_t;
|
||||
|
||||
int starting_input = 0;
|
||||
int input_value = 0;
|
||||
txt_label_t *input_box;
|
||||
int first_operand;
|
||||
operator_t operator = OP_NONE;
|
||||
|
||||
void UpdateInputBox(void)
|
||||
{
|
||||
char buf[20];
|
||||
|
||||
sprintf(buf, " %i", input_value);
|
||||
TXT_SetLabel(input_box, buf);
|
||||
}
|
||||
|
||||
void InsertNumber(TXT_UNCAST_ARG(button), TXT_UNCAST_ARG(value))
|
||||
{
|
||||
TXT_CAST_ARG(int, value);
|
||||
|
||||
if (starting_input)
|
||||
{
|
||||
input_value = 0;
|
||||
starting_input = 0;
|
||||
}
|
||||
|
||||
input_value *= 10;
|
||||
input_value += *value;
|
||||
UpdateInputBox();
|
||||
}
|
||||
|
||||
void AddNumberButton(txt_table_t *table, int value)
|
||||
{
|
||||
txt_button_t *button;
|
||||
char buf[10];
|
||||
int *val_copy;
|
||||
|
||||
val_copy = malloc(sizeof(int));
|
||||
*val_copy = value;
|
||||
|
||||
sprintf(buf, " %i ", value);
|
||||
|
||||
button = TXT_NewButton(buf);
|
||||
TXT_AddWidget(table, button);
|
||||
TXT_SignalConnect(button, "pressed", InsertNumber, val_copy);
|
||||
}
|
||||
|
||||
void Operator(TXT_UNCAST_ARG(button), TXT_UNCAST_ARG(op))
|
||||
{
|
||||
TXT_CAST_ARG(operator_t, op);
|
||||
|
||||
first_operand = input_value;
|
||||
operator = *op;
|
||||
starting_input = 1;
|
||||
}
|
||||
|
||||
void AddOperatorButton(txt_table_t *table, char *label, operator_t op)
|
||||
{
|
||||
txt_button_t *button;
|
||||
char buf[10];
|
||||
operator_t *op_copy;
|
||||
|
||||
op_copy = malloc(sizeof(operator_t));
|
||||
*op_copy = op;
|
||||
|
||||
sprintf(buf, " %s ", label);
|
||||
button = TXT_NewButton(buf);
|
||||
|
||||
TXT_AddWidget(table, button);
|
||||
TXT_SignalConnect(button, "pressed", Operator, op_copy);
|
||||
}
|
||||
|
||||
void Calculate(TXT_UNCAST_ARG(button), void *unused)
|
||||
{
|
||||
switch (operator)
|
||||
{
|
||||
case OP_PLUS:
|
||||
input_value = first_operand + input_value;
|
||||
break;
|
||||
case OP_MINUS:
|
||||
input_value = first_operand - input_value;
|
||||
break;
|
||||
case OP_MULT:
|
||||
input_value = first_operand * input_value;
|
||||
break;
|
||||
case OP_DIV:
|
||||
input_value = first_operand / input_value;
|
||||
break;
|
||||
}
|
||||
|
||||
UpdateInputBox();
|
||||
|
||||
operator = OP_NONE;
|
||||
starting_input = 1;
|
||||
}
|
||||
|
||||
void BuildGUI()
|
||||
{
|
||||
txt_window_t *window;
|
||||
txt_table_t *table;
|
||||
txt_button_t *equals_button;
|
||||
|
||||
window = TXT_NewWindow("Calculator");
|
||||
|
||||
TXT_AddWidget(window, TXT_NewSeparator(NULL));
|
||||
input_box = TXT_NewLabel("asdf");
|
||||
TXT_AddWidget(window, input_box);
|
||||
TXT_AddWidget(window, TXT_NewSeparator(NULL));
|
||||
TXT_AddWidget(window, TXT_NewLabel(""));
|
||||
|
||||
table = TXT_NewTable(4);
|
||||
TXT_AddWidget(window, table);
|
||||
|
||||
AddNumberButton(table, 7);
|
||||
AddNumberButton(table, 8);
|
||||
AddNumberButton(table, 9);
|
||||
AddOperatorButton(table, "*", OP_MULT);
|
||||
AddNumberButton(table, 4);
|
||||
AddNumberButton(table, 5);
|
||||
AddNumberButton(table, 6);
|
||||
AddOperatorButton(table, "-", OP_MINUS);
|
||||
AddNumberButton(table, 1);
|
||||
AddNumberButton(table, 2);
|
||||
AddNumberButton(table, 3);
|
||||
AddOperatorButton(table, "+", OP_PLUS);
|
||||
AddNumberButton(table, 0);
|
||||
TXT_AddWidget(table, TXT_NewLabel(""));
|
||||
equals_button = TXT_NewButton(" = ");
|
||||
TXT_SignalConnect(equals_button, "pressed", Calculate, NULL);
|
||||
TXT_AddWidget(table, equals_button);
|
||||
AddOperatorButton(table, "/", OP_DIV);
|
||||
|
||||
TXT_AddWidget(window, TXT_NewLabel(""));
|
||||
UpdateInputBox();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
TXT_Init();
|
||||
TXT_SetDesktopTitle("Calculator demo");
|
||||
|
||||
BuildGUI();
|
||||
|
||||
TXT_GUIMainLoop();
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue