File: layoutconfig.c

package info (click to toggle)
gtoaster 0.19991130-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,700 kB
  • ctags: 1,318
  • sloc: ansic: 12,739; sh: 353; makefile: 217; sed: 93
file content (226 lines) | stat: -rw-r--r-- 5,646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* remember layout configuration of gnometoaster's windows,esp.
 * the size of the main window */

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "layoutconfig.h"
#include "configfile.h"
#include "varman.h"
#include "preferences.h"

/* uncomment for debugging */
/* #define DEBUG */

#define LAYOUTCONFIG_CONFIGSECTION "[Layout]"

typedef struct 
  {
     GtkWidget *w;
     char name[255];
     char value[255];
  }
layoutconfig_widgetentry;

GList *layoutconfig_widgets=NULL;

/* temporary buffer for the following function */
char layoutconfig_getvalue_copy[255];
/* return a specific definition within a widget entry */
char *layoutconfig_getstring(char *settings,char *name)
{
   char *result;
   #ifdef DEBUG
   printf ("layoutconfig_getstring: scanning %s for %s\n",
	   settings,name);
   #endif
   strcpy((char*)&layoutconfig_getvalue_copy,settings);
   result=strstr((char*)&layoutconfig_getvalue_copy,name);
   if ((result!=NULL)&&(*(result+strlen(name))=='='))
     {	
	/* position after '=' */
	result+=(strlen(name)+1);
	/* terminate with 0 */
	if (strchr(result,',')!=NULL)
	  *strchr(result,',')=0;
     };
   #ifdef DEBUG
   printf ("layoutconfig_getstring: result=%s\n",result);
   #endif
   return result;
};
int layoutconfig_getvalue(char *settings,char *name)
{
   char *string;
   int result;
   string=layoutconfig_getstring(settings,name);
   if (string!=NULL)
     {	
	sscanf(string,"%d",&result);
     }
   else
     result=0;
   return result;
};

/* update a widget with the values stored in value */
void layoutconfig_updatewidget(GtkWidget *w,char *value)
{
   if (w!=NULL)
     {
	if (w->window!=NULL)
	  {	     
	     /* go lowlevel here ?!? */
	     gdk_window_move_resize(w->window,
				    layoutconfig_getvalue(value,"xpos"),
				    layoutconfig_getvalue(value,"ypos"),
				    layoutconfig_getvalue(value,"width"),
				    layoutconfig_getvalue(value,"height"));
	  };
     };
}; 

/* store informations about widget w in value */
void layoutconfig_getwidgetinfo(GtkWidget *w,char *value)
{
   int x,y,width,height;
   if (w!=NULL)
     {	
	gdk_window_get_position(w->window,&x,&y);	
	gdk_window_get_size(w->window,&width,&height);
	strcpy(value,"");
	sprintf(value,"width=%d,height=%d,xpos=%d,ypos=%d",
		width,height,x,y);
     };
};
   

layoutconfig_widgetentry *layoutconfig_findentry(gchar *name)
{
   GList *current;
   layoutconfig_widgetentry *result=NULL;
   
   for (current=layoutconfig_widgets;
	current!=NULL;
	current=current->next)
     {
	if (!strcasecmp((char*)&((layoutconfig_widgetentry*)current->data)->name,	
			name))
	  result=(layoutconfig_widgetentry*)current->data;
     };
   return result;
};

void layoutconfig_loaddatabase(gchar *filename)
{
   FILE *f;
   layoutconfig_widgetentry *e;
   char name[255];
   char value[255];
   
   f=fopen(filename,"r");
      
   /* if config file exists */
   if (f!=NULL)
     {	
	/* if layout definitions exist */   
	if (configfile_seeksection(f,LAYOUTCONFIG_CONFIGSECTION))
	  {
	     do
	       {
		  configfile_getnextentry(f,(char*)&name,(char*)&value);
		  if (strlen((char*)&name)>0)
		    {		  
		       e=layoutconfig_findentry((char*)&name);
		       if (e==NULL)
			 {		  
			    e=(layoutconfig_widgetentry*)malloc(sizeof(layoutconfig_widgetentry));
			    layoutconfig_widgets=g_list_append(layoutconfig_widgets,
							       (gpointer)e);
			    strcpy((char*)&e->name,(char*)&name);
			    strcpy((char*)&e->value,(char*)&value);
			    e->w=NULL;
			 };
		       /* update widget properties *
			* in case there's already a widget registered with this name */
		       layoutconfig_updatewidget(e->w,(char*)&value);
		    };
	       }
	     while (strlen((char*)&name)>0);
	  };
	/* close config file only if it could be opened successfully */
	fclose(f);
     };
};
 
/* save callback. gets called whenever the config file is saved 
 * updates all widget settings and writes them into the config file */
void layoutconfig_save(FILE *f,gpointer data)
{
   GList *current;
   layoutconfig_widgetentry *e;
   
   for (current=layoutconfig_widgets;
	current!=NULL;
	current=current->next)
     {
	e=(layoutconfig_widgetentry*)current->data;
	layoutconfig_getwidgetinfo(e->w,(char*)&e->value);
	fprintf(f,"%s=%s\n",(char*)&e->name,(char*)&e->value);	
     };
};
   
  
/* initialize layoutconfig,register configfile entry */
void layoutconfig_init()
{
   
   layoutconfig_loaddatabase(varman_replacevars_copy(dynamic_defs,
						     "$HOME/.gtoasterrc"));
   configfile_registersection(LAYOUTCONFIG_CONFIGSECTION,
			      layoutconfig_save,
			      NULL);
};

/* use this instead of gtk_widget_show() for widgets that should remember
 * their layout */
void layoutconfig_widget_show(GtkWidget *w,
			      gchar *name)
{
   layoutconfig_widgetentry *e;

   gtk_widget_show(w);
   e=layoutconfig_findentry(name);
   if (e==NULL)
     {		  
	e=(layoutconfig_widgetentry*)malloc(sizeof(layoutconfig_widgetentry));
	layoutconfig_widgets=g_list_append(layoutconfig_widgets,
					   (gpointer)e);
	strcpy((char*)&e->name,name);
	layoutconfig_getwidgetinfo(w,(char*)&e->value);
     }
   else
     layoutconfig_updatewidget(w,(char*)&e->value);
   e->w=w;
};

/* use this instead of gtk_widget_hide() for widgets that should remember
 * their layout */
void layoutconfig_widget_hide(GtkWidget *w,
			      gchar *name)
{
   layoutconfig_widgetentry *e;

   e=layoutconfig_findentry(name);
   /* store widget's layout informations if corresponding entry was found */
   if (e!=NULL)
     {	
	layoutconfig_getwidgetinfo(w,(char*)&e->value);
	e->w=NULL;
     };
   gtk_widget_hide(w);
};