From de4cae787fbf023196cbd8a9f6283305a6690927 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Mon, 10 Jun 2013 21:40:59 +0000 Subject: FontComboBox updated Signed-off-by: Gonzalo Odiard --- diff --git a/fontcombobox.py b/fontcombobox.py index 9de5e34..9cb8579 100644 --- a/fontcombobox.py +++ b/fontcombobox.py @@ -15,52 +15,184 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import os +import shutil + from gi.repository import Gtk from gi.repository import GObject +from gi.repository import Gio + +from sugar3.graphics.icon import Icon +from sugar3.graphics.palette import Palette, ToolInvoker +from sugar3.graphics.palettemenu import PaletteMenuBox +from sugar3.graphics.palettemenu import PaletteMenuItem +from sugar3.graphics import style +from sugar3 import env + +DEFAULT_FONTS = ['Sans', 'Serif', 'Monospace'] +USER_FONTS_FILE_PATH = env.get_profile_path('fonts') +GLOBAL_FONTS_FILE_PATH = '/etc/sugar_fonts' + -FONT_BLACKLIST = ['cmex10', 'cmmi10', 'cmr10', 'cmsy10', 'esint10', 'eufm10', - 'msam10', 'msbm10', 'rsfs10', 'wasy10'] +class FontLabel(Gtk.Label): + def __init__(self, default_font='Sans'): + Gtk.Label.__init__(self) + self._font = None + self.set_font(default_font) -class FontComboBox(Gtk.ComboBox): + def set_font(self, font): + if self._font != font: + self.set_markup('%s' % (font, font)) + + +class FontComboBox(Gtk.ToolItem): + + __gsignals__ = { + 'changed': (GObject.SignalFlags.RUN_LAST, None, ([])), } def __init__(self): - GObject.GObject.__init__(self) - font_renderer = Gtk.CellRendererText() - self.pack_start(font_renderer, True) - self.add_attribute(font_renderer, 'text', 0) - self.add_attribute(font_renderer, 'font', 0) - font_model = Gtk.ListStore(str) + self._palette_invoker = ToolInvoker() + Gtk.ToolItem.__init__(self) + self._font_label = FontLabel() + bt = Gtk.Button('') + bt.remove(bt.get_children()[0]) + box = Gtk.HBox() + bt.add(box) + icon = Icon(icon_name='font-text') + box.pack_start(icon, False, False, 10) + box.pack_start(self._font_label, False, False, 10) + self.add(bt) + self.show_all() + + self._font_name = 'Sans' + + # theme the button, can be removed if add the style to the sugar css + if style.zoom(100) == 100: + subcell_size = 15 + else: + subcell_size = 11 + radius = 2 * subcell_size + theme = "GtkButton {border-radius: %dpx;}" % radius + css_provider = Gtk.CssProvider() + css_provider.load_from_data(theme) + style_context = bt.get_style_context() + style_context.add_provider(css_provider, + Gtk.STYLE_PROVIDER_PRIORITY_USER) + + # init palette + self._hide_tooltip_on_click = True + self._palette_invoker.attach_tool(self) + self._palette_invoker.props.toggle_palette = True + + self.palette = Palette('Select font') + self.palette.set_invoker(self._palette_invoker) + + # load the fonts in the palette menu + self._menu_box = PaletteMenuBox() + self.props.palette.set_content(self._menu_box) + self._menu_box.show() context = self.get_pango_context() - font_index = 0 - self.faces = {} + self._init_font_list() + + tmp_list = [] for family in context.list_families(): name = family.get_name() - if name not in FONT_BLACKLIST: - font_model.append([name]) - # TODO gtk3 -# font_faces = [] -# for face in family.list_faces(): -# face_name = face.get_face_name() -# font_faces.append(face_name) -# self.faces[name] = font_faces - - font_model.set_sort_column_id(0, Gtk.SortType.ASCENDING) - self.set_model(font_model) - self.show() + if name in self._font_white_list: + tmp_list.append(name) + for name in sorted(tmp_list): + self._add_menu(name, self.__font_selected_cb) + + self._font_label.set_font(self._font_name) + + def _init_font_list(self): + self._font_white_list = [] + self._font_white_list.extend(DEFAULT_FONTS) + + # check if there are a user configuration file + if not os.path.exists(USER_FONTS_FILE_PATH): + # verify if exists a file in /etc + if os.path.exists(GLOBAL_FONTS_FILE_PATH): + shutil.copy(GLOBAL_FONTS_FILE_PATH, USER_FONTS_FILE_PATH) + + if os.path.exists(USER_FONTS_FILE_PATH): + # get the font names in the file to the white list + fonts_file = open(USER_FONTS_FILE_PATH) + # get the font names in the file to the white list + for line in fonts_file: + self._font_white_list.append(line.strip()) + # monitor changes in the file + gio_fonts_file = Gio.File.new_for_path(USER_FONTS_FILE_PATH) + self.monitor = gio_fonts_file.monitor_file( + Gio.FileMonitorFlags.NONE, None) + self.monitor.set_rate_limit(5000) + self.monitor.connect('changed', self._reload_fonts) + + def _reload_fonts(self, monitor, gio_file, other_file, event): + if event != Gio.FileMonitorEvent.CHANGES_DONE_HINT: + return + self._font_white_list = [] + self._font_white_list.extend(DEFAULT_FONTS) + fonts_file = open(USER_FONTS_FILE_PATH) + for line in fonts_file: + self._font_white_list.append(line.strip()) + # update the menu + for child in self._menu_box.get_children(): + self._menu_box.remove(child) + child = None + context = self.get_pango_context() + tmp_list = [] + for family in context.list_families(): + name = family.get_name() + if name in self._font_white_list: + tmp_list.append(name) + for name in sorted(tmp_list): + self._add_menu(name, self.__font_selected_cb) + return False + + def __font_selected_cb(self, menu, font_name): + self._font_name = font_name + self._font_label.set_font(font_name) + self.emit('changed') + + def _add_menu(self, font_name, activate_cb): + label = '%s' % (font_name, font_name) + menu_item = PaletteMenuItem() + menu_item.set_label(label) + menu_item.connect('activate', activate_cb, font_name) + self._menu_box.append_item(menu_item) + menu_item.show() + + def __destroy_cb(self, icon): + if self._palette_invoker is not None: + self._palette_invoker.detach() + + def create_palette(self): + return None + + def get_palette(self): + return self._palette_invoker.palette + + def set_palette(self, palette): + self._palette_invoker.palette = palette + + palette = GObject.property( + type=object, setter=set_palette, getter=get_palette) + + def get_palette_invoker(self): + return self._palette_invoker + + def set_palette_invoker(self, palette_invoker): + self._palette_invoker.detach() + self._palette_invoker = palette_invoker + + palette_invoker = GObject.property( + type=object, setter=set_palette_invoker, getter=get_palette_invoker) def set_font_name(self, font_name): - count = 0 - tree_iter = self.get_model().get_iter_first() - while tree_iter is not None: - value = self.get_model().get_value(tree_iter, 0) - if value == font_name: - self.set_active(count) - count = count + 1 - tree_iter = self.get_model().iter_next(tree_iter) + self._font_label.set_font(font_name) def get_font_name(self): - tree_iter = self.get_active_iter() - return self.get_model().get_value(tree_iter, 0) + return self._font_name -- cgit v0.9.1