[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: moving nested subwins
From: |
Clive Nicolson |
Subject: |
Re: moving nested subwins |
Date: |
Thu, 7 Jun 2007 13:21:58 +1200 (NZST) |
Oops I forgot to attach the code.
Hi,
here is some code which demonstrates how to fix the "menu subwin moving"
problem for mvwin. This code is nonrecursive and I think will not break user
code which traverses the window parent tree and recalculates _begy
and _begx. It also fixes the "form subwin moving" problem!
mvderwin needs a somewhat slightly different fix.
Anyone want a patch?
Clive
#include <stdlib.h>
#include <stdio.h>
typedef struct _win_st WINDOW;
struct _win_st {
int _begy;
int _begx;
WINDOW *_parent;
/* New fields */
WINDOW *_nc_lastchild;
WINDOW *_nc_lastsibling;
/* Test fields */
char name[16+1];
int moved;
};
static int i;
/* Move all descendants of root */
static void
moveall(WINDOW *root, int dy, int dx) {
WINDOW *window;
window = root;
loop:
while (window->_nc_lastchild)
window = window->_nc_lastchild;
while (window != root) {
{
i--;
printf("%s 1: %p %p %p",
window->name,
(void *)window->_parent,
(void *)window->_nc_lastchild, (void *)window->_nc_lastsibling);
if (window->moved)
printf(" ****");
else
window->moved = 1;
printf("\n");
}
window->_begy += dy;
window->_begx += dx;
if (window->_nc_lastsibling) {
window = window->_nc_lastsibling;
goto loop;
} else
window = window->_parent;
}
}
static void
addsubwindow(WINDOW *parent, WINDOW *window) {
window->_parent = parent;
window->_nc_lastchild = NULL;
window->_nc_lastsibling = parent->_nc_lastchild;
parent->_nc_lastchild = window;
}
static void
delwindow(WINDOW *window) {
WINDOW *child;
child = window->_nc_lastchild;
while (child) {
WINDOW *temp;
temp = child->_nc_lastsibling;
child->_parent = NULL;
child->_nc_lastsibling = NULL;
child = temp;
}
if (window->_parent) {
WINDOW *parent;
parent = window->_parent;
if (parent->_nc_lastchild) {
if (parent->_nc_lastchild == window)
parent->_nc_lastchild = window->_nc_lastsibling;
else {
WINDOW *sibling;
sibling = parent->_nc_lastchild;
while (sibling && sibling->_nc_lastsibling != window)
sibling = sibling->_nc_lastsibling;
if (sibling)
sibling->_nc_lastsibling = window->_nc_lastsibling;
}
}
}
free(window);
}
static WINDOW *
build(WINDOW *parent) {
WINDOW *win;
win = malloc(sizeof(*win));
win->_begy = 0;
win->_begx = 0;
win->_parent = parent;
win->_nc_lastchild = (random() <= (RAND_MAX-1)/2) ? NULL : build(win);
win->_nc_lastsibling = (random() <= (RAND_MAX-1)/2) ? NULL : build(parent);
sprintf(win->name, "%p", (void *)win);
win->moved = 0;
printf("%s 0\n", win->name);
i++;
return win;
}
int
main(int argc, char **argv) {
WINDOW *root;
i = 0;
root = build(NULL);
moveall(root, 2, 4);
printf("i=%i\n", i);
if (0) {
addsubwindow(root, NULL);
delwindow(root);
}
return (EXIT_SUCCESS);
}