import gtk
import os

from numpy import float32, float64

from reinteract.custom_result import CustomResult

class PlayResult(CustomResult):
    def __init__(self, data):
        self.__data = data

    def create_widget(self):
        widget = gtk.Button("Play")
        widget.connect('clicked', self.play)

        return widget

    def play(self, *args):
        if self.__data.dtype == float32:
            command = 'play -t raw -r 44100 -f -4 -L -q -'
        else:
            command = 'play -t raw -r 44100 -f -8 -L -q -'
            
        f = os.popen(command, 'w')
        self.__data.tofile(f)
        f.close()
    
def play(data):
    if data.dtype != float32 and data.dtype != float64:
        raise TypeError("Data must be float32 or float64, not %s", data.dtype)
    
    return PlayResult(data)

@@ -303,16 +303,84 @@
 .play)%0A%0A
+        widget.connect('button_press_event', self.on_button_press)%0A%0A
         
@@ -476,33 +476,33 @@
       command = 
-'
+%22
 play -t raw -r 4
@@ -511,33 +511,33 @@
 00 -f -4 -L -q -
-'
+%22
 %0A        else:%0A 
@@ -561,13 +561,414 @@
 d = 
-'play
+%22play -t raw -r 44100 -f -8 -L -q -%22%0A            %0A        f = os.popen(command, 'w')%0A        self.__data.tofile(f)%0A        f.close()%0A%0A    def __save(self, filename):%0A        escaped = filename.replace(%22'%22, r%22'%5C''%22)%0A        print repr(escaped)%0A        %0A        if self.__data.dtype == float32:%0A            command = %22sox -t raw -r 44100 -f -4 -L -q - '%25s'%22 %25 escaped%0A        else:%0A            command = %22sox
  -t 
@@ -993,17 +993,32 @@
  -L -q -
-'
+ '%25s'%22 %25 escaped
 %0A       
@@ -1105,16 +1105,1336 @@
 .close()
+%0A%0A    def on_button_press(self, button, event):%0A        if event.button == 3:%0A            toplevel = button.get_toplevel()%0A        %0A            menu = gtk.Menu()%0A            menu_item = gtk.ImageMenuItem(stock_id=gtk.STOCK_SAVE_AS)%0A            menu_item.show()%0A            menu.add(menu_item)%0A%0A            def on_selection_done(menu):%0A                menu.destroy()%0A            menu.connect('selection-done', on_selection_done)%0A%0A            def on_activate(menu):%0A                chooser = gtk.FileChooserDialog(%22Save As...%22, toplevel, gtk.FILE_CHOOSER_ACTION_SAVE,%0A                                                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,%0A                                                 gtk.STOCK_SAVE,   gtk.RESPONSE_OK))%0A                chooser.set_default_response(gtk.RESPONSE_OK)%0A                response = chooser.run()%0A                filename = None%0A                if response == gtk.RESPONSE_OK:%0A                    filename = chooser.get_filename()%0A%0A                chooser.destroy()%0A                %0A                if filename != None:%0A                    self.__save(filename)%0A                    %0A            menu_item.connect('activate', on_activate)%0A            %0A            menu.popup(None, None, None, event.button, event.time)%0A            %0A            return True%0A        return False
 %0A    %0Ade

Add right click "Save As" to the play output widget