2 # -*- coding: utf-8 -*-
4 from __future__
import absolute_import
7 from jben
import global_refs
10 class StoredSizeBase(object):
12 """Mixin class for stored sizes.
14 Note that this class sets several handlers:
20 Do not connect these handlers in subclasses, or you'll end up with two
21 calls which is probably not what you want! Rather, please just override
22 these functions and call them from within your customized versions.
24 Alternatively, if you *insist* on adding additional connectors, then
25 use function names which won't collide with the ones here.
29 def __init__(self
, param
, width
=-1, height
=-1):
30 self
.param_name
= param
31 prefs
= global_refs
.app
.prefs
34 pref
= prefs
.get(param
)
36 width
, height
= [int(v
) for v
in pref
.split("x", 1)]
37 self
.set_default_size(width
, height
)
38 self
.connect("delete-event", self
.delete_event
)
39 self
.connect("window-state-event", self
.window_state_event
)
40 self
.connect("configure-event", self
.configure_event
)
41 self
.stored_size
= (width
, height
)
42 self
.prior_size
= self
.stored_size
43 self
.maximized
= False
45 def delete_event(self
, widget
, event
, data
=None):
46 prefs
= global_refs
.app
.prefs
47 prefs
[self
.param_name
] = "%dx%d" % self
.stored_size
50 # Not sure how best to handle size tracking *ONLY* when *NOT* maximized...
51 # Sadly, the window state changes AFTER the resize.
52 # Best thing I can think of is hack it: store the two most recent size
53 # values, update them on any new size events, and force a fallback on
55 # Although, this does have a corner case if the window is the same size
56 # before the maximize...
58 def configure_event(self
, widget
, event
, data
=None):
59 if not self
.maximized
:
60 new_size
= (event
.width
, event
.height
)
61 if new_size
!= self
.stored_size
:
62 self
.prior_size
= self
.stored_size
63 self
.stored_size
= new_size
66 def window_state_event(self
, widget
, event
, data
=None):
67 old_state
= self
.maximized
69 (event
.new_window_state
& gtk
.gdk
.WINDOW_STATE_MAXIMIZED
) != 0
70 # Ugly hack for non-maximized size tracking
71 if self
.maximized
and not old_state
:
72 self
.stored_size
= self
.prior_size
76 class StoredSizeWindow(gtk
.Window
, StoredSizeBase
):
78 """Modified gtk.Window with size stored in the preferences object.
80 Although defaults are defined, this class really expects the first 3
81 values to be explicitly set, and then any remaining args will be properly
82 passed to the gtk.Window.__init__ function.
86 def __init__(self
, param
, width
=-1, height
=-1, *args
, **kwargs
):
87 gtk
.Window
.__init
__(self
, *args
, **kwargs
)
88 StoredSizeBase
.__init
__(self
, param
, width
, height
)
91 class StoredSizeDialog(gtk
.Dialog
, StoredSizeBase
):
93 """Modified gtk.Dialog with size stored in the preferences object.
95 Although defaults are defined, this class really expects the first 3
96 values to be explicitly set, and then any remaining args will be properly
97 passed to the gtk.Dialog.__init__ function.
101 def __init__(self
, param
, width
=-1, height
=-1, *args
, **kwargs
):
102 gtk
.Dialog
.__init
__(self
, *args
, **kwargs
)
103 StoredSizeBase
.__init
__(self
, param
, width
, height
)