[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference bulova::decw_jan-89_to_nov-90

Title:DECWINDOWS 26-JAN-89 to 29-NOV-90
Notice:See 1639.0 for VMS V5.3 kit; 2043.0 for 5.4 IFT kit
Moderator:STAR::VATNE
Created:Mon Oct 30 1989
Last Modified:Mon Dec 31 1990
Last Successful Update:Fri Jun 06 1997
Number of topics:3726
Total number of notes:19516

2515.0. ""Main Window Widget won't take colormap override"" by RHETT::BONNE () Mon Mar 26 1990 15:15

    Is there a bug in the uws 2.2 DW toolkit wherein a call to
    DwtMainWindowCreate with an override list fails for the colormap?
    
    The following code creates a top level widget, then the main window
    widget with an override list including height and width as well as the
    colormap and everything BUT the colormap is overridden.  When you click
    on Run, it will create another window and display your default
    colormap.  When you click in the window, it will switch to the created
    colormap (not real pretty I admit..), but then clicking back in the
    main window widget, it reverts to the default colormap.
    
    Anyone had problems setting up a colormap for a main window widget?
    
    Compile with the standard cc -o pgm pgm.c -ldwt -lX11
    Jim 
    
#include  <stdio.h>
#ifdef VMS
#include <decw$include:Xlib.h>
#include <decw$include:Xutil.h>
#else
#include <sys/file.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/DECwDwtApplProg.h>
#endif

#define  NIMAGES         2 
#define  NCOLORS         240 
#define  WCOLORS_BEG     0
#define  WCOLORS_END     3
#define  COLORS_BEG      8 
#define  COLORS_END      247 

#define  MXCOLOR             65535.0
#define  MXLINE              80
#define  PIXEL_DEPTH         8
#define  WINDOW_BORDER_SIZE  0      
#define  WINDOW_ORGIN_X      400 
#define  WINDOW_ORGIN_Y      200 

#define  WINDOW_HEIGHT   200
#define  WINDOW_WIDTH    400 
#define  BUTTON_1_X      50  
#define  BUTTON_1_Y      50  
#define  BUTTON_2_X      150  
#define  BUTTON_2_Y      50  
#define  BUTTON_3_X      250  
#define  BUTTON_3_Y      50  
#define  VERTICAL        1
#define  HORIZONTAL      2
#define  LABEL_X         150
#define  LABEL_Y         100  
#define  LABEL_MAX       100  
#define  ORG_X           500
#define  ORG_Y           500
                             
typedef  unsigned char  BYTE;

unsigned long           pixtab[256];

Colormap                cmap;        
Display                 *display;                                        
Drawable                win_draw;
Status                  status;
GC                      gc;
Pixmap			pixmap[NIMAGES];
Window                  window; 
Visual                  *visual; 
XColor                  xcolor[NCOLORS];
XEvent                  event;
XExposeEvent            *expose;     
XGCValues               gc_val;
XImage                  *ximage[NIMAGES];
XSetWindowAttributes    win_att;                          
XWindowAttributes       win_attr;
XVisualInfo             visualinfo;     

Widget  w_top,          /*  Widget declaration  */ 
        w_main,             
        w_button_1,
        w_button_2,      
	w_button_3, 
        w_label;            

void    run(),                                 /*  Callback declarations  */   
        quit(),
	color();

DwtCallback                                    /*  Callback pointers  */
        cb_run[]   = {{run, NULL}, {NULL}},      
        cb_quit[]  = {{quit, NULL}, {NULL}},
	cb_color[] = {{color, NULL}, {NULL}};

DwtCompString                                  /*  Labels  */
        button_1_label,
        button_2_label,
        button_3_label,
        label_label;    
                         
   
main (argc, argv)  
int  argc;
char **argv;
{
	Arg	args[10];

/*  Initialize the display and colormap  */

	winit();

/*  Set up and manage main window  */

	wcolor ();

        w_top  = XtInitialize (NULL, "applicationShellWidgetClass", 0,
0, &argc, argv);
	XtSetArg (args[0], DwtNwidth,  400);
	XtSetArg (args[1], DwtNheight, 2000);
	XtSetArg (args[2], DwtNacceptFocus, TRUE);
	XtSetArg (args[3], DwtNcolormap, cmap);
/************************************************************************
/* This is the problem.. the above arg for colormap does not take
/* effect.  The other params do!  Is this a bug??
/********************************************************************/
        w_main = DwtMainWindowCreate (w_top, "MainWindow", args, 4);

	if (w_main == 0) {
		printf("Main window create problem\n");
		exit (1);
		}

        XtManageChild (w_main);
                                        
/*  Set up widgets  */
                 
        button_1_label = DwtLatin1String ("Run");
        w_button_1 = DwtPushButton (w_main, "RunButton", BUTTON_1_X,
                                    BUTTON_1_Y, button_1_label, cb_run, NULL);
        XtManageChild (w_button_1);

        button_2_label = DwtLatin1String ("Quit");
        w_button_2 = DwtPushButton (w_main, "QuitButton", BUTTON_2_X,
                                    BUTTON_2_Y, button_2_label, cb_quit, NULL);
        XtManageChild (w_button_2);

        button_3_label = DwtLatin1String ("Color");
        w_button_3 = DwtPushButton (w_main, "ColorButton", BUTTON_3_X,
                                    BUTTON_3_Y, button_3_label, cb_color, NULL);
        XtManageChild (w_button_3);

        label_label = DwtLatin1String ("Color Map Test");
        w_label = DwtLabel (w_main, "TitleLabel", LABEL_X,
                            LABEL_Y, label_label, NULL);
        XtManageChild (w_label);  

/*  Open window and process events  */

        XtRealizeWidget (w_top);
        XtMainLoop ();
}                                           


void run (widget, tag, reason)          /*  Run callback procedure  */
Widget  widget;
long    tag, *reason;
{                                
        int     value; 
	void	raster();
          
        label_label = DwtLatin1String ("Call Application");
        w_label = DwtLabel (w_main, "TitleLabel", LABEL_X,
                            LABEL_Y, label_label, NULL);
        XtManageChild (w_label);  
	raster(); 
}


void quit (widget, tag, reason)         /*  Quit callback procedure  */
Widget  widget;
long    tag, *reason;
{
        XtUnmanageChild (widget);
        exit (1);
}


void color (widget, tag, reason)         /*  Color callback procedure  */
Widget  widget;
long    tag, *reason;
{
	int    	index, mode, n; 
        float  	r[256], g[256], b[256];

	mode = 0;
	for (index = 0; index < NCOLORS; index++) { 
		r[index] = 0.0; 
		g[index] = 1.0 - (float)(index) / (float)(NCOLORS); 
		b[index] = 1.0 - (float)(index) / (float)(NCOLORS); 
               	if (index >= COLORS_BEG && index <= COLORS_END) 
			wlut (&mode, &index, &r[index], &g[index], &b[index]);
	}
	mode = 1;
	wlut (&mode, &index, r, g, b);
}


void raster()
{
	BYTE	*images[NIMAGES]; 
	int    	i, image, index, mode, n;
	int    	x, xdst, xsize, y, ydst, ysize; 
        float  	r[256], g[256], b[256];

/*  Open window  */

	xsize = 512;
	ysize = 256;
 	wopn (&xsize, &ysize);

/*  Set LUT  */

	mode = 0;
	for (index = 0; index < NCOLORS; index++) { 
		r[index] = .0; 
		g[index] = (float)(index) / (float)(NCOLORS); 
		b[index] = (float)(index) / (float)(NCOLORS); 
               	if (index >= COLORS_BEG && index <= COLORS_END) 
			wlut (&mode, &index, &r[index], &g[index], &b[index]);
	}
	mode = 1;
	wlut (&mode, &index, r, g, b);

/*  Allocate memory  */

	image = 0;
	images[image] = (BYTE *)(malloc (sizeof (BYTE) * xsize * ysize));

/*  Load image data  */

	for (y = 0; y < ysize; y++) {
		for (x = 0; x < xsize; x++) {
			*(images[image] + y * xsize + x) = (BYTE)(x / 2);
		}
	}
        mode = 0;	
	xdst = 0;
        ydst = 0;
	wimage (&mode, images[image], &image, &xsize, &ysize, &xdst, &ydst);
        mode = 1;
	wimage (&mode, images[image], &image, &xsize, &ysize, &xdst, &ydst);
}


/*  C version of x11index.c  */


wclr ()
{
        XClearWindow (display, window);              
}       


wcls ()
{
        XCloseDisplay(display);
}


wimage (mode, image, i, xsize, ysize, xdst, ydst)
BYTE	*image;
int     *i, *mode, *xdst, *xsize, *ydst, *ysize;
{
        if (*mode == 0) {
                pixmap[*i] = XCreatePixmap (display, win_draw, *xsize, 
                                                *ysize, PIXEL_DEPTH); 
                ximage[*i] = XCreateImage (display, visual, PIXEL_DEPTH,
			     ZPixmap, 0, image, *xsize, *ysize, 8, *xsize); 
        	XPutImage (display, pixmap[*i], gc, ximage[*i], 
                   	   0, 0, *xdst, *ydst, *xsize, *ysize); 
        } else if (*mode == 1) {
                XCopyArea (display, pixmap[*i], window, gc, 0, 0,
                           *xsize, *ysize, *xdst, *ydst);
       		XFlush (display);
	}
}


winit ()
{

/*  Connect with server                           */                       

        display = XOpenDisplay (getenv ("DISPLAY"));  
                                        
/*  Initialize visual data structure  */

        visual = DefaultVisual (display, 0);
        status = XMatchVisualInfo (display, 0, PIXEL_DEPTH,
                                   PseudoColor, &visualinfo);
        visual = visualinfo.visual;

/*  Setup colormap  */ 

        cmap = XCreateColormap (display, RootWindow (display, 0), 
                                    visual, AllocNone);
        status = XAllocColorCells (display, cmap, 0, NULL, 0,
                                   pixtab, 4);  
        if (status == 0) {     
                printf ("XAllocColorCells failed \n");
                exit (1);
        }
}


wcolor ()
{
        int    b, g, i, mode, r;        
	float  value;
        static float  wcolors[8][3] = {{ .0,  .0,  .0}, /* Display background */
                                       {1.0, 1.0, 1.0}, /* Display foreground */
                                       { .9,  .0,  .0}, /* Pointer background */
                                       { .0,  .9,  .0}, /* Pointer Foreground */
                                       { .0,  .0,  .9}, /* Window background  */
                                       { .9,  .9,  .0}, /* Window border      */
                                       { .0,  .9,  .9}, /* Window foreground  */
                                       { .9,  .0,  .9}  /* Window highlight   */
        };                           

        status = XAllocColorCells (display, cmap, 0, NULL, 0,
                                   pixtab, NCOLORS);  
        if (status == 0) {     
                printf ("XAllocColorCells failed \n");
                exit (1);
        }

/*  Set window colors  */

	mode = 0;
	for (i = WCOLORS_BEG; i <= WCOLORS_END; i++) {  
                wlut (&mode, &i, &wcolors[i][0],
                      &wcolors[i][1], &wcolors[i][2]);
        }

/*  Set initial colors  */

	for (i = COLORS_BEG; i <= COLORS_END; i++) {  
                value = (float)(i) / (float)(NCOLORS - 1);
                wlut (&mode, &i, &value, &value, &value);
        }
	mode = 1;
        wlut (&mode, &i, &value, &value, &value);
}


wlut (mode, index, red, green, blue)
int     *index, *mode; 
float   *blue, *green, *red;
{
        if (*mode == 0) {
                if ((*index >= WCOLORS_BEG && *index <= WCOLORS_END) ||
                    (*index >= COLORS_BEG  && *index <= COLORS_END)) {
                      xcolor[*index].pixel = (unsigned long)(*index);        
                      xcolor[*index].red   = (unsigned short)(*red   * MXCOLOR);
                      xcolor[*index].green = (unsigned short)(*green * MXCOLOR);
                      xcolor[*index].blue  = (unsigned short)(*blue  * MXCOLOR);
                      xcolor[*index].flags = DoRed | DoGreen | DoBlue;
                }
        } else if (*mode == 1) {
                XStoreColors (display, cmap, xcolor, NCOLORS);
                XStoreColors (display, cmap, xcolor, NCOLORS);
                XStoreColors (display, cmap, xcolor, NCOLORS);
        }
}


wopn (width, height)
int     *height, *width;
{

/*  Window  */

        win_att.background_pixel = 0x0000; 
        win_att.border_pixel     = 0x0000; 
        win_att.colormap         = cmap;        
        win_att.event_mask       = ExposureMask | ButtonPressMask;
        window = XCreateWindow (display, RootWindow (display, 0),
                                WINDOW_ORGIN_X, WINDOW_ORGIN_Y,
                                *width, *height, WINDOW_BORDER_SIZE,
                                PIXEL_DEPTH, InputOutput, visual,
                                CWColormap    | CWBackPixel |               
                                CWBorderPixel | CWEventMask, 
                                &win_att);
        XSetWindowColormap (display, window, cmap);
        XStoreName (display, window, "Raster Window");
        XMapWindow (display, window); 
        XFlush (display);

/*  Create window drawable  */

        gc_val.function = GXcopy;
        gc = XCreateGC (display, window, GCFunction, &gc_val);
        win_draw = (Drawable)(window);     
                                      
/*  Wait for an exposure event before proceeding  */

        expose = (XExposeEvent *) &event;
        event.type = 0;   
        while (event.type != Expose || expose->window != window ||
                                       expose->count != 0) {  
                XNextEvent (display, &event);
        }
}
T.RTitleUserPersonal
Name
DateLines
2515.1R3 Intrinsics ignore the XtNcolorMap resourceLEOVAX::TREGGIARIMon Apr 02 1990 17:594
This has been discussed in this notes file or DECWINDOWS_PROGRAMMING or
DECWTOOLKIT (or maybe all 3...).

Leo