Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   determing whitespace to center text (http://www.programmingforums.org/showthread.php?t=15193)

malavar Feb 15th, 2008 1:52 PM

determing whitespace to center text
 
hello, im trying to center my title in a console application, no matter what size the window is, and i dont quite know how to calculate how many whitespaces are on each line, is there a c++ function that counts the white spaces for a given console size/width?

Ancient Dragon Feb 15th, 2008 3:45 PM

Re: determing whitespace to center text
 
If you use MFC there is a CDC c++ class that will help you with that centering. There is also the win32 api function TextOut() But you have to know where you want the text to start (x and y coordinates). That will depend on the font and font size being used. If you navigate through some of the links in MSDN you will probably find some example(s) you can use.

malavar Feb 15th, 2008 4:22 PM

Re: determing whitespace to center text
 
Quote:

Originally Posted by Ancient Dragon (Post 141112)
If you use MFC there is a CDC c++ class that will help you with that centering. There is also the win32 api function TextOut() But you have to know where you want the text to start (x and y coordinates). That will depend on the font and font size being used. If you navigate through some of the links in MSDN you will probably find some example(s) you can use.

hey thanks for the reply, im using linux, i was hoping that there was a function in the standard library that comes with C++. its a pretty simple concept, and it is a console based application. sorry i didnt give enough information in the first place. i have a simple algorithm that centers it, but its based on a fixed width, i.e running it in a tty terminal it will be centered, but when running it in a gui based terminal i.e gnome terminal or kterm, it will not be centered.

Ancient Dragon Feb 15th, 2008 5:50 PM

Re: determing whitespace to center text
 
Quote:

Originally Posted by malavar (Post 141115)
hey thanks for the reply, im using linux,

Oh well then what I posted isn't relevent to you.

Quote:

Originally Posted by malavar (Post 141115)
i was hoping that there was a function in the standard library that comes with C++. its a pretty simple concept, and it is a console based application.

There is nothing in the standard C or C++ libraries that will help you. If you are using fixed-width fonts then all you have to do is simple calculation to find the center. (screen_width - string_length) /2 = center of screen.

When using GUI terminals you have to calculate with width or the screen and string in pixels. I don't know how to do that in *nix, but it will depend on what GUI libraries you are using.

Game_Ender Feb 17th, 2008 6:09 PM

Re: determing whitespace to center text
 
I think you want to use something like ncurses. Don't try to reinvent the console GUI, they did that like 20 years ago ;).

Ancient Dragon Feb 17th, 2008 7:05 PM

Re: determing whitespace to center text
 
ncurses is useless on GUI screens and not necessary for this question on text screens.

Game_Ender Feb 18th, 2008 10:16 AM

Re: determing whitespace to center text
 
He is a linux user talking about "consoles". Which means he writing a terminal program. If you are writing a terminal program and wish to make a decent text bested interface, using something like ncurses is the way to go. This is the same thing I would say to someone on windows trying to slog it out with the Win32 API. I would tell them to go use something like QT or wxWidgets. Somebodies already done the work take advantage of it.

Ancient Dragon Feb 18th, 2008 9:15 PM

Re: determing whitespace to center text
 
Quote:

Originally Posted by malavar (Post 141115)
hey thanks for the reply, im using linux, i was hoping that there was a function in the standard library that comes with C++. its a pretty simple concept, and it is a console based application. sorry i didnt give enough information in the first place. i have a simple algorithm that centers it, but its based on a fixed width, i.e running it in a tty terminal it will be centered, but when running it in a gui based terminal i.e gnome terminal or kterm, it will not be centered.

Quote:

Originally Posted by Game_Ender (Post 141185)
He is a linux user talking about "consoles". Which means he writing a terminal program. If you are writing a terminal program and wish to make a decent text bested interface, using something like ncurses is the way to go. This is the same thing I would say to someone on windows trying to slog it out with the Win32 API. I would tell them to go use something like QT or wxWidgets. Somebodies already done the work take advantage of it.

You are quite right about linux, but his problem is with centering while using a GUI based terminal it is broken. My experience with linux GUI is X11R6 and Motief. I know QT is a better solution for students, and there are probably others too that I'm not aware of. Whatever the tools used the solution is probably the same -- getting the width of the terminal in pixels and the width in pixels of the text to be centered. There must be functions in the gui tools to get that information, but I don'w know that those functions are.

Game_Ender Feb 19th, 2008 12:27 AM

Re: determing whitespace to center text
 
Well the problem can't really be pixel width because my gnome terminal open right beside the firefox I am currently using, has fixed widths fonts. The issue is that I can decide for it to be 80 characters wide (the standard) or 180 characters wide. Now its obvious terminal programs can determine the width, at least in characters, because when I open nano in that terminal its completely aware of how many characters wide the terminal is.

A quick download of the nano source code shows that it indeed uses ncurses. Although it also uses terminfo, termcap, and termlib. From what I have found they might be real source of terminal information needed. Of course ncurses might just use them underneath.

The point I was trying to make above was not to use QT, but that this is a very solved problem for which you want to use a text based user interface library like ncurses.

Ancient Dragon Feb 19th, 2008 8:46 AM

Re: determing whitespace to center text
 
If gnome terminal has fixed width font then possibly your suggestion will work. I don't have such a terminal so I can't really say.


All times are GMT -5. The time now is 4:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC