source: window.c @ fe49685

release-1.10release-1.7release-1.8release-1.9
Last change on this file since fe49685 was fe49685, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Be better about noticing failed window creations Any failure and we simply don't realize the window.
  • Property mode set to 100644
File size: 12.0 KB
Line 
1#include "owl.h"
2
3#include <assert.h>
4
5struct _owl_window { /*noproto*/
6  /* hierarchy information */
7  owl_window *parent;
8  owl_window *child;
9  owl_window *next, *prev;
10  /* flags */
11  int dirty : 1;
12  int dirty_subtree : 1;
13  int mapped : 1;
14  int is_screen : 1;
15  /* window information */
16  WINDOW *win;
17  PANEL *pan;
18  int nlines, ncols;
19  int begin_y, begin_x;
20  /* hooks */
21  void (*redraw_cb)(owl_window *, WINDOW *, void *);
22  void  *redraw_cbdata;
23  void (*redraw_cbdata_destroy)(void *);
24
25  void (*size_cb)(owl_window *, void *);
26  void  *size_cbdata;
27  void (*size_cbdata_destroy)(void *);
28
29  void (*destroy_cb)(owl_window *, void *);
30  void  *destroy_cbdata;
31  void (*destroy_cbdata_destroy)(void *);
32};
33
34static owl_window *_owl_window_new(owl_window *parent, int nlines, int ncols, int begin_y, int begin_x);
35
36static void _owl_window_unlink(owl_window *w);
37static void _owl_window_link(owl_window *w, owl_window *parent);
38
39static void _owl_window_create_curses(owl_window *w);
40static void _owl_window_destroy_curses(owl_window *w);
41
42static void _owl_window_realize(owl_window *w);
43static void _owl_window_unrealize(owl_window *w);
44
45/** singletons **/
46
47static WINDOW *_dummy_window(void)
48{
49  static WINDOW *dummy = NULL;
50  if (!dummy) {
51    dummy = newwin(1, 1, 0, 0);
52  }
53  return dummy;
54}
55
56static void _screen_calculate_size(owl_window *screen, void *user_data)
57{
58  owl_global *g = user_data;
59  int lines, cols;
60  owl_global_get_terminal_size(&lines, &cols);
61  if (!g->lines) g->lines = lines;
62  if (!g->cols) g->cols = cols;
63  owl_window_resize(screen, g->lines, g->cols);
64}
65
66owl_window *owl_window_get_screen(void)
67{
68  static owl_window *screen = NULL;
69  if (!screen) {
70    /* The screen is magical. It's 'mapped', but the only mean of it going
71     * invisible is if we're tore down curses (i.e. screen resize) */
72    screen = _owl_window_new(NULL, g.lines, g.cols, 0, 0);
73    screen->is_screen = 1;
74    owl_window_set_size_cb(screen, _screen_calculate_size, &g, 0);
75    owl_window_map(screen, 0);
76  }
77  return screen;
78}
79
80/** Creation and Destruction **/
81
82owl_window *owl_window_new(owl_window *parent)
83{
84  if (!parent)
85    parent = owl_window_get_screen();
86  return _owl_window_new(parent, 0, 0, 0, 0);
87}
88
89static owl_window *_owl_window_new(owl_window *parent, int nlines, int ncols, int begin_y, int begin_x)
90{
91  owl_window *w;
92
93  w = owl_malloc(sizeof(owl_window));
94  memset(w, 0, sizeof(*w));
95
96  w->nlines = nlines;
97  w->ncols = ncols;
98  w->begin_y = begin_y;
99  w->begin_x = begin_x;
100
101  _owl_window_link(w, parent);
102  if (parent && parent->is_screen) {
103    w->pan = new_panel(_dummy_window());
104    set_panel_userptr(w->pan, w);
105  }
106
107  return w;
108}
109
110void owl_window_delete(owl_window *w)
111{
112  /* destroy all children */
113  owl_window_children_foreach_onearg(w, owl_window_delete);
114
115  /* notify everyone of your imminent demise */
116  if (w->destroy_cb)
117    w->destroy_cb(w, w->destroy_cbdata);
118
119  /* clear all cbs */
120  owl_window_set_redraw_cb(w, 0, 0, 0);
121  owl_window_set_size_cb(w, 0, 0, 0);
122  owl_window_set_destroy_cb(w, 0, 0, 0);
123
124  /* destroy curses structures */
125  owl_window_unmap(w);
126  if (w->pan) {
127    del_panel(w->pan);
128  }
129
130  /* remove from hierarchy */
131  _owl_window_unlink(w);
132  owl_free(w);
133}
134
135/** Callbacks **/
136
137void owl_window_set_redraw_cb(owl_window *w, void (*cb)(owl_window*, WINDOW*, void*), void *cbdata, void (*cbdata_destroy)(void*))
138{
139  if (w->redraw_cbdata_destroy) {
140    w->redraw_cbdata_destroy(w->redraw_cbdata);
141    w->redraw_cbdata = 0;
142    w->redraw_cbdata_destroy = 0;
143  }
144
145  w->redraw_cb = cb;
146  w->redraw_cbdata = cbdata;
147  w->redraw_cbdata_destroy = cbdata_destroy;
148
149  /* mark the window as dirty, to take new cb in account */
150  owl_window_dirty(w);
151}
152
153void owl_window_set_size_cb(owl_window *w, void (*cb)(owl_window*, void*), void *cbdata, void (*cbdata_destroy)(void*))
154{
155  if (w->size_cbdata_destroy) {
156    w->size_cbdata_destroy(w->size_cbdata);
157    w->size_cbdata = 0;
158    w->size_cbdata_destroy = 0;
159  }
160
161  w->size_cb = cb;
162  w->size_cbdata = cbdata;
163  w->size_cbdata_destroy = cbdata_destroy;
164
165  owl_window_recompute_position(w);
166}
167
168void owl_window_set_destroy_cb(owl_window *w, void (*cb)(owl_window*, void*), void *cbdata, void (*cbdata_destroy)(void*))
169{
170  if (w->destroy_cbdata_destroy) {
171    w->destroy_cbdata_destroy(w->destroy_cbdata);
172    w->destroy_cbdata = 0;
173    w->destroy_cbdata_destroy = 0;
174  }
175
176  w->destroy_cb = cb;
177  w->destroy_cbdata = cbdata;
178  w->destroy_cbdata_destroy = cbdata_destroy;
179}
180
181/** Hierarchy **/
182
183static void _owl_window_unlink(owl_window *w)
184{
185  /* make sure the window is unmapped first */
186  _owl_window_unrealize(w);
187  /* unlink parent/child information */
188  if (w->parent) {
189    if (w->prev)
190      w->prev->next = w->next;
191    if (w->next)
192      w->next->prev = w->prev;
193    if (w->parent->child == w)
194      w->parent->child = w->next;
195  }
196  w->parent = NULL;
197}
198
199static void _owl_window_link(owl_window *w, owl_window *parent)
200{
201  if (w->parent == parent)
202    return;
203  if (w->parent)
204    _owl_window_unlink(w);
205
206  w->parent = parent;
207  if (parent) {
208    w->next = parent->child;
209    parent->child = w;
210  }
211}
212
213/* mimic g_list_foreach for consistency */
214void owl_window_children_foreach(owl_window *parent, GFunc func, gpointer user_data)
215{
216  owl_window *w;
217  for (w = parent->child;
218       w != NULL;
219       w = w->next) {
220    func(w, user_data);
221  }
222}
223
224void owl_window_children_foreach_onearg(owl_window *parent, void (*func)(owl_window*))
225{
226  owl_window *w;
227  for (w = parent->child;
228       w != NULL;
229       w = w->next) {
230    func(w);
231  }
232}
233
234owl_window *owl_window_get_parent(owl_window *w)
235{
236  return w->parent;
237}
238
239/** Internal window management **/
240
241static void _owl_window_create_curses(owl_window *w)
242{
243  if (w->is_screen) {
244    resizeterm(w->nlines, w->ncols);
245    w->win = stdscr;
246  } else if (w->pan) {
247    w->win = newwin(w->nlines, w->ncols, w->begin_y, w->begin_x);
248    replace_panel(w->pan, w->win);
249  } else {
250    if (w->parent == NULL || w->parent->win == NULL)
251      return;
252    w->win = derwin(w->parent->win, w->nlines, w->ncols, w->begin_y, w->begin_x);
253  }
254}
255
256static void _owl_window_destroy_curses(owl_window *w)
257{
258  if (w->is_screen) {
259    /* don't deallocate the dummy */
260    w->win = NULL;
261  } else {
262    if (w->pan) {
263      /* panels assume their windows always exist, so we put in a fake one */
264      replace_panel(w->pan, _dummy_window());
265    }
266    if (w->win) {
267      /* and destroy our own window */
268      delwin(w->win);
269      w->win = NULL;
270    }
271  }
272}
273
274static void _map_recurse_curry(owl_window *w)
275{
276  owl_window_map(w, 1);
277}
278
279void owl_window_map(owl_window *w, int recurse)
280{
281  w->mapped = 1;
282  _owl_window_realize(w);
283  if (w->pan)
284    show_panel(w->pan);
285  if (recurse)
286    owl_window_children_foreach_onearg(w, _map_recurse_curry);
287}
288
289void owl_window_unmap(owl_window *w)
290{
291  /* you can't unmap the screen */
292  if (w->is_screen)
293    return;
294  w->mapped = 0;
295  if (w->pan)
296    hide_panel(w->pan);
297  _owl_window_unrealize(w);
298}
299
300int owl_window_is_mapped(owl_window *w)
301{
302  return w->mapped;
303}
304
305int owl_window_is_realized(owl_window *w)
306{
307  return w->win != NULL;
308}
309
310int owl_window_is_toplevel(owl_window *w)
311{
312  return w->pan != NULL;
313}
314
315static void _owl_window_realize(owl_window *w)
316{
317  /* check if we can create a window */
318  if ((w->parent && w->parent->win == NULL)
319      || !w->mapped
320      || w->win != NULL)
321    return;
322  _owl_window_create_curses(w);
323  if (w->win == NULL)
324    return;
325  /* schedule a repaint */
326  owl_window_dirty(w);
327  /* map the children */
328  owl_window_children_foreach_onearg(w, _owl_window_realize);
329}
330
331static void _owl_window_unrealize(owl_window *w)
332{
333  if (w->win == NULL)
334    return;
335  /* unmap all the children */
336  owl_window_children_foreach_onearg(w, _owl_window_unrealize);
337  _owl_window_destroy_curses(w);
338  /* subwins leave a mess in the parent; dirty it */
339  if (w->parent)
340    owl_window_dirty(w->parent);
341}
342
343/** Painting and book-keeping **/
344
345void owl_window_dirty(owl_window *w)
346{
347  /* don't put the screen on this list; pointless */
348  if (w->is_screen)
349    return;
350  if (!owl_window_is_realized(w))
351    return;
352  if (!w->dirty) {
353    w->dirty = 1;
354    while (w && !w->dirty_subtree) {
355      w->dirty_subtree = 1;
356      w = w->parent;
357    }
358    owl_global_set_needrefresh(&g);
359  }
360}
361
362void owl_window_dirty_children(owl_window *w)
363{
364  owl_window_children_foreach_onearg(w, owl_window_dirty);
365}
366
367static void _owl_window_redraw(owl_window *w)
368{
369  if (!w->dirty) return;
370  if (w->win && w->redraw_cb) {
371    w->redraw_cb(w, w->win, w->redraw_cbdata);
372    wsyncup(w->win);
373  }
374  w->dirty = 0;
375}
376
377static void _owl_window_redraw_subtree(owl_window *w)
378{
379  if (!w->dirty_subtree)
380    return;
381  _owl_window_redraw(w);
382  owl_window_children_foreach_onearg(w, _owl_window_redraw_subtree);
383}
384
385/*
386Redraw all the windows with scheduled redraws.
387NOTE: This function shouldn't be called outside the event loop
388*/
389void owl_window_redraw_scheduled(void)
390{
391  _owl_window_redraw_subtree(owl_window_get_screen());
392}
393
394void owl_window_erase_cb(owl_window *w, WINDOW *win, void *user_data)
395{
396  werase(win);
397  owl_window_dirty_children(w);
398}
399
400/** Window position **/
401
402void owl_window_get_position(owl_window *w, int *nlines, int *ncols, int *begin_y, int *begin_x)
403{
404  if (nlines)  *nlines  = w->nlines;
405  if (ncols)   *ncols   = w->ncols;
406  if (begin_y) *begin_y = w->begin_y;
407  if (begin_x) *begin_x = w->begin_x;
408}
409
410void owl_window_move(owl_window *w, int begin_y, int begin_x)
411{
412  if (w->is_screen) return; /* can't move the screen */
413  if (w->begin_y == begin_y && w->begin_x == begin_x) return;
414
415  w->begin_y = begin_y;
416  w->begin_x = begin_x;
417  if (w->mapped) {
418    /* Window is mapped, we must try to have a window at the end */
419    if (w->win) {
420      /* We actually do have a window; let's move it */
421      if (w->pan) {
422        if (move_panel(w->pan, begin_y, begin_x) == OK)
423          return;
424      } else {
425        if (mvderwin(w->win, begin_y, begin_x) == OK) {
426          /* now both we and the parent are dirty */
427          owl_window_dirty(w->parent);
428          owl_window_dirty(w);
429          return;
430        }
431      }
432    }
433    /* We don't have a window or failed to move it. Fine. Brute force. */
434    _owl_window_unrealize(w);
435    _owl_window_realize(w);
436  }
437}
438
439void owl_window_set_position(owl_window *w, int nlines, int ncols, int begin_y, int begin_x)
440{
441  /* can't move the screen */
442  if (w->is_screen) {
443    begin_y = begin_x = 0;
444  }
445
446  if (w->nlines == nlines && w->ncols == ncols) {
447    /* moving is easier */
448    owl_window_move(w, begin_y, begin_x);
449    return;
450  }
451
452  w->begin_y = begin_y;
453  w->begin_x = begin_x;
454  w->nlines = nlines;
455  w->ncols = ncols;
456  /* window is mapped, we must try to have a window at the end */
457  if (w->mapped) {
458    /* resizing in ncurses is hard: give up do a unrealize/realize */
459    _owl_window_unrealize(w);
460  }
461  /* recalculate children sizes BEFORE remapping, so that everything can resize */
462  owl_window_children_foreach_onearg(w, owl_window_recompute_position);
463  if (w->mapped) {
464    _owl_window_realize(w);
465  }
466}
467
468void owl_window_resize(owl_window *w, int nlines, int ncols)
469{
470  owl_window_set_position(w, nlines, ncols, w->begin_y, w->begin_x);
471}
472
473void owl_window_recompute_position(owl_window *w)
474{
475  if (w->size_cb) {
476    /* TODO: size_cb probably wants to actually take four int*s */
477    w->size_cb(w, w->size_cbdata);
478  }
479}
480
481
482/** Stacking order **/
483
484void owl_window_top(owl_window *w) {
485  if (!w->pan) {
486    owl_function_debugmsg("Warning: owl_window_top only makes sense on top-level window");
487    return;
488  }
489  top_panel(w->pan);
490}
491
492owl_window *owl_window_above(owl_window *w) {
493  PANEL *pan;
494
495  if (!w->pan) {
496    owl_function_debugmsg("Warning: owl_window_above only makes sense on top-level window");
497    return NULL;
498  }
499  pan = panel_above(w->pan);
500  /* cast because panel_userptr pointlessly returns a const void * */
501  return pan ? (void*) panel_userptr(pan) : NULL;
502}
503
504owl_window *owl_window_below(owl_window *w) {
505  PANEL *pan;
506
507  if (!w->pan) {
508    owl_function_debugmsg("Warning: owl_window_above only makes sense on top-level window");
509    return NULL;
510  }
511  pan = panel_below(w->pan);
512  /* cast because panel_userptr pointlessly returns a const void * */
513  return pan ? (void*) panel_userptr(pan) : NULL;
514}
Note: See TracBrowser for help on using the repository browser.