2005-06-05 Peter Bartok <pbartok@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Guidelines
1                 Guidelines for hacking SWF
2                 ==========================
3
4 This document describes some of the minimal coding guidelines
5 to be followed while hacking the new SWF implementation. These
6 guidelines are for the sake of consistency.
7
8 1. Please refer to the design document of SWF to understand the
9    implementation.
10
11 2. Please follow the general coding style described for the Mono
12    project (/cvs/mcs/class/README).
13
14 3. Method stubbing is highly discouraged. It's recommended to submit
15    an implemented method instead of just the signature. If you have
16    to stub a property or method, please use the [MonoTODO ("what")]
17    attribute to document what still needs to be done.
18
19 4. When you implement the drawing method for a control in a theme, it
20    should make call to ControlPaint class methods and System.Drawing
21    classes. If it is not possible to implement a control's draw method
22    under these restrictions and you need some functionality from
23    XplatUIDriver, please let us know. We will try to enhance the
24    driver, if *really* required.
25
26 5. As mentioned in the design doc also, double buffering must be used
27    by any new controls being added. Whenever a property or method that
28    changes the look of the control is called, the bitmap representing
29    the control should be updated. The Paint method should only copy
30    the bitmap to the screen, and *not* recalculate or paint the control.
31    To aid in double-buffering, the Control class provides the 
32    Control.DeviceContext and Control.ImageBuffer properties.
33
34    A typical OnPaint will look like this:
35
36       protected override void OnPaint (PaintEventArgs pevent) {
37          pevent.Graphics.DrawImage (this.ImageBuffer, 
38                                     pevent.ClipRectangle, 
39                                     pevent.ClipRectangle, 
40                                     GraphicsUnit.Pixel);
41       }
42
43    The ImageBuffer bitmap is supposed to contain the representation
44    of the control, often drawn when a property is set, similar to this:
45
46       public Color RectColor {
47          set {
48             sb.Color = value;
49             Redraw ();
50          }
51       }
52         
53       internal void Redraw () {
54           this.DeviceContext.FillRectangle (sb, this.ClientRectangle);
55       }
56
57
58 6. Minimize redraws as much as possible by utilizing the clipRectangle 
59    when possible.
60
61 7. Setting the size of a control raises a resize event even if the
62    control size is same. Be careful is setting the size and it's better
63    to avoid changing the control size as much as possible. Wherever
64    possible try scaling the control bitmap as per the size needs.
65
66 8. Make sure to call the base class event methods when overriding them.
67
68 9. Define regions in your code, as it makes it easy to browse the code
69    in the editors which can collapse/expand regions. Also, keep the 
70    methods and properties sorted alphabetically.
71
72 10. Last but not the least, please let others on the mono-winforms-list
73     know about your work, so that duplication can be avoided.
74     
75 11. Theme.cs provides Pen and Brush caching. This allows to share
76     the same brushes and pens between different controls and also to avoid
77     to create and destroy them in every draw operation. You should not create
78     Brushes or Pens directly, you should ask the Resource Pool for them. For
79     example, instead of:
80
81     new SolidBrush(button.BackColor);
82     
83     you should use:
84     
85     ResPool.GetSolidBrush (button.BackColor);
86     
87     Look at SystemResPool class for more details.
88
89 Happy hacking!
90