1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquickmenubar_p.h"
41
42#include <private/qguiapplication_p.h>
43#include <QtGui/qpa/qplatformtheme.h>
44#include <QtGui/qpa/qplatformmenu.h>
45#include <qquickwindow.h>
46
47QT_BEGIN_NAMESPACE
48
49
50/*!
51 \class QQuickMenuBar1
52 \internal
53 */
54
55/*!
56 \qmltype MenuBarPrivate
57 \instantiates QQuickMenuBar1
58 \internal
59 \inqmlmodule QtQuick.Controls
60 */
61
62/*!
63 \qmlproperty list<Menu> MenuBar::menus
64 \default
65
66 The list of menus in the menubar.
67
68 \sa Menu
69*/
70
71QQuickMenuBar1::QQuickMenuBar1(QObject *parent)
72 : QObject(parent), m_platformMenuBar(0), m_contentItem(0), m_parentWindow(0)
73{
74}
75
76QQuickMenuBar1::~QQuickMenuBar1()
77{
78 if (isNative())
79 setNativeNoNotify(false);
80}
81
82QQmlListProperty<QQuickMenu1> QQuickMenuBar1::menus()
83{
84 return QQmlListProperty<QQuickMenu1>(this, 0, &QQuickMenuBar1::append_menu, &QQuickMenuBar1::count_menu, &QQuickMenuBar1::at_menu, 0);
85}
86
87bool QQuickMenuBar1::isNative() const
88{
89 return m_platformMenuBar != 0;
90}
91
92void QQuickMenuBar1::setNative(bool native)
93{
94 bool wasNative = isNative();
95 setNativeNoNotify(native);
96 if (isNative() != wasNative)
97 emit nativeChanged();
98}
99
100void QQuickMenuBar1::setNativeNoNotify(bool native)
101{
102 // QTBUG-51372
103 if (QGuiApplication::platformName() == QStringLiteral("xcb"))
104 return;
105
106 if (native) {
107 if (!m_platformMenuBar) {
108 m_platformMenuBar = QGuiApplicationPrivate::platformTheme()->createPlatformMenuBar();
109 if (m_platformMenuBar) {
110 m_platformMenuBar->handleReparent(newParentWindow: m_parentWindow);
111 for (QQuickMenu1 *menu : qAsConst(t&: m_menus))
112 m_platformMenuBar->insertMenu(menu: menu->platformMenu(), before: 0 /* append */);
113 }
114 }
115 } else {
116 if (m_platformMenuBar) {
117 for (QQuickMenu1 *menu : qAsConst(t&: m_menus))
118 m_platformMenuBar->removeMenu(menu: menu->platformMenu());
119 }
120 delete m_platformMenuBar;
121 m_platformMenuBar = 0;
122 }
123}
124
125void QQuickMenuBar1::setContentItem(QQuickItem *item)
126{
127 if (item != m_contentItem) {
128 m_contentItem = item;
129 emit contentItemChanged();
130 }
131}
132
133void QQuickMenuBar1::setParentWindow(QQuickWindow *newParentWindow)
134{
135 if (newParentWindow != m_parentWindow) {
136 m_parentWindow = newParentWindow;
137 if (m_platformMenuBar)
138 m_platformMenuBar->handleReparent(newParentWindow: m_parentWindow);
139 }
140}
141
142void QQuickMenuBar1::append_menu(QQmlListProperty<QQuickMenu1> *list, QQuickMenu1 *menu)
143{
144 if (QQuickMenuBar1 *menuBar = qobject_cast<QQuickMenuBar1 *>(object: list->object)) {
145 menu->setParent(menuBar);
146 menuBar->m_menus.append(t: menu);
147
148 if (menuBar->m_platformMenuBar)
149 menuBar->m_platformMenuBar->insertMenu(menu: menu->platformMenu(), before: 0 /* append */);
150
151 emit menuBar->menusChanged();
152 }
153}
154
155int QQuickMenuBar1::count_menu(QQmlListProperty<QQuickMenu1> *list)
156{
157 if (QQuickMenuBar1 *menuBar = qobject_cast<QQuickMenuBar1 *>(object: list->object))
158 return menuBar->m_menus.size();
159 return 0;
160}
161
162QQuickMenu1 *QQuickMenuBar1::at_menu(QQmlListProperty<QQuickMenu1> *list, int index)
163{
164 QQuickMenuBar1 *menuBar = qobject_cast<QQuickMenuBar1 *>(object: list->object);
165 if (menuBar && 0 <= index && index < menuBar->m_menus.size())
166 return menuBar->m_menus[index];
167 return 0;
168}
169
170QT_END_NAMESPACE
171

source code of qtquickcontrols/src/controls/qquickmenubar.cpp