root/branches/0.3/src/mesk/gtk_utils.py

Revision 756, 6.0 kB (checked in by nicfit, 2 years ago)

Better plalist drop sorting (artist, album year, track num)
Busy cursor support during long URI drops

Line 
1 # -*- coding: utf-8 -*-
2 ################################################################################
3 #  Copyright (C) 2006  Travis Shirk <travis@pobox.com>
4 #
5 #  This program is free software; you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation; either version 2 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 #
19 #  $Id$
20 ################################################################################
21 import os, sys
22 import gobject, gtk, gtk.glade, pango
23 import xml.sax.saxutils
24
25 def escape_pango_markup(s):
26     escape_str = ''
27     if s:
28         escape_str = gobject.markup_escape_text(s)
29     return escape_str
30
31 def unescape_pango_markup(s):
32     unesc_str = ''
33     if s:
34         unesc_str = xml.sax.saxutils.unescape(s, {'&apos;': '\'',
35                                                   '&quot;': '"',
36                                                  })
37     return unesc_str
38
39 # This function was ripped from Gajim (http://www.gajim.org)
40 def get_default_font():
41     '''Get the desktop setting for application font
42     first check for GNOME, then XFCE and last KDE
43     it returns None on failure or else a string 'Font Size' '''
44
45     # Gnome/gconf
46     try:
47         import gconf
48         # in try because daemon may not be there
49         client = gconf.client_get_default()
50         return client.get_string('/desktop/gnome/interface/font_name')
51     except:
52         pass
53
54     # try to get xfce default font
55     # Xfce 4.2 adopts freedesktop.org's Base Directory Specification
56     # see http://www.xfce.org/~benny/xfce/file-locations.html
57     # and http://freedesktop.org/Standards/basedir-spec
58     xdg_config_home = os.environ.get('XDG_CONFIG_HOME', '')
59     if xdg_config_home == '':
60         xdg_config_home = os.path.expanduser('~/.config') # default     
61     xfce_config_file = os.path.join(xdg_config_home,
62                                     'xfce4/mcs_settings/gtk.xml')
63
64     # KDE
65     kde_config_file = os.path.expanduser('~/.kde/share/config/kdeglobals')
66
67     if os.path.exists(xfce_config_file):
68         try:
69             for line in file(xfce_config_file):
70                 if line.find('name="Gtk/FontName"') != -1:
71                     start = line.find('value="') + 7
72                     return line[start:line.find('"', start)]
73         except:
74             #we talk about file
75             print >> sys.stderr, \
76                      'Error: cannot open %s for reading' % xfce_config_file
77     elif os.path.exists(kde_config_file):
78         try:
79             for line in file(kde_config_file):
80                 if line.find('font=') == 0: # font=Verdana,9,other_numbers
81                     start = 5 # 5 is len('font=')
82                     line = line[start:]
83                     values = line.split(',')
84                     font_name = values[0]
85                     font_size = values[1]
86                     font_string = '%s %s' % (font_name, font_size) # Verdana 9
87                     return font_string
88         except:
89              #we talk about file
90              print >> sys.stderr, \
91                       'Error: cannot open %s for reading' % kde_config_file
92
93     return 'Sans 10'
94
95 def get_glade(symbol, glade_file=None):
96     '''Create a glade object for the named symbol.  By default the symbol
97     name is also used as the glade filename.  This can be overridden by
98     with the glade_file argument.'''
99     glade_dir = os.path.join("data", "glade")
100     if not glade_file:
101         glade_file = '%s.glade' % symbol
102     return gtk.glade.XML(os.path.join(glade_dir, glade_file), symbol, 'mesk')
103
104 def update_pending_events():
105     while gtk.events_pending():
106         gtk.main_iteration(False)
107
108 def default_linkbutton_callback(button, arg=None):
109         import utils
110         utils.load_web_page(button.get_uri())
111
112 def set_cursor(window, cursor):
113     if not type(cursor) is gtk.gdk.Cursor and cursor is not None:
114         cursor = gtk.gdk.Cursor(cursor)
115     window.set_cursor(cursor)
116     update_pending_events()
117
118 __LEFT_PTR_WATCH = None
119 def set_busy_cursor(window):
120     global __LEFT_PTR_WATCH
121     if __LEFT_PTR_WATCH is None:
122         os.environ['XCURSOR_DISCOVER'] = '1' #Turn on logging in Xlib
123         # Busy cursor code from Pádraig Brady <P@draigBrady.com>
124         # cursor_data hash is 08e8e1c95fe2fc01f976f1e063a24ccd
125         cursor_data = "\
126 \x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\
127 \x0c\x00\x00\x00\x1c\x00\x00\x00\x3c\x00\x00\x00\
128 \x7c\x00\x00\x00\xfc\x00\x00\x00\xfc\x01\x00\x00\
129 \xfc\x3b\x00\x00\x7c\x38\x00\x00\x6c\x54\x00\x00\
130 \xc4\xdc\x00\x00\xc0\x44\x00\x00\x80\x39\x00\x00\
131 \x80\x39\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
132 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
133 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
134 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
135 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
136 \x00\x00\x00\x00\x00\x00\x00\x00"
137
138         try:
139             pix = gtk.gdk.bitmap_create_from_data(None, cursor_data, 32, 32)
140             color = gtk.gdk.Color()
141             __LEFT_PTR_WATCH = gtk.gdk.Cursor(pix, pix, color, color, 2, 2)
142         except TypeError:
143             # old bug http://bugzilla.gnome.org/show_bug.cgi?id=103616
144             # default "WATCH" cursor
145             __LEFT_PTR_WATCH = gtk.gdk.Cursor(gtk.gdk.WATCH)
146     set_cursor(window, __LEFT_PTR_WATCH)
147
148 __INVISIBLE_CURSOR = None
149 def set_invisible_cursor(window):
150     global __INVISIBLE_CURSOR
151     if __INVISIBLE_CURSOR is None:
152         pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
153         color = gtk.gdk.Color()
154         __INVISIBLE_CURSOR = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
155     set_cursor(window, __INVISIBLE_CURSOR)
Note: See TracBrowser for help on using the browser.