gmrun/src/main.cc

228 lines
6.2 KiB
C++
Raw Normal View History

2001-02-23 07:48:14 +00:00
/*****************************************************************************
* $Id: main.cc,v 1.8 2001/06/27 07:00:55 mishoo Exp $
2001-02-23 07:48:14 +00:00
* Copyright (C) 2000, Mishoo
* Author: Mihai Bazon Email: mishoo@fenrir.infoiasi.ro
*
* Distributed under the terms of the GNU General Public License. You are
* free to use/modify/distribute this program as long as you comply to the
* terms of the GNU General Public License, version 2 or above, at your
* option, and provided that this copyright notice remains intact.
*****************************************************************************/
#include "gtkcompletionline.h"
#include "history.h"
2001-05-04 09:05:44 +00:00
#include "prefs.h"
2001-02-23 07:48:14 +00:00
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <string>
#include <iostream>
2001-02-23 07:48:14 +00:00
using namespace std;
GtkStyle* style_notfound(GtkWidget *w)
{
static GtkStyle *style;
2001-02-23 07:48:14 +00:00
if (!style) {
style = gtk_style_copy(gtk_widget_get_style(w));
style->fg[GTK_STATE_NORMAL] = (GdkColor){0, 0xFFFF, 0x0000, 0x0000};
gtk_style_ref(style);
}
return style;
2001-02-23 07:48:14 +00:00
}
GtkStyle* style_notunique(GtkWidget *w)
{
static GtkStyle *style;
2001-02-23 07:48:14 +00:00
if (!style) {
style = gtk_style_copy(gtk_widget_get_style(w));
style->fg[GTK_STATE_NORMAL] = (GdkColor){0, 0x0000, 0x0000, 0xFFFF};
gtk_style_ref(style);
}
return style;
2001-02-23 07:48:14 +00:00
}
GtkStyle* style_unique(GtkWidget *w)
{
static GtkStyle *style;
2001-02-23 07:48:14 +00:00
if (!style) {
style = gtk_style_copy(gtk_widget_get_style(w));
style->fg[GTK_STATE_NORMAL] = (GdkColor){0, 0x0000, 0xFFFF, 0x0000};
gtk_style_ref(style);
}
return style;
2001-02-23 07:48:14 +00:00
}
int
IsStringBlank(const char *str)
{
int i=-1;
while(++i<strlen(str)) {
if( str[i] != ' ' )
return 0;
}
return 1;
}
2001-02-23 07:48:14 +00:00
static void
on_compline_activated(GtkEntry *entry, gpointer data)
{
const char *progname = gtk_entry_get_text(entry);
if(!IsStringBlank(progname)) {
string tmp = progname;
tmp += " &";
system(tmp.c_str());
history.append(progname);
}
gtk_main_quit();
2001-02-23 07:48:14 +00:00
}
static void
on_compline_runwithterm(GtkCompletionLine *cl, gpointer data)
{
string command(gtk_entry_get_text(GTK_ENTRY(cl)));
string tmp;
2001-05-04 09:05:44 +00:00
string term;
string::size_type i;
i = command.find_first_not_of(" \t");
if (i != string::npos) {
if (!configuration.get_string("TermExec", term)) {
term = "xterm -e";
}
tmp = term;
tmp += " ";
tmp += command;
tmp += " &";
} else {
if (!configuration.get_string("Terminal", term)) {
term = "xterm";
}
tmp = term + " &";
2001-05-04 09:05:44 +00:00
}
#ifdef DEBUG
cerr << tmp << endl;
#endif
system(tmp.c_str());
history.append(command.c_str());
gtk_main_quit();
}
2001-02-23 07:48:14 +00:00
static void
on_compline_unique(GtkCompletionLine *cl, GtkWidget *label)
{
gtk_label_set_text(GTK_LABEL(label), "unique");
gtk_widget_set_style(label, style_unique(label));
2001-02-23 07:48:14 +00:00
}
static void
on_compline_notunique(GtkCompletionLine *cl, GtkWidget *label)
{
gtk_label_set_text(GTK_LABEL(label), "not unique");
gtk_widget_set_style(label, style_notunique(label));
2001-02-23 07:48:14 +00:00
}
static void
on_compline_incomplete(GtkCompletionLine *cl, GtkWidget *label)
{
gtk_label_set_text(GTK_LABEL(label), "not found");
gtk_widget_set_style(label, style_notfound(label));
2001-02-23 07:48:14 +00:00
}
static void
on_compline_uparrow(GtkCompletionLine *cl, GtkWidget *label)
{
history.set_default(gtk_entry_get_text(GTK_ENTRY(cl)));
gtk_entry_set_text(GTK_ENTRY(cl), history.prev());
2001-02-23 07:48:14 +00:00
}
static void
on_compline_dnarrow(GtkCompletionLine *cl, GtkWidget *label)
{
history.set_default(gtk_entry_get_text(GTK_ENTRY(cl)));
gtk_entry_set_text(GTK_ENTRY(cl), history.next());
2001-02-23 07:48:14 +00:00
}
int main(int argc, char **argv)
{
GtkWidget *win;
GtkWidget *compline;
gtk_init(&argc, &argv);
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_name(win, "Msh_Run_Window");
gtk_window_set_title(GTK_WINDOW(win), "Execute program feat. completion");
gtk_window_set_policy(GTK_WINDOW(win), FALSE, FALSE, TRUE);
gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(win), 4);
gtk_signal_connect(GTK_OBJECT(win), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
GtkWidget *hbox = gtk_vbox_new(FALSE, 2);
gtk_widget_show(hbox);
gtk_container_add(GTK_CONTAINER(win), hbox);
GtkWidget *label = gtk_label_new("Run program:");
gtk_widget_show(label);
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 1.0);
gtk_misc_set_padding(GTK_MISC(label), 10, 0);
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
GtkAccelGroup *accels = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(win), accels);
2001-02-23 07:48:14 +00:00
compline = gtk_completion_line_new();
gtk_widget_set_name(compline, "Msh_Run_Compline");
2001-05-04 09:05:44 +00:00
int prefs_width;
if (!configuration.get_int("Width", prefs_width))
prefs_width = 500;
gtk_widget_set_usize(compline, prefs_width, -2);
gtk_widget_add_accelerator(compline, "destroy", accels,
GDK_Escape, 0, (GtkAccelFlags)0);
gtk_signal_connect(GTK_OBJECT(compline), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
gtk_signal_connect(GTK_OBJECT(compline), "activate",
GTK_SIGNAL_FUNC(on_compline_activated), NULL);
gtk_signal_connect(GTK_OBJECT(compline), "runwithterm",
GTK_SIGNAL_FUNC(on_compline_runwithterm), NULL);
gtk_signal_connect(GTK_OBJECT(compline), "unique",
GTK_SIGNAL_FUNC(on_compline_unique), label);
gtk_signal_connect(GTK_OBJECT(compline), "notunique",
GTK_SIGNAL_FUNC(on_compline_notunique), label);
gtk_signal_connect(GTK_OBJECT(compline), "incomplete",
GTK_SIGNAL_FUNC(on_compline_incomplete), label);
gtk_signal_connect(GTK_OBJECT(compline), "uparrow",
GTK_SIGNAL_FUNC(on_compline_uparrow), label);
gtk_signal_connect(GTK_OBJECT(compline), "dnarrow",
GTK_SIGNAL_FUNC(on_compline_dnarrow), label);
gtk_widget_show(compline);
2001-02-23 07:48:14 +00:00
gtk_box_pack_start(GTK_BOX(hbox), compline, TRUE, TRUE, 0);
2001-02-23 07:48:14 +00:00
gtk_widget_show(win);
int prefs_top;
int prefs_left;
gdk_window_get_position(win->window, &prefs_top, &prefs_left);
configuration.get_int("Top", prefs_top);
configuration.get_int("Left", prefs_left);
gdk_window_move(win->window, prefs_left, prefs_top);
gtk_window_set_focus(GTK_WINDOW(win), compline);
2001-02-23 07:48:14 +00:00
gtk_main();
2001-02-23 07:48:14 +00:00
}