| File: | demos/ctk-demo/main.c |
| Warning: | line 431, column 15 Access of 'tokens' at index 3, while it holds only 3 'char *' elements |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include <errno(*__errno_location ()).h> | |||
| 2 | #include <stdio.h> | |||
| 3 | #include <stdlib.h> | |||
| 4 | #include <string.h> | |||
| 5 | ||||
| 6 | #include "config.h" | |||
| 7 | ||||
| 8 | #include <ctk/ctk.h> | |||
| 9 | #include <glib/gstdio.h> | |||
| 10 | ||||
| 11 | #include "demos.h" | |||
| 12 | ||||
| 13 | static CtkWidget *info_view; | |||
| 14 | static CtkWidget *source_view; | |||
| 15 | ||||
| 16 | static gchar *current_file = NULL((void*)0); | |||
| 17 | ||||
| 18 | static CtkWidget *notebook; | |||
| 19 | static CtkWidget *treeview; | |||
| 20 | static CtkWidget *headerbar; | |||
| 21 | ||||
| 22 | enum { | |||
| 23 | NAME_COLUMN, | |||
| 24 | TITLE_COLUMN, | |||
| 25 | FILENAME_COLUMN, | |||
| 26 | FUNC_COLUMN, | |||
| 27 | STYLE_COLUMN, | |||
| 28 | NUM_COLUMNS | |||
| 29 | }; | |||
| 30 | ||||
| 31 | typedef struct _CallbackData CallbackData; | |||
| 32 | struct _CallbackData | |||
| 33 | { | |||
| 34 | CtkTreeModel *model; | |||
| 35 | CtkTreePath *path; | |||
| 36 | }; | |||
| 37 | ||||
| 38 | static void | |||
| 39 | activate_about (GSimpleAction *action G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 40 | GVariant *parameter G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 41 | gpointer user_data) | |||
| 42 | { | |||
| 43 | CtkApplication *app = user_data; | |||
| 44 | const gchar *authors[] = { | |||
| 45 | "The CTK+ Team", | |||
| 46 | NULL((void*)0) | |||
| 47 | }; | |||
| 48 | ||||
| 49 | ctk_show_about_dialog (CTK_WINDOW (ctk_application_get_active_window (app))((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((ctk_application_get_active_window (app))), ((ctk_window_get_type ())))))), | |||
| 50 | "program-name", "CTK+ Demo", | |||
| 51 | "version", g_strdup_printf ("%s,\nRunning against CTK+ %d.%d.%d", | |||
| 52 | PACKAGE_VERSION"3.25.7", | |||
| 53 | ctk_get_major_version (), | |||
| 54 | ctk_get_minor_version (), | |||
| 55 | ctk_get_micro_version ()), | |||
| 56 | "copyright", "(C) 1997-2013 The CTK+ Team", | |||
| 57 | "license-type", CTK_LICENSE_LGPL_2_1, | |||
| 58 | "website", "http://github.com/cafe-desktop/ctk", | |||
| 59 | "comments", "Program to demonstrate CTK+ widgets", | |||
| 60 | "authors", authors, | |||
| 61 | "logo-icon-name", "ctk3-demo", | |||
| 62 | "title", "About CTK+ Demo", | |||
| 63 | NULL((void*)0)); | |||
| 64 | } | |||
| 65 | ||||
| 66 | static void | |||
| 67 | activate_quit (GSimpleAction *action G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 68 | GVariant *parameter G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 69 | gpointer user_data) | |||
| 70 | { | |||
| 71 | CtkApplication *app = user_data; | |||
| 72 | GList *list, *next; | |||
| 73 | ||||
| 74 | list = ctk_application_get_windows (app); | |||
| 75 | while (list) | |||
| 76 | { | |||
| 77 | CtkWidget *win; | |||
| 78 | ||||
| 79 | win = list->data; | |||
| 80 | next = list->next; | |||
| 81 | ||||
| 82 | ctk_widget_destroy (CTK_WIDGET (win)((((CtkWidget*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((win)), ((ctk_widget_get_type ()))))))); | |||
| 83 | ||||
| 84 | list = next; | |||
| 85 | } | |||
| 86 | } | |||
| 87 | ||||
| 88 | static void | |||
| 89 | window_closed_cb (CtkWidget *window G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 90 | gpointer data) | |||
| 91 | { | |||
| 92 | CallbackData *cbdata = data; | |||
| 93 | CtkTreeIter iter; | |||
| 94 | PangoStyle style; | |||
| 95 | ||||
| 96 | ctk_tree_model_get_iter (cbdata->model, &iter, cbdata->path); | |||
| 97 | ctk_tree_model_get (CTK_TREE_MODEL (cbdata->model)((((CtkTreeModel*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((cbdata->model)), ((ctk_tree_model_get_type ())))))), &iter, | |||
| 98 | STYLE_COLUMN, &style, | |||
| 99 | -1); | |||
| 100 | if (style == PANGO_STYLE_ITALIC) | |||
| 101 | ctk_tree_store_set (CTK_TREE_STORE (cbdata->model)((((CtkTreeStore*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((cbdata->model)), ((ctk_tree_store_get_type ())))))), &iter, | |||
| 102 | STYLE_COLUMN, PANGO_STYLE_NORMAL, | |||
| 103 | -1); | |||
| 104 | ||||
| 105 | ctk_tree_path_free (cbdata->path); | |||
| 106 | g_free (cbdata); | |||
| 107 | } | |||
| 108 | ||||
| 109 | static void | |||
| 110 | run_example_for_row (CtkWidget *window, | |||
| 111 | CtkTreeModel *model, | |||
| 112 | CtkTreeIter *iter) | |||
| 113 | { | |||
| 114 | PangoStyle style; | |||
| 115 | GDoDemoFunc func; | |||
| 116 | ||||
| 117 | ctk_tree_model_get (CTK_TREE_MODEL (model)((((CtkTreeModel*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((model)), ((ctk_tree_model_get_type ())))))), | |||
| 118 | iter, | |||
| 119 | FUNC_COLUMN, &func, | |||
| 120 | STYLE_COLUMN, &style, | |||
| 121 | -1); | |||
| 122 | ||||
| 123 | if (func) | |||
| 124 | { | |||
| 125 | CtkWidget *demo; | |||
| 126 | ||||
| 127 | ctk_tree_store_set (CTK_TREE_STORE (model)((((CtkTreeStore*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((model)), ((ctk_tree_store_get_type ())))))), | |||
| 128 | iter, | |||
| 129 | STYLE_COLUMN, (style == PANGO_STYLE_ITALIC ? PANGO_STYLE_NORMAL : PANGO_STYLE_ITALIC), | |||
| 130 | -1); | |||
| 131 | demo = (func) (window); | |||
| 132 | ||||
| 133 | if (demo != NULL((void*)0)) | |||
| 134 | { | |||
| 135 | CallbackData *cbdata; | |||
| 136 | ||||
| 137 | cbdata = g_new (CallbackData, 1)((CallbackData *) g_malloc_n ((1), sizeof (CallbackData))); | |||
| 138 | cbdata->model = model; | |||
| 139 | cbdata->path = ctk_tree_model_get_path (model, iter); | |||
| 140 | ||||
| 141 | if (ctk_widget_is_toplevel (demo)) | |||
| 142 | { | |||
| 143 | ctk_window_set_transient_for (CTK_WINDOW (demo)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((demo)), ((ctk_window_get_type ())))))), CTK_WINDOW (window)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((window)), ((ctk_window_get_type ()))))))); | |||
| 144 | ctk_window_set_modal (CTK_WINDOW (demo)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((demo)), ((ctk_window_get_type ())))))), TRUE(!(0))); | |||
| 145 | } | |||
| 146 | ||||
| 147 | g_signal_connect (demo, "destroy",g_signal_connect_data ((demo), ("destroy"), (((GCallback) (window_closed_cb ))), (cbdata), ((void*)0), (GConnectFlags) 0) | |||
| 148 | G_CALLBACK (window_closed_cb), cbdata)g_signal_connect_data ((demo), ("destroy"), (((GCallback) (window_closed_cb ))), (cbdata), ((void*)0), (GConnectFlags) 0); | |||
| 149 | } | |||
| 150 | } | |||
| 151 | } | |||
| 152 | ||||
| 153 | static void | |||
| 154 | activate_run (GSimpleAction *action G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 155 | GVariant *parameter G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 156 | gpointer user_data) | |||
| 157 | { | |||
| 158 | CtkTreeSelection *selection; | |||
| 159 | CtkTreeModel *model; | |||
| 160 | CtkTreeIter iter; | |||
| 161 | ||||
| 162 | selection = ctk_tree_view_get_selection (CTK_TREE_VIEW (treeview)((((CtkTreeView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((treeview)), ((ctk_tree_view_get_type ()))))))); | |||
| 163 | ||||
| 164 | if (ctk_tree_selection_get_selected (selection, &model, &iter)) { | |||
| 165 | CtkWidget *window = user_data; | |||
| 166 | run_example_for_row (window, model, &iter); | |||
| 167 | } | |||
| 168 | } | |||
| 169 | ||||
| 170 | /* Stupid syntax highlighting. | |||
| 171 | * | |||
| 172 | * No regex was used in the making of this highlighting. | |||
| 173 | * It should only work for simple cases. This is good, as | |||
| 174 | * that's all we should have in the demos. | |||
| 175 | */ | |||
| 176 | /* This code should not be used elsewhere, except perhaps as an example of how | |||
| 177 | * to iterate through a text buffer. | |||
| 178 | */ | |||
| 179 | enum { | |||
| 180 | STATE_NORMAL, | |||
| 181 | STATE_IN_COMMENT | |||
| 182 | }; | |||
| 183 | ||||
| 184 | static gchar *tokens[] = | |||
| 185 | { | |||
| 186 | "/*", | |||
| 187 | "\"", | |||
| 188 | NULL((void*)0) | |||
| 189 | }; | |||
| 190 | ||||
| 191 | static gchar *types[] = | |||
| 192 | { | |||
| 193 | "static", | |||
| 194 | "const ", | |||
| 195 | "void", | |||
| 196 | "gint", | |||
| 197 | " int ", | |||
| 198 | " char ", | |||
| 199 | "gchar ", | |||
| 200 | "gfloat", | |||
| 201 | "float", | |||
| 202 | "double", | |||
| 203 | "gint8", | |||
| 204 | "gint16", | |||
| 205 | "gint32", | |||
| 206 | "guint", | |||
| 207 | "guint8", | |||
| 208 | "guint16", | |||
| 209 | "guint32", | |||
| 210 | "guchar", | |||
| 211 | "glong", | |||
| 212 | "gboolean" , | |||
| 213 | "gshort", | |||
| 214 | "gushort", | |||
| 215 | "gulong", | |||
| 216 | "gdouble", | |||
| 217 | "gldouble", | |||
| 218 | "gpointer", | |||
| 219 | "NULL", | |||
| 220 | "GList", | |||
| 221 | "GSList", | |||
| 222 | "FALSE", | |||
| 223 | "TRUE", | |||
| 224 | "FILE ", | |||
| 225 | "CtkColorSelection ", | |||
| 226 | "CtkWidget ", | |||
| 227 | "CtkButton ", | |||
| 228 | "CdkColor ", | |||
| 229 | "CdkRectangle ", | |||
| 230 | "CdkEventExpose ", | |||
| 231 | "CdkGC ", | |||
| 232 | "GdkPixbufLoader ", | |||
| 233 | "GdkPixbuf ", | |||
| 234 | "GError", | |||
| 235 | "size_t", | |||
| 236 | "CtkAboutDialog ", | |||
| 237 | "CtkAction ", | |||
| 238 | "CtkActionEntry ", | |||
| 239 | "CtkRadioActionEntry ", | |||
| 240 | "CtkIconFactory ", | |||
| 241 | "CtkIconSet ", | |||
| 242 | "CtkTextBuffer ", | |||
| 243 | "CtkStatusbar ", | |||
| 244 | "CtkTextIter ", | |||
| 245 | "CtkTextMark ", | |||
| 246 | "CdkEventWindowState ", | |||
| 247 | "CtkActionGroup ", | |||
| 248 | "CtkUIManager ", | |||
| 249 | "CtkRadioAction ", | |||
| 250 | "CtkActionClass ", | |||
| 251 | "CtkToggleActionEntry ", | |||
| 252 | "CtkAssistant ", | |||
| 253 | "CtkBuilder ", | |||
| 254 | "CtkSizeGroup ", | |||
| 255 | "CtkTreeModel ", | |||
| 256 | "CtkTreeSelection ", | |||
| 257 | "CdkDisplay ", | |||
| 258 | "CdkScreen ", | |||
| 259 | "CdkWindow ", | |||
| 260 | "CdkEventButton ", | |||
| 261 | "CdkCursor ", | |||
| 262 | "CtkTreeIter ", | |||
| 263 | "CtkTreeViewColumn ", | |||
| 264 | "CdkDisplayManager ", | |||
| 265 | "CtkClipboard ", | |||
| 266 | "CtkIconSize ", | |||
| 267 | "CtkImage ", | |||
| 268 | "CdkDragContext ", | |||
| 269 | "CtkSelectionData ", | |||
| 270 | "CtkDialog ", | |||
| 271 | "CtkMenuItem ", | |||
| 272 | "CtkListStore ", | |||
| 273 | "CtkCellLayout ", | |||
| 274 | "CtkCellRenderer ", | |||
| 275 | "CtkTreePath ", | |||
| 276 | "CtkTreeStore ", | |||
| 277 | "CtkEntry ", | |||
| 278 | "CtkEditable ", | |||
| 279 | "CtkEditableInterface ", | |||
| 280 | "CdkPixmap ", | |||
| 281 | "CdkEventConfigure ", | |||
| 282 | "CdkEventMotion ", | |||
| 283 | "CdkModifierType ", | |||
| 284 | "CtkEntryCompletion ", | |||
| 285 | "CtkToolItem ", | |||
| 286 | "GDir ", | |||
| 287 | "CtkIconView ", | |||
| 288 | "CtkCellRendererText ", | |||
| 289 | "CtkContainer ", | |||
| 290 | "CtkAccelGroup ", | |||
| 291 | "CtkPaned ", | |||
| 292 | "CtkPrintOperation ", | |||
| 293 | "CtkPrintContext ", | |||
| 294 | "cairo_t ", | |||
| 295 | "PangoLayout " | |||
| 296 | "PangoFontDescription ", | |||
| 297 | "PangoRenderer ", | |||
| 298 | "PangoMatrix ", | |||
| 299 | "PangoContext ", | |||
| 300 | "PangoLayout ", | |||
| 301 | "CtkTable ", | |||
| 302 | "CtkToggleButton ", | |||
| 303 | "GString ", | |||
| 304 | "CtkIconSize ", | |||
| 305 | "CtkTreeView ", | |||
| 306 | "CtkTextTag ", | |||
| 307 | "CdkEvent ", | |||
| 308 | "CdkEventKey ", | |||
| 309 | "CtkTextView ", | |||
| 310 | "CdkEventVisibility ", | |||
| 311 | "CdkBitmap ", | |||
| 312 | "CtkTextChildAnchor ", | |||
| 313 | "GArray ", | |||
| 314 | "CtkCellEditable ", | |||
| 315 | "CtkCellRendererToggle ", | |||
| 316 | NULL((void*)0) | |||
| 317 | }; | |||
| 318 | ||||
| 319 | static gchar *control[] = | |||
| 320 | { | |||
| 321 | " if ", | |||
| 322 | " while ", | |||
| 323 | " else", | |||
| 324 | " do ", | |||
| 325 | " for ", | |||
| 326 | "?", | |||
| 327 | ":", | |||
| 328 | "return ", | |||
| 329 | "goto ", | |||
| 330 | NULL((void*)0) | |||
| 331 | }; | |||
| 332 | void | |||
| 333 | parse_chars (gchar *text, | |||
| 334 | gchar **end_ptr, | |||
| 335 | gint *state, | |||
| 336 | gchar **tag, | |||
| 337 | gboolean start) | |||
| 338 | { | |||
| 339 | gint i; | |||
| 340 | gchar *next_token; | |||
| 341 | ||||
| 342 | /* Handle comments first */ | |||
| 343 | if (*state == STATE_IN_COMMENT) | |||
| 344 | { | |||
| 345 | *end_ptr = strstr (text, "*/"); | |||
| 346 | if (*end_ptr) | |||
| 347 | { | |||
| 348 | *end_ptr += 2; | |||
| 349 | *state = STATE_NORMAL; | |||
| 350 | *tag = "comment"; | |||
| 351 | } | |||
| 352 | return; | |||
| 353 | } | |||
| 354 | ||||
| 355 | *tag = NULL((void*)0); | |||
| 356 | *end_ptr = NULL((void*)0); | |||
| 357 | ||||
| 358 | /* check for comment */ | |||
| 359 | if (!strncmp (text, "/*", 2)) | |||
| 360 | { | |||
| 361 | *end_ptr = strstr (text, "*/"); | |||
| 362 | if (*end_ptr) | |||
| 363 | *end_ptr += 2; | |||
| 364 | else | |||
| 365 | *state = STATE_IN_COMMENT; | |||
| 366 | *tag = "comment"; | |||
| 367 | return; | |||
| 368 | } | |||
| 369 | ||||
| 370 | /* check for preprocessor defines */ | |||
| 371 | if (*text == '#' && start) | |||
| 372 | { | |||
| 373 | *end_ptr = NULL((void*)0); | |||
| 374 | *tag = "preprocessor"; | |||
| 375 | return; | |||
| 376 | } | |||
| 377 | ||||
| 378 | /* functions */ | |||
| 379 | if (start
| |||
| 380 | { | |||
| 381 | if (strstr (text, "(")) | |||
| 382 | { | |||
| 383 | *end_ptr = strstr (text, "("); | |||
| 384 | *tag = "function"; | |||
| 385 | return; | |||
| 386 | } | |||
| 387 | } | |||
| 388 | /* check for types */ | |||
| 389 | for (i = 0; types[i] != NULL((void*)0); i++) | |||
| 390 | if (!strncmp (text, types[i], strlen (types[i])) || | |||
| 391 | (start && types[i][0] == ' ' && !strncmp (text, types[i] + 1, strlen (types[i]) - 1))) | |||
| 392 | { | |||
| 393 | *end_ptr = text + strlen (types[i]); | |||
| 394 | *tag = "type"; | |||
| 395 | return; | |||
| 396 | } | |||
| 397 | ||||
| 398 | /* check for control */ | |||
| 399 | for (i = 0; control[i] != NULL((void*)0); i++) | |||
| 400 | if (!strncmp (text, control[i], strlen (control[i]))) | |||
| 401 | { | |||
| 402 | *end_ptr = text + strlen (control[i]); | |||
| 403 | *tag = "control"; | |||
| 404 | return; | |||
| 405 | } | |||
| 406 | ||||
| 407 | /* check for string */ | |||
| 408 | if (text[0] == '"') | |||
| 409 | { | |||
| 410 | gint maybe_escape = FALSE(0); | |||
| 411 | ||||
| 412 | *end_ptr = text + 1; | |||
| 413 | *tag = "string"; | |||
| 414 | while (**end_ptr != '\000') | |||
| 415 | { | |||
| 416 | if (**end_ptr == '\"' && !maybe_escape) | |||
| 417 | { | |||
| 418 | *end_ptr += 1; | |||
| 419 | return; | |||
| 420 | } | |||
| 421 | if (**end_ptr == '\\') | |||
| 422 | maybe_escape = TRUE(!(0)); | |||
| 423 | else | |||
| 424 | maybe_escape = FALSE(0); | |||
| 425 | *end_ptr += 1; | |||
| 426 | } | |||
| 427 | return; | |||
| 428 | } | |||
| 429 | ||||
| 430 | /* not at the start of a tag. Find the next one. */ | |||
| 431 | for (i = 0; tokens[i] != NULL((void*)0); i++) | |||
| ||||
| 432 | { | |||
| 433 | next_token = strstr (text, tokens[i]); | |||
| 434 | if (next_token) | |||
| 435 | { | |||
| 436 | if (*end_ptr) | |||
| 437 | *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token; | |||
| 438 | else | |||
| 439 | *end_ptr = next_token; | |||
| 440 | } | |||
| 441 | } | |||
| 442 | ||||
| 443 | for (i = 0; types[i] != NULL((void*)0); i++) | |||
| 444 | { | |||
| 445 | next_token = strstr (text, types[i]); | |||
| 446 | if (next_token) | |||
| 447 | { | |||
| 448 | if (*end_ptr) | |||
| 449 | *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token; | |||
| 450 | else | |||
| 451 | *end_ptr = next_token; | |||
| 452 | } | |||
| 453 | } | |||
| 454 | ||||
| 455 | for (i = 0; control[i] != NULL((void*)0); i++) | |||
| 456 | { | |||
| 457 | next_token = strstr (text, control[i]); | |||
| 458 | if (next_token) | |||
| 459 | { | |||
| 460 | if (*end_ptr) | |||
| 461 | *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token; | |||
| 462 | else | |||
| 463 | *end_ptr = next_token; | |||
| 464 | } | |||
| 465 | } | |||
| 466 | } | |||
| 467 | ||||
| 468 | /* While not as cool as c-mode, this will do as a quick attempt at highlighting */ | |||
| 469 | static void | |||
| 470 | fontify (CtkTextBuffer *source_buffer) | |||
| 471 | { | |||
| 472 | CtkTextIter start_iter, next_iter, tmp_iter; | |||
| 473 | gint state; | |||
| 474 | gchar *text; | |||
| 475 | gchar *tag; | |||
| 476 | ||||
| 477 | ctk_text_buffer_create_tag (source_buffer, "source", | |||
| 478 | "font", "monospace", | |||
| 479 | NULL((void*)0)); | |||
| 480 | ctk_text_buffer_create_tag (source_buffer, "comment", | |||
| 481 | "foreground", "DodgerBlue", | |||
| 482 | NULL((void*)0)); | |||
| 483 | ctk_text_buffer_create_tag (source_buffer, "type", | |||
| 484 | "foreground", "ForestGreen", | |||
| 485 | NULL((void*)0)); | |||
| 486 | ctk_text_buffer_create_tag (source_buffer, "string", | |||
| 487 | "foreground", "RosyBrown", | |||
| 488 | "weight", PANGO_WEIGHT_BOLD, | |||
| 489 | NULL((void*)0)); | |||
| 490 | ctk_text_buffer_create_tag (source_buffer, "control", | |||
| 491 | "foreground", "purple", | |||
| 492 | NULL((void*)0)); | |||
| 493 | ctk_text_buffer_create_tag (source_buffer, "preprocessor", | |||
| 494 | "style", PANGO_STYLE_OBLIQUE, | |||
| 495 | "foreground", "burlywood4", | |||
| 496 | NULL((void*)0)); | |||
| 497 | ctk_text_buffer_create_tag (source_buffer, "function", | |||
| 498 | "weight", PANGO_WEIGHT_BOLD, | |||
| 499 | "foreground", "DarkGoldenrod4", | |||
| 500 | NULL((void*)0)); | |||
| 501 | ||||
| 502 | ctk_text_buffer_get_bounds (source_buffer, &start_iter, &tmp_iter); | |||
| 503 | ctk_text_buffer_apply_tag_by_name (source_buffer, "source", &start_iter, &tmp_iter); | |||
| 504 | ||||
| 505 | state = STATE_NORMAL; | |||
| 506 | ||||
| 507 | ctk_text_buffer_get_iter_at_offset (source_buffer, &start_iter, 0); | |||
| 508 | ||||
| 509 | next_iter = start_iter; | |||
| 510 | while (ctk_text_iter_forward_line (&next_iter)) | |||
| 511 | { | |||
| 512 | gchar *start_ptr, *end_ptr; | |||
| 513 | gboolean start = TRUE(!(0)); | |||
| 514 | ||||
| 515 | start_ptr = text = ctk_text_iter_get_text (&start_iter, &next_iter); | |||
| 516 | ||||
| 517 | do | |||
| 518 | { | |||
| 519 | parse_chars (start_ptr, &end_ptr, &state, &tag, start); | |||
| 520 | ||||
| 521 | start = FALSE(0); | |||
| 522 | if (end_ptr) | |||
| 523 | { | |||
| 524 | tmp_iter = start_iter; | |||
| 525 | ctk_text_iter_forward_chars (&tmp_iter, end_ptr - start_ptr); | |||
| 526 | } | |||
| 527 | else | |||
| 528 | { | |||
| 529 | tmp_iter = next_iter; | |||
| 530 | } | |||
| 531 | if (tag) | |||
| 532 | ctk_text_buffer_apply_tag_by_name (source_buffer, tag, &start_iter, &tmp_iter); | |||
| 533 | ||||
| 534 | start_iter = tmp_iter; | |||
| 535 | start_ptr = end_ptr; | |||
| 536 | } | |||
| 537 | while (end_ptr); | |||
| 538 | ||||
| 539 | g_free (text); | |||
| 540 | start_iter = next_iter; | |||
| 541 | } | |||
| 542 | } | |||
| 543 | ||||
| 544 | static CtkWidget *create_text (CtkWidget **text_view, gboolean is_source); | |||
| 545 | ||||
| 546 | static void | |||
| 547 | add_data_tab (const gchar *demoname) | |||
| 548 | { | |||
| 549 | gchar *resource_dir; | |||
| 550 | gchar **resources; | |||
| 551 | guint i; | |||
| 552 | ||||
| 553 | resource_dir = g_strconcat ("/", demoname, NULL((void*)0)); | |||
| 554 | resources = g_resources_enumerate_children (resource_dir, 0, NULL((void*)0)); | |||
| 555 | if (resources == NULL((void*)0)) | |||
| 556 | { | |||
| 557 | g_free (resource_dir); | |||
| 558 | return; | |||
| 559 | } | |||
| 560 | ||||
| 561 | for (i = 0; resources[i]; i++) | |||
| 562 | { | |||
| 563 | gchar *resource_name; | |||
| 564 | CtkWidget *widget, *label; | |||
| 565 | ||||
| 566 | resource_name = g_strconcat (resource_dir, "/", resources[i], NULL((void*)0)); | |||
| 567 | ||||
| 568 | widget = ctk_image_new_from_resource (resource_name); | |||
| 569 | if (ctk_image_get_pixbuf (CTK_IMAGE (widget)((((CtkImage*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((widget)), ((ctk_image_get_type ()))))))) == NULL((void*)0) && | |||
| 570 | ctk_image_get_animation (CTK_IMAGE (widget)((((CtkImage*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((widget)), ((ctk_image_get_type ()))))))) == NULL((void*)0)) | |||
| 571 | { | |||
| 572 | GBytes *bytes; | |||
| 573 | ||||
| 574 | /* So we've used the best API available to figure out it's | |||
| 575 | * not an image. Let's try something else then. | |||
| 576 | */ | |||
| 577 | g_object_ref_sink (widget)((__typeof__ (widget)) (g_object_ref_sink) (widget)); | |||
| 578 | g_object_unref (widget); | |||
| 579 | ||||
| 580 | bytes = g_resources_lookup_data (resource_name, 0, NULL((void*)0)); | |||
| 581 | g_assert (bytes)do { if (bytes) ; else g_assertion_message_expr (((gchar*) 0) , "main.c", 581, ((const char*) (__func__)), "bytes"); } while (0); | |||
| 582 | ||||
| 583 | if (g_utf8_validate (g_bytes_get_data (bytes, NULL((void*)0)), g_bytes_get_size (bytes), NULL((void*)0))) | |||
| 584 | { | |||
| 585 | /* Looks like it parses as text. Dump it into a textview then! */ | |||
| 586 | CtkTextBuffer *buffer; | |||
| 587 | CtkWidget *textview; | |||
| 588 | ||||
| 589 | widget = create_text (&textview, FALSE(0)); | |||
| 590 | buffer = ctk_text_buffer_new (NULL((void*)0)); | |||
| 591 | ctk_text_buffer_set_text (buffer, g_bytes_get_data (bytes, NULL((void*)0)), g_bytes_get_size (bytes)); | |||
| 592 | if (g_str_has_suffix (resource_name, ".c")(__builtin_constant_p (".c")? __extension__ ({ const char * const __str = (resource_name); const char * const __suffix = (".c" ); gboolean __result = (0); if (__str == ((void*)0) || __suffix == ((void*)0)) __result = (g_str_has_suffix) (__str, __suffix ); else { const size_t __str_len = strlen (((__str) + !(__str ))); const size_t __suffix_len = strlen (((__suffix) + !(__suffix ))); if (__str_len >= __suffix_len) __result = memcmp (__str + __str_len - __suffix_len, ((__suffix) + !(__suffix)), __suffix_len ) == 0; } __result; }) : (g_str_has_suffix) (resource_name, ".c" ) )) | |||
| 593 | fontify (buffer); | |||
| 594 | ctk_text_view_set_buffer (CTK_TEXT_VIEW (textview)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((textview)), ((ctk_text_view_get_type ())))))), buffer); | |||
| 595 | } | |||
| 596 | else | |||
| 597 | { | |||
| 598 | g_warning ("Don't know how to display resource '%s'", resource_name); | |||
| 599 | widget = NULL((void*)0); | |||
| 600 | } | |||
| 601 | ||||
| 602 | g_bytes_unref (bytes); | |||
| 603 | } | |||
| 604 | ||||
| 605 | ctk_widget_show_all (widget); | |||
| 606 | label = ctk_label_new (resources[i]); | |||
| 607 | ctk_widget_show (label); | |||
| 608 | ctk_notebook_append_page (CTK_NOTEBOOK (notebook)((((CtkNotebook*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((notebook)), ((ctk_notebook_get_type ())))))), widget, label); | |||
| 609 | ctk_container_child_set (CTK_CONTAINER (notebook)((((CtkContainer*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((notebook)), ((ctk_container_get_type ())))))), | |||
| 610 | CTK_WIDGET (widget)((((CtkWidget*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((widget)), ((ctk_widget_get_type ())))))), | |||
| 611 | "tab-expand", TRUE(!(0)), | |||
| 612 | NULL((void*)0)); | |||
| 613 | ||||
| 614 | g_free (resource_name); | |||
| 615 | } | |||
| 616 | ||||
| 617 | g_strfreev (resources); | |||
| 618 | g_free (resource_dir); | |||
| 619 | } | |||
| 620 | ||||
| 621 | static void | |||
| 622 | remove_data_tabs (void) | |||
| 623 | { | |||
| 624 | gint i; | |||
| 625 | ||||
| 626 | for (i = ctk_notebook_get_n_pages (CTK_NOTEBOOK (notebook)((((CtkNotebook*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((notebook)), ((ctk_notebook_get_type ()))))))) - 1; i > 1; i--) | |||
| 627 | ctk_notebook_remove_page (CTK_NOTEBOOK (notebook)((((CtkNotebook*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((notebook)), ((ctk_notebook_get_type ())))))), i); | |||
| 628 | } | |||
| 629 | ||||
| 630 | void | |||
| 631 | load_file (const gchar *demoname, | |||
| 632 | const gchar *filename) | |||
| 633 | { | |||
| 634 | CtkTextBuffer *info_buffer, *source_buffer; | |||
| 635 | CtkTextIter start, end; | |||
| 636 | char *resource_filename; | |||
| 637 | GError *err = NULL((void*)0); | |||
| 638 | int state = 0; | |||
| 639 | gboolean in_para = 0; | |||
| 640 | gchar **lines; | |||
| 641 | GBytes *bytes; | |||
| 642 | gint i; | |||
| 643 | ||||
| 644 | if (!g_strcmp0 (current_file, filename)) | |||
| 645 | return; | |||
| 646 | ||||
| 647 | remove_data_tabs (); | |||
| 648 | ||||
| 649 | add_data_tab (demoname); | |||
| 650 | ||||
| 651 | g_free (current_file); | |||
| 652 | current_file = g_strdup (filename)g_strdup_inline (filename); | |||
| 653 | ||||
| 654 | info_buffer = ctk_text_buffer_new (NULL((void*)0)); | |||
| 655 | ctk_text_buffer_create_tag (info_buffer, "title", | |||
| 656 | "font", "Sans 18", | |||
| 657 | "pixels-below-lines", 10, | |||
| 658 | NULL((void*)0)); | |||
| 659 | ||||
| 660 | source_buffer = ctk_text_buffer_new (NULL((void*)0)); | |||
| 661 | ||||
| 662 | resource_filename = g_strconcat ("/sources/", filename, NULL((void*)0)); | |||
| 663 | bytes = g_resources_lookup_data (resource_filename, 0, &err); | |||
| 664 | g_free (resource_filename); | |||
| 665 | ||||
| 666 | if (bytes == NULL((void*)0)) | |||
| 667 | { | |||
| 668 | g_warning ("Cannot open source for %s: %s", filename, err->message); | |||
| 669 | g_error_free (err); | |||
| 670 | return; | |||
| 671 | } | |||
| 672 | ||||
| 673 | lines = g_strsplit (g_bytes_get_data (bytes, NULL((void*)0)), "\n", -1); | |||
| 674 | g_bytes_unref (bytes); | |||
| 675 | ||||
| 676 | ctk_text_buffer_get_iter_at_offset (info_buffer, &start, 0); | |||
| 677 | for (i = 0; lines[i] != NULL((void*)0); i++) | |||
| 678 | { | |||
| 679 | gchar *p; | |||
| 680 | gchar *q; | |||
| 681 | gchar *r; | |||
| 682 | ||||
| 683 | /* Make sure \r is stripped at the end for the poor windows people */ | |||
| 684 | lines[i] = g_strchomp (lines[i]); | |||
| 685 | ||||
| 686 | p = lines[i]; | |||
| 687 | switch (state) | |||
| 688 | { | |||
| 689 | case 0: | |||
| 690 | /* Reading title */ | |||
| 691 | while (*p == '/' || *p == '*' || g_ascii_isspace (*p)((g_ascii_table[(guchar) (*p)] & G_ASCII_SPACE) != 0)) | |||
| 692 | p++; | |||
| 693 | r = p; | |||
| 694 | while (*r != '\0') | |||
| 695 | { | |||
| 696 | while (*r != '/' && *r != ':' && *r != '\0') | |||
| 697 | r++; | |||
| 698 | if (*r == '/') | |||
| 699 | { | |||
| 700 | r++; | |||
| 701 | p = r; | |||
| 702 | } | |||
| 703 | if (r[0] == ':' && r[1] == ':') | |||
| 704 | *r = '\0'; | |||
| 705 | } | |||
| 706 | q = p + strlen (p); | |||
| 707 | while (q > p && g_ascii_isspace (*(q - 1))((g_ascii_table[(guchar) (*(q - 1))] & G_ASCII_SPACE) != 0 )) | |||
| 708 | q--; | |||
| 709 | ||||
| 710 | ||||
| 711 | if (q > p) | |||
| 712 | { | |||
| 713 | int len_chars = g_utf8_pointer_to_offset (p, q); | |||
| 714 | ||||
| 715 | end = start; | |||
| 716 | ||||
| 717 | g_assert (strlen (p) >= q - p)do { if (strlen (p) >= q - p) ; else g_assertion_message_expr (((gchar*) 0), "main.c", 717, ((const char*) (__func__)), "strlen (p) >= q - p" ); } while (0); | |||
| 718 | ctk_text_buffer_insert (info_buffer, &end, p, q - p); | |||
| 719 | start = end; | |||
| 720 | ||||
| 721 | ctk_text_iter_backward_chars (&start, len_chars); | |||
| 722 | ctk_text_buffer_apply_tag_by_name (info_buffer, "title", &start, &end); | |||
| 723 | ||||
| 724 | start = end; | |||
| 725 | ||||
| 726 | while (*p && *p != '\n') p++; | |||
| 727 | ||||
| 728 | state++; | |||
| 729 | } | |||
| 730 | break; | |||
| 731 | ||||
| 732 | case 1: | |||
| 733 | /* Reading body of info section */ | |||
| 734 | while (g_ascii_isspace (*p)((g_ascii_table[(guchar) (*p)] & G_ASCII_SPACE) != 0)) | |||
| 735 | p++; | |||
| 736 | if (*p == '*' && *(p + 1) == '/') | |||
| 737 | { | |||
| 738 | ctk_text_buffer_get_iter_at_offset (source_buffer, &start, 0); | |||
| 739 | state++; | |||
| 740 | } | |||
| 741 | else | |||
| 742 | { | |||
| 743 | int len; | |||
| 744 | ||||
| 745 | while (*p == '*' || g_ascii_isspace (*p)((g_ascii_table[(guchar) (*p)] & G_ASCII_SPACE) != 0)) | |||
| 746 | p++; | |||
| 747 | ||||
| 748 | len = strlen (p); | |||
| 749 | while (g_ascii_isspace (*(p + len - 1))((g_ascii_table[(guchar) (*(p + len - 1))] & G_ASCII_SPACE ) != 0)) | |||
| 750 | len--; | |||
| 751 | ||||
| 752 | if (len > 0) | |||
| 753 | { | |||
| 754 | if (in_para) | |||
| 755 | ctk_text_buffer_insert (info_buffer, &start, " ", 1); | |||
| 756 | ||||
| 757 | g_assert (strlen (p) >= len)do { if (strlen (p) >= len) ; else g_assertion_message_expr (((gchar*) 0), "main.c", 757, ((const char*) (__func__)), "strlen (p) >= len" ); } while (0); | |||
| 758 | ctk_text_buffer_insert (info_buffer, &start, p, len); | |||
| 759 | in_para = 1; | |||
| 760 | } | |||
| 761 | else | |||
| 762 | { | |||
| 763 | ctk_text_buffer_insert (info_buffer, &start, "\n", 1); | |||
| 764 | in_para = 0; | |||
| 765 | } | |||
| 766 | } | |||
| 767 | break; | |||
| 768 | ||||
| 769 | case 2: | |||
| 770 | /* Skipping blank lines */ | |||
| 771 | while (g_ascii_isspace (*p)((g_ascii_table[(guchar) (*p)] & G_ASCII_SPACE) != 0)) | |||
| 772 | p++; | |||
| 773 | ||||
| 774 | if (!*p) | |||
| 775 | break; | |||
| 776 | ||||
| 777 | p = lines[i]; | |||
| 778 | state++; | |||
| 779 | /* Fall through */ | |||
| 780 | ||||
| 781 | case 3: | |||
| 782 | /* Reading program body */ | |||
| 783 | ctk_text_buffer_insert (source_buffer, &start, p, -1); | |||
| 784 | if (lines[i+1] != NULL((void*)0)) | |||
| 785 | ctk_text_buffer_insert (source_buffer, &start, "\n", 1); | |||
| 786 | break; | |||
| 787 | } | |||
| 788 | } | |||
| 789 | ||||
| 790 | g_strfreev (lines); | |||
| 791 | ||||
| 792 | fontify (source_buffer); | |||
| 793 | ||||
| 794 | ctk_text_view_set_buffer (CTK_TEXT_VIEW (source_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((source_view)), ((ctk_text_view_get_type ())))))), source_buffer); | |||
| 795 | g_object_unref (source_buffer); | |||
| 796 | ||||
| 797 | ctk_text_view_set_buffer (CTK_TEXT_VIEW (info_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((info_view)), ((ctk_text_view_get_type ())))))), info_buffer); | |||
| 798 | g_object_unref (info_buffer); | |||
| 799 | } | |||
| 800 | ||||
| 801 | static void | |||
| 802 | selection_cb (CtkTreeSelection *selection, | |||
| 803 | CtkTreeModel *model) | |||
| 804 | { | |||
| 805 | CtkTreeIter iter; | |||
| 806 | char *name; | |||
| 807 | char *filename; | |||
| 808 | char *title; | |||
| 809 | ||||
| 810 | if (! ctk_tree_selection_get_selected (selection, NULL((void*)0), &iter)) | |||
| 811 | return; | |||
| 812 | ||||
| 813 | ctk_tree_model_get (model, &iter, | |||
| 814 | NAME_COLUMN, &name, | |||
| 815 | TITLE_COLUMN, &title, | |||
| 816 | FILENAME_COLUMN, &filename, | |||
| 817 | -1); | |||
| 818 | ||||
| 819 | if (filename) | |||
| 820 | load_file (name, filename); | |||
| 821 | ||||
| 822 | ctk_header_bar_set_title (CTK_HEADER_BAR (headerbar)((((CtkHeaderBar*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((headerbar)), ((ctk_header_bar_get_type ())))))), title); | |||
| 823 | ||||
| 824 | g_free (name); | |||
| 825 | g_free (title); | |||
| 826 | g_free (filename); | |||
| 827 | } | |||
| 828 | ||||
| 829 | static CtkWidget * | |||
| 830 | create_text (CtkWidget **view, | |||
| 831 | gboolean is_source) | |||
| 832 | { | |||
| 833 | CtkWidget *scrolled_window; | |||
| 834 | CtkWidget *text_view; | |||
| 835 | ||||
| 836 | scrolled_window = ctk_scrolled_window_new (NULL((void*)0), NULL((void*)0)); | |||
| 837 | ctk_scrolled_window_set_policy (CTK_SCROLLED_WINDOW (scrolled_window)((((CtkScrolledWindow*) (void *) g_type_check_instance_cast ( (GTypeInstance*) ((scrolled_window)), ((ctk_scrolled_window_get_type ())))))), | |||
| 838 | CTK_POLICY_AUTOMATIC, | |||
| 839 | CTK_POLICY_AUTOMATIC); | |||
| 840 | ctk_scrolled_window_set_shadow_type (CTK_SCROLLED_WINDOW (scrolled_window)((((CtkScrolledWindow*) (void *) g_type_check_instance_cast ( (GTypeInstance*) ((scrolled_window)), ((ctk_scrolled_window_get_type ())))))), | |||
| 841 | CTK_SHADOW_NONE); | |||
| 842 | ||||
| 843 | *view = text_view = ctk_text_view_new (); | |||
| 844 | g_object_set (text_view, | |||
| 845 | "left-margin", 20, | |||
| 846 | "right-margin", 20, | |||
| 847 | "top-margin", 20, | |||
| 848 | "bottom-margin", 20, | |||
| 849 | NULL((void*)0)); | |||
| 850 | ||||
| 851 | ctk_text_view_set_editable (CTK_TEXT_VIEW (text_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((text_view)), ((ctk_text_view_get_type ())))))), FALSE(0)); | |||
| 852 | ctk_text_view_set_cursor_visible (CTK_TEXT_VIEW (text_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((text_view)), ((ctk_text_view_get_type ())))))), FALSE(0)); | |||
| 853 | ||||
| 854 | ctk_container_add (CTK_CONTAINER (scrolled_window)((((CtkContainer*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((scrolled_window)), ((ctk_container_get_type ())))))), text_view); | |||
| 855 | ||||
| 856 | if (is_source) | |||
| 857 | { | |||
| 858 | ctk_text_view_set_monospace (CTK_TEXT_VIEW (text_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((text_view)), ((ctk_text_view_get_type ())))))), TRUE(!(0))); | |||
| 859 | ctk_text_view_set_wrap_mode (CTK_TEXT_VIEW (text_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((text_view)), ((ctk_text_view_get_type ())))))), CTK_WRAP_NONE); | |||
| 860 | } | |||
| 861 | else | |||
| 862 | { | |||
| 863 | /* Make it a bit nicer for text. */ | |||
| 864 | ctk_text_view_set_wrap_mode (CTK_TEXT_VIEW (text_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((text_view)), ((ctk_text_view_get_type ())))))), CTK_WRAP_WORD); | |||
| 865 | ctk_text_view_set_pixels_above_lines (CTK_TEXT_VIEW (text_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((text_view)), ((ctk_text_view_get_type ())))))), 2); | |||
| 866 | ctk_text_view_set_pixels_below_lines (CTK_TEXT_VIEW (text_view)((((CtkTextView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((text_view)), ((ctk_text_view_get_type ())))))), 2); | |||
| 867 | } | |||
| 868 | ||||
| 869 | return scrolled_window; | |||
| 870 | } | |||
| 871 | ||||
| 872 | static void | |||
| 873 | populate_model (CtkTreeModel *model) | |||
| 874 | { | |||
| 875 | Demo *d = ctk_demos; | |||
| 876 | ||||
| 877 | /* this code only supports 1 level of children. If we | |||
| 878 | * want more we probably have to use a recursing function. | |||
| 879 | */ | |||
| 880 | while (d->title) | |||
| 881 | { | |||
| 882 | Demo *children = d->children; | |||
| 883 | CtkTreeIter iter; | |||
| 884 | ||||
| 885 | ctk_tree_store_append (CTK_TREE_STORE (model)((((CtkTreeStore*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((model)), ((ctk_tree_store_get_type ())))))), &iter, NULL((void*)0)); | |||
| 886 | ||||
| 887 | ctk_tree_store_set (CTK_TREE_STORE (model)((((CtkTreeStore*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((model)), ((ctk_tree_store_get_type ())))))), | |||
| 888 | &iter, | |||
| 889 | NAME_COLUMN, d->name, | |||
| 890 | TITLE_COLUMN, d->title, | |||
| 891 | FILENAME_COLUMN, d->filename, | |||
| 892 | FUNC_COLUMN, d->func, | |||
| 893 | STYLE_COLUMN, PANGO_STYLE_NORMAL, | |||
| 894 | -1); | |||
| 895 | ||||
| 896 | d++; | |||
| 897 | ||||
| 898 | if (!children) | |||
| 899 | continue; | |||
| 900 | ||||
| 901 | while (children->title) | |||
| 902 | { | |||
| 903 | CtkTreeIter child_iter; | |||
| 904 | ||||
| 905 | ctk_tree_store_append (CTK_TREE_STORE (model)((((CtkTreeStore*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((model)), ((ctk_tree_store_get_type ())))))), &child_iter, &iter); | |||
| 906 | ||||
| 907 | ctk_tree_store_set (CTK_TREE_STORE (model)((((CtkTreeStore*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((model)), ((ctk_tree_store_get_type ())))))), | |||
| 908 | &child_iter, | |||
| 909 | NAME_COLUMN, children->name, | |||
| 910 | TITLE_COLUMN, children->title, | |||
| 911 | FILENAME_COLUMN, children->filename, | |||
| 912 | FUNC_COLUMN, children->func, | |||
| 913 | STYLE_COLUMN, PANGO_STYLE_NORMAL, | |||
| 914 | -1); | |||
| 915 | ||||
| 916 | children++; | |||
| 917 | } | |||
| 918 | } | |||
| 919 | ||||
| 920 | } | |||
| 921 | ||||
| 922 | static void | |||
| 923 | startup (GApplication *app) | |||
| 924 | { | |||
| 925 | CtkBuilder *builder; | |||
| 926 | GMenuModel *appmenu; | |||
| 927 | gchar *ids[] = { "appmenu", NULL((void*)0) }; | |||
| 928 | ||||
| 929 | builder = ctk_builder_new (); | |||
| 930 | ctk_builder_add_objects_from_resource (builder, "/ui/appmenu.ui", ids, NULL((void*)0)); | |||
| 931 | ||||
| 932 | appmenu = (GMenuModel *)ctk_builder_get_object (builder, "appmenu"); | |||
| 933 | ||||
| 934 | ctk_application_set_app_menu (CTK_APPLICATION (app)((((CtkApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((ctk_application_get_type ())))))), appmenu); | |||
| 935 | ||||
| 936 | g_object_unref (builder); | |||
| 937 | } | |||
| 938 | ||||
| 939 | static void | |||
| 940 | row_activated_cb (CtkWidget *tree_view, | |||
| 941 | CtkTreePath *path, | |||
| 942 | CtkTreeViewColumn *column G_GNUC_UNUSED__attribute__ ((__unused__))) | |||
| 943 | { | |||
| 944 | CtkTreeIter iter; | |||
| 945 | CtkWidget *window; | |||
| 946 | CtkTreeModel *model; | |||
| 947 | ||||
| 948 | window = ctk_widget_get_toplevel (tree_view); | |||
| 949 | model = ctk_tree_view_get_model (CTK_TREE_VIEW (tree_view)((((CtkTreeView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((tree_view)), ((ctk_tree_view_get_type ()))))))); | |||
| 950 | ctk_tree_model_get_iter (model, &iter, path); | |||
| 951 | ||||
| 952 | run_example_for_row (window, model, &iter); | |||
| 953 | } | |||
| 954 | ||||
| 955 | static void | |||
| 956 | start_cb (CtkMenuItem *item G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 957 | CtkWidget *scrollbar) | |||
| 958 | { | |||
| 959 | CtkAdjustment *adj; | |||
| 960 | ||||
| 961 | adj = ctk_range_get_adjustment (CTK_RANGE (scrollbar)((((CtkRange*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((scrollbar)), ((ctk_range_get_type ()))))))); | |||
| 962 | ctk_adjustment_set_value (adj, ctk_adjustment_get_lower (adj)); | |||
| 963 | } | |||
| 964 | ||||
| 965 | static void | |||
| 966 | end_cb (CtkMenuItem *item G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 967 | CtkWidget *scrollbar) | |||
| 968 | { | |||
| 969 | CtkAdjustment *adj; | |||
| 970 | ||||
| 971 | adj = ctk_range_get_adjustment (CTK_RANGE (scrollbar)((((CtkRange*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((scrollbar)), ((ctk_range_get_type ()))))))); | |||
| 972 | ctk_adjustment_set_value (adj, ctk_adjustment_get_upper (adj) - ctk_adjustment_get_page_size (adj)); | |||
| 973 | } | |||
| 974 | ||||
| 975 | static gboolean | |||
| 976 | scrollbar_popup (CtkWidget *scrollbar G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 977 | CtkWidget *menu) | |||
| 978 | { | |||
| 979 | ctk_menu_popup_at_pointer (CTK_MENU (menu)((((CtkMenu*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((menu)), ((ctk_menu_get_type ())))))), NULL((void*)0)); | |||
| 980 | ||||
| 981 | return TRUE(!(0)); | |||
| 982 | } | |||
| 983 | ||||
| 984 | static void | |||
| 985 | activate (GApplication *app) | |||
| 986 | { | |||
| 987 | CtkBuilder *builder; | |||
| 988 | CtkWindow *window; | |||
| 989 | CtkWidget *widget; | |||
| 990 | CtkTreeModel *model; | |||
| 991 | CtkTreeIter iter; | |||
| 992 | GError *error = NULL((void*)0); | |||
| 993 | CtkWidget *sw; | |||
| 994 | CtkWidget *scrollbar; | |||
| 995 | CtkWidget *menu; | |||
| 996 | CtkWidget *item; | |||
| 997 | ||||
| 998 | static GActionEntry win_entries[] = { | |||
| 999 | { .name = "run", .activate = activate_run } | |||
| 1000 | }; | |||
| 1001 | ||||
| 1002 | builder = ctk_builder_new (); | |||
| 1003 | ctk_builder_add_from_resource (builder, "/ui/main.ui", &error); | |||
| 1004 | if (error != NULL((void*)0)) | |||
| 1005 | { | |||
| 1006 | g_critical ("%s", error->message); | |||
| 1007 | exit (1); | |||
| 1008 | } | |||
| 1009 | ||||
| 1010 | window = (CtkWindow *)ctk_builder_get_object (builder, "window"); | |||
| 1011 | ctk_application_add_window (CTK_APPLICATION (app)((((CtkApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((ctk_application_get_type ())))))), window); | |||
| 1012 | g_action_map_add_action_entries (G_ACTION_MAP (window)((((GActionMap*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((window)), ((g_action_map_get_type ())))))), | |||
| 1013 | win_entries, G_N_ELEMENTS (win_entries)(sizeof (win_entries) / sizeof ((win_entries)[0])), | |||
| 1014 | window); | |||
| 1015 | ||||
| 1016 | notebook = (CtkWidget *)ctk_builder_get_object (builder, "notebook"); | |||
| 1017 | ||||
| 1018 | info_view = (CtkWidget *)ctk_builder_get_object (builder, "info-textview"); | |||
| 1019 | source_view = (CtkWidget *)ctk_builder_get_object (builder, "source-textview"); | |||
| 1020 | headerbar = (CtkWidget *)ctk_builder_get_object (builder, "headerbar"); | |||
| 1021 | treeview = (CtkWidget *)ctk_builder_get_object (builder, "treeview"); | |||
| 1022 | model = ctk_tree_view_get_model (CTK_TREE_VIEW (treeview)((((CtkTreeView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((treeview)), ((ctk_tree_view_get_type ()))))))); | |||
| 1023 | ||||
| 1024 | sw = (CtkWidget *)ctk_builder_get_object (builder, "source-scrolledwindow"); | |||
| 1025 | scrollbar = ctk_scrolled_window_get_vscrollbar (CTK_SCROLLED_WINDOW (sw)((((CtkScrolledWindow*) (void *) g_type_check_instance_cast ( (GTypeInstance*) ((sw)), ((ctk_scrolled_window_get_type ()))) )))); | |||
| 1026 | ||||
| 1027 | menu = ctk_menu_new (); | |||
| 1028 | ||||
| 1029 | item = ctk_menu_item_new_with_label ("Start"); | |||
| 1030 | g_signal_connect (item, "activate", G_CALLBACK (start_cb), scrollbar)g_signal_connect_data ((item), ("activate"), (((GCallback) (start_cb ))), (scrollbar), ((void*)0), (GConnectFlags) 0); | |||
| 1031 | ctk_menu_shell_append (CTK_MENU_SHELL (menu)((((CtkMenuShell*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((menu)), ((ctk_menu_shell_get_type ())))))), item); | |||
| 1032 | ||||
| 1033 | item = ctk_menu_item_new_with_label ("End"); | |||
| 1034 | g_signal_connect (item, "activate", G_CALLBACK (end_cb), scrollbar)g_signal_connect_data ((item), ("activate"), (((GCallback) (end_cb ))), (scrollbar), ((void*)0), (GConnectFlags) 0); | |||
| 1035 | ctk_menu_shell_append (CTK_MENU_SHELL (menu)((((CtkMenuShell*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((menu)), ((ctk_menu_shell_get_type ())))))), item); | |||
| 1036 | ||||
| 1037 | ctk_widget_show_all (menu); | |||
| 1038 | ||||
| 1039 | g_signal_connect (scrollbar, "popup-menu", G_CALLBACK (scrollbar_popup), menu)g_signal_connect_data ((scrollbar), ("popup-menu"), (((GCallback ) (scrollbar_popup))), (menu), ((void*)0), (GConnectFlags) 0); | |||
| 1040 | ||||
| 1041 | load_file (ctk_demos[0].name, ctk_demos[0].filename); | |||
| 1042 | ||||
| 1043 | populate_model (model); | |||
| 1044 | ||||
| 1045 | g_signal_connect (treeview, "row-activated", G_CALLBACK (row_activated_cb), model)g_signal_connect_data ((treeview), ("row-activated"), (((GCallback ) (row_activated_cb))), (model), ((void*)0), (GConnectFlags) 0 ); | |||
| 1046 | ||||
| 1047 | widget = (CtkWidget *)ctk_builder_get_object (builder, "treeview-selection"); | |||
| 1048 | g_signal_connect (widget, "changed", G_CALLBACK (selection_cb), model)g_signal_connect_data ((widget), ("changed"), (((GCallback) ( selection_cb))), (model), ((void*)0), (GConnectFlags) 0); | |||
| 1049 | ||||
| 1050 | ctk_tree_model_get_iter_first (ctk_tree_view_get_model (CTK_TREE_VIEW (treeview)((((CtkTreeView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((treeview)), ((ctk_tree_view_get_type ()))))))), &iter); | |||
| 1051 | ctk_tree_selection_select_iter (CTK_TREE_SELECTION (widget)((((CtkTreeSelection*) (void *) g_type_check_instance_cast (( GTypeInstance*) ((widget)), ((ctk_tree_selection_get_type ()) ))))), &iter); | |||
| 1052 | ||||
| 1053 | ctk_tree_view_collapse_all (CTK_TREE_VIEW (treeview)((((CtkTreeView*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((treeview)), ((ctk_tree_view_get_type ()))))))); | |||
| 1054 | ||||
| 1055 | ctk_widget_show_all (CTK_WIDGET (window)((((CtkWidget*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((window)), ((ctk_widget_get_type ()))))))); | |||
| 1056 | ||||
| 1057 | g_object_unref (builder); | |||
| 1058 | } | |||
| 1059 | ||||
| 1060 | static gboolean | |||
| 1061 | auto_quit (gpointer data) | |||
| 1062 | { | |||
| 1063 | g_application_quit (G_APPLICATION (data)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((data)), ((g_application_get_type ()))))))); | |||
| 1064 | return G_SOURCE_REMOVE(0); | |||
| 1065 | } | |||
| 1066 | ||||
| 1067 | static void | |||
| 1068 | list_demos (void) | |||
| 1069 | { | |||
| 1070 | Demo *d; | |||
| 1071 | ||||
| 1072 | d = ctk_demos; | |||
| 1073 | ||||
| 1074 | while (d->title) | |||
| 1075 | { | |||
| 1076 | Demo *c; | |||
| 1077 | ||||
| 1078 | c = d->children; | |||
| 1079 | if (d->name) | |||
| 1080 | g_print ("%s\n", d->name); | |||
| 1081 | d++; | |||
| 1082 | while (c && c->title) | |||
| 1083 | { | |||
| 1084 | if (c->name) | |||
| 1085 | g_print ("%s\n", c->name); | |||
| 1086 | c++; | |||
| 1087 | } | |||
| 1088 | } | |||
| 1089 | } | |||
| 1090 | ||||
| 1091 | static gint | |||
| 1092 | command_line (GApplication *app, | |||
| 1093 | GApplicationCommandLine *cmdline) | |||
| 1094 | { | |||
| 1095 | GVariantDict *options; | |||
| 1096 | const gchar *name = NULL((void*)0); | |||
| 1097 | gboolean autoquit = FALSE(0); | |||
| 1098 | gboolean list = FALSE(0); | |||
| 1099 | Demo *d, *c; | |||
| 1100 | GDoDemoFunc func = 0; | |||
| 1101 | CtkWidget *window, *demo; | |||
| 1102 | ||||
| 1103 | activate (app); | |||
| ||||
| 1104 | ||||
| 1105 | options = g_application_command_line_get_options_dict (cmdline); | |||
| 1106 | g_variant_dict_lookup (options, "run", "&s", &name); | |||
| 1107 | g_variant_dict_lookup (options, "autoquit", "b", &autoquit); | |||
| 1108 | g_variant_dict_lookup (options, "list", "b", &list); | |||
| 1109 | ||||
| 1110 | if (list) | |||
| 1111 | { | |||
| 1112 | list_demos (); | |||
| 1113 | g_application_quit (app); | |||
| 1114 | return 0; | |||
| 1115 | } | |||
| 1116 | ||||
| 1117 | if (name == NULL((void*)0)) | |||
| 1118 | goto out; | |||
| 1119 | ||||
| 1120 | window = ctk_application_get_windows (CTK_APPLICATION (app)((((CtkApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((ctk_application_get_type ())))))))->data; | |||
| 1121 | ||||
| 1122 | d = ctk_demos; | |||
| 1123 | ||||
| 1124 | while (d->title) | |||
| 1125 | { | |||
| 1126 | c = d->children; | |||
| 1127 | if (g_strcmp0 (d->name, name) == 0) | |||
| 1128 | { | |||
| 1129 | func = d->func; | |||
| 1130 | goto out; | |||
| 1131 | } | |||
| 1132 | d++; | |||
| 1133 | while (c && c->title) | |||
| 1134 | { | |||
| 1135 | if (g_strcmp0 (c->name, name) == 0) | |||
| 1136 | { | |||
| 1137 | func = c->func; | |||
| 1138 | goto out; | |||
| 1139 | } | |||
| 1140 | c++; | |||
| 1141 | } | |||
| 1142 | } | |||
| 1143 | ||||
| 1144 | out: | |||
| 1145 | if (func) | |||
| 1146 | { | |||
| 1147 | demo = (func) (window); | |||
| 1148 | ||||
| 1149 | ctk_window_set_transient_for (CTK_WINDOW (demo)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((demo)), ((ctk_window_get_type ())))))), CTK_WINDOW (window)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((window)), ((ctk_window_get_type ()))))))); | |||
| 1150 | ctk_window_set_modal (CTK_WINDOW (demo)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((demo)), ((ctk_window_get_type ())))))), TRUE(!(0))); | |||
| 1151 | } | |||
| 1152 | ||||
| 1153 | if (autoquit) | |||
| 1154 | g_timeout_add_seconds (1, auto_quit, app); | |||
| 1155 | ||||
| 1156 | return 0; | |||
| 1157 | } | |||
| 1158 | ||||
| 1159 | static void | |||
| 1160 | print_version (void) | |||
| 1161 | { | |||
| 1162 | g_print ("ctk3-demo %d.%d.%d\n", | |||
| 1163 | ctk_get_major_version (), | |||
| 1164 | ctk_get_minor_version (), | |||
| 1165 | ctk_get_micro_version ()); | |||
| 1166 | } | |||
| 1167 | ||||
| 1168 | static int | |||
| 1169 | local_options (GApplication *app G_GNUC_UNUSED__attribute__ ((__unused__)), | |||
| 1170 | GVariantDict *options, | |||
| 1171 | gpointer data G_GNUC_UNUSED__attribute__ ((__unused__))) | |||
| 1172 | { | |||
| 1173 | gboolean version = FALSE(0); | |||
| 1174 | ||||
| 1175 | g_variant_dict_lookup (options, "version", "b", &version); | |||
| 1176 | ||||
| 1177 | if (version) | |||
| 1178 | { | |||
| 1179 | print_version (); | |||
| 1180 | return 0; | |||
| 1181 | } | |||
| 1182 | ||||
| 1183 | return -1; | |||
| 1184 | } | |||
| 1185 | ||||
| 1186 | int | |||
| 1187 | main (int argc, char **argv) | |||
| 1188 | { | |||
| 1189 | CtkApplication *app; | |||
| 1190 | static GActionEntry app_entries[] = { | |||
| 1191 | { .name = "about", .activate = activate_about }, | |||
| 1192 | { .name = "quit", .activate = activate_quit }, | |||
| 1193 | }; | |||
| 1194 | ||||
| 1195 | /* Most code in ctk-demo is intended to be exemplary, but not | |||
| 1196 | * these few lines, which are just a hack so ctk-demo will work | |||
| 1197 | * in the CTK tree without installing it. | |||
| 1198 | */ | |||
| 1199 | if (g_file_test ("../../modules/input/immodules.cache", G_FILE_TEST_EXISTS)) | |||
| 1200 | { | |||
| 1201 | g_setenv ("CTK_IM_MODULE_FILE", "../../modules/input/immodules.cache", TRUE(!(0))); | |||
| 1202 | } | |||
| 1203 | /* -- End of hack -- */ | |||
| 1204 | ||||
| 1205 | app = ctk_application_new ("org.ctk.Demo", G_APPLICATION_NON_UNIQUE|G_APPLICATION_HANDLES_COMMAND_LINE); | |||
| 1206 | ||||
| 1207 | g_action_map_add_action_entries (G_ACTION_MAP (app)((((GActionMap*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((g_action_map_get_type ())))))), | |||
| 1208 | app_entries, G_N_ELEMENTS (app_entries)(sizeof (app_entries) / sizeof ((app_entries)[0])), | |||
| 1209 | app); | |||
| 1210 | ||||
| 1211 | g_application_add_main_option (G_APPLICATION (app)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((g_application_get_type ())))))), "version", 0, 0, G_OPTION_ARG_NONE, "Show program version", NULL((void*)0)); | |||
| 1212 | g_application_add_main_option (G_APPLICATION (app)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((g_application_get_type ())))))), "run", 0, 0, G_OPTION_ARG_STRING, "Run an example", "EXAMPLE"); | |||
| 1213 | g_application_add_main_option (G_APPLICATION (app)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((g_application_get_type ())))))), "list", 0, 0, G_OPTION_ARG_NONE, "List examples", NULL((void*)0)); | |||
| 1214 | g_application_add_main_option (G_APPLICATION (app)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((g_application_get_type ())))))), "autoquit", 0, 0, G_OPTION_ARG_NONE, "Quit after a delay", NULL((void*)0)); | |||
| 1215 | ||||
| 1216 | g_signal_connect (app, "startup", G_CALLBACK (startup), NULL)g_signal_connect_data ((app), ("startup"), (((GCallback) (startup ))), (((void*)0)), ((void*)0), (GConnectFlags) 0); | |||
| 1217 | g_signal_connect (app, "activate", G_CALLBACK (activate), NULL)g_signal_connect_data ((app), ("activate"), (((GCallback) (activate ))), (((void*)0)), ((void*)0), (GConnectFlags) 0); | |||
| 1218 | g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL)g_signal_connect_data ((app), ("command-line"), (((GCallback) (command_line))), (((void*)0)), ((void*)0), (GConnectFlags) 0 ); | |||
| 1219 | g_signal_connect (app, "handle-local-options", G_CALLBACK (local_options), NULL)g_signal_connect_data ((app), ("handle-local-options"), (((GCallback ) (local_options))), (((void*)0)), ((void*)0), (GConnectFlags ) 0); | |||
| 1220 | ||||
| 1221 | g_application_run (G_APPLICATION (app)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((app)), ((g_application_get_type ())))))), argc, argv); | |||
| 1222 | ||||
| 1223 | return 0; | |||
| 1224 | } |