-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
293 lines (246 loc) · 9.26 KB
/
window.cpp
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
/*
* graphic depictions, a visual workbench for graphs
*
* Copyright (C) 2016 Matvey Soloviev
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <GL/glxew.h>
#include "stdafx.h"
#include "window.h"
#include "imgui/imgui.h"
CSMainWindow *wnd;
#define None 0L
void CSMainWindow::Create()
{
wnd=this;
XInitThreads();
XSetWindowAttributes windowAttributes;
XEvent event;
Colormap colorMap;
int errorBase;
int eventBase;
// Open a connection to the X server
g_pDisplay = XOpenDisplay( NULL );
if( g_pDisplay == NULL )
{
fprintf(stderr, "glxsimple: %s\n", "could not open display");
exit(1);
}
// Make sure OpenGL's GLX extension supported
if( !glXQueryExtension( g_pDisplay, &errorBase, &eventBase ) )
{
fprintf(stderr, "glxsimple: %s\n", "X server has no OpenGL GLX extension");
exit(1);
}
// Find an appropriate visual
int doubleBufferVisual[] =
{
GLX_RGBA, // Needs to support OpenGL
GLX_DEPTH_SIZE, 1, // Needs to support a 16 bit depth buffer
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
//GLX_ALPHA_SIZE, 1,
GLX_STENCIL_SIZE, 8,
GLX_DOUBLEBUFFER, // Needs to support double-buffering
//GLX_SAMPLE_BUFFERS, 1,
//GLX_SAMPLES, 4,
None // end of list
};
// Try for the double-bufferd visual first
visualInfo = glXChooseVisual( g_pDisplay, DefaultScreen(g_pDisplay), doubleBufferVisual );
if( visualInfo == NULL )
{
fprintf(stderr, "glxsimple: %s\n", "no RGB visual with depth buffer");
exit(1);
}
glxContext = glXCreateContext( g_pDisplay,
visualInfo,
0, // No sharing of display lists
GL_TRUE ); // Direct rendering if possible
if( glxContext == NULL )
{
fprintf(stderr, "glxsimple: %s\n", "could not create rendering context");
exit(1);
}
/* GLXContext glxContext2 = glXCreateContext( g_pDisplay,
visualInfo,
glxContext, //share display lists; this is the important part
GL_TRUE ); // Direct rendering if possible
*/
// Create an X colormap since we're probably not using the default visual
colorMap = XCreateColormap( g_pDisplay,
RootWindow(g_pDisplay, visualInfo->screen),
visualInfo->visual,
AllocNone );
windowAttributes.colormap = colorMap;
windowAttributes.border_pixel = 0;
windowAttributes.background_pixel = 0;
windowAttributes.background_pixmap = None;
windowAttributes.event_mask = ExposureMask |
VisibilityChangeMask |
KeyPressMask |
KeyReleaseMask |
ButtonPressMask |
ButtonReleaseMask |
PointerMotionMask |
StructureNotifyMask |
SubstructureNotifyMask |
FocusChangeMask;
// Create an X window with the selected visual
g_window = XCreateWindow( g_pDisplay,
RootWindow(g_pDisplay, visualInfo->screen),
20, 20, // x/y position of top-left outside corner of the window
w=WWIDTH, h=WHEIGHT, // Width and height of window
0, // Border width
visualInfo->depth,
InputOutput,
visualInfo->visual,
CWBackPixel | CWBorderPixel | CWColormap | CWEventMask,
&windowAttributes );
XSetStandardProperties( g_pDisplay,
g_window,
"graphic depictions",
"gd",
None,
0,
NULL,
NULL );
// Bind the rendering context to the window
glXMakeCurrent( g_pDisplay, g_window, glxContext );
//printf("Direct Rendering: %s\n",glXIsDirect(g_pDisplay, glxContext) ? "true" : "false");
//printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
//printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
XFree(visualInfo);
//RECT rt;
//GetClientRect(hWnd,&rt);
//SetWindowPos(hWnd,NULL,50,50,1600-(rt.right-rt.left),1200-(rt.bottom-rt.top),0);
}
void CSMainWindow::InitGL()
{
}
void CSMainWindow::Destroy()
{
}
void CSMainWindow::Show()
{
// Request the X window to be displayed on the screen
XMapWindow( g_pDisplay, g_window );
}
#define DRAG_THRESHOLD 8
int CSMainWindow::CheckMessages()
{
XEvent event;
while (XPending(g_pDisplay) ) // Loop to compress events
{
XNextEvent( g_pDisplay, &event );
switch( event.type )
{
case ButtonPress:
{
px=event.xbutton.x;
py=event.xbutton.y;
if(ImGui::GetIO().WantCaptureMouse) {
//printf("click suppressed\n");
px=py=-999; }
s.e->keys[event.xbutton.button]=true;
}
break;
case ButtonRelease:
{
s.e->keys[event.xbutton.button]=false;
if(!ImGui::GetIO().WantCaptureMouse) {
if( (abs(px-event.xbutton.x) + abs(py-event.xbutton.y))<DRAG_THRESHOLD ) {
s.e->Click(event.xbutton.button,event.xbutton.x,event.xbutton.y);
}
s.e->CancelDragging();
}
}
break;
case KeyPress:
{
// if(!s.e->keys[event.xkey.keycode&0xFF]) s.ed->HandleKey(event.xkey.keycode&0xFF);
char buf[16]; KeySym k;
XLookupString(&event.xkey,buf,16,&k,NULL);
if(k<0xE000) ImGui::GetIO().AddInputCharacter(k);
if(k>='A' && k<='Z') k+=('a'-'A'); // normalise capitals to decollide
if(k>=0xFE00 && k<0xFF00) break; // none of these are of use
if(!ImGui::GetIO().WantCaptureKeyboard) {
s.e->keys[k&0xFF]=true;
}
ImGui::GetIO().KeysDown[k&0xFF]=true;
//fprintf( stderr, "KeyPress event\n" );
}
break;
case KeyRelease:
{
char buf[16]; KeySym k;
XLookupString(&event.xkey,buf,16,&k,NULL);
if(k>='A' && k<='Z') k+=('a'-'A'); // normalise capitals to decollide
if(k>=0xFE00 && k<0xFF00) break; // none of these are of use
s.e->keys[k&0xFF]=false;
ImGui::GetIO().KeysDown[k&0xFF]=false;
//fprintf( stderr, "KeyRelease event\n" );
}
break;
case MotionNotify:
{
s.e->graphics.mx=event.xmotion.x;
s.e->graphics.my=event.xmotion.y;
/* TODO: Should this really be here? */
/*if( s.e->keys[1] && px!=-999 && py!=-999 && (abs(px-event.xmotion.x) + abs(py-event.xmotion.y))>=DRAG_THRESHOLD ) {
s.e->StartDragging(px,py);
}*/
}
break;
case Expose:
{
SetActive();
fprintf( stderr, "Expose event\n" );
}
break;
case ConfigureNotify:
{
glViewport( 0, 0, w=event.xconfigure.width, h=event.xconfigure.height );
}
break;
}
}
return 1;
}
void CSMainWindow::SetWindowTitle(const char *title)
{
#ifdef WIN32
SetWindowText(GetHWND(),text);
#else
XSetStandardProperties( g_pDisplay,
g_window,
title,
"gd",
None,
0,
NULL,
NULL );
#endif
}
char *CSMainWindow::GetFilename(char *title, bool save)
{
static char buf[128];
sprintf(buf,"zenity --file-selection --title=%s %s",title,save?"--save":"");
FILE *ofd = popen(buf,"r");
char *fn = NULL;
fscanf(ofd," %m[^\n] ",&fn);
fclose(ofd);
return fn;}