Subject: FVWM: [PATCH]Buttons on FvwmTaskBar have positions corresponding to window locations

From: Cheuk-san Edward Wang <[email-masked]>
Date: Fri, 5 Jun 1998 20:16:49 GMT

I usually have many windows opened at once, like mutiple rxvt's,
netscape's, etc. They have the same names and it was a pain to find
the one that I wanted. This patch will sort the buttons according to
the X coordinates of the centers of the windows. Now a window on
the left of the screen will have its button on the left, and a window
on the right will have its button on the right. This patch makes my
life much easier. I've been using it for a month and haven't found any 
problems yet.

Enjoy 

Ed Wang

--- ButtonArray.c.orig	Fri Jun  5 15:35:50 1998
+++ ButtonArray.c	Fri Jun  5 15:22:08 1998
@@ -77,7 +77,7 @@
 /* -------------------------------------------------------------------------
    ButtonNew - Allocates and fills a new button structure
    ------------------------------------------------------------------------- */
-Button *ButtonNew(char *title, Picture *p, int state, int count)
+Button *ButtonNew(char *title, Picture *p, long frame_center, int state, int count)
 {
   Button *new;
 
@@ -94,9 +94,9 @@
     new->p.picture = 0;
   }
 
+   new->frame_center = frame_center;
   new->state = state;
   new->count = count;
-  new->next  = NULL;
   new->needsupdate = 1;
 
   return new;
@@ -255,19 +255,77 @@
 /* -------------------------------------------------------------------------
    AddButton - Allocate space for and add the button to the list
    ------------------------------------------------------------------------- */
-void AddButton(ButtonArray *array, char *title, Picture *p, int state, int count)
+void AddButton(ButtonArray *array, char *title, Picture *p, long frame_center, int state, int count)
 {
   Button *new, *temp;
 
-  new = ButtonNew(title, p, state, count);
-  if (array->head == NULL) array->head = new;
-  else {
-    for (temp=array->head; temp->next; temp=temp->next);
+  new = ButtonNew(title, p, frame_center, state, count);
+  if (array->head == NULL || array->head->frame_center > frame_center) {
+     new->next = array->head;
+     array->head = new;
+  } else {
+    for (temp=array->head; temp->next && temp->next->frame_center<frame_center; 
+	 temp=temp->next);
+     new->next = temp->next;
     temp->next = new;
   }
   array->count++;
 
   ArrangeButtonArray (array);
+}
+
+/* -------------------------------------------------------------------------
+   InsertButton - Insert a button at it's new position
+   ------------------------------------------------------------------------- */
+void InsertButton(ButtonArray *array, Button *but)
+{
+   Button *temp;
+   
+   if (but->frame_center < array->head->frame_center) {
+      but->next = array->head;
+      array->head = but;
+   } else {
+      for(temp=array->head; temp->next && temp->next->frame_center < but->frame_center;
+	  temp=temp->next);
+      but->next = temp->next;
+      temp->next = but;
+   }
+}
+   
+/* -------------------------------------------------------------------------
+   MoveButton - Move a button to it's new position
+   ------------------------------------------------------------------------- */
+int MoveButton(ButtonArray *array, int butnum, long frame_center)
+{
+   Button *temp, *temp2;
+   
+   temp = array->head;
+   if (temp == NULL) return 0;
+   if (temp->count == butnum) {
+      temp->frame_center = frame_center;
+      if ((temp->next == NULL) || (temp->next->frame_center >= frame_center))
+	/* don't need to redraw */
+	return 0;
+      else {
+	 array->head = temp->next;
+	 InsertButton(array, temp);
+	 return 1;
+      }
+   } else {
+      for (; temp->next && temp->next->count != butnum; temp=temp->next);
+      temp2 = temp->next;
+      if (temp2 == NULL) return 0;
+      temp2->frame_center = frame_center;
+      if ((temp->frame_center <= frame_center) && 
+	  ((temp2->next == NULL) || (temp2->next->frame_center >= frame_center)))
+	return 0;
+      else {
+	 /* remove button */
+	 temp->next=temp2->next;
+	 InsertButton(array, temp2);
+	 return 1;
+      }
+   }
 }
 
 /* -------------------------------------------------------------------------
--- ButtonArray.h.orig	Fri Jun  5 15:35:53 1998
+++ ButtonArray.h	Thu May 14 14:56:24 1998
@@ -29,6 +29,7 @@
   int    state, needsupdate, truncate, count;
   struct button *next;
   Picture p;
+   long frame_center;
 } Button;
 
 typedef struct {
@@ -40,12 +41,12 @@
 /* Function Prototypes */
 
 void Draw3dRect(Window wn, int x, int y, int w, int h, int state);
-Button *ButtonNew(char *title, Picture *p, int state, int count); 
+Button *ButtonNew(char *title, Picture *p, long frame_center, int state, int count); 
 int ButtonUpdate(Button *button, char *title, int state);
 char *ButtonName(Button *button);
 void InitArray(ButtonArray *array, int x, int y, int w, int h, int tw);
 void UpdateArray(ButtonArray *array, int x, int y, int w, int h, int tw);
-void AddButton(ButtonArray *array, char *title, Picture *p, int state, int count);
+void AddButton(ButtonArray *array, char *title, Picture *p, long frame_center, int state, int count);
 int UpdateButton(ButtonArray *array, int butnum, char *title, int state);
 int UpdateButtonPicture(ButtonArray *array, int butnum, Picture *p);
 void RemoveButton(ButtonArray *array, int butnum);
@@ -58,6 +59,7 @@
 int WhichButton(ButtonArray *array, int x, int y);
 int LocateButton(ButtonArray *array, int xp,  int yp,
                                      int *xb, int *yb,
-                                     char **name, int *trunc); 
+                                     char **name, int *trunc);
+int MoveButton(ButtonArray *array, int butnum, long frame_center);
 void ArrangeButtonArray(ButtonArray *array);
 void ButtonDraw(Button *button, int x, int y, int w, int h);
--- FvwmTaskBar.c.orig	Fri Jun  5 15:36:00 1998
+++ FvwmTaskBar.c	Fri Jun  5 15:24:54 1998
@@ -325,7 +325,8 @@
       if (GetDeskNumber(&windows,i,&Desk) && DeskOnly) {
         if (DeskNumber != Desk && DeskNumber == body[7]) {
           /* window moving to current desktop */
-          AddButton(&buttons, ItemName(&windows,i), GetItemPicture(&windows,i), BUTTON_UP, i);
+          AddButton(&buttons, ItemName(&windows,i), GetItemPicture(&windows,i),
+		    ItemFrameCenter(&windows, i), BUTTON_UP, i);
           redraw = 1;
         }
         if (DeskNumber != body[7] && DeskNumber == Desk) {
@@ -333,13 +334,15 @@
           RemoveButton(&buttons, i);
           redraw = 1;
         }
-      }
-      UpdateItemFlagsDesk(&windows, body[0], body[8], body[7]);
+      } else if (MoveButton(&buttons, i, body[3]+body[5]/2))
+	   redraw = 1;
+      UpdateItemFlagsDesk(&windows, body[0], body[8], body[7],
+			  body[3]+body[5]/2);
       break;
     }
 
     if (!(body[8] & WINDOWLISTSKIP) || !UseSkipList) {
-      AddItem(&windows, body[0], body[8], body[7], Count++);
+      AddItem(&windows, body[0], body[8], body[7], body[3]+body[5]/2, Count++);
       if (Count > COUNT_LIMIT) Count = 0;
     }
     break;
@@ -389,7 +392,8 @@
       if (GetDeskNumber(&windows, i, &Desk) == 0) return; /* ?? */
       if (!DeskOnly || Desk == DeskNumber)
         {
-        AddButton(&buttons, string, NULL, BUTTON_UP, i);
+        AddButton(&buttons, string, NULL, ItemFrameCenter(&windows, i), BUTTON_UP,
+		  i);
         redraw = 1;
         }
       }
@@ -455,7 +459,8 @@
 
     for (item=windows.head; item; item=item->next)
 	if (DeskNumber == item->Desk || (item->flags & STICKY))
-	    AddButton(&buttons, item->name, &(item->p), BUTTON_UP, item->count);
+	    AddButton(&buttons, item->name, &(item->p), item->frame_center, 
+		      BUTTON_UP, item->count);
     
     RedrawWindow(1);
 }
--- List.c.orig	Fri Jun  5 15:36:03 1998
+++ List.c	Fri Jun  5 15:29:00 1998
@@ -42,14 +42,16 @@
 /******************************************************************************
   AddItem - Allocates spaces for and appends an item to the list
 ******************************************************************************/
-void AddItem(List *list, long id, long flags, long Desk, int count)
+void AddItem(List *list, long id, long flags, long Desk, long frame_center, int count)
 {
 Item *new;
+
   new=(Item *)safemalloc(sizeof(Item));
   new->id=id;
   new->name=NULL;
   new->flags=flags;
   new->Desk=Desk;
+   new->frame_center=frame_center;
   new->count=count;
   new->next=NULL;
 
@@ -129,13 +131,14 @@
   return temp->count;
 }
 
-int UpdateItemFlagsDesk(List *list, long id, long flags, long desk)
+int UpdateItemFlagsDesk(List *list, long id, long flags, long desk, long frame_center)
 {
 Item *temp;
   for(temp=list->head;temp!=NULL && id!=temp->id;temp=temp->next);
   if (temp==NULL) return -1;
   if (flags!=-1) temp->flags=flags;
   temp->Desk=desk;
+   temp->frame_center=frame_center;
   return temp->count;
 }
 
@@ -297,7 +300,7 @@
 Item *temp;
   for(temp=source->head; temp!=NULL && temp->count!=n; temp=temp->next);
   if (temp==NULL) return;
-  AddItem(dest,temp->id,temp->flags,temp->Desk,temp->count);
+  AddItem(dest,temp->id,temp->flags,temp->Desk,temp->frame_center,temp->count);
   UpdateItemName(dest,temp->id,temp->name);
   DeleteItem(source,temp->id);
 }
@@ -344,4 +347,16 @@
   for (temp=list->head;temp && temp->count!=n;temp=temp->next);
   if (temp==NULL) return 0;
   return &(temp->p);
+}
+
+/******************************************************************************
+  ItemFrameCenter - Returns the x-coordinate of the center
+******************************************************************************/
+long ItemFrameCenter(List *list, int n)
+{
+Item *temp;
+
+  for (temp=list->head;temp && temp->count!=n;temp=temp->next);
+  if (temp==NULL) return 0;
+  return temp->frame_center;
 }
--- List.h.orig	Fri Jun  5 15:36:08 1998
+++ List.h	Thu May 14 15:02:41 1998
@@ -20,6 +20,7 @@
   char *name;
   long flags;
   long Desk;
+  long frame_center;
   int  count;
   Picture p;
   struct item *next;
@@ -32,13 +33,13 @@
 
 /* Function Prototypes */
 void InitList(List *list);
-void AddItem(List *list, long id, long flags, long Desk, int count);
+void AddItem(List *list, long id, long flags, long Desk, long frame_center, int count);
 void AddItemName(List *list, char *string, long flags);
 int FindItem(List *list, long id);
 int FindNameItem(List *list, char *string);
 int UpdateItemName(List *list, long id, char *string);
 int UpdateItemFlags(List *list, long id, long flags);
-int UpdateItemFlagsDesk(List *list, long id, long flags, long desk);
+int UpdateItemFlagsDesk(List *list, long id, long flags, long desk, long frame_center);
 int UpdateNameItem(List *list, char *string, long id, long flags);
 void FreeItem(Item *ptr);
 int DeleteItem(List *list,long id);
@@ -54,4 +55,4 @@
 void UpdateItemPicture(List *list, int n, Picture *p);
 int GetDeskNumber(List *list, int n, long *Desk);
 Picture *GetItemPicture(List *list, int n);
-
+long ItemFrameCenter(List *list, int n);
--
Visit the official FVWM web page at <URL:http://www.hpc.uh.edu/fvwm/>.
To unsubscribe from the list, send "unsubscribe fvwm" in the body of a
message to [email-masked].
To report problems, send mail to [email-masked].