| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include "glutrender.h" |
| 14 |
#include "viewport.h" |
| 15 |
#include "arcball.h" |
| 16 |
#include "gui/appmouse.h" |
| 17 |
#include "glexpose.h" |
| 18 |
#include "gui/glcompui.h" |
| 19 |
#include <util/exit.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*/ |
| 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 |
|
| 45 |
static void cb_reshape(int width, int height) |
| 46 |
{ |
| 47 |
float aspect; |
| 48 |
view->w = width; |
| 49 |
view->h = height; |
| 50 |
if (view->widgets) |
| 51 |
glcompsetUpdateBorder(view->widgets, view->w, view->h); |
| 52 |
glViewport(0, 0, view->w, view->h); |
| 53 |
/* setup various opengl things that we need */ |
| 54 |
glMatrixMode(GL_PROJECTION); |
| 55 |
glLoadIdentity(); |
| 56 |
*view->arcball = init_arcBall(view->w, view->h); |
| 57 |
if (view->w > view->h) { |
| 58 |
aspect = (float) view->w / (float) view->h; |
| 59 |
glOrtho(-aspect * GL_VIEWPORT_FACTOR, aspect * GL_VIEWPORT_FACTOR, |
| 60 |
GL_VIEWPORT_FACTOR * -1, GL_VIEWPORT_FACTOR, -1500, 1500); |
| 61 |
} else { |
| 62 |
aspect = (float) view->h / (float) view->w; |
| 63 |
glOrtho(GL_VIEWPORT_FACTOR * -1, GL_VIEWPORT_FACTOR, |
| 64 |
-aspect * GL_VIEWPORT_FACTOR, aspect * GL_VIEWPORT_FACTOR, |
| 65 |
-1500, 1500); |
| 66 |
} |
| 67 |
|
| 68 |
glMatrixMode(GL_MODELVIEW); |
| 69 |
glLoadIdentity(); |
| 70 |
/*** OpenGL END ***/ |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
static void cb_display(void) |
| 75 |
{ |
| 76 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 77 |
glLoadIdentity(); |
| 78 |
glexpose_main(view); //draw all stuff |
| 79 |
glutSwapBuffers(); |
| 80 |
if (view->initFile) { |
| 81 |
view->initFile = 0; |
| 82 |
if (view->activeGraph == 0) |
| 83 |
close_graph(view); |
| 84 |
add_graph_to_viewport_from_file(view->initFileName); |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
static void cb_mouseclick(int button, int state, int x, int y) |
| 90 |
{ |
| 91 |
if (view->g == 0) return; |
| 92 |
begin_x = (float) x; |
| 93 |
begin_y = (float) y; |
| 94 |
if(state==GLUT_DOWN) |
| 95 |
{ |
| 96 |
view->widgets->base.common.functions.mousedown(&view->widgets->base, |
| 97 |
(float)x, (float)y, |
| 98 |
getGlCompMouseType(button)); |
| 99 |
if (button == GLUT_LEFT_BUTTON) |
| 100 |
appmouse_left_click_down(view,x,y); |
| 101 |
if (button == GLUT_RIGHT_BUTTON) |
| 102 |
appmouse_right_click_down(view,x,y); |
| 103 |
if (button == GLUT_MIDDLE_BUTTON) |
| 104 |
appmouse_middle_click_down(view,x,y); |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
view->arcball->isDragging = 0; |
| 109 |
view->widgets->base.common.functions.mouseup(&view->widgets->base, |
| 110 |
(float)x, (float)y, |
| 111 |
getGlCompMouseType(button)); |
| 112 |
|
| 113 |
if (button == GLUT_LEFT_BUTTON || button == GLUT_RIGHT_BUTTON || |
| 114 |
button == GLUT_MIDDLE_BUTTON) |
| 115 |
appmouse_up(view, x, y); |
| 116 |
dx = 0.0; |
| 117 |
dy = 0.0; |
| 118 |
} |
| 119 |
cb_display(); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
// mouse moving with a button clicked (dragging) |
| 124 |
static void cb_drag(int X, int Y) |
| 125 |
{ |
| 126 |
|
| 127 |
float x = (float) X; |
| 128 |
float y = (float) Y; |
| 129 |
|
| 130 |
if (view->widgets) |
| 131 |
view->widgets->base.common.functions.mouseover(&view->widgets->base, x, y); |
| 132 |
|
| 133 |
dx = x - begin_x; |
| 134 |
dy = y - begin_y; |
| 135 |
view->mouse.dragX = dx; |
| 136 |
view->mouse.dragY = dy; |
| 137 |
appmouse_move(view,x,y); |
| 138 |
|
| 139 |
if (view->mouse.down) |
| 140 |
appmouse_drag(view, x, y); |
| 141 |
begin_x = x; |
| 142 |
begin_y = y; |
| 143 |
cb_display(); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
static void cb_keyboard(unsigned char key, int x, int y) |
| 148 |
{ |
| 149 |
(void)x; |
| 150 |
(void)y; |
| 151 |
|
| 152 |
if (key==27) /*ESC*/ |
| 153 |
graphviz_exit(1); |
| 154 |
if(key=='3') |
| 155 |
switch2D3D(NULL, 0, 0,glMouseLeftButton); |
| 156 |
if(key=='c') |
| 157 |
menu_click_center(NULL, 0, 0,glMouseLeftButton); |
| 158 |
|
| 159 |
if(key=='+') |
| 160 |
menu_click_zoom_plus(NULL, 0, 0,glMouseLeftButton); |
| 161 |
if(key=='-') |
| 162 |
menu_click_zoom_minus(NULL, 0, 0,glMouseLeftButton); |
| 163 |
if(key=='p') |
| 164 |
menu_click_pan(NULL, 0, 0,glMouseLeftButton); |
| 165 |
|
| 166 |
|
| 167 |
appmouse_key_press(view,key); |
| 168 |
} |
| 169 |
|
| 170 |
static void cb_keyboard_up(unsigned char key, int x, int y) |
| 171 |
{ |
| 172 |
(void)key; |
| 173 |
(void)x; |
| 174 |
(void)y; |
| 175 |
|
| 176 |
appmouse_key_release(view); |
| 177 |
} |
| 178 |
|
| 179 |
static void cb_special_key(int key, int x, int y) |
| 180 |
{ |
| 181 |
(void)x; |
| 182 |
(void)y; |
| 183 |
|
| 184 |
if(key==GLUT_KEY_F1) |
| 185 |
{ |
| 186 |
printf("Currently help is not available\n"); |
| 187 |
} |
| 188 |
appmouse_key_press(view,key); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
static void cb_special_key_up(int key, int x, int y) |
| 193 |
{ |
| 194 |
(void)x; |
| 195 |
(void)y; |
| 196 |
|
| 197 |
if(key==GLUT_KEY_F1) |
| 198 |
{ |
| 199 |
printf("Currently help is not available\n"); |
| 200 |
} |
| 201 |
appmouse_key_release(view); |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
static void cb_game_mode(char* optArg) |
| 206 |
{ |
| 207 |
|
| 208 |
glutGameModeString(optArg); |
| 209 |
if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) |
| 210 |
{ |
| 211 |
glutEnterGameMode(); |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
printf("smyrna cannot initialize requested screen resolution and rate!\n"); |
| 216 |
graphviz_exit(-1); |
| 217 |
|
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
int cb_glutinit(int w, int h, int *argcp, char *argv[], char *optArg) { |
| 222 |
/* |
| 223 |
w,h: width and height of the window in pixels |
| 224 |
argcp argv: main function's parameters, required for glutinit |
| 225 |
*/ |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
glutInit(argcp,argv); //this is required by some OS. |
| 230 |
|
| 231 |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); |
| 232 |
// The Type Of Depth Testing To Do |
| 233 |
glDisable(GL_DEPTH); |
| 234 |
glClearDepth(1.0f); // Depth Buffer Setup |
| 235 |
glEnable(GL_DEPTH_TEST); // Enables Depth Testing |
| 236 |
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To |
| 237 |
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations |
| 238 |
|
| 239 |
|
| 240 |
cb_game_mode(optArg); |
| 241 |
|
| 242 |
/*register callbacks here*/ |
| 243 |
glutDisplayFunc(cb_display); |
| 244 |
glutReshapeFunc(cb_reshape); |
| 245 |
glutKeyboardFunc(cb_keyboard); |
| 246 |
glutKeyboardUpFunc( cb_keyboard_up); |
| 247 |
glutMouseFunc(cb_mouseclick); |
| 248 |
glutMotionFunc(cb_drag); |
| 249 |
glutPassiveMotionFunc(NULL); |
| 250 |
glutVisibilityFunc(NULL); |
| 251 |
glutEntryFunc(NULL); |
| 252 |
glutSpecialFunc(cb_special_key); |
| 253 |
glutSpecialUpFunc(cb_special_key_up); |
| 254 |
|
| 255 |
//Attach extra call backs if needed in the future |
| 256 |
/* glutOverlayDisplayFunc |
| 257 |
glutSpaceballMotionFunc |
| 258 |
glutSpaceballRotateFunc |
| 259 |
glutSpaceballButtonFunc |
| 260 |
glutButtonBoxFunc |
| 261 |
glutDialsFunc |
| 262 |
glutTabletMotionFunc |
| 263 |
glutTabletButtonFunc |
| 264 |
glutMenuStatusFunc |
| 265 |
glutIdleFunc |
| 266 |
glutTimerFunc |
| 267 |
*/ |
| 268 |
|
| 269 |
|
| 270 |
//pass control to glut |
| 271 |
cb_reshape(w,h); |
| 272 |
glutMainLoop (); |
| 273 |
|
| 274 |
|
| 275 |
return 0; //we should never reach here |
| 276 |
|
| 277 |
} |