source: window.c @ f6fae8d

release-1.10release-1.7release-1.8release-1.9
Last change on this file since f6fae8d was 84a4aca, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Add a cursor-tracking mechanism to owl_window
  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[449af72]1#include "owl.h"
2
3#include <assert.h>
4
5struct _owl_window { /*noproto*/
[053f751]6  GObject object;
[449af72]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;
[50031f0]14  int shown : 1;
[449af72]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};
22
[7a6e6c7]23enum {
24  REDRAW,
25  RESIZED,
26  LAST_SIGNAL
27};
28
29static guint window_signals[LAST_SIGNAL] = { 0 };
30
[053f751]31static void owl_window_dispose(GObject *gobject);
32static void owl_window_finalize(GObject *gobject);
33
[449af72]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
[46e2e56]41static void _owl_window_realize(owl_window *w);
42static void _owl_window_unrealize(owl_window *w);
[449af72]43
[7a6e6c7]44static void _owl_window_redraw_cleanup(owl_window *w, WINDOW *win);
45
[84a4aca]46static owl_window *cursor_owner;
47
[053f751]48G_DEFINE_TYPE (OwlWindow, owl_window, G_TYPE_OBJECT)
49
50static void owl_window_class_init (OwlWindowClass *klass)
51{
[7a6e6c7]52  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
53
54  /* Set up the vtabl */
55  gobject_class->dispose = owl_window_dispose;
56  gobject_class->finalize = owl_window_finalize;
57
58  klass->redraw = _owl_window_redraw_cleanup;
59  klass->resized = NULL;
60
61  /* Create the signals, remember IDs */
62  window_signals[REDRAW] =
63    g_signal_new("redraw",
64                 G_TYPE_FROM_CLASS(gobject_class),
65                 G_SIGNAL_RUN_CLEANUP,
66                 G_STRUCT_OFFSET(OwlWindowClass, redraw),
67                 NULL, NULL,
68                 g_cclosure_marshal_VOID__POINTER,
69                 G_TYPE_NONE,
70                 1,
71                 G_TYPE_POINTER, NULL);
72
73  /* TODO: maybe type should be VOID__INT_INT_INT_INT; will need to generate a
74   * marshaller */
75  window_signals[RESIZED] =
76    g_signal_new("resized",
77                 G_TYPE_FROM_CLASS(gobject_class),
78                 G_SIGNAL_RUN_FIRST,
79                 G_STRUCT_OFFSET(OwlWindowClass, resized),
80                 NULL, NULL,
81                 g_cclosure_marshal_VOID__VOID,
82                 G_TYPE_NONE,
83                 0,
84                 NULL);
[053f751]85}
86
87static void owl_window_dispose (GObject *object)
88{
89  owl_window *w = OWL_WINDOW (object);
90
91  /* Unmap the window */
92  owl_window_hide (w);
93
94  /* Unlink and unref all children */
95  while (w->child) {
96    owl_window *child = w->child;
97    owl_window_unlink (child);
98  }
99
100  /* Remove from hierarchy */
101  owl_window_unlink (w);
102
103  G_OBJECT_CLASS (owl_window_parent_class)->dispose (object);
104}
105
106static void owl_window_finalize (GObject *object)
107{
108  owl_window *w = OWL_WINDOW(object);
109
110  if (w->pan) {
111    del_panel(w->pan);
112    w->pan = NULL;
113  }
114
115  G_OBJECT_CLASS (owl_window_parent_class)->finalize (object);
116}
117
118static void owl_window_init (owl_window *w)
119{
120}
121
[449af72]122/** singletons **/
123
124static WINDOW *_dummy_window(void)
125{
126  static WINDOW *dummy = NULL;
127  if (!dummy) {
128    dummy = newwin(1, 1, 0, 0);
129  }
130  return dummy;
131}
132
133owl_window *owl_window_get_screen(void)
134{
135  static owl_window *screen = NULL;
136  if (!screen) {
[50031f0]137    /* The screen is magical. It's 'shown', but the only mean of it going
[449af72]138     * invisible is if we're tore down curses (i.e. screen resize) */
139    screen = _owl_window_new(NULL, g.lines, g.cols, 0, 0);
140    screen->is_screen = 1;
[402ed3d3]141    owl_window_show(screen);
[449af72]142  }
143  return screen;
144}
145
146/** Creation and Destruction **/
147
[d106110]148owl_window *owl_window_new(owl_window *parent)
[449af72]149{
150  if (!parent)
151    parent = owl_window_get_screen();
[d106110]152  return _owl_window_new(parent, 0, 0, 0, 0);
[449af72]153}
154
155static owl_window *_owl_window_new(owl_window *parent, int nlines, int ncols, int begin_y, int begin_x)
156{
157  owl_window *w;
158
[053f751]159  w = g_object_new (OWL_TYPE_WINDOW, NULL);
160  if (w == NULL) g_error("Failed to create owl_window instance");
[449af72]161
162  w->nlines = nlines;
163  w->ncols = ncols;
164  w->begin_y = begin_y;
165  w->begin_x = begin_x;
166
167  _owl_window_link(w, parent);
168  if (parent && parent->is_screen) {
169    w->pan = new_panel(_dummy_window());
170    set_panel_userptr(w->pan, w);
171  }
172
173  return w;
174}
175
176/** Hierarchy **/
177
[3da1f4f]178void owl_window_unlink(owl_window *w)
[449af72]179{
[ec4ccfc]180  /* make sure the window is unmapped first */
[46e2e56]181  _owl_window_unrealize(w);
[449af72]182  /* unlink parent/child information */
183  if (w->parent) {
184    if (w->prev)
185      w->prev->next = w->next;
186    if (w->next)
187      w->next->prev = w->prev;
188    if (w->parent->child == w)
189      w->parent->child = w->next;
[933aa7d]190    w->parent = NULL;
[053f751]191    g_object_unref (w);
[449af72]192  }
193}
194
195static void _owl_window_link(owl_window *w, owl_window *parent)
196{
197  if (w->parent == parent)
198    return;
199
[933aa7d]200  owl_window_unlink(w);
[449af72]201  if (parent) {
[933aa7d]202    w->parent = parent;
[449af72]203    w->next = parent->child;
204    parent->child = w;
[053f751]205    g_object_ref (w);
[449af72]206  }
207}
208
209/* mimic g_list_foreach for consistency */
210void owl_window_children_foreach(owl_window *parent, GFunc func, gpointer user_data)
211{
212  owl_window *w;
213  for (w = parent->child;
214       w != NULL;
215       w = w->next) {
216    func(w, user_data);
217  }
218}
219
[b6cb985]220owl_window *owl_window_get_parent(owl_window *w)
221{
222  return w->parent;
223}
224
[449af72]225/** Internal window management **/
226
227static void _owl_window_create_curses(owl_window *w)
228{
229  if (w->is_screen) {
230    resizeterm(w->nlines, w->ncols);
[2dfccc7]231    w->win = stdscr;
[449af72]232  } else if (w->pan) {
233    w->win = newwin(w->nlines, w->ncols, w->begin_y, w->begin_x);
234    replace_panel(w->pan, w->win);
235  } else {
[fe49685]236    if (w->parent == NULL || w->parent->win == NULL)
237      return;
[449af72]238    w->win = derwin(w->parent->win, w->nlines, w->ncols, w->begin_y, w->begin_x);
239  }
240}
241
242static void _owl_window_destroy_curses(owl_window *w)
243{
244  if (w->is_screen) {
245    /* don't deallocate the dummy */
246    w->win = NULL;
247  } else {
248    if (w->pan) {
249      /* panels assume their windows always exist, so we put in a fake one */
250      replace_panel(w->pan, _dummy_window());
251    }
252    if (w->win) {
253      /* and destroy our own window */
254      delwin(w->win);
255      w->win = NULL;
256    }
257  }
258}
259
[402ed3d3]260void owl_window_show(owl_window *w)
[449af72]261{
[50031f0]262  w->shown = 1;
[46e2e56]263  _owl_window_realize(w);
[449af72]264  if (w->pan)
265    show_panel(w->pan);
[402ed3d3]266}
267
268void owl_window_show_all(owl_window *w)
269{
270  owl_window_show(w);
[7a70e26]271  owl_window_children_foreach(w, (GFunc)owl_window_show, 0);
[449af72]272}
273
[50031f0]274void owl_window_hide(owl_window *w)
[449af72]275{
276  /* you can't unmap the screen */
277  if (w->is_screen)
278    return;
[50031f0]279  w->shown = 0;
[449af72]280  if (w->pan)
281    hide_panel(w->pan);
[46e2e56]282  _owl_window_unrealize(w);
[449af72]283}
284
[50031f0]285int owl_window_is_shown(owl_window *w)
[449af72]286{
[50031f0]287  return w->shown;
[449af72]288}
289
[ce7c6c3]290int owl_window_is_realized(owl_window *w)
[449af72]291{
292  return w->win != NULL;
293}
294
295int owl_window_is_toplevel(owl_window *w)
296{
297  return w->pan != NULL;
298}
299
[46e2e56]300static void _owl_window_realize(owl_window *w)
[449af72]301{
302  /* check if we can create a window */
303  if ((w->parent && w->parent->win == NULL)
[50031f0]304      || !w->shown
[449af72]305      || w->win != NULL)
306    return;
307  _owl_window_create_curses(w);
[fe49685]308  if (w->win == NULL)
309    return;
[449af72]310  /* schedule a repaint */
311  owl_window_dirty(w);
[fe49685]312  /* map the children */
[7a70e26]313  owl_window_children_foreach(w, (GFunc)_owl_window_realize, 0);
[449af72]314}
315
[46e2e56]316static void _owl_window_unrealize(owl_window *w)
[449af72]317{
318  if (w->win == NULL)
319    return;
320  /* unmap all the children */
[7a70e26]321  owl_window_children_foreach(w, (GFunc)_owl_window_unrealize, 0);
[449af72]322  _owl_window_destroy_curses(w);
[cb5a9f3]323  w->dirty = w->dirty_subtree = 0;
[449af72]324  /* subwins leave a mess in the parent; dirty it */
325  if (w->parent)
326    owl_window_dirty(w->parent);
327}
328
329/** Painting and book-keeping **/
330
[84a4aca]331void owl_window_set_cursor(owl_window *w)
332{
333  cursor_owner = w;
334  g_object_add_weak_pointer(G_OBJECT(w), (gpointer*) &cursor_owner);
335}
336
[449af72]337void owl_window_dirty(owl_window *w)
338{
339  /* don't put the screen on this list; pointless */
340  if (w->is_screen)
341    return;
[7cbef8c]342  if (!owl_window_is_realized(w))
343    return;
[449af72]344  if (!w->dirty) {
345    w->dirty = 1;
346    while (w && !w->dirty_subtree) {
347      w->dirty_subtree = 1;
348      w = w->parent;
349    }
350    owl_global_set_needrefresh(&g);
351  }
352}
353
354void owl_window_dirty_children(owl_window *w)
355{
[7a70e26]356  owl_window_children_foreach(w, (GFunc)owl_window_dirty, 0);
[449af72]357}
358
359static void _owl_window_redraw(owl_window *w)
360{
361  if (!w->dirty) return;
[7a6e6c7]362  if (w->win) {
363    g_signal_emit(w, window_signals[REDRAW], 0, w->win);
[449af72]364  }
365  w->dirty = 0;
366}
367
368static void _owl_window_redraw_subtree(owl_window *w)
369{
370  if (!w->dirty_subtree)
371    return;
372  _owl_window_redraw(w);
[7a70e26]373  owl_window_children_foreach(w, (GFunc)_owl_window_redraw_subtree, 0);
[449af72]374}
375
376/*
377Redraw all the windows with scheduled redraws.
378NOTE: This function shouldn't be called outside the event loop
379*/
380void owl_window_redraw_scheduled(void)
381{
382  _owl_window_redraw_subtree(owl_window_get_screen());
[a57f87a]383  update_panels();
[84a4aca]384  if (cursor_owner && cursor_owner->win)
385    wnoutrefresh(cursor_owner->win);
[449af72]386}
387
[7a6e6c7]388static void _owl_window_redraw_cleanup(owl_window *w, WINDOW *win)
389{
390  wsyncup(win);
391}
392
[7c8811c]393void owl_window_erase_cb(owl_window *w, WINDOW *win, void *user_data)
394{
395  werase(win);
396  owl_window_dirty_children(w);
397}
398
[449af72]399/** Window position **/
400
401void owl_window_get_position(owl_window *w, int *nlines, int *ncols, int *begin_y, int *begin_x)
402{
403  if (nlines)  *nlines  = w->nlines;
404  if (ncols)   *ncols   = w->ncols;
405  if (begin_y) *begin_y = w->begin_y;
406  if (begin_x) *begin_x = w->begin_x;
407}
408
[b0cbde4]409void owl_window_move(owl_window *w, int begin_y, int begin_x)
410{
[6b93305]411  if (w->is_screen) return; /* can't move the screen */
412  if (w->begin_y == begin_y && w->begin_x == begin_x) return;
[b0cbde4]413
414  w->begin_y = begin_y;
415  w->begin_x = begin_x;
[50031f0]416  if (w->shown) {
417    /* Window is shown, we must try to have a window at the end */
[b0cbde4]418    if (w->win) {
419      /* We actually do have a window; let's move it */
420      if (w->pan) {
421        if (move_panel(w->pan, begin_y, begin_x) == OK)
422          return;
423      } else {
424        if (mvderwin(w->win, begin_y, begin_x) == OK) {
425          /* now both we and the parent are dirty */
426          owl_window_dirty(w->parent);
427          owl_window_dirty(w);
428          return;
429        }
430      }
431    }
432    /* We don't have a window or failed to move it. Fine. Brute force. */
[46e2e56]433    _owl_window_unrealize(w);
434    _owl_window_realize(w);
[b0cbde4]435  }
436}
437
[449af72]438void owl_window_set_position(owl_window *w, int nlines, int ncols, int begin_y, int begin_x)
439{
440  /* can't move the screen */
441  if (w->is_screen) {
442    begin_y = begin_x = 0;
443  }
444
445  if (w->nlines == nlines && w->ncols == ncols) {
446    /* moving is easier */
447    owl_window_move(w, begin_y, begin_x);
448    return;
449  }
450
451  w->begin_y = begin_y;
452  w->begin_x = begin_x;
453  w->nlines = nlines;
454  w->ncols = ncols;
[50031f0]455  /* window is shown, we must try to have a window at the end */
456  if (w->shown) {
[46e2e56]457    /* resizing in ncurses is hard: give up do a unrealize/realize */
458    _owl_window_unrealize(w);
[449af72]459  }
[7a6e6c7]460  g_signal_emit(w, window_signals[RESIZED], 0);
[50031f0]461  if (w->shown) {
[46e2e56]462    _owl_window_realize(w);
[449af72]463  }
464}
465
466void owl_window_resize(owl_window *w, int nlines, int ncols)
467{
468  owl_window_set_position(w, nlines, ncols, w->begin_y, w->begin_x);
469}
470
471/** Stacking order **/
472
473void owl_window_top(owl_window *w) {
474  if (!w->pan) {
[7c8811c]475    owl_function_debugmsg("Warning: owl_window_top only makes sense on top-level window");
[449af72]476    return;
477  }
478  top_panel(w->pan);
479}
480
481owl_window *owl_window_above(owl_window *w) {
482  PANEL *pan;
483
484  if (!w->pan) {
485    owl_function_debugmsg("Warning: owl_window_above only makes sense on top-level window");
486    return NULL;
487  }
488  pan = panel_above(w->pan);
489  /* cast because panel_userptr pointlessly returns a const void * */
490  return pan ? (void*) panel_userptr(pan) : NULL;
491}
492
493owl_window *owl_window_below(owl_window *w) {
494  PANEL *pan;
495
496  if (!w->pan) {
497    owl_function_debugmsg("Warning: owl_window_above only makes sense on top-level window");
498    return NULL;
499  }
500  pan = panel_below(w->pan);
501  /* cast because panel_userptr pointlessly returns a const void * */
502  return pan ? (void*) panel_userptr(pan) : NULL;
503}
Note: See TracBrowser for help on using the repository browser.