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.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Cary Jensen
  • 3,751
  • 3
  • 32
  • 55
  • 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. – Cary Jensen Nov 21 '10 at 10:20

3 Answers3

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.

Marco Cantù
  • 1,195
  • 7
  • 13
  • 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. – Cary Jensen Nov 18 '10 at 15:39
2

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

Henri Gourvest
  • 989
  • 8
  • 12
  • 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. – Cary Jensen Nov 16 '10 at 17:43
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.

Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • 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. – Cary Jensen Nov 18 '10 at 11:08
  • 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. – Ondrej Kelle Nov 18 '10 at 12:03
  • 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. – Cary Jensen Nov 18 '10 at 15:43