Revision: 3010 http://geany.svn.sourceforge.net/geany/?rev=3010&view=rev Author: ntrel Date: 2008-09-26 17:28:50 +0000 (Fri, 26 Sep 2008)
Log Message: ----------- Use GeanyDocumentPrivate instead of using inheritance for non-public fields (this was unnecessary and meant using ugly casts).
Modified Paths: -------------- trunk/ChangeLog trunk/src/document.c trunk/src/document.h trunk/src/documentprivate.h trunk/src/editor.c trunk/src/notebook.c trunk/src/prefs.c trunk/src/symbols.c trunk/src/treeviews.c trunk/src/ui_utils.c
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/ChangeLog 2008-09-26 17:28:50 UTC (rev 3010) @@ -20,6 +20,11 @@ * HACKING: Update to mention separate is_comment_style() and is_string_style() functions. + * src/documentprivate.h, src/prefs.c, src/treeviews.c, src/notebook.c, + src/document.c, src/document.h, src/editor.c, src/symbols.c, + src/ui_utils.c: + Use GeanyDocumentPrivate instead of using inheritance for non-public + fields (this was unnecessary and meant using ugly casts).
2008-09-25 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
Modified: trunk/src/document.c =================================================================== --- trunk/src/document.c 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/document.c 2008-09-26 17:28:50 UTC (rev 3010) @@ -303,9 +303,9 @@ * The flag is_valid is set to TRUE in document_create(). */ static void init_doc_struct(GeanyDocument *new_doc) { - Document *full_doc = DOCUMENT(new_doc); + GeanyDocumentPrivate *priv;
- memset(full_doc, 0, sizeof(Document)); + memset(new_doc, 0, sizeof(GeanyDocument));
new_doc->is_valid = FALSE; new_doc->has_tags = FALSE; @@ -321,12 +321,14 @@ new_doc->last_check = time(NULL); new_doc->real_path = NULL;
- full_doc->tag_store = NULL; - full_doc->tag_tree = NULL; - full_doc->saved_encoding.encoding = NULL; - full_doc->saved_encoding.has_bom = FALSE; - full_doc->undo_actions = NULL; - full_doc->redo_actions = NULL; + new_doc->priv = g_new0(GeanyDocumentPrivate, 1); + priv = new_doc->priv; + priv->tag_store = NULL; + priv->tag_tree = NULL; + priv->saved_encoding.encoding = NULL; + priv->saved_encoding.has_bom = FALSE; + priv->undo_actions = NULL; + priv->redo_actions = NULL; }
@@ -350,7 +352,7 @@ static void queue_colourise(GeanyDocument *doc) { /* Colourise the editor before it is next drawn */ - DOCUMENT(doc)->colourise_needed = TRUE; + doc->priv->colourise_needed = TRUE;
/* If the editor doesn't need drawing (e.g. after saving the current * document), we need to force a redraw, so the expose event is triggered. @@ -379,7 +381,7 @@ new_idx = document_get_new_idx(); if (new_idx == -1) /* expand the array, no free places */ { - Document *new_doc = g_new0(Document, 1); + GeanyDocument *new_doc = g_new0(GeanyDocument, 1);
new_idx = documents_array->len; g_ptr_array_add(documents_array, new_doc); @@ -403,7 +405,7 @@ GtkTreeSelection *sel;
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.tree_openfiles)); - gtk_tree_selection_select_iter(sel, &DOCUMENT(this)->iter); + gtk_tree_selection_select_iter(sel, &this->priv->iter); }
ui_document_buttons_update(); @@ -441,7 +443,6 @@ gboolean document_remove_page(guint page_num) { GeanyDocument *doc = document_get_from_page(page_num); - Document *fdoc = DOCUMENT(doc);
if (doc == NULL) { @@ -469,7 +470,7 @@ navqueue_remove_file(doc->file_name); msgwin_status_add(_("File %s closed."), DOC_FILENAME(doc)); g_free(doc->encoding); - g_free(fdoc->saved_encoding.encoding); + g_free(doc->priv->saved_encoding.encoding); g_free(doc->file_name); g_free(doc->real_path); tm_workspace_remove_object(doc->tm_file, TRUE, TRUE); @@ -485,6 +486,8 @@ doc->has_bom = FALSE; doc->tm_file = NULL; document_undo_clear(doc); + g_free(doc->priv); + if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0) { treeviews_update_tag_list(NULL, FALSE); @@ -501,11 +504,9 @@ /* used to keep a record of the unchanged document state encoding */ static void store_saved_encoding(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); - - g_free(fdoc->saved_encoding.encoding); - fdoc->saved_encoding.encoding = g_strdup(doc->encoding); - fdoc->saved_encoding.has_bom = doc->has_bom; + g_free(doc->priv->saved_encoding.encoding); + doc->priv->saved_encoding.encoding = g_strdup(doc->encoding); + doc->priv->saved_encoding.has_bom = doc->has_bom; }
@@ -1535,9 +1536,8 @@
tm_workspace_update(TM_WORK_OBJECT(app->tm_workspace), TRUE, TRUE, FALSE); { - Document *fdoc = DOCUMENT(doc); - gtk_label_set_text(GTK_LABEL(fdoc->tab_label), base_name); - gtk_label_set_text(GTK_LABEL(fdoc->tabmenu_label), base_name); + gtk_label_set_text(GTK_LABEL(doc->priv->tab_label), base_name); + gtk_label_set_text(GTK_LABEL(doc->priv->tabmenu_label), base_name); } msgwin_status_add(_("File %s saved."), doc->file_name); ui_update_statusbar(doc, -1); @@ -2174,12 +2174,11 @@ /* Clears the Undo and Redo buffer (to be called when reloading or closing the document) */ void document_undo_clear(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); undo_action *a;
- while (g_trash_stack_height(&fdoc->undo_actions) > 0) + while (g_trash_stack_height(&doc->priv->undo_actions) > 0) { - a = g_trash_stack_pop(&fdoc->undo_actions); + a = g_trash_stack_pop(&doc->priv->undo_actions); if (a != NULL) { switch (a->type) @@ -2191,11 +2190,11 @@ g_free(a); } } - fdoc->undo_actions = NULL; + doc->priv->undo_actions = NULL;
- while (g_trash_stack_height(&fdoc->redo_actions) > 0) + while (g_trash_stack_height(&doc->priv->redo_actions) > 0) { - a = g_trash_stack_pop(&fdoc->redo_actions); + a = g_trash_stack_pop(&doc->priv->redo_actions); if (a != NULL) { switch (a->type) @@ -2207,7 +2206,7 @@ g_free(a); } } - fdoc->redo_actions = NULL; + doc->priv->redo_actions = NULL;
if (! main_status.quitting && doc->editor != NULL) document_set_text_changed(doc, FALSE); @@ -2219,7 +2218,6 @@
void document_undo_add(GeanyDocument *doc, guint type, gpointer data) { - Document *fdoc = DOCUMENT(doc); undo_action *action;
if (doc == NULL) @@ -2229,7 +2227,7 @@ action->type = type; action->data = data;
- g_trash_stack_push(&fdoc->undo_actions, action); + g_trash_stack_push(&doc->priv->undo_actions, action);
document_set_text_changed(doc, TRUE); ui_update_popup_reundo_items(doc); @@ -2241,12 +2239,10 @@
gboolean document_can_undo(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); - if (doc == NULL) return FALSE;
- if (g_trash_stack_height(&fdoc->undo_actions) > 0 || sci_can_undo(doc->editor->sci)) + if (g_trash_stack_height(&doc->priv->undo_actions) > 0 || sci_can_undo(doc->editor->sci)) return TRUE; else return FALSE; @@ -2255,25 +2251,22 @@
static void update_changed_state(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); - doc->changed = (sci_is_modified(doc->editor->sci) || - doc->has_bom != fdoc->saved_encoding.has_bom || - ! utils_str_equal(doc->encoding, fdoc->saved_encoding.encoding)); + doc->has_bom != doc->priv->saved_encoding.has_bom || + ! utils_str_equal(doc->encoding, doc->priv->saved_encoding.encoding)); document_set_text_changed(doc, doc->changed); }
void document_undo(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); undo_action *action;
if (doc == NULL) return;
- action = g_trash_stack_pop(&fdoc->undo_actions); + action = g_trash_stack_pop(&doc->priv->undo_actions);
if (action == NULL) { @@ -2328,12 +2321,10 @@
gboolean document_can_redo(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); - if (doc == NULL) return FALSE;
- if (g_trash_stack_height(&fdoc->redo_actions) > 0 || sci_can_redo(doc->editor->sci)) + if (g_trash_stack_height(&doc->priv->redo_actions) > 0 || sci_can_redo(doc->editor->sci)) return TRUE; else return FALSE; @@ -2342,13 +2333,12 @@
void document_redo(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); undo_action *action;
if (doc == NULL) return;
- action = g_trash_stack_pop(&fdoc->redo_actions); + action = g_trash_stack_pop(&doc->priv->redo_actions);
if (action == NULL) { @@ -2402,7 +2392,6 @@
static void document_redo_add(GeanyDocument *doc, guint type, gpointer data) { - Document *fdoc = DOCUMENT(doc); undo_action *action;
if (doc == NULL) @@ -2412,7 +2401,7 @@ action->type = type; action->data = data;
- g_trash_stack_push(&fdoc->redo_actions, action); + g_trash_stack_push(&doc->priv->redo_actions, action);
document_set_text_changed(doc, TRUE); ui_update_popup_reundo_items(doc);
Modified: trunk/src/document.h =================================================================== --- trunk/src/document.h 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/document.h 2008-09-26 17:28:50 UTC (rev 3010) @@ -105,6 +105,8 @@ * not be set elsewhere. * @see file_name. */ gchar *real_path; + + struct GeanyDocumentPrivate *priv; /* should be last, append fields before this item */ };
extern GPtrArray *documents_array;
Modified: trunk/src/documentprivate.h =================================================================== --- trunk/src/documentprivate.h 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/documentprivate.h 2008-09-26 17:28:50 UTC (rev 3010) @@ -41,16 +41,13 @@ { gchar *encoding; gboolean has_bom; -} FileEncoding; +} +FileEncoding;
-#define DOCUMENT(doc_ptr) ((Document*)doc_ptr) - -/* This type 'inherits' from GeanyDocument so Document* can be cast to GeanyDocument*. */ -typedef struct Document +/* Private GeanyDocument fields */ +typedef struct GeanyDocumentPrivate { - struct GeanyDocument public; /* must come first */ - /* GtkLabel shown in the notebook header. */ GtkWidget *tab_label; /* GtkLabel shown in the notebook right-click menu. */ @@ -69,6 +66,6 @@ FileEncoding saved_encoding; gboolean colourise_needed; } -Document; +GeanyDocumentPrivate;
#endif
Modified: trunk/src/editor.c =================================================================== --- trunk/src/editor.c 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/editor.c 2008-09-26 17:28:50 UTC (rev 3010) @@ -3808,10 +3808,10 @@ { GeanyDocument *doc = user_data;
- if (DOCUMENT(doc)->colourise_needed) + if (doc->priv->colourise_needed) { editor_colourise(doc->editor->sci); - DOCUMENT(doc)->colourise_needed = FALSE; + doc->priv->colourise_needed = FALSE; } return FALSE; /* propagate event */ }
Modified: trunk/src/notebook.c =================================================================== --- trunk/src/notebook.c 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/notebook.c 2008-09-26 17:28:50 UTC (rev 3010) @@ -443,7 +443,6 @@ GtkWidget *hbox, *ebox; gint tabnum; gchar *title; - Document *fdoc = DOCUMENT(this); GtkWidget *page;
g_return_val_if_fail(this != NULL, -1); @@ -451,7 +450,7 @@ page = GTK_WIDGET(this->editor->sci); title = g_path_get_basename(DOC_FILENAME(this));
- fdoc->tab_label = gtk_label_new(title); + this->priv->tab_label = gtk_label_new(title);
ebox = gtk_event_box_new(); GTK_WIDGET_SET_FLAGS(ebox, GTK_NO_WINDOW); @@ -459,7 +458,7 @@
hbox = gtk_hbox_new(FALSE, 2); GTK_WIDGET_UNSET_FLAGS(hbox, GTK_CAN_FOCUS); - gtk_container_add(GTK_CONTAINER(ebox), fdoc->tab_label); + gtk_container_add(GTK_CONTAINER(ebox), this->priv->tab_label); gtk_box_pack_start(GTK_BOX(hbox), ebox, FALSE, FALSE, 0);
if (file_prefs.show_tab_cross) @@ -495,15 +494,15 @@
gtk_widget_show_all(hbox);
- fdoc->tabmenu_label = gtk_label_new(title); - gtk_misc_set_alignment(GTK_MISC(fdoc->tabmenu_label), 0.0, 0); + this->priv->tabmenu_label = gtk_label_new(title); + gtk_misc_set_alignment(GTK_MISC(this->priv->tabmenu_label), 0.0, 0);
if (file_prefs.tab_order_ltr) tabnum = gtk_notebook_append_page_menu(GTK_NOTEBOOK(main_widgets.notebook), page, - hbox, fdoc->tabmenu_label); + hbox, this->priv->tabmenu_label); else tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), page, - hbox, fdoc->tabmenu_label, 0); + hbox, this->priv->tabmenu_label, 0);
tab_count_changed();
Modified: trunk/src/prefs.c =================================================================== --- trunk/src/prefs.c 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/prefs.c 2008-09-26 17:28:50 UTC (rev 3010) @@ -1195,10 +1195,10 @@ interface_prefs.tagbar_font = g_strdup(fontbtn); for (i = 0; i < documents_array->len; i++) { - Document *fdoc = DOCUMENT(documents[i]); + GeanyDocument *doc = documents[i];
- if (documents[i]->is_valid && GTK_IS_WIDGET(fdoc->tag_tree)) - ui_widget_modify_font_from_string(fdoc->tag_tree, + if (documents[i]->is_valid && GTK_IS_WIDGET(doc->priv->tag_tree)) + ui_widget_modify_font_from_string(doc->priv->tag_tree, interface_prefs.tagbar_font); } if (GTK_IS_WIDGET(tv.default_tag_tree))
Modified: trunk/src/symbols.c =================================================================== --- trunk/src/symbols.c 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/symbols.c 2008-09-26 17:28:50 UTC (rev 3010) @@ -569,8 +569,7 @@ static void init_tag_list(GeanyDocument *doc) { filetype_id ft_id = doc->file_type->id; - Document *fdoc = DOCUMENT(doc); - GtkTreeStore *tag_store = fdoc->tag_store; + GtkTreeStore *tag_store = doc->priv->tag_store;
init_tag_iters();
@@ -849,12 +848,10 @@ GtkTreeIter iter; static gint prev_sort_mode = SYMBOLS_SORT_BY_NAME; filetype_id ft_id; - Document *fdoc;
g_return_val_if_fail(doc != NULL, FALSE);
ft_id = FILETYPE_ID(doc->file_type); - fdoc = DOCUMENT(doc);
if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS) sort_mode = prev_sort_mode; @@ -866,11 +863,11 @@ return FALSE;
/* Make sure the model stays with us after the tree view unrefs it */ - g_object_ref(GTK_TREE_MODEL(fdoc->tag_store)); + g_object_ref(GTK_TREE_MODEL(doc->priv->tag_store)); /* Detach model from view */ - gtk_tree_view_set_model(GTK_TREE_VIEW(fdoc->tag_tree), NULL); + gtk_tree_view_set_model(GTK_TREE_VIEW(doc->priv->tag_tree), NULL); /* Clear all contents */ - gtk_tree_store_clear(fdoc->tag_store); + gtk_tree_store_clear(doc->priv->tag_store);
init_tag_list(doc); for (tmp = (GList*)tags; tmp; tmp = g_list_next(tmp)) @@ -953,10 +950,10 @@
if (parent) { - gtk_tree_model_get(GTK_TREE_MODEL(fdoc->tag_store), parent, + gtk_tree_model_get(GTK_TREE_MODEL(doc->priv->tag_store), parent, SYMBOLS_COLUMN_ICON, &icon, -1); - gtk_tree_store_append(fdoc->tag_store, &iter, parent); - gtk_tree_store_set(fdoc->tag_store, &iter, + gtk_tree_store_append(doc->priv->tag_store, &iter, parent); + gtk_tree_store_set(doc->priv->tag_store, &iter, SYMBOLS_COLUMN_ICON, icon, SYMBOLS_COLUMN_NAME, buf, SYMBOLS_COLUMN_LINE, symbol->line, -1); @@ -965,12 +962,12 @@ g_object_unref(icon); } } - hide_empty_rows(fdoc->tag_store); + hide_empty_rows(doc->priv->tag_store); /* Re-attach model to view */ - gtk_tree_view_set_model(GTK_TREE_VIEW(fdoc->tag_tree), - GTK_TREE_MODEL(fdoc->tag_store)); - g_object_unref(GTK_TREE_MODEL(fdoc->tag_store)); - gtk_tree_view_expand_all(GTK_TREE_VIEW(fdoc->tag_tree)); + gtk_tree_view_set_model(GTK_TREE_VIEW(doc->priv->tag_tree), + GTK_TREE_MODEL(doc->priv->tag_store)); + g_object_unref(GTK_TREE_MODEL(doc->priv->tag_store)); + gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
return TRUE; }
Modified: trunk/src/treeviews.c =================================================================== --- trunk/src/treeviews.c 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/treeviews.c 2008-09-26 17:28:50 UTC (rev 3010) @@ -154,8 +154,6 @@ /* update = rescan the tags for doc->filename */ void treeviews_update_tag_list(GeanyDocument *doc, gboolean update) { - Document *fdoc = DOCUMENT(doc); - if (gtk_bin_get_child(GTK_BIN(tag_window))) gtk_container_remove(GTK_CONTAINER(tag_window), gtk_bin_get_child(GTK_BIN(tag_window)));
@@ -187,14 +185,14 @@
if (update) { /* updating the tag list in the left tag window */ - if (fdoc->tag_tree == NULL) + if (doc->priv->tag_tree == NULL) { - fdoc->tag_store = gtk_tree_store_new( + doc->priv->tag_store = gtk_tree_store_new( SYMBOLS_N_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT); - fdoc->tag_tree = gtk_tree_view_new(); - prepare_taglist(fdoc->tag_tree, fdoc->tag_store); - gtk_widget_show(fdoc->tag_tree); - g_object_ref((gpointer)fdoc->tag_tree); /* to hold it after removing */ + doc->priv->tag_tree = gtk_tree_view_new(); + prepare_taglist(doc->priv->tag_tree, doc->priv->tag_store); + gtk_widget_show(doc->priv->tag_tree); + g_object_ref((gpointer)doc->priv->tag_tree); /* to hold it after removing */ }
doc->has_tags = symbols_recreate_tag_list(doc, SYMBOLS_SORT_USE_PREVIOUS); @@ -202,7 +200,7 @@
if (doc->has_tags) { - gtk_container_add(GTK_CONTAINER(tag_window), fdoc->tag_tree); + gtk_container_add(GTK_CONTAINER(tag_window), doc->priv->tag_tree); } else { @@ -300,8 +298,7 @@ * This is called recursively in treeviews_openfiles_update_all(). */ void treeviews_openfiles_add(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); - GtkTreeIter *iter = &fdoc->iter; + GtkTreeIter *iter = &doc->priv->iter;
gtk_list_store_append(store_openfiles, iter); treeviews_openfiles_update(doc); @@ -310,7 +307,6 @@
void treeviews_openfiles_update(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); gchar *basename; GdkColor *color = document_get_status_color(doc);
@@ -318,7 +314,7 @@ basename = DOC_FILENAME(doc); else basename = g_path_get_basename(DOC_FILENAME(doc)); - gtk_list_store_set(store_openfiles, &fdoc->iter, + gtk_list_store_set(store_openfiles, &doc->priv->iter, #if GTK_CHECK_VERSION(2, 12, 0) 0, basename, 1, doc, 2, color, 3, DOC_FILENAME(doc), -1); #else @@ -348,20 +344,19 @@
void treeviews_remove_document(GeanyDocument *doc) { - Document *fdoc = DOCUMENT(doc); - GtkTreeIter *iter = &fdoc->iter; + GtkTreeIter *iter = &doc->priv->iter;
gtk_list_store_remove(store_openfiles, iter);
- if (GTK_IS_WIDGET(fdoc->tag_tree)) + if (GTK_IS_WIDGET(doc->priv->tag_tree)) { - gtk_widget_destroy(fdoc->tag_tree); - if (GTK_IS_TREE_VIEW(fdoc->tag_tree)) + gtk_widget_destroy(doc->priv->tag_tree); + if (GTK_IS_TREE_VIEW(doc->priv->tag_tree)) { /* Because it was ref'd in treeviews_update_tag_list, it needs unref'ing */ - g_object_unref((gpointer)fdoc->tag_tree); + g_object_unref((gpointer)doc->priv->tag_tree); } - fdoc->tag_tree = NULL; + doc->priv->tag_tree = NULL; } }
Modified: trunk/src/ui_utils.c =================================================================== --- trunk/src/ui_utils.c 2008-09-26 16:49:52 UTC (rev 3009) +++ trunk/src/ui_utils.c 2008-09-26 17:28:50 UTC (rev 3010) @@ -1242,13 +1242,12 @@ void ui_update_tab_status(GeanyDocument *doc) { GdkColor *color = document_get_status_color(doc); - Document *fdoc = DOCUMENT(doc);
/* NULL color will reset to default */ - gtk_widget_modify_fg(fdoc->tab_label, GTK_STATE_NORMAL, color); - gtk_widget_modify_fg(fdoc->tab_label, GTK_STATE_ACTIVE, color); - gtk_widget_modify_fg(fdoc->tabmenu_label, GTK_STATE_NORMAL, color); - gtk_widget_modify_fg(fdoc->tabmenu_label, GTK_STATE_ACTIVE, color); + gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_NORMAL, color); + gtk_widget_modify_fg(doc->priv->tab_label, GTK_STATE_ACTIVE, color); + gtk_widget_modify_fg(doc->priv->tabmenu_label, GTK_STATE_NORMAL, color); + gtk_widget_modify_fg(doc->priv->tabmenu_label, GTK_STATE_ACTIVE, color);
treeviews_openfiles_update(doc); }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.