1 /* utils.h
2  * Header file for functions useful across the different modules of
3  * drawing and non-drawing code.
4  *
5  * also includes useful constants
6  *
7  * for Denemo, a gtk+ frontend to GNU Lilypond
8  * (c) 1999-2005 Matthew Hiller, 2008, 2009 Richard Shann
9  */
10 #ifndef UTILS_H
11 #define UTILS_H
12 
13 #include <denemo/denemo.h>
14 #include <gdk/gdk.h>
15 //FIXME, these two need to be an enum in denemo_types.h and the field showaccidental changed from boolean to this type.
16 #define DENEMO_REMINDER (2)
17 #define DENEMO_CAUTIONARY (3)
18 
19 #define HIGHLIGHT_OFFSET (20)   /* Fairly arbitrary value to transform codes '0', '1' ... to a new range with the meaning highlight the whole-note, half-note, ..., glyph */
20 #define MAXEXTRASPACE (150)     /* maximum space for ledger lines, for sanity */
21 
22 #define LINE_SPACE 10
23 #define HALF_LINE_SPACE 5
24 #define NO_OF_LINES 5
25 #define STAFF_HEIGHT (LINE_SPACE * (NO_OF_LINES - 1))
26 #define LYRICS_HEIGHT (STAFF_HEIGHT/2)
27 #define MID_STAFF_HEIGHT 2*LINE_SPACE
28 /*#define LEFT_MARGIN 20 now variable for braces to take space */
29 //#define KEY_MARGIN (LEFT_MARGIN+35)
30 #define SPACE_FOR_TIME 35
31 #define RIGHT_MARGIN 20
32 #define SPACE_FOR_BARLINE 10
33 #define HALF_BARLINE_SPACE 5
34 #define WHOLE_NUMTICKS 1536
35 #define FONT "Sans 9"
36 #define TIMESIGFONT "Sans 24"
37 
38 #ifndef g_info
39 #ifdef G_HAVE_ISO_VARARGS
40 #define g_info(...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__)
41 #elif defined(G_HAVE_GNUC_VARARGS)
42 #define g_info(format) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
43 #endif
44 #endif
45 
46 const gchar *get_user_data_dir (gboolean create);
47 
48 const gchar *locateprintdir (void);
49 /* Adds a callback that processes the "activate" signal coming from
50  * a widget */
51 
52 #define processenter(entry, callback,  cbdata, dialog)\
53   g_signal_connect(G_OBJECT(entry), "activate", \
54              G_CALLBACK(callback), \
55              &cbdata); \
56   if (dialog) \
57     g_signal_connect_object (G_OBJECT (entry), "activate", \
58                    G_CALLBACK(gtk_widget_destroy), \
59                    G_OBJECT (dialog), G_CONNECT_AFTER)
60 
61 
62 void drawbitmapinverse_cr (cairo_t * cr, DenemoGraphic * mask, gint x, gint y, gboolean invert);
63 
64 void drawfetachar_cr (cairo_t * cr, gunichar uc, double x, double y);
65 
66 //void
67 //setcairocolor (cairo_t * cr, GdkGC * gc);
68 
69 void drawnormaltext_cr (cairo_t * cr, const char *text, double x, double y);
70 
71 void drawlargetext_cr (cairo_t * cr, const char *text, double x, double y);
72 
73 void drawtext_cr (cairo_t * cr, const char *text, double x, double y, double size);
74 
75 void draw_staff_brace (cairo_t *cr, gboolean curly, gint x, gint y, gint height);
76 
77 gint draw_for_directives (cairo_t * cr, GList * directives, gint x, gint y, gboolean at_cursor);
78 
79 gchar *pretty_name (gchar *lilynote);
80 
81 /* Gives space after a note or rest */
82 
83 gint space_after (gint numticks, gint wholenotewidth);
84 
85 /* Returns height of a note based on what the note is and and the current
86  * clef context */
87 
88 gint calculateheight (gint mid_c_offset, gint dclef);
89 
90 /* Translates a mid_c_offset into 0 (c) through 6 (b). Useful for
91  * getting accidentals to persist */
92 
93 gint offsettonumber (gint n);
94 
95 gchar *mid_c_offsettolily (int mid_c_offset, int enshift);
96 
97 gchar mid_c_offsettoname (gint mid_c_offset);
98 
99 gint mid_c_offsettooctave (gint mid_c_offset);
100 
101 void set_grace_numticks (DenemoObject * theobj, gint multiplier);
102 
103 void set_tuplefied_numticks (DenemoObject * theobj, gint numerator, gint denominator);
104 
105 void set_basic_numticks (DenemoObject * theobj);
106 
107 void setpixelmin (DenemoObject * theobj);
108 
109 void freeit (gpointer data, gpointer user_data);
110 
111 void popup_menu (gchar * name);
112 
113 void warningmessage (gchar * msg);
114 
115 void warningdialog (gchar * msg);
116 void infowarningdialog (gchar * msg, gboolean info);
117 GtkWidget *infodialog (gchar * msg);
118 
119 GtkWindow *progressbar (gchar * msg, gpointer callback);
120 void progressbar_stop (void);
121 void busy_cursor (GtkWidget *area);
122 void normal_cursor (GtkWidget *area);
123 void headerfields (GtkWidget * dialog, GtkListStore * list_store, GtkTreeIter * iter, DenemoMovement * si, gboolean isdialog);
124 /* default isdialog shall be TRUE */
125 
126 
127 void initdir (void);
128 const gchar *get_system_data_dir (void);
129 /* UNUSED
130 const gchar *get_system_conf_dir (void);
131 */
132 const gchar *get_system_locale_dir (void);
133 const gchar *get_system_bin_dir (void);
134 const gchar *get_system_font_dir(void);
135 const gchar *get_executable_dir(void);
136 /* get directory of current Denemo.project or home directory if untitled. User must free the returned string */
137 gchar *get_project_dir(void);
138 const gchar* get_local_dir(DenemoDirectory dir);
139 gchar* get_system_dir(DenemoDirectory dir);
140 void kill_process (GPid pid);
141 
142 gchar *music_font (gchar * str);
143 
144 void set_title_bar (DenemoProject * gui);
145 void score_status (DenemoProject * gui, gboolean change);
146 void write_status (DenemoProject * gui);
147 gboolean confirm (gchar * primary, gchar * secondary);
148 gboolean choose_option (gchar *title, gchar * primary, gchar * secondary);
149 void nullify_gstring (GString ** s);
150 gchar *choose_file (gchar *title, gchar *startdir, GList *extensions);
151 gchar *string_dialog_entry (DenemoProject * gui, gchar * title, gchar * instruction, gchar * initial_value);
152 
153 gchar *string_dialog_entry_with_widget (DenemoProject * gui, gchar * title, gchar * instruction, gchar * initial_value, GtkWidget * extra_widget);
154 gchar *string_dialog_editor_with_widget (DenemoProject * gui, gchar * wlabel, gchar * direction, gchar * PreValue, GtkWidget * widget, gpointer keypress_callback);
155 gchar *string_dialog_entry_with_widget_opt (DenemoProject * gui, gchar * wlabel, gchar * direction, gchar * PreValue, GtkWidget * widget, gboolean modal);
156 gchar *string_dialog_editor_with_widget_opt (DenemoProject * gui, gchar * wlabel, gchar * direction, gchar * PreValue, GtkWidget * widget, gboolean modal, gpointer keypress_callback);
157 void note2lilynotename (struct note *noteobject, GString * ret);
158 void note2lilyaccidental (struct note *noteobject, GString * ret);
159 void note2lilyoctave (struct note *noteobject, GString * ret);
160 void chord2lilybaseduration (struct chord *chordobject, GString * ret);
161 void chord2lilyduration (struct chord *chordobject, GString * ret);
162 void chord2lilynumdots (struct chord *chordobject, GString * ret);
163 gchar *get_fretdiagram_as_markup (void);
164 gchar *get_fakechord_as_markup (gchar *size, gchar *font);
165 
166 
167 #define UTILS_H_PARAM_ASSIGN(param_name) if( (str = g_strstr_len(values->str+i,strlen(values->str+i), #param_name)))\
168 nothing=FALSE,param_name = (*(str+strlen(#param_name))=='=')?str+strlen(#param_name)+1:NULL;
169 
170 #define GET_1PARAM(action, param, param_name) \
171   G_GNUC_UNUSED gchar * query = NULL;\
172   gchar * param_name = NULL;\
173   DenemoScriptParam dummy;\
174   dummy.string=NULL;\
175   if(param==NULL)\
176     param = &dummy;\
177   param->status = FALSE;\
178 if(!action && param){\
179     G_GNUC_UNUSED gboolean nothing=TRUE;\
180     GString *values = ((DenemoScriptParam *)param)->string;\
181     if(values) {\
182       gchar *str;\
183       guint i;\
184       for(i=0;i<values->len;i+=strlen(values->str+i)+1) {\
185          UTILS_H_PARAM_ASSIGN(query)\
186          UTILS_H_PARAM_ASSIGN(param_name)\
187       }\
188     }\
189     if(param_name==NULL)\
190        param_name = values?values->str:NULL;\
191 }
192 
193 #define GET_2PARAMS(action, param, param_name1, param_name2) \
194 gchar * query = NULL;\
195 gchar * param_name1 = NULL;\
196 gchar * param_name2 = NULL;\
197   DenemoScriptParam dummy;\
198   dummy.string=NULL;\
199   if(param==NULL)\
200     param = &dummy;\
201   param->status = FALSE;\
202 if(!action && param){\
203     gboolean nothing=TRUE;\
204     GString *values = ((DenemoScriptParam *)param)->string;\
205     if(values) {\
206      gchar *str;\
207      guint i;\
208        for(i=0;i<values->len;i+=strlen(values->str+i)+1) {\
209           UTILS_H_PARAM_ASSIGN(query)\
210           UTILS_H_PARAM_ASSIGN(param_name1)\
211           UTILS_H_PARAM_ASSIGN(param_name2)\
212        }\
213      }\
214      if(nothing)\
215       param_name1=values?values->str:NULL;\
216 }
217 #define GET_3PARAMS(action, param, param_name1, param_name2, param_name3) \
218 gchar * query = NULL;\
219 gchar * param_name1 = NULL;\
220 gchar * param_name2 = NULL;\
221 gchar * param_name3 = NULL;\
222   DenemoScriptParam dummy;\
223   dummy.string=NULL;\
224   if(param==NULL)\
225     param = &dummy;\
226   param->status = FALSE;\
227 if(!action && param){\
228     gboolean nothing=TRUE;\
229     GString *values = ((DenemoScriptParam *)param)->string;\
230     if(values) {\
231      gchar *str;\
232      guint i;\
233        for(i=0;i<values->len;i+=strlen(values->str+i)+1) {\
234           UTILS_H_PARAM_ASSIGN(query)\
235           UTILS_H_PARAM_ASSIGN(param_name1)\
236           UTILS_H_PARAM_ASSIGN(param_name2)\
237           UTILS_H_PARAM_ASSIGN(param_name3)\
238        }\
239      }\
240      if(nothing)\
241       param_name1=values?values->str:NULL;\
242 }
243 #define GET_4PARAMS(action, param, param_name1, param_name2, param_name3, param_name4) \
244 G_GNUC_UNUSED gchar * query = NULL;\
245 gchar * param_name1 = NULL;\
246 gchar * param_name2 = NULL;\
247 gchar * param_name3 = NULL;\
248 gchar * param_name4 = NULL;\
249   DenemoScriptParam dummy;\
250   dummy.string=NULL;\
251   if(param==NULL)\
252     param = &dummy;\
253   param->status = FALSE;\
254 if(!action && param){\
255     GString *values = ((DenemoScriptParam *)param)->string;\
256     gboolean nothing=TRUE;\
257     if(values) {\
258      gchar *str;\
259      guint i;\
260        for(i=0;i<values->len;i+=strlen(values->str+i)+1) {\
261           UTILS_H_PARAM_ASSIGN(query)\
262           UTILS_H_PARAM_ASSIGN(param_name1)\
263           UTILS_H_PARAM_ASSIGN(param_name2)\
264           UTILS_H_PARAM_ASSIGN(param_name3)\
265           UTILS_H_PARAM_ASSIGN(param_name4)\
266        }\
267      }\
268      if(nothing)\
269       param_name1=values?values->str:NULL;\
270 }
271 
272 gchar *get_option (gchar *title, gchar * str, gint length);
273 void console_output (gchar * text);
274 
275 gint get_override (GList * g);
276 
277 void add_font_directory (gchar * fontpath);
278 void add_font_file (gchar * fontpath);
279 const gchar *get_prefix_dir (void);
280 gboolean run_file_association (gchar * filenam);
281 gchar *make_temp_dir (void);
282 gchar *remove_extension (gchar * name);
283 gchar *substitute_extension (gchar * name, gchar * extension);
284 void init_denemo_notenames (void);
285 gint get_widget_height (GtkWidget * w);
286 gint get_widget_width (GtkWidget * w);
287 void switch_back_to_main_window (void);
288 void use_markup (GtkWidget * widget);
289 
290 typedef enum
291 {
292   TwoKey=0,
293   SingleKey=1,
294   MouseGesture=2,
295   KeyPlusMouse=3
296 } DenemoShortcutType;
297 void initialize_keystroke_help (void);
298 void KeyStrokeAwait (gchar * first_keypress);
299 void KeyStrokeDecline (gchar * first_keypress);
300 void KeyStrokeShow (gchar * str, gint command_idx, DenemoShortcutType type);
301 void MouseGestureShow (gchar *str, gchar *help, DenemoShortcutType type);
302 void KeyPlusMouseGestureShow(gchar *str, gint command_idx);
303 
304 note *
305 findnote (DenemoObject * curObj, gint cursory);
306 note *
307 findnote_strict (DenemoObject * curObj, gint cursory);
308 #include "core/kbd-custom.h"
309 #define get_label_for_command(name) lookup_label_from_idx(Denemo.map, lookup_command_from_name(Denemo.map, name))
310 #define get_tooltip_for_command(name) lookup_tooltip_from_idx(Denemo.map, lookup_command_from_name(Denemo.map, name))
311 #define get_menu_path_for_command(name) lookup_menu_path_from_idx(Denemo.map, lookup_command_from_name(Denemo.map, name))
312 
313 #ifdef FAKE_TOOLTIPS
314 gboolean show_tooltip(GtkWidget *w, GdkEvent *ev, gchar *text);
315 void free_tooltip(GtkWidget *w, gchar *text);
316 #endif
317 
318 void write_input_status();
319 enum clefs cleftypefromname (gchar * str);
320 gchar* find_dir_for_file(gchar* filename, GList* dirs);
321 gchar* find_dir_for_files(GList* files, GList* dirs);
322 gchar* find_path_for_file(gchar* filename, GList* dirs);
323 gchar* find_denemo_file (DenemoDirectory dir, gchar* filename);
324 gchar *escape_scheme (gchar *input);
325 gchar *time_spent_editing(void);
326 void reset_editing_timer(void);
327 gboolean shift_held_down(void);
328 #endif /* UTILS_H */
329