klavaro  3.13
About: Klavaro is a touch typing tutor program.
  Fossies Dox: klavaro-3.13.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

auxiliar.c
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Klavaro - a flexible touch typing tutor */
3 /* Copyright (C) 2005-2021 Felipe Emmanuel Ferreira de Castro */
4 /* */
5 /* This file is part of Klavaro, which is a free software: you can */
6 /* redistribute it and/or modify it under the terms of the GNU General */
7 /* Public License as published by the Free Software Foundation, either */
8 /* version 3 of the License, or (at your option) any later version. */
9 /* */
10 /* Klavaro is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details (in the file COPYING). */
14 /* You should have received a copy of the GNU General Public License */
15 /* along with Klavaro. If not, see <https://www.gnu.org/licenses/> */
16 /**************************************************************************/
17 
18 #include <sys/stat.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <glib/gstdio.h>
22 #include <gtk/gtk.h>
23 
24 #include "auxiliar.h"
25 #include "main.h"
26 
27 extern GtkBuilder *gui;
28 
29 GObject *
30 get_obj (gchar *name)
31 {
32  GObject *obj;
33  obj = gtk_builder_get_object (gui, name);
34  if (obj == NULL)
35  g_warning ("Object not found: %s", name);
36  return (obj);
37 }
38 
39 GtkWidget *
40 get_wg (gchar *name)
41 {
42  GObject *obj;
43  obj = gtk_builder_get_object (gui, name);
44  if (obj == NULL)
45  g_warning ("Widget not found: %s", name);
46  return (GTK_WIDGET (obj));
47 }
48 
49 GtkWindow *
50 get_win (gchar *name)
51 {
52  GObject *obj;
53  obj = gtk_builder_get_object (gui, name);
54  if (obj == NULL)
55  g_warning ("Window not found: %s", name);
56  return (GTK_WINDOW (obj));
57 }
58 
59 /* Set the background color of a widget
60  */
61 void
62 set_wg_bg_color (GtkWidget * widget, gchar *bg_color)
63 {
64  static GtkCssProvider *cssp = NULL;
65  GtkStyleContext *sc;
66  gchar *css_bg_color;
67 
68  if (cssp == NULL)
69  cssp = gtk_css_provider_new ();
70 
71  css_bg_color = g_strdup_printf (
72  "textview text {background-color: %s;}\n"
73  ".background {background-color: %s;}"
74  , bg_color, bg_color);
75  sc = gtk_widget_get_style_context (widget);
76  gtk_style_context_remove_provider (sc, GTK_STYLE_PROVIDER (cssp));
77  gtk_css_provider_load_from_data (cssp, css_bg_color, -1, NULL);
78  if (!gtk_style_context_has_class (sc, "background"))
79  gtk_style_context_add_class (sc, "background");
80  gtk_style_context_add_provider (sc, GTK_STYLE_PROVIDER (cssp), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
81  g_free (css_bg_color);
82 }
83 
84 /* Set an image widget with the name of the file provided in the data dir
85  */
86 void
87 set_pixmap (gchar *widget, gchar *image)
88 {
89  gchar *tmp;
90  GtkImage *img;
91 
92  tmp = g_build_filename (main_path_data (), image, NULL);
93  img = GTK_IMAGE (get_wg (widget));
94  gtk_image_set_from_file (img, tmp);
95  g_free (tmp);
96 }
97 
98 /* Search for the user directory and create it if not found
99  */
100 void
102 {
103  GDir *dh;
104 
105  dh = g_dir_open (main_path_user (), 0, NULL);
106  if (dh == NULL)
107  {
108  g_message ("creating an empty user folder:\n %s", main_path_user ());
109  g_mkdir (main_path_user (), DIR_PERM);
110  dh = g_dir_open (main_path_user (), 0, NULL);
111  if (dh == NULL)
112  g_error ("could not creat a user folder, so we must quit!");
113  }
114  g_dir_close (dh);
115 }
116 
117 /* Compare two strings, so that it applies to other sorting functions.
118  */
119 gint
120 compare_string_function (gconstpointer a, gconstpointer b)
121 {
122  return (strcasecmp (a, b));
123 }
GObject * get_obj(gchar *name)
Definition: auxiliar.c:30
void set_wg_bg_color(GtkWidget *widget, gchar *bg_color)
Definition: auxiliar.c:62
void set_pixmap(gchar *widget, gchar *image)
Definition: auxiliar.c:87
GtkWidget * get_wg(gchar *name)
Definition: auxiliar.c:40
void assert_user_dir()
Definition: auxiliar.c:101
GtkBuilder * gui
Definition: main.c:39
gint compare_string_function(gconstpointer a, gconstpointer b)
Definition: auxiliar.c:120
GtkWindow * get_win(gchar *name)
Definition: auxiliar.c:50
gchar * name
Definition: keyboard.c:45
gchar * main_path_user()
Definition: main.c:61
gchar * main_path_data()
Definition: main.c:73
#define DIR_PERM
Definition: main.h:33