summaryrefslogtreecommitdiff
path: root/vcl/unx/gtk/window/gtksalmenu.cxx
blob: 0c78997e9177482b7bfe20b81d19f631c8f79a0e (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

#include "unx/gtk/gtksalmenu.hxx"

//#include <gtk/gtk.h>
#include <unx/gtk/glomenu.h>
#include <unx/gtk/gloactiongroup.h>
#include <vcl/menu.hxx>
#include <unx/gtk/gtkinst.hxx>

#include <iostream>

using namespace std;

//Some menus are special, this is the list of them
gboolean
isSpecialSubmenu (OUString command)
{
    const gchar * specialSubmenus[11] = {".uno:CharFontName",
                                         ".uno:FontHeight",
                                         ".uno:ObjectMenue",
                                         ".uno:InsertPageHeader",
                                         ".uno:InsertPageFooter",
                                         ".uno:ChangeControlType",
                                         ".uno:AvailableToolbars",
                                         ".uno:ScriptOrganizer",
                                         ".uno:RecentFileList",
                                         ".uno:AddDirect",
                                         ".uno:AutoPilotMenu"};

    for (gint i = 0; i < 11; i++)
    {
        if (command.equals (OUString::createFromAscii (specialSubmenus[i])))
            return TRUE;
    }
    return FALSE;
}

static void
dispatchAction (GSimpleAction   *action,
                GVariant        *parameter,
                gpointer        user_data)
{
    GTK_YIELD_GRAB();

    if ( user_data ) {
        GtkSalMenuItem *pSalMenuItem = static_cast< GtkSalMenuItem* >( user_data );

        if ( !pSalMenuItem->mpSubMenu ) {
            const GtkSalFrame *pFrame = pSalMenuItem->mpParentMenu ? pSalMenuItem->mpParentMenu->GetFrame() : NULL;

            if ( pFrame && !pFrame->GetParent() ) {
                ((PopupMenu*) pSalMenuItem->mpVCLMenu)->SetSelectedEntry( pSalMenuItem->mnId );
                SalMenuEvent aMenuEvt( pSalMenuItem->mnId, pSalMenuItem->mpVCLMenu );
                pFrame->CallCallback( SALEVENT_MENUCOMMAND, &aMenuEvt );
            }
            else if ( pSalMenuItem->mpVCLMenu )
            {
                // if an item from submenu was selected. the corresponding Window does not exist because
                // we use native popup menus, so we have to set the selected menuitem directly
                // incidentally this of course works for top level popup menus, too
                PopupMenu * pPopupMenu = dynamic_cast<PopupMenu *>(pSalMenuItem->mpVCLMenu);
                if( pPopupMenu )
                {
                    // FIXME: revise this ugly code

                    // select handlers in vcl are dispatch on the original menu
                    // if not consumed by the select handler of the current menu
                    // however since only the starting menu ever came into Execute
                    // the hierarchy is not build up. Workaround this by getting
                    // the menu it should have been

                    // get started from hierarchy in vcl menus
                    GtkSalMenu* pParentMenu = pSalMenuItem->mpParentMenu;
                    Menu* pCurMenu = pSalMenuItem->mpVCLMenu;
                    while( pParentMenu && pParentMenu->GetMenu() )
                    {
                        pCurMenu = pParentMenu->GetMenu();
                        pParentMenu = pParentMenu->GetParentSalMenu();
                    }

                    pPopupMenu->SetSelectedEntry( pSalMenuItem->mnId );
                    pPopupMenu->ImplSelectWithStart( pCurMenu );
                }
                else
                    OSL_FAIL( "menubar item without frame !" );
            }
        }
    }
}

void generateActions( GtkSalMenu* pMenu, GLOActionGroup* pActionGroup )
{
    if ( !pMenu || !pMenu->GetMenuModel() )
        return;

    for (sal_uInt16 i = 0; i < pMenu->GetItemCount(); i++) {
        GtkSalMenuItem *pSalMenuItem = pMenu->GetItemAtPos( i );

        if ( pSalMenuItem->mpAction ) {
            g_lo_action_group_insert( pActionGroup, pSalMenuItem->mpAction );
        }

        generateActions( pSalMenuItem->mpSubMenu, pActionGroup );
    }
}

void
gdk_x11_window_set_utf8_property  (GdkWindow *window,
                                   const gchar *name,
                                   const gchar *value)
{
  GdkDisplay *display;

  //if (!WINDOW_IS_TOPLEVEL (window))
    //return;

  display = gdk_window_get_display (window);

  if (value != NULL)
    {
      XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
                       GDK_WINDOW_XID (window),
                       gdk_x11_get_xatom_by_name_for_display (display, name),
                       gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
                       PropModeReplace, (guchar *)value, strlen (value));
    }
  else
    {
      XDeleteProperty (GDK_DISPLAY_XDISPLAY (display),
                       GDK_WINDOW_XID (window),
                       gdk_x11_get_xatom_by_name_for_display (display, name));
    }
}

void GtkSalMenu::publishMenu( GMenuModel *pMenu, GActionGroup *pActionGroup )
{
    if ( mMenubarId ) {
        g_dbus_connection_unexport_menu_model( pSessionBus, mMenubarId );
        mMenubarId = 0;
    }

    if ( mActionGroupId ) {
        g_dbus_connection_unexport_action_group( pSessionBus, mActionGroupId );
        mActionGroupId = 0;
    }

    if ( aDBusMenubarPath ) {
        mMenubarId = g_dbus_connection_export_menu_model (pSessionBus, aDBusMenubarPath, pMenu, NULL);
        if(!mMenubarId) puts("Fail export menubar");
    }

    if ( aDBusPath ) {
        mActionGroupId = g_dbus_connection_export_action_group( pSessionBus, aDBusPath, pActionGroup, NULL);
    }
}


GtkSalMenuItem* GtkSalMenu::GetSalMenuItem( sal_uInt16 nId )
{
    for ( sal_uInt16 i = 0; i < maItems.size(); i++ )
    {
        GtkSalMenuItem* pSalMenuItem = maItems[ i ];

        if ( pSalMenuItem->mnId == nId ) {
            return pSalMenuItem;
        }

        if ( pSalMenuItem->mpSubMenu )
        {
            pSalMenuItem = pSalMenuItem->mpSubMenu->GetSalMenuItem( nId );
            if (pSalMenuItem) {
                return pSalMenuItem;
            }
        }
    }

    return NULL;
}

GtkSalMenu::GtkSalMenu( sal_Bool bMenuBar ) :
    mbMenuBar( bMenuBar ),
    mpVCLMenu( NULL ),
    mpParentSalMenu( NULL ),
    mpFrame( NULL ),
    aDBusPath( NULL ),
    aDBusMenubarPath( NULL ),
    pSessionBus( NULL ),
    mMenubarId( 0 ),
    mActionGroupId ( 0 )
{
    mpCurrentSection = G_MENU_MODEL( g_lo_menu_new() );
    maSections.push_back( mpCurrentSection );

    mpMenuModel = G_MENU_MODEL( g_lo_menu_new() );
    g_lo_menu_append_section( G_LO_MENU( mpMenuModel ), NULL, mpCurrentSection );

    if (bMenuBar) {
        pSessionBus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
        if(!pSessionBus) puts ("Fail bus get");
    }
}

GtkSalMenu::~GtkSalMenu()
{
    if ( mpFrame ) {
        GtkWidget *widget = GTK_WIDGET( mpFrame->getWindow() );
        GdkWindow *gdkWindow = gtk_widget_get_window( widget );
        if (gdkWindow) {
            gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_UNIQUE_BUS_NAME", NULL );
            gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_APPLICATION_OBJECT_PATH", NULL );
            gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_WINDOW_OBJECT_PATH", NULL );
            gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_MENUBAR_OBJECT_PATH", NULL );
        }
    }

    if ( mMenubarId ) {
        g_dbus_connection_unexport_menu_model( pSessionBus, mMenubarId );
        mMenubarId = 0;
    }

    if ( mActionGroupId ) {
        g_dbus_connection_unexport_action_group( pSessionBus, mActionGroupId );
        mActionGroupId = 0;
    }

    if ( pSessionBus ) {
        g_dbus_connection_flush_sync( pSessionBus, NULL, NULL );
    }

    pSessionBus = NULL;

    g_free( aDBusPath );
    g_free( aDBusMenubarPath );

    maSections.clear();
    maItems.clear();

    g_object_unref( mpMenuModel );
    g_object_unref( mpCurrentSection );
}

sal_Bool GtkSalMenu::VisibleMenuBar()
{
    return sal_False;
}

void GtkSalMenu::InsertItem( SalMenuItem* pSalMenuItem, unsigned nPos )
{
    GtkSalMenuItem *pGtkSalMenuItem = static_cast<GtkSalMenuItem*>( pSalMenuItem );

    if ( pGtkSalMenuItem->mpMenuItem ) {
        pGtkSalMenuItem->mpParentSection = mpCurrentSection;
        pGtkSalMenuItem->mnPos = g_menu_model_get_n_items( mpCurrentSection );

        maItems.push_back( pGtkSalMenuItem );

        g_lo_menu_insert_item( G_LO_MENU( mpCurrentSection ), pGtkSalMenuItem->mnPos, pGtkSalMenuItem->mpMenuItem );
    } else {
        // If no mpMenuItem exists, then item is a separator.
        mpCurrentSection = G_MENU_MODEL( g_lo_menu_new() );
        maSections.push_back( mpCurrentSection );
        g_lo_menu_append_section( G_LO_MENU( mpMenuModel ), NULL, mpCurrentSection );
    }

    pGtkSalMenuItem->mpParentMenu = this;
}

void GtkSalMenu::RemoveItem( unsigned nPos )
{
}

void GtkSalMenu::SetSubMenu( SalMenuItem* pSalMenuItem, SalMenu* pSubMenu, unsigned nPos )
{
    GtkSalMenuItem *pGtkSalMenuItem = static_cast<GtkSalMenuItem*>( pSalMenuItem );
    GtkSalMenu *pGtkSubMenu = static_cast<GtkSalMenu*>( pSubMenu );

    if ( pGtkSubMenu ) {
        pGtkSalMenuItem->mpSubMenu = pGtkSubMenu;
        g_lo_menu_item_set_submenu( pGtkSalMenuItem->mpMenuItem, pGtkSubMenu->mpMenuModel );

        if ( !pGtkSubMenu->mpParentSalMenu ) {
            pGtkSubMenu->mpParentSalMenu = this;
        }
    }
}

void GtkSalMenu::SetFrame( const SalFrame* pFrame )
{
    mpFrame = static_cast<const GtkSalFrame*>( pFrame );

    GtkWidget *widget = GTK_WIDGET( mpFrame->getWindow() );

    GdkWindow *gdkWindow = gtk_widget_get_window( widget );

    if (gdkWindow) {
        XLIB_Window windowId = GDK_WINDOW_XID( gdkWindow );

        aDBusPath = g_strdup_printf("/window/%lu", windowId);
        gchar* aDBusWindowPath = g_strdup_printf( "/window/%lu", windowId );
        aDBusMenubarPath = g_strdup_printf( "/window/%lu/menus/menubar", windowId );

        gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_UNIQUE_BUS_NAME", g_dbus_connection_get_unique_name( pSessionBus ) );
        gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_APPLICATION_OBJECT_PATH", "" );
        gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_WINDOW_OBJECT_PATH", aDBusWindowPath );
        gdk_x11_window_set_utf8_property ( gdkWindow, "_GTK_MENUBAR_OBJECT_PATH", aDBusMenubarPath );

        // Try to publish the menu with the right bus data.
        Freeze();
    }
}

const GtkSalFrame* GtkSalMenu::GetFrame() const
{
    const GtkSalMenu* pMenu = this;
    while( pMenu && ! pMenu->mpFrame )
        pMenu = pMenu->mpParentSalMenu;
    return pMenu ? pMenu->mpFrame : NULL;
}

void GtkSalMenu::CheckItem( unsigned nPos, sal_Bool bCheck )
{
}

void GtkSalMenu::EnableItem( unsigned nPos, sal_Bool bEnable )
{
    sal_uInt16 itemId = mpVCLMenu->GetItemId( nPos );

    GtkSalMenuItem *pSalMenuItem = GetSalMenuItem( itemId );

    if ( pSalMenuItem && pSalMenuItem->mpAction ) {
        gboolean bItemEnabled = (bEnable == sal_True) ? TRUE : FALSE;
        g_simple_action_set_enabled( G_SIMPLE_ACTION( pSalMenuItem->mpAction ), bItemEnabled );
    }
}

void GtkSalMenu::SetItemText( unsigned nPos, SalMenuItem* pSalMenuItem, const rtl::OUString& rText )
{
    // Replace the "~" character with "_".
    rtl::OUString aText = rText.replace( '~', '_' );
    rtl::OString aConvertedText = OUStringToOString(aText, RTL_TEXTENCODING_UTF8);

    GtkSalMenuItem *pGtkSalMenuItem = static_cast<GtkSalMenuItem*>( pSalMenuItem );

    GLOMenuItem *pMenuItem = G_LO_MENU_ITEM( pGtkSalMenuItem->mpMenuItem );

    g_lo_menu_item_set_label( pMenuItem, aConvertedText.getStr() );

    if ( pGtkSalMenuItem->mpParentSection ) {
        g_lo_menu_remove( G_LO_MENU( pGtkSalMenuItem->mpParentSection ), pGtkSalMenuItem->mnPos );
        g_lo_menu_insert_item( G_LO_MENU( pGtkSalMenuItem->mpParentSection ), pGtkSalMenuItem->mnPos, pGtkSalMenuItem->mpMenuItem );
    }
}

void GtkSalMenu::SetItemImage( unsigned nPos, SalMenuItem* pSalMenuItem, const Image& rImage)
{
}

void GtkSalMenu::SetAccelerator( unsigned nPos, SalMenuItem* pSalMenuItem, const KeyCode& rKeyCode, const rtl::OUString& rKeyName )
{
}

void GtkSalMenu::SetItemCommand( unsigned nPos, SalMenuItem* pSalMenuItem, const rtl::OUString& aCommandStr )
{
    GtkSalMenuItem* pGtkSalMenuItem = static_cast< GtkSalMenuItem* >( pSalMenuItem );

    if ( pGtkSalMenuItem->mpAction ) {
        g_object_unref( pGtkSalMenuItem->mpAction );
    }

    rtl::OString aOCommandStr = rtl::OUStringToOString( aCommandStr, RTL_TEXTENCODING_UTF8 );

    GSimpleAction *pAction = g_simple_action_new( aOCommandStr.getStr(), NULL );

//    if ( !pGtkSalMenuItem->mpVCLMenu->GetPopupMenu( pGtkSalMenuItem->mnId ) ) {
        g_signal_connect(pAction, "activate", G_CALLBACK( dispatchAction ), pGtkSalMenuItem);
//    }

    pGtkSalMenuItem->mpAction = G_ACTION( pAction );


    rtl::OString aItemCommand = "win." + aOCommandStr;
    g_lo_menu_item_set_action_and_target( pGtkSalMenuItem->mpMenuItem, aItemCommand.getStr(), NULL );

    if ( pGtkSalMenuItem->mpParentSection ) {
        g_lo_menu_remove( G_LO_MENU( pGtkSalMenuItem->mpParentSection ), pGtkSalMenuItem->mnPos );
        g_lo_menu_insert_item( G_LO_MENU( pGtkSalMenuItem->mpParentSection ), pGtkSalMenuItem->mnPos, pGtkSalMenuItem->mpMenuItem );
    }
}

void GtkSalMenu::GetSystemMenuData( SystemMenuData* pData )
{
}

bool GtkSalMenu::ShowNativePopupMenu(FloatingWindow * pWin, const Rectangle& rRect, sal_uLong nFlags)
{
    return TRUE;
}

void updateNativeMenu( GtkSalMenu* pMenu ) {
    if ( pMenu ) {
        for ( sal_uInt16 i = 0; i < pMenu->GetItemCount(); i++ ) {
            GtkSalMenuItem* pSalMenuItem = pMenu->GetItemAtPos( i );
            String aText = pSalMenuItem->mpVCLMenu->GetItemText( pSalMenuItem->mnId );

            // Force updating of native menu labels.
            pMenu->SetItemText( i, pSalMenuItem, aText );

            if ( pSalMenuItem->mpSubMenu && pSalMenuItem->mpSubMenu->GetMenu() ) {
                pSalMenuItem->mpSubMenu->GetMenu()->Activate();
                updateNativeMenu( pSalMenuItem->mpSubMenu );
            }
        }
    }
}
void GtkSalMenu::Freeze()
{
    updateNativeMenu( this );

    GLOActionGroup *mpActionGroup = g_lo_action_group_new();

    generateActions( this, mpActionGroup );

    // Menubar would have one section only.
    this->publishMenu( mpMenuModel, G_ACTION_GROUP( mpActionGroup ) );
}

// =======================================================================

/*
 * GtkSalMenuItem
 */

GtkSalMenuItem::GtkSalMenuItem( const SalItemParams* pItemData ) :
    mnId( pItemData->nId ),
    mnPos( 0 ),
    mpVCLMenu( pItemData->pMenu ),
    mpParentMenu( NULL ),
    mpSubMenu( NULL ),
    mpMenuItem( NULL ),
    mpAction( NULL )
{
    if ( pItemData->eType != MENUITEM_SEPARATOR ) {
        mpMenuItem = g_lo_menu_item_new( "EMPTY STRING", NULL );
    }
}

GtkSalMenuItem::~GtkSalMenuItem()
{
    if ( mpMenuItem ) {
        g_object_unref( mpMenuItem );
    }
}