]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQUI_x11.cc
compile some more
[duncan/yast2-qt4.git] / src / YQUI_x11.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:         YUIQt_x11.cc
14
15   Author:       Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define QT3_SUPPORT
22 #include <qevent.h>
23 #include <qcursor.h>
24 #include <q3widgetstack.h>
25 #include <q3vbox.h>
26 #include <qwidget.h>
27 #include <qtextcodec.h>
28 #include <qregexp.h>
29 #include <qlocale.h>
30 #include <qmessagebox.h>
31 #include <QColorGroup>
32 #include <QtGui/qdesktopwidget.h>
33
34
35 #define y2log_component "qt-ui"
36 #include <ycp/y2log.h>
37
38 #include "YEvent.h"
39 #include "YQDialog.h"
40 #include "YQi18n.h"
41 #include "QY2Settings.h"
42 #include "YQUI.h"
43
44 // Include low-level X headers AFTER Qt headers:
45 // X.h pollutes the global namespace (!!!) with pretty useless #defines
46 // like "Above", "Below" etc. that clash with some Qt headers.
47 #include <X11/Xlib.h>
48
49
50 int YQUI::getDisplayWidth()
51 {
52     return qApp->desktop()->width();
53 }
54
55
56 int YQUI::getDisplayHeight()
57 {
58     return qApp->desktop()->height();
59 }
60
61
62 int YQUI::getDisplayDepth()
63 {
64     return qApp->desktop()->depth();
65 }
66
67
68 long YQUI::getDisplayColors()
69 {
70     return 1L << qApp->desktop()->depth();
71 }
72
73
74 int YQUI::getDefaultWidth()
75 {
76     return _default_size.width();
77 }
78
79
80 int YQUI::getDefaultHeight()
81 {
82     return _default_size.height();
83 }
84
85
86 int YQUI::defaultSize(YUIDimension dim) const
87 {
88     if ( haveWM() )
89         return dim == YD_HORIZ ? _default_size.width() : _default_size.height();
90     else
91         return dim == YD_HORIZ ? qApp->desktop()->width() : qApp->desktop()->height();
92 }
93
94
95 QWidget* YQUI::mainWidget()
96 {
97     return _main_win;
98 };
99
100
101 void YQUI::beep()
102 {
103     qApp->beep();
104 }
105
106
107 void
108 YQUI::busyCursor( void )
109 {
110     qApp->setOverrideCursor( Qt::BusyCursor );
111 }
112
113
114 void
115 YQUI::normalCursor( void )
116 {
117     if ( _busy_cursor_timer->isActive() )
118         _busy_cursor_timer->stop();
119
120     while ( qApp->overrideCursor() )
121         qApp->restoreOverrideCursor();
122 }
123
124
125 void YQUI::toggleVisionImpairedPalette()
126 {
127     if ( _usingVisionImpairedPalette )
128     {
129         qApp->setPalette( normalPalette(),
130                           true );  // informWidgets
131
132         _usingVisionImpairedPalette = false;
133     }
134     else
135     {
136         qApp->setPalette( visionImpairedPalette(),
137                           true );  // informWidgets
138
139         _usingVisionImpairedPalette = true;
140     }
141 }
142
143
144 QPalette YQUI::visionImpairedPalette()
145 {
146     const QColor dark  ( 0x20, 0x20, 0x20 );
147
148     QColorGroup activeCg;       // for the active window (the one with the keyboard focus)
149
150     activeCg.setColor( QColorGroup::Background,         Qt::black       );
151     activeCg.setColor( QColorGroup::Foreground,         Qt::cyan        );
152     activeCg.setColor( QColorGroup::Text,               Qt::cyan        );
153     activeCg.setColor( QColorGroup::Base,               dark            );
154     activeCg.setColor( QColorGroup::Button,             dark            );
155     activeCg.setColor( QColorGroup::ButtonText,         Qt::green       );
156     activeCg.setColor( QColorGroup::Highlight,          Qt::yellow      );
157     activeCg.setColor( QColorGroup::HighlightedText,    Qt::black       );
158
159     QColorGroup inactiveCg;     // for other windows (those that don't have the keyboard focus)
160
161     inactiveCg.setColor( QColorGroup::Background,       Qt::black       );
162     inactiveCg.setColor( QColorGroup::Foreground,       Qt::cyan        );
163     inactiveCg.setColor( QColorGroup::Text,             Qt::cyan        );
164     inactiveCg.setColor( QColorGroup::Base,             dark            );
165     inactiveCg.setColor( QColorGroup::Button,           dark            );
166     inactiveCg.setColor( QColorGroup::ButtonText,       Qt::green       );
167
168     QColorGroup disabledCg;     // for disabled widgets
169
170     disabledCg.setColor( QColorGroup::Background,       Qt::black       );
171     disabledCg.setColor( QColorGroup::Foreground,       Qt::gray        );
172     disabledCg.setColor( QColorGroup::Text,             Qt::gray        );
173     disabledCg.setColor( QColorGroup::Base,             dark            );
174     disabledCg.setColor( QColorGroup::Button,           dark            );
175     disabledCg.setColor( QColorGroup::ButtonText,       Qt::gray        );
176
177     QPalette pal( activeCg, disabledCg, inactiveCg );
178
179     return pal;
180 }
181
182
183 bool YQUI::close()
184 {
185     sendEvent( new YCancelEvent() );
186     return true;
187 }
188
189
190 bool YQUI::eventFilter( QObject * obj, QEvent * ev )
191 {
192     if ( ev->type() == QEvent::Close )
193     {
194         // Handle WM_CLOSE - but only if it comes from a dialog that is managed by the UI,
195         // not from an independent Qt dialog (such as the package selector popups)
196
197         QWidget * objDialog = 0;
198
199         if ( obj && obj->isWidgetType() )
200         {
201             objDialog = (QWidget *) obj;
202             objDialog = objDialog->topLevelWidget();
203         }
204
205         if ( objDialog &&
206              ( objDialog == mainWidget() ||
207                objDialog == (QObject *) YDialog::currentDialog()->widgetRep() ) )
208         {
209             emit wmClose();
210
211             if ( ! _wm_close_blocked )
212             {
213                 // Don't simply close the application window, return from UserInput()
214                 // with `id(`cancel) and let the YCP application decide how to handle
215                 // that (e.g., ask for confirmation).
216
217                 y2debug( "Caught window close event - returning with `cancel" );
218                 sendEvent( new YCancelEvent() );
219             }
220
221             return true;        // Event processed
222         }
223     }
224     else if ( ev->type() == QEvent::Show )
225     {
226         if ( obj == _main_win )
227         {
228 #ifdef FIXME_PROBABLY_OBSOLETE
229             if ( _widget_stack->count() > 0 )
230             {
231                 // Work around QWidgetStack bug: The last raiseWidget() call
232                 // (from closeDialog() ) might have failed if the widget was
233                 // invisible at that time, e.g., because the user had switched to
234                 // some other virtual desktop (bugzilla bug #11310)
235
236                 _widget_stack->setCurrentWidget( whatever );
237             }
238 #endif
239         }
240         else
241         {
242             return showEventFilter( obj, ev );
243         }
244     }
245     return QObject::eventFilter( obj, ev );
246 }
247
248
249 bool YQUI::showEventFilter( QObject * obj, QEvent * ev )
250 {
251     if ( ! haveWM() )
252     {
253         // Make sure newly opened windows get the keyboard focus even without a
254         // window manager. Otherwise the app might be unusable without a mouse.
255
256         QWidget * widget = dynamic_cast<QWidget *> (obj);
257
258         if ( widget )
259             widget->setActiveWindow();
260     }
261
262     return false;       // Don't stop event processing
263 }
264
265
266 /**
267  * UI-specific conversion from logical layout spacing units (80x25)
268  * to device dependent units (640x480).
269  **/
270 int YQUI::deviceUnits( YUIDimension dim, float size )
271 {
272     if ( dim==YD_HORIZ )        size *= ( 640.0/80 );
273     else                        size *= ( 480.0/25 );
274
275     return (int) ( size + 0.5 );
276 }
277
278
279 /**
280  * Default conversion from device dependent layout spacing units (640x480)
281  * to logical layout units (80x25).
282  *
283  * This default function assumes 80x25 units.
284  * Derived UIs may want to reimplement this.
285  **/
286 float YQUI::layoutUnits( YUIDimension dim, int device_units )
287 {
288     float size = (float) device_units;
289
290     if ( dim==YD_HORIZ )        size *= ( 80/640.0 );
291     else                        size *= ( 25/480.0 );
292
293     return size;
294 }
295
296
297 void YQUI::maybeLeftHandedUser()
298 {
299     if ( _askedForLeftHandedMouse )
300         return;
301
302
303     QString message =
304         _( "You clicked the right mouse button "
305            "where a left-click was expected."
306            "\n"
307            "Switch left and right mouse buttons?"
308            );
309     int button = QMessageBox::question( 0,
310                                         // Popup dialog caption
311                                         _( "Unexpected Click" ),
312                                         message,
313                                         QMessageBox::Yes | QMessageBox::Default,
314                                         QMessageBox::No,
315                                         QMessageBox::Cancel | QMessageBox::Escape );
316
317     if ( button == QMessageBox::Yes )
318     {
319
320         const char * command =
321             _leftHandedMouse ?
322             "xmodmap -e \"pointer = 1 2 3\"":   // switch back to right-handed mouse
323             "xmodmap -e \"pointer = 3 2 1\"";   // switch to left-handed mouse
324
325         _leftHandedMouse         = ! _leftHandedMouse;  // might be set repeatedly!
326         _askedForLeftHandedMouse = false;       // give the user a chance to switch back
327         y2milestone( "Switching mouse buttons: %s", command );
328
329         system( command );
330     }
331     else if ( button == 1 )     // No
332     {
333         _askedForLeftHandedMouse = true;
334     }
335 }
336
337
338
339 // EOF