File: FL_graph.cpp

package info (click to toggle)
csound-plugins 1.0.2~dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,960 kB
  • sloc: cpp: 16,586; ansic: 7,522; python: 458; sh: 94; makefile: 25
file content (504 lines) | stat: -rw-r--r-- 15,628 bytes parent folder | download | duplicates (5)
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

/*
  FL_graph.cpp: code for drawing graphs using FLTK library

  Copyright (C) 2003 John ffitch

  This file is part of Csound.

  The Csound Library is free software; you can redistribute it
  and/or modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  Csound 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 Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Csound; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  02110-1301 USA
*/

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Button.H>

#ifdef _MSC_VER
#include <math.h>
#endif
#include "csdl.h"
#include "cwindow.h"
#include "winFLTK.h"
#include <inttypes.h>

#define NUMOFWINDOWS (30)
#define XINIT    10      /* set default window location */
#define YINIT    50
#define WIDTH    450     /* start off small - user resizes */
#define HEIGHT   150
#define BDR      5
#define BORDERW  10      /* inset from L & R edge */
#define TXHGHT   14      /* baseline offset for text */
#define MAXLSEGS 4096    /* X can only deal with so many linesegs .. */

class graph_box : public Fl_Window {
  void draw();
public:
  int curr;
  int last;
  CSOUND *csound;
  graph_box(void *cs, int x, int y, int w, int h, const char *l = 0)
    : Fl_Window(x, y, w, h, l)
  {
      curr = last = -1;
      csound = (CSOUND*)cs;
  }
  //void add_graph(WINDAT *wdptr);
};

typedef struct {

  Fl_Choice   *choice;
  Fl_Button   *end;
  Fl_Menu_Item *menu;
  graph_box   *graph;
  int graph_created;
  Fl_Window   *form;
} FLGRAPH_GLOBALS;

#define ST(x)   ((flgraphGlobals)->x)

/*void printouts(CSOUND *csound){
    csound->Message(csound, "menu object: %p\n", ST(menu));
    csound->Message(csound, "form object: %p\n", ST(form));
    csound->Message(csound, "graph object: %p\n", ST(graph));
    csound->Message(csound, "choice object: %p\n", ST(choice));
    csound->Message(csound, "globals %p\n",  csound->flgraphGlobals);
}*/

void flgraph_init(CSOUND *csound)
{

    FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
    if (flgraphGlobals==NULL) {
     csound->CreateGlobalVariable(csound, "FLGRAPH_GLOBALS",
                                  sizeof(FLGRAPH_GLOBALS));
     flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
      //csound->flgraphGlobals =
      //(FLGRAPH_GLOBALS*) csound->Malloc(csound,sizeof(FLGRAPH_GLOBALS));
    }
      ST(form) = (Fl_Window *) 0;
      ST(choice) = (Fl_Choice *) 0;
      ST(end) = (Fl_Button *) 0;
      ST(graph) = (graph_box *) 0;
      ST(menu) = (Fl_Menu_Item *) 0;
      ST(graph_created) = 0;


      /*ST(menu) =  (Fl_Menu_Item*) csound->Calloc(csound,
        sizeof(Fl_Menu_Item)*(1+NUMOFWINDOWS));*/
      /* VL: moved menu object to be built at each new compilation */

}

/* static  graph_box   *graph = (graph_box *) 0; */

void graph_box::draw()
{
    FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
    Fl_Window::draw();
    fl_color(0, 0, 0);
    fl_line_style(FL_SOLID);
    fl_rect(0, 0, w(), h());
    if (curr >= 0) {
      WINDAT      *win   = (WINDAT*) ST(menu)[curr].user_data_;
      if (!win)
        return;
      MYFLT       *fdata = win->fdata;
      int32       npts   = win->npts;
      char        *msg   = win->caption;
      short       win_x, win_y,        win_h;     /* window rect */
      short       gra_x, gra_y, gra_w, gra_h;     /* graph rect is inset */
      short       y_axis;
      int         lsegs, pts_pls;
      int         pol;
      char        string[400];

      pol  = win->polarity;

      win_x = 0;  win_y = 0;              /* window pixels addressed relative */
      win_h = h();

      /* set new width and height so we leave a 20% border around the plot */
      gra_w = w() - 2*BORDERW;
      gra_h = h();
      gra_x = win_x + BORDERW;
      gra_y = win_y;
      /* figure height of Y axis - top, middle or bottom */
      if (pol == (short)BIPOL)
        y_axis = gra_y + (gra_h/2);
      else if (pol == (short)NEGPOL)
        y_axis = gra_y;
        else                /* POSPOL */
        y_axis = gra_y + gra_h;

      if (npts < MAXLSEGS) {
        lsegs = npts;                   /* one lineseg per datum */
        pts_pls = 1;
      }
      else {
        pts_pls = npts/MAXLSEGS;
        if (npts%MAXLSEGS) pts_pls++;
        lsegs = npts/pts_pls;
      }
      fl_begin_line();
      {       /* take scale factors out of for-loop for faster run-time */
        MYFLT x_scale = gra_w / (MYFLT) (lsegs - 1);
        MYFLT y_scale = gra_h / win->oabsmax; /* unipolar default */
        MYFLT f, ma, mi, *fdptr = fdata;
        int   c, i = 0, j = lsegs;

        if (pol == (short) BIPOL)
          y_scale /= 2.0;             /* max data scales to h/2 */
        /* put x-y pairs into a point list for XDraw */
        while (j--) {
          int x = gra_x + (short) ((MYFLT) i++ * x_scale);
          int y;
          if (pts_pls == 1)
            f = *fdptr++;
          else {
            ma = mi = *fdptr++;
            for (c = 1; c < pts_pls; ++c)
              if ((f = *fdptr++) > ma)    ma = f;
              else if ( f<mi )            mi = f;
            if (ma < 0)             f = mi;
            else if (mi > 0)        f = ma;
            else if (ma > -mi)      f = ma;
            else f = mi;
          }
          y = y_axis - (short) (f * y_scale);
          fl_vertex(x, y);
        }
      }
      fl_end_line();

      /* now draw axes: y-axis is always on the left edge,
         x-axis height is determined by the case we're in */
      fl_line(gra_x, y_axis, (gra_x + gra_w), y_axis);
      fl_line(gra_x, y_axis, (gra_x + gra_w), y_axis);


      fl_line(gra_x, gra_y, gra_x, (gra_y + gra_h));
      if (win->danflag) {       /* flag to add dotted divider */
        fl_line_style(FL_DOT);
        fl_line(win_x+w()/2, win_y, win_x+w()/2, win_y+win_h);
      }
      if (pol != NEGPOL)
      sprintf(string, "%s  %" PRIi32 " points, max %5.3f", msg, npts, win->oabsmax);
      else
      sprintf(string, "%s  %" PRIi32 " points, max %5.3f", msg, npts, win->max);

      ST(form)->label(string);
    }
    fl_line_style(FL_SOLID);
}



void add_graph(CSOUND *csound, WINDAT *wdptr)
{
    FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
    WINDAT *n = (WINDAT*) malloc(sizeof(WINDAT));
    int    m;
    WINDAT *old;
    int    replacing = 0;

    memcpy(n, wdptr, sizeof(WINDAT));
    n->fdata = (MYFLT*) malloc(n->npts * sizeof(MYFLT));
    memcpy(n->fdata, wdptr->fdata, n->npts * sizeof(MYFLT));


    for (m = 0; m < NUMOFWINDOWS; m++) {  // If text the same use slot
      if (ST(menu) != NULL) {
        if (ST(menu)[m].text != NULL && strlen(wdptr->caption) == 0){
          if(strcmp(wdptr->caption, ST(menu)[m].text) == 0) {
            replacing = 1;
            goto replace;
          }
        }
      }
    }
    // Use a new slot, cycling round
    ST(graph)->last++;
    m = ST(graph)->last;
    if (m >= NUMOFWINDOWS)
      m = ST(graph)->last = 0;
 replace:

    old = (WINDAT*)ST(menu)[m].user_data_;
    if (old) {
      free((void*) old->fdata);
      free((void*) old);
    }
    ST(menu)[m].user_data_ = n;
    if (replacing == 0) {
      if (ST(menu)[m].text != NULL)
        free((void*) ST(menu)[m].text);
      ST(menu)[m].text = (const char*) malloc(strlen(n->caption) + 1);
      strcpy((char*) ST(menu)[m].text, n->caption);
    }


    // ST(graph)->curr = m;
    //  ST(choice)->value(m);

    ST(graph)->curr = ST(choice)->value();  /* VL: 29.04.09 fix */
    ST(graph)->redraw();
}

void do_redraw(Fl_Widget *, void *cs)
{
    CSOUND *csound = (CSOUND*)cs;
    FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
    ST(graph)->curr = ST(choice)->value();
    ST(graph)->redraw();
}

void makeWindow(CSOUND *csound, char *name)
{
    FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
    if (ST(form))
      return;

    ST(form) = new Fl_Window(WIDTH, HEIGHT, name);
    ST(menu) = new Fl_Menu_Item[1+NUMOFWINDOWS];
    memset(ST(menu), 0, sizeof(Fl_Menu_Item)*(1+NUMOFWINDOWS));
    ST(choice) = new Fl_Choice(140, 0, 140, 20, "Choose Graph");
    ST(choice)->menu(ST(menu));
    ST(choice)->value(0);
    ST(choice)->callback((Fl_Callback*) do_redraw,  (void*)csound);
    ST(graph) = new graph_box(csound,
                          BDR, 30 + BDR, WIDTH - 2 * BDR, HEIGHT - 30 - 2 * BDR);
    ST(graph)->end();
    ST(end) = new Fl_Button(WIDTH - 40, 0, 35, 20, "Quit");
    ST(end)->hide();
    ST(form)->resizable(ST(graph));
    ST(form)->end();
    ST(graph_created) = 1;

}

void graphs_reset(CSOUND * csound){
  IGN(csound);
  //if (csound->flgraphGlobals != NULL)
  //  csound->Free(csound, csound->flgraphGlobals);
}

extern "C" {

  void DrawGraph_FLTK(CSOUND *csound, WINDAT *wdptr)
  {

      add_graph(csound, wdptr);
      csound->CheckEvents(csound);
  }

  uintptr_t MakeWindow_FLTK(CSOUND *csound, char *name)
  {
      FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
      if (ST(form) == NULL) {
        makeWindow(csound, name);
        ST(form)->show();
      }

      return (uintptr_t) ST(form);
  }

  int CsoundYield_FLTK(CSOUND *csound){

#ifndef NO_FLTK_THREADS

      /* nothing to do, unless no widget thread is running */
      if (csound->QueryGlobalVariable(csound, "_widgets_globals") != NULL)
        return 1;
#endif
      Fl_wait_locked(csound, 0.0);
      return 1;
  }

  void kill_graph(CSOUND *csound, uintptr_t m)
  {
      FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                         "FLGRAPH_GLOBALS");
      for (int i = 0; i < NUMOFWINDOWS; i++) {
        WINDAT *n = (WINDAT*) ST(menu)[i].user_data_;
        if (n != NULL && ((uintptr_t) n == m ||n->windid == m)) {
          free(n->fdata);
          free(n);
          free((void*) ST(menu)[i].text);
          ST(menu)[i].user_data_ = (void*) 0;
          ST(menu)[i].text = (char*) 0;
          return;
        }
      }
  }

  int ExitGraph_FLTK(CSOUND *csound)
  {
      FLGRAPH_GLOBALS *flgraphGlobals =
         (FLGRAPH_GLOBALS *) csound->QueryGlobalVariable(csound,
                                                        "FLGRAPH_GLOBALS");
      if (flgraphGlobals == 0) {
          return OK;
      }
      if (ST(form) && ST(graph_created) == 1) {

        if (ST(form)->shown() && !(getFLTKFlags(csound) & 256)) {
        const char *env = csound->GetEnv(csound, "CSNOSTOP");
        if (env == NULL || strcmp(env, "yes") != 0) {
          ST(end)->show();
           // print click-Exit message in most recently active window
          while (ST(end)->value() == 0 && ST(form)->shown()) {
            Fl_wait_locked(csound, 0.03);
          }
          }
         }

        delete ST(form);
        ST(form) = (Fl_Window *) 0;
        Fl_wait_locked(csound, 0.0);

      ST(choice) = (Fl_Choice *) 0;
      ST(graph) = (graph_box *) 0;
      ST(end) = (Fl_Button *) 0;
      ST(graph_created) = 0;

       for (int i = 0; i < NUMOFWINDOWS; i++) {
        WINDAT *n = (WINDAT*) ST(menu)[i].user_data_;
        if (n)
          kill_graph(csound, (uintptr_t) ((void*) n));
       }
       if (ST(menu)){
         delete[] ST(menu);
         ST(menu) = (Fl_Menu_Item *) 0;
       }



      }

      return 0;
  }

#define GUTTERH 20           /* space for text at top & bottom */
#define BORDERW 10           /* inset from L & R edge */

  void MakeXYin_FLTK(CSOUND *csound, XYINDAT *w, MYFLT x, MYFLT y)
  {
      if (w->windid == (uintptr_t) 0) {
        Fl_Window *xyin = new Fl_Window(WIDTH, WIDTH, "XY input");
        short   win_x, win_y;
        short   gra_x, gra_y, gra_w, gra_h;

        win_x = 0;  win_y = 0;           /* window pixels addressed relative */

        Fl_lock(csound);
        xyin->show();
        Fl_wait(csound, 0.0);
        Fl_unlock(csound);

        /* set new width and height so we leave a 20% border around the plot */
        gra_w = xyin->w() - 2*BORDERW;      gra_h = xyin->h() - 2*GUTTERH;
        gra_x = win_x + BORDERW;            gra_y = win_y + GUTTERH;
        w->m_x = gra_x + (int) (x * (MYFLT) gra_w);
        w->m_y = gra_y + (int) (y * (MYFLT) gra_h);
        w->down = 0;

        Fl_lock(csound);
        Fl_wait(csound, 0.0);
        xyin->make_current();
        fl_color(0, 0, 0);
        fl_line_style(FL_DOT);
        fl_line(gra_x, w->m_y, (gra_x + gra_w), w->m_y);
        fl_line(w->m_x, gra_y, w->m_x, (gra_y + gra_h));
        Fl_unlock(csound);
        w->windid = (uintptr_t) xyin;
      }
  }

  void ReadXYin_FLTK(CSOUND *csound, XYINDAT *wdptr)
  {
      short     win_x, win_y;
      short     gra_x, gra_y, gra_w, gra_h;
      short     m_x, m_y;
      Fl_Window *xwin = (Fl_Window*) wdptr->windid;

      Fl_lock(csound);
      Fl_wait(csound, 0.0);
      m_x = Fl::event_x();
      m_y = Fl::event_y();
      wdptr->down = (short) (Fl::event_button1() ? 1 : 0);
      Fl_unlock(csound);

      if (!wdptr->down)
        return;

      win_x = 0;  win_y = 0;      /* window pixels addressed relative */

      /* set new width and height so we leave a 20% border around the plot */
      gra_w = xwin->w() - 2*BORDERW;      gra_h = xwin->h() - 2*GUTTERH;
      gra_x = win_x + BORDERW;            gra_y = win_y + GUTTERH;

      /* clip mouse position */
      if (m_x < gra_x)            m_x = gra_x;
      else if (m_x > gra_x+gra_w) m_x = gra_x+gra_w;
      if (m_y < gra_y)            m_y = gra_y;
      else if (m_y > gra_y+gra_h) m_y = gra_y+gra_h;

      if (m_x != wdptr->m_x || m_y != wdptr->m_y) { /* only redo if changed */
        Fl_lock(csound);
        xwin->make_current();
        /* undraw old crosshairs */
        fl_color(FL_BACKGROUND_COLOR);
        fl_line_style(FL_SOLID);
        fl_line(gra_x, wdptr->m_y, (gra_x + gra_w), wdptr->m_y);
        fl_line(wdptr->m_x, gra_y, wdptr->m_x, (gra_y + gra_h));
        // Draw new
        fl_color(0, 0, 0);
        fl_line_style(FL_SOLID);
        fl_line(gra_x, m_y, (gra_x + gra_w), m_y);
        fl_line(m_x, gra_y, m_x, (gra_y + gra_h));
        Fl_unlock(csound);
        wdptr->m_x = m_x;       wdptr->m_y = m_y;
        wdptr->x = ((MYFLT) m_x - gra_x) / (MYFLT) gra_w;
        wdptr->y = ((MYFLT) m_y - gra_y) / (MYFLT) gra_h;
      }
  }

  void KillXYin_FLTK(CSOUND *csound, XYINDAT *wdptr)
  {
    IGN(csound);
      delete ((Fl_Window*) wdptr->windid);
      wdptr->windid = (uintptr_t) 0;
  }
}