Jump to content
Eternal Lands Official Forums
Guest baneazaghal

Word-wrapping label

Recommended Posts

Guest baneazaghal

I've started working on the "tab-map" code wanting to improve it a bit and to add some more options to it (assigning categories to marks, filtering by them etc). What I need now is to create a label with some text that does the wrapping, and place it on the black part of the screen to the right of the map picture itself (I'll also need a window like this as well). I've managed to create a window, add (regular) labels to it, tried using extended labels/text fields, and it simply doesn't work (I've tried to figure it out by looking at notepad code, and failed). If you have some tutorial for me (regarding EL's widgets), apart from the doxygen docs, or if you could write me a really simple example (that just creates a window with some word-wrapped text in it), I'd be very grateful. Thanks :o

 

P.S.

This is the right spot to ask about this, right?

Share this post


Link to post
Share on other sites

I've started working on the "tab-map" code wanting to improve it a bit and to add some more options to it (assigning categories to marks, filtering by them etc).

Just in case: are you aware there is already the ability to filter marks in the tab map? If not, press CTRL+F and whilst in map-view and type some text.

Share this post


Link to post
Share on other sites
Guest baneazaghal

Yes, I am aware of that, but I want to implement additional functionality. Take a look at this thread for more information on what I want to add :o

Share this post


Link to post
Share on other sites
Guest baneazaghal

Ok, I've managed to play with regular "word-wrapping" labels (I took a look at dialog code). But I simply can't get the text_field_add_extended to work. I'm guessing I'm doing something wrong, but have no idea what. I tried taking a look at notepad code, and still clueless. Some help would really be appreciated.

int temp_window,temp_widget;
text_message buffer;
int len=strlen(marks[0].desc);

buffer.chan_idx=CHAT_NONE;
buffer.data=NULL;
buffer.size=0;
buffer.len=0;

buffer.len=len;
buffer.data=calloc(len,sizeof(char));
my_strcp(buffer.data,marks[0].desc);
buffer.size=len+20;
temp_window=create_window(marks[0].text,-1,0,0,0,100,100,ELW_WIN_DEFAULT);
temp_widget=text_field_add_extended(temp_window,0,NULL,0,0,100,100,TEXT_FIELD_BORDER|TEXT_FIELD_NO_KEYPRESS,1.0f,1.0f,0.0f,0.0f,&buffer,1,FILTER_ALL,0,0,0.0,1.0,0.0);

Share this post


Link to post
Share on other sites

buffer.size should reflect the real size of the buffer, so the sequence

buffer.len=len;
buffer.data=calloc(len,sizeof(char));
my_strcp(buffer.data,marks[0].desc);
buffer.size=len+20;

should really be

buffer.size=len+20;
buffer.data=calloc(buffer.size,sizeof(char));
my_strcp(buffer.data,marks[0].desc);
buffer.len=len;

(note that the first argument to the calloc is buffer.size, not buffer.len).

Because your buffer.size is greater than the actual buffer size, it's well possible that the line wrapping code starts overwriting precious data when it inserts line break characters.

Share this post


Link to post
Share on other sites
Guest baneazaghal

It still doesn't work. The current code creates a window, creates a text_field with a red border, and no text at all. Using some printf statements, I found out that buffer.data garbled after the call to text_field_add_extended.

Before:
This is one big MF 12345678901234567890 that should test some crapish-code I wrote!
This is one big MF 12345678901234567890 that should test some crapish-code I wrote!
83 83
After:
wrote!h-c
This is one big MF 12345678901234567890 that should test some crapish-code I wrote!
83 94

 

First line is the contents of marks[0].desc, second is the contents of buffer.data. Numbers represent the results of strlen function.

Edited by baneazaghal

Share this post


Link to post
Share on other sites

The buffer isn't garbled, but the line wrap character '\r' means return to start of line when you print it on a terminal.

 

Still not sure why it doesn't show anything, though.

 

EDIT: I assumed the two 'After' lines shoudl've been the other way round, i.e. first has length 94 and belongs to buffer.data, second has 83 characters and belongs to marks[0].desc

 

EDIT 2: do you see anything when you limit the text to, say, 3 characters?

Edited by Grum

Share this post


Link to post
Share on other sites
Guest baneazaghal

Ah, so that's why my printf doesn't show what I expected :confused: I tried putting "123" instead, nothing shows up again.

 

P.S.

Why in the world this thing's not using '\n'?

Share this post


Link to post
Share on other sites

I've started working on the "tab-map" code wanting to improve it a bit and to add some more options to it (assigning categories to marks, filtering by them etc).,,,,

Hi baneazaghal. I'm not 100% sure what you're planning with this but I'm sure it will be great :). However, please can I make one request, that the current, simple, ctrl-f filter functionality is maintained, at least as an option. OK, I have a vested interest as I did the ctrl-f patch but I really can't see me wanting to sort my marks by category. :P OK, I should wait and see. However, I currently find it really simple and straight forward to find what I need in amongst my unorganized mass of marks with just a few key presses. Good luck with the patch in any case :hug:

 

Did I use too many icons :)

Share this post


Link to post
Share on other sites
Guest baneazaghal

Err... Well, for start, I can't get going with the patch, since I can't make the damn text_field to work. Also, even if I do make a patch, I'm not sure if it will get accepted. I probably won't touch the Ctrl+F code much, but I'm guessing that those marks could actually all be kept in one category, so the behavior would be the same :)

 

P.S.

Ok, seriously, can anyone write me down a working example for creating a window with text_field widget on it?

Share this post


Link to post
Share on other sites
Guest baneazaghal

I am getting kind of desperate here. I would be very grateful if anyone could write me the following code:

 

1. Create a window

2. Create a text_message

3. Populate text_message with some string etc.

4. Create text_field in the window via add_text_field_extended, and populate it with the text_message created above (possibly during creation of text_field?)

 

Nothing fancy is needed, keep it as simple as possible if you can. :)

Share this post


Link to post
Share on other sites

I am getting kind of desperate here. I would be very grateful if anyone could write me the following code:

Have you checked examples in existing code? I wrote the serverpopup.c code that does exactly what you describe. To see what it does, just call display_server_popup_win(const char * const message), passing in a text string.

 

edit: added call example.

Edited by bluap

Share this post


Link to post
Share on other sites
Guest baneazaghal

Ok, I really don't get it what I'm doing wrong here:

 

int size=strlen(marks[0].desc)*2;
buffer.chan_idx=CHAT_NONE;
buffer.data=(char *) calloc(size,sizeof(Uint8)); //Is it ok to use char instead of Uint8 here?
safe_strncpy(buffer.data,marks[0].desc,size*sizeof(Uint8)); //Previous comment
buffer.len=strlen(buffer.data);
buffer.size=size;
buffer.wrap_width=0;
buffer.wrap_zoom=0;
buffer.wrap_lines=0;
buffer.max_line_width=0;
temp_window=create_window(marks[0].text,-1,0,0,0,100,100,ELW_TITLE_BAR|ELW_DRAGGABLE|ELW_USE_BACKGROUND|ELW_USE_BORDER|ELW_SHOW|ELW_TITLE_NAME|ELW_ALPHA_BORDER);
rewrap_message(&buffer,1,100,NULL);
temp_widget=text_field_add_extended(temp_window,0,NULL,0,0,100,100,TEXT_FIELD_BORDER|TEXT_FIELD_NO_KEYPRESS,1.0,0.77f,0.57f,0.39f,&buffer,1,FILTER_NONE,0,0,-1.0,
				-1.0,-1.0);

 

If possible, could you write me a simpler example than the one given in display_server_popup_win(const char* message)? Most part of that code is a bit confusing to me, to be honest. I could probably get it after some time, though, if my questions are too boring to you :)

Share this post


Link to post
Share on other sites

The following quick and dirty code works OK:

int testwin(void)
{
static int temp_window = -1;
char *testmessage = "Hello world";
size_t testmessage_len = strlen(testmessage);
int size = testmessage_len * 2;
static text_message buffer;
static int temp_widget = 101;

buffer.chan_idx=CHAT_NONE;
buffer.data=(char *) calloc(size,sizeof(Uint8));
safe_strncpy(buffer.data,testmessage,size*sizeof(Uint8));
buffer.len=strlen(buffer.data);
buffer.size=size;
buffer.wrap_width=0;
buffer.wrap_zoom=0;
buffer.wrap_lines=0;
buffer.max_line_width=0;

temp_window = create_window("Test window",game_root_win,0,0,0,100,100,
	  ELW_TITLE_BAR|ELW_DRAGGABLE|ELW_USE_BACKGROUND|
	  ELW_USE_BORDER|ELW_SHOW|ELW_TITLE_NAME|ELW_ALPHA_BORDER);

rewrap_message(&buffer,1,90,NULL);

temp_widget=text_field_add_extended(temp_window,temp_widget,NULL,0,0,90,90,
	TEXT_FIELD_BORDER|TEXT_FIELD_NO_KEYPRESS,1.0,0.77f,0.57f,0.39f,&buffer,1,
	FILTER_NONE,0,0,-1.0,-1.0,-1.0);

return 1;
}

Not sure what way your's is failing but this crashed the client until I made buffer static - obviously, the window handling code needs the structure not to disappear once the function returns :) Can't tell how your buffer is allocated but I hope this helps. I hooked the function into init_commands() in console.c bye the way.

 

edited for typos.

Edited by bluap

Share this post


Link to post
Share on other sites

Yes,, the text_field stores a pointer to a text_message, so the text_message shouldn't disappear while the text_field is in use.

Share this post


Link to post
Share on other sites
Guest baneazaghal

Ah, I knew it I was doing something VERY dumb :D Still haven't tried declaring it static and testing, but thanks :)

 

EDIT: Ah, it works now. Damn the lazy C++ programmers, eh? :)

Edited by baneazaghal

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×