Click here to Skip to main content
14,271,874 members

Writing a Platform and GUI Toolkit Independent OpenGL Class

Rate this:
4.95 (30 votes)
Please Sign up or sign in to vote.
4.95 (30 votes)
1 Nov 2010CPOL
Article showing how to write OS and GUI toolkit portable OpenGL view class
basecode
linux
MultiGL
GLView
GLViewGTKApp
.project
AUTHORS
autom4te.cache
output.0
requests
traces.0
ChangeLog
config.guess
config.h.in
config.sub
configure
configure.in
COPYING
depcomp
glviewgtkapp.gladep
INSTALL
install-sh
Makefile.am
Makefile.in
missing
mkinstalldirs
NEWS
po
ChangeLog
Makefile.in.in
POTFILES.in
README
src
Makefile.am
Makefile.in
stamp-h.in
GLViewMotifApp
.project
autom4te.cache
output.0
requests
traces.0
config.h.in
configure
configure.ac
depcomp
install-sh
Makefile.am
Makefile.in
missing
src
Makefile.am
Makefile.in
GLViewQtApp
.project
GLViewQtApp.ico
GLViewQtApp.pro.user
glviewqtapp.qrc
Makefile
Resources
.project
autom4te.cache
output.0
requests
traces.0
config.guess
config.h.in
config.sub
configure
configure.ac
depcomp
install-sh
m4
Makefile.am
Makefile.in
missing
src
Makefile.am
Makefile.in
win32
MultiGL
GLView
GLViewGTKApp
AUTHORS
ChangeLog
configure.in
devcpp
glviewgtkapp.dev
glviewgtkapp.gladep
Makefile.am
msvc
glviewgtkapp.suo
glviewgtkapp.vcproj
NEWS
po
ChangeLog
POTFILES.in
README
src
Makefile.am
stamp-h.in
GLViewMFCDlgApp
GLViewMFCDlgApp.aps
GLViewMFCDlgApp.vcproj
res
GLViewMFCDlgApp.ico
GLViewMFCMDIApp
GLViewMFCMDIApp.vcproj
res
buttons.bmp
filelarge.bmp
filesmall.bmp
GLViewMFCMDIApp.ico
GLViewMFCMDIAppDoc.ico
main.bmp
Toolbar.bmp
Toolbar256.bmp
writelarge.bmp
writesmall.bmp
GLViewQtApp
GLViewQtApp
GLViewQtApp.ico
glviewqtapp.qrc
GLViewQtApp.vcproj
Makefile
moc_glviewqtapp.o
Resources
GLView.cpp.bak
GLView.suo
GLView.vcproj
MultiGL.suo
/*
 * =====================================================================================
 *
 *       Filename:  glviewmotifapp.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  10/17/2010 03:44:27 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Sharjith N. (SSN), sharjith@gmail.com
 *        Company:  Sharjith's Inc.
 *
 * =====================================================================================
 */

#include <X11/Intrinsic.h>
#include <X11/Xutil.h>

#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/Form.h>
#include <Xm/DrawingA.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/Label.h>

#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;

#include <GLView.h>

#define WIDTH    900
#define HEIGHT   700
#define MENU_QUIT       1
#define MENU_ABOUT		2

Widget toplevel, drawing_area, message_area;
static CGLView* pGLView = NULL;
Widget quit_option, about_option;

void show_about(Widget parent)
{
	Widget           dialog;
	Arg              arg[5];
	XmString         xms;
	int              n = 0;
#ifdef DEBUG
	cout << "show_about" << endl;
#endif

	xms = XmStringCreateLocalized ("GLView Test Motif Application");
	XtSetArg (arg[n], XmNmessageString, xms); n++;
	dialog = XmCreateInformationDialog (parent, "About", arg, n);
	XtVaSetValues (dialog, XmNdialogStyle, XmDIALOG_SYSTEM_MODAL,     
			NULL);

	XmStringFree (xms);
	XtUnmanageChild (XtNameToWidget (dialog, "Cancel"));
	XtManageChild (dialog);
}

void quitCB(Widget w,
		XtPointer client_data,
		XtPointer call_data)
{
	if(pGLView)
	{
		delete pGLView;
	}
	exit(0);
}

void quit_app(Widget parent)
{
	Widget         dialog;
	XmString       msg, yes, no;
	extern void    my_callback(Widget, XtPointer, XtPointer);

	dialog = XmCreateQuestionDialog (parent, "Question", NULL, 0);
	yes = XmStringCreateLocalized ("Yes");
	no = XmStringCreateLocalized ("No");
	msg = XmStringCreateLocalized ("Do you want to quit?");
	XtVaSetValues (dialog, XmNmessageString, msg, XmNokLabelString, yes,
			XmNcancelLabelString, no, NULL);
	XtAddCallback (dialog, XmNokCallback, quitCB, NULL);

	XmStringFree (yes);
	XmStringFree (no);
	XmStringFree (msg);
	XtUnmanageChild (XtNameToWidget (dialog, "Help"));
	XtManageChild (dialog);
}

void exposeCB(Widget w,
		int client_data,
		XmDrawingAreaCallbackStruct *cd)
{
#ifdef DEBUG
	cout << "exposeCB" << endl;
#endif
	if(pGLView != NULL)
	{
		pGLView->RenderScene();
	}

}

void resizeCB(Widget w,
		int client_data,
		XmDrawingAreaCallbackStruct *cd)
{
	Arg args[5];
	int n = 0;
	Dimension width, height;
	XtSetArg (args[n], XmNwidth, &width);  n++;
	XtSetArg (args[n], XmNheight, &height);  n++;
	XtGetValues (w, args, n);
#ifdef DEBUG
	cout << "resizeCB width=" << width << " height=" << height << endl;
#endif
	if(pGLView != NULL)
	{
		pGLView->Resize(width, height);
	}
}

void menuCB(Widget w,
		int client_data,
		XmAnyCallbackStruct *call_data)
/* handles menu options. */
{
	switch (client_data)
	{
		case MENU_QUIT:
			quit_app(drawing_area);
			break;
		case MENU_ABOUT:
			show_about(drawing_area);
			break;
	}
}

Widget make_menu_option(char *option_name,int client_data,Widget menu)
{
	int ac;
	Arg al[10];
	Widget b;

	ac = 0;
	XtSetArg(al[ac], XmNlabelString,
			XmStringCreateLtoR(option_name,
				XmSTRING_DEFAULT_CHARSET)); ac++;
	b=XtCreateManagedWidget(option_name,xmPushButtonWidgetClass,
			menu,al,ac);
	XtAddCallback (b, XmNactivateCallback, (XtCallbackProc)menuCB, (XtPointer)client_data);
	return(b);
}

Widget make_menu(char *menu_name, Widget menu_bar)
{
	int ac;
	Arg al[10];
	Widget menu, cascade;

	ac = 0;
	menu = XmCreatePulldownMenu (menu_bar, menu_name, al, ac);

	ac = 0;
	XtSetArg (al[ac], XmNsubMenuId, menu);  ac++;
	XtSetArg(al[ac], XmNlabelString,
			XmStringCreateLtoR(menu_name, XmSTRING_DEFAULT_CHARSET)); ac++;
	cascade = XmCreateCascadeButton (menu_bar, menu_name, al, ac);
	XtManageChild (cascade); 

	return(menu);
}

void create_menus(Widget menu_bar)
{
	int ac;
	Arg al[10];
	Widget menu;

	menu=make_menu("File",menu_bar);
	quit_option = make_menu_option("Quit",MENU_QUIT,menu);

	menu=make_menu("Help",menu_bar);
	about_option = make_menu_option("About",MENU_ABOUT,menu);

	/* Tell the menubar which button is the help menu  */
    if (menu = XtNameToWidget (menu_bar, "Help"))
        XtVaSetValues (menu_bar, XmNmenuHelpWidget, menu, NULL);
}

int main(int argc, char *argv[])
{
	Widget toplevel, main_w, form, label, menu_bar, command_w;
	Arg al[10];
	int ac;

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

	/* default window size. */
	ac=0;
	XtSetArg(al[ac],XmNheight, HEIGHT); ac++;
	XtSetArg(al[ac],XmNwidth, WIDTH); ac++;
	XtSetArg(al[ac], XmNcommandWindowLocation,
			XmCOMMAND_BELOW_WORKSPACE); ac++;
	XtSetValues(toplevel,al,ac);

	main_w = XmCreateMainWindow (toplevel, "main_w", al, ac);
	/* create a form widget. */
	ac=0;
	form=XtCreateManagedWidget("form",xmFormWidgetClass,
			main_w,al,ac);

	/* create a menu bar and attach it to the form. */
	ac=0;
	XtSetArg(al[ac], XmNtopAttachment,    XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNleftAttachment,   XmATTACH_FORM); ac++;
	menu_bar=XmCreateMenuBar(form,"menu_bar",al,ac);

	XtManageChild(menu_bar);

	create_menus(menu_bar);


	/* create a drawing area widget. */
	ac=0;

	XtSetArg(al[ac], XmNtopAttachment,    XmATTACH_WIDGET); ac++;
	XtSetArg(al[ac], XmNtopWidget, menu_bar); ac++;
	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNleftAttachment,   XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNbottomAttachment,   XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNeditMode,XmMULTI_LINE_EDIT); ac++;
	drawing_area=XtCreateManagedWidget("drawing_area", xmDrawingAreaWidgetClass, form,al,ac);

	XtAddCallback (drawing_area, XmNexposeCallback,
			(XtCallbackProc)exposeCB, NULL);
	XtAddCallback (drawing_area, XmNresizeCallback,
			(XtCallbackProc)resizeCB, NULL);


	/* message  area */
    message_area = XtVaCreateManagedWidget ("Ready",
	xmLabelWidgetClass, main_w,
	XmNalignment,   XmALIGNMENT_BEGINNING,
	NULL);

	XtManageChild (main_w);
	XtRealizeWidget(toplevel);

	pGLView = new CGLView();
	if(pGLView == NULL)
	{
		cout << "Failed to create CGLView!" << endl;
		exit(0);
	}
	else
	{
		Arg args[5];
		int n = 0;
		Dimension width, height;
		XtSetArg (args[n], XmNwidth, &width);  n++;
		XtSetArg (args[n], XmNheight, &height);  n++;
		XtGetValues (drawing_area, args, n);
		pGLView->SetWindow(XtDisplay(drawing_area), XtWindow(drawing_area));
		pGLView->SetupGLContext(true);
		pGLView->Resize(width, height);
	}


	XtMainLoop();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sharjith
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Stats

73.2K views
6.9K downloads
88 bookmarked