Skip to content

Commit

Permalink
Arreglos estéticos al menu de la botonera
Browse files Browse the repository at this point in the history
  • Loading branch information
icarito committed Dec 27, 2014
1 parent 6018ddd commit 1f87d87
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 30 deletions.
50 changes: 20 additions & 30 deletions Toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import gobject
import pango

from popupmenubutton import PopupMenuButton

from ConfigParser import SafeConfigParser

from Globales import COLORES
Expand All @@ -47,8 +49,6 @@ def __init__(self):

toolbar = gtk.Toolbar()

self.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
self.modify_fg(gtk.STATE_NORMAL, COLORES["text"])
toolbar.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
toolbar.modify_fg(gtk.STATE_NORMAL, COLORES["text"])

Expand All @@ -74,6 +74,7 @@ def __init__(self):
item.set_expand(True)
label = gtk.Label(text)
label.modify_font(pango.FontDescription("DejaVu Sans Bold 16"))
label.modify_fg(gtk.STATE_NORMAL, COLORES["text"])
boton = gtk.ToggleToolButton()
boton.set_label_widget(label)
boton.connect("toggled", self.__do_toggled)
Expand All @@ -84,19 +85,27 @@ def __init__(self):
separador.props.draw = True
toolbar.insert(separador, -1)

self.menubar = MenuBar()
self.menu = Menu()

self.menubutton = PopupMenuButton("Topics")
self.menubutton.child.modify_font(pango.FontDescription("DejaVu Sans Bold 16"))
self.menubutton.child.modify_fg(gtk.STATE_NORMAL, COLORES["text"])
self.menubutton.child.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
self.menubutton.set_menu(self.menu)

item = gtk.ToolItem()
item.set_expand(True)
item.add(self.menubar)
item.add(self.menubutton)
toolbar.insert(item, -1)

self.add(toolbar)
self.show_all()

self.menubar.connect("activar", self.__emit_accion_menu)
self.menu.connect("activar", self.__emit_accion_menu)

def __emit_accion_menu(self, widget, topic):
for button in self.buttons:
button.set_active(False)
self.emit("video", topic)

def __go_home(self, widget):
Expand All @@ -110,47 +119,28 @@ def __do_toggled(self, widget):
if label != button.get_label_widget().get_text():
button.set_active(False)
self.emit("activar", label)
self.menubutton.set_active(False)
else:
for button in self.buttons:
if button.get_active():
return
self.buttons[0].set_active(True)


class MenuBar(gtk.MenuBar):
class Menu(gtk.Menu):

__gsignals__ = {
"activar": (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, (gobject.TYPE_STRING, ))}

def __init__(self):

gtk.MenuBar.__init__(self)
gtk.Menu.__init__(self)

self.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
self.modify_fg(gtk.STATE_NORMAL, COLORES["text"])
self.modify_font(pango.FontDescription("DejaVu Sans Bold 16"))

itemmenu = gtk.MenuItem("Topics")
itemmenu.child.modify_font(pango.FontDescription("DejaVu Sans Bold 16"))
itemmenu.child.modify_fg(gtk.STATE_NORMAL, COLORES["text"])
itemmenu.child.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])

menu = gtk.Menu()
menu.modify_font(pango.FontDescription("DejaVu Sans 12"))
menu.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
menu.modify_fg(gtk.STATE_NORMAL, COLORES["text"])
itemmenu.set_submenu(menu)
self.append(itemmenu)

topics = os.path.join(BASE_PATH, "Topics")
for arch in sorted(os.listdir(topics)):
item = gtk.MenuItem()
try:
item.get_child().destroy()
except:
pass


parser = SafeConfigParser()
metadata = os.path.join(topics, arch, "topic.ini")
Expand All @@ -163,10 +153,10 @@ def __init__(self):
boton.set_padding(xpad=20, ypad=20)
item.add(boton)
item.connect("activate", self.__emit_accion_menu, arch)
menu.append(item)
item.show()
boton.show()
self.append(item)

def __emit_accion_menu(self, widget, arch):
#widget.get_children()[0].set_active(True)
#label = widget.get_children()[0].get_label()
label = arch
self.emit("activar", os.path.join(BASE_PATH, "Topics", label))
132 changes: 132 additions & 0 deletions popupmenubutton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright 2008-2011 Zuza Software Foundation
#
# This file is part of Virtaal.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

import gtk

# Positioning constants below:
# POS_CENTER_BELOW: Centers the pop-up window below the button (default).
# POS_CENTER_ABOVE: Centers the pop-up window above the button.
# POS_NW_SW: Positions the pop-up window so that its North West (top left)
# corner is on the South West corner of the button.
# POS_NE_SE: Positions the pop-up window so that its North East (top right)
# corner is on the South East corner of the button. RTL of POS_NW_SW
# POS_NW_NE: Positions the pop-up window so that its North West (top left)
# corner is on the North East corner of the button.
# POS_SW_NW: Positions the pop-up window so that its South West (bottom left)
# corner is on the North West corner of the button.
# POS_SE_NE: Positions the pop-up window so that its South East (bottom right)
# corner is on the North East corner of the button. RTL of POS_SW_NW
POS_CENTER_BELOW, POS_CENTER_ABOVE, POS_NW_SW, POS_NE_SE, POS_NW_NE, POS_SW_NW, POS_SE_NE = range(7)
# XXX: Add position symbols above as needed and implementation in
# _update_popup_geometry()

_rtl_pos_map = {
POS_CENTER_BELOW: POS_CENTER_BELOW,
POS_CENTER_ABOVE: POS_CENTER_ABOVE,
POS_SW_NW: POS_SE_NE,
POS_NW_SW: POS_NE_SE,
}

class PopupMenuButton(gtk.ToggleButton):
"""A toggle button that displays a pop-up menu when clicked."""

# INITIALIZERS #
def __init__(self, label=None, menu_pos=POS_NW_SW):
gtk.ToggleButton.__init__(self, label=label)
self.set_relief(gtk.RELIEF_NONE)
self.set_menu(gtk.Menu())

if self.get_direction() == gtk.TEXT_DIR_LTR:
self.menu_pos = menu_pos
else:
self.menu_pos = _rtl_pos_map.get(menu_pos, POS_SE_NE)

self.connect('toggled', self._on_toggled)


# ACCESSORS #
def set_menu(self, menu):
if getattr(self, '_menu_selection_done_id', None):
self.menu.disconnect(self._menu_selection_done_id)
self.menu = menu
self._menu_selection_done_id = self.menu.connect('selection-done', self._on_menu_selection_done)

def get_label_widget(self):
return self.child

def _get_text(self):
return unicode(self.get_label())
def _set_text(self, value):
self.set_label(value)
text = property(_get_text, _set_text)


# METHODS #
def _calculate_popup_pos(self, menu):
menu_width, menu_height = 0, 0
menu_alloc = menu.get_allocation()
if menu_alloc.height > 1:
menu_height = menu_alloc.height
menu_width = menu_alloc.width
else:
menu_width, menu_height = menu.size_request()

btn_window_xy = self.window.get_origin()
btn_alloc = self.get_allocation()

# Default values are POS_SW_NW
x = btn_window_xy[0] + btn_alloc.x
y = btn_window_xy[1] + btn_alloc.y - menu_height
if self.menu_pos == POS_NW_SW:
y = btn_window_xy[1] + btn_alloc.y + btn_alloc.height
elif self.menu_pos == POS_NE_SE:
x -= (menu_width - btn_alloc.width)
y = btn_window_xy[1] + btn_alloc.y + btn_alloc.height
elif self.menu_pos == POS_SE_NE:
x -= (menu_width - btn_alloc.width)
elif self.menu_pos == POS_NW_NE:
x += btn_alloc.width
y = btn_window_xy[1] + btn_alloc.y
elif self.menu_pos == POS_CENTER_BELOW:
x -= (menu_width - btn_alloc.width) / 2
elif self.menu_pos == POS_CENTER_ABOVE:
x -= (menu_width - btn_alloc.width) / 2
y = btn_window_xy[1] - menu_height
return (x, y, True)

def popdown(self):
self.menu.popdown()
return True

def popup(self):
self.menu.popup(None, None, self._calculate_popup_pos, 0, 0)


# EVENT HANDLERS #
def _on_menu_selection_done(self, menu):
self.set_active(False)

def _on_toggled(self, togglebutton):
assert self is togglebutton

if self.get_active():
self.popup()
else:
self.popdown()

0 comments on commit 1f87d87

Please sign in to comment.