D Paste by Anonymous
Description: DFL anchor manager
Hide line numbers

Create new paste
Post a reply
View replies

Paste:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
struct AnchorInfo
{
   void layout()
   {
      if(!ctrl)
         return;
      Rect rect = ctrl.bounds;
      if(styles & AnchorStyles.RIGHT)
      {
         if(styles & AnchorStyles.LEFT)
         {
            rect.width = ctrl.parent.clientSize.width - rect.x - r;
         }
         else
         {
            rect.x = ctrl.parent.clientSize.width - r - rect.width;
         }
      }
      if(styles & AnchorStyles.BOTTOM)
      {
         if(styles & AnchorStyles.TOP)
         {
            rect.height = ctrl.parent.clientSize.height - rect.y - b;
         }
         else
         {
            rect.y = ctrl.parent.clientSize.height - b - rect.height;
         }
      }
      ctrl.bounds = rect;
   }
   
   
   void set(Control ctrl, AnchorStyles styles)
   {
      assert(ctrl.parent);
      this.ctrl = ctrl;
      this.styles = styles;
      r = ctrl.parent.clientSize.width - ctrl.right;
      b = ctrl.parent.clientSize.height - ctrl.bottom;
   }
   
   
   Control ctrl;
   AnchorStyles styles;
   private:
   int r, b;
}


/*
What you do is design your DFL GUI and then lock it into place with AnchorInfo,
and the specified bounds will be anchored to the parent's edges...
*/


class MyForm: Form
{
   Label label;
   AnchorInfo alabel;

   this()
   {
      /* set label and its bounds */
      alabel.set(label, AnchorStyles.BOTTOM); // Lock these bounds to the bottom.
   }

   protected override void onLayout(LayoutEventArgs ea)
   {
      super.onLayout(ea);
      
      alabel.layout(); // Let it handle the layout of label.
   }
}


/*
Note that this code does not work with auto-scaling.
*/

Replies:
Reply by Louis Lu
Thank you, Chris. It works like a charm.

But I have to change the data type of 'styles' into ubyte to accept a combined setting like AnchorStyles.BOTTOM|AnchorStyles.RIGHT.

Reply by CM
Yea, I think that's an issue with DMD.

    (some replies deleted)