summaryrefslogtreecommitdiffstats
path: root/kdeui/kmenubar.cpp
blob: 2014142e8f46b6a9b8d35711229107148291d80a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* This file is part of the KDE libraries
    Copyright (C) 1997, 1998, 1999, 2000  Sven Radej (radej@kde.org)
    Copyright (C) 1997, 1998, 1999, 2000 Matthias Ettrich (ettrich@kde.org)
    Copyright (C) 1999, 2000 Daniel "Mosfet" Duley (mosfet@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
    */


#ifndef INCLUDE_MENUITEM_DEF
#define INCLUDE_MENUITEM_DEF
#endif

#include "config.h"
#include <tqevent.h>
#include <tqobjectlist.h>
#include <tqaccel.h>
#include <tqpainter.h>
#include <tqstyle.h>
#include <tqtimer.h>

#include <kconfig.h>
#include <kglobalsettings.h>
#include <kmenubar.h>
#include <kapplication.h>
#include <kglobal.h>
#include <kdebug.h>
#include <kmanagerselection.h>

#ifdef Q_WS_X11
#include <kwin.h> 
#include <kwinmodule.h> 
#include <qxembed.h> 

#include <X11/Xlib.h> 
#include <X11/Xutil.h> 
#include <X11/Xatom.h> 

#ifndef None
#define None 0L
#endif
#endif

/*

 Toplevel menubar (not for the fallback size handling done by itself):
 - should not alter position or set strut
 - every toplevel must have at most one matching topmenu
 - embedder won't allow shrinking below a certain size
 - must have WM_TRANSIENT_FOR pointing the its mainwindow
     - the exception is desktop's menubar, which can be transient for root window
       because of using root window as the desktop window
 - Fitts' Law

*/

class KMenuBar::KMenuBarPrivate
{
public:
    KMenuBarPrivate()
	:   forcedTopLevel( false ),
	    topLevel( false ),
	    wasTopLevel( false ),
#ifdef Q_WS_X11
	    selection( NULL ),
#endif
            min_size( 0, 0 )
	{
	}
    ~KMenuBarPrivate()
        {
#ifdef Q_WS_X11
        delete selection;
#endif
        }
    bool forcedTopLevel;
    bool topLevel;
    bool wasTopLevel; // when TLW is fullscreen, remember state
    int frameStyle; // only valid in toplevel mode
    int lineWidth;  // dtto
    int margin;     // dtto
    bool fallback_mode; // dtto
#ifdef Q_WS_X11
    KSelectionWatcher* selection;
#endif
    TQTimer selection_timer;
    TQSize min_size;
    static Atom makeSelectionAtom();
};

#ifdef Q_WS_X11
static Atom selection_atom = None;
static Atom msg_type_atom = None;

static
void initAtoms()
{
    char nm[ 100 ];
    sprintf( nm, "_KDE_TOPMENU_OWNER_S%d", DefaultScreen( qt_xdisplay()));
    char nm2[] = "_KDE_TOPMENU_MINSIZE";
    char* names[ 2 ] = { nm, nm2 };
    Atom atoms[ 2 ];
    XInternAtoms( qt_xdisplay(), names, 2, False, atoms );
    selection_atom = atoms[ 0 ];
    msg_type_atom = atoms[ 1 ];
}
#endif

Atom KMenuBar::KMenuBarPrivate::makeSelectionAtom()
{
#ifdef Q_WS_X11
    if( selection_atom == None )
	initAtoms();
    return selection_atom;
#else
    return 0;
#endif
}

KMenuBar::KMenuBar(TQWidget *parent, const char *name)
  : TQMenuBar(parent, name)
{
#ifdef Q_WS_X11
    QXEmbed::initialize();
#endif
    d = new KMenuBarPrivate;
    connect( &d->selection_timer, TQT_SIGNAL( timeout()),
        this, TQT_SLOT( selectionTimeout()));

    connect( tqApp->desktop(), TQT_SIGNAL( resized( int )), TQT_SLOT( updateFallbackSize()));

    if ( kapp )
        // toolbarAppearanceChanged(int) is sent when changing macstyle
        connect( kapp, TQT_SIGNAL(toolbarAppearanceChanged(int)),
            this, TQT_SLOT(slotReadConfig()));

    slotReadConfig();
}

KMenuBar::~KMenuBar()
{
  delete d;
}

void KMenuBar::setTopLevelMenu(bool top_level)
{
  d->forcedTopLevel = top_level;
  setTopLevelMenuInternal( top_level );
}

void KMenuBar::setTopLevelMenuInternal(bool top_level)
{
  if (d->forcedTopLevel)
    top_level = true;

  d->wasTopLevel = top_level;
  if( parentWidget()
      && parentWidget()->tqtopLevelWidget()->isFullScreen())
    top_level = false;

  if ( isTopLevelMenu() == top_level )
    return;
  d->topLevel = top_level;
  if ( isTopLevelMenu() )
  {
#ifdef Q_WS_X11
      d->selection = new KSelectionWatcher( KMenuBarPrivate::makeSelectionAtom(),
          DefaultScreen( qt_xdisplay()));
      connect( d->selection, TQT_SIGNAL( newOwner( Window )),
          this, TQT_SLOT( updateFallbackSize()));
      connect( d->selection, TQT_SIGNAL( lostOwner()),
          this, TQT_SLOT( updateFallbackSize()));
#endif
      d->frameStyle = frameStyle();
      d->lineWidth = lineWidth();
      d->margin = margin();
      d->fallback_mode = false;
      bool wasShown = !isHidden();
      reparent( parentWidget(), (WFlags)(WType_TopLevel | WStyle_Tool | WStyle_Customize | WStyle_NoBorder), TQPoint(0,0), false );
#ifdef Q_WS_X11
      KWin::setType( winId(), NET::TopMenu );
      if( parentWidget())
          XSetTransientForHint( qt_xdisplay(), winId(), parentWidget()->tqtopLevelWidget()->winId());
#endif
      TQMenuBar::setFrameStyle( NoFrame );
      TQMenuBar::setLineWidth( 0 );
      TQMenuBar::setMargin( 0 );
      updateFallbackSize();
      d->min_size = TQSize( 0, 0 );
      if( parentWidget() && !parentWidget()->isTopLevel())
          setShown( parentWidget()->isVisible());
      else if ( wasShown )
          show();
  } else
  {
#ifdef Q_WS_X11
      delete d->selection;
      d->selection = NULL;
#endif
      setBackgroundMode( PaletteButton );
      setFrameStyle( d->frameStyle );
      setLineWidth( d->lineWidth );
      setMargin( d->margin );
      setMinimumSize( 0, 0 );
      setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
      updateMenuBarSize();
      if ( parentWidget() )
          reparent( parentWidget(), TQPoint(0,0), !isHidden());
  }
}

bool KMenuBar::isTopLevelMenu() const
{
  return d->topLevel;
}

// KDE4 remove
void KMenuBar::show()
{
    TQMenuBar::show();
}

void KMenuBar::slotReadConfig()
{
  KConfig *config = KGlobal::config();
  KConfigGroupSaver saver( config, "KDE" );
  setTopLevelMenuInternal( config->readBoolEntry( "macStyle", false ) );
}

bool KMenuBar::eventFilter(TQObject *obj, TQEvent *ev)
{
    if ( d->topLevel )
    {
	if ( parentWidget() && TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(parentWidget()->tqtopLevelWidget())  )
        {
	    if( ev->type() == TQEvent::Resize )
		return false; // ignore resizing of parent, TQMenuBar would try to adjust size
	    if ( ev->type() == TQEvent::Accel || ev->type() == TQEvent::AccelAvailable )
            {
		if ( TQApplication::sendEvent( tqtopLevelWidget(), ev ) )
		    return true;
	    }
            if(ev->type() == TQEvent::ShowFullScreen )
                // will update the state properly
                setTopLevelMenuInternal( d->topLevel );
        }
        if( parentWidget() && TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(parentWidget()) && ev->type() == TQEvent::Reparent )
            {
#ifdef Q_WS_X11
            XSetTransientForHint( qt_xdisplay(), winId(), parentWidget()->tqtopLevelWidget()->winId());
#else
            //TODO: WIN32?
#endif
            setShown( parentWidget()->isTopLevel() || parentWidget()->isVisible());
            }
        if( parentWidget() && !parentWidget()->isTopLevel() && TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(parentWidget()))
        { // if the parent is not toplevel, KMenuBar needs to match its visibility status
            if( ev->type() == TQEvent::Show )
                {
#ifdef Q_WS_X11
                XSetTransientForHint( qt_xdisplay(), winId(), parentWidget()->tqtopLevelWidget()->winId());
#else
                //TODO: WIN32?
#endif
                show();
                }
            if( ev->type() == TQEvent::Hide )
                hide();
	}
    }
    else
    {
        if( parentWidget() && TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(parentWidget()->tqtopLevelWidget()))
        {
            if( ev->type() == TQEvent::WindowStateChange
                && !parentWidget()->tqtopLevelWidget()->isFullScreen() )
                setTopLevelMenuInternal( d->wasTopLevel );
        }
    }
    return TQMenuBar::eventFilter( obj, ev );
}

// KDE4 remove
void KMenuBar::showEvent( TQShowEvent *e )
{
    TQMenuBar::showEvent(e);
}

void KMenuBar::updateFallbackSize()
{
    if( !d->topLevel )
	return;
#ifdef Q_WS_X11
    if( d->selection->owner() != None )
#endif
    { // somebody is managing us, don't mess anything, undo changes
      // done in fallback mode if needed
        d->selection_timer.stop();
        if( d->fallback_mode )
        {
            d->fallback_mode = false;
            KWin::setStrut( winId(), 0, 0, 0, 0 );
            setMinimumSize( 0, 0 );
            setMaximumSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
            updateMenuBarSize();
        }
	return;
    }
    if( d->selection_timer.isActive())
	return;
    d->selection_timer.start( 100, true );
}

void KMenuBar::selectionTimeout()
{ // nobody is managing us, handle resizing
    if ( d->topLevel )
    {
        d->fallback_mode = true; // KMenuBar is handling its position itself
        KConfigGroup xineramaConfig(KGlobal::config(),"Xinerama");
        int screen = xineramaConfig.readNumEntry("MenubarScreen",
            TQApplication::desktop()->screenNumber(TQPoint(0,0)) );
        TQRect area;
        if (kapp->desktop()->numScreens() < 2)
            area = kapp->desktop()->geometry();
        else
            area = kapp->desktop()->screenGeometry(screen);
        int margin = 0;
	move(area.left() - margin, area.top() - margin); 
        setFixedSize(area.width() + 2* margin , heightForWidth( area.width() + 2 * margin ) );
#ifdef Q_WS_X11
        int strut_height = height() - margin;
        if( strut_height < 0 )
            strut_height = 0;
        KWin::setStrut( winId(), 0, 0, strut_height, 0 );
#endif
    }
}

int KMenuBar::block_resize = 0;

void KMenuBar::resizeEvent( TQResizeEvent *e )
{
    if( e->spontaneous() && d->topLevel && !d->fallback_mode )
        {
        ++block_resize; // do not respond with configure request to ConfigureNotify event
        TQMenuBar::resizeEvent(e); // to avoid possible infinite loop
        --block_resize;
        }
    else
        TQMenuBar::resizeEvent(e);
}

void KMenuBar::setGeometry( const TQRect& r )
{
    setGeometry( r.x(), r.y(), r.width(), r.height() );
}

void KMenuBar::setGeometry( int x, int y, int w, int h )
{
    if( block_resize > 0 )
    {
	move( x, y );
	return;
    }
    checkSize( w, h );
    if( geometry() != TQRect( x, y, w, h ))
        TQMenuBar::setGeometry( x, y, w, h );
}

void KMenuBar::resize( int w, int h )
{
    if( block_resize > 0 )
	return;
    checkSize( w, h );
    if( size() != TQSize( w, h ))
        TQMenuBar::resize( w, h );
//    kdDebug() << "RS:" << w << ":" << h << ":" << width() << ":" << height() << ":" << minimumWidth() << ":" << minimumHeight() << endl;
}

void KMenuBar::checkSize( int& w, int& h )
{
    if( !d->topLevel || d->fallback_mode )
	return;
    TQSize s = tqsizeHint();
    w = s.width();
    h = s.height();
    // This is not done as setMinimumSize(), because that would set the minimum
    // size in WM_NORMAL_HINTS, and KWin would not allow changing to smaller size
    // anymore
    w = KMAX( w, d->min_size.width());
    h = KMAX( h, d->min_size.height());
}

// QMenuBar's tqsizeHint() gives wrong size (insufficient width), which causes wrapping in the kicker applet
TQSize KMenuBar::tqsizeHint() const
{
    if( !d->topLevel || block_resize > 0 )
        return TQMenuBar::tqsizeHint();
    // Since TQMenuBar::tqsizeHint() may indirectly call resize(),
    // avoid infinite recursion.
    ++block_resize;
    // find the minimum useful height, and enlarge the width until the menu fits in that height (one row)
    int h = heightForWidth( 1000000 );
    int w = TQMenuBar::tqsizeHint().width();
    // optimization - don't call heightForWidth() too many times
    while( heightForWidth( w + 12 ) > h )
        w += 12;
    while( heightForWidth( w + 4 ) > h )
        w += 4;
    while( heightForWidth( w ) > h )
        ++w;
    --block_resize;
    return TQSize( w, h );
}

#ifdef Q_WS_X11
bool KMenuBar::x11Event( XEvent* ev )
{
    if( ev->type == ClientMessage && ev->xclient.message_type == msg_type_atom
        && ev->xclient.window == winId())
    {
        // TQMenuBar is trying really hard to keep the size it deems right.
        // Forcing minimum size and blocking resizing to match parent size
        // in checkResizingToParent() seem to be the only way to make
        // KMenuBar keep the size it wants
	d->min_size = TQSize( ev->xclient.data.l[ 1 ], ev->xclient.data.l[ 2 ] );
//        kdDebug() << "MINSIZE:" << d->min_size << endl;
        updateMenuBarSize();
	return true;
    }
    return TQMenuBar::x11Event( ev );
}
#endif

void KMenuBar::updateMenuBarSize()
    {
    menuContentsChanged(); // trigger invalidating calculated size
    resize( tqsizeHint());   // and resize to preferred size
    }

void KMenuBar::setFrameStyle( int style )
{
    if( d->topLevel )
	d->frameStyle = style;
    else
	TQMenuBar::setFrameStyle( style );
}

void KMenuBar::setLineWidth( int width )
{
    if( d->topLevel )
	d->lineWidth = width;
    else
	TQMenuBar::setLineWidth( width );
}

void KMenuBar::setMargin( int margin )
{
    if( d->topLevel )
	d->margin = margin;
    else
	TQMenuBar::setMargin( margin );
}

void KMenuBar::closeEvent( TQCloseEvent* e )
{
    if( d->topLevel )
        e->ignore(); // mainly for the fallback mode 
    else
        TQMenuBar::closeEvent( e );
}

void KMenuBar::drawContents( TQPainter* p )
{
    // Closes the BR77113
    // We need to overload this method to paint only the menu items
    // This way when the KMenuBar is embedded in the menu applet it
    // integrates correctly.
    //
    // Background mode and origin are set so late because of styles
    // using the polish() method to modify these settings.
    //
    // Of course this hack can safely be removed when real transparency
    // will be available

    if( !d->topLevel )
    {
        TQMenuBar::drawContents(p);
    }
    else
    {
        bool up_enabled = isUpdatesEnabled();
        BackgroundMode bg_mode = backgroundMode();
        BackgroundOrigin bg_origin = backgroundOrigin();
        
        tqsetUpdatesEnabled(false);
        setBackgroundMode(X11ParentRelative);
        setBackgroundOrigin(WindowOrigin);

	p->eraseRect( rect() );
	erase();
        
        TQColorGroup g = tqcolorGroup();
        bool e;

        for ( int i=0; i<(int)count(); i++ )
        {
            TQMenuItem *mi = findItem( idAt( i ) );

            if ( !mi->text().isNull() || mi->pixmap() )
            {
                TQRect r = tqitemRect(i);
                if(r.isEmpty() || !mi->isVisible())
                    continue;

                e = mi->isEnabledAndVisible();
                if ( e )
                    g = isEnabled() ? ( isActiveWindow() ? tqpalette().active() :
                                        tqpalette().inactive() ) : tqpalette().disabled();
                else
                    g = tqpalette().disabled();

                bool item_active = ( actItem ==  i );

                p->setClipRect(r);

                if( item_active )
                {
                    TQStyle::SFlags flags = TQStyle::Style_Default;
                    if (isEnabled() && e)
                        flags |= TQStyle::Style_Enabled;
                    if ( item_active )
                        flags |= TQStyle::Style_Active;
                    if ( item_active && actItemDown )
                        flags |= TQStyle::Style_Down;
                    flags |= TQStyle::Style_HasFocus;

                    tqstyle().tqdrawControl(TQStyle::CE_MenuBarItem, p, this,
                                        r, g, flags, TQStyleOption(mi));
                }
                else
                {
                    tqstyle().drawItem(p, r, AlignCenter | AlignVCenter | ShowPrefix,
                                     g, e, mi->pixmap(), mi->text());
                }
            }
        }

        setBackgroundOrigin(bg_origin);
        setBackgroundMode(bg_mode);
        tqsetUpdatesEnabled(up_enabled);
    }
}

void KMenuBar::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "kmenubar.moc"