How do you retrieve a new unit name from Delphi's open tools API
Asked Answered
D

3

7

I have written a wizard in Delphi XE, and it is working fine. However, I have not figured out yet how to access the generated default unit name (or form name or project name) that Delphi's OTA can create.

In my old-style wizard I was able to call ToolServices.GetNewModuleName to discover an available unit and form name that I could use when generating the associated source files. What is the equivalent in today's open tools API?

According to the ToolsAPI unit comments, I should return a blank from the IOTAModuleCreator.GetImplFileName method to have Delphi generate the file name. I am returning an empty string from this method, but still cannot see where I can access the file name that Delphi is generating.

Departed answered 16/11, 2010 at 16:27 Comment(1)
I still have not figured out why under some circumstances Delphi does not generate the default unit name, but I am convinced that it has something to do with which of the interfaces you implement. While Marco's solution permits you to ask Delphi directly for a name, the other two answers that appear at this time are also correct in that ModuleIdent (and similar parameters) usually contain this information. All three answers should be considered correct. I will post a followup when I learn more.Departed
B
4

There is a specific method for getting a new form and unit name:

(BorlandIDEServices as IOTAModuleServices).GetNewModuleAndClassName( '', UnitIdent, FormName, FileName);

I've used in a few examples and it seems to work fine.

Belding answered 18/11, 2010 at 14:47 Comment(1)
Thanks, Marco! This technique certainly works. I am also convinced that the proper use of IOTACreators will also do this, and I am modifying my wizard to create a project, and not just a unit module to see if that makes a difference in when the default name is automatically generated by Delphi. Depending on what I find I ultimately may mark all three of these answers as correct.Departed
A
2

The method IOTAModuleCreator.NewImplSource have a "ModuleIdent" parametter, it is the unit name.

Assumptive answered 16/11, 2010 at 16:45 Comment(1)
That is the method that I implemented to return an IOTAFile implementation that returns the source for the unit. The value of the ModuleIdent parameter passed to NewImplSource, however, is blank. My assumption was that if I returned a blank from the GetImplFileName, Delphi would generate the unit name and pass it to NewImplSource. Instead, ModuleIdent is blank.Departed
U
2

In my tests, it works as you expected (ModuleIdent parameter in NewImplSource method receives the new unit name). Check your implementation again, especially make sure that:

  • IOTACreator.GetUnnamed returns True
  • IOTACreator.GetExisting returns False
  • IOTACreator.GetCreatorType returns the appropriate identifier (sUnit, sForm, etc.) - I'm not sure about this but it might be important, too

Here is a working example. I just checked it and the code still seems to work as expected in Delphi XE.

Unleavened answered 17/11, 2010 at 14:26 Comment(3)
Thank you for your input. I am returning True from GetUnnamed, False from GetExisting, and ToolsAPI.sUnit from GetCreator (since I am creating a unit). Here is my IOTACreator decl. Did you implement any other interfaces? TCompEditorAppModuleCreator = class(TInterfacedObject, IOTACreator, IOTAModuleCreator). Also, did you also have an IOTAProjectCreator (and IOTAProjectCreate50) implementation in one of your implementations? My wizard did not, since I was creating only a unit, and not a project. I wonder if that makes a difference.Departed
Perhaps it's important to implement IOTAModuleCreator.GetOwner? My implementation returns the currently active project. In recent versions of Delphi, you can use IOTAModuleServices.GetActiveProject to get it.Unleavened
I am going to add an IOTAProjectCreator to my Wizard in order to create an entire project, not just a module, and I'll see if that makes a difference in Delphi's default name generation. Will post the results later.Departed

© 2022 - 2024 — McMap. All rights reserved.