Topic: Coding Problem from a puzzled newbie


Author: greyhelm@cis.ksu.edu (Karl S Hagen )
Date: 20 May 1992 04:56:59 GMT
Raw View
  Hi,

    I am working on a project at present that requires that I use C++
   and X.  Unfortunately,  my problem is more in the realm of C++ than
   X.  Here it is:

        1) C++ version 2.0
 2) X Window System Release 5
 3) Computer: Solbourne 5/603 ( 3 processors - Lots of Memory)

 Trying to do:
  1) Create a Window Object that when created(via a
     constructor) it will handle all I/O to that window
     and destroy it accordingly.
  2) This Window Object will be used to create several
     independant windows.  I figured an all encompassing
     object to handle the window details would be a good
     step.  I can do the windows in regular C, but that
     really is not the goal.

 Problem:
  For some reason when the program runs (compiles with
  No errors or warnings) it constructs the object
  but does not store or pass any data.  I have looked in
  the C++ books I have and I am pretty sure it is good
  code(for a newbie).

 Here is the Output:
  Commencing Program.
  Creating a New window.
  testCreating a New Window.
  Initializing Display for    i,    i?8   h   ,   i,    i?      h   L   i+      k4      k7    k5    k5/   kE      k51   k56   k5<   k5.
  Checking Command Line for Display Name.
  Checking Command Line for Geometry Settings.
  Checking Command Line for Font Name.
  Setting Up defaults.
   Loading Fonts
   Loading Fonts
  : cannot open              font


       Here is the code:

// main module for a test of object.

#include "screen.h"

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

    cout << "Commencing Program. \n";

    TextWindow test(argv[0]);

    test.CreateWindow(argc,argv);

    sleep(100000);

    exit(0);
}

--------------------------------screen.h-----------------------------------
//  screen.h  - Screen handler routinges
//    X Includes

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/cursorfont.h>

//     Standard C Includes

#include <stdlib.h>
#include <stream.h>
#include <locale.h>
#include <string.h>
#include <malloc.h>

//   Defines

#define REGULAR_VIDEO  0
#define REVERSE_VIDEO  1
#define BORDER_WIDTH   3

//   Globals

class TextWindow {
    unsigned int display_width, display_height, display_depth;
    Display *display;
    int screen,x_location, y_location,width, height;
    Window window, rootwindow;
    GC gc;
    Pixmap icon;
    Cursor cursor;
    XFontStruct *font;
    char *display_name, *font_name;
    unsigned long black,white;
    char *application_name;
    XSizeHints size_hints;
    void Init_Display(int,char *[]);
    void CheckDisplayName(int,char *[]);
    void CheckGeometry(int,char *[]);
    void CheckFontName(int,char *[]);
    void MakeGC(int);
    void Load_Font();
    void SetUpSizeHints(void);
 public:
    TextWindow(char *);
    ~TextWindow();
    void CreateWindow(int,char *[]);
    void DestroyWindow(void);
};

---------------------------screen.C---------------------------------------
// screen.C

#include "screen.h"

// globals

void TextWindow :: Init_Display(int argc,char *argv[])
{

#ifdef DEBUG
    cout << form("Initializing Display for %s.\n",application_name);
#endif
    CheckDisplayName(argc, argv);
    CheckGeometry(argc,argv);
    CheckFontName(argc,argv);

    if ((display = XOpenDisplay(display_name)) == NULL) {
 cout << form("%s: cannot connect to X server %s\n",
       application_name, XDisplayName(display_name));
 exit(-1);
    }
#ifdef DEBUG
    cout << "Setting Up defaults.\n";
#endif
    screen = DefaultScreen(display);
    rootwindow = RootWindow(display,screen);
    black = BlackPixel(display, screen);
    white = WhitePixel(display,screen);
    display_width = DisplayWidth(display, screen);
    display_height = DisplayHeight(display, screen);
    display_depth = DisplayPlanes(display,screen);
}

void TextWindow :: MakeGC(int mode)
{
#ifdef DEBUG
    cout << "Setting Up - GC\n";
#endif
    gc = XCreateGC(display,rootwindow,0,NULL);
    XSetFont(display,gc,font->fid);
    if(mode == REVERSE_VIDEO) {
 XSetForeground(display,gc,white);
 XSetBackground(display,gc,black);
    }
    else {
 XSetForeground(display,gc,black);
 XSetBackground(display,gc,white);
    }
}

void TextWindow :: Load_Font()
{
#ifdef DEBUG
    cout << " Loading Fonts\n";
#endif
    if ((font = XLoadQueryFont(display,font_name)) == NULL) {
 cout << form("%s: cannot open %s font \n",application_name,font_name);
 exit(-1);
    }
}

void TextWindow :: CheckDisplayName(int argc,char *argv[])
{
    int     counter;
    display_name = NULL;
    counter         = 1;
#ifdef DEBUG
    cout << "Checking Command Line for Display Name.\n";
#endif

    while(counter < argc)
    {
 if((strncmp(argv[counter],"-display",8)) == 0)
 {
     counter++;
     if (counter < argc)
     {
  display_name = new char[sizeof(argv[counter])];
  (void) strcpy(display_name,argv[counter]);
     }
     else
     {
  cout << "Error: usage is -display DisplayName\n";
  exit(-1);
     }
 }
 counter++;
    }
}

void TextWindow :: CheckFontName(int argc,char *argv[])
{
    int     counter;
    font_name = NULL;
    counter         = 1;

#ifdef DEBUG
    cout << "Checking Command Line for Font Name. \n";
#endif

    while(counter < argc)
    {
 if((strncmp(argv[counter],"-font",5)) == 0)
 {
     counter++;
     if (counter < argc)
     {
  font_name = new char[sizeof(argv[counter])];
  (void) strcpy(font_name,argv[counter]);
     }
     else
     {
  cout << "Error: usage is -font FontName\n";
  exit(-1);
     }
 }
 counter++;
    }
}

void TextWindow :: CheckGeometry(int argc,char *argv[])
{
    int     status;
    int     x1, y1;
    unsigned int width1, height1;
    int     counter;

#ifdef DEBUG
    cout << "Checking Command Line for Geometry Settings.\n";
#endif

    counter  = 1;
    while(counter < argc)
    {
 if(strncmp(argv[counter],"-geom",5) == 0)
 {
     counter++;
     if (counter < argc)
     {
  status = XParseGeometry(argv[counter],&x1, &y1,
     &width1, &height1);
  if (status & XValue)
      x_location = x1;
  if (status & YValue)
      y_location = y1;
  if (status & WidthValue)
      width = width1;
  if (status & HeightValue)
      height = height1;
  if (status & XNegative)
      x_location = display_width - width + x_location;
  if (status & YNegative)
      y_location = display_height - height + y_location;
     }
 }
 counter++;
    }
}

void TextWindow :: SetUpSizeHints()
{
#ifdef DEBUG
    cout << "Setting Up Size Hints.\n";
#endif

    size_hints.flags = PPosition | PSize | PMinSize | PMaxSize;
    size_hints.x = x_location;
    size_hints.y = y_location;
    size_hints.width = width;
    size_hints.height = height;
    size_hints.min_width = width;
    size_hints.min_height = height;
}

TextWindow :: TextWindow(char *name)
{
#ifdef DEBUG
    cout << "Creating a New window.\n";
#endif
    application_name = new char[sizeof(name)+1];
    strcpy(application_name,name);
    cout << application_name;
}

void TextWindow :: CreateWindow(int argc,char *argv[])
{
#ifdef DEBUG
    cout << "Creating a New Window.\n";
#endif
    Init_Display(argc,argv);
    Load_Font();
    SetUpSizeHints();
    window = XCreateSimpleWindow(display,rootwindow,0,0,width,height,
     BORDER_WIDTH,black,white);
    XSetStandardProperties(display,window,application_name,application_name,
      None,argv,argc,&size_hints);
    XSelectInput(display,window,ExposureMask | ButtonPressMask |
   EnterWindowMask | LeaveWindowMask);
    cursor = XCreateFontCursor(display,XC_left_ptr);
    XDefineCursor(display,window,cursor);
    MakeGC(REGULAR_VIDEO);
}

TextWindow :: ~TextWindow()
{
#ifdef DEBUG
    cout << "Cleaning Up.\n";
#endif
    XFreeCursor(display,cursor);
    XDestroyWindow(display,window);
    XUnloadFont(display,font->fid);
    XCloseDisplay(display);
    delete application_name;
    delete font_name;
    delete display_name;
}


-----------------------------Makefile------------------------------

PROGRAM = test

TARGETS = all $(PROGRAM) clean depend

CCSOURCES = screen.C main.C

CC = CC

# Uncomment the following line for multiprocessor/distributed code
#MULT_PROC = +M

# Uncomment the following line if you are using multiprocessor code
# and you wish to connect to machines outside of the domain of your
# "master" processor.
#RESOLVE = -lresolv

# Add any other defines you need here
DEFINES = -DDEBUG

# Add any other libraries you need here
LIBS = -lX11

# Location of your include file directories (for makedepend)
XINCDIR   = /usr/X11/include
C++INCDIR = /usr/local/C++/include

# you should not need to change anything below this line
########################################################

OBJECTS = $(CCSOURCES:.C=.o)
SOURCES = $(CCSOURCES)

CCFLAGS = -c -g -pipe  $(MULT_PROC)
CPPFLAGS = $(DEFINES)
LDFLAGS =  $(MULT_PROC)

LIBRARIES = $(LIBX11) $(RESOLVE) $(LIBS)

.SUFFIXES: .C .o $(SUFFIXES)

.PRECIOUS: $(PROGRAM)

all: $(PROGRAM)

$(PROGRAM): $(OBJECTS)
 $(CC) -o $@ $(LDFLAGS) $(OBJECTS) $(LIBRARIES)

.C.o:
 $(CC) $(CPPFLAGS) $(CCFLAGS) $<

clean:
 $(RM) core $(OBJECTS) *.C

spotless: clean
 $(RM) $(PROGRAM)

depend:
 makedepend -o -I$(CCINCDIR) -I$(C++INCDIR) -I$(XINCDIR) -- $(SOURCES)

# DO NOT DELETE THIS LINE -- make depend depends on it.

screen-I: screen.h /usr/include/X11/Xlib.h /usr/local/C++/include/sys/types.h
screen-I: /usr/local/C++/include/cc/sys/types.h
screen-I: /usr/local/C++/include/sys/stdtypes.h
screen-I: /usr/local/C++/include/cc/sys/stdtypes.h
screen-I: /usr/include/sys/sysmacros.h /usr/include/X11/X.h
screen-I: /usr/include/X11/Xfuncproto.h /usr/include/X11/Xosdefs.h
screen-I: /usr/local/C++/include/stddef.h /usr/include/X11/Xutil.h
screen-I: /usr/include/X11/Xos.h /usr/local/C++/include/string.h
screen-I: /usr/local/C++/include/memory.h /usr/local/C++/include/cc/memory.h
screen-I: /usr/local/C++/include/cc/string.h /usr/local/C++/include/fcntl.h
screen-I: /usr/local/C++/include/cc/fcntl.h
screen-I: /usr/local/C++/include/cc/sys/fcntlcom.h
screen-I: /usr/local/C++/include/sys/stat.h
screen-I: /usr/local/C++/include/cc/sys/stat.h
screen-I: /usr/local/C++/include/unistd.h /usr/local/C++/include/cc/unistd.h
screen-I: /usr/local/C++/include/sys/time.h
screen-I: /usr/local/C++/include/cc/sys/time.h
screen-I: /usr/local/C++/include/cc/sys/time.h /usr/local/C++/include/stdio.h
screen-I: /usr/local/C++/include/errno.h
screen-I: /usr/local/C++/include/cc/sys/errno.h
screen-I: /usr/local/C++/include/cc/stdio.h /usr/local/C++/include/iostream.h
screen-I: /usr/include/locale.h



---------------------------------------------------------------------------


     Thanks for any and all help ....
   Karl S. Hagen




Author: skdutta@cs.tamu.edu (Saumen K Dutta)
Date: Wed, 20 May 1992 05:22:07 GMT
Raw View
In article <greyhelm.706337908@depot.cis.ksu.edu.cis.ksu.edu> greyhelm@cis.ksu.edu (Karl S Hagen ) writes:
>  Hi,
>
>    I am working on a project at present that requires that I use C++
>   and X.  Unfortunately,  my problem is more in the realm of C++ than
>   X.  Here it is:
>
>        1) C++ version 2.0
> 2) X Window System Release 5
> 3) Computer: Solbourne 5/603 ( 3 processors - Lots of Memory)
>
> Trying to do:
>  1) Create a Window Object that when created(via a
>     constructor) it will handle all I/O to that window
>     and destroy it accordingly.
>  2) This Window Object will be used to create several
>     independant windows.  I figured an all encompassing
>     object to handle the window details would be a good
>     step.  I can do the windows in regular C, but that
>     really is not the goal.
>
> Problem:
>  For some reason when the program runs (compiles with
>  No errors or warnings) it constructs the object
>  but does not store or pass any data.  I have looked in
>  the C++ books I have and I am pretty sure it is good
>  code(for a newbie).
>
> Here is the Output:
>  Commencing Program.
>  Creating a New window.
>  testCreating a New Window.
>  Initializing Display for    i,   i?8   h   ,   i,   i?      h   L   i+      k4      k7   k5    k5/   kE      k51   k56   k5<   k5.
>  Checking Command Line for Display Name.
>  Checking Command Line for Geometry Settings.
>  Checking Command Line for Font Name.
>  Setting Up defaults.
>   Loading Fonts
>   Loading Fonts
>  : cannot open              font
>
>
>       Here is the code:
>

.... around 380 lines of code

>
>
>---------------------------------------------------------------------------
>
>
>     Thanks for any and all help ....
>   Karl S. Hagen

I have done some work with Xlib and C++. I or somebody like me would try to
help you but when I see long code it turns me off. Please cut down the code to
pinpoint the problem. It becomes simpler to read and you are likely to receive
more response.

Thanks
skdutta




Author: jbm@hal.com (Brad Might)
Date: 20 May 92 16:33:39 GMT
Raw View

There is no event handling code in your program.


Please read your X manuals about Events and Handling them !

brad
--
- standard disclaimers apply -
jbm@hal.com (Brad Might)
HaL Computer Systems - (512)794-2855
<SGML>more than just a new breakfast cereal</>




Author: tnelson@bellahs.com (Tom Nelson RD)
Date: Thu, 21 May 1992 15:53:01 GMT
Raw View
In article <JBM.92May20093339@infinity.hal.com> jbm@hal.com (Brad Might) writes:
>
>There is no event handling code in your program.
>
>Please read your X manuals about Events and Handling them !
>
>brad

I think that there was an atricle on this in the X-Journal one (or two) issues
back.

-----------------------------------------------------------------------
Thomas Nelson                                       tnelson@bellahs.com
                                                      tdark@wet.com
... Any resemblance between the above views and those of my employer,
my terminal, or the view out my window are purely coincidental.  Any
resemblance between the above and my own views is non-deterministic.