File: windowdimensionstracker.py

package info (click to toggle)
taskcoach 1.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,496 kB
  • ctags: 17,810
  • sloc: python: 72,170; makefile: 254; ansic: 120; xml: 29; sh: 16
file content (143 lines) | stat: -rw-r--r-- 6,459 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''
Task Coach - Your friendly task manager
Copyright (C) 2004-2014 Task Coach developers <developers@taskcoach.org>

Task Coach 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 3 of the License, or
(at your option) any later version.

Task Coach 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 wx
from taskcoachlib import operating_system


class _Tracker(object):
    ''' Utility methods for setting and getting values from/to the 
        settings. '''
    
    def __init__(self, settings, section):
        super(_Tracker, self).__init__()
        self.__settings = settings
        self.__section = section
               
    def set_setting(self, setting, value):
        ''' Store the value for the setting in the settings. '''
        self.__settings.setvalue(self.__section, setting, value)
        
    def get_setting(self, setting):
        ''' Get the value for the setting from the settings and return it. ''' 
        return self.__settings.getvalue(self.__section, setting)
        

class WindowSizeAndPositionTracker(_Tracker):
    ''' Track the size and position of a window in the settings. '''

    def __init__(self, window, settings, section):
        super(WindowSizeAndPositionTracker, self).__init__(settings, section)
        self._window = window
        self.__set_dimensions()
        self._window.Bind(wx.EVT_SIZE, self.on_change_size)
        self._window.Bind(wx.EVT_MOVE, self.on_change_position)
        self._window.Bind(wx.EVT_MAXIMIZE, self.on_maximize)

    def on_change_size(self, event):
        ''' Handle a size event by saving the new size of the window in the
            settings. '''
        # Ignore the EVT_SIZE when the window is maximized or iconized. 
        # Note how this depends on the EVT_MAXIMIZE being sent before the 
        # EVT_SIZE.
        maximized = self._window.IsMaximized()
        if not maximized and not self._window.IsIconized():
            self.set_setting('size', self._window.GetClientSize() \
                            if operating_system.isMac() else event.GetSize())
        # Jerome, 2008/07/12: On my system (KDE 3.5.7), EVT_MAXIMIZE
        # is not triggered, so set 'maximized' to True here as well as in 
        # onMaximize:
        self.set_setting('maximized', maximized)
        event.Skip()

    def on_change_position(self, event):
        ''' Handle a move event by saving the new position of the window in
            the settings. '''
        if not self._window.IsMaximized():
            self.set_setting('maximized', False)
            if not self._window.IsIconized():
                # Only save position when the window is not maximized 
                # *and* not minimized
                self.set_setting('position', event.GetPosition())
        event.Skip()

    def on_maximize(self, event):
        ''' Handle a maximize event by saving the window maximization state in 
            the settings. '''
        self.set_setting('maximized', True)
        event.Skip()

    def __set_dimensions(self):
        ''' Set the window position and size based on the settings. '''
        x, y = self.get_setting('position')  # pylint: disable=C0103
        width, height = self.get_setting('size')
        if operating_system.isMac():
            # Under MacOS 10.5 and 10.4, when setting the size, the actual 
            # window height is increased by 40 pixels. Dunno why, but it's 
            # highly annoying. This doesn't hold for dialogs though. Sigh.
            if not isinstance(self._window, wx.Dialog):
                height += 18
        self._window.SetDimensions(x, y, width, height)
        if operating_system.isMac():
            self._window.SetClientSize((width, height))
        if self.get_setting('maximized'):
            self._window.Maximize()
        # Check that the window is on a valid display and move if necessary:
        if wx.Display.GetFromWindow(self._window) == wx.NOT_FOUND:
            # Not (0, 0) because on OSX this hides the window bar...
            self._window.SetDimensions(50, 50, width, height)
            if operating_system.isMac():
                self._window.SetClientSize((width, height))

                
class WindowDimensionsTracker(WindowSizeAndPositionTracker):
    ''' Track the dimensions of a window in the settings. '''
    
    def __init__(self, window, settings):
        super(WindowDimensionsTracker, self).__init__(window, settings, 
                                                      'window')
        self.__settings = settings
        if self.__start_iconized():
            if operating_system.isMac() or operating_system.isGTK():
                # Need to show the window on Mac OS X first, otherwise it   
                # won't be properly minimized. On wxGTK we need to show the
                # window first, otherwise clicking the task bar icon won't
                # show it.
                self._window.Show()
            self._window.Iconize(True)
            if not operating_system.isMac() and \
                self.get_setting('hidewheniconized'):
                # Seems like hiding the window after it's been
                # iconized actually closes it on Mac OS...
                wx.CallAfter(self._window.Hide)                

    def __start_iconized(self):
        ''' Return whether the window should be opened iconized. '''
        start_iconized = self.__settings.get('window', 'starticonized')
        if start_iconized == 'Always':
            return True
        if start_iconized == 'Never':
            return False
        return self.get_setting('iconized')
     
    def save_position(self):
        ''' Save the position of the window in the settings. '''
        iconized = self._window.IsIconized()
        self.set_setting('iconized', iconized)
        if not iconized:
            self.set_setting('position', self._window.GetPosition())