Merge pull request #1667 from eht16/issue1018_win_execute_builtin_path
[geany-mirror.git] / src / gb.c
blobac8b3070e47d0fed15ab147e608e1fabeefbce6b
1 /*
2 * gb.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2014 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * A small Pong-like.
27 #include "utils.h"
29 #include "gtkcompat.h"
32 #define AREA_SIZE 300
33 #define BALL_SIZE 4
34 #define BORDER_THIKNESS 4
35 #define HANDLE_THIKNESS 4
36 #define HANDLE_SHRINK 3
39 #define GEANY_TYPE_PONG (geany_pong_get_type())
40 #define GEANY_PONG(o) (G_TYPE_CHECK_INSTANCE_CAST((o), GEANY_TYPE_PONG, GeanyPong))
41 #define GEANY_IS_PONG(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), GEANY_TYPE_PONG))
44 typedef struct _GeanyPong GeanyPong;
45 typedef struct _GeanyPongClass GeanyPongClass;
47 struct _GeanyPong
49 GtkDialog parent;
50 /* no need for private data as the whole thing is private */
52 GtkWidget *score_label;
53 GtkWidget *area;
55 gint area_height;
56 gint area_width;
58 guint ball_speed;
59 gdouble ball_pos[2];
60 gdouble ball_vec[2];
61 gint handle_width;
62 gint handle_pos;
64 guint score;
66 guint source_id;
69 struct _GeanyPongClass
71 GtkDialogClass parent_class;
75 static void geany_pong_finalize(GObject *obj);
76 static void geany_pong_response(GtkDialog *self, gint response);
77 static GType geany_pong_get_type(void) G_GNUC_CONST;
80 G_DEFINE_TYPE(GeanyPong, geany_pong, GTK_TYPE_DIALOG)
83 #if GTK_CHECK_VERSION(3, 0, 0)
84 static void geany_pong_set_cairo_source_color(cairo_t *cr, GdkRGBA *c, gdouble a)
86 cairo_set_source_rgba(cr, c->red, c->green, c->blue, MIN(c->alpha, a));
88 #else
89 static void geany_pong_set_cairo_source_color(cairo_t *cr, GdkColor *c, gdouble a)
91 cairo_set_source_rgba(cr, c->red/65535.0, c->green/65535.0, c->blue/65535.0, a);
93 #endif
96 static gboolean geany_pong_area_draw(GtkWidget *area, cairo_t *cr, GeanyPong *self)
98 #if GTK_CHECK_VERSION(3, 0, 0)
99 /* we use the window style context because the area one has a transparent
100 * background and we want something to paint for the overlay */
101 GtkStyleContext *ctx = gtk_widget_get_style_context(GTK_WIDGET(self));
102 GtkStateFlags state = gtk_style_context_get_state(ctx);
103 GdkRGBA fg, bg;
105 gtk_style_context_get_color(ctx, state, &fg);
106 gtk_style_context_get_background_color(ctx, state, &bg);
107 #else
108 GtkStyle *style = gtk_widget_get_style(area);
109 GdkColor fg = style->fg[GTK_STATE_NORMAL];
110 GdkColor bg = style->bg[GTK_STATE_NORMAL];
111 #endif
113 self->area_width = gtk_widget_get_allocated_width(area);
114 self->area_height = gtk_widget_get_allocated_height(area);
116 cairo_set_line_width(cr, BORDER_THIKNESS);
118 /* draw the border */
119 cairo_rectangle(cr, BORDER_THIKNESS/2, BORDER_THIKNESS/2,
120 self->area_width - BORDER_THIKNESS, self->area_height /* we don't wanna see the bottom */);
121 geany_pong_set_cairo_source_color(cr, &fg, 1.0);
122 cairo_stroke(cr);
124 /* draw the handle */
125 cairo_rectangle(cr, self->handle_pos - self->handle_width/2, self->area_height - HANDLE_THIKNESS,
126 self->handle_width, HANDLE_THIKNESS);
127 cairo_fill(cr);
129 /* draw the ball */
130 cairo_arc(cr, self->ball_pos[0], self->ball_pos[1], BALL_SIZE, 0, 2*G_PI);
131 cairo_fill(cr);
133 /* if not running, add an info */
134 if (! self->source_id || self->handle_width < 1)
136 PangoLayout *layout;
137 gint pw, ph;
138 gdouble scale;
140 geany_pong_set_cairo_source_color(cr, &bg, 0.8);
141 cairo_rectangle(cr, 0, 0, self->area_width, self->area_height);
142 cairo_paint(cr);
144 geany_pong_set_cairo_source_color(cr, &fg, 1.0);
145 layout = pango_cairo_create_layout(cr);
146 #if GTK_CHECK_VERSION(3, 0, 0)
147 PangoFontDescription *font = NULL;
148 gtk_style_context_get(ctx, state, GTK_STYLE_PROPERTY_FONT, &font, NULL);
149 if (font)
151 pango_layout_set_font_description(layout, font);
152 pango_font_description_free(font);
154 #else
155 pango_layout_set_font_description(layout, style->font_desc);
156 #endif
157 if (! self->handle_width)
158 pango_layout_set_markup(layout, "<b>You won!</b>\n<small>OK, go back to work now.</small>", -1);
159 else
160 pango_layout_set_text(layout, "Click to Play", -1);
161 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
162 pango_layout_get_pixel_size(layout, &pw, &ph);
164 scale = MIN(0.9 * self->area_width / pw, 0.9 * self->area_height / ph);
165 cairo_move_to(cr, (self->area_width - pw * scale) / 2, (self->area_height - ph * scale) / 2);
166 cairo_scale(cr, scale, scale);
167 pango_cairo_show_layout(cr, layout);
169 g_object_unref(layout);
172 return TRUE;
176 #if ! GTK_CHECK_VERSION(3, 0, 0)
177 static gboolean geany_pong_area_expose(GtkWidget *area, GdkEventExpose *event, GeanyPong *self)
179 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(area));
180 gboolean ret;
182 ret = geany_pong_area_draw(area, cr, self);
183 cairo_destroy(cr);
185 return ret;
187 #endif
190 static void geany_pong_reset_ball(GeanyPong *self)
192 self->ball_speed = 5;
193 self->ball_pos[0] = self->area_width / 2;
194 self->ball_pos[1] = self->area_height / 2;
195 self->ball_vec[0] = g_random_double_range(.2, .8);
196 self->ball_vec[1] = 1.0 - self->ball_vec[0];
197 if (g_random_boolean())
198 self->ball_vec[0] *= -1;
202 static void geany_pong_update_score(GeanyPong *self)
204 gchar buf[16];
206 g_snprintf(buf, sizeof buf, "%u", self->score);
207 gtk_label_set_text(GTK_LABEL(self->score_label), buf);
211 static gboolean geany_pong_area_timeout(gpointer data)
213 GeanyPong *self = data;
214 const gdouble x = BORDER_THIKNESS + BALL_SIZE/2;
215 const gdouble y = BORDER_THIKNESS + BALL_SIZE/2;
216 const gdouble w = self->area_width - BORDER_THIKNESS - BALL_SIZE/2;
217 const gdouble h = self->area_height - HANDLE_THIKNESS - BALL_SIZE/2;
218 const gdouble old_ball_pos[2] = { self->ball_pos[0], self->ball_pos[1] };
219 const gdouble step[2] = { self->ball_speed * self->ball_vec[0],
220 self->ball_speed * self->ball_vec[1] };
222 /* left & right */
223 if (self->ball_pos[0] + step[0] >= w ||
224 self->ball_pos[0] + step[0] <= x)
225 self->ball_vec[0] = -self->ball_vec[0];
226 /* top */
227 if (self->ball_pos[1] + step[1] <= y)
228 self->ball_vec[1] = -self->ball_vec[1];
229 /* bottom */
230 if (self->ball_pos[1] + step[1] >= h)
232 if (self->ball_pos[0] + step[0] >= self->handle_pos - self->handle_width/2 &&
233 self->ball_pos[0] + step[0] <= self->handle_pos + self->handle_width/2 &&
234 /* only bounce *above* the handle, not below */
235 self->ball_pos[1] <= h)
237 self->score += self->ball_speed * 2;
238 geany_pong_update_score(self);
240 self->ball_vec[1] = -self->ball_vec[1];
241 self->ball_speed++;
242 self->handle_width -= HANDLE_SHRINK;
243 /* we don't allow a handle smaller than a shrink step */
244 if (self->handle_width < HANDLE_SHRINK)
246 self->handle_width = 0;
247 self->source_id = 0;
250 /* let the ball fall completely off before losing */
251 else if (self->ball_pos[1] + step[1] >= self->area_height + BALL_SIZE)
252 { /* lost! */
253 self->source_id = 0;
254 geany_pong_reset_ball(self);
258 if (self->source_id)
260 self->ball_pos[0] += self->ball_speed * self->ball_vec[0];
261 self->ball_pos[1] += self->ball_speed * self->ball_vec[1];
264 if (! self->source_id)
266 /* we will draw a text all over, just invalidate everything */
267 gtk_widget_queue_draw(self->area);
269 else
271 /* compute the rough bounding box to redraw the ball */
272 const gint bb[4] = {
273 (gint) MIN(self->ball_pos[0], old_ball_pos[0]) - BALL_SIZE - 1,
274 (gint) MIN(self->ball_pos[1], old_ball_pos[1]) - BALL_SIZE - 1,
275 (gint) MAX(self->ball_pos[0], old_ball_pos[0]) + BALL_SIZE + 1,
276 (gint) MAX(self->ball_pos[1], old_ball_pos[1]) + BALL_SIZE + 1
279 gtk_widget_queue_draw_area(self->area, bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1]);
280 /* always redraw the handle in case it has moved */
281 gtk_widget_queue_draw_area(self->area,
282 BORDER_THIKNESS, self->area_height - HANDLE_THIKNESS,
283 self->area_width - BORDER_THIKNESS*2, HANDLE_THIKNESS);
286 return self->source_id != 0;
290 static gboolean geany_pong_area_button_press(GtkWidget *area, GdkEventButton *event, GeanyPong *self)
292 if (event->type == GDK_BUTTON_PRESS &&
293 self->handle_width > 0)
295 if (! self->source_id)
296 self->source_id = g_timeout_add(1000/60, geany_pong_area_timeout, self);
297 else
299 g_source_remove(self->source_id);
300 self->source_id = 0;
302 gtk_widget_queue_draw(area);
303 return TRUE;
306 return FALSE;
310 static gboolean geany_pong_area_motion_notify(GtkWidget *area, GdkEventMotion *event, GeanyPong *self)
312 self->handle_pos = (gint) event->x;
313 /* clamp so the handle is always fully in */
314 if (self->handle_pos < self->handle_width/2 + BORDER_THIKNESS)
315 self->handle_pos = self->handle_width/2 + BORDER_THIKNESS;
316 else if (self->handle_pos > self->area_width - self->handle_width/2 - BORDER_THIKNESS)
317 self->handle_pos = self->area_width - self->handle_width/2 - BORDER_THIKNESS;
319 return TRUE;
323 static void geany_pong_class_init(GeanyPongClass *klass)
325 GObjectClass *object_class = G_OBJECT_CLASS(klass);
326 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS(klass);
328 object_class->finalize = geany_pong_finalize;
329 dialog_class->response = geany_pong_response;
333 static void geany_pong_init(GeanyPong *self)
335 GtkWidget *vbox;
336 GtkWidget *hbox;
337 GtkWidget *label;
339 self->score = 0;
340 self->source_id = 0;
341 self->area_height = AREA_SIZE;
342 self->area_width = AREA_SIZE;
343 self->handle_width = self->area_width / 2;
344 self->handle_pos = self->area_width / 2;
345 geany_pong_reset_ball(self);
347 gtk_window_set_title(GTK_WINDOW(self), "Happy Easter!");
348 gtk_window_set_position(GTK_WINDOW(self), GTK_WIN_POS_CENTER_ON_PARENT);
349 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
350 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
351 gtk_window_set_skip_pager_hint(GTK_WINDOW(self), TRUE);
352 gtk_window_set_resizable(GTK_WINDOW(self), FALSE);
354 vbox = gtk_vbox_new(FALSE, 0);
355 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
356 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(self))), vbox, TRUE, TRUE, 0);
358 hbox = gtk_hbox_new(FALSE, 6);
359 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
361 label = gtk_label_new("Score:");
362 gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
363 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
365 self->score_label = gtk_label_new("0");
366 gtk_box_pack_start(GTK_BOX(hbox), self->score_label, FALSE, FALSE, 0);
368 self->area = gtk_drawing_area_new();
369 gtk_widget_add_events(self->area, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
370 #if GTK_CHECK_VERSION(3, 0, 0)
371 g_signal_connect(self->area, "draw", G_CALLBACK(geany_pong_area_draw), self);
372 #else
373 g_signal_connect(self->area, "expose-event", G_CALLBACK(geany_pong_area_expose), self);
374 #endif
375 g_signal_connect(self->area, "button-press-event", G_CALLBACK(geany_pong_area_button_press), self);
376 g_signal_connect(self->area, "motion-notify-event", G_CALLBACK(geany_pong_area_motion_notify), self);
377 gtk_widget_set_size_request(self->area, AREA_SIZE, AREA_SIZE);
378 gtk_box_pack_start(GTK_BOX(vbox), self->area, TRUE, TRUE, 0);
380 gtk_dialog_add_buttons(GTK_DIALOG(self),
381 GTK_STOCK_HELP, GTK_RESPONSE_HELP,
382 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
383 NULL);
384 gtk_dialog_set_default_response(GTK_DIALOG(self), GTK_RESPONSE_HELP);
385 gtk_widget_grab_focus(gtk_dialog_get_widget_for_response(GTK_DIALOG(self), GTK_RESPONSE_HELP));
387 gtk_widget_show_all(vbox);
391 static void geany_pong_finalize(GObject *obj)
393 GeanyPong *self = GEANY_PONG(obj);
395 if (self->source_id)
396 g_source_remove(self->source_id);
398 G_OBJECT_CLASS(geany_pong_parent_class)->finalize(obj);
402 static void geany_pong_help(GeanyPong *self)
404 GtkWidget *dialog;
405 GtkWidget *vbox;
406 GtkWidget *scrolledwindow;
407 GtkWidget *textview;
408 GtkTextBuffer *buffer;
410 dialog = gtk_dialog_new_with_buttons("Help", GTK_WINDOW(self),
411 GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
412 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
413 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
414 gtk_container_set_border_width(GTK_CONTAINER(dialog), 1);
415 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
417 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
419 scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
420 gtk_box_pack_start(GTK_BOX(vbox), scrolledwindow, TRUE, TRUE, 0);
421 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow), 5);
422 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
423 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_IN);
425 textview = gtk_text_view_new();
426 gtk_container_add(GTK_CONTAINER(scrolledwindow), textview);
427 gtk_widget_set_size_request(textview, 450, -1);
428 gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
429 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
430 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
431 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview), 2);
432 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(textview), 2);
434 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
435 gtk_text_buffer_set_text(buffer,
436 "A small Pong-like\n"
437 "\n"
438 "Click to start, and then bounce the ball off the walls without it "
439 "falling down the bottom edge. You control the bottom handle with "
440 "the mouse, but beware! the ball goes faster and faster and the "
441 "handle grows smaller and smaller!", -1);
443 gtk_widget_show_all(dialog);
444 gtk_dialog_run(GTK_DIALOG(dialog));
445 gtk_widget_destroy(dialog);
449 static void geany_pong_response(GtkDialog *self, gint response)
451 g_return_if_fail(GEANY_IS_PONG(self));
453 switch (response)
455 case GTK_RESPONSE_HELP:
456 geany_pong_help(GEANY_PONG(self));
457 break;
459 default:
460 gtk_widget_destroy(GTK_WIDGET(self));
465 static GtkWidget *geany_pong_new(GtkWindow *parent)
467 return g_object_new(GEANY_TYPE_PONG, "transient-for", parent, NULL);
471 static gboolean gb_on_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
473 static gchar text[] = "geany";
475 if (event->keyval < 0x80)
477 memmove (text, &text[1], G_N_ELEMENTS(text) - 1);
478 text[G_N_ELEMENTS(text) - 2] = (gchar) event->keyval;
480 if (utils_str_equal(text, "geany"))
482 GtkWidget *pong = geany_pong_new(GTK_WINDOW(widget));
483 gtk_widget_show(pong);
484 return TRUE;
488 return FALSE;