1 2 #include <Alert.h> 3 #include <Autolock.h> 4 #include <Path.h> 5 #include <MenuItem.h> 6 #include <CharacterSet.h> 7 #include <CharacterSetRoster.h> 8 #include <Screen.h> 9 #include <stdio.h> 10 11 #include <Constants.h> 12 #include <StyledEditApp.h> 13 #include <StyledEditWindow.h> 14 15 using namespace BPrivate; 16 17 BRect windowRect(7-15,26-15,507,426); 18 19 void cascade() { 20 BScreen screen(NULL); 21 BRect screenBorder = screen.Frame(); 22 float left = windowRect.left + 15; 23 if (left + windowRect.Width() > screenBorder.right) { 24 left = 7; 25 } 26 float top = windowRect.top + 15; 27 if (top + windowRect.Height() > screenBorder.bottom) { 28 top = 26; 29 } 30 windowRect.OffsetTo(BPoint(left,top)); 31 } 32 33 void uncascade() { 34 BScreen screen(NULL); 35 BRect screenBorder = screen.Frame(); 36 float left = windowRect.left - 15; 37 if (left < 7) { 38 left = screenBorder.right - windowRect.Width() - 7; 39 left = left - ((int)left % 15) + 7; 40 } 41 float top = windowRect.top - 15; 42 if (top < 26) { 43 top = screenBorder.bottom - windowRect.Height() - 26; 44 top = top - ((int)left % 15) + 26; 45 } 46 windowRect.OffsetTo(BPoint(left,top)); 47 } 48 49 StyledEditApp * styled_edit_app; 50 51 StyledEditApp::StyledEditApp() 52 : BApplication(APP_SIGNATURE) 53 { 54 fOpenPanel= new BFilePanel(); 55 BMenuBar * menuBar = 56 dynamic_cast<BMenuBar*>(fOpenPanel->Window()->FindView("MenuBar")); 57 58 fOpenAsEncoding = 0; 59 fOpenPanelEncodingMenu= new BMenu("Encoding"); 60 menuBar->AddItem(fOpenPanelEncodingMenu); 61 fOpenPanelEncodingMenu->SetRadioMode(true); 62 63 BCharacterSetRoster roster; 64 BCharacterSet charset; 65 while (roster.GetNextCharacterSet(&charset) == B_NO_ERROR) { 66 BString name(charset.GetPrintName()); 67 const char * mime = charset.GetMIMEName(); 68 if (mime) { 69 name.Append(" ("); 70 name.Append(mime); 71 name.Append(")"); 72 } 73 BMenuItem * item = new BMenuItem(name.String(),new BMessage(OPEN_AS_ENCODING)); 74 item->SetTarget(this); 75 fOpenPanelEncodingMenu->AddItem(item); 76 if (charset.GetFontID() == fOpenAsEncoding) { 77 item->SetMarked(true); 78 } 79 } 80 81 fWindowCount= 0; 82 fNext_Untitled_Window= 1; 83 styled_edit_app = this; 84 } /***StyledEditApp::StyledEditApp()***/ 85 86 void StyledEditApp::DispatchMessage(BMessage *msg, BHandler *handler) 87 { 88 if ( msg->what == B_ARGV_RECEIVED ) { 89 int32 argc; 90 if (msg->FindInt32("argc",&argc) != B_OK) { 91 argc=0; 92 } 93 const char ** argv = new (const char*)[argc]; 94 for (int arg = 0; (arg < argc) ; arg++) { 95 if (msg->FindString("argv",arg,&argv[arg]) != B_OK) { 96 argv[arg] = ""; 97 } 98 } 99 const char * cwd; 100 if (msg->FindString("cwd",&cwd) != B_OK) { 101 cwd = ""; 102 } 103 ArgvReceived(argc, argv, cwd); 104 } else { 105 BApplication::DispatchMessage(msg,handler); 106 } 107 } 108 109 110 void 111 StyledEditApp::MessageReceived(BMessage *message) 112 { 113 switch(message->what) { 114 case MENU_NEW: 115 OpenDocument(); 116 break; 117 case MENU_OPEN: 118 fOpenPanel->Show(); // 119 break; 120 case B_SILENT_RELAUNCH: 121 OpenDocument(); 122 break; 123 case OPEN_AS_ENCODING: 124 void * ptr; 125 if (message->FindPointer("source",&ptr) == B_OK) { 126 if (fOpenPanelEncodingMenu != 0) { 127 fOpenAsEncoding = (uint32)fOpenPanelEncodingMenu->IndexOf((BMenuItem*)ptr); 128 } 129 } 130 break; 131 default: 132 BApplication::MessageReceived(message); 133 break; 134 } 135 } 136 137 void 138 StyledEditApp::OpenDocument() 139 { 140 cascade(); 141 new StyledEditWindow(windowRect,fNext_Untitled_Window++,fOpenAsEncoding); 142 fWindowCount++; 143 } 144 145 void 146 StyledEditApp::OpenDocument(entry_ref * ref) 147 { 148 cascade(); 149 new StyledEditWindow(windowRect,ref,fOpenAsEncoding); 150 fWindowCount++; 151 } 152 153 void 154 StyledEditApp::CloseDocument() 155 { 156 uncascade(); 157 fWindowCount--; 158 if (fWindowCount == 0) { 159 BAutolock lock(this); 160 Quit(); 161 } 162 } 163 164 void 165 StyledEditApp::RefsReceived(BMessage *message) 166 { 167 int32 refNum; 168 entry_ref ref; 169 status_t err; 170 171 refNum = 0; 172 do { 173 err = message->FindRef("refs", refNum, &ref); 174 if (err != B_OK) 175 return; 176 OpenDocument(&ref); 177 refNum++; 178 } while (true); 179 } /***StyledEditApp::RefsReceived();***/ 180 181 void 182 StyledEditApp::ArgvReceived(int32 argc, const char *argv[], const char * cwd) 183 { 184 for (int i = 1 ; (i < argc) ; i++) { 185 BPath path; 186 if (argv[i][0] == '/') { 187 path.SetTo(argv[i]); 188 } else { 189 path.SetTo(cwd,argv[i]); 190 } 191 if (path.InitCheck() != B_OK) { 192 printf("path.InitCheck failed: \""); 193 if (argv[i][0] == '/') { 194 printf("%s",argv[i]); 195 } else { 196 printf("%s/%s",cwd,argv[i]); 197 } 198 printf("\".\n"); 199 continue; 200 } 201 202 entry_ref ref; 203 if (get_ref_for_path(path.Path(), &ref) != B_OK) { 204 printf("get_ref_for_path failed: \""); 205 printf("%s",path.Path()); 206 printf("\".\n"); 207 continue; 208 } 209 OpenDocument(&ref); 210 } 211 } 212 213 void 214 StyledEditApp::ReadyToRun() 215 { 216 if (fWindowCount == 0) { 217 OpenDocument(); 218 } 219 } 220 221 int32 222 StyledEditApp::NumberOfWindows() 223 { 224 225 return fWindowCount; 226 227 }/***StyledEditApp::NumberOfWindows()***/ 228 229 int 230 main() 231 { 232 StyledEditApp styledEdit; 233 styledEdit.Run(); 234 return 0; 235 } 236 237