~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/ed_art.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
# Name: ed_art.py                                                             #
 
3
# Purpose: Provides artwork for Editra based on object ids                    #
 
4
# Author: Cody Precord <cprecord@editra.org>                                  #
 
5
# Copyright: (c) 2007 Cody Precord <staff@editra.org>                         #
 
6
# License: wxWindows License                                                  #
 
7
###############################################################################
 
8
 
 
9
"""
 
10
Provides and ArtProvider class that works off of object ID's to return an
 
11
associated art resource. The provider works hand in hand with Editra's theme
 
12
framework that allows for themes to be provided as plugins that act as the
 
13
resource providers for the ArtProvider.
 
14
 
 
15
@summary: Editra's ArtProvider, supplies icons based off of object ids
 
16
 
 
17
"""
 
18
 
 
19
__author__ = "Cody Precord <cprecord@editra.org>"
 
20
__cvsid__ = "$Id: ed_art.py 66815 2011-01-29 20:46:20Z CJP $"
 
21
__revision__ = "$Revision: 66815 $"
 
22
 
 
23
#--------------------------------------------------------------------------#
 
24
# Dependancies
 
25
import wx
 
26
import ed_glob
 
27
from profiler import Profile_Get
 
28
import syntax.syntax as syntax
 
29
import ed_theme
 
30
 
 
31
#--------------------------------------------------------------------------#
 
32
 
 
33
# Map for default system/wx provided graphic resources.
 
34
# For toolbar ones fall back to ones provided by the default Tango theme
 
35
DEFAULT = {
 
36
            ed_glob.ID_ADD_BM  : wx.ART_ADD_BOOKMARK,
 
37
            ed_glob.ID_BIN_FILE : wx.ART_EXECUTABLE_FILE,
 
38
            ed_glob.ID_CDROM   : wx.ART_CDROM,
 
39
            ed_glob.ID_COPY    : wx.ART_COPY,
 
40
            ed_glob.ID_CUT     : wx.ART_CUT,
 
41
            ed_glob.ID_DELETE  : wx.ART_DELETE,
 
42
            ed_glob.ID_DEL_BM  : wx.ART_DEL_BOOKMARK,
 
43
#            ed_glob.ID_DOCPROP : wx.ART_NORMAL_FILE,        # Bad match
 
44
            ed_glob.ID_DOWN    : wx.ART_GO_DOWN,
 
45
            ed_glob.ID_EXIT    : wx.ART_QUIT,
 
46
            ed_glob.ID_FILE    : wx.ART_NORMAL_FILE,
 
47
            ed_glob.ID_FIND    : wx.ART_FIND,
 
48
            ed_glob.ID_FIND_REPLACE : wx.ART_FIND_AND_REPLACE,
 
49
            ed_glob.ID_FLOPPY  : wx.ART_FLOPPY,
 
50
            ed_glob.ID_FOLDER  : wx.ART_FOLDER,
 
51
            ed_glob.ID_HARDDISK : wx.ART_HARDDISK,
 
52
            ed_glob.ID_NEW     : wx.ART_NEW,
 
53
            ed_glob.ID_NEXT_MARK : wx.ART_GO_FORWARD,
 
54
            ed_glob.ID_OPEN    : wx.ART_FILE_OPEN,
 
55
#            ed_glob.ID_PACKAGE : wx.ART_HARDDISK,           # Poor match
 
56
            ed_glob.ID_PASTE   : wx.ART_PASTE,
 
57
#            ed_glob.ID_PREF    : wx.ART_EXECUTABLE_FILE,    # Bad match
 
58
            ed_glob.ID_PRE_MARK : wx.ART_GO_BACK,
 
59
            ed_glob.ID_PRINT   : wx.ART_PRINT,
 
60
            ed_glob.ID_REDO    : wx.ART_REDO,
 
61
            ed_glob.ID_SAVE    : wx.ART_FILE_SAVE,
 
62
            ed_glob.ID_SAVEAS  : wx.ART_FILE_SAVE_AS,
 
63
            ed_glob.ID_STOP    : wx.ART_ERROR,
 
64
#            ed_glob.ID_THEME   : wx.ART_INFORMATION,   # Bad match
 
65
            ed_glob.ID_UNDO    : wx.ART_UNDO,
 
66
            ed_glob.ID_UP      : wx.ART_GO_UP,
 
67
            ed_glob.ID_USB     : wx.ART_REMOVABLE,
 
68
#            ed_glob.ID_WEB     : wx.ART_HARDDISK       # Bad match
 
69
}
 
70
 
 
71
#--------------------------------------------------------------------------#
 
72
 
 
73
class EditraArt(wx.ArtProvider):
 
74
    """Editras Art Provider. Provides the mimetype images and loads any custom
 
75
    user defined icon sets as well. Editra theme specific icons are looked up
 
76
    by passing an objects related id as a string to this providers CreateBitmap
 
77
    function for it to talk to the theme resource provider. If the id is not
 
78
    a defined object ID it is simply ignored or passed to the the next
 
79
    ArtProvider in the chain to handle.
 
80
 
 
81
    """
 
82
    def __init__(self):
 
83
        """Initializes Editra's art provider"""
 
84
        super(EditraArt, self).__init__()
 
85
        self._library = ed_theme.BitmapProvider(wx.GetApp().GetPluginManager())
 
86
 
 
87
    def CreateBitmap(self, art_id, client, size):
 
88
        """Lookup and return an associated bitmap from the current theme if
 
89
        one exisists. If the art_id is not a theme defined id and is a wx
 
90
        defined art resource then it is passed to the next ArtProvider in the
 
91
        stack to evaluate.
 
92
 
 
93
        @return: Requested bitmap from current theme if one exists
 
94
        @rtype: wx.Bitmap
 
95
 
 
96
        """
 
97
        # All art ids we can handle can be converted to int
 
98
        try:
 
99
            art_id = int(art_id)
 
100
        except ValueError:
 
101
            return wx.NullBitmap
 
102
 
 
103
        # If using default theme let the system provide the art when possible
 
104
        # this is mostly for GTK where there is a native art provider that can
 
105
        # provide theme icons.
 
106
        if Profile_Get('ICONS', 'str').lower() == u'default' and art_id in DEFAULT:
 
107
            if client == wx.ART_MENU:
 
108
                size = (16, 16)
 
109
            elif client == wx.ART_TOOLBAR:
 
110
                size = Profile_Get('ICON_SZ', default=(24, 24))
 
111
            return wx.ArtProvider.GetBitmap(DEFAULT[art_id], client, size)
 
112
 
 
113
        # If a custom theme is set fetch the requested bitmap
 
114
        bmp = self._library.GetBitmap(art_id, client)
 
115
        if not bmp.IsNull() and bmp.IsOk():
 
116
            # Dont scale toolbar icons on wxMac as the toolbar handles it
 
117
            # internally and produces much nicer results.
 
118
            if client == wx.ART_TOOLBAR and not wx.Platform == '__WXMAC__':
 
119
                if size == wx.DefaultSize:
 
120
                    size = Profile_Get('ICON_SZ', default=(24, 24))
 
121
 
 
122
                img_sz = bmp.GetSize()
 
123
                if size[0] < img_sz[0]:
 
124
                    img = wx.ImageFromBitmap(bmp)
 
125
                    img.Rescale(size[0], size[1], wx.IMAGE_QUALITY_HIGH)
 
126
                    bmp = wx.BitmapFromImage(img)
 
127
 
 
128
            elif client == wx.ART_MENU and bmp.GetSize() != (16, 16):
 
129
                img = wx.ImageFromBitmap(bmp)
 
130
                img.Rescale(16, 16, wx.IMAGE_QUALITY_HIGH)
 
131
                bmp = wx.BitmapFromImage(img)
 
132
        elif client == wx.ART_TOOLBAR:
 
133
            # Dont fail on a toolbar icon return a warning icon when nothing is
 
134
            # found in the bitmap provider.
 
135
            bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, client, size)
 
136
        elif art_id in syntax.SYNTAX_IDS:
 
137
            # Dont fail when requesting mime type icons, fallback to the system
 
138
            # icon for a normal file in this case.
 
139
            bmp = wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE,
 
140
                                           wx.ART_MENU, (16, 16))
 
141
 
 
142
        if bmp.IsOk() and not bmp.IsNull():
 
143
            return bmp
 
144
 
 
145
        # All failed so return a Null
 
146
        return wx.NullBitmap
 
147
 
 
148
#-----------------------------------------------------------------------------#