Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2008, 12:52 PM   #1
malavar
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 malavar is on a distinguished road
Send a message via AIM to malavar
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?
malavar is offline   Reply With Quote
Old Feb 15th, 2008, 2:45 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Feb 15th, 2008, 3:22 PM   #3
malavar
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 malavar is on a distinguished road
Send a message via AIM to malavar
Re: determing whitespace to center text

Quote:
Originally Posted by Ancient Dragon View Post
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.
malavar is offline   Reply With Quote
Old Feb 15th, 2008, 4:50 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: determing whitespace to center text

Quote:
Originally Posted by malavar View Post
hey thanks for the reply, im using linux,
Oh well then what I posted isn't relevent to you.

Quote:
Originally Posted by malavar View Post
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.
Ancient Dragon is offline   Reply With Quote
Old Feb 17th, 2008, 5:09 PM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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 .
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Feb 17th, 2008, 6:05 PM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: determing whitespace to center text

ncurses is useless on GUI screens and not necessary for this question on text screens.
Ancient Dragon is offline   Reply With Quote
Old Feb 18th, 2008, 9:16 AM   #7
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Feb 18th, 2008, 8:15 PM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: determing whitespace to center text

Quote:
Originally Posted by malavar View Post
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 View Post
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.
Ancient Dragon is offline   Reply With Quote
Old Feb 18th, 2008, 11:27 PM   #9
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Feb 19th, 2008, 7:46 AM   #10
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting spaces between formatted text fields Fall Back Son Java 7 Feb 12th, 2008 8:40 PM
[perl] graphical text editor glimmy Existing Project Development 0 Aug 6th, 2006 11:47 PM
How to detect cursor location and insert text??? syntax-error C# 3 Jun 30th, 2005 1:42 AM
Printing rich text with colored background jarrod C# 1 Feb 23rd, 2005 11:48 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:59 AM.

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