GUI/Gadget.cs
author moel.mich
Mon, 06 Sep 2010 19:53:13 +0000
changeset 176 c16fd81b520a
child 181 9901dbb25f18
permissions -rw-r--r--
Added a desktop gadget implementation.
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@176
    57
    public void Dispose() {
moel@176
    58
      this.graphics.Dispose();
moel@176
    59
      this.buffer.Dispose();
moel@176
    60
    }
moel@176
    61
moel@176
    62
    public Point Location {
moel@176
    63
      get {
moel@176
    64
        return window.Location;
moel@176
    65
      }
moel@176
    66
      set {
moel@176
    67
        window.Location = value;
moel@176
    68
      }
moel@176
    69
    }
moel@176
    70
moel@176
    71
    public event EventHandler LocationChanged {
moel@176
    72
      add {
moel@176
    73
        window.LocationChanged += value;
moel@176
    74
      }
moel@176
    75
      remove {
moel@176
    76
        window.LocationChanged -= value;
moel@176
    77
      }
moel@176
    78
    }
moel@176
    79
moel@176
    80
    protected virtual Size Size {
moel@176
    81
      get {
moel@176
    82
        return window.Size; 
moel@176
    83
      }
moel@176
    84
      set {
moel@176
    85
        if (window.Size != value) {
moel@176
    86
          DisposeBuffer();
moel@176
    87
          this.window.Size = value;          
moel@176
    88
          CreateBuffer();
moel@176
    89
        }
moel@176
    90
      }
moel@176
    91
    }
moel@176
    92
moel@176
    93
    public byte Opacity {
moel@176
    94
      get {
moel@176
    95
        return window.Opacity;
moel@176
    96
      }
moel@176
    97
      set {
moel@176
    98
        window.Opacity = value;
moel@176
    99
      }
moel@176
   100
    }
moel@176
   101
moel@176
   102
    public bool LockPosition {
moel@176
   103
      get {
moel@176
   104
        return window.LockPosition;
moel@176
   105
      }
moel@176
   106
      set {
moel@176
   107
        window.LockPosition = value;
moel@176
   108
      }
moel@176
   109
    }
moel@176
   110
moel@176
   111
    public bool AlwaysOnTop {
moel@176
   112
      get {
moel@176
   113
        return window.AlwaysOnTop;
moel@176
   114
      }
moel@176
   115
      set {
moel@176
   116
        window.AlwaysOnTop = value;
moel@176
   117
      }
moel@176
   118
    }
moel@176
   119
moel@176
   120
    public ContextMenu ContextMenu {
moel@176
   121
      get {
moel@176
   122
        return window.ContextMenu;
moel@176
   123
      }
moel@176
   124
      set {
moel@176
   125
        window.ContextMenu = value;
moel@176
   126
      }
moel@176
   127
    }
moel@176
   128
moel@176
   129
    private void CreateBuffer() {
moel@176
   130
      this.buffer = new Bitmap(window.Size.Width, window.Size.Height, 
moel@176
   131
        PixelFormat.Format32bppArgb);
moel@176
   132
      this.graphics = Graphics.FromImage(this.buffer);
moel@176
   133
      if (Environment.OSVersion.Version.Major > 5) {
moel@176
   134
        this.graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
moel@176
   135
        this.graphics.SmoothingMode = SmoothingMode.HighQuality;
moel@176
   136
      }
moel@176
   137
    }
moel@176
   138
moel@176
   139
    private void DisposeBuffer() {
moel@176
   140
      if (buffer != null) {
moel@176
   141
        this.buffer.Dispose();
moel@176
   142
        this.buffer = null;
moel@176
   143
      }
moel@176
   144
      if (graphics != null) {
moel@176
   145
        this.graphics.Dispose();
moel@176
   146
        this.graphics = null;
moel@176
   147
      }
moel@176
   148
    }
moel@176
   149
moel@176
   150
    public bool Visible {
moel@176
   151
      get {
moel@176
   152
        return window.Visible;
moel@176
   153
      }
moel@176
   154
      set {
moel@176
   155
        if (value != window.Visible) {
moel@176
   156
          if (value)
moel@176
   157
            Redraw();
moel@176
   158
          window.Visible = value;
moel@176
   159
        }
moel@176
   160
      }
moel@176
   161
    }
moel@176
   162
moel@176
   163
    public void Redraw() {
moel@176
   164
      OnPaint(new PaintEventArgs(graphics, 
moel@176
   165
        new Rectangle(Point.Empty, window.Size)));
moel@176
   166
      window.Update(buffer);
moel@176
   167
    }
moel@176
   168
moel@176
   169
    protected abstract void OnPaint(PaintEventArgs e);
moel@176
   170
  
moel@176
   171
  }
moel@176
   172
}