Re: Format

2021-05-21 Thread cc via Digitalmars-d-learn

On Saturday, 22 May 2021 at 03:14:35 UTC, cc wrote:

Oops, disregard this.  I had an error in my imports.😓
It does in fact work in @safe.


I should add as an aside then that there is an issue of errors 
from the body of a toString template not being displayed, and 
instead the template being silently skipped, e.g.:


```d
@safe void toString(W)(ref W writer) if (isOutputRange!(W, char)) 
{

writer.formattedWrite("FOO:%s", x);
	syntactically correct; // Program will compile without error, 
but this function will now never be called

}
```

I ran into a similar challenge with opDispatch some time ago: 
https://forum.dlang.org/post/axgwhzzawncbpcvqq...@forum.dlang.org
In this case I assume it's some consequence of NOT wanting to 
emit a warning or error for every single class or struct that 
doesn't have a templated toString when the various output writers 
go looking for one.


Something like this can help reveal errors, not sure if there's 
an easier way:

```d
unittest {
struct DebugWriter {
void put(C)(C c) {}
}
DebugWriter wr;
Foo foo;
foo.toString(wr);
}
```
`Error: undefined identifier 'syntactically'`
`Error: template instance 
'amemfailuretest.Foo.toString!(DebugWriter)' error instantiating`


Re: Format

2021-05-21 Thread cc via Digitalmars-d-learn

On Saturday, 22 May 2021 at 03:07:10 UTC, cc wrote:
Ahh, in that case it would appear formattedWrite isn't @safe at 
all.  Looks like you have to stick with put()?

```d
@safe void toString(W)(ref W writer) if (isOutputRange!(W, 
char)) {

//writer.formattedWrite!("FOO:%s", x); // fails
import std.conv;
put(writer, "FOO:");
put(writer, x.to!string);
}
```


Oops, disregard this.  I had an error in my imports.😓
It does in fact work in @safe.


Re: Format

2021-05-21 Thread cc via Digitalmars-d-learn

On Friday, 21 May 2021 at 16:53:48 UTC, drug wrote:

21.05.2021 18:28, cc пишет:

On Friday, 21 May 2021 at 14:19:03 UTC, newbie wrote:

Thank you, and formatValue?


formattedWrite should handle this.

```d
@safe struct Foo {
 int x = 3;
 void toString(W)(ref W writer) if (isOutputRange!(W, 
char)) {

     writer.formattedWrite("Foo(%s)", x);
 }
}
Foo foo;
writeln(foo);
```

Oddly enough this form of toString works from @safe code even 
if it's not marked @safe, or even marked @system...


I guess that because it is a template so the compiler is able 
to deduce its attributes. What about the case when it is marked 
@system - in this case compiler ignore that method due to the 
fact that it is @system and generate the default one. Because 
your implementation of the method is equal to the default 
implementation you didn't see the difference but it exists. Try 
to make your implementation of `toString` different and you'll 
see.


Ahh, in that case it would appear formattedWrite isn't @safe at 
all.  Looks like you have to stick with put()?

```d
@safe void toString(W)(ref W writer) if (isOutputRange!(W, char)) 
{

//writer.formattedWrite!("FOO:%s", x); // fails
import std.conv;
put(writer, "FOO:");
put(writer, x.to!string);
}
```


Re: Formatted output not on screen but in a string

2021-05-21 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 21 May 2021 at 22:37:34 UTC, Alain De Vos wrote:

Next code format a string and prints it.
But I want the formatted string stored in a string
```
//Decimal place separator %,
writefln!"%,s"(123456789);
//123,456,789
```


std.format.format


Formatted output not on screen but in a string

2021-05-21 Thread Alain De Vos via Digitalmars-d-learn

Next code format a string and prints it.
But I want the formatted string stored in a string
```
//Decimal place separator %,
writefln!"%,s"(123456789);
//123,456,789
```


Re: gtkd , addondraw is deprecated

2021-05-21 Thread Mike Wey via Digitalmars-d-learn

On 21-05-2021 12:35, Alain De Vos wrote:
What is the advised function to use instead of 
gtk.Widget.Widget.addOnDraw ?

```
this()
     { addOnDraw(&drawCallback);}
```


Change the `Context` passed to the drawCallback to a `Scoped!Context` 
only the non scoped overload is deprecated.


```
bool drawCallback(Scoped!Context cr, Widget widget)
```

--
Mike Wey


Re: Format

2021-05-21 Thread cc via Digitalmars-d-learn

On Friday, 21 May 2021 at 14:19:03 UTC, newbie wrote:

Thank you, and formatValue?


formattedWrite should handle this.

```d
@safe struct Foo {
int x = 3;
void toString(W)(ref W writer) if (isOutputRange!(W, char)) {
writer.formattedWrite("Foo(%s)", x);
}
}
Foo foo;
writeln(foo);
```

Oddly enough this form of toString works from @safe code even if 
it's not marked @safe, or even marked @system...


Re: Format

2021-05-21 Thread newbie via Digitalmars-d-learn

On Friday, 21 May 2021 at 13:59:13 UTC, drug wrote:

21.05.2021 16:45, newbie пишет:
I am following 
https://wiki.dlang.org/Defining_custom_print_format_specifiers, why sink and formatValue are not @safe? What are the best practice for toString in safe code? Thank you


sink is obsolete now, use W(riter)

```D
import std.range : isOutputRange;

void toString(W)(ref W writer) const
if (isOutputRange!(W, char))
{
// your stuff
}
```
writer can be @safe, @nogc, nothrow etc like you want


Thank you, and formatValue?


Re: Format

2021-05-21 Thread drug via Digitalmars-d-learn

21.05.2021 16:45, newbie пишет:
I am following 
https://wiki.dlang.org/Defining_custom_print_format_specifiers, why sink 
and formatValue are not @safe? What are the best practice for toString 
in safe code? Thank you


sink is obsolete now, use W(riter)

```D
import std.range : isOutputRange;

void toString(W)(ref W writer) const
if (isOutputRange!(W, char))
{
// your stuff
}
```
writer can be @safe, @nogc, nothrow etc like you want


Format

2021-05-21 Thread newbie via Digitalmars-d-learn
I am following 
https://wiki.dlang.org/Defining_custom_print_format_specifiers, 
why sink and formatValue are not @safe? What are the best 
practice for toString in safe code? Thank you


Re: gtkd ,drawingarea, capture mouse pressed

2021-05-21 Thread Alain De Vos via Digitalmars-d-learn

I found something,
https://api.gtkd.org/gdk.Event.Event.getCoords.html



Re: gtkd ,drawingarea, capture mouse pressed

2021-05-21 Thread drug via Digitalmars-d-learn

21.05.2021 15:39, Alain De Vos пишет:

I'll have a look at that website.
With this code I capture the mouse press event

```
this()
{
addEvents(GdkEventMask.BUTTON_PRESS_MASK);
addOnDraw(&drawCallback);
addOnButtonPress(&onButtonPress);
}

```

```
public bool onButtonPress(Event event, Widget widget)
{
writeln("Button pressed");
return false;
}
```

But I still need the coordinates of pointer.

I'm not gtk user too but you could try to use 
GdkEventMask.BUTTON_MOTION_MASK to get pointer

motion events while some button is pressed


Re: gtkd ,drawingarea, capture mouse pressed

2021-05-21 Thread Alain De Vos via Digitalmars-d-learn

I'll have a look at that website.
With this code I capture the mouse press event

```
this()
{
addEvents(GdkEventMask.BUTTON_PRESS_MASK);
addOnDraw(&drawCallback);
addOnButtonPress(&onButtonPress);
}

```

```
public bool onButtonPress(Event event, Widget widget)
{
writeln("Button pressed");
return false;
}
```

But I still need the coordinates of pointer.



Re: gtkd ,drawingarea, capture mouse pressed

2021-05-21 Thread evilrat via Digitalmars-d-learn

On Friday, 21 May 2021 at 12:28:36 UTC, Alain De Vos wrote:
On a gtkd drawingarea I want to capture the mouse-pressed event 
and get the coordinates of the pointer on the area.

I have
```
addEvents(GdkEventMask.BUTTON_PRESS_MASK);
```
Maybe I must add a signal-handler ?


Not a gtk user, but maybe this will help (has quite a list of 
examples on gtkd, newest first)


https://gtkdcoding.com/


gtkd ,drawingarea, capture mouse pressed

2021-05-21 Thread Alain De Vos via Digitalmars-d-learn
On a gtkd drawingarea I want to capture the mouse-pressed event 
and get the coordinates of the pointer on the area.

I have
```
addEvents(GdkEventMask.BUTTON_PRESS_MASK);
```
Maybe I must add a signal-handler ?


gtkd , addondraw is deprecated

2021-05-21 Thread Alain De Vos via Digitalmars-d-learn
What is the advised function to use instead of 
gtk.Widget.Widget.addOnDraw ?

```
this()
{ addOnDraw(&drawCallback);}
```