about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-09-26 00:25:04 +0100
committerJames Booth <boothj5@gmail.com>2013-09-26 00:25:04 +0100
commit2625630ff367eeab566065e92258d4724bfeefed (patch)
tree1831207eca3dd0f3a837b0414d1ed1aef83b1863
parent8be4cd1f2731ebec069c5848148b6748f318ea98 (diff)
downloadprofani-tty-2625630ff367eeab566065e92258d4724bfeefed.tar.gz
Next and previous windows, handle KEY_CODE_YES terms, jump empty wins
-rw-r--r--src/ui/core.c62
-rw-r--r--src/ui/inputwin.c28
-rw-r--r--src/ui/ui.h2
-rw-r--r--src/ui/windows.c53
-rw-r--r--src/ui/windows.h2
5 files changed, 131 insertions, 16 deletions
diff --git a/src/ui/core.c b/src/ui/core.c
index 6b415a2b..86d216ad 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -616,6 +616,68 @@ ui_switch_win(const int i)
 }
 
 void
+ui_next_win(void)
+{
+    ui_current_page_off();
+    ProfWin *new_current = wins_get_next();
+    int i = wins_get_num(new_current);
+    wins_set_current_by_num(i);
+    ui_current_page_off();
+
+    new_current->unread = 0;
+
+    if (i == 1) {
+        title_bar_title();
+        status_bar_active(1);
+    } else {
+        PContact contact = roster_get_contact(new_current->from);
+        if (contact != NULL) {
+            if (p_contact_name(contact) != NULL) {
+                title_bar_set_recipient(p_contact_name(contact));
+            } else {
+                title_bar_set_recipient(new_current->from);
+            }
+        } else {
+            title_bar_set_recipient(new_current->from);
+        }
+        title_bar_draw();;
+        status_bar_active(i);
+    }
+    wins_refresh_current();
+}
+
+void
+ui_previous_win(void)
+{
+    ui_current_page_off();
+    ProfWin *new_current = wins_get_previous();
+    int i = wins_get_num(new_current);
+    wins_set_current_by_num(i);
+    ui_current_page_off();
+
+    new_current->unread = 0;
+
+    if (i == 1) {
+        title_bar_title();
+        status_bar_active(1);
+    } else {
+        PContact contact = roster_get_contact(new_current->from);
+        if (contact != NULL) {
+            if (p_contact_name(contact) != NULL) {
+                title_bar_set_recipient(p_contact_name(contact));
+            } else {
+                title_bar_set_recipient(new_current->from);
+            }
+        } else {
+            title_bar_set_recipient(new_current->from);
+        }
+        title_bar_draw();;
+        status_bar_active(i);
+    }
+    wins_refresh_current();
+}
+
+void
 ui_clear_current(void)
 {
     wins_clear_current();
diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c
index dc8e994a..dfcb533d 100644
--- a/src/ui/inputwin.c
+++ b/src/ui/inputwin.c
@@ -360,6 +360,16 @@ _handle_edit(int result, const wint_t ch, char *input, int *size)
 
         return 1;
 
+    // ALT-LEFT
+    } else if ((result == KEY_CODE_YES) && (ch == 537)) {
+        ui_previous_win();
+        return 1;
+
+    // ALT-RIGHT
+    } else if ((result == KEY_CODE_YES) && (ch == 552)) {
+        ui_next_win();
+        return 1;
+
     // other editing keys
     } else {
         switch(ch) {
@@ -560,8 +570,6 @@ _handle_alt_key(char *input, int *size, int key)
 {
     int end_del = getcurx(inp_win);
     int start_del = end_del;
-    int current = wins_get_current_num();
-    int new;
 
     switch (key)
     {
@@ -596,22 +604,10 @@ _handle_alt_key(char *input, int *size, int key)
             ui_switch_win(0);
             break;
         case KEY_LEFT:
-            if (current == 0) {
-                new = 9;
-            } else {
-                new = current - 1;
-            }
-
-            ui_switch_win(new);
+            ui_previous_win();
             break;
         case KEY_RIGHT:
-            if (current == 9) {
-                new = 0;
-            } else {
-                new = current + 1;
-            }
-
-            ui_switch_win(new);
+            ui_next_win();
             break;
         case 263:
         case 127:
diff --git a/src/ui/ui.h b/src/ui/ui.h
index fa74bed6..4cde751e 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -52,6 +52,8 @@ void ui_idle(void);
 void ui_handle_special_keys(const wint_t * const ch, const char * const inp,
     const int size);
 void ui_switch_win(const int i);
+void ui_next_win(void);
+void ui_previous_win(void);
 unsigned long ui_get_idle_time(void);
 void ui_reset_idle_time(void);
 void ui_new_chat_win(const char * const to);
diff --git a/src/ui/windows.c b/src/ui/windows.c
index e69b2433..684e98fd 100644
--- a/src/ui/windows.c
+++ b/src/ui/windows.c
@@ -91,6 +91,59 @@ wins_get_by_num(int i)
 }
 
 ProfWin *
+wins_get_next(void)
+{
+    // get and sort win nums
+    GList *keys = g_hash_table_get_keys(windows);
+    keys = g_list_sort(keys, cmp_win_num);
+    GList *curr = keys;
+
+    // find our place in the list
+    while (curr != NULL) {
+        if (current == GPOINTER_TO_INT(curr->data)) {
+            break;
+        }
+        curr = g_list_next(curr);
+    }
+
+    // if there is a next window return it
+    curr = g_list_next(curr);
+    if (curr != NULL) {
+        return wins_get_by_num(GPOINTER_TO_INT(curr->data));
+    // otherwise return the first window (console)
+    } else {
+        return wins_get_console();
+    }
+}
+
+ProfWin *
+wins_get_previous(void)
+{
+    // get and sort win nums
+    GList *keys = g_hash_table_get_keys(windows);
+    keys = g_list_sort(keys, cmp_win_num);
+    GList *curr = keys;
+
+    // find our place in the list
+    while (curr != NULL) {
+        if (current == GPOINTER_TO_INT(curr->data)) {
+            break;
+        }
+        curr = g_list_next(curr);
+    }
+
+    // if there is a previous window return it
+    curr = g_list_previous(curr);
+    if (curr != NULL) {
+        return wins_get_by_num(GPOINTER_TO_INT(curr->data));
+    // otherwise return the last window
+    } else {
+        int new_num = GPOINTER_TO_INT(g_list_last(keys)->data);
+        return wins_get_by_num(new_num);
+    }
+}
+
+ProfWin *
 wins_get_by_recipient(const char * const recipient)
 {
     GList *values = g_hash_table_get_values(windows);
diff --git a/src/ui/windows.h b/src/ui/windows.h
index 6d93c434..3cf593e8 100644
--- a/src/ui/windows.h
+++ b/src/ui/windows.h
@@ -28,6 +28,8 @@ ProfWin * wins_get_console(void);
 ProfWin * wins_get_current(void);
 void wins_set_current_by_num(int i);
 ProfWin * wins_get_by_num(int i);
+ProfWin * wins_get_next(void);
+ProfWin * wins_get_previous(void);
 ProfWin * wins_get_by_recipient(const char * const recipient);
 int wins_get_num(ProfWin *window);
 int wins_get_current_num(void);