2 * gb.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "gtkcompat.h"
32 #define BORDER_THIKNESS 4
33 #define HANDLE_THIKNESS 4
34 #define HANDLE_SHRINK 3
37 #define GEANY_TYPE_PONG (geany_pong_get_type())
38 #define GEANY_PONG(o) (G_TYPE_CHECK_INSTANCE_CAST((o), GEANY_TYPE_PONG, GeanyPong))
39 #define GEANY_IS_PONG(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), GEANY_TYPE_PONG))
42 typedef struct _GeanyPong GeanyPong
;
43 typedef struct _GeanyPongClass GeanyPongClass
;
48 /* no need for private data as the whole thing is private */
50 GtkWidget
*score_label
;
67 struct _GeanyPongClass
69 GtkDialogClass parent_class
;
73 static void geany_pong_finalize(GObject
*obj
);
74 static void geany_pong_response(GtkDialog
*self
, gint response
);
75 static GType
geany_pong_get_type(void) G_GNUC_CONST
;
78 G_DEFINE_TYPE(GeanyPong
, geany_pong
, GTK_TYPE_DIALOG
)
81 #if GTK_CHECK_VERSION(3, 0, 0)
82 static void geany_pong_set_cairo_source_color(cairo_t
*cr
, GdkRGBA
*c
, gdouble a
)
84 cairo_set_source_rgba(cr
, c
->red
, c
->green
, c
->blue
, MIN(c
->alpha
, a
));
87 static void geany_pong_set_cairo_source_color(cairo_t
*cr
, GdkColor
*c
, gdouble a
)
89 cairo_set_source_rgba(cr
, c
->red
/65535.0, c
->green
/65535.0, c
->blue
/65535.0, a
);
94 static gboolean
geany_pong_area_draw(GtkWidget
*area
, cairo_t
*cr
, GeanyPong
*self
)
96 #if GTK_CHECK_VERSION(3, 0, 0)
97 /* we use the window style context because the area one has a transparent
98 * background and we want something to paint for the overlay */
99 GtkStyleContext
*ctx
= gtk_widget_get_style_context(GTK_WIDGET(self
));
100 GtkStateFlags state
= gtk_style_context_get_state(ctx
);
103 gtk_style_context_get_color(ctx
, state
, &fg
);
104 gtk_style_context_get_background_color(ctx
, state
, &bg
);
106 GtkStyle
*style
= gtk_widget_get_style(area
);
107 GdkColor fg
= style
->fg
[GTK_STATE_NORMAL
];
108 GdkColor bg
= style
->bg
[GTK_STATE_NORMAL
];
111 self
->area_width
= gtk_widget_get_allocated_width(area
);
112 self
->area_height
= gtk_widget_get_allocated_height(area
);
114 cairo_set_line_width(cr
, BORDER_THIKNESS
);
116 /* draw the border */
117 cairo_rectangle(cr
, BORDER_THIKNESS
/2, BORDER_THIKNESS
/2,
118 self
->area_width
- BORDER_THIKNESS
, self
->area_height
/* we don't wanna see the bottom */);
119 geany_pong_set_cairo_source_color(cr
, &fg
, 1.0);
122 /* draw the handle */
123 cairo_rectangle(cr
, self
->handle_pos
- self
->handle_width
/2, self
->area_height
- HANDLE_THIKNESS
,
124 self
->handle_width
, HANDLE_THIKNESS
);
128 cairo_arc(cr
, self
->ball_pos
[0], self
->ball_pos
[1], BALL_SIZE
, 0, 2*G_PI
);
131 /* if not running, add an info */
132 if (! self
->source_id
|| self
->handle_width
< 1)
138 geany_pong_set_cairo_source_color(cr
, &bg
, 0.8);
139 cairo_rectangle(cr
, 0, 0, self
->area_width
, self
->area_height
);
142 geany_pong_set_cairo_source_color(cr
, &fg
, 1.0);
143 layout
= pango_cairo_create_layout(cr
);
144 #if GTK_CHECK_VERSION(3, 0, 0)
145 PangoFontDescription
*font
= NULL
;
146 gtk_style_context_get(ctx
, state
, GTK_STYLE_PROPERTY_FONT
, &font
, NULL
);
149 pango_layout_set_font_description(layout
, font
);
150 pango_font_description_free(font
);
153 pango_layout_set_font_description(layout
, style
->font_desc
);
155 if (! self
->handle_width
)
156 pango_layout_set_markup(layout
, "<b>You won!</b>\n<small>OK, go back to work now.</small>", -1);
158 pango_layout_set_text(layout
, "Click to Play", -1);
159 pango_layout_set_alignment(layout
, PANGO_ALIGN_CENTER
);
160 pango_layout_get_pixel_size(layout
, &pw
, &ph
);
162 scale
= MIN(0.9 * self
->area_width
/ pw
, 0.9 * self
->area_height
/ ph
);
163 cairo_move_to(cr
, (self
->area_width
- pw
* scale
) / 2, (self
->area_height
- ph
* scale
) / 2);
164 cairo_scale(cr
, scale
, scale
);
165 pango_cairo_show_layout(cr
, layout
);
167 g_object_unref(layout
);
174 #if ! GTK_CHECK_VERSION(3, 0, 0)
175 static gboolean
geany_pong_area_expose(GtkWidget
*area
, GdkEventExpose
*event
, GeanyPong
*self
)
177 cairo_t
*cr
= gdk_cairo_create(gtk_widget_get_window(area
));
180 ret
= geany_pong_area_draw(area
, cr
, self
);
188 static void geany_pong_reset_ball(GeanyPong
*self
)
190 self
->ball_speed
= 5;
191 self
->ball_pos
[0] = self
->area_width
/ 2;
192 self
->ball_pos
[1] = self
->area_height
/ 2;
193 self
->ball_vec
[0] = g_random_double_range(.2, .8);
194 self
->ball_vec
[1] = 1.0 - self
->ball_vec
[0];
195 if (g_random_boolean())
196 self
->ball_vec
[0] *= -1;
200 static void geany_pong_update_score(GeanyPong
*self
)
204 g_snprintf(buf
, sizeof buf
, "%u", self
->score
);
205 gtk_label_set_text(GTK_LABEL(self
->score_label
), buf
);
209 static gboolean
geany_pong_area_timeout(gpointer data
)
211 GeanyPong
*self
= data
;
212 const gdouble x
= BORDER_THIKNESS
+ BALL_SIZE
/2;
213 const gdouble y
= BORDER_THIKNESS
+ BALL_SIZE
/2;
214 const gdouble w
= self
->area_width
- BORDER_THIKNESS
- BALL_SIZE
/2;
215 const gdouble h
= self
->area_height
- HANDLE_THIKNESS
- BALL_SIZE
/2;
216 const gdouble old_ball_pos
[2] = { self
->ball_pos
[0], self
->ball_pos
[1] };
217 const gdouble step
[2] = { self
->ball_speed
* self
->ball_vec
[0],
218 self
->ball_speed
* self
->ball_vec
[1] };
221 if (self
->ball_pos
[0] + step
[0] >= w
||
222 self
->ball_pos
[0] + step
[0] <= x
)
223 self
->ball_vec
[0] = -self
->ball_vec
[0];
225 if (self
->ball_pos
[1] + step
[1] <= y
)
226 self
->ball_vec
[1] = -self
->ball_vec
[1];
228 if (self
->ball_pos
[1] + step
[1] >= h
)
230 if (self
->ball_pos
[0] + step
[0] >= self
->handle_pos
- self
->handle_width
/2 &&
231 self
->ball_pos
[0] + step
[0] <= self
->handle_pos
+ self
->handle_width
/2 &&
232 /* only bounce *above* the handle, not below */
233 self
->ball_pos
[1] <= h
)
235 self
->score
+= self
->ball_speed
* 2;
236 geany_pong_update_score(self
);
238 self
->ball_vec
[1] = -self
->ball_vec
[1];
240 self
->handle_width
-= HANDLE_SHRINK
;
241 /* we don't allow a handle smaller than a shrink step */
242 if (self
->handle_width
< HANDLE_SHRINK
)
244 self
->handle_width
= 0;
248 /* let the ball fall completely off before losing */
249 else if (self
->ball_pos
[1] + step
[1] >= self
->area_height
+ BALL_SIZE
)
252 geany_pong_reset_ball(self
);
258 self
->ball_pos
[0] += self
->ball_speed
* self
->ball_vec
[0];
259 self
->ball_pos
[1] += self
->ball_speed
* self
->ball_vec
[1];
262 if (! self
->source_id
)
264 /* we will draw a text all over, just invalidate everything */
265 gtk_widget_queue_draw(self
->area
);
269 /* compute the rough bounding box to redraw the ball */
271 (gint
) MIN(self
->ball_pos
[0], old_ball_pos
[0]) - BALL_SIZE
- 1,
272 (gint
) MIN(self
->ball_pos
[1], old_ball_pos
[1]) - BALL_SIZE
- 1,
273 (gint
) MAX(self
->ball_pos
[0], old_ball_pos
[0]) + BALL_SIZE
+ 1,
274 (gint
) MAX(self
->ball_pos
[1], old_ball_pos
[1]) + BALL_SIZE
+ 1
277 gtk_widget_queue_draw_area(self
->area
, bb
[0], bb
[1], bb
[2] - bb
[0], bb
[3] - bb
[1]);
278 /* always redraw the handle in case it has moved */
279 gtk_widget_queue_draw_area(self
->area
,
280 BORDER_THIKNESS
, self
->area_height
- HANDLE_THIKNESS
,
281 self
->area_width
- BORDER_THIKNESS
*2, HANDLE_THIKNESS
);
284 return self
->source_id
!= 0;
288 static gboolean
geany_pong_area_button_press(GtkWidget
*area
, GdkEventButton
*event
, GeanyPong
*self
)
290 if (event
->type
== GDK_BUTTON_PRESS
&&
291 self
->handle_width
> 0)
293 if (! self
->source_id
)
294 self
->source_id
= g_timeout_add(1000/60, geany_pong_area_timeout
, self
);
297 g_source_remove(self
->source_id
);
300 gtk_widget_queue_draw(area
);
308 static gboolean
geany_pong_area_motion_notify(GtkWidget
*area
, GdkEventMotion
*event
, GeanyPong
*self
)
310 self
->handle_pos
= (gint
) event
->x
;
311 /* clamp so the handle is always fully in */
312 if (self
->handle_pos
< self
->handle_width
/2 + BORDER_THIKNESS
)
313 self
->handle_pos
= self
->handle_width
/2 + BORDER_THIKNESS
;
314 else if (self
->handle_pos
> self
->area_width
- self
->handle_width
/2 - BORDER_THIKNESS
)
315 self
->handle_pos
= self
->area_width
- self
->handle_width
/2 - BORDER_THIKNESS
;
321 static void geany_pong_class_init(GeanyPongClass
*klass
)
323 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
324 GtkDialogClass
*dialog_class
= GTK_DIALOG_CLASS(klass
);
326 object_class
->finalize
= geany_pong_finalize
;
327 dialog_class
->response
= geany_pong_response
;
331 static void geany_pong_init(GeanyPong
*self
)
339 self
->area_height
= AREA_SIZE
;
340 self
->area_width
= AREA_SIZE
;
341 self
->handle_width
= self
->area_width
/ 2;
342 self
->handle_pos
= self
->area_width
/ 2;
343 geany_pong_reset_ball(self
);
345 gtk_window_set_title(GTK_WINDOW(self
), "Happy Easter!");
346 gtk_window_set_position(GTK_WINDOW(self
), GTK_WIN_POS_CENTER_ON_PARENT
);
347 gtk_window_set_destroy_with_parent(GTK_WINDOW(self
), TRUE
);
348 gtk_window_set_modal(GTK_WINDOW(self
), TRUE
);
349 gtk_window_set_skip_pager_hint(GTK_WINDOW(self
), TRUE
);
350 gtk_window_set_resizable(GTK_WINDOW(self
), FALSE
);
352 vbox
= gtk_vbox_new(FALSE
, 0);
353 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 5);
354 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(self
))), vbox
, TRUE
, TRUE
, 0);
356 hbox
= gtk_hbox_new(FALSE
, 6);
357 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
359 label
= gtk_label_new("Score:");
360 gtk_misc_set_alignment(GTK_MISC(label
), 1.0, 0.5);
361 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 0);
363 self
->score_label
= gtk_label_new("0");
364 gtk_box_pack_start(GTK_BOX(hbox
), self
->score_label
, FALSE
, FALSE
, 0);
366 self
->area
= gtk_drawing_area_new();
367 gtk_widget_add_events(self
->area
, GDK_BUTTON_PRESS_MASK
| GDK_POINTER_MOTION_MASK
);
368 #if GTK_CHECK_VERSION(3, 0, 0)
369 g_signal_connect(self
->area
, "draw", G_CALLBACK(geany_pong_area_draw
), self
);
371 g_signal_connect(self
->area
, "expose-event", G_CALLBACK(geany_pong_area_expose
), self
);
373 g_signal_connect(self
->area
, "button-press-event", G_CALLBACK(geany_pong_area_button_press
), self
);
374 g_signal_connect(self
->area
, "motion-notify-event", G_CALLBACK(geany_pong_area_motion_notify
), self
);
375 gtk_widget_set_size_request(self
->area
, AREA_SIZE
, AREA_SIZE
);
376 gtk_box_pack_start(GTK_BOX(vbox
), self
->area
, TRUE
, TRUE
, 0);
378 gtk_dialog_add_buttons(GTK_DIALOG(self
),
379 GTK_STOCK_HELP
, GTK_RESPONSE_HELP
,
380 GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
,
382 gtk_dialog_set_default_response(GTK_DIALOG(self
), GTK_RESPONSE_HELP
);
383 gtk_widget_grab_focus(gtk_dialog_get_widget_for_response(GTK_DIALOG(self
), GTK_RESPONSE_HELP
));
385 gtk_widget_show_all(vbox
);
389 static void geany_pong_finalize(GObject
*obj
)
391 GeanyPong
*self
= GEANY_PONG(obj
);
394 g_source_remove(self
->source_id
);
396 G_OBJECT_CLASS(geany_pong_parent_class
)->finalize(obj
);
400 static void geany_pong_help(GeanyPong
*self
)
404 GtkWidget
*scrolledwindow
;
406 GtkTextBuffer
*buffer
;
408 dialog
= gtk_dialog_new_with_buttons("Help", GTK_WINDOW(self
),
409 GTK_DIALOG_DESTROY_WITH_PARENT
| GTK_DIALOG_MODAL
,
410 GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
, NULL
);
411 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_CLOSE
);
412 gtk_container_set_border_width(GTK_CONTAINER(dialog
), 1);
413 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
415 vbox
= gtk_dialog_get_content_area(GTK_DIALOG(dialog
));
417 scrolledwindow
= gtk_scrolled_window_new(NULL
, NULL
);
418 gtk_box_pack_start(GTK_BOX(vbox
), scrolledwindow
, TRUE
, TRUE
, 0);
419 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow
), 5);
420 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow
), GTK_POLICY_NEVER
, GTK_POLICY_NEVER
);
421 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow
), GTK_SHADOW_IN
);
423 textview
= gtk_text_view_new();
424 gtk_container_add(GTK_CONTAINER(scrolledwindow
), textview
);
425 gtk_widget_set_size_request(textview
, 450, -1);
426 gtk_text_view_set_editable(GTK_TEXT_VIEW(textview
), FALSE
);
427 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview
), GTK_WRAP_WORD
);
428 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview
), FALSE
);
429 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview
), 2);
430 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(textview
), 2);
432 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
));
433 gtk_text_buffer_set_text(buffer
,
434 "A small Pong-like\n"
436 "Click to start, and then bounce the ball off the walls without it "
437 "falling down the bottom edge. You control the bottom handle with "
438 "the mouse, but beware! the ball goes faster and faster and the "
439 "handle grows smaller and smaller!", -1);
441 gtk_widget_show_all(dialog
);
442 gtk_dialog_run(GTK_DIALOG(dialog
));
443 gtk_widget_destroy(dialog
);
447 static void geany_pong_response(GtkDialog
*self
, gint response
)
449 g_return_if_fail(GEANY_IS_PONG(self
));
453 case GTK_RESPONSE_HELP
:
454 geany_pong_help(GEANY_PONG(self
));
458 gtk_widget_destroy(GTK_WIDGET(self
));
463 static GtkWidget
*geany_pong_new(GtkWindow
*parent
)
465 return g_object_new(GEANY_TYPE_PONG
, "transient-for", parent
, NULL
);
469 static gboolean
gb_on_key_pressed(GtkWidget
*widget
, GdkEventKey
*event
, gpointer user_data
)
471 static gchar text
[] = "geany";
473 if (event
->keyval
< 0x80)
475 memmove (text
, &text
[1], G_N_ELEMENTS(text
) - 1);
476 text
[G_N_ELEMENTS(text
) - 2] = (gchar
) event
->keyval
;
478 if (utils_str_equal(text
, "geany"))
480 GtkWidget
*pong
= geany_pong_new(GTK_WINDOW(widget
));
481 gtk_widget_show(pong
);