GTK SourceView scroll to mark not working - gtk3

I'm trying to fill a SourceView's buffer with text and then scroll to make a specific line visible, like this:
lines = '....'.split('\n')
line_number = 76 # For instance, assuming lines has at least this many lines
buffer = view.get_buffer()
for line in lines:
buffer.insert(end_iter, line + '\n')
iter = buffer.get_iter_at_line()
mark = buffer.get_mark('insert')
buffer.move_mark(mark, iter)
mark = buffer.get_mark('selection_bound')
buffer.move_mark(mark, iter)
view.scroll_to_mark(mark, 0.3, True, 0, 0.5)
This scrolls to more-or-less random places in the buffer. Is there something I'm doing wrong here? Or does this just not work?

You are right, scroll_to_iter depends on the idle recalculate. For that matter, scroll_to_mark does too. This works for me:
from gi.repository import GLib
#........ code here
GLib.idle_add(view.scroll_to_mark, mark, 0.1, True, 0.0, 0.5)

Related

PineScript - Keep in labels in the future when based on a fixed event

Thanks to the PineCoders on Twitter for pointing me in this direction.
I'm tracking both a hard stop (based on an entry indicator) and trailing stop and would like to keep them aligned.
bar_index+5 works great for the trailing stop, but for the hard stop, it simply places it 5 bars after entry indicator.
I've tried factoring in ta.barssince and it seems to have no effect, presumably since it believes the indicator is bar_index or something... I'm at a loss.
Here's the code:
SLAlabelText = str.tostring(SLCalc, "#.##")
var SLALabel = label.new(x = bar_index, y = SLCalc, color = color.red, style = label.style_none, textcolor = color.white, text=SLAlabelText)
if longBoy and stopLossA
label.set_xy(SLALabel, x = bar_index+5, y = SLCalc)
label.set_text(SLALabel, SLAlabelText)
label.set_tooltip(SLALabel, "Stop Loss: " + SLAlabelText)
"longBoy" is the indicator's confirmed state.
Chart image. Apologies for the meme stock, it was the best example.

pymunk boxes on flat surfaces freak out

I am having an issue with boxes that land on flat surfaces freaking out when they land, and changing their angle slightly. The following piece of code simulates a box with a flat bottom landing on a flat surface.
import pymunk
space = pymunk.Space()
space.gravity = (0, -1)
box_body = pymunk.Body()
box_shape = pymunk.Poly.create_box(box_body, (1, 1))
box_body.position = (0, 20)
box_body.velocity = (0, 0)
box_shape.mass = 1
floor = pymunk.Segment(space.static_body, (-5, 0), (5, 0), 1)
space.add(floor, box_body, box_shape)
for _ in range(63):
space.step(0.1)
print(box_body.angle)
I would expect the angle to be 0.0, as there is nothing that should be causing it to rotate. However, I actually get a value of -4.8183679268731794e-14. If I change the 63 to 64, I get 7.805375706901558e-15. It's fairly minor in this case, but when other dynamic objects interact with the box, it goes crazy and can often launch other objects at high speed. Why does this happen, and what can I do to prevent it? I've tried increasing the mass significantly, but the results don't change.

Infinite Background problem with division SpriteKit

I've been trying to implement an infinite background animation, which should change between 4 images of equal height and then repeat the sequence. However, it does not seem to work properly.
Note anchorPoint = CGPoint(x: 0.5, y: 0)
func updateBackground(currentTime: TimeInterval){
var delta: CGFloat = 0.0
if lastUpdate != nil {
delta = CGFloat(currentTime - lastUpdate)
}
//First increment position
activeBackground1.position.y += delta*backgroundVelocity
activeBackground2.position.y += delta*backgroundVelocity
//Detect bounds surpass
if activeBackground1.position.y > activeBackground1.size.height + screen.height/2 {
lastSky = (lastSky + 1)%4
sky1 = SKTexture(imageNamed: "sky" + String(lastSky))
activeBackground1.texture = sky1
//Reposition: background1 new position is equal to minus the entire height of
//background2 + its y size.
activeBackground1.position.y = -abs(activeBackground2.size.height-activeBackground2.position.y)
}
if activeBackground2.position.y > activeBackground2.size.height + screen.height/2 {
lastSky = (lastSky + 1)%4
sky1 = SKTexture(imageNamed: "sky" + String(lastSky))
activeBackground2.texture = sky1
activeBackground2.position.y = -abs(activeBackground1.size.height-activeBackground1.position.y)
}
}
The update algorithm works fine, but when it is needed to reposition one of the two background, it seems there is an offset of about 10.0 CGFloat from one background to another. What am I doing wrong?
EDIT: It turned out that the error was located in my image, which presented some blank rows and therefore generated visualisation glitches. So my code works perfectly.
I do the test and most likely you should use something like:
activeBackground2.position.y = activeBackground1.size.height + activeBackground1.position.y
instead of
activeBackground2.position.y = -abs(activeBackground1.size.height-activeBackground1.position.y)
I did this example and it works correctly: https://github.com/Maetschl/SpriteKitExamples/tree/master/InfiniteBackground/InfiniteBackground
Feel free to see and use.
Your problem is floating point math causing rounding errors. I am on a phone right now so I can’t wrote code, but what you want to do is have 1 parent SKNode to handle your entire background.
Add your background slivers to the parent node.
You then place the moving action on the parent only.
As each sliver leaves the screen, you take the sliver, and move it to the end of the other slivers.
This jumping should always be done with integer math, leaving no holes.
Basically:
The floating point moving math is done on the parent node.
The integer based tile jumping is done on each of the slivers.

How to get Pango Cairo to word wrap properly?

I'm having problems getting Pango Cairo to word wrap. Below is some demo code. I am setting the layout's width to the same as the red rectangle, so I would expect it to wrap to the red rectangle. As it is, it is simply putting one word on each line, as though the width was set very small. If I use pango.WRAP_WORD_CHAR, I only get one character to the line.
What am I doing wrong? How do I get the layout to wrap to the width I specified?
EDIT If I set the width to 100000, the words wrap correctly. This implies that the set_width and the construction arguments are using different units. Any ideas?
# -*- coding: utf-8 -*-
import sys
import cairo
import pango
import pangocairo
SIZE = 200
HALF = 100
QUARTER = 50
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, SIZE, SIZE)
context = cairo.Context(surface)
context.set_source_rgb(1, 0, 0)
context.rectangle(QUARTER, QUARTER, HALF, HALF)
context.fill()
context.set_source_rgb(1, 1, 0)
context.translate(QUARTER, QUARTER)
pangocairo_context = pangocairo.CairoContext(context)
layout = pangocairo_context.create_layout()
layout.set_width(HALF)
layout.set_alignment(pango.ALIGN_LEFT)
layout.set_wrap(pango.WRAP_WORD)
layout.set_font_description(pango.FontDescription("Arial 10"))
layout.set_text("The Quick Brown Fox Jumps Over The Piqued Gymnast")
pangocairo_context.update_layout(layout)
pangocairo_context.show_layout(layout)
context.show_page()
with file("test.png", "w") as op:
surface.write_to_png(op)
I found the answer. The value must be multiplied by pango.SCALE. This doesn't seem to be mentioned in the documentation for the function in the C API.

Controlling window position of a Powershell console

This works:
(Get-Host).UI.RawUI
$a = (Get-Host).UI.RawUI
$a.BackgroundColor = "white"
$a.ForegroundColor = "black"
$size = (Get-Host).UI.RawUI.WindowSize
$size.Width = 80
$size.Height = 30
(Get-Host).UI.RawUI.WindowSize = $size
But this doesn't work, and I am not sure how to make it work:
$position = (Get-Host).UI.RawUI.Windowposition
$position.X = 0
$position.Y = 30
(Get-Host).UI.RawUI.Windowposition = $position
The error I get is strange. It complains about "buffer" when I am trying to set external window position:
Exception setting "WindowPosition": "Cannot use the
specified Window X (column) position because it extends
past the width of the screen buffer. Specify another X
position, starting with 0 as the left most column of
the buffer.
The error is not really strange, because WindowPosition Gets and sets the position, in characters, of the view window relative to the screen buffer.
It does not set the position of the Window, but to put it crudely, the position in the buffer that you see through the view of the window. So in your case, you are getting the error because it is outside the buffer.
http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostrawuserinterface.windowposition%28v=vs.85%29.aspx
Unfortunately, setting the position of the window is not simple. There is a snapin for it though - http://wasp.codeplex.com/ ( use Set-WindowPosition)
Take a look at this script: Resize-Console.ps1 – Resize console window/buffer using arrow keys.
It is hopefully useful itself and partially should answer the question (the size part).