root/sweep/trunk/src/undo_dialog.c

Revision 688, 11.8 kB (checked in by erikd, 2 years ago)

Clean up large number of '#if 0' and '#if 1' code blocks.

Remove code chunks surrounded by '#if 0' .. '#endif'.
Remove '#if 1' and '#endif' around code chunks that are actually being used.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Sweep, a sound wave editor.
3  *
4  * Copyright (C) 2000 Conrad Parker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtk.h>
30
31 #include <sweep/sweep_i18n.h>
32 #include <sweep/sweep_undo.h>
33 #include <sweep/sweep_sample.h>
34 #include <sweep/sweep_sounddata.h>
35 #include <sweep/sweep_selection.h>
36
37 #include "sweep_app.h"
38 #include "edit.h"
39 #include "interface.h"
40 #include "callbacks.h"
41
42 #include "../pixmaps/undo.xpm"
43 #include "../pixmaps/redo.xpm"
44 #include "../pixmaps/done.xpm"
45
46 /*#define DEBUG*/
47
48 static GtkWidget * undo_dialog = NULL;
49 static GtkWidget * undo_clist = NULL;
50 static GtkWidget * combo;
51 static GtkWidget * undo_button, * redo_button, * revert_button;
52 static sw_sample * ud_sample = NULL;
53
54 static void
55 undo_dialog_destroy (void)
56 {
57   undo_dialog = NULL;
58 }
59
60 static void
61 undo_dialog_cancel_cb (GtkWidget * widget, gpointer data)
62 {
63   GtkWidget * dialog;
64
65   dialog = gtk_widget_get_toplevel (widget);
66   gtk_widget_hide (dialog);
67 }
68
69 /*
70  * Must be called with sample->edit_mutex held
71  */
72 void
73 undo_dialog_refresh_edit_mode (sw_sample * sample)
74 {
75   if (sample != ud_sample) return;
76
77   if (undo_dialog == NULL) return;
78
79   switch (sample->edit_mode) {
80   case SWEEP_EDIT_MODE_READY:
81     gtk_widget_set_sensitive (undo_button, TRUE);
82     gtk_widget_set_sensitive (redo_button, TRUE);
83     gtk_widget_set_sensitive (undo_clist, TRUE);
84     gtk_widget_set_sensitive (revert_button, TRUE);
85     break;
86   case SWEEP_EDIT_MODE_META:
87   case SWEEP_EDIT_MODE_FILTER:
88     gtk_widget_set_sensitive (undo_button, FALSE);
89     gtk_widget_set_sensitive (redo_button, FALSE);
90     gtk_widget_set_sensitive (undo_clist, FALSE);
91     gtk_widget_set_sensitive (revert_button, FALSE);
92     break;
93   case SWEEP_EDIT_MODE_ALLOC:
94     gtk_widget_set_sensitive (undo_button, FALSE);
95     gtk_widget_set_sensitive (redo_button, FALSE);
96     gtk_widget_set_sensitive (undo_clist, FALSE);
97     gtk_widget_set_sensitive (revert_button, FALSE);
98     break;
99   default:
100     g_assert_not_reached ();
101   }
102 }
103
104 static void
105 _undo_dialog_set_sample (sw_sample * sample, gboolean select_current)
106 {
107   GtkCList * clist;
108   GList * gl;
109   sw_op_instance * inst;
110   gint i = 0;
111   gchar * list_item[] = { "" };
112   GdkColormap * colormap;
113   GdkPixmap * pixmap_data;
114   GdkBitmap * mask;
115   gboolean done = FALSE;
116  
117   g_mutex_lock (sample->ops_mutex);
118
119   clist = GTK_CLIST(undo_clist);
120
121   colormap = gtk_widget_get_default_colormap ();
122   pixmap_data =
123     gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
124                                              NULL, done_xpm);
125   gtk_clist_freeze (clist);
126
127   gtk_clist_clear (clist);
128
129   for (gl = g_list_last (sample->registered_ops); gl; gl = gl->prev) {
130     inst = (sw_op_instance *)gl->data;
131
132     gtk_clist_append (clist, list_item);
133     gtk_clist_set_text (clist, i, 1, _(inst->description));
134
135     if (gl == sample->current_undo) {
136       done = TRUE;
137       gtk_clist_moveto (clist, i, 0, 0.5, -1);
138       if (select_current) gtk_clist_select_row (clist, i, 1);
139     }
140
141     if (done)
142       gtk_clist_set_pixmap (clist, i, 0, pixmap_data, mask);
143
144     i++;
145   }
146
147   gtk_clist_append (clist, list_item);
148   gtk_clist_set_text (clist, i, 1, _("Original data"));
149   gtk_clist_set_pixmap (clist, i, 0, pixmap_data, mask);
150
151   if (sample->current_undo == NULL) {
152     gtk_clist_moveto (clist, i, 0, 0.5, -1);
153     if (select_current) gtk_clist_select_row (clist, i, 1);
154   }
155
156   gtk_clist_thaw (clist);
157
158   g_mutex_unlock (sample->ops_mutex);
159
160   ud_sample = sample;
161   gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(combo)->entry),
162                       g_basename(sample->pathname));
163
164   g_mutex_lock (ud_sample->edit_mutex);
165   undo_dialog_refresh_edit_mode (ud_sample);
166   g_mutex_unlock (ud_sample->edit_mutex);
167 }
168
169 void
170 undo_dialog_set_sample (sw_sample * sample)
171 {
172   if (sample == NULL) return;
173
174   if (undo_dialog == NULL || !GTK_WIDGET_VISIBLE(undo_dialog))
175     return;
176
177   _undo_dialog_set_sample (sample, (sample != ud_sample));
178 }
179
180 void
181 undo_dialog_refresh_history (sw_sample * sample)
182 {
183   if (sample != ud_sample)
184     return;
185
186   if (undo_dialog == NULL || !GTK_WIDGET_VISIBLE(undo_dialog))
187     return;
188  
189   _undo_dialog_set_sample (sample, FALSE);
190 }
191
192 void
193 undo_dialog_refresh_sample_list (void)
194 {
195   GList * cbitems = NULL;
196
197   if (undo_dialog == NULL)
198     return;
199
200   if ((cbitems = sample_bank_list_names ()) != NULL)
201     gtk_combo_set_popdown_strings (GTK_COMBO(combo), cbitems);
202
203   g_list_free (cbitems);
204 }
205
206 static void
207 undo_dialog_entry_changed_cb (GtkWidget * widget, gpointer data)
208 {
209   GtkEntry * entry;
210   gchar * new_text;
211   sw_sample * sample;
212
213   entry = GTK_ENTRY(GTK_COMBO(combo)->entry);
214   new_text = (gchar *)gtk_entry_get_text (entry);
215
216   sample = sample_bank_find_byname (new_text);
217
218   if (sample == NULL) return;
219
220   gtk_signal_handler_block_by_data (GTK_OBJECT(entry), NULL);
221   _undo_dialog_set_sample (sample, TRUE);
222   gtk_signal_handler_unblock_by_data (GTK_OBJECT(entry), NULL);
223 }
224
225 static void
226 undo_dialog_revert_cb (GtkWidget * widget, gpointer data)
227 {
228   GList * gl, * sel_gl = NULL;
229   /*  sw_op_instance * inst;*/
230   gint i = 0, s;
231   /*  gboolean need_undo = FALSE;*/
232
233   s = GPOINTER_TO_INT((GTK_CLIST(undo_clist)->selection)->data);
234
235   g_mutex_lock (ud_sample->ops_mutex);
236
237   for (gl = g_list_last (ud_sample->registered_ops); gl; gl = gl->prev) {
238     if (i == s) {
239       sel_gl = gl;
240       break;
241     }
242
243     i++;
244   }
245
246   g_mutex_unlock (ud_sample->ops_mutex);
247
248   revert_op (ud_sample, sel_gl);
249 }
250
251 static void
252 ud_undo_cb (GtkWidget * widget, gpointer data)
253 {
254   undo_current (ud_sample);
255 }
256
257 static void
258 ud_redo_cb (GtkWidget * widget, gpointer data)
259 {
260   redo_current (ud_sample);
261 }
262
263 static GtkWidget *
264 ud_create_pixmap_button (GtkWidget * widget, gchar ** xpm_data,
265                          const gchar * label_text, const gchar * tip_text,
266                          GCallback clicked)
267 {
268   GtkWidget * hbox;
269   GtkWidget * label;
270   GtkWidget * pixmap;
271   GtkWidget * button;
272   GtkTooltips * tooltips;
273
274   button = gtk_button_new ();
275
276   hbox = gtk_hbox_new (FALSE, 2);
277   gtk_container_add (GTK_CONTAINER(button), hbox);
278   gtk_container_set_border_width (GTK_CONTAINER(button), 8);
279   gtk_widget_show (hbox);
280
281   if (xpm_data != NULL) {
282     pixmap = create_widget_from_xpm (widget, xpm_data);
283     gtk_box_pack_start (GTK_BOX(hbox), pixmap, FALSE, FALSE, 8);
284     gtk_widget_show (pixmap);
285   }
286
287   if (label_text != NULL) {
288     label = gtk_label_new (label_text);
289     gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 8);
290     gtk_widget_show (label);
291   }
292
293   if (tip_text != NULL) {
294     tooltips = gtk_tooltips_new ();
295     gtk_tooltips_set_tip (tooltips, button, tip_text, NULL);
296   }
297
298   if (clicked != NULL) {
299     g_signal_connect (G_OBJECT (button), "clicked",
300                         G_CALLBACK(clicked), NULL);
301   }
302
303   return button;
304 }
305
306 void
307 undo_dialog_create (sw_sample * sample)
308 {
309   GtkWidget * vbox;
310   GtkWidget * hbox /* , *button_hbox */;
311   GtkWidget * label;
312   /*  GtkWidget * ok_button;*/
313   GtkWidget * button;
314   GtkWidget * scrolled;
315   gchar * titles[] = { "", N_("Action") };
316   GClosure *gclosure;
317   GtkAccelGroup * accel_group;
318
319   if (undo_dialog == NULL) {
320     undo_dialog = gtk_dialog_new ();
321     gtk_window_set_wmclass(GTK_WINDOW(undo_dialog), "undo_dialog", "Sweep");
322     gtk_window_set_title(GTK_WINDOW(undo_dialog), _("Sweep: History"));
323     gtk_window_set_resizable (GTK_WINDOW(undo_dialog), FALSE);
324     gtk_window_set_position (GTK_WINDOW(undo_dialog), GTK_WIN_POS_MOUSE);
325     gtk_container_set_border_width  (GTK_CONTAINER(undo_dialog), 8);
326
327     accel_group = gtk_accel_group_new ();
328     gtk_window_add_accel_group (GTK_WINDOW(undo_dialog), accel_group);
329
330     g_signal_connect (G_OBJECT(undo_dialog), "destroy",
331                       G_CALLBACK(undo_dialog_destroy), NULL);
332
333
334    gclosure = g_cclosure_new  ((GCallback)hide_window_cb, NULL, NULL);
335    gtk_accel_group_connect (accel_group,
336                                              GDK_w,
337                                              GDK_CONTROL_MASK,
338                                              0, /* non of the GtkAccelFlags seem suitable? */
339                                              gclosure);
340    
341     vbox = GTK_DIALOG(undo_dialog)->vbox;
342
343     hbox = gtk_hbox_new (FALSE, 8);
344     gtk_box_pack_start (GTK_BOX(vbox), hbox, TRUE, TRUE, 8);
345     gtk_widget_show (hbox);
346
347     label = gtk_label_new (_("File:"));
348     gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
349     gtk_widget_show (label);
350
351     combo = gtk_combo_new ();
352     gtk_box_pack_start (GTK_BOX(hbox), combo, TRUE, TRUE, 0);
353     gtk_widget_show (combo);
354
355     gtk_entry_set_editable (GTK_ENTRY(GTK_COMBO(combo)->entry), FALSE);
356
357     g_signal_connect (G_OBJECT(GTK_COMBO(combo)->entry), "changed",
358                         G_CALLBACK(undo_dialog_entry_changed_cb), NULL);
359
360     hbox = gtk_hbox_new (TRUE, 8);
361     gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
362     gtk_widget_show (hbox);
363
364     button = ud_create_pixmap_button (undo_dialog, undo_xpm, _("Undo"), _("Undo"),
365                                    G_CALLBACK (ud_undo_cb));
366     gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
367     gtk_widget_show (button);
368     gtk_widget_add_accelerator (button, "clicked", accel_group,
369                                 GDK_z, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
370     undo_button = button;
371
372     button = ud_create_pixmap_button (undo_dialog, redo_xpm, _("Redo"), _("Redo"),
373                                    G_CALLBACK (ud_redo_cb));
374     gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
375     gtk_widget_show (button);
376     gtk_widget_add_accelerator (button, "clicked", accel_group,
377                                 GDK_r, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
378     redo_button = button;
379
380     scrolled = gtk_scrolled_window_new (NULL, NULL);
381     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrolled),
382                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
383     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(undo_dialog)->vbox), scrolled,
384                         FALSE, FALSE, 0);
385     gtk_widget_set_usize (scrolled, 360, 240);
386     gtk_widget_show (scrolled);
387
388     undo_clist = gtk_clist_new_with_titles (2, titles);
389     gtk_clist_set_column_width (GTK_CLIST(undo_clist), 0, 20);
390     gtk_clist_set_selection_mode (GTK_CLIST(undo_clist), GTK_SELECTION_BROWSE);
391     gtk_clist_column_titles_passive (GTK_CLIST(undo_clist));
392     /* set title actively for i18n */
393     gtk_clist_set_column_title(GTK_CLIST(undo_clist), 1, _(titles[1]));
394     gtk_container_add (GTK_CONTAINER(scrolled), undo_clist);
395     gtk_widget_show (undo_clist);
396
397     button = gtk_button_new_with_label (_("Revert to selected state"));
398     GTK_WIDGET_SET_FLAGS (GTK_WIDGET (button), GTK_CAN_DEFAULT);
399     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(undo_dialog)->action_area),
400                         button, TRUE, TRUE, 0);
401     gtk_widget_show (button);
402     g_signal_connect (G_OBJECT(button), "clicked",
403                         G_CALLBACK (undo_dialog_revert_cb),
404                         NULL);
405     revert_button = button;
406
407     /* Cancel */
408
409     button = gtk_button_new_with_label (_("Close"));
410     GTK_WIDGET_SET_FLAGS (GTK_WIDGET (button), GTK_CAN_DEFAULT);
411     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(undo_dialog)->action_area),
412                         button, FALSE, FALSE, 0);
413     gtk_widget_show (button);
414     g_signal_connect (G_OBJECT(button), "clicked",
415                         G_CALLBACK (undo_dialog_cancel_cb),
416                         NULL);
417
418     gtk_widget_grab_default (button);
419   }
420
421   undo_dialog_refresh_sample_list ();
422   _undo_dialog_set_sample (sample, TRUE);
423  
424   if (!GTK_WIDGET_VISIBLE(undo_dialog)) {
425     gtk_widget_show (undo_dialog);
426   } else {
427     gdk_window_raise (undo_dialog->window);
428   }
429 }
Note: See TracBrowser for help on using the browser.