Use debian 2.7 only
[fpbd-bostik.git] / pyfpdb / XTables.py
blob4da8576ca37f8dab814879ff2b8c5b035188004a
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """XWindows specific methods for TableWindows Class.
4 """
5 # Copyright 2008 - 2011, Ray E. Barker
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ########################################################################
23 import L10n
24 _ = L10n.get_translation()
26 # Standard Library modules
27 import re
28 import os
29 import logging
31 # pyGTK modules
32 import gtk
34 # Other Library modules
35 import wnck
37 # FPDB modules
38 from TableWindow import Table_Window
39 import Configuration
41 # Wnck caches the results of queries. A window once retrieved remains in
42 # the list of Wnck internal objects even after the window no longer
43 # exists. To make things worse, event callbacks for signal
44 # "window-closed" can only be set for the WnckScreen, not for individual
45 # WnckWindow objects. For this reason, we need to track the known table
46 # windows.
47 WNCK_XTABLES = set()
49 # Prototype for callback is 'func(WnckScreen, WnckWindow, user_data)';
50 # We're only interested in the XID of tables we're tracking.
51 def remove_wnck_win(scr, w, *args):
52 _xid = w.get_xid()
53 if _xid in WNCK_XTABLES:
54 WNCK_XTABLES.remove(_xid)
56 # Connect the signal handler to the single global root (screen)
57 root = wnck.screen_get_default()
58 root.connect('window-closed', remove_wnck_win)
61 c = Configuration.Config()
62 log = logging.getLogger("hud")
64 class Table(Table_Window):
66 def find_table_parameters(self):
68 # This is called by __init__(). Find the poker table window of interest,
69 # given the self.search_string. Then populate self.number, self.title,
70 # self.window, and self.parent (if required).
72 self.number = None
73 self.wnck_table_w = None
75 # Flush GTK event loop before calling wnck.get_*
76 while gtk.events_pending():
77 gtk.main_iteration(False)
79 for win in root.get_windows():
80 w_title = win.get_name()
81 if re.search(self.search_string, w_title, re.I):
82 log.info('"%s" matches: "%s"' % (w_title, self.search_string))
83 title = w_title.replace('"', '')
84 if self.check_bad_words(title): continue
85 # XXX: If we could connect to 'window-closed' here, it
86 # would make things SOOO much easier... Alas, the signal
87 # is not available for individual windows.
88 self.wnck_table_w = win
89 self.number = int(win.get_xid())
90 self.title = title
91 # XID is a consistent key
92 WNCK_XTABLES.add(self.number)
93 break
95 if self.number is None:
96 log.warning(_("No match in XTables for table '%s'.") % self.search_string)
97 return None
99 # def get_window_from_xid(self, id):
100 # for outside in root.query_tree().children:
101 # if outside.id == id:
102 # return (outside, outside.query_tree().parent)
103 # for inside in outside.query_tree().children:
104 # if inside.id == id: # GNOME, Xfce
105 # return (inside, inside.query_tree().parent)
106 # for wayinside in inside.query_tree().children:
107 # if wayinside.id == id: # KDE
108 # parent = wayinside.query_tree().parent
109 # return (wayinside, parent.query_tree().parent)
110 # return (None, None)
112 #def get_geometry(self):
113 #try:
114 #my_geo = self.window.get_geometry()
115 #if self.parent is None:
116 #return {'x' : my_geo.x,
117 #'y' : my_geo.y,
118 #'width' : my_geo.width,
119 #'height' : my_geo.height
121 #else:
122 #pa_geo = self.parent.get_geometry()
123 #return {'x' : my_geo.x + pa_geo.x,
124 #'y' : my_geo.y + pa_geo.y,
125 #'width' : my_geo.width,
126 #'height' : my_geo.height
128 #except:
129 #return None
131 # This function serves a double purpose. It fetches the X geometry
132 # information from the WnckWindow, which is the normal behaviour -
133 # but it also is used to track for window lifecycle. When
134 # get_geometry() returns False [None is deal as False], the table is
135 # assumed dead and thus the HUD instance may be killed off.
136 def get_geometry(self):
137 if self.number not in WNCK_XTABLES:
138 return None
139 (_x, _y, _w, _h) = self.wnck_table_w.get_client_window_geometry()
140 return {'x' : int(_x),
141 'y' : int(_y),
142 'width' : int(_w),
143 'height' : int(_h)
146 def get_window_title(self):
147 return self.wnck_table_w.get_name()
150 def topify(self, window):
151 # The idea here is to call set_transient_for on the HUD window, with the table window
152 # as the argument. This should keep the HUD window on top of the table window, as if
153 # the hud window was a dialog belonging to the table.
155 # This is the gdkhandle for the HUD window
156 gdkwindow = gtk.gdk.window_foreign_new(window.window.xid)
157 gdkwindow.set_transient_for(self.gdkhandle)