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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright © 2011 Canonical Ltd.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* licence, 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* Author: Antonio Fernández <antonio.fernandez@aentos.es>
*/
#include <unx/gtk/gtksalmenu.hxx>
#include <unx/gtk/glomenu.h>
#include <unx/gtk/gloactiongroup.h>
#include <vcl/menu.hxx>
#include <unx/gtk/gtkinst.hxx>
#if GTK_CHECK_VERSION(3,0,0)
# include <gdk/gdkkeysyms-compat.h>
#endif
#include <svtools/menuoptions.hxx>
#include <framework/menuconfiguration.hxx>
#include <sal/log.hxx>
static sal_Bool bMenuVisibility = sal_False;
/*
* This function generates an alternative command name to avoid name collisions
* or to give a valid command name to certain menu items.
*/
static gchar* GetCommandForSpecialItem( GtkSalMenuItem* pSalMenuItem )
{
gchar* aCommand = NULL;
sal_uInt16 nId = pSalMenuItem->mnId;
Menu* pMenu = pSalMenuItem->mpVCLMenu;
// If item belongs to window list, generate a command with "window-(id)" format.
if ( ( nId >= START_ITEMID_WINDOWLIST ) && ( nId <= END_ITEMID_WINDOWLIST ) )
aCommand = g_strdup_printf( "window-%d", nId );
else
if ( pMenu )
{
rtl::OUString aMenuCommand = pMenu->GetItemCommand( nId );
MenuItemBits nBits = pMenu->GetItemBits( nId );
if ( aMenuCommand.equalsAscii(".uno:Presentation") && nBits == 0 )
aCommand = g_strdup(".uno:Presentation2");
}
return aCommand;
}
static void KeyCodeToGdkKey ( const KeyCode& rKeyCode, guint* pGdkKeyCode, GdkModifierType *pGdkModifiers )
{
if ( pGdkKeyCode == NULL || pGdkModifiers == NULL )
return;
// Get GDK key modifiers
GdkModifierType nModifiers = (GdkModifierType) 0;
if ( rKeyCode.IsShift() )
nModifiers = (GdkModifierType) ( nModifiers | GDK_SHIFT_MASK );
if ( rKeyCode.IsMod1() )
nModifiers = (GdkModifierType) ( nModifiers | GDK_CONTROL_MASK );
if ( rKeyCode.IsMod2() )
nModifiers = (GdkModifierType) ( nModifiers | GDK_MOD1_MASK );
*pGdkModifiers = nModifiers;
// Get GDK keycode.
guint nKeyCode = 0;
guint nCode = rKeyCode.GetCode();
if ( nCode >= KEY_0 && nCode <= KEY_9 )
nKeyCode = ( nCode - KEY_0 ) + GDK_0;
else if ( nCode >= KEY_A && nCode <= KEY_Z )
nKeyCode = ( nCode - KEY_A ) + GDK_A;
else if ( nCode >= KEY_F1 && nCode <= KEY_F26 )
nKeyCode = ( nCode - KEY_F1 ) + GDK_F1;
else
{
switch( nCode )
{
case KEY_DOWN: nKeyCode = GDK_Down; break;
case KEY_UP: nKeyCode = GDK_Up; break;
case KEY_LEFT: nKeyCode = GDK_Left; break;
case KEY_RIGHT: nKeyCode = GDK_Right; break;
case KEY_HOME: nKeyCode = GDK_Home; break;
case KEY_END: nKeyCode = GDK_End; break;
case KEY_PAGEUP: nKeyCode = GDK_Page_Up; break;
case KEY_PAGEDOWN: nKeyCode = GDK_Page_Down; break;
case KEY_RETURN: nKeyCode = GDK_Return; break;
case KEY_ESCAPE: nKeyCode = GDK_Escape; break;
case KEY_TAB: nKeyCode = GDK_Tab; break;
case KEY_BACKSPACE: nKeyCode = GDK_BackSpace; break;
case KEY_SPACE: nKeyCode = GDK_space; break;
case KEY_INSERT: nKeyCode = GDK_Insert; break;
case KEY_DELETE: nKeyCode = GDK_Delete; break;
case KEY_ADD: nKeyCode = GDK_plus; break;
case KEY_SUBTRACT: nKeyCode = GDK_minus; break;
case KEY_MULTIPLY: nKeyCode = GDK_asterisk; break;
case KEY_DIVIDE: nKeyCode = GDK_slash; break;
case KEY_POINT: nKeyCode = GDK_period; break;
case KEY_COMMA: nKeyCode = GDK_comma; break;
case KEY_LESS: nKeyCode = GDK_less; break;
case KEY_GREATER: nKeyCode = GDK_greater; break;
case KEY_EQUAL: nKeyCode = GDK_equal; break;
case KEY_FIND: nKeyCode = GDK_Find; break;
case KEY_CONTEXTMENU: nKeyCode = GDK_Menu; break;
case KEY_HELP: nKeyCode = GDK_Help; break;
case KEY_UNDO: nKeyCode = GDK_Undo; break;
case KEY_REPEAT: nKeyCode = GDK_Redo; break;
case KEY_DECIMAL: nKeyCode = GDK_KP_Decimal; break;
case KEY_TILDE: nKeyCode = GDK_asciitilde; break;
case KEY_QUOTELEFT: nKeyCode = GDK_quoteleft; break;
case KEY_BRACKETLEFT: nKeyCode = GDK_bracketleft; break;
case KEY_BRACKETRIGHT: nKeyCode = GDK_bracketright; break;
case KEY_SEMICOLON: nKeyCode = GDK_semicolon; break;
// Special cases
case KEY_COPY: nKeyCode = GDK_Copy; break;
case KEY_CUT: nKeyCode = GDK_Cut; break;
case KEY_PASTE: nKeyCode = GDK_Paste; break;
case KEY_OPEN: nKeyCode = GDK_Open; break;
}
}
*pGdkKeyCode = nKeyCode;
}
bool GtkSalMenu::PrepUpdate()
{
const GtkSalFrame* pFrame = GetFrame();
if (pFrame)
{
GtkSalFrame* pNonConstFrame = ( GtkSalFrame* ) pFrame;
GtkSalMenu* pSalMenu = ((GtkSalMenu*) this);
if ( !pNonConstFrame->GetMenu() )
pNonConstFrame->SetMenu( pSalMenu );
if ( bMenuVisibility && mpMenuModel && mpActionGroup )
return true;
}
return false;
}
/*
* Menu updating methods
*/
void RemoveSpareItemsFromNativeMenu( GLOMenu* pMenu, GList** pOldCommandList, unsigned nSection, unsigned nValidItems )
{
sal_Int32 nSectionItems = g_lo_menu_get_n_items_from_section( pMenu, nSection );
while ( nSectionItems > (sal_Int32) nValidItems )
{
gchar* aCommand = g_lo_menu_get_command_from_item_in_section( pMenu, nSection, --nSectionItems );
if ( aCommand != NULL && pOldCommandList != NULL )
*pOldCommandList = g_list_append( *pOldCommandList, g_strdup( aCommand ) );
g_free( aCommand );
g_lo_menu_remove_from_section( pMenu, nSection, nSectionItems );
}
}
void RemoveSpareSectionsFromNativeMenu( GLOMenu* pMenu, GList** pOldCommandList, unsigned nLastSection )
{
if ( pMenu == NULL || pOldCommandList == NULL )
return;
sal_Int32 n = g_menu_model_get_n_items( G_MENU_MODEL( pMenu ) ) - 1;
for ( ; n > (sal_Int32) nLastSection; n-- )
{
RemoveSpareItemsFromNativeMenu( pMenu, pOldCommandList, n, 0 );
g_lo_menu_remove( pMenu, n );
}
}
gint CompareStr( gpointer str1, gpointer str2 )
{
return g_strcmp0( (const gchar*) str1, (const gchar*) str2 );
}
void RemoveUnusedCommands( GLOActionGroup* pActionGroup, GList* pOldCommandList, GList* pNewCommandList )
{
if ( pActionGroup == NULL || pOldCommandList == NULL )
return;
while ( pNewCommandList != NULL )
{
GList* pNewCommand = g_list_first( pNewCommandList );
pNewCommandList = g_list_remove_link( pNewCommandList, pNewCommand );
gpointer aCommand = g_list_nth_data( pNewCommand, 0 );
GList* pOldCommand = g_list_find_custom( pOldCommandList, aCommand, (GCompareFunc) CompareStr );
if ( pOldCommand != NULL )
{
pOldCommandList = g_list_remove_link( pOldCommandList, pOldCommand );
g_list_free_full( pOldCommand, g_free );
}
g_list_free_full( pNewCommand, g_free );
}
while ( pOldCommandList != NULL )
{
GList* pCommand = g_list_first( pOldCommandList );
pOldCommandList = g_list_remove_link( pOldCommandList, pCommand );
gchar* aCommand = (gchar*) g_list_nth_data( pCommand, 0 );
g_lo_action_group_remove( pActionGroup, aCommand );
g_list_free_full( pCommand, g_free );
}
}
void GtkSalMenu::UpdateNativeMenu()
{
SolarMutexGuard aGuard;
//GTK_YIELD_GRAB();
if( !PrepUpdate() )
return;
Menu* pVCLMenu = mpVCLMenu;
GLOMenu* pLOMenu = G_LO_MENU( mpMenuModel );
GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( mpActionGroup );
GList *pOldCommandList = NULL;
GList *pNewCommandList = NULL;
sal_uInt16 nLOMenuSize = g_menu_model_get_n_items( G_MENU_MODEL( pLOMenu ) );
if ( nLOMenuSize == 0 )
g_lo_menu_new_section( pLOMenu, 0, NULL );
sal_Int32 nSection = 0;
sal_Int32 nItemPos = 0;
sal_Int32 validItems = 0;
sal_Int32 nItem;
for ( nItem = 0; nItem < ( sal_Int32 ) GetItemCount(); nItem++ ) {
if ( IsItemVisible( nItem ) == sal_False )
continue;
GtkSalMenuItem *pSalMenuItem = GetItemAtPos( nItem );
sal_uInt16 nId = pSalMenuItem->mnId;
if ( pSalMenuItem->mnType == MENUITEM_SEPARATOR )
{
// Delete extra items from current section.
RemoveSpareItemsFromNativeMenu( pLOMenu, &pOldCommandList, nSection, validItems );
nSection++;
nItemPos = 0;
validItems = 0;
if ( nLOMenuSize <= nSection )
{
g_lo_menu_new_section( pLOMenu, nSection, NULL );
nLOMenuSize++;
}
continue;
}
if ( nItemPos >= g_lo_menu_get_n_items_from_section( pLOMenu, nSection ) )
g_lo_menu_insert_in_section( pLOMenu, nSection, nItemPos, "EMPTY STRING" );
// Get internal menu item values.
String aText = pVCLMenu->GetItemText( nId );
rtl::OUString aCommand( pVCLMenu->GetItemCommand( nId ) );
sal_Bool itemEnabled = pVCLMenu->IsItemEnabled( nId );
KeyCode nAccelKey = pVCLMenu->GetAccelKey( nId );
sal_Bool itemChecked = pVCLMenu->IsItemChecked( nId );
MenuItemBits itemBits = pVCLMenu->GetItemBits( nId );
// Convert internal values to native values.
gboolean bChecked = ( itemChecked == sal_True ) ? TRUE : FALSE;
gboolean bEnabled = ( itemEnabled == sal_True ) ? TRUE : FALSE;
// gchar* aNativeCommand = g_strdup( rtl::OUStringToOString( aCommand, RTL_TEXTENCODING_UTF8 ).getStr() );
gchar* aNativeCommand = GetCommandForSpecialItem( pSalMenuItem );
// Store current item command in command list.
gchar *aCurrentCommand = g_lo_menu_get_command_from_item_in_section( pLOMenu, nSection, nItemPos );
if ( aCurrentCommand != NULL )
pOldCommandList = g_list_append( pOldCommandList, aCurrentCommand );
// Force updating of native menu labels.
NativeSetItemText( nSection, nItemPos, aText );
NativeSetAccelerator( nSection, nItemPos, nAccelKey, nAccelKey.GetName( GetFrame()->GetWindow() ) );
// Some items are special, so they have different commands.
// if ( g_strcmp0( aNativeCommand, "" ) == 0 )
if ( !aNativeCommand )
{
// gchar *aSpecialItemCmd = GetCommandForSpecialItem( pSalMenuItem );
aNativeCommand = g_strdup( rtl::OUStringToOString( aCommand, RTL_TEXTENCODING_UTF8 ).getStr() );
// if ( aSpecialItemCmd != NULL )
// {
// g_free( aNativeCommand );
// aNativeCommand = aSpecialItemCmd;
// }
}
if ( g_strcmp0( aNativeCommand, "" ) != 0 && pSalMenuItem->mpSubMenu == NULL )
{
NativeSetItemCommand( nSection, nItemPos, nId, aNativeCommand, itemBits, bChecked, FALSE );
NativeCheckItem( nSection, nItemPos, itemBits, bChecked );
NativeSetEnableItem( aNativeCommand, bEnabled );
pNewCommandList = g_list_append( pNewCommandList, g_strdup( aNativeCommand ) );
}
GtkSalMenu* pSubmenu = pSalMenuItem->mpSubMenu;
if ( pSubmenu && pSubmenu->GetMenu() )
{
NativeSetItemCommand( nSection, nItemPos, nId, aNativeCommand, itemBits, FALSE, TRUE );
pNewCommandList = g_list_append( pNewCommandList, g_strdup( aNativeCommand ) );
GLOMenu* pSubMenuModel = g_lo_menu_get_submenu_from_item_in_section( pLOMenu, nSection, nItemPos );
if ( pSubMenuModel == NULL )
{
//pSubMenuModel = g_lo_menu_new();
//g_lo_menu_set_submenu_to_item_in_section( pLOMenu, nSection, nItemPos, G_MENU_MODEL( pSubMenuModel ) );
g_lo_menu_new_submenu_in_item_in_section( pLOMenu, nSection, nItemPos );
pSubMenuModel = g_lo_menu_get_submenu_from_item_in_section( pLOMenu, nSection, nItemPos );
}
g_object_unref( pSubMenuModel );
pSubmenu->SetMenuModel( G_MENU_MODEL( pSubMenuModel ) );
pSubmenu->SetActionGroup( G_ACTION_GROUP( pActionGroup ) );
pSubmenu->GetMenu()->Activate();
pSubmenu->GetMenu()->Deactivate();
pSubmenu->UpdateNativeMenu();
}
g_free( aNativeCommand );
++nItemPos;
++validItems;
}
// Delete extra items in last section.
RemoveSpareItemsFromNativeMenu( pLOMenu, &pOldCommandList, nSection, validItems );
// Delete extra sections.
RemoveSpareSectionsFromNativeMenu( pLOMenu, &pOldCommandList, nSection );
// Delete unused commands.
RemoveUnusedCommands( pActionGroup, pOldCommandList, pNewCommandList );
}
/*
* GtkSalMenu
*/
GtkSalMenu::GtkSalMenu( sal_Bool bMenuBar ) :
mbMenuBar( bMenuBar ),
mpVCLMenu( NULL ),
mpOldSalMenu( NULL ),
mpParentSalMenu( NULL ),
mpFrame( NULL ),
mpMenuModel( NULL ),
mpActionGroup( NULL )
{
}
GtkSalMenu::~GtkSalMenu()
{
SolarMutexGuard aGuard;
//GTK_YIELD_GRAB();
if ( mbMenuBar == sal_True )
{
if ( mpMenuModel )
{
// g_lo_menu_remove( G_LO_MENU( mpMenuModel ), 0 );
g_object_unref( mpMenuModel );
}
}
maItems.clear();
}
sal_Bool GtkSalMenu::VisibleMenuBar()
{
return bMenuVisibility;
}
void GtkSalMenu::InsertItem( SalMenuItem* pSalMenuItem, unsigned nPos )
{
SolarMutexGuard aGuard;
GtkSalMenuItem *pItem = static_cast<GtkSalMenuItem*>( pSalMenuItem );
if ( nPos == MENU_APPEND )
maItems.push_back( pItem );
else
maItems.insert( maItems.begin() + nPos, pItem );
pItem->mpParentMenu = this;
}
void GtkSalMenu::RemoveItem( unsigned nPos )
{
SolarMutexGuard aGuard;
maItems.erase( maItems.begin() + nPos );
}
void GtkSalMenu::SetSubMenu( SalMenuItem* pSalMenuItem, SalMenu* pSubMenu, unsigned )
{
SolarMutexGuard aGuard;
GtkSalMenuItem *pItem = static_cast< GtkSalMenuItem* >( pSalMenuItem );
GtkSalMenu *pGtkSubMenu = static_cast< GtkSalMenu* >( pSubMenu );
if ( pGtkSubMenu == NULL )
return;
pGtkSubMenu->mpParentSalMenu = this;
pItem->mpSubMenu = pGtkSubMenu;
}
void GtkSalMenu::SetFrame( const SalFrame* pFrame )
{
SolarMutexGuard aGuard;
GTK_YIELD_GRAB();
assert(mbMenuBar);
SAL_INFO("vcl.unity", "GtkSalMenu set to frame");
mpFrame = static_cast< const GtkSalFrame* >( pFrame );
GtkSalFrame* pFrameNonConst = const_cast<GtkSalFrame*>(mpFrame);
// if we had a menu on the GtkSalMenu we have to free it as we generate a
// full menu anyway and we might need to reuse an existing model and
// actiongroup
mpOldSalMenu = static_cast< GtkSalMenu* >( pFrameNonConst->GetMenu() );
pFrameNonConst->SetMenu( this );
pFrameNonConst->EnsureAppMenuWatch();
// Clean menu model and action group if needed.
GtkWidget* pWidget = pFrameNonConst->getWindow();
GdkWindow* gdkWindow = gtk_widget_get_window( pWidget );
GLOMenu* pMenuModel = G_LO_MENU( g_object_get_data( G_OBJECT( gdkWindow ), "g-lo-menubar" ) );
GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( g_object_get_data( G_OBJECT( gdkWindow ), "g-lo-action-group" ) );
if ( pMenuModel )
{
if ( g_menu_model_get_n_items( G_MENU_MODEL( pMenuModel ) ) > 0 )
g_lo_menu_remove( pMenuModel, 0 );
mpMenuModel = G_MENU_MODEL( g_lo_menu_new() );
}
if ( pActionGroup )
{
g_lo_action_group_clear( pActionGroup );
mpActionGroup = G_ACTION_GROUP( pActionGroup );
}
// Generate the main menu structure.
if (bMenuVisibility)
UpdateNativeMenu();
g_lo_menu_insert_section( pMenuModel, 0, NULL, mpMenuModel );
}
const GtkSalFrame* GtkSalMenu::GetFrame() const
{
SolarMutexGuard aGuard;
const GtkSalMenu* pMenu = this;
while( pMenu && ! pMenu->mpFrame )
pMenu = pMenu->mpParentSalMenu;
return pMenu ? pMenu->mpFrame : NULL;
}
void GtkSalMenu::NativeCheckItem( unsigned nSection, unsigned nItemPos, MenuItemBits bits, gboolean bCheck )
{
SolarMutexGuard aGuard;
if ( mpActionGroup == NULL )
return;
gchar* aCommand = g_lo_menu_get_command_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos );
if ( aCommand != NULL || g_strcmp0( aCommand, "" ) != 0 )
{
GVariant *pCheckValue = NULL;
GVariant *pCurrentState = g_action_group_get_action_state( mpActionGroup, aCommand );
if ( bits & MIB_RADIOCHECK )
pCheckValue = ( bCheck == TRUE ) ? g_variant_new_string( aCommand ) : g_variant_new_string( "" );
else
{
// By default, all checked items are checkmark buttons.
if ( bCheck == TRUE || ( ( bCheck == FALSE ) && pCurrentState != NULL ) )
pCheckValue = g_variant_new_boolean( bCheck );
}
if ( pCheckValue != NULL && ( pCurrentState == NULL || g_variant_equal( pCurrentState, pCheckValue ) == FALSE ) )
g_action_group_change_action_state( mpActionGroup, aCommand, pCheckValue );
if ( pCurrentState != NULL )
g_variant_unref( pCurrentState );
}
if ( aCommand )
g_free( aCommand );
}
void GtkSalMenu::NativeSetEnableItem( gchar* aCommand, gboolean bEnable )
{
SolarMutexGuard aGuard;
GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( mpActionGroup );
if ( g_action_group_get_action_enabled( G_ACTION_GROUP( pActionGroup ), aCommand ) != bEnable )
g_lo_action_group_set_action_enabled( pActionGroup, aCommand, bEnable );
}
void GtkSalMenu::NativeSetItemText( unsigned nSection, unsigned nItemPos, const rtl::OUString& rText )
{
SolarMutexGuard aGuard;
// Replace the '~' character with '_'.
rtl::OUString aText = rText.replace( '~', '_' );
rtl::OString aConvertedText = OUStringToOString( aText, RTL_TEXTENCODING_UTF8 );
// Update item text only when necessary.
gchar* aLabel = g_lo_menu_get_label_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos );
if ( aLabel == NULL || g_strcmp0( aLabel, aConvertedText.getStr() ) != 0 )
g_lo_menu_set_label_to_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos, aConvertedText.getStr() );
if ( aLabel )
g_free( aLabel );
}
void GtkSalMenu::NativeSetAccelerator( unsigned nSection, unsigned nItemPos, const KeyCode& rKeyCode, const rtl::OUString& rKeyName )
{
SolarMutexGuard aGuard;
if ( rKeyName.isEmpty() )
return;
guint nKeyCode;
GdkModifierType nModifiers;
KeyCodeToGdkKey( rKeyCode, &nKeyCode, &nModifiers );
gchar* aAccelerator = gtk_accelerator_name( nKeyCode, nModifiers );
gchar* aCurrentAccel = g_lo_menu_get_accelerator_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos );
if ( aCurrentAccel == NULL && g_strcmp0( aCurrentAccel, aAccelerator ) != 0 )
g_lo_menu_set_accelerator_to_item_in_section ( G_LO_MENU( mpMenuModel ), nSection, nItemPos, aAccelerator );
g_free( aAccelerator );
}
void GtkSalMenu::NativeSetItemCommand( unsigned nSection,
unsigned nItemPos,
sal_uInt16 nId,
const gchar* aCommand,
MenuItemBits nBits,
gboolean bChecked,
gboolean bIsSubmenu )
{
SolarMutexGuard aGuard;
GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( mpActionGroup );
GVariant *pTarget = NULL;
if ( g_action_group_has_action( mpActionGroup, aCommand ) == FALSE ) {
if ( ( nBits & MIB_CHECKABLE ) || ( bIsSubmenu == TRUE ) )
{
// Item is a checkmark button.
GVariantType* pStateType = g_variant_type_new( (gchar*) G_VARIANT_TYPE_BOOLEAN );
GVariant* pState = g_variant_new_boolean( bChecked );
g_lo_action_group_insert_stateful( pActionGroup, aCommand, nId, bIsSubmenu, NULL, pStateType, NULL, pState );
}
else if ( nBits & MIB_RADIOCHECK )
{
// Item is a radio button.
GVariantType* pParameterType = g_variant_type_new( (gchar*) G_VARIANT_TYPE_STRING );
GVariantType* pStateType = g_variant_type_new( (gchar*) G_VARIANT_TYPE_STRING );
GVariant* pState = g_variant_new_string( "" );
pTarget = g_variant_new_string( aCommand );
g_lo_action_group_insert_stateful( pActionGroup, aCommand, nId, FALSE, pParameterType, pStateType, NULL, pState );
}
else
{
// Item is not special, so insert a stateless action.
g_lo_action_group_insert( pActionGroup, aCommand, nId, FALSE );
}
}
GLOMenu* pMenu = G_LO_MENU( mpMenuModel );
// Menu item is not updated unless it's necessary.
gchar* aCurrentCommand = g_lo_menu_get_command_from_item_in_section( pMenu, nSection, nItemPos );
if ( aCurrentCommand == NULL || g_strcmp0( aCurrentCommand, aCommand ) != 0 )
{
g_lo_menu_set_command_to_item_in_section( pMenu, nSection, nItemPos, aCommand );
gchar* aItemCommand = g_strconcat("win.", aCommand, NULL );
if ( bIsSubmenu == TRUE )
g_lo_menu_set_submenu_action_to_item_in_section( pMenu, nSection, nItemPos, aItemCommand );
else
g_lo_menu_set_action_and_target_value_to_item_in_section( pMenu, nSection, nItemPos, aItemCommand, pTarget );
g_free( aItemCommand );
}
if ( aCurrentCommand )
g_free( aCurrentCommand );
}
GtkSalMenu* GtkSalMenu::GetMenuForItemCommand( gchar* aCommand, gboolean bGetSubmenu )
{
SolarMutexGuard aGuard;
GtkSalMenu* pMenu = NULL;
for ( sal_uInt16 nPos = 0; nPos < maItems.size(); nPos++ )
{
GtkSalMenuItem *pSalItem = maItems[ nPos ];
String aItemCommand = mpVCLMenu->GetItemCommand( pSalItem->mnId );
gchar* aItemCommandStr = (gchar*) rtl::OUStringToOString( aItemCommand, RTL_TEXTENCODING_UTF8 ).getStr();
if ( g_strcmp0( aItemCommandStr, aCommand ) == 0 )
{
pMenu = ( bGetSubmenu == TRUE ) ? pSalItem->mpSubMenu : this;
break;
}
else
{
if ( pSalItem->mpSubMenu != NULL )
pMenu = pSalItem->mpSubMenu->GetMenuForItemCommand( aCommand, bGetSubmenu );
if ( pMenu != NULL )
break;
}
}
return pMenu;
}
void GtkSalMenu::DispatchCommand( gint itemId, const gchar *aCommand )
{
SolarMutexGuard aGuard;
// Only the menubar is allowed to dispatch commands.
if ( mbMenuBar != TRUE )
return;
GtkSalMenu* pSalSubMenu = GetMenuForItemCommand( (gchar*) aCommand, FALSE );
Menu* pSubMenu = ( pSalSubMenu != NULL ) ? pSalSubMenu->GetMenu() : NULL;
MenuBar* pMenuBar = static_cast< MenuBar* >( mpVCLMenu );
pMenuBar->HandleMenuCommandEvent( pSubMenu, itemId );
}
void GtkSalMenu::Activate( const gchar* aMenuCommand )
{
if ( mbMenuBar != TRUE )
return;
GtkSalMenu* pSalSubMenu = GetMenuForItemCommand( (gchar*) aMenuCommand, TRUE );
if ( pSalSubMenu != NULL ) {
MenuBar* pMenuBar = static_cast< MenuBar* >( mpVCLMenu );
pMenuBar->HandleMenuActivateEvent( pSalSubMenu->mpVCLMenu );
pSalSubMenu->UpdateNativeMenu();
}
}
void GtkSalMenu::Deactivate( const gchar* aMenuCommand )
{
if ( mbMenuBar == sal_False )
return;
GtkSalMenu* pSalSubMenu = GetMenuForItemCommand( (gchar*) aMenuCommand, TRUE );
if ( pSalSubMenu != NULL ) {
MenuBar* pMenuBar = static_cast< MenuBar* >( mpVCLMenu );
pMenuBar->HandleMenuDeActivateEvent( pSalSubMenu->mpVCLMenu );
}
}
void GtkSalMenu::Display( sal_Bool bVisible )
{
if ( mbMenuBar == sal_False || mpVCLMenu == NULL )
return;
bMenuVisibility = bVisible;
sal_Bool bVCLMenuVisible = ( bVisible == sal_True ) ? sal_False : sal_True;
MenuBar* pMenuBar = static_cast< MenuBar* >( mpVCLMenu );
pMenuBar->SetDisplayable( bVCLMenuVisible );
}
sal_Bool GtkSalMenu::IsItemVisible( unsigned nPos )
{
SolarMutexGuard aGuard;
sal_Bool bVisible = sal_False;
if ( nPos < maItems.size() )
bVisible = ( ( GtkSalMenuItem* ) maItems[ nPos ])->mbVisible;
return bVisible;
}
void GtkSalMenu::CheckItem( unsigned, sal_Bool )
{
}
void GtkSalMenu::EnableItem( unsigned, sal_Bool )
{
}
void GtkSalMenu::ShowItem( unsigned nPos, sal_Bool bShow )
{
SolarMutexGuard aGuard;
if ( nPos < maItems.size() )
( ( GtkSalMenuItem* ) maItems[ nPos ] )->mbVisible = bShow;
}
void GtkSalMenu::SetItemText( unsigned, SalMenuItem*, const rtl::OUString& )
{
}
void GtkSalMenu::SetItemImage( unsigned, SalMenuItem*, const Image& )
{
}
void GtkSalMenu::SetAccelerator( unsigned, SalMenuItem*, const KeyCode&, const rtl::OUString& )
{
}
void GtkSalMenu::SetItemCommand( unsigned, SalMenuItem*, const rtl::OUString& )
{
}
void GtkSalMenu::GetSystemMenuData( SystemMenuData* )
{
}
void GtkSalMenu::Freeze()
{
}
// =======================================================================
/*
* GtkSalMenuItem
*/
GtkSalMenuItem::GtkSalMenuItem( const SalItemParams* pItemData ) :
mnId( pItemData->nId ),
mnType( pItemData->eType ),
mbVisible( sal_True ),
mpVCLMenu( pItemData->pMenu ),
mpParentMenu( NULL ),
mpSubMenu( NULL )
{
}
GtkSalMenuItem::~GtkSalMenuItem()
{
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|