pdf-presenter/src/ControlBar.vala

166 lines
4.3 KiB
Vala

/*
* pdf-presenter - advanced pdf presentation util
*
* Copyright (C) 2010 Sebastian Reichel <sre@ring0.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using Gtk;
class ControlToolbar : Toolbar {
private ToolButton prev;
private Entry entry;
private ToolButton next;
private Label curr_time;
private Label pres_time;
private ToolButton config;
private int page = 1;
private int pages = 0;
private int timestamp1 = 0;
private int timestamp2 = 0;
public signal void clicked(string button);
public signal void page_changed(int page);
public ControlToolbar(int n_pages) {
/* Toolbar Configuration */
set_show_arrow(false);
pages = n_pages;
/* Init Toolbar Elements */
prev = new ToolButton.from_stock(Stock.GO_BACK);
entry = new Entry();
entry.set_width_chars(3);
next = new ToolButton.from_stock(Stock.GO_FORWARD);
config = new ToolButton.from_stock(Stock.PREFERENCES);
curr_time = new Label("Time");
pres_time = new Label("Duration");
pres_time.set_markup("<b>Duration</b>");
var separator1 = new SeparatorToolItem();
var separator2 = new SeparatorToolItem();
var entry_container = new ToolItem();
entry_container.add(entry);
var vbox_container = new ToolItem();
var vbox = new VBox(false, 0);
vbox.add(curr_time);
vbox.add(pres_time);
vbox_container.add(vbox);
/* Fill Toolbar */
add(prev);
add(entry_container);
add(next);
add(separator1);
add(vbox_container);
add(separator2);
add(config);
update_toolbar();
/* we don't want focus on a button */
entry.grab_focus();
/* update clock */
Timeout.add_seconds(1,update_time);
timestamp2 = (int) time_t();
update_time();
/* bind button signals */
next.clicked.connect(() => {clicked("next");});
prev.clicked.connect(() => {clicked("previous");});
config.clicked.connect(() => {clicked("config");});
this.clicked.connect(button_handler);
entry.key_press_event.connect(entry_handler);
}
bool entry_handler(Gdk.EventKey event) {
if(event.keyval == Gdk.keyval_from_name("Return")) {
int new_page = int.parse(entry.text);
if(page < 1) page = 1;
if(page > pages) page = pages;
set_page(new_page);
page_changed(new_page-1);
return true;
}
if(event.keyval < '0' || event.keyval > '9' && event.keyval != Gdk.keyval_from_name("BackSpace")) {
return true;
}
return false;
}
void button_handler(string button) {
switch(button) {
case "next":
set_page(page+1);
page_changed(page-1);
break;
case "previous":
set_page(page-1);
page_changed(page-1);
break;
default:
break;
}
}
string int2time(int timestamp, bool local=false) {
Time t;
if(local)
t = Time.local((time_t) timestamp);
else
t = Time.gm((time_t) timestamp);
return "%02d:%02d:%02d".printf(t.hour, t.minute, t.second);
}
private bool update_time() {
timestamp1++;
timestamp2++;
pres_time.set_markup("<b>"+int2time(timestamp1)+"</b>");
curr_time.set_text(int2time(timestamp2,true));
return true;
}
private void update_toolbar() {
if(page <= 1) prev.set_sensitive(false);
else prev.set_sensitive(true);
if(page >= pages) next.set_sensitive(false);
else next.set_sensitive(true);
entry.set_text("%d".printf(page));
}
public void set_page(int n) {
page = n;
update_toolbar();
}
}