e53bea40ff1f1e8b01768e0e4dd92e12f841f949
[sdk] / ide / src / designer / ToolBox.ec
1 import "ide"
2
3 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
4
5 static void Dummy()
6 {
7    Window a;
8    a.OnLeftButtonUp(0,0,0);
9 }
10
11 class ToolBox : Window
12 {
13    text = $"Toolbox";
14    background = formColor;
15    borderStyle = sizable;
16    hasClose = true;
17    //size = { 200, 500 };
18    size = { 150, 300 };
19    anchor = { right = 0 };
20    //anchor = { right = 0, top = 0, bottom = 0 };
21    hasVertScroll = true;
22    menu = Menu { };
23
24    /* isRemote = true;
25    inactive = true;
26    stayOnTop = true; */
27
28    char className[1024];
29    int controlY;
30    Button arrowControl;
31    ObjectInfo selectedClass;
32    CodeEditor codeEditor;
33    char * selectedControl;
34    int numControls;
35
36    Menu viewMenu { menu, $"View" };
37    MenuItem { viewMenu, $"View Toolbox" };
38
39    ~ToolBox()
40    {
41       delete arrowControl;
42    }
43    property char * controlClass
44    {
45       set
46       {
47          arrowControl.checked = true;
48          className[0] = 0;
49          ((Designer)GetActiveDesigner()).objectClass = null;
50       }
51       get
52       {
53          return (char *)className;
54       }
55    }
56    property CodeEditor codeEditor
57    {
58       set { this.codeEditor = value; }
59    }
60    property ObjectInfo selectedClass
61    {
62       set
63       {
64          if(value != selectedClass ||
65             (value && value.instance && (!selectedClass.instance || selectedClass.instance._class != value.instance._class)))
66          {
67             Button control, next;
68
69             selectedControl = null;
70
71             selectedClass = value;
72
73             for(control = (Button)firstChild; control; control = next)
74             {
75                next = (Button)control.next;
76                if(control != arrowControl && !control.nonClient)
77                {
78                   if(control.checked)
79                      selectedControl = (char *)control.id;
80                   control.Destroy(0);
81                }
82             }
83             // TOFIX: It seems the virtual space does not get adjusted by only destroying the buttons?
84             scrollArea = { 0, 0 };
85
86             //className = null;      // Had to drag twice on code...
87             arrowControl.checked = true;
88
89             if(value && value.instance)
90             {
91                controlY = 25;
92                numControls = 1;
93                ((Designer)GetActiveDesigner()).FillToolBox();
94             }
95          }
96       }
97    }
98
99    bool OnClose(bool parentClosing)
100    {
101       visible = false;
102       if(!parentClosing)
103          ide.RepositionWindows(false);
104       return parentClosing;
105    }
106
107    bool OnKeyDown(Key key, unichar ch)
108    {
109       if(key == escape)
110       {
111          if(ide.activeClient) ide.activeClient.Activate();
112          //Destroy(0);
113       }
114       return true;
115    }
116
117    void AddControl(Class _class)
118    {
119       Button control = CreateControl(&controlY, (const char *)eClass_GetProperty(_class, "icon"), _class.name, _class.name);
120       numControls++;
121       if(selectedControl && !strcmp(selectedControl, _class.name))
122       {
123          control.checked = true;
124          strcpy(className, _class.name);
125          ((Designer)GetActiveDesigner()).objectClass = className;
126       }
127    }
128
129    ToolBox()
130    {
131       int y = 5;
132       arrowControl = CreateControl(&y, ":others/mousePointer.png", $"Pointer", null);
133       arrowControl.checked = true;
134       incref arrowControl;
135    }
136
137    bool OnLeftButtonUp(int x, int y, Modifiers mods)
138    {
139       Designer designer = (Designer)GetActiveDesigner();
140       if(designer && designer.isDragging)
141       {
142          ((Designer)GetActiveDesigner()).isDragging = false;
143          ReleaseCapture();
144          return rootWindow.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp,
145             x + absPosition.x + clientStart.x, y + absPosition.y + clientStart.y, &mods, false, false);
146       }
147       return true;
148    }
149
150    Button CreateControl(int * y, const char * bitmapFile, const char * text, const void * id)
151    {
152       Button control
153       {
154          this;
155          bevelOver = true;
156          isRadio = true;
157          text = text;
158          minClientSize.w = 120;
159          size.h = 20;
160          anchor = Anchor { left = 5, top = *y, right = 5 };
161          alignment = left;
162          bitmap = { bitmapFile };
163          id = (int64)id;
164
165          bool NotifyPushed(Button control, int x, int y, Modifiers mods)
166          {
167             control.checked = true;
168             strcpy(className, control.text); //id;
169             if((Designer)GetActiveDesigner())
170             {
171                ((Designer)GetActiveDesigner()).objectClass = control.id ? className : null; //(char *)control.text; //id;
172                ((Designer)GetActiveDesigner()).isDragging = true;
173                control.ReleaseCapture();
174                Capture();
175                cursor = ((GuiApplication)__thisModule.application).GetCursor(arrow);
176             }
177             return true;
178          }
179
180          bool NotifyDoubleClick(Button control, int x, int y, Modifiers mods)
181          {
182             if(control != arrowControl)
183                codeEditor.AddControl();
184             return false;
185          }
186
187          bool NotifyReleased(Button control, int x, int y, Modifiers mods)
188          {
189             // ((Designer)GetActiveDesigner()).isDragging = false;
190             return true;
191          }
192       };
193       control.Create();
194       *y += 20;
195       return control;
196    }
197 }