source: window.c @ 7a70e26

release-1.7release-1.8release-1.9
Last change on this file since 7a70e26 was 7a70e26, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Punt owl_window_children_foreach_onearg Use GFunc casts everywhere. This horrid and depends on calling convention, but glib relies on it EVERYWHERE. If our environment doesn't support it, we'll have worse problems anyway.
  • Property mode set to 100644
File size: 11.9 KB
Line 
1#include "owl.h"
2
3#include <assert.h>
4
5struct _owl_window { /*noproto*/
6  GObject object;
7  /* hierarchy information */
8  owl_window *parent;
9  owl_window *child;
10  owl_window *next, *prev;
11  /* flags */
12  int dirty : 1;
13  int dirty_subtree : 1;
14  int shown : 1;
15  int is_screen : 1;
16  /* window information */
17  WINDOW *win;
18  PANEL *pan;
19  int nlines, ncols;
20  int begin_y, begin_x;
21  /* hooks */
22  void (*redraw_cb)(owl_window *, WINDOW *, void *);
23  void  *redraw_cbdata;
24  void (*redraw_cbdata_destroy)(void *);
25
26  void (*size_cb)(owl_window *, void *);
27  void  *size_cbdata;
28  void (*size_cbdata_destroy)(void *);
29};
30
31static void owl_window_dispose(GObject *gobject);
32static void owl_window_finalize(GObject *gobject);
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_link(owl_window *w, owl_window *parent);
37
38static void _owl_window_create_curses(owl_window *w);
39static void _owl_window_destroy_curses(owl_window *w);
40
41static void _owl_window_realize(owl_window *w);
42static void _owl_window_unrealize(owl_window *w);
43
44G_DEFINE_TYPE (OwlWindow, owl_window, G_TYPE_OBJECT)
45
46static void owl_window_class_init (OwlWindowClass *klass)
47{
48  GObjectClass *object_class = G_OBJECT_CLASS (klass);
49
50  object_class->dispose = owl_window_dispose;
51  object_class->finalize = owl_window_finalize;
52}
53
54static void owl_window_dispose (GObject *object)
55{
56  owl_window *w = OWL_WINDOW (object);
57
58  /* Unmap the window */
59  owl_window_hide (w);
60
61  /* Unlink and unref all children */
62  while (w->child) {
63    owl_window *child = w->child;
64    owl_window_unlink (child);
65  }
66
67  /* Clear all cbs */
68  owl_window_set_redraw_cb (w, 0, 0, 0);
69  owl_window_set_size_cb (w, 0, 0, 0);
70
71  /* Remove from hierarchy */
72  owl_window_unlink (w);
73
74  G_OBJECT_CLASS (owl_window_parent_class)->dispose (object);
75}
76
77static void owl_window_finalize (GObject *object)
78{
79  owl_window *w = OWL_WINDOW(object);
80
81  if (w->pan) {
82    del_panel(w->pan);
83    w->pan = NULL;
84  }
85
86  G_OBJECT_CLASS (owl_window_parent_class)->finalize (object);
87}
88
89static void owl_window_init (owl_window *w)
90{
91}
92
93/** singletons **/
94
95static WINDOW *_dummy_window(void)
96{
97  static WINDOW *dummy = NULL;
98  if (!dummy) {
99    dummy = newwin(1, 1, 0, 0);
100  }
101  return dummy;
102}
103
104static void _screen_calculate_size(owl_window *screen, void *user_data)
105{
106  owl_global *g = user_data;
107  int lines, cols;
108  owl_global_get_terminal_size(&lines, &cols);
109  if (!g->lines) g->lines = lines;
110  if (!g->cols) g->cols = cols;
111  owl_window_resize(screen, g->lines, g->cols);
112}
113
114owl_window *owl_window_get_screen(void)
115{
116  static owl_window *screen = NULL;
117  if (!screen) {
118    /* The screen is magical. It's 'shown', but the only mean of it going
119     * invisible is if we're tore down curses (i.e. screen resize) */
120    screen = _owl_window_new(NULL, g.lines, g.cols, 0, 0);
121    screen->is_screen = 1;
122    owl_window_set_size_cb(screen, _screen_calculate_size, &g, 0);
123    owl_window_show(screen);
124  }
125  return screen;
126}
127
128/** Creation and Destruction **/
129
130owl_window *owl_window_new(owl_window *parent)
131{
132  if (!parent)
133    parent = owl_window_get_screen();
134  return _owl_window_new(parent, 0, 0, 0, 0);
135}
136
137static owl_window *_owl_window_new(owl_window *parent, int nlines, int ncols, int begin_y, int begin_x)
138{
139  owl_window *w;
140
141  w = g_object_new (OWL_TYPE_WINDOW, NULL);
142  if (w == NULL) g_error("Failed to create owl_window instance");
143
144  w->nlines = nlines;
145  w->ncols = ncols;
146  w->begin_y = begin_y;
147  w->begin_x = begin_x;
148
149  _owl_window_link(w, parent);
150  if (parent && parent->is_screen) {
151    w->pan = new_panel(_dummy_window());
152    set_panel_userptr(w->pan, w);
153  }
154
155  return w;
156}
157
158/** Callbacks **/
159
160void owl_window_set_redraw_cb(owl_window *w, void (*cb)(owl_window*, WINDOW*, void*), void *cbdata, void (*cbdata_destroy)(void*))
161{
162  if (w->redraw_cbdata_destroy) {
163    w->redraw_cbdata_destroy(w->redraw_cbdata);
164    w->redraw_cbdata = 0;
165    w->redraw_cbdata_destroy = 0;
166  }
167
168  w->redraw_cb = cb;
169  w->redraw_cbdata = cbdata;
170  w->redraw_cbdata_destroy = cbdata_destroy;
171
172  /* mark the window as dirty, to take new cb in account */
173  owl_window_dirty(w);
174}
175
176void owl_window_set_size_cb(owl_window *w, void (*cb)(owl_window*, void*), void *cbdata, void (*cbdata_destroy)(void*))
177{
178  if (w->size_cbdata_destroy) {
179    w->size_cbdata_destroy(w->size_cbdata);
180    w->size_cbdata = 0;
181    w->size_cbdata_destroy = 0;
182  }
183
184  w->size_cb = cb;
185  w->size_cbdata = cbdata;
186  w->size_cbdata_destroy = cbdata_destroy;
187
188  owl_window_recompute_position(w);
189}
190
191/** Hierarchy **/
192
193void owl_window_unlink(owl_window *w)
194{
195  /* make sure the window is unmapped first */
196  _owl_window_unrealize(w);
197  /* unlink parent/child information */
198  if (w->parent) {
199    if (w->prev)
200      w->prev->next = w->next;
201    if (w->next)
202      w->next->prev = w->prev;
203    if (w->parent->child == w)
204      w->parent->child = w->next;
205    w->parent = NULL;
206    g_object_unref (w);
207  }
208}
209
210static void _owl_window_link(owl_window *w, owl_window *parent)
211{
212  if (w->parent == parent)
213    return;
214
215  owl_window_unlink(w);
216  if (parent) {
217    w->parent = parent;
218    w->next = parent->child;
219    parent->child = w;
220    g_object_ref (w);
221  }
222}
223
224/* mimic g_list_foreach for consistency */
225void owl_window_children_foreach(owl_window *parent, GFunc func, gpointer user_data)
226{
227  owl_window *w;
228  for (w = parent->child;
229       w != NULL;
230       w = w->next) {
231    func(w, user_data);
232  }
233}
234
235owl_window *owl_window_get_parent(owl_window *w)
236{
237  return w->parent;
238}
239
240/** Internal window management **/
241
242static void _owl_window_create_curses(owl_window *w)
243{
244  if (w->is_screen) {
245    resizeterm(w->nlines, w->ncols);
246    w->win = stdscr;
247  } else if (w->pan) {
248    w->win = newwin(w->nlines, w->ncols, w->begin_y, w->begin_x);
249    replace_panel(w->pan, w->win);
250  } else {
251    if (w->parent == NULL || w->parent->win == NULL)
252      return;
253    w->win = derwin(w->parent->win, w->nlines, w->ncols, w->begin_y, w->begin_x);
254  }
255}
256
257static void _owl_window_destroy_curses(owl_window *w)
258{
259  if (w->is_screen) {
260    /* don't deallocate the dummy */
261    w->win = NULL;
262  } else {
263    if (w->pan) {
264      /* panels assume their windows always exist, so we put in a fake one */
265      replace_panel(w->pan, _dummy_window());
266    }
267    if (w->win) {
268      /* and destroy our own window */
269      delwin(w->win);
270      w->win = NULL;
271    }
272  }
273}
274
275void owl_window_show(owl_window *w)
276{
277  w->shown = 1;
278  _owl_window_realize(w);
279  if (w->pan)
280    show_panel(w->pan);
281}
282
283void owl_window_show_all(owl_window *w)
284{
285  owl_window_show(w);
286  owl_window_children_foreach(w, (GFunc)owl_window_show, 0);
287}
288
289void owl_window_hide(owl_window *w)
290{
291  /* you can't unmap the screen */
292  if (w->is_screen)
293    return;
294  w->shown = 0;
295  if (w->pan)
296    hide_panel(w->pan);
297  _owl_window_unrealize(w);
298}
299
300int owl_window_is_shown(owl_window *w)
301{
302  return w->shown;
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->shown
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(w, (GFunc)_owl_window_realize, 0);
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(w, (GFunc)_owl_window_unrealize, 0);
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(w, (GFunc)owl_window_dirty, 0);
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(w, (GFunc)_owl_window_redraw_subtree, 0);
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->shown) {
418    /* Window is shown, 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 shown, we must try to have a window at the end */
457  if (w->shown) {
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(w, (GFunc)owl_window_recompute_position, 0);
463  if (w->shown) {
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.