Add status bar, progress bar, command line.
[virt-resize-ui.git] / window.ml
index b00c6b0..a42f1a1 100644 (file)
--- a/window.ml
+++ b/window.ml
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *)
 
+open Printf
+
+open Utils
+
 type file_menu = {
   quit_item : GMenu.menu_item;
 }
@@ -41,6 +45,34 @@ type buttons = {
 class window =
   let title = "Resize a virtual machine - virt-resize-ui" in
 object (self)
+  val mutable tabs = None
+  val mutable statusbar_context = None
+  val mutable progress_bar : GRange.progress_bar option = None
+
+  method source_tab = (Option.get tabs).source_tab
+
+  method set_statusbar msg =
+    Option.may (
+      fun c ->
+        c#pop ();
+        ignore (c#push msg)
+    ) statusbar_context
+
+  method progress (position, total) =
+    Option.may (
+      fun pb ->
+        if position = 0L && total = 1L then
+          pb#pulse ()
+        else (
+          let frac = Int64.to_float position /. Int64.to_float total in
+          if frac < 0. || frac > 1. then
+            eprintf "warning: progress bar out of range: %Ld / %Ld (%g)\n"
+              position total frac;
+          let frac = if frac < 0. then 0. else if frac > 1. then 1. else frac in
+          pb#set_fraction frac
+        )
+    ) progress_bar
+
   initializer
   (* Window. *)
   let window = GWindow.window ~width:700 ~height:700 ~title () in
@@ -63,8 +95,10 @@ object (self)
     let about = factory#add_item "About virt-resize-ui ..." in
     { about_item = about } in
 
+  ignore help_menu;
+
   (* Tabbed notebook for main part of the display. *)
-  let tabs =
+  tabs <- (
     let nb = GPack.notebook ~packing:(vbox#pack ~expand:true ~fill:true) () in
 
     let src = Source_tab.tab () in
@@ -85,8 +119,15 @@ object (self)
       (GMisc.label ~text:"Expand logical volumes" () :> GObj.widget) in
     ignore (nb#append_page ~tab_label (lvs :> GObj.widget));
 
-    { source_tab = src; destination_tab = dest;
-      partitions_tab = parts; logvols_tab = lvs } in
+    Some { source_tab = src; destination_tab = dest;
+           partitions_tab = parts; logvols_tab = lvs });
+
+  (* Status bar and progress bar. *)
+  let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
+  progress_bar <- Some (GRange.progress_bar ~packing:hbox#pack ());
+  let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
+  statusbar_context <- Some (statusbar#new_context ~name:"Standard");
+  ignore ((Option.get statusbar_context)#push title);
 
   (* Buttons. *)
   let buttons =
@@ -104,9 +145,6 @@ object (self)
     { prev_button = prev; next_button = next; go_button = go;
       exit_button = ex } in
 
-  ignore help_menu;
-  ignore tabs;
-
   (* Quit button. *)
   let quit _ = GMain.quit (); false in
   ignore (window#connect#destroy ~callback:GMain.quit);