Blender V2.61 - r43446

rna_text.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Blender Foundation (2008)
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 #include <stdlib.h>
00029 #include <limits.h>
00030 
00031 #include "MEM_guardedalloc.h"
00032 
00033 #include "BKE_text.h"
00034 
00035 #include "RNA_define.h"
00036 
00037 #include "rna_internal.h"
00038 
00039 #include "DNA_text_types.h"
00040 
00041 #include "WM_types.h"
00042 
00043 #ifdef RNA_RUNTIME
00044 
00045 int text_file_modified(Text *text); /* XXX bad level call */
00046 
00047 static void rna_Text_filename_get(PointerRNA *ptr, char *value)
00048 {
00049     Text *text= (Text*)ptr->data;
00050 
00051     if(text->name)
00052         strcpy(value, text->name);
00053     else
00054         value[0]= '\0';
00055 }
00056 
00057 static int rna_Text_filename_length(PointerRNA *ptr)
00058 {
00059     Text *text= (Text*)ptr->data;
00060     return (text->name)? strlen(text->name): 0;
00061 }
00062 
00063 static void rna_Text_filename_set(PointerRNA *ptr, const char *value)
00064 {
00065     Text *text= (Text*)ptr->data;
00066 
00067     if(text->name)
00068         MEM_freeN(text->name);
00069 
00070     if(value[0])
00071         text->name= BLI_strdup(value);
00072     else
00073         text->name= NULL;
00074 }
00075 
00076 static int rna_Text_modified_get(PointerRNA *ptr)
00077 {
00078     Text *text= (Text*)ptr->data;
00079     return text_file_modified(text);
00080 }
00081 
00082 static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
00083 {
00084     TextLine *line= (TextLine*)ptr->data;
00085 
00086     if(line->line)
00087         strcpy(value, line->line);
00088     else
00089         value[0]= '\0';
00090 }
00091 
00092 static int rna_TextLine_body_length(PointerRNA *ptr)
00093 {
00094     TextLine *line= (TextLine*)ptr->data;
00095     return line->len;
00096 }
00097 
00098 static void rna_TextLine_body_set(PointerRNA *ptr, const char *value)
00099 {
00100     TextLine *line= (TextLine*)ptr->data;
00101     int len= strlen(value);
00102 
00103     if(line->line)
00104         MEM_freeN(line->line);
00105 
00106     line->line= MEM_mallocN((len + 1) * sizeof(char), "rna_text_body");
00107     line->len= len;
00108     memcpy(line->line, value, len + 1);
00109 
00110     if(line->format) {
00111         MEM_freeN(line->format);
00112         line->format= NULL;
00113     }
00114 }
00115 
00116 #else
00117 
00118 static void rna_def_text_line(BlenderRNA *brna)
00119 {
00120     StructRNA *srna;
00121     PropertyRNA *prop;
00122     
00123     srna = RNA_def_struct(brna, "TextLine", NULL);
00124     RNA_def_struct_ui_text(srna, "Text Line", "Line of text in a Text datablock");
00125     
00126     prop= RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
00127     RNA_def_property_string_funcs(prop, "rna_TextLine_body_get", "rna_TextLine_body_length", "rna_TextLine_body_set");
00128     RNA_def_property_ui_text(prop, "Line", "Text in the line");
00129     RNA_def_property_update(prop, NC_TEXT|NA_EDITED, NULL);
00130 }
00131 
00132 static void rna_def_text_marker(BlenderRNA *brna)
00133 {
00134     StructRNA *srna;
00135     PropertyRNA *prop;
00136     
00137     srna = RNA_def_struct(brna, "TextMarker", NULL);
00138     RNA_def_struct_ui_text(srna, "Text Marker", "Marker highlighting a portion of text in a Text datablock");
00139 
00140     prop= RNA_def_property(srna, "line", PROP_INT, PROP_UNSIGNED);
00141     RNA_def_property_int_sdna(prop, NULL, "lineno");
00142     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00143     RNA_def_property_ui_text(prop, "Line", "Line in which the marker is located");
00144     
00145     prop= RNA_def_property(srna, "character_index_start", PROP_INT, PROP_UNSIGNED);
00146     RNA_def_property_int_sdna(prop, NULL, "start");
00147     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00148     RNA_def_property_ui_text(prop, "Start", "Start position of the marker in the line");
00149 
00150     prop= RNA_def_property(srna, "character_index_end", PROP_INT, PROP_UNSIGNED);
00151     RNA_def_property_int_sdna(prop, NULL, "end");
00152     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00153     RNA_def_property_ui_text(prop, "End", "Start position of the marker in the line");
00154     
00155     prop= RNA_def_property(srna, "group", PROP_INT, PROP_UNSIGNED);
00156     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00157     RNA_def_property_range(prop, 0, (int)0xFFFF);
00158     RNA_def_property_ui_text(prop, "Group", "");
00159     
00160     prop= RNA_def_property(srna, "is_temporary", PROP_BOOLEAN, PROP_NONE);
00161     RNA_def_property_boolean_sdna(prop, NULL, "flags", TMARK_TEMP);
00162     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00163     RNA_def_property_ui_text(prop, "Temporary", "Marker is temporary");
00164 
00165     prop= RNA_def_property(srna, "use_edit_all", PROP_BOOLEAN, PROP_NONE);
00166     RNA_def_property_boolean_sdna(prop, NULL, "flags", TMARK_EDITALL);
00167     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00168     RNA_def_property_ui_text(prop, "Edit All", "Edit all markers of the same group as one");
00169     
00170     prop= RNA_def_property(srna, "color", PROP_FLOAT, PROP_COLOR_GAMMA);
00171     RNA_def_property_range(prop, 0.0f, 1.0f);
00172     RNA_def_property_ui_text(prop, "Color", "Color to display the marker with");
00173 }
00174 
00175 static void rna_def_text(BlenderRNA *brna)
00176 {
00177     StructRNA *srna;
00178     PropertyRNA *prop;
00179     
00180     srna = RNA_def_struct(brna, "Text", "ID");
00181     RNA_def_struct_ui_text(srna, "Text", "Text datablock referencing an external or packed text file");
00182     RNA_def_struct_ui_icon(srna, ICON_TEXT);
00183     RNA_def_struct_clear_flag(srna, STRUCT_ID_REFCOUNT);
00184     
00185     prop= RNA_def_property(srna, "filepath", PROP_STRING, PROP_NONE);
00186     RNA_def_property_string_funcs(prop, "rna_Text_filename_get", "rna_Text_filename_length", "rna_Text_filename_set");
00187     RNA_def_property_ui_text(prop, "File Path", "Filename of the text file");
00188 
00189     prop= RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
00190     RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISDIRTY);
00191     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00192     RNA_def_property_ui_text(prop, "Dirty", "Text file has been edited since last save");
00193 
00194     prop= RNA_def_property(srna, "is_modified", PROP_BOOLEAN, PROP_NONE);
00195     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00196     RNA_def_property_boolean_funcs(prop, "rna_Text_modified_get", NULL);
00197     RNA_def_property_ui_text(prop, "Modified", "Text file on disk is different than the one in memory");
00198 
00199     prop= RNA_def_property(srna, "is_in_memory", PROP_BOOLEAN, PROP_NONE);
00200     RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISMEM);
00201     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00202     RNA_def_property_ui_text(prop, "Memory", "Text file is in memory, without a corresponding file on disk");
00203     
00204     prop= RNA_def_property(srna, "use_module", PROP_BOOLEAN, PROP_NONE);
00205     RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISSCRIPT);
00206     RNA_def_property_ui_text(prop, "Register", "Register this text as a module on loading, Text name must end with \".py\"");
00207 
00208     prop= RNA_def_property(srna, "use_tabs_as_spaces", PROP_BOOLEAN, PROP_NONE);
00209     RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_TABSTOSPACES);
00210     RNA_def_property_ui_text(prop, "Tabs as Spaces", "Automatically converts all new tabs into spaces");
00211 
00212     prop= RNA_def_property(srna, "lines", PROP_COLLECTION, PROP_NONE);
00213     RNA_def_property_struct_type(prop, "TextLine");
00214     RNA_def_property_ui_text(prop, "Lines", "Lines of text");
00215     
00216     prop= RNA_def_property(srna, "current_line", PROP_POINTER, PROP_NONE);
00217     RNA_def_property_flag(prop, PROP_NEVER_NULL);
00218     RNA_def_property_pointer_sdna(prop, NULL, "curl");
00219     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00220     RNA_def_property_struct_type(prop, "TextLine");
00221     RNA_def_property_ui_text(prop, "Current Line", "Current line, and start line of selection if one exists");
00222 
00223     prop= RNA_def_property(srna, "current_character", PROP_INT, PROP_UNSIGNED);
00224     RNA_def_property_int_sdna(prop, NULL, "curc");
00225     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00226     RNA_def_property_ui_text(prop, "Current Character", "Index of current character in current line, and also start index of character in selection if one exists");
00227     
00228     prop= RNA_def_property(srna, "select_end_line", PROP_POINTER, PROP_NONE);
00229     RNA_def_property_flag(prop, PROP_NEVER_NULL);
00230     RNA_def_property_pointer_sdna(prop, NULL, "sell");
00231     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00232     RNA_def_property_struct_type(prop, "TextLine");
00233     RNA_def_property_ui_text(prop, "Selection End Line", "End line of selection");
00234     
00235     prop= RNA_def_property(srna, "select_end_character", PROP_INT, PROP_UNSIGNED);
00236     RNA_def_property_int_sdna(prop, NULL, "selc");
00237     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00238     RNA_def_property_ui_text(prop, "Selection End Character", "Index of character after end of selection in the selection end line");
00239     
00240     prop= RNA_def_property(srna, "markers", PROP_COLLECTION, PROP_NONE);
00241     RNA_def_property_struct_type(prop, "TextMarker");
00242     RNA_def_property_ui_text(prop, "Markers", "Text markers highlighting part of the text");
00243 
00244     RNA_api_text(srna);
00245 }
00246 
00247 void RNA_def_text(BlenderRNA *brna)
00248 {
00249     rna_def_text_line(brna);
00250     rna_def_text_marker(brna);
00251     rna_def_text(brna);
00252 }
00253 
00254 #endif