File: draw.c

package info (click to toggle)
denemo 0.5.9-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,500 kB
  • ctags: 2,415
  • sloc: ansic: 23,057; sh: 3,321; yacc: 1,737; makefile: 449; lex: 376
file content (504 lines) | stat: -rw-r--r-- 15,964 bytes parent folder | download
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
/* draw.c
 * loop that draws all the items in the presently-displayed part of
 * the score
 *  
 * for Denemo, a gtk+ frontend to GNU Lilypond
 * (c) 1999, 2000, 2001 Matthew Hiller
 */

#include <stdio.h>
#include <string.h>
#include "calculatepositions.h"
#include "commandfuncs.h"
#include "contexts.h"
#include "datastructures.h"
#include "draw.h"		/* Which includes gtk.h */
#include "drawingprims.h"
#include "gcs.h"
#include "slurs.h"
#include "hairpin.h"
#include "staffops.h"
#include "utils.h"
#include "pixmaps/toomany.xbm"

#define REDEXCL_WIDTH 3
#define REDEXCL_HEIGHT 13

/* This function just creates a backing pixmap of the appropriate
 * size, recaculates the number of measures that can be fit into
 * the display, and returns */

gint
scorearea_configure_event (GtkWidget * widget, GdkEventConfigure * event,
			   struct scoreinfo *si)
{
  /* Create a new backing pixmap of the appropriate size */
  if (si->pixmap)
    gdk_pixmap_unref (si->pixmap);

  si->pixmap = gdk_pixmap_new (widget->window,
			       widget->allocation.width,
			       widget->allocation.height, -1);

  set_width_to_work_with (si);
  nudgerightward (si);
  nudge_downward (si);
  return TRUE;
}

#define EXTRAFORSELECTRECT 2

struct infotopass
{
  gint clef;
  gint key;
  gint curaccs[7];
  gint keyaccs[7];
  gint stem_directive;
  gint time1, time2;
  gint tickspermeasure;
  gint wholenotewidth;
  gint objnum;
  gint measurenum;
  gint staffnum;
  gint markx1, markx2;
  gint marky1, marky2;
  /*GString *dynamic; */
  GtkWidget *widget;
  measurenode *curmeasure;
  GList *mwidthiterator;
  GSList *slur_stack;
  GSList *hairpin_stack;
};

/* Draws a single object in a measure within a staff. Returns whether the
 * mudelaitem is off the end of the measure or not */

gboolean
draw_object (objnode * curobj, gint x, gint y,
	     struct scoreinfo *si, struct infotopass *itp)
{
  static GdkGC *blackgc = NULL;
  static GdkGC *greengc = NULL;
  mudelaobject *mudelaitem = curobj->data;	/* The current note, rest, etc.
						 * being painted */
  gint extra;

  if (!blackgc)
    blackgc = gcs_blackgc ();

  /* Should we set cursor-context info before drawing? */

  if (si->currentobject == curobj)
    {
      si->cursorclef = itp->clef;
      if (!si->cursor_appending)
	memcpy (si->cursoraccs, itp->curaccs, SEVENGINTS);
    }

  /* Now actually draw it */

  switch (mudelaitem->type)
    {
    case CHORD:
      draw_chord (si->pixmap, blackgc, curobj, x + mudelaitem->x, y,
		  GPOINTER_TO_INT (itp->mwidthiterator->data), itp->curaccs);

      if(mudelaitem->u.chordval.dynamics)
	draw_dynamic(si->pixmap, blackgc, itp->widget->style->font,
		     x + mudelaitem->x, y, mudelaitem);
			
      if (mudelaitem->u.chordval.slur_end_p)
	draw_slur (si->pixmap, blackgc, &(itp->slur_stack), x + mudelaitem->x,
		   y);
      if (mudelaitem->u.chordval.slur_begin_p)
	itp->slur_stack =
	  push_slur_stack (itp->slur_stack, x + mudelaitem->x);

      if (mudelaitem->u.chordval.crescendo_begin_p)
	itp->hairpin_stack =
	  push_hairpin_stack (itp->hairpin_stack, x + mudelaitem->x);
      else if (mudelaitem->u.chordval.diminuendo_begin_p)
	itp->hairpin_stack =
	  push_hairpin_stack (itp->hairpin_stack, x + mudelaitem->x);
      if (mudelaitem->u.chordval.crescendo_end_p)
	draw_hairpin (si->pixmap, blackgc, &(itp->hairpin_stack),
		      x + mudelaitem->x, y, 1);
      else if (mudelaitem->u.chordval.diminuendo_end_p)
	draw_hairpin (si->pixmap, blackgc, &(itp->hairpin_stack),
		      x + mudelaitem->x, y, 0);


      break;
    case TUPOPEN:
    case TUPCLOSE:
      draw_tupbracket (si->pixmap, blackgc, itp->widget->style->font,
		       x + mudelaitem->x, y, mudelaitem);
      break;
    case CLEF:
      draw_clef (si->pixmap, blackgc, x + mudelaitem->x, y,
		 itp->clef = mudelaitem->u.clefval.type);
      if (si->currentobject == curobj && si->cursor_appending)
	si->cursorclef = itp->clef;
      break;
    case KEYSIG:
      draw_key (si->pixmap, blackgc, x + mudelaitem->x, y,
		mudelaitem->u.keyval.number, itp->key, itp->clef, TRUE);
      itp->key = mudelaitem->u.keyval.number;
      memcpy (itp->keyaccs, mudelaitem->u.keyval.accs, SEVENGINTS);
      memcpy (itp->curaccs, itp->keyaccs, SEVENGINTS);
      if (si->currentmeasure == itp->curmeasure)
	/* We're in the current measure */
	memcpy (si->nextmeasureaccs, itp->keyaccs, SEVENGINTS);
      break;
    case TIMESIG:
      draw_timesig (si->pixmap, blackgc, itp->widget->style->font,
		    x + mudelaitem->x, y,
		    itp->time1 = mudelaitem->u.timeval.time1,
		    itp->time2 = mudelaitem->u.timeval.time2);
      if (si->currentmeasure == itp->curmeasure)
	{
	  /* This is the current measure */
	  si->cursortime1 = itp->time1;
	  si->cursortime2 = itp->time2;
	}
      /* The following assumes no multiple simultaneous time signatures */
      itp->tickspermeasure = WHOLE_NUMTICKS * itp->time1 / itp->time2;
      break;
    case STEMDIRECTIVE:
      draw_stem_directive (si->pixmap, blackgc, itp->widget->style->font,
			   x + mudelaitem->x, y, mudelaitem);
      itp->stem_directive = mudelaitem->u.stemval.type;
      break;
    case LILYDIRECTIVE:
      draw_lily_dir (si->pixmap, blackgc, itp->widget->style->font,
		     x + mudelaitem->x, y, mudelaitem);
      break;
    case DYNAMIC:
      /*draw_dynamic (si->pixmap, blackgc, itp->widget->style->font,
		    x + mudelaitem->x, y, mudelaitem);*/
      /*itp->dynamic = mudelaitem->u.dynval.type; */
      break;
    case GRACE_START:
    case GRACE_END:
      draw_gracebracket (si->pixmap, blackgc, itp->widget->style->font,
			 x + mudelaitem->x, y, mudelaitem);
      break;
    default:
      /* Nothing */
      break;
    }
  if (si->currentobject == curobj)
    {				/* Draw the cursor */
      /* Determine if it needs to be red or not */
      if (si->cursor_appending || mudelaitem->type != CHORD)
	si->cursoroffend =
	  (mudelaitem->starttickofnextnote >= itp->tickspermeasure);
      else
	si->cursoroffend =
	  (mudelaitem->starttickofnextnote > itp->tickspermeasure);
      if (si->cursor_appending)
	{
	  extra = MAX (mudelaitem->minpixelsalloted,
		       space_after (mudelaitem->durinticks,
				    itp->wholenotewidth));
	  draw_cursor (si->pixmap, si, x + mudelaitem->x + extra, y,
		       si->cursorclef);
	  memcpy (si->cursoraccs, itp->curaccs, SEVENGINTS);
	}
      else
	draw_cursor (si->pixmap, si, x + mudelaitem->x, y, si->cursorclef);
    }				/* End cursor drawing */

  /* Now quite possibly update the mark */

  if (si->firststaffmarked == itp->staffnum
      && si->firstmeasuremarked == itp->measurenum
      && si->firstobjmarked == itp->objnum)
    itp->markx1 = x + mudelaitem->x - EXTRAFORSELECTRECT;
  if (si->laststaffmarked == itp->staffnum
      && si->lastmeasuremarked == itp->measurenum
      && si->lastobjmarked == itp->objnum)
    itp->markx2 = x + mudelaitem->x + mudelaitem->minpixelsalloted
      + EXTRAFORSELECTRECT;

  /* And give a return value and we're done */

  return (mudelaitem->starttickofnextnote > itp->tickspermeasure);
}

/* Draws a single measure within a staff. */

void
draw_measure (measurenode * curmeasure, gint x, gint y,
	      struct scoreinfo *si, struct infotopass *itp)
{
  static GdkPixmap *redexclaim = NULL;
  static GdkGC *redgc;
  static GdkGC *blackgc;
  static GdkFont *mnumfont;
  static GString *mstring;
  gboolean offend = FALSE;
  objnode *curobj;

  /* initialization */
  if (!redexclaim)
    {
      redexclaim = bitmaphelper (itp->widget, toomany);
      mnumfont =
	gdk_fontset_load
	("-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-iso8859-*");
      if (!mnumfont)
	mnumfont = itp->widget->style->font;
      mstring = g_string_new (NULL);
      redgc = gcs_redgc ();
      blackgc = gcs_blackgc ();
    }

  /* Set information about the state at the current measure, if necessary */

  memcpy (itp->curaccs, itp->keyaccs, SEVENGINTS);
  itp->wholenotewidth = si->measurewidth * itp->time2 / itp->time1;
  if (curmeasure == si->currentmeasure)
    {
      si->curmeasureclef = itp->clef;
      memcpy (si->curmeasureaccs, itp->keyaccs, SEVENGINTS);
      memcpy (si->nextmeasureaccs, itp->keyaccs, SEVENGINTS);
      si->curmeasurekey = itp->key;
      si->curmeasure_stem_directive = itp->stem_directive;
      si->cursortime1 = itp->time1;
      si->cursortime2 = itp->time2;
    }

  /* If this is the current staff, paint the measure number at the
   * preceding barline */

  if (si->currentstaffnum == itp->staffnum)
    {
      g_string_sprintf (mstring, "%d", itp->measurenum);
      gdk_draw_text (si->pixmap, mnumfont, blackgc, x - SPACE_FOR_BARLINE,
		     y - 2, mstring->str, mstring->len);
      if (si->currentmeasurenum == itp->measurenum && !si->currentobject)
	{
	  /* That is, the cursor's at the beginning of this blank measure */
	  si->cursoroffend = FALSE;
	  draw_cursor (si->pixmap, si, x, y, itp->clef);
	  memcpy (si->cursoraccs, itp->curaccs, SEVENGINTS);
	  si->cursorclef = itp->clef;
	}
    }
  curobj = curmeasure->data;
  /* These default values for the markx'es may be necessary down
   * the road */
  if (si->firststaffmarked == itp->staffnum
      && si->firstmeasuremarked == itp->measurenum)
    {
      if (!curobj)
	itp->markx1 = x - EXTRAFORSELECTRECT;
      else
	itp->markx1 = x + GPOINTER_TO_INT (itp->mwidthiterator->data);
    }
  if (si->laststaffmarked == itp->staffnum
      && si->lastmeasuremarked == itp->measurenum)
    {
      if (!curobj || si->lastobjmarked >= g_list_length (curobj))
	itp->markx2 = x + GPOINTER_TO_INT (itp->mwidthiterator->data)
	  + SPACE_FOR_BARLINE + EXTRAFORSELECTRECT;
      else
	itp->markx2 = x;
    }
  /* Paint the red exclamation point, if necessary */

  /* Draw each mudelaitem */
  for (itp->objnum = 0; curobj; curobj = curobj->next, itp->objnum++)
    offend = draw_object (curobj, x, y, si, itp);

  if (offend)
    drawbitmapinverse (si->pixmap, redgc, redexclaim,
		       x, y - 8 - REDEXCL_HEIGHT,
		       REDEXCL_WIDTH, REDEXCL_HEIGHT);
}

/* Draws a single staff */

void
draw_staff (staff * curstaffstruct, gint y,
	    struct scoreinfo *si, struct infotopass *itp)
{
  static GdkFont *staffnamefont = NULL;
  static GdkGC *blackgc;
  gint x, i;
  gboolean primaryvoice = (curstaffstruct->voicenumber == 1);

  if (!staffnamefont)
    {
      staffnamefont =
	gdk_fontset_load
	("-*-helvetica-medium-r-normal-*-12-*-*-*-*-*-iso8859-*");
      if (!staffnamefont)
	staffnamefont = itp->widget->style->font;
      blackgc = gcs_blackgc ();
    }
  draw_clef (si->pixmap, blackgc, LEFT_MARGIN, y,
	     itp->clef = curstaffstruct->leftmost_clefcontext);
  x = KEY_MARGIN;
  draw_key (si->pixmap, blackgc, x, y,
	    itp->key = curstaffstruct->leftmost_keysigcontext,
	    0, itp->clef, TRUE);
  memcpy (itp->keyaccs, curstaffstruct->leftmost_keyaccs, SEVENGINTS);
  x += si->maxkeywidth;
  draw_timesig (si->pixmap, blackgc, itp->widget->style->font, x, y,
		itp->time1 = curstaffstruct->leftmost_time1context,
		itp->time2 = curstaffstruct->leftmost_time2context);
  x += SPACE_FOR_TIME;
  itp->stem_directive = curstaffstruct->leftmost_stem_directive;
  itp->tickspermeasure = WHOLE_NUMTICKS * itp->time1 / itp->time2;
  if (si->firststaffmarked == itp->staffnum)
    itp->marky1 = y - EXTRAFORSELECTRECT;
  if (si->laststaffmarked == itp->staffnum)
    itp->marky2 = y + STAFF_HEIGHT + EXTRAFORSELECTRECT;

  if (primaryvoice)
    /* Draw staff name */
    gdk_draw_text (si->pixmap, staffnamefont, blackgc, KEY_MARGIN, y - 8,
		   curstaffstruct->denemo_name->str,
		   curstaffstruct->denemo_name->len);
  else if (curstaffstruct == si->currentstaff->data)
    /* Replace me with something better */
    gdk_draw_text (si->pixmap, staffnamefont, blackgc, KEY_MARGIN, y,
		   _("Nonprimary voice"), 16);

  /* Loop that will draw each measure. Basically a for loop, but was uglier
   * when written that way.  */
  itp->measurenum = si->leftmeasurenum;
  itp->curmeasure =
    g_list_nth (curstaffstruct->measures, itp->measurenum - 1);
  itp->mwidthiterator = g_list_nth (si->measurewidths, itp->measurenum - 1);

  while (itp->measurenum <= si->rightmeasurenum)
    {
      draw_measure (itp->curmeasure, x, y, si, itp);
      x += GPOINTER_TO_INT (itp->mwidthiterator->data) + SPACE_FOR_BARLINE;
      itp->curmeasure = itp->curmeasure->next;
      itp->mwidthiterator = itp->mwidthiterator->next;
      itp->measurenum++;
    }

  /* now draw the staff lines, reset itp->slur_stack, and we're done */
  for (i = 0; i < curstaffstruct->no_of_lines; i++, y += LINE_SPACE)
    gdk_draw_line (si->pixmap, blackgc, LEFT_MARGIN, y,
		   x - HALF_BARLINE_SPACE, y);
  /* Initialize the slur_stack for this staff. For the time being,
     slurs that begin and/or end after the portion of the music
     that is shown are not drawn. */
  if (itp->slur_stack)
    {
      g_slist_free (itp->slur_stack);
      itp->slur_stack = NULL;
    }
}

/* This actually draws the score, staff-by-staff */

void
draw_score (GtkWidget * widget, struct scoreinfo *si)
{
  staffnode *curstaff;
  gint x, y, i;
  /* For top of stave */
  gint top_y;
  struct infotopass itp;
  static GdkGC *blackgc = NULL;
  static GdkGC *bluegc;
  GList *mwidthiterator;

  /* One-time inits */
  if (!blackgc)
    {
      blackgc = gcs_blackgc ();
      bluegc = gcs_bluegc ();
    }

  /* Initialize some fields in itp */

  itp.widget = widget;
  itp.slur_stack = NULL;

  /* Draw each staff */
  for ((itp.staffnum = si->top_staff,
	curstaff = g_list_nth (si->thescore, si->top_staff - 1),
	y = si->staffspace / 4);
       curstaff && itp.staffnum <= si->bottom_staff; itp.staffnum++)
    {
      y += ((staff *)curstaff->data)->space_above;	
      draw_staff (curstaff->data, y, si, &itp);
      curstaff = curstaff->next;
      if (curstaff && ((staff *) curstaff->data)->voicenumber == 1
	  && itp.staffnum < si->bottom_staff)
	y += (si->staffspace + ((staff*)curstaff->data)->space_below);
    }

  /* Now draw the barlines between the measures, across all the staffs */
  /* needed for finding top of first stave */
  curstaff = g_list_nth (si->thescore, si->top_staff - 1);
  y += STAFF_HEIGHT;
  mwidthiterator = g_list_nth (si->measurewidths, si->leftmeasurenum - 1);
  for (x = KEY_MARGIN + si->maxkeywidth + SPACE_FOR_TIME - HALF_BARLINE_SPACE,
       i = si->leftmeasurenum;
       i <= si->rightmeasurenum; mwidthiterator = mwidthiterator->next, i++)
    {
      x += GPOINTER_TO_INT (mwidthiterator->data) + SPACE_FOR_BARLINE;
      if (!mwidthiterator->next)       /* Last measure - draw double-barline */
	x -= 3;
      top_y = (si->staffspace / 4) + ((staff *)curstaff->data)->space_above;
      gdk_draw_line (si->pixmap, blackgc, x, top_y, x, y);

      if (!mwidthiterator->next)
	{
	  /* Again, we've reached the end of the score and should
	   * draw a double-barline */
	  x += 3;
	  gdk_draw_rectangle (si->pixmap, blackgc, TRUE, x,
			      top_y, 4,
			      y - si->staffspace / 4 + 1 - 
			      ((staff *)curstaff->data)->space_above);
	}
    }				/* End barline drawing loop */
  /* Draw the selection rectangle */
  if (si->markstaffnum)
    draw_selection (si->pixmap, bluegc, itp.markx1, itp.marky1, itp.markx2,
		    itp.marky2);
  /* And we're done */
}

/* Here we have the function that actually draws the score. Note that
 * it does not clip intelligently at all */

gint
scorearea_expose_event (GtkWidget * widget, GdkEventExpose * event,
			gpointer data)
{
  struct scoreinfo *si = data;

  /* Clear the backing pixmap */

  gdk_draw_rectangle (si->pixmap,
		      widget->style->white_gc,
		      TRUE,
		      0, 0,
		      widget->allocation.width, widget->allocation.height);

  /* Draw the score */
  draw_score (widget, si);

  /* Now actually draw the backing pixmap onto the drawing area */

  gdk_draw_pixmap (si->scorearea->window,
		   si->scorearea->style->black_gc,
		   si->pixmap,
		   0, 0, 0, 0,
		   si->scorearea->allocation.width,
		   si->scorearea->allocation.height);

  return TRUE;
}