📄 ds_view.cpp
字号:
#include "ds_head.h"
#include "ds_array.h"
#include "ds_view.h"
static BOOL DSV_SetViewFilter( HVIEW hView, FILTERPROC filterproc );
static void DSV_UpdateOtherViews( HVIEW hView, OPER_EVENT* oper_event );
static UINT DSV_GetField( HVIEW hView, UINT view_field );
extern BOOL _DST_AddRecord( HTABLE hTable );
extern BOOL _DST_DeleteRecord( HTABLE hTable, LPOSITION pos );
extern BOOL _DST_SetFieldValue( HTABLE hTable, LPOSITION record_pos, UINT field_index, TB_VARIANT field_value );
extern BOOL _DST_GetFieldValue( HTABLE hTable, LPOSITION record_pos, UINT field_index, TB_VARIANT* field_value );
extern BOOL DST_AttachView( HTABLE hTable, HVIEW hView );
HVIEW DSV_CreateView( HTABLE hTable, const char* view_name, UINT field_num )
{
DS_VIEW* pView;
if( !DST_IsTable( hTable ) )
{
return NULL;
}
if( view_name == NULL || strlen(view_name) > VIEW_NAME_MAXLEN )
{
return NULL;
}
if( field_num <= 0 )
{
return NULL;
}
pView = (DS_VIEW*)malloc( sizeof(DS_VIEW) );
if( pView == NULL )
{
return NULL;
}
//create field
if( !DSA_CreateArray( &pView->field_info, field_num, sizeof(UINT) ) )
{
free( pView );
return NULL;
}
//initial record list
DSL_InitList( &pView->record_list, 0 );
pView->field_num = field_num;
pView->record_num = 0;
strcpy( pView->view_name, view_name );
pView->hOwner_table = hTable;
DST_AttachView( hTable, pView );
return pView;
}
BOOL DSV_DestroyView( HVIEW hView )
{
LPOSITION curpos, nextpos;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( hView->field_info.head )
{
free( hView->field_info.head );
hView->field_info.head = NULL;
}
hView->field_info.count = 0;
hView->field_info.size = 0;
curpos = hView->record_list.head;
while( curpos )
{
nextpos = DSL_NextPos( curpos );
if( !DSL_RemoveAt( &hView->record_list, curpos ) )
{
; //print erro
}
curpos = nextpos;
}
free( hView );
return TRUE;
}
BOOL DSV_GetFieldInfo( HVIEW hView, UINT field_index, TB_FIELD* fieldinfo )
{
UINT table_fieldindex;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( field_index >= hView->field_num )
return FALSE;
if( fieldinfo == NULL )
return FALSE;
table_fieldindex = DSV_GetField( hView, field_index );
return DST_GetFieldInfo( hView->hOwner_table, table_fieldindex, fieldinfo );
}
BOOL DSV_AddRecord( HVIEW hView )
{
OPER_EVENT oper_event;
LPOSITION table_pos;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( !_DST_AddRecord( hView->hOwner_table ) )
{
return FALSE;
}
table_pos = hView->hOwner_table->current_position;
if( !DSV_AddViewRecord( hView, table_pos ) )
{
return FALSE;
}
oper_event.oper_type = OPER_ADD;
oper_event.oper_record = table_pos;
oper_event.oper_field = -1;
DSV_UpdateOtherViews( hView, &oper_event );
return TRUE;
}
static BOOL DSV_AddViewRecord( HVIEW hView, LPOSITION table_pos )
{
hView->current_position = DSL_AddTail( &hView->record_list, table_pos );
if( hView->filter_proc != NULL )
{
if( hView->filter_proc( hView->current_position ) <= 0 )
{
DSL_RemoveTail( &hView->record_list );
return FALSE;
}
}
if( hView->compare_proc )
{
DSV_MoveFirst( hView );
while( !DSV_IsEOF( hView ) )
{
if( hView->compare_proc( table_pos ) < 0 )
{
DSL_RemoveTail( &hView->record_list );
DSL_InsertBefore( &hView->record_list, hView->current_position, table_pos );
break;
}
DSV_MoveNext( hView );
}
}
hView->record_num = hView->record_list.count;
return TRUE;
}
BOOL DSV_DeleteCurRecord( HVIEW hView )
{
if( hView == NULL )
{
return FALSE;
}
return DSV_DeleteRecord( hView, hView->current_position );
}
BOOL DSV_DeleteCurViewRecord( HVIEW hView )
{
if( hView == NULL )
{
return FALSE;
}
return DSV_DeleteViewRecord( hView, hView->current_position );
}
BOOL DSV_DeleteRecord( HVIEW hView, LPOSITION pos )
{
LPOSITION new_curpos;
LPOSITION table_pos;
OPER_EVENT oper_event;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( pos == NULL )
return FALSE;
new_curpos = pos->next;
if( new_curpos == NULL )
{
new_curpos = hView->record_list.head;
}
table_pos = (LPOSITION)pos->data;
if( !_DST_DeleteRecord( hView->hOwner_table, table_pos) )
{
return FALSE;
}
if( !DSV_DeleteViewRecord( hView, pos ) )
{
return FALSE;
}
if( hView->current_position == pos )
{
hView->current_position = new_curpos;
}
oper_event.oper_type = OPER_DELETE;
oper_event.oper_record = table_pos;
oper_event.oper_field = -1;
DSV_UpdateOtherViews( hView, &oper_event );
return TRUE;
}
static BOOL DSV_DeleteViewRecord( HVIEW hView, LPOSITION pos )
{
LPOSITION new_curpos;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( pos == NULL )
return FALSE;
new_curpos = pos->next;
if( new_curpos == NULL )
{
new_curpos = hView->record_list.head;
}
if( !DSL_RemoveAt( &hView->record_list, pos ) )
return FALSE;
if( hView->current_position == pos )
{
hView->current_position = new_curpos;
}
hView->record_num = hView->record_list.count;
return TRUE;
}
BOOL DSV_SetFieldValue( HVIEW hView, UINT field_index, TB_VARIANT field_value )
{
UINT table_fieldindex;
OPER_EVENT oper_event;
LPOSITION table_pos;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( field_index >= hView->field_num )
return FALSE;
table_pos = (LPOSITION)hView->current_position->data;
table_fieldindex = DSV_GetField( hView, field_index );
if( !_DST_SetFieldValue( hView->hOwner_table, table_pos, table_fieldindex, field_value ) )
{
return FALSE;
}
oper_event.oper_type = OPER_MODIFY;
oper_event.oper_record = table_pos;
oper_event.oper_field = -1;
DSV_UpdateOtherViews( hView, &oper_event );
return TRUE;
}
BOOL DSV_GetFieldValue( HVIEW hView, UINT field_index, TB_VARIANT* field_value )
{
LPOSITION table_record;
UINT table_fieldindex;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( field_index >= hView->field_num )
return FALSE;
table_record = (LPOSITION)hView->current_position->data;
table_fieldindex = DSV_GetField( hView, field_index );
return _DST_GetFieldValue( hView->hOwner_table, table_record, table_fieldindex, field_value );
}
void DSV_MoveNext( HVIEW hView )
{
hView->current_position = hView->current_position->next;
}
void DSV_MovePrev( HVIEW hView )
{
hView->current_position = hView->current_position->prev;
}
void DSV_MoveFirst( HVIEW hView )
{
hView->current_position = hView->record_list.head;
}
void DSV_MoveLast( HVIEW hView )
{
hView->current_position = hView->record_list.tail;
}
int DSV_GetRecordCount( HVIEW hView )
{
if( !DSV_IsView( hView ) )
{
return -1;
}
return hView->record_num;
}
BOOL DSV_IsView( HVIEW hView )
{
HTABLE hTable;
LPOSITION pos;
TB_VIEW *tb_view;
if( hView == NULL )
{
return FALSE;
}
hTable = hView->hOwner_table;
if( !DST_IsTable(hTable) )
{
return FALSE;
}
pos = hTable->self_views.head;
while( pos )
{
tb_view = (TB_VIEW*)pos->data;
if( hView == tb_view->view_handle )
{
return TRUE;
}
pos = DSL_NextPos( pos );
}
return FALSE;
}
BOOL DSV_AttachField( HVIEW hView, UINT view_field, UINT table_field )
{
if( !DSV_IsView( hView ) )
{
return FALSE;
}
if( view_field >= hView->field_num )
{
return FALSE;
}
if( table_field >= hView->hOwner_table->field_num )
{
return FALSE;
}
return DSA_SetAt( &hView->field_info, view_field, &table_field );
}
void DSV_RefreshRecord( HVIEW hView, LPOSITION pos )
{
if( !DSV_IsView( hView ) )
{
return;
}
if( hView->filter_proc != NULL )
{
if( hView->filter_proc( hView->current_position ) <= 0 )
{
DSL_RemoveTail( &hView->record_list );
}
}
}
void DSV_RefreshView( HVIEW hView )
{
LPOSITION curpos;
HTABLE hTable;
if( !DSV_IsView( hView ) )
{
return;
}
DSV_Clear( hView );
hTable = hView->hOwner_table;
curpos = hTable->record_list.head;
while( curpos )
{
DSV_AddViewRecord( hView, curpos );
curpos = DSL_NextPos( curpos );
}
}
BOOL DSV_SetParam( HVIEW hView, DSV_FILTERPROC filterproc, DSV_COMPAREPROC compareproc, DSV_UPDATEPROC updateproc )
{
if( !DSV_IsView( hView ) )
{
return FALSE;
}
hView->filter_proc = filterproc;
hView->compare_proc = compareproc;
hView->update_proc = updateproc;
return TRUE;
}
static BOOL DSV_SetViewFilter( HVIEW hView, FILTERPROC filterproc )
{
LPOSITION curpos;
TB_VIEW* tb_view;
if( !DSV_IsView( hView ) )
{
return FALSE;
}
curpos = hView->hOwner_table->self_views.head;
while( curpos )
{
tb_view = (TB_VIEW*)curpos->data;
if( tb_view->view_handle == hView )
{
tb_view->filter_proc = filterproc;
return TRUE;
}
curpos = DSL_NextPos( curpos );
}
return FALSE;
}
void DSV_Clear( HVIEW hView )
{
if( !DSV_IsView( hView ) )
{
return;
}
DSL_ClearList( &hView->record_list );
}
//由视图域位置获得表的域位置
static UINT DSV_GetField( HVIEW hView, UINT view_field )
{
return *(UINT*)DSA_GetAt( &hView->field_info, view_field );
}
//由表中记录位置获得视图的记录位置
static LPOSITION DSV_GetRecord( HVIEW hView, LPOSITION tb_pos )
{
LPOSITION curpos;
curpos = hView->record_list.head;
while( curpos )
{
if( curpos->data == tb_pos )
{
return curpos;
}
curpos = DSL_NextPos( curpos );
}
return NULL;
}
void DSV_Update( HVIEW hView, OPER_EVENT* oper_event )
{
LPOSITION view_pos;
BOOL bResult = TRUE;
switch( oper_event->oper_type )
{
case OPER_ADD:
{
bResult = DSV_AddViewRecord( hView, oper_event->oper_record );
}
break;
case OPER_DELETE:
{
view_pos = DSV_GetRecord( hView, oper_event->oper_record );
if( view_pos == NULL )
{
return;
}
bResult = DSV_DeleteViewRecord( hView, view_pos );
}
break;
case OPER_MODIFY:
{
view_pos = DSV_GetRecord( hView, oper_event->oper_record );
if( view_pos == NULL )
{
return;
}
bResult = TRUE;
}
break;
}
if( bResult )
{
if( hView->update_proc )
{
hView->update_proc( oper_event->oper_type, oper_event->oper_record, oper_event->oper_field );
}
}
}
BOOL DSV_IsBOF( HVIEW hView )
{
return FALSE;
}
BOOL DSV_IsEOF( HVIEW hView )
{
if( hView == NULL )
return FALSE;
return hView->current_position?FALSE:TRUE;
}
void DSV_SetAbsolutePostion( HVIEW hView, LPOSITION pos )
{
hView->current_position = pos;
}
static void DSV_UpdateOtherViews( HVIEW hView, OPER_EVENT* oper_event )
{
LPOSITION view_pos;
TB_VIEW * tb_view;
HTABLE hTable;
hTable = hView->hOwner_table;
view_pos = hTable->self_views.head;
while( view_pos )
{
tb_view = (TB_VIEW*)view_pos->data;
if( tb_view->view_handle != hView )
{
DSV_Update( (DS_VIEW*)tb_view->view_handle, oper_event );
}
view_pos = DSL_NextPos( view_pos );
}
}
INT DSV_GetFieldCount( HVIEW hView )
{
if( !DSV_IsView( hView ) )
{
return -1;
}
return hView->field_num;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -