TextLength()

20 posts / 0 new
Last post
mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
TextLength()

I am trying to get the longest length of some strings. It is working

  1. int32
  2. SAK_LongestStrLenPixels(int32 From,int32 To)
  3. {
  4. STRPTR Text;
  5. int32 MaxLen=0, Len, i;
  6.  
  7. for (i=From; i<=To; i++)
  8. {
  9. if ((Text=SAK_LocaleString(i)))
  10. {
  11. if ((Len=IGraphics->TextLength(MainWindow->RPort,Text,IUtility->Strlen(Text)))>MaxLen)
  12. MaxLen=Len;
  13. }
  14. }
  15.  
  16. return(MaxLen);
  17. }

Except it is not using MainWindow's font, which is proportional. It is using the system default font, which is mono spaced. I am not assigning a different font for my window, why isn't it using the correct one to measure the lengths?

gazelle
gazelle's picture
Offline
Last seen: 1 year 9 months ago
Joined: 2011-04-13 12:52
Re: TextLength()

You're not using a GimmeZeroZero window by chance?

from intuition.library/OpenWindow:


BUGS
When you open a window, Intuition will set the font of
the window's RastPort to the font of the window's screen.
This does not work right for GimmeZeroZero windows: the
BorderRPort RastPort has the font set correctly, but
Window.RPort is set up with the system default font.
For compatibility reasons, we won't be fixing this problem.

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()

Nope. Using window.class to create/layout the guts.

jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 6 days ago
Joined: 2013-05-30 00:53
Re: TextLength()

And your 'MainWindow' struct what Font (struct TextFont *IFont;) is using?
Maybe you can post your window creation code, in case misssing some tag.

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()
  1. if (!(Objects[OID_WINDOW]=IIntuition->NewObject(IWindow->WINDOW_GetClass(),NULL,
  2. WA_PubScreen, DefaultPubScreen,
  3. WA_Title, WindowTitle,
  4. WA_Left, Prefs->WindowX+(10*(PortNumber-1)),
  5. WA_Top, Prefs->WindowY+(10*(PortNumber-1)),
  6. WA_Width, Prefs->WindowWidth,
  7. WA_Height, Prefs->WindowHeight,
  8. WA_Flags, WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_SIZEBBOTTOM | WFLG_SIZEGADGET | WFLG_ACTIVATE,
  9. WA_IDCMP, IDCMP_MENUPICK | IDCMP_RAWKEY | IDCMP_EXTENDEDMOUSE | IDCMP_MOUSEBUTTONS | IDCMP_DISKINSERTED | IDCMP_DISKREMOVED | IDCMP_NEWSIZE | IDCMP_CHANGEWINDOW,
  10. WINDOW_UniqueID, PortBuffer,
  11. WINDOW_AppPort, UserPort,
  12. WINDOW_PopupGadget, TRUE,
  13. WINDOW_JumpScreensMenu, TRUE,
  14. WINDOW_AppWindow, TRUE,
  15. WINDOW_AppMsgHook, appMsgHook,
  16. WINDOW_IconifyGadget, TRUE,
  17. WINDOW_GadgetHelp, TRUE,
  18. WINDOW_IDCMPHook, idcmpHook,
  19. WINDOW_IDCMPHookBits, IDCMP_IDCMPUPDATE | IDCMP_RAWKEY | IDCMP_EXTENDEDMOUSE | IDCMP_MOUSEBUTTONS | IDCMP_DISKINSERTED | IDCMP_DISKREMOVED,
  20. WINDOW_Layout, Objects[OID_LAYOUT]=IIntuition->NewObject(ILayout->LAYOUT_GetClass(),NULL,

DefaultPubScreen is NULL, so Workbench.

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()

Figured it out. I should be looking at the screen's RastPort, not the window. Which makes sense so I can get the max text length to build the gadgets before opening the window.
if ((Len=IGraphics->TextLength(&DefaultPubScreen->RastPort,Text,IUtility->Strlen(Text)))>MaxLen)

hypex
hypex's picture
Offline
Last seen: 3 months 3 weeks ago
Joined: 2011-09-09 16:20
Re: TextLength()

That's interesting. When I want to get text length of strings I last used IntuiTextLength().

OldFart
OldFart's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2010-11-30 14:09
Re: TextLength()

@mritter0:

Nitpicking, but from the snippet of code you supplied it shows that, per NewObject()-call, you are (still) using the GetClass()-function as supplied by the class itself. As of V50 this is deprecated practice. See the Autodoc below, regarding this topic.

OldFart


  1. intuition.library/OpenClass intuition.library/OpenClass
  2.  
  3. NAME
  4. OpenClass -- Open a diskbased BOOPSI class. (V50)
  5.  
  6. SYNOPSIS
  7. struct ClassLibrary *result = OpenClass(CONST_STRPTR name, uint32 version,
  8. Class **class_ptr);
  9.  
  10. FUNCTION
  11. This function can be used to open a disk-based BOOPSI class. It works
  12. like exec.library/OpenLibrary() but also returns a pointer to the class.
  13.  
  14. Use of this function can eliminate the need for getting the class
  15. interface and using the deprecated *_GetClass() functions.
  16.  
  17. INPUTS
  18. name - Name of the BOOPSI class to open
  19. (including possible path prefix like "gadgets/" or "images/").
  20. version - Minimum version of the class required.
  21. class_ptr - Pointer to a Class pointer.
  22. Use this pointer with NewObject().
  23.  
  24. RESULT
  25. result - Pointer to the ClassLibrary base or NULL on error.
  26.  
  27. NOTES
  28. Take care this function may fail miserably when the class
  29. doesn't use a struct ClassLibrary * base!
  30.  
  31. SEE ALSO
  32. CloseClass(), NewObject()
  33.  

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()

Previously I posted a question on Hyperion Forum and Steven Solie said that the Reaction macros are GOING to be deprecated so don't use them. Fine. Go into reaction_macros.h and replace the #define shortcut with the code it represents (in my code). That is the code.

What should I be using then for the window and gadgets?

Edit: I am opening the class before this.

  1. WindowBase=IExec->OpenLibrary("window.class",54);
  2. if (!WindowBase || !LIB_IS_AT_LEAST((struct Library *)WindowBase,54,1))
  3. {
  4. SAK_EasyRequest(Objects[OID_WINDOW],MainWindow,RequesterTitle,REQIMAGE_ERROR,0,SAK_LocaleString(MSG_LIBS_COULD_NOT_OPEN_X_CLASS),SAK_LocaleString(MSG_TERMINATE),"Window",54,1);
  5.  
  6. return(FALSE);
  7. }
  8. IWindow=(struct WindowIFace *)IExec->GetInterface(WindowBase,"main",1,NULL);
OldFart
OldFart's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2010-11-30 14:09
Re: TextLength()

@mritter0:

Here's an idea about how to do:

  1. // WindowBase=IExec->OpenLibrary("window.class",54);
  2. WindowBase=Intuition->OpenClass("window.class",54, &WindowClass);
  3. if (!WindowBase || !LIB_IS_AT_LEAST((struct Library *)WindowBase,54,1))
  4. {
  5. SAK_EasyRequest(Objects[OID_WINDOW],MainWindow,RequesterTitle,REQIMAGE_ERROR,0,SAK_LocaleString(MSG_LIBS_COULD_NOT_OPEN_X_CLASS),SAK_LocaleString(MSG_TERMINATE),"Window",54,1);
  6.  
  7. return(FALSE);
  8. }
  9. IWindow=(struct WindowIFace *)IExec->GetInterface(WindowBase,"main",1,NULL);
  10.  
  11. Object *MyWindow = IIntuition->NewObject(WindowClass, NULL, ....., TAG_END);
  12.  
  13. .
  14. .
  15. etc.,
  16. etc.,
  17. .
  18. .
  19.  
  20. IIntuition->CloseClass(WindowBase);

An excellent tutorial about this subject by Trixie you can find HERE. This is a very good read and is highly endorsed!

About the ReAction macro's: my perception about them is that they are absolute crap and severly contribute to ReAction's infamous image. Avoid them! They should have been deprecated decades ago if ever introduced. Anything you create yourself in that respect will PROBABLY suit you better then those macro's. But, as I said already: this is MY perception.

OldFart

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
Re: TextLength()


About the ReAction macro's: my perception about them is that they are absolute crap and severly contribute to ReAction's infamous image. Avoid them! They should have been deprecated decades ago if ever introduced. Anything you create yourself in that respect will PROBABLY suit you better then those macro's. But, as I said already: this is MY perception.

AFAIK the WindowObject, VGroupObject, etc. macros are inspired by MUI which has very similar macros for GUI creation.

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()

@OldFart: Nit picks are fine with me. I want it right. Thanks. Updated it. I kind of had it that way back when I first started my project on OS3.9. OpenClass() isn't there so I was using someone's custom class opener.

I read Trixie's post but it didn't "click" that I still needed to update my code to OS4 since it was working.

Trixie does struct ClassLibrary * and I started with that but get redefined errors because the classes are still struct Library * in the proto header files (proto/chooser.h for example). Instead of casting them, just update them in the SDK. I would rather be "forced" to update my code so I know it will work in the future and be more stable in the present.

People say don't use the ReAction macros anymore. Some say don't use libauto anymore. I was told (suggested) to not use __USE_INLINE__ anymore. Most (if not all) of the SDK examples follow any of these suggestions. Even the new MenuClass example is using old code. If the core developers aren't doing things the way they tell us we should be doing it, how are we supposed to know what to do? If the examples supplied to us are outdated, how are we to know about newer functions?

There is an obsolete file for DOS functions. Make one for every library and get people in the habit of updating their code. Better yet, don't let the obsolete file update their code without their knowledge, just throw an error. But make a large, global, obsolete file in the root of the SDK with the new functions to use instead. It wouldn't bother me at all to update my code, but I have to know there is a file to look at to see what to use now.

Just do it. Force programmers to update their code and "do it the right way." Backwards compatibility can only go so far.

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
Re: TextLength()

@mritter0


Trixie does struct ClassLibrary * and I started with that but get redefined errors because the classes are still struct Library * in the proto header files (proto/chooser.h for example).

FWIW I'm considering changing the proto header files' extern definitions for class libraries to struct ClassLibrary * but as this change will probably break a lot of existing code I'd like to have some input from other developers first before making such a change. Also it will cause problems for programs that need to be compiled for both older and newer AmigaOS so some kind of define to switch back to the old struct Library * definition might be in order.

As is currently you can either define your library base globals as struct Library * and add casting in your OpenClass()/CloseClass() calls or define __NOLIBBASE__ to disable the proto file extern definition.

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: TextLength()

@mritter
While everyone else is concerned with the use of macros, I'm wondering if using the screen's font is the right choice. Since you appear to be concerned about gadget text, what happens if the user has set custom fonts & font sizes in the "GUI preferences/Gadgets/General" tab? Will that affect your use of the screen font to calculate text lengths??

X1000 - OS 4.1FE

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()

@xenic: Yes, gadget text is what I want to account for. But since the window is not open yet the default screen font is all I can look at. Unless there is something else.....

@salass00: I hope I didn't sound too rash/harsh. The OS is almost too flexible. Some things just need to forced to be done a certain way, while simple other things (labels, images) can still have some of the nice flexibility.

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Re: TextLength()

A gadget class in a ReAction GUI gets its GM_DOMAIN method called with a RastPort set up properly for layout. There is no need to look at other font sources.

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: TextLength()

Yes, gadget text is what I want to account for. But since the window is not open yet the default screen font is all I can look at. Unless there is something else.....

There is GUIA_GadgetTextAttr & GUIA_LabelTextAttr in SDK:Include/include_h/intuition/gui.h but I've never attempted to get any of those settings myself.

X1000 - OS 4.1FE

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()

@Thomas: Finding size of system font

In your reply #2 you say to use the screen font. If not, then what are the ReAction classes using to find the correct font and size?

@xenic: GUIA_GadgetTextAttr is probably what I need. I was just reading the gui.h and says to not access them unless have a really good reason. Guess it wouldn't hurt to read them, just not write to them.

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: TextLength()

@mritter

GUIA_GadgetTextAttr is probably what I need. I was just reading the gui.h and says to not access them unless have a really good reason. Guess it wouldn't hurt to read them, just not write to them.

You really don't want to override user preferences, so those GUI attributes should only be read. I was wrong about never having used them myself. I modified an old 'personal-use' gadtools program to use the same gadget fonts as Reaction so it would be more readable. Here is a snippet of code I used:

  1. struct TextAttr *sysfontTA = NULL;
  2. struct DrawInfo *sdi;
  3. uint32 result = 0;
  4.  
  5. sdi = IIntuition->GetScreenDrawInfo(screen);
  6. if (sdi)
  7. {
  8. result = IIntuition->GetGUIAttrs(NULL,sdi,GUIA_GadgetTextAttr,
  9. &sysfontTA, TAG_END);
  10. IIntuition->FreeScreenDrawInfo(screen,sdi);
  11. }
  12.  
  13. if ((result < 1) || (!sysfontTA))
  14. {
  15. result = IIntuition->GetScreenAttr(screen, SA_Font, &sysfontTA,
  16. sizeof(sysfontTA));
  17. }

X1000 - OS 4.1FE

mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: TextLength()

@xenic: Updated my code with a mix of yours and mine. Works just fine. Thanks.

Log in or register to post comments