Creating and Rendering a Custom Container Box and Container Items in C++ Builder/FMX - listbox

I am looking into sub-classing a container component to act as a "listbox" container of sub-classed TPanels that are acting as "listitem" members. Basically, my CustomListItems are TPanels that contain some images, labels, and/or other controls. These CustomListItems will be created dynamically and will have dynamic sizes. Their widths shall be equal to that of their parent container, but their heights might differ.
I want my container "listbox" to mostly behave like a standard TListBox, in that it shall be vertically SCROLLABLE, and that the added items shall be stacked on top of each other in the most automatic way possible. For example, doing:
CustomListItem* myCustomListItem = new CustomListItem(this, param1, param2, param3); // this = TOwner
myCustomListItem->Parent = myCustomListBox;
will automatically position the added child TPanel item in the first vacant slot of its CustomListBox container.
What I am seeking is the most standard way for implementing this parent/child, container/contained relationship. I am unsure what container component to sub-class and how to go about this. I am considering two approaches:
Possibly using a TLayout, TGridLayout, or TGridPanelLayout container if it is vertically scrollable and if it will handle rendering/positioning the added child TPanels (my CustomListItems) automatically.
If there is no "easy" or "standard" way to implement the above behavior, I would maybe consider using a plain TVertScrollBox as the "listbox" container, and then custom-handle the positioning and repositioning of the added items "manually".
For the latter approach, what I need is information about which events might trigger the insertion and removal of child items, and how to handle those events properly. For example, I would keep track of added items and their positions relative to the parent container. When new items are inserted, I can then manually handle positioning them correctly in the container. I can do that "internally" by overriding/overloading (not sure) the AddObject method, but I would still need to handle certain events for "external" insertion/removal of objects (e.g: myCustomListItem->Parent = myCustomListBox;)
I hope I made my question clear enough. Any help would be highly appreciated.

Related

Show TTreeViewItem's Expander button

I have a FireMonkey application that shows list of items in a TTreeView. Some items represent leaf objects, others represent containers.
My idea is not to pre-load the whole tree at once. I want to add the sub items to containers only after the user clicks on a particular container item.
For this to happen I need to show the user that the corresponding item is a container:
Is there any way to show the expander of a particular TTreeViewItem without adding any subitems using styles, RTTI etc.?
I'm using Delphi XE7.
There's a common trick using a "dummy" node within each unexpanded node. Each node which has items will have just an empty child node. When the user expands that node, check for this dummy node. If it's there, delete it, and then fill in the real items. I'm not exactly sure how similar the tree view is in Firemonkey and VCL, but the concept should work the same.

Copy a TPanel and all of it's Controls at runtime

Delphi-version5.
I have a file comparison app I am putting together and it has a Left and Right Panel on the main form. Each panel (pnlLeft and pnlRight) has an identical range of Controls and events only separated by name. i.e btnCheckLeft(... and btnCheckRight(... and I am having problems keeping the code changes synched between the two Panels.
I am wondering if I can make a single Panel (on a separate Form) with all of the controls and events and then at runtime, copy it twice, once for the pnlLeft an the other, the pnlRight to the main form. On that single Form, I can manage the coding just the once instead of trying to duplicate everything.
During the process, set the Align and the Event names to apply to each copy as in...
The first copy to the blank main form...
pnlLeft.Align:=alLeft;
pnlLeft.Button1Click(Sender)...
the second copy
pnlRight.Align:=alLeft;
pnlRight.Button1Click(Sender)...
How or is that possible? I think I need a Component of the full Panel, but I am not skilled enough to create a Component of this complexity yet.
I checked this
how to copy all the TLabels parented with a TPanel on delphi to another TPanel?
and this
Duplicating components at Run-Time
but don't think either will do what I need.
Thanks for anything you can help with.
The solution to your problem does not lie in duplicating a control at run time. Instead, design a TFrame object. Put all your panel-specific code on there, including any controls it needs. Then, simply instantiate it twice, and give each one a different name. Then you can refer to LeftPanel.btnCheck and RightPanel.btnCheck.

Actionscript: Position elements one after another automatically

Is it possible to make Sprites position themselves one after another automatically.
Similar to display:display-block in CSS.
For example i'm adding Sprites to some parent in a row, the way they are in xml skin file and they are automatically position themselves next to each other.
Is there an event, which tells that a child were added to the parent?
I could implement some extended Sprite, to position pushed elements automatically, by calculating where the last element is located?
Why the hell Actionscipt doesn't have something like CSS in it? It's a pain in the ass to build UI inside it, compared to HTML...
Madness...
In case of "raw" ActionScript you should either search for an appropriate library or do it your self. ActionScript doesn't define any CSS like logic.
On the other hand Flex does, but you will not find any CSS that let you define layout. CSS is used to modify visual appearance of containers and components in Flex. Layout is usually done with the layout property of the container classes provided by Flex. For example yo can use a Group and set its layout property to an instance of HorizontalLayout or VerticalLayout. This will automatically position all child components of the group either in x or in y direction. In case of using Flex you cannot use sprites directly, but there are many components you can use. Maybe there is one for your purposes.
See the Flex API documentation. The spark.components package will be a good starting point.

Delphi: Managing multiple instances of TFrames

In this article there is an explanation of how to use TFrames as an alternative to TTabSheets in a Delphi PageControl.
I've been pondering a similar exercise, which the above doesn't seem to provide the solution for, and for which my solution seems to be overly complicated.
The requirement is for a tabbed interface, where each tab can be one of a number of different designs. Each design is implemented as a TFrame.
Due to the nature of the application being designed, we may have multiple copies of any frame open at any particular moment (with the content of each tab differing, but not the design) so that the user can compare the details of 2/3 different items at the same time.
For example in one session we may have 3 tabs open, all of Frame design A.
On another occasion we may have 3 tabs open each of Frame A, B and C.
The design needs to be flexible enough that we can add Frames to the design on request.
At the moment the solution that I have is to have separate TLists managing each type of Frame that we have open, with perhaps a master TList to keep track of the tabs that are open. As I said, over complicated.
Does anyone have a suggestion of how this could be handled more simply?
I'd probably leave out the master TList of frame instances.
If you need them you'll be able to get them by interrogating the TPageControl directly or through each of your individual TLists for each frame type.
Apart from that your approach sounds reasonable.
I have an application that manages frames on a single panel with my own menu control to control which frame is visible. When I need to make a frame visible I simple set visible:=false for every frame on the panel except for the one that I want. I am in control of what goes onto the panel so I know that at the very least each control is a Tframe and I can get to each frame by iterating over the Panel's Controls property. I then use interfaces to communicate between my main form and my frames.
Now if you want to use a standard windows tabbed interface you could still use the page control as you have suggested, you know that each TtabSheet has a single Tframe on it and you can check it's type and work with it as required. I don't see why you'd need a Tlist because if you really need to get at the "list of Tframes" you could build it dyamically anyway by iterating over the TtabSheets in the page control.
An alternative which would work similarly to my first approach, but gets you nice Windows tabs, would be to use a TtabControl instead of a TpageControl. With the TtabControl you basically just get a Tstrings instance (in the Tabs property) which represents all of the tabs. Since it's a Tstrings you can associate an object (ie your Tframe) with each item and hence each tab. When you click a tab you hide everything and show the correct Tframe. You also have your list because it's attached to the TtabControl via the Tabs property. You just have to handle the visibility of the frames yourself.
We use a TPageControl and create runtime a TTabSheet descendant, which has a new property for our own TFrame (we do not need to scan through .Controls or .Components to search our frame each time).

Design-time drag and drop in Delphi?

Before Delphi 2006 (I think) introduced the TFlowPanel and TGridPanel, I did a control that was similar in concept. It still does a couple of things those controls do not do, and when upgrading my code to Delphi 2009, I decided to add a couple of enhancements to that as well.
Right now, the order of the child controls is determined by their creation order. The FlowPanel and GridPanel show a better way with ControlIndex and other filtered properties, but I was wondering if there is a way to handle drag and drop reordering in design-time? As far as I can tell, dragging an edit control and dropping it onto my panel doesn't call anything that I can access at design-time.
I was half-fantasising about a way to either detect the drop operation directly, or to perhaps detect when a control is moved so I can determine where it should go.
Any ideas?
Update:
OK, got it working. The container control was already overriding AlignControls to manage the placement of the controls. When you drag the nested control and drop it, AlignControls is again called. I then compared the new coordinates of the control with the other controls in the list and moved it to the appropriate position.
There were a couple of problems that I had to work through (mostly related to the many calls to AlignControls) but the basic concept is simple enough. Thanks to all the commenters for all the help.
You can't drag a control that's already on the form and drop it onto your panel. Dragging is only for moving a control, not for changing its parent. To change the parent, cut and paste.
If the control is already on your panel, and you want to move it to another position on your panel, then the panel can control the layout by overriding the TWinControl.AlignControls method. When a control is moved, its SetBounds method is called, and among the things tha happens is that it calls AlignControl(Self) on its parent window. That calls AlignControls. Look in Controls.pas, and you'll see that that's a complicated method, but it's what is responsible for the layout of the children on a control, and that's exactly what you're planning to change.
Perhaps some of these suggestions might help.
You can re-parent a control in the designer without having to do cut-and-paste. View the structure pane, and simply drag the visual control to the node of another parent in the structure pane. If you have things in a flowpanel, drag everything out of the flow panel and drag them back in the order that you want them to be.
(You can re-parent ANY visual control this way, without changing anything other than its parent. I highly recommend doing it this way.)
You can view the form as text, and move the declaration order around in there -- but obviously you'll need to be careful when editing the "resource" file directly.
You can set tab order in the designer, so you could make a different control based on tab order that works as you want. You can right click on the form and change the creation order of the non-visual controls, but that doesn't work with visual controls.
Have you tried to write an "OnDragDrop" event for your grid component, where you check if your component is in design mode?
I haven't written such a component yet, but I don't see why the event shouldn't trigger.

Resources