GUI/Gadget.cs
author moel.mich
Tue, 07 Sep 2010 22:15:02 +0000
changeset 181 9901dbb25f18
parent 176 c16fd81b520a
child 183 3096735e99b2
permissions -rw-r--r--
Improved the gadget formatting and added an option to remove the hardware names in the gadget.
moel@176
     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@176
    79
    protected virtual Size Size {
moel@176
    80
      get {
moel@176
    81
        return window.Size; 
moel@176
    82
      }
moel@176
    83
      set {
moel@176
    84
        if (window.Size != value) {
moel@176
    85
          DisposeBuffer();
moel@176
    86
          this.window.Size = value;          
moel@176
    87
          CreateBuffer();
moel@176
    88
        }
moel@176
    89
      }
moel@176
    90
    }
moel@176
    91
moel@176
    92
    public byte Opacity {
moel@176
    93
      get {
moel@176
    94
        return window.Opacity;
moel@176
    95
      }
moel@176
    96
      set {
moel@176
    97
        window.Opacity = value;
moel@176
    98
      }
moel@176
    99
    }
moel@176
   100
moel@176
   101
    public bool LockPosition {
moel@176
   102
      get {
moel@176
   103
        return window.LockPosition;
moel@176
   104
      }
moel@176
   105
      set {
moel@176
   106
        window.LockPosition = value;
moel@176
   107
      }
moel@176
   108
    }
moel@176
   109
moel@176
   110
    public bool AlwaysOnTop {
moel@176
   111
      get {
moel@176
   112
        return window.AlwaysOnTop;
moel@176
   113
      }
moel@176
   114
      set {
moel@176
   115
        window.AlwaysOnTop = value;
moel@176
   116
      }
moel@176
   117
    }
moel@176
   118
moel@176
   119
    public ContextMenu ContextMenu {
moel@176
   120
      get {
moel@176
   121
        return window.ContextMenu;
moel@176
   122
      }
moel@176
   123
      set {
moel@176
   124
        window.ContextMenu = value;
moel@176
   125
      }
moel@176
   126
    }
moel@176
   127
moel@176
   128
    private void CreateBuffer() {
moel@176
   129
      this.buffer = new Bitmap(window.Size.Width, window.Size.Height, 
moel@176
   130
        PixelFormat.Format32bppArgb);
moel@176
   131
      this.graphics = Graphics.FromImage(this.buffer);
moel@176
   132
      if (Environment.OSVersion.Version.Major > 5) {
moel@176
   133
        this.graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
moel@176
   134
        this.graphics.SmoothingMode = SmoothingMode.HighQuality;
moel@176
   135
      }
moel@176
   136
    }
moel@176
   137
moel@176
   138
    private void DisposeBuffer() {
moel@176
   139
      if (buffer != null) {
moel@176
   140
        this.buffer.Dispose();
moel@176
   141
        this.buffer = null;
moel@176
   142
      }
moel@176
   143
      if (graphics != null) {
moel@176
   144
        this.graphics.Dispose();
moel@176
   145
        this.graphics = null;
moel@176
   146
      }
moel@176
   147
    }
moel@176
   148
moel@176
   149
    public bool Visible {
moel@176
   150
      get {
moel@176
   151
        return window.Visible;
moel@176
   152
      }
moel@176
   153
      set {
moel@176
   154
        if (value != window.Visible) {
moel@176
   155
          if (value)
moel@176
   156
            Redraw();
moel@176
   157
          window.Visible = value;
moel@176
   158
        }
moel@176
   159
      }
moel@176
   160
    }
moel@176
   161
moel@176
   162
    public void Redraw() {
moel@176
   163
      OnPaint(new PaintEventArgs(graphics, 
moel@176
   164
        new Rectangle(Point.Empty, window.Size)));
moel@176
   165
      window.Update(buffer);
moel@176
   166
    }
moel@176
   167
moel@176
   168
    protected abstract void OnPaint(PaintEventArgs e);
moel@176
   169
  
moel@176
   170
  }
moel@176
   171
}