~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MacPlatform/Dialogs/MacAlertDialogHandler.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// MacAlertFileDialogHandler.cs
 
3
//  
 
4
// Author:
 
5
//       Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 Novell, Inc.
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Drawing;
 
29
using System.Linq;
 
30
using System.Collections.Generic;
 
31
using MonoMac.Foundation;
 
32
using MonoMac.AppKit;
 
33
 
 
34
using MonoDevelop.Core;
 
35
using MonoDevelop.Ide;
 
36
using MonoDevelop.Ide.Extensions;
 
37
using MonoDevelop.Components.Extensions;
 
38
using MonoDevelop.MacInterop;
 
39
        
 
40
namespace MonoDevelop.Platform.Mac
 
41
{
 
42
        class MacAlertDialogHandler : IAlertDialogHandler
 
43
        {
 
44
                public bool Run (AlertDialogData data)
 
45
                {
 
46
                        using (var alert = new NSAlert ()) {
 
47
                                
 
48
                                if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information) {
 
49
                                        alert.AlertStyle = NSAlertStyle.Critical;
 
50
                                } else if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Warning) {
 
51
                                        alert.AlertStyle = NSAlertStyle.Warning;
 
52
                                } else if (data.Message.Icon == MonoDevelop.Ide.Gui.Stock.Information) {
 
53
                                        alert.AlertStyle = NSAlertStyle.Informational;
 
54
                                }
 
55
                                
 
56
                                //FIXME: use correct size so we don't get horrible scaling?
 
57
                                if (!string.IsNullOrEmpty (data.Message.Icon)) {
 
58
                                        var pix = ImageService.GetPixbuf (data.Message.Icon, Gtk.IconSize.Dialog);
 
59
                                        byte[] buf = pix.SaveToBuffer ("tiff");
 
60
                                        unsafe {
 
61
                                                fixed (byte* b = buf) {
 
62
                                                        alert.Icon = new NSImage (NSData.FromBytes ((IntPtr)b, (uint)buf.Length));
 
63
                                                }
 
64
                                        }
 
65
                                }
 
66
                                
 
67
                                alert.MessageText = data.Message.Text;
 
68
                                alert.InformativeText = data.Message.SecondaryText ?? "";
 
69
                                
 
70
                                var buttons = data.Buttons.Reverse ().ToList ();
 
71
                                
 
72
                                for (int i = 0; i < buttons.Count - 1; i++) {
 
73
                                        if (i == data.Message.DefaultButton) {
 
74
                                                var next = buttons[i];
 
75
                                                for (int j = buttons.Count - 1; j >= i; j--) {
 
76
                                                        var tmp = buttons[j];
 
77
                                                        buttons[j] = next;
 
78
                                                        next = tmp;
 
79
                                                }
 
80
                                                break;
 
81
                                        }
 
82
                                }
 
83
                                
 
84
                                foreach (var button in buttons) {
 
85
                                        var label = button.Label;
 
86
                                        if (button.IsStockButton)
 
87
                                                label = Gtk.Stock.Lookup (label).Label;
 
88
                                        label = label.Replace ("_", "");
 
89
                                        
 
90
                                        //this message seems to be a standard Mac message since alert handles it specially
 
91
                                        if (button == AlertButton.CloseWithoutSave)
 
92
                                                label = GettextCatalog.GetString ("Don't Save");
 
93
                                        
 
94
                                        alert.AddButton (label);
 
95
                                }
 
96
                                
 
97
                                
 
98
                                NSButton[] optionButtons = null;
 
99
                                if (data.Options.Count > 0) {
 
100
                                        var box = new MDBox (LayoutDirection.Vertical, 2, 2);
 
101
                                        optionButtons = new NSButton[data.Options.Count];
 
102
                                        
 
103
                                        for (int i = data.Options.Count - 1; i >= 0; i--) {
 
104
                                                var option = data.Options[i];
 
105
                                                var button = new NSButton () {
 
106
                                                        Title = option.Text,
 
107
                                                        Tag = i,
 
108
                                                        State = option.Value? NSCellStateValue.On : NSCellStateValue.Off,
 
109
                                                };
 
110
                                                button.SetButtonType (NSButtonType.Switch);
 
111
                                                optionButtons[i] = button;
 
112
                                                box.Add (new MDAlignment (button, true) { XAlign = LayoutAlign.Begin });
 
113
                                        }
 
114
                                        
 
115
                                        box.Layout ();
 
116
                                        alert.AccessoryView = box.View;
 
117
                                }
 
118
                                
 
119
                                NSButton applyToAllCheck = null;
 
120
                                if (data.Message.AllowApplyToAll) {
 
121
                                        alert.ShowsSuppressionButton = true;
 
122
                                        applyToAllCheck = alert.SuppressionButton;
 
123
                                        applyToAllCheck.Title = GettextCatalog.GetString ("Apply to all");
 
124
                                }
 
125
                                
 
126
                                alert.Layout ();
 
127
                                
 
128
                                int result = alert.RunModal () - (int)NSAlertButtonReturn.First;
 
129
                                
 
130
                                data.ResultButton = buttons [result];
 
131
                                
 
132
                                if (optionButtons != null) {
 
133
                                        foreach (var button in optionButtons) {
 
134
                                                var option = data.Options[button.Tag];
 
135
                                                data.Message.SetOptionValue (option.Id, button.State != 0);
 
136
                                        }
 
137
                                }
 
138
                                
 
139
                                if (applyToAllCheck != null && applyToAllCheck.State != 0)
 
140
                                        data.ApplyToAll = true;
 
141
                                
 
142
                                GtkQuartz.FocusWindow (data.TransientFor ?? MessageService.RootWindow);
 
143
                        }
 
144
                        
 
145
                        return true;
 
146
                }
 
147
        }
 
148
}