PreviewWindow.py :  » GUI » IceMe » IceMe-1.1.1 » src » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » IceMe 
IceMe » IceMe 1.1.1 » src » PreviewWindow.py
import os
import string
import stat
from popen2 import popen2
from gtk import *
from constants import *
from MessageBox import *
import gdkpixbuf

class PicMenuItem(GtkMenuItem):

    def __init__(self, text, pix, mask):
        GtkMenuItem.__init__(self)
        hbox = GtkHBox(FALSE, 3)
        if (pix, mask) != (None, None):
            icon = GtkPixmap(pix, mask)
            icon.show()
            hbox.pack_start(icon, FALSE, FALSE)
        if text:
            label = GtkLabel(text)
            label.set_justify(JUSTIFY_LEFT)
            label.show()
            hbox.pack_start(label, FALSE, FALSE)
        hbox.show()
        self.add(hbox)



class PreviewWindow(GtkWindow):

    def __init__(self, tree, empty_pix, empty_mask):
        GtkWindow.__init__(self, WINDOW_DIALOG, "IceMe: preview menu")
        self.set_default_size(400, -1)
        self.connect("delete_event", self.close)
        self.tree = tree
        self.empty_pix = empty_pix
        self.empty_mask = empty_mask
        menu = GtkMenuBar()
        menu.set_shadow_type(SHADOW_OUT)
        nodes = tree.base_nodes()
        self.createMenu(menu, nodes[0])
        toolbar_menu = nodes[2]
        for n in toolbar_menu.children:
            self.createMenu(menu, n, 1)
        menu.show()
        self.add(menu)

    def close(self, x=None, y=None):
        self.hide()
        self.destroy()

    def createMenu(self, menu, node, toolbar=0):
        type = self.tree.getNodeType(node)
        if type == MENUTREE_SEPARATOR:
            mi = GtkMenuItem()
            menu.append(mi)
            mi.show()
        elif type == MENUTREE_SUBMENU:
            name = self.tree.getNodeName(node)
            pix, mask = self.tree.getNodeIcon(node)

            if toolbar:
                if pix == self.empty_pix:
                    pix, mask = None, None
                else:
                    name = None

            mi = PicMenuItem(name, pix, mask)
            submenu = GtkMenu()
            mi.set_submenu(submenu)
            for n in node.children:
                self.createMenu(submenu, n)
            submenu.show()
            menu.append(mi)
            mi.show()
        else:
            command = self.tree.getNodeCommand(node)
            if self.command_ok(command):
                name = self.tree.getNodeName(node)
                pix, mask = self.tree.getNodeIcon(node)
                if toolbar:
                    if pix == self.empty_pix:
                        pix, mask = None, None
                    else:
                        name = None

                mi = PicMenuItem(name, pix, mask)
                mi.set_data("command", command)
                mi.set_data("type", type)
                mi.connect("activate", self.on_menu_activate)
                menu.append(mi)
                mi.show()

    def command_ok(self, command_string):
        "Check if command_string starts with an existing executable."
        if not command_string:
            return 0
        cmd = string.split(command_string, " ")[0]
        # the following code mimics IceWM's executable detection closely:
        if cmd[0] == os.sep:
            # absolute filename:
            # the file must have read and execute access and be regular:
            return os.access(cmd, R_OK|X_OK) and os.path.isfile(cmd)
        # search in $PATH:
        for path in PATH:
            fullcmd = os.path.join(path, cmd)
            if os.access(fullcmd, R_OK|X_OK) and os.path.isfile(fullcmd):
                return 1
        return 0

    def on_menu_activate(self, menuitem):
        command = menuitem.get_data("command")
        type = menuitem.get_data("type")
        if type == MENUTREE_RESTART:
            message("Info",
                    "This menu entry quits IceWM\n"
                    "and starts %s\n"
                    "as the new window manager.\n"
                    "\n"
                    "IceMe cannot perform this\n"
                    "operation." % command)
        else:
            print "IceMe: executing", command
            popen2(command)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.