Perl TK resize window - perl

Is it possible to automatically resize a Tk window to the width and height of the text inside of the text plot?

You can easily measure how wide some text is in a particular font:
$font = $widget->Font(....); # Get the font however you want
$lineWidth = $font->measure($text); # Do this for each line; take the max
$lineHeight = $font->metrics(-linespace); # Get the height of a line; x # lines
Add in a bit of slop for borders, etc., and use this to set the size of the toplevel:
$toplevel->configure(-width=>$width, -height=>$height);
If it's not a toplevel that you want to control, it's easiest to do this by putting the contents in a frame and controlling the size of that frame (same method, same options). Also be aware that if you have too many rows or too long lines, it won't fit on the screen anyway.

Related

How do I set a maximum text width for a GtkTextView?

I have a GtkTextView, and I would like to be able to set a maximum line width for the text. If the width of the TextView exceeds the maximum text width, the extra space should be filled with padding on the left and right on the text. Although Gtk supports a min-width CSS property, there appears to be no max-width property. Instead I tried to dynamically set the margins whenever the TextView is resized by connecting size-allocate to
def on_textview_size_allocate(textview, allocation):
width = allocation.width
if width > max_width:
textview.set_left_margin((width - max_width) / 2)
textview.set_right_margin((width - max_width) / 2)
else:
textview.set_left_margin(0)
textview.set_right_margin(0)
This produces the desired text line width for any given TextView width, but leads to strange behavior when resizing the window. Resizing the window to a smaller width happens with a slow delay. Attempting to maximize the window makes the window jump to a width much larger than the screen. size-allocate may not be the correct signal to connect to, but I have not been able to find any other way to dynamically set margins when the TextView is resized.
What is the correct way to achieve a maximum text line width?
I came up with a solution. I created a custom container by inheriting from GtkBin, overrode do_size_allocate, and added my GtkTextView to that container:
class MyContainer(Gtk.Bin):
max_width = 500
def do_size_allocate(self, allocation):
# if container width exceeds max width
if allocation.width > self.max_width:
# calculate extra space
extra_space = allocation.width - self.max_width
# subtract extra space from allocation width
allocation.width -= extra_space
# move allocation to the right so that it is centered
allocation.x += extra_space / 2
# run GtkBin's normal do_size_allocate
Gtk.Bin.do_size_allocate(self, allocation)

iWatch: WKInterfaceLabel is it possible to stop text from being cut off with "..." at the end of a label?

The text in my WKInterfaceLabel is way too long and causes the text to be cut off with dots at the end. I know for UILabel for iOS you can easily resolve this issue by enabling clip mode. I don't believe there is any way for me to resolve this for watchkit. This is going to force me to use an Image if I can't prevent the text from being cut off. Any tips or suggestions is appreciated.
You have a couple options depending on how you want the view to respond. In your interface story board select your label and open the attributes inspector.
Your first option is to change the font to a smaller size. This is more for a static label that you want to style and leave set.
Your second option is to adjust the min scale value, changing this will automatically shrink the text to fit the window up to the value provided. For example if your font size is 12pt and you set the scale to .5, the font will shrink up to 6pt before appending the ellipsis (...).
Your third option is to set the number of lines to 0 (or a higher number). This will move the text down onto the next line.
Set the number of lines to 0 and ensure the label and any containing groups are set to fit content.
if you want your font size adjust according to label size follow this method
in WKInterfaceTable attribute inspector set min scale to 0
like in screen shoot
Result before Min scale = 0
Result after Min scale = 0
Note: your no of lines also set to 1

Get the maximum number of text lines that fit into a Pango layout?

I'm using a Pango inside a Cairo context inside a GTK window. How can I calculate how many text lines and columns would fit inside the area?
Lines are easy; you can render a sample line of text in a PangoLayout and call pango_layout_get_pixel_extents() on it, then do the required math to figure out how many of those would fit vertically in your GtkWindow.
Columns are not so easy (and whatever number you calculate won't mean much) unless you are using a monospaced font. If your font is monospaced, then use the same method as for lines.

matlab: having a good control of MyBox, box with text

my problem is I want to have a box for additional bigger comments to the graph.
How can I create this box, so that it will be always on the right bottom side outside of my graph, and it will be automatically adjusting its size to the amount of text inside? Also, if I will have longer text, I need it to be split into more lines, not just one line extremely long.
I use it like that, however I have a poor control over what is happening, this just creates a certain size in a certain place, text is cut.. please help :)
x = rand(110)*100;
y = x;
plot(x,y)
MyBox = uicontrol('style','text')
set(MyBox,'String','optional longer information to be put into diagram')
set(MyBox,'Position',[10,0,40,10])
I am afraid you can not do that automatically - Text boxes do not use callbacks. From the documentation:
Users cannot change static text interactively. Static text controls do not activate callback routines when clicked.
What you can do is call textwrap function, which will automatically wrap the text you pass to it and return new Text box position:
MyBox = uicontrol('style','text');
% set initial position - first three values are kept constant after wrap, the hight can be changed
set(MyBox,'Position',[10,10,30,10])
% adjust the height of your Text box and wrap the text
[outstring,newpos] = textwrap(MyBox,{'optional longer information to be put into diagram'});
% set new position and text
set(MyBox,'Position',newpos, 'String', outstring)
You will have to call textwrap and set(...) yourself whenever you change the string in the text box.

How to automatically set optimal frame width of Emacs?

How to automatically set optimal frame width of Emacs?
When open a file with Emacs from command line, how to let it automatically set the frame size such that
width is 2 characters wider than the widest row or the computer screen width, which ever is smaller
height is 80 rows, or the computer screen height, which ever is smaller?
You can use a find-file-hook which sets your frame's size. You'll need things like display-width and display-height, and you'll need to loop through the whole file computing the width of each line (using forward-line to move to the next line, end-of-line to go the end of the line and current-column to find the width). And you'll probably need to fiddle with off-by-one details in order to account for things like the menu-bar, the fringes, ...
Me? I prefer to declare that files should not go over 80 columns so I don't need to adjust the frame width.
This is just what libraries fit-frame.el and autofit-frame.el are for. See also http://www.emacswiki.org/FrameSize.