rendered paste body#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import gtk
import cairo
import fnmatch
#~ import cPickle as pickle
from ADeskBar.core import logINFO, find_icon, launch_command
class MenuItem:
def __init__(self, label, command, icon, submenu):
self.label = label
self.command = command.split('%')[0]
self.icon = icon
self.submenu = submenu
#-----------------------------------------------------------------------
class Bar():
#-----------------------------------------------------------------------
def __init__(self):
self.create_window()
self.screen_changed(self.window)
self.init_bar_pos()
self.screen_changed(self.nbook)
#~ sys.exit()
self.window.show_all()
def init_bar_pos(self):
#~ self.bar_width , self.bar_height = self.window.get_size()
screen_width, screen_height = gtk.gdk.screen_width(), gtk.gdk.screen_height()
width, height = 100,200
#~ x, y = (screen_width - width)/2 , (screen_height - height)/2
x, y = 10, 150
#~ self.window.move(x,y)
#~ self.window.resize(width, height)
self.frame = gtk.Frame()
self.frame.set_size_request(300,250)
self.nbook = gtk.Notebook()
self.nbook.set_tab_pos(gtk.POS_LEFT)
self.nbook.set_border_width(2)
self.frame.add(self.nbook)
self.window.add(self.frame)
self.create_menu()
self.nbook.show()
def expose(self, widget, event):
## Full transparent window
cr = self.window.window.cairo_create()
cr.set_source_rgba(0, 0, 0, 0)
cr.set_operator(cairo.OPERATOR_SOURCE)
cr.paint()
x, y, width, height = self.window.get_allocation()
x, y, width, height = x+1, y+1, width-2, height-2
self.draw_shape(cr, x, y, width, height)
def draw_shape(self, cr, x, y, width, height, flag=False):
opacity = 0.45
r, g, b = 0.0, 0.0, 0.0
cr.translate(x, y)
lg = cairo.LinearGradient(0, y, 0, y+height)
i = 0.0
while i <= 0.3:
lg.add_color_stop_rgba(1-i, r-(i*r), g-(i*g), b-(i*b), opacity)
i += 0.1
i = 0.7
while i <= 1.0:
lg.add_color_stop_rgba(1-i, r-(1-i)*r, g-(1-i)*g, b-(1-i)*b, opacity)
i += 0.1
radius = min(10, width/2, height/2)
cr.move_to (0, radius)
cr.arc (radius, radius, radius, 3.14, 1.5 * 3.14)
cr.line_to (width - radius, 0)
cr.arc (width - radius, 0 + radius, radius, 1.5 * 3.14, 0.0)
cr.line_to (width , height - radius)
cr.arc (width - radius, height - radius, radius, 0.0, 0.5 * 3.14)
cr.line_to (radius, height)
cr.arc (radius, height - radius, radius, 0.5 * 3.14, 3.14)
cr.close_path ()
cr.set_source(lg)
cr.fill()
cr.save()
#~ cr.translate(x, y)
cr.move_to (0, radius)
cr.arc (radius, radius, radius, 3.14, 1.5 * 3.14)
cr.line_to (width - radius, 0)
cr.arc (width - radius, 0 + radius, radius, 1.5 * 3.14, 0.0)
cr.line_to (width , height - radius)
cr.arc (width - radius, height - radius, radius, 0.0, 0.5 * 3.14)
cr.line_to (radius, height)
cr.arc (radius, height - radius, radius, 0.5 * 3.14, 3.14)
cr.close_path ()
cr.set_source_rgba (0.3, 0.3, 0.3, 0.7)
cr.set_line_width(1)
cr.stroke()
cr.restore()
return False
def doquit(self, widget=None, data=None):
gtk.main_quit()
def run(self):
gtk.main()
def create_window(self):
self.window = gtk.Window() ## gtk.WINDOW_TOPLEVEL)
self.window.add_events(gtk.gdk.BUTTON_PRESS_MASK|
gtk.gdk.ENTER_NOTIFY|
gtk.gdk.LEAVE_NOTIFY)
self.window.connect("destroy", self.doquit)
self.window.connect('expose-event', self.expose)
self.window.connect("button_press_event", self.doquit)
self.window.connect("focus", self.event_focus)
self.window.stick()
self.window.set_decorated(False)
#~ self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK)
self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
#~ self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_UTILITY)
self.window.set_skip_taskbar_hint(True)
self.window.set_resizable(True)
self.window.set_border_width(10)
self.window.set_app_paintable(True)
self.window.set_title("ADeskBar")
self.window.set_name("ADeskBarWindow")
def event_focus(self, widget, event):
print "Got Focus .."
def screen_changed(self, widget, old_screen=None):
# To check if the display supports alpha channels, get the colormap
screen = widget.get_screen()
colormap = screen.get_rgba_colormap()
if colormap == None:
colormap = screen.get_rgb_colormap()
self.supports_alpha = False
else:
self.supports_alpha = True
# Now we have a colormap appropriate for the screen, use it
widget.set_colormap(colormap)
return False
def create_menu(self):
cat_dict = {
'Sound & Video':[],
'Programming':[],
'Education':[],
'Games':[],
'Graphics':[],
'Internet':[],
'Office':[],
'System Tools':[],
'Accessories':[],
'Others':[]
}
for file in os.listdir('/usr/share/applications/'):
if fnmatch.fnmatch(file,'*.desktop'):
try:
item = self.ParseDesktopFile('/usr/share/applications/%s' % file)
cat_dict[item.submenu].append(item)
except:
pass
#~ print ("can't parse %s" % file)
#~ for file in os.listdir('/usr/share/applications/kde'):
#~ if fnmatch.fnmatch(file,'*.desktop'):
#~ try:
#~ item = self.ParseDesktopFile('/usr/share/applications/kde/%s' % file)
#~ cat_dict[item.submenu].append(item)
#~ except:
#~ pass
#~ for file in os.listdir('/usr/share/applications/kde4'):
#~ if fnmatch.fnmatch(file,'*.desktop'):
#~ try:
#~ item = self.ParseDesktopFile('/usr/share/applications/kde4/%s' % file)
#~ cat_dict[item.submenu].append(item)
#~ except:
#~ pass
list = self.sortdic(cat_dict)
for item in list:
for item1 in item:
pass
#~ print item1.label
for key in cat_dict:
if not len(cat_dict[key]) == 0:
bBox = gtk.VBox()
bBox.set_spacing(4)
bBox.set_border_width(2)
scrolled = gtk.ScrolledWindow()
scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
scrolled.add_with_viewport(bBox)
label = gtk.Label(key)
label.set_alignment(0, 1)
self.nbook.append_page(scrolled, label)
for item in cat_dict[key]:
#~ widget = gtk.EventBox()
#~ widget.set_visible_window(False)
#~ widget.set_above_child(True)
#~ widget.set_size_request(self.cfg['icon_size'], self.cfg['icon_size'])
widget = gtk.Button() # item.label)
widget.set_relief(gtk.RELIEF_NONE)
widget.set_border_width(0)
widget.set_focus_on_click(False)
widget.set_property('can-focus', False)
image = gtk.Image()
image.set_size_request(24,24)
label = gtk.Label(item.label)
label.set_alignment(0.2, 1)
align = gtk.Alignment(1, 0, 0.5, 0.5)
align.add(label)
box = gtk.HBox(False, 4)
box.pack_start(image, False, False)
box.pack_start(align, False, False)
widget.add(box)
icon_path = find_icon(item.icon)
if icon_path == None:
icon_path = "ADeskBar/images/def_icon.png"
#~ print "icon =", item.icon, "path =", icon_path
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(icon_path, 24, 24)
image.set_from_pixbuf(pixbuf)
image.show()
widget.connect("button-release-event", self.ExecuteAction, item.command)
bBox.pack_start(widget,False,False)
widget.show()
def sortdic(self, adict):
''' Takes a dictionary and returns a SORTED list of items (not keys) '''
items = adict.items()
items.sort()
return [value for key, value in items]
def ParseDesktopFile(self, filename):
''' Parses a .desktop file '''
label = ''
command = ''
icon = ''
submenu = ''
try:
f = open(filename,"r")
except:
print "can't open", filename
for line in f.readlines():
if line[0] != '#':
text = line.split('=')
if text[0] == 'Name' :
label = text[1].replace ( "\n", "" )
elif text[0] == 'Exec':
command = text[1].replace ( "\n", "" )
elif text[0] == 'Icon':
icon = text[1].replace ( "\n", "" )
elif text[0] == 'Categories':
if ';' in text[1]:
categories = text[1].split(';')
else:
categories = text[1]
if 'AudioVideo' in categories:
submenu = 'Sound & Video'
elif 'Development' in categories:
submenu = 'Programming'
elif 'Education' in categories:
submenu = 'Education'
elif 'Game' in categories:
submenu = 'Games'
elif 'Graphics' in categories:
submenu = 'Graphics'
elif 'Network' in categories:
submenu = 'Internet'
elif 'Office' in categories:
submenu = 'Office'
elif 'System' in categories:
submenu = 'System Tools'
elif 'Utility' in categories:
submenu = 'Accessories'
else:
submenu = 'Others'
f.close()
if label != '' and submenu != '':
return MenuItem(label, command, icon, submenu)
else:
print 'un truc bizarre ici .. '
return MenuItem(label, command, icon, submenu)
def ExecuteAction(self, widget, event, cmd):
launch_command(cmd)
self.doquit()
#-------------------------
if __name__ == "__main__":
#-------------------------
## need to change directory
SRC_PATH = os.path.dirname(os.path.realpath( __file__ ))
os.chdir(SRC_PATH)
bar = Bar()
bar.run()