From 9fcdc88ab52f5fdbb96311490036131bfcb4500b Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Tue, 15 Jan 2013 20:34:14 +0000 Subject: More changes to port TamTamMini to gtk3 Signed-off-by: Gonzalo Odiard --- (limited to 'common') diff --git a/common/Util/LoopSettings.py b/common/Util/LoopSettings.py index a36dd94..abef0e1 100644 --- a/common/Util/LoopSettings.py +++ b/common/Util/LoopSettings.py @@ -1,17 +1,16 @@ -import pygtk -pygtk.require('2.0') -import gtk -import gobject +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject import os from common.Util.ThemeWidgets import * import common.Config as Config import commands Tooltips = Config.Tooltips() -class LoopSettings( gtk.VBox ): +class LoopSettings(Gtk.VBox): def __init__( self, popup, playFunction, setChannelFunction, doneLoopSettingsPopup ): - gtk.VBox.__init__( self ) - self.tooltips = gtk.Tooltips() + Gtk.VBox.__init__( self ) + self.tooltips = Gtk.Tooltips() self.popup = popup self.playFunction = playFunction self.setChannel = setChannelFunction @@ -24,111 +23,111 @@ class LoopSettings( gtk.VBox ): self.register = 0 self.ok = True - self.settingsBox = gtk.HBox() - self.pack_start(self.settingsBox) + self.settingsBox = Gtk.HBox() + self.pack_start(self.settingsBox, True, True, 0) - self.fixed = gtk.Fixed() - self.settingsBox.pack_start(self.fixed) + self.fixed = Gtk.Fixed() + self.settingsBox.pack_start(self.fixed, True, True, 0) - self.mainBox = gtk.VBox() + self.mainBox = Gtk.VBox() - self.controlsBox = gtk.HBox() + self.controlsBox = Gtk.HBox() self.GUI = {} - nameBox = gtk.VBox() - self.nameEntry = gtk.Entry() + nameBox = Gtk.VBox() + self.nameEntry = Gtk.Entry() self.nameEntry.set_text("name_of_the_sound") - nameBox.pack_start(self.nameEntry) + nameBox.pack_start(self.nameEntry, True, True, 0) self.mainBox.pack_start(nameBox, False, False, 5) - loopedBox = gtk.HBox() - loopedLabel = gtk.Label("Looped sound: ") + loopedBox = Gtk.HBox() + loopedLabel = Gtk.Label("Looped sound: ") loopedBox.pack_start(loopedLabel) loopedToggle = ImageToggleButton('checkOff.png', 'checkOn.png') loopedToggle.connect('button-press-event', self.handleLooped ) loopedBox.pack_start(loopedToggle) self.mainBox.pack_start(loopedBox, False, False, 5) - categoryBox = gtk.HBox() - categoryMenu = gtk.MenuBar() - cmenu = gtk.Menu() + categoryBox = Gtk.HBox() + categoryMenu = Gtk.MenuBar() + cmenu = Gtk.Menu() for cat in Config.CATEGORIES: if cat != 'all': - entry = gtk.MenuItem(cat) + entry = Gtk.MenuItem(cat) cmenu.append(entry) entry.connect("activate", self.handleCategory, cat) entry.show() - self.categoryButton = gtk.Button("Category") + self.categoryButton = Gtk.Button("Category") self.categoryButton.connect_object("event", self.categoryBtnPress, cmenu) categoryBox.pack_end(self.categoryButton) #self.mainBox.pack_start(categoryBox, False, False, 5) - registerBox = gtk.HBox() - registerMenu = gtk.MenuBar() - rmenu = gtk.Menu() + registerBox = Gtk.HBox() + registerMenu = Gtk.MenuBar() + rmenu = Gtk.Menu() self.registerList = ['LOW', 'MID', 'HIGH', 'PUNCH'] for reg in self.registerList: - entry = gtk.MenuItem(reg) + entry = Gtk.MenuItem(reg) rmenu.append(entry) entry.connect("activate", self.handleRegister, self.registerList.index(reg)) entry.show() - self.registerButton = gtk.Button("Register") + self.registerButton = Gtk.Button("Register") self.registerButton.connect_object("event", self.registerBtnPress, rmenu) - registerBox.pack_end(self.registerButton) + registerBox.pack_end(self.registerButton, True, True, 0) self.mainBox.pack_start(registerBox, False, False, 5) - startBox = gtk.VBox() - self.startAdjust = gtk.Adjustment( 0.01, 0, 1., .001, .001, 0) + startBox = Gtk.VBox() + self.startAdjust = Gtk.Adjustment( 0.01, 0, 1., .001, .001, 0) self.GUI['startSlider'] = ImageVScale('sliderEditVolume.png', self.startAdjust, 7) self.startAdjust.connect("value-changed", self.handleStart) self.GUI['startSlider'].set_inverted(True) self.GUI['startSlider'].set_size_request(50, 200) - self.startEntry = gtk.Entry() + self.startEntry = Gtk.Entry() self.startEntry.set_width_chars(5) self.handleStart( self.startAdjust ) startBox.pack_start(self.GUI['startSlider'], True, True, 5) startBox.pack_start(self.startEntry, True, True, 5) - self.controlsBox.pack_start(startBox) + self.controlsBox.pack_start(startBox, True, True, 0) - endBox = gtk.VBox() - self.endAdjust = gtk.Adjustment( 0.9, 0, 1, .001, .001, 0) + endBox = Gtk.VBox() + self.endAdjust = Gtk.Adjustment( 0.9, 0, 1, .001, .001, 0) self.GUI['endSlider'] = ImageVScale('sliderEditVolume.png', self.endAdjust, 7) self.endAdjust.connect("value-changed", self.handleEnd) self.GUI['endSlider'].set_inverted(True) self.GUI['endSlider'].set_size_request(50, 200) - self.endEntry = gtk.Entry() + self.endEntry = Gtk.Entry() self.endEntry.set_width_chars(5) self.handleEnd( self.endAdjust ) endBox.pack_start(self.GUI['endSlider'], True, True, 5) endBox.pack_start(self.endEntry, True, True, 5) - self.controlsBox.pack_start(endBox) + self.controlsBox.pack_start(endBox, True, True, 0) - durBox = gtk.VBox() - self.durAdjust = gtk.Adjustment( 0.01, 0, 0.2, .001, .001, 0) + durBox = Gtk.VBox() + self.durAdjust = Gtk.Adjustment( 0.01, 0, 0.2, .001, .001, 0) self.GUI['durSlider'] = ImageVScale('sliderEditVolume.png', self.durAdjust, 7) self.durAdjust.connect("value-changed", self.handleDur) self.GUI['durSlider'].set_inverted(True) self.GUI['durSlider'].set_size_request(50, 200) - self.durEntry = gtk.Entry() + self.durEntry = Gtk.Entry() self.durEntry.set_width_chars(5) self.handleDur( self.durAdjust ) durBox.pack_start(self.GUI['durSlider'], True, True, 5) durBox.pack_start(self.durEntry, True, True, 5) - self.controlsBox.pack_start(durBox) + self.controlsBox.pack_start(durBox, True, True, 0) self.mainBox.pack_start(self.controlsBox, False, False, 5) - previewBox = gtk.VBox() + previewBox = Gtk.VBox() self.playStopButton = ImageToggleButton('miniplay.png', 'stop.png') self.playStopButton.connect('button-press-event' , self.handlePlayButton) - previewBox.pack_start(self.playStopButton) + previewBox.pack_start(self.playStopButton, True, True, 0) self.mainBox.pack_start(previewBox, False, False, 5) - checkBox = gtk.VBox() + checkBox = Gtk.VBox() checkButton = ImageButton('check.png') checkButton.connect('clicked' , self.handleCheck) checkBox.pack_start(checkButton) @@ -174,7 +173,7 @@ class LoopSettings( gtk.VBox ): self.loopedSound = True def categoryBtnPress(self, widget, event): - if event.type == gtk.gdk.BUTTON_PRESS: + if event.type == Gdk.EventType.BUTTON_PRESS: widget.popup(None, None, None, event.button, event.time) return True return False @@ -184,7 +183,7 @@ class LoopSettings( gtk.VBox ): self.categoryButton.set_label(self.category) def registerBtnPress(self, widget, event): - if event.type == gtk.gdk.BUTTON_PRESS: + if event.type == Gdk.EventType.BUTTON_PRESS: widget.popup(None, None, None, event.button, event.time) return True return False @@ -218,7 +217,7 @@ class LoopSettings( gtk.VBox ): if self.ok: self.playFunction(widget.get_active(), self.loopedSound) if self.loopedSound == False and widget.get_active() == False: - self.timeoutStop = gobject.timeout_add(int(self.soundLength * 1000)+500, self.playButtonState) + self.timeoutStop = GObject.timeout_add(int(self.soundLength * 1000)+500, self.playButtonState) def setButtonState(self): self.ok = False @@ -228,5 +227,5 @@ class LoopSettings( gtk.VBox ): def playButtonState(self): self.ok = False self.playStopButton.set_active(False) - gobject.source_remove(self.timeoutStop) + GObject.source_remove(self.timeoutStop) self.ok = True diff --git a/common/Util/Network.py b/common/Util/Network.py index 79fbab5..2042e75 100644 --- a/common/Util/Network.py +++ b/common/Util/Network.py @@ -18,8 +18,8 @@ import xdrlib import random import time -import gtk -import gobject +from gi.repository import Gdk +from gi.repository import GObject import common.Config as Config PORT = 24460 @@ -48,7 +48,7 @@ message_enum = [ ("PR_LATENCY_QUERY", 4), # test latency ("PR_SYNC_QUERY", 4), # test sync ("PR_TEMPO_QUERY", 0), # test sync -("PR_REQUEST_TEMPO_CHANGE", 4), # request tempo change +("PR_REQUEST_TEMPO_CHANGE", 4), # request tempo change ("MAX_MSG_ID", 0) ] @@ -68,7 +68,7 @@ if MAX_MSG_ID > 256: class Listener( threading.Thread ): - + def __init__( self, owner, listenerSocket, inputSockets, outputSockets, exceptSockets ): threading.Thread.__init__(self) self.owner = owner @@ -92,16 +92,16 @@ class Listener( threading.Thread ): continue else: break # exit thread - gtk.gdk.threads_enter() + Gdk.threads_enter() self.owner._processSockets( inputReady ) - gtk.gdk.threads_leave() + Gdk.threads_leave() except socket.error, (value, message): print "Listener:: socket error: " + message - gtk.gdk.threads_leave() + Gdk.threads_leave() break class Connection: - + def __init__( self, sock, address ): self.socket = sock self.address = address @@ -144,7 +144,7 @@ class Network: except socket.error, (value,message): print "Network:: FAILED to open listenerSocket: " + message mode = MD_OFFLINE - + self.inputSockets = [ self.listenerSocket ] # NOTE that these array pointers are passed into self.outputSockets = [] # the Listener and should not be reset self.exceptSockets = [] # @@ -160,7 +160,7 @@ class Network: def shutdown( self ): if Config.DEBUG > 1: print "Network:: shutting down!" - + if self.listener: self.listenerSocket.sendto( "EXIT", ("localhost",LISTENER_PORT) ) time.sleep(0.01) # get off the cpu so the listerer thread has a chance to clear.. IS THERE A BETTER WAY TO DO THIS? @@ -181,13 +181,13 @@ class Network: if self._fromListener: self._clearSockets() elif self.listener: # make the listener wake so sockets can close properly - self.listenerSocket.sendto( "CLEAR", ("localhost",LISTENER_PORT) ) + self.listenerSocket.sendto( "CLEAR", ("localhost",LISTENER_PORT) ) time.sleep(0.01) # get off the cpu so the listerer thread has a chance to clear.. IS THERE A BETTER WAY TO DO THIS? self.hostAddress = None # initialize new mode - self.mode = mode + self.mode = mode if self.mode == MD_HOST: if Config.DEBUG > 1: print "Network:: initializing network, host mode" try: @@ -237,7 +237,7 @@ class Network: self.listener = None elif self.mode == MD_WAIT: - if Config.DEBUG > 1: print "Network:: initializing network, wait mode" + if Config.DEBUG > 1: print "Network:: initializing network, wait mode" try: self.socket = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) address = ("",WAIT_PORT) @@ -259,7 +259,7 @@ class Network: if self.listener: self.listenerSocket.sendto( "EXIT", ("localhost", LISTENER_PORT) ) self.listener = None - + else: if Config.DEBUG > 1: print "Network:: offline" if self.listener: @@ -267,7 +267,7 @@ class Network: self.listener = None for watcher in self.statusWatcher: - watcher( self.mode ) + watcher( self.mode ) def _clearSockets( self ): for s in self.inputSockets: @@ -285,14 +285,14 @@ class Network: def introducePeer( self, ip ): if Config.DEBUG > 1: print "Network:: introducing self to peer " + ip - try: + try: poke = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) poke.setblocking(0) except socket.error, (value, message): print "Network::introducePeer:: FAILED to open socket: " + message return if poke.connect_ex( (ip, WAIT_PORT) ): # failed to connect - gobject.timeout_add( 500, self._pokePeer, poke, ip, 0 ) + GObject.timeout_add( 500, self._pokePeer, poke, ip, 0 ) else: # connected if Config.DEBUG > 1: print "Netwtork:: introduction succeeded" poke.close() @@ -302,14 +302,14 @@ class Network: if retry > 120: # give up print "Network::introducePeer:: peer failed to respond after 60 seconds, giving up!" else: - gobject.timeout_add( 500, self._pokePeer, poke, ip, retry+1 ) + GObject.timeout_add( 500, self._pokePeer, poke, ip, retry+1 ) else: # connected if Config.DEBUG > 1: print "Netwtork:: introduction succeeded" poke.close() return False - + def addPeer( self, peer, address ): if Config.DEBUG > 1: print "Network:: adding peer: %s" % address[0] self.connection[peer] = Connection( peer, address ) @@ -374,7 +374,7 @@ class Network: def isWaiting( self ): if self.mode == MD_WAIT: return True return False - + #----------------------------------------------------------------------- # Message Senders @@ -383,8 +383,8 @@ class Network: # - message type will be automatically inserted before the data # - message size will be automatically inserted if applicable # - to is only defined in HOST mode - def send( self, message, data = "", to = None ): - if self.mode == MD_OFFLINE: + def send( self, message, data = "", to = None ): + if self.mode == MD_OFFLINE: return length = len(data) @@ -444,14 +444,14 @@ class Network: self.packer.reset() for sock in self.connection: - if sock == self.socket: + if sock == self.socket: continue try: sock.send( msg ) except socket.error, (value, errmsg): print "Network:: FAILED to send message (%s) to %s: %s" % (MSG_NAME[message], self.connection[sock].address[0], errmsg) # TODO something intelligent - + def sendLatencyQuery( self, handler ): if self.mode != MD_PEER: return @@ -468,7 +468,7 @@ class Network: def _processSockets( self, inputReady ): - self._fromListener = True + self._fromListener = True if self.mode == MD_HOST: @@ -480,7 +480,7 @@ class Network: self.addPeer( peer, address ) except socket.error, (value, message): print "Network:: error accepting connection: " + message - + else: try: data = s.recv(MAX_SIZE) @@ -493,7 +493,7 @@ class Network: print "Network:: error reading data: " + message elif self.mode == MD_PEER: - + for s in inputReady: try: data = s.recv(MAX_SIZE) @@ -506,7 +506,7 @@ class Network: print "Network:: error reading data: " + message else: # MD_WAIT - + for s in inputReady: try: peer, address = self.socket.accept() @@ -520,7 +520,7 @@ class Network: def processStream( self, sock, newData = "" ): con = self.connection[sock] con.recvBuf += newData - + if con.waitingForData == -1: # message size in char con.waitingForData = ord(con.recvBuf[0]) con.recvBuf = con.recvBuf[1:] @@ -539,7 +539,7 @@ class Network: con.recvBuf = con.recvBuf[con.waitingForData:] con.waitingForData = 0 for func in self.processMessage[con.message]: - gobject.idle_add( func, sock, con.message, data ) + GObject.idle_add( func, sock, con.message, data ) else: return # wait for more data @@ -548,14 +548,14 @@ class Network: if MSG_SIZE[con.message] == 0: con.recvBuf = con.recvBuf[1:] for func in self.processMessage[con.message]: - gobject.idle_add( func, sock, con.message, "" ) + GObject.idle_add( func, sock, con.message, "" ) else: con.waitingForData = MSG_SIZE[con.message] con.recvBuf = con.recvBuf[1:] if len(con.recvBuf): self.processStream( sock ) - + #-- HOST handlers ------------------------------------------------------ def processPR_LATENCY_QUERY( self, sock, message, data ): self.send( HT_LATENCY_REPLY, data, sock ) diff --git a/common/Util/ThemeWidgets.py b/common/Util/ThemeWidgets.py index 98c1acb..8bc00ce 100644 --- a/common/Util/ThemeWidgets.py +++ b/common/Util/ThemeWidgets.py @@ -39,6 +39,7 @@ class ImageHScale( Gtk.HScale ): name = image_name + "ImageHScale" self.set_name(name) + # TODO: replace by new gtk3 styles rc_str = """ style "scale_style" { GtkRange::slider_width = %d @@ -46,7 +47,7 @@ style "scale_style" { } widget "*%s*" style "scale_style" """ % ( self.sliderPixbuf.get_width(), self.sliderPixbuf.get_height(), name) - gtk.rc_parse_string( rc_str ) + #gtk.rc_parse_string( rc_str ) self.pixbufWidth = self.sliderPixbuf.get_width() self.pixbufHeight = self.sliderPixbuf.get_height() @@ -76,7 +77,7 @@ widget "*%s*" style "scale_style" val = round(self.snap*self.get_value())/self.snap if val != self.get_value(): self.set_value( val ) - return True + return True def expose( self, widget, event ): @@ -113,13 +114,16 @@ class ImageVScale( Gtk.VScale ): insensitive_name=None, trough_color="#3D403A", snap=False): image_name = imagefile(image_name) - Gtk.VScale.__init__( self, adjustment ) + Gtk.VScale.__init__(self) + self.set_adjustment(adjustment) + #Gtk.VScale.__init__( self, adjustment ) if snap: self.snap = 1/snap else: self.snap = False - colormap = self.get_colormap() - self.troughcolor = colormap.alloc_color( trough_color, True, True ) + # TODO: Gtk3 port + #colormap = self.get_colormap() + #self.troughcolor = colormap.alloc_color( trough_color, True, True ) img = Gtk.Image() img.set_from_file( image_name ) @@ -135,6 +139,7 @@ class ImageVScale( Gtk.VScale ): name = image_name + "ImageVScale" self.set_name(name) + # TODO: Gtk3 port rc_str = """ style "scale_style" { GtkRange::slider_width = %d @@ -142,7 +147,7 @@ style "scale_style" { } widget "*%s*" style "scale_style" """ % ( self.sliderPixbuf.get_width(), self.sliderPixbuf.get_height(), name) - gtk.rc_parse_string( rc_str ) + #gtk.rc_parse_string( rc_str ) self.pixbufWidth = self.sliderPixbuf.get_width() self.pixbufHeight = self.sliderPixbuf.get_height() @@ -151,7 +156,7 @@ widget "*%s*" style "scale_style" self.set_draw_value(False) - self.connect( "expose-event", self.expose ) + self.connect("draw", self.__draw_cb) self.connect( "size-allocate", self.size_allocate ) self.connect( "button-release-event", self.button_release ) adjustment.connect( "value-changed", self.value_changed ) @@ -172,7 +177,7 @@ widget "*%s*" style "scale_style" if val != self.get_value(): self.set_value( val ) - def expose( self, widget, event ): + def __draw_cb( self, widget, ctx): style = self.get_style() gc = style.fg_gc[Gtk.StateType.NORMAL] @@ -318,7 +323,7 @@ class RoundHBox( Gtk.HBox ): _, self.fillcolor = Gdk.Color.parse(fillcolor) _, self.bordercolor = Gdk.Color.parse(bordercolor) - self.connect( "draw", self.expose ) + self.connect( "draw", self.draw) self.connect( "size-allocate", self.size_allocate ) def update_constants( self ): @@ -374,7 +379,7 @@ class RoundHBox( Gtk.HBox ): def set_border_color( self, color ): _, self.bordercolor = Gdk.Color.parse(color) - def expose( self, widget, cr): + def draw( self, widget, cr): if self.alloc == None: return #TP.ProfileBegin( "Round*Box::expose" ) @@ -480,7 +485,7 @@ class RoundVBox( Gtk.VBox ): #self.fillcolor = colormap.alloc_color(fillcolor,True,True) #self.bordercolor = colormap.alloc_color(bordercolor,True,True) - self.connect( "expose-event",self.expose ) + self.connect( "draw",self.__draw_cb) self.connect( "size-allocate", self.size_allocate ) def update_constants( self ): @@ -537,9 +542,10 @@ class RoundVBox( Gtk.VBox ): colormap = self.get_colormap() self.bordercolor = colormap.alloc_color(color,True,True) - def expose( self, widget, event ): + def __draw_cb( self, widget, ctx): - if self.alloc == None: return + if self.get_allocation() is None: + return #TP.ProfileBegin( "Round*Box::expose" ) @@ -620,7 +626,7 @@ class RoundFixed( Gtk.Fixed ): self.fillcolor = Gdk.Color.parse(fillcolor) self.bordercolor = Gdk.Color.parse(bordercolor) - self.connect( "draw", self.expose ) + self.connect( "draw", self.draw ) self.connect( "size-allocate", self.size_allocate ) def update_constants( self ): @@ -675,7 +681,7 @@ class RoundFixed( Gtk.Fixed ): def set_border_color( self, color ): self.bordercolor = Gdk.Color.parse(color) - def expose( self, widget, cr ): + def draw( self, widget, cr ): if self.alloc == None: return @@ -767,46 +773,33 @@ class ImageButton(Gtk.Button): self.backgroundFill = backgroundFill - def newPrepareImage(name, path): - if path.endswith(".svg"): + def prepareImage(name, path): + path = Config.imagefile(path) + logging.error('ImageButton prepareImage %s', path) + if path.endswith(".png"): + pix = cairo.ImageSurface.create_from_png(path) + self.is_png = True + + elif path.endswith(".svg"): pix = GdkPixbuf.Pixbuf.new_from_file(path) - self.image[name] = pix - self.itype[name] = ITYPE.PIXBUF - self.iwidth[name] = pix.get_width() - self.iwidthDIV2[name] = self.iwidth[name]//2 - self.iheight[name] = pix.get_height() - self.iheightDIV2[name] = self.iheight[name]//2 + self.is_png = False + + self.image[name] = pix + self.itype[name] = ITYPE.PIXBUF - def prepareImage( name, path ): - pix = gtk.gdk.pixbuf_new_from_file(path) - if pix.get_has_alpha(): - if backgroundFill == None: - self.image[name] = pix - self.itype[name] = ITYPE.PIXBUF - else: - self.image[name] = gtk.gdk.Pixmap( win, pix.get_width(), pix.get_height() ) - colormap = self.get_colormap() - self.gc.foreground = colormap.alloc_color( backgroundFill, True, True ) - self.image[name].draw_rectangle( self.gc, True, 0, 0, pix.get_width(), pix.get_height() ) - self.image[name].draw_pixbuf( self.gc, pix, 0, 0, 0, 0, pix.get_width(), pix.get_height(), gtk.gdk.RGB_DITHER_NONE ) - self.itype[name] = ITYPE.PIXMAP - else: - self.image[name] = gtk.gdk.Pixmap( win, pix.get_width(), pix.get_height() ) - self.image[name].draw_pixbuf( self.gc, pix, 0, 0, 0, 0, pix.get_width(), pix.get_height(), gtk.gdk.RGB_DITHER_NONE ) - self.itype[name] = ITYPE.PIXMAP self.iwidth[name] = pix.get_width() self.iwidthDIV2[name] = self.iwidth[name]//2 self.iheight[name] = pix.get_height() self.iheightDIV2[name] = self.iheight[name]//2 - newPrepareImage( "main", mainImg_path ) + prepareImage("main", mainImg_path) if enterImg_path != None: - newPrepareImage( "enter", enterImg_path ) + prepareImage("enter", enterImg_path) self.connect('enter-notify-event',self.on_btn_enter) self.connect('leave-notify-event',self.on_btn_leave) if clickImg_path != None: - newPrepareImage( "click", clickImg_path ) + prepareImage("click", clickImg_path) self.connect('pressed',self.on_btn_press, None) self.connect('released',self.on_btn_release, None) if enterImg_path == None: @@ -833,18 +826,24 @@ class ImageButton(Gtk.Button): self.drawY = allocation.y + allocation.height//2 def draw(self, widget, cr): - #Gdk.cairo_set_source_pixbuf(cr, self.image[self.curImage], 0, 0) - #cr.paint() cr.rectangle(0, 0, 100, 100) cr.fill() + if self.is_png: + cr.set_source_surface(self.image[self.curImage], 0, 0) + cr.paint() + else: + Gdk.cairo_set_source_pixbuf(cr, self.image[self.curImage], 0, 0) + cr.paint() + return True + #TODO Gtk3: verify draw is similar and remove expose def expose(self, widget, event): if self.itype[self.curImage] == ITYPE.PIXBUF: self.window.draw_pixbuf( self.gc, self.image[self.curImage], 0, 0, self.drawX - self.iwidthDIV2[self.curImage], self.drawY - self.iheightDIV2[self.curImage], self.iwidth[self.curImage], self.iheight[self.curImage], gtk.gdk.RGB_DITHER_NONE) else: - self.window.draw_drawable( self.gc, self.image[self.curImage], 0, 0, self.drawX - self.iwidthDIV2[self.curImage], self.drawY - self.iheightDIV2[self.curImage], self.iwidth[self.curImage], self.iheight[self.curImage] ) + self.window.draw_drawable( self.gc, self.image[self.curImage], 0, 0, self.drawX - self.iwidthDIV2[self.curImage], self.drawY - self.iheightDIV2[self.curImage], self.iwidth[self.curImage], self.iheight[self.curImage] ) return True - + def setImage(self, name, pix): print "setImage ", name, pix if name == "main" and self.image["main"] == self.image["enter"]: @@ -905,7 +904,7 @@ class ImageButton(Gtk.Button): self.curImage = self.upImage self.down = False self.queue_draw() - + def set_palette(self, palette): self._palette = palette self._palette.props.invoker = WidgetInvoker(self) @@ -930,7 +929,7 @@ class ImageToggleButton(Gtk.ToggleButton): self.iwidthDIV2 = {} self.iheight = {} self.iheightDIV2 = {} - + self.backgroundFill = backgroundFill def prepareImage( name, path ): @@ -990,6 +989,7 @@ class ImageToggleButton(Gtk.ToggleButton): cr.paint() return True + #TODO Gtk3: verify draw is similar and remove expose def expose(self, widget, cr): if self.itype[self.curImage] == ITYPE.PIXBUF: cr.set_source_surface(self.image[self.curImage], 0, 0) @@ -1060,7 +1060,7 @@ class ImageToggleButton(Gtk.ToggleButton): def released( self, widget ): self.clicked = False self.toggleImage( self ) - + def on_btn_enter(self, widget, event ): if event.mode == Gdk.CrossingMode.NORMAL: self.within = True @@ -1078,7 +1078,7 @@ class ImageToggleButton(Gtk.ToggleButton): else: self.curImage = "alt" self.queue_draw() - + def set_palette(self, palette): self._palette = palette self._palette.props.invoker = WidgetInvoker(self) @@ -1097,8 +1097,7 @@ class ImageRadioButton(Gtk.RadioButton): self.within = False self.clicked = False - win = gtk.gdk.get_default_root_window() - self.gc = gtk.gdk.GC( win ) + win = Gdk.get_default_root_window() self.image = {} self.itype = {} self.iwidth = {} @@ -1109,22 +1108,17 @@ class ImageRadioButton(Gtk.RadioButton): self.backgroundFill = backgroundFill def prepareImage( name, path ): - pix = gtk.gdk.pixbuf_new_from_file(path) - if pix.get_has_alpha(): - if backgroundFill == None: - self.image[name] = pix - self.itype[name] = ITYPE.PIXBUF - else: - self.image[name] = gtk.gdk.Pixmap( win, pix.get_width(), pix.get_height() ) - colormap = self.get_colormap() - self.gc.foreground = colormap.alloc_color( backgroundFill, True, True ) - self.image[name].draw_rectangle( self.gc, True, 0, 0, pix.get_width(), pix.get_height() ) - self.image[name].draw_pixbuf( self.gc, pix, 0, 0, 0, 0, pix.get_width(), pix.get_height(), gtk.gdk.RGB_DITHER_NONE ) - self.itype[name] = ITYPE.PIXMAP - else: - self.image[name] = gtk.gdk.Pixmap( win, pix.get_width(), pix.get_height() ) - self.image[name].draw_pixbuf( self.gc, pix, 0, 0, 0, 0, pix.get_width(), pix.get_height(), gtk.gdk.RGB_DITHER_NONE ) - self.itype[name] = ITYPE.PIXMAP + if path.endswith(".png"): + pix = cairo.ImageSurface.create_from_png(path) + self.is_png = True + + elif path.endswith(".svg"): + pix = GdkPixbuf.Pixbuf.new_from_file(path) + self.is_png = False + + self.image[name] = pix + self.itype[name] = ITYPE.PIXBUF + self.iwidth[name] = pix.get_width() self.iwidthDIV2[name] = self.iwidth[name]//2 self.iheight[name] = pix.get_height() @@ -1149,8 +1143,8 @@ class ImageRadioButton(Gtk.RadioButton): self.connect("toggled", self.toggleImage ) self.connect('pressed',self.pressed ) self.connect('released',self.released ) - self.connect('expose-event', self.expose) self.connect('size-allocate', self.size_allocate) + self.connect('draw', self.draw) self.set_size_request(self.iwidth["main"],self.iheight["main"]) @@ -1161,6 +1155,16 @@ class ImageRadioButton(Gtk.RadioButton): self.drawX = allocation.x + allocation.width//2 self.drawY = allocation.y + allocation.height//2 + def draw(self, widget, cr): + if self.is_png: + cr.set_source_surface(self.image[self.curImage], 0, 0) + cr.paint() + else: + Gdk.cairo_set_source_pixbuf(cr, self.image[self.curImage], 0, 0) + cr.paint() + return True + + #TODO Gtk3: verify draw is similar and remove expose def expose(self, widget, event): if self.itype[self.curImage] == ITYPE.PIXBUF: self.window.draw_pixbuf( self.gc, self.image[self.curImage], 0, 0, self.drawX - self.iwidthDIV2[self.curImage], self.drawY - self.iheightDIV2[self.curImage], self.iwidth[self.curImage], self.iheight[self.curImage], gtk.gdk.RGB_DITHER_NONE) @@ -1224,7 +1228,7 @@ class ImageRadioButton(Gtk.RadioButton): def released( self, widget ): self.clicked = False self.toggleImage( self ) - + def on_btn_enter(self, widget, event): if event.mode == gtk.gdk.CROSSING_NORMAL: self.within = True @@ -1242,12 +1246,12 @@ class ImageRadioButton(Gtk.RadioButton): else: self.curImage = "alt" self.queue_draw() - + def set_palette(self, palette): self._palette = palette self._palette.props.invoker = WidgetInvoker(self) self._palette.props.invoker._position_hint = WidgetInvoker.AT_CURSOR - + class keyButton(Gtk.Button): import cairo def __init__(self, width, height, fillcolor, strokecolor): @@ -1255,26 +1259,26 @@ class keyButton(Gtk.Button): self.alloc = None win = gtk.gdk.get_default_root_window() self.gc = gtk.gdk.GC(win) - + self.connect('expose-event', self.expose) self.connect('size-allocate', self.size_allocate) - + self.width = width self.height = height self.fillcolor = fillcolor self.strokecolor = strokecolor - + self.set_size_request(self.width,self.height) - + def size_allocate(self, widget, allocation): self.alloc = allocation self.drawX = allocation.x + allocation.width//2 self.drawY = allocation.y + allocation.height//2 - + def expose(self, widget, event): self.draw() return True - + def draw(self): self.cr = self.window.cairo_create() self.cr.set_source_rgb(self.fillcolor[0],self.fillcolor[1],self.fillcolor[2]) @@ -1284,8 +1288,8 @@ class keyButton(Gtk.Button): self.cr.set_source_rgb(self.strokecolor[0],self.strokecolor[1],self.strokecolor[2]) self.draw_round_rect(self.cr,self.drawX - self.width//2, self.drawY - self.height //2, self.width,self.height,10) self.cr.stroke() - - def draw_round_rect(self,context,x,y,w,h,r): + + def draw_round_rect(self,context,x,y,w,h,r): context.move_to(x+r,y) # Move to A context.line_to(x+w-r,y) # Straight line to B context.curve_to(x+w,y,x+w,y,x+w,y+r) # Curve to C, Control points are both at Q @@ -1296,15 +1300,15 @@ class keyButton(Gtk.Button): context.line_to(x,y+r) # Line to H context.curve_to(x,y,x,y,x+r,y) # Curve to A return - + def set_fillcolor(self,r,g,b): self.fillcolor = [r,g,b] self.queue_draw() - + def set_strokecolor(self,r,g,b): self.strokecolor = [r,g,b] self.queue_draw() - + class BigComboBox(Gtk.ComboBox): def __init__(self): Gtk.ComboBox.__init__(self) @@ -1338,7 +1342,7 @@ class BigComboBox(Gtk.ComboBox): width, height = Gtk.icon_size_lookup(size) else: width, height = size - if icon_name[0:6] == "theme:": + if icon_name[0:6] == "theme:": icon_name = self._get_real_name_from_theme(icon_name[6:], size) pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_name, width, height) else: -- cgit v0.9.1