[Lunar-commits] <lconnman> lconnman

Samuel Verstraete samuel.verstraete at gmail.com
Thu Sep 8 04:13:34 CEST 2011


commit 30d2a1b92ad9e0e735f33edd4fc8982e52dfd18f
Author: Samuel Verstraete <samuel.verstraete at gmail.com>
Date:   Thu Sep 8 04:13:34 2011 +0200

    lconnman
---
 src/Makefile               |    2 +-
 src/gui/choice_window.vala |   66 ---------------------------
 src/gui/curseslist.vala    |  106 ++++++++++++++++++++++++++++++++++++++++++++
 src/gui/enums.vala         |   11 +++++
 src/lconnman.vala          |   26 ++++++++---
 5 files changed, 137 insertions(+), 74 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index 81d728e..7e705da 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,5 +1,5 @@
 VALAFILES=connmanmanager.vala lconnman.vala
-VALAFILES+=gui/choice_window.vala
+VALAFILES+=gui/curseslist.vala gui/enums.vala
 #PFX=/home/pancake/prg/racu/tmp
 #CFLAGS=-I${PFX}/include/ncurses
 LDFLAGS=-Wl -ldl -lncurses
diff --git a/src/gui/choice_window.vala b/src/gui/choice_window.vala
deleted file mode 100644
index 9cc8bed..0000000
--- a/src/gui/choice_window.vala
+++ /dev/null
@@ -1,66 +0,0 @@
-using Curses;
-
-public class ChoiceWindow {
-  //construction information
-  private int Width;
-  private int Height;
-  private string[] Choices;
-
-  //internal affairs
-  private int highlight = 1;
-  private int choice = 0;
-  private int c;
-  private Window window;
-
-  public ChoiceWindow(string[] choices, int width, int height) {
-    this.Width = width;
-    this.Height = height;
-    this.Choices = choices;
-
-    //put the window in the middle of the screen;
-    int startx = (COLS - Width) / 2;
-    int starty = (LINES - Height) / 2;
-
-    this.window = new Window (Height, Width, starty, startx);
-    this.window.bkgdset (COLOR_PAIR(1));
-    this.window.clrtobot();
-    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();
-  }
-
-  public void next_choice(){
-    if (this.highlight == this.Choices.length) {
-      this.highlight = 1;
-    } else {
-      ++this.highlight;
-    }
-    this.print_choice_menu();
-  }
-
-  private void print_choice_menu() {
-    int x,y,i;
-
-    x = 2;
-    y = 2;
-    for (i = 0; i < this.Choices.length; i++) {
-      if (this.highlight == i + 1) { //if this is the currently highlighted item, inverse colors;
-        this.window.attron(Attribute.REVERSE);
-        this.window.mvprintw(y, x, "%s", this.Choices[i]);
-        this.window.attroff(Attribute.REVERSE);
-      } else {
-        this.window.mvprintw(y, x, "%s", this.Choices[i]);
-      }
-      y++;
-    }
-    this.window.refresh();
-  }
-}
diff --git a/src/gui/curseslist.vala b/src/gui/curseslist.vala
new file mode 100644
index 0000000..360250a
--- /dev/null
+++ b/src/gui/curseslist.vala
@@ -0,0 +1,106 @@
+using Curses;
+
+public class CursesList {
+  //construction information
+  private int Width;
+  private int Height;
+  private string[] Choices;
+  private Orientation Orientation;
+  private Alignment Alignment;
+
+  //internal affairs
+  private int highlight = 1;
+  private Window window;
+
+  public CursesList(string[] choices, Orientation orientation, Alignment alignment, int width, int height) {
+    this.Width = width;
+    this.Height = height;
+    this.Choices = choices;
+    this.Orientation = orientation;
+    this.Alignment = alignment;
+
+    int startx, starty;
+    switch (this.Alignment) {
+      case Alignment.Top:
+        startx = 0;
+        starty = 0;
+        break;
+      case Alignment.Bottom:
+        startx = 0;
+        starty = LINES - this.Height;
+        break;
+      case Alignment.Left:
+        startx = 0;
+        starty = 0;
+        break;
+      case Alignment.Right:
+        startx = COLS - this.Width;
+        starty = 0;
+        break;
+      default:
+        startx = 0; starty = 0;
+        break;
+    }
+
+    this.window = new Window (Height, Width, starty, startx);
+    this.window.bkgdset (COLOR_PAIR(1));
+    this.window.clrtobot();
+    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();
+  }
+
+  public void next_choice(){
+    if (this.highlight == this.Choices.length) {
+      this.highlight = 1;
+    } else {
+      ++this.highlight;
+    }
+    this.print_choice_menu();
+  }
+
+  private void print_choice_menu() {
+    int x,y,i;
+
+    x = 2;
+    y = 2;
+
+    if (this.Orientation == Orientation.Vertical) {
+      for (i = 0; i < this.Choices.length; i++) {
+        if (this.highlight == i + 1) { //if this is the currently highlighted item, inverse colors;
+          this.window.attron(Attribute.REVERSE);
+          this.window.mvprintw(y, x, "%s", this.Choices[i]);
+          this.window.attroff(Attribute.REVERSE);
+        } else {
+          this.window.mvprintw(y, x, "%s", this.Choices[i]);
+        }
+        y++;
+      }
+    }
+    else { /* orientation is horizontal */
+      for (i = 0; i < this.Choices.length; i++) {
+        if (this.highlight == i + 1) {
+	        this.window.attron(Attribute.REVERSE);
+          this.window.mvprintw(y, x, " < %s > ", this.Choices[i]);
+          this.window.attroff(Attribute.REVERSE);
+          } else {
+            this.window.mvprintw(y, x, " < %s > ", this.Choices[i]);
+          }
+          x = x + this.Choices[i].length + 10;
+        }
+      }
+    this.window.refresh();
+  }
+
+  public string get_selected_choice() {
+    return this.Choices[this.highlight - 1];
+  }
+}
diff --git a/src/gui/enums.vala b/src/gui/enums.vala
new file mode 100644
index 0000000..5b8728b
--- /dev/null
+++ b/src/gui/enums.vala
@@ -0,0 +1,11 @@
+public enum Orientation {
+  Vertical,
+  Horizontal
+}
+
+public enum Alignment {
+  Top,
+  Bottom,
+  Left,
+  Right
+}
diff --git a/src/lconnman.vala b/src/lconnman.vala
index 0f077a4..7324a4a 100644
--- a/src/lconnman.vala
+++ b/src/lconnman.vala
@@ -9,8 +9,6 @@ int main() {
   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
@@ -20,11 +18,16 @@ int main() {
   refresh();
 
   string[] services =  get_services();
-  ChoiceWindow cw = new ChoiceWindow(services, COLS, (int)(0.8 * LINES));
+  CursesList cw = new CursesList(services, Orientation.Vertical, Alignment.Top, COLS, LINES - 4);
+  string[] buttons = new string[3];
+  buttons[0] = "configure";
+  buttons[1] = "connect";
+  buttons[2] = "exit";
+  CursesList buttonBox = new CursesList(buttons, Orientation.Horizontal, Alignment.Bottom, COLS, 5);
 
   string action = "";
   int c;
-  while(true){
+  while(action != "quit"){
     c = d.getch();
     switch(c) {
       case Key.UP:
@@ -33,15 +36,23 @@ int main() {
       case Key.DOWN:
         cw.next_choice();
         break;
+      case Key.LEFT:
+        buttonBox.previous_choice();
+        break;
+      case Key.RIGHT:
+        buttonBox.next_choice();
+        break;
       case 113: //should be q;
         action = "quit";
         break;
+      case Key.ENTER: //not working?
+        if (buttonBox.get_selected_choice() == "exit") {
+          action = "quit";
+        }
+        break;
       default:
         break;
     }
-    if (action == "quit") {
-      break;
-    }
   }
   endwin();
   return 0;
@@ -64,3 +75,4 @@ private static string[] get_services(){
   }
   return str_services;
 }
+


More information about the Lunar-commits mailing list