~slimeypete/pyroom/linespacing

20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
1
from status_label import FadeLabel
27.3.3 by Adam Rooke
included all theme files.
2
27.1.28 by Peter Frost
Added a new exception class and firmed-up error handling. Added GUI element for error reporting.
3
from pyroom_error import PyroomError
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
4
import gtk
5
import pango
6
import gtksourceview
27.1.1 by Lancelotz
Preferences-UI development has started.
7
import gtk.glade
27.3.1 by Adam Rooke
First commit.
8
import ConfigParser
27.1.1 by Lancelotz
Preferences-UI development has started.
9
27.1.3 by Lancelotz
Styles now change when the user changes them in combobox, and some other improvements...
10
class GUI():
27.1.28 by Peter Frost
Added a new exception class and firmed-up error handling. Added GUI element for error reporting.
11
    def __init__(self, style, verbose):
12
        self.verbose = verbose
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
13
        self.status = FadeLabel()
27.1.28 by Peter Frost
Added a new exception class and firmed-up error handling. Added GUI element for error reporting.
14
        self.error = FadeLabel()
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
15
        self.style = style
16
17
        # Main window
18
19
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
20
        self.window.set_name('PyRoom')
21
        self.window.set_title("PyRoom")
22
        self.window.connect('delete_event', self.delete_event)
23
        self.window.connect('destroy', self.destroy)
24
25
        self.textbox = gtksourceview.SourceView()
26
        self.textbox.connect('scroll-event', self.scroll_event)
27
28
        self.textbox.set_wrap_mode(gtk.WRAP_WORD)
29
30
        self.fixed = gtk.Fixed()
31
        self.vbox = gtk.VBox()
32
        self.window.add(self.fixed)
33
        self.fixed.put(self.vbox, 0, 0)
34
35
        self.boxout = gtk.EventBox()
36
        self.boxout.set_border_width(1)
37
        self.boxin = gtk.EventBox()
38
        self.boxin.set_border_width(1)
39
        self.vbox.pack_start(self.boxout, True, True, 6)
40
        self.boxout.add(self.boxin)
41
42
        self.scrolled = gtk.ScrolledWindow()
43
        self.boxin.add(self.scrolled)
44
        self.scrolled.add(self.textbox)
45
        self.scrolled.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
46
        self.scrolled.show()
47
        self.scrolled.set_property('resize-mode', gtk.RESIZE_PARENT)
48
        self.textbox.set_property('resize-mode', gtk.RESIZE_PARENT)
49
        self.vbox.set_property('resize-mode', gtk.RESIZE_PARENT)
50
        self.vbox.show_all()
51
27.1.28 by Peter Frost
Added a new exception class and firmed-up error handling. Added GUI element for error reporting.
52
        # Error
53
        self.hbox2 = gtk.HBox()
54
        self.hbox2.set_spacing(12)
55
        self.hbox2.pack_end(self.error, True, True, 0)
56
        self.vbox.pack_end(self.hbox2, False, False, 0)
57
        self.error.set_alignment(0.0, 0.5)
58
        self.error.set_justify(gtk.JUSTIFY_LEFT)
59
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
60
        # Status
61
        self.hbox = gtk.HBox()
62
        self.hbox.set_spacing(12)
63
        self.hbox.pack_end(self.status, True, True, 0)
64
        self.vbox.pack_end(self.hbox, False, False, 0)
65
        self.status.set_alignment(0.0, 0.5)
66
        self.status.set_justify(gtk.JUSTIFY_LEFT)
27.1.28 by Peter Frost
Added a new exception class and firmed-up error handling. Added GUI element for error reporting.
67
68
27.3.1 by Adam Rooke
First commit.
69
        self.config = ConfigParser.ConfigParser()
70
        self.conf = ConfigParser.ConfigParser()
71
        self.conf.read("example.conf")
27.1.18 by Adam Rooke
Merging
72
        if self.style:
73
            theme = "./themes/" + style + ".theme"
74
        else:
75
            theme = "./themes/" + self.conf.get("visual","theme") + ".theme"
27.3.1 by Adam Rooke
First commit.
76
        self.config.read(theme)
27.3.2 by Adam Rooke
-removed a few debugging lines of code
77
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
78
    def quit(self):
79
        """ quit pyroom """
80
        gtk.main_quit()
81
82
    def delete_event(self, widget, event, data=None):
83
        """ Quit """
84
        return False
85
86
    def destroy(self, widget, data=None):
87
        """ Quit """
88
        gtk.main_quit()
25 by Bruno Bord
I18n on some strings added by Adam.
89
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
90
    def scroll_event(self, widget, event):
25 by Bruno Bord
I18n on some strings added by Adam.
91
        """ Scroll event dispatcher """
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
92
93
        if event.direction == gtk.gdk.SCROLL_UP:
94
            self.scroll_up()
95
        elif event.direction == gtk.gdk.SCROLL_DOWN:
96
            self.scroll_down()
25 by Bruno Bord
I18n on some strings added by Adam.
97
20.1.1 by Bruno Bord
Massive merge from Adam Rooke's branch (code-reorganise).
98
    def scroll_down(self):
99
        """ Scroll window down """
100
101
        adj = self.scrolled.get_vadjustment()
102
        if adj.upper > adj.page_size:
103
            adj.value = min(adj.upper - adj.page_size, adj.value
104
                             + adj.step_increment)
105
106
    def scroll_up(self):
107
        """ Scroll window up """
108
109
        adj = self.scrolled.get_vadjustment()
110
        if adj.value > adj.step_increment:
111
            adj.value -= adj.step_increment
112
        else:
113
            adj.value = 0
114
27.1.18 by Adam Rooke
Merging
115
    def apply_style(self, style=None, mode='normal'):
31 by Peter Frost
Added some better error handling so that invalid themes don't crash the whole application
116
        try:
117
            if mode == 'normal':
118
                self.window.modify_bg(gtk.STATE_NORMAL,
119
                                      gtk.gdk.color_parse(self.config.get("theme","background")))
120
                self.textbox.modify_bg(gtk.STATE_NORMAL,
121
                                       gtk.gdk.color_parse(self.config.get("theme","background")))
122
                self.textbox.modify_base(gtk.STATE_NORMAL,
123
                                         gtk.gdk.color_parse(self.config.get("theme","background")))
124
                self.textbox.modify_text(gtk.STATE_NORMAL,
125
                                         gtk.gdk.color_parse(self.config.get("theme","foreground")))
126
                self.textbox.modify_fg(gtk.STATE_NORMAL,
127
                                       gtk.gdk.color_parse(self.config.get("theme","foreground")))
128
                self.textbox.set_pixels_below_lines(int(self.config.get("theme", "linespacing")))
129
	        self.textbox.set_pixels_above_lines(int(self.config.get("theme", "linespacing")))
130
	        self.textbox.set_pixels_inside_wrap(int(self.config.get("theme", "linespacing")))
131
                self.status.active_color = self.config.get("theme","foreground")
132
                self.status.inactive_color = self.config.get("theme","background")
133
                self.error.active_color = self.config.get("theme","foreground")
134
                self.error.inactive_color = self.config.get("theme","background")
135
                self.boxout.modify_bg(gtk.STATE_NORMAL,
136
                                      gtk.gdk.color_parse(self.config.get("theme","border")))
137
                font_and_size = '%s %d' % (self.config.get("theme","font"),
138
                                           float(self.config.get("theme","fontsize")))
139
                self.textbox.modify_font(pango.FontDescription(font_and_size))
140
141
                gtk.rc_parse_string("""
142
	            style "pyroom-colored-cursor" {
143
                GtkTextView::cursor-color = '"""
144
                                     + self.config.get("theme","foreground")
145
                                     + """'
146
                }
147
                class "GtkWidget" style "pyroom-colored-cursor"
148
	            """)
149
                (w, h) = (gtk.gdk.screen_width(), gtk.gdk.screen_height())
150
                width = int(float(self.config.get("theme","width")) * w)
151
                height =int(float(self.config.get("theme","height")) * h)
152
                self.vbox.set_size_request(width, height)
153
                self.fixed.move(self.vbox, int(((1 - float(self.config.get("theme","width"))) * w)/ 2),
154
                    int(((1-float(self.config.get("theme","height"))) * h) / 2))
155
                self.textbox.set_border_width(int(self.config.get("theme",'padding')))
156
            elif mode == 'custom':
157
                self.style = style
158
                self.window.modify_bg(gtk.STATE_NORMAL,
159
                                      gtk.gdk.color_parse(self.style['background']))
160
                self.textbox.modify_bg(gtk.STATE_NORMAL,
161
                                       gtk.gdk.color_parse(self.style['background']))
162
                self.textbox.modify_base(gtk.STATE_NORMAL,
163
                                         gtk.gdk.color_parse(self.style['background']))
164
                self.textbox.modify_text(gtk.STATE_NORMAL,
165
                                         gtk.gdk.color_parse(self.style['foreground']))
166
                self.textbox.modify_fg(gtk.STATE_NORMAL,
167
                                       gtk.gdk.color_parse(self.style['foreground']))
168
                self.status.active_color = self.style['foreground']
169
                self.status.inactive_color = self.style['background']
170
                self.boxout.modify_bg(gtk.STATE_NORMAL,
171
                                      gtk.gdk.color_parse(self.style['border']))
172
                font_and_size = '%s %d' % (self.style['font'],
173
                                           float(self.style['fontsize']))
174
                self.textbox.modify_font(pango.FontDescription(font_and_size))
175
	        self.textbox.set_pixels_below_lines(int(self.style["linespacing"]))
176
	        self.textbox.set_pixels_above_lines(int(self.style["linespacing"]))
177
	        self.textbox.set_pixels_inside_wrap(int(self.style["linespacing"]))
178
179
                gtk.rc_parse_string("""
180
	            style "pyroom-colored-cursor" {
181
                GtkTextView::cursor-color = '"""
182
                                     + self.style['foreground']
183
                                     + """'
184
                }
185
                class "GtkWidget" style "pyroom-colored-cursor"
186
	            """)
187
                (w, h) = (gtk.gdk.screen_width(), gtk.gdk.screen_height())
188
                width = int(self.style['size'][0] * w)
189
                height = int(self.style['size'][1] * h)
190
                self.vbox.set_size_request(width, height)
191
                self.fixed.move(self.vbox, int(((1 - self.style['size'][0]) * w)/ 2),
27.1.18 by Adam Rooke
Merging
192
                int(((1 - self.style['size'][1]) * h) / 2))
31 by Peter Frost
Added some better error handling so that invalid themes don't crash the whole application
193
                self.textbox.set_border_width(self.style['padding'])
194
195
        except:
196
            raise PyroomError(_('Unable to apply style'))
197