Ignore:
Timestamp:
Jan 14, 2007, 8:02:42 PM (19 years ago)
Author:
RBRi
Message:

another refactoring step for cleaning up the command line handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/NewView/StartupUnit.pas

    r58 r61  
    1313  OS2Def,
    1414  Classes,
    15   ACLString,
    1615  GlobalFilelistUnit,
    1716  SharedMemoryUnit,
     
    2423function AccessSharedMemory: TSuballocatedSharedMemory;
    2524
    26 // Returns true if the program should be started as normal.
    27 // False if it should immediately exit.
    28 function Startup: boolean;
    29 
    3025// Look for any items that are actually specifiying environment
    3126// variables, and expand them to the contents of the variables
     
    3732// the help and bookshelf paths.
    3833Function FindHelpFile( FileName: string ): string;
     34Procedure PostNewViewTextMessage( Window: HWND;
     35                                  MessageType: ULONG;
     36                                  s: string );
    3937
    4038var
    41   CmdLineParameters: TCmdLineParameters;
    4239  SharedMemory: TSubAllocatedSharedMemory;
    4340  GlobalFilelist: TGlobalFilelist;
     
    5047  DebugUnit,
    5148  PMWin,
    52   ACLUtility,
    5349  ACLStringUtility,
    5450  ACLFileUtility,
    55   AStringUtilityUnit,
    5651  HelpManagerUnit;
    5752
     
    167162
    168163
    169 // If another instance already has the files open
    170 // activate it and return true.
    171 function FindExistingWindow: HWND;
    172 var
    173   FileItems: TStringList;
    174   Filenames: TStringList;
    175   FullFilePath: string;
    176   i: longint;
    177 
    178   FileWindow: HWND;
    179 begin
    180   Result := NULLHANDLE;
    181 
    182   if length(CmdLineParameters.getInterpretedFileNames) = 0 then
    183     // not loading files; nothing to check
    184     exit;
    185 
    186   FileItems := TStringList.Create;
    187   Filenames := TStringList.Create;
    188 
    189   StringToList(CmdLineParameters.getInterpretedFileNames, FileItems, '+' );
    190   TranslateIPFEnvironmentVars( FileItems, FileNames );
    191 
    192   for i := 0 to FileNames.Count - 1 do
    193   begin
    194     FullFilePath := FindHelpFile( Filenames[ i ] );
    195     if FullFilePath <> '' then
    196     begin
    197       FileWindow := GlobalFilelist.FindFile( FullFilePath );
    198 
    199       if FileWindow = NULLHANDLE then
    200       begin
    201         // not found - stop searching.
    202         Result := NULLHANDLE; // no match
    203         break;
    204       end;
    205 
    206       // found it
    207 
    208       // is it the same as any previous match?
    209       if Result <> NULLHANDLE then
    210       begin
    211         if FileWindow <> Result then
    212         begin
    213           // no, so we don't have a match.
    214           // NOTE: We just excluded something: if the same file is
    215           // open in multiple windows then we may not check all combinations
    216           Result := NULLHANDLE; // no match
    217           break;
    218         end;
    219       end
    220       else
    221       begin
    222         // no match yet - store this one
    223         Result := FileWindow;
    224       end;
    225 
    226     end;
    227   end;
    228 
    229   Filenames.Destroy;
    230   FileItems.Destroy;
    231 
    232 end;
    233 
    234164function AccessSharedMemory: TSuballocatedSharedMemory;
    235165begin
     
    253183end;
    254184
    255 function Startup: boolean;
    256 var
    257   tmpCmdLine: String;
    258   ExistingWindow: HWND;
    259 begin
    260   // open shared memory
    261   SharedMemory := AccessSharedMemory;
    262 
    263   // get access to the system-global filelist
    264   GlobalFilelist := TGlobalFilelist.Create;
    265 
    266   // parse parameters into Parameters object
    267   tmpCmdLine := nativeOS2GetCmdLineParameter;
    268 
    269   CmdLineParameters := TCmdLineParameters.Create;
    270   CmdLineParameters.parseCmdLine(tmpCmdLine);
    271 
    272   ExistingWindow := FindExistingWindow;
    273 
    274   if ExistingWindow <> NULLHANDLE then
    275   begin
    276     // want to exit without running fully
    277     Result := false;
    278 
    279     // destroy global list - nobody else will
    280     GlobalFilelist.Destroy;
    281 
    282     WinSetFocus( HWND_DESKTOP, ExistingWindow );
    283 
    284     // topic search
    285     if not CmdLineParameters.getSearchFlag
    286        AND not CmdLineParameters.getGlobalSearchFlag
    287        AND (CmdLineParameters.getInterpretedSearchText <> '')
    288     then
    289     begin
    290       PostNewViewTextMessage( ExistingWindow,
    291                               NHM_SEARCH,
    292                               CmdLineParameters.getInterpretedSearchText);
    293     end;
    294 
    295     if CmdLineParameters.getGlobalSearchFlag then
    296     begin
    297       PostNewViewTextMessage( ExistingWindow,
    298                               NHM_GLOBAL_SEARCH,
    299                               CmdLineParameters.getInterpretedSearchText);
    300     end;
    301 
    302     if CmdLineParameters.getShowUsageFlag then
    303     begin
    304       WinPostMsg( ExistingWindow,
    305                NHM_SHOW_USAGE,
    306                0,
    307                0 );
    308     end;
    309 
    310     if CmdLineParameters.getHelpManagerFlag then
    311     begin
    312       // tell the new help manager instance to talk to the
    313       // other viewer
    314       WinPostMsg( CmdLineParameters.getHelpManagerWindow,
    315                NHM_VIEWER_READY,
    316                ExistingWindow,
    317                0 );
    318     end;
    319 
    320   end
    321   else
    322   begin
    323     // run as normal
    324     Result := true;
    325   end;
    326 end;
    327185
    328186Initialization
Note: See TracChangeset for help on using the changeset viewer.