~mantas/pinta/stable

« back to all changes in this revision

Viewing changes to Pinta.Core/Managers/ToolManager.cs

  • Committer: Iain Lane
  • Date: 2010-02-19 15:33:30 UTC
  • Revision ID: git-v1:92cc09614a4e2801bfe39cd971f6b31e694a500b
ImportedĀ UpstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ToolManager.cs
 
3
//  
 
4
// Author:
 
5
//       Jonathan Pobst <monkey@jpobst.com>
 
6
// 
 
7
// Copyright (c) 2010 Jonathan Pobst
 
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.Collections.Generic;
 
29
using Gtk;
 
30
 
 
31
namespace Pinta.Core
 
32
{
 
33
        public class ToolManager : IEnumerable<BaseTool>
 
34
        {
 
35
                int index = -1;
 
36
                int prev_index = -1;
 
37
                
 
38
                private List<BaseTool> Tools;
 
39
 
 
40
                public ToolManager ()
 
41
                {
 
42
                        Tools = new List<BaseTool> ();
 
43
                }
 
44
 
 
45
                public void AddTool (BaseTool tool)
 
46
                {
 
47
                        tool.ToolItem.Clicked += HandlePbToolItemClicked;
 
48
                        tool.ToolItem.Sensitive = tool.Enabled;
 
49
                        
 
50
                        Tools.Add (tool);       
 
51
                }
 
52
                
 
53
                void HandlePbToolItemClicked (object sender, EventArgs e)
 
54
                {
 
55
                        ToggleToolButton tb = (ToggleToolButton)sender;
 
56
 
 
57
                        BaseTool t = FindTool (tb.Label);
 
58
                
 
59
                        // Don't let the user unselect the current tool 
 
60
                        if (t.Name == CurrentTool.Name) {
 
61
                                //tb.Active = true;
 
62
                                //return;
 
63
                        }
 
64
 
 
65
                        SetCurrentTool (t);
 
66
                }
 
67
 
 
68
                private BaseTool FindTool (string name)
 
69
                {
 
70
                        name = name.ToLowerInvariant ();
 
71
                        
 
72
                        foreach (BaseTool tool in Tools) {
 
73
                                if (tool.Name.ToLowerInvariant () == name) {
 
74
                                        return tool;
 
75
                                }
 
76
                        }
 
77
                        
 
78
                        return null;
 
79
                }
 
80
                
 
81
                public BaseTool CurrentTool {
 
82
                        get { return Tools[index]; }
 
83
                }
 
84
                
 
85
                public BaseTool PreviousTool {
 
86
                        get { return Tools[prev_index]; }
 
87
                }
 
88
 
 
89
                public void SetCurrentTool (BaseTool tool)
 
90
                {
 
91
                        int i = Tools.IndexOf (tool);
 
92
                        
 
93
                        if (index == i)
 
94
                                return;
 
95
                        
 
96
                        // Unload previous tool if needed
 
97
                        if (index >= 0) {
 
98
                                Tools[index].DoClearToolBar (PintaCore.Chrome.ToolToolBar);
 
99
                                Tools[index].DoDeactivated ();
 
100
                                Tools[index].ToolItem.Active = false;
 
101
                                prev_index = index;
 
102
                        }
 
103
                        
 
104
                        // Load new tool
 
105
                        index = i;
 
106
                        tool.DoBuildToolBar (PintaCore.Chrome.ToolToolBar);
 
107
                        tool.ToolItem.Active = true;
 
108
                        
 
109
                        PintaCore.Workspace.Invalidate ();
 
110
                        PintaCore.Chrome.SetStatusBarText (string.Format (" {0}: {1}", tool.Name, tool.StatusBarText));
 
111
                }
 
112
 
 
113
                public void SetCurrentTool (string tool)
 
114
                {
 
115
                        BaseTool t = FindTool (tool);
 
116
                        
 
117
                        if (t != null)
 
118
                                SetCurrentTool (t);
 
119
                }
 
120
                
 
121
                #region IEnumerable<BaseTool> implementation
 
122
                public IEnumerator<BaseTool> GetEnumerator ()
 
123
                {
 
124
                        return Tools.GetEnumerator ();
 
125
                }
 
126
                #endregion
 
127
 
 
128
                #region IEnumerable implementation
 
129
                System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
 
130
                {
 
131
                        return Tools.GetEnumerator ();
 
132
                }
 
133
                #endregion
 
134
        }
 
135
}