Add Header to TListView in c++ (firemonkey) - firemonkey

It is easy to find examples to add a Header item to a TListView in Delphi. I'm having no luck finding such for c++. I'm trying to translate this code from Object Pas to c++ but I can't figure out a translation for the "Purpose.Header".
with ListView1.Items.Add do
begin
Text := Format('Header %d', [Group]);
Purpose := TListItemPurpose.Header;
end;
In c++ I have this:
ListView1->BeginUpdate();
TListViewItem* item = ListView1->Items->Add();
item->Purpose = ?????????? // this is the line I need syntax for
item->Text = "My Header";
ListView1->EndUpdate();

TListItemPurpose is an enum type. FireMonkey is compiled with scoped enums enabled (strongly typed enums in C++11 and later).
The Delphi code you showed translates to C++ as follows:
TListViewItem *Item = ListView1->Items->Add();
Item->Text = Format(L"Header %d", ARRAYOFCONST(( Group )));
Item->Purpose = TListItemPurpose::Header;

Related

C++/CLI: Change Property of a dynamically added control at Runtime

I have seen it working with C# but not in Visual C++ 2015
System::Windows::Forms::Label^ mylabel= (gcnew System::Windows::Forms::Label());
mylabel->Name = L"pole";
mylabel->Text = "Hello";
this->Controls->Add(mylabel);
Note that mylabel is a temporary variable here.
Now the code work for C#
Control cc = this.Controls.Find("pole", true).First();
cc.text="New";
And I've tried this as there is no .First() or ->first(),
Control^ x = this->Controls->Find(L"pole", true);
and definitely an error shows
`cli::array<System::Windows::Forms::Control ^, 1> ^" cannot be used to initialize an entity of type "System::Windows::Forms::Control ^`"
How can I get that object as Control in runtime?
The Find method returns an array. In your C# example you call First() which returns the first item in the array (returning a reference to the Control). In the C++ example you do not call First() or do anything to retrieve a single item. That is why the error message indicates that you can't convert and array (note cli::array in error) to a Control reference.

TDirectory::GetFiles listing ignoring case on iOS (FMX, C++)

The code below lists files that have extension .cfg and it works fine on Win32. But, on iOS if i have a file that a user named with caps for the extension (e.g. test.CFG) then i miss it. I found this post using Delphi that might work using TDirectory::TFilterPredicate but i don't know how to implement in C++Builder.
TStringDynArray list;
TSearchOption searchOption;
UnicodeString DocsPath;
int lenDocsFolder;
DocsPath = System::Ioutils::TPath::GetDocumentsPath();
lenDocsFolder = DocsPath.Length();
searchOption = TSearchOption::soTopDirectoryOnly;
try
{
list = TDirectory::GetFiles(DocsPath, "*.cfg", searchOption);
}
catch (...)
{
ShowMessage("Incorrect path or search mask");
return;
}
I suppose i can just run a *.cfg block of code followed by a *.CFG but i'm hoping there is a cleaner approach.
Sorry, but I'm not used to C++. But this applies to both C++ and Delphi.
You are calling:
TDirectory.GetFiles(
const Path, SearchPattern: string;
const SearchOption: TSearchOption): TStringDynArray;
If you instead call this overloaded version:
TDirectory.GetFiles(
const Path, SearchPattern: string;
const SearchOption: TSearchOption;
const Predicate: TFilterPredicate): TStringDynArray;
you should be able to get what you need.
The TFilterPredicate type is defined as:
TFilterPredicate = reference to function(
const Path: string;
const SearchRec: TSearchRec): Boolean;
and should be the correct way to override the way files are matched.
I tried the Using a Lambda Expression from the link Remy posted in comment. I got an E2188 Expression syntaxerror until i disabled the classic Borland compiler. The code works great for simple predicate (on both Win32 and iOS).
String ext(".cfg");
files = TDirectory::GetFiles(CalcPath,
[ext](const String Path, const System::Sysutils::TSearchRec &SearchRec) -> bool
{
return ExtractFileExt(SearchRec.Name) == ext;
});
Now, how do i modify the extension string to return results for both .cfg and .CFG at same time?
String ext(".cfg"); // works fine
String ext(".cfg;.CFG"); // finds nothing

Got language as 0 when calling VerQueryValue

My code below is similar to the example in the VerQueryValue() documentation:
// Structure used to store enumerated languages and code pages.
HRESULT hr;
struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
// Read the list of languages and code pages.
VerQueryValue(pBlock,
TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate);
// Read the file description for each language and code page.
for( i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++ )
{
hr = StringCchPrintf(SubBlock, 50,
TEXT("\\StringFileInfo\\%04x%04x\\FileDescription"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);
if (FAILED(hr))
{
// TODO: write error handler.
}
// Retrieve file description for language and code page "i".
VerQueryValue(pBlock,
SubBlock,
&lpBuffer,
&dwBytes);
}
The code can retrieve string informations for other exe files, but only for 1 particular file, it can not retrieve string infos (I noticed the language is zero for this case).
By using file explorer, I can see the language is 'Language Neutral'. But, the code shows language part is 0 and codepage part is 1252.
Is it normal to get language as 0?
Furthermore, I'm not able to get CompanyName and other string, but those are available from windows file explorer.
Here's the screenshots in VC++ for that exe:
Any ideas?

Getting list of pointing devices in Windows (pascal)

I'm using Lazarus/FPC and I'm looking for a way to get a list of pointing devices in Windows - and then ultimately to be able to disable and enable particular devices.
A bit of Googling turned up this on MSDN and this on the FreePascal wiki.
These look like a good starting point but unfortunately I'm falling at the first hurdle... I can't figure out how to create the manager object that is referred to in the example.
The MSDN example is (C#):
private void PopulatePointers(TreeView tvDevices)
{
//Add "Pointer Devices" node to TreeView
TreeNode pointerNode = new TreeNode("Pointer Devices");
tvInputDevices.Nodes.Add(pointerNode);
//Populate Attached Mouse/Pointing Devices
foreach(DeviceInstance di in
Manager.GetDevices(DeviceClass.Pointer,EnumDevicesFlags.AttachedOnly))
{
//Get device name
TreeNode nameNode = new TreeNode(di.InstanceName);
nameNode.Tag = di;
TreeNode guidNode = new TreeNode(
"Guid = " + di.InstanceGuid);
//Add nodes
nameNode.Nodes.Add(guidNode);
pointerNode.Nodes.Add(nameNode);
}
}
Which I have partially translated to Pascal as:
uses windows, DirectInput;
procedure getPointingDevices();
begin
for pointingDevice in Manager.GetDevices(DeviceType.Keyboard,EnumDevicesFlags.AttachedOnly) do
begin
devicesTree.Items.AddChild(devicesTree.Items.TopLvlItems[0],pointingDevice.InstanceName);
end;
devicesTree.Items.TopLvlItems[0].Expand(true);
end;
and I have included DirectInput.pas, DirectX.inc, DXTypes.pas, Jedi.inc, Xinput.pas (some of which may not actually be needed, I'll work that out later) in the project.
Obviously I need to create the Manager object to be able to access its methods, but I have no idea how to do that from the documentation I've read so far.
What you are looking for is the DirectInput IDirectInput8 COM interface.
To enumerate input devices, obtain the IDirectInput8 interface using the DirectInput8Create() function, and then use its EnumDevices() or EnumDevicesBySemantics() method. For example:
uses
Windows, DirectInput;
function MyEnumCallback(lpddi: LPCDIDEVICEINSTANCE; pvRef: Pointer): BOOL; stdcall;
var
Tree: TTreeView;
begin
Tree := TTreeView(pvRef);
Tree.Items.AddChild(Tree.Items.TopLvlItems[0], lpddi.tszInstanceName);
end;
procedure getPointingDevices;
var
DI: IDirectInput8;
begin
OleCheck(DirectInput8Create(HInstance, DIRECTINPUT_VERSION, IDirectInput8, #DI, nil));
OleCheck(DI.EnumDevices(DI8DEVCLASS_POINTER, #MyEnumCallback, devicesTree, DIEDFL_ATTACHEDONLY));
devicesTree.Items.TopLvlItems[0].Expand(true);
end;

How can I reuse code between Javascript macros and minimize work done within the macros?

I currently have two macros that are part of a (very limited-audience) plugin I'm developing, that both look basically like:
(function(){
exports.name = "name";
exports.params = [
{name: "value"}
];
function get(tiddler) {
// return some contents of some tiddler fields according to some rule
}
function parse(data) {
// convert string to some kind of useful object
}
function logic(x, y) {
// determine whether the two objects correspond in some way
};
function format(data, title) {
// produce WikiText for a link with some additional decoration
};
exports.run = function(value) {
value = parse(value);
var result = [];
this.wiki.each(function(tiddler, title) {
var data = get(tiddler);
if (data !== undefined && logic(value, parse(data))) {
result.push(format(data, title));
}
});
return result.join(" | ");
};
})();
So they're already fairly neatly factored when considered individually; the problem is that only the core logic is really different between the two macros. How can I share the functions get, logic and format between the macros? I tried just putting them in a separate tiddler, but that doesn't work; when the macros run, TW raises an error claiming that the functions are "not defined". Wrapping each function as its own javascript macro in a separate tiddler, e.g.
(function(){
exports.name = "get";
exports.params = [
{name: "tiddler"}
];
exports.run = function(tiddler) {
// return some contents of some tiddler fields according to some rule
}
})();
also didn't help.
I'd also like to set this up to be more modular/flexible, by turning the main get/parse/logic/format process into a custom filter, then letting a normal filter expression take care of the iteration and using e.g. the widget or <> macro to display the items. How exactly do I set this up? The documentation tells me
If the provided filter operators are not enough, a developer can add
new filters by adding a module with the filteroperator type
but I can't find any documentation of the API for this, nor any examples.
How can I share the functions get, logic and format between the macros?
You can use the Common/JS standard require('<tiddler title>') syntax to access the exports of another tiddler. The target tiddler should be set up as a JS module (ie, the type field set to application/javascript and the module-type field set to library). You can see an example here:
https://github.com/Jermolene/TiddlyWiki5/blob/master/core/modules/widgets/count.js#L15
I'd also like to set this up to be more modular/flexible, by turning the main get/parse/logic/format process into a custom filter, then letting a normal filter expression take care of the iteration and using e.g. the widget or <> macro to display the items. How exactly do I set this up?
The API for writing filter operators isn't currently documented, but there are many examples to look at:
https://github.com/Jermolene/TiddlyWiki5/tree/master/core/modules/filters

Resources