Commits

Anonymous committed 8b75359

A few upadtes to make it work

Comments (0)

Files changed (2)

-extern mod tb(name="termbox");
+extern mod tb(name="termbox", vers = "0.1.1");
+extern mod gui(name="gui", vers = "0.1");
 
 // =======================================================
 fn main() {
+
+  // Ejemplo 1
   unsafe {
     tb::init();
-    tb::print(1, 1, tb::Bold, tb::White, tb::Black, ~"Hello, world!"); 
+    let size:tb::Size = tb::get_size();
+    let frame1 = gui::Frame{x:0,y:0,w:10,h:size.h, active:true};
+    let frame2 = gui::Frame{x:10,y:0,w:size.w-10,h:size.h, active:false};
+    frame1.gen_border();
+    frame2.gen_border();
+    // Menu
+    let file = gui::SubMenu(~[(~"Open",gui::donothing,0),
+                              (~"Close",gui::donothing,0)]);
+    let menu = gui::Menu(~[file]);
+
+    
+    gui::print(1, 1, tb::BU_White, tb::Black, ~"Hello, world!"); 
     tb::present(); 
-
+    //io::println(fmt!("Size: %? %?", size.w,size.h));
     loop {
        let tmp = tb::poll_event();
-/*
-        match tmp {
-            tb::KeyEvent{md:_,key:_,ch:_ } => break,
-          _            => ()
-        } 
-*/
 
        match tmp {
-/*            
-            tb::KeyEvent(_ , _ , 113) => break,
-            tb::KeyEvent(md, key, ch ) => io::println(fmt!("KEY: %? %? %?",md, key, ch) ),
-*/         
-           tb::KeyEvent{md:_ , key:_ , ch:113} => break,
-           tb::KeyEvent{md, key, ch }          => io::println(fmt!("KEY: %? %? %?",md, key, ch) ),
-           _                                   => ()
+           tb::KeyEvent(tb::Key{md:_ , key:_ , ch:113}) => break,
+           tb::KeyEvent(tb::Key{md, key, ch })          => io::println(fmt!("KEY: %? %? %?",md, key, ch) ),
+           tb::ResizeEvent(tb::Size{w, h}) => io::println(fmt!("Resize: w:%?  h:%?", w, h)),
+           _                                            => ()
         }
     }
 
     tb::shutdown();
   }
+
+  // Ejemplo 2
+  unsafe {
+
+  }
+    io::println(fmt!("%? %? %? %?", 0x07,0x07 as i16,0x10 as i16, 0x07 as i16 | 0x10 as i16 ))
 }
-#[link(name = "termbox", vers = "0.1.0", author = "josemaria.alkala")];
+#[link(name = "termbox", vers = "0.1.1", author = "josemaria.alkala")];
 #[crate_type = "lib"];
-//pub mod tb {
 extern mod std;
 
+// FFI to termbox
 use core::libc::{c_uint,c_int};
-
+#[link_name="termbox"]  // by default links like: -ltermbox
+//#[link_args="termbox"] // If another linking were necessary
 extern mod ffi {
-    // Declaramos la API del módulo externo
     fn tb_init() -> c_int;
     fn tb_shutdown();
 
     fn tb_select_input_mode(mode: c_int) -> c_int;
     fn tb_set_clear_attributes(fg: u16, bg: u16);
 
-    //fn tb_peek_event(ev: *mut tb_event, timeout: c_uint) -> c_int;
-    fn tb_peek_event(ev: *tb_event, timeout: c_uint) -> c_int;
-    fn tb_poll_event(ev: *tb_event) -> c_int;
+    fn tb_peek_event(ev: *mut tb_event, timeout: c_uint) -> c_int;
+    fn tb_poll_event(ev: *mut tb_event) -> c_int;
 }
 
-///////// to_mut_unsafe_ptr(&mut ev)
-// en lugar de: fn peek_event
-// Definimos las elementos que se van a definir 
-
 struct tb_event {
   t:u8, // type
   m:u8, // mod
 }
 
 
-// Comodity functions ("Rusty" interface)
-// - no conviene meterno dentro de bloques unsafe, para que sea 
-// más explícito para quien use la librería
+// Convenience functions
 pub enum Color {
-     Black   = 0x00,
-     Red     = 0x01,
-     Green   = 0x02,
-     Yellow  = 0x03,
-     Blue    = 0x04,
-     Magenta = 0x05,
-     Cyan    = 0x06,
-     White   = 0x07
+     Black   = 0, //0x00,
+     Red     = 1, //0x01,
+     Green   = 2, //0x02,
+     Yellow  = 3, //0x03,
+     Blue    = 4, //0x04,
+     Magenta = 5, //0x05,
+     Cyan    = 6, //0x06,
+     White   = 7, //0x07,
+
+     B_Black   = 16, //0x00 as u16 | 0x10 as u16,
+     B_Red     = 17, //0x01 as u16 | 0x10 as u16,
+     B_Green   = 18, //0x02 as u16 | 0x10 as u16,
+     B_Yellow  = 19, //0x03 as u16 | 0x10 as u16,
+     B_Blue    = 20, //0x04 as u16 | 0x10 as u16,
+     B_Magenta = 21, //0x05 as u16 | 0x10 as u16,
+     B_Cyan    = 22, //0x06 as u16 | 0x10 as u16,
+     B_White   = 23, //0x07 as u16 | 0x10 as u16,
+
+     U_Black   = 32, //0x00 as u16 | 0x20 as u16,
+     U_Red     = 33, //0x01 as u16 | 0x20 as u16,
+     U_Green   = 34, //0x02 as u16 | 0x20 as u16,
+     U_Yellow  = 35, //0x03 as u16 | 0x20 as u16,
+     U_Blue    = 36, //0x04 as u16 | 0x20 as u16,
+     U_Magenta = 37, //0x05 as u16 | 0x20 as u16,
+     U_Cyan    = 38, //0x06 as u16 | 0x20 as u16,
+     U_White   = 39, //0x07 as u16 | 0x20 as u16,
+
+     BU_Black   = 48, //0x00 as u16 | 0x30 as u16,
+     BU_Red     = 49, //0x01 as u16 | 0x20 as u16,
+     BU_Green   = 50, //0x02 as u16 | 0x20 as u16,
+     BU_Yellow  = 51, //0x03 as u16 | 0x20 as u16,
+     BU_Blue    = 52, //0x04 as u16 | 0x20 as u16,
+     BU_Magenta = 53, //0x05 as u16 | 0x20 as u16,
+     BU_Cyan    = 54, //0x06 as u16 | 0x20 as u16,
+     BU_White   = 55 //0x07 as u16 | 0x20 as u16,
 }
-
+/*
 pub enum Style {
      Normal        = 0x00,
      Bold          = 0x10,
      Underline     = 0x20,
      BoldUnderline = 0x30
 }
+*/
 
 pub unsafe fn init() -> int { 
   ffi::tb_init() as int
 
 
 // low-level wrapper
-unsafe fn change_cell(x: uint, y: uint, ch: u32, fg: Color, bg: Color) { 
-  ffi::tb_change_cell(x as c_uint, y as c_uint, ch, fg as u16, bg as u16); 
+pub unsafe fn change_cell(x: uint, y: uint, ch: u32, fg: Color, bg: Color) { 
+  ffi::tb_change_cell(x as c_uint, y as c_uint, ch, fg as u16, bg as u16)
 }
 
-/**
- * Print a string to the buffer.  Leftmost charater is at (x, y).
- */
-pub unsafe fn print(x: uint, y: uint, sty: Style, fg: Color, bg: Color, s: ~str) {
-    let fg: u16 = (fg as u16) | (sty as u16);
-    let bg = bg as u16;
-    for s.each_chari |i, ch| {
-        ffi::tb_change_cell((x + i) as c_uint, y as c_uint, ch as u32, fg, bg);
-    }
+pub struct Size {
+   w:uint,
+   h:uint
 }
 
-/**
- * Print a charater to the buffer.
- */
-unsafe fn print_ch(x: uint, y: uint, sty: Style, fg: Color, bg: Color, ch: char) {
-    let fg: u16 = fg as u16 | sty as u16;
-    let bg: u16 = bg as u16;
-    ffi::tb_change_cell(x as c_uint, y as c_uint, ch as u32, fg, bg);
+pub struct Position {  // Top left
+   x:uint,
+   y:uint
 }
 
+pub struct Key {
+  md: u8, 
+  key: u16, 
+  ch: u32
+}  
+
 pub enum Event {
-    pub KeyEvent{md: u8, key: u16, ch: u32},  
-    pub ResizeEvent{w: i32, h: i32},
+    KeyEvent(Key), //{md: u8, key: u16, ch: u32},  
+    ResizeEvent(Size), //{w: i32, h: i32},
     NoEvent,
     NotManagedEvent
 }
 
+pub unsafe fn get_size() -> Size {
+  let width =  ffi::tb_width() as uint;
+  let height =  ffi::tb_height() as uint;
+  Size{w:width,h:height}
+}
+
 fn unpack_event(ev_type: c_int, ev: &tb_event) -> Event {
     match ev_type {
         0 => NoEvent,
-        1 => KeyEvent{md: ev.m, key: ev.key, ch: ev.ch},
-        2 => ResizeEvent{w: ev.w, h: ev.h},
+        1 => KeyEvent(Key{md: ev.m, key: ev.key, ch: ev.ch}),
+        2 => ResizeEvent(Size{w: ev.w as uint, h: ev.h as uint}),
         _ => NotManagedEvent
     }
 }
  */
 unsafe fn peek_event(timeout: uint) -> Event {
     let mut ev = tb_event{  t:0, m:0, key:0, ch:0,  w:0, h:0 };
-    let rc = ffi::tb_peek_event(ptr::addr_of(&ev), timeout as c_uint);
+    let rc = ffi::tb_peek_event(ptr::to_mut_unsafe_ptr(&mut ev), timeout as c_uint);
       unpack_event(rc, &ev)
 }
 
  * Blocking function to return next event.
  */
 pub unsafe fn poll_event() -> Event {
-    let ev = tb_event{  t:0, m:0, key:0, ch:0,  w:0, h:0 };
-    let rc = ffi::tb_poll_event(ptr::addr_of(&ev));
+    let mut ev = tb_event{  t:0, m:0, key:0, ch:0,  w:0, h:0 };
+    let rc = ffi::tb_poll_event(ptr::to_mut_unsafe_ptr(&mut ev)); //ptr::addr_of(&ev)
     unpack_event(rc, &ev)
 }
 
-
-/*
-// Convenience functions
-fn with_term(-f: fn~()) {
-    init();
-    let res = task::try(f);
-    shutdown();
-    if result::is_err(res) {
-        #error("with_term: An error occured.");
-    }
-}
-*/
-
-//}
-
-
-// =======================================================
-fn main() {
-  unsafe {
-    init();
-    print(1, 1, Bold, White, Black, ~"Hello, world!"); 
-    present(); 
-
-    loop {
-       let tmp = poll_event();
-       match tmp {
-            KeyEvent{ md:_ , key:_ , ch:113} => break,
-            KeyEvent{md, key, ch } => io::println(fmt!("KEY: %? %? %?",md, key, ch) ),
-            _ => ()
-        }
-    }
-    shutdown();
-  }
-}