~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/monomac/samples/Preferences/Preferences/PreferencesWindowController.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Linq;
 
4
using MonoMac.Foundation;
 
5
using MonoMac.AppKit;
 
6
using System.Drawing;
 
7
 
 
8
namespace Preferences
 
9
{
 
10
        public partial class PreferencesWindowController : NSWindowController
 
11
        {
 
12
                #region Constructors
 
13
                
 
14
                // Called when created from unmanaged code
 
15
                public PreferencesWindowController (IntPtr handle) : base (handle)
 
16
                {
 
17
                        Initialize ();
 
18
                }
 
19
                
 
20
                // Called when created directly from a XIB file
 
21
                [Export ("initWithCoder:")]
 
22
                public PreferencesWindowController (NSCoder coder) : base (coder)
 
23
                {
 
24
                        Initialize ();
 
25
                }
 
26
                
 
27
                // Call to load from the XIB/NIB file
 
28
                public PreferencesWindowController () : base ("PreferencesWindow")
 
29
                {
 
30
                        Initialize ();
 
31
                }
 
32
                
 
33
                // Shared initialization code
 
34
                void Initialize ()
 
35
                {
 
36
                }
 
37
                
 
38
                #endregion
 
39
 
 
40
                List<IPreferencesTab> tabs = new List<IPreferencesTab>();
 
41
                PreferencesToolbarDelegate toolbarDelegate;
 
42
 
 
43
                // When Preference Window is loaded from a NIB we create a view controller
 
44
                // for each tab in preferences window and initialize the toolbar.
 
45
                public override void AwakeFromNib()
 
46
                {
 
47
                        base.AwakeFromNib();
 
48
                        
 
49
                        tabs.Add(new GeneralViewController());
 
50
                        tabs.Add(new FontsAndColorsViewController());
 
51
                        tabs.Add(new PrivacyViewController());
 
52
 
 
53
                        InitializeToolbar();
 
54
                }
 
55
                
 
56
                void InitializeToolbar()
 
57
                {
 
58
                        toolbarDelegate = new PreferencesToolbarDelegate(tabs);
 
59
                        toolbarDelegate.SelectionChanged += HandleSelectionChanged;
 
60
 
 
61
                        Window.Toolbar = CreateToolbar ();
 
62
                        
 
63
                        HandleSelectionChanged(this, null); // Called once when the window is created to make first tab visible
 
64
                }
 
65
 
 
66
                NSToolbar CreateToolbar ()
 
67
                {
 
68
                        var tb = new NSToolbar ("PreferencesToolbar");
 
69
                        tb.AllowsUserCustomization = false;
 
70
                        tb.Delegate = toolbarDelegate;
 
71
                        tb.SelectedItemIdentifier = tabs.First().Name;
 
72
                        return tb;
 
73
                }
 
74
 
 
75
                // Called when user clicks on toolbar item to change the preferences tab.
 
76
                void HandleSelectionChanged (object sender, EventArgs e)
 
77
                {
 
78
                        var selectedTab = tabs.Single(s => s.Name.Equals(Window.Toolbar.SelectedItemIdentifier));
 
79
                        Window.Title = selectedTab.Name;
 
80
                        ShowSelectedTab(selectedTab);
 
81
                }
 
82
 
 
83
                // Change preferences tab view to selected one. Animate resizing of the window if the selected
 
84
                // tab is different size than the currently visible one.
 
85
                void ShowSelectedTab(IPreferencesTab selectedTab)
 
86
                {
 
87
                        float delta = Window.ContentView.Frame.Height - selectedTab.View.Frame.Height; // Delta must be calculated before currect tab view is removed
 
88
                        RemoveCurrentTabView();
 
89
                        Window.SetFrame(CalculateNewFrameForWindow (delta), true, true);
 
90
                        Window.ContentView.AddSubview(selectedTab.View);
 
91
                }
 
92
                
 
93
                void RemoveCurrentTabView()
 
94
                {
 
95
                        if (Window.ContentView.Subviews.Any())
 
96
                                Window.ContentView.Subviews.Single().RemoveFromSuperview();
 
97
                }
 
98
 
 
99
                RectangleF CalculateNewFrameForWindow (float delta)
 
100
                {
 
101
                        return new RectangleF (Window.Frame.X, Window.Frame.Y + delta, Window.Frame.Width, Window.Frame.Height - delta);
 
102
                }
 
103
        }
 
104
}
 
105