Package dtk :: Package ui :: Module titlebar

Source Code for Module dtk.ui.titlebar

  1  #! /usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # Copyright (C) 2011 ~ 2012 Deepin, Inc. 
  5  #               2011 ~ 2012 Wang Yong 
  6  #  
  7  # Author:     Wang Yong <lazycat.manatee@gmail.com> 
  8  # Maintainer: Wang Yong <lazycat.manatee@gmail.com> 
  9  #  
 10  # This program is free software: you can redistribute it and/or modify 
 11  # it under the terms of the GNU General Public License as published by 
 12  # the Free Software Foundation, either version 3 of the License, or 
 13  # any later version. 
 14  #  
 15  # This program is distributed in the hope that it will be useful, 
 16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18  # GNU General Public License for more details. 
 19  #  
 20  # You should have received a copy of the GNU General Public License 
 21  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 22   
 23  from box import EventBox, ImageBox 
 24  from button import ThemeButton, MenuButton, MinButton, MaxButton, CloseButton 
 25  from draw import draw_line 
 26  from label import Label 
 27  from locales import _ 
 28  import tooltip as Tooltip 
 29  from utils import window_is_max 
 30  import gobject 
 31  import gtk 
 32  import pango 
 33   
34 -class Titlebar(EventBox):
35 """ 36 Titlebar defines every thing of a title bar of a application based on deepin ui. 37 38 @undocumented: expose_titlebar_separator 39 """
40 - def __init__(self, 41 button_mask=["theme", "menu", "max", "min", "close"], 42 icon_dpixbuf=None, 43 app_name=None, 44 title=None, 45 add_separator=False, 46 height=26, 47 show_title=True, 48 ):
49 """ 50 Initialize the title bar. 51 52 @param button_mask: A string list. Each item of it indicates that there is a corresponding button on the title bar. By default, it's ["theme", "menu", "max", "min", "close"], which means theme button, menu button, max button, min button and close button, respectively. 53 @param icon_dpixbuf: A pixbuf of type dtk.ui.theme.DynamicPixbuf. It will be displayed at the top left of the window. By default, it's None. 54 @param app_name: Application name string. It will be displayed just next to the icon_dpixbuf. By default, it's None. 55 @param title: Title string of the application. It will be displayed on the center of the title bar. By default, it's None. 56 @param add_separator: If True, add a separation line between the title bar and the body of the window. By default, it's False. 57 @param height: The hight of the title bar. By default, it's 26 pixels. 58 @param show_title: If False, the title bar will not be displayed. By default, it's True. 59 """ 60 # Init. 61 EventBox.__init__(self) 62 self.set_size_request(-1, height) 63 self.v_layout_box = gtk.VBox() 64 self.h_layout_box = gtk.HBox() 65 self.add(self.v_layout_box) 66 self.v_layout_box.pack_start(self.h_layout_box, True, True) 67 68 # Init separator. 69 if add_separator: 70 self.separator = gtk.HBox() 71 self.separator.set_size_request(-1, 1) 72 self.separator.connect("expose-event", self.expose_titlebar_separator) 73 self.v_layout_box.pack_start(self.separator, True, True) 74 75 # Add drag event box. 76 self.drag_box = EventBox() 77 self.h_layout_box.pack_start(self.drag_box, True, True) 78 79 # Init left box to contain icon and title. 80 self.left_box = gtk.HBox() 81 self.drag_box.add(self.left_box) 82 83 if show_title: 84 # Add icon. 85 if icon_dpixbuf != None: 86 self.icon_image_box = ImageBox(icon_dpixbuf) 87 self.icon_align = gtk.Alignment() 88 self.icon_align.set(0.5, 0.5, 0.0, 0.0) 89 self.icon_align.set_padding(5, 5, 5, 0) 90 self.icon_align.add(self.icon_image_box) 91 self.left_box.pack_start(self.icon_align, False, False) 92 93 # Add app name. 94 if app_name != None: 95 self.app_name_box = Label(app_name, enable_gaussian=True) 96 self.app_name_align = gtk.Alignment() 97 self.app_name_align.set(0.5, 0.5, 0.0, 0.0) 98 self.app_name_align.set_padding(2, 0, 5, 0) 99 self.app_name_align.add(self.app_name_box) 100 self.left_box.pack_start(self.app_name_align, False, False) 101 102 # Add title. 103 if title != None: 104 self.title_box = Label(title, enable_gaussian=True, text_x_align=pango.ALIGN_CENTER) 105 self.title_align = gtk.Alignment() 106 self.title_align.set(0.5, 0.5, 0.0, 0.0) 107 self.title_align.set_padding(2, 0, 30, 30) 108 self.title_align.add(self.title_box) 109 self.left_box.pack_start(self.title_align, True, True) 110 111 # Add button box. 112 self.button_box = gtk.HBox() 113 self.button_align = gtk.Alignment() 114 self.button_align.set(1.0, 0.0, 0.0, 0.0) 115 self.button_align.set_padding(0, 0, 0, 0) 116 self.button_align.add(self.button_box) 117 self.h_layout_box.pack_start(self.button_align, False, False) 118 119 # Add theme button. 120 if "theme" in button_mask: 121 self.theme_button = ThemeButton() 122 self.button_box.pack_start(self.theme_button, False, False, 1) 123 Tooltip.text(self.theme_button, _("Change skin")).show_delay(self.theme_button, 2000) 124 125 # Add menu button. 126 if "menu" in button_mask: 127 self.menu_button = MenuButton() 128 self.button_box.pack_start(self.menu_button, False, False, 1) 129 Tooltip.text(self.menu_button, _("Main menu")).show_delay(self.menu_button, 2000) 130 131 # Add min button. 132 if "min" in button_mask: 133 self.min_button = MinButton() 134 self.button_box.pack_start(self.min_button, False, False, 1) 135 Tooltip.text(self.min_button, _("Minimum")).show_delay(self.min_button, 2000) 136 137 # Add max button. 138 if "max" in button_mask: 139 self.max_button = MaxButton() 140 self.button_box.pack_start(self.max_button, False, False, 1) 141 Tooltip.text(self.max_button, _("Maximize")).show_delay(self.max_button, 2000) 142 143 # Add close button. 144 if "close" in button_mask: 145 self.close_button = CloseButton() 146 self.button_box.pack_start(self.close_button, False, False) 147 Tooltip.text(self.close_button, _("Close")).show_delay(self.close_button, 2000) 148 149 # Show. 150 self.show_all()
151
152 - def expose_titlebar_separator(self, widget, event):
153 """ 154 Expose the separation line between the titlebar and the body of the window. 155 156 @param widget: A widget of type Gtk.Widget. 157 @param event: Not used. 158 @return: Always return True. 159 """ 160 # Init. 161 cr = widget.window.cairo_create() 162 rect = widget.allocation 163 164 # Draw separator. 165 cr.set_source_rgba(1, 1, 1, 0.5) 166 draw_line(cr, rect.x + 1, rect.y + 2, rect.x + rect.width - 1, rect.y + 1) 167 168 return True
169
170 - def change_title(self, title):
171 """ 172 Change the title of the application, which is diplayed on the center of the title bar. 173 174 @param title: New title string that want to set. 175 """ 176 self.title_box.set_text(title)
177 178 gobject.type_register(Titlebar) 179 180 if __name__ == "__main__": 181
182 - def max_signal(w):
183 if window_is_max(w): 184 win.unmaximize() 185 print "min" 186 else: 187 win.maximize() 188 print "max" 189 190 win = gtk.Window(gtk.WINDOW_TOPLEVEL) 191 win.connect("destroy", gtk.main_quit) 192 tit = Titlebar() 193 tit.max_button.connect("clicked", max_signal) 194 win.add(tit.box) 195 win.show_all() 196 197 gtk.main() 198