How to retrieve the identifier of a Label? - ada

i'm trying to progress in GtkAda using Glade 3.40 which seems cool.
I much like thie idea to let the engine of Gtk works for the coder, thru GkAda, & let the coder to concentrate on real added value.
-So i tried some examples on internet.
The following one compiles & is partially OK. We may see the window with the title, but there is no print of the text in the window. Probably because "lbl" doesn't content the good value to address the window.
-But i don't know how to initialize it. I made many trials without success..
Is anyone could help, please?
Thanks
Mark
With Gtk.Main; Use Gtk.Main;
With Gtk.Window; Use Gtk.Window;
With Gtk.Label; Use Gtk.Label;
With Glib.Convert; Use Glib.Convert;
With Gtk.Enums; Use Gtk.Enums;
Procedure Texte is
Win : Gtk_Window;
Lbl : Gtk_Label ;
newline : constant character := character'val(10) ;
Txt : CONSTANT String := Locale_To_Utf8
("Ça c'est une chaîne de" & Newline &
"caractères encodée en UTF-8" & Newline &
"grâce à Locale_To_UTF8") ;
begin
Init ;
Gtk_New (Win) ;
Win.Set_Default_Size (100,100) ;
win.set_title ("Du texte !") ;
Gtk_New (Lbl,txt);
Lbl.Set_Justify (Justify_center);
win.add (lbl); -- No layout
Win.show_all ;
Main ;
end Texte ;

Related

FreePascal mouse input when using graph window?

GetMouseX does not work when using a graph window.
uses wincert,graph,winmouse;
var x,y:integer;
Begin
x:=GetMouseX;
End.
This gives me Error:Identifier not found "GetMouseX".
Trying this with unit 'mouse' works in the console window, but not in the graphics window.
Can anyone please help?
Yes, winmouse is not a version/subset of mouse like wincrt is a subset of crt for windows "graph" use..
It is just an unit to let mouse work with graph. Look at the unit to see what functions are available (lpressed,rpressed,getlastbuttonpress etc)
After a long search I managed to find a list of functions for winmouse. The page was in... Polish or something, but the example code worked like a charm.
uses graph, wincrt, winmouse;
var gd,gm: integer;
ch:char;
i:integer;
mposx, mposy, state: longint;
begin
gd := d4bit;
gm := m640x480;
initgraph(gd,gm,'');
initmouse;
randomize;
for i:=1 to 10 do
circle(random(640),random(480),150);
repeat
if keypressed then ch:= readkey;
if lpressed then
begin
repeat until not lpressed;
getmousestate(mposx,mposy,state);
if getpixel(mposx,mposy)<15 then
begin
setfillstyle(solidfill,random(14)+1);
floodfill(mposx,mposy,15);
end;
end;
until ch = #27;
closegraph;
end.
Thanks for the help.

How to load an image to Turbo Pascal using the graph unit

I need to load a single bitimage to a Pascal program, is there a way to do it or I must draw pixel by pixel?
As far as I remember, Turbo pascal had functions
GetImage(X1, Y1, X2, Y2: integer; var BitMap)
PutImage(X, Y: integer; var BitMap; BitBlt: word);
BitMap is just a chunk of memory with the bitmap. This way you can get image from screen to memory and vice versa. I think there is no direct function to get image from file to screen. But if you have image in right format on the disc, you can load it to memory and then use PutImage.
By using graph unit you can load BGI graphics in turbo pascal.
Refer this for more info...
http://pascal-programming.info/lesson8.php
Here is a sample code from the above link...
Program Lesson8_Program1;
Uses Crt,Graph;
Var GraphicsDriver, GraphicsMode,
ErrCode : Integer;
{two var's are needed for initialisation}
Begin
Writeln('Initialising Graphics, please wait...');
GraphicsDriver := Detect;
InitGraph(GraphicsDriver, GraphicsMode,'');
{IMPORTANT, read the following or
otherwise graphics will not work!! ;)}
(*between the inverted commas,
type in the path of the graphics BGI file
(usually 'C:\TP\BGI'),
OR
change the dir in the file menu (PRESS Alt+F)
and roll down your mouse pointer to the 'change dir'
menu; then either type the path to the BGI file,
or go to C: -> TP -> BGI*)
ErrCode := GraphResult;
If GraphResult <> grOK then { <> means 'not equal to' }
Begin
ClrScr;
Writeln('Graphics error occured: ',
GraphErrorMsg(ErrCode));
Writeln('If a file not found error is displayed above');
Writeln('then, change the dir from the current');
Writeln('location to C:\ -> TP -> BGI, '+
+'from the file menu!');
Readln;
Halt(1);
End Else
Begin
Randomize;
SetColor(Random(15) + 1); {Set text colour}
{Output text at 20 pixels from the top of the screen,
and 20 other from the left side of the screen.}
OutTextXY(20,20,'Welcome to the new generation
of Pascal Programming:');
OutTextXY(20,30,'Pascal Graphics!!');
OutTextXY(25,70,'You will learn more
graphics procedures and');
OutTextXY(25,80,'functions, later in this lesson :-)');
Readln;
End;
CloseGraph;
End.
Refer this for more info...
http://pascal-programming.info/lesson8.php

Get words out of a file in Ada

I have a folder with files containing some text. I am trying to go through all the files one after the other, and count how many times we see every word in the text files.
I know how to open the file, but once I'm in the file I don't know how to read each word one after the other, and go to the next word.
If anyone has some ideas to guide me it'd be great.
Read the file a line at a time into a string using Get_Line, then break the line up into the individual words.
Here's one way of doing it, I needed some playtime with the containers.
Using streams is still the best solution for your problem, given the multiple files.
Text_Search.ads
Pragma Ada_2012;
With
Ada.Containers.Indefinite_Ordered_Maps;
Package Text_Search with Elaborate_Body is
Text : Constant String :=
ASCII.HT &
"We hold these truths to be self-evident, that all men are created " &
"equal, that they are endowed by their Creator with certain unalienable "&
"Rights, that among these are Life, Liberty and the pursuit of " &
"Happiness.--That to secure these rights, Governments are instituted " &
"among Men, deriving their just powers from the consent of the governed" &
", --That whenever any Form of Government becomes destructive of these " &
"ends, it is the Right of the People to alter or to abolish it, and to " &
"institute new Government, laying its foundation on such principles " &
"and organizing its powers in such form, as to them shall seem most " &
"likely to effect their Safety and Happiness. Prudence, indeed, will " &
"dictate that Governments long established should not be changed for " &
"light and transient causes; and accordingly all experience hath shewn, "&
"that mankind are more disposed to suffer, while evils are sufferable, " &
"than to right themselves by abolishing the forms to which they are " &
"accustomed. But when a long train of abuses and usurpations, pursuing " &
"invariably the same Object evinces a design to reduce them under " &
"absolute Despotism, it is their right, it is their duty, to throw off " &
"such Government, and to provide new Guards for their future security." &
"now the necessity which constrains them to alter their former Systems " &
"of Government. The history of the present King of Great Britain is a " &
"history of repeated injuries and usurpations, all having in direct " &
"object the establishment of an absolute Tyranny over these States. To " &
"prove this, let Facts be submitted to a candid world.";
Package Word_List is New Ada.Containers.Indefinite_Ordered_Maps(
Key_Type => String,
Element_Type => Positive
);
Function Create_Map( Words : String ) Return Word_List.Map;
Words : Word_List.map;
End Text_Search;
Text_Search.adb
Package Body Text_Search is
Function Create_Map( Words : String ) Return Word_List.Map is
Delimiters : Array (Character) of Boolean:=
('.' | ' ' | '-' | ',' | ';' | ASCII.HT => True, Others => False);
Index, Start, Stop : Positive := Words'First;
begin
Return Result : Word_List.Map do
Parse:
loop
Start:= Index;
-- Ignore initial delimeters.
while Delimiters(Words(Start)) loop
Start:= 1+Start;
end loop;
Stop:= Start;
while not Delimiters(Words(Stop)) loop
Stop:= 1+Stop;
end loop;
declare
-- Because we stop *on* a delimiter we mustn't include it.
Subtype R is Positive Range Start..Stop-1;
Substring : String renames Words(R);
begin
-- if it's there, increment; otherwise add it.
if Result.Contains( Substring ) then
Result(Substring):= 1 + Result(Substring);
else
Result.Include( Key => substring, New_Item => 1 );
end if;
end;
Index:= Stop + 1;
end loop parse;
exception
When Constraint_Error => null; -- we run until our index fails.
end return;
End Create_Map;
Begin
Words:= Create_Map( Words => Text );
End Text_Search;
Test.adb
Pragma Ada_2012;
Pragma Assertion_Policy( Check );
With
Text_Search,
Ada.Text_IO;
Procedure Test is
Procedure Print_Word( Item : Text_Search.Word_List.Cursor ) is
use Text_Search.Word_List;
Word : String renames Key(Item);
Word_Column : String(1..20) := (others => ' ');
begin
Word_Column(1..Word'Length+1):= Word & ':';
Ada.Text_IO.Put_Line( Word_Column & Positive'Image(Element(Item)) );
End Print_Word;
Begin
Text_Search.Words.Iterate( Print_Word'Access );
End Test;
Instead of going by individual words, you could read the file a line at a time into a string using Get_Line, and then use regular expressions: Regular Expressions in Ada?
If you're using Ada 2012 here's how I would recommend doing it:
With Ada.Containers.Indefinite_Ordered_Maps.
Instantiate a Map with String as key, and Positive as Key;
Grab the string; I'd use either a single string or stream-processing.
Break the input-text into words; this can be done on-the-fly if you use streams.
When you get a word (from #4) add it to your map if it doesn't exist, otherwise increment the element.
When you are finished, just run a For Element of WORD_MAP Loop that prints the string & count.
There's several ways you could handle strings in #3:
Perfectly sized via recursive function call [terminating on a non-word character or the end of input].
Unbounded_String
Vector (Positive, Character) -- append for valid characters, convert to array [string] and add to the map when an invalid-character [or the end of input] is encountered -- working variable.
Not Null Access String working variable.

ASP.NET MVC pseudo-localization

Is there any tool I can use to perform pseudolocalization on an ASP.NET MVC app (using the Razor view engine) in order to help localizability testing?
i wrote a small utility app that lets me convert text into "pseudo".
It's based on a simple function which looks at each character in the string. If the character IsAlpha then i add an accent to it.
i add an accent by appending the character with a random diacritic mark between U+0300 and U+036F (the diacritical marks range).
So now i just localize my application/web-site like i were one of the dutch speaking localizers.
ỉ w͝rͨo͡t̮e̤ a͈͟ s̏m͈a͞l̪lͅ u͇t̽i̡l̔i͜țy̭ a͟p̉p͕ t̼h̥aͤt̖͊ l͊e̗t̘s̈́
m̶eͭ c͟o̙n̚v̈e̲r̓t̝ t͌e̽x̳t͈ i̍n̔t̖o̺ "p̪̜s̔e̊u̟d̮o̱".
I͔t͒'sͪ b̞aͧs̜e̋dͫ o̸͚n͙ a̼ s̼i̥m̢ͫpͫl͗e̺ f̙uͪn͎̉c͙t͒ḯo̾n̑ w̰h̙i̷c͟ḣ̌
l̼óoͦk͔s͑ a̬t̆ e̓a̔c̈hͭ c̚h̻a̿r̴aͭc̤t̙eͦŗ͉ i̞n̴ t̀h̸e̮ͫ s̖t͘r͕i͇n͒g̴͐.
I̳fͣ t̰ĥe̿ c͋h͡a͖r̸a̸c̟t̼e̞rͪ Ìs̀A̒l͒p̷h̗a̲ ţh̽e͚nͥ i̡ a̰d͙d̬
a̅n̊ a̐c̒c̒e̤͞n̜t̽ t̆o͑ i̴tͪ.
i̋ a̸d̋d́ a̰n̏ a͛c͏c̣͗e̾n͡t̎ b̷̞ẏ a̐p͞p̧͔eͩn͊ḍ͋ín̓g͚ t̬h͚ẹ
ćh̞a͗ŗa͖c͐t̊e̷rͅ w͙iͩt̮hͮ a͡ r͙a͋nͅd̃o͞m̚ dͬi̠a͚c̸r̾iͩt͖i͋c̭ m̓ḁr̵k̡
b̞ë́t̼w̓e̬ë́n̨ U̵+0300 a̢n͙d͜ U͕+036F͓ (t̯͉hͪ͟eͮ
d͓i͉aͣc͕̬r͜i͒t́i͓c̯a͚l̼ m̓a̅r̝̜k͎s̯ r͋ăn̪̒g̟e̱̮).
There's also an option to add padding to text, so allow for other languages not using the same amount of space as english
Today -> [T̀oͨd̼a͈̹y̭ !!]
Control Panel -> [C̊ȍńt̪r͗o͂l͇ P̈ȁñeͮl͢ !!!!!]
Oxydative Decarboxilation -> [Ŏx͞y̠dͥa͂t̿i̚v͙eͪ D̼e̖c̿aͤr͖b̐o̼x̹i͂l͖a̠t̮i̓o̧n̰ !!!!!!!!!]
And my PsuedoLocalizeText function:
function TForm1.PsuedoLocalizeText(s: WideString; AddPadding: Boolean): WideString;
var
i: Integer;
sb: TWideStringBuilder;
x: Integer;
tiny: WideString;
const
n: WideString = ''; //n=nothing
begin
//Code is public domain. No attribution required.
sb := TWideStringBuilder.Create;
try
for i := 1 to Length(s) do
begin
//http://www.fileformat.info/info/unicode/block/combining_diacritical_marks/list.htm
//U+0300 .. U+36F are combining diacritic marks
x := $300+Random($36f-$300);
if Windows.IsCharAlphaW(s[i]) then
begin
tiny := n+ s[i] + WideChar(x);
if Random(10) < 1 then
tiny := n+tiny+WideChar($300+Random($36f-$300));
sb.Append(tiny);
end
else
sb.Append(s[i]);
end;
Result := sb.ToString;
finally
sb.Free;
end;
if AddPadding then
begin
Result := '['+Result+' ';
for i := 1 to Ceil(Length(s) / 3) do
begin
Result := n+Result+'!';
end;
Result := Result+']';
end;
end;
Update My psuedolocalizer:
My next task is to have it work on *.resx, but exclude *.xx-yy.resx, so it can work on WinForms applications as well.
And of course fill in Size, Attributes, get the right icon for Folders.
Then maybe have it highlight the fr-FR or qps-ploc part of filenames.
There is a "pseudoizer" tool in the downloads for this article: http://msdn.microsoft.com/en-us/magazine/cc163991.aspx
It includes both source (winforms) and binary if you're lazy
After not finding anything quick, free and easy, I built Pseudolocalize.com! Just copy/paste/click and you've got the pseudolocalized version of your strings!

Using Vim, how can I make CSS rules into one liners?

I would like to come up with a Vim substitution command to turn multi-line CSS rules, like this one:
#main {
padding: 0;
margin: 10px auto;
}
into compacted single-line rules, like so:
#main {padding:0;margin:10px auto;}
I have a ton of CSS rules that are taking up too many lines, and I cannot figure out the :%s/ commands to use.
Here's a one-liner:
:%s/{\_.\{-}}/\=substitute(submatch(0), '\n', '', 'g')/
\_. matches any character, including a newline, and \{-} is the non-greedy version of *, so {\_.\{-}} matches everything between a matching pair of curly braces, inclusive.
The \= allows you to substitute the result of a vim expression, which we here use to strip out all the newlines '\n' from the matched text (in submatch(0)) using the substitute() function.
The inverse (converting the one-line version to multi-line) can also be done as a one liner:
:%s/{\_.\{-}}/\=substitute(submatch(0), '[{;]', '\0\r', 'g')/
If you are at the beginning or end of the rule, V%J will join it into a single line:
Go to the opening (or closing) brace
Hit V to enter visual mode
Hit % to match the other brace, selecting the whole rule
Hit J to join the lines
Try something like this:
:%s/{\n/{/g
:%s/;\n/;/g
:%s/{\s+/{/g
:%s/;\s+/;/g
This removes the newlines after opening braces and semicolons ('{' and ';') and then removes the extra whitespace between the concatenated lines.
If you want to change the file, go for rampion's solution.
If you don't want (or can't) change the file, you can play with a custom folding as it permits to choose what and how to display the folded text. For instance:
" {rtp}/fold/css-fold.vim
" [-- local settings --] {{{1
setlocal foldexpr=CssFold(v:lnum)
setlocal foldtext=CssFoldText()
let b:width1 = 20
let b:width2 = 15
nnoremap <buffer> + :let b:width2+=1<cr><c-l>
nnoremap <buffer> - :let b:width2-=1<cr><c-l>
" [-- global definitions --] {{{1
if exists('*CssFold')
setlocal foldmethod=expr
" finish
endif
function! CssFold(lnum)
let cline = getline(a:lnum)
if cline =~ '{\s*$'
return 'a1'
elseif cline =~ '}\s*$'
return 's1'
else
return '='
endif
endfunction
function! s:Complete(txt, width)
let length = strlen(a:txt)
if length > a:width
return a:txt
endif
return a:txt . repeat(' ', a:width - length)
endfunction
function! CssFoldText()
let lnum = v:foldstart
let txt = s:Complete(getline(lnum), b:width1)
let lnum += 1
while lnum < v:foldend
let add = s:Complete(substitute(getline(lnum), '^\s*\(\S\+\)\s*:\s*\(.\{-}\)\s*;\s*$', '\1: \2;', ''), b:width2)
if add !~ '^\s*$'
let txt .= ' ' . add
endif
let lnum += 1
endwhile
return txt. '}'
endfunction
I leave the sorting of the fields as exercise. Hint: get all the lines between v:foldstart+1 and v:voldend in a List, sort the list, build the string, and that's all.
I won’t answer the question directly, but instead I suggest you to reconsider your needs. I think that your “bad” example is in fact the better one. It is more readable, easier to modify and reason about. Good indentation is very important not only when it comes to programming languages, but also in CSS and HTML.
You mention that CSS rules are “taking up too many lines”. If you are worried about file size, you should consider using CSS and JS minifiers like YUI Compressor instead of making the code less readable.
A convenient way of doing this transformation is to run the following
short command:
:g/{/,/}/j
Go to the first line of the file, and use the command gqG to run the whole file through the formatter. Assuming runs of nonempty lines should be collapsed in the whole file.

Resources