summaryrefslogtreecommitdiff
path: root/app/bin/smalldlg.c
blob: e4213a542f4927809035f913e18f014654e76e39 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/** \file smalldlg.c
 * Several simple and smaller dialogs. 
 *
 * $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/bin/smalldlg.c,v 1.6 2009-09-21 18:24:33 m_fischer Exp $
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *                2007 Martin Fischer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#ifndef WINDOWS
#include <unistd.h>
#include <dirent.h>
#endif
#ifdef WINDOWS
#include <io.h>
#include <windows.h>
#if _MSC_VER >1300
	#define strdup _strdup
#endif
#else
#include <sys/stat.h>
#endif

#include "wlib.h"
#include "common.h"
#include "draw.h"
#include "misc.h"
#include "custom.h"
#include "param.h"

#include "smalldlg.h"
#include "i18n.h"

wWin_p aboutW;
static wWin_p tipW;					/**< window handle for tip dialog */

static long showTipAtStart = 1;		/**< flag for visibility */

static dynArr_t tips_da;			/**< dynamic array for all tips */
#define tips(N) DYNARR_N( char *, tips_da, N )

static char * tipLabels[] = { N_("Show tips at start"), NULL };
static paramTextData_t tipTextData = { 40, 10 };

static paramData_t tipPLs[] = {
#define I_TIPTEXT		(1)
#define tipT			((wText_p)tipPLs[I_TIPTEXT].control)
	{   PD_MESSAGE, N_("Did you know..."), NULL, 0, NULL, NULL, BM_LARGE },
	{   PD_TEXT, NULL, "text", 0, &tipTextData, NULL, BO_READONLY|BT_CHARUNITS },
	{   PD_BUTTON, (void*)ShowTip, "prev", PDO_DLGRESETMARGIN, NULL, N_("Previous Tip"), 0L, (void *)(SHOWTIP_FORCESHOW | SHOWTIP_PREVTIP) },	
	{   PD_BUTTON, (void*)ShowTip, "next", PDO_DLGHORZ, NULL, N_("Next Tip"), 0L, (void *)(SHOWTIP_FORCESHOW | SHOWTIP_NEXTTIP) },
	{   PD_TOGGLE, &showTipAtStart, "showatstart", PDO_DLGCMDBUTTON, tipLabels, NULL, BC_NOBORDER }};

static paramGroup_t tipPG = { "tip", 0, tipPLs, sizeof tipPLs/sizeof tipPLs[0] };

/**
 * Create and initialize the tip of the day window. The dialog box is created and the list of tips is loaded
 * into memory. 
 */

static void CreateTipW( void )
{
	FILE * tipF;
	char buff[4096];
	char * cp;

	tipW = ParamCreateDialog( &tipPG, MakeWindowTitle(_("Tip of the Day")), _("Ok"), (paramActionOkProc)wHide, NULL, FALSE, NULL, F_CENTER, NULL );

	/* open the tip file */
	sprintf( buff, "%s%s%s.tip", libDir, FILE_SEP_CHAR, sProdNameLower );
	tipF = fopen( buff, "r" );
	
	/* if tip file could not be opened, the only tip is an error message for the situation */
	if (tipF == NULL) {
		DYNARR_APPEND( char *, tips_da, 1 );
		tips(0) = N_("No tips are available");
/*	TODO: enable buttons only if tips are available
		wControlActive( prev, FALSE );
		wControlActive( next, FALSE ); */
	} else {
		/* read all the tips from the file */
		while (fgets( buff, sizeof buff, tipF )) {

			/* lines starting with hash sign are ignored (comments) */
			if (buff[0] == '#')
				continue;
				
			/* remove CRs and LFs at end of line */				
			cp = buff+strlen(buff)-1;
			if (*cp=='\n') cp--;
			if (*cp=='\r') cp--;
			
			/* get next line if the line was empty */
			if (cp < buff)
				continue;

			cp[1] = 0;

			/* if line ended with a continuation sign, get the rest */
			while (*cp=='\\') {
				/* put LF at end */
				*cp++ = '\n';
				
				/* read a line */	
				if (!fgets( cp, (sizeof buff) - (cp-buff), tipF )) {
					return;
				}
	
				/* lines starting with hash sign are ignored (comments) */
				if (*cp=='#')
					continue;

				/* remove CRs and LFs at end of line */				
				cp += strlen(cp)-1;
				if (*cp=='\n') cp--;
				if (*cp=='\r') cp--;
				cp[1] = 0;
			}
			
			/* allocate memory for the tip and store pointer in dynamic array */
			DYNARR_APPEND( char *, tips_da, 10 );
			tips(tips_da.cnt-1) = strdup( buff );
		}
	}
}

/**
 * Show tip of the day. As far as necessary, the dialog is created. The index of
 * the last tip shown is retrieved from the preferences and the next tip is 
 * selected. At the end, the index of the shown tip is saved into the preferences.
 *
 * \param IN flags see definitions in smalldlg.h for possible values
 *
 */

void ShowTip( long flags )
{
	long tipNum;
	
	if (showTipAtStart || (flags & SHOWTIP_FORCESHOW))  
	{
		if (tipW == NULL) {
			CreateTipW();
		}
		ParamLoadControls( &tipPG );
		wTextClear( tipT );
		wPrefGetInteger( "misc", "tip-number", &tipNum, 0 );
		
		if( flags & SHOWTIP_PREVTIP ) {
			if(tipNum == 0 )
				tipNum = tips_da.cnt - 1;
			else
				tipNum--;	
		} else {
			if (tipNum >= tips_da.cnt - 1)
				tipNum = 0;
			else
				tipNum++;	
		}		

		wTextAppend( tipT, _(tips(tipNum)) );

		wPrefSetInteger( "misc", "tip-number", tipNum );
		wShow( tipW );
	}	
}

/*--------------------------------------------------------------------*/

#include "bitmaps/xtc.xpm"

static paramTextData_t aboutTextData = { 70, 10 };

#define DESCRIPTION N_("XTrackCAD is a CAD (computer-aided design) program for designing model railroad layouts.")
static paramData_t aboutPLs[] = {
#define I_ABOUTDRAW				(0)
	{   PD_BITMAP, NULL, "about", PDO_NOPSHUPD, NULL, NULL, 0 },
#define I_ABOUTVERSION			(1)
	{   PD_MESSAGE, NULL, NULL, PDO_DLGNEWCOLUMN, NULL, NULL, BM_LARGE },
#define I_COPYRIGHT				 (2)
#define COPYRIGHT_T			((wText_p)aboutPLs[I_COPYRIGHT].control)
	{   PD_TEXT, NULL, NULL, PDO_DLGRESIZE, &aboutTextData, NULL, BT_CHARUNITS }
};
static paramGroup_t aboutPG = { "about", 0, aboutPLs, sizeof aboutPLs/sizeof aboutPLs[0] };

/** 
 *	Create and show the About window.
 */

void CreateAboutW( void *ptr )
{
	char *copyright = sAboutProd;
	
	if( !aboutW ) {
		aboutPLs[I_ABOUTDRAW].winData = wIconCreatePixMap( xtc_xpm );
		ParamRegister( &aboutPG );
		aboutW = ParamCreateDialog( &aboutPG, MakeWindowTitle(_("About")), _("Ok"), (paramActionOkProc)wHide, NULL, FALSE, NULL, F_TOP|F_CENTER, NULL );
		ParamLoadMessage( &aboutPG, I_ABOUTVERSION, sAboutProd );
		wTextAppend( COPYRIGHT_T, DESCRIPTION );
		wTextAppend( COPYRIGHT_T, "\n\nXTrackCAD is Copyright 2003 by Sillub Technology and 2007 by Martin Fischer and Bob Blackwell." );
		wTextAppend( COPYRIGHT_T, "\n\nIcons by: Tango Desktop Project (http://tango.freedesktop.org)");
		wTextAppend( COPYRIGHT_T, "\n\nContributions by: Robert Heller, Mikko Nissinen, Timothy M. Shead, Daniel Luis Spagnol" );
		wTextAppend( COPYRIGHT_T, "\n\nParameter Files by: Ralph Boyd, Dwayne Ward" );
		wTextAppend( COPYRIGHT_T, "\n\nuthash Copyright notice:" );
		wTextAppend( COPYRIGHT_T, "\nCopyright (c) 2005-2015, Troy D. Hanson  http://troydhanson.github.com/uthash/");
		wTextAppend( COPYRIGHT_T, "\nAll rights reserved.");
	} 
		
	wShow( aboutW );
}	

/*--------------------------------------------------------------------*/

/**
 * Initialize the functions for small dialogs.
 */
 
void InitSmallDlg( void )
{
	ParamRegister( &tipPG );     
}