Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/gl/gl-ui.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2022 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 #include "gl-app.h" | |
| 24 | |
| 25 #include <string.h> | |
| 26 #include <stdlib.h> | |
| 27 #include <stdio.h> | |
| 28 | |
| 29 #ifndef FREEGLUT | |
| 30 /* freeglut extension no-ops */ | |
| 31 void glutExit(void) {} | |
| 32 void glutMouseWheelFunc(void *fn) {} | |
| 33 void glutInitErrorFunc(void *fn) {} | |
| 34 void glutInitWarningFunc(void *fn) {} | |
| 35 #define glutSetOption(X,Y) | |
| 36 #endif | |
| 37 | |
| 38 enum | |
| 39 { | |
| 40 /* Default UI sizes */ | |
| 41 DEFAULT_UI_FONTSIZE = 15, | |
| 42 DEFAULT_UI_BASELINE = 14, | |
| 43 DEFAULT_UI_LINEHEIGHT = 18, | |
| 44 DEFAULT_UI_GRIDSIZE = DEFAULT_UI_LINEHEIGHT + 6, | |
| 45 }; | |
| 46 | |
| 47 struct ui ui; | |
| 48 | |
| 49 #if defined(FREEGLUT) && (GLUT_API_VERSION >= 6) | |
| 50 | |
| 51 void ui_set_clipboard(const char *buf) | |
| 52 { | |
| 53 glutSetClipboard(GLUT_PRIMARY, buf); | |
| 54 glutSetClipboard(GLUT_CLIPBOARD, buf); | |
| 55 } | |
| 56 | |
| 57 const char *ui_get_clipboard(void) | |
| 58 { | |
| 59 return glutGetClipboard(GLUT_CLIPBOARD); | |
| 60 } | |
| 61 | |
| 62 #else | |
| 63 | |
| 64 static char *clipboard_buffer = NULL; | |
| 65 | |
| 66 void ui_set_clipboard(const char *buf) | |
| 67 { | |
| 68 fz_free(ctx, clipboard_buffer); | |
| 69 clipboard_buffer = fz_strdup(ctx, buf); | |
| 70 } | |
| 71 | |
| 72 const char *ui_get_clipboard(void) | |
| 73 { | |
| 74 return clipboard_buffer; | |
| 75 } | |
| 76 | |
| 77 #endif | |
| 78 | |
| 79 static const char *ogl_error_string(GLenum code) | |
| 80 { | |
| 81 #define CASE(E) case E: return #E; break | |
| 82 switch (code) | |
| 83 { | |
| 84 /* glGetError */ | |
| 85 CASE(GL_NO_ERROR); | |
| 86 CASE(GL_INVALID_ENUM); | |
| 87 CASE(GL_INVALID_VALUE); | |
| 88 CASE(GL_INVALID_OPERATION); | |
| 89 CASE(GL_OUT_OF_MEMORY); | |
| 90 CASE(GL_STACK_UNDERFLOW); | |
| 91 CASE(GL_STACK_OVERFLOW); | |
| 92 default: return "(unknown)"; | |
| 93 } | |
| 94 #undef CASE | |
| 95 } | |
| 96 | |
| 97 static int has_ARB_texture_non_power_of_two = 1; | |
| 98 static GLint max_texture_size = 8192; | |
| 99 | |
| 100 void ui_init_draw(void) | |
| 101 { | |
| 102 } | |
| 103 | |
| 104 static unsigned int next_power_of_two(unsigned int n) | |
| 105 { | |
| 106 --n; | |
| 107 n |= n >> 1; | |
| 108 n |= n >> 2; | |
| 109 n |= n >> 4; | |
| 110 n |= n >> 8; | |
| 111 n |= n >> 16; | |
| 112 return ++n; | |
| 113 } | |
| 114 | |
| 115 void ui_texture_from_pixmap(struct texture *tex, fz_pixmap *pix) | |
| 116 { | |
| 117 if (!tex->id) | |
| 118 glGenTextures(1, &tex->id); | |
| 119 glBindTexture(GL_TEXTURE_2D, tex->id); | |
| 120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 122 | |
| 123 tex->x = pix->x; | |
| 124 tex->y = pix->y; | |
| 125 tex->w = pix->w; | |
| 126 tex->h = pix->h; | |
| 127 | |
| 128 if (has_ARB_texture_non_power_of_two) | |
| 129 { | |
| 130 if (tex->w > max_texture_size || tex->h > max_texture_size) | |
| 131 fz_warn(ctx, "texture size (%d x %d) exceeds implementation limit (%d)", tex->w, tex->h, max_texture_size); | |
| 132 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->w, tex->h, 0, pix->n == 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, pix->samples); | |
| 134 tex->s = 1; | |
| 135 tex->t = 1; | |
| 136 } | |
| 137 else | |
| 138 { | |
| 139 int w2 = next_power_of_two(tex->w); | |
| 140 int h2 = next_power_of_two(tex->h); | |
| 141 if (w2 > max_texture_size || h2 > max_texture_size) | |
| 142 fz_warn(ctx, "texture size (%d x %d) exceeds implementation limit (%d)", w2, h2, max_texture_size); | |
| 143 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 144 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w2, h2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); | |
| 145 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex->w, tex->h, pix->n == 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, pix->samples); | |
| 146 tex->s = (float) tex->w / w2; | |
| 147 tex->t = (float) tex->h / h2; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void ui_draw_image(struct texture *tex, float x, float y) | |
| 152 { | |
| 153 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | |
| 154 glEnable(GL_BLEND); | |
| 155 glBindTexture(GL_TEXTURE_2D, tex->id); | |
| 156 glEnable(GL_TEXTURE_2D); | |
| 157 glBegin(GL_TRIANGLE_STRIP); | |
| 158 { | |
| 159 glColor4f(1, 1, 1, 1); | |
| 160 glTexCoord2f(0, tex->t); | |
| 161 glVertex2f(x + tex->x, y + tex->y + tex->h); | |
| 162 glTexCoord2f(0, 0); | |
| 163 glVertex2f(x + tex->x, y + tex->y); | |
| 164 glTexCoord2f(tex->s, tex->t); | |
| 165 glVertex2f(x + tex->x + tex->w, y + tex->y + tex->h); | |
| 166 glTexCoord2f(tex->s, 0); | |
| 167 glVertex2f(x + tex->x + tex->w, y + tex->y); | |
| 168 } | |
| 169 glEnd(); | |
| 170 glDisable(GL_TEXTURE_2D); | |
| 171 glDisable(GL_BLEND); | |
| 172 } | |
| 173 | |
| 174 void glColorHex(unsigned int hex) | |
| 175 { | |
| 176 float r = ((hex>>16)&0xff) / 255.0f; | |
| 177 float g = ((hex>>8)&0xff) / 255.0f; | |
| 178 float b = ((hex)&0xff) / 255.0f; | |
| 179 glColor3f(r, g, b); | |
| 180 } | |
| 181 | |
| 182 void ui_draw_bevel_imp(fz_irect area, unsigned ot, unsigned it, unsigned ib, unsigned ob) | |
| 183 { | |
| 184 glColorHex(ot); | |
| 185 glRectf(area.x0, area.y0, area.x1-1, area.y0+1); | |
| 186 glRectf(area.x0, area.y0+1, area.x0+1, area.y1-1); | |
| 187 glColorHex(ob); | |
| 188 glRectf(area.x1-1, area.y0, area.x1, area.y1); | |
| 189 glRectf(area.x0, area.y1-1, area.x1-1, area.y1); | |
| 190 glColorHex(it); | |
| 191 glRectf(area.x0+1, area.y0+1, area.x1-2, area.y0+2); | |
| 192 glRectf(area.x0+1, area.y0+2, area.x0+2, area.y1-2); | |
| 193 glColorHex(ib); | |
| 194 glRectf(area.x1-2, area.y0+1, area.x1-1, area.y1-1); | |
| 195 glRectf(area.x0+1, area.y1-2, area.x1-2, area.y1-1); | |
| 196 } | |
| 197 | |
| 198 void ui_draw_bevel(fz_irect area, int depressed) | |
| 199 { | |
| 200 if (depressed) | |
| 201 ui_draw_bevel_imp(area, UI_COLOR_BEVEL_2, UI_COLOR_BEVEL_1, UI_COLOR_BEVEL_3, UI_COLOR_BEVEL_4); | |
| 202 else | |
| 203 ui_draw_bevel_imp(area, UI_COLOR_BEVEL_4, UI_COLOR_BEVEL_3, UI_COLOR_BEVEL_2, UI_COLOR_BEVEL_1); | |
| 204 } | |
| 205 | |
| 206 void ui_draw_ibevel(fz_irect area, int depressed) | |
| 207 { | |
| 208 if (depressed) | |
| 209 ui_draw_bevel_imp(area, UI_COLOR_BEVEL_2, UI_COLOR_BEVEL_1, UI_COLOR_BEVEL_3, UI_COLOR_BEVEL_4); | |
| 210 else | |
| 211 ui_draw_bevel_imp(area, UI_COLOR_BEVEL_3, UI_COLOR_BEVEL_4, UI_COLOR_BEVEL_2, UI_COLOR_BEVEL_1); | |
| 212 } | |
| 213 | |
| 214 void ui_draw_bevel_rect(fz_irect area, unsigned int fill, int depressed) | |
| 215 { | |
| 216 ui_draw_bevel(area, depressed); | |
| 217 glColorHex(fill); | |
| 218 glRectf(area.x0+2, area.y0+2, area.x1-2, area.y1-2); | |
| 219 } | |
| 220 | |
| 221 void ui_draw_ibevel_rect(fz_irect area, unsigned int fill, int depressed) | |
| 222 { | |
| 223 ui_draw_ibevel(area, depressed); | |
| 224 glColorHex(fill); | |
| 225 glRectf(area.x0+2, area.y0+2, area.x1-2, area.y1-2); | |
| 226 } | |
| 227 | |
| 228 #if defined(FREEGLUT) && (GLUT_API_VERSION >= 6) | |
| 229 static void on_keyboard(int key, int x, int y) | |
| 230 #else | |
| 231 static void on_keyboard(unsigned char key, int x, int y) | |
| 232 #endif | |
| 233 { | |
| 234 #ifdef __APPLE__ | |
| 235 /* Apple's GLUT has swapped DELETE and BACKSPACE */ | |
| 236 if (key == 8) | |
| 237 key = 127; | |
| 238 else if (key == 127) | |
| 239 key = 8; | |
| 240 #endif | |
| 241 ui.x = x; | |
| 242 ui.y = y; | |
| 243 ui.key = key; | |
| 244 ui.mod = glutGetModifiers(); | |
| 245 ui.plain = !(ui.mod & ~GLUT_ACTIVE_SHIFT); | |
| 246 run_main_loop(); | |
| 247 ui.key = ui.plain = 0; | |
| 248 ui_invalidate(); // TODO: leave this to caller | |
| 249 } | |
| 250 | |
| 251 static void on_special(int key, int x, int y) | |
| 252 { | |
| 253 ui.x = x; | |
| 254 ui.y = y; | |
| 255 ui.key = 0; | |
| 256 | |
| 257 switch (key) | |
| 258 { | |
| 259 case GLUT_KEY_INSERT: ui.key = KEY_INSERT; break; | |
| 260 #ifdef GLUT_KEY_DELETE | |
| 261 case GLUT_KEY_DELETE: ui.key = KEY_DELETE; break; | |
| 262 #endif | |
| 263 case GLUT_KEY_RIGHT: ui.key = KEY_RIGHT; break; | |
| 264 case GLUT_KEY_LEFT: ui.key = KEY_LEFT; break; | |
| 265 case GLUT_KEY_DOWN: ui.key = KEY_DOWN; break; | |
| 266 case GLUT_KEY_UP: ui.key = KEY_UP; break; | |
| 267 case GLUT_KEY_PAGE_UP: ui.key = KEY_PAGE_UP; break; | |
| 268 case GLUT_KEY_PAGE_DOWN: ui.key = KEY_PAGE_DOWN; break; | |
| 269 case GLUT_KEY_HOME: ui.key = KEY_HOME; break; | |
| 270 case GLUT_KEY_END: ui.key = KEY_END; break; | |
| 271 case GLUT_KEY_F1: ui.key = KEY_F1; break; | |
| 272 case GLUT_KEY_F2: ui.key = KEY_F2; break; | |
| 273 case GLUT_KEY_F3: ui.key = KEY_F3; break; | |
| 274 case GLUT_KEY_F4: ui.key = KEY_F4; break; | |
| 275 case GLUT_KEY_F5: ui.key = KEY_F5; break; | |
| 276 case GLUT_KEY_F6: ui.key = KEY_F6; break; | |
| 277 case GLUT_KEY_F7: ui.key = KEY_F7; break; | |
| 278 case GLUT_KEY_F8: ui.key = KEY_F8; break; | |
| 279 case GLUT_KEY_F9: ui.key = KEY_F9; break; | |
| 280 case GLUT_KEY_F10: ui.key = KEY_F10; break; | |
| 281 case GLUT_KEY_F11: ui.key = KEY_F11; break; | |
| 282 case GLUT_KEY_F12: ui.key = KEY_F12; break; | |
| 283 } | |
| 284 | |
| 285 if (ui.key) | |
| 286 { | |
| 287 ui.mod = glutGetModifiers(); | |
| 288 ui.plain = !(ui.mod & ~GLUT_ACTIVE_SHIFT); | |
| 289 run_main_loop(); | |
| 290 ui.key = ui.plain = 0; | |
| 291 ui_invalidate(); // TODO: leave this to caller | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 static void on_wheel(int wheel, int direction, int x, int y) | |
| 296 { | |
| 297 ui.scroll_x = wheel == 1 ? direction : 0; | |
| 298 ui.scroll_y = wheel == 0 ? direction : 0; | |
| 299 ui.mod = glutGetModifiers(); | |
| 300 run_main_loop(); | |
| 301 ui_invalidate(); // TODO: leave this to caller | |
| 302 ui.scroll_x = ui.scroll_y = 0; | |
| 303 } | |
| 304 | |
| 305 static void on_mouse(int button, int action, int x, int y) | |
| 306 { | |
| 307 ui.x = x; | |
| 308 ui.y = y; | |
| 309 if (action == GLUT_DOWN) | |
| 310 { | |
| 311 switch (button) | |
| 312 { | |
| 313 case GLUT_LEFT_BUTTON: | |
| 314 ui.down_x = x; | |
| 315 ui.down_y = y; | |
| 316 ui.down = 1; | |
| 317 break; | |
| 318 case GLUT_MIDDLE_BUTTON: | |
| 319 ui.middle_x = x; | |
| 320 ui.middle_y = y; | |
| 321 ui.middle = 1; | |
| 322 break; | |
| 323 case GLUT_RIGHT_BUTTON: | |
| 324 ui.right_x = x; | |
| 325 ui.right_y = y; | |
| 326 ui.right = 1; | |
| 327 break; | |
| 328 case 3: on_wheel(0, 1, x, y); break; | |
| 329 case 4: on_wheel(0, -1, x, y); break; | |
| 330 case 5: on_wheel(1, 1, x, y); break; | |
| 331 case 6: on_wheel(1, -1, x, y); break; | |
| 332 } | |
| 333 } | |
| 334 else if (action == GLUT_UP) | |
| 335 { | |
| 336 switch (button) | |
| 337 { | |
| 338 case GLUT_LEFT_BUTTON: ui.down = 0; break; | |
| 339 case GLUT_MIDDLE_BUTTON: ui.middle = 0; break; | |
| 340 case GLUT_RIGHT_BUTTON: ui.right = 0; break; | |
| 341 } | |
| 342 } | |
| 343 ui.mod = glutGetModifiers(); | |
| 344 run_main_loop(); | |
| 345 ui_invalidate(); // TODO: leave this to caller | |
| 346 } | |
| 347 | |
| 348 static void on_motion(int x, int y) | |
| 349 { | |
| 350 ui.x = x; | |
| 351 ui.y = y; | |
| 352 ui_invalidate(); | |
| 353 } | |
| 354 | |
| 355 static void on_passive_motion(int x, int y) | |
| 356 { | |
| 357 ui.x = x; | |
| 358 ui.y = y; | |
| 359 ui_invalidate(); | |
| 360 } | |
| 361 | |
| 362 static void on_reshape(int w, int h) | |
| 363 { | |
| 364 ui.window_w = w; | |
| 365 ui.window_h = h; | |
| 366 } | |
| 367 | |
| 368 static void on_display(void) | |
| 369 { | |
| 370 run_main_loop(); | |
| 371 } | |
| 372 | |
| 373 static void on_error(const char *fmt, va_list ap) | |
| 374 { | |
| 375 #ifdef _WIN32 | |
| 376 char buf[1000]; | |
| 377 fz_vsnprintf(buf, sizeof buf, fmt, ap); | |
| 378 MessageBoxA(NULL, buf, "MuPDF GLUT Error", MB_ICONERROR); | |
| 379 #else | |
| 380 fprintf(stderr, "GLUT error: "); | |
| 381 vfprintf(stderr, fmt, ap); | |
| 382 fprintf(stderr, "\n"); | |
| 383 #endif | |
| 384 } | |
| 385 | |
| 386 static void on_warning(const char *fmt, va_list ap) | |
| 387 { | |
| 388 fprintf(stderr, "GLUT warning: "); | |
| 389 vfprintf(stderr, fmt, ap); | |
| 390 fprintf(stderr, "\n"); | |
| 391 } | |
| 392 | |
| 393 static void on_timer(int timer_id) | |
| 394 { | |
| 395 if (reloadrequested) | |
| 396 { | |
| 397 reload(); | |
| 398 ui_invalidate(); | |
| 399 reloadrequested = 0; | |
| 400 } | |
| 401 glutTimerFunc(500, on_timer, 0); | |
| 402 } | |
| 403 | |
| 404 void ui_init_dpi(float override_scale) | |
| 405 { | |
| 406 ui.scale = 1; | |
| 407 | |
| 408 if (override_scale) | |
| 409 { | |
| 410 ui.scale = override_scale; | |
| 411 } | |
| 412 else | |
| 413 { | |
| 414 int wmm = glutGet(GLUT_SCREEN_WIDTH_MM); | |
| 415 int wpx = glutGet(GLUT_SCREEN_WIDTH); | |
| 416 int hmm = glutGet(GLUT_SCREEN_HEIGHT_MM); | |
| 417 int hpx = glutGet(GLUT_SCREEN_HEIGHT); | |
| 418 if (wmm > 0 && hmm > 0) | |
| 419 { | |
| 420 float ppi = ((wpx * 254) / wmm + (hpx * 254) / hmm) / 20; | |
| 421 if (ppi >= 288) ui.scale = 3; | |
| 422 else if (ppi >= 192) ui.scale = 2; | |
| 423 else if (ppi >= 144) ui.scale = 1.5f; | |
| 424 } | |
| 425 } | |
| 426 | |
| 427 ui.fontsize = DEFAULT_UI_FONTSIZE * ui.scale; | |
| 428 ui.baseline = DEFAULT_UI_BASELINE * ui.scale; | |
| 429 ui.lineheight = DEFAULT_UI_LINEHEIGHT * ui.scale; | |
| 430 ui.gridsize = DEFAULT_UI_GRIDSIZE * ui.scale; | |
| 431 ui.padsize = 2 * ui.scale; | |
| 432 } | |
| 433 | |
| 434 void ui_init(int w, int h, const char *title) | |
| 435 { | |
| 436 #ifdef FREEGLUT | |
| 437 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS); | |
| 438 #endif | |
| 439 | |
| 440 glutInitErrorFunc(on_error); | |
| 441 glutInitWarningFunc(on_warning); | |
| 442 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); | |
| 443 glutInitWindowSize(w, h); | |
| 444 glutCreateWindow(title); | |
| 445 | |
| 446 glutTimerFunc(500, on_timer, 0); | |
| 447 glutReshapeFunc(on_reshape); | |
| 448 glutDisplayFunc(on_display); | |
| 449 #if defined(FREEGLUT) && (GLUT_API_VERSION >= 6) | |
| 450 glutKeyboardExtFunc(on_keyboard); | |
| 451 #else | |
| 452 fz_warn(ctx, "This version of MuPDF has been built WITHOUT clipboard or unicode input support!"); | |
| 453 fz_warn(ctx, "Please file a complaint with your friendly local distribution manager."); | |
| 454 glutKeyboardFunc(on_keyboard); | |
| 455 #endif | |
| 456 glutSpecialFunc(on_special); | |
| 457 glutMouseFunc(on_mouse); | |
| 458 glutMotionFunc(on_motion); | |
| 459 glutPassiveMotionFunc(on_passive_motion); | |
| 460 glutMouseWheelFunc(on_wheel); | |
| 461 | |
| 462 has_ARB_texture_non_power_of_two = glutExtensionSupported("GL_ARB_texture_non_power_of_two"); | |
| 463 if (!has_ARB_texture_non_power_of_two) | |
| 464 fz_warn(ctx, "OpenGL implementation does not support non-power of two texture sizes"); | |
| 465 | |
| 466 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); | |
| 467 | |
| 468 ui_init_fonts(); | |
| 469 | |
| 470 ui.overlay_list = glGenLists(1); | |
| 471 } | |
| 472 | |
| 473 void ui_finish(void) | |
| 474 { | |
| 475 pdf_drop_annot(ctx, ui.selected_annot); | |
| 476 glDeleteLists(ui.overlay_list, 1); | |
| 477 ui_finish_fonts(); | |
| 478 glutExit(); | |
| 479 } | |
| 480 | |
| 481 void ui_invalidate(void) | |
| 482 { | |
| 483 glutPostRedisplay(); | |
| 484 } | |
| 485 | |
| 486 void ui_begin(void) | |
| 487 { | |
| 488 ui.hot = NULL; | |
| 489 | |
| 490 ui.cavity = ui.cavity_stack; | |
| 491 ui.cavity->x0 = 0; | |
| 492 ui.cavity->y0 = 0; | |
| 493 ui.cavity->x1 = ui.window_w; | |
| 494 ui.cavity->y1 = ui.window_h; | |
| 495 | |
| 496 ui.layout = ui.layout_stack; | |
| 497 ui.layout->side = ALL; | |
| 498 ui.layout->fill = BOTH; | |
| 499 ui.layout->anchor = NW; | |
| 500 ui.layout->padx = 0; | |
| 501 ui.layout->pady = 0; | |
| 502 | |
| 503 ui.cursor = GLUT_CURSOR_INHERIT; | |
| 504 | |
| 505 ui.overlay = 0; | |
| 506 | |
| 507 glViewport(0, 0, ui.window_w, ui.window_h); | |
| 508 glClear(GL_COLOR_BUFFER_BIT); | |
| 509 | |
| 510 glMatrixMode(GL_PROJECTION); | |
| 511 glLoadIdentity(); | |
| 512 glOrtho(0, ui.window_w, ui.window_h, 0, -1, 1); | |
| 513 | |
| 514 glMatrixMode(GL_MODELVIEW); | |
| 515 glLoadIdentity(); | |
| 516 } | |
| 517 | |
| 518 void ui_end(void) | |
| 519 { | |
| 520 int code; | |
| 521 | |
| 522 if (ui.overlay) | |
| 523 glCallList(ui.overlay_list); | |
| 524 | |
| 525 if (ui.cursor != ui.last_cursor) | |
| 526 { | |
| 527 glutSetCursor(ui.cursor); | |
| 528 ui.last_cursor = ui.cursor; | |
| 529 } | |
| 530 | |
| 531 code = glGetError(); | |
| 532 if (code != GL_NO_ERROR) | |
| 533 fz_warn(ctx, "glGetError: %s", ogl_error_string(code)); | |
| 534 | |
| 535 if (!ui.active && (ui.down || ui.middle || ui.right)) | |
| 536 ui.active = "dummy"; | |
| 537 | |
| 538 if ((ui.grab_down && !ui.down) || (ui.grab_middle && !ui.middle) || (ui.grab_right && !ui.right)) | |
| 539 { | |
| 540 ui.grab_down = ui.grab_middle = ui.grab_right = 0; | |
| 541 ui.active = NULL; | |
| 542 } | |
| 543 | |
| 544 if (ui.active) | |
| 545 { | |
| 546 if (ui.active != ui.focus) | |
| 547 ui.focus = NULL; | |
| 548 if (!ui.grab_down && !ui.grab_middle && !ui.grab_right) | |
| 549 { | |
| 550 ui.grab_down = ui.down; | |
| 551 ui.grab_middle = ui.middle; | |
| 552 ui.grab_right = ui.right; | |
| 553 } | |
| 554 } | |
| 555 | |
| 556 glutSwapBuffers(); | |
| 557 } | |
| 558 | |
| 559 /* Widgets */ | |
| 560 | |
| 561 int ui_mouse_inside(fz_irect area) | |
| 562 { | |
| 563 if (ui.x >= area.x0 && ui.x < area.x1 && ui.y >= area.y0 && ui.y < area.y1) | |
| 564 return 1; | |
| 565 return 0; | |
| 566 } | |
| 567 | |
| 568 fz_irect ui_pack_layout(int slave_w, int slave_h, enum side side, enum fill fill, enum anchor anchor, int padx, int pady) | |
| 569 { | |
| 570 fz_irect parcel, slave; | |
| 571 int parcel_w, parcel_h; | |
| 572 int anchor_x, anchor_y; | |
| 573 | |
| 574 switch (side) | |
| 575 { | |
| 576 default: | |
| 577 case ALL: | |
| 578 parcel.x0 = ui.cavity->x0 + padx; | |
| 579 parcel.x1 = ui.cavity->x1 - padx; | |
| 580 parcel.y0 = ui.cavity->y0 + pady; | |
| 581 parcel.y1 = ui.cavity->y1 - pady; | |
| 582 ui.cavity->x0 = ui.cavity->x1; | |
| 583 ui.cavity->y0 = ui.cavity->y1; | |
| 584 break; | |
| 585 case T: | |
| 586 parcel.x0 = ui.cavity->x0 + padx; | |
| 587 parcel.x1 = ui.cavity->x1 - padx; | |
| 588 parcel.y0 = ui.cavity->y0 + pady; | |
| 589 parcel.y1 = ui.cavity->y0 + pady + slave_h; | |
| 590 ui.cavity->y0 = parcel.y1 + pady; | |
| 591 break; | |
| 592 case B: | |
| 593 parcel.x0 = ui.cavity->x0 + padx; | |
| 594 parcel.x1 = ui.cavity->x1 - padx; | |
| 595 parcel.y0 = ui.cavity->y1 - pady - slave_h; | |
| 596 parcel.y1 = ui.cavity->y1 - pady; | |
| 597 ui.cavity->y1 = parcel.y0 - pady; | |
| 598 break; | |
| 599 case L: | |
| 600 parcel.x0 = ui.cavity->x0 + padx; | |
| 601 parcel.x1 = ui.cavity->x0 + padx + slave_w; | |
| 602 parcel.y0 = ui.cavity->y0 + pady; | |
| 603 parcel.y1 = ui.cavity->y1 - pady; | |
| 604 ui.cavity->x0 = parcel.x1 + padx; | |
| 605 break; | |
| 606 case R: | |
| 607 parcel.x0 = ui.cavity->x1 - padx - slave_w; | |
| 608 parcel.x1 = ui.cavity->x1 - padx; | |
| 609 parcel.y0 = ui.cavity->y0 + pady; | |
| 610 parcel.y1 = ui.cavity->y1 - pady; | |
| 611 ui.cavity->x1 = parcel.x0 - padx; | |
| 612 break; | |
| 613 } | |
| 614 | |
| 615 parcel_w = parcel.x1 - parcel.x0; | |
| 616 parcel_h = parcel.y1 - parcel.y0; | |
| 617 | |
| 618 if (fill & X) | |
| 619 slave_w = parcel_w; | |
| 620 if (fill & Y) | |
| 621 slave_h = parcel_h; | |
| 622 | |
| 623 anchor_x = parcel_w - slave_w; | |
| 624 anchor_y = parcel_h - slave_h; | |
| 625 | |
| 626 switch (anchor) | |
| 627 { | |
| 628 default: | |
| 629 case CENTER: | |
| 630 slave.x0 = parcel.x0 + anchor_x / 2; | |
| 631 slave.y0 = parcel.y0 + anchor_y / 2; | |
| 632 break; | |
| 633 case N: | |
| 634 slave.x0 = parcel.x0 + anchor_x / 2; | |
| 635 slave.y0 = parcel.y0; | |
| 636 break; | |
| 637 case NE: | |
| 638 slave.x0 = parcel.x0 + anchor_x; | |
| 639 slave.y0 = parcel.y0; | |
| 640 break; | |
| 641 case E: | |
| 642 slave.x0 = parcel.x0 + anchor_x; | |
| 643 slave.y0 = parcel.y0 + anchor_y / 2; | |
| 644 break; | |
| 645 case SE: | |
| 646 slave.x0 = parcel.x0 + anchor_x; | |
| 647 slave.y0 = parcel.y0 + anchor_y; | |
| 648 break; | |
| 649 case S: | |
| 650 slave.x0 = parcel.x0 + anchor_x / 2; | |
| 651 slave.y0 = parcel.y0 + anchor_y; | |
| 652 break; | |
| 653 case SW: | |
| 654 slave.x0 = parcel.x0; | |
| 655 slave.y0 = parcel.y0 + anchor_y; | |
| 656 break; | |
| 657 case W: | |
| 658 slave.x0 = parcel.x0; | |
| 659 slave.y0 = parcel.y0 + anchor_y / 2; | |
| 660 break; | |
| 661 case NW: | |
| 662 slave.x0 = parcel.x0; | |
| 663 slave.y0 = parcel.y0; | |
| 664 break; | |
| 665 } | |
| 666 | |
| 667 slave.x1 = slave.x0 + slave_w; | |
| 668 slave.y1 = slave.y0 + slave_h; | |
| 669 | |
| 670 return slave; | |
| 671 } | |
| 672 | |
| 673 fz_irect ui_pack(int slave_w, int slave_h) | |
| 674 { | |
| 675 return ui_pack_layout(slave_w, slave_h, ui.layout->side, ui.layout->fill, ui.layout->anchor, ui.layout->padx, ui.layout->pady); | |
| 676 } | |
| 677 | |
| 678 int ui_available_width(void) | |
| 679 { | |
| 680 return ui.cavity->x1 - ui.cavity->x0 - ui.layout->padx * 2; | |
| 681 } | |
| 682 | |
| 683 int ui_available_height(void) | |
| 684 { | |
| 685 return ui.cavity->y1 - ui.cavity->y0 - ui.layout->pady * 2; | |
| 686 } | |
| 687 | |
| 688 void ui_pack_push(fz_irect cavity) | |
| 689 { | |
| 690 *(++ui.cavity) = cavity; | |
| 691 ++ui.layout; | |
| 692 ui.layout->side = ALL; | |
| 693 ui.layout->fill = BOTH; | |
| 694 ui.layout->anchor = NW; | |
| 695 ui.layout->padx = 0; | |
| 696 ui.layout->pady = 0; | |
| 697 } | |
| 698 | |
| 699 void ui_pack_pop(void) | |
| 700 { | |
| 701 --ui.cavity; | |
| 702 --ui.layout; | |
| 703 } | |
| 704 | |
| 705 void ui_layout(enum side side, enum fill fill, enum anchor anchor, int padx, int pady) | |
| 706 { | |
| 707 ui.layout->side = side; | |
| 708 ui.layout->fill = fill; | |
| 709 ui.layout->anchor = anchor; | |
| 710 ui.layout->padx = padx; | |
| 711 ui.layout->pady = pady; | |
| 712 } | |
| 713 | |
| 714 void ui_panel_begin(int w, int h, int padx, int pady, int opaque) | |
| 715 { | |
| 716 fz_irect area = ui_pack(w, h); | |
| 717 if (opaque) | |
| 718 { | |
| 719 glColorHex(UI_COLOR_PANEL); | |
| 720 glRectf(area.x0, area.y0, area.x1, area.y1); | |
| 721 } | |
| 722 area.x0 += padx; area.y0 += pady; | |
| 723 area.x1 -= padx; area.y1 -= pady; | |
| 724 ui_pack_push(area); | |
| 725 } | |
| 726 | |
| 727 void ui_panel_end(void) | |
| 728 { | |
| 729 ui_pack_pop(); | |
| 730 } | |
| 731 | |
| 732 void ui_dialog_begin(int w, int h) | |
| 733 { | |
| 734 fz_irect area; | |
| 735 int x, y; | |
| 736 w += 24 + 4; | |
| 737 h += 24 + 4; | |
| 738 if (w > ui.window_w) w = ui.window_w - 20; | |
| 739 if (h > ui.window_h) h = ui.window_h - 20; | |
| 740 x = (ui.window_w-w)/2; | |
| 741 y = (ui.window_h-h)/3; | |
| 742 area = fz_make_irect(x, y, x+w, y+h); | |
| 743 ui_draw_bevel_rect(area, UI_COLOR_PANEL, 0); | |
| 744 area = fz_expand_irect(area, -14); | |
| 745 ui_pack_push(area); | |
| 746 } | |
| 747 | |
| 748 void ui_dialog_end(void) | |
| 749 { | |
| 750 ui_pack_pop(); | |
| 751 } | |
| 752 | |
| 753 void ui_spacer(void) | |
| 754 { | |
| 755 ui_pack(ui.lineheight / 2, ui.lineheight / 2); | |
| 756 } | |
| 757 | |
| 758 void ui_label(const char *fmt, ...) | |
| 759 { | |
| 760 char buf[512]; | |
| 761 struct line lines[20]; | |
| 762 int avail, used, n; | |
| 763 fz_irect area; | |
| 764 va_list ap; | |
| 765 | |
| 766 va_start(ap, fmt); | |
| 767 fz_vsnprintf(buf, sizeof buf, fmt, ap); | |
| 768 va_end(ap); | |
| 769 | |
| 770 avail = ui_available_width(); | |
| 771 n = ui_break_lines(buf, lines, nelem(lines), avail, &used); | |
| 772 area = ui_pack(used, n * ui.lineheight); | |
| 773 glColorHex(UI_COLOR_TEXT_FG); | |
| 774 ui_draw_lines(area.x0, area.y0, lines, n); | |
| 775 } | |
| 776 | |
| 777 int ui_button(const char *label) | |
| 778 { | |
| 779 return ui_button_aux(label, 0); | |
| 780 } | |
| 781 | |
| 782 int ui_button_aux(const char *label, int flags) | |
| 783 { | |
| 784 int width = ui_measure_string(label); | |
| 785 fz_irect area = ui_pack(width + 20, ui.gridsize); | |
| 786 int text_x = area.x0 + ((area.x1 - area.x0) - width) / 2; | |
| 787 int pressed = 0; | |
| 788 int disabled = (flags & 1); | |
| 789 | |
| 790 if (!disabled) | |
| 791 { | |
| 792 if (ui_mouse_inside(area)) | |
| 793 { | |
| 794 ui.hot = label; | |
| 795 if (!ui.active && ui.down) | |
| 796 ui.active = label; | |
| 797 } | |
| 798 | |
| 799 pressed = (ui.hot == label && ui.active == label && ui.down); | |
| 800 } | |
| 801 ui_draw_bevel_rect(area, UI_COLOR_BUTTON, pressed); | |
| 802 glColorHex(disabled ? UI_COLOR_TEXT_GRAY : UI_COLOR_TEXT_FG); | |
| 803 ui_draw_string(text_x + pressed, area.y0+3 + pressed, label); | |
| 804 | |
| 805 return !disabled && ui.hot == label && ui.active == label && !ui.down; | |
| 806 } | |
| 807 | |
| 808 int ui_checkbox(const char *label, int *value) | |
| 809 { | |
| 810 return ui_checkbox_aux(label, value, 0); | |
| 811 } | |
| 812 | |
| 813 int ui_checkbox_aux(const char *label, int *value, int flags) | |
| 814 { | |
| 815 int width = ui_measure_string(label); | |
| 816 fz_irect area = ui_pack(13 + 4 + width, ui.lineheight); | |
| 817 fz_irect mark = { area.x0, area.y0 + ui.baseline-12, area.x0 + 13, area.y0 + ui.baseline+1 }; | |
| 818 int pressed = 0; | |
| 819 int disabled = (flags & 1); | |
| 820 | |
| 821 glColorHex(disabled ? UI_COLOR_TEXT_GRAY : UI_COLOR_TEXT_FG); | |
| 822 ui_draw_string(mark.x1 + 4, area.y0, label); | |
| 823 | |
| 824 if (!disabled) | |
| 825 { | |
| 826 if (ui_mouse_inside(area)) | |
| 827 { | |
| 828 ui.hot = label; | |
| 829 if (!ui.active && ui.down) | |
| 830 ui.active = label; | |
| 831 } | |
| 832 | |
| 833 if (ui.hot == label && ui.active == label && !ui.down) | |
| 834 *value = !*value; | |
| 835 | |
| 836 pressed = (ui.hot == label && ui.active == label && ui.down); | |
| 837 } | |
| 838 ui_draw_bevel_rect(mark, (disabled || pressed) ? UI_COLOR_PANEL : UI_COLOR_TEXT_BG, 1); | |
| 839 if (*value) | |
| 840 { | |
| 841 float ax = mark.x0+2 + 1, ay = mark.y0+2 + 3; | |
| 842 float bx = mark.x0+2 + 4, by = mark.y0+2 + 5; | |
| 843 float cx = mark.x0+2 + 8, cy = mark.y0+2 + 1; | |
| 844 glColorHex(disabled ? UI_COLOR_TEXT_GRAY : UI_COLOR_TEXT_FG); | |
| 845 glBegin(GL_TRIANGLE_STRIP); | |
| 846 glVertex2f(ax, ay); glVertex2f(ax, ay+3); | |
| 847 glVertex2f(bx, by); glVertex2f(bx, by+3); | |
| 848 glVertex2f(cx, cy); glVertex2f(cx, cy+3); | |
| 849 glEnd(); | |
| 850 } | |
| 851 | |
| 852 return !disabled && ui.hot == label && ui.active == label && !ui.down; | |
| 853 } | |
| 854 | |
| 855 int ui_slider(int *value, int min, int max, int width) | |
| 856 { | |
| 857 static int start_value = 0; | |
| 858 fz_irect area = ui_pack(width, ui.lineheight); | |
| 859 int m = 6; | |
| 860 int w = area.x1 - area.x0 - m * 2; | |
| 861 int h = area.y1 - area.y0; | |
| 862 fz_irect gutter = { area.x0, area.y0+h/2-2, area.x1, area.y0+h/2+2 }; | |
| 863 fz_irect thumb; | |
| 864 int x; | |
| 865 | |
| 866 if (ui_mouse_inside(area)) | |
| 867 { | |
| 868 ui.hot = value; | |
| 869 if (!ui.active && ui.down) | |
| 870 { | |
| 871 ui.active = value; | |
| 872 start_value = *value; | |
| 873 } | |
| 874 } | |
| 875 | |
| 876 if (ui.active == value) | |
| 877 { | |
| 878 if (ui.y < area.y0 || ui.y > area.y1) | |
| 879 *value = start_value; | |
| 880 else | |
| 881 { | |
| 882 float v = (float)(ui.x - (area.x0+m)) / w; | |
| 883 *value = fz_clamp(min + v * (max - min), min, max); | |
| 884 } | |
| 885 } | |
| 886 | |
| 887 x = ((*value - min) * w) / (max - min); | |
| 888 thumb = fz_make_irect(area.x0+m + x-m, area.y0, area.x0+m + x+m, area.y1); | |
| 889 | |
| 890 ui_draw_bevel(gutter, 1); | |
| 891 ui_draw_bevel_rect(thumb, UI_COLOR_BUTTON, 0); | |
| 892 | |
| 893 return *value != start_value && ui.active == value && !ui.down; | |
| 894 } | |
| 895 | |
| 896 void ui_splitter(int *start, int *v, int min, int max, enum side side) | |
| 897 { | |
| 898 fz_irect area = { 0 }; | |
| 899 | |
| 900 if (side == L || side == R) | |
| 901 area = ui_pack(4, 0); | |
| 902 else if (side == T || side == B) | |
| 903 area = ui_pack(0, 4); | |
| 904 | |
| 905 if (ui_mouse_inside(area)) | |
| 906 { | |
| 907 ui.hot = v; | |
| 908 if (!ui.active && ui.down) | |
| 909 { | |
| 910 ui.active = v; | |
| 911 *start = *v; | |
| 912 } | |
| 913 } | |
| 914 | |
| 915 if (ui.active == v) | |
| 916 { | |
| 917 // how we slide the splitter coords depends on the packing direction | |
| 918 switch (ui.layout->side) | |
| 919 { | |
| 920 default: | |
| 921 case L: *v = fz_clampi(*start + (ui.x - ui.down_x), min, max); break; | |
| 922 case R: *v = fz_clampi(*start + (ui.down_x - ui.x), min, max); break; | |
| 923 case B: *v = fz_clampi(*start + (ui.down_y - ui.y), min, max); break; | |
| 924 case T: *v = fz_clampi(*start + (ui.y - ui.down_y), min, max); break; | |
| 925 } | |
| 926 } | |
| 927 | |
| 928 if (ui.hot == v || ui.active == v) | |
| 929 { | |
| 930 if (side == L || side == R) | |
| 931 ui.cursor = GLUT_CURSOR_LEFT_RIGHT; | |
| 932 else if (side == T || side == B) | |
| 933 ui.cursor = GLUT_CURSOR_UP_DOWN; | |
| 934 } | |
| 935 | |
| 936 if (side == R) | |
| 937 { | |
| 938 glColorHex(UI_COLOR_PANEL); | |
| 939 glRectf(area.x0+0, area.y0, area.x0+2, area.y1); | |
| 940 glColorHex(UI_COLOR_BEVEL_2); | |
| 941 glRectf(area.x0+2, area.y0, area.x0+3, area.y1); | |
| 942 glColorHex(UI_COLOR_BEVEL_1); | |
| 943 glRectf(area.x0+3, area.y0, area.x0+4, area.y1); | |
| 944 } | |
| 945 else if (side == L) | |
| 946 { | |
| 947 glColorHex(UI_COLOR_BEVEL_4); | |
| 948 glRectf(area.x0+0, area.y0, area.x0+1, area.y1); | |
| 949 glColorHex(UI_COLOR_BEVEL_3); | |
| 950 glRectf(area.x0+1, area.y0, area.x0+3, area.y1); | |
| 951 glColorHex(UI_COLOR_PANEL); | |
| 952 glRectf(area.x0+2, area.y0, area.x0+4, area.y1); | |
| 953 } | |
| 954 else if (side == T) | |
| 955 { | |
| 956 glColorHex(UI_COLOR_BEVEL_4); | |
| 957 glRectf(area.x0, area.y0+0, area.x1, area.y0+1); | |
| 958 glColorHex(UI_COLOR_BEVEL_3); | |
| 959 glRectf(area.x0, area.y0+1, area.x1, area.y0+2); | |
| 960 glColorHex(UI_COLOR_PANEL); | |
| 961 glRectf(area.x0, area.y0+2, area.x1, area.y0+4); | |
| 962 } | |
| 963 else if (side == B) | |
| 964 { | |
| 965 glColorHex(UI_COLOR_PANEL); | |
| 966 glRectf(area.x0, area.y0+0, area.x1, area.y0+2); | |
| 967 glColorHex(UI_COLOR_BEVEL_2); | |
| 968 glRectf(area.x0, area.y0+2, area.x1, area.y0+3); | |
| 969 glColorHex(UI_COLOR_BEVEL_1); | |
| 970 glRectf(area.x0, area.y0+3, area.x1, area.y0+4); | |
| 971 } | |
| 972 } | |
| 973 | |
| 974 void ui_scrollbar(int x0, int y0, int x1, int y1, int *value, int page_size, int max, int *sticky) | |
| 975 { | |
| 976 static float start_top = 0; /* we can only drag in one scrollbar at a time, so static is safe */ | |
| 977 float top; | |
| 978 | |
| 979 int total_h = y1 - y0; | |
| 980 int thumb_h = fz_maxi(x1 - x0, total_h * page_size / max); | |
| 981 int avail_h = total_h - thumb_h; | |
| 982 | |
| 983 max -= page_size; | |
| 984 | |
| 985 if (max <= 0) | |
| 986 { | |
| 987 *value = 0; | |
| 988 glColorHex(UI_COLOR_SCROLLBAR); | |
| 989 glRectf(x0, y0, x1, y1); | |
| 990 return; | |
| 991 } | |
| 992 | |
| 993 if (sticky) | |
| 994 { | |
| 995 if (*sticky <= -1) | |
| 996 *value = 0; | |
| 997 else if (*sticky >= 1) | |
| 998 *value = max; | |
| 999 } | |
| 1000 | |
| 1001 top = (float) *value * avail_h / max; | |
| 1002 | |
| 1003 if (ui.down && !ui.active) | |
| 1004 { | |
| 1005 if (ui.x >= x0 && ui.x < x1 && ui.y >= y0 && ui.y < y1) | |
| 1006 { | |
| 1007 if (ui.y < y0 + top) | |
| 1008 { | |
| 1009 ui.active = "pgup"; | |
| 1010 *value -= page_size; | |
| 1011 } | |
| 1012 else if (ui.y >= y0 + top + thumb_h) | |
| 1013 { | |
| 1014 ui.active = "pgdn"; | |
| 1015 *value += page_size; | |
| 1016 } | |
| 1017 else | |
| 1018 { | |
| 1019 ui.hot = value; | |
| 1020 ui.active = value; | |
| 1021 start_top = top; | |
| 1022 } | |
| 1023 } | |
| 1024 } | |
| 1025 | |
| 1026 if (ui.active == value) | |
| 1027 { | |
| 1028 *value = (start_top + ui.y - ui.down_y) * max / avail_h; | |
| 1029 } | |
| 1030 | |
| 1031 if (*value < 0) | |
| 1032 *value = 0; | |
| 1033 else if (*value > max) | |
| 1034 *value = max; | |
| 1035 | |
| 1036 if (sticky) | |
| 1037 { | |
| 1038 if (*sticky == 0 && *value == 0) | |
| 1039 *sticky = -1; | |
| 1040 else if (*sticky == 0 && *value == max) | |
| 1041 *sticky = 1; | |
| 1042 else if (*sticky <= -1 && *value != 0) | |
| 1043 *sticky = 0; | |
| 1044 else if (*sticky >= 1 && *value != max) | |
| 1045 *sticky = 0; | |
| 1046 } | |
| 1047 | |
| 1048 top = (float) *value * avail_h / max; | |
| 1049 | |
| 1050 glColorHex(UI_COLOR_SCROLLBAR); | |
| 1051 glRectf(x0, y0, x1, y1); | |
| 1052 ui_draw_ibevel_rect(fz_make_irect(x0, y0+top, x1, y0+top+thumb_h), UI_COLOR_BUTTON, 0); | |
| 1053 } | |
| 1054 | |
| 1055 void ui_tree_begin(struct list *list, int count, int req_w, int req_h, int is_tree) | |
| 1056 { | |
| 1057 static int start_scroll_y = 0; /* we can only drag in one list at a time, so static is safe */ | |
| 1058 | |
| 1059 fz_irect outer_area = ui_pack(req_w, req_h); | |
| 1060 fz_irect area = { outer_area.x0+2, outer_area.y0+2, outer_area.x1-2, outer_area.y1-2 }; | |
| 1061 | |
| 1062 int max_scroll_y = count * ui.lineheight - (area.y1-area.y0); | |
| 1063 | |
| 1064 if (max_scroll_y > 0) | |
| 1065 area.x1 -= 16; | |
| 1066 | |
| 1067 if (ui_mouse_inside(area)) | |
| 1068 { | |
| 1069 ui.hot = list; | |
| 1070 if (!ui.active && ui.middle) | |
| 1071 { | |
| 1072 ui.active = list; | |
| 1073 start_scroll_y = list->scroll_y; | |
| 1074 } | |
| 1075 } | |
| 1076 | |
| 1077 /* middle button dragging */ | |
| 1078 if (ui.active == list) | |
| 1079 list->scroll_y = start_scroll_y + (ui.middle_y - ui.y) * 5; | |
| 1080 | |
| 1081 /* scroll wheel events */ | |
| 1082 if (ui.hot == list) | |
| 1083 list->scroll_y -= ui.scroll_y * ui.lineheight * 3; | |
| 1084 | |
| 1085 /* keyboard keys */ | |
| 1086 if (ui.hot == list && ui.key == KEY_HOME) | |
| 1087 list->scroll_y = 0; | |
| 1088 if (ui.hot == list && ui.key == KEY_END) | |
| 1089 list->scroll_y = max_scroll_y; | |
| 1090 if (ui.hot == list && ui.key == KEY_PAGE_UP) | |
| 1091 list->scroll_y -= ((area.y1 - area.y0) / ui.lineheight) * ui.lineheight; | |
| 1092 if (ui.hot == list && ui.key == KEY_PAGE_DOWN) | |
| 1093 list->scroll_y += ((area.y1 - area.y0) / ui.lineheight) * ui.lineheight; | |
| 1094 | |
| 1095 /* clamp scrolling to client area */ | |
| 1096 if (list->scroll_y >= max_scroll_y) | |
| 1097 list->scroll_y = max_scroll_y; | |
| 1098 if (list->scroll_y < 0) | |
| 1099 list->scroll_y = 0; | |
| 1100 | |
| 1101 ui_draw_bevel_rect(outer_area, UI_COLOR_TEXT_BG, 1); | |
| 1102 if (max_scroll_y > 0) | |
| 1103 { | |
| 1104 ui_scrollbar(area.x1, area.y0, area.x1+16, area.y1, | |
| 1105 &list->scroll_y, area.y1-area.y0, count * ui.lineheight, NULL); | |
| 1106 } | |
| 1107 | |
| 1108 list->is_tree = is_tree; | |
| 1109 list->area = area; | |
| 1110 list->item_y = area.y0 - list->scroll_y; | |
| 1111 | |
| 1112 glScissor(list->area.x0, ui.window_h-list->area.y1, list->area.x1-list->area.x0, list->area.y1-list->area.y0); | |
| 1113 glEnable(GL_SCISSOR_TEST); | |
| 1114 } | |
| 1115 | |
| 1116 int ui_tree_item(struct list *list, const void *id, const char *label, int selected, int depth, int is_branch, int *is_open) | |
| 1117 { | |
| 1118 fz_irect area = { list->area.x0, list->item_y, list->area.x1, list->item_y + ui.lineheight }; | |
| 1119 int x_handle, x_item; | |
| 1120 | |
| 1121 x_item = ui.lineheight / 4; | |
| 1122 x_item += depth * ui.lineheight; | |
| 1123 x_handle = x_item; | |
| 1124 if (list->is_tree) | |
| 1125 x_item += ui_measure_character(0x25BC) + ui.lineheight / 4; | |
| 1126 | |
| 1127 /* only process visible items */ | |
| 1128 if (area.y1 >= list->area.y0 && area.y0 <= list->area.y1) | |
| 1129 { | |
| 1130 if (ui_mouse_inside(list->area) && ui_mouse_inside(area)) | |
| 1131 { | |
| 1132 if (list->is_tree && ui.x < area.x0 + x_item) | |
| 1133 { | |
| 1134 ui.hot = is_open; | |
| 1135 } | |
| 1136 else | |
| 1137 ui.hot = id; | |
| 1138 if (!ui.active && ui.down) | |
| 1139 { | |
| 1140 if (list->is_tree && ui.hot == is_open) | |
| 1141 *is_open = !*is_open; | |
| 1142 ui.active = ui.hot; | |
| 1143 } | |
| 1144 } | |
| 1145 | |
| 1146 if (ui.active == id || selected) | |
| 1147 { | |
| 1148 glColorHex(UI_COLOR_TEXT_SEL_BG); | |
| 1149 glRectf(area.x0, area.y0, area.x1, area.y1); | |
| 1150 glColorHex(UI_COLOR_TEXT_SEL_FG); | |
| 1151 } | |
| 1152 else | |
| 1153 { | |
| 1154 glColorHex(UI_COLOR_TEXT_FG); | |
| 1155 } | |
| 1156 | |
| 1157 ui_draw_string(area.x0 + x_item, area.y0, label); | |
| 1158 if (list->is_tree && is_branch) | |
| 1159 ui_draw_character(area.x0 + x_handle, area.y0, | |
| 1160 *is_open ? 0x25BC : 0x25B6); | |
| 1161 } | |
| 1162 | |
| 1163 list->item_y += ui.lineheight; | |
| 1164 | |
| 1165 /* trigger on mouse up */ | |
| 1166 return ui.active == id && !ui.down; | |
| 1167 } | |
| 1168 | |
| 1169 void ui_list_begin(struct list *list, int count, int req_w, int req_h) | |
| 1170 { | |
| 1171 ui_tree_begin(list, count, req_w, req_h, 0); | |
| 1172 } | |
| 1173 | |
| 1174 int ui_list_item(struct list *list, const void *id, const char *label, int selected) | |
| 1175 { | |
| 1176 return ui_tree_item(list, id, label, selected, 0, 0, NULL); | |
| 1177 } | |
| 1178 | |
| 1179 void ui_tree_end(struct list *list) | |
| 1180 { | |
| 1181 glDisable(GL_SCISSOR_TEST); | |
| 1182 } | |
| 1183 | |
| 1184 void ui_list_end(struct list *list) | |
| 1185 { | |
| 1186 ui_tree_end(list); | |
| 1187 } | |
| 1188 | |
| 1189 void ui_label_with_scrollbar(char *text, int width, int height, int *scroll, int *sticky) | |
| 1190 { | |
| 1191 struct line lines[500]; | |
| 1192 fz_irect area; | |
| 1193 int n; | |
| 1194 | |
| 1195 area = ui_pack(width, height); | |
| 1196 n = ui_break_lines(text, lines, nelem(lines), area.x1-area.x0 - 16, NULL); | |
| 1197 if (n > (area.y1-area.y0) / ui.lineheight) | |
| 1198 { | |
| 1199 if (ui_mouse_inside(area)) | |
| 1200 { | |
| 1201 *scroll -= ui.scroll_y * ui.lineheight * 3; | |
| 1202 if (ui.scroll_y != 0 && sticky) | |
| 1203 *sticky = 0; | |
| 1204 } | |
| 1205 ui_scrollbar(area.x1-16, area.y0, area.x1, area.y1, | |
| 1206 scroll, area.y1-area.y0, n * ui.lineheight, sticky); | |
| 1207 } | |
| 1208 else | |
| 1209 *scroll = 0; | |
| 1210 | |
| 1211 glScissor(area.x0, ui.window_h-area.y1, area.x1-area.x0-16, area.y1-area.y0); | |
| 1212 glEnable(GL_SCISSOR_TEST); | |
| 1213 glColorHex(UI_COLOR_TEXT_FG); | |
| 1214 ui_draw_lines(area.x0, area.y0 - *scroll, lines, n); | |
| 1215 glDisable(GL_SCISSOR_TEST); | |
| 1216 } | |
| 1217 | |
| 1218 int ui_popup(const void *id, const char *label, int is_button, int count) | |
| 1219 { | |
| 1220 return ui_popup_aux(id, label, is_button, count, 0); | |
| 1221 } | |
| 1222 | |
| 1223 int ui_popup_aux(const void *id, const char *label, int is_button, int count, int flags) | |
| 1224 { | |
| 1225 int width = ui_measure_string(label); | |
| 1226 fz_irect area = ui_pack(width + 22 + 6, ui.gridsize); | |
| 1227 fz_irect menu_area; | |
| 1228 int pressed = 0; | |
| 1229 int disabled = (flags & 1); | |
| 1230 | |
| 1231 if (!disabled) | |
| 1232 { | |
| 1233 if (ui_mouse_inside(area)) | |
| 1234 { | |
| 1235 ui.hot = id; | |
| 1236 if (!ui.active && ui.down) | |
| 1237 ui.active = id; | |
| 1238 } | |
| 1239 | |
| 1240 pressed = (ui.active == id); | |
| 1241 } | |
| 1242 | |
| 1243 if (is_button) | |
| 1244 { | |
| 1245 ui_draw_bevel_rect(area, UI_COLOR_BUTTON, pressed); | |
| 1246 glColorHex(disabled? UI_COLOR_TEXT_GRAY : UI_COLOR_TEXT_FG); | |
| 1247 ui_draw_string(area.x0 + 6+pressed, area.y0+3+pressed, label); | |
| 1248 glBegin(GL_TRIANGLES); | |
| 1249 glVertex2f(area.x1+pressed-8-10, area.y0+pressed+9); | |
| 1250 glVertex2f(area.x1+pressed-8, area.y0+pressed+9); | |
| 1251 glVertex2f(area.x1+pressed-8-4, area.y0+pressed+14); | |
| 1252 glEnd(); | |
| 1253 } | |
| 1254 else | |
| 1255 { | |
| 1256 fz_irect arrow = { area.x1-22, area.y0+2, area.x1-2, area.y1-2 }; | |
| 1257 ui_draw_bevel_rect(area, UI_COLOR_TEXT_BG, 1); | |
| 1258 glColorHex(disabled ? UI_COLOR_TEXT_GRAY : UI_COLOR_TEXT_FG); | |
| 1259 ui_draw_string(area.x0 + 6, area.y0+3, label); | |
| 1260 ui_draw_ibevel_rect(arrow, UI_COLOR_BUTTON, pressed); | |
| 1261 | |
| 1262 glColorHex(disabled ? UI_COLOR_TEXT_GRAY : UI_COLOR_TEXT_FG); | |
| 1263 glBegin(GL_TRIANGLES); | |
| 1264 glVertex2f(area.x1+pressed-8-10, area.y0+pressed+9); | |
| 1265 glVertex2f(area.x1+pressed-8, area.y0+pressed+9); | |
| 1266 glVertex2f(area.x1+pressed-8-4, area.y0+pressed+14); | |
| 1267 glEnd(); | |
| 1268 } | |
| 1269 | |
| 1270 if (pressed) | |
| 1271 { | |
| 1272 ui.overlay = 1; | |
| 1273 | |
| 1274 glNewList(ui.overlay_list, GL_COMPILE); | |
| 1275 | |
| 1276 /* Area inside the border line */ | |
| 1277 menu_area.x0 = area.x0+1; | |
| 1278 menu_area.x1 = area.x1-1; // TODO: width of submenu | |
| 1279 if (area.y1+2 + count * ui.lineheight < ui.window_h) | |
| 1280 { | |
| 1281 menu_area.y0 = area.y1+2; | |
| 1282 menu_area.y1 = menu_area.y0 + count * ui.lineheight; | |
| 1283 } | |
| 1284 else | |
| 1285 { | |
| 1286 menu_area.y1 = area.y0-2; | |
| 1287 menu_area.y0 = menu_area.y1 - count * ui.lineheight; | |
| 1288 } | |
| 1289 | |
| 1290 glColorHex(UI_COLOR_TEXT_FG); | |
| 1291 glRectf(menu_area.x0-1, menu_area.y0-1, menu_area.x1+1, menu_area.y1+1); | |
| 1292 glColorHex(UI_COLOR_TEXT_BG); | |
| 1293 glRectf(menu_area.x0, menu_area.y0, menu_area.x1, menu_area.y1); | |
| 1294 | |
| 1295 ui_pack_push(menu_area); | |
| 1296 ui_layout(T, X, NW, 0, 0); | |
| 1297 } | |
| 1298 | |
| 1299 return pressed; | |
| 1300 } | |
| 1301 | |
| 1302 int ui_popup_item(const char *title) | |
| 1303 { | |
| 1304 return ui_popup_item_aux(title, 0); | |
| 1305 } | |
| 1306 | |
| 1307 int ui_popup_item_aux(const char *title, int flags) | |
| 1308 { | |
| 1309 fz_irect area = ui_pack(0, ui.lineheight); | |
| 1310 int disabled = (flags & 1); | |
| 1311 | |
| 1312 if (!disabled && ui_mouse_inside(area)) | |
| 1313 { | |
| 1314 ui.hot = title; | |
| 1315 glColorHex(UI_COLOR_TEXT_SEL_BG); | |
| 1316 glRectf(area.x0, area.y0, area.x1, area.y1); | |
| 1317 glColorHex(UI_COLOR_TEXT_SEL_FG); | |
| 1318 ui_draw_string(area.x0 + 4, area.y0, title); | |
| 1319 } | |
| 1320 else | |
| 1321 { | |
| 1322 glColorHex(disabled ? UI_COLOR_TEXT_GRAY : UI_COLOR_TEXT_FG); | |
| 1323 ui_draw_string(area.x0 + 4, area.y0, title); | |
| 1324 } | |
| 1325 | |
| 1326 return !disabled && ui.hot == title && !ui.down; | |
| 1327 } | |
| 1328 | |
| 1329 void ui_popup_end(void) | |
| 1330 { | |
| 1331 glEndList(); | |
| 1332 ui_pack_pop(); | |
| 1333 } | |
| 1334 | |
| 1335 int ui_select(const void *id, const char *current, const char *options[], int n) | |
| 1336 { | |
| 1337 return ui_select_aux(id, current, options, n, 0); | |
| 1338 } | |
| 1339 | |
| 1340 int ui_select_aux(const void *id, const char *current, const char *options[], int n, int flags) | |
| 1341 { | |
| 1342 int i, choice = -1; | |
| 1343 if (ui_popup_aux(id, current, 0, n, flags)) | |
| 1344 { | |
| 1345 for (i = 0; i < n; ++i) | |
| 1346 if (ui_popup_item_aux(options[i], flags)) | |
| 1347 choice = i; | |
| 1348 ui_popup_end(); | |
| 1349 } | |
| 1350 return choice; | |
| 1351 } | |
| 1352 | |
| 1353 void ui_select_annot(pdf_annot *annot) | |
| 1354 { | |
| 1355 pdf_drop_annot(ctx, ui.selected_annot); | |
| 1356 ui.selected_annot = annot; | |
| 1357 } |
