~ubuntu-mozilla-daily/thunderbird/thunderbird-3.1.head.daily

« back to all changes in this revision

Viewing changes to debian/globalmenu/components/src/uGlobalMenuItem.cpp

  • Committer: Fabien Tassin
  • Date: 2011-04-07 03:36:13 UTC
  • mfrom: (0.1.264 3.1)
  • Revision ID: fta@ubuntu.com-20110407033613-9f6x8hnfv2kwduug
* Merge with thunderbird-3.1.head #264

Show diffs side-by-side

added added

removed removed

Lines of Context:
361
361
PRUint32
362
362
uGlobalMenuItem::MozKeyCodeToGdkKeyCode(PRUint32 aMozKeyCode)
363
363
{
364
 
  if(aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_A &&
365
 
     aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_Z)
366
 
    return aMozKeyCode;
367
 
 
368
 
  if(aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_0 &&
369
 
     aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_9)
370
 
    return aMozKeyCode;
371
 
 
372
 
  if(aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_NUMPAD0 &&
373
 
     aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_NUMPAD9)
 
364
  if (aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_A &&
 
365
      aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_Z)
 
366
    return aMozKeyCode;
 
367
 
 
368
  if (aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_0 &&
 
369
      aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_9)
 
370
    return aMozKeyCode;
 
371
 
 
372
  if (aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_NUMPAD0 &&
 
373
      aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_NUMPAD9)
374
374
    return aMozKeyCode - nsIDOMKeyEvent::DOM_VK_NUMPAD0 + GDK_0;
375
375
 
376
 
  if(aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_F1 &&
377
 
     aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_F24)
 
376
  if (aMozKeyCode >= nsIDOMKeyEvent::DOM_VK_F1 &&
 
377
      aMozKeyCode <= nsIDOMKeyEvent::DOM_VK_F24)
378
378
    return aMozKeyCode - nsIDOMKeyEvent::DOM_VK_F1 + GDK_KEY_F1;
379
379
 
380
 
  for(PRUint32 i = 0; i < NS_ARRAY_LENGTH(nsKeycodes); i++) {
381
 
    if(nsKeycodes[i].vkCode == aMozKeyCode)
 
380
  for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(nsKeycodes); i++) {
 
381
    if (nsKeycodes[i].vkCode == aMozKeyCode)
382
382
      return nsKeycodes[i].keysym;
383
383
  }
384
384
 
393
393
 
394
394
  PRUint32 modifier = 0;
395
395
 
396
 
  if(!modStr.IsEmpty()) {
 
396
  if (!modStr.IsEmpty()) {
397
397
    char* str = ToNewUTF8String(modStr);
398
398
    char *token = strtok(str, ", \t");
399
399
    while(token) {
400
 
      if(strcmp(token, "shift") == 0) {
 
400
      if (strcmp(token, "shift") == 0) {
401
401
        modifier |= GDK_SHIFT_MASK;
402
 
      } else if(strcmp(token, "alt") == 0) {
 
402
      } else if (strcmp(token, "alt") == 0) {
403
403
        modifier |= GDK_MOD1_MASK;
404
 
      } else if(strcmp(token, "meta") == 0) {
 
404
      } else if (strcmp(token, "meta") == 0) {
405
405
        modifier |= GDK_META_MASK;
406
 
      } else if(strcmp(token, "control") == 0) {
 
406
      } else if (strcmp(token, "control") == 0) {
407
407
        modifier |= GDK_CONTROL_MASK;
408
 
      } else if(strcmp(token, "accel") == 0) {
 
408
      } else if (strcmp(token, "accel") == 0) {
409
409
        nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
410
 
        if(prefs) {
 
410
        if (prefs) {
411
411
          PRInt32 accel;
412
412
          prefs->GetIntPref("ui.key.accelKey", &accel);
413
 
          if(accel == nsIDOMKeyEvent::DOM_VK_META) {
 
413
          if (accel == nsIDOMKeyEvent::DOM_VK_META) {
414
414
            modifier |= GDK_META_MASK;
415
 
          } else if(accel == nsIDOMKeyEvent::DOM_VK_ALT) {
 
415
          } else if (accel == nsIDOMKeyEvent::DOM_VK_ALT) {
416
416
            modifier |= GDK_MOD1_MASK;
417
417
          } else {
418
418
            modifier |= GDK_CONTROL_MASK;
436
436
  nsCAutoString cKeyStr;
437
437
  CopyUTF16toUTF8(keyStr, cKeyStr);
438
438
 
439
 
  if(!cKeyStr.IsEmpty()) {
 
439
  if (!cKeyStr.IsEmpty()) {
440
440
    key = gdk_keyval_from_name(cKeyStr.get());
441
441
  }
442
442
 
446
446
 
447
447
  if (key == 0) {
448
448
    mKeyContent->GetAttr(kNameSpaceID_None, uWidgetAtoms::keycode, keyStr);
449
 
    if(!keyStr.IsEmpty())
 
449
    if (!keyStr.IsEmpty())
450
450
      key = MozKeyCodeToGdkKeyCode(GetKeyCode(keyStr));
451
451
  }
452
452
 
454
454
    key = GDK_VoidSymbol;
455
455
  }
456
456
 
457
 
  if(key != GDK_VoidSymbol) {
 
457
  if (key != GDK_VoidSymbol) {
458
458
    dbusmenu_menuitem_property_set_shortcut(mDbusMenuItem, key,
459
459
                                       static_cast<GdkModifierType>(modifier));
460
460
  } else {
472
472
                                           uWidgetAtoms::type,
473
473
                                           attrs, eCaseMatters);
474
474
 
475
 
  if(type >= 0 && type < 2) {
476
 
    if(type == 0) {
 
475
  if (type >= 0 && type < 2) {
 
476
    if (type == 0) {
477
477
      dbusmenu_menuitem_property_set(mDbusMenuItem,
478
478
                                     DBUSMENU_MENUITEM_PROP_TOGGLE_TYPE,
479
479
                                     DBUSMENU_MENUITEM_TOGGLE_CHECK);
480
480
      mType = CheckBox;
481
 
    } else if(type == 1) {
 
481
    } else if (type == 1) {
482
482
      dbusmenu_menuitem_property_set(mDbusMenuItem,
483
483
                                     DBUSMENU_MENUITEM_PROP_TOGGLE_TYPE,
484
484
                                     DBUSMENU_MENUITEM_TOGGLE_RADIO);
497
497
                                       DBUSMENU_MENUITEM_TOGGLE_STATE_CHECKED : 
498
498
                                        DBUSMENU_MENUITEM_TOGGLE_STATE_UNCHECKED);
499
499
 
500
 
    if(mCommandContent && lastToggleState != mToggleState) {
 
500
    if (mCommandContent && lastToggleState != mToggleState) {
501
501
      mContent->SetAttr(kNameSpaceID_None, uWidgetAtoms::checked,
502
502
                        mToggleState ? NS_LITERAL_STRING("true") :
503
503
                         NS_LITERAL_STRING("false"), PR_TRUE);
518
518
uGlobalMenuItem::SyncProperties()
519
519
{
520
520
  SyncVisibilityFromContent();
521
 
  if(mCommandContent) {
522
 
    SyncSensitivityFromContent(mCommandContent);
523
 
  } else {
524
 
    SyncSensitivityFromContent();
525
 
  }
526
 
  SyncLabelFromContent();
 
521
  SyncSensitivityFromContent(mCommandContent);
 
522
  SyncLabelFromContent(mCommandContent);
527
523
  SyncTypeAndStateFromContent();
528
 
  if(mKeyContent) {
 
524
  if (mKeyContent) {
529
525
    SyncAccelFromContent();
530
526
  } else {
531
527
    dbusmenu_menuitem_property_remove(mDbusMenuItem,
556
552
 
557
553
  // XXX: it is important to update the checkbox state before dispatching
558
554
  //      the event, as the handler might check the new state
559
 
  if(!mContent->AttrValueIs(kNameSpaceID_None, uWidgetAtoms::autocheck,
560
 
                           uWidgetAtoms::_false, eCaseMatters) && 
561
 
     mType != Normal) {
 
555
  if (!mContent->AttrValueIs(kNameSpaceID_None, uWidgetAtoms::autocheck,
 
556
                             uWidgetAtoms::_false, eCaseMatters) && 
 
557
      mType != Normal) {
562
558
    if (!mToggleState) {
563
559
      // We're currently not checked, so check now
564
560
      mContent->SetAttr(kNameSpaceID_None, uWidgetAtoms::checked,
570
566
  }
571
567
 
572
568
  nsIDocument *doc = mContent->GetOwnerDoc();
573
 
  if(doc) {
 
569
  if (doc) {
574
570
    nsCOMPtr<nsIDOMDocumentEvent> docEvent = do_QueryInterface(doc);
575
 
    if(docEvent) {
 
571
    if (docEvent) {
576
572
      nsCOMPtr<nsIDOMEvent> event;
577
573
      docEvent->CreateEvent(NS_LITERAL_STRING("xulcommandevent"),
578
574
                            getter_AddRefs(event));
579
 
      if(event) {
 
575
      if (event) {
580
576
        nsCOMPtr<nsIDOMXULCommandEvent> cmdEvent = do_QueryInterface(event);
581
 
        if(cmdEvent) {
 
577
        if (cmdEvent) {
582
578
          nsCOMPtr<nsIDOMDocumentView> domDocView = do_QueryInterface(doc);
583
579
          nsCOMPtr<nsIDOMAbstractView> window;
584
580
          domDocView->GetDefaultView(getter_AddRefs(window));
585
 
          if(window) {
 
581
          if (window) {
586
582
            cmdEvent->InitCommandEvent(NS_LITERAL_STRING("command"),
587
583
                                       PR_TRUE, PR_TRUE, window, 0,
588
584
                                       PR_FALSE, PR_FALSE, PR_FALSE,
589
585
                                       PR_FALSE, nsnull);
590
586
            nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mContent);
591
 
            if(target) {
 
587
            if (target) {
592
588
              nsCOMPtr<nsIPrivateDOMEvent> priv = do_QueryInterface(event);
593
 
              if(priv) {
 
589
              if (priv) {
594
590
                priv->SetTrusted(PR_TRUE);
595
591
              }
596
592
              PRBool dummy;
601
597
      }
602
598
    }
603
599
  }
604
 
 
605
 
  if(mParent->GetType() == Menu) {
606
 
    // XXX: Is this ever false really? I guess someone could add a menuitem
607
 
    //      to the menubar
608
 
    uGlobalMenu *parent = static_cast<uGlobalMenu *>(mParent);
609
 
    parent->OnClose(PR_TRUE);
610
 
  }
611
600
}
612
601
 
613
602
nsresult
614
603
uGlobalMenuItem::ConstructDbusMenuItem()
615
604
{
616
605
  mDbusMenuItem = dbusmenu_menuitem_new();
617
 
  if(!mDbusMenuItem)
 
606
  if (!mDbusMenuItem)
618
607
    return NS_ERROR_OUT_OF_MEMORY;
619
608
 
620
609
  mHandlerID = g_signal_connect(G_OBJECT(mDbusMenuItem),
644
633
  mMenuBar = aMenuBar;
645
634
 
646
635
  nsIDocument *doc = mContent->GetCurrentDoc();
647
 
  if(doc) {
 
636
  if (doc) {
648
637
    nsAutoString attr;
649
638
    mContent->GetAttr(kNameSpaceID_None, uWidgetAtoms::command, attr);
650
639
#ifdef MOZILLA_1_9_2_BRANCH
651
640
    nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(doc));
652
641
    nsCOMPtr<nsIDOMElement> domElmt;
653
642
#endif
654
 
    if(!attr.IsEmpty()) {
 
643
    if (!attr.IsEmpty()) {
655
644
#ifdef MOZILLA_1_9_2_BRANCH
656
645
      if (domDoc) {
657
646
        domDoc->GetElementById(attr, getter_AddRefs(domElmt));
664
653
    }
665
654
 
666
655
    mContent->GetAttr(kNameSpaceID_None, uWidgetAtoms::key, attr);
667
 
    if(!attr.IsEmpty()) {
 
656
    if (!attr.IsEmpty()) {
668
657
#ifdef MOZILLA_1_9_2_BRANCH
669
658
      if (domDoc) {
670
659
        domDoc->GetElementById(attr, getter_AddRefs(domElmt));
677
666
    }
678
667
  }
679
668
 
680
 
  nsresult rv = ConstructDbusMenuItem();
681
 
  NS_ENSURE_SUCCESS(rv, rv);
682
 
 
683
669
  mListener->RegisterForContentChanges(mContent, this);
684
 
  if(mCommandContent) {
 
670
  if (mCommandContent) {
685
671
    mListener->RegisterForContentChanges(mCommandContent, this);
686
672
  }
687
 
  if(mKeyContent) {
 
673
  if (mKeyContent) {
688
674
    mListener->RegisterForContentChanges(mKeyContent, this);
689
675
  }
690
676
 
 
677
  nsresult rv = ConstructDbusMenuItem();
 
678
  NS_ENSURE_SUCCESS(rv, rv);
 
679
 
691
680
  return NS_OK;
692
681
}
693
682
 
727
716
uGlobalMenuItem::~uGlobalMenuItem()
728
717
{
729
718
  mListener->UnregisterForContentChanges(mContent);
730
 
  if(mCommandContent) {
 
719
  if (mCommandContent) {
731
720
    mListener->UnregisterForContentChanges(mCommandContent);
732
721
  }
733
 
  if(mKeyContent) {
 
722
  if (mKeyContent) {
734
723
    mListener->UnregisterForContentChanges(mKeyContent);
735
724
  }
736
725
 
737
726
  DestroyIconLoader();
738
727
 
739
 
  if(mDbusMenuItem) {
 
728
  if (mDbusMenuItem) {
740
729
    g_signal_handler_disconnect(mDbusMenuItem, mHandlerID);
741
730
    g_object_unref(mDbusMenuItem);
742
731
  }
772
761
 
773
762
  nsIDocument *doc = mContent->GetCurrentDoc();
774
763
 
775
 
  if(aAttribute == uWidgetAtoms::command && doc && aContent == mContent) {
776
 
    if(mCommandContent) {
 
764
  if (aAttribute == uWidgetAtoms::command && doc && aContent == mContent) {
 
765
    if (mCommandContent) {
777
766
      mListener->UnregisterForContentChanges(mCommandContent);
778
767
    }
779
768
    nsAutoString command;
780
769
    mContent->GetAttr(kNameSpaceID_None, uWidgetAtoms::command, command);
781
 
    if(!command.IsEmpty()) {
 
770
    if (!command.IsEmpty()) {
782
771
#ifdef MOZILLA_1_9_2_BRANCH
783
772
      nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(doc));
784
773
      nsCOMPtr<nsIDOMElement> domElmt;
791
780
#else
792
781
      mCommandContent = doc->GetElementById(command);
793
782
#endif
794
 
      if(mCommandContent) {
 
783
      if (mCommandContent) {
795
784
        mListener->RegisterForContentChanges(mCommandContent, this);
796
785
      }
797
786
    } else {
798
787
      mCommandContent = nsnull;
799
788
    }
800
789
    SyncProperties();
801
 
  } else if(aAttribute == uWidgetAtoms::key && doc) {
802
 
    if(mKeyContent) {
 
790
  } else if (aAttribute == uWidgetAtoms::key && doc && aContent == mContent) {
 
791
    if (mKeyContent) {
803
792
      mListener->UnregisterForContentChanges(mKeyContent);
804
793
    }
805
794
    nsAutoString key;
806
795
    mContent->GetAttr(kNameSpaceID_None, uWidgetAtoms::key, key);
807
 
    if(!key.IsEmpty()) {
 
796
    if (!key.IsEmpty()) {
808
797
#ifdef MOZILLA_1_9_2_BRANCH
809
798
      nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(doc));
810
799
      nsCOMPtr<nsIDOMElement> domElmt;
817
806
#else
818
807
      mKeyContent = doc->GetElementById(key);
819
808
#endif
820
 
      if(mKeyContent) {
 
809
      if (mKeyContent) {
821
810
        mListener->RegisterForContentChanges(mKeyContent, this);
822
811
      }
823
812
    } else {
824
813
      mKeyContent = nsnull;
825
814
    }
826
815
    SyncProperties();
827
 
  } else if(aAttribute == uWidgetAtoms::label ||
828
 
            aAttribute == uWidgetAtoms::hidden ||
829
 
            aAttribute == uWidgetAtoms::disabled ||
830
 
            aAttribute == uWidgetAtoms::key ||
831
 
            aAttribute == uWidgetAtoms::keycode ||
832
 
            aAttribute == uWidgetAtoms::modifiers ||
833
 
            aAttribute == uWidgetAtoms::checked ||
834
 
            aAttribute == uWidgetAtoms::type ||
835
 
            aAttribute == uWidgetAtoms::image ||
836
 
            aAttribute == uWidgetAtoms::_class) {
837
 
    SyncProperties();
838
 
 
839
 
    if (aAttribute == uWidgetAtoms::checked &&
840
 
        mContent->AttrValueIs(kNameSpaceID_None, uWidgetAtoms::checked,
841
 
          uWidgetAtoms::_true, eCaseMatters)) {
 
816
  } else if (aAttribute == uWidgetAtoms::label ||
 
817
             aAttribute == uWidgetAtoms::accesskey) {
 
818
    SyncLabelFromContent(mCommandContent);
 
819
  } else if (aAttribute == uWidgetAtoms::hidden) {
 
820
    SyncVisibilityFromContent();
 
821
  } else if (aAttribute == uWidgetAtoms::disabled) {
 
822
    SyncSensitivityFromContent(mCommandContent);
 
823
  } else if (aAttribute == uWidgetAtoms::keycode ||
 
824
             aAttribute == uWidgetAtoms::key ||
 
825
             aAttribute == uWidgetAtoms::modifiers) {
 
826
    SyncAccelFromContent();
 
827
  } else if (aAttribute == uWidgetAtoms::checked) {
 
828
    SyncTypeAndStateFromContent();
 
829
    if (mContent->AttrValueIs(kNameSpaceID_None, uWidgetAtoms::checked,
 
830
        uWidgetAtoms::_true, eCaseMatters)) {
842
831
      UncheckSiblings();
843
832
    }
 
833
  } else if (aAttribute == uWidgetAtoms::type) {
 
834
    SyncTypeAndStateFromContent();
 
835
  } else if (aAttribute == uWidgetAtoms::image) {
 
836
    SyncIconFromContent();
 
837
  } else if (aAttribute == uWidgetAtoms::_class) {
 
838
    UpdateInfoFromContentClass();
844
839
  }
845
840
}
846
841