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