OSDN Git Service

RAD-493: Added context menu to group chat
[radegast/radegast.git] / Radegast / GUI / Floater.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
9 //     * Redistributions of source code must retain the above copyright notice,
10 //       this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above copyright
12 //       notice, this list of conditions and the following disclaimer in the
13 //       documentation and/or other materials provided with the distribution.
14 //     * Neither the name of the application "Radegast", nor the names of its
15 //       contributors may be used to endorse or promote products derived from
16 //       this software without specific prior written permission.
17 // 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // $Id$
30 //
31
32 using System;
33 using System.Collections.Generic;
34 using System.Linq;
35 using System.Text;
36 using System.Drawing;
37 using System.Windows.Forms;
38
39 namespace Radegast
40 {
41     public class Floater : RadegastForm
42     {
43         public Control Control, TrackedControl;
44         public bool DockedToMain;
45
46         int mainTop;
47         int mainLeft;
48         Size mainSize;
49         Form parentForm;
50
51         public Floater(RadegastInstance instance, Control control, Control trackedControl)
52             : base(instance)
53         {
54             this.Control = control;
55             this.TrackedControl = trackedControl;
56             SettingsKeyBase = "tab_window_" + control.GetType().Name;
57             AutoSavePosition = true;
58
59             FormBorderStyle = FormBorderStyle.SizableToolWindow;
60             Text = Control.Text;
61             
62             TrackedControl.ParentChanged += new EventHandler(Control_ParentChanged);
63             Control.TextChanged += new EventHandler(Control_TextChanged);
64             Activated += new EventHandler(Floater_Activated);
65             Deactivate += new EventHandler(Floater_Deactivate);
66             ResizeEnd += new EventHandler(Floater_ResizeEnd);
67             Shown += new EventHandler(Floater_Shown);
68             Load += new EventHandler(Floater_Load);
69
70         }
71
72         void Floater_Load(object sender, EventArgs e)
73         {
74             Control.Dock = DockStyle.Fill;
75             ClientSize = Control.Size;
76             Controls.Add(Control);
77             SaveMainFormPos();
78             parentForm.Move += new EventHandler(parentForm_Move);
79         }
80
81         void Floater_Shown(object sender, EventArgs e)
82         {
83             UpdatePos();
84         }
85
86         void Floater_ResizeEnd(object sender, EventArgs e)
87         {
88             UpdatePos();
89         }
90
91         void Floater_Activated(object sender, EventArgs e)
92         {
93             Opacity = 1;
94         }
95
96         void Floater_Deactivate(object sender, EventArgs e)
97         {
98             if (DockedToMain)
99             {
100                 Opacity = 0.75;
101             }
102         }
103
104         void Control_TextChanged(object sender, EventArgs e)
105         {
106             Text = Control.Text;
107         }
108
109         void parentForm_Move(object sender, EventArgs e)
110         {
111             if (DockedToMain)
112             {
113                 Left += (parentForm.Left - mainLeft);
114                 Top += (parentForm.Top - mainTop);
115             }
116             SaveMainFormPos();
117             UpdatePos();
118         }
119
120         void Control_ParentChanged(object sender, EventArgs e)
121         {
122             if (parentForm != null)
123             {
124                 parentForm.Move -= new EventHandler(parentForm_Move);
125
126             }
127             SaveMainFormPos();
128             if (parentForm != null)
129             {
130                 Owner = parentForm;
131                 parentForm.Move += new EventHandler(parentForm_Move);
132                 UpdatePos();
133             }
134         }
135
136         void SaveMainFormPos()
137         {
138             parentForm = TrackedControl.FindForm();
139             if (parentForm != null)
140             {
141                 mainTop = parentForm.Top;
142                 mainLeft = parentForm.Left;
143                 mainSize = parentForm.Size;
144             }
145         }
146
147         private void UpdatePos()
148         {
149             Rectangle mainRect = new Rectangle(new Point(mainLeft, mainTop), mainSize);
150             Rectangle myRect = new Rectangle(new Point(Left, Top), Size);
151
152             if (mainRect == Rectangle.Union(mainRect, myRect))
153             {
154                 DockedToMain = true;
155             }
156             else
157             {
158                 DockedToMain = false;
159             }
160         }
161     }
162 }