Looping through windows on a screen

4 posts / 0 new
Last post
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
Looping through windows on a screen
When looping through windows on a screen with the code below, and searching for a non-existing windowtitle, i get a crash? No problem when the title exists (CS)
  1. struct Window * GetWindow( STRPTR screenname, STRPTR windowabbrev ) {
  2. struct Screen *nxtscreen, *scr;
  3. struct Window *win, *selwin;
  4. int lgtwt, lgtabbr;
  5. char wtshort[31];
  6. BOOL winfound =FALSE;
  7. lgtabbr = strlen(windowabbrev);
  8. if (screenname[0] == 0 )
  9. {
  10. scr = LockPubScreen(NULL);
  11. }
  12. else scr = LockPubScreen(screenname);
  13. if (!(scr )) return (NULL); /// NULL Default public screen , mopstly WB screen
  14. win = scr->FirstWindow;
  15. while (win)
  16. {
  17. lgtwt = strlen(win->Title); /// ########crash here when all windows looped through
  18. if (lgtwt >= lgtabbr)
  19. {
  20. strncpy (wtshort, (char *)win->Title, lgtabbr);
  21. wtshort[lgtabbr]=0;
  22. if (strcmp(wtshort, windowabbrev) == 0) {
  23. winfound=TRUE;
  24. break;
  25. }
  26. }
  27. win = win->NextWindow;
  28. }
  29. if (winfound)
  30. {
  31. PutStr("found");
  32. UnlockPubScreen(screenname, scr);
  33. return (win);
  34. }
  35. else
  36. {
  37. UnlockPubScreen(screenname, scr);
  38. return (NULL);
  39. }
  40. }
What is wring with the loop? Why doesn't it stop when win is NULL ??
OldFart
OldFart's picture
Offline
Last seen: 2 months 6 days ago
Joined: 2010-11-30 14:09
I'm not exactly sure as it
I'm not exactly sure as it has been a while since I last touched this issue (my MicroA1 broke down 2 years ago...), but if I remember correctly, some windows have no titlestring, hence win->Title is NULL. Maybe this helps:
  1. struct Window * GetWindow(STRPTR screenname, STRPTR windowabbrev)
  2. {
  3. struct Window *win = NULL;
  4.  
  5. struct Screen *scr;
  6.  
  7. if (
  8. (screenname != NULL) &&
  9. (screenname[0] == 0)
  10. )
  11. {
  12. scr = LockPubScreen(NULL);
  13. }
  14. else
  15. {
  16. scr = LockPubScreen(screenname);
  17. }
  18.  
  19. if (scr)
  20. {
  21. BOOL winfound = FALSE;
  22. int lgtwt,
  23. lgtabbr = strlen(windowabbrev);
  24. char wtshort[31];
  25.  
  26. win = scr->FirstWindow;
  27.  
  28. while (
  29. (win != NULL) &&
  30. (winfound == FALSE)
  31. )
  32. {
  33. if (win->Title != NULL)
  34. {
  35. lgtwt = strlen(win->Title);
  36.  
  37. if (lgtwt >= lgtabbr)
  38. {
  39. strncpy (wtshort, (char *)win->Title, lgtabbr);
  40. wtshort[lgtabbr] = 0;
  41.  
  42. if (strcmp(wtshort, windowabbrev) == 0)
  43. {
  44. winfound = TRUE;
  45. }
  46. }
  47. }
  48.  
  49. win = win->NextWindow;
  50. }
  51.  
  52. if (winfound == TRUE)
  53. {
  54. PutStr("found");
  55. }
  56.  
  57. UnlockPubScreen(screenname, scr);
  58. }
  59.  
  60. return (win);
  61. }
Try it. Added an extra safeguard and removed some superfluous code. I'm sorry this being a nasty habbit of mine. OldFart
thomas
thomas's picture
Offline
Last seen: 1 day 7 hours ago
Joined: 2011-05-16 14:23
@OldFart: your routine does
@OldFart: your routine does not return the window which matches the title but the next one. Here is a shorter one and it works (tested):
  1. struct Window *get_window (STRPTR screenname, STRPTR windowabbrev)
  2.  
  3. {
  4. struct Window *win = NULL;
  5.  
  6. if (windowabbrev)
  7. {
  8. long abbrlen = strlen(windowabbrev);
  9. struct Screen *scr;
  10.  
  11. if (scr = LockPubScreen (screenname && !screenname[0] ? NULL : screenname))
  12. {
  13. for (win = scr->FirstWindow; win; win = win->NextWindow)
  14. {
  15. if (win->Title && !Strnicmp(win->Title,windowabbrev,abbrlen))
  16. break;
  17. }
  18.  
  19. UnlockPubScreen (NULL,scr);
  20. }
  21. }
  22.  
  23. return (win);
  24. }
Note: I intentionally use Strnicmp which does not care about upper/lower case. You can as well use strncmp if case matters.
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2012-01-08 20:57
@OldFart &Thomas Thanks for
@OldFart &Thomas Thanks for the help Works OK now
Log in or register to post comments