PDA

View Full Version : C GUI programs in Xcode




TommySalami
Feb 24, 2008, 09:38 PM
So I'm taking a C class and we've been using windows machines in class to do the programming, but I've been using Xcode with no problems until now. What do I use in Xcode to write a GUI based C program? Specifically, what could I use to compile and run this source code?

//----------------------------------------------------------------------
// SECTION 1: Preprocessor Commands
//----------------------------------------------------------------------

// Include library headers for UNIX GUI programming
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Text.h>
#include <Xm/PushB.h>
#include <Xm/Label.h>
#include <stdio.h>
#include <string.h>

// Define the IDs of widgets
#define ID_button1 1001
#define ID_inbox1 2001
#define ID_inbox2 2002
#define ID_outbox 2003

// Event-handler prototype
void ProcessMessages( Widget , XtPointer , XEvent *, Boolean * ) ;

//----------------------------------------------------------------------
// SECTION 2: Computational Code
//----------------------------------------------------------------------

// Function Button1_Press() specifies what to do when button1 is pressed
void Button1_Press ( Widget window )
{
// declare local variables
char string1[50], string2[50], string3[50] ;
float var1, var2, var3;
Widget box ;
// get operands 1 and 2 as strings from inbox 1 and 2
box = XtNameToWidget ( window , "inbox1" );
strcpy( string1 , XmTextGetString( box ) );
box = XtNameToWidget ( window , "inbox2" );
strcpy( string2 , XmTextGetString( box ) );
// convert operands 1 and 2 from strings to floats
sscanf ( string1 , " %g ", &var1 );
sscanf ( string2 , " %g ", &var2 );
// perform the addition
var3 = var1 + var2;
// convert the result from float to string
sprintf( string3, " %g ", var3 );
// place result string into the outbox
box = XtNameToWidget ( window , "outbox" );
XmTextSetString( box , string3 ) ;
}

//----------------------------------------------------------------------
// SECTION 3: GUI Building
//----------------------------------------------------------------------

// Function MakeControls(): builds control objects on a window
void MakeControls ( Widget window )
{
// Create the "Operand 1" label
XtVaCreateManagedWidget( NULL, xmLabelWidgetClass, window,
XmNlabelString, XmStringCreateLocalized("Operand 1"),
XmNx, 10, XmNy, 30, XmNwidth, 100, XmNheight, 50, NULL);
// Create the "Operand 2" label
XtVaCreateManagedWidget( NULL, xmLabelWidgetClass, window,
XmNlabelString, XmStringCreateLocalized("Operand 2"),
XmNx, 160, XmNy, 30, XmNwidth, 100, XmNheight, 50, NULL);
// Create the "Result" label
XtVaCreateManagedWidget( NULL, xmLabelWidgetClass, window,
XmNlabelString, XmStringCreateLocalized("Result"),
XmNx, 310, XmNy, 30, XmNwidth, 100, XmNheight, 50, NULL);
// Create the "=" label
XtVaCreateManagedWidget( NULL, xmLabelWidgetClass, window,
XmNlabelString, XmStringCreateLocalized("="),
XmNx, 270, XmNy, 100, XmNwidth, 30, XmNheight, 30, NULL);
// Create the input Text box for operand 1
XtVaCreateManagedWidget( "inbox1", xmTextWidgetClass, window,
XmNx, 10, XmNy, 90, XmNwidth, 100, XmNheight, 50, NULL);
// Create the input Text box for operand 2
XtVaCreateManagedWidget( "inbox2", xmTextWidgetClass, window,
XmNx, 160, XmNy, 90, XmNwidth, 100, XmNheight, 50, NULL);
// Create the output Text box for the result
XtVaCreateManagedWidget( "outbox", xmTextWidgetClass, window, XmNeditable, False,
XmNx, 310, XmNy, 90, XmNwidth, 100, XmNheight, 50, NULL);
// Create the "+" button
XtVaCreateManagedWidget( "button1", xmPushButtonWidgetClass, window,
XmNlabelString, XmStringCreateLocalized("+"),
XmNx, 120, XmNy, 100, XmNwidth, 30, XmNheight, 30, NULL);
XtAddEventHandler( XtNameToWidget(window, "button1"), ButtonPressMask , FALSE,
(XtEventHandler) ProcessMessages, (XtPointer) ID_button1);
}

//----------------------------------------------------------------------
// SECTION 4: Event Dispatcher
//----------------------------------------------------------------------

// Function ProcessMessages(): Specifies how to respond to user interactions (X Window System)
void ProcessMessages(Widget window, XtPointer wParam, XEvent * message, Boolean * cont)
{
switch ( (*message).type)
{
case CreateNotify: // what to do when the mainform is created
MakeControls( XtNameToWidget( window, "mainform") );
break;
case ButtonPress: // what to do if a "button" is pressed
switch( (int) wParam )
{
case ID_button1: // button was button1?
Button1_Press( XtParent( window) );
break;
}
break;
}
}

//----------------------------------------------------------------------
// SECTION 5: Main Window and Application Code
//----------------------------------------------------------------------

// The main program starts here (X Window System)
int main(int argc, char *argv[])
{
// Declare local variables
Widget mainshell;
XEvent message;
XtAppContext app_context;
// create and then display the main window
strcpy(argv[0], "ENBE 241: GUI-Based Adder");
mainshell = XtVaOpenApplication(&app_context, "", NULL, 0, &argc, argv, NULL,
sessionShellWidgetClass,
XmNwidth, 450, XmNheight, 200, NULL);
XtAddEventHandler(mainshell, SubstructureNotifyMask , FALSE,
(XtEventHandler) ProcessMessages, (XtPointer) NULL);
XtRealizeWidget( mainshell );
// create a form on which to build controls
XtVaCreateManagedWidget("mainform", xmFormWidgetClass, mainshell, NULL);
// Continuously get and process messages
while (! XtAppGetExitFlag( app_context ))
{
XtAppNextEvent( app_context, &message);
XtDispatchEvent( &message );
}
// That's It!
return( 0 );
}



kainjow
Feb 24, 2008, 09:52 PM
Looks like you need X11 (http://developer.apple.com/opensource/tools/X11.html) installed along with OpenMotif (http://www.openmotif.org/).

TommySalami
Feb 24, 2008, 10:11 PM
OK, but what do I do to install OpenMotif/what libraries do I need?

friarlicious
Mar 2, 2008, 08:53 PM
Sorry, I don't know where to find Motif, but I'd guess macports or fink (assuming that it wasn't already installed with Xcode).