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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use windows::Win32::System::Com::{IDispatch, VARIANT};

use crate::idispatch_ext::IDispatchExt;
use crate::types::*;
use crate::variant_ext::VariantExt;

/// A component that has an IDispatch value. Every component needs this, and this trait guarantees that.
pub trait HasDispatch<T = Self> {
    /// Get the IDispatch object for low-level access to this component.
    fn get_idispatch(&self) -> &IDispatch;
}

/// A GuiBox is a simple frame with a name (also called a "Group Box"). The items inside the frame are
/// not children of the box. The type prefix is "box".
pub trait GuiBoxMethods<T: GuiVComponentMethods = Self>: GuiVComponentMethods<T> {
    /// Height of the GuiBox in character metric.
    fn char_height(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("CharHeight")?.to_i64()?)
    }

    /// Left coordinate of the GuiBox in character metric.
    fn char_left(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("CharLeft")?.to_i64()?)
    }
    /// Top coordinate of the GuiBox in character metric.
    fn char_top(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("CharTop")?.to_i64()?)
    }
    /// Width of the GuiBox in character metric.
    fn char_width(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("CharWidth")?.to_i64()?)
    }
}

/// GuiComponent is the base class for most classes in the Scripting API. It was designed to allow generic
/// programming, meaning you can work with objects without knowing their exact type.
///
/// Note: Type is named `kind` due to Rust restrictions.
///
/// Note: Parent is not currently implemented.
pub trait GuiComponentMethods<T: HasDispatch = Self>: HasDispatch<T> {
    /// This property is TRUE, if the object is a container and therefore has the Children property.
    fn container_type(&self) -> crate::Result<bool> {
        Ok(self.get_idispatch().get("ContainerType")?.to_bool()?)
    }

    /// An object id is a unique textual identifier for the object. It is built in a URLlike formatting,
    /// starting at the GuiApplication object and drilling down to the respective object.
    fn id(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("Id")?.to_string()?)
    }

    /// The name property is especially useful when working with simple scripts that only access dynpro
    /// fields. In that case a field can be found using its name and type information, which is easier to
    /// read than a possibly very long id. However, there is no guarantee that there are no two objects
    /// with the same name and type in a given dynpro.
    fn name(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("Name")?.to_string()?)
    }

    /// The type information of GuiComponent can be used to determine which properties and methods an object
    /// supports. The value of the type string is the name of the type taken from this documentation.
    fn kind(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("Type")?.to_string()?)
    }

    /// While the Type property is a string value, the TypeAsNumber property is a long value that can
    /// alternatively be used to identify an object's type . It was added for better performance in methods
    /// such as FindByIdEx. Possible values for this property are taken from the GuiComponentTypeenumeration.
    fn kind_as_number(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("TypeAsNumber")?.to_i64()?)
    }
}

/// This interface resembles GuiVContainer. The only difference is that it is not intended for visual objects
/// but rather administrative objects such as connections or sessions. Objects exposing this interface will
/// therefore support GuiComponent but not GuiVComponent. GuiContainer extends the GuiComponent Object.
pub trait GuiContainerMethods<T: GuiComponentMethods = Self>: GuiComponentMethods<T> {
    /// Search through the object's descendants for a given id. If the parameter is a fully qualified id, the
    /// function will first check if the container object's id is a prefix of the id parameter. If that is the
    /// case, this prefix is truncated. If no descendant with the given id can be found the function raises an
    /// exception.
    fn find_by_id<S>(&self, id: S) -> crate::Result<SAPComponent>
    where
        S: AsRef<str>,
    {
        let comp = GuiComponent {
            inner: self
                .get_idispatch()
                .call("FindById", vec![VARIANT::from_str(id.as_ref())])?
                .to_idispatch()?
                .clone(),
        };
        Ok(SAPComponent::from(comp))
    }

    /// This collection contains all direct children of the object.
    fn children(&self, n: u32) -> crate::Result<GuiSession> {
        Ok(GuiSession {
            inner: self
                .get_idispatch()
                .get_named("Children", VARIANT::from_i32(n as i32))?
                .to_idispatch()?
                .clone(),
        })
    }
}

/// The GuiVComponent interface is exposed by all visual objects, such as windows, buttons or text fields.
/// Like GuiComponent, it is an abstract interface. Any object supporting the GuiVComponent interface also
/// exposes the GuiComponent interface. GuiVComponent extends the GuiComponent Object.
pub trait GuiVComponentMethods<T: GuiComponentMethods = Self>: GuiComponentMethods<T> {
    // /// This function dumps the state of the object. The parameter innerObject may be used to specify
    // /// for which internal object the data should be dumped. Only the most complex components, such as
    // /// the GuiCtrlGridView, support this parameter. All other components always dump their full state.
    // /// All components that support this parameter have in common that they return general information
    // /// about the control’s state if the parameter “innerObject” contains an empty string. The
    // /// available values for the innerObject parameter are specified as part of the class description
    // /// for those components that support it.
    // fn dump_state<S>(&self, inner_object: S) -> crate::Result<GuiCollection>
    // where
    //     S: AsRef<str>,
    // {
    //     Ok(GuiCollection {
    //         inner: self
    //             .get_idispatch()
    //             .call("DumpState", vec![VARIANT::from_str(inner_object.as_ref())])?
    //             .to_idispatch()?,
    //     })
    // }

    /// This function can be used to set the focus onto an object. If a user interacts with SAP GUI,
    /// it moves the focus whenever the interaction is with a new object. Interacting with an object
    /// through the scripting component does not change the focus. There are some cases in which the
    /// SAP application explicitly checks for the focus and behaves differently depending on the
    /// focused object.
    fn set_focus(&self) -> crate::Result<()> {
        let _ = self.get_idispatch().call("SetFocus", vec![])?;
        Ok(())
    }

    /// Calling this method of a component will display a red frame around the specified component
    /// if the parameter on is true. The frame will be removed if on is false. Some components such
    /// as GuiCtrlGridView support displaying the frame around inner objects, such as cells. The
    /// format of the innerObject string is the same as for the dumpState method.
    fn visualize(&self, on: bool) -> crate::Result<()> {
        let _ = self
            .get_idispatch()
            .call("Visualize", vec![VARIANT::from_bool(on)])?;
        Ok(())
    }

    // TODO
    // fn acc_label_collection(&self) -> crate::Result<GuiComponentCollection> {
    //     Ok(self.get_idispatch().get("AccLabelCollection")?)
    // }

    /// An additional text for accessibility support.
    fn acc_text(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("AccText")?.to_string()?)
    }

    /// An additional text for accessibility support.
    fn acc_text_on_request(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("AccTextOnRequest")?.to_string()?)
    }

    /// An additional tooltip text for accessibility support.
    fn acc_tooltip(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("AccTooltip")?.to_string()?)
    }

    /// An object is changeable if it is neither disabled nor read-only.
    fn changeable(&self) -> crate::Result<bool> {
        Ok(self.get_idispatch().get("Changeable")?.to_bool()?)
    }

    /// Tooltip text generated from the short text defined in the data dictionary for the given screen
    /// element type.
    fn default_tooltip(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("DefaultTooltip")?.to_string()?)
    }

    /// Height of the component in pixels.
    fn height(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("Height")?.to_i64()?)
    }

    /// If the object has been assigned an icon, then this property is the name of the icon, otherwise
    /// it is an empty string.
    fn icon_name(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("IconName")?.to_string()?)
    }

    /// The property is TRUE if the component's text is visualized in the SAP symbol font.
    fn is_symbol_font(&self) -> crate::Result<bool> {
        Ok(self.get_idispatch().get("IsSymbolFont")?.to_bool()?)
    }

    /// Left position of the element in screen coordinates
    fn left(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("Left")?.to_i64()?)
    }

    /// An object is modified if its state has been changed by the user and this change has not yet been
    /// sent to the SAP system.
    fn modified(&self) -> crate::Result<bool> {
        Ok(self.get_idispatch().get("Modified")?.to_bool()?)
    }

    /// If the control is hosted by the Frame object, the value of the property is this frame. Overwise
    /// NULL.
    fn parent_frame(&self) -> crate::Result<GuiComponent> {
        Ok(GuiComponent {
            inner: self
                .get_idispatch()
                .get("ParentFrame")?
                .to_idispatch()?
                .clone(),
        })
    }

    /// The y position of the component in screen coordinates.
    fn screen_left(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("ScreenLeft")?.to_i64()?)
    }

    /// The x position of the component in screen coordinates.
    fn screen_top(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("ScreenTop")?.to_i64()?)
    }

    /// The value of this property very much depends on the type of the object on which it is called.
    /// This is obvious for text fields or menu items. On the other hand this property is empty for toolbar
    /// buttons and is the class id for shells. You can read the text property of a label, but you can’t
    /// change it, whereas you can only set the text property of a password field, but not read it.
    fn text(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("Text")?.to_string()?)
    }

    /// The value of this property very much depends on the type of the object on which it is called.
    /// This is obvious for text fields or menu items. On the other hand this property is empty for toolbar
    /// buttons and is the class id for shells. You can read the text property of a label, but you can’t
    /// change it, whereas you can only set the text property of a password field, but not read it.
    fn set_text<S>(&self, value: S) -> crate::Result<()>
    where
        S: AsRef<str>,
    {
        self.get_idispatch()
            .set("Text", VARIANT::from_str(value.as_ref()))?;
        Ok(())
    }

    /// The tooltip contains a text which is designed to help a user understand the meaning of a given text
    /// field or button.
    fn tooltip(&self) -> crate::Result<String> {
        Ok(self.get_idispatch().get("Tooltip")?.to_string()?)
    }

    /// Top coordinate of the element in screen coordinates.
    fn top(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("Top")?.to_i64()?)
    }

    /// Width of the component in pixels.
    fn width(&self) -> crate::Result<i64> {
        Ok(self.get_idispatch().get("Width")?.to_i64()?)
    }
}

/// An object exposes the GuiVContainer interface if it is both visible and can have children. It will then
/// also expose GuiComponent and GuiVComponent. Examples of this interface are windows and subscreens,
/// toolbars or controls having children, such as the splitter control. GuiVContainer extends the GuiContainer
/// Object and the GuiVComponent Object.
pub trait GuiVContainerMethods<T: GuiContainerMethods + GuiVComponentMethods = Self>:
    GuiVComponentMethods<T>
{
    // TODO fn find_all_by_name(&self);
    // TODO fn find_all_by_name_ex(&self);

    /// Unlike FindById, this function does not guarantee a unique result. It will simply return the first
    /// descendant matching both the name and type parameters. This is a more natural description of the
    /// object than the complex id, but it only makes sense on dynpro objects as most other objects do not
    /// have a meaningful name. If no descendant with matching name and type can be found, the function
    /// raises an exception.
    fn find_by_name<S>(&self, name: S, kind: S) -> crate::Result<SAPComponent>
    where
        S: AsRef<str>,
    {
        Ok(SAPComponent::from(GuiComponent {
            inner: self
                .get_idispatch()
                .call(
                    "FindByName",
                    vec![
                        VARIANT::from_str(name.as_ref()),
                        VARIANT::from_str(kind.as_ref()),
                    ],
                )?
                .to_idispatch()?
                .clone(),
        }))
    }

    /// This method works exactly like FindByName, but takes the type parameter with data type long coming
    /// from the GuiComponentType enumeration. See also GuiComponentType.
    fn find_by_name_ex<S>(&self, name: S, kind: i64) -> crate::Result<SAPComponent>
    where
        S: AsRef<str>,
    {
        Ok(SAPComponent::from(GuiComponent {
            inner: self
                .get_idispatch()
                .call(
                    "FindByNameEx",
                    vec![VARIANT::from_str(name.as_ref()), VARIANT::from_i64(kind)],
                )?
                .to_idispatch()?
                .clone(),
        }))
    }
}