/usr/include/faust/gui/faustgtk.h is in faust-common 0.9.95~repack1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 | /************************************************************************
FAUST Architecture File
Copyright (C) 2003-2016 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This Architecture section 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/>.
EXCEPTION : As a special exception, you may create a larger work
that contains this FAUST architecture section and distribute
that work under terms of your choice, so long as this FAUST
architecture section is not modified.
************************************************************************
************************************************************************/
#ifndef FAUST_GTKUI_H
#define FAUST_GTKUI_H
#include "faust/gui/GUI.h"
#include "faust/gui/MetaDataUI.h"
/******************************************************************************
*******************************************************************************
GRAPHIC USER INTERFACE
gtk interface
*******************************************************************************
*******************************************************************************/
#include <string>
#include <set>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <assert.h>
#define stackSize 256
// Insertion modes
#define kSingleMode 0
#define kBoxMode 1
#define kTabMode 2
//------------ calculate needed precision
static int precision(double n)
{
if (n < 0.009999) return 3;
else if (n < 0.099999) return 2;
else if (n < 0.999999) return 1;
else return 0;
}
namespace gtk_knob
{
class GtkKnob
{
private:
double start_x, start_y, max_value;
public:
GtkRange parent;
int last_quadrant;
GtkKnob();
~GtkKnob();
GtkWidget* gtk_knob_new_with_adjustment(GtkAdjustment *_adjustment);
};
#define GTK_TYPE_KNOB (gtk_knob_get_type())
#define GTK_KNOB(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_KNOB, GtkKnob))
#define GTK_IS_KNOB(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_KNOB))
#define GTK_KNOB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_KNOB, GtkKnobClass))
#define GTK_IS_KNOB_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_KNOB))
GtkKnob::GtkKnob()
// GtkKnob constructor
{}
GtkKnob::~GtkKnob()
{
// Nothing specific to do...
}
struct GtkKnobClass {
GtkRangeClass parent_class;
int knob_x;
int knob_y;
int knob_step;
int button_is;
};
//------forward declaration
GType gtk_knob_get_type ();
/****************************************************************
** calculate the knop pointer with dead zone
*/
const double scale_zero = 20 * (M_PI/180); // defines "dead zone" for knobs
static void knob_expose(GtkWidget* widget, int knob_x, int knob_y, GdkEventExpose *event, int arc_offset)
{
/** check resize **/
int grow;
if(widget->allocation.width > widget->allocation.height) {
grow = widget->allocation.height;
} else {
grow = widget->allocation.width;
}
knob_x = grow-4;
knob_y = grow-4;
/** get values for the knob **/
GtkAdjustment *adj = gtk_range_get_adjustment(GTK_RANGE(widget));
int knobx = (widget->allocation.x+2 + (widget->allocation.width-4 - knob_x) * 0.5);
int knoby = (widget->allocation.y+2 + (widget->allocation.height-4 - knob_y) * 0.5);
int knobx1 = (widget->allocation.x+2 + (widget->allocation.width-4)* 0.5);
int knoby1 = (widget->allocation.y+2 + (widget->allocation.height-4) * 0.5);
double knobstate = (adj->value - adj->lower) / (adj->upper - adj->lower);
double angle = scale_zero + knobstate * 2 * (M_PI - scale_zero);
double knobstate1 = (0. - adj->lower) / (adj->upper - adj->lower);
double pointer_off = knob_x/6;
double radius = std::min(knob_x-pointer_off, knob_y-pointer_off) / 2;
double lengh_x = (knobx+radius+pointer_off/2) - radius * sin(angle);
double lengh_y = (knoby+radius+pointer_off/2) + radius * cos(angle);
double radius1 = std::min(knob_x, knob_y) / 2 ;
/** get widget forground color convert to cairo **/
GtkStyle *style = gtk_widget_get_style (widget);
double r = std::min(0.6,style->fg[gtk_widget_get_state(widget)].red/65535.0),
g = std::min(0.6,style->fg[gtk_widget_get_state(widget)].green/65535.0),
b = std::min(0.6,style->fg[gtk_widget_get_state(widget)].blue/65535.0);
/** paint focus **/
if (GTK_WIDGET_HAS_FOCUS(widget)) {
gtk_paint_focus(widget->style, widget->window, GTK_STATE_NORMAL, NULL, widget, NULL,
knobx-2, knoby-2, knob_x+4, knob_y+4);
}
/** create clowing knobs with cairo **/
cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(widget->window));
GdkRegion *region;
region = gdk_region_rectangle (&widget->allocation);
gdk_region_intersect (region, event->region);
gdk_cairo_region (cr, region);
cairo_clip (cr);
cairo_arc(cr,knobx1+arc_offset, knoby1+arc_offset, knob_x/2.1, 0, 2 * M_PI );
cairo_pattern_t*pat =
cairo_pattern_create_radial (knobx1+arc_offset-knob_x/6,knoby1+arc_offset-knob_x/6, 1,knobx1+arc_offset,knoby1+arc_offset,knob_x/2.1 );
if (adj->lower<0 && adj->value>0.) {
cairo_pattern_add_color_stop_rgb (pat, 0, r+0.4, g+0.4 + knobstate-knobstate1, b+0.4);
cairo_pattern_add_color_stop_rgb (pat, 0.7, r+0.15, g+0.15 + (knobstate-knobstate1)*0.5, b+0.15);
cairo_pattern_add_color_stop_rgb (pat, 1, r, g, b);
} else if(adj->lower<0 && adj->value<=0.) {
cairo_pattern_add_color_stop_rgb (pat, 0, r+0.4 +knobstate1- knobstate, g+0.4, b+0.4);
cairo_pattern_add_color_stop_rgb (pat, 0.7, r+0.15 +(knobstate1- knobstate)*0.5, g+0.15, b+0.15);
cairo_pattern_add_color_stop_rgb (pat, 1, r, g, b);
} else {
cairo_pattern_add_color_stop_rgb (pat, 0, r+0.4, g+0.4 +knobstate, b+0.4);
cairo_pattern_add_color_stop_rgb (pat, 0.7, r+0.15, g+0.15 + knobstate*0.5, b+0.15);
cairo_pattern_add_color_stop_rgb (pat, 1, r, g, b);
}
cairo_set_source (cr, pat);
cairo_fill_preserve (cr);
gdk_cairo_set_source_color(cr, gtk_widget_get_style (widget)->fg);
cairo_set_line_width(cr, 2.0);
cairo_stroke(cr);
/** create a rotating pointer on the kob**/
cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
cairo_set_line_width(cr,std::max(3, std::min(7, knob_x/15)));
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
cairo_move_to(cr, knobx+radius1, knoby+radius1);
cairo_line_to(cr,lengh_x,lengh_y);
cairo_stroke(cr);
cairo_set_source_rgb(cr, 0.9, 0.9, 0.9);
cairo_set_line_width(cr,std::min(5, std::max(1,knob_x/30)));
cairo_move_to(cr, knobx+radius1, knoby+radius1);
cairo_line_to(cr,lengh_x,lengh_y);
cairo_stroke(cr);
cairo_pattern_destroy (pat);
gdk_region_destroy (region);
cairo_destroy(cr);
}
/****************************************************************
** general expose events for all "knob" controllers
*/
//----------- draw the Knob when moved
static gboolean gtk_knob_expose(GtkWidget* widget, GdkEventExpose *event)
{
g_assert(GTK_IS_KNOB(widget));
GtkKnobClass *klass = GTK_KNOB_CLASS(GTK_OBJECT_GET_CLASS(widget));
knob_expose(widget, klass->knob_x, klass->knob_y, event, 0);
return true;
}
/****************************************************************
** set initial size for GdkDrawable per type
*/
static void gtk_knob_size_request(GtkWidget* widget, GtkRequisition *requisition)
{
g_assert(GTK_IS_KNOB(widget));
GtkKnobClass *klass = GTK_KNOB_CLASS(GTK_OBJECT_GET_CLASS(widget));
requisition->width = klass->knob_x;
requisition->height = klass->knob_y;
}
/****************************************************************
** set value from key bindings
*/
static void gtk_knob_set_value(GtkWidget* widget, int dir_down)
{
g_assert(GTK_IS_KNOB(widget));
GtkAdjustment *adj = gtk_range_get_adjustment(GTK_RANGE(widget));
int oldstep = (int)(0.5f + (adj->value - adj->lower) / adj->step_increment);
int step;
int nsteps = (int)(0.5f + (adj->upper - adj->lower) / adj->step_increment);
if (dir_down) {
step = oldstep - 1;
} else {
step = oldstep + 1;
}
FAUSTFLOAT value = adj->lower + step * double(adj->upper - adj->lower) / nsteps;
gtk_widget_grab_focus(widget);
gtk_range_set_value(GTK_RANGE(widget), value);
}
/****************************************************************
** keyboard bindings
*/
static gboolean gtk_knob_key_press(GtkWidget* widget, GdkEventKey *event)
{
g_assert(GTK_IS_KNOB(widget));
GtkAdjustment *adj = gtk_range_get_adjustment(GTK_RANGE(widget));
switch (event->keyval) {
case GDK_Home:
gtk_range_set_value(GTK_RANGE(widget), adj->lower);
return true;
case GDK_End:
gtk_range_set_value(GTK_RANGE(widget), adj->upper);
return true;
case GDK_Up:
gtk_knob_set_value(widget, 0);
return true;
case GDK_Right:
gtk_knob_set_value(widget, 0);
return true;
case GDK_Down:
gtk_knob_set_value(widget, 1);
return true;
case GDK_Left:
gtk_knob_set_value(widget, 1);
return true;
}
return false;
}
/****************************************************************
** alternative (radial) knob motion mode (ctrl + mouse pressed)
*/
static void knob_pointer_event(GtkWidget* widget, gdouble x, gdouble y, int knob_x, int knob_y,
bool drag, int state)
{
static double last_y = 2e20;
GtkKnob *knob = GTK_KNOB(widget);
GtkAdjustment *adj = gtk_range_get_adjustment(GTK_RANGE(widget));
double radius = std::min(knob_x, knob_y) / 2;
int knobx = (widget->allocation.width - knob_x) / 2;
int knoby = (widget->allocation.height - knob_y) / 2;
double posx = (knobx + radius) - x; // x axis right -> left
double posy = (knoby + radius) - y; // y axis top -> bottom
double value;
if (!drag) {
if (state & GDK_CONTROL_MASK) {
last_y = 2e20;
return;
} else {
last_y = posy;
}
}
if (last_y < 1e20) { // in drag started with Control Key
const double scaling = 0.005;
double scal = (state & GDK_SHIFT_MASK ? scaling*0.1 : scaling);
value = (last_y - posy) * scal;
last_y = posy;
gtk_range_set_value(GTK_RANGE(widget), adj->value - value * (adj->upper - adj->lower));
return;
}
double angle = atan2(-posx, posy) + M_PI; // clockwise, zero at 6 o'clock, 0 .. 2*M_PI
if (drag) {
// block "forbidden zone" and direct moves between quadrant 1 and 4
int quadrant = 1 + int(angle/M_PI_2);
if (knob->last_quadrant == 1 && (quadrant == 3 || quadrant == 4)) {
angle = scale_zero;
} else if (knob->last_quadrant == 4 && (quadrant == 1 || quadrant == 2)) {
angle = 2*M_PI - scale_zero;
} else {
if (angle < scale_zero) {
angle = scale_zero;
} else if (angle > 2*M_PI - scale_zero) {
angle = 2*M_PI - scale_zero;
}
knob->last_quadrant = quadrant;
}
} else {
if (angle < scale_zero) {
angle = scale_zero;
} else if (angle > 2*M_PI - scale_zero) {
angle = 2*M_PI - scale_zero;
}
knob->last_quadrant = 0;
}
angle = (angle - scale_zero) / (2 * (M_PI-scale_zero)); // normalize to 0..1
gtk_range_set_value(GTK_RANGE(widget), adj->lower + angle * (adj->upper - adj->lower));
}
/****************************************************************
** mouse button pressed set value
*/
static gboolean gtk_knob_button_press(GtkWidget* widget, GdkEventButton *event)
{
g_assert(GTK_IS_KNOB(widget));
GtkKnobClass *klass = GTK_KNOB_CLASS(GTK_OBJECT_GET_CLASS(widget));
switch (event->button) {
case 1: // left button
gtk_widget_grab_focus(widget);
gtk_widget_grab_default (widget);
gtk_grab_add(widget);
klass->button_is = 1;
knob_pointer_event(widget, event->x, event->y, klass->knob_x, klass->knob_y,
false, event->state);
break;
case 2: //wheel
klass->button_is = 2;
break;
case 3: // right button
klass->button_is = 3;
break;
default: // do nothing
break;
}
return true;
}
/****************************************************************
** mouse button release
*/
static gboolean gtk_knob_button_release(GtkWidget* widget, GdkEventButton *event)
{
g_assert(GTK_IS_KNOB(widget));
GTK_KNOB_CLASS(GTK_OBJECT_GET_CLASS(widget))->button_is = 0;
if (GTK_WIDGET_HAS_GRAB(widget))
gtk_grab_remove(widget);
return false;
}
/****************************************************************
** set the value from mouse movement
*/
static gboolean gtk_knob_pointer_motion(GtkWidget* widget, GdkEventMotion *event)
{
g_assert(GTK_IS_KNOB(widget));
GtkKnobClass *klass = GTK_KNOB_CLASS(GTK_OBJECT_GET_CLASS(widget));
gdk_event_request_motions (event);
if (GTK_WIDGET_HAS_GRAB(widget)) {
knob_pointer_event(widget, event->x, event->y, klass->knob_x, klass->knob_y,
true, event->state);
}
return false;
}
/****************************************************************
** set value from mouseweel
*/
static gboolean gtk_knob_scroll(GtkWidget* widget, GdkEventScroll *event)
{
usleep(5000);
gtk_knob_set_value(widget, event->direction);
return false;
}
/****************************************************************
** init the GtkKnobClass
*/
static void gtk_knob_class_init(GtkKnobClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
/** set here the sizes and steps for the used knob **/
//--------- small knob size and steps
klass->knob_x = 30;
klass->knob_y = 30;
klass->knob_step = 86;
//--------- event button
klass->button_is = 0;
//--------- connect the events with funktions
widget_class->expose_event = gtk_knob_expose;
widget_class->size_request = gtk_knob_size_request;
widget_class->button_press_event = gtk_knob_button_press;
widget_class->button_release_event = gtk_knob_button_release;
widget_class->motion_notify_event = gtk_knob_pointer_motion;
widget_class->key_press_event = gtk_knob_key_press;
widget_class->scroll_event = gtk_knob_scroll;
}
/****************************************************************
** init the Knob type/size
*/
static void gtk_knob_init(GtkKnob *knob)
{
GtkWidget* widget = GTK_WIDGET(knob);
GtkKnobClass *klass = GTK_KNOB_CLASS(GTK_OBJECT_GET_CLASS(widget));
GTK_WIDGET_SET_FLAGS (GTK_WIDGET(knob), GTK_CAN_FOCUS);
GTK_WIDGET_SET_FLAGS (GTK_WIDGET(knob), GTK_CAN_DEFAULT);
widget->requisition.width = klass->knob_x;
widget->requisition.height = klass->knob_y;
}
/****************************************************************
** redraw when value changed
*/
static gboolean gtk_knob_value_changed(gpointer obj)
{
GtkWidget* widget = (GtkWidget* )obj;
gtk_widget_queue_draw(widget);
return false;
}
/****************************************************************
** create small knob
*/
GtkWidget* GtkKnob::gtk_knob_new_with_adjustment(GtkAdjustment *_adjustment)
{
GtkWidget* widget = GTK_WIDGET( g_object_new (GTK_TYPE_KNOB, NULL ));
GtkKnob *knob = GTK_KNOB(widget);
knob->last_quadrant = 0;
if (widget) {
gtk_range_set_adjustment(GTK_RANGE(widget), _adjustment);
g_signal_connect(GTK_OBJECT(widget), "value-changed",
G_CALLBACK(gtk_knob_value_changed), widget);
}
return widget;
}
/****************************************************************
** get the Knob type
*/
GType gtk_knob_get_type(void)
{
static GType kn_type = 0;
if (!kn_type) {
static const GTypeInfo kn_info = {
sizeof(GtkKnobClass), NULL, NULL, (GClassInitFunc)gtk_knob_class_init, NULL, NULL, sizeof(GtkKnob), 0, (GInstanceInitFunc)gtk_knob_init, NULL
};
kn_type = g_type_register_static(GTK_TYPE_RANGE, "GtkKnob", &kn_info, (GTypeFlags)0);
}
return kn_type;
}
}/* end of gtk_knob namespace */
gtk_knob::GtkKnob myGtkKnob;
class GTKUI : public GUI, public MetaDataUI
{
private:
static bool fInitialized;
protected:
GtkWidget* fWindow;
int fTop;
GtkWidget* fBox[stackSize];
int fMode[stackSize];
bool fStopped;
GtkWidget* addWidget(const char* label, GtkWidget* w);
virtual void pushBox(int mode, GtkWidget* w);
public:
static const gboolean expand = true;
static const gboolean fill = true;
static const gboolean homogene = false;
GTKUI(char * name, int* pargc, char*** pargv);
// -- Labels and metadata
virtual void declare(FAUSTFLOAT* zone, const char* key, const char* value);
virtual int checkLabelOptions(GtkWidget* widget, const std::string& fullLabel, std::string& simplifiedLabel);
virtual void checkForTooltip(FAUSTFLOAT* zone, GtkWidget* widget);
// -- layout groups
virtual void openTabBox(const char* label = "");
virtual void openHorizontalBox(const char* label = "");
virtual void openVerticalBox(const char* label = "");
virtual void closeBox();
// -- active widgets
virtual void addButton(const char* label, FAUSTFLOAT* zone);
virtual void addCheckButton(const char* label, FAUSTFLOAT* zone);
virtual void addVerticalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step);
virtual void addHorizontalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step);
virtual void addNumEntry(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step);
// -- passive display widgets
virtual void addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max);
virtual void addVerticalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max);
// -- layout groups - internal
virtual void openFrameBox(const char* label);
// -- extra widget's layouts
virtual void openDialogBox(const char* label, FAUSTFLOAT* zone);
virtual void openEventBox(const char* label = "");
virtual void openHandleBox(const char* label = "");
virtual void openExpanderBox(const char* label, FAUSTFLOAT* zone);
virtual void adjustStack(int n);
// -- active widgets - internal
virtual void addToggleButton(const char* label, FAUSTFLOAT* zone);
virtual void addKnob(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step);
// -- passive display widgets - internal
virtual void addNumDisplay(const char* label, FAUSTFLOAT* zone, int precision);
virtual void addTextDisplay(const char* label, FAUSTFLOAT* zone, const char* names[], FAUSTFLOAT min, FAUSTFLOAT max);
virtual void show();
virtual bool run();
};
/******************************************************************************
*******************************************************************************
GRAPHIC USER INTERFACE (v2)
gtk implementation
*******************************************************************************
*******************************************************************************/
// global static fields
bool GTKUI::fInitialized = false;
static gint delete_event(GtkWidget* widget, GdkEvent *event, gpointer data)
{
return FALSE;
}
static void destroy_event(GtkWidget* widget, gpointer data)
{
gtk_main_quit();
}
GTKUI::GTKUI(char * name, int* pargc, char*** pargv)
{
if (!fInitialized) {
gtk_init(pargc, pargv);
fInitialized = true;
}
fWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
//gtk_container_set_border_width (GTK_CONTAINER (fWindow), 10);
gtk_window_set_title (GTK_WINDOW (fWindow), name);
gtk_signal_connect (GTK_OBJECT (fWindow), "delete_event", GTK_SIGNAL_FUNC (delete_event), NULL);
gtk_signal_connect (GTK_OBJECT (fWindow), "destroy", GTK_SIGNAL_FUNC (destroy_event), NULL);
fTop = 0;
fBox[fTop] = gtk_vbox_new (homogene, 4);
fMode[fTop] = kBoxMode;
gtk_container_add (GTK_CONTAINER (fWindow), fBox[fTop]);
fStopped = false;
}
// empilement des boites
void GTKUI::pushBox(int mode, GtkWidget* w)
{
++fTop;
assert(fTop < stackSize);
fMode[fTop] = mode;
fBox[fTop] = w;
}
/**
* Remove n levels from the stack S before the top level
* adjustStack(n): S -> S' with S' = S(0),S(n+1),S(n+2),...
*/
void GTKUI::adjustStack(int n)
{
if (n > 0) {
assert(fTop >= n);
fTop -= n;
fMode[fTop] = fMode[fTop+n];
fBox[fTop] = fBox[fTop+n];
}
}
void GTKUI::closeBox()
{
--fTop;
assert(fTop >= 0);
}
/**
* Analyses the widget zone metadata declarations and takes
* appropriate actions
*/
void GTKUI::declare(FAUSTFLOAT* zone, const char* key, const char* value)
{
MetaDataUI::declare(zone, key, value);
}
/**
* Analyses a full label and activates the relevant options. returns a simplified
* label (without options) and an amount of stack adjustement (in case additional
* containers were pushed on the stack).
*/
int GTKUI::checkLabelOptions(GtkWidget* widget, const std::string& fullLabel, std::string& simplifiedLabel)
{
std::map<std::string, std::string> metadata;
extractMetadata(fullLabel, simplifiedLabel, metadata);
if (metadata.count("tooltip")) {
gtk_tooltips_set_tip (gtk_tooltips_new (), widget, metadata["tooltip"].c_str(), NULL);
}
if (metadata["option"] == "detachable") {
openHandleBox(simplifiedLabel.c_str());
return 1;
}
//---------------------
if (gGroupTooltip != std::string()) {
gtk_tooltips_set_tip (gtk_tooltips_new (), widget, gGroupTooltip.c_str(), NULL);
gGroupTooltip = std::string();
}
//----------------------
// no adjustement of the stack needed
return 0;
}
/**
* Check if a tooltip is associated to a zone and add it to the corresponding widget
*/
void GTKUI::checkForTooltip(FAUSTFLOAT* zone, GtkWidget* widget)
{
if (fTooltip.count(zone)) {
gtk_tooltips_set_tip (gtk_tooltips_new (), widget, fTooltip[zone].c_str(), NULL);
}
}
// The different boxes
void GTKUI::openFrameBox(const char* label)
{
GtkWidget* box = gtk_frame_new (label);
//gtk_container_set_border_width (GTK_CONTAINER (box), 10);
pushBox(kSingleMode, addWidget(label, box));
}
void GTKUI::openTabBox(const char* fullLabel)
{
std::string label;
GtkWidget* widget = gtk_notebook_new();
int adjust = checkLabelOptions(widget, fullLabel, label);
pushBox(kTabMode, addWidget(label.c_str(), widget));
// adjust stack because otherwise Handlebox will remain open
adjustStack(adjust);
}
void GTKUI::openHorizontalBox(const char* fullLabel)
{
std::string label;
GtkWidget* box = gtk_hbox_new (homogene, 4);
int adjust = checkLabelOptions(box, fullLabel, label);
gtk_container_set_border_width (GTK_CONTAINER (box), 10);
label = startWith(label, "0x") ? "" : label;
if (fMode[fTop] != kTabMode && label[0] != 0) {
GtkWidget* frame = addWidget(label.c_str(), gtk_frame_new (label.c_str()));
gtk_container_add (GTK_CONTAINER(frame), box);
gtk_widget_show(box);
pushBox(kBoxMode, box);
} else {
pushBox(kBoxMode, addWidget(label.c_str(), box));
}
// adjust stack because otherwise Handlebox will remain open
adjustStack(adjust);
}
void GTKUI::openVerticalBox(const char* fullLabel)
{
std::string label;
GtkWidget* box = gtk_vbox_new (homogene, 4);
int adjust = checkLabelOptions(box, fullLabel, label);
gtk_container_set_border_width (GTK_CONTAINER (box), 10);
label = startWith(label, "0x") ? "" : label;
if (fMode[fTop] != kTabMode && label[0] != 0) {
GtkWidget* frame = addWidget(label.c_str(), gtk_frame_new (label.c_str()));
gtk_container_add (GTK_CONTAINER(frame), box);
gtk_widget_show(box);
pushBox(kBoxMode, box);
} else {
pushBox(kBoxMode, addWidget(label.c_str(), box));
}
// adjust stack because otherwise Handlebox will remain open
adjustStack(adjust);
}
void GTKUI::openHandleBox(const char* label)
{
GtkWidget* box = gtk_hbox_new (homogene, 4);
gtk_container_set_border_width (GTK_CONTAINER (box), 2);
label = startWith(label, "0x") ? "" : label;
if (fMode[fTop] != kTabMode && label[0] != 0) {
GtkWidget* frame = addWidget(label, gtk_handle_box_new ());
gtk_container_add (GTK_CONTAINER(frame), box);
gtk_widget_show(box);
pushBox(kBoxMode, box);
} else {
pushBox(kBoxMode, addWidget(label, box));
}
}
void GTKUI::openEventBox(const char* label)
{
GtkWidget* box = gtk_hbox_new (homogene, 4);
gtk_container_set_border_width (GTK_CONTAINER (box), 2);
label = startWith(label, "0x") ? "" : label;
if (fMode[fTop] != kTabMode && label[0] != 0) {
GtkWidget* frame = addWidget(label, gtk_event_box_new ());
gtk_container_add (GTK_CONTAINER(frame), box);
gtk_widget_show(box);
pushBox(kBoxMode, box);
} else {
pushBox(kBoxMode, addWidget(label, box));
}
}
struct uiExpanderBox : public uiItem
{
GtkExpander* fButton;
uiExpanderBox(GUI* ui, FAUSTFLOAT* zone, GtkExpander* b) : uiItem(ui, zone), fButton(b) {}
static void expanded (GtkWidget* widget, gpointer data)
{
FAUSTFLOAT v = gtk_expander_get_expanded (GTK_EXPANDER(widget));
if (v == 1.000000) {
v = 0;
} else {
v = 1;
}
((uiItem*)data)->modifyZone(v);
}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
gtk_expander_set_expanded(GTK_EXPANDER(fButton), v);
}
};
void GTKUI::openExpanderBox(const char* label, FAUSTFLOAT* zone)
{
*zone = 0.0;
GtkWidget* box = gtk_hbox_new (homogene, 4);
gtk_container_set_border_width (GTK_CONTAINER (box), 2);
label = startWith(label, "0x") ? "" : label;
if (fMode[fTop] != kTabMode && label[0] != 0) {
GtkWidget* frame = addWidget(label, gtk_expander_new (label));
gtk_container_add (GTK_CONTAINER(frame), box);
uiExpanderBox* c = new uiExpanderBox(this, zone, GTK_EXPANDER(frame));
gtk_signal_connect (GTK_OBJECT (frame), "activate", GTK_SIGNAL_FUNC (uiExpanderBox::expanded), (gpointer)c);
gtk_widget_show(box);
pushBox(kBoxMode, box);
} else {
pushBox(kBoxMode, addWidget(label, box));
}
}
GtkWidget* GTKUI::addWidget(const char* label, GtkWidget* w)
{
switch (fMode[fTop]) {
case kSingleMode : gtk_container_add (GTK_CONTAINER(fBox[fTop]), w); break;
case kBoxMode : gtk_box_pack_start (GTK_BOX(fBox[fTop]), w, expand, fill, 0); break;
case kTabMode : gtk_notebook_append_page (GTK_NOTEBOOK(fBox[fTop]), w, gtk_label_new(label)); break;
}
gtk_widget_show (w);
return w;
}
// --------------------------- Press button ---------------------------
struct uiButton : public uiItem
{
GtkButton* fButton;
uiButton(GUI* ui, FAUSTFLOAT* zone, GtkButton* b) : uiItem(ui, zone), fButton(b) {}
static void pressed(GtkWidget* widget, gpointer data)
{
uiItem* c = (uiItem*) data;
c->modifyZone(1.0);
}
static void released(GtkWidget* widget, gpointer data)
{
uiItem* c = (uiItem*) data;
c->modifyZone(0.0);
}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
if (v > 0.0) gtk_button_pressed(fButton); else gtk_button_released(fButton);
}
};
void GTKUI::addButton(const char* label, FAUSTFLOAT* zone)
{
*zone = 0.0;
GtkWidget* button = gtk_button_new_with_label (label);
addWidget(label, button);
uiButton* c = new uiButton(this, zone, GTK_BUTTON(button));
gtk_signal_connect (GTK_OBJECT (button), "pressed", GTK_SIGNAL_FUNC (uiButton::pressed), (gpointer) c);
gtk_signal_connect (GTK_OBJECT (button), "released", GTK_SIGNAL_FUNC (uiButton::released), (gpointer) c);
checkForTooltip(zone, button);
}
// --------------------------- Toggle Buttons ---------------------------
struct uiToggleButton : public uiItem
{
GtkToggleButton* fButton;
uiToggleButton(GUI* ui, FAUSTFLOAT* zone, GtkToggleButton* b) : uiItem(ui, zone), fButton(b) {}
static void toggled (GtkWidget* widget, gpointer data)
{
FAUSTFLOAT v = (GTK_TOGGLE_BUTTON (widget)->active) ? 1.0 : 0.0;
((uiItem*)data)->modifyZone(v);
}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
gtk_toggle_button_set_active(fButton, v > 0.0);
}
};
void GTKUI::addToggleButton(const char* label, FAUSTFLOAT* zone)
{
*zone = 0.0;
GtkWidget* button = gtk_toggle_button_new_with_label (label);
addWidget(label, button);
uiToggleButton* c = new uiToggleButton(this, zone, GTK_TOGGLE_BUTTON(button));
gtk_signal_connect (GTK_OBJECT (button), "toggled", GTK_SIGNAL_FUNC (uiToggleButton::toggled), (gpointer) c);
checkForTooltip(zone, button);
}
void show_dialog(GtkWidget* widget, gpointer data)
{
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget))) {
gtk_widget_show(GTK_WIDGET(data));
gint root_x, root_y;
gtk_window_get_position (GTK_WINDOW(data), &root_x, &root_y);
root_y -= 120;
gtk_window_move(GTK_WINDOW(data), root_x, root_y);
}
else gtk_widget_hide(GTK_WIDGET(data));
}
static gboolean deleteevent(GtkWidget* widget, gpointer data)
{
return true;
}
void GTKUI::openDialogBox(const char* label, FAUSTFLOAT* zone)
{
// create toplevel window and set properties
GtkWidget* dialog = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_decorated(GTK_WINDOW(dialog), true);
gtk_window_set_deletable(GTK_WINDOW(dialog), FALSE);
gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
gtk_window_set_gravity(GTK_WINDOW(dialog), GDK_GRAVITY_SOUTH);
gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(fWindow));
gtk_window_set_position (GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
gtk_window_set_keep_below (GTK_WINDOW(dialog), FALSE);
gtk_window_set_title (GTK_WINDOW (dialog), label);
g_signal_connect (G_OBJECT (dialog), "delete_event", G_CALLBACK (deleteevent), NULL);
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), true);
GtkWidget* box = gtk_hbox_new (homogene, 4);
*zone = 0.0;
GtkWidget* button = gtk_toggle_button_new ();
gtk_signal_connect (GTK_OBJECT (button), "toggled", GTK_SIGNAL_FUNC (show_dialog), (gpointer) dialog);
gtk_container_add (GTK_CONTAINER(fBox[fTop]), button);
gtk_container_add (GTK_CONTAINER(dialog), box);
gtk_widget_show (button);
gtk_widget_show(box);
pushBox(kBoxMode, box);
}
// --------------------------- Check Button ---------------------------
struct uiCheckButton : public uiItem
{
GtkToggleButton* fButton;
uiCheckButton(GUI* ui, FAUSTFLOAT* zone, GtkToggleButton* b) : uiItem(ui, zone), fButton(b) {}
static void toggled (GtkWidget* widget, gpointer data)
{
FAUSTFLOAT v = (GTK_TOGGLE_BUTTON (widget)->active) ? 1.0 : 0.0;
((uiItem*)data)->modifyZone(v);
}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
gtk_toggle_button_set_active(fButton, v > 0.0);
}
};
void GTKUI::addCheckButton(const char* label, FAUSTFLOAT* zone)
{
*zone = 0.0;
GtkWidget* button = gtk_check_button_new_with_label (label);
addWidget(label, button);
uiCheckButton* c = new uiCheckButton(this, zone, GTK_TOGGLE_BUTTON(button));
gtk_signal_connect (GTK_OBJECT (button), "toggled", GTK_SIGNAL_FUNC(uiCheckButton::toggled), (gpointer) c);
checkForTooltip(zone, button);
}
// --------------------------- Adjustmenty based widgets ---------------------------
struct uiAdjustment : public uiItem
{
GtkAdjustment* fAdj;
uiAdjustment(GUI* ui, FAUSTFLOAT* zone, GtkAdjustment* adj) : uiItem(ui, zone), fAdj(adj) {}
static void changed (GtkWidget* widget, gpointer data)
{
FAUSTFLOAT v = GTK_ADJUSTMENT (widget)->value;
((uiItem*)data)->modifyZone(v);
}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
gtk_adjustment_set_value(fAdj, v);
}
};
// --------------------------- format knob value display ---------------------------
struct uiValueDisplay : public uiItem
{
GtkLabel* fLabel;
int fPrecision ;
uiValueDisplay(GUI* ui, FAUSTFLOAT* zone, GtkLabel* label, int precision)
: uiItem(ui, zone), fLabel(label), fPrecision(precision) {}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
char s[64];
if (fPrecision <= 0) {
snprintf(s, 63, "%d", int(v));
} else if (fPrecision > 3) {
snprintf(s, 63, "%f", v);
} else if (fPrecision == 1) {
const char* format[] = {"%.1f", "%.2f", "%.3f"};
snprintf(s, 63, format[1-1], v);
} else if (fPrecision == 2) {
const char* format[] = {"%.1f", "%.2f", "%.3f"};
snprintf(s, 63, format[2-1], v);
} else {
const char* format[] = {"%.1f", "%.2f", "%.3f"};
snprintf(s, 63, format[3-1], v);
}
gtk_label_set_text(fLabel, s);
}
};
// ------------------------------- Knob -----------------------------------------
void GTKUI::addKnob(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{
*zone = init;
GtkObject* adj = gtk_adjustment_new(init, min, max, step, 10*step, 0);
uiAdjustment* c = new uiAdjustment(this, zone, GTK_ADJUSTMENT(adj));
gtk_signal_connect(GTK_OBJECT (adj), "value-changed", GTK_SIGNAL_FUNC (uiAdjustment::changed), (gpointer) c);
GtkWidget* slider = gtk_vbox_new (FALSE, 0);
GtkWidget* fil = gtk_vbox_new (FALSE, 0);
GtkWidget* rei = gtk_vbox_new (FALSE, 0);
GtkWidget* re = myGtkKnob.gtk_knob_new_with_adjustment(GTK_ADJUSTMENT(adj));
GtkWidget* lw = gtk_label_new("");
new uiValueDisplay(this, zone, GTK_LABEL(lw),precision(step));
gtk_container_add (GTK_CONTAINER(rei), re);
if (fGuiSize[zone]) {
FAUSTFLOAT size = 30 * fGuiSize[zone];
gtk_widget_set_size_request(rei, size, size );
gtk_box_pack_start (GTK_BOX(slider), fil, true, true, 0);
gtk_box_pack_start (GTK_BOX(slider), rei, FALSE, FALSE, 0);
} else {
gtk_container_add (GTK_CONTAINER(slider), fil);
gtk_container_add (GTK_CONTAINER(slider), rei);
}
gtk_container_add (GTK_CONTAINER(slider), lw);
gtk_widget_show_all(slider);
label = startWith(label, "0x") ? "" : label;
if (label && label[0] != 0) {
openFrameBox(label);
addWidget(label, slider);
closeBox();
} else {
addWidget(label, slider);
}
checkForTooltip(zone, slider);
}
// -------------------------- Vertical Slider -----------------------------------
void GTKUI::addVerticalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{
if (isKnob(zone)) {
addKnob(label, zone, init, min, max, step);
return;
}
*zone = init;
GtkObject* adj = gtk_adjustment_new(init, min, max, step, 10*step, 0);
uiAdjustment* c = new uiAdjustment(this, zone, GTK_ADJUSTMENT(adj));
gtk_signal_connect (GTK_OBJECT (adj), "value-changed", GTK_SIGNAL_FUNC (uiAdjustment::changed), (gpointer) c);
GtkWidget* slider = gtk_vscale_new (GTK_ADJUSTMENT(adj));
gtk_scale_set_digits(GTK_SCALE(slider), precision(step));
FAUSTFLOAT size = 160;
if(fGuiSize[zone]) {
size = 160 * fGuiSize[zone];
}
gtk_widget_set_size_request(slider, -1, size);
gtk_range_set_inverted (GTK_RANGE(slider), true);
label = startWith(label, "0x") ? "" : label;
if (label && label[0] != 0) {
openFrameBox(label);
addWidget(label, slider);
closeBox();
} else {
addWidget(label, slider);
}
checkForTooltip(zone, slider);
}
// -------------------------- Horizontal Slider -----------------------------------
void GTKUI::addHorizontalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{
if (isKnob(zone)) {
addKnob(label, zone, init, min, max, step);
return;
}
*zone = init;
GtkObject* adj = gtk_adjustment_new(init, min, max, step, 10*step, 0);
uiAdjustment* c = new uiAdjustment(this, zone, GTK_ADJUSTMENT(adj));
gtk_signal_connect (GTK_OBJECT (adj), "value-changed", GTK_SIGNAL_FUNC (uiAdjustment::changed), (gpointer) c);
GtkWidget* slider = gtk_hscale_new (GTK_ADJUSTMENT(adj));
gtk_scale_set_digits(GTK_SCALE(slider), precision(step));
FAUSTFLOAT size = 160;
if(fGuiSize[zone]) {
size = 160 * fGuiSize[zone];
}
gtk_widget_set_size_request(slider, size, -1);
label = startWith(label, "0x") ? "" : label;
if (label && label[0] != 0) {
openFrameBox(label);
addWidget(label, slider);
closeBox();
} else {
addWidget(label, slider);
}
checkForTooltip(zone, slider);
}
// ------------------------------ Num Entry -----------------------------------
void GTKUI::addNumEntry(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{
if (isKnob(zone)) {
addKnob(label, zone, init, min, max, step);
return;
}
*zone = init;
GtkObject* adj = gtk_adjustment_new(init, min, max, step, 10*step, step);
uiAdjustment* c = new uiAdjustment(this, zone, GTK_ADJUSTMENT(adj));
gtk_signal_connect (GTK_OBJECT (adj), "value-changed", GTK_SIGNAL_FUNC (uiAdjustment::changed), (gpointer) c);
GtkWidget* spinner = gtk_spin_button_new (GTK_ADJUSTMENT(adj), 0.005, precision(step));
label = startWith(label, "0x") ? "" : label;
if (label && label[0] != 0) {
openFrameBox(label);
addWidget(label, spinner);
closeBox();
} else {
addWidget(label, spinner);
}
checkForTooltip(zone, spinner);
}
// ========================== passive widgets ===============================
// ------------------------------ Progress Bar -----------------------------------
struct uiBargraph : public uiItem
{
GtkProgressBar* fProgressBar;
FAUSTFLOAT fMin;
FAUSTFLOAT fMax;
uiBargraph(GUI* ui, FAUSTFLOAT* zone, GtkProgressBar* pbar, FAUSTFLOAT lo, FAUSTFLOAT hi)
: uiItem(ui, zone), fProgressBar(pbar), fMin(lo), fMax(hi) {}
FAUSTFLOAT scale(FAUSTFLOAT v) { return (v-fMin)/(fMax-fMin); }
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
gtk_progress_bar_set_fraction(fProgressBar, scale(v));
}
};
void GTKUI::addVerticalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi)
{
GtkWidget* pb = gtk_progress_bar_new();
gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(pb), GTK_PROGRESS_BOTTOM_TO_TOP);
gtk_widget_set_size_request(pb, 8, -1);
new uiBargraph(this, zone, GTK_PROGRESS_BAR(pb), lo, hi);
label = startWith(label, "0x") ? "" : label;
if (label && label[0] != 0) {
openFrameBox(label);
addWidget(label, pb);
closeBox();
} else {
addWidget(label, pb);
}
checkForTooltip(zone, pb);
}
void GTKUI::addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi)
{
GtkWidget* pb = gtk_progress_bar_new();
gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(pb), GTK_PROGRESS_LEFT_TO_RIGHT);
gtk_widget_set_size_request(pb, -1, 8);
new uiBargraph(this, zone, GTK_PROGRESS_BAR(pb), lo, hi);
label = startWith(label, "0x") ? "" : label;
if (label && label[0] != 0) {
openFrameBox(label);
addWidget(label, pb);
closeBox();
} else {
addWidget(label, pb);
}
checkForTooltip(zone, pb);
}
// ------------------------------ Num Display -----------------------------------
struct uiNumDisplay : public uiItem
{
GtkLabel* fLabel;
int fPrecision;
uiNumDisplay(GUI* ui, FAUSTFLOAT* zone, GtkLabel* label, int precision)
: uiItem(ui, zone), fLabel(label), fPrecision(precision) {}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
char s[64];
if (fPrecision <= 0) {
snprintf(s, 63, "%d", int(v));
} else if (fPrecision>3) {
snprintf(s, 63, "%f", v);
} else {
const char* format[] = {"%.1f", "%.2f", "%.3f"};
snprintf(s, 63, format[fPrecision-1], v);
}
gtk_label_set_text(fLabel, s);
}
};
void GTKUI::addNumDisplay(const char* label, FAUSTFLOAT* zone, int precision)
{
GtkWidget* lw = gtk_label_new("");
new uiNumDisplay(this, zone, GTK_LABEL(lw), precision);
openFrameBox(label);
addWidget(label, lw);
closeBox();
checkForTooltip(zone, lw);
}
// ------------------------------ Text Display -----------------------------------
struct uiTextDisplay : public uiItem
{
GtkLabel* fLabel;
const char** fNames;
FAUSTFLOAT fMin;
FAUSTFLOAT fMax;
int fNum;
uiTextDisplay (GUI* ui, FAUSTFLOAT* zone, GtkLabel* label, const char* names[], FAUSTFLOAT lo, FAUSTFLOAT hi)
: uiItem(ui, zone), fLabel(label), fNames(names), fMin(lo), fMax(hi)
{
fNum = 0;
while (fNames[fNum] != 0) fNum++;
}
virtual void reflectZone()
{
FAUSTFLOAT v = *fZone;
fCache = v;
int idx = int(fNum*(v-fMin)/(fMax-fMin));
if (idx < 0) idx = 0;
else if (idx >= fNum) idx = fNum-1;
gtk_label_set_text(fLabel, fNames[idx]);
}
};
void GTKUI::addTextDisplay(const char* label, FAUSTFLOAT* zone, const char* names[], FAUSTFLOAT lo, FAUSTFLOAT hi)
{
GtkWidget* lw = gtk_label_new("");
new uiTextDisplay (this, zone, GTK_LABEL(lw), names, lo, hi);
openFrameBox(label);
addWidget(label, lw);
closeBox();
checkForTooltip(zone, lw);
}
void GTKUI::show()
{
assert(fTop == 0);
gtk_widget_show(fBox[0]);
gtk_widget_show(fWindow);
}
/**
* Update all user items reflecting zone z
*/
static gboolean callUpdateAllGuis(gpointer)
{
GUI::updateAllGuis();
return true;
}
bool GTKUI::run()
{
assert(fTop == 0);
gtk_widget_show(fBox[0]);
gtk_widget_show(fWindow);
gtk_timeout_add(40, callUpdateAllGuis, 0);
gtk_main();
stop();
return true;
}
#endif
|