summaryrefslogtreecommitdiffstats
path: root/widget/gtk/nsMenuContainer.cpp
blob: 081e98a6aebdc36ab8c72bfcd80c9a3810e1f0d3 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/DebugOnly.h"
#include "mozilla/Move.h"
#include "nsGkAtoms.h"
#include "nsIAtom.h"
#include "nsIContent.h"

#include "nsDbusmenu.h"
#include "nsMenu.h"
#include "nsMenuItem.h"
#include "nsMenuSeparator.h"

#include "nsMenuContainer.h"

using namespace mozilla;

const nsMenuContainer::ChildTArray::index_type nsMenuContainer::NoIndex = nsMenuContainer::ChildTArray::NoIndex;

typedef UniquePtr<nsMenuObject> (*nsMenuObjectConstructor)(nsMenuContainer*,
                                                           nsIContent*);

template<class T>
static UniquePtr<nsMenuObject> CreateMenuObject(nsMenuContainer* aContainer,
                                                nsIContent* aContent) {
    return UniquePtr<T>(new T(aContainer, aContent));
}

static nsMenuObjectConstructor
GetMenuObjectConstructor(nsIContent* aContent) {
    if (aContent->IsXULElement(nsGkAtoms::menuitem)) {
        return CreateMenuObject<nsMenuItem>;
    } else if (aContent->IsXULElement(nsGkAtoms::menu)) {
        return CreateMenuObject<nsMenu>;
    } else if (aContent->IsXULElement(nsGkAtoms::menuseparator)) {
        return CreateMenuObject<nsMenuSeparator>;
    }

    return nullptr;
}

static bool
ContentIsSupported(nsIContent* aContent) {
    return GetMenuObjectConstructor(aContent) ? true : false;
}

nsMenuContainer::nsMenuContainer(nsMenuContainer* aParent,
                                 nsIContent* aContent) :
    nsMenuObject(aParent, aContent) {
}

nsMenuContainer::nsMenuContainer(nsNativeMenuDocListener* aListener,
                                 nsIContent* aContent) :
    nsMenuObject(aListener, aContent) {
}

UniquePtr<nsMenuObject>
nsMenuContainer::CreateChild(nsIContent* aContent) {
    nsMenuObjectConstructor ctor = GetMenuObjectConstructor(aContent);
    if (!ctor) {
        // There are plenty of node types we might stumble across that
        // aren't supported
        return nullptr;
    }

    UniquePtr<nsMenuObject> res = ctor(this, aContent);
    return Move(res);
}

size_t
nsMenuContainer::IndexOf(nsIContent* aChild) const {
    if (!aChild) {
        return NoIndex;
    }

    size_t count = ChildCount();
    for (size_t i = 0; i < count; ++i) {
        if (ChildAt(i)->ContentNode() == aChild) {
            return i;
        }
    }

    return NoIndex;
}

void
nsMenuContainer::RemoveChildAt(size_t aIndex, bool aUpdateNative) {
    MOZ_ASSERT(aIndex < ChildCount());

    if (aUpdateNative) {
        MOZ_ALWAYS_TRUE(
            dbusmenu_menuitem_child_delete(GetNativeData(),
                                           ChildAt(aIndex)->GetNativeData()));
    }

    mChildren.RemoveElementAt(aIndex);
}

void
nsMenuContainer::RemoveChild(nsIContent* aChild, bool aUpdateNative) {
    size_t index = IndexOf(aChild);
    if (index == NoIndex) {
        return;
    }

    RemoveChildAt(index, aUpdateNative);
}

void
nsMenuContainer::InsertChildAfter(UniquePtr<nsMenuObject> aChild,
                                  nsIContent* aPrevSibling,
                                  bool aUpdateNative) {
    size_t index = IndexOf(aPrevSibling);
    MOZ_ASSERT(!aPrevSibling || index != NoIndex);

    ++index;

    if (aUpdateNative) {
        aChild->CreateNativeData();
        MOZ_ALWAYS_TRUE(
            dbusmenu_menuitem_child_add_position(GetNativeData(),
                                                 aChild->GetNativeData(),
                                                 index));
    }

    MOZ_ALWAYS_TRUE(mChildren.InsertElementAt(index, Move(aChild)));
}

void
nsMenuContainer::AppendChild(UniquePtr<nsMenuObject> aChild,
                             bool aUpdateNative) {
    if (aUpdateNative) {
        aChild->CreateNativeData();
        MOZ_ALWAYS_TRUE(
            dbusmenu_menuitem_child_append(GetNativeData(),
                                           aChild->GetNativeData()));
    }

    MOZ_ALWAYS_TRUE(mChildren.AppendElement(Move(aChild)));
}

bool
nsMenuContainer::NeedsRebuild() const {
    return false;
}

/* static */ nsIContent*
nsMenuContainer::GetPreviousSupportedSibling(nsIContent* aContent) {
    do {
        aContent = aContent->GetPreviousSibling();
    } while (aContent && !ContentIsSupported(aContent));

    return aContent;
}