Using the same frame multiple times lazarus / 31-LCL ---------------------------------------------------------------------- MarkMLl · 2022-10-21 07:21 Once the LCL has initialised a frame that has been placed on a form by the IDE, is it possible (and safe) to manually copy (the reference to) that frame to e.g. a different notebook page or possibly to a different modal form? The scenario is that I'm writing a control program for a small/cheap bench instrument, and while by-and-large there's an intuitive flow through notebook pages there's one place where I need to duplicate some settings. However the instrument's own UI uses switching between pages to start/stop various activities, and I'm trying to preserve that as far as possible in the notebook flow. So there's one place where I need to refer to the content of a different notebook page, preferably without switching notebook pages as such. MarkMLl ---------------------------------------------------------------------- mas steindorff · 2022-10-21 07:41 Who will be the frame's parent? Who frees the frame on close?  May be better to have 2 frames work one record/set of data, Mas ---------------------------------------------------------------------- MarkMLl · 2022-10-21 07:58 Maybe, yes. I'm still working through the possibilities: one, which might work out to be the simplest to code, is to inhibit the "switching page changes mode" behaviour when in one particular mode. This is for a JDS6600 signal generator which has a multi-page LCD UI, but if you send it commands over the USB/serial connection you can get it to e.g. change signal burst properties on the fly. MarkMLl ---------------------------------------------------------------------- cdbc · 2022-10-21 11:11 Hi @MarkMLI: I was fiddling with frames too, and (mistakenly) thought they'd be stored in some central (to lazarus) repository, but alas, when I tried to use the same frame in another project...  Then I tried the manual way, copying the frame-files and inserting in lpi- file and uses clause, then put it on tabsheet in the other project, it kinda worked, but was all botched  :'( Who would have thunk it... I haven't started digging in the ide yet, but i my mind, there has to be a better way? I thought frames were for code reusing. Regards Benny ---------------------------------------------------------------------- Joanna · 2022-10-21 11:15 I don’t know if this is relevant , but I use frames with tcdpagecontrol and destroy them when I change pages and store the relevant data in an inifile.   In this way a frame on one page can  update data in the inifile pertaining to another frame. When the frame is recreated the data will be retrieved from inifile. The term frame can be confusing because it means so many things on different platforms. When it comes to lazarus it’s best to think of it as a gui control. Mark in my experience it’s better to create frames at runtime if you plan to reuse them. ---------------------------------------------------------------------- af0815 · 2022-10-21 12:20 I have some multi-used frames in one common (git-)repository and the other projects can this use as a sub-repository. So the code of the frames is common and multiused. BUT i always use the frames ONLY dynamic in my code. I never use Frames placed in the IDE because this works not so good. If you make a new eventhandler by accident, clear this in the code, it never works, because there are fragments in the lfm and this will never correctd by automatic. So you have suddenly a never working project. ---------------------------------------------------------------------- MarkMLl · 2022-10-21 12:26 Quote from: cdbc on October 21, 2022, 01:11:34 pm@MarkMLI: I was fiddling with frames too, and (mistakenly) thought they'd be stored in some central (to lazarus) repository, but alas, when I tried to use the same frame in another project...  Then I tried the manual way, copying the frame-files and inserting in lpi-file and uses clause, then put it on tabsheet in the other project, it kinda worked, but was all botched  :'( Who would have thunk it... I haven't started digging in the ide yet, but i my mind, there has to be a better way? I thought frames were for code reusing. No, they're for component organisation and reuse in the scope of a single project. If we were talking about a (vector) drawing application or a CAD program we'd probably be using the term "group" here. As far as copying frames around is concerned: I don't think I've tried doing that but- like copying forms- I think that what you need is the .pas and .lfm, and then adding the .pas to the project using the IDE's "Project Information" form. I think you'll find that a lot of positioning etc. can be tightened up using the Anchors facility, which is effectively a distillation of alignment, border spacing and so on. However there might be problems working across different widgetsets and with different DPI settings... don't worry about that overmuch since at least the World is moving away from resizeable windows now that the rule is that absolutely everything has to run full-screen on a 'phone (I'm not sure to what extent that's dripping with venomous sarcasm or whether I'm serious, but at least there isn't much pressure to encapsulate everything for a Facebook page yet). In terms of graphical component reuse across programs, I don't know the extent to which you can put things like this in a package (I specifically mean bundled in such a way that the Lazarus IDE etc. can interpret the content) but I think the orthodox way would be to get involved with writing LCL-based components and installing them in the IDE... which has immediate implications if you want to be able to ship a project to somebody else. As far as I'm concerned, components and IDE extensions are badly- documented arcana and I am glad to say that I've never been in a position where there wasn't some workaround compatible with my substantial workload. Noting Joanna's point, I've streamed (term used loosely) menu structure and current settings to an extended-format .ini file before now. However I've got reservations about doing something like that to actually create content in place of using the IDE: I've certainly found the combination of tabbed pagecontrol+frames to be particularly valuable when- e.g.- setting up a chain of rules to process incoming data. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2022-10-21 12:30 Quote from: af0815 on October 21, 2022, 02:20:36 pmI have some multi- used frames in one common (git-)repository and the other projects can this use as a sub-repository. So the code of the frames is common and multiused. BUT i always use the frames ONLY dynamic in my code. I never use Frames placed in the IDE because this works not so good. If you make a new eventhandler by accident, clear this in the code, it never works, because there are fragments in the lfm and this will never correctd by automatic. So you have suddenly a never working project. (I think, this is Delphi compatible SCNR ) I remarked a few days ago on how it was possible to get into a situation where one had to clean things up by editing the .lfm as text. However I've also been reminded by the stuff I'm currently working on that event handlers do have to be installed by associating them with the frame after it's been placed on a form, and I've also found a few cases where I've had to delete and reinstantiate a frame on a form in order to get modifications picked up. MarkMLl ---------------------------------------------------------------------- Nicole · 2022-10-21 12:37 This I asked myself as well some weeks ago. The community answered me and I tried. Let me put the result in short: I re-did it and will not do this again. Too complex against too small advantages. ---------------------------------------------------------------------- MarkMLl · 2022-10-21 12:47 You did /what/: used frames in general or tried sharing a frame across forms? If you're going to say things like that I suggest at least providing a link to previous discussion. MarkMLl ---------------------------------------------------------------------- jamie · 2022-10-21 16:31 Write a function to create and set the parent, owner and all the events to 1 set. The sender will report which frame is calling it Also, you can reference the owner which would be a tabsheet in this case. ---------------------------------------------------------------------- mas steindorff · 2022-10-21 19:26 I find Frame very useful and use them a lot but the IDE has had bugs dropping them in to position at design time.  later if I update the Frame, the drop Frame does not update and there was other issues I think are the same Joanna pointed out. Now I always place them at run time on a panel with 3 simple lines Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   myFram := TFram_my_Data.Create(PanelFram); // will auto free  myFram.Parent := PanelFram;  // " "  myFram.Align  := alClient;       // expand the frame to fit the panel ...  the 2nd line may be redundant but I found I needed it for one project and have always kept it. The ... is where I may assign onShow events or data records to the frame as needed.  A simple frame can just be used to translate a data set /record that lives outside the frame so if two or more frames point to the same record, they show the same display. Is not that what your after MarkMLI?  then you do not need to keep creating and destroying frames.  just update the "external to the frame" record. I have done that before and I've had no issues having up to 5 frames scattered throughout my software showing and modifying a common data source.  Mas ---------------------------------------------------------------------- Lulu · 2022-10-21 19:49 I also use frames a lot, in the same way as Mas Steindorff. There is no problem to encapsulate a predefined frame in another predefined frame in another... etc. This way the GUI part and its code is greatly modularized ---------------------------------------------------------------------- MarkMLl · 2022-10-21 19:55 Quote from: mas steindorff on October 21, 2022, 09:26:54 pmIs not that what your after MarkMLI?  then you do not need to keep creating and destroying frames.  just update the "external to the frame" record. No, I was trying to find out whether a single frame instance- not just multiple independent instantiations- could be referred to multiple times by different pagecontrol tabs etc. Apart from that I broadly agree, although most- not necessarily all- of the deferred update problem can be fixed by restarting the IDE. It's been like that for so long that I presume it's a feature we have to live with, but the possibility of modified properties being unintentionally saved with the form is far more serious. MarkMLl ---------------------------------------------------------------------- Joanna · 2022-10-21 22:05 There are a few more things to be aware of with frames such as your static constants values will be shared by all copies of the frame class. I also highly recommend against setting any events using the frame object inspector, it has resulted in some crashes when I tried to use frame at different location. I use almost entirely frames in my gui applications because they are modular and easy to test. I have many layers of frame nesting starting with small frames. For example  a base frame with only a flowpanel which is inherited by a frame which has label and combobox inside the flow panel. The label combobox frames can be grouped within a flowpanel frame  To make a date and time input frame. When making changes to nested frames created at design time There can be some problems that are only fixable by opening up lfm file and removing the parts of frame no longer used. Changing things in .pas fIle does not remove from lfm file it seems and the result will be a crash with a cryptic message about something being undefined or not found. I’ve even managed to crash the ide when playing with frames. I like being able to have a frame that only creates child frames under certain conditions like if an sql table contains text field etc. The idea of resizable windows going away horrifies me.. I have designed my gui applications for desktop to work well with a large range of widths. The frames and controls flow to fill the space efficiently. My only problem is I can’t do multiline with tcdpagecontrol and tpagecontrol refuses to let me control the tab colors. ---------------------------------------------------------------------- MarkMLl · 2022-10-22 05:59 Quote from: Joanna on October 22, 2022, 12:05:11 amI also highly recommend against setting any events using the frame object inspector, it has resulted in some crashes when I tried to use frame at different location. I use almost entirely frames in my gui applications because they are modular and easy to test. I have many layers of frame nesting starting with small frames. ...Changing things in .pas fIle does not remove from lfm file it seems and the result will be a crash with a cryptic message about something being undefined or not found. I’ve even managed to crash the ide when playing with frames. My recent experience is that setting an event at the point of design quite simply had no effect... but I didn't investigate what had actually gone into which .lfm and what was being done with that information at startup and/or instantiation. I don't know if things were the same the last time I used frames moderately heavily. I note your comment about nested frames. However one has to wonder whether this was anticipated by the implementors and whether it is contributing to the instability you report. MarkMLl ---------------------------------------------------------------------- jamie · 2022-10-22 17:21 Frames has always been a problem cataloging them.  You create a frame, use it in a project to later decide that you need to make changes or additions to it.  The IDE should offer at that time to update your current use of it, but it does not unless that has changed?  What should happen is the IDE should give you a choice to catalog existing frame with a version number and that number should be somewhere in the lfm file of the project so when loaded it will simple use that one.  If you decide to use a newer version of that frame the IDE should allow you to simply update the form and update the lfm file to indicate that.  Of course, you can also decide to drop back a version etc.  basically a Version control for the Frames built into the IDE and allow the IDE to update the use of it when changes made. Currently it's cumbersome. ---------------------------------------------------------------------- Joanna · 2022-10-22 23:19 I’ve found that if you create events using the object inspector it will auto create them in the .pas file for you. I only do this when I’m not sure of what the parameter types of the event should be. I’m not sure if nested frames was planned or not it certainly should have been expected. I know that nested frames of same type can be nested which is a bit confusing to code. It’s also possible to have frames with controls in them be inherited by other frames that added more controls but I found that to be tricky getting them to work right. Sometimes things created at runtime have better behavior than if They were created at design time. I’m not sure why that is. I’m sorry I don’t remember the exact details.. Jamie have you ever tried inheriting frames from other frames? That might cover the additions part. ---------------------------------------------------------------------- MarkMLl · 2022-10-23 09:30 Quote from: Joanna on October 23, 2022, 01:19:54 amI’m not sure if nested frames was planned or not it certainly should have been expected. I know that nested frames of same type can be nested which is a bit confusing to code. It would be interesting if @PascalDragon had any comment on this: I think I remember from discussion on one of the mailing lists that there was some issue in the early days of frames that required (or at least strongly suggested) an updated compiler, I think it was around 2.6.4. Apart from that I agree that they particularly shine when instantiated explicitly, e.g. into a newly-created notebook page... which obviously /requires/ the programmer to be explicit as to how events are being handled etc. Thinking about various comments in this thread, I find myself wondering whether some mechanism to share (broadcast?) messages between same-class... the information conveyed could be entirely class-specific. MarkMLl ---------------------------------------------------------------------- Joanna · 2022-10-23 12:04 How does broadcast messages work for a class ? I’ve never used it. ---------------------------------------------------------------------- MarkMLl · 2022-10-23 12:12 Quote from: Joanna on October 23, 2022, 02:04:40 pmHow does broadcast messages work for a class ? I’ve never used it. No, I'm talking about some sort of internal IPC- not Windows-style Messages or anything like that. If the participants were within the same thread and of the same class then they should be free to move arbitrary binary data around- or even just a "I want attention" notification- since even if the IDE sometimes has problems updating things the compiler seems to get it right. Subject to any potential version issues introduced by different versions of what was supposed to be the same frame in different packages. MarkMLl ---------------------------------------------------------------------- jamie · 2022-10-23 13:24 there are Observer and observed members at the base of most of these classes. with that, you can attach events to notify others of things happening. ---------------------------------------------------------------------- howardpc · 2022-10-23 13:28 Quote from: Joanna on October 23, 2022, 02:04:40 pmHow does broadcast messages work for a class ? I’ve never used it. Delphi introduced a Dispatch() method in TObject, which FPC also naturally supports. However it is very little used in the LCL. I think the only component that really uses the functionality is TCustomGrid which uses the method to send TGridMessage records in its low level editor. ---------------------------------------------------------------------- Joanna · 2022-10-23 14:00 I’m not familiar with how to do multithreaded applications. I’ve heard about those dispatch messages and observed things but I’m not sure what they would be used for. I wouldn’t mind looking at some code that shows what to do with them. I either assign event procedures to gui controls or use properties wherein setting the property calls a procedure which I find similar to triggering an event. ---------------------------------------------------------------------- howardpc · 2022-10-23 14:37 Quote from: Joanna on October 23, 2022, 04:00:19 pmI’m not familiar with how to do multithreaded applications. You don't need to be to use Dispatch() which operates within the single threaded main loop that the Application instance runs for LCL GUI applications. ---------------------------------------------------------------------- MarkMLl · 2022-11-10 13:58 Quote from: Joanna on October 22, 2022, 12:05:11 amI also highly recommend against setting any events using the frame object inspector, it has resulted in some crashes when I tried to use frame at different location. While the pain is raw after an extended editing session: I don't know to what extent these are "design features" rather than "errata", but experience over the last few days suggests at least two issues: * Adding an event handler in the frame designer after the frame has been placed on a form is likely to result in IDE issues which can only be resolved by deleting and replacing the frame on the form. For this reason, it is worth sticking to the default frame naming convention and having a panel on the form into which a frame can be dropped with client alignment. * Changing a property in the frame designer first copies the existing value to any copies of that frame which have been placed on a form in an "inherited" section, even if the form has made no previous attempt to override them. I presume that this could also be fixed by a delete- replace, but it can also be fixed by closing the IDE and removing all inherited properties in the form's .lfm. ...plus obviously * Editing a property of a frame in the context of a form (i.e. rather than in the original frame editor) might not have the intended result. So far, I have not seen a single case where an "inherited" block in a .lfm is anything other than an indication of problems. I suspect that nested frames would, if anything, make the above worse. Debian Linux x86_64, Lazarus 2.2.4 with FPC 3.2.2 using the GTK2 widgetset for both the IDE and target. MarkMLl ---------------------------------------------------------------------- mas steindorff · 2022-11-10 20:53 I totally agree with your findings which is why I create and place my frames at run time.  however, I have not had any issues with nested frames.  I tend to use frames as components as well as mini windows.  Maybe as components, they I have them only interact with their parent frame and are not changed much once I have them done.  I will often have 8 to 16 of these included on a subframe as a way of setting /showing bits of a word in a more human view however it is the job of the parent frame to pass them the data. like you, I'll drop a frame on a panel at design time just to see how it looks but I then remove it one I get close to running the app.  I like frames even as a "use once" mode as they can remove a lot of code from the "main" window.  Otherwise the code gets too long  or you end up with scatter code where other units modify the main forms' visual components (a distasteful approach for an OOPS programmer). Mas ---------------------------------------------------------------------- MarkMLl · 2022-11-10 21:19 Quote from: mas steindorff on November 10, 2022, 09:53:14 pmI like frames even as a "use once" mode as they can remove a lot of code from the "main" window.  Otherwise the code gets too long  or you end up with scatter code where other units modify the main forms' visual components (a distasteful approach for an OOPS programmer). That's why, in the current project, I fairly abruptly increased the number of frames I was using: it made sense to move each notebook page into its own unit. I suspect that the crucial thing is whether nested frames cause inherited blocks to be inserted in intermediate .lfm files, or only in that describing the main form. I'm not going to raise a bug on this, pending comments from the local IDE maintainers: I am very much disinclined to waste even a few minutes of my time to be told that I'm up against something they have no intention of changing. But my own feeling is that there is something wrong with the code that decides when to insert an inherited block, and possibly also with the event handling. MarkMLl ---------------------------------------------------------------------- cdbc · 2022-11-11 13:49 Hi @MarkMLI: I'm with Jamie on this one... All Tframes descend from TPersistent and thereby implement the IFPOObserved interface. One could implement an observer class in the center of it all, and let it register itself with every TFrame, thus all communication, you could send to this ObserverObject, which would then delegate, etc... Amongst others, that's what I do, atm. Ohh, and dynamic instantiation only. Regards Benny ---------------------------------------------------------------------- af0815 · 2022-11-11 15:43 Quote from: mas steindorff on November 10, 2022, 09:53:14 pmI totally agree with your findings which is why I create and place my frames at run time.  however, I have not had any issues with nested frames.  I tend to use frames as components as well as mini windows.  I am too. Data goes to the frame with properties and data between the frames with events, maintained by the parents. One problem in the discussion is, to create the handling rules of frames in the designer. And a way to check if something is changed in the placed frame. Normal the IDE knows if a files is external changed (i think by timestamp). But not if the frame have changed. The actual rules should be known (eg. published in the wiki) and then it is possible to declare the rules for a better implementation. But i see there will be some issue :-) in removing a handler. ---------------------------------------------------------------------- marcov · 2022-11-11 22:45 I have had issues with reparenting frames (changing the parent to another form) under Delphi. Some components (like popupmenu) don't follow the hierarchy for their events. ---------------------------------------------------------------------- MarkMLl · 2022-11-12 07:54 Quote from: MarkMLl on November 10, 2022, 02:58:04 pm... For this reason, it is worth sticking to the default frame naming convention and having a panel on the form into which a frame can be dropped with client alignment. It occurred to me overnight that, while using client alignment is pretty much unavoidable, it might be contributing to the problem: if the containing form is opened hence loads the frames, one of things it might try to save is the actual size and position of some of the components. However, attempting to prove or disprove the above might have helped localise the problem. I'd got a "clean" collection of .lfms, i.e. no spurious inherited blocks. I opened the form that contains various frames, added a space to the corresponding .pas file, and closed the form. On inspecting the form, it had gained inherited blocks for TCheckGroup, TRadioGroup, TCheckBox, TGroupBox and TStringGrid controls, but not for various page controls, buttons etc. The distinguishing feature of the CheckGroup and RadioGroup controls in particular is that they tend to have an ItemIndex and/or various checked states that are set to non-default values as part of the design process... however it is sizes rather than these which are being saved. I think it's something to do with anchors: almost all of the inserted blocks look like this: Code: [Select]          inline FramePulse1: TFramePulse             Height = 477             Width = 691             Align = alClient             ClientHeight = 477             ClientWidth = 691             DesignLeft = 1320             DesignTop = 166             inherited CheckBoxPulseEnabled: TCheckBox               AnchorSideRight.Control = FramePulse1                                                          Left = 594                                                                                  end                                                                                          end                                                                                  Note the way that a component is anchored to a frame. and that frame has been aligned alClient. MarkMLl