[Lunar-commits] <lconnman> lconnman: move event handling out of the choice_window, to enable multiple window key handling

Samuel samuel.verstraete at gmail.com
Sun Aug 28 13:34:23 CEST 2011


commit 08cb6f90affad78901835a85fe5c70fc30f65f9d
Author: Samuel <samuel.verstraete at gmail.com>
Date:   Sun Aug 28 13:34:23 2011 +0200

    lconnman: move event handling out of the choice_window, to enable
    multiple window key handling
---
 src/gui/choice_window.vala |   49 ++++++++++++++-----------------------------
 src/lconnman.vala          |   38 +++++++++++++++++++++++++++++----
 2 files changed, 49 insertions(+), 38 deletions(-)

diff --git a/src/gui/choice_window.vala b/src/gui/choice_window.vala
index 78b42cf..9cc8bed 100644
--- a/src/gui/choice_window.vala
+++ b/src/gui/choice_window.vala
@@ -12,7 +12,6 @@ public class ChoiceWindow {
   private int c;
   private Window window;
 
-
   public ChoiceWindow(string[] choices, int width, int height) {
     this.Width = width;
     this.Height = height;
@@ -25,42 +24,26 @@ public class ChoiceWindow {
     this.window = new Window (Height, Width, starty, startx);
     this.window.bkgdset (COLOR_PAIR(1));
     this.window.clrtobot();
-    this.window.keypad (true); //enable the up and down movement;
-
-    //this will not work, need to figure out a way, maybe create a new window?
-    //mvprintw(0,0, "Use arrow keys to go up and down, Press enter to select a choice");
+    this.window.box(0,0);
+    this.print_choice_menu();
+  }
 
+  public void previous_choice(){
+    if (this.highlight == 1) {
+      this.highlight = this.Choices.length;
+    } else {
+      --this.highlight;
+    }
     this.print_choice_menu();
-    this.window.box(0,0);
+  }
 
-    while(true){
-      c = this.window.getch();
-      switch(c) {
-        case Key.UP:
-          if (this.highlight == 1){
-            this.highlight = this.Choices.length;
-          } else {
-            --this.highlight;
-          }
-          break;
-        case Key.DOWN:
-          if (this.highlight == this.Choices.length) {
-            this.highlight = 1;
-          } else {
-            ++this.highlight;
-          }
-          break;
-        case 10:
-          this.choice = this.highlight;
-          break;
-        default:
-          break;
-      }
-      this.print_choice_menu();
-      if (this.choice != 0) //user pushed enter, quit infinite loop
-        break;
+  public void next_choice(){
+    if (this.highlight == this.Choices.length) {
+      this.highlight = 1;
+    } else {
+      ++this.highlight;
     }
-    this.window.refresh();
+    this.print_choice_menu();
   }
 
   private void print_choice_menu() {
diff --git a/src/lconnman.vala b/src/lconnman.vala
index effe0d8..0f077a4 100644
--- a/src/lconnman.vala
+++ b/src/lconnman.vala
@@ -5,13 +5,44 @@ int main() {
   noecho();
   cbreak();
   curs_set(0);
-
   start_color();
   init_pair((short)1, Color.WHITE, Color.BLUE);
+  init_pair((short)2, Color.WHITE, Color.GREEN);
+
+  //bkgdset(COLOR_PAIR(2));
+  //clrtobot();
+  //create a window to pull in events, this doesn't seem to work on the stdscr.
+  var d = new Window(1,1,0,0);
+  //enable up and down keys
+  d.keypad(true);
+  //print instructions at the bottom of the screen
+  mvprintw(LINES-1,1,"%s", "Push the 'q' key to quit");
+  refresh();
 
   string[] services =  get_services();
   ChoiceWindow cw = new ChoiceWindow(services, COLS, (int)(0.8 * LINES));
 
+  string action = "";
+  int c;
+  while(true){
+    c = d.getch();
+    switch(c) {
+      case Key.UP:
+        cw.previous_choice();
+        break;
+      case Key.DOWN:
+        cw.next_choice();
+        break;
+      case 113: //should be q;
+        action = "quit";
+        break;
+      default:
+        break;
+    }
+    if (action == "quit") {
+      break;
+    }
+  }
   endwin();
   return 0;
 }
@@ -19,7 +50,7 @@ int main() {
 private static string[] get_services(){
   ConnmanManager manager = new ConnmanManager();
   Service[] services = manager.get_services();
-  string[] str_services = new string[services.length + 1];
+  string[] str_services = new string[services.length];
   int number_services = 0;
 
   for( int i = 0; i < services.length; i++ ) {
@@ -31,8 +62,5 @@ private static string[] get_services(){
 
     str_services[i] = service_type.print(false) + "." + service_name.print(false);
   }
-
-  str_services[services.length] = "Exit";
-
   return str_services;
 }


More information about the Lunar-commits mailing list