Permalink
Browse files

Main window basic implementation.

  • Loading branch information...
1 parent 61ad08e commit 6c233c3a7626ad49e103cd57969374bc462084a6 @dnohales committed Jan 15, 2012
Showing with 342 additions and 29 deletions.
  1. +1 −0 data/Makefile.am
  2. +13 −0 data/gablabel/content-reformat.js
  3. +184 −0 data/gablabel/gablabel.ui
  4. +3 −3 src/Makefile.am
  5. +139 −23 src/gablabel.vala
  6. +2 −3 vapi/config.vapi
View
@@ -0,0 +1 @@
+SUBDIRS = gablabel
@@ -0,0 +1,13 @@
+function removeElementById(id){
+ var elem = document.getElementById(id);
+ if(elem){
+ elem.parentNode.removeChild(elem);
+ }
+}
+
+removeElementById("gb");
+removeElementById("gt-logo");
+removeElementById("gt-ft-mkt");
+removeElementById("select_document");
+removeElementById("gt-res-tip");
+document.getElementById("ft-r").innerHTML = "Powered by Google";
@@ -0,0 +1,184 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+ <requires lib="gtk+" version="2.24"/>
+ <!-- interface-naming-policy project-wide -->
+ <object class="GtkImage" id="image_fullscreen">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="stock">gtk-fullscreen</property>
+ </object>
+ <object class="GtkImage" id="image_refresh">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="stock">gtk-refresh</property>
+ </object>
+ <object class="GtkMenu" id="main_menu">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <child>
+ <object class="GtkImageMenuItem" id="main_menu_fullscreen">
+ <property name="label" translatable="yes">Fullscreen</property>
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <property name="image">image_fullscreen</property>
+ <property name="use_stock">False</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkImageMenuItem" id="main_menu_reload">
+ <property name="label" translatable="yes">Reload</property>
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <property name="image">image_refresh</property>
+ <property name="use_stock">False</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkImageMenuItem" id="main_menu_preferences">
+ <property name="label">gtk-preferences</property>
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <property name="use_underline">True</property>
+ <property name="use_stock">True</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkSeparatorMenuItem" id="menuitem2">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkImageMenuItem" id="main_menu_about">
+ <property name="label">gtk-about</property>
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <property name="use_underline">True</property>
+ <property name="use_stock">True</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkImageMenuItem" id="main_menu_quit">
+ <property name="label">gtk-quit</property>
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <property name="use_stock">True</property>
+ </object>
+ </child>
+ </object>
+ <object class="GtkWindow" id="mainwindow">
+ <property name="can_focus">False</property>
+ <property name="title" translatable="yes">Gablabel text translator</property>
+ <child>
+ <object class="GtkVBox" id="central_widget">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <child>
+ <object class="GtkToolbar" id="toolbar">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="toolbar_style">both-horiz</property>
+ <child>
+ <object class="GtkToolButton" id="toolbutton_reload">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="tooltip_text" translatable="yes">Press this button when the
+translator does not work well</property>
+ <property name="use_action_appearance">False</property>
+ <property name="is_important">True</property>
+ <property name="label" translatable="yes">Reload</property>
+ <property name="use_underline">True</property>
+ <property name="stock_id">gtk-refresh</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="homogeneous">True</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkToolButton" id="toolbutton_about">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <property name="is_important">True</property>
+ <property name="label" translatable="yes">Preferences</property>
+ <property name="use_underline">True</property>
+ <property name="stock_id">gtk-preferences</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="homogeneous">True</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkToolItem" id="toolbutton_separator">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <child>
+ <placeholder/>
+ </child>
+ </object>
+ <packing>
+ <property name="expand">True</property>
+ <property name="homogeneous">True</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkMenuToolButton" id="toolbutton_menu">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="use_action_appearance">False</property>
+ <property name="is_important">True</property>
+ <property name="label" translatable="yes">Menu</property>
+ <property name="use_underline">True</property>
+ <property name="stock_id">gtk-properties</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ </packing>
+ </child>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="fill">True</property>
+ <property name="position">0</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkScrolledWindow" id="web_view_parent">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="hscrollbar_policy">automatic</property>
+ <property name="vscrollbar_policy">automatic</property>
+ <child>
+ <placeholder/>
+ </child>
+ </object>
+ <packing>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkLabel" id="aux_label">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ </object>
+ <packing>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
+ <property name="position">2</property>
+ </packing>
+ </child>
+ </object>
+ </child>
+ </object>
+</interface>
View
@@ -6,9 +6,9 @@ VALAFLAGS = \
$(VALA_PACKAGES)
INCLUDES = \
- -DDATADIR=\"$(datadir)\" \
- -DPACKAGE_DATADIR=\""$(datadir)/gablabel"\" \
- -DPACKAGE_LOCALEDIR=\""$(datadir)/locale"\" \
+ -DDATAROOT_DIR=\"$(datarootdir)\" \
+ -DDATA_DIR=\""$(datadir)/gablabel"\" \
+ -DLOCALE_DIR=\""$(localedir)"\" \
$(GABLABEL_CFLAGS) \
-include $(CONFIG_HEADER)
View
@@ -1,28 +1,144 @@
-/* main.vala
- *
- * Copyright (C) 2010 Damián Nohales
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * Author:
- * Damián Nohales <damiannohales@gmail.com>
- */
using Gtk;
using WebKit;
+using Config;
-static int main (string[] args) {
- stdout.puts("Hello World 2!");
- return 0;
+namespace Gablabel
+{
+ public class TranslatorWebView : WebView
+ {
+ public signal void translator_load_started();
+ public signal void translator_load_finished();
+
+ private const string TRANSLATOR_URL = "http://translate.google.com/";
+
+ public TranslatorWebView() {
+ this.document_load_finished.connect(on_download_finish);
+ }
+
+ public void load_translator()
+ {
+ this.translator_load_started();
+ this.navigation_policy_decision_requested.disconnect(analyze_navigation_policy);
+ this.load_uri(TRANSLATOR_URL);
+ }
+
+ private void reformat_content()
+ {
+ try{
+ string script;
+ FileUtils.get_contents(Config.DATA_DIR + "/content-reformat.js", out script);
+ this.execute_script(script);
+ } catch(Error e){
+ }
+ }
+
+ private void on_download_finish(WebFrame frame)
+ {
+ this.navigation_policy_decision_requested.connect(analyze_navigation_policy);
+ this.reformat_content();
+ this.translator_load_finished();
+ }
+
+ private bool analyze_navigation_policy(WebFrame frame, NetworkRequest request, WebNavigationAction action, WebPolicyDecision decision) {
+ decision.ignore();
+ return true;
+ }
+ }
+
+ public class MainWindow : Window
+ {
+ private TranslatorWebView webView;
+ private Label auxLabel;
+ private Menu mainMenu;
+ private ToolButton buttonReload;
+ private ImageMenuItem menuItemReload;
+ private bool isFullscreen;
+
+ public MainWindow() throws Error{
+ //Widgets creation
+ var builder = new Builder();
+ builder.add_from_file(Config.DATA_DIR + "/gablabel.ui");
+
+ var central_widget = builder.get_object("central_widget") as VBox;
+ (builder.get_object("mainwindow") as Window).remove(central_widget);
+ this.add(central_widget);
+
+ webView = new TranslatorWebView();
+ (builder.get_object("web_view_parent") as ScrolledWindow).add(webView);
+
+ auxLabel = builder.get_object("aux_label") as Label;
+ mainMenu = builder.get_object("main_menu") as Menu;
+ buttonReload = builder.get_object("toolbutton_reload") as ToolButton;
+ menuItemReload = builder.get_object("main_menu_reload") as ImageMenuItem;
+
+ //Signals connection
+ this.destroy.connect(Gtk.main_quit);
+ this.window_state_event.connect(on_window_state_event);
+
+ webView.translator_load_started.connect(on_translator_load_started);
+ webView.translator_load_finished.connect(on_translator_load_finished);
+
+ var buttonMainMenu = (builder.get_object("toolbutton_menu") as MenuToolButton);
+ buttonMainMenu.set_menu(mainMenu);
+ buttonMainMenu.clicked.connect(() => { buttonMainMenu.show_menu(); });
+ buttonReload.clicked.connect(webView.load_translator);
+
+ (builder.get_object("main_menu_fullscreen") as ImageMenuItem).activate.connect(() => {
+ if(this.isFullscreen){
+ this.unfullscreen();
+ } else{
+ this.fullscreen();
+ }
+ });
+ menuItemReload.activate.connect(webView.load_translator);
+ (builder.get_object("main_menu_quit") as ImageMenuItem).activate.connect(Gtk.main_quit);
+ }
+
+ public void start() {
+ this.default_height = 350;
+ this.default_width = 800;
+ this.show_all();
+
+ webView.load_translator();
+ }
+
+ public void on_translator_load_started() {
+ webView.get_parent().hide_all();
+ auxLabel.label = "Loading Google Translator, please wait...";
+ auxLabel.show_all();
+ buttonReload.sensitive = false;
+ menuItemReload.sensitive = false;
+ }
+
+ public void on_translator_load_finished(){
+ webView.get_parent().show_all();
+ auxLabel.hide_all();
+ buttonReload.sensitive = true;
+ menuItemReload.sensitive = true;
+ }
+
+ public bool on_window_state_event(Gdk.EventWindowState event){
+ this.isFullscreen = event.new_window_state == Gdk.WindowState.FULLSCREEN;
+
+ return false;
+ }
+ }
}
+void main(string[] args)
+{
+ Gtk.init(ref args);
+
+ Intl.bindtextdomain( Config.GETTEXT_PACKAGE, Config.LOCALE_DIR );
+ Intl.bind_textdomain_codeset( Config.GETTEXT_PACKAGE, "UTF-8" );
+ Intl.textdomain( Config.GETTEXT_PACKAGE );
+
+ try{
+ var window = new Gablabel.MainWindow();
+ window.start();
+
+ Gtk.main();
+ } catch(Error e){
+ stderr.printf("Failed to load the UI file: " + e.message);
+ }
+}
View
@@ -10,7 +10,6 @@ namespace Config {
/* Configured paths - these variables are not present in config.h, they are
* passed to underlying C code as cmd line macros. */
- public const string PACKAGE_LOCALEDIR; /* /usr/local/share/locale */
- public const string PACKAGE_DATADIR; /* /usr/local/share/gablabel */
- public const string PACKAGE_LIBDIR; /* /usr/local/lib/gablabel */
+ public const string LOCALE_DIR; /* /usr/local/share/locale */
+ public const string DATA_DIR; /* /usr/local/share/gablabel */
}

0 comments on commit 6c233c3

Please sign in to comment.