Jon Helt-Hansen Jon Helt-Hansen - 3 months ago 6
C Question

Buttons in Newt

I need a simple terminal-based window/widget library with a C or C++ interface in Linux. I found newt, which seems good enough for my needs, but I just have one issue which I cannot get around and google hasn't been very helpful:

How do I determine which button was pressed? Below is a simple button example with 2 buttons - "Ok" and "Cancel". How do I determine which was pressed?

#include <newt.h>
#include <stdlib.h>

void main(void) {
newtComponent form, b1, b2;
newtInit();
newtCls();

newtOpenWindow(10, 5, 40, 6, "Button Sample");

b1 = newtButton(10, 1, "Ok");
b2 = newtCompactButton(22, 2, "Cancel");
form = newtForm(NULL, NULL, 0);
newtFormAddComponents(form, b1, b2, NULL);

newtRunForm(form);

newtFormDestroy(form);
newtFinished();
}


UPDATE:

After receiving feedback I haved tried to add a callback function as per my understanding:

#include <newt.h>
#include <stdlib.h>

void b1Callback(newComponent co, void * data)
{
*((int*)data) = 4;
}

void main(void) {
void* data;
*((int*)data) = 0;
newtComponent form, b1, b2;
newtInit();
newtCls();

newtOpenWindow(10, 5, 40, 6, "Button Sample");

b1 = newtButton(10, 1, "Ok");
b2 = newtCompactButton(22, 2, "Cancel");
newtComponentAddCallback(b1, b1Callback, data);
form = newtForm(NULL, NULL, 0);
newtFormAddComponents(form, b1, b2, NULL);

newtRunForm(form);

newtFormDestroy(form);
newtFinished();
}

Answer

My solution was:

  • Set up a variable as newtComponent answer
  • After destroy the form call this function answer = newtFormGetCurrent(form);
  • and check if answer was my b1 button if( answer == b1 ).
Comments