summaryrefslogtreecommitdiffstats
path: root/src/glw/boilerplate.c
blob: c53a3fa73567e7e25d1305b350ef656238b384f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*

                                BOILERPLATE

   To get started with mixed model programming with Motif and OpenGL, this
   boilerplate `application' might help get you started.

   This program honors two environment variables:

      SETVISUAL <id>    Makes the application use the indicated visual,
                        instead of the one chosen by glxChooseVisual.

      SAMEVISUAL        Make the application use the same visual for the
                        GUI as for the 3D GL Widget.

   The basic idea is to minimize colormap `flash' on systems with only one
   hardware colormap, especially when focus shifts between several
   of the application's windows, e.g. the about box.

   If you have suggestions for improvements, please mail to:


      Jeroen van der Zijp  <jvz@cyberia.cfdrc.com>


   Feel free to turn this into a useful program!!

*/


/*

    This code is hereby placed under GNU GENERAL PUBLIC LICENSE.
    Copyright (C) 1996 Jeroen van der Zijp <jvz@cyberia.cfdrc.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


/* Include the kitchen sink */
#include <stdio.h>
#include <stdlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/CascadeB.h>
#include <Xm/BulletinB.h>
#include <Xm/DialogS.h>
#include <Xm/Frame.h>
#include <Xm/MessageB.h>
#include <Xm/Form.h>
#include <Xm/Separator.h>
#include <Xm/MwmUtil.h>

/* Now some good stuff */
#include <GLwMDrawA.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>


/* Stuff */
Display      *display;
Visual       *gui_visual;
Colormap      gui_colormap;
Colormap      gl_colormap;
XVisualInfo  *gl_visualinfo;
XtAppContext  app_context;
Widget        toplevel;
Widget        mainwindow;
Widget        menubar;
Widget        mainform;
Widget        mainframe;
Widget        glwidget;
Widget        button;
Widget        separator;
GLXContext    glx_context;

#ifndef __cplusplus
#define c_class class
#endif

/* Requested attributes; fix as you see fit */
static int glxConfig[]={
  GLX_RGBA,
  GLX_DOUBLEBUFFER,
  GLX_DEPTH_SIZE,   16,
  GLX_RED_SIZE,     1,
  GLX_GREEN_SIZE,   1,
  GLX_BLUE_SIZE,    1,
  None
  };


/* Forwards */
static void exposeCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs);
static void initCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs);
static void resizeCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs);
static void inputCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs);
static void byeCB(Widget w,XtPointer client_data,XtPointer call_data);
static void aboutCB(Widget w,XtPointer client_data,XtPointer call_data);
static char* showvisualclass(int cls);



/* Sample application */
int main(int argc, char *argv[]){
  char *thevisual;
  XVisualInfo vi;
  int nvisinfos,visid,n;
  Arg args[30];
  Widget pane,cascade,but;

  /*
  ** Initialize toolkit
  ** We do *not* use XtAppInitialize as we want to figure visual and
  ** colormap BEFORE we create the top level shell!!!
  */
  XtToolkitInitialize();

  /* Make application context */
  app_context=XtCreateApplicationContext();

  /* Try open display */
  display=XtOpenDisplay(app_context,NULL,"boilerPlate","BoilerPlate",NULL,0,&argc,argv);

  /* Report failure */
  if(!display){
    fprintf(stderr,"Unable to open the specified display.\n");
    fprintf(stderr,"Set your `DISPLAY' environment variable properly or\n");
    fprintf(stderr,"use the `xhost' command to authorize access to the display.\n");
    exit(1);
    }

  /* Check for extension; for Mesa, this is always cool */
  if(!glXQueryExtension(display,NULL,NULL)){
    fprintf(stderr,"The specified display does not support the OpenGL extension\n");
    exit(1);
    }

  /* Init with default visual and colormap */
  gui_visual=DefaultVisual(display,0);
  gui_colormap=DefaultColormap(display,0);
  gl_colormap=DefaultColormap(display,0);

  /* User insists on a specific visual */
  if((thevisual=getenv("SETVISUAL"))!=NULL){
    if(sscanf(thevisual,"%x",&visid)==1){
      vi.visualid=visid;
      gl_visualinfo=XGetVisualInfo(display,VisualIDMask,&vi,&nvisinfos);
      }
    else{
      fprintf(stderr,"Please set the `SETVISUAL' variable in hexadecimal\n");
      fprintf(stderr,"Use one of the Visual ID's reported by `xdpyinfo'\n");
      exit(1);
      }
    }

  /* Find visual the regular way */
  else{
    gl_visualinfo=glXChooseVisual(display,DefaultScreen(display),glxConfig);
    }

  /* Make sure we have a visual */
  if(!gl_visualinfo){
    fprintf(stderr,"Unable to obtain visual for graphics\n");
    exit(1);
    }

  /* Show what visual is being used */
  fprintf(stderr,"Using the following visual:\n");
  fprintf(stderr,"  visualid: %lx\n",gl_visualinfo->visualid);
  fprintf(stderr,"     depth: %d\n",gl_visualinfo->depth);
  fprintf(stderr,"    screen: %d\n",gl_visualinfo->screen);
  fprintf(stderr,"  bits/rgb: %d\n",gl_visualinfo->bits_per_rgb);
  fprintf(stderr,"     class: %s\n",showvisualclass(gl_visualinfo->c_class));

  /*
  ** If not using default visual, we need a colormap for this visual.
  ** Yes, the GL widget can allocate one itself, but we want to make
  ** sure the GUI and the 3D have the same one (if hardware does not
  ** allow more than one simultaneously).
  ** This prevents nasty flashing when the window with the 3D widget
  ** looses the focus.
  */
  if(gl_visualinfo->visual!=DefaultVisual(display,0)){
    fprintf(stderr,"Making another colormap\n");
    gl_colormap=XCreateColormap(display,
                                RootWindow(display,0),
                                gl_visualinfo->visual,
                                AllocNone);
    if(!gl_colormap){
      fprintf(stderr,"Unable to create private colormap\n");
      exit(1);
      }
    }

  /*
  ** Use common visual for GUI and GL?
  ** Maybe you can invoke some hardware interrogation function and
  ** see if more than one hardware map is supported.  For the purpose
  ** of this demo, we'll use an environment variable instead.
  */
  if(getenv("SAMEVISUAL")!=NULL){
    gui_visual=gl_visualinfo->visual;
    gui_colormap=gl_colormap;
    }

  fprintf(stderr,"GUI uses visual: %lx\n",XVisualIDFromVisual(gui_visual));

  /* Create application shell, finally */
  n=0;
  XtSetArg(args[n],XmNvisual,gui_visual); n++;  	/* Plug in that visual */
  XtSetArg(args[n],XmNcolormap,gui_colormap); n++;	/* And that colormap */
  toplevel=XtAppCreateShell("boilerPlate","BoilerPlate",
                            applicationShellWidgetClass,display,args,n);


  /* Main window */
  n=0;
  mainwindow=XmCreateMainWindow(toplevel,"window",args,n);
  XtManageChild(mainwindow);

  /* Make a menu */
  n=0;
  XtSetArg(args[n],XmNmarginWidth,0); n++;
  XtSetArg(args[n],XmNmarginHeight,0); n++;
  menubar=XmCreateMenuBar(mainwindow,"menubar",args,2);
  XtManageChild(menubar);

  n=0;
  pane=XmCreatePulldownMenu(menubar,"pane",args,n);
  n=0;
  but=XmCreatePushButton(pane,"Open",args,n);
  XtManageChild(but);
  but=XmCreatePushButton(pane,"Save",args,n);
  XtManageChild(but);
  but=XmCreatePushButton(pane,"Save As",args,n);
  XtManageChild(but);
  but=XmCreatePushButton(pane,"Quit",args,n);
  XtAddCallback(but,XmNactivateCallback,byeCB,(XtPointer)NULL);
  XtManageChild(but);
  XtSetArg(args[0],XmNsubMenuId,pane);
  cascade=XmCreateCascadeButton(menubar,"File",args,1);
  XtManageChild(cascade);

  n=0;
  pane=XmCreatePulldownMenu(menubar,"pane",args,n);
  n=0;
  but=XmCreatePushButton(pane,"About",args,n);
  XtAddCallback(but,XmNactivateCallback,aboutCB,(XtPointer)NULL);
  XtManageChild(but);
  XtSetArg(args[0],XmNsubMenuId,pane);
  cascade=XmCreateCascadeButton(menubar,"Help",args,1);
  XtManageChild(cascade);
  XtVaSetValues(menubar,XmNmenuHelpWidget,cascade,NULL);

  /* Main window form */
  n=0;
  XtSetArg(args[n],XmNmarginWidth,5); n++;
  XtSetArg(args[n],XmNmarginHeight,5); n++;
  mainform=XmCreateForm(mainwindow,"mainForm",args,n);
  XtManageChild(mainform);

  /* Some nice button */
  n=0;
  XtSetArg(args[n],XmNbottomAttachment,XmATTACH_FORM); n++;
  XtSetArg(args[n],XmNrightAttachment,XmATTACH_FORM); n++;
  button=XmCreatePushButton(mainform,"Bye",args,n);
  XtAddCallback(button,XmNactivateCallback,byeCB,(XtPointer)NULL);
  XtManageChild(button);

  n=0;
  XtSetArg(args[n],XmNleftAttachment,XmATTACH_FORM); n++;
  XtSetArg(args[n],XmNrightAttachment,XmATTACH_FORM); n++;
  XtSetArg(args[n],XmNbottomAttachment,XmATTACH_WIDGET); n++;
  XtSetArg(args[n],XmNbottomWidget,button); n++;
  XtSetArg(args[n],XmNshadowType,XmSHADOW_ETCHED_IN); n++;
  separator=XmCreateSeparator(mainform,"separator",args,n);
  XtManageChild(separator);

  /* Main window frame */
  n = 0;
  XtSetArg(args[n],XmNleftAttachment,XmATTACH_FORM); n++;
  XtSetArg(args[n],XmNrightAttachment,XmATTACH_FORM); n++;
  XtSetArg(args[n],XmNtopAttachment,XmATTACH_FORM); n++;
  XtSetArg(args[n],XmNbottomAttachment,XmATTACH_WIDGET); n++;
  XtSetArg(args[n],XmNbottomWidget,separator); n++;
  XtSetArg(args[n],XmNshadowType,XmSHADOW_IN); n++;
  mainframe = XmCreateFrame(mainform,"mainFrame",args,n);
  XtManageChild(mainframe);

  /* GL drawing area */
  n = 0;
  XtSetArg(args[n],XmNcolormap,gl_colormap); n++;
  XtSetArg(args[n],GLwNvisualInfo,gl_visualinfo); n++;
  XtSetArg(args[n],GLwNinstallColormap,True); n++;
  XtSetArg(args[n],XmNtraversalOn,True); n++;
  XtSetArg(args[n],XmNwidth,400); n++;
  XtSetArg(args[n],XmNheight,300); n++;
  glwidget = GLwCreateMDrawingArea(mainframe,"glWidget",args,n);
  XtAddCallback(glwidget,GLwNexposeCallback,(XtCallbackProc)exposeCB,(XtPointer)NULL);
  XtAddCallback(glwidget,GLwNresizeCallback,(XtCallbackProc)resizeCB,(XtPointer)NULL);
  XtAddCallback(glwidget,GLwNginitCallback,(XtCallbackProc)initCB,(XtPointer)NULL);
  XtAddCallback(glwidget,GLwNinputCallback,(XtCallbackProc)inputCB,(XtPointer)NULL);
  XtManageChild(glwidget);

  /* Set into main window */
  XmMainWindowSetAreas(mainwindow,menubar,NULL,NULL,NULL,mainform);
  XtRealizeWidget(toplevel);

  /* Loop until were done */
  XtAppMainLoop(app_context);
  return 0;
  }


/* Show visual class */
static char* showvisualclass(int cls){
  if(cls==TrueColor) return "TrueColor";
  if(cls==DirectColor) return "DirectColor";
  if(cls==PseudoColor) return "PseudoColor";
  if(cls==StaticColor) return "StaticColor";
  if(cls==GrayScale) return "GrayScale";
  if(cls==StaticGray) return "StaticGray";
  return "Unknown";
  }


static void exposeCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs){
  GLwDrawingAreaMakeCurrent(glwidget,glx_context);

  glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0,1.0,0.0,1.0);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();

  glColor3f(1.0,0.0,0.0);
  glBegin(GL_LINE_STRIP);
  glVertex3f(-1.0,-1.0,0.0);
  glVertex3f( 1.0, 1.0,0.0);
  glEnd();
  glXSwapBuffers(display,XtWindow(glwidget));
  }


/* Initialize widget */
static void initCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs){

  /* First, create context. We prefer direct rendering */
  glx_context=glXCreateContext(display,gl_visualinfo,0,TRUE);
  if(!glx_context){
    fprintf(stderr,"Unable to create gl context\n");
    exit(1);
    }

  /* Make it current */
  GLwDrawingAreaMakeCurrent(glwidget,glx_context);

  /* Set a viewport */
  glViewport(0,0,cbs->width,cbs->height);

  /* You might want to do a lot more here ... */
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  glClearColor(1.0,1.0,1.0,1.0);
  glClearDepth(1.0);


  }


/* Widget changed size, so adjust txform */
static void resizeCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs){
  GLwDrawingAreaMakeCurrent(glwidget,glx_context);
  glViewport(0,0,cbs->width,cbs->height);

  /* blablabla */
  }


/* Boilerplate event handling */
static void inputCB(Widget w,XtPointer client_data,GLwDrawingAreaCallbackStruct *cbs){
  switch(cbs->event->type){
    case ButtonPress:
      switch(cbs->event->xbutton.button){
        case Button1: fprintf(stderr,"Pressed 1\n"); break;
        case Button2: fprintf(stderr,"Pressed 2\n"); break;
        case Button3: fprintf(stderr,"Pressed 3\n"); break;
        }
      break;
    case ButtonRelease:
      switch(cbs->event->xbutton.button){
        case Button1: fprintf(stderr,"Released 1\n"); break;
        case Button2: fprintf(stderr,"Released 2\n"); break;
        case Button3: fprintf(stderr,"Released 3\n"); break;
        }
      break;
    case MotionNotify:
      fprintf(stderr,"Moved mouse to (%d %d)\n",
              cbs->event->xbutton.x,
              cbs->event->xbutton.y);
      break;
    }
  }


/* Hasta la vista, baby */
static void byeCB(Widget w,XtPointer client_data,XtPointer call_data){
  exit(0);
  }


/* Pop informative panel */
static void aboutCB(Widget w,XtPointer client_data,XtPointer call_data){
  Arg args[10];
  XmString str;
  Widget box;
  str=XmStringCreateLtoR("Boilerplate Mixed Model Programming Example\n\n   (C) 1996 Jeroen van der Zijp \n\n    jvz@cyberia.cfdrc.com",XmSTRING_DEFAULT_CHARSET);
  XtSetArg(args[0],XmNnoResize,True);
  XtSetArg(args[1],XmNautoUnmanage,True);
  XtSetArg(args[2],XmNmessageString,str);
  XtSetArg(args[3],XmNdefaultPosition,False);
  box=XmCreateInformationDialog(toplevel,"About Boilerplate",args,4);
  XtManageChild(box);
  XtUnmanageChild(XmMessageBoxGetChild(box,XmDIALOG_HELP_BUTTON));
  XtUnmanageChild(XmMessageBoxGetChild(box,XmDIALOG_CANCEL_BUTTON));
  XmStringFree(str);
  }