LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2017, 09:03 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Compiling a simple "Hello World" with motif library (Xm) for LINUX?


Hello

Would you eventually know the following method to do compile:

Code:
gcc -lXm -o hello motif.c
/usr/bin/ld: /tmp/cc8fCx6v.o: undefined reference to symbol 'XtMainLoop'
//usr/lib/i386-linux-gnu/libXt.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Code:
  apt-get install libmotif-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libmotif-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 185 not upgraded.

with the following
Code:
// gcc -lXm -o hello motif.c 
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/Label.h>

main(int argc, char *argv[])
{
  Widget toplevel, msg;
  Arg al[10];
  int ac;

  toplevel=XtInitialize(argv[0], "", NULL, 0, &argc, argv);
  ac = 0;
  XtSetArg(al[ac], XmNlabelString,
           XmStringCreate("Hello World!", 
                          XmSTRING_DEFAULT_CHARSET)); ac++;
  msg = XtCreateManagedWidget("msg", xmLabelWidgetClass, 
                              toplevel, al, ac);
  XtRealizeWidget(toplevel);
  XtMainLoop();
}
 
Old 04-07-2017, 10:53 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Quote:
libXt.so.6: error adding symbols: DSO missing
Simple :
Code:
gcc -lXm -lXt -o hello motif.c
Attached Images
 
 
Old 04-07-2017, 11:16 AM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by knudfl View Post
Simple :
Code:
gcc -lXm -lXt -o hello motif.c
Thank you very much !! Awesome, it worked. Now I try to get an inputbox....


Code:
 
// #include <X11/Intrinsic.h>  <--- this is NOT needed

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h> //
// for fexist
#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>  
// time 
#include <time.h>



#include <Xm/Xm.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>




int main(int argc, char *argv[])
{

  printf( "RUnning motif... \n" );
 



/*
 *    push_button.c
 *
 *     This file demonstrates creating a XmPushButton widget. It again uses
 *  the concept of the XmString [see label.c] and introduces the concept of
 *  a callback.
 *
 *                                                - Dave Shreiner
 *                                                  19 March 1991
 */

//#include <Xm/Xm.h>      /* X/Motif header file */
//#include <Xm/PushB.h>   /* header file for pushbutton widget type. */
/*----------------------------------------------------------------------------
      Declare variables and initialize the application.  [see label.c]
-----------------------------------------------------------------------------*/

   Widget      toplevel, button;
   Arg         arg;
   XmString    xmstr;
   void        callback_handler();

   toplevel = XtInitialize(argv[0], "MotifDemo", NULL, 0, &argc, argv);

/*----------------------------------------------------------------------------
      Set up the label string for the push button.
-----------------------------------------------------------------------------*/

   xmstr = XmStringCreateSimple("Push Me");
   XtSetArg(arg, XmNlabelString, xmstr);

/*----------------------------------------------------------------------------
      Create a push button widget using the XmCreatePushButton() call.  This
      function has identical parameters to the XmCreateLabel() call in label.c.
-----------------------------------------------------------------------------*/

   button = XmCreatePushButton(toplevel, "button", &arg, 1);

/*----------------------------------------------------------------------------
      Add a callback processing function to the push button widget.  A callback
      is one of the ways that X lets a user know that something has happened.
      Each diffrent type of widget has a set of callback resources that it
      can "register" for.  Callbacks are usually like "the user activated a
      push button" or "the user moved a scrollbar", as compared to more low
      level events like "the mosue pointer was moved", or "a keyboard button
      was pressed".

      The parameters for this function are as follows :

         1) the WIDGET that the callback is to be added for (in this case,
            "button")

         2) the CALLBACK action that is of interest (the user pressing the
            button, which Motif knows as "XmNactivateCallback".  A complete
            listing of all callbacks to a particular type of widget is
            available in the OSF/Motif Programmer's reference)

         3) the FUNCTION that will respond to the callback (when the user
            does the action, execute these lines of code.  For us, the
             function is named "callback_handler" [see below])

         4) any data that we want passed to the function.  This can be a
            single value (int, float, double, etc.), or a pointer to whatever
            (structure, file, character array, etc.).  (here, we don't want to
            pass anything in, so we give a NULL value)

-----------------------------------------------------------------------------*/

   XtAddCallback(button, XmNactivateCallback, callback_handler, NULL);

/*----------------------------------------------------------------------------
      Manage the child widget (in this case "button"), let X know we want
      the window to appear, and then we'll process all the events sent from
      the X server.
-----------------------------------------------------------------------------*/

   XtManageChild(button);

   XtRealizeWidget(toplevel);
   XtMainLoop();
}



/*----------------------------------------------------------------------------
      Define a function to be passed into the XtAddCallback() call.  The
      declaration of a callback handling function is always of the from

         void function_name(widget, client_supplied_data, callback_data)
         Widget   widget;
         caddr_t  client_supplied_data, callback_data;

         the parameters are as follows :

            1) the WIDGET that the callback originated from.  The value
               returned will only every be one of the widgets that this
               function was registered with.  (in out example, the widget
               will be button widget, since that's the only widget that
               a callback was added to)

            2) the DATA that the user could supply in the last arguement to
               the XtAddCallback() call.  The caddr_t type is just a place
               holder, and should be replaced with the correct value for the
               type of data that is being passed in.  (For example, if we
               were passing in a character array (char *), then we would
               declare the "client_supplied_data" to be of type "char *", not
               caddr_t.)  [see scroll_bar.c for an example of passing data
               into a callback handling routine]

            3) information about the CALLBACK itself.  Generally, Motif defines
               a callback structure that is specific to each widget.  (For
               example, the type of structure returned from a push button
               callback is an "XmPushButtonCallbackStruct".) Once again, the
               caddr_t declaration for the argument is done for place holding.
               This is done since one callback handling routine could be used
               to process many different types of callbacks, each with a
               different type.  [see scroll_bar.c for more detail about
               using the CALLBACK parameter]

-----------------------------------------------------------------------------*/

void callback_handler(widget, client_data, callback_data)
Widget widget;
caddr_t client_data, callback_data;
{

/*----------------------------------------------------------------------------
      Do the actions that are to occur when the user presses the push button.
      In general, there would be some code here to react to some user request.
-----------------------------------------------------------------------------*/

   printf("  You pressed a push button.\n");
}
 
Old 04-07-2017, 11:21 AM   #4
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Et Voila, my First Inputbox !!

Code:
/*
 *  gcc   -lXm -lXt   inputbox.c    
 *
 *     This file demonstrates a XmText editor widget.  In addition, it
 *  provides more examples of working with the XmString type, callbacks, 
 *  and setting widget resources.
 *
 *                                                - Dave Shreiner
 *                                                  20 March 1991
 */

#include <Xm/Xm.h>      /* X/Motif header file */
#include <Xm/Text.h>    /* header file for Text widget type. */

main(argc, argv)
int argc;
char *argv[];
{

/*----------------------------------------------------------------------------
      Declare variables and initialize the application.  [see label.c]
-----------------------------------------------------------------------------*/

   Widget   toplevel, text;
   Arg      arg;
   void     enter();

   toplevel = XtInitialize(argv[0], "MotifDemo", NULL, 0, &argc, argv);

/*----------------------------------------------------------------------------
      Create a push button widget using the XmCreateText() call.  This
      function has identical parameters to the XmCreateLabel() call in label.c.
-----------------------------------------------------------------------------*/

   text = XmCreateText(toplevel, "text", NULL, 0);

/*----------------------------------------------------------------------------
      Define the width of the widget to be 400 pixels. [see label.c]
-----------------------------------------------------------------------------*/

   XtSetArg(arg, XmNwidth, 400);

/*----------------------------------------------------------------------------
      Set the resource value of the text widget after its creation.  In some
      instances, its not possible to set up resources before the widgets is
      created, and so the function XtSetValues() allows you to use X Arg
      type structures (as above), and set them somewhere else in the code
      after the widget has been created.
-----------------------------------------------------------------------------*/

   XtSetValues(text, &arg, 1);

/*----------------------------------------------------------------------------
      Register an activation callback for the text widget.  Activation for
      the text widget occurs when the user presses the <return> key.
      [see push_button.c for more on callbacks]
-----------------------------------------------------------------------------*/

   XtAddCallback(text, XmNactivateCallback, enter, NULL);

/*----------------------------------------------------------------------------
      Manage the child widget (in this case "text"), let X know we want the
      window to appear, and then we'll process all the events sent from the
      X server.
-----------------------------------------------------------------------------*/

   XtManageChild(text);

   XtRealizeWidget(toplevel);
   XtMainLoop();
}


/*----------------------------------------------------------------------------
      Define a callback function.  This routine uses the function
      XmTextGetString() to read the text from the text widget.  In addition,
      we use the function XtFree() to release the memory back to the system.
      This is important since, X/Motif usually allocates memory for you,
      releiving the program of the responsibility of allocating memory in
      some situations.  (See the OSF/Motif Programmer's Reference to see when
      calling XtFree() is necessary)
-----------------------------------------------------------------------------*/

void enter(widget, client_data, call_data)
Widget   widget;
caddr_t  client_data, call_data;
{
   char  *string;

   string = XmTextGetString(widget);
   printf(" The text widget contains : '%s'\n", string);
   XtFree(string);
}
 
Old 04-07-2017, 11:32 AM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Would you know if that one can be compiled on linux with Xt Xm and X11 ?

Code:
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/Scale.h>
#include <Xm/Label.h>

/* prototype colour_change function */

void change_colour(Widget , int , 
                   XmScaleCallbackStruct *);

/* Globals */

Widget label; /* this widget gets coloured by 
                 slider values for RGB */
XColor color; /* the current colour of the label */

main(int argc, char *argv[])

{
    Widget        top_wid, form, rowcol, scale;
    XtAppContext  app;
    XmString      label_str, red, blue, green;

    top_wid = XtVaAppInitialize(&app, "Colour", NULL, 0,
        &argc, argv, NULL, NULL);

    if (DefaultDepthOfScreen(XtScreen(top_wid)) < 2) {
        puts("You must be using a color screen.");
        exit(1);
    }
    
    
    /* Set colour flags field for full RGB display */

    color.flags = DoRed|DoGreen|DoBlue;
    
    /* initialize first colour */
    XAllocColor(XtDisplay(top_wid),
        DefaultColormapOfScreen(XtScreen(top_wid)), &color);
        
    /* build form to contain label and rowcolumn */

    form = XtVaCreateManagedWidget("form",
        xmFormWidgetClass, top_wid, 
        NULL);
        
    label_str = XmStringCreateLocalized("Colour Me");

    label = XtVaCreateManagedWidget("Label",
        xmLabelWidgetClass,   form,
        XmNlabelString, label_str,
        XmNheight,     300,
        XmNwidth,      300,
        XmNbackground, color.pixel,
        /* Form Attachment resources */
        XmNtopAttachment, XmATTACH_FORM,
        XmNleftAttachment, XmATTACH_FORM,
        XmNrightAttachment, XmATTACH_FORM,
        NULL);
        
    XmStringFree(label_str);

    /* Build rowcolumn to contain 3 scales for RGB input */
    
    rowcol = XtVaCreateWidget("rowcol", 
        xmRowColumnWidgetClass, form,
        XmNorientation, XmVERTICAL,
        /* Form Attachment resources */
        XmNtopAttachment, XmATTACH_WIDGET,
        XmNtopWidget, label,
        XmNbottomAttachment, XmATTACH_FORM,
        XmNleftAttachment, XmATTACH_FORM,
        XmNrightAttachment, XmATTACH_FORM,
        NULL);


    red = XmStringCreateLocalized("Red");

    /* reuses scale widget variable */
    
    scale = XtVaCreateManagedWidget("Red",
        xmScaleWidgetClass, rowcol,
        XmNshowValue, True,
        XmNorientation, XmHORIZONTAL,
        XmNmaximum, 255,
        XmNtitleString, red,
        NULL);
        
    XmStringFree(red);
    
    /* Trap scale valuechange and drags for callbacks */
    
    XtAddCallback(scale, XmNdragCallback, 
                  change_colour, DoRed);
    XtAddCallback(scale, XmNvalueChangedCallback, 
                  change_colour, DoRed);


    green = XmStringCreateLocalized("Green");
    
    scale = XtVaCreateManagedWidget("Green",
        xmScaleWidgetClass, rowcol,
        XmNshowValue, True,
        XmNorientation, XmHORIZONTAL,
        XmNmaximum, 255,
        XmNtitleString, green,
        NULL);
        
    XmStringFree(green);

    XtAddCallback(scale, XmNdragCallback, 
                  change_colour, DoGreen);
    XtAddCallback(scale, XmNvalueChangedCallback, 
                  change_colour, DoGreen);


    blue = XmStringCreateLocalized("Blue");
    
    scale = XtVaCreateManagedWidget("Blue",
        xmScaleWidgetClass, rowcol,
        XmNshowValue, True,
        XmNorientation, XmHORIZONTAL,
        XmNmaximum, 255,
        XmNtitleString, blue,
        NULL);
        
    XmStringFree(blue);
    
    XtAddCallback(scale, XmNdragCallback, 
                  change_colour, DoBlue);
    XtAddCallback(scale, XmNvalueChangedCallback, 
                  change_colour, DoBlue);


    XtManageChild(rowcol);

    XtRealizeWidget(top_wid);
    XtAppMainLoop(app);
}

void
change_colour(Widget scale_w, int rgb, 
              XmScaleCallbackStruct *cbs)

{
    Colormap cmap = DefaultColormapOfScreen(XtScreen(scale_w));
    
    /* rgb variable tells us which RGB scale was selected */

    switch (rgb) {
        case DoRed :
            color.red = (cbs->value << 8);
            break;
        case DoGreen :
            color.green = (cbs->value << 8);
            break;
        case DoBlue :
            color.blue = (cbs->value << 8);
    }

    /* reuse the same color index 1 */
    
    XFreeColors(XtDisplay(scale_w), cmap, &color.pixel, 1, 0);
    if (!XAllocColor(XtDisplay(scale_w), cmap, &color))
        puts("Couldn't XallocColor!"), exit(1);
    XtVaSetValues(label, XmNbackground, color.pixel, NULL);
}
 
Old 04-07-2017, 11:51 AM   #6
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
I have added all the possible working examples on linux that I could find, as a list of example files, here:

https://github.com/spartrekus/motif-...-example-files

hoping it helps

It is much better motif than fltk!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Valgrind reports memory leaks in very simple MPI "hello world" ejspeiro Programming 6 11-02-2012 11:35 AM
error in creating rpm package for a simple "Hello World" c program jayasekar Linux - Newbie 8 12-02-2009 12:53 AM
Error in Creating rpm package for a simple "Hello World" Program jayasekar Linux - Software 2 12-01-2009 08:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration