Re: returning D string from C++?
Am Sat, 05 Aug 2017 20:17:23 + schrieb bitwise: > virtual DString getTitle() const { > DString ret; > ret.length = GetWindowTextLength(_hwnd) + 1; > ret.ptr = (const char*)gc_malloc(ret.length, 0xA, NULL); > GetWindowText(_hwnd, (char*)ret.ptr, ret.length); > return ret; > } In due diligence, you are casting an ANSI string into a UTF-8 string which will result in broken Unicode for non-ASCII window titles. In any case it is better to use the wide-character versions of Windows-API functions nowadays. (Those ending in 'W' instead of 'A'). Starting with Windows 2000, the core was upgraded to UTF-16[1], which means you don't have to implement the lossy conversion to ANSI code pages and end up like this ... [information loss] UTF-8 <-> Windows codepage <-> UTF-16 || in your code inside Windows ... but instead directly pass and get Unicode strings like this ... UTF-8 <-> UTF-16 | in your code string to zero terminated UTF-16: http://dlang.org/phobos/std_utf.html#toUTF16z zero terminated UTF-16 to string: ptr.to!string() or just ptr[0..len] if known Second I'd like to mention that you should have set ret.length = GetWindowText(_hwnd, (char*)ret.ptr, ret.length); Currently your length is anything from 1 to N bytes longer than the actual string[2], which is not obvious because any debug printing or display of the string stops at the embedded \0 terminator. [1] https://en.wikipedia.org/wiki/Unicode_in_Microsoft_Windows [2] https://msdn.microsoft.com/de-de/library/windows/desktop/ms633521(v=vs.85).aspx -- Marco
Re: Create class on stack
On Sunday, 6 August 2017 at 02:32:05 UTC, Adam D. Ruppe wrote: On Sunday, 6 August 2017 at 02:19:19 UTC, FoxyBrown wrote: Also, does it do the allocation at compile time(reserve space on the stack for the variable along with all the others or does it "allocate" space on the stack at runtime?... which is slightly slower). compile time. It works like a static array of the appropriate size. though the cost if ti was at runtime is small regardless. I think it is just a register subtract. yeah, I know, but no need for it ;) Still better than the heap but was just curious ;) No need to waste cycles if it's not necessary.
Re: Create class on stack
On Sunday, 6 August 2017 at 02:19:19 UTC, FoxyBrown wrote: Also, does it do the allocation at compile time(reserve space on the stack for the variable along with all the others or does it "allocate" space on the stack at runtime?... which is slightly slower). compile time. It works like a static array of the appropriate size. though the cost if ti was at runtime is small regardless. I think it is just a register subtract.
Re: Create class on stack
On Sunday, 6 August 2017 at 02:10:31 UTC, Moritz Maxeiner wrote: On Sunday, 6 August 2017 at 01:18:50 UTC, Johnson Jones wrote: On Saturday, 5 August 2017 at 23:09:09 UTC, Moritz Maxeiner wrote: On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote: using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it. Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct? The easy way is through std.typecons.scoped [1]. Here be dragons, though, because classes are reference types. [1] https://dlang.org/phobos/std_typecons.html#.scoped Thanks, I didn't think it created on the stack but it makes sense to do so. See the source [1] as to why: typeof(scoped!T) is a (non-copyable) struct that holds the memory for the T object inside it. The only issue is that it escaping the reference? Yes, don't escape references, that's the reason for my comment: Here be dragons, though, because classes are reference types. [1] https://github.com/dlang/phobos/blob/v2.075.0/std/typecons.d#L6613 I don't think you understand what I'm saying. If I use this method to create a "reference" type on the stack rather than the heap, is the only issue worrying about not having that variable be used outside that scope(i.e., have it "escape")? Obviously since it's on the stack it will be invalid after the function call, but I'm talking about other pitfalls. I don't see any but I want to be sure. Also, does it do the allocation at compile time(reserve space on the stack for the variable along with all the others or does it "allocate" space on the stack at runtime?... which is slightly slower).
Re: Create class on stack
On Sunday, 6 August 2017 at 01:18:50 UTC, Johnson Jones wrote: On Saturday, 5 August 2017 at 23:09:09 UTC, Moritz Maxeiner wrote: On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote: using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it. Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct? The easy way is through std.typecons.scoped [1]. Here be dragons, though, because classes are reference types. [1] https://dlang.org/phobos/std_typecons.html#.scoped Thanks, I didn't think it created on the stack but it makes sense to do so. See the source [1] as to why: typeof(scoped!T) is a (non-copyable) struct that holds the memory for the T object inside it. The only issue is that it escaping the reference? Yes, don't escape references, that's the reason for my comment: Here be dragons, though, because classes are reference types. [1] https://github.com/dlang/phobos/blob/v2.075.0/std/typecons.d#L6613
gtkD: events being triggered twice
GtkEventBox - Enter GtkEventBox - Enter Down GtkEventBox - Leave Up GtkEventBox - Leave GtkEventBox - Leave That is when I move the mouse over the event box then click then move out out then release. I would expect Enter Down Leave Up The fact that enter and leave are not paired equally is a problem. Can be worked around but seems like it would be a bug. the code is simply ebox.addOnEnterNotify(delegate(Event e, Widget w) { writeln(w.getName(), " - ", "Enter"); return true; }); ebox.addOnLeaveNotify((Event e, Widget w) { writeln(w.getName(), " - ", "Leave"); return true; });
Re: Create class on stack
On Saturday, 5 August 2017 at 23:09:09 UTC, Moritz Maxeiner wrote: On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote: using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it. Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct? The easy way is through std.typecons.scoped [1]. Here be dragons, though, because classes are reference types. [1] https://dlang.org/phobos/std_typecons.html#.scoped Thanks, I didn't think it created on the stack but it makes sense to do so. The only issue is that it escaping the reference?
Re: Getting enum from value
On Saturday, 5 August 2017 at 20:11:27 UTC, Matthew Remmel wrote: On Saturday, 5 August 2017 at 18:26:10 UTC, Kreikey wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel I'm annoyed that I didn't think of trying to cast it. That works great if the value exists in the enum. It does something weird if the value doesn't though. This is my test.d file: import std.stdio; enum Foo { A = "AV", B = "BV" } void main() { Foo k = cast(Foo)"BV"; // Works and prints correctly k = cast(Foo)"CV"; writeln("Type: ", typeid(k)); // Type: test.Foo writeln("Value: ", k); // Value: cast(Foo)CV } The output shows the type being the Foo enum but the value is 'cast(Foo)CV'. I would of expected an error or exception to be thrown if it wasn't able to cast into an actual enum member. Is this something with how the enums are implemented under the hood? That was my first post on this forum, so I'm glad it was at least a little bit useful :-D I think the reasoning for no error on bad casts is that casting is a blunt instrument that assumes the programmer knows what he's doing, and it breaks the type system. So you'd want to use one of the aforementioned solutions if you're set on using enums in this way. You might also consider using associative arrays, but it's also a bit cumbersome. There's no way to get around searching: capitals = [ "Indiana" : "Indianapolis", "Illinois" : "Chicago", "Ohio" : "Columbus" ]; auto r = capitals.byKeyValue.find!((a, b) => a.value == b)("Chicago"); if (!r.empty) { writeln(capitals[r.front.key]); } else { writeln("not found"); } You could also define another associative array statesByCapital with the key : value orders reversed, and then you could also do statesByCapitol["Chicago"]. Of course then you'd have to keep things in sync if things change. But I discovered a neat trick you could use to generate such a two way mapping. You could define one array string[] capitals, and another array string[] states. Then you could do: auto capitalsByState = assocArray(zip(states, capitals)); auto statesByCapital = assocArray(zip(capitals, states)); If your data doesn't change for the lifetime of the program, that looks like a nice way to do it.
Re: Create class on stack
On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote: using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it. Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct? The easy way is through std.typecons.scoped [1]. Here be dragons, though, because classes are reference types. [1] https://dlang.org/phobos/std_typecons.html#.scoped
Re: returning D string from C++?
On Saturday, 5 August 2017 at 20:17:23 UTC, bitwise wrote: I have a Windows native window class in C++, and I need a function to return the window title. [...] As long as you have a reachable reference to the GC memory SOMEWHERE, the GC won't reclaim it. It doesn't have to be on the stack as long as it is reachable through the stack.
Re: gtkD load images
On 08/05/2017 10:30 PM, Mike Wey wrote: On 05-08-17 15:23, Johnson Jones wrote: On Saturday, 5 August 2017 at 12:51:13 UTC, Mike Wey wrote: [...] There are two issues here, you need to properly escape the slash: "C:a.jpg". [...] ``` Pixbuf p = new Pixbuf(r"C:\\a.jpg"); ``` Thanks. Why do I need 4 slashes? Is that standard with gtk because strings are interpreted twice or something? Seemed to work though. Nothing specific to GTK but in D and other programing languages the \ is used as an escape character, so you can use special characters in your sting like `\n` for a newline. But this means you will need to use \\ to get an literal back slash. I think you missed the point of the question. In the end, the path should contain only one backslash. But with `"C:a.jpg"` and `r"C:\\a.jpg"` you get two. Why do you need two? Does the library do another round of escape sequence handling?
Re: gtkD window centering message up and no app on taskbar
On 05-08-17 20:14, Johnson Jones wrote: When trying to center the window. If one uses ALWAYS_CENTERED any resizing of the window is totally busted. CENTER also does not work. move(0,0) seems to not be relative to the main display. I'd basically like to center the window on the main display or at least be able to set coordinates properly. Windows sets (0,0) to be the lower left corner of the main display I believe. What happens is that the gtk window, when using 0,0 actually is like -1000,0 or something in windows coordinates and ends up on my secondary monitor. When the app starts there's no taskbar icon. Luckily I still have the console shown but Eventually I'll need the taskbar. I'm not setting skipTaskBarHint, but I have tried both true and false without any difference. gtk.Widget.translateCoordinates or gtk.Fixed could be useful for positioning the widgets. Windows will only show the taskbar icon if you are not running the application from the console. -- Mike Wey
Re: gtkD load images
On 05-08-17 15:23, Johnson Jones wrote: On Saturday, 5 August 2017 at 12:51:13 UTC, Mike Wey wrote: On 03-08-17 21:56, Johnson Jones wrote: If I do something like import gdkpixbuf.Pixbuf; Pixbuf.newFromResource("C:\\a.jpg"); There are two issues here, you need to properly escape the slash: "C:a.jpg". And a.jpg is not a resource file, so you would use the Pixbuf constuctor to load an image file. ``` Pixbuf p = new Pixbuf(r"C:\\a.jpg"); ``` Thanks. Why do I need 4 slashes? Is that standard with gtk because strings are interpreted twice or something? Seemed to work though. Nothing specific to GTK but in D and other programing languages the \ is used as an escape character, so you can use special characters in your sting like `\n` for a newline. But this means you will need to use \\ to get an literal back slash. https://dlang.org/spec/lex.html#double_quoted_strings You can also use an wysiwyg string by using `r"` at the start so what you type is what you get. https://dlang.org/spec/lex.html#wysiwyg -- Mike Wey
Re: Create class on stack
On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote: using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it. Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct? Emplace ? https://dlang.org/phobos/std_conv.html#emplace
returning D string from C++?
I have a Windows native window class in C++, and I need a function to return the window title. So in D, I have this: // isn't D's ABI stable enough to just return this from C++ // and call it a string in the extern(C++) interface? anyways.. struct DString { size_t length; immutable(char)* ptr; string toString() { return ptr[0..length]; } alias toString this; } extern(C++) interface NativeWindow { DString getTitle() const; } and in C++, this: class NativeWindow { public: struct DString { size_t length; const char* ptr; }; virtual DString getTitle() const { DString ret; ret.length = GetWindowTextLength(_hwnd) + 1; ret.ptr = (const char*)gc_malloc(ret.length, 0xA, NULL); GetWindowText(_hwnd, (char*)ret.ptr, ret.length); return ret; } }; So while it's not generally safe to _store_ pointers to D's GC allocated memory exclusively in C++, I've read that D's GC scans the stack, and getTitle() is being called from D(and so, is on that stack..right?). So is the string I'm returning safe from GC collection? Thanks
Re: Getting enum from value
On Saturday, 5 August 2017 at 18:26:10 UTC, Kreikey wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e [...] Capitals c = cast(Capitals)"Chicago"; writeln(c);// Illinois I'm annoyed that I didn't think of trying to cast it. That works great if the value exists in the enum. It does something weird if the value doesn't though. This is my test.d file: import std.stdio; enum Foo { A = "AV", B = "BV" } void main() { Foo k = cast(Foo)"BV"; // Works and prints correctly k = cast(Foo)"CV"; writeln("Type: ", typeid(k)); // Type: test.Foo writeln("Value: ", k); // Value: cast(Foo)CV } The output shows the type being the Foo enum but the value is 'cast(Foo)CV'. I would of expected an error or exception to be thrown if it wasn't able to cast into an actual enum member. Is this something with how the enums are implemented under the hood?
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 19:19:06 UTC, Simon Bürger wrote: On Saturday, 5 August 2017 at 18:54:22 UTC, ikod wrote: Maybe std.functional.partial can help you. Nope. int i = 1; alias dg = partial!(writeln, i); i = 2; dg(); still prints '2' as it should because 'partial' takes 'i' as a symbol, which is - for this purpose - kinda like "by reference". Anyway, I solved my problem already a while ago by replacing delegates with custom struct's that implement the call-operator. I started this thread just out of curiosity, because as I see it, the purpose of lambdas is pretty much to remove the need for such custom constructions. This one works void delegate()[3] dgs; for(int i = 0; i < 3; ++i) { (k){ dgs[k] = {writefln("%s", k); }; }(i); } dgs.each!(a => a());
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 18:54:22 UTC, ikod wrote: Maybe std.functional.partial can help you. Nope. int i = 1; alias dg = partial!(writeln, i); i = 2; dg(); still prints '2' as it should because 'partial' takes 'i' as a symbol, which is - for this purpose - kinda like "by reference". Anyway, I solved my problem already a while ago by replacing delegates with custom struct's that implement the call-operator. I started this thread just out of curiosity, because as I see it, the purpose of lambdas is pretty much to remove the need for such custom constructions.
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 18:54:22 UTC, ikod wrote: On Saturday, 5 August 2017 at 18:45:34 UTC, Simon Bürger wrote: On Saturday, 5 August 2017 at 18:22:38 UTC, Stefan Koch wrote: [...] No, sometimes I want i to be the value it has at the time the delegate was defined. My actual usecase was more like this: void delegate()[3] dgs; for(int i = 0; i < 3; ++i) dgs[i] = (){writefln("%s", i); }; And I want three different delegates, not three times the same. I tried the following: void delegate()[3] dgs; for(int i = 0; i < 3; ++i) { int j = i; dgs[i] = (){writefln("%s", j); }; } I thought that 'j' should be considered a new variable each time around, but sadly it doesn't work. Maybe std.functional.partial can help you. Thanks. But std.functional.partial takes the fixed arguments as template parameters, so they must be known at compile-time. Anyway, I solved my problem already a while ago by replacing delegates with custom structures which overload the call-operator. I opened this thread just out of curiosity. Takes a couple lines more but works fine.
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 18:45:34 UTC, Simon Bürger wrote: On Saturday, 5 August 2017 at 18:22:38 UTC, Stefan Koch wrote: [...] No, sometimes I want i to be the value it has at the time the delegate was defined. My actual usecase was more like this: void delegate()[3] dgs; for(int i = 0; i < 3; ++i) dgs[i] = (){writefln("%s", i); }; And I want three different delegates, not three times the same. I tried the following: void delegate()[3] dgs; for(int i = 0; i < 3; ++i) { int j = i; dgs[i] = (){writefln("%s", j); }; } I thought that 'j' should be considered a new variable each time around, but sadly it doesn't work. Maybe std.functional.partial can help you.
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 18:22:38 UTC, Stefan Koch wrote: On Saturday, 5 August 2017 at 18:19:05 UTC, Stefan Koch wrote: On Saturday, 5 August 2017 at 18:17:49 UTC, Simon Bürger wrote: If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But this capturing is always by reference. Example: int i = 1; auto dg = (){ writefln("%s", i); }; i = 2; dg(); // prints '2' Is there a way to make the delegate "capture by value" so that the call prints '1'? Note that in C++, both variants are available using [&]() { printf("%d", i); } and [=]() { printf("%d", i); } respectively. No currently there is not. and it'd be rather useless I guess. You want i to be whatever the context i is a the point where you call the delegate. Not at the point where you define the delegate. No, sometimes I want i to be the value it has at the time the delegate was defined. My actual usecase was more like this: void delegate()[3] dgs; for(int i = 0; i < 3; ++i) dgs[i] = (){writefln("%s", i); }; And I want three different delegates, not three times the same. I tried the following: void delegate()[3] dgs; for(int i = 0; i < 3; ++i) { int j = i; dgs[i] = (){writefln("%s", j); }; } I thought that 'j' should be considered a new variable each time around, but sadly it doesn't work.
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 18:17:49 UTC, Simon Bürger wrote: If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But this capturing is always by reference. Example: int i = 1; auto dg = (){ writefln("%s", i); }; i = 2; dg(); // prints '2' Is there a way to make the delegate "capture by value" so that the call prints '1'? Note that in C++, both variants are available using [&]() { printf("%d", i); } and [=]() { printf("%d", i); } respectively. There is, but it isn't pretty. import std.stdio; void main() { int i = 1; int* n = null; auto dg = (){ if (n is null) n = cast(int*)i; else writefln("%s", n); }; dg(); i = 2; dg(); // prints '1' } 1. I'm pretty sure that D creates the delegate "lazily" in the sense that the first call is what captures the variable. Hence, we must call it where we want to capture, not after the change occurs. 2. We use a temp local variable to act as a place holder. A singleton basically. You might be able to wrap this up in some type of template that makes it easier to use but it does work.
Re: Getting enum from value
On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e [...] Capitals c = cast(Capitals)"Chicago"; writeln(c);// Illinois
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 18:19:05 UTC, Stefan Koch wrote: On Saturday, 5 August 2017 at 18:17:49 UTC, Simon Bürger wrote: If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But this capturing is always by reference. Example: int i = 1; auto dg = (){ writefln("%s", i); }; i = 2; dg(); // prints '2' Is there a way to make the delegate "capture by value" so that the call prints '1'? Note that in C++, both variants are available using [&]() { printf("%d", i); } and [=]() { printf("%d", i); } respectively. No currently there is not. and it'd be rather useless I guess. You want i to be whatever the context i is a the point where you call the delegate. Not at the point where you define the delegate.
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 18:17:49 UTC, Simon Bürger wrote: If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But this capturing is always by reference. Example: int i = 1; auto dg = (){ writefln("%s", i); }; i = 2; dg(); // prints '2' Is there a way to make the delegate "capture by value" so that the call prints '1'? Note that in C++, both variants are available using [&]() { printf("%d", i); } and [=]() { printf("%d", i); } respectively. No currently there is not.
lambda function with "capture by value"
If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But this capturing is always by reference. Example: int i = 1; auto dg = (){ writefln("%s", i); }; i = 2; dg(); // prints '2' Is there a way to make the delegate "capture by value" so that the call prints '1'? Note that in C++, both variants are available using [&]() { printf("%d", i); } and [=]() { printf("%d", i); } respectively.
gtkD window centering message up and no app on taskbar
When trying to center the window. If one uses ALWAYS_CENTERED any resizing of the window is totally busted. CENTER also does not work. move(0,0) seems to not be relative to the main display. I'd basically like to center the window on the main display or at least be able to set coordinates properly. Windows sets (0,0) to be the lower left corner of the main display I believe. What happens is that the gtk window, when using 0,0 actually is like -1000,0 or something in windows coordinates and ends up on my secondary monitor. When the app starts there's no taskbar icon. Luckily I still have the console shown but Eventually I'll need the taskbar. I'm not setting skipTaskBarHint, but I have tried both true and false without any difference.
Re: Getting enum from value
On 08/05/2017 07:05 PM, ag0aep6g wrote: E enumFromValue(E)(string s) The type of `s` should probably be a template parameter as well.
Re: Getting enum from value
On 08/05/2017 05:33 PM, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e enum Capitals { Indiana = "Indianapolis", Illinois = "Chicago", Ohio = "Columbus" } Capitals enumFromValue(string s) { switch (s) { case Capitals.Indiana: return Capitals.Indiana; case Capitals.Illinois: return Capitals.Illinois; case Capitals.Ohio: return Capitals.Ohio; default: throw new Exception(format("No Capitals enum member with value %s", s)); } } int main() { Capitals c = enumFromValue("Chicago"); // works // I tried using std.conv, but it matches on the enum member name c = to!Capitals("Chicago") // fails, no member named Chicago } With how redundant the enumFromValue(string) implementation is, I would think there would be an easier way to do it. I'm sure you could use a mixin, a template, or std.traits. I was hoping there was a more 'builtin' way to do it though. Something along the simplicity of: int main() { Capitals c = Capitals("Chicago"); } Any ideas? As far as I know, there's no built-in way to do this. But you can simplify and generalize your `enumFromValue`: enum Capitals { Indiana = "Indianapolis", Illinois = "Chicago", Ohio = "Columbus" } E enumFromValue(E)(string s) { import std.format: format; import std.traits: EnumMembers; switch (s) { foreach (c; EnumMembers!E) { case c: return c; } default: immutable string msgfmt = "enum %s has no member with value %s"; throw new Exception(format(msgfmt, E.stringof, s)); } } void main() { auto c = enumFromValue!Capitals("Chicago"); assert(c == Capitals.Illinois); }
Create class on stack
using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it. Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct?
Re: Getting enum from value
On Saturday, 5 August 2017 at 15:42:53 UTC, Rene Zwanenburg wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: Any ideas? You can use to! in std.conv: import std.stdio; import std.conv; enum Foo { A = "A", B = "B" } void main() { writeln("A".to!Foo); } This only works because the enum name and the value are the same. Its actually converting on the enum name, which happens to be the same as the value. This doesn't work if the values is different: enum Foo { A = "AV", B = "BV" } int main() { writeln("AV".to!Foo); // Throws exceptions return 0; } It looks like Temtaime's solution works: enum Foo { A = "AV", B = "BV", C = "CV", } Foo K = [ EnumMembers!Foo ].find!(a => a == `BV`)[0]; I can probably make a template or something out of this to make the syntax simpler.
Re: Size of D bool vs size of C++ bool
On Friday, 4 August 2017 at 20:38:16 UTC, Steven Schveighoffer wrote: On 8/4/17 4:16 PM, Jeremy DeHaan wrote: I'm trying to do some binding code, and I know that C++ bool isn't defined to be a specific size like D's bool. That said, can I assume that the two are the same size on the most platforms? I shudder to think that D may work with a platform that doesn't consider it to be 1 byte :) The only platforms I'm really interested in are Windows, Linux, OSX, iOS, FreeBSD, Android. The only thing that might throw me off is if there are some things that Linux or FreeBSD target where this is not the case, but these machines are probably out of the scope of my project. I would say any platform that D currently supports, C++ bool is defined to be 1 byte. The ldc/gdc guys would know better. -Steve Thanks, Steve. I was hoping this was the case and it will significantly simplify a lot of my binding code. I'm curious to see what systems don't have a bool size of 1 byte, but perhaps I'll put a check in my CMake file and prevent the project from building if it isn't.
Re: gtk get actual pixel height of widget
On Saturday, 5 August 2017 at 15:23:15 UTC, Johnson Jones wrote: I am trying to set positions of widgets automatically. e.g., I have a paned widget and I to set the position of the handle manually based on a percentage of the window. e.g., 0.5 will set the handle midway and both children will have the same height. I 0.2 will set it to to 20%. [...] Sorry, I think I was running my code before the window was actually being shown and I guess these values are not set until the actual display of the window.
Re: Getting enum from value
On Saturday, 5 August 2017 at 15:42:53 UTC, Rene Zwanenburg wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: Any ideas? You can use to! in std.conv: import std.stdio; import std.conv; enum Foo { A = "A", B = "B" } void main() { writeln("A".to!Foo); } Are you fools ? Did you ever read the post ? I think this is a minimal solution: enum Foo { A = "AV", B = "BV", C = "CV", } Foo K = [ EnumMembers!Foo ].find!(a => a == `BV`)[0];
Re: How to build GUI-based applications in D ?
thank you everybody for your time to answer my questions.
Re: How to build GUI-based applications in D ?
On Saturday, 5 August 2017 at 07:10:50 UTC, aberba wrote: The DlangUI docs has you covered with everything you need to set it up both on the github README file or the github wiki. Its just: dub init PROJECT_NAME dlangui This will create project and add dlangui as dependency. Creating a project requires Internet connection to download the dlangui package. You may also add dlangui as a dependency in the project's dub.json file. thank you aberba ok, so this is useless to me. i want something fully functional stand-alone tools. i have no internet connection there.
Re: Getting enum from value
On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: Any ideas? You can use to! in std.conv: import std.stdio; import std.conv; enum Foo { A = "A", B = "B" } void main() { writeln("A".to!Foo); }
Re: Getting enum from value
On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e [...] What you want is already in the standard library. std.conv.to can convert strings to enums and back.
Re: gtk: get property
On Saturday, 5 August 2017 at 15:19:43 UTC, Gerald wrote: On Saturday, 5 August 2017 at 15:08:21 UTC, Johnson Jones wrote: I am trying to get the handle size of panned. Not sure if I'm doing it right but [...] I'm using this in Tilix: Value handleSize = new Value(0); paned.styleGetProperty("handle-size", handleSize); Awesome! Thanks! I didn't see that method in the list of 1000's of function in Visual D ;/ Figured everything that was a getter started with get.
Getting enum from value
I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e enum Capitals { Indiana = "Indianapolis", Illinois = "Chicago", Ohio = "Columbus" } Capitals enumFromValue(string s) { switch (s) { case Capitals.Indiana: return Capitals.Indiana; case Capitals.Illinois: return Capitals.Illinois; case Capitals.Ohio: return Capitals.Ohio; default: throw new Exception(format("No Capitals enum member with value %s", s)); } } int main() { Capitals c = enumFromValue("Chicago"); // works // I tried using std.conv, but it matches on the enum member name c = to!Capitals("Chicago") // fails, no member named Chicago } With how redundant the enumFromValue(string) implementation is, I would think there would be an easier way to do it. I'm sure you could use a mixin, a template, or std.traits. I was hoping there was a more 'builtin' way to do it though. Something along the simplicity of: int main() { Capitals c = Capitals("Chicago"); } Any ideas?
gtk get actual pixel height of widget
I am trying to set positions of widgets automatically. e.g., I have a paned widget and I to set the position of the handle manually based on a percentage of the window. e.g., 0.5 will set the handle midway and both children will have the same height. I 0.2 will set it to to 20%. I want it to retain this proportion when the window is resized. The problem is I cannot get get the paned widgets actual height(nor the handle size). paned.getHeight() returns -1. If I use the main window's height, things go wonky because, I guess the border size and title bar size skew the calculations. I'm still learning this api and how it all functions and works. Some things are not so obvious nor logical. getHeight should return the height. If -1 means "leave it up to the internals" then there should be some other height function that works like getActualHeight() but there isn't or I can't find anything that works. If I do writeln(mainPaned.getAllocatedHeight()); writeln(mainPaned.getChild1.getAllocatedHeight()); writeln(mainPaned.getChild2.getAllocatedHeight()); then I get something like 800 1 1 where 800 is the height I used to set the window using auto width = 1000, height = 800; mainWindow.resize(width,height); which, I'd expect it to actually be smaller as either it doesn't take in to account the titlebar or the resize function above is not for the full application window.
Re: gtk: get property
On Saturday, 5 August 2017 at 15:08:21 UTC, Johnson Jones wrote: I am trying to get the handle size of panned. Not sure if I'm doing it right but [...] I'm using this in Tilix: Value handleSize = new Value(0); paned.styleGetProperty("handle-size", handleSize);
gtk: get property
I am trying to get the handle size of panned. Not sure if I'm doing it right but Value value = new Value(); paned.getProperty("handle-size", value); GLib-GObject-CRITICAL **: g_object_get_property: assertion 'G_IS_VALUE (value)' failed or I get stuff like GLib-GObject-WARNING **: g_object_get_property: object class 'GtkStyle' has no property named 'handle-size' if I do mainPaned.getStyle().getProperty("handle-size", value); I haven't been able to figure out how to get it. I've also tried mainPaned.getStyle().getStyleProperty(... but the first parameter is a GType which is suppose to be the widget type yet I am getting value types like INT BOOl, etc. Not sure if there are two types of GTypes enum GType : size_t { INVALID = 0<<2, NONE = 1<<2, INTERFACE = 2<<2, CHAR = 3<<2, UCHAR = 4<<2, BOOLEAN = 5<<2, INT = 6<<2, UINT = 7<<2, LONG = 8<<2, ULONG = 9<<2, INT64 = 10<<2, UINT64 = 11<<2, ENUM = 12<<2, FLAGS = 13<<2, FLOAT = 14<<2, DOUBLE = 15<<2, STRING = 16<<2, POINTER = 17<<2, BOXED = 18<<2, PARAM = 19<<2, OBJECT = 20<<2, VARIANT = 21<<2, } If that what I'm suppose to use then not sure which one I use for Paned ;)
Re: Remove instance from array
On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote: On Wednesday, 5 July 2017 at 15:56:45 UTC, Igor Shirkalin wrote: On Wednesday, 5 July 2017 at 15:48:14 UTC, Jolly James wrote: On Wednesday, 5 July 2017 at 15:44:47 UTC, Igor Shirkalin wrote: On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote: WhatEver[] q = []; [...] auto i = new WhatEver(); q[] = i; How does one remove that instance 'i'? What exactly do you want to remove? After a[]=i your array contain a lot of references to 'i'. I would like to know how works: removing - the first - and all references to 'i' inside the 'q'. Perhaps, for all references to i it should look like: a = a.filter!(a => a !is i).array; Thank you! :) But why a containers so complicated in D? In C# I would go for a generic List, which would support structs and classes, where I simply could call '.Remove(T item)' or '.RemoveAt(int index)'. I would know how this works, because the method names make sense, the docs are straight forward. Here in D everything looks like climbing mount everest. When you ask how to use D's containers you are recommended to use dynamic arrays instead. When you look at the docs for std.algorithm, e.g. the .remove section, you get bombed with things like 'SwapStrategy.unstable', asserts and tuples, but you aren't told how to simply remove 1 specific element. I don't know c sharp, but I can tell everything about c++ and python. To climb a everest in python you have to know almost nothing, in c++ you have to know almost everything. In D you have to be smarter, you do not need to climb a everest but you have to know minimum to do that. Spend a year in learning and get the best result in minutes).
Re: Fix gtkD api display
On 04-08-17 17:24, Gerald wrote: On Friday, 4 August 2017 at 15:08:27 UTC, Mike Wey wrote: Improving the documentation is something i want to do but there are always some more important things to do. Like the Questions/Issues you posted earlier. So unless somebody volunteers it won't happen anytime soon. Mike I had contributed the makeddox.sh script awhile ago, it generates much nicer documentation then candydocs in my IHMO and includes a nice search box. If there is something lacking in it that needs to be improved before it can be used let me know and I'll do the work. The only issue with it that I am aware of is you need to manually copy the public ddox css into the generated folder. I didn't see an easy way to determine it's location automatically. One issue is the shear size of the generated documentation, though the current version of ddox no longer generates a ton of unused files bringing the size down from 15-20GB to a mere 2GB. So it has at leased become manageable to host it on the VPS that hosts gtkd.org. Now remains figuring out setting up the proper redirects on the server, and a few personal preferences about ddox: The need to go trough the empty module page when browsing the documentation. For a lot / most? functions the complete documentation is in the overview defeating the purpose of the one page per artifact. And im not a big fan of the one page per artifact style of documentation. -- Mike Wey
Re: gtkD load images
On Saturday, 5 August 2017 at 12:51:13 UTC, Mike Wey wrote: On 03-08-17 21:56, Johnson Jones wrote: If I do something like import gdkpixbuf.Pixbuf; Pixbuf.newFromResource("C:\\a.jpg"); There are two issues here, you need to properly escape the slash: "C:a.jpg". And a.jpg is not a resource file, so you would use the Pixbuf constuctor to load an image file. ``` Pixbuf p = new Pixbuf(r"C:\\a.jpg"); ``` Thanks. Why do I need 4 slashes? Is that standard with gtk because strings are interpreted twice or something? Seemed to work though.
Re: GtkD custom theme on Windows
On 04-08-17 05:06, Andres Clari wrote: I've made a linux program with GtkD, and so far, it's been pretty awesome, however I'm thinking about porting it to Windows also, but the Adwaita theme is too fugly, and cringy, so I'd want to use a compatible theme, which is supposed to be doable. What would be the way to go to make a GtkD app use a custom GTK theme in Windows? I tried this in the past, but never succeeded following documentation found online. I didn't try it myself but it should be something like this: -Download a theme from gnome-look.org -Extract the theme to: C:\\Program Files\Gtk-Runtime\share\themes -Edit C:\\Program Files\Gtk-Runtime\etc\gtk-3.0\settings.ini and add: ``` gtk-theme-name = Name_of_Theme ``` -- Mike Wey
Re: gtkD load images
On 03-08-17 21:56, Johnson Jones wrote: If I do something like import gdkpixbuf.Pixbuf; Pixbuf.newFromResource("C:\\a.jpg"); There are two issues here, you need to properly escape the slash: "C:a.jpg". And a.jpg is not a resource file, so you would use the Pixbuf constuctor to load an image file. ``` Pixbuf p = new Pixbuf(r"C:\\a.jpg"); ``` -- Mike Wey
Re: Bug in gtkd?
On 03-08-17 23:11, Johnson Jones wrote: On Thursday, 3 August 2017 at 21:00:17 UTC, Mike Wey wrote: On 03-08-17 22:40, Johnson Jones wrote: Ok, so, I linked the gtk to the msys gtk that I installed before when trying to get glade to work and it worked! seems that msys is much more up to date than anything else as it just works(I need to remember than in the future). The problem I see is this: When I get ready to release my app to the public, I can't expect them to all have to install msys and build. msys seems to clump everything together and I don't know what files I need to extract to be able to bundle everything together. Any ideas how to solve that problem? At least now I can move ahead and actually make some progress on my app. Would still be nice to get the x86 vs x64 issue resolved so I don't have to keep switching between the two for testing purposes. Since Visual D was just patched to handle x64 BP's I guess I can stay with that for now. I'll try to build and test some new installers tomorrow that will include the loaders. Thanks. Could you take a look at the loading image thread I started when you get time? I can't seem to get an image to load even though it seems straight forward. These are the pixbufs I'm using mingw32/mingw-w64-i686-gdk-pixbuf2 2.36.6-2 [installed] An image loading library (mingw-w64) mingw64/mingw-w64-x86_64-gdk-pixbuf2 2.36.6-2 [installed] An image loading library (mingw-w64) in x64 it crashes completely without an exception though... which is why I want an easy way to switch between the two architectures... since x64 seems to be more unstable than x86 but sometimes it's the reverse, and ultimately I'll want to release in x64. Also, do I ever need to rebuild gdk when changing gtk installations? Does it ever grab anything from them at compile time or is it all at runtime? The new installers are available: https://gtkd.org/Downloads/runtime/ You don't need to rebuild GtkD when changing GTK installations, it does it all at runtime. -- Mike Wey
Re: How to build GUI-based applications in D ?
On 2017-08-01 17:45, ashit wrote: thank you James i should try that. i was always enjoy the pure and efficiency of C. that made me stubborn to learn java. Just to be clear, there's no Java code in DWT. Everything is ported to D. -- /Jacob Carlborg
Re: How to build GUI-based applications in D ?
On Thursday, 3 August 2017 at 10:02:19 UTC, ashit wrote: On Tuesday, 1 August 2017 at 16:12:45 UTC, Dukc wrote: On Tuesday, 1 August 2017 at 15:18:12 UTC, ashit wrote: i couldn't set control's width and height (Button widget) shows error. maybe it works a different way. 1. Try layoutHeight/width. Remember to set it for the main widget too, not just the children of it. 2. DlangUI is not intended to define sizes in pixels as a standard practice. Instead, use layouts and layout sizes. This is intended to courage you to make your program resolution-agnostic. But I'm a beginner at this topic too. Take these with a grain of salt thank you Dukc it worked, i should adapt with this different naming style. (as comparing to C#) [yesterday] but today, when i went to create another project, it failed. i get this message: D:\ashit\documents\D\simpled>dub init simpled dlangui Couldn't find package: dlangui. it works without the "dlangui" option, but then when i execute run command: D:\ashit\documents\D\simpled>dub run Performing "debug" build using dmd for x86. simpled ~master: building configuration "application"... source\app.d(2,8): Error: module dlangui is in file 'dlangui.d' which cannot be read import path[0] = source import path[1] = C:\dmd2\windows\bin\..\..\src\phobos import path[2] = C:\dmd2\windows\bin\..\..\src\druntime\import dmd failed with exit code 1. this is the path i have extracted the dlangui files: D:\ashit\software\D Compiler\DlangUI\dlangui-master how to define dlangui for DUB? The DlangUI docs has you covered with everything you need to set it up both on the github README file or the github wiki. Its just: dub init PROJECT_NAME dlangui This will create project and add dlangui as dependency. Creating a project requires Internet connection to download the dlangui package. You may also add dlangui as a dependency in the project's dub.json file.