GUI/Gadget.cs
author moel.mich
Mon, 16 May 2011 22:04:04 +0000
changeset 289 d4798e7f4388
parent 244 99f16e21cdc8
child 302 44c0e7f76e9e
permissions -rw-r--r--
Fixed Issue 215.
moel@202
     1
/*
moel@176
     2
  
moel@176
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@176
     4
moel@176
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@176
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@176
     7
  the License. You may obtain a copy of the License at
moel@176
     8
 
moel@176
     9
  http://www.mozilla.org/MPL/
moel@176
    10
moel@176
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@176
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@176
    13
  for the specific language governing rights and limitations under the License.
moel@176
    14
moel@176
    15
  The Original Code is the Open Hardware Monitor code.
moel@176
    16
moel@176
    17
  The Initial Developer of the Original Code is 
moel@176
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@176
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@176
    20
  the Initial Developer. All Rights Reserved.
moel@176
    21
moel@176
    22
  Contributor(s):
moel@176
    23
moel@176
    24
  Alternatively, the contents of this file may be used under the terms of
moel@176
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@176
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@176
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@176
    28
  of those above. If you wish to allow use of your version of this file only
moel@176
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@176
    30
  use your version of this file under the terms of the MPL, indicate your
moel@176
    31
  decision by deleting the provisions above and replace them with the notice
moel@176
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@176
    33
  the provisions above, a recipient may use your version of this file under
moel@176
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@176
    35
 
moel@176
    36
*/
moel@176
    37
moel@176
    38
using System;
moel@176
    39
using System.Drawing;
moel@176
    40
using System.Drawing.Drawing2D;
moel@176
    41
using System.Drawing.Imaging;
moel@176
    42
using System.Drawing.Text;
moel@176
    43
using System.Windows.Forms;
moel@176
    44
moel@176
    45
namespace OpenHardwareMonitor.GUI {
moel@176
    46
  public abstract class Gadget : IDisposable {
moel@176
    47
moel@176
    48
    private GadgetWindow window;
moel@176
    49
    private Bitmap buffer;
moel@176
    50
    private Graphics graphics;
moel@176
    51
moel@176
    52
    public Gadget() {
moel@176
    53
      this.window = new GadgetWindow();
moel@176
    54
      CreateBuffer();
moel@176
    55
    }
moel@176
    56
moel@181
    57
    public virtual void Dispose() {
moel@181
    58
      DisposeBuffer();
moel@176
    59
    }
moel@176
    60
moel@176
    61
    public Point Location {
moel@176
    62
      get {
moel@176
    63
        return window.Location;
moel@176
    64
      }
moel@176
    65
      set {
moel@176
    66
        window.Location = value;
moel@176
    67
      }
moel@176
    68
    }
moel@176
    69
moel@176
    70
    public event EventHandler LocationChanged {
moel@176
    71
      add {
moel@176
    72
        window.LocationChanged += value;
moel@176
    73
      }
moel@176
    74
      remove {
moel@176
    75
        window.LocationChanged -= value;
moel@176
    76
      }
moel@176
    77
    }
moel@176
    78
moel@183
    79
    public virtual Size Size {
moel@176
    80
      get {
moel@176
    81
        return window.Size; 
moel@176
    82
      }
moel@183
    83
      set {        
moel@183
    84
        this.window.Size = value;
moel@183
    85
      }
moel@183
    86
    }
moel@183
    87
moel@183
    88
    public event EventHandler SizeChanged {
moel@183
    89
      add {
moel@183
    90
        window.SizeChanged += value;
moel@183
    91
      }
moel@183
    92
      remove {
moel@183
    93
        window.SizeChanged -= value;
moel@176
    94
      }
moel@176
    95
    }
moel@176
    96
moel@176
    97
    public byte Opacity {
moel@176
    98
      get {
moel@176
    99
        return window.Opacity;
moel@176
   100
      }
moel@176
   101
      set {
moel@176
   102
        window.Opacity = value;
moel@176
   103
      }
moel@176
   104
    }
moel@176
   105
moel@183
   106
    public bool LockPositionAndSize {
moel@176
   107
      get {
moel@183
   108
        return window.LockPositionAndSize;
moel@176
   109
      }
moel@176
   110
      set {
moel@183
   111
        window.LockPositionAndSize = value;
moel@176
   112
      }
moel@176
   113
    }
moel@176
   114
moel@176
   115
    public bool AlwaysOnTop {
moel@176
   116
      get {
moel@176
   117
        return window.AlwaysOnTop;
moel@176
   118
      }
moel@176
   119
      set {
moel@176
   120
        window.AlwaysOnTop = value;
moel@176
   121
      }
moel@176
   122
    }
moel@176
   123
moel@176
   124
    public ContextMenu ContextMenu {
moel@176
   125
      get {
moel@176
   126
        return window.ContextMenu;
moel@176
   127
      }
moel@176
   128
      set {
moel@176
   129
        window.ContextMenu = value;
moel@176
   130
      }
moel@176
   131
    }
moel@176
   132
moel@183
   133
    public event HitTestEventHandler HitTest {
moel@183
   134
      add {
moel@183
   135
        window.HitTest += value;
moel@183
   136
      }
moel@183
   137
      remove {
moel@183
   138
        window.HitTest -= value;
moel@183
   139
      }
moel@244
   140
    }
moel@244
   141
moel@244
   142
    public event MouseEventHandler MouseDoubleClick {
moel@244
   143
      add {
moel@244
   144
        window.MouseDoubleClick += value;
moel@244
   145
      }
moel@244
   146
      remove {
moel@244
   147
        window.MouseDoubleClick -= value;
moel@244
   148
      }
moel@244
   149
    }
moel@183
   150
moel@176
   151
    private void CreateBuffer() {
moel@176
   152
      this.buffer = new Bitmap(window.Size.Width, window.Size.Height, 
moel@176
   153
        PixelFormat.Format32bppArgb);
moel@176
   154
      this.graphics = Graphics.FromImage(this.buffer);
moel@176
   155
      if (Environment.OSVersion.Version.Major > 5) {
moel@176
   156
        this.graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
moel@176
   157
        this.graphics.SmoothingMode = SmoothingMode.HighQuality;
moel@176
   158
      }
moel@176
   159
    }
moel@176
   160
moel@176
   161
    private void DisposeBuffer() {
moel@176
   162
      if (buffer != null) {
moel@176
   163
        this.buffer.Dispose();
moel@176
   164
        this.buffer = null;
moel@176
   165
      }
moel@176
   166
      if (graphics != null) {
moel@176
   167
        this.graphics.Dispose();
moel@176
   168
        this.graphics = null;
moel@176
   169
      }
moel@176
   170
    }
moel@176
   171
moel@176
   172
    public bool Visible {
moel@176
   173
      get {
moel@176
   174
        return window.Visible;
moel@176
   175
      }
moel@176
   176
      set {
moel@176
   177
        if (value != window.Visible) {
moel@202
   178
          window.Visible = value;
moel@289
   179
          if (VisibleChanged != null)
moel@289
   180
            VisibleChanged(this, EventArgs.Empty);
moel@176
   181
          if (value)
moel@202
   182
            Redraw();          
moel@176
   183
        }
moel@176
   184
      }
moel@176
   185
    }
moel@176
   186
moel@289
   187
    public event EventHandler VisibleChanged;
moel@289
   188
moel@176
   189
    public void Redraw() {
moel@202
   190
      if (!window.Visible)
moel@202
   191
        return;
moel@202
   192
      
moel@183
   193
      if (window.Size != buffer.Size) {
moel@183
   194
        DisposeBuffer();
moel@183
   195
        CreateBuffer();
moel@183
   196
      }
moel@183
   197
moel@176
   198
      OnPaint(new PaintEventArgs(graphics, 
moel@176
   199
        new Rectangle(Point.Empty, window.Size)));
moel@176
   200
      window.Update(buffer);
moel@176
   201
    }
moel@176
   202
moel@176
   203
    protected abstract void OnPaint(PaintEventArgs e);
moel@176
   204
  
moel@176
   205
  }
moel@176
   206
}