File: timedialog.c

package info (click to toggle)
denemo 0.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 26,724 kB
  • sloc: ansic: 59,006; lisp: 13,874; sh: 11,666; makefile: 2,082; xml: 634; sed: 16
file content (291 lines) | stat: -rw-r--r-- 8,570 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
/* timesigdialog.cpp
 * a callback that creates a dialog boxes prompting the
 * user for information on changing the time signature
 
 * for Denemo, a gtk+ frontend to GNU Lilypond
 * (c) 2000-2005 Matthew Hiller */

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "calculatepositions.h"
#include "commandfuncs.h"
#include "contexts.h"
#include <denemo/denemo.h>
#include "dialogs.h"
#include "draw.h"
#include "measureops.h"
#include "objops.h"
#include "staffops.h"
#include "utils.h"

/**
 * Is the integer a power of 2, or the value 1
 *
 */
static gint
ispow2 (gint x)
{
  if(x<1)
    return 0;
  for (; !(x & 1); x >>= 1)
    ;	/* Go through all the low order bits that are equal to 0 */
  return (x == 1);
}


/**
 * Set the timesig values for the given staff
 * 
 */
static void
settimesig (DenemoStaff * curstaffstruct, gint time1, gint time2)
{
  curstaffstruct->timesig.time1 = time1;
  curstaffstruct->timesig.time2 = time2;
  beamsandstemdirswholestaff (curstaffstruct);
}

/**
 * Set the initial timesig on current staff or across entire score
 * @param si pointer to the DenemoScore structure.
 * @param curstaffstruct the staff to set the timesig for.
 * @param time1 the time signature nominator
 * @param time2 the time signature denominator
 * @param all_staves apply the new time signature to all staves
 *
 * @return None
 */
void
dnm_setinitialtimesig (DenemoScore * si, DenemoStaff * curstaffstruct, 
		       gint time1, gint time2, gboolean all_staves)
{
  staffnode *curstaff;
  take_snapshot();
  if (time1 && time2 && ispow2 (time2))
    {
      if (all_staves)
	{
	  for (curstaff = si->thescore; curstaff; curstaff = curstaff->next)
	    {
	      curstaffstruct = (DenemoStaff *) curstaff->data;
	      settimesig (curstaffstruct, time1, time2);
	    }
	  find_leftmost_allcontexts (si);
	}
      else
	{
	  settimesig (curstaffstruct, time1, time2);
	  find_leftmost_staffcontext (curstaffstruct, si);
	}

    }
}


/**
 * Insert time sig change on either a single staff or across the 
 * entire score.
 */
static void
insert_timesig (DenemoScore * si, DenemoStaff * curstaffstruct, gint time1,
		gint time2)
{
  staffnode *curstaff = NULL;
  measurenode *curmeasure = NULL;
  objnode *firstobj = NULL;
  DenemoObject *firstmudobj = NULL;
  gboolean replacing = FALSE;	/* if we don't use this trick, anomalous
				 * stuff can happen when replacing a time
				 * signature */

  for (curstaff = si->thescore; curstaff; curstaff = curstaff->next)
    {
      if (((DenemoStaff *) curstaff->data)->is_parasite)
	continue;

      curmeasure = g_list_nth (firstmeasurenode (curstaff),
			       si->currentmeasurenum - 1);
      /* First, look to see if there already is a time signature change at
         the beginning of this measure. If so, delete it first. */
      if(!curmeasure)
	continue;
      firstobj = firstobjnode (curmeasure);
      if (firstobj)
	firstmudobj = (DenemoObject *) firstobj->data;
      else
	firstmudobj = NULL;
      if (firstmudobj && firstmudobj->type == TIMESIG)
	{
	  replacing = TRUE;
	  curmeasure->data =
	    g_list_remove_link ((objnode *) curmeasure->data, firstobj);
	  freeobject (firstmudobj);
	  g_list_free_1 (firstobj);
	}
      curmeasure->data = g_list_prepend ((objnode *) curmeasure->data,
					 dnm_newtimesigobj (time1, time2));
      if (curmeasure == si->currentmeasure)
	{
	  if (!replacing)
	    si->cursor_x++;
	  if (si->cursor_appending)
	    si->currentobject = g_list_last ((objnode *) curmeasure->data);
	  else
	    si->currentobject =
	      g_list_nth ((objnode *) curmeasure->data, si->cursor_x);
	}
      beamsandstemdirswholestaff ((DenemoStaff *) curstaff->data);
    }

}

/**
 * Callback to insert a time sig change
 * Calls timesig_change with the INSERT argument
 */
void
timesig_change_insert (GtkAction *action, DenemoScriptParam * param)
{
  GET_1PARAM(action, param, timesigname);
  DenemoGUI *gui = Denemo.gui;
  if(query) {
    gchar *curtimesig = g_strdup_printf("%d/%d", gui->si->cursortime1, gui->si->cursortime2);
    g_string_assign(param->string, curtimesig);
    g_free(curtimesig);
    param->status = TRUE;
    return;
  }
  if(timesigname==NULL)
    timesig_change (gui, INSERT);
  else {
    DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;
    gint time1, time2;
    sscanf(timesigname, "%d/%d", &time1, &time2);
    if( time1 && time2) {
      insert_timesig (gui->si, curstaffstruct, time1, time2);
      param->status = TRUE;
      displayhelper (gui);
    }
  }   
}

/**
 * Callback to change the initial  time sig change
 * Calls timesig_change with the CHANGEINITIAL argument
 */
void
timesig_change_initial (GtkAction *action, DenemoScriptParam * param)
{
  GET_1PARAM(action, param, timesigname);
  DenemoGUI *gui = Denemo.gui;
  if(query) {
    GList *curstaff = gui->si->thescore;
    DenemoStaff *curstaffstruct = (DenemoStaff *) curstaff->data;
    gchar *curtimesig = g_strdup_printf("%d/%d", curstaffstruct->timesig.time1, curstaffstruct->timesig.time2);
    g_string_assign(param->string, curtimesig);
    g_free(curtimesig);
    param->status = TRUE;
    return;
  }

  if(timesigname==NULL)
    timesig_change (gui, CHANGEINITIAL);
  else {
    DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;
    gint time1, time2;
    sscanf(timesigname, "%d/%d", &time1, &time2);
    if( time1 && time2) {
      dnm_setinitialtimesig (gui->si, curstaffstruct, time1, 
			     time2, TRUE);
      param->status = TRUE;
      displayhelper (gui);
    }
  }
}

/**
 * Time sig change dialog.  allows the user to set
 * the time signature to insert or change
 */
void
timesig_change (DenemoGUI * gui, actiontype action)
{
  GtkWidget *dialog;
  GtkWidget *label;
  GtkWidget *textentry1;
  GtkWidget *textentry2;
  GtkWidget *checkbutton;

  DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;

  if (gui->si->lily_file && action == CHANGEINITIAL)
    return;	  /* no code for this yet - just edit textually */

  dialog = 
    gtk_dialog_new_with_buttons (((action == CHANGEINITIAL) ? 
				  _("Change initial time signature") : 
				  _("Insert time signature change")), 
				 NULL,	/* parent window */
				 (GtkDialogFlags) 
				 (GTK_DIALOG_MODAL |
				  GTK_DIALOG_DESTROY_WITH_PARENT),
				 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
				 GTK_STOCK_CANCEL, GTK_STOCK_CANCEL,
				 NULL);

  gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
				  12);
  
  label = gtk_label_new (_("Enter desired time signature:"));
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
		      label, FALSE, FALSE, 8);
  
  textentry1 = gtk_spin_button_new_with_range (1, 128, 1.0);
  gtk_spin_button_set_digits (GTK_SPIN_BUTTON (textentry1), 0);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (textentry1),
			     (gdouble) curstaffstruct->timesig.time1);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), textentry1, FALSE,
		      FALSE, 8);
  gtk_entry_set_activates_default (GTK_ENTRY (textentry1), TRUE);

  textentry2 = gtk_spin_button_new_with_range (1, 16, 1.0);
  gtk_spin_button_set_digits (GTK_SPIN_BUTTON (textentry2), 0);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (textentry2),
			     (gdouble) curstaffstruct->timesig.time2);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), textentry2, FALSE,
		      FALSE, 8);
  gtk_entry_set_activates_default (GTK_ENTRY (textentry2), TRUE);

  checkbutton = gtk_check_button_new_with_label (_("Apply to all staves"));
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
		      checkbutton, FALSE, FALSE, 8);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton), TRUE);

  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
  gtk_widget_show_all (dialog);
  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
    {
      gint time1 =
	gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (textentry1));
      gint time2 =
	gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (textentry2));
      gboolean all_staves =
	gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton));
      if (action == CHANGEINITIAL)
	{
	  dnm_setinitialtimesig (gui->si, curstaffstruct, time1, 
				 time2, all_staves);
	}
      else
	{
	  if(gui->si->currentobject && ((DenemoObject*)gui->si->currentobject->data)->type==TIMESIG)
	    deleteobject(gui);
	  insert_timesig (gui->si, curstaffstruct, time1, time2);
	}
      displayhelper (gui);
    }
  score_status(gui, TRUE);
  gtk_widget_destroy (dialog);
}