Example #1
0
void __cdecl thread(void *args)
{
	int id;
	HANDLE *event_list=args;
	HANDLE event=0,event_idle=0;
	if(args==0)
		return;
	event=event_list[0];
	event_idle=event_list[1];
	if(event==0 || event_idle==0)
		return;
	printf("worker thread started\n");
	while(TRUE){
		stop_thread_menu(FALSE);
		SetEvent(event_idle);
		id=WaitForSingleObject(event,INFINITE);
		if(id==WAIT_OBJECT_0){
			stop_thread_menu(TRUE);
			strncpy(localinfo,taskinfo,sizeof(localinfo));
			printf("db=%s\n",localinfo);
			switch(task){
			case TASK_CLOSE_DB:
				{
					DB_TREE *db=0;
					if(find_db_tree(localinfo,&db)){
						intelli_del_db(db->name);
						mdi_remove_db(db);
						set_status_bar_text(ghstatusbar,0,"closed %s",localinfo);
					}
					else
						set_status_bar_text(ghstatusbar,0,"cant find %s",localinfo);
				}
				break;
			case TASK_OPEN_DB_AND_TABLE:
			case TASK_OPEN_DB:
				{
					DB_TREE *db=0;
					char *table;
					int result=FALSE;
					table=strstr(localinfo,";TABLE=");
					if(table!=0){
						table[0]=0;
						table++;
					}
					SetWindowText(ghstatusbar,"opening DB");
					if(!wait_for_treeview()){
						SetWindowText(ghstatusbar,"treeview error");
						break;
					}
					acquire_db_tree(localinfo,&db);
					if(!mdi_open_db(db)){
						char str[512];
						mdi_remove_db(db);
						_snprintf(str,sizeof(str),"Cant open %s",localinfo);
						str[sizeof(str)-1]=0;
						MessageBox(ghmainframe,str,"OPEN DB FAIL",MB_OK);
						SetWindowText(ghstatusbar,"error opening DB");
						set_focus_after_open(db);
					}
					else{
						intelli_add_db(db->name);
						set_focus_after_open(db);
						reassign_tables(db);
						if(task==TASK_OPEN_DB_AND_TABLE &&
							table!=0 && strncmp(table,"TABLE=",sizeof("TABLE=")-1)==0){
							table+=sizeof("TABLE=")-1;
							ResetEvent(event);
							task_open_table(db->name,table);
							continue;
						}
						else{
							load_tables_if_empty(db);
							if(keep_closed)
								close_db(db);
							result=TRUE;
						}
					}
					set_status_bar_text(ghstatusbar,0,"open DB %s %s",result?"done":"failed",keep_closed?"(closed DB)":"");
				}
				break;
			case TASK_GET_COL_INFO:
				{
					void *db=0;
					char table[80]={0};
					sscanf(localinfo,"DB=0x%08X;TABLE=%79[ -~]",&db,table);
					table[sizeof(table)-1]=0;
					if(db!=0){
						int result;
						set_status_bar_text(ghstatusbar,0,"getting col info for %s",table);
						result=get_col_info(db,table);
						if(keep_closed)
							close_db(db);
						if(result)
							set_status_bar_text(ghstatusbar,0,"col info done, %s",keep_closed?"(closed DB)":"");
						set_focus_after_open(db);
					}
				}
				break;
			case TASK_GET_INDEX_INFO:
				{
					void *db=0;
					char table[80]={0};
					sscanf(localinfo,"DB=0x%08X;TABLE=%79[ -~]",&db,table);
					table[sizeof(table)-1]=0;
					if(db!=0){
						int result;
						set_status_bar_text(ghstatusbar,0,"getting index info for %s",table);
						result=get_index_info(db,table);
						if(keep_closed)
							close_db(db);
						if(result)
							set_status_bar_text(ghstatusbar,0,"index info done, %s",keep_closed?"(closed DB)":"");
						set_focus_after_open(db);
					}
				}
				break;
			case TASK_GET_FOREIGN_KEYS:
				{
					void *db=0;
					char table[80]={0};
					sscanf(localinfo,"DB=0x%08X;TABLE=%79[ -~]",&db,table);
					table[sizeof(table)-1]=0;
					if(db!=0){
						int result;
						set_status_bar_text(ghstatusbar,0,"getting foreign keys for %s",table);
						result=get_foreign_keys(db,table);
						if(keep_closed)
							close_db(db);
						if(result)
							set_status_bar_text(ghstatusbar,0,"index info done, %s",keep_closed?"(closed DB)":"");
						set_focus_after_open(db);
					}
				}
				break;
			case TASK_UPDATE_ROW:
				{
					int result=FALSE;
					void *win=0;
					int row=0;
					sscanf(localinfo,"WIN=0x%08X;ROW=%i",&win,&row);
					if(win!=0){
						char *s=strstr(localinfo,"DATA=");
						if(s!=0){
							s+=sizeof("DATA=")-1;
							result=update_row(win,row,s);
							if(keep_closed){
								void *db=0;
								acquire_db_tree_from_win(win,&db);
								close_db(db);
							}
						}
					}
					set_status_bar_text(ghstatusbar,0,"update row %s %s",
						result?"done":"failed",
						keep_closed?"(closed DB)":"");
				}
				break;
			case TASK_DELETE_ROW:
				{
					int result=FALSE;
					void *win=0;
					int row=0;
					sscanf(localinfo,"WIN=0x%08X;ROW=%i",&win,&row);
					if(win!=0){
						result=delete_row(win,row);
						if(keep_closed){
							void *db=0;
							acquire_db_tree_from_win(win,&db);
							close_db(db);
						}
					}
					set_status_bar_text(ghstatusbar,0,"delete row %s",
						result?"done":"failed",
						(win!=0 && keep_closed)?"(closed DB)":"");
				}
				break;
			case TASK_INSERT_ROW:
				{
					int result=FALSE;
					void *win=0,*hlistview=0;
					sscanf(localinfo,"WIN=%08X,HLISTVIEW=0x%08X",&win,&hlistview);
					if(win!=0 && hlistview!=0){
						int msg=IDOK;
						result=insert_row(win,hlistview);
						if(!result)
							msg=IDCANCEL;
						PostMessage(GetParent(hlistview),WM_USER,msg,hlistview);
						if(keep_closed){
							void *db=0;
							acquire_db_tree_from_win(win,&db);
							close_db(db);
						}
					}
					set_status_bar_text(ghstatusbar,0,"inserting row %s %s",
						result?"done":"failed",
						(win!=0 && hlistview!=0 && keep_closed)?"(closed DB)":"");

				}
				break;
			case TASK_EXECUTE_QUERY:
				{
					void *win=0;
					int result=FALSE;
					win=strtoul(localinfo,0,0);
					if(win==0)
						mdi_get_current_win(&win);
					if(win!=0){
						char *s=0;
						int size=0;
						reopen_db(win);
						mdi_create_abort(win);
						mdi_get_edit_text(win,&s,&size);
						if(s!=0){
							sql_remove_comments(s,size);
							result=execute_sql(win,s,TRUE);
							free(s);
						}
						mdi_destroy_abort(win);
						if(keep_closed){
							void *db=0;
							acquire_db_tree_from_win(win,&db);
							close_db(db);
						}
						set_focus_after_result(win,result);
					}
					if(!result)
						set_status_bar_text(ghstatusbar,0,"execute sql failed %s",
							keep_closed?"(closed DB)":"");
				}
				break;
			case TASK_NEW_QUERY:
				{
					void *db=0;
					find_selected_tree(&db);
					if(db!=0){
						void *win=0;
						char *tname="new_query";
						if(acquire_table_window(&win,tname)){
							create_table_window(ghmdiclient,win);
							set_focus_after_result(win,FALSE);
							assign_db_to_table(db,win,tname);
						}
					}
				}
				break;
			case TASK_REFRESH_TABLES:
			case TASK_REFRESH_TABLES_ALL:
				{
					int result=FALSE;
					DB_TREE *db=0;
					SetWindowText(ghstatusbar,"refreshing tables");
					if(acquire_db_tree(localinfo,&db)){
						if(!mdi_open_db(db)){
							mdi_remove_db(db);
							SetWindowText(ghstatusbar,"error opening DB");
						}
						else{
							intelli_add_db(db->name);
							result=refresh_tables(db,task==TASK_REFRESH_TABLES_ALL);
							if(keep_closed)
								close_db(db);
						}
						if(result)
							set_status_bar_text(ghstatusbar,0,"refreshed tables %s",
								keep_closed?"(closed DB)":"");
					}
					else
						SetWindowText(ghstatusbar,"error refreshing cant acquire table");
				}
				break;
			case TASK_LIST_TABLES:
				{
					int result=FALSE;
					DB_TREE *db=0;
					SetWindowText(ghstatusbar,"listing tables");
					if(acquire_db_tree(localinfo,&db)){
						if(!mdi_open_db(db)){
							mdi_remove_db(db);
							SetWindowText(ghstatusbar,"error opening DB");
						}
						else{
							intelli_add_db(db->name);
							result=get_table_list(db);
							if(keep_closed)
								close_db(db);
						}
						if(result){
							set_status_bar_text(ghstatusbar,0,"done listing tables %s",
								keep_closed?"(closed DB)":"");
						}else{
							SetWindowText(ghstatusbar,"error listing tables");
						}
					}
					else
						SetWindowText(ghstatusbar,"error refreshing cant acquire tree");
				}
				break;
			case TASK_LIST_PROCS:
				{
					int result=FALSE;
					DB_TREE *db=0;
					SetWindowText(ghstatusbar,"listing stored procedures");
					if(acquire_db_tree(localinfo,&db)){
						if(!mdi_open_db(db)){
							mdi_remove_db(db);
							SetWindowText(ghstatusbar,"error opening DB");
						}
						else{
							intelli_add_db(db->name);
							result=get_proc_list(db);
							if(keep_closed)
								close_db(db);
						}
						if(result){
							set_status_bar_text(ghstatusbar,0,"done listing stored procs %s",
								keep_closed?"(closed DB)":"");
						}else{
							SetWindowText(ghstatusbar,"error listing stored procs");
						}
					}
					else
						SetWindowText(ghstatusbar,"error refreshing cant acquire tree");
				}
				break;
			case TASK_OPEN_TABLE:
				{
					int result=FALSE;
					char dbname[MAX_PATH*2]={0},table[80]={0},*p;
					p=strrchr(localinfo,';');
					if(p!=0){
						void *db=0;
						p[0]=0;
						strncpy(dbname,localinfo,sizeof(dbname));
						strncpy(table,p+1,sizeof(table));
						if(find_db_tree(dbname,&db)){
							void *win=0;
							if(acquire_table_window(&win,table)){
								char sql[256]={0};
								create_table_window(ghmdiclient,win);
								open_db(db);
								assign_db_to_table(db,win,table);
								if(strchr(table,' ')!=0)
									_snprintf(sql,sizeof(sql),"SELECT * FROM [%s]",table);
								else
									_snprintf(sql,sizeof(sql),"SELECT * FROM %s",table);
								mdi_set_edit_text(win,sql);
								mdi_create_abort(win);
								result=execute_sql(win,sql,TRUE);
								mdi_destroy_abort(win);
								load_tables_if_empty(db);
								set_focus_after_result(win,result);
								if(keep_closed)
									close_db(db);

							}
							else
								free_window(win);

						}
					}
					if(!result)
						set_status_bar_text(ghstatusbar,0,"open table:%s %s %s",
							table,"failed",keep_closed?"(closed DB)":"");
				}
				break;
			case TASK_INTELLISENSE_ADD_ALL:
				{
					void *win=0;
					int result=FALSE;
					win=strtoul(localinfo,0,0);
					if(win==0)
						mdi_get_current_win(&win);
					if(win!=0){
						reopen_db(win);
						mdi_create_abort(win);
						result=intellisense_add_all(win);
						mdi_destroy_abort(win);
						if(keep_closed){
							void *db=0;
							acquire_db_tree_from_win(win,&db);
							close_db(db);
						}
						set_focus_after_result(win,FALSE);
					}
					if(!result)
						set_status_bar_text(ghstatusbar,0,"intellisense add all failed %s",
							keep_closed?"(closed DB)":"");
				}
				break;
			default:
				break;
			}
		}
		ResetEvent(event);
	}
	CloseHandle(event);
	hworker=0;
}
Example #2
0
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	static HWND last_focus=0;
	if(FALSE)
	//if(msg!=WM_MOUSEFIRST&&msg!=WM_NCHITTEST&&msg!=WM_SETCURSOR&&msg!=WM_ENTERIDLE&&msg!=WM_NOTIFY)
	if(msg!=WM_NCHITTEST&&msg!=WM_SETCURSOR&&msg!=WM_ENTERIDLE&&msg!=WM_MOUSEMOVE&&msg!=WM_NCMOUSEMOVE)
	{
		static DWORD tick=0;
		if((GetTickCount()-tick)>500)
			printf("--\n");
		print_msg(msg,lparam,wparam,hwnd);
		tick=GetTickCount();
	}
    switch(msg)
    {
	case WM_MENUSELECT:
		break;
	case WM_CREATE:
		{
			RECT rect={0};
			extern int keep_closed,trim_trailing,left_justify;
			get_ini_value("SETTINGS","KEEP_CLOSED",&keep_closed);
			get_ini_value("SETTINGS","TRIM_TRAILING",&trim_trailing);
			get_ini_value("SETTINGS","LEFT_JUSTIFY",&left_justify);
			load_icon(hwnd);

			load_window_size(hwnd,"MAIN_WINDOW");

			GetClientRect(hwnd,&rect);
			get_ini_value("SETTINGS","TREE_WIDTH",&tree_width);
			if(tree_width>rect.right-10 || tree_width<10){
				tree_width=rect.right/4;
				if(tree_width<12)
					tree_width=12;
			}

			ghmdiclient=create_mdiclient(hwnd,ghmenu,ghinstance);
			ghdbview=create_dbview(hwnd,ghinstance);
			ghstatusbar=CreateStatusWindow(WS_CHILD|WS_VISIBLE,"ready",hwnd,IDC_STATUS);
			create_status_bar_parts(hwnd,ghstatusbar);

			resize_main_window(hwnd,tree_width);
		}
		break;
	case WM_DROPFILES:
		process_drop(hwnd,wparam);
		break;
	case WM_COPYDATA:
		if(lparam!=0){
			COPYDATASTRUCT *cd=lparam;
			process_cmd_line(cd->lpData);
		}
		break;
	case WM_USER:
		debug_window_focus(lparam,"WM_USER");
		switch(wparam){
		case IDC_TREEVIEW:
			if(lparam!=0)
				SetFocus(lparam);
			break;
		case IDC_MDI_LISTVIEW:
			if(lparam!=0){
				last_focus=lparam;
				SetFocus(lparam);
			}
			break;
		case IDC_LV_EDIT:
			if(lparam!=0)
				last_focus=lparam;
			break;
		}
		break;
	case WM_USER+1:
		debug_window_focus(last_focus,"WMUSER+1");
		if(last_focus!=0)
			SetFocus(last_focus);
		break;
	case WM_NCACTIVATE:
		debug_window_focus(last_focus,"NCACTIVATE wparam=%08X",wparam);
		if(wparam==0){
			last_focus=GetFocus();
		}
		else{
			PostMessage(hwnd,WM_USER+1,0,0);
		}
		break;
	case WM_ACTIVATEAPP: //close any tooltip on app switch
		debug_window_focus(last_focus,"ACTIVATEAPP wparam=%08X",wparam);
		if(wparam){
			PostMessage(hwnd,WM_USER+1,0,0);
		}
		break;
	case WM_KILLFOCUS:
	case WM_RBUTTONDOWN:
	case WM_LBUTTONUP:
		if(main_drag){
			ReleaseCapture();
			main_drag=FALSE;
			write_ini_value("SETTINGS","TREE_WIDTH",tree_width);
		}
		break;
	case WM_LBUTTONDOWN:
		{
			int x=LOWORD(lparam);
			if(x>=(tree_width-10) && x<(tree_width+10)){
				SetCapture(hwnd);
				SetCursor(LoadCursor(NULL,IDC_SIZEWE));
				main_drag=TRUE;
			}
		}
		break;
	case WM_MOUSEFIRST:
		{
			int x=LOWORD(lparam);
			if(x>=(tree_width-10) && x<(tree_width+10))
				SetCursor(LoadCursor(NULL,IDC_SIZEWE));
			if(main_drag){
				RECT rect;
				GetClientRect(ghmainframe,&rect);
				if(x>10 && x<rect.right-10){
					tree_width=x;
					resize_main_window(hwnd,tree_width);
				}
			}
		}
		break;
	case WM_COMMAND:
		switch(LOWORD(wparam)){
		case IDM_OPEN:
			if((GetKeyState(VK_SHIFT)&0x8000) || GetKeyState(VK_CONTROL)&0x8000)
				task_open_db("");
			else{
				if(does_key_exist("SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources","Journal"))
					task_open_db("UID=dba;PWD=sql;DSN=Journal");
				else
					task_open_db("");
			}
			break;
		case IDM_CLOSE:
			{
			HANDLE hroot=0;
			if(tree_find_focused_root(&hroot)){
				char str[MAX_PATH]={0};
				tree_get_db_table(hroot,str,sizeof(str),0,0,0);
				if(str[0]!=0){
					set_status_bar_text(ghstatusbar,0,"closing %s",str);
					task_close_db(str);
				}
			}
			else
				set_status_bar_text(ghstatusbar,0,"select a DB");
			}
			break;
		case IDM_SETTINGS:
			DialogBox(ghinstance,MAKEINTRESOURCE(IDD_SETTINGS),hwnd,settings_proc);
			break;
		case IDM_RECENT:
			DialogBox(ghinstance,MAKEINTRESOURCE(IDD_RECENT),hwnd,recent_proc);
			break;
		case IDM_STOP_THREAD:
			{
				int click=MessageBox(hwnd,"Are you sure you want to terminate the task?","Warning",MB_OKCANCEL|MB_SYSTEMMODAL);
				if(click==IDOK){
					terminate_worker_thread();
					stop_thread_menu(FALSE);
				}
			}
			break;
		case IDM_QUERY:
			task_new_query();
			break;
		case IDC_EXECUTE_SQL:
			task_execute_query(NULL);
			break;
		case IDM_TILE_DIALOG:
			tile_select_dialog(hwnd);
			break;
		case IDM_WINDOW_TILE:
			mdi_tile_windows_vert();
			break;
		case IDM_WINDOW_CASCADE:
			mdi_cascade_win_vert();
			break;
		case IDM_WINDOW_LRTILE:
			mdi_tile_windows_horo();
			break;
		case IDM_REFRESH_ALL:
			refresh_all_dialog(hwnd);
			break;
		case IDM_REORDER:
			reorder_win_dialog(hwnd);
			break;
		}
		break;
	case WM_SIZE:
		resize_main_window(hwnd,tree_width);
		create_status_bar_parts(ghmainframe,ghstatusbar);
		return 0;
		break;
	case WM_QUERYENDSESSION:
		return 1; //ok to end session
		break;
	case WM_ENDSESSION:
		if(wparam){
			if(!(GetKeyState(VK_SHIFT)&0x8000))
				save_window_size(hwnd,"MAIN_WINDOW");
		}
		return 0;
	case WM_CLOSE:
        break;
	case WM_DESTROY:
		if(!(GetKeyState(VK_SHIFT)&0x8000))
			save_window_size(hwnd,"MAIN_WINDOW");
		PostQuitMessage(0);
        break;
    }
	return DefFrameProc(hwnd, ghmdiclient, msg, wparam, lparam);
}