Streamlined Control Center Code

Programming issues and discussion
efgee
Expert
Posts: 115
Joined: 29 Dec 2013, 20:58

Streamlined Control Center Code

Postby efgee » 19 Jan 2014, 21:37

Streamlined the code of the control center a bit.
Way smaller now (same functionality).
Don't have a bitbucket account, so I post it here:
Hope you guys like it.

Code: Select all

/*  Copyright (c) alphaOS
 *  Written by simargl <archpup-at-gmail-dot-com>
 *  Modified by efgee <efgee2003-at-yahoo-dot-com>
 * 
 *  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/>.
 */

using Gtk;

private class program : Gtk.Window
{
  const string NAME         = "Control Center";
  const string VERSION      = "1.1.0";
  const string DESCRIPTION  = _("Central place for accessing system configuration tools");
  const string ICON         = "control-center";
  const int    MAX_ROW_APPS = 4; // max apps per row can be changed
 
  Gtk.Grid grid; 
  static int app_number;
  static int row_number;
 
  public program()
  {

    // Grid
    grid = new Gtk.Grid();
    grid.set_column_spacing(25);
    grid.set_column_homogeneous(true);

    app_number = 0;
    row_number = 0;
   
    create_group("<b>Personal</b>");
    create_entry("Wallpaper",       "wpset",               "preferences-desktop-wallpaper", "Change your desktop wallpaper");
    create_entry("Appearance",      "lxappearance",        "preferences-desktop-theme",     "Customize Look and Feel");
    create_entry("Openbox",         "obconf",              "obconf",                        "Tweak settings for Openbox");
    create_entry("Menu Editor",     "kickshaw",            "menu-editor",                   "Kickshaw is a menu editor for Openbox");

    create_group("<b>Hardware</b>");
    create_entry("Display",         "lxrandr",             "lxrandr",                       "Change screen resolution and configure external monitors");
    create_entry("Input Devices",   "lxinput",             "lxinput",                       "Configure keyboard, mouse, and other input devices");
    create_entry("Network",         "frisbee",             "gnome-nettool",                 "Frisbee wireless scanner");
    create_entry("Firewall Config", "firewall_install.sh", "preferences-system-firewall",   "Linux firewall configuration utility");

    create_group("<b>System</b>");
    create_entry("Task Manager",    "lxtask",              "utilities-system-monitor",      "Manage running processes");
    create_entry("Grub4DOS",        "grub4dos-ui",         "grub4dos-ui",                   "Configure Grub4DOS bootup loader");
    create_entry("Setup Savefile",  "makepfile.sh",        "application-x-fs4",             "AlphaOS Savefile Creator");

    var menuitem_about = new Gtk.MenuItem.with_label(_("About"));
    menuitem_about.activate.connect(about_dialog);
   
    var menu = new Gtk.Menu();
    menu.append(menuitem_about);
    menu.show_all();
   
    var menubutton = new Gtk.MenuButton();
    menubutton.valign = Gtk.Align.CENTER;
    menubutton.set_popup(menu);
    menubutton.set_image(new Gtk.Image.from_icon_name("emblem-system-symbolic", Gtk.IconSize.MENU));
   
    var headerbar = new Gtk.HeaderBar();
    headerbar.set_show_close_button(true);
    headerbar.set_title(NAME);
    headerbar.pack_end(menubutton);
   
    var window = new Gtk.Window();
    window.window_position = WindowPosition.CENTER;
    window.set_titlebar(headerbar);
    window.add(grid);
    window.set_resizable(false);
    window.set_border_width(10);
    window.set_icon_name(ICON);
    window.show_all();
    window.destroy.connect(Gtk.main_quit);
  }
 
  // creates new
  // argument: label
  private void create_group(string label) {
   
    var group_name = new Gtk.Label(label);
    group_name.set_use_markup(true);
    group_name.set_alignment(0, 1);
   
    if (row_number != 0) {
      group_name.height_request = 50;
    }
   
    app_number = 0;
    row_number = row_number + 2;
    grid.attach(group_name, app_number, row_number, 1, 1);
   
    row_number = row_number + 1;
  }

  // creates new entry
  // arguments: label, app, tooltip, icon
  private void create_entry(string label, string appname, string icon, string tooltip) {

    if (app_number == MAX_ROW_APPS) {
      app_number = 0;
      row_number = row_number + 2;
    }
   
    var entry_image = new Gtk.Image.from_icon_name(icon, Gtk.IconSize.DND);
    entry_image.set_pixel_size(73);
   
    var entry_button = new Button();
    entry_button.set_image(entry_image);
    entry_button.set_tooltip_text(tooltip);
    entry_button.clicked.connect(() => { button_item_clicked(appname); });
    set_button_size_relief_focus(entry_button);
    grid.attach(entry_button, app_number, row_number, 1, 1);
   
    var entry_label = new Label(label);
    grid.attach(entry_label, app_number, (row_number + 1), 1, 1);
   
    app_number++;
  }

  private void button_item_clicked(string item_name)
  {
    try
    {
      Process.spawn_command_line_async(item_name);
    }
    catch (GLib.Error e)
    {
      stderr.printf ("%s\n", e.message);
    }
  }
 
  private void set_button_size_relief_focus(Gtk.Button button_name)
  {
    button_name.set_relief(ReliefStyle.NONE);
    button_name.height_request = 86;
    button_name.set_always_show_image(true);
    button_name.set_image_position(Gtk.PositionType.TOP);
    button_name.set_can_focus(false);
  }
 
  private void about_dialog()
  {
    var about = new AboutDialog();
    about.set_program_name(NAME);
    about.set_version(VERSION);
    about.set_comments(DESCRIPTION);
    about.set_logo_icon_name(ICON);
    about.set_copyright("Copyright \xc2\xa9 alphaOS");
    about.set_website("http://alphaos.tuxfamily.org");
    about.set_property("skip-taskbar-hint", true);
    about.license_type = Gtk.License.GPL_3_0;
    about.run();
    about.hide();
  }

  public static int main (string[] args)
  {
    Gtk.init(ref args);
    new program();
    Gtk.main();
    return 0;
  }
}

simargl
Site Admin
Posts: 466
Joined: 16 May 2013, 10:54
Contact:

Re: Streamlined Control Center Code

Postby simargl » 19 Jan 2014, 23:09

It's perfect! I pushed this version to bitbucket, only added in some places _( for gettext and translation support. Same program, but almost twice less code lines. Great!


Return to “Scripting and Programming”

Who is online

Users browsing this forum: No registered users and 12 guests

cron