1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 #include "glutrender.h"
15 #include "viewport.h"
16 #include "arcball.h"
17 #include "appmouse.h"
18 #include "glexpose.h"
19 #include "glcompui.h"
20 
21     /*call backs */
22 
23 static float begin_x = 0.0;
24 static float begin_y = 0.0;
25 static float dx = 0.0;
26 static float dy = 0.0;
27 
28 /*mouse mode mapping funvtion from glut to glcomp*/
getGlCompMouseType(int n)29 static glMouseButtonType getGlCompMouseType(int n)
30 {
31     switch (n) {
32     case GLUT_LEFT_BUTTON:
33 	return glMouseLeftButton;
34     case GLUT_RIGHT_BUTTON:
35 	return glMouseMiddleButton;
36     case GLUT_MIDDLE_BUTTON:
37 	return glMouseRightButton;
38 
39     default:
40 	return glMouseLeftButton;
41     }
42 }
43 
44 
cb_reshape(int width,int height)45 void cb_reshape(int width, int height)
46 {
47     /* static int doonce=0; */
48     int vPort[4];
49     float aspect;
50     view->w = width;
51     view->h = height;
52     if (view->widgets)
53 	glcompsetUpdateBorder(view->widgets, view->w, view->h);
54     glViewport(0, 0, view->w, view->h);
55     /* get current viewport */
56     glGetIntegerv(GL_VIEWPORT, vPort);
57     /* setup various opengl things that we need */
58     glMatrixMode(GL_PROJECTION);
59     glLoadIdentity();
60     init_arcBall(view->arcball, (GLfloat) view->w, (GLfloat) view->h);
61     if (view->w > view->h) {
62 	aspect = (float) view->w / (float) view->h;
63 	glOrtho(-aspect * GL_VIEWPORT_FACTOR, aspect * GL_VIEWPORT_FACTOR,
64 		GL_VIEWPORT_FACTOR * -1, GL_VIEWPORT_FACTOR, -1500, 1500);
65     } else {
66 	aspect = (float) view->h / (float) view->w;
67 	glOrtho(GL_VIEWPORT_FACTOR * -1, GL_VIEWPORT_FACTOR,
68 		-aspect * GL_VIEWPORT_FACTOR, aspect * GL_VIEWPORT_FACTOR,
69 		-1500, 1500);
70     }
71 
72     glMatrixMode(GL_MODELVIEW);
73     glLoadIdentity();
74 	/*** OpenGL END ***/
75 
76 }
cb_display(void)77 void cb_display(void )
78 {
79 //    glClearColor(view->bgColor.R, view->bgColor.G, view->bgColor.B, view->bgColor.A);
80     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
81     glLoadIdentity();
82     glexpose_main(view);	//draw all stuff
83 //    draw_cube_tex();
84     glutSwapBuffers();
85     if (view->initFile) {
86 	view->initFile = 0;
87 	if (view->activeGraph == 0)
88 	    close_graph(view, 0);
89 	add_graph_to_viewport_from_file(view->initFileName);
90     }
91 
92 }
cb_mouseclick(int button,int state,int x,int y)93 void cb_mouseclick(int button, int state,int x, int y)
94 {
95     Agraph_t* g;
96 
97     if (view->g == 0) return;
98     g=view->g[view->activeGraph];
99     begin_x = (float) x;
100     begin_y = (float) y;
101     if(state==GLUT_DOWN)
102     {
103 	view->widgets->common.functions.mousedown((glCompObj*)view->widgets,(GLfloat) x,(GLfloat)y,getGlCompMouseType(button));
104 	if (button ==  GLUT_LEFT_BUTTON)
105 	    appmouse_left_click_down(view,x,y);
106         if (button ==  GLUT_RIGHT_BUTTON)
107 	    appmouse_right_click_down(view,x,y);
108 	if (button ==  GLUT_MIDDLE_BUTTON)
109 	    appmouse_middle_click_down(view,x,y);
110     }
111     else
112     {
113 	view->FontSizeConst = GetOGLDistance(14);
114 	view->arcball->isDragging = 0;
115 	view->widgets->common.functions.mouseup((glCompObj*)view->widgets,(GLfloat) x,(GLfloat) y,getGlCompMouseType(button));
116 
117 	if (button ==  GLUT_LEFT_BUTTON)
118 	    appmouse_left_click_up(view,x,y);
119 	if (button ==  GLUT_LEFT_BUTTON)
120 	    appmouse_right_click_up(view,x,y);
121 	if (button ==  GLUT_MIDDLE_BUTTON)
122 	    appmouse_middle_click_up(view,x,y);
123 	dx = 0.0;
124 	dy = 0.0;
125     }
126     cb_display();
127 
128 }
cb_mouseover(int x,int y)129 void cb_mouseover(int x,int y)/*no mouse click only mouse pointer moving on context*/
130 {
131 
132 
133 
134 
135 
136 
137 
138 
139 }
cb_drag(int X,int Y)140 void cb_drag(int X,int Y)/*mouse moving witha button clicked (dragging)*/
141 {
142 
143     float x = (float) X;
144     float y = (float) Y;
145 
146     if (view->widgets)
147 	view->widgets->common.functions.mouseover((glCompObj*)view->widgets, (GLfloat) x,(GLfloat) y);
148 
149     dx = x - begin_x;
150     dy = y - begin_y;
151     view->mouse.dragX = dx;
152     view->mouse.dragY = dy;
153     appmouse_move(view,x,y);
154 
155     if((view->mouse.t==glMouseLeftButton) && (view->mouse.down)  )
156 	appmouse_left_drag(view,x,y);
157     if((view->mouse.t==glMouseRightButton) && (view->mouse.down))
158 	appmouse_right_drag(view,x,y);
159     if((view->mouse.t==glMouseMiddleButton) && (view->mouse.down))
160 	appmouse_middle_drag(view,x,y);
161     begin_x = x;
162     begin_y = y;
163     cb_display();
164 
165 }
cb_mouseentry(int state)166 void cb_mouseentry(int state)
167 {
168     if(state==GLUT_LEFT)
169     {
170 	//TODO when mouse leaves the scene (which might be impossible in full screen modes with one monitor
171     }
172     else // GLUT_ENTERED
173     {
174 	//TODO mouse is back in scene
175     }
176 
177 }
cb_keyboard(unsigned char key,int x,int y)178 void cb_keyboard(unsigned char key,int x, int y)
179 {
180     if (key==27)    /*ESC*/
181 	exit (1);
182     if(key=='3')
183 	switch2D3D(NULL, 0, 0,glMouseLeftButton);
184     if(key=='c')
185         menu_click_center(NULL, 0, 0,glMouseLeftButton);
186 
187     if(key=='+')
188         menu_click_zoom_plus(NULL, 0, 0,glMouseLeftButton);
189     if(key=='-')
190         menu_click_zoom_minus(NULL, 0, 0,glMouseLeftButton);
191     if(key=='p')
192         menu_click_pan(NULL, 0, 0,glMouseLeftButton);
193 
194 
195     appmouse_key_press(view,key);
196 }
cb_keyboard_up(unsigned char key,int x,int y)197 void cb_keyboard_up(unsigned char key,int x, int y)
198 {
199     appmouse_key_release(view,key);;
200 }
201 
202 
cb_special_key(int key,int x,int y)203 void cb_special_key(int key, int x, int y)
204 {
205     if(key==GLUT_KEY_F1)
206     {
207 	printf("Currently help is not available\n");
208     }
209     appmouse_key_press(view,key);
210 
211 }
cb_special_key_up(int key,int x,int y)212 void cb_special_key_up(int key, int x, int y)
213 {
214     if(key==GLUT_KEY_F1)
215     {
216 	printf("Currently help is not available\n");
217     }
218     appmouse_key_release(view,key);
219 
220 }
221 
cb_game_mode(char * optArg)222 static int cb_game_mode(char* optArg)
223 {
224 
225     glutGameModeString(optArg);
226     if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
227     {
228     	glutEnterGameMode();
229 	return 1;
230 
231     }
232     else
233     {
234 	printf("smyrna cannot initialize requested screen resolution and rate!\n");
235 	exit(-1);
236 
237     }
238 
239 }
cb_windowed_mode(int w,int h)240 static int cb_windowed_mode(int w,int h)
241 {
242   glutInitWindowSize(w, h);
243   glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_ACCUM | GLUT_DOUBLE);
244   glutCreateWindow("smyrna");
245   return 1;
246 
247 }
248 
cb_glutinit(int x,int y,int w,int h,int bits,int s_rate,int fullscreen,int * argcp,char * argv[],char * optArg)249 int cb_glutinit(int x,int y,int w,int h, int bits,int s_rate,int fullscreen,int* argcp, char *argv[],char* optArg)
250 {
251     /*
252     x,y:window position , unless in full screen mode
253     w,h: width and height of the window in pixels
254     bits: display color bit count
255     s_rate: for full screen mode this value represents refresh rate in hertz
256     fullscreen: if it will be a fullscreen window,
257     argcp argv: main function's parameters, required for glutinit
258     */
259 
260 
261 
262     glutInit(argcp,argv); //this is required by some OS.
263 
264 //    glutInitDisplayMode( GLUT_ALPHA | GLUT_DOUBLE | GLUT_RGBA);
265 	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
266 	// The Type Of Depth Testing To Do
267 /*	glEnable(GL_BLEND);
268 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
269 	glShadeModel(GL_SMOOTH);*/
270         glDisable(GL_DEPTH);
271 	glClearDepth(1.0f);		// Depth Buffer Setup
272 	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
273 	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To
274 	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
275 
276 
277     if (fullscreen)
278 	cb_game_mode(optArg);
279     else    //well, use gtk then
280     {
281 	cb_windowed_mode(w,h);
282 
283     }
284 
285     /*register callbacks here*/
286     glutDisplayFunc(cb_display);
287     glutReshapeFunc(cb_reshape);
288     glutKeyboardFunc(cb_keyboard);
289     glutKeyboardUpFunc( cb_keyboard_up);
290     glutMouseFunc(cb_mouseclick);
291     glutMotionFunc(cb_drag);
292     glutPassiveMotionFunc(cb_mouseover);
293     glutVisibilityFunc(NULL);
294     glutEntryFunc(cb_mouseentry);//if mouse pointer left or entered the scene
295     glutSpecialFunc(cb_special_key);
296     glutSpecialUpFunc(cb_special_key_up);
297 
298     //Attach extra call backs if needed in the future
299 /*  glutOverlayDisplayFunc
300     glutSpaceballMotionFunc
301     glutSpaceballRotateFunc
302     glutSpaceballButtonFunc
303     glutButtonBoxFunc
304     glutDialsFunc
305     glutTabletMotionFunc
306     glutTabletButtonFunc
307     glutMenuStatusFunc
308     glutIdleFunc
309     glutTimerFunc
310 */
311 
312 
313     //pass control to glut
314     cb_reshape(w,h);
315     glutMainLoop ();
316 
317 
318     return 0; //we should never reach here
319 
320 }
321 
322 
323