~ubuntu-branches/debian/jessie/banshee-community-extensions/jessie

« back to all changes in this revision

Viewing changes to src/ClutterFlow/ClutterFlow/ClutterToggleButton.cs

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-09-20 18:45:46 UTC
  • mfrom: (1.2.9 upstream) (5.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20110920184546-3ahue2qplydc4t0e
Tags: 2.2.0-1
* [4940fab] Imported Upstream version 2.2.0
  + Notable bug fixes:
    - Karaoke: Fix crash when switching to Now Playing
    - Lyrics: Fix crash when switching to Now Playing

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
namespace ClutterFlow.Buttons
35
35
{
36
 
        
37
 
        
38
 
        public abstract class ClutterToggleButton : ClutterButtonState
39
 
        {
40
 
 
41
 
                public event EventHandler Toggled;
42
 
                protected void InvokeToggled () {
43
 
                        if (Toggled!=null) Toggled (this, EventArgs.Empty);
44
 
                }
45
 
                
46
 
                //// <value>
47
 
                /// MaxBits represents the toggle buttons maximal bit-states to be set:
48
 
                ///     1: mouse_over
49
 
                ///     2: mouse_down
50
 
                ///     4: toggled
51
 
                /// </value>
52
 
                protected override int MaxState {
53
 
                        get { return 7; }
54
 
                }
55
 
 
56
 
                private bool freeze_state = false;
57
 
                public virtual bool IsActive {
58
 
                        get { return (state & 4) > 0;   }
59
 
                        set {
60
 
                                if (!freeze_state) {
61
 
                                        if (value && (state & 4)==0) {
62
 
                                                State |= 4;
63
 
                                                InvokeToggled ();
64
 
                                        } else if (!value && (state & 4)>0) {
65
 
                                                State &= ~4;
66
 
                                                InvokeToggled ();
67
 
                                        }
68
 
                                }
69
 
                        }
70
 
                }
71
 
                public virtual void SetSilent (bool value)
72
 
                {
73
 
                        if (!freeze_state) {
74
 
                                if (value && (state & 4)==0) {
75
 
                                        State |= 4;
76
 
                                } else if (!value && (state & 4)>0) {
77
 
                                        State &= ~4;
78
 
                                }
79
 
                        }
80
 
                }
81
 
                
82
 
                public virtual CairoTexture StateTexture {
83
 
                        get { return (IsActive) ? active_button.StateTexture : passive_button.StateTexture; }
84
 
                }
85
 
                
86
 
                protected ClutterGenericButton passive_button;
87
 
                protected ClutterGenericButton active_button;
88
 
 
89
 
                public ClutterToggleButton (uint width, uint height, bool toggled) : this (width, height, toggled ? 0x04 : 0x00)
90
 
                {
91
 
                }
92
 
                
93
 
                public ClutterToggleButton (uint width, uint height, int state) : base ()
94
 
                {
95
 
                        this.SetSize (width, height);
96
 
                        this.state = state;
97
 
 
98
 
                        passive_button = new ClutterGenericButton(width, height, state, CreatePassiveTexture);
99
 
                        passive_button.BubbleEvents = true;
100
 
                        active_button  = new ClutterGenericButton(width, height, state, CreateActiveTexture);
101
 
                        active_button.BubbleEvents = true;
102
 
                        
103
 
                        Initialise ();
104
 
                }
105
 
 
106
 
                protected override void Initialise () {
107
 
                        Add (passive_button);
108
 
                        Add (active_button);
109
 
                        
110
 
                        base.Initialise ();
111
 
 
112
 
                        active_button.Update ();
113
 
                        passive_button.Update ();
114
 
                        Update ();
115
 
                        Show ();
116
 
                }
117
 
                
118
 
                #region Event Handling
119
 
                protected override void HandleButtonReleaseEvent(object o, ButtonReleaseEventArgs args)
120
 
                {
121
 
                        State = (state^4) & ~2;
122
 
                        freeze_state = true; //freeze the state untill after the event has finished handling
123
 
                        InvokeToggled ();
124
 
                        freeze_state = false; //thaw the state
125
 
                        args.RetVal = !bubble;
126
 
                }
127
 
                #endregion
128
 
                
129
 
                public override void Update () {
130
 
                        if ((state & 4) > 0) {
131
 
                                passive_button.Hide ();
132
 
                                active_button.State = state;
133
 
                                active_button.Show ();
134
 
                        } else {
135
 
                                active_button.Hide ();
136
 
                                passive_button.State = state;
137
 
                                passive_button.Show ();
138
 
                        }
139
 
                        Show ();
140
 
                }
141
 
 
142
 
                protected abstract void CreatePassiveTexture (Clutter.CairoTexture texture, int with_state);
143
 
                protected abstract void CreateActiveTexture (Clutter.CairoTexture texture, int with_state);
144
 
        }
 
36
 
 
37
 
 
38
    public abstract class ClutterToggleButton : ClutterButtonState
 
39
    {
 
40
 
 
41
        public event EventHandler Toggled;
 
42
        protected void InvokeToggled () {
 
43
            if (Toggled!=null) Toggled (this, EventArgs.Empty);
 
44
        }
 
45
 
 
46
        //// <value>
 
47
        /// MaxBits represents the toggle buttons maximal bit-states to be set:
 
48
        ///     1: mouse_over
 
49
        ///     2: mouse_down
 
50
        ///     4: toggled
 
51
        /// </value>
 
52
        protected override int MaxState {
 
53
            get { return 7; }
 
54
        }
 
55
 
 
56
        private bool freeze_state = false;
 
57
        public virtual bool IsActive {
 
58
            get { return (state & 4) > 0;    }
 
59
            set {
 
60
                if (!freeze_state) {
 
61
                    if (value && (state & 4)==0) {
 
62
                        State |= 4;
 
63
                        InvokeToggled ();
 
64
                    } else if (!value && (state & 4)>0) {
 
65
                        State &= ~4;
 
66
                        InvokeToggled ();
 
67
                    }
 
68
                }
 
69
            }
 
70
        }
 
71
        public virtual void SetSilent (bool value)
 
72
        {
 
73
            if (!freeze_state) {
 
74
                if (value && (state & 4)==0) {
 
75
                    State |= 4;
 
76
                } else if (!value && (state & 4)>0) {
 
77
                    State &= ~4;
 
78
                }
 
79
            }
 
80
        }
 
81
 
 
82
        public virtual CairoTexture StateTexture {
 
83
            get { return (IsActive) ? active_button.StateTexture : passive_button.StateTexture; }
 
84
        }
 
85
 
 
86
        protected ClutterGenericButton passive_button;
 
87
        protected ClutterGenericButton active_button;
 
88
 
 
89
        public ClutterToggleButton (uint width, uint height, bool toggled) : this (width, height, toggled ? 0x04 : 0x00)
 
90
        {
 
91
        }
 
92
 
 
93
        public ClutterToggleButton (uint width, uint height, int state) : base ()
 
94
        {
 
95
            this.SetSize (width, height);
 
96
            this.state = state;
 
97
 
 
98
            passive_button = new ClutterGenericButton(width, height, state, CreatePassiveTexture);
 
99
            passive_button.BubbleEvents = true;
 
100
            active_button  = new ClutterGenericButton(width, height, state, CreateActiveTexture);
 
101
            active_button.BubbleEvents = true;
 
102
 
 
103
            Initialise ();
 
104
        }
 
105
 
 
106
        protected override void Initialise () {
 
107
            Add (passive_button);
 
108
            Add (active_button);
 
109
 
 
110
            base.Initialise ();
 
111
 
 
112
            active_button.Update ();
 
113
            passive_button.Update ();
 
114
            Update ();
 
115
            Show ();
 
116
        }
 
117
 
 
118
        #region Event Handling
 
119
        protected override void HandleButtonReleaseEvent(object o, ButtonReleaseEventArgs args)
 
120
        {
 
121
            State = (state^4) & ~2;
 
122
            freeze_state = true; //freeze the state untill after the event has finished handling
 
123
            InvokeToggled ();
 
124
            freeze_state = false; //thaw the state
 
125
            args.RetVal = !bubble;
 
126
        }
 
127
        #endregion
 
128
 
 
129
        public override void Update () {
 
130
            if ((state & 4) > 0) {
 
131
                passive_button.Hide ();
 
132
                active_button.State = state;
 
133
                active_button.Show ();
 
134
            } else {
 
135
                active_button.Hide ();
 
136
                passive_button.State = state;
 
137
                passive_button.Show ();
 
138
            }
 
139
            Show ();
 
140
        }
 
141
 
 
142
        protected abstract void CreatePassiveTexture (Clutter.CairoTexture texture, int with_state);
 
143
        protected abstract void CreateActiveTexture (Clutter.CairoTexture texture, int with_state);
 
144
    }
145
145
}