summaryrefslogtreecommitdiff
path: root/message
blob: e8f870f27536fdabd8cfe72aa6fe2c843fa6100b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

   #[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 <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <iostream>
#include <fstream>
#include <string>
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 <windows.h>
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