KanjiParser.search reimplemented as a generator.
[jben2_gui.git] / widget_storedsize.py
blobc834f4838c1cfcf17340ac22431c9ae0eac1e62b
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 import gtk
5 import preferences
7 class StoredSizeBase(object):
9 """Mixin class for stored sizes.
11 Note that this class sets several handlers:
13 * delete-event
14 * configure-event
15 * window-state-event
17 Do not connect these handlers in subclasses, or you'll end up with two
18 calls which is probably not what you want! Rather, please just override
19 these functions and call them from within your customized versions.
21 Alternatively, if you *insist* on adding additional connectors, then
22 use function names which won't collide with the ones here.
24 """
26 def __init__(self, param, width=-1, height=-1):
27 self.param_name = param
28 pref = None
29 if param:
30 pref = preferences.options.get(param)
31 if pref:
32 width, height = [int(v) for v in pref.split("x", 1)]
33 self.set_default_size(width, height)
34 self.connect("delete-event", self.delete_event)
35 self.connect("window-state-event", self.window_state_event)
36 self.connect("configure-event", self.configure_event)
37 self.stored_size = (width, height)
38 self.prior_size = self.stored_size
39 self.maximized = False
41 def delete_event(self, widget, event, data=None):
42 preferences.options[self.param_name] = "%dx%d" % self.stored_size
43 return False
45 # Not sure how best to handle size tracking *ONLY* when *NOT* maximized...
46 # Sadly, the window state changes AFTER the resize.
47 # Best thing I can think of is hack it: store the two most recent size
48 # values, update them on any new size events, and force a fallback on
49 # maximizing.
50 # Although, this does have a corner case if the window is the same size
51 # before the maximize...
53 def configure_event(self, widget, event, data=None):
54 if not self.maximized:
55 new_size = (event.width, event.height)
56 if new_size != self.stored_size:
57 self.prior_size = self.stored_size
58 self.stored_size = new_size
59 return False
61 def window_state_event(self, widget, event, data=None):
62 old_state = self.maximized
63 self.maximized = \
64 (event.new_window_state & gtk.gdk.WINDOW_STATE_MAXIMIZED) != 0
65 # Ugly hack for non-maximized size tracking
66 if self.maximized and not old_state:
67 self.stored_size = self.prior_size
68 return False
71 class StoredSizeWindow(gtk.Window, StoredSizeBase):
73 """Modified gtk.Window with size stored in the preferences singleton.
75 Although defaults are defined, this class really expects the first 3
76 values to be explicitly set, and then any remaining args will be properly
77 passed to the gtk.Window.__init__ function.
79 """
81 def __init__(self, param, width=-1, height=-1, *args, **kwargs):
82 gtk.Window.__init__(self, *args, **kwargs)
83 StoredSizeBase.__init__(self, param, width, height)
86 class StoredSizeDialog(gtk.Dialog, StoredSizeBase):
88 """Modified gtk.Dialog with size stored in the preferences singleton.
90 Although defaults are defined, this class really expects the first 3
91 values to be explicitly set, and then any remaining args will be properly
92 passed to the gtk.Dialog.__init__ function.
94 """
96 def __init__(self, param, width=-1, height=-1, *args, **kwargs):
97 gtk.Dialog.__init__(self, *args, **kwargs)
98 StoredSizeBase.__init__(self, param, width, height)