Cleanup and fix bugs in palette positioning (again)

This commit is contained in:
Marco Pesenti Gritti 2007-08-29 20:09:19 +02:00
parent 72b3a3e2e2
commit 7fefc55133

View File

@ -102,6 +102,7 @@ class Palette(gtk.Window):
self.palette_state = self.PRIMARY self.palette_state = self.PRIMARY
self._current_alignment = None
self._old_alloc = None self._old_alloc = None
self._full_request = [0, 0] self._full_request = [0, 0]
self._cursor_x = 0 self._cursor_x = 0
@ -274,21 +275,26 @@ class Palette(gtk.Window):
self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG) self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
self._update_accept_focus() self._update_accept_focus()
def _in_screen(self, x, y): def _in_screen(self, rect):
[width, height] = self._full_request
screen_area = self._invoker.get_screen_area() screen_area = self._invoker.get_screen_area()
return rect.x >= screen_area.x and \
rect.y >= screen_area.y and \
rect.x + rect.width <= screen_area.width and \
rect.y + rect.height <= screen_area.height
return x >= screen_area.x and \ def _get_rectangle(self, alignments, full=False, inv_rect=None):
y >= screen_area.y and \ palette_halign = alignments[0]
x + width <= screen_area.width and \ palette_valign = alignments[1]
y + height <= screen_area.height invoker_halign = alignments[2]
invoker_valign = alignments[3]
def _get_position(self, palette_halign, palette_valign,
invoker_halign, invoker_valign, inv_rect=None):
if inv_rect == None: if inv_rect == None:
inv_rect = self._invoker.get_rect() inv_rect = self._invoker.get_rect()
palette_width, palette_height = self.size_request() if full:
palette_width, palette_height = self._full_request
else:
palette_width, palette_height = self.size_request()
x = inv_rect.x + inv_rect.width * invoker_halign + \ x = inv_rect.x + inv_rect.width * invoker_halign + \
palette_width * palette_halign palette_width * palette_halign
@ -296,36 +302,24 @@ class Palette(gtk.Window):
y = inv_rect.y + inv_rect.height * invoker_valign + \ y = inv_rect.y + inv_rect.height * invoker_valign + \
palette_height * palette_valign palette_height * palette_valign
return int(x), int(y) return gtk.gdk.Rectangle(int(x), int(y),
palette_width, palette_height)
def _get_around_alignments(self): def _get_around_alignments(self):
return ((0.0, 0.0, 0.0, 1.0), return [(0.0, 0.0, 0.0, 1.0),
(-1.0, 0.0, 1.0, 1.0), (-1.0, 0.0, 1.0, 1.0),
(0.0, 0.0, 1.0, 0.0), (0.0, 0.0, 1.0, 0.0),
(0.0, -1.0, 1.0, 1.0), (0.0, -1.0, 1.0, 1.0),
(0.0, -1.0, 0.0, 0.0), (0.0, -1.0, 0.0, 0.0),
(-1.0, -1.0, 1.0, 0.0), (-1.0, -1.0, 1.0, 0.0),
(-1.0, 0.0, 0.0, 0.0), (-1.0, 0.0, 0.0, 0.0),
(-1.0, -1.0, 0.0, 1.0)) (-1.0, -1.0, 0.0, 1.0)]
def _get_around_position(self): def _get_at_cursor_alignments(self, inv_rect=None):
for align in self._get_around_alignments(): return [(0.0, 0.0, 1.0, 1.0),
x, y = self._get_position(*align) (0.0, -1.0, 1.0, 0.0),
if self._in_screen(x, y): (-1.0, -1.0, 0.0, 0.0),
return x, y (-1.0, 0.0, 0.0, 1.0)]
return x, y
def _get_at_cursor_position(self, inv_rect=None):
x, y = self._get_position(0.0, 0.0, 1.0, 1.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(0.0, -1.0, 1.0, 0.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(-1.0, -1.0, 0.0, 0.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(-1.0, 0.0, 0.0, 1.0, inv_rect)
return x, y
def _update_full_request(self): def _update_full_request(self):
state = self.palette_state state = self.palette_state
@ -353,17 +347,24 @@ class Palette(gtk.Window):
else: else:
position = self._position position = self._position
inv_rect = None
if position == self.AT_CURSOR: if position == self.AT_CURSOR:
dist = style.PALETTE_CURSOR_DISTANCE dist = style.PALETTE_CURSOR_DISTANCE
rect = gtk.gdk.Rectangle(self._cursor_x - dist, inv_rect = gtk.gdk.Rectangle(self._cursor_x - dist,
self._cursor_y - dist, self._cursor_y - dist,
dist * 2, dist * 2) dist * 2, dist * 2)
x, y = self._get_at_cursor_position(rect) alignments = self._get_around_alignments()[:]
elif position == self.AROUND: if self._current_alignment is not None:
x, y = self._get_around_position() alignments.remove(self._current_alignment)
alignments.insert(0, self._current_alignment)
self.move(x, y) for align in alignments:
rect = self._get_rectangle(align, inv_rect=inv_rect)
if self._in_screen(rect):
break
self.move(rect.x, rect.y)
def _show(self): def _show(self):
if self._up: if self._up:
@ -375,6 +376,12 @@ class Palette(gtk.Window):
self._palette_popup_sid = _palette_observer.connect( self._palette_popup_sid = _palette_observer.connect(
'popup', self._palette_observer_popup_cb) 'popup', self._palette_observer_popup_cb)
for align in self._get_around_alignments():
rect = self._get_rectangle(align, full=True)
if self._in_screen(rect):
self._current_alignment = align
break
self._update_position() self._update_position()
self.menu.set_active(True) self.menu.set_active(True)
self.show() self.show()