#[1]Ubuntu Forums RSS Feed Thread: [2]WriteRoom/Darkroom/? View Single Post #[3]10 Old July 10th, 2006 [4]dicesquirrel dicesquirrel is offline First Cup of Ubuntu Join Date: Apr 2006 Beans: 4 Re: WriteRoom/Darkroom/? _________________________________________________________________ With the gracious help of one of my friends, Arek the Absolute on GameDev.net, I now have a solution to my problems! Even better than that, I have the source, and I've been tweaking it (with his considerable help) to be even better with each iteration. Hopefully someday it will be ready for primetime in Universe or something! Create a new directory and navigate to it: Code: mkdir aedit cd aedit Open up a new file with gedit: Code: gedit main.cpp Paste this text into the gedit window: Code: /* 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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* This program is simply a sensory-deprivation themed text editor, for concen tration purposes. CTRL+SHIFT+BACKSPACE quits, CTRL+SHIFT+S saves. The first line is used as t he filename. Compile with -DSCROLL option to enable a vertical scrollbar. */ #include #include #include #include #include using namespace std; void Destroy(); gboolean Accel(GtkAccelGroup *accel_group, GObject *acceleratable, guint keyval , GdkModifierType modifier); void modify_cursor_color(GtkWidget *widget, GdkColor *color); GtkWindow *window = NULL; GtkWidget *scroll = NULL; GtkWidget *edit = NULL; /* the main program is given as a DLL when compiled for Windows to allow the ma in program to locate the GTK runtimes before executing the program itself. This is irrelevant to any othe r platform. */ #ifdef _WIN32 #include extern "C" __declspec(dllexport) int Main(int argc, char *argv[]){ #else int main(int argc, char *argv[]){ #endif gtk_init(&argc,&argv); // create the window window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_fullscreen(window); gtk_window_set_title(window,"Editor"); gtk_window_set_keep_above(window,true); gtk_widget_set_size_request(GTK_WIDGET(window),600,400); g_signal_connect(window,"delete_event",G_CALLBACK(Destroy),NULL); edit = gtk_text_view_new(); //set the side margins for maximum readability gtk_text_view_set_left_margin(GTK_TEXT_VIEW(edit),256); gtk_text_view_set_right_margin(GTK_TEXT_VIEW(edit),256); gtk_widget_set_name(edit,"GtkEditorView"); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(edit),GTK_WRAP_WORD); scroll = gtk_scrolled_window_new(NULL,NULL); // simply add a scroll window to allow for convenient scrolling capabilitie s like the mouse wheel gtk_container_add(GTK_CONTAINER(window),scroll); ((GtkScrolledWindow*)scroll)->hscrollbar_policy = GTK_POLICY_NEVER; #ifdef SCROLL ((GtkScrolledWindow*)scroll)->vscrollbar_policy = GTK_POLICY_AUTOMATIC; #else ((GtkScrolledWindow*)scroll)->vscrollbar_policy = GTK_POLICY_NEVER; #endif gtk_container_add(GTK_CONTAINER(scroll),edit); GdkColor color; gdk_color_parse("black",&color); gtk_widget_modify_base(edit,GTK_STATE_NORMAL,&color); gdk_color_parse("green",&color); gtk_widget_modify_text(edit,GTK_STATE_NORMAL,&color); gdk_color_parse("yellow",&color); modify_cursor_color(edit,&color); // set the font to monospace GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit)); PangoFontDescription *font_desc = pango_font_description_from_string("Couri er New,Monospace 12"); gtk_widget_modify_font(edit,font_desc); pango_font_description_free(font_desc); // if specified in the command prompt, attempt to open a file if (argc > 1){ ifstream file(argv[1]); if (file){ // prepend the file name on its own line for saving purposes string body = argv[1]; body.append("\n"); while (file && file.peek() != EOF) body += file.get(); gtk_text_buffer_set_text(buffer,body.c_str(),body.length()); } } else { // if no file is specified, simply provide a quick introduction string intro = "filename.txt\n\n" "CTRL+SHIFT+BACKSPACE to quit (without saving)\n" "CTRL+SHIFT+S to save (first line is used as filename)\n"; //Comment the next line if you don't want the initial text shown. gtk_text_buffer_set_text(buffer,intro.c_str(),intro.length()); } GtkAccelGroup *accel = gtk_accel_group_new(); GClosure *closure; GdkModifierType CTRL_SHIFT = (GdkModifierType)(GDK_CONTROL_MASK | GDK_SHIFT _MASK); // add the quit shortcut closure = g_cclosure_new(G_CALLBACK(Accel),NULL,NULL); gtk_accel_group_connect(accel,GDK_BackSpace,CTRL_SHIFT,(GtkAccelFlags)0,clo sure); // add the save shortcut closure = g_cclosure_new(G_CALLBACK(Accel),NULL,NULL); gtk_accel_group_connect(accel,GDK_S,CTRL_SHIFT,(GtkAccelFlags)0,closure); gtk_window_add_accel_group(window,accel); gtk_widget_show_all(GTK_WIDGET(window)); // enter the main application loop gtk_main(); return 0; } void Destroy(){ gtk_main_quit(); } gboolean Accel(GtkAccelGroup *accel_group, GObject *acceleratable, guint keyval , GdkModifierType modifier){ if (keyval == GDK_BackSpace) Destroy(); else if (keyval == GDK_S || keyval == GDK_s){ GtkTextIter start, end; GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit)); gtk_text_buffer_get_start_iter(buffer,&start); gtk_text_buffer_get_iter_at_line(buffer,&end,1); gchar *line = gtk_text_buffer_get_slice(buffer,&start,&end,false); if (!line) return true; if (line[0]){ for (int i = 0; line[i] != 0; i++){ if (line[i] == '\b' || line[i] == '\r' || line[i] == '\n') line [i] = 0; } ofstream file(line); start = end; gtk_text_buffer_get_end_iter(buffer,&end); gchar *body = gtk_text_buffer_get_slice(buffer,&start,&end,false); file << body; g_free(body); file.close(); } g_free(line); } return true; } /* Thanks to gedit source: A hack to change the cursor color (widget must be named!) */ void modify_cursor_color(GtkWidget *widget, GdkColor *color){ static const char cursor_color_rc[] = "style \"editor\"\n" "{\n" "GtkTextView::cursor-color=\"#%04x%04x%04x\"\n" "}\n" "widget \"*.%s\" style : application \"editor\"\n"; const gchar *name; gchar *rc_temp; name = gtk_widget_get_name(widget); if (!name) return; if (color){ rc_temp = g_strdup_printf(cursor_color_rc, color->red, color->green, color->blue, name); } else { GtkRcStyle *rc_style; rc_style = gtk_widget_get_modifier_style(widget); rc_temp = g_strdup_printf(cursor_color_rc, rc_style->text[GTK_STATE_NORMAL].red, rc_style->text[GTK_STATE_NORMAL].green, rc_style->text[GTK_STATE_NORMAL].blue, name); } gtk_rc_parse_string(rc_temp); gtk_widget_reset_rc_styles(widget); g_free(rc_temp); } Save it and exit gedit. Now, back in the terminal, enter: Code: g++ -o editor main.cpp `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2 .0` Run the program: Code: ./editor It's sloppy, it could probably be done better, and could stand to have a few more features. Arek and I would love to hear any suggestions you have. Also, if anyone has any suggestions on how to pad the margins out based on number of characters in the column rather than an arbitrary number of pixels, we'd love to hear it, that's one tangle we've yet to work out. Thanks! [5]Reply With Quote dicesquirrel [6]View Public Profile [7]Send a private message to dicesquirrel [8]Find all posts by dicesquirrel Sponsored Links [BUTTON] References 1. http://ubuntuforums.org/external.php?type=RSS2 2. http://ubuntuforums.org/showthread.php?s=5b38ff9e6e55bf8fab9dfc06b168bcd3&p=1235260#post1235260 3. http://ubuntuforums.org/showpost.php?s=5b38ff9e6e55bf8fab9dfc06b168bcd3&p=1235260&postcount=10 4. http://ubuntuforums.org/member.php?s=5b38ff9e6e55bf8fab9dfc06b168bcd3&u=93805 5. http://ubuntuforums.org/newreply.php?s=5b38ff9e6e55bf8fab9dfc06b168bcd3&do=newreply&p=1235260 6. http://ubuntuforums.org/member.php?s=5b38ff9e6e55bf8fab9dfc06b168bcd3&u=93805 7. http://ubuntuforums.org/private.php?s=5b38ff9e6e55bf8fab9dfc06b168bcd3&do=newpm&u=93805 8. http://ubuntuforums.org/search.php?s=5b38ff9e6e55bf8fab9dfc06b168bcd3&do=finduser&u=93805