PageRenderTime 414ms CodeModel.GetById 191ms app.highlight 12ms RepoModel.GetById 209ms app.codeStats 0ms

/apps/designer/select_file.c

http://ftk.googlecode.com/
C | 175 lines | 109 code | 37 blank | 29 comment | 7 complexity | 53384550be9f1b2a00af2edb9657473b MD5 | raw file
  1/*
  2 * File: save.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief: ui to save xul file.
  5 *
  6 * Copyright (c) 2009 - 2011  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2011-09-27 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "save.h"
 33#include "code_gen.h"
 34#include "ftk_util.h"
 35#include "ftk_file_system.h"
 36#include "ftk_file_browser.h"
 37
 38static char s_last_file[FTK_MAX_PATH+1] = {0};
 39
 40typedef struct _Info
 41{
 42	FtkWidget* widget_file_name;
 43	FtkListener on_select;
 44	void* on_select_ctx;
 45	char* init_path;
 46}Info;
 47
 48static Info* info_create(void)
 49{
 50	Info* info = FTK_NEW(Info);
 51	
 52	return info;
 53}
 54
 55static void info_destroy(void* data)
 56{
 57	Info* info = (Info*)data;
 58	if(info->init_path != NULL)
 59	{
 60		FTK_FREE(info->init_path);
 61	}
 62
 63	FTK_FREE(data);
 64	
 65	return;
 66}
 67
 68static Ret on_file_selected(void* ctx, int index, const char* path)
 69{
 70	FtkWidget* win = (FtkWidget*)ctx;
 71	char filename[FTK_MAX_PATH+1] = {0};
 72	Info* info = (Info*)ftk_widget_user_data(win);
 73	ftk_strncpy(filename, path, FTK_MAX_PATH);
 74
 75	ftk_normalize_path(filename);
 76	ftk_widget_set_text(info->widget_file_name, filename);
 77	
 78	return RET_OK;
 79}
 80
 81static Ret button_browse_clicked(void* ctx, void* obj)
 82{
 83
 84	FtkWidget* win = ftk_file_browser_create(FTK_FILE_BROWER_SINGLE_CHOOSER);
 85	ftk_file_browser_set_choosed_handler(win, on_file_selected, ctx);
 86	ftk_file_browser_set_path(win, s_last_file);
 87	ftk_file_browser_load(win);
 88
 89	return RET_OK;
 90}
 91
 92static Ret button_ok_clicked(void* ctx, void* obj)
 93{
 94	char filename[FTK_MAX_PATH+1] = {0};
 95	FtkWidget* win = (FtkWidget*)ctx;
 96	Info* info = (Info*)ftk_widget_user_data(win);
 97	ftk_strncpy(filename, ftk_widget_get_text(info->widget_file_name), FTK_MAX_PATH);
 98
 99	return_val_if_fail(strlen(filename) > 0, RET_FAIL);
100
101	if(info->on_select != NULL)
102	{
103		info->on_select(info->on_select_ctx, filename);
104		ftk_strncpy(s_last_file, filename, FTK_MAX_PATH);
105	}
106
107	ftk_widget_unref(win);
108
109	return RET_OK;
110}
111
112static Ret button_cancel_clicked(void* ctx, void* obj)
113{
114	FtkWidget* win = (FtkWidget*)ctx;
115
116	ftk_widget_unref(win);
117
118	return RET_OK;
119}
120
121Ret designer_select_file(const char* title, const char* init_path, FtkListener on_select, void* ctx)
122{
123	int width = 0;
124	int height = 0;
125	int y_offset = 0;
126	
127	Info* info = NULL;
128	FtkWidget* win = NULL;
129	FtkWidget* label = NULL;
130	FtkWidget* entry = NULL;	
131	FtkWidget* button = NULL;
132	
133	info = info_create();
134	return_val_if_fail(info != NULL, RET_FAIL);
135
136	win = ftk_app_window_create();
137	ftk_widget_set_user_data(win, info_destroy, info);
138	ftk_widget_set_text(win, title);
139	
140	width = ftk_widget_width(win);
141	height = ftk_widget_height(win);
142
143	info->on_select = on_select;
144	info->on_select_ctx = ctx;
145	info->init_path = ftk_strdup(init_path);
146
147	y_offset = height/3 - 30;
148	label = ftk_label_create(win, 0, y_offset, width/5, 30);
149	ftk_widget_set_text(label, _("FileName:"));
150	
151	y_offset = height/3;
152	info->widget_file_name = entry = ftk_entry_create(win, 5, y_offset, width*4/5-2, 30);
153	if(s_last_file[0] == '\0')
154	{
155		ftk_fs_get_cwd(s_last_file);
156	}
157	ftk_widget_set_text(entry, s_last_file);
158	
159	button = ftk_button_create(win, width*4/5, y_offset-5, width/5, 40);
160	ftk_widget_set_text(button, _("..."));
161	ftk_button_set_clicked_listener(button, button_browse_clicked, win);
162
163	button = ftk_button_create(win, width/5, height-60, width/5, 50);
164	ftk_widget_set_text(button, _("OK"));
165	ftk_button_set_clicked_listener(button, button_ok_clicked, win);
166
167	button = ftk_button_create(win, width*3/5, height-60, width/5, 50);
168	ftk_widget_set_text(button, _("Cancel"));
169	ftk_button_set_clicked_listener(button, button_cancel_clicked, win);
170	
171	ftk_widget_show_all(win, 1);
172
173	return RET_OK;
174}
175