OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
editview.h File Reference
#include <gtk/gtk.h>
#include <gtksourceview/gtksourcelanguagemanager.h>
#include <gtksourceview/gtksourcestyleschememanager.h>
#include "mape/forward.h"
#include "mape/mapgen.h"
Include dependency graph for editview.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  MapeEditView_
 

Typedefs

typedef enum MapeEditViewError_ MapeEditViewError
 

Enumerations

enum  MapeEditViewError_ { MAPE_EDIT_VIEW_ERROR_MISSING_MAPS, MAPE_EDIT_VIEW_ERROR_UNKNOWN_ENCODING, MAPE_EDIT_VIEW_ERROR_FAILED }
 

Functions

MapeEditViewmape_edit_view_new (MapePreView *pre_view, MapeStatusbar *statusbar, GError **error)
 
void mape_edit_view_destroy (MapeEditView *view)
 
void mape_edit_view_clear (MapeEditView *view)
 
gboolean mape_edit_view_open (MapeEditView *view, const gchar *filename, GError **error)
 
gboolean mape_edit_view_save (MapeEditView *view, const gchar *filename, GError **error)
 
gboolean mape_edit_view_get_modified (MapeEditView *view)
 
void mape_edit_view_undo (MapeEditView *edit_view)
 
void mape_edit_view_redo (MapeEditView *edit_view)
 
void mape_edit_view_apply_preferences (MapeEditView *edit_view, MapePreferences *preferences)
 
void mape_edit_view_reload (MapeEditView *edit_view)
 

Class Documentation

struct MapeEditView_

Definition at line 31 of file editview.h.

Collaboration diagram for MapeEditView_:
[legend]
Class Members
const gchar * encoding
gchar * file_path
gboolean fixed_seed
PangoFontDescription * font_desc
GtkSourceLanguageManager * lang_manager
unsigned int map_height
unsigned int map_width
double map_zoom
MapePreView * pre_view
unsigned int random_seed
GThread * render_thread
gboolean rerender
MapeStatusbar * statusbar
GtkSourceStyleSchemeManager * style_manager
MapeMapgenType type
GtkWidget * view
GtkWidget * window

Typedef Documentation

Enumeration Type Documentation

Enumerator
MAPE_EDIT_VIEW_ERROR_MISSING_MAPS 
MAPE_EDIT_VIEW_ERROR_UNKNOWN_ENCODING 
MAPE_EDIT_VIEW_ERROR_FAILED 

Definition at line 25 of file editview.h.

Function Documentation

void mape_edit_view_apply_preferences ( MapeEditView edit_view,
MapePreferences preferences 
)

Definition at line 704 of file editview.c.

References MapePreferences_::auto_indentation, MapePreferences_::bracket_matching, MapePreferences_::fixed_seed, MapeEditView_::fixed_seed, MapePreferences_::highlight_line, MapePreferences_::line_numbers, MapePreferences_::map_height, MapeEditView_::map_height, MapePreferences_::map_width, MapeEditView_::map_width, MapePreferences_::map_zoom, MapeEditView_::map_zoom, mape_edit_view_reload(), MapePreferences_::random_seed, MapeEditView_::random_seed, MapePreferences_::tab_to_spaces, MapePreferences_::tab_width, MapePreferences_::text_wrapping, and MapeEditView_::view.

Referenced by mape_window_new().

706 {
707  GtkSourceView* view;
708  GtkSourceBuffer* buffer;
709  GtkWrapMode wrap_mode;
710 
711  view = GTK_SOURCE_VIEW(edit_view->view);
712  buffer = GTK_SOURCE_BUFFER(
713  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view))
714  );
715 
716  gtk_source_view_set_tab_width(
717  view,
718  preferences->tab_width
719  );
720 
721  gtk_source_view_set_insert_spaces_instead_of_tabs(
722  view,
723  preferences->tab_to_spaces
724  );
725 
726  gtk_source_view_set_auto_indent(
727  view,
728  preferences->auto_indentation
729  );
730 
731  wrap_mode = GTK_WRAP_CHAR;
732  if(preferences->text_wrapping == FALSE)
733  wrap_mode = GTK_WRAP_NONE;
734  gtk_text_view_set_wrap_mode(
735  GTK_TEXT_VIEW(view),
736  wrap_mode
737  );
738 
739  gtk_source_view_set_show_line_numbers(
740  view,
741  preferences->line_numbers
742  );
743 
744  gtk_source_view_set_highlight_current_line(
745  view,
746  preferences->highlight_line
747  );
748 
749  gtk_source_buffer_set_highlight_matching_brackets(
750  buffer,
751  preferences->bracket_matching
752  );
753 
754  edit_view->fixed_seed = preferences->fixed_seed;
755  edit_view->random_seed = preferences->random_seed;
756  edit_view->map_width = preferences->map_width;
757  edit_view->map_height = preferences->map_height;
758  edit_view->map_zoom = preferences->map_zoom;
759 
760  /* Rerender with new random settings */
761  mape_edit_view_reload(edit_view);
762 }
unsigned int map_width
Definition: editview.h:48
gboolean line_numbers
Definition: preferences.h:27
unsigned int tab_width
Definition: preferences.h:23
gboolean highlight_line
Definition: preferences.h:28
double map_zoom
Definition: editview.h:50
gboolean tab_to_spaces
Definition: preferences.h:24
void mape_edit_view_reload(MapeEditView *edit_view)
Definition: editview.c:764
unsigned int map_height
Definition: editview.h:49
gboolean auto_indentation
Definition: preferences.h:25
GtkWidget * view
Definition: editview.h:33
gboolean fixed_seed
Definition: editview.h:45
gboolean text_wrapping
Definition: preferences.h:26
unsigned int random_seed
Definition: preferences.h:31
gboolean bracket_matching
Definition: preferences.h:29
unsigned int map_width
Definition: preferences.h:32
unsigned int random_seed
Definition: editview.h:46
gboolean fixed_seed
Definition: preferences.h:30
unsigned int map_height
Definition: preferences.h:33

Here is the call graph for this function:

Here is the caller graph for this function:

void mape_edit_view_clear ( MapeEditView view)

Definition at line 526 of file editview.c.

References MapeEditView_::file_path, and MapeEditView_::view.

527 {
528  g_free(view->file_path);
529  view->file_path = NULL;
530 
531  /* TODO: Undoable action dingsen */
532  /* (statische mape_edit_view_set_contents-Call?) */
533  gtk_text_buffer_set_text(
534  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view)),
535  "",
536  0
537  );
538 
539  gtk_text_buffer_set_modified(
540  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view)),
541  FALSE
542  );
543 }
GtkWidget * view
Definition: editview.h:33
gchar * file_path
Definition: editview.h:34
void mape_edit_view_destroy ( MapeEditView view)

Definition at line 505 of file editview.c.

References MapeEditView_::file_path, MapeEditView_::lang_manager, MapeEditView_::render_thread, and MapeEditView_::style_manager.

Referenced by mape_window_destroy(), and mape_window_new().

506 {
507  gpointer data;
508 
509  /* Wait for render thread to finish */
510  if(view->render_thread != NULL)
511  {
512  data = g_thread_join(view->render_thread);
513  view->render_thread = NULL;
514 
515  /* Don't let idle handler run since edit_view is about to be destroyed */
516  g_assert(((ThreadResult*)data)->idle_id != 0);
517  g_source_remove(((ThreadResult*)data)->idle_id);
518  }
519 
520  g_object_unref(G_OBJECT(view->lang_manager) );
521  g_object_unref(G_OBJECT(view->style_manager));
522  g_free(view->file_path);
523  free(view);
524 }
GThread * render_thread
Definition: editview.h:52
gchar * file_path
Definition: editview.h:34
GtkSourceLanguageManager * lang_manager
Definition: editview.h:38
GtkSourceStyleSchemeManager * style_manager
Definition: editview.h:39

Here is the caller graph for this function:

gboolean mape_edit_view_get_modified ( MapeEditView view)

Definition at line 679 of file editview.c.

References MapeEditView_::view.

680 {
681  return gtk_text_buffer_get_modified(
682  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view))
683  );
684 }
GtkWidget * view
Definition: editview.h:33
MapeEditView* mape_edit_view_new ( MapePreView pre_view,
MapeStatusbar statusbar,
GError **  error 
)

Definition at line 393 of file editview.c.

References MapeEditView_::encoding, MapePreView_::event_box, MapeEditView_::file_path, MapeEditView_::fixed_seed, MapeEditView_::font_desc, MapeEditView_::lang_manager, MAPE_MAPGEN_NONE, MapeEditView_::pre_view, MapeEditView_::render_thread, MapeEditView_::rerender, MapeEditView_::statusbar, MapeEditView_::style_manager, MapeEditView_::type, MapeEditView_::view, and MapeEditView_::window.

Referenced by mape_window_new().

396 {
397  MapeEditView* view;
398  GtkSourceBuffer* buf;
399  GPtrArray* search_dirs;
400  const gchar* const* data_dirs;
401  const gchar* const* dir;
402 
403  view = malloc(sizeof(MapeEditView) );
404  view->pre_view = pre_view;
405  view->statusbar = statusbar;
406  view->file_path = NULL;
407  view->encoding = "UTF-8";
408  view->type = MAPE_MAPGEN_NONE;
409  view->render_thread = NULL;
410  view->rerender = FALSE;
411  view->fixed_seed = FALSE;
412 
413  view->view = gtk_source_view_new();
414  buf = GTK_SOURCE_BUFFER(
415  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view) )
416  );
417 
418  g_signal_connect_after(
419  G_OBJECT(buf),
420  "changed",
421  G_CALLBACK(mape_edit_view_cb_changed),
422  view
423  );
424 
425  g_signal_connect(
426  G_OBJECT(pre_view->event_box),
427  "button-press-event",
428  G_CALLBACK(mape_edit_view_cb_update),
429  view
430  );
431 
432  view->font_desc = pango_font_description_new();
433  pango_font_description_set_family(view->font_desc, "monospace");
434  gtk_widget_modify_font(view->view, view->font_desc);
435 
436  search_dirs = g_ptr_array_new();
437 #ifdef G_OS_WIN32
438  g_ptr_array_add(
439  search_dirs,
440  g_win32_get_package_installation_subdirectory(NULL, NULL, "mape-syntax")
441  );
442 #endif
443 
444  g_ptr_array_add(
445  search_dirs,
446  g_build_filename(g_get_home_dir(), ".mape-syntax", NULL)
447  );
448 
449  g_ptr_array_add(
450  search_dirs,
451  g_strdup("./mape-syntax")
452  );
453 
454  g_ptr_array_add(
455  search_dirs,
456  g_strdup("./src/mape/mape-syntax")
457  );
458 
459  data_dirs = g_get_system_data_dirs();
460  for(dir = data_dirs; *dir != NULL; ++ dir)
461  g_ptr_array_add(search_dirs, g_build_filename(*dir, "mape", NULL));
462  g_ptr_array_add(search_dirs, NULL);
463 
464  view->lang_manager = gtk_source_language_manager_new();
465  gtk_source_language_manager_set_search_path(
466  view->lang_manager,
467  (gchar**)search_dirs->pdata
468  );
469 
470  view->style_manager = gtk_source_style_scheme_manager_new();
471  gtk_source_style_scheme_manager_set_search_path(
472  view->style_manager,
473  (gchar**)search_dirs->pdata
474  );
475 
476  g_ptr_array_foreach(search_dirs, (GFunc)g_free, NULL);
477  g_ptr_array_free(search_dirs, TRUE);
478 
479  mape_edit_view_set_filename(view, NULL);
480 
481  gtk_widget_show(view->view);
482 
483  view->window = gtk_scrolled_window_new(NULL, NULL);
484 
485  gtk_scrolled_window_set_policy(
486  GTK_SCROLLED_WINDOW(view->window),
487  GTK_POLICY_AUTOMATIC,
488  GTK_POLICY_AUTOMATIC
489  );
490 
491  gtk_scrolled_window_set_shadow_type(
492  GTK_SCROLLED_WINDOW(view->window),
493  GTK_SHADOW_IN
494  );
495 
496  gtk_container_add(
497  GTK_CONTAINER(view->window),
498  view->view
499  );
500 
501  gtk_widget_show(view->window);
502  return view;
503 }
MapeMapgenType type
Definition: editview.h:36
GThread * render_thread
Definition: editview.h:52
PangoFontDescription * font_desc
Definition: editview.h:40
GtkWidget * window
Definition: editview.h:32
GtkWidget * view
Definition: editview.h:33
gboolean fixed_seed
Definition: editview.h:45
gboolean rerender
Definition: editview.h:53
const gchar * encoding
Definition: editview.h:35
gchar * file_path
Definition: editview.h:34
GtkSourceLanguageManager * lang_manager
Definition: editview.h:38
MapeStatusbar * statusbar
Definition: editview.h:43
MapePreView * pre_view
Definition: editview.h:42
GtkWidget * event_box
Definition: preview.h:30
GtkSourceStyleSchemeManager * style_manager
Definition: editview.h:39

Here is the caller graph for this function:

gboolean mape_edit_view_open ( MapeEditView view,
const gchar *  filename,
GError **  error 
)

Definition at line 545 of file editview.c.

References MapeEditView_::encoding, MAPE_EDIT_VIEW_ERROR_UNKNOWN_ENCODING, and MapeEditView_::view.

548 {
549  gboolean result;
550  gchar* contents;
551  gchar* conv;
552  gchar* utf8_file;
553  gsize length;
554 
555  result = g_file_get_contents(filename, &contents, &length, error);
556  if(result == FALSE) return FALSE;
557 
558  /* Assume UTF-8 */
559  result = g_utf8_validate(contents, length, NULL);
560 
561  if(result == FALSE)
562  {
563  /* No UTF-8, try LATIN1 */
564  conv = g_convert(
565  contents,
566  -1,
567  "UTF-8",
568  "LATIN1",
569  NULL,
570  NULL,
571  NULL
572  );
573 
574  g_free(contents);
575  if(conv == NULL)
576  {
577  utf8_file = g_filename_to_utf8(
578  filename,
579  -1,
580  NULL,
581  NULL,
582  NULL
583  );
584 
585  if(utf8_file == NULL)
586  utf8_file = g_strdup("<unknown file name>");
587 
588  g_set_error(
589  error,
590  g_quark_from_static_string(
591  "MAPE_EDIT_VIEW_ERROR"
592  ),
594  "Could not read file %s: Either the encoding "
595  "is unknown or it is a binary file",
596  utf8_file
597  );
598 
599  g_free(utf8_file);
600  return FALSE;
601  }
602 
603  /* Conversion succeeded */
604  contents = conv;
605  view->encoding = "LATIN1";
606  }
607  else
608  {
609  view->encoding = "UTF-8";
610  }
611 
612  mape_edit_view_set_filename(view, filename);
613 
614  /* TODO: Undoable action dingsen */
615  /* (statische mape_edit_view_set_contents-Call?) */
616  gtk_text_buffer_set_text(
617  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view)),
618  contents,
619  length
620  );
621 
622  g_free(contents);
623 
624  gtk_text_buffer_set_modified(
625  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view)),
626  FALSE
627  );
628 
629  return TRUE;
630 }
GtkWidget * view
Definition: editview.h:33
const gchar * encoding
Definition: editview.h:35
void mape_edit_view_redo ( MapeEditView edit_view)

Definition at line 695 of file editview.c.

References MapeEditView_::view.

696 {
697  gtk_source_buffer_redo(
698  GTK_SOURCE_BUFFER(
699  gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit_view->view))
700  )
701  );
702 }
GtkWidget * view
Definition: editview.h:33
void mape_edit_view_reload ( MapeEditView edit_view)

Definition at line 764 of file editview.c.

References _ThreadData::file_path, MapeEditView_::file_path, MapeEditView_::fixed_seed, _ThreadData::map_height, MapeEditView_::map_height, _ThreadData::map_width, MapeEditView_::map_width, _ThreadData::map_zoom, MapeEditView_::map_zoom, mape_random_seed(), mape_statusbar_set_compile(), MapeMatTexView_::mat_map, _ThreadData::mat_map, MapePreView_::mat_tex, MapeEditView_::pre_view, MapeEditView_::random_seed, MapeEditView_::render_thread, MapeEditView_::rerender, _ThreadData::source, _ThreadData::start_time, MapeEditView_::statusbar, MapeMatTexView_::tex_map, _ThreadData::tex_map, _ThreadData::type, MapeEditView_::type, _ThreadData::view, and MapeEditView_::view.

Referenced by mape_edit_view_apply_preferences(), and mape_edit_view_save().

765 {
766  GError* error = NULL;
767  ThreadData* data;
768  GtkTextBuffer* buffer;
769  GtkTextIter begin;
770  GtkTextIter end;
771 
772  if(edit_view->render_thread == NULL)
773  {
774  data = g_slice_new(ThreadData);
775 
776  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit_view->view) );
777  gtk_text_buffer_get_start_iter(buffer, &begin);
778  gtk_text_buffer_get_end_iter(buffer, &end);
779 
780  /* TODO: We need to ref view so that it is guaranteed to be alive in the
781  * thread result handler */
782  data->view = edit_view;
783  data->source = gtk_text_buffer_get_text(buffer, &begin, &end, TRUE);
784  data->type = edit_view->type;
785  data->file_path = g_strdup(edit_view->file_path);
786 
787  /* TODO: We need to ref these so noone can delete them while the thread
788  * uses them. */
789  data->mat_map = edit_view->pre_view->mat_tex->mat_map,
790  data->tex_map = edit_view->pre_view->mat_tex->tex_map,
791 
792  data->map_width = edit_view->map_width;
793  data->map_height = edit_view->map_height;
794  data->map_zoom = edit_view->map_zoom;
795  data->start_time = g_get_monotonic_time();
796 
797  if(edit_view->fixed_seed == TRUE)
798  mape_random_seed(edit_view->random_seed);
799 
801  edit_view->statusbar,
802  "Rendering map..."
803  );
804 
805  edit_view->rerender = FALSE;
806 
807  edit_view->render_thread = g_thread_create(
808  mape_edit_view_thread_entry,
809  data,
810  TRUE,
811  &error
812  );
813 
814  if(edit_view->render_thread == NULL)
815  {
817  edit_view->statusbar,
818  error->message
819  );
820 
821  g_free(data->source);
822  g_slice_free(ThreadData, data);
823 
824  g_error_free(error);
825  }
826  }
827  else
828  {
829  /* Rerender when thread finished */
830  edit_view->rerender = TRUE;
831  }
832 }
unsigned int map_width
Definition: editview.h:48
guint map_height
Definition: editview.c:36
MapeMapgenType type
Definition: editview.h:36
gchar * file_path
Definition: editview.c:32
double map_zoom
Definition: editview.h:50
MapeTextureMap * tex_map
Definition: mattexview.h:33
GThread * render_thread
Definition: editview.h:52
guint map_width
Definition: editview.c:35
MapeTextureMap * tex_map
Definition: editview.c:34
unsigned int map_height
Definition: editview.h:49
MapeMatTexView * mat_tex
Definition: preview.h:34
void mape_statusbar_set_compile(MapeStatusbar *bar, const gchar *text)
Definition: statusbar.c:47
GtkWidget * view
Definition: editview.h:33
MapeMaterialMap * mat_map
Definition: mattexview.h:32
gboolean fixed_seed
Definition: editview.h:45
gboolean rerender
Definition: editview.h:53
gdouble map_zoom
Definition: editview.c:37
gint64 start_time
Definition: editview.c:38
MapeEditView * view
Definition: editview.c:29
gchar * file_path
Definition: editview.h:34
MapeMaterialMap * mat_map
Definition: editview.c:33
MapeMapgenType type
Definition: editview.c:31
MapeStatusbar * statusbar
Definition: editview.h:43
void mape_random_seed(unsigned int seed)
Definition: random.c:42
unsigned int random_seed
Definition: editview.h:46
MapePreView * pre_view
Definition: editview.h:42
gchar * source
Definition: editview.c:30

Here is the call graph for this function:

Here is the caller graph for this function:

gboolean mape_edit_view_save ( MapeEditView view,
const gchar *  filename,
GError **  error 
)

Definition at line 632 of file editview.c.

References MapeEditView_::encoding, mape_edit_view_reload(), and MapeEditView_::view.

635 {
636  GtkTextBuffer* buffer;
637  GtkTextIter begin;
638  GtkTextIter end;
639  gchar* source;
640  gchar* conv;
641  gboolean result;
642 
643  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view) );
644  gtk_text_buffer_get_start_iter(buffer, &begin);
645  gtk_text_buffer_get_end_iter(buffer, &end);
646  source = gtk_text_buffer_get_text(buffer, &begin, &end, TRUE);
647 
648  conv = g_convert(
649  source,
650  -1,
651  view->encoding,
652  "UTF-8",
653  NULL,
654  NULL,
655  error
656  );
657 
658  g_free(source);
659  if(conv == NULL) return FALSE;
660 
661  result = g_file_set_contents(filename, conv, -1, error);
662  g_free(conv);
663  if(result == FALSE) return FALSE;
664 
665  gtk_text_buffer_set_modified(
666  gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view)),
667  FALSE
668  );
669 
670  mape_edit_view_set_filename(view, filename);
671 
672  /* Rerender with new file path --
673  * different Script.c lookup for algo=script overlays */
674  mape_edit_view_reload(view);
675 
676  return TRUE;
677 }
void mape_edit_view_reload(MapeEditView *edit_view)
Definition: editview.c:764
GtkWidget * view
Definition: editview.h:33
const gchar * encoding
Definition: editview.h:35

Here is the call graph for this function:

void mape_edit_view_undo ( MapeEditView edit_view)

Definition at line 686 of file editview.c.

References MapeEditView_::view.

687 {
688  gtk_source_buffer_undo(
689  GTK_SOURCE_BUFFER(
690  gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit_view->view))
691  )
692  );
693 }
GtkWidget * view
Definition: editview.h:33