Removed old jben.py initialization. Now controlled via jben.app.Application object.
[jben2_gui.git] / jben / jben / interface / gtk / widget / storedsize.py
blob82643bea5ba820c3ad817b8878baf8a55accc101
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 from __future__ import absolute_import
6 import gtk
7 from jben import global_refs
10 class StoredSizeBase(object):
12 """Mixin class for stored sizes.
14 Note that this class sets several handlers:
16 * delete-event
17 * configure-event
18 * window-state-event
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.
27 """
29 def __init__(self, param, width=-1, height=-1):
30 self.param_name = param
31 prefs = global_refs.app.prefs
32 pref = None
33 if param:
34 pref = prefs.get(param)
35 if pref:
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
48 return False
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
54 # maximizing.
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
64 return False
66 def window_state_event(self, widget, event, data=None):
67 old_state = self.maximized
68 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
73 return False
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.
84 """
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.
99 """
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)